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)


('Training set', (1470, 50, 50, 3), (1470, 1))
('Test set', (630, 50, 50, 3), (630, 1))

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


(50, 50, 3)

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'])


---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)
<ipython-input-51-5e3da11c3e2e> in <module>()
      1 filename = "-modelCairo.hdf5"
----> 2 model.load_weights(filename)
      3 
      4 model.compile(loss='categorical_crossentropy',
      5               optimizer='adadelta',

/home/vagrant/anaconda2/lib/python2.7/site-packages/keras/engine/topology.pyc in load_weights(self, filepath, by_name)
   2492         '''
   2493         import h5py
-> 2494         f = h5py.File(filepath, mode='r')
   2495         if 'layer_names' not in f.attrs and 'model_weights' in f:
   2496             f = f['model_weights']

/home/vagrant/anaconda2/lib/python2.7/site-packages/h5py/_hl/files.pyc in __init__(self, name, mode, driver, libver, userblock_size, swmr, **kwds)
    270 
    271                 fapl = make_fapl(driver, libver, **kwds)
--> 272                 fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
    273 
    274                 if swmr_support:

/home/vagrant/anaconda2/lib/python2.7/site-packages/h5py/_hl/files.pyc in make_fid(name, mode, userblock_size, fapl, fcpl, swmr)
     90         if swmr and swmr_support:
     91             flags |= h5f.ACC_SWMR_READ
---> 92         fid = h5f.open(name, flags, fapl=fapl)
     93     elif mode == 'r+':
     94         fid = h5f.open(name, h5f.ACC_RDWR, fapl=fapl)

h5py/_objects.pyx in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/work/h5py/_objects.c:2696)()

h5py/_objects.pyx in h5py._objects.with_phil.wrapper (/home/ilan/minonda/conda-bld/work/h5py/_objects.c:2654)()

h5py/h5f.pyx in h5py.h5f.open (/home/ilan/minonda/conda-bld/work/h5py/h5f.c:1942)()

IOError: Unable to open file (Unable to open file: name = '-modelcairo.hdf5', errno = 2, error message = 'no such file or directory', flags = 0, o_flags = 0)

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


category: 0
prediction: 0
(1, 50, 50, 3)
(6, 6, 512)

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


(None, 4608)
(None, 512)