-
Notifications
You must be signed in to change notification settings - Fork 1
/
foodapp_imageclassficiation_high_accuracy.py
328 lines (261 loc) · 10.7 KB
/
foodapp_imageclassficiation_high_accuracy.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
# -*- coding: utf-8 -*-
"""FoodApp-ImageClassficiation-High-Accuracy.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1-FRFdXNjelZxmJwjypDKEbHgnGlgaQif
"""
## Spring 2021 VC1A
## Supervisor: Name: Margrethe Horlyck-Romanovsky
## Organization / Department: Department of Health and Nutrition Sciences
## CISC 4900: Food Detection App
## By: Lalla Sankara, Sergio Castaneda, Andrew Brennan
import tensorflow as tf
from tensorflow import keras
from keras.models import Sequential
from keras.layers import Dense, Conv2D , MaxPool2D , Flatten , Dropout
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from sklearn.metrics import classification_report,confusion_matrix
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
print(tf.__version__)
#Download data set and extract: From Tensor Flow 101
import os
import cv2
def get_data_extract():
if "food-101" in os.listdir():
print("Dataset already exists")
else:
print("Downloading the data...")
!wget http://data.vision.ee.ethz.ch/cvl/food-101.tar.gz
print("Dataset downloaded!")
print("Extracting data..")
!tar xzvf food-101.tar.gz
print("Extraction done!")
# Download data and extract it to folder
get_data_extract()
# Check the extracted dataset folder
!ls food-101/
#showcase data set and images
import os
os.listdir('food-101/images')
#
!head food-101/meta/train.txt
#
os.listdir('food-101/meta')
#
!head food-101/meta/classes.txt
# Commented out IPython magic to ensure Python compatibility.
## Visual Random image from each of the 101 classes
## USing
import matplotlib.pyplot as plt
import matplotlib.image as img
# %matplotlib inline
import numpy as np
from collections import defaultdict
import collections
import os
# 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
"""Spliting Data by a Train and Test Sets"""
## Split Data into train and test using train.txt and test.txt
# Helper method to split dataset into train and test folders
from shutil import copy
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 files in Train folder
print("Total number of samples in train folder")
!find food-101/train -type d -or -type f -printf '.' | wc -c
#Check files in Test folder
print("Total number of samples in test folder")
!find food-101/test -type d -or -type f -printf '.' | wc -c
# List of all 101 types of foods(sorted alphabetically)
foods_sorted
# Helper method to create train_mini and test_mini data samples
from shutil import copytree, rmtree
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))
# Helper function to select n random food classes
import random
def pick_n_random_classes(n):
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
# 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")
!find food-101/train_mini -type d -or -type f -printf '.' | wc -c
print("Creating test data folder with new classes")
dataset_mini(food_list, src_test, dest_test)
print("Total number of samples in test folder")
!find food-101/test_mini -type d -or -type f -printf '.' | wc -c
# 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")
!find food-101/train_mini -type d -or -type f -printf '.' | wc -c
print("Creating test data folder with new classes")
dataset_mini(food_list, src_test, dest_test)
print("Total number of samples in test folder")
!find food-101/test_mini -type d -or -type f -printf '.' | wc -c
"""Model Building"""
# Pretrained Inception MOdel On subset of data with 11 food classes
import tensorflow as tf
import tensorflow.keras.backend as K
from tensorflow.keras import regularizers
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D, GlobalAveragePooling2D, AveragePooling2D
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
import numpy as np
K.clear_session()
n_classes = n
img_width, img_height = 299, 299
train_data_dir = 'food-101/train_mini'
validation_data_dir = 'food-101/test_mini'
nb_train_samples = 8250 #75750
nb_validation_samples = 2750 #25250
batch_size = 16
train_datagen = ImageDataGenerator(
rescale=1. / 255,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True)
test_datagen = ImageDataGenerator(rescale=1. / 255)
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,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'])
checkpointer = ModelCheckpoint(filepath='FoodImageModel.hdf5', verbose=1, save_best_only=True)
csv_logger = CSVLogger('history.log')
history_11class = 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=10,
verbose=1,
callbacks=[csv_logger, checkpointer])
model.save('FoodImageModel.hdf5')
plot_accuracy(history_11class,'FOOD101-Inceptionv3')
plot_loss(history_11class,'FOOD101-Inceptionv3')
# Commented out IPython magic to ensure Python compatibility.
# # Loading the best saved model to make predictions
# %%time
# from tensorflow.keras.models import load_model
# K.clear_session()
# model_best = load_model('FoodImageModel.hdf5',compile = False)
"""### Predicting classes for new images from internet using the best trained model"""
from google.colab import drive
drive.mount('/content/drive')
# Commented out IPython magic to ensure Python compatibility.
# # Loading the best model to make predictions
# %%time
# import tensorflow.keras.backend as K
# from tensorflow.keras.models import load_model
#
# K.clear_session()
# model_best = load_model('FoodImageModel.hdf5',compile = False)
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np
import os
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 /= 255.
pred = model.predict(img)
index = np.argmax(pred)
food_list.sort()
pred_value = food_list[index]
if show:
plt.imshow(img[0])
plt.axis('off')
plt.title(pred_value)
plt.show()
# Downloading or uploading images from internet or you local computer
from google.colab import files
uploaded = files.upload()
# Make a list of downloaded images and test the trained model
images = []
images.append('')
images.append('')
predict_class(model_best, images, True)