-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathuse_pretrained_weights_resnet.py
183 lines (140 loc) · 4.73 KB
/
use_pretrained_weights_resnet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# https://deeplearningcourses.com/c/advanced-computer-vision
# https://www.udemy.com/advanced-computer-vision
from __future__ import print_function, division
from builtins import range, input
# Note: you may need to update your version of future
# sudo pip install -U future
from keras.layers import Input, Lambda, Dense, Flatten
from keras.models import Model
from keras.applications.resnet import ResNet50, preprocess_input
# from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from sklearn.metrics import confusion_matrix
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
# re-size all the images to this
IMAGE_SIZE = [100, 100] # feel free to change depending on dataset
# training config:
epochs = 16
batch_size = 32
# https://www.kaggle.com/paultimothymooney/blood-cells
# train_path = '../large_files/blood_cell_images/TRAIN'
# valid_path = '../large_files/blood_cell_images/TEST'
# https://www.kaggle.com/moltean/fruits
# train_path = '../large_files/fruits-360/Training'
# valid_path = '../large_files/fruits-360/Validation'
train_path = '../large_files/fruits-360-small/Training'
valid_path = '../large_files/fruits-360-small/Validation'
# useful for getting number of files
image_files = glob(train_path + '/*/*.jp*g')
valid_image_files = glob(valid_path + '/*/*.jp*g')
# useful for getting number of classes
folders = glob(train_path + '/*')
# look at an image for fun
plt.imshow(image.load_img(np.random.choice(image_files)))
plt.show()
# add preprocessing layer to the front of VGG
res = ResNet50(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
# don't train existing weights
for layer in res.layers:
layer.trainable = False
# our layers - you can add more if you want
x = Flatten()(res.output)
# x = Dense(1000, activation='relu')(x)
prediction = Dense(len(folders), activation='softmax')(x)
# create a model object
model = Model(inputs=res.input, outputs=prediction)
# view the structure of the model
model.summary()
# tell the model what cost and optimization method to use
model.compile(
loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy']
)
# create an instance of ImageDataGenerator
gen = ImageDataGenerator(
rotation_range=20,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip=True,
preprocessing_function=preprocess_input
)
# test generator to see how it works and some other useful things
# get label mapping for confusion matrix plot later
test_gen = gen.flow_from_directory(valid_path, target_size=IMAGE_SIZE)
print(test_gen.class_indices)
labels = [None] * len(test_gen.class_indices)
for k, v in test_gen.class_indices.items():
labels[v] = k
# should be a strangely colored image (due to VGG weights being BGR)
for x, y in test_gen:
print("min:", x[0].min(), "max:", x[0].max())
plt.title(labels[np.argmax(y[0])])
plt.imshow(x[0])
plt.show()
break
# create generators
train_generator = gen.flow_from_directory(
train_path,
target_size=IMAGE_SIZE,
shuffle=True,
batch_size=batch_size,
)
valid_generator = gen.flow_from_directory(
valid_path,
target_size=IMAGE_SIZE,
shuffle=True,
batch_size=batch_size,
)
# fit the model
r = model.fit(
train_generator,
validation_data=valid_generator,
epochs=epochs,
steps_per_epoch=len(image_files) // batch_size,
validation_steps=len(valid_image_files) // batch_size,
)
def get_confusion_matrix(data_path, N):
# we need to see the data in the same order
# for both predictions and targets
print("Generating confusion matrix", N)
predictions = []
targets = []
i = 0
for x, y in gen.flow_from_directory(data_path, target_size=IMAGE_SIZE, shuffle=False, batch_size=batch_size * 2):
i += 1
if i % 50 == 0:
print(i)
p = model.predict(x)
p = np.argmax(p, axis=1)
y = np.argmax(y, axis=1)
predictions = np.concatenate((predictions, p))
targets = np.concatenate((targets, y))
if len(targets) >= N:
break
cm = confusion_matrix(targets, predictions)
return cm
cm = get_confusion_matrix(train_path, len(image_files))
print(cm)
valid_cm = get_confusion_matrix(valid_path, len(valid_image_files))
print(valid_cm)
# plot some data
# loss
plt.plot(r.history['loss'], label='train loss')
plt.plot(r.history['val_loss'], label='val loss')
plt.legend()
plt.show()
# accuracies
plt.plot(r.history['accuracy'], label='train acc')
plt.plot(r.history['val_accuracy'], label='val acc')
plt.legend()
plt.show()
from util import plot_confusion_matrix
plot_confusion_matrix(cm, labels, title='Train confusion matrix')
plot_confusion_matrix(valid_cm, labels, title='Validation confusion matrix')