-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcnn.py
More file actions
115 lines (89 loc) · 3.18 KB
/
Copy pathcnn.py
File metadata and controls
115 lines (89 loc) · 3.18 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
import os
import cv2
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from keras.layers import Dense, Dropout, Flatten, MaxPooling2D,AveragePooling2D
from keras.layers.convolutional import Conv2D
from keras.models import Sequential
from keras.optimizers import Adam
from keras.utils.np_utils import to_categorical
from sklearn.model_selection import train_test_split
################
path = 'myData'
################
images = []
classNo = []
myList = os.listdir(path)
noOfClasses = len(myList)
for x in range(0, noOfClasses):
myPicList = os.listdir(path + '/' + str(x))
for y in myPicList:
curImg = cv2.imread(path + '/' + str(x) + '/' + y)
curImg = cv2.resize(curImg, (32, 32))
images.append(curImg)
classNo.append(x)
print(x, end=" ")
images = np.array(images)
classNo = np.array(classNo)
X_train_full, X_test, y_train_full, y_test = train_test_split(images, classNo)
X_train, X_val, y_train, y_val = train_test_split(X_train_full, y_train_full)
noOfSamples = []
for x in range(0, noOfClasses):
noOfSamples.append(len(np.where(y_train == x)[0]))
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_val = np.array(list(map(preProcessing, X_val)))
print(X_train.shape)
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_val = X_val.reshape(X_val.shape[0], X_val.shape[1], X_val.shape[2], 1)
print(X_train.shape)
y_train = to_categorical(y_train, noOfClasses)
y_test = to_categorical(y_test, noOfClasses)
y_val = to_categorical(y_val, noOfClasses)
print(y_train.shape)
def myModel():
noOfFilters = 60
sizeOfFilter1 = (5, 5)
sizeOfFilter2 = (3, 3)
sizeOfPool = (2, 2)
noOfNodes = 500
model = Sequential()
model.add((Conv2D(6, sizeOfFilter1, input_shape=(32, 32, 1), activation='relu')))
model.add(MaxPooling2D(pool_size=sizeOfPool))
model.add((Conv2D(16, sizeOfFilter1, activation='relu')))
model.add(MaxPooling2D(pool_size=sizeOfPool))
model.add(Flatten())
model.add(Dense(120,activation='relu'))
model.add(Dense(84, activation='relu'))
model.add(Dropout(0.4))
model.add(Dense(noOfClasses, activation='softmax'))
model.compile(Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])
return model
model = myModel()
print(model.summary())
history = model.fit(X_train,y_train,validation_data=(X_val,y_val),epochs=5,steps_per_epoch=1000)
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()
score = model.evaluate(X_test,y_test,verbose=0)
print('Test Score=', score[0])
print('Test Accuracy=', score[1])
export_path = 'model'
tf.saved_model.save(model, export_path)