-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcifar100.py
237 lines (170 loc) · 6.46 KB
/
cifar100.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
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 28 20:42:34 2017
@author: rojod
"""
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 20 20:03:50 2017
@author: rojod
"""
import numpy as np
import matplotlib.pyplot as plt
import keras
import utils.utils as ut
import os
from keras.models import Sequential
from keras.datasets import cifar100
#from keras.layers import Dense, Dropout, Activation, Flatten
from keras.optimizers import Adam
from keras.utils.np_utils import to_categorical
from keras.callbacks import EarlyStopping, ReduceLROnPlateau
from keras.preprocessing.image import ImageDataGenerator
from keras.layers import *
from keras.regularizers import l2
# ---------------------------------------------------------
# Load and preprocess data
# ---------------------------------------------------------
(x_train, y_train), (x_test, y_test) = cifar100.load_data()
X_train=x_train
X_test=x_test
X_train, X_valid = np.split(x_train, [-7500])
y_train, y_valid = np.split(y_train, [-7500])
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# channel-wise standard normalization
mX = np.mean(x_train, axis=(0, 1, 2))
sX = np.std(x_train, axis=(0, 1, 2))
X_train = (X_train - mX) / sX
X_valid = (X_valid - mX) / sX
X_test = (X_test - mX) / sX
print( y_train.shape)
#y
Y_train = to_categorical(y_train, 100)
Y_valid = to_categorical(y_valid, 100)
Y_test = to_categorical(y_test, 100)
print( X_train.shape)
input1 = Input(shape=(32, 32, 3))
###############################################################
#residual unit, the holy grail :)#
def residual_unit(z0, n,drop=0.0):
zstart = Conv2D(n, (1, 1),padding="same",activation='elu')(z0)
Dropout(drop)(zstart)
BatchNormalization(gamma_regularizer=l2(1E-4),
beta_regularizer=l2(1E-4))(zstart)
z = Conv2D(n, (3, 3),padding="same",activation='elu')(zstart)
Dropout(drop)(z)
BatchNormalization(gamma_regularizer=l2(1E-4),
beta_regularizer=l2(1E-4))(z)
z = Conv2D(n, (3, 3),padding="same")(z)
Dropout(drop)(z)
z = add([z,zstart])
Dropout(drop)(z)
return Activation("elu")(z)
###########################################################
z0 = Conv2D(16, (3, 3),activation='elu')( input1 )
Dropout(0.75)(z0)
z0 = Conv2D(16, (2, 2),activation='elu')( z0 )
Dropout(0.75)(z0)
#PATH 1
z = MaxPooling2D((2, 2), strides=(2, 2))(z0)
z = Conv2D(16, (2, 2),activation='elu')( z )
Dropout(0.75)(z)
z=residual_unit(z,32,drop=0.75)
z = MaxPooling2D((2, 2), strides=(1, 1))(z)
z=residual_unit(z,32,drop=0.75)
BatchNormalization(gamma_regularizer=l2(1E-4),
beta_regularizer=l2(1E-4))(z)
z = MaxPooling2D((2, 2), strides=(1, 1))(z)
z1=residual_unit(z,32,drop=0.75)
z=concatenate([z, z1])
z1=residual_unit(z,64,drop=0.75)
z=concatenate([z, z1])
BatchNormalization(gamma_regularizer=l2(1E-4),
beta_regularizer=l2(1E-4))(z)
#Concatenate them
z = MaxPooling2D((4, 4), strides=(2, 2))(z)
Dropout(0.75)(z)
z=residual_unit(z,128,drop=0.75)
z=Flatten()(z)
output=Dense(100, activation= "softmax")(z)
model = keras.models.Model(inputs=input1, outputs=output)
model.summary()
model.compile(
loss='categorical_crossentropy',
optimizer="adam",
metrics=["accuracy"])
# data augmentation, what does this do?
generator = ImageDataGenerator(
width_shift_range=4. / 32,
height_shift_range=4. / 32,
fill_mode='constant',
horizontal_flip=True,
rotation_range=5.)
batch_size = 50
steps_per_epoch = len(X_train) // batch_size
# fit using augmented data generator
fit=model.fit_generator(
generator.flow(X_train, Y_train, batch_size=batch_size),
steps_per_epoch=steps_per_epoch,
epochs=200,
validation_data=(X_valid, Y_valid), # fit_generator doesn't work with validation_split
verbose=2,
callbacks=[ReduceLROnPlateau(monitor='val_loss', factor=2. / 3, patience=5, verbose=1),
EarlyStopping(monitor='val_loss', patience=10)])
# ----------------------------------------------
# Some plots
# ----------------------------------------------
# predicted probabilities for the test set
[loss, accuracy] = model.evaluate(X_test, Y_test, verbose=0)
Yp = model.predict(X_test)
yp = np.argmax(Yp, axis=1)
folder = 'results/cifar100/'
if not os.path.exists(folder):
os.makedirs(folder)
"""
wrong order...
cifar100_classes = np.array(["beaver", "dolphin", "otter", "seal", "whale",
"aquarium fish", "flatfish", "ray", "shark", "trout",
"orchids", "poppies", "roses", "sunflowers", "tulips",
"bottles", "bowls", "cans", "cups", "plates",
"apples", "mushrooms", "oranges", "pears", "sweet peppers",
"clock", "computer keyboard", "lamp", "telephone", "television",
"bed", "chair", "couch", "table", "wardrobe",
"bee", "beetle", "butterfly", "caterpillar", "cockroach",
"bear", "leopard", "lion", "tiger", "wolf",
"bridge", "castle", "house", "road", "skyscraper",
"cloud", "forest", "mountain", "plain", "sea",
"camel", "cattle", "chimpanzee", "elephant", "kangaroo",
"fox", "porcupine", "possum", "raccoon", "skunk",
"crab", "lobster", "snail", "spider", "worm",
"baby", "boy", "girl", "man", "woman",
"crocodile", "dinosaur", "lizard", "snake", "turtle",
"hamster", "mouse", "rabbit", "shrew", "squirrel",
"maple", "oak", "palm", "pine", "willow",
"bicycle", "bus", "motorcycle", "pickup truck", "train",
"lawn-mower", "rocket", "streetcar", "tank", "tractor"])
"""
# plot some test images along with the prediction
'''
for i in range(10):
ut.plot_prediction(
Yp[i],
data.test.images[i],
data.test.labels[i],
classes,
fname=folder + 'test-%i.png' % i)
'''
for i in range(50):
ut.plot_prediction(
Yp[i],
x_test[i],
y_test[i],
fname=folder + 'test-%i.png' % i,
top_n=10)
print(yp, y_test)
# plot the confusion matrix
ut.plot_confusion(yp, y_test[:,0],
fname=folder + 'confusion.png')
print("loss:", loss)
print("accuracy:", accuracy)