-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCNN_Training.py
237 lines (194 loc) · 8.78 KB
/
CNN_Training.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
import cv2
import keras
import numpy as np
import os
import pickle
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
# from sklearn.externals
import joblib
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
# from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, BatchNormalization, MaxPool2D, Conv2D
from keras.optimizers import Adam
# from keras.layers.convolutional import Conv2D, MaxPool2D
from tensorflow.python.keras.callbacks import LearningRateScheduler
################################################################################################
path = 'data'
images = []
test_ratio = 0.2
val_ratio = 0.2
class_number = []
no_of_samples = []
image_Dimension = [32, 32]
batchsize_value = 50
epochs_value = 20
steps_per_epoch_value = 2000
HD='Hierarchical_data/trained_CNN_model.h5'
################################################################################################
_list_ = os.listdir(path)
print(len(_list_))
no_of_class = len(_list_)
################################################################################################
# Importing the folders as class
################################################################################################
for x in range(0, no_of_class):
picture_list = os.listdir(path + "/Number-" + str(x))
for y in picture_list:
current_image = cv2.imread(path + "/Number-" + str(x) + "/" + y)
# print(path+"/Number-"+str(x)+"/"+y)
current_image = cv2.resize(current_image, (32, 32))
images.append(current_image)
class_number.append(x)
print(x, end=" ")
print(" ")
# print(len(images)) = print(len(images))
################################################################################################
# Convert into numpy array
################################################################################################
images = np.array(images)
class_number = np.array(class_number)
print(images.shape)
# print(class_number.shape)
################################################################################################
# Spliting the data
# 20%testing and 80%training
################################################################################################
x_train, x_test, y_train, y_test = train_test_split(images, class_number, test_size=test_ratio,random_state=2)
x_train, x_validation, y_train, y_validation = train_test_split(x_train, y_train, test_size=val_ratio,random_state=2)
print(x_train.shape)
print(x_test.shape)
print(x_validation.shape)
# x_train contains images and y_train contains ids
# as the numbers(0-9) is 10
for x in range(0, no_of_class):
# print(len(np.where(y_train==x)[0]))
no_of_samples.append(len(np.where(y_train == x)[0]))
print(no_of_samples)
plt.figure(figsize=(10, 5))
plt.bar(range(0, no_of_class), no_of_samples)
plt.title("Number of Images in each Class")
plt.xlabel("Class ID")
plt.ylabel("Number of Images")
plt.show()
################################################################################################
# Preprocessing the Image
################################################################################################
def preProcessing(img):
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.equalizeHist(img)
img = img / 255 #normalization - brings to 0 ... 1
return img
# img = preProcessing(x_train[30])
# img=cv2.resize(img,(300,300))
# cv2.imshow("PreProcessed",img)
# cv2.waitKey(0)
# x_train=np.array(lists(map(preProcessing,x_train)))
x_train = np.array(list(map(preProcessing, x_train)))
# img = x_train[30]
# img=cv2.resize(img,(300,300))
# cv2.imshow("PreProcessed",img)
# cv2.waitKey(0)
# print(x_train[30].shape)
x_test = np.array(list(map(preProcessing, x_test)))
x_validation = np.array(list(map(preProcessing, x_validation)))
print(x_train.shape)
x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], x_train.shape[2], 1)
# print(x_train.shape)
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], x_test.shape[2], 1)
x_validation = x_validation.reshape(x_validation.shape[0], x_validation.shape[1], x_validation.shape[2], 1)
################################################################################################
# Convolutional Neural Networks
################################################################################################
def CNNmodel():
no_of_filters = 60
size_of_filter = (5, 5)
size_of_filter2 = (3, 3)
size_of_pool = (2, 2)
no_of_node = 512
model = Sequential()
model.add((Conv2D(no_of_filters, size_of_filter, input_shape=(32, 32, 1), activation='relu')))
model.add(BatchNormalization())
model.add((Conv2D(no_of_filters, size_of_filter, activation='relu')))
model.add(BatchNormalization())
model.add(MaxPool2D(strides=size_of_pool))
model.add(Dropout(0.25))
model.add((Conv2D(no_of_filters * 2, size_of_filter2, activation='relu')))
model.add(BatchNormalization())
model.add((Conv2D(no_of_filters * 2, size_of_filter2, activation='relu')))
model.add(BatchNormalization())
model.add(MaxPool2D(strides=size_of_pool))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(no_of_node, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(no_of_node * 2, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
optimiser=Adam(lr=1e-4)
model.compile(optimizer=optimiser, loss='categorical_crossentropy', metrics=['accuracy'])
return model
model = CNNmodel()
print(model.summary())
################################################################################################
# Augment part
################################################################################################
data_Generator = ImageDataGenerator(width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.1,
shear_range=0.1,
rotation_range=10)
data_Generator.fit(x_train)
y_train = to_categorical(y_train, no_of_class)
y_test = to_categorical(y_test, no_of_class)
y_validation = to_categorical(y_validation, no_of_class)
################################################################################################
# history=model.fit_generator(data_Generator.flow(x_train,y_train,batch_size=batchsize_value),
# steps_per_epoch=steps_per_epoch_value,
# epochs=epochs_value,
# validation_data=(x_validation,y_validation),
# shuffle=1)
annealer = LearningRateScheduler(lambda x: 1e-3 * 0.9 ** x)
history = model.fit(data_Generator.flow(x_train, y_train, batch_size=batchsize_value), steps_per_epoch=x_train.shape[0] // batchsize_value,
epochs=epochs_value, validation_data=(x_validation, y_validation), shuffle=True, callbacks=[annealer], verbose=2)
#print(history)
################################################################################################
# ploting the accuracy and loss graph
################################################################################################
plt.figure(1)
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.legend(['training', 'validation'])
plt.title('Loss')
plt.xlabel('Epoch')
plt.figure(2)
plt.plot(history.history['accuracy'])
plt.plot(history.history['val_accuracy'])
plt.legend(['training', 'validation'])
plt.title('Accuracy')
plt.xlabel('Epoch')
plt.show()
################################################################################################
# Evaluating the test results
################################################################################################
score = model.evaluate(x_test, y_test, verbose=0)
print('Test score: ', score[0])
print('Test Accuracy: ', score[1])
#im=images.reshape((1,32,32,1))
#prediction=model.predict_classes(images)
#print(prediction[137])
################################################################################################
# Storing as h5py object
################################################################################################
model.save('HD')
################################################################################################
#Other alternative for storing the models
################################################################################################
# with open('HD','bw') as pickle_out:
# pickle.dump(model,pickle_out)
# # pickle_out.close()
#
# # joblib.dump(model,'model_trained.pkl')
################################################################################################