-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObject Classification.py
More file actions
318 lines (227 loc) · 11 KB
/
Object Classification.py
File metadata and controls
318 lines (227 loc) · 11 KB
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
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import tensorflow as tf
import cv2
from random import shuffle
from os import listdir, path
from tqdm import tqdm
import matplotlib.pyplot as plt
import copy
train_data_directory = "./data/train"
validation_data_directory = "./data/dev"
IMG_SIZE = 64
CHANNELS = 1
class_size = 2
lr = 0.01
def label_data(img_name):
"""
:param img_name: The image name
:return: The image label
"""
name = str(img_name)[:3]
if name == "cat":
return [1, 0]
elif name == "dog":
return [0, 1]
def create_data(img_dir, validation_data=False):
"""
:param img_dir: The images directory
:param validation_data: Flag to decide if data should be augmented for training
:return: A list of all the image data
"""
data = []
for img in tqdm(listdir(img_dir)):
label = label_data(img)
img_path = path.join(img_dir, img)
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
img1 = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
data.append([np.array(img1)/255, np.array(label)])
if not validation_data:
"""
Augment the training data by horizontally flipping each training example to
generate a "new" training example
"""
img2 = cv2.flip(img1, 1)
data.append([np.array(img2)/255, np.array(label)])
shuffle(data)
return data
train_data = create_data(train_data_directory)
validation_data = create_data(validation_data_directory, True)
X_train = np.array([data[0] for data in train_data]).reshape(-1, IMG_SIZE, IMG_SIZE, CHANNELS)
Y_train = np.array([data[1] for data in train_data]).reshape(-1, class_size)
X_train_mlp = np.array([data[0] for data in train_data]).reshape(-1, IMG_SIZE * IMG_SIZE * CHANNELS)
X_validation = np.array([data[0] for data in validation_data]).reshape(-1, IMG_SIZE, IMG_SIZE, CHANNELS)
Y_validation = np.array([data[1] for data in validation_data]).reshape(-1, class_size)
X_validation_mlp = np.array([data[0] for data in validation_data]).reshape(-1, IMG_SIZE * IMG_SIZE * CHANNELS)
def mlp_model(input_shape, layer_sizes, dropout_rate=0.0):
"""
Function to create and return a deep multilayer perceptron model for image classification
:param input_shape: The shape of the input data
:param layer_sizes: The number of units in the hidden and output layers
:param dropout_rate: The dropout rate
:return: An n-Hidden Layer MLP model
"""
input_layer = tf.keras.Input(shape=input_shape)
X = tf.keras.layers.Dense(layer_sizes[0])(input_layer)
X = tf.keras.layers.BatchNormalization()(X)
X = tf.keras.layers.Activation('relu')(X)
X = tf.keras.layers.Dropout(dropout_rate)(X)
for layer_size in layer_sizes[1:-1]:
X = tf.keras.layers.Dense(layer_size)(X)
X = tf.keras.layers.BatchNormalization()(X)
X = tf.keras.layers.Activation('relu')(X)
X = tf.keras.layers.Dropout(dropout_rate)(X)
X = tf.keras.layers.Dense(layer_sizes[-1])(X)
X = tf.keras.layers.BatchNormalization()(X)
output = tf.keras.layers.Activation('softmax')(X)
model = tf.keras.Model(inputs=input_layer, outputs=output)
return model
model = mlp_model((IMG_SIZE * IMG_SIZE * CHANNELS, ), [1500, 1500, 1000, 500, class_size], 0.2)
optimizer = tf.keras.optimizers.Adam(learning_rate=lr, beta_1 = 0.9, beta_2=0.999)
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
history = model.fit(X_train_mlp, Y_train, batch_size=32, epochs=30)
predictions = model.predict(X_validation_mlp, batch_size=32)
correct = tf.equal(tf.math.argmax(predictions, axis=1), tf.math.argmax(Y_validation, axis=1))
validation_accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
print("Validation Accuracy = ", float(validation_accuracy) * 100, "%")
# model.save("./models/mlp_model.h5")
n_row, n_col = 3, 3
samples = np.random.choice(len(X_validation_mlp), n_row * n_col, replace=False)
_, ax = plt.subplots(n_row, n_col)
for i in range(n_row):
for j in range(n_col):
ax[i, j].imshow(X_validation_mlp[samples[i * n_col + j]].reshape(IMG_SIZE, IMG_SIZE))
ax[i, j].label_outer()
label = "Cat" if float(tf.math.argmax(predictions[samples[i * n_col + j]])) == 0.0 else "Dog"
ax[i, j].set_title(label)
plt.show()
def simple_cnn_model(input_shape, n_conv_blocks, n_dense, n_classes, dropout_rate=0.0):
"""
Function to create an return a convolutional neural network model for image classification
:param input_shape: The image input shape
:param n_conv_blocks: The number of convolution-maxpool blocks
:param n_dense: The number of units in the dense layers
:param n_classes: The number of output classes
:param dropout_rate: The dropout rate
:return: A CNN model
"""
input_layer = tf.keras.Input(shape=input_shape)
X = input_layer
for i in range(n_conv_blocks):
X = tf.keras.layers.Conv2D(32 * 2 ** i, 5, strides=(1, 1), padding='same')(X)
X = tf.keras.layers.BatchNormalization()(X)
X = tf.keras.layers.Activation('relu')(X)
X = tf.keras.layers.Dropout(dropout_rate)(X)
X = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))(X)
X = tf.keras.layers.Flatten()(X)
for n in n_dense:
X = tf.keras.layers.Dense(n)(X)
X = tf.keras.layers.BatchNormalization()(X)
X = tf.keras.layers.Activation('relu')(X)
X = tf.keras.layers.Dropout(dropout_rate)(X)
X = tf.keras.layers.Dense(n_classes)(X)
X = tf.keras.layers.BatchNormalization()(X)
output = tf.keras.layers.Activation('softmax')(X)
model = tf.keras.Model(inputs=input_layer, outputs=output)
return model
cnn_model = simple_cnn_model((IMG_SIZE, IMG_SIZE, 1, ), 3, [500, 500], 2, 0.2)
optimizer = tf.keras.optimizers.Adam(learning_rate=lr, beta_1 = 0.9, beta_2=0.999)
cnn_model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
cnn_model.summary()
history = cnn_model.fit(X_train, Y_train, batch_size=32, epochs=10)
cnn_predictions = cnn_model.predict(X_validation, batch_size=32)
correct = tf.equal(tf.math.argmax(cnn_predictions, axis=1), tf.math.argmax(Y_validation, axis=1))
validation_accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
print("CNN Validation Accuracy = ", float(validation_accuracy) * 100, "%")
# cnn_model.save("./models/cnn_model.h5")
n_row, n_col = 3, 3
cnn_samples = np.random.choice(len(X_validation), n_row * n_col, replace=False)
_, ax = plt.subplots(n_row, n_col)
for i in range(n_row):
for j in range(n_col):
ax[i, j].imshow(X_validation[cnn_samples[i * n_col + j]].reshape(IMG_SIZE, IMG_SIZE))
ax[i, j].label_outer()
label = "Cat" if float(tf.math.argmax(predictions[cnn_samples[i * n_col + j]])) == 0.0 else "Dog"
ax[i, j].set_title(label)
plt.show()
def residual_block(X, n_conv_layers, n_filters, filter_size, dropout_rate=0.0):
"""
Function to create and return a residual block for a residual network model
:param X: The input to the block
:param n_conv_layers: The number of convolutional layers in the block
:param n_filters: The number of filters in each convolutional layer
:param filter_size: The filter size
:param dropout_rate: The dropout rate
:return: A residual block
"""
assert (n_conv_layers >= 2), "A residual block should have at least 2 layers"
X_copy = X
if X_copy.shape[-1] != n_filters:
X_copy = tf.keras.layers.Conv2D(n_filters, filter_size, padding='same', strides=(1, 1))(X_copy)
X_copy = tf.keras.layers.BatchNormalization()(X_copy)
for i in range(n_conv_layers):
X = tf.keras.layers.Conv2D(n_filters, filter_size, padding='same', strides=(1, 1))(X)
X = tf.keras.layers.BatchNormalization()(X)
if i < n_conv_layers - 1:
X = tf.keras.layers.Activation('relu')(X)
X = tf.keras.layers.Add()([X_copy, X])
X = tf.keras.layers.BatchNormalization()(X)
X = tf.keras.layers.Activation('relu')(X)
X = tf.keras.layers.Dropout(dropout_rate)(X)
return X
def simple_residual_network(input_shape, res_block_sizes, n_dense, n_classes, dropout_rate=0.0):
"""
Function to create and return a simple residual network model for image classification
:param input_shape: The input shape
:param res_block_sizes: A list containing the number of conv layers in each residual block
:param n_dense: A list containing the number of units in each fully-connected/dense layer
:param n_classes: The number of output classes
:param dropout_rate: The dropout rate
:return: A residual network model
"""
input_layer = tf.keras.layers.Input(shape=input_shape)
X = tf.keras.layers.Conv2D(64, 3, padding='same', strides=(1, 1))(input_layer)
X = tf.keras.layers.BatchNormalization()(X)
X = tf.keras.layers.Activation('relu')(X)
X = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))(X)
X = tf.keras.layers.Dropout(dropout_rate)(X)
i = 1
for res_block_size in res_block_sizes:
X = residual_block(X, res_block_size, 64 * 2 ** (i // 2), 3, dropout_rate)
if i < len(res_block_sizes) - 1:
X = tf.keras.layers.MaxPool2D(pool_size=(2, 2), strides=(2, 2))(X)
i += 1
X = tf.keras.layers.AveragePooling2D(pool_size=(2, 2), strides=(2, 2))(X)
X = tf.keras.layers.Flatten()(X)
for n in n_dense:
X = tf.keras.layers.Dense(n)(X)
X = tf.keras.layers.BatchNormalization()(X)
X = tf.keras.layers.Activation('relu')(X)
X = tf.keras.layers.Dropout(dropout_rate)(X)
X = tf.keras.layers.Dense(n_classes)(X)
X = tf.keras.layers.BatchNormalization()(X)
output = tf.keras.layers.Activation('softmax')(X)
model = tf.keras.Model(inputs=input_layer, outputs=output)
return model
resnet_model = simple_residual_network((IMG_SIZE, IMG_SIZE, 1, ), [2, 2, 2, 2], [500], 2, 0.2)
optimizer = tf.keras.optimizers.Adam(learning_rate=lr, beta_1 = 0.9, beta_2=0.999)
resnet_model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
resnet_model.summary()
history = resnet_model.fit(X_train, Y_train, batch_size=32, epochs=10)
resnet_predictions = resnet_model.predict(X_validation, batch_size=32)
correct = tf.equal(tf.math.argmax(resnet_predictions, axis=1), tf.math.argmax(Y_validation, axis=1))
validation_accuracy = tf.reduce_mean(tf.cast(correct, tf.float32))
print("Resnet Validation Accuracy = ", float(validation_accuracy) * 100, "%")
# resnet_model.save("./models/resnet_model.h5")
n_row, n_col = 3, 3
resnet_samples = np.random.choice(len(X_validation), n_row * n_col, replace=False)
_, ax = plt.subplots(n_row, n_col)
for i in range(n_row):
for j in range(n_col):
ax[i, j].imshow(X_validation[resnet_samples[i * n_col + j]].reshape(IMG_SIZE, IMG_SIZE))
ax[i, j].label_outer()
label = "Cat" if float(tf.math.argmax(predictions[resnet_samples[i * n_col + j]])) == 0.0 else "Dog"
ax[i, j].set_xlabel(label)
plt.show()