def CNNS_model(): if regularization > 0: W_regularizer = l2(regularization) b_regularizer = l2(regularization) else: W_regularizer = None b_regularizer = None # Input Block inputs = Input(shape=(img_width, img_height, 3), name='input') if stddev > 0: inputs = GaussianNoise(stddev=stddev)(inputs) x = Convolution2D(96, (7, 7), border_mode='same', subsample=2, W_regularizer=l2(regularization), b_regularizer=l2(regularization), name='conv_1')(inputs) if LRN2D_norm: x = LRN2D(alpha=alpha, beta=beta)(x) x = Activation('relu')(x) x = MaxPooling2D(pool_size=(3, 3), name='max_pooling_1')(x) # Conv1 Block x = Convolution2D(256, (5, 5), border_mode='same', W_regularizer=W_regularizer, b_regularizer=b_regularizer, name='conv_2')(x) x = Activation('relu')(x) x = MaxPooling2D(pool_size=(2, 2), name='max_pooling_2')(x) # Conv2 Block x = Convolution2D(512, (3, 3), border_mode='same', W_regularizer=W_regularizer, b_regularizer=b_regularizer, name='conv_3')(x) x = Activation('relu')(x) x = Convolution2D(512, (3, 3), border_mode='same', W_regularizer=W_regularizer, b_regularizer=b_regularizer, name='conv_4')(x) x = Activation('relu')(x) x = Convolution2D(512, (3, 3), border_mode='same', W_regularizer=W_regularizer, b_regularizer=b_regularizer, name='conv_5')(x) x = Activation('relu')(x) x = MaxPooling2D((3, 3), name='max_pooling_3')(x) x = Flatten()(x) # Dense Block x = Dense(4096, activation='relu', name='full6')(x) if dropout > 0: x = Dropout(dropout)(x) x = Dense(4096, activation='relu', name='full7')(x) if dropout > 0: x = Dropout(dropout)(x) # Predictions Block x = Dense(8, activation='softmax', name='full8')(x) model = Model(inputs, x, name='cnns_model') return model
def conv2D_lrn2d(x,filters,kernel_size,strides=(1,1),padding='same',data_format=DATA_FORMAT,dilation_rate=(1,1),activation='relu',use_bias=True,kernel_initializer='glorot_uniform',bias_initializer='zeros',kernel_regularizer=None,bias_regularizer=None,activity_regularizer=None,kernel_constraint=None,bias_constraint=None,lrn2d_norm=LRN2D_NORM,weight_decay=WEIGHT_DECAY): if weight_decay: kernel_regularizer=regularizers.l2(weight_decay) bias_regularizer=regularizers.l2(weight_decay) else: kernel_regularizer=None bias_regularizer=None x=Conv2D(filters=filters,kernel_size=kernel_size,strides=strides,padding=padding,data_format=data_format,dilation_rate=dilation_rate,activation=activation,use_bias=use_bias,kernel_initializer=kernel_initializer,bias_initializer=bias_initializer,kernel_regularizer=kernel_regularizer,bias_regularizer=bias_regularizer,activity_regularizer=activity_regularizer,kernel_constraint=kernel_constraint,bias_constraint=bias_constraint)(x) if lrn2d_norm: x=LRN2D(alpha=ALPHA,beta=BETA)(x) return x
def conv2D_lrn2d(x, nb_filter, nb_row, nb_col, border_mode='same', subsample=(1, 1), activation='relu', LRN2D_norm=True, weight_decay=WEIGHT_DECAY, dim_ordering=DIM_ORDERING): ''' Info: Function taken from the Inceptionv3.py script keras github Utility function to apply to a tensor a module Convolution + lrn2d with optional weight decay (L2 weight regularization). ''' if weight_decay: W_regularizer = regularizers.l2(weight_decay) b_regularizer = regularizers.l2(weight_decay) else: W_regularizer = None b_regularizer = None x = Convolution2D(nb_filter, nb_row, nb_col, subsample=subsample, activation=activation, border_mode=border_mode, W_regularizer=W_regularizer, b_regularizer=b_regularizer, bias=False, dim_ordering=dim_ordering)(x) x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x) if LRN2D_norm: x = LRN2D(alpha=ALPHA, beta=BETA)(x) x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x) return x
def create_model(): if DATA_FORMAT == 'channels_first': INP_SHAPE = (3, 224, 224) img_input = Input(shape=INP_SHAPE) CONCAT_AXIS = 1 elif DATA_FORMAT == 'channels_last': INP_SHAPE = (224, 224, 3) img_input = Input(shape=INP_SHAPE) CONCAT_AXIS = 3 else: raise Exception('Invalid Dim Ordering: ' + str(DIM_ORDERING)) x = conv2D_lrn2d(img_input, 64, (7, 7), 2, padding='same', lrn2d_norm=False) x = MaxPooling2D(pool_size=(3, 3), strides=2, padding='same', data_format=DATA_FORMAT)(x) x = LRN2D(alpha=ALPHA, beta=BETA)(x) x = conv2D_lrn2d(x, 64, (1, 1), 1, padding='same', lrn2d_norm=False) x = conv2D_lrn2d(x, 192, (3, 3), 1, padding='same', lrn2d_norm=True) x = MaxPooling2D(pool_size=(3, 3), strides=2, padding='same', data_format=DATA_FORMAT)(x) x = inception_module(x, params=[(64, ), (96, 128), (16, 32), (32, )], concat_axis=CONCAT_AXIS) #3a x = inception_module(x, params=[(128, ), (128, 192), (32, 96), (64, )], concat_axis=CONCAT_AXIS) #3b x = MaxPooling2D(pool_size=(3, 3), strides=2, padding='same', data_format=DATA_FORMAT)(x) x = inception_module(x, params=[(192, ), (96, 208), (16, 48), (64, )], concat_axis=CONCAT_AXIS) #4a x = inception_module(x, params=[(160, ), (112, 224), (24, 64), (64, )], concat_axis=CONCAT_AXIS) #4b x = inception_module(x, params=[(128, ), (128, 256), (24, 64), (64, )], concat_axis=CONCAT_AXIS) #4c x = inception_module(x, params=[(112, ), (144, 288), (32, 64), (64, )], concat_axis=CONCAT_AXIS) #4d x = inception_module(x, params=[(256, ), (160, 320), (32, 128), (128, )], concat_axis=CONCAT_AXIS) #4e x = MaxPooling2D(pool_size=(3, 3), strides=2, padding='same', data_format=DATA_FORMAT)(x) x = inception_module(x, params=[(256, ), (160, 320), (32, 128), (128, )], concat_axis=CONCAT_AXIS) #5a x = inception_module(x, params=[(384, ), (192, 384), (48, 128), (128, )], concat_axis=CONCAT_AXIS) #5b x = AveragePooling2D(pool_size=(7, 7), strides=1, padding='valid', data_format=DATA_FORMAT)(x) x = Flatten()(x) x = Dropout(DROPOUT)(x) x = Dense(output_dim=NB_CLASS, activation='linear')(x) x = Dense(output_dim=NB_CLASS, activation='softmax')(x) return x, img_input, CONCAT_AXIS, INP_SHAPE, DATA_FORMAT
def create_model(): # Define image input layer if DIM_ORDERING == 'th': INP_SHAPE = (3, 224, 224) # 3 - Number of RGB Colours img_input = Input(shape=INP_SHAPE) CONCAT_AXIS = 1 elif DIM_ORDERING == 'tf': INP_SHAPE = (224, 224, 3) # 3 - Number of RGB Colours img_input = Input(shape=INP_SHAPE) CONCAT_AXIS = 3 else: raise Exception('Invalid dim ordering: ' + str(DIM_ORDERING)) # Channel 1 - Convolution Net Input Layer x = conv2D_lrn2d(img_input, 3, 11, 11, subsample=(1, 1), border_mode='same') x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x) # Channel 1 - Convolution Net Layer 1 x = conv2D_lrn2d(x, 96, 55, 55, subsample=(1, 1), border_mode='same') x = MaxPooling2D(strides=(2, 2), pool_size=(2, 2), dim_ordering=DIM_ORDERING)(x) x = LRN2D(alpha=ALPHA, beta=BETA)(x) x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x) # Channel 1 - Convolution Net Layer 2 x = conv2D_lrn2d(x, 192, 27, 27, subsample=(1, 1), border_mode='same') x = MaxPooling2D(strides=(2, 2), pool_size=(2, 2), dim_ordering=DIM_ORDERING)(x) x = LRN2D(alpha=ALPHA, beta=BETA)(x) x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x) # Channel 1 - Convolution Net Layer 3 x = conv2D_lrn2d(x, 288, 13, 13, subsample=(1, 1), border_mode='same') x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x) # Channel 1 - Convolution Net Layer 4 x = conv2D_lrn2d(x, 288, 13, 13, subsample=(1, 1), border_mode='same') x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x) # Channel 1 - Convolution Net Layer 5 x = conv2D_lrn2d(x, 256, 13, 13, subsample=(1, 1), border_mode='same') x = MaxPooling2D(strides=(2, 2), pool_size=(2, 2), dim_ordering=DIM_ORDERING)(x) x = ZeroPadding2D(padding=(1, 1), dim_ordering=DIM_ORDERING)(x) # Channel 1 - Cov Net Layer 7 x = Flatten()(x) x = Dense(4096, activation='relu')(x) x = Dropout(DROPOUT)(x) # Channel 1 - Cov Net Layer 8 x = Dense(4096, activation='relu')(x) x = Dropout(DROPOUT)(x) # Final Channel - Cov Net 9 x = Dense(output_dim=NB_CLASS, activation='softmax')(x) return x, img_input, CONCAT_AXIS, INP_SHAPE, DIM_ORDERING