-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_CNN.py
More file actions
330 lines (240 loc) · 9.71 KB
/
model_CNN.py
File metadata and controls
330 lines (240 loc) · 9.71 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
319
320
321
322
323
324
325
326
327
328
329
330
# -*- coding: utf-8 -*-
"""
Created on Thu Jan 7 19:11:21 2021
@author: srcdo
"""
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution2D, MaxPooling2D
from tensorflow.keras import optimizers
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.model_selection import train_test_split
#from Alexnet import AlexNet
# input image dimensions
img_rows, img_cols = 64, 64
# number of channels
img_channels = 3
#%%
# data
path1 = 'E:/road_pathole/training/0' #path of folder of images
path2 = 'E:/road_pathole/training_set/0' #path of folder to save images
listing = os.listdir(path1)
num_samples=size(listing)
print(num_samples)
for file in listing:
im = Image.open(path1 + '\\' + file)
img = im.resize((img_rows,img_cols))
gray = img.convert(mode='RGB')
#need to do some more processing here
gray.save(path2 +'\\' + file, "png")
imlist = os.listdir(path2)
im1 = array(Image.open('COVID/2/' + imlist[0])) # open one image to get size
m,n = im1.shape[0:2] # get the size of the images
imnbr = len(imlist) # get the number of images
# # create matrix to store all flattened images
# immatrix = array([array(Image.open('input_data_resized/'+ im2)).flatten()
# for im2 in imlist],'f')
# label=np.ones((num_samples,),dtype = int)
# label[0:245]=0
# label[245:288]=1
# size(label)
# data,Label = shuffle(immatrix,label, random_state=2)
# train_data = [data,Label]
# img=immatrix[439].reshape(img_rows,img_cols)
# # 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 = 7
# # number of epochs to train
# nb_epoch = 100
# # number of convolutional filters to use
# nb_filters = 64
# # 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)
# X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols , 1)
# X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols , 1)
# 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 = 20
# #plt.imshow(X_train[i, 0], interpolation='nearest')
# print("label : ", Y_train[i,:])
# #%%
# model = Sequential()
# # #model.add(Convolution2D(nb_filters, nb_conv, nb_conv, padding='same',input_shape=(1, img_rows, img_cols)))
# # model.add(Convolution2D(64,3, 3, strides=(4, 4), padding='same',input_shape=(1, img_rows, img_cols), activation='relu'))
# # 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')
# # model.add(Conv2D(
# # 96, 11, strides=(4, 4), input_shape=(IMAGE_SIZE, IMAGE_SIZE, CHANNELS), activation='relu'))
# # model.add(MaxPooling2D(pool_size=(3, 3), strides=2))
# # model.add(BatchNormalization())
# # # Second Convolution Block
# # model.add(Conv2D(256, 5, strides=(1, 1),
# # activation='relu', padding='same'))
# # model.add(MaxPooling2D(pool_size=(3, 3), strides=2))
# # model.add(BatchNormalization())
# # # Third Convolution Block
# # model.add(Conv2D(384, 3, strides=(1, 1),
# # activation='relu', padding='same'))
# # model.add(MaxPooling2D(pool_size=(3, 3), strides=2))
# # # Fourth Convolution Block
# # model.add(Conv2D(384, 3, strides=(1, 1),
# # activation='relu', padding='same'))
# # # Fifth Convolution Block
# # model.add(Conv2D(256, 3, strides=(1, 1),
# # activation='relu', padding='same'))
# # model.add(Dropout(0.5))
# # # Fully connected layer
# # model.add(Flatten())
# # # First hidden unit
# # model.add(Dense(512, activation='relu', kernel_initializer='uniform'))
# # model.add(Dropout(0.5))
# # # Second hidden unit
# # model.add(Dense(512, activation='relu', kernel_initializer='uniform'))
# # model.add(Dropout(0.5))
# # # Output layer
# # model.add(Dense(CLASSES, activation='softmax',
# # kernel_initializer='uniform'))
# # model.summary()
# LEARN_RATE = 1.0e-4
# # Model Architecture and Compilation
# model = AlexNet(7, 200, 1)
# adam = Adam(lr=LEARN_RATE, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.0)
# model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
# from keras.callbacks import ModelCheckpoint
# from keras import callbacks
# filename='model_train_new.csv'
# csv_log=callbacks.CSVLogger(filename, separator=',', append=False)
# early_stopping=callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='min')
# checkpoint = ModelCheckpoint('best_model.h5', monitor='val_loss', save_best_only=True, mode='auto')
# #steps_per_epoch = int(len(Y_train) / BATCH_SIZE) # 300
# # validation_steps = int(len(Y_test) / BATCH_SIZE) # 90
# callbacks_list = [csv_log,early_stopping,checkpoint]
# #%%
# hist = model.fit(X_train, Y_train, batch_size=batch_size, epochs=100,
# verbose=1, validation_data=(X_test, Y_test))
# model.save('best_model.h5')
# model.save('best_model_CSV.csv')
# # hist = model.fit(X_train, Y_train, batch_size=batch_size, epochs=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['accuracy']
# val_acc=hist.history['val_accuracy']
# 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, verbose=0)
# print('Test score:', score[0])
# print('Test accuracy:', score[1])
# print(model.predict_classes(X_test[1:20]))
# print(Y_test[1:20])
# #%%
# # 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(X_test) # to predict probability
# target_names = ['class 0(Normal)',
# 'class 1(Abnormal_Agenesis_of_the_Corpus_Callosum)',
# 'class 2(Abnormal_Agenesis_of_the_Septi_Pellucidi)',
# 'class 3(Abnormal_Cerebellar_Hypoplasia)',
# 'class 4(Abnormal_Dandy_Walker_VariantMalformation)',
# 'class 5(Abnormal_Megacisterna_Magna)',
# 'class 6(Abnormal_Venous_Malformation)']
# size(target_names)
# 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.h5"
# model.save_weights(fname,overwrite=True)
# # Loading weights
# fname = "weights-Test-CNN.h5"
# model.load_weights(fname)