def center_bias(x, img_rows, img_cols, downsampling_factor_net, downsampling_factor_product, out_name=None, elt_name=None): rows_elt = math.ceil(img_rows / downsampling_factor_net) // downsampling_factor_product cols_elt = math.ceil(img_cols / downsampling_factor_net) // downsampling_factor_product eltprod = EltWiseProduct(init='zero', trainable=True, W_regularizer=l2(1/(rows_elt*cols_elt*N_CAT)), name=elt_name)(x) output_ml_net = Activation('relu', name=out_name)(eltprod) return output_ml_net
def ml_net_model(img_rows=480, img_cols=640, downsampling_factor_net=8, downsampling_factor_product=10): f = h5py.File("vgg16_weights.h5") input_ml_net = Input(shape=(3, img_rows, img_cols)) ######################################################### # FEATURE EXTRACTION NETWORK # ######################################################### weights = get_weights_vgg16(f, 1) conv1_1 = Convolution2D(64, 3, 3, weights=weights, activation='relu', border_mode='same')(input_ml_net) weights = get_weights_vgg16(f, 3) conv1_2 = Convolution2D(64, 3, 3, weights=weights, activation='relu', border_mode='same')(conv1_1) conv1_pool = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(conv1_2) weights = get_weights_vgg16(f, 6) conv2_1 = Convolution2D(128, 3, 3, weights=weights, activation='relu', border_mode='same')(conv1_pool) weights = get_weights_vgg16(f, 8) conv2_2 = Convolution2D(128, 3, 3, weights=weights, activation='relu', border_mode='same')(conv2_1) conv2_pool = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(conv2_2) weights = get_weights_vgg16(f, 11) conv3_1 = Convolution2D(256, 3, 3, weights=weights, activation='relu', border_mode='same')(conv2_pool) weights = get_weights_vgg16(f, 13) conv3_2 = Convolution2D(256, 3, 3, weights=weights, activation='relu', border_mode='same')(conv3_1) weights = get_weights_vgg16(f, 15) conv3_3 = Convolution2D(256, 3, 3, weights=weights, activation='relu', border_mode='same')(conv3_2) conv3_pool = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(conv3_3) weights = get_weights_vgg16(f, 18) conv4_1 = Convolution2D(512, 3, 3, weights=weights, activation='relu', border_mode='same')(conv3_pool) weights = get_weights_vgg16(f, 20) conv4_2 = Convolution2D(512, 3, 3, weights=weights, activation='relu', border_mode='same')(conv4_1) weights = get_weights_vgg16(f, 22) conv4_3 = Convolution2D(512, 3, 3, weights=weights, activation='relu', border_mode='same')(conv4_2) conv4_pool = MaxPooling2D((2, 2), strides=(1, 1), border_mode='same')(conv4_3) weights = get_weights_vgg16(f, 25) conv5_1 = Convolution2D(512, 3, 3, weights=weights, activation='relu', border_mode='same')(conv4_pool) weights = get_weights_vgg16(f, 27) conv5_2 = Convolution2D(512, 3, 3, weights=weights, activation='relu', border_mode='same')(conv5_1) weights = get_weights_vgg16(f, 29) conv5_3 = Convolution2D(512, 3, 3, weights=weights, activation='relu', border_mode='same')(conv5_2) ######################################################### # ENCODING NETWORK # ######################################################### concatenated = merge([conv3_pool, conv4_pool, conv5_3], mode='concat', concat_axis=1) dropout = Dropout(0.5)(concatenated) int_conv = Convolution2D(64, 3, 3, init='glorot_normal', activation='relu', border_mode='same')(dropout) pre_final_conv = Convolution2D(1, 1, 1, init='glorot_normal', activation='relu')(int_conv) ######################################################### # PRIOR LEARNING # ######################################################### rows_elt = math.ceil( img_rows / downsampling_factor_net) // downsampling_factor_product cols_elt = math.ceil( img_cols / downsampling_factor_net) // downsampling_factor_product eltprod = EltWiseProduct(init='zero', W_regularizer=l2( 1 / (rows_elt * cols_elt)))(pre_final_conv) output_ml_net = Activation('relu')(eltprod) model = Model(input=[input_ml_net], output=[output_ml_net]) for layer in model.layers: print(layer.input_shape, layer.output_shape) return model
def build_model(img_rows=480, img_cols=640, downsampling_factor_net=8, downsampling_factor_product=10, use_pretrained=None, intermediate=False): input_ml_net = Input(shape=(3, img_rows, img_cols)) conv1_1 = VGGConvLayer(64, vgg_layer=1)(input_ml_net) conv1_2 = VGGConvLayer(64, vgg_layer=3)(conv1_1) conv1_pool = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(conv1_2) conv2_1 = VGGConvLayer(128, vgg_layer=6)(conv1_pool) conv2_2 = VGGConvLayer(128, vgg_layer=8)(conv2_1) conv2_pool = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(conv2_2) conv3_1 = VGGConvLayer(256, vgg_layer=11)(conv2_pool) conv3_2 = VGGConvLayer(256, vgg_layer=13)(conv3_1) conv3_3 = VGGConvLayer(256, vgg_layer=15)(conv3_2) conv3_pool = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(conv3_3) conv4_1 = VGGConvLayer(512, vgg_layer=18)(conv3_pool) conv4_2 = VGGConvLayer(512, vgg_layer=20)(conv4_1) conv4_3 = VGGConvLayer(512, vgg_layer=22)(conv4_2) conv4_pool = MaxPooling2D((2, 2), strides=(1, 1), border_mode='same')(conv4_3) conv5_1 = VGGConvLayer(512, vgg_layer=25)(conv4_pool) conv5_2 = VGGConvLayer(512, vgg_layer=27)(conv5_1) conv5_3 = VGGConvLayer(512, vgg_layer=29)(conv5_2) concatenated = merge([conv3_pool, conv4_pool, conv5_3], mode='concat', concat_axis=1) dropout = Dropout(0.5)(concatenated) int_conv = Convolution2D(64, 3, 3, init='glorot_normal', activation='relu', border_mode='same', trainable=True, name='int_conv')(dropout) pre_final_conv = Convolution2D(1, 1, 1, init='glorot_normal', activation='relu', name='pre_final_conv')(int_conv) rows_elt = math.ceil( img_rows / downsampling_factor_net) // downsampling_factor_product cols_elt = math.ceil( img_cols / downsampling_factor_net) // downsampling_factor_product eltprod = EltWiseProduct(init='zero', trainable=True, W_regularizer=l2(1 / (rows_elt * cols_elt)), name='eltprod')(pre_final_conv) output_ml_net = Activation('relu')(eltprod) model = Model(input=[input_ml_net], output=[output_ml_net]) # load pretrained weights if use_pretrained == 'mlnet': model.load_weights('models/mlnet_salicon_weights.pkl') elif use_pretrained == 'my': model.load_weights('models/generalist_finetune.h5') elif use_pretrained is None: return model else: raise StandardError('INVALID use_pretrained') if intermediate: # still has correct weights model = Model(input=[input_ml_net], output=[concatenated]) return model