-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVehicle-Attributes-Model.py
319 lines (218 loc) · 10.4 KB
/
Vehicle-Attributes-Model.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
from google.colab import drive
drive.mount('/content/drive')
!unzip /content/drive/My\ Drive/Project_VeRI/VeRi.zip
"""## Imports"""
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D
from keras_preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.preprocessing import image
from tensorflow.keras import layers
from tensorflow.keras import Model
from sklearn.metrics import classification_report, confusion_matrix
from tensorflow.keras.callbacks import TensorBoard, CSVLogger
import datetime
import os
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.applications.inception_v3 import InceptionV3
from tqdm import tqdm
"""## Pre-Processing Data"""
class data():
def __init__(self, train_path , test_path):
self.train_path = train_path
self.test_path = test_path
def load_data(self):
train_df = pd.read_csv(self.train_path)
test_df = pd.read_csv(self.test_path)
train_df.columns = ['ImageName' , 'VehicleID' ,'CameraID' , 'ColorID' ,'TypeID']
test_df.columns = ['ImageName' , 'VehicleID' ,'CameraID' , 'ColorID' ,'TypeID']
train_df.drop(columns = ['CameraID'], axis = 1,inplace = True)
train_df.drop(columns = ['VehicleID'], axis = 1,inplace = True)
test_df.drop(columns = ['VehicleID'], axis = 1,inplace = True)
test_df.drop(columns = ['CameraID'], axis = 1,inplace = True)
index_df = train_df[train_df['TypeID'] == 5].index
train_df.drop(index_df, inplace=True)
return train_df, test_df
def Enumerate(self, col):
dict_train = {}
dict_test = {}
self.train_df, self.test_df = self.load_data()
arr_train = np.sort(self.train_df[col].unique())
arr_test = np.sort(self.test_df[col].unique())
for count, item in enumerate(arr_train):
dict_train[item] = count
for count, item in enumerate(arr_test):
dict_test[item] = count
return dict_train ,dict_test
def change(self):
#VehicleID_dict_train, VehicleID_dict_test = self.Enumerate('VehicleID')
ColorID_dict_train, ColorID_dict_test = self.Enumerate('ColorID')
TypeID_dict_train, TypeID_dict_test = self.Enumerate('TypeID')
#self.train_df = self.train_df.replace({"VehicleID": VehicleID_dict_train})
self.train_df = self.train_df.replace({"ColorID": ColorID_dict_train})
self.train_df = self.train_df.replace({"TypeID": TypeID_dict_train})
#self.test_df = self.test_df.replace({"VehicleID": VehicleID_dict_test})
self.test_df = self.test_df.replace({"ColorID": ColorID_dict_test})
self.test_df = self.test_df.replace({"TypeID": TypeID_dict_test})
return self.train_df, self.test_df
train_path = '/content/drive/MyDrive/Project_VeRI/train_label.csv'
test_path = '/content/drive/MyDrive/Project_VeRI/test_label.csv'
m = data(train_path, test_path)
train_df , test_df = m.change()
"""## Custom Generator"""
#Custom DataGenerator for Veri Dataset
class VeriDataGenerator(tf.keras.utils.Sequence):
def __init__(self, df, X_col, y_col, batch_size=32,input_size = (100,100,3), shuffle=True):
self.batch_size = batch_size
self.df = df.copy()
self.input_size = input_size
self.shuffle = shuffle
self.X_col = X_col
self.y_col = y_col
self.n = len(self.df)
#self.n_vehicleID = df[y_col['VehicleID']].nunique()
self.n_colorID = df[y_col['ColorID']].nunique()
self.n_typeID = df[y_col['TypeID']].nunique()
def __get_input(self, path, target_size):
image = tf.keras.preprocessing.image.load_img("/content/VeRi/image_train/" + path)
image_arr = tf.keras.preprocessing.image.img_to_array(image)
image_arr = tf.image.resize(image_arr,(target_size[0], target_size[1])).numpy()
return image_arr/255.
def __get_output(self, label, num_classes):
return tf.keras.utils.to_categorical(label, num_classes=num_classes, dtype='float32')
def __get_data(self, batches):
path_batch = batches[self.X_col['ImageName']]
#vehicleID_batch = batches[self.y_col['VehicleID']]
colorID_batch = batches[self.y_col['ColorID']]
TypeID_batch = batches[self.y_col['TypeID']]
X_batch = np.asarray([self.__get_input(x, self.input_size) for x in path_batch])
#y0_batch = np.asarray([self.__get_output(y, self.n_vehicleID) for y in vehicleID_batch])
y0_batch = np.asarray([self.__get_output(y, self.n_colorID) for y in colorID_batch])
y1_batch = np.asarray([self.__get_output(y, self.n_typeID) for y in TypeID_batch])
return X_batch, tuple([y0_batch, y1_batch])
def __len__(self):
return self.n // self.batch_size
def __getitem__(self, index):
batches = self.df[index * self.batch_size:(index + 1) * self.batch_size]
X, y = self.__get_data(batches)
return X, y
def on_epoch_end(self):
pass
def data_show(self):
print(self.df)
def total(self):
print("Total Images:", len(self.df))
#Custom DataGenerator for Veri Dataset
class VeriDataGenerator_Valid(tf.keras.utils.Sequence):
def __init__(self, df, X_col, y_col, batch_size=32,input_size = (100,100,3), shuffle=True):
self.batch_size = batch_size
self.df = df.copy()
self.input_size = input_size
self.shuffle = shuffle
self.X_col = X_col
self.y_col = y_col
self.n = len(self.df)
#self.n_vehicleID = df[y_col['VehicleID']].nunique()
self.n_colorID = df[y_col['ColorID']].nunique()
self.n_typeID = df[y_col['TypeID']].nunique()
def __get_input(self, path, target_size):
image = tf.keras.preprocessing.image.load_img("/content/VeRi/image_test/" + path)
image_arr = tf.keras.preprocessing.image.img_to_array(image)
image_arr = tf.image.resize(image_arr,(target_size[0], target_size[1])).numpy()
return image_arr/255.
def __get_output(self, label, num_classes):
return tf.keras.utils.to_categorical(label, num_classes=num_classes, dtype='float32')
def __get_data(self, batches):
path_batch = batches[self.X_col['ImageName']]
#vehicleID_batch = batches[self.y_col['VehicleID']]
colorID_batch = batches[self.y_col['ColorID']]
TypeID_batch = batches[self.y_col['TypeID']]
X_batch = np.asarray([self.__get_input(x, self.input_size) for x in path_batch])
#y0_batch = np.asarray([self.__get_output(y, self.n_vehicleID) for y in vehicleID_batch])
y0_batch = np.asarray([self.__get_output(y, self.n_colorID) for y in colorID_batch])
y1_batch = np.asarray([self.__get_output(y, self.n_typeID) for y in TypeID_batch])
return X_batch, tuple([y0_batch, y1_batch])
def __len__(self):
return self.n // self.batch_size
def __getitem__(self, index):
batches = self.df[index * self.batch_size:(index + 1) * self.batch_size]
X, y = self.__get_data(batches)
return X, y
def on_epoch_end(self):
pass
def data_show(self):
print(self.df)
def total(self):
print("Total Images:", len(self.df))
train_data = train_df.sample(frac=1, random_state=42).reset_index(drop=True)
valid_data = test_df.sample(frac=1, random_state=42).reset_index(drop=True)
traingen = VeriDataGenerator(train_data,
X_col={'ImageName':'ImageName'},
y_col={'ColorID': 'ColorID' , 'TypeID':'TypeID'},
batch_size=32, input_size=(224,224,3))
validgen = VeriDataGenerator_Valid(valid_data,
X_col={'ImageName':'ImageName'},
y_col={'ColorID': 'ColorID' , 'TypeID':'TypeID'},
batch_size=32, input_size=(224,224,3))
"""## Model
### Inception v3
"""
pre_trained_model = InceptionV3(input_shape=(224, 224, 3), include_top=False, weights='imagenet')
#pre_trained_model.summary()
tf.keras.backend.clear_session()
for layer in pre_trained_model.layers:
layer.trainable = False
last_layer = pre_trained_model.get_layer('mixed7')
last_output = last_layer.output
x = Flatten()(last_output)
x = Dense(512, activation='relu')(x)
x = Dropout(0.2)(x)
output_1 = tf.keras.layers.Dense(10, activation='softmax', name='ColorID_Output')(x)
output_2 = tf.keras.layers.Dense(8, activation='softmax', name='TypeID_Output')(x)
model = Model(pre_trained_model.input, [output_1, output_2])
Adam = tf.keras.optimizers.Adam(beta_1=0.9, beta_2=0.999)
model.compile(optimizer=Adam, loss='categorical_crossentropy', metrics=['acc'])
model.summary()
"""#### Callbacks"""
def exponential_decay(lr0, s):
def exponential_decay_fn(epoch):
return lr0 * 0.1 ** (epoch / s)
return exponential_decay_fn
exponential_decay_fn = exponential_decay(lr0=0.0009, s=5)
lr_scheduler_ed = keras.callbacks.LearningRateScheduler(exponential_decay_fn)
early_stopping_m = keras.callbacks.EarlyStopping(monitor='val_loss', mode='min', verbose=1, patience=5)
csv_file = 'appearance_attr_baseline.csv'
"""### Training"""
STEP_SIZE_TRAIN=traingen.n//traingen.batch_size
STEP_SIZE_VALID=validgen.n//validgen.batch_size
history = model.fit(traingen,
steps_per_epoch = STEP_SIZE_TRAIN,
epochs=5,
validation_data = validgen,
validation_steps = STEP_SIZE_VALID,
callbacks=[CSVLogger(csv_file), tensorboard_callback],
verbose=1)
pd.read_csv(csv_file).head()
plt.figure(figsize=(12,12))
pd.DataFrame(history.history).plot()
plt.figure()
plt.show()
loss = history.history['loss']
val_loss = history.history['val_loss']
cam_acc = history.history['ColorID_Output_acc']
val_cam_acc = history.history['val_ColorID_Output_acc']
type_acc = history.history['TypeID_Output_acc']
val_type_acc = history.history['val_TypeID_Output_loss']
epochs = range(0, 5)
plt.plot(epochs, loss, 'g')
plt.plot(epochs, val_loss, 'b')
plt.plot(epochs, cam_acc, 'g')
plt.plot(epochs, val_cam_acc, 'b')
plt.plot(epochs, type_acc, 'g')
plt.plot(epochs, val_type_acc, 'b')