In [14]:
import pickle
pickle_file = '-images.pickle'
with open(pickle_file, 'rb') as f:
save = pickle.load(f)
X_train = save['X_train']
y_train = save['y_train']
X_test = save['X_test']
y_test = save['y_test']
del save # hint to help gc free up memory
print('Training set', X_train.shape, y_train.shape)
print('Test set', X_test.shape, y_test.shape)
In [15]:
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Convolution2D, MaxPooling2D, AveragePooling2D
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from keras import backend as K
In [16]:
# number of classes
num_classes = 2
# image dimensions
img_rows, img_cols = X_train.shape[1], X_train.shape[2]
if K.image_dim_ordering() == 'th':
X_train = X_train.reshape(X_train.shape[0], 3, img_rows, img_cols)
X_test = X_test.reshape(X_test.shape[0], 3, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 3)
X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 3)
input_shape = (img_rows, img_cols, 3)
Y_train = np_utils.to_categorical(y_train, num_classes)
Y_test = np_utils.to_categorical(y_test, num_classes)
print input_shape
In [17]:
# model hyperparameters
batch_size = 16
nb_epoch = 30
# network architecture
patch_size_1 = 9
patch_size_2 = 5
patch_size_3 = 3
patch_size_4 = 3
depth_1 = 128
depth_2 = 256
depth_3 = 512
depth_4 = 1024
pool_size = 2
num_hidden_1 = 512
num_hidden_2 = 1028
dropout = 0.5
In [47]:
# create new Keras Sequential model
model = Sequential()
# add first convolutional layer to model and specify it's depth and filter size
# for the first layer we also have to specify the size of each input image
# which we calculated above
model.add(Convolution2D(depth_1, patch_size_1, patch_size_1,
border_mode='valid',
input_shape=input_shape))
# apply 'relu' activation function for first layer
model.add(Activation('relu'))
# apply max pooling to reduce the size of the image by a factor of 2
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
# repeat these operations for the second convolutional layer
# this time Keras can figure out the input size
# from the previous layer on it's own
model.add(Convolution2D(depth_2, patch_size_2, patch_size_2,
border_mode='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
model.add(Convolution2D(depth_3, patch_size_3, patch_size_3,
border_mode='valid'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))
# flatten the three-dimensional convolutional layer to a single layer of neurons
model.add(Flatten())
# add the first fully connected layer, applying 'relu' activation and dropout
model.add(Dense(num_hidden_1))
model.add(Activation('relu'))
model.add(Dropout(dropout))
# add the second fully connected layer
model.add(Dense(num_hidden_2))
model.add(Activation('relu'))
model.add(Dropout(dropout))
# add the final classification layer with the number of neurons
# matching the number of classes we are trying to learn
model.add(Dense(num_classes))
# apply the 'softmax' activation to the final layer to convert the output to
# a probability distribution
model.add(Activation('softmax'))
In [51]:
filename = "-modelCairo.hdf5"
model.load_weights(filename)
model.compile(loss='categorical_crossentropy',
optimizer='adadelta',
metrics=['accuracy'])
In [52]:
def get_activations(model, layer, X_batch):
get_activations = K.function([model.layers[0].input], [model.layers[layer].output])
activations = get_activations([X_batch,0])
return activations
In [66]:
%matplotlib inline
from matplotlib.pyplot import imshow
import matplotlib.pyplot as plt
img_num = (10)
X_t = X_train[img_num:img_num+1]
img = X_t[0][0]
category = np.argmax(Y_train[img_num])
print "category:", category
print "prediction:", np.argmax(model.predict(X_t)[0])
mi, ma = np.min(img), np.max(img)
imshow(img, cmap = plt.get_cmap('gray'), vmin = mi, vmax = ma, interpolation='nearest')
plt.axis('off')
plt.show()
print X_t.shape
my_featuremaps = get_activations(model, 7, X_t)
maps = my_featuremaps[0][0]
print maps.shape
In [70]:
lay = maps[4]
mi, ma = np.min(lay), np.max(lay)
imshow(lay, cmap = plt.get_cmap('gray'), vmin = mi, vmax = ma, interpolation='nearest')
plt.axis('off')
plt.show()
In [71]:
layer = model.layers[10]
print layer.input_shape
print layer.output_shape