/
cifar_conv_autoencoder.py
203 lines (160 loc) · 7.15 KB
/
cifar_conv_autoencoder.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
from __future__ import absolute_import
from __future__ import print_function
from keras.layers import containers
from keras.datasets import cifar10
from keras.preprocessing.image import ImageDataGenerator
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, Adadelta, Adagrad, RMSprop
from keras.utils import np_utils, generic_utils
from six.moves import range
import cPickle as pickle
from scipy.spatial.distance import cdist
import numpy as np
from keras.layers.core import AutoEncoder
import matplotlib
matplotlib.use('Agg')
import pylab
'''
Train a (fairly simple) deep CNN on the CIFAR10 small images dataset.
GPU run command:
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python cifar10_cnn.py
It gets down to 0.65 test logloss in 25 epochs, and down to 0.55 after 50 epochs.
(it's still underfitting at that point, though).
Note: the data was pickled with Python 2, and some encoding issues might prevent you
from loading it in Python 3. You might have to load it in Python 2,
save it in a different format, load it in Python 3 and repickle it.
'''
batch_size = 32
nb_classes = 10
nb_epoch = 10
data_augmentation = True
# shape of the image (SHAPE x SHAPE)
shapex, shapey = 32, 32
# number of convolutional filters to use at each layer
nb_filters = [32, 64]
# level of pooling to perform at each layer (POOL x POOL)
nb_pool = [2, 2]
# level of convolution to perform at each layer (CONV x CONV)
nb_conv = [3, 3]
# the CIFAR10 images are RGB
image_dimensions = 3
# the data, shuffled and split between tran and test sets
(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
X_train = X_train[0:10000]
X_test = X_test[0:100]
# 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)
model = containers.Sequential()
model.add(Convolution2D(nb_filters[0], image_dimensions, nb_conv[0], nb_conv[0], border_mode='full'))
model.add(Activation('relu'))
model.add(MaxPooling2D(poolsize=(nb_pool[0], nb_pool[0])))
model.add(Dropout(0.25))
model.add(Convolution2D(nb_filters[1], nb_filters[0], nb_conv[0], nb_conv[0], border_mode='full'))
model.add(Activation('relu'))
#model.add(MaxPooling2D(poolsize=(nb_pool[0], nb_pool[0])))
model.add(Flatten())
model.add(Dense(23104,256))
#model.add(Activation('sigmoid'))
encoder = model
model = None
colors = [name for name, hex in matplotlib.colors.cnames.iteritems()][0:10]
print(colors)
#encoder = containers.Sequential([Dropout(0.2), Dense(X_train.shape[1], 512), Dense(512, 256), Dense(256, 128)])
decoder = containers.Sequential([ Dense(256, X_train.shape[1]*(X_train.shape[2]**2))])
# the image dimensions are the original dimensions divided by any pooling
# each pixel has a number of filters, determined by the last Convolution2D layer
model = Sequential()
model.add(AutoEncoder(encoder=encoder, decoder=decoder, output_reconstruction=True))
# let's train the model using SGD + momentum (how original).
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-6)
model.compile(loss='mean_squared_error', optimizer=rmsprop)
if not data_augmentation:
print("Not using data augmentation or normalization")
X_train = X_train.astype("float32")
X_test = X_test.astype("float32")
X_train /= 255
X_test /= 255
model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch)
score = model.evaluate(X_test, Y_test, batch_size=batch_size)
print('Test score:', score)
else:
print("Using real time data augmentation")
# this will do preprocessing and realtime data augmentation
datagen = ImageDataGenerator(
featurewise_center=True, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=True, # divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=20, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.2, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.2, # randomly shift images vertically (fraction of total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False) # randomly flip images
# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(X_train)
for e in range(nb_epoch):
print('-'*40)
print('Epoch', e)
print('-'*40)
print("Training...")
# batch train with realtime data augmentation
progbar = generic_utils.Progbar(X_train.shape[0])
for X_batch, Y_batch in datagen.flow(X_train, Y_train):
loss = model.train_on_batch(X_batch, X_batch.reshape(X_batch.shape[0],X_train.shape[2]**2*3))
progbar.add(X_batch.shape[0], values=[("train loss", loss)])
print("Testing...")
# test time!
progbar = generic_utils.Progbar(X_test.shape[0])
for X_batch, Y_batch in datagen.flow(X_test, X_test):
score = model.test_on_batch(X_batch, X_batch.reshape(X_batch.shape[0],X_train.shape[2]**2*3))
progbar.add(X_batch.shape[0], values=[("test loss", score)])
model2 = Sequential()
model2.add(encoder)
codes = []
targets = []
model2.compile(loss = "mean_squared_error", optimizer = "sgd")
for X_batch, Y_batch in datagen.flow(X_train, Y_train):
codes.append(model2.predict(X_batch))
targets.append(np.argmax(Y_batch))
print('stack it...')
codes = np.vstack(codes)
targets = np.vstack(targets)
print(codes.shape,'code shape')
pickle.dump(codes,open('/home/tim/codes.p','wb'))
pickle.dump(targets,open('/home/tim/codesy.p','wb'))
for lbl in range(10):
idx = np.where(y_train==lbl)[0]
points = 100
#pylab.scatter(codes[idx[0:points]][:,0],codes[idx[0:points]][:,1],c=colors[lbl])
#pylab.savefig('/home/tim/test2.png')
Y = cdist(codes, codes, 'euclidean')
sample_size = 2000
total = 0
classes = {}
for i in range(sample_size): classes[i] = []
for i in range(sample_size):
idx = np.argsort(Y[i])[0:20]
target = np.argmax(Y_train[idx[0]])
for pred in idx:
classes[pred].append(target)
#print(target)
correct = np.sum(Y_train[idx][:,target])
#print([target,correct])
total +=correct
correct = 0
for key in classes:
values = classes[key]
values.sort()
target = np.argmax(Y_train[key])
correct += target ==np.argmax(np.bincount(values))
#print([target,np.argmax(np.bincount(values))])
print([total, total/float(sample_size*20)])