Skip to content

CubeRun Model

John Martinsson edited this page Dec 19, 2016 · 2 revisions

Keras Code

from keras.layers import Input
from keras.layers import Dense, Activation, Dropout, Flatten
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers.normalization import BatchNormalization
from keras.models import Model
from keras import backend as K

def CubeRun(nb_classes, input_shape):
    """ Instantiate a CubeRun architecture

    # Arguments
        nb_classes  : the number of classification classes
        input_shape : the shape of the input layer (rows, columns)

    # Returns
        A Keras model instance
    """

    img_input = Input(shape=input_shape)
    bn_axis = 3

    # x = Dropout(0.2)(img_input)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(img_input)

    # conv (64 5x5 kernels, stride size 1x2)
    x = Convolution2D(64, 5, 5, subsample=(1, 2), activation='relu',
                      init="he_normal", border_mode="same", name='conv_1')(x)
    # max pooling (2x2 kernels, stride size 2x2)
    x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)

    # batch normalization
    x = BatchNormalization(axis=bn_axis, name='bn_conv2')(x)
    # conv (64 5x5 kernels, stride size 1x1)
    x = Convolution2D(64, 5, 5, subsample=(1, 1), activation='relu',
                      init="he_normal", border_mode="same", name='conv2')(x)
    # max pooling (2x2 kernels, stride size 2x2)
    x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)

    # batch normalization
    x = BatchNormalization(axis=bn_axis, name='bn_conv3')(x)
    # conv (128 5x5 kernels, stride size 1x1)
    x = Convolution2D(128, 5, 5, subsample=(1, 1), activation='relu',
                      init="he_normal", border_mode="same", name='conv3')(x)
    # max pooling (2x2 kernels, stride size 2x2)
    x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)

    # batch normalization
    x = BatchNormalization(axis=bn_axis, name='bn_conv4')(x)
    # conv (256 5x5 kernels, stride size 1x1)
    x = Convolution2D(256, 5, 5, subsample=(1, 1), activation='relu',
                      init="he_normal", border_mode="same", name='conv4')(x)
    # max pooling (2x2 kernels, stride size 2x2)
    x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)

    # batch normalization
    x = BatchNormalization(axis=bn_axis, name='bn_conv5')(x)
    # conv (256 3x3 kernels, stride size 1x1)
    x = Convolution2D(256, 3, 3, subsample=(1, 1), activation='relu',
                      init="he_normal", border_mode="same", name='conv5')(x)
    # max pooling (2x2 kernels, stride size 2x2)
    x = MaxPooling2D(pool_size=(2, 2), strides=(2, 2))(x)

    x = BatchNormalization(axis=bn_axis, name='bn_dense')(x)
    # flatten 3D feature maps to 1D feature vectors
    x = Flatten(name='flatten')(x)

    # dense layer
    x = Dropout(0.4)(x)
    x = Dense(1024, activation='relu', name='dense')(x)

    # soft max layer
    x = Dropout(0.4)(x)
    x = Dense(nb_classes, activation='softmax', name='softmax')(x)

    model = Model(img_input, x)

    return model

Model Summary

In [2]: cuberun = CubeRun(20, (256, 512, 1))

In [3]: cuberun.summary()
____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 256, 512, 1)   0                                            
____________________________________________________________________________________________________
bn_conv1 (BatchNormalization)    (None, 256, 512, 1)   2           input_1[0][0]                    
____________________________________________________________________________________________________
conv_1 (Convolution2D)           (None, 256, 256, 64)  1664        bn_conv1[0][0]                   
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D)    (None, 128, 128, 64)  0           conv_1[0][0]                     
____________________________________________________________________________________________________
bn_conv2 (BatchNormalization)    (None, 128, 128, 64)  128         maxpooling2d_1[0][0]             
____________________________________________________________________________________________________
conv2 (Convolution2D)            (None, 128, 128, 64)  102464      bn_conv2[0][0]                   
____________________________________________________________________________________________________
maxpooling2d_2 (MaxPooling2D)    (None, 64, 64, 64)    0           conv2[0][0]                      
____________________________________________________________________________________________________
bn_conv3 (BatchNormalization)    (None, 64, 64, 64)    128         maxpooling2d_2[0][0]             
____________________________________________________________________________________________________
conv3 (Convolution2D)            (None, 64, 64, 128)   204928      bn_conv3[0][0]                   
____________________________________________________________________________________________________
maxpooling2d_3 (MaxPooling2D)    (None, 32, 32, 128)   0           conv3[0][0]                      
____________________________________________________________________________________________________
bn_conv4 (BatchNormalization)    (None, 32, 32, 128)   256         maxpooling2d_3[0][0]             
____________________________________________________________________________________________________
conv4 (Convolution2D)            (None, 32, 32, 256)   819456      bn_conv4[0][0]                   
____________________________________________________________________________________________________
maxpooling2d_4 (MaxPooling2D)    (None, 16, 16, 256)   0           conv4[0][0]                      
____________________________________________________________________________________________________
bn_conv5 (BatchNormalization)    (None, 16, 16, 256)   512         maxpooling2d_4[0][0]             
____________________________________________________________________________________________________
conv5 (Convolution2D)            (None, 16, 16, 256)   590080      bn_conv5[0][0]                   
____________________________________________________________________________________________________
maxpooling2d_5 (MaxPooling2D)    (None, 8, 8, 256)     0           conv5[0][0]                      
____________________________________________________________________________________________________
bn_dense (BatchNormalization)    (None, 8, 8, 256)     512         maxpooling2d_5[0][0]             
____________________________________________________________________________________________________
flatten (Flatten)                (None, 16384)         0           bn_dense[0][0]                   
____________________________________________________________________________________________________
dropout_1 (Dropout)              (None, 16384)         0           flatten[0][0]                    
____________________________________________________________________________________________________
dense (Dense)                    (None, 1024)          16778240    dropout_1[0][0]                  
____________________________________________________________________________________________________
dropout_2 (Dropout)              (None, 1024)          0           dense[0][0]                      
____________________________________________________________________________________________________
softmax (Dense)                  (None, 20)            20500       dropout_2[0][0]                  
====================================================================================================
Total params: 18518870
____________________________________________________________________________________________________