-
Notifications
You must be signed in to change notification settings - Fork 31
/
project_ai_bootcamp.py
356 lines (286 loc) · 11.9 KB
/
project_ai_bootcamp.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
# -*- coding: utf-8 -*-
"""project ai bootcamp
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19qp989w1IqgR-l2ht9iyCMmSqA2as6pT
"""
# Uncomment the below line of code if you want to try TensorFlow v2.0
#!pip install tf-nightly-gpu-2.0-preview
# Commented out IPython magic to ensure Python compatibility.
from __future__ import absolute_import, division, print_function
import tensorflow as tf
import tensorflow.keras.backend as K
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
from tensorflow.keras import regularizers
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Dense, Dropout
from tensorflow.keras.layers import GlobalAveragePooling2D
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.callbacks import ModelCheckpoint, CSVLogger
from tensorflow.keras.optimizers import SGD
from tensorflow.keras.regularizers import l2
from tensorflow import keras
from tensorflow.keras import models
from tensorflow.keras.applications.inception_v3 import preprocess_input
import cv2
import os
import random
import collections
from collections import defaultdict
from shutil import copy
from shutil import copytree, rmtree
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img
# %matplotlib inline
# Check TF version and whether GPU is enabled
print(tf.__version__)
print(tf.test.gpu_device_name())
# Clone tensorflow/examples repo which has images to evaluate trained model
!git clone https://github.com/tensorflow/examples.git
# Helper function to download data and extract
def get_data_extract():
if "food-101" in os.listdir():
print("Dataset already exists")
else:
tf.keras.utils.get_file(
'food-101.tar.gz',
'http://data.vision.ee.ethz.ch/cvl/food-101.tar.gz',
cache_subdir='/content',
extract=True,
archive_format='tar',
cache_dir=None
)
print("Dataset downloaded and extracted!")
# Download data and extract it to folder
get_data_extract()
# Check the extracted dataset folder
os.listdir('food-101/')
os.listdir('food-101/images')
os.listdir('food-101/meta')
# Visualize the data, showing one image per class from 101 classes
rows = 17
cols = 6
fig, ax = plt.subplots(rows, cols, figsize=(25,25))
fig.suptitle("Showing one random image from each class", y=1.05, fontsize=24) # Adding y=1.05, fontsize=24 helped me fix the suptitle overlapping with axes issue
data_dir = "food-101/images/"
foods_sorted = sorted(os.listdir(data_dir))
food_id = 0
for i in range(rows):
for j in range(cols):
try:
food_selected = foods_sorted[food_id]
food_id += 1
except:
break
food_selected_images = os.listdir(os.path.join(data_dir,food_selected)) # returns the list of all files present in each food category
food_selected_random = np.random.choice(food_selected_images) # picks one food item from the list as choice, takes a list and returns one random item
img = plt.imread(os.path.join(data_dir,food_selected, food_selected_random))
ax[i][j].imshow(img)
ax[i][j].set_title(food_selected, pad = 10)
plt.setp(ax, xticks=[],yticks=[])
plt.tight_layout()
# https://matplotlib.org/users/tight_layout_guide.html
# Helper method to split dataset into train and test folders
def prepare_data(filepath, src,dest):
classes_images = defaultdict(list)
with open(filepath, 'r') as txt:
paths = [read.strip() for read in txt.readlines()]
for p in paths:
food = p.split('/')
classes_images[food[0]].append(food[1] + '.jpg')
for food in classes_images.keys():
print("\nCopying images into ",food)
if not os.path.exists(os.path.join(dest,food)):
os.makedirs(os.path.join(dest,food))
for i in classes_images[food]:
copy(os.path.join(src,food,i), os.path.join(dest,food,i))
print("Copying Done!")
# Prepare train dataset by copying images from food-101/images to food-101/train using the file train.txt
print("Creating train data...")
prepare_data('food-101/meta/train.txt', 'food-101/images', 'food-101/train')
# Prepare test data by copying images from food-101/images to food-101/test using the file test.txt
print("Creating test data...")
prepare_data('food-101/meta/test.txt', 'food-101/images', 'food-101/test')
# Check how many files are in the train folder
train_files = sum([len(files) for i, j, files in os.walk("food-101/train")])
print("Total number of samples in train folder")
print(train_files)
# Check how many files are in the test folder
test_files = sum([len(files) for i, j, files in os.walk("food-101/test")])
print("Total number of samples in test folder")
print(test_files)
# List of all 101 types of foods(sorted alphabetically)
foods_sorted
# Helper method to create train_mini and test_mini data samples
def dataset_mini(food_list, src, dest):
if os.path.exists(dest):
rmtree(dest) # removing dataset_mini(if it already exists) folders so that we will have only the classes that we want
os.makedirs(dest)
for food_item in food_list :
print("Copying images into",food_item)
copytree(os.path.join(src,food_item), os.path.join(dest,food_item))
# picking 3 food items and generating separate data folders for the same
food_list = ['samosa','pizza','omelette']
src_train = 'food-101/train'
dest_train = 'food-101/train_mini'
src_test = 'food-101/test'
dest_test = 'food-101/test_mini'
print("Creating train data folder with new classes")
dataset_mini(food_list, src_train, dest_train)
print("Total number of samples in train folder")
train_files = sum([len(files) for i, j, files in os.walk("food-101/train_mini")])
print(train_files)
print("Creating test data folder with new classes")
dataset_mini(food_list, src_test, dest_test)
print("Total number of samples in test folder")
test_files = sum([len(files) for i, j, files in os.walk("food-101/test_mini")])
print(test_files)
def train_model(n_classes,num_epochs, nb_train_samples,nb_validation_samples):
K.clear_session()
img_width, img_height = 299, 299
train_data_dir = 'food-101/train_mini'
validation_data_dir = 'food-101/test_mini'
batch_size = 16
bestmodel_path = 'bestmodel_'+str(n_classes)+'class.hdf5'
trainedmodel_path = 'trainedmodel_'+str(n_classes)+'class.hdf5'
history_path = 'history_'+str(n_classes)+'.log'
train_datagen = ImageDataGenerator(
preprocessing_function=preprocess_input,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(preprocessing_function=preprocess_input)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=batch_size,
class_mode='categorical')
inception = InceptionV3(weights='imagenet', include_top=False)
x = inception.output
x = GlobalAveragePooling2D()(x)
x = Dense(128,activation='relu')(x)
x = Dropout(0.2)(x)
predictions = Dense(n_classes,kernel_regularizer=regularizers.l2(0.005), activation='softmax')(x)
model = Model(inputs=inception.input, outputs=predictions)
model.compile(optimizer=SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy'])
checkpoint = ModelCheckpoint(filepath=bestmodel_path, verbose=1, save_best_only=True)
csv_logger = CSVLogger(history_path)
history = model.fit_generator(train_generator,
steps_per_epoch = nb_train_samples // batch_size,
validation_data=validation_generator,
validation_steps=nb_validation_samples // batch_size,
epochs=num_epochs,
verbose=1,
callbacks=[csv_logger, checkpoint])
model.save(trainedmodel_path)
class_map = train_generator.class_indices
return history, class_map
# Train the model with data from 3 classes
n_classes = 3
epochs = 5
nb_train_samples = train_files
nb_validation_samples = test_files
history, class_map_3 = train_model(n_classes,epochs, nb_train_samples,nb_validation_samples)
print(class_map_3)
def plot_accuracy(history,title):
plt.title(title)
plt.plot(history.history['acc']) # change acc to accuracy if testing TF 2.0
plt.plot(history.history['val_acc']) # change val_accuracy if testing TF 2.0
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train_accuracy', 'validation_accuracy'], loc='best')
plt.show()
def plot_loss(history,title):
plt.title(title)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train_loss', 'validation_loss'], loc='best')
plt.show()
plot_accuracy(history,'FOOD101-Inceptionv3')
plot_loss(history,'FOOD101-Inceptionv3')
# Commented out IPython magic to ensure Python compatibility.
# %%time
# # Loading the best saved model to make predictions
#
# K.clear_session()
# model_best = load_model('bestmodel_3class.hdf5',compile = False)
def predict_class(model, images, show = True):
for img in images:
img = image.load_img(img, target_size=(299, 299))
img = image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
pred = model.predict(img)
index = np.argmax(pred)
food_list.sort()
pred_value = food_list[index]
#print(pred)
if show:
plt.imshow(img[0])
plt.axis('off')
plt.title(pred_value)
plt.show()
# Make a list of images and test the trained model
images = []
imagepath = '/content/examples/community/en/multi_class_classification/images/'
images.append(imagepath+'samosa.jpg')
images.append(imagepath+'pizza.jpg')
images.append(imagepath+'omelette.jpg')
predict_class(model_best, images, True)
"""The model got them all right!"""
# Helper function to select n random food classes
def pick_n_random_classes(n):
random.seed(9000)
food_list = []
random_food_indices = random.sample(range(len(foods_sorted)),n) # We are picking n random food classes
for i in random_food_indices:
food_list.append(foods_sorted[i])
food_list.sort()
print("These are the randomly picked food classes we will be training the model on...\n", food_list)
return food_list
# Lets try with more classes than just 3. Also, this time lets randomly pick the food classes
n = 11
food_list = pick_n_random_classes(11)
# Create the new data subset of n classes
print("Creating training data folder with new classes...")
dataset_mini(food_list, src_train, dest_train)
print("Total number of samples in train folder")
train_files = sum([len(files) for i, j, files in os.walk("food-101/train_mini")])
print(train_files)
print("Creating test data folder with new classes")
dataset_mini(food_list, src_test, dest_test)
print("Total number of samples in test folder")
test_files = sum([len(files) for i, j, files in os.walk("food-101/test_mini")])
print(test_files)
# Train the model with data from 3 classes
n_classes = 11
epochs = 5
nb_train_samples = train_files
nb_validation_samples = test_files
history, class_map_11 = train_model(n_classes,epochs, nb_train_samples,nb_validation_samples)
print(class_map_11)
plot_accuracy(history,'FOOD101-Inceptionv3')
plot_loss(history,'FOOD101-Inceptionv3')
# Commented out IPython magic to ensure Python compatibility.
# %%time
# # Loading the best saved model to make predictions
#
# K.clear_session()
# model_best = load_model('bestmodel_11class.hdf5',compile = False)
# Make a list of downloaded images and test the trained model
images = []
images.append(imagepath+'friedrice.jpg')
images.append(imagepath+'hotdog.jpg')
images.append(imagepath+'icecream.jpg')
images.append(imagepath+'pizza.jpg')
predict_class(model_best, images, True)