In [1]:
import numpy as np
import cPickle
import matplotlib.pyplot as plt

print ('loading .... ')

def extractImagesAndLabels(path, file):
    f = open(path+file, 'rb')
    dict = cPickle.load(f)
    images = dict['data']
    images = np.reshape(images, (10000, 3, 32, 32))
    labels = dict['labels']
    return images, labels, dict

def extractCategories(path, file):
    f = open(path+file, 'rb')
    dict = cPickle.load(f)
    return dict['label_names']

images, labels, datadict = extractImagesAndLabels("data/CIFAR-10/cifar-10-batches-py/", "data_batch_big_1")
categories = extractCategories("data/CIFAR-10/cifar-10-batches-py/", "batches.meta")

print ('finished loading .... ')


loading .... 
finished loading .... 

In [2]:
def getBigImage(id):
    bigimages = datadict['bigdata']
    bigimages = np.reshape(bigimages, (10000, 126, 126, 3))
    image = bigimages[id]
    #image = image.transpose([1, 2, 0])
    image = image.astype('float32')
    image /= 255
    return image

imgid=35
image = getBigImage(imgid)
print(image.shape)
%matplotlib inline
imgplot = plt.imshow(image)
categoryid = labels[imgid]
print(categories[categoryid])


(126, 126, 3)
airplane

In [13]:
from __future__ import print_function
import numpy as np 
np.random.seed(1337) 

import keras
from keras.datasets import cifar10
from keras.models import Model
from keras.layers import Dense, Activation, Flatten, Input, MaxPooling2D
from keras.layers import Conv2D
import h5py  # to ensure we have this package installed 

from keras.callbacks import ModelCheckpoint

batch_size = 32
num_classes = 10
epochs = 150


Using TensorFlow backend.

In [24]:
bigimages = datadict['bigdata']
bigimages = np.reshape(bigimages, (10000, 126, 126, 3))

x_train = bigimages[0:9000]
print ("x_train shape : "+str(x_train.shape))
y_train = labels[0:9000]
print ("y_train shape : "+str(len(y_train)))

x_test = bigimages[9000:10000]
print ("x_test shape : "+str(x_test.shape))
y_test = labels[9000:10000]
print ("y_test shape : "+str(len(y_test)))

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255


x_train shape : (9000, 126, 126, 3)
y_train shape : 9000
x_test shape : (1000, 126, 126, 3)
y_test shape : 1000

In [25]:
# Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

In [26]:
# input layer is the same as our typical CNN model 
inputs = Input(shape=(126, 126, 3))

## ----------- New Stuff Starts Here --------- 


tower_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(inputs)
tower_1 = Conv2D(64, (7, 7), padding='same', activation='relu', name='t1_conv')(tower_1)

tower_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(inputs)
tower_2 = Conv2D(64, (11, 11), padding='same', activation='relu')(tower_2)

tower_3 = MaxPooling2D((11, 11), strides=(1, 1), padding='same')(inputs)
tower_3 = Conv2D(64, (1, 1), padding='same', activation='relu')(tower_3)

x = keras.layers.concatenate([tower_1, tower_2, tower_3], axis=1) 

## ----------- New Stuff Ends Here --------- 

# Rest of the model, again, remains the same 

x = Conv2D(8, (3, 3))(x)    
x = Activation('relu')(x) 
x = MaxPooling2D(pool_size=(2, 2))(x) 
x = Flatten()(x) 

x = Dense(num_classes)(x) 

output = Activation('softmax')(x)

In [27]:
model = Model([inputs], output)

In [28]:
model.summary()


__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_3 (InputLayer)            (None, 126, 126, 3)  0                                            
__________________________________________________________________________________________________
conv2d_11 (Conv2D)              (None, 126, 126, 64) 256         input_3[0][0]                    
__________________________________________________________________________________________________
conv2d_12 (Conv2D)              (None, 126, 126, 64) 256         input_3[0][0]                    
__________________________________________________________________________________________________
max_pooling2d_5 (MaxPooling2D)  (None, 126, 126, 3)  0           input_3[0][0]                    
__________________________________________________________________________________________________
t1_conv (Conv2D)                (None, 126, 126, 64) 200768      conv2d_11[0][0]                  
__________________________________________________________________________________________________
conv2d_13 (Conv2D)              (None, 126, 126, 64) 495680      conv2d_12[0][0]                  
__________________________________________________________________________________________________
conv2d_14 (Conv2D)              (None, 126, 126, 64) 256         max_pooling2d_5[0][0]            
__________________________________________________________________________________________________
concatenate_3 (Concatenate)     (None, 378, 126, 64) 0           t1_conv[0][0]                    
                                                                 conv2d_13[0][0]                  
                                                                 conv2d_14[0][0]                  
__________________________________________________________________________________________________
conv2d_15 (Conv2D)              (None, 376, 124, 8)  4616        concatenate_3[0][0]              
__________________________________________________________________________________________________
activation_5 (Activation)       (None, 376, 124, 8)  0           conv2d_15[0][0]                  
__________________________________________________________________________________________________
max_pooling2d_6 (MaxPooling2D)  (None, 188, 62, 8)   0           activation_5[0][0]               
__________________________________________________________________________________________________
flatten_3 (Flatten)             (None, 93248)        0           max_pooling2d_6[0][0]            
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 10)           932490      flatten_3[0][0]                  
__________________________________________________________________________________________________
activation_6 (Activation)       (None, 10)           0           dense_3[0][0]                    
==================================================================================================
Total params: 1,634,322
Trainable params: 1,634,322
Non-trainable params: 0
__________________________________________________________________________________________________

In [ ]:
# initiate RMSprop optimizer
opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6)

# Let's train the model using RMSprop
model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])

filepath="checkpoints/cifar10-inception1big-{epoch:02d}-{val_acc:.2f}.hdf5"
checkpoint = ModelCheckpoint(filepath, 
                             monitor='val_acc', 
                             verbose=1, 
                             mode='max')

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          validation_data=(x_test, y_test),
          callbacks=[checkpoint])


Train on 9000 samples, validate on 1000 samples
Epoch 1/150
9000/9000 [==============================] - 161s 18ms/step - loss: 1.8981 - acc: 0.3154 - val_loss: 1.7514 - val_acc: 0.3600

Epoch 00001: saving model to checkpoints/cifar10-inception1big-01-0.36.hdf5
Epoch 2/150
9000/9000 [==============================] - 160s 18ms/step - loss: 1.5403 - acc: 0.4586 - val_loss: 1.5805 - val_acc: 0.4320

Epoch 00002: saving model to checkpoints/cifar10-inception1big-02-0.43.hdf5
Epoch 3/150
1600/9000 [====>.........................] - ETA: 2:08 - loss: 1.3676 - acc: 0.5088

In [ ]: