-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer_Learning.py
374 lines (283 loc) · 10.7 KB
/
transfer_Learning.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
# General
import os
import cv2
import argparse
import numpy as np
# Keras
from keras import backend as K
from keras import __version__
from keras.applications.inception_v3 import InceptionV3, preprocess_input
from keras.models import Model
from keras.layers import Dense, AveragePooling2D, GlobalAveragePooling2D, Input, Flatten, Dropout
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
from tensorflow.python.framework.graph_util import convert_variables_to_constants
# In[ ]:
# Folder_Path = r'C:\Users\Sachin13390\Desktop\NLP'
IM_WIDTH, IM_HEIGHT = 299, 299 #fixed size for InceptionV3
image_Dir_Not_Valid_Error = "Input image directory not valid - "
# In[ ]:
def Get_Sub_Folders_Count_From_Root_Folders(Folder_Path):
return (len(next(os.walk(Folder_Path))[1]))
# In[ ]:
def check_If_Directory_Exists(dir_Path):
if os.path.exists(dir_Path):
return True
return False
# In[ ]:
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
graph = session.graph
with graph.as_default():
freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
output_names = output_names or []
output_names += [v.op.name for v in tf.global_variables()]
input_graph_def = graph.as_graph_def()
if clear_devices:
for node in input_graph_def.node:
node.device = ""
frozen_graph = convert_variables_to_constants(session, input_graph_def,
output_names, freeze_var_names)
return frozen_graph
# In[ ]:
def Get_Train_And_Test_Generator(train_image_dir, test_image_dir, FLAGS):
train_datagen = ImageDataGenerator(
rotation_range = 20, width_shift_range=0.2, height_shift_range=0.2,
rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True,
vertical_flip=True, fill_mode='nearest')
test_datagen = ImageDataGenerator(
rotation_range=20, width_shift_range=0.2, height_shift_range=0.2,
rescale=1./255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True,
vertical_flip=True, fill_mode='nearest')
train_generator = train_datagen.flow_from_directory(
train_image_dir, target_size=(IM_WIDTH, IM_HEIGHT),
batch_size=32, class_mode='categorical')
test_generator = test_datagen.flow_from_directory(
test_image_dir, target_size=(IM_WIDTH, IM_HEIGHT),
batch_size=32, class_mode='categorical')
return (train_generator, test_generator)
# In[ ]:
def Get_New_Layer_Model(base_Model, no_Of_Classes):
x = base_Model.output
x = AveragePooling2D((8, 8), border_mode='valid', name='avg_pool')(x)
x = Dropout(0.2)(x)
x = Flatten()(x)
predictions = Dense(no_Of_Classes, activation='softmax')(x)
model = Model(input=base_Model.input, output=predictions)
return model
# In[ ]:
def train_Model(FLAGS):
if not os.path.exists(FLAGS.train_image_dir):
print(image_Dir_Not_Valid_Error, FLAGS.train_image_dir)
return
if not os.path.exists(FLAGS.test_image_dir):
return (image_Dir_Not_Valid_Error, FLAGS.test_image_dir)
no_Of_Classes = Get_Sub_Folders_Count_From_Root_Folders(FLAGS.train_image_dir)
train_generator, test_generator = Get_Train_And_Test_Generator(
FLAGS.train_image_dir,
FLAGS.test_image_dir,
FLAGS)
input_tensor = ''
if(K.image_dim_ordering() == 'th'): # For tensorflow
input_tensor = Input(shape=(3, IM_WIDTH, IM_HEIGHT))
else:
input_tensor = Input(shape=(IM_WIDTH, IM_HEIGHT, 3))
# Get Inception model from Keras
base_model = InceptionV3(input_tensor = input_tensor, weights = 'imagenet', include_top = False)
for layer in base_model.layers:
layer.trainable = False
model = Get_New_Layer_Model(base_model, no_Of_Classes)
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
model.fit_generator(train_generator,
steps_per_epoch = 10,
epochs = FLAGS.how_many_training_steps, validation_data = test_generator, validation_steps = 16)
frozen_graph = freeze_session(K.get_session(),
output_names=[out.op.name for out in model.outputs])
tf.train.write_graph(frozen_graph, "some_directory", "ZZZZZZZ.pb", as_text=False)
# model.save(FLAGS.output_graph)
# In[ ]:
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--train_image_dir',
type=str,
default='',
help='Path to train folders of labeled images.'
)
parser.add_argument(
'--test_image_dir',
type=str,
default='',
help='Path to test folders of labeled images.'
)
parser.add_argument(
'--output_graph',
type=str,
default='/tmp/output_graph.pb',
help='Where to save the trained graph.'
)
parser.add_argument(
'--output_labels',
type=str,
default='/tmp/output_labels.txt',
help='Where to save the trained graph\'s labels.'
)
parser.add_argument(
'--summaries_dir',
type=str,
default='/tmp/retrain_logs',
help='Where to save summary logs for TensorBoard.'
)
parser.add_argument(
'--how_many_training_steps',
type=int,
default=4000,
help='How many training steps to run before ending.'
)
parser.add_argument(
'--learning_rate',
type=float,
default=0.01,
help='How large a learning rate to use when training.'
)
parser.add_argument(
'--testing_percentage',
type=int,
default=10,
help='What percentage of images to use as a test set.'
)
parser.add_argument(
'--validation_percentage',
type=int,
default=10,
help='What percentage of images to use as a validation set.'
)
parser.add_argument(
'--eval_step_interval',
type=int,
default=10,
help='How often to evaluate the training results.'
)
parser.add_argument(
'--train_batch_size',
type=int,
default=100,
help='How many images to train on at a time.'
)
parser.add_argument(
'--test_batch_size',
type=int,
default=-1,
help="""\
How many images to test on. This test set is only used once, to evaluate
the final accuracy of the model after training completes.
A value of -1 causes the entire test set to be used, which leads to more
stable results across runs.\
"""
)
parser.add_argument(
'--validation_batch_size',
type=int,
default=100,
help="""\
How many images to use in an evaluation batch. This validation set is
used much more often than the test set, and is an early indicator of how
accurate the model is during training.
A value of -1 causes the entire validation set to be used, which leads to
more stable results across training iterations, but may be slower on large
training sets.\
"""
)
parser.add_argument(
'--print_misclassified_test_images',
default=False,
help="""\
Whether to print out a list of all misclassified test images.\
""",
action='store_true'
)
parser.add_argument(
'--model_dir',
type=str,
default='/tmp/imagenet',
help="""\
Path to classify_image_graph_def.pb,
imagenet_synset_to_human_label_map.txt, and
imagenet_2012_challenge_label_map_proto.pbtxt.\
"""
)
parser.add_argument(
'--bottleneck_dir',
type=str,
default='/tmp/bottleneck',
help='Path to cache bottleneck layer values as files.'
)
parser.add_argument(
'--final_tensor_name',
type=str,
default='final_result',
help="""\
The name of the output classification layer in the retrained graph.\
"""
)
parser.add_argument(
'--flip_left_right',
default=False,
help="""\
Whether to randomly flip half of the training images horizontally.\
""",
action='store_true'
)
parser.add_argument(
'--random_crop',
type=int,
default=0,
help="""\
A percentage determining how much of a margin to randomly crop off the
training images.\
"""
)
parser.add_argument(
'--random_scale',
type=int,
default=0,
help="""\
A percentage determining how much to randomly scale up the size of the
training images by.\
"""
)
parser.add_argument(
'--random_brightness',
type=int,
default=0,
help="""\
A percentage determining how much to randomly multiply the training image
input pixels up or down by.\
"""
)
FLAGS, unparsed = parser.parse_known_args()
train_Model(FLAGS)
# # ###################### Testing ######################
# In[ ]:
# model_File_Path = r'C:\Users\Sachin13390\Desktop\Transfer_learning\Trained_Model\5_Epoch_Trained_Model.pb'
# image_Folder_Path = r'C:\Users\Sachin13390\Desktop\Transfer_learning\Test_Images'
# image_Path = r'C:\Users\Sachin13390\Desktop\Transfer_learning\Test_Images\7.jpg'
# model = load_model(model_File_Path)
# In[ ]:
# for i in range (1,14):
# image_Path = os.path.join(image_Folder_Path, (str(i) + '.jpg'))
# # print(image_Path)
# image = cv2.imread(image_Path)
# image = cv2.resize(image, (299, 299))
# # cv2.imshow('image',image)
# # cv2.waitKey(0)
# image = image.reshape(1, 299, 299, 3)
# image = image/255
# print(np.;\argmax(model.predict(image)))
# In[ ]:
# python transfer_Learning.py --train_image_dir=C:\\Users\\Sachin13390\\Desktop\\Transfer_learning\\Data_CNN\\Train --test_image_dir=C:\\Users\\Sachin13390\\Desktop\\Transfer_learning\\Data_CNN\\Test --output_graph=C:\\Users\\Sachin13390\\Desktop\\Transfer_learning\\Trained_Model\\trained_Model.h5 --how_many_training_steps=1
# In[ ]: