-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNN.py
286 lines (213 loc) · 6.92 KB
/
CNN.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
#KERAS
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from keras.optimizers import SGD,RMSprop,adam
from keras.utils import np_utils
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
import os
import theano
from PIL import Image
from numpy import *
# SKLEARN
from sklearn.utils import shuffle
from sklearn.cross_validation import train_test_split
# input image dimensions
img_rows, img_cols = 64, 64
# number of channels
img_channels = 1
#%%
# data
folder = "greyscale"
folder = "greyscalesave"
path1 = os.path.join(os.getcwd(), folder) #path of folder of images
path2 = os.path.join(os.getcwd(), foldersave) #path of folder to save images
listing = os.listdir(path1)
num_samples=size(listing)
print (num_samples)
for file in listing:
img = Image.open(path1 + '\\' + file)
# img = im.resize((img_rows,img_cols))
gray = img.convert('L')
#need to do some more processing here
gray.save(path2 +'\\' + file, "JPEG")
graylist = os.listdir(path2)
im1 = array(Image.open('E:\CI\dataset\gray' + '\\'+ graylist[0])) # open one image to get size
m,n = im1.shape[0:2] # get the size of the images
imnbr = len(graylist) # get the number of images
# create matrix to store all flattened images
immatrix = array([array(Image.open( path2 + '\\' + im2)).flatten()
for im2 in graylist],'f')
label=np.ones((num_samples,),dtype = str)
label[0:499]=1
label[500:999]=2
label[1000:1499]=3
label[1500:1999]=4
label[2000:2499]=5
label[2500:2999]=6
label[3000:3499]=7
label[3500:3999]=8
label[4000:4499]=9
label[4500:4999]='A'
label[5000:5499]='B'
label[5500:5999]='C'
label[6000:6499]='D'
label[6500:6999]='E'
label[7000:7499]='F'
label[7500:7999]='G'
label[8000:8499]='H'
label[8500:8999]='I'
label[9000:9499]='K'
label[9500:9999]='L'
label[10000:10499]='M'
label[10500:10999]='N'
label[11000:11499]='O'
label[11500:11999]='P'
label[12000:12499]='Q'
label[12500:12999]='R'
label[13000:13499]='S'
label[13500:13999]='T'
label[14000:14499]='U'
label[14500:14999]='V'
label[15000:15499]='W'
label[15500:15999]='X'
label[16000:16499]='Y'
data,Label = shuffle(immatrix,label, random_state=2)
train_data = [data,Label]
img=immatrix[167].reshape(151,151)
plt.imshow(img)
plt.imshow(img,cmap='gray')
print (train_data[0].shape)
print (train_data[1].shape)
#%%
#batch_size to train
batch_size = 32
# number of output classes
nb_classes = 3
# number of epochs to train
nb_epoch = 20
# number of convolutional filters to use
nb_filters = 32
# size of pooling area for max pooling
nb_pool = 2
# convolution kernel size
nb_conv = 3
#%%
(X, y) = (train_data[0],train_data[1])
# STEP 1: split X and y into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=4)
X_train = X_train.reshape(X_train.shape[0], 1, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
i = 100
plt.imshow(X_train[i, 0], interpolation='nearest')
print("label : ", Y_train[i,:])
#%%
model = Sequential()
model.add(Convolution2D(nb_filters, nb_conv, nb_conv,
border_mode='valid',
input_shape=(1, img_rows, img_cols)))
convout1 = Activation('relu')
model.add(convout1)
model.add(Convolution2D(nb_filters, nb_conv, nb_conv))
convout2 = Activation('relu')
model.add(convout2)
model.add(MaxPooling2D(pool_size=(nb_pool, nb_pool)))
model.add(Dropout(0.5))
model.add(Flatten())
model.add(Dense(128))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy', optimizer='adadelta')
#%%
hist = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
show_accuracy=True, verbose=1, validation_data=(X_test, Y_test))
hist = model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch,
show_accuracy=True, verbose=1, validation_split=0.2)
# visualizing losses and accuracy
train_loss=hist.history['loss']
val_loss=hist.history['val_loss']
train_acc=hist.history['acc']
val_acc=hist.history['val_acc']
xc=range(nb_epoch)
plt.figure(1,figsize=(7,5))
plt.plot(xc,train_loss)
plt.plot(xc,val_loss)
plt.xlabel('num of Epochs')
plt.ylabel('loss')
plt.title('train_loss vs val_loss')
plt.grid(True)
plt.legend(['train','val'])
print (plt.style.available) # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])
plt.figure(2,figsize=(7,5))
plt.plot(xc,train_acc)
plt.plot(xc,val_acc)
plt.xlabel('num of Epochs')
plt.ylabel('accuracy')
plt.title('train_acc vs val_acc')
plt.grid(True)
plt.legend(['train','val'],loc=4)
#print plt.style.available # use bmh, classic,ggplot for big pictures
plt.style.use(['classic'])
#%%
score = model.evaluate(X_test, Y_test, show_accuracy=True, verbose=0)
print('Test score:', score[0])
print('Test accuracy:', score[1])
print(model.predict_classes(X_test[1:5]))
print(Y_test[1:5])
#%%
# visualizing intermediate layers
output_layer = model.layers[1].get_output()
output_fn = theano.function([model.layers[0].get_input()], output_layer)
# the input image
input_image=X_train[0:1,:,:,:]
print(input_image.shape)
plt.imshow(input_image[0,0,:,:],cmap ='gray')
plt.imshow(input_image[0,0,:,:])
output_image = output_fn(input_image)
print(output_image.shape)
# Rearrange dimension so we can plot the result
output_image = np.rollaxis(np.rollaxis(output_image, 3, 1), 3, 1)
print(output_image.shape)
fig=plt.figure(figsize=(8,8))
for i in range(32):
ax = fig.add_subplot(6, 6, i+1)
#ax.imshow(output_image[0,:,:,i],interpolation='nearest' ) #to see the first filter
ax.imshow(output_image[0,:,:,i],cmap=matplotlib.cm.gray)
plt.xticks(np.array([]))
plt.yticks(np.array([]))
plt.tight_layout()
plt
# Confusion Matrix
from sklearn.metrics import classification_report,confusion_matrix
Y_pred = model.predict(X_test)
print(Y_pred)
y_pred = np.argmax(Y_pred, axis=1)
print(y_pred)
# (or)
y_pred = model.predict_classes(X_test)
print(y_pred)
p=model.predict_proba(X_test) # to predict probability
target_names = ['class 0(BIKES)', 'class 1(CARS)', 'class 2(HORSES)']
print(classification_report(np.argmax(Y_test,axis=1), y_pred,target_names=target_names))
print(confusion_matrix(np.argmax(Y_test,axis=1), y_pred))
# saving weights
fname = "weights-Test-CNN.hdf5"
model.save_weights(fname,overwrite=True)
# Loading weights
fname = "weights-Test-CNN.hdf5"
model.load_weights(fname)