-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
272 lines (182 loc) · 7.81 KB
/
train.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
#imports
import tensorflow as tf
from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img, load_img, img_to_array
import numpy as np
import pickle
from sklearn.metrics import confusion_matrix, recall_score, precision_score, f1_score
from tensorflow import keras
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from tensorflow.keras.layers import Activation, Dropout, Flatten, Dense
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.callbacks import ModelCheckpoint
class Metrics(keras.callbacks.Callback):
"""
Implementation of custom metrics: Precision, Recall, F-Measure and Confusion Matrix
"""
def on_train_begin(self, logs={}):
self._data = []
def on_epoch_end(self, batch, logs={}):
for i in range(len(self.validation_data)):
x_val, y_val = self.validation_data.__getitem__(i)
print(y_val.shape)
print(y_val[1])
print(y_val[2])
y_predict = np.asarray(model.predict(self.validation_data, steps = 1))
print(y_predict.shape)
with tf.Session() as sess:
lab = [i for i in range(0,75)]
print(y_val.shape)
y_val = np.argmax(y_val, axis=1)
y_predict = np.argmax(y_predict, axis=1)
print("y_val: ", y_val)
print("y_predict:", y_predict)
print("\nMetrics for Epoch")
print("Confusion Matrix:\n",confusion_matrix(y_val,y_predict, labels=lab))
print("Recall: ", recall_score(y_val,y_predict, average=None, labels=lab))
print("Precision: ", precision_score(y_val,y_predict, average = None, labels=lab))
print("F1_score: ", f1_score(y_val,y_predict, average =None, labels=lab))
print("\n")
self._data.append({
'val_recall': recall_score(y_val, y_predict, average = None, labels=lab),
'val_precision': precision_score(y_val, y_predict, average = None, labels=lab),
'val_f1_score': f1_score(y_val,y_predict, average = None, labels=lab),
})
return
def get_data(self):
return self._data
metrics = Metrics()
def lr_scheduler(epoch,lr):
"""
Learning rate scheduler decays the learning rate by factor of 0.1 every 10 epochs after 20 epochs
"""
decay_rate = 0.1
if epoch==10:
return lr*decay_rate
elif epoch%10==0 and epoch >20:
return lr*decay_rate
return lr
# To put in the model, put it inside callbacks
LRScheduler = keras.callbacks.LearningRateScheduler(lr_scheduler,verbose=1)
#Data Generator for Data augmentation. Edit possible
train_datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
validation_split = 0.008)
#333 validation
train_generator = train_datagen.flow_from_directory(
'datasets/large_sample', # this is the target directory
target_size=(150, 150), # all images will be resized to 150x150
batch_size=28,
class_mode='categorical',
subset = 'training') # since we use binary_crossentropy loss, we need binary labels
val_generator = train_datagen.flow_from_directory(
'datasets/large_sample', # this is the target directory
target_size=(150, 150), # all images will be resized to 150x150
batch_size=28,
class_mode='categorical',
subset = 'validation') # since we use binary_crossentropy loss, we need binary labels
labels = (train_generator.class_indices)
labels = dict((v,k) for k,v in labels.items())
print(labels)
#Naive Model
input_tensor = keras.layers.Input(shape = (150,150,3))
model = ResNet50(input_tensor=input_tensor, include_top=False, weights = None)
x=model.output
x = tf.keras.layers.GlobalAveragePooling2D(data_format='channels_last')(x)
x = tf.keras.layers.Dense(1024,activation='relu')(x)
x = tf.keras.layers.Dropout(0.4)(x)
x=tf.keras.layers.Dense(75,activation ='softmax')(x)
model = tf.keras.models.Model(model.input, x)
model.compile(loss='categorical_crossentropy',
optimizer=tf.keras.optimizers.SGD(lr=0.01)
,metrics=['accuracy'])
checkpoint = ModelCheckpoint('model_resA.h5',monitor='val_acc',save_best_only=True,period=1,verbose=0)
checkpointB = ModelCheckpoint('model_resB.h5', period = 1,verbose=0)
print(model.summary())
#model.load_weights('weights_resA.h5')
#history=model.fit_generator(train_generator,
# steps_per_epoch = 1800,
# epochs = 20, validation_data = val_generator, callbacks=[checkpoint,LRScheduler])
#file_pi = open('Train_histA',"wb")
#pickle.dump(history.history,file_pi)
#file_pi.close()
#model = load_model('model_ResA.h5')
model=load_model('model_resA.h5')
img = load_img('datasets/large_sample/almond_desert/6350.jpg') # this is a PIL image
img=img.resize((150,150))
x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150)
x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 3, 150, 150)
print(x.shape)
y = model.predict(x)
print(y)
print(labels[np.argmax(y)])
for i,layer in enumerate(model.layers):
print(i,layer.name)
#Data Generator for Data augmentation. Edit possible
val_datagen = ImageDataGenerator(
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
rescale=1./255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
fill_mode='nearest',
validation_split = 0.2)
small_train_generator = val_datagen.flow_from_directory(
'datasets/small_sample', # this is the target directory
target_size=(150, 150), # all images will be resized to 150x150
batch_size=32,
class_mode='categorical',
subset = 'training')
small_val_generator = val_datagen.flow_from_directory(
'datasets/small_sample', # this is the target directory
target_size=(150, 150), # all images will be resized to 150x150
batch_size=32,
class_mode='categorical',
subset = 'validation')
for layer in model.layers[:-4]:
layer.trainable=False
for i,layer in enumerate(model.layers):
print(i,layer.trainable)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
new_model = tf.keras.models.Model(model.input, model.layers[-5].output)
print(new_model.summary())
y=new_model.output
y = tf.keras.layers.GlobalAveragePooling2D(data_format='channels_last')(y)
y = tf.keras.layers.Dense(1024,activation='relu')(y)
y = tf.keras.layers.Dropout(0.4)(y)
y=tf.keras.layers.Dense(25,activation ='softmax')(y)
new_model = tf.keras.models.Model(new_model.input, y)
new_model.compile(loss='categorical_crossentropy',
optimizer='sgd',
metrics=['accuracy'])
#new_model.summary()
for i,layer in enumerate(model.layers):
print(i,layer.trainable)
new_model = load_model('model_resB.h5')
#historyB=new_model.fit_generator(small_train_generator,
# steps_per_epoch =7,
# epochs = 10, validation_data = small_val_generator,callbacks=[checkpointB])
##file_pi = open('Train_histB',"wb")
#pickle.dump(historyB.history, file_pi)
#file_pi.close()
img = load_img('datasets/small_sample/stir_fried_lettuce/6581.jpg') # this is a PIL image
img=img.resize((150,150))
x = img_to_array(img) # this is a Numpy array with shape (3, 150, 150)
x = x.reshape((1,) + x.shape) # this is a Numpy array with shape (1, 3, 150, 150)
print(x.shape)
labelsSmall = (small_train_generator.class_indices)
labelsSmall = dict((v,k) for k,v in labelsSmall.items())
print(labelsSmall, end="\n")
y = new_model.predict(x)
print(y)
print(labelsSmall[np.argmax(y)])