See video: https://youtu.be/6kwQEBMandw?t=6526
In [1]:
from theano.sandbox import cuda
#cuda.use('gpu2')
In [2]:
%matplotlib inline
import utils; reload(utils)
from utils import *
from __future__ import division, print_function
In [3]:
batch_size=64
In [4]:
from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
(X_train.shape, y_train.shape, X_test.shape, y_test.shape)
Out[4]:
In [5]:
X_test = np.expand_dims(X_test,1)
X_train = np.expand_dims(X_train,1)
In [6]:
X_train.shape
Out[6]:
In [7]:
y_train[:5]
Out[7]:
In [8]:
y_train = onehot(y_train)
y_test = onehot(y_test)
In [9]:
y_train[:5]
Out[9]:
In [10]:
mean_px = X_train.mean().astype(np.float32)
std_px = X_train.std().astype(np.float32)
In [11]:
def norm_input(x): return (x-mean_px)/std_px
In [12]:
def get_lin_model():
model = Sequential([
Lambda(norm_input, input_shape=(1,28,28)),
Flatten(),
Dense(10, activation='softmax')
])
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
return model
In [13]:
lm = get_lin_model()
In [14]:
gen = image.ImageDataGenerator()
batches = gen.flow(X_train, y_train, batch_size=64)
test_batches = gen.flow(X_test, y_test, batch_size=64)
In [16]:
lm.fit_generator(batches, batches.n, nb_epoch=1,
validation_data=test_batches, nb_val_samples=test_batches.n)
Out[16]:
In [18]:
lm.optimizer.lr=0.1
In [20]:
lm.fit_generator(batches, batches.n, nb_epoch=1,
validation_data=test_batches, nb_val_samples=test_batches.n)
Out[20]:
In [21]:
lm.optimizer.lr=0.01
In [23]:
lm.fit_generator(batches, batches.n, nb_epoch=4,
validation_data=test_batches, nb_val_samples=test_batches.n)
Out[23]:
In [24]:
def get_fc_model():
model = Sequential([
Lambda(norm_input, input_shape=(1,28,28)),
Flatten(),
Dense(512, activation='softmax'),
Dense(10, activation='softmax')
])
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
return model
In [25]:
fc = get_fc_model()
In [27]:
fc.fit_generator(batches, batches.n, nb_epoch=1,
validation_data=test_batches, nb_val_samples=test_batches.n)
Out[27]:
In [28]:
fc.optimizer.lr=0.1
In [30]:
fc.fit_generator(batches, batches.n, nb_epoch=4,
validation_data=test_batches, nb_val_samples=test_batches.n)
Out[30]:
In [31]:
fc.optimizer.lr=0.01
In [33]:
fc.fit_generator(batches, batches.n, nb_epoch=4,
validation_data=test_batches, nb_val_samples=test_batches.n)
Out[33]:
In [34]:
def get_model():
model = Sequential([
Lambda(norm_input, input_shape=(1,28,28)),
Convolution2D(32,3,3, activation='relu'),
Convolution2D(32,3,3, activation='relu'),
MaxPooling2D(),
Convolution2D(64,3,3, activation='relu'),
Convolution2D(64,3,3, activation='relu'),
MaxPooling2D(),
Flatten(),
Dense(512, activation='relu'),
Dense(10, activation='softmax')
])
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
return model
In [ ]:
model = get_model()
In [ ]:
model.fit_generator(batches, batches.n, nb_epoch=1,
validation_data=test_batches, nb_val_samples=test_batches.n)
In [37]:
model.optimizer.lr=0.1
In [38]:
model.fit_generator(batches, batches.n, nb_epoch=1,
validation_data=test_batches, nb_val_samples=test_batches.n)
Out[38]:
In [39]:
model.optimizer.lr=0.01
In [ ]:
model.fit_generator(batches, batches.n, nb_epoch=8,
validation_data=test_batches, nb_val_samples=test_batches.n)
In [23]:
model = get_model()
In [76]:
gen = image.ImageDataGenerator(rotation_range=8, width_shift_range=0.08, shear_range=0.3,
height_shift_range=0.08, zoom_range=0.08)
batches = gen.flow(X_train, y_train, batch_size=64)
test_batches = gen.flow(X_test, y_test, batch_size=64)
In [24]:
model.fit_generator(batches, batches.n, nb_epoch=1,
validation_data=test_batches, nb_val_samples=test_batches.n)
Out[24]:
In [25]:
model.optimizer.lr=0.1
In [26]:
model.fit_generator(batches, batches.N, nb_epoch=4,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[26]:
In [27]:
model.optimizer.lr=0.01
In [28]:
model.fit_generator(batches, batches.N, nb_epoch=8,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[28]:
In [29]:
model.optimizer.lr=0.001
In [30]:
model.fit_generator(batches, batches.N, nb_epoch=14,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[30]:
In [31]:
model.optimizer.lr=0.0001
In [32]:
model.fit_generator(batches, batches.N, nb_epoch=10,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[32]:
In [125]:
def get_model_bn():
model = Sequential([
Lambda(norm_input, input_shape=(1,28,28)),
Convolution2D(32,3,3, activation='relu'),
BatchNormalization(axis=1),
Convolution2D(32,3,3, activation='relu'),
MaxPooling2D(),
BatchNormalization(axis=1),
Convolution2D(64,3,3, activation='relu'),
BatchNormalization(axis=1),
Convolution2D(64,3,3, activation='relu'),
MaxPooling2D(),
Flatten(),
BatchNormalization(),
Dense(512, activation='relu'),
BatchNormalization(),
Dense(10, activation='softmax')
])
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
return model
In [126]:
model = get_model_bn()
In [127]:
model.fit_generator(batches, batches.N, nb_epoch=1,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[127]:
In [128]:
model.optimizer.lr=0.1
In [129]:
model.fit_generator(batches, batches.N, nb_epoch=4,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[129]:
In [130]:
model.optimizer.lr=0.01
In [131]:
model.fit_generator(batches, batches.N, nb_epoch=12,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[131]:
In [132]:
model.optimizer.lr=0.001
In [133]:
model.fit_generator(batches, batches.N, nb_epoch=12,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[133]:
In [79]:
def get_model_bn_do():
model = Sequential([
Lambda(norm_input, input_shape=(1,28,28)),
Convolution2D(32,3,3, activation='relu'),
BatchNormalization(axis=1),
Convolution2D(32,3,3, activation='relu'),
MaxPooling2D(),
BatchNormalization(axis=1),
Convolution2D(64,3,3, activation='relu'),
BatchNormalization(axis=1),
Convolution2D(64,3,3, activation='relu'),
MaxPooling2D(),
Flatten(),
BatchNormalization(),
Dense(512, activation='relu'),
BatchNormalization(),
Dropout(0.5),
Dense(10, activation='softmax')
])
model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])
return model
In [80]:
model = get_model_bn_do()
In [81]:
model.fit_generator(batches, batches.N, nb_epoch=1,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[81]:
In [82]:
model.optimizer.lr=0.1
In [83]:
model.fit_generator(batches, batches.N, nb_epoch=4,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[83]:
In [84]:
model.optimizer.lr=0.01
In [85]:
model.fit_generator(batches, batches.N, nb_epoch=12,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[85]:
In [86]:
model.optimizer.lr=0.001
In [89]:
model.fit_generator(batches, batches.N, nb_epoch=1,
validation_data=test_batches, nb_val_samples=test_batches.N)
Out[89]:
In [90]:
def fit_model():
model = get_model_bn_do()
model.fit_generator(batches, batches.N, nb_epoch=1, verbose=0,
validation_data=test_batches, nb_val_samples=test_batches.N)
model.optimizer.lr=0.1
model.fit_generator(batches, batches.N, nb_epoch=4, verbose=0,
validation_data=test_batches, nb_val_samples=test_batches.N)
model.optimizer.lr=0.01
model.fit_generator(batches, batches.N, nb_epoch=12, verbose=0,
validation_data=test_batches, nb_val_samples=test_batches.N)
model.optimizer.lr=0.001
model.fit_generator(batches, batches.N, nb_epoch=18, verbose=0,
validation_data=test_batches, nb_val_samples=test_batches.N)
return model
In [91]:
models = [fit_model() for i in range(6)]
In [92]:
path = "data/mnist/"
model_path = path + 'models/'
In [93]:
for i,m in enumerate(models):
m.save_weights(model_path+'cnn-mnist23-'+str(i)+'.pkl')
In [94]:
evals = np.array([m.evaluate(X_test, y_test, batch_size=256) for m in models])
In [95]:
evals.mean(axis=0)
Out[95]:
In [96]:
all_preds = np.stack([m.predict(X_test, batch_size=256) for m in models])
In [97]:
all_preds.shape
Out[97]:
In [98]:
avg_preds = all_preds.mean(axis=0)
In [99]:
keras.metrics.categorical_accuracy(y_test, avg_preds).eval()
Out[99]:
In [ ]: