Repository navigation
Expand file tree
/
Copy pathtrain.py
More file actions
170 lines (138 loc) · 4.97 KB
/
Copy pathtrain.py
File metadata and controls
170 lines (138 loc) · 4.97 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
import cv2
import numpy as np
import os
from tqdm import tqdm
import pickle
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
from keras.utils.np_utils import to_categorical
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import Adam
from keras.layers import Dropout,Flatten
from keras.layers.convolutional import Conv2D,MaxPooling2D
print('Program Started...')
############################
path = "myData"
test_ratio = 0.2
validation_ratio = 0.2
imageDimensions = (32,32,3)
############################
myList = os.listdir(path)
print(f"Total No of Classes : {len(myList)}")
noOfClasses = len(myList)
images = []
classNo = []
for x in range(0,noOfClasses):
myPicList = os.listdir(path + "/" + str(x) )
for y in tqdm(myPicList):
curImg = cv2.imread(path + "/" + str(x) + "/" + str(y))
curImg = cv2.resize(curImg , (imageDimensions[0] , imageDimensions[1]))
images.append(curImg)
classNo.append(x)
print(x)
print(len(images))
print(len(classNo))
images = np.array(images)
classNo = np.array(classNo)
print(images.shape)
#### Splitting the Data
X_train , X_test , y_train , y_test = train_test_split(images , classNo , test_size=test_ratio )
X_train , X_validation , y_train , y_validation = train_test_split(X_train , y_train , test_size=validation_ratio)
print(X_train.shape)
print(X_test.shape)
print(X_validation.shape)
numberOfSamples = []
for x in range(0,noOfClasses):
print( len(np.where(y_train==x)[0]) )
numberOfSamples.append( len(np.where(y_train==x)[0]) )
print(numberOfSamples)
plt.figure(figsize = (10,5))
plt.bar(range(0,noOfClasses) , numberOfSamples)
plt.title("No of images for each class")
plt.xlabel("class Id")
plt.ylabel("number.of images")
plt.show()
def preProcessing(img):
img = cv2.cvtColor(img , cv2.COLOR_BGR2GRAY)
img = cv2.equalizeHist(img)
img = img/255
return img
X_train = np.array(list(map(preProcessing , X_train)))
X_test = np.array(list(map(preProcessing , X_test)))
X_validation = np.array(list(map(preProcessing , X_validation)))
X_train = X_train.reshape(X_train.shape[0],X_train.shape[1],X_train.shape[2],1)
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)
print(X_train.shape)
# img = X_train[30]
# print(img.shape)
# img = cv2.resize(img , (300,300))
# cv2.imshow("preProcessed image" , img)
# cv2.waitKey(0)
#### IMAGE AUGMENTATION
dataGen = ImageDataGenerator(width_shift_range=0.1,
height_shift_range=0.1,
zoom_range=0.2,
shear_range=0.1,
rotation_range=10)
dataGen.fit(X_train)
y_train = to_categorical(y_train,noOfClasses)
y_test = to_categorical(y_test,noOfClasses)
y_validation = to_categorical(y_validation,noOfClasses)
def myModel():
noOfFilters = 60
sizeOfFilter1 = (5,5)
sizeOfFilter2 = (3, 3)
sizeOfPool = (2,2)
noOfNodes= 500
model = Sequential()
model.add((Conv2D(noOfFilters,sizeOfFilter1,input_shape=(imageDimensions[0],
imageDimensions[1],1),activation='relu')))
model.add((Conv2D(noOfFilters, sizeOfFilter1, activation='relu')))
model.add(MaxPooling2D(pool_size=sizeOfPool))
model.add((Conv2D(noOfFilters//2, sizeOfFilter2, activation='relu')))
model.add((Conv2D(noOfFilters//2, sizeOfFilter2, activation='relu')))
model.add(MaxPooling2D(pool_size=sizeOfPool))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(noOfNodes,activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(noOfClasses, activation='softmax'))
model.compile(Adam(lr=0.001),loss='categorical_crossentropy',metrics=['accuracy'])
return model
model = myModel()
print(model.summary())
batchSizeVal = 50
epochsVal = 2
stepsPerEpochVal = 2000
history = model.fit_generator(dataGen.flow(X_train,y_train,
batch_size=batchSizeVal),
steps_per_epoch=stepsPerEpochVal,
epochs=epochsVal,
validation_data=(X_validation,y_validation),
shuffle=1)
#### PLOT THE RESULTS
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()
#### EVALUATE USING TEST IMAGES
score = model.evaluate(X_test,y_test,verbose=0)
print('Test Score = ',score[0])
print('Test Accuracy =', score[1])
#### SAVE THE TRAINED MODEL
model.save('model.h5')
# pickle_out= open("model_trained.p", "wb")
# pickle.dump(model,pickle_out)
# pickle_out.close()