-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_cnn.py
205 lines (178 loc) · 7.22 KB
/
image_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
# -*- coding: utf-8 -*-
"""image_cnn.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/19GW6Kq0aOzx38Ea__HK1PhcH_2hJ3q9s
"""
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import cifar10
from keras.utils import np_utils
from keras import backend as K
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, BatchNormalization
from keras.callbacks import ModelCheckpoint, EarlyStopping, ReduceLROnPlateau, LearningRateScheduler
from keras.constraints import maxnorm
from keras import regularizers
from keras import optimizers
from keras.layers.convolutional import Conv2D
from keras.layers.convolutional import MaxPooling2D
from keras.utils import np_utils
# Here we just make sure the image format is as desired. This will make the feature (x)
# data - i.e. the RGB pixel values - for each image have the shape 3x32x32.
if K.backend()=='tensorflow':
K.tensorflow_backend.set_image_dim_ordering('th')
def myGetModel(CIF):
"""Specify the CNN architecture"""
weight_decay = 1e-4
model = Sequential()
#FEATURE DETECTION
#First layer
model.add(Conv2D(32, kernel_size=(3,3),
input_shape=(3,32,32),
kernel_regularizer=regularizers.l2(weight_decay),
padding = 'same',
activation = 'relu'))
model.add(BatchNormalization())
model.add(Dropout(0.2))
#Second layer
model.add(Conv2D(32, kernel_size=(3,3),
padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay),
activation = 'relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(BatchNormalization())
model.add(Dropout(0.25))
#Third layer
model.add(Conv2D(64, kernel_size=(3,3),
padding = 'same',
kernel_regularizer=regularizers.l2(weight_decay),
activation = 'relu'))
model.add(BatchNormalization())
model.add(Dropout(0.3))
#Fourth layer
model.add(Conv2D(64,kernel_size=(3,3),
padding = 'valid',
kernel_regularizer=regularizers.l2(weight_decay),
activation = 'relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(BatchNormalization())
model.add(Dropout(0.4))
model.add(Flatten())
#CLASSIFICATION
#Fully connected layer
model.add(Dense(512, activation = 'relu'))
model.add(Dropout(0.5))
#Output layer w/ softmax
model.add(Dense(CIF.num_classes, activation='softmax'))
#Compile the mode
opt_rms = optimizers.RMSprop(lr=0.001, rho=0.9, epsilon=1e-6, decay=1e-6)
model.compile(loss='categorical_crossentropy', optimizer=opt_rms, metrics=['acc'])
#model.summary()
return model
def lr_schedule(epoch):
lrate = 0.001
if epoch > 75:
lrate = 0.0005
if epoch > 100:
lrate = 0.0003
return lrate
def myFitModel(model, CIF):
"""Fit the model to data"""
early = EarlyStopping(monitor='val_loss',
min_delta=0,
patience=10,
mode='auto')
checkpointer = ModelCheckpoint(filepath='/tmp/weights-best.hdf5',
save_best_only=True)
reduce_lr = ReduceLROnPlateau(monitor='val_loss',
factor=0.1,
patience=5,
min_lr=1e-8)
model.fit(CIF.x_train, CIF.y_train,
batch_size=128,
epochs=100,
validation_data=(CIF.x_valid, CIF.y_valid),
callbacks=[LearningRateScheduler(lr_schedule), checkpointer, reduce_lr, early])
model.load_weights('/tmp/weights-best.hdf5')
return model
def runImageClassification(getModel=None,fitModel=None,seed=7):
# Fetch data. You may need to be connected to the internet the first time this is done.
# After the first time, it should be available in your system. On the off chance this
# is not the case on your system and you find yourself repeatedly downloading the data,
# you should change this code so you can load the data once and pass it to this function.
print("Preparing data...")
data=CIFAR(seed)
# Create model
print("Creating model...")
model=getModel(data)
# Fit model
print("Fitting model...")
model=fitModel(model,data)
# Evaluate on test data
print("Evaluating model...")
score = model.evaluate(data.x_test, data.y_test, verbose=0)
print('Test accuracy:', score[1])
class CIFAR:
def __init__(self,seed=0):
# Get and split data
data = self.__getData(seed)
self.x_train_raw=data[0][0]
self.y_train_raw=data[0][1]
self.x_valid_raw=data[1][0]
self.y_valid_raw=data[1][1]
self.x_test_raw=data[2][0]
self.y_test_raw=data[2][1]
# Record input/output dimensions
self.num_classes=10
self.input_dim=self.x_train_raw.shape[1:]
# Convert data
self.y_train = np_utils.to_categorical(self.y_train_raw, self.num_classes)
self.y_valid = np_utils.to_categorical(self.y_valid_raw, self.num_classes)
self.y_test = np_utils.to_categorical(self.y_test_raw, self.num_classes)
self.x_train = self.x_train_raw.astype('float32')
self.x_valid = self.x_valid_raw.astype('float32')
self.x_test = self.x_test_raw.astype('float32')
self.x_train /= 255
self.x_valid /= 255
self.x_test /= 255
# Class names
self.class_names=['airplane','automobile','bird','cat','deer',
'dog','frog','horse','ship','truck']
def __getData (self,seed=0):
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
return self.__shuffleData(x_train,y_train,x_test,y_test,seed)
def __shuffleData (self,x_train,y_train,x_test,y_test,seed=0):
tr_perc=.75
va_perc=.15
x=np.concatenate((x_train,x_test))
y=np.concatenate((y_train,y_test))
np.random.seed(seed)
np.random.shuffle(x)
np.random.seed(seed)
np.random.shuffle(y)
indices = np.random.permutation(len(x))
tr=round(len(x)*tr_perc)
va=round(len(x)*va_perc)
self.tr_indices=indices[0:tr]
self.va_indices=indices[tr:(tr+va)]
self.te_indices=indices[(tr+va):len(x)]
x_tr=x[self.tr_indices,]
x_va=x[self.va_indices,]
x_te=x[self.te_indices,]
y_tr=y[self.tr_indices,]
y_va=y[self.va_indices,]
y_te=y[self.te_indices,]
return ((x_tr,y_tr),(x_va,y_va),(x_te,y_te))
# Print figure with 10 random images, one from each class
def showImages(self):
fig = plt.figure(figsize=(8,3))
for i in range(self.num_classes):
ax = fig.add_subplot(2, 5, 1 + i, xticks=[], yticks=[])
idx = np.where(self.y_valid_raw[:]==i)[0]
features_idx = self.x_valid_raw[idx,::]
img_num = np.random.randint(features_idx.shape[0])
im = np.transpose(features_idx[img_num,::],(1,2,0))
ax.set_title(self.class_names[i])
plt.imshow(im)
plt.show()