def add_context(model: Sequential) -> Sequential: """ Append the context layers to the frontend. """ model.add(ZeroPadding2D(padding=(33, 33))) model.add(Convolution2D(42, 3, 3, activation='relu', name='ct_conv1_1')) model.add(Convolution2D(42, 3, 3, activation='relu', name='ct_conv1_2')) model.add(AtrousConvolution2D(84, 3, 3, atrous_rate=(2, 2), activation='relu', name='ct_conv2_1')) model.add(AtrousConvolution2D(168, 3, 3, atrous_rate=(4, 4), activation='relu', name='ct_conv3_1')) model.add(AtrousConvolution2D(336, 3, 3, atrous_rate=(8, 8), activation='relu', name='ct_conv4_1')) model.add(AtrousConvolution2D(672, 3, 3, atrous_rate=(16, 16), activation='relu', name='ct_conv5_1')) model.add(Convolution2D(672, 3, 3, activation='relu', name='ct_fc1')) model.add(Convolution2D(21, 1, 1, name='ct_final')) return model
def dilnet1(input_width, input_height) -> Sequential: model = Sequential() model.add( Conv2D(16, 1, activation='relu', name='conv1_1', input_shape=(input_width, input_height, NUM_CHANNELS))) model.add(Conv2D(16, 1, activation='relu', name='conv1_2')) model.add(Conv2D(16, 1, activation='relu', name='conv2_1')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(Conv2D(32, 2, activation='relu', name='conv2_2')) model.add(Conv2D(32, 2, activation='relu', name='conv3_1')) model.add( AtrousConvolution2D(64, 3, atrous_rate=(2, 2), activation='relu', name='fc6')) model.add(Dropout(0.5)) model.add(Convolution2D(64, 2, activation='relu', name='fc7')) model.add(Dropout(0.5)) model.add(Convolution2D(1, 1, activation='linear', name='fc-final')) model.add(GlobalAveragePooling2D()) model.add(Activation('sigmoid')) return model
def build_model(): model = Sequential() # 第一个卷积层,4个卷积核 model.add( Convolution2D(8, 5, 5, border_mode='valid', dim_ordering='th', input_shape=(1, 20, 20))) model.add(ZeroPadding2D((1, 1))) model.add(GaussianNoise(0.001)) model.add(Activation('tanh')) model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th')) model.add(Activation('tanh')) # 第二个卷积层,8个卷积核, # model.add(GaussianNoise(0.001)) # model.add(UpSampling2D(size=(2, 2), dim_ordering='th')) model.add( AtrousConvolution2D(16, 3, 3, border_mode='valid', dim_ordering='th')) # model.add(ZeroPadding2D((1, 1))) model.add(Activation('tanh')) # model.add(MaxPooling2D(pool_size=(2, 2), dim_ordering='th')) # model.add(Activation('tanh')) # 全连接层,先将前一层输出的二维特征图flatten为一维的 model.add(Flatten()) model.add(Dense(20)) model.add(Activation('tanh')) # LSTM 层 model.add(Reshape((20, 1))) model.add( LSTM(input_dim=1, output_dim=32, activation='tanh', inner_activation='tanh', return_sequences=True)) model.add(GaussianNoise(0.01)) model.add( LSTM(64, activation='tanh', inner_activation='tanh', return_sequences=False)) model.add(Dropout(0.2)) # Dropout overfitting model.add(Dense(1)) model.add(Activation('linear')) start = time.time() # 使用SGD + momentum # model.compile里的参数loss就是损失函数(目标函数) # sgd = SGD(lr=0.08, decay=1e-6, momentum=0.9, nesterov=True) # model.compile(loss="mse", optimizer=sgd) model.compile(loss="mse", optimizer="Nadam") # Nadam # rmsprop print "Compilation Time : ", time.time() - start return model
def __transition_up_block(ip, nb_filters, type='upsampling', output_shape=None, weight_decay=1E-4): ''' SubpixelConvolutional Upscaling (factor = 2) Args: ip: keras tensor nb_filters: number of layers type: can be 'upsampling', 'subpixel', 'deconv', or 'atrous'. Determines type of upsampling performed output_shape: required if type = 'deconv'. Output shape of tensor weight_decay: weight decay factor Returns: keras tensor, after applying upsampling operation. ''' if type == 'upsampling': x = UpSampling2D()(ip) elif type == 'subpixel': x = Conv2D(nb_filters, (3, 3), padding="same", kernel_regularizer=l2(weight_decay), activation='relu', use_bias=False, kernel_initializer='he_uniform')(ip) # x = Convolution2D(nb_filters, 3, 3, activation="relu", border_mode='same', W_regularizer=l2(weight_decay), # bias=False, init='he_uniform')(ip) x = SubPixelUpscaling(scale_factor=2)(x) x = Conv2D(nb_filters, (3, 3), activation="relu", padding='same', kernel_regularizer=l2(weight_decay), use_bias=False, kernel_initializer='he_uniform')(x) elif type == 'atrous': # waiting on https://github.com/fchollet/keras/issues/4018 x = AtrousConvolution2D(nb_filters, 3, 3, activation="relu", W_regularizer=l2(weight_decay), bias=False, atrous_rate=(2, 2), init='he_uniform')(ip) else: x = Deconvolution2D(nb_filters, 3, 3, output_shape, activation='relu', border_mode='same', subsample=(2, 2), init='he_uniform')(ip) return x
def res_block(x, n_filters): res = Conv2D(filters=n_filters, kernel_size=(3, 3), strides=(1, 1), padding='same')(x) res = InstanceNormalization(axis=3)(res) res = LeakyReLU(0.2)(res) # res = Conv2D(filters=n_filters, kernel_size=(3, 3), strides=(1, 1), padding='same')(res) res = AtrousConvolution2D(n_filters, 3, 3, atrous_rate=(2, 2), border_mode='same')(res) res = InstanceNormalization(axis=3)(res) res = Activation('relu')(res) # res = LeakyReLU(0.2)(res) # # res = Conv2D(filters=n_filters, kernel_size=(1, 1), strides=(1, 1), padding='same')(res) # res = InstanceNormalization(axis=3)(res) # res = LeakyReLU(0.2)(res) x = Conv2D(filters=n_filters, kernel_size=(1, 1), strides=(1, 1), padding='same')(x) out = Add()([x, res]) return out
def get_dilation_model_voc( input_shape=(500, 500, 3), apply_softmax=True, classes=21 ): model_in = Input(shape=input_shape) # else: # if not K.is_keras_tensor(input_tensor): # model_in = Input(tensor=input_tensor, shape=input_shape) # else: # model_in = input_tensor h = Convolution2D(64, 3, 3, activation='relu', name='conv1_1')(model_in) h = Convolution2D(64, 3, 3, activation='relu', name='conv1_2')(h) h = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool1')(h) h = Convolution2D(128, 3, 3, activation='relu', name='conv2_1')(h) h = Convolution2D(128, 3, 3, activation='relu', name='conv2_2')(h) h = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool2')(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_1')(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_2')(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_3')(h) h = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool3')(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_1')(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_2')(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_3')(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_1')(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_2')(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_3')(h) h = AtrousConvolution2D(4096, 7, 7, atrous_rate=(4, 4), activation='relu', name='fc6')(h) h = Dropout(0.5, name='drop6')(h) h = Convolution2D(4096, 1, 1, activation='relu', name='fc7')(h) h = Dropout(0.5, name='drop7')(h) h = Convolution2D(classes, 1, 1, activation='relu', name='fc-final')(h) h = ZeroPadding2D(padding=(33, 33))(h) h = Convolution2D(2 * classes, 3, 3, activation='relu', name='ct_conv1_1')(h) h = Convolution2D(2 * classes, 3, 3, activation='relu', name='ct_conv1_2')(h) h = AtrousConvolution2D(4 * classes, 3, 3, atrous_rate=(2, 2), activation='relu', name='ct_conv2_1')(h) h = AtrousConvolution2D(8 * classes, 3, 3, atrous_rate=(4, 4), activation='relu', name='ct_conv3_1')(h) h = AtrousConvolution2D(16 * classes, 3, 3, atrous_rate=(8, 8), activation='relu', name='ct_conv4_1')(h) h = AtrousConvolution2D(32 * classes, 3, 3, atrous_rate=(16, 16), activation='relu', name='ct_conv5_1')(h) h = Convolution2D(32 * classes, 3, 3, activation='relu', name='ct_fc1')(h) model_out = Convolution2D(classes, 1, 1, name='ct_final', activation="softmax")(h) # if apply_softmax: # model_out = softmax(logits) # else: # model_out = logits model = Model(input=model_in, output=model_out, name='dilation_voc12') return model
def build_block(input, filters, r, c, border_mode='same'): x = input conv1 = BatchNormalization(axis=1)(x) conv1 = AtrousConvolution2D(filters / 2, 1, 1, border_mode=border_mode)(x) conv1 = SReLU()(conv1) conv1 = BatchNormalization(axis=1)(conv1) conv1 = AtrousConvolution2D(filters / 2, 3, 3, border_mode=border_mode)(x) conv1 = SReLU()(conv1) conv1 = BatchNormalization(axis=1)(conv1) conv1 = AtrousConvolution2D(filters, r, c, border_mode=border_mode)(conv1) conv1 = SReLU()(conv1) y = conv1 y = merge([x, y], mode="sum") return y
def f(input): l1 = AtrousConvolution2D(n_atrous_filters, atrous_filter_size, 1, atrous_rate=(atrous_rate, 1), border_mode='same')(input) l2a = Convolution2D(n_conv_filters, conv_filter_size, 1, activation='tanh', border_mode='same')(l1) l2b = Convolution2D(n_conv_filters, conv_filter_size, 1, activation='sigmoid', border_mode='same')(l1) l2 = merge([l2a, l2b], mode='mul') l3 = Convolution2D(1, 1, 1, activation='relu', border_mode='same')(l2) l4 = merge([l3, input], mode='sum') return l4, l3
def vision_block_CONV2D(input_layer, number_of_filters_vec, kernel_size_vec, \ flag_dilated_convolution_vec, \ flag_resnet_vec, flag_size_1_convolution_on_shortcut_in_resnet_vec, \ flag_batch_normalization_vec, \ flag_size_1_convolution_after_2D_convolution_vec, flag_batch_normalization_after_size_1_convolution_vec, \ activation_type_str_vec): number_of_kernel_sizes_in_layer = length(kernel_size_vec); for kernel_size_counter in arange(0,number_of_kernel_sizes_in_layer,1): #Get parameters for each kernel size filters in layer: number_of_filters = number_of_filters_vec[kernel_size_counter]; kernel_size = kernel_size_vec[kernel_size_counter]; flag_dilated_convolution = flag_dilated_convolution_vec[kernel_size_counter]; flag_resnet = flag_resnet_vec[kernel_size_counter]; flag_size_1_convolution_on_shortcut_in_resnet = flag_size_1_convolution_on_shortcut_in_resnet_vec[kernel_size_counter]; flag_batch_normalization = flag_batch_normalization_vec[kernel_size_counter]; flag_size_1_convolution_after_2D_convolution = flag_size_1_convolution_after_2D_convolution_vec[kernel_size_counter]; flag_batch_normalization_after_size_1_convolution = flag_batch_normalization_after_size_1_convolution_vec[kernel_size_counter]; activation_type_str = activation_type_str_vec[kernel_size_counter]; if flag_dilated_convolution==1: vision_block_current_kernel_size = AtrousConvolution2D(number_of_filters, kernel_size, atrous_rate=dilation_rate,border_mode='same')(input_layer); else: vision_block_current_kernel_size = Conv2D(number_of_filters, kernel_size,padding='same')(input_layer); if flag_batch_normalization==1: vision_block_current_kernel_size = BatchNormalization()(vision_block_current_kernel_size); vision_block_current_kernel_size = Activation(activation_type_str)(vision_block_current_kernel_size); if flag_size_1_convolution_after_2D_convolution==1: vision_block_current_kernel_size = Conv2D(number_of_filters, 1, padding='same')(vision_block_current_kernel_size); if flag_batch_normalization_after_size_1_convolution==1: vision_block_current_kernel_size = BatchNormalization()(vision_block_current_kernel_size); vision_block_current_kernel_size = Activation(activation_type_str)(vision_block_current_kernel_size); if flag_resnet==1: if flag_size_1_convolution_on_shortcut_in_resnet==1: input_layer = Conv2D(number_of_filters,1,border_mode='same'); if flag_batch_normalization_after_size_1_convolution==1: input_layer = BatchNormalization()(input_layer); vision_block_current_kernel_size = Merge([vision_block_current_kernel_size,input_layer],mode='sum') if kernel_size_counter == 0: vision_block = vision_block_current_kernel_size; else: vision_block = Concatenate([vision_block,vision_block_current_kernel_size]); #END OF KERNEL SIZE FOR LOOP return vision_block
def dilated_layer(tparams, filter_size, number_filter, prefix, input_dims, atrous_rate, activation): c1 = AtrousConvolution2D(number_filter, filter_size, filter_size, atrous_rate=atrous_rate, border_mode='same', activation=activation) c1.build(input_dims) c1.W = tparams[prefix + '_w'] c1.b = tparams[prefix + '_b'] return c1
def dilatedOrg(input_width=500, input_height=500): model = Sequential() # model.add(ZeroPadding2D((1, 1), input_shape=(input_width, input_height, 3))) model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1', input_shape=(input_width, input_height, 3))) model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1')) model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1')) model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2')) model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3')) model.add(MaxPooling2D((2, 2), strides=(2, 2))) model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1')) model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2')) model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3')) # Compared to the original VGG16, we skip the next 2 MaxPool layers, # and go ahead with dilated convolutional layers instead model.add(AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_1')) model.add(AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_2')) model.add(AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_3')) # Compared to the VGG16, we replace the FC layer with a convolution model.add(AtrousConvolution2D(4096, 7, 7, atrous_rate=(4, 4), activation='relu', name='fc6')) model.add(Dropout(0.5)) model.add(Convolution2D(4096, 1, 1, activation='relu', name='fc7')) model.add(Dropout(0.5)) # Note: this layer has linear activations, not ReLU model.add(Convolution2D(21, 1, 1, activation='linear', name='fc-final')) print((model.summary())) # model.layers[-1].output_shape == (None, 16, 16, 21) return model
def model_dilated2(params): # params h=params['row'] w=params['col'] lr=params['lr'] weights_path=params['weights'] C=32 inputs = Input((1, h, w)) conv1 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(inputs) conv2 = AtrousConvolution2D(C, 3, 3, atrous_rate=(1,1), activation='relu', border_mode='same')(conv1) conv3 = AtrousConvolution2D(C, 3, 3, atrous_rate=(2,2), activation='relu', border_mode='same')(conv2) conv4 = AtrousConvolution2D(C, 3, 3, atrous_rate=(4,4), activation='relu', border_mode='same')(conv3) conv5 = AtrousConvolution2D(C, 3, 3, atrous_rate=(8,8), activation='relu', border_mode='same')(conv4) conv6 = AtrousConvolution2D(C, 3, 3, atrous_rate=(16,16), activation='relu', border_mode='same')(conv5) conv7 = AtrousConvolution2D(C, 3, 3, atrous_rate=(32,32), activation='relu', border_mode='same')(conv6) conv8 = AtrousConvolution2D(C, 3, 3, atrous_rate=(1,1), activation='relu', border_mode='same')(conv7) conv9 = AtrousConvolution2D(192, 3, 3, atrous_rate=(1,1), activation='relu', border_mode='same')(conv8) conv10 = Convolution2D(1, 1, 1, activation='sigmoid', border_mode='same')(conv9) model = Model(input=inputs, output=conv10) print 'output: ', model.output_shape #load previous weights if weights_path: model.load_weights(weights_path) model.compile(optimizer=Adam(lr), loss=dice_coef_loss, metrics=[dice_coef]) #model.compile(optimizer=Adam(lr), Nestrov=True, loss='mean_squared_error', metrics=loss) #model.compile(loss='binary_crossentropy', optimizer=Adam(lr)) return model
def DACblock(x, n_filters): feature1 = AtrousConvolution2D(nb_filter=n_filters, nb_row=3, nb_col=3, atrous_rate=(1, 1), border_mode='same')(x) feature2 = AtrousConvolution2D(nb_filter=n_filters, nb_row=3, nb_col=3, atrous_rate=(3, 3), border_mode='same')(x) feature2 = AtrousConvolution2D(nb_filter=n_filters, nb_row=1, nb_col=1, atrous_rate=(1, 1), border_mode='same')(feature2) feature3 = AtrousConvolution2D(nb_filter=n_filters, nb_row=3, nb_col=3, atrous_rate=(3, 3), border_mode='same')(feature1) feature4 = AtrousConvolution2D(nb_filter=n_filters, nb_row=3, nb_col=3, atrous_rate=(5, 5), border_mode='same')(feature3) feature3 = AtrousConvolution2D(nb_filter=n_filters, nb_row=1, nb_col=1, atrous_rate=(1, 1), border_mode='same')(feature3) feature4 = AtrousConvolution2D(nb_filter=n_filters, nb_row=1, nb_col=1, atrous_rate=(1, 1), border_mode='same')(feature4) return add([x, feature1, feature2, feature3, feature4])
def dilnet2(input_width, input_height) -> Sequential: model = Sequential() model.add( Conv2D(16, 2, activation='relu', input_shape=(input_width, input_height, NUM_CHANNELS))) model.add(Conv2D(16, 2, activation='relu')) model.add(Conv2D(16, 2, activation='relu')) model.add(Conv2D(32, 2, activation='relu')) model.add(Conv2D(32, 2, activation='relu')) model.add(Conv2D(32, 2, activation='relu')) model.add(AtrousConvolution2D(64, 4, atrous_rate=(2, 2), activation='relu')) model.add(Dropout(0.5)) model.add(Convolution2D(64, 2, activation='relu')) model.add(Dropout(0.5)) model.add(Convolution2D(1, 1, activation='linear')) model.add(GlobalAveragePooling2D()) model.add(Activation('sigmoid')) return model
def SSD300(input_shape, num_classes=21): """SSD300 architecture. # Arguments input_shape: Shape of the input image, expected to be either (300, 300, 3) or (3, 300, 300)(not tested). num_classes: Number of classes including background. # References https://arxiv.org/abs/1512.02325 """ net = {} # Block 1 input_tensor = input_tensor = Input(shape=input_shape) img_size = (input_shape[1], input_shape[0]) net['input'] = input_tensor net['conv1_1'] = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_1')(net['input']) net['conv1_2'] = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_2')(net['conv1_1']) net['pool1'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool1')(net['conv1_2']) # Block 2 net['conv2_1'] = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_1')(net['pool1']) net['conv2_2'] = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_2')(net['conv2_1']) net['pool2'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool2')(net['conv2_2']) # Block 3 net['conv3_1'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_1')(net['pool2']) net['conv3_2'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_2')(net['conv3_1']) net['conv3_3'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_3')(net['conv3_2']) net['pool3'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool3')(net['conv3_3']) # Block 4 net['conv4_1'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_1')(net['pool3']) net['conv4_2'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_2')(net['conv4_1']) net['conv4_3'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_3')(net['conv4_2']) net['pool4'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool4')(net['conv4_3']) # Block 5 net['conv5_1'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv5_1')(net['pool4']) net['conv5_2'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv5_2')(net['conv5_1']) net['conv5_3'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv5_3')(net['conv5_2']) net['pool5'] = MaxPooling2D((3, 3), strides=(1, 1), border_mode='same', name='pool5')(net['conv5_3']) # FC6 net['fc6'] = AtrousConvolution2D(1024, 3, 3, atrous_rate=(6, 6), activation='relu', border_mode='same', name='fc6')(net['pool5']) # x = Dropout(0.5, name='drop6')(x) # FC7 net['fc7'] = Convolution2D(1024, 1, 1, activation='relu', border_mode='same', name='fc7')(net['fc6']) # x = Dropout(0.5, name='drop7')(x) # Block 6 net['conv6_1'] = Convolution2D(256, 1, 1, activation='relu', border_mode='same', name='conv6_1')(net['fc7']) net['conv6_2'] = Convolution2D(512, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv6_2')(net['conv6_1']) # Block 7 net['conv7_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv7_1')(net['conv6_2']) net['conv7_2'] = ZeroPadding2D()(net['conv7_1']) net['conv7_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='valid', name='conv7_2')(net['conv7_2']) # Block 8 net['conv8_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv8_1')(net['conv7_2']) net['conv8_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv8_2')(net['conv8_1']) # Last Pool net['pool6'] = GlobalAveragePooling2D(name='pool6')(net['conv8_2']) # Prediction from conv4_3 net['conv4_3_norm'] = Normalize(20, name='conv4_3_norm')(net['conv4_3']) num_priors = 3 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv4_3_norm_mbox_loc')(net['conv4_3_norm']) net['conv4_3_norm_mbox_loc'] = x flatten = Flatten(name='conv4_3_norm_mbox_loc_flat') net['conv4_3_norm_mbox_loc_flat'] = flatten(net['conv4_3_norm_mbox_loc']) name = 'conv4_3_norm_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv4_3_norm']) net['conv4_3_norm_mbox_conf'] = x flatten = Flatten(name='conv4_3_norm_mbox_conf_flat') net['conv4_3_norm_mbox_conf_flat'] = flatten(net['conv4_3_norm_mbox_conf']) priorbox = PriorBox(img_size, 30.0, aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv4_3_norm_mbox_priorbox') net['conv4_3_norm_mbox_priorbox'] = priorbox(net['conv4_3_norm']) # Prediction from fc7 num_priors = 6 net['fc7_mbox_loc'] = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='fc7_mbox_loc')(net['fc7']) flatten = Flatten(name='fc7_mbox_loc_flat') net['fc7_mbox_loc_flat'] = flatten(net['fc7_mbox_loc']) name = 'fc7_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) net['fc7_mbox_conf'] = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['fc7']) flatten = Flatten(name='fc7_mbox_conf_flat') net['fc7_mbox_conf_flat'] = flatten(net['fc7_mbox_conf']) priorbox = PriorBox(img_size, 60.0, max_size=114.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='fc7_mbox_priorbox') net['fc7_mbox_priorbox'] = priorbox(net['fc7']) # Prediction from conv6_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv6_2_mbox_loc')(net['conv6_2']) net['conv6_2_mbox_loc'] = x flatten = Flatten(name='conv6_2_mbox_loc_flat') net['conv6_2_mbox_loc_flat'] = flatten(net['conv6_2_mbox_loc']) name = 'conv6_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv6_2']) net['conv6_2_mbox_conf'] = x flatten = Flatten(name='conv6_2_mbox_conf_flat') net['conv6_2_mbox_conf_flat'] = flatten(net['conv6_2_mbox_conf']) priorbox = PriorBox(img_size, 114.0, max_size=168.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv6_2_mbox_priorbox') net['conv6_2_mbox_priorbox'] = priorbox(net['conv6_2']) # Prediction from conv7_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv7_2_mbox_loc')(net['conv7_2']) net['conv7_2_mbox_loc'] = x flatten = Flatten(name='conv7_2_mbox_loc_flat') net['conv7_2_mbox_loc_flat'] = flatten(net['conv7_2_mbox_loc']) name = 'conv7_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv7_2']) net['conv7_2_mbox_conf'] = x flatten = Flatten(name='conv7_2_mbox_conf_flat') net['conv7_2_mbox_conf_flat'] = flatten(net['conv7_2_mbox_conf']) priorbox = PriorBox(img_size, 168.0, max_size=222.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv7_2_mbox_priorbox') net['conv7_2_mbox_priorbox'] = priorbox(net['conv7_2']) # Prediction from conv8_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv8_2_mbox_loc')(net['conv8_2']) net['conv8_2_mbox_loc'] = x flatten = Flatten(name='conv8_2_mbox_loc_flat') net['conv8_2_mbox_loc_flat'] = flatten(net['conv8_2_mbox_loc']) name = 'conv8_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv8_2']) net['conv8_2_mbox_conf'] = x flatten = Flatten(name='conv8_2_mbox_conf_flat') net['conv8_2_mbox_conf_flat'] = flatten(net['conv8_2_mbox_conf']) priorbox = PriorBox(img_size, 222.0, max_size=276.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv8_2_mbox_priorbox') net['conv8_2_mbox_priorbox'] = priorbox(net['conv8_2']) # Prediction from pool6 num_priors = 6 x = Dense(num_priors * 4, name='pool6_mbox_loc_flat')(net['pool6']) net['pool6_mbox_loc_flat'] = x name = 'pool6_mbox_conf_flat' if num_classes != 21: name += '_{}'.format(num_classes) x = Dense(num_priors * num_classes, name=name)(net['pool6']) net['pool6_mbox_conf_flat'] = x priorbox = PriorBox(img_size, 276.0, max_size=330.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='pool6_mbox_priorbox') if K.image_dim_ordering() == 'tf': target_shape = (1, 1, 256) else: target_shape = (256, 1, 1) net['pool6_reshaped'] = Reshape(target_shape, name='pool6_reshaped')(net['pool6']) net['pool6_mbox_priorbox'] = priorbox(net['pool6_reshaped']) # Gather all predictions net['mbox_loc'] = concatenate([ net['conv4_3_norm_mbox_loc_flat'], net['fc7_mbox_loc_flat'], net['conv6_2_mbox_loc_flat'], net['conv7_2_mbox_loc_flat'], net['conv8_2_mbox_loc_flat'], net['pool6_mbox_loc_flat'] ], axis=1, name='mbox_loc') net['mbox_conf'] = concatenate([ net['conv4_3_norm_mbox_conf_flat'], net['fc7_mbox_conf_flat'], net['conv6_2_mbox_conf_flat'], net['conv7_2_mbox_conf_flat'], net['conv8_2_mbox_conf_flat'], net['pool6_mbox_conf_flat'] ], axis=1, name='mbox_conf') net['mbox_priorbox'] = concatenate([ net['conv4_3_norm_mbox_priorbox'], net['fc7_mbox_priorbox'], net['conv6_2_mbox_priorbox'], net['conv7_2_mbox_priorbox'], net['conv8_2_mbox_priorbox'], net['pool6_mbox_priorbox'] ], axis=1, name='mbox_priorbox') if hasattr(net['mbox_loc'], '_keras_shape'): num_boxes = net['mbox_loc']._keras_shape[-1] // 4 elif hasattr(net['mbox_loc'], 'int_shape'): num_boxes = K.int_shape(net['mbox_loc'])[-1] // 4 net['mbox_loc'] = Reshape((num_boxes, 4), name='mbox_loc_final')(net['mbox_loc']) net['mbox_conf'] = Reshape((num_boxes, num_classes), name='mbox_conf_logits')(net['mbox_conf']) net['mbox_conf'] = Activation('softmax', name='mbox_conf_final')(net['mbox_conf']) net['predictions'] = concatenate( [net['mbox_loc'], net['mbox_conf'], net['mbox_priorbox']], axis=2, name='predictions') model = Model(net['input'], net['predictions']) return model
def DeeplabV2(input_shape, upsampling=8, apply_softmax=True, weights='voc2012', input_tensor=None, classes=21): """Instantiate the DeeplabV2 architecture with VGG16 encoder, optionally loading weights pre-trained on VOC2012 segmentation. Note that pre-trained model is only available for Theano dim ordering. The model and the weights should be compatible with both TensorFlow and Theano backends. # Arguments input_shape: shape tuple. It should have exactly 3 inputs channels, and the axis ordering should be coherent with what specified in your keras.json (e.g. use (3, 512, 512) for 'th' and (512, 512, 3) for 'tf'). upsampling: final front end upsampling (default is 8x). apply_softmax: whether to apply softmax or return logits. weights: one of `None` (random initialization) or `voc2012` (pre-training on VOC2012 segmentation). input_tensor: optional Keras tensor (i.e. output of `layers.Input()`) to use as image input for the model. classes: optional number of classes to classify images into, only to be specified if `include_top` is True, and if no `weights` argument is specified. # Returns A Keras model instance. """ if weights not in {'voc2012', None}: raise ValueError('The `weights` argument should be either ' '`None` (random initialization) or `voc2012` ' '(pre-training on VOC2012 segmentation).') if weights == 'voc2012' and classes != 21: raise ValueError( 'If using `weights` as voc2012 `classes` should be 21') if input_shape is None: raise ValueError('Please provide a valid input_shape to deeplab') if input_tensor is None: img_input = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # Block 1 h = ZeroPadding2D(padding=(1, 1))(img_input) h = Convolution2D(64, 3, 3, activation='relu', name='conv1_1')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(64, 3, 3, activation='relu', name='conv1_2')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(h) # Block 2 h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(128, 3, 3, activation='relu', name='conv2_1')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(128, 3, 3, activation='relu', name='conv2_2')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(h) # Block 3 h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_1')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_2')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_3')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = MaxPooling2D(pool_size=(3, 3), strides=(2, 2))(h) # Block 4 h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_1')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_2')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_3')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = MaxPooling2D(pool_size=(3, 3), strides=(1, 1))(h) # Block 5 h = ZeroPadding2D(padding=(2, 2))(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_1')(h) h = ZeroPadding2D(padding=(2, 2))(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_2')(h) h = ZeroPadding2D(padding=(2, 2))(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_3')(h) h = ZeroPadding2D(padding=(1, 1))(h) p5 = MaxPooling2D(pool_size=(3, 3), strides=(1, 1))(h) # branching for Atrous Spatial Pyramid Pooling # hole = 6 b1 = ZeroPadding2D(padding=(6, 6))(p5) b1 = AtrousConvolution2D(1024, 3, 3, atrous_rate=(6, 6), activation='relu', name='fc6_1')(b1) b1 = Dropout(0.5)(b1) b1 = Convolution2D(1024, 1, 1, activation='relu', name='fc7_1')(b1) b1 = Dropout(0.5)(b1) b1 = Convolution2D(21, 1, 1, activation='relu', name='fc8_voc12_1')(b1) # hole = 12 b2 = ZeroPadding2D(padding=(12, 12))(p5) b2 = AtrousConvolution2D(1024, 3, 3, atrous_rate=(12, 12), activation='relu', name='fc6_2')(b2) b2 = Dropout(0.5)(b2) b2 = Convolution2D(1024, 1, 1, activation='relu', name='fc7_2')(b2) b2 = Dropout(0.5)(b2) b2 = Convolution2D(21, 1, 1, activation='relu', name='fc8_voc12_2')(b2) # hole = 18 b3 = ZeroPadding2D(padding=(18, 18))(p5) b3 = AtrousConvolution2D(1024, 3, 3, atrous_rate=(18, 18), activation='relu', name='fc6_3')(b3) b3 = Dropout(0.5)(b3) b3 = Convolution2D(1024, 1, 1, activation='relu', name='fc7_3')(b3) b3 = Dropout(0.5)(b3) b3 = Convolution2D(21, 1, 1, activation='relu', name='fc8_voc12_3')(b3) # hole = 24 b4 = ZeroPadding2D(padding=(24, 24))(p5) b4 = AtrousConvolution2D(1024, 3, 3, atrous_rate=(24, 24), activation='relu', name='fc6_4')(b4) b4 = Dropout(0.5)(b4) b4 = Convolution2D(1024, 1, 1, activation='relu', name='fc7_4')(b4) b4 = Dropout(0.5)(b4) b4 = Convolution2D(21, 1, 1, activation='relu', name='fc8_voc12_4')(b4) s = merge([b1, b2, b3, b4], mode='sum') logits = BilinearUpsampling(upsampling=upsampling)(s) if apply_softmax: out = softmax(logits) else: out = logits # Ensure that the model takes into account # any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model. model = Model(inputs, out, name='deeplabV2') # load weights if weights == 'voc2012': if K.image_dim_ordering() == 'th': weights_path = get_file('deeplabV2_weights_th.h5', TH_WEIGHTS_PATH, cache_subdir='models') model.load_weights(weights_path) if K.backend() == 'tensorflow': warnings.warn('You are using the TensorFlow backend, yet you ' 'are using the Theano ' 'image dimension ordering convention ' '(`image_dim_ordering="th"`). ' 'For best performance, set ' '`image_dim_ordering="tf"` in ' 'your Keras config ' 'at ~/.keras/keras.json.') convert_all_kernels_in_model(model) else: raise NotImplementedError( 'Pretrained DeeplabV2 model is not available for' 'voc2012 dataset and tensorflow dim ordering') return model
def SSDXception_BN(input_shape, num_classes=2): """SSD300 + XCeption architecture. # Arguments input_shape: Shape of the input image, expected to be either (300, 300, 3) or (3, 300, 300)(not tested). num_classes: Number of classes including background. # References https://arxiv.org/abs/1512.02325 """ net = {} img_size = (input_shape[1], input_shape[0]) input_tensor = Input(shape=input_shape) net['input'] = input_tensor xception_head = Xception(include_top=False, input_tensor=input_tensor) activation_layer_names = [] for layer in xception_head.layers: # print(layer.name) if layer.outbound_nodes: net[layer.name] = layer.output layer.trainable = False # print(layer.name) if layer.name.startswith('activation'): activation_layer_names.append(layer.name) # print(activation_layer_names) # prev_size_layer_name = activation_layer_names[-10] net['pool5'] = AveragePooling2D((3, 3), strides=(1, 1), border_mode='same', name='pool5')(xception_head.output) # FC6 net['fc6'] = AtrousConvolution2D(512, 3, 3, atrous_rate=(3, 3), activation='relu', border_mode='same', name='fc6')(net['pool5']) # x = Dropout(0.5, name='drop6')(x) # FC7 # net['fc7'] = Convolution2D(1024, 1, 1, activation='relu', # border_mode='same', name='fc7')(net['fc6']) net['fc7'] = net['fc6'] # net['fc7'] = xception_head.output # x = Dropout(0.5, name='drop7')(x) # Block 6 net['conv6_1'] = wrap_with_bn( net, Convolution2D(256, 1, 1, activation='linear', border_mode='same', name='conv6_1')(net['fc7']), name='6_1') net['conv6_2'] = Convolution2D(512, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv6_2')(net['conv6_1']) # Block 7 net['conv7_1'] = wrap_with_bn( net, Convolution2D(128, 1, 1, activation='linear', border_mode='same', name='conv7_1')(net['conv6_2']), name='7_1') net['conv7_2'] = ZeroPadding2D()(net['conv7_1']) net['conv7_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='valid', name='conv7_2')(net['conv7_2']) # Block 8 net['conv8_1'] = wrap_with_bn( net, Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv8_1')(net['conv7_2']), name='8_1') net['conv8_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv8_2')(net['conv8_1']) # Last Pool net['pool6'] = GlobalAveragePooling2D(name='pool6')(net['conv8_2']) # Prediction from fc7 num_priors = 6 net['fc7_mbox_loc'] = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='fc7_mbox_loc')(net['fc7']) flatten = Flatten(name='fc7_mbox_loc_flat') net['fc7_mbox_loc_flat'] = flatten(net['fc7_mbox_loc']) name = 'fc7_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) net['fc7_mbox_conf'] = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['fc7']) flatten = Flatten(name='fc7_mbox_conf_flat') net['fc7_mbox_conf_flat'] = flatten(net['fc7_mbox_conf']) priorbox = PriorBox(img_size, min_size=180, max_size=180.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='fc7_mbox_priorbox') net['fc7_mbox_priorbox'] = priorbox(net['fc7']) # Prediction from conv6_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv6_2_mbox_loc')(net['conv6_2']) net['conv6_2_mbox_loc'] = x flatten = Flatten(name='conv6_2_mbox_loc_flat') net['conv6_2_mbox_loc_flat'] = flatten(net['conv6_2_mbox_loc']) name = 'conv6_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv6_2']) net['conv6_2_mbox_conf'] = x flatten = Flatten(name='conv6_2_mbox_conf_flat') net['conv6_2_mbox_conf_flat'] = flatten(net['conv6_2_mbox_conf']) priorbox = PriorBox(img_size, 280.0, max_size=280.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv6_2_mbox_priorbox') net['conv6_2_mbox_priorbox'] = priorbox(net['conv6_2']) # Prediction from conv7_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv7_2_mbox_loc')(net['conv7_2']) net['conv7_2_mbox_loc'] = x flatten = Flatten(name='conv7_2_mbox_loc_flat') net['conv7_2_mbox_loc_flat'] = flatten(net['conv7_2_mbox_loc']) name = 'conv7_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv7_2']) net['conv7_2_mbox_conf'] = x flatten = Flatten(name='conv7_2_mbox_conf_flat') net['conv7_2_mbox_conf_flat'] = flatten(net['conv7_2_mbox_conf']) priorbox = PriorBox(img_size, 420.0, max_size=420.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv7_2_mbox_priorbox') net['conv7_2_mbox_priorbox'] = priorbox(net['conv7_2']) # Prediction from conv8_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv8_2_mbox_loc')(net['conv8_2']) net['conv8_2_mbox_loc'] = x flatten = Flatten(name='conv8_2_mbox_loc_flat') net['conv8_2_mbox_loc_flat'] = flatten(net['conv8_2_mbox_loc']) name = 'conv8_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv8_2']) net['conv8_2_mbox_conf'] = x flatten = Flatten(name='conv8_2_mbox_conf_flat') net['conv8_2_mbox_conf_flat'] = flatten(net['conv8_2_mbox_conf']) priorbox = PriorBox(img_size, 640.0, max_size=640.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv8_2_mbox_priorbox') net['conv8_2_mbox_priorbox'] = priorbox(net['conv8_2']) # Prediction from pool6 num_priors = 6 x = Dense(num_priors * 4, name='pool6_mbox_loc_flat')(net['pool6']) net['pool6_mbox_loc_flat'] = x name = 'pool6_mbox_conf_flat' if num_classes != 21: name += '_{}'.format(num_classes) x = Dense(num_priors * num_classes, name=name)(net['pool6']) net['pool6_mbox_conf_flat'] = x priorbox = PriorBox(img_size, 900.0, max_size=900.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='pool6_mbox_priorbox') if K.image_dim_ordering() == 'tf': target_shape = (1, 1, 256) else: target_shape = (256, 1, 1) net['pool6_reshaped'] = Reshape(target_shape, name='pool6_reshaped')(net['pool6']) net['pool6_mbox_priorbox'] = priorbox(net['pool6_reshaped']) # Gather all predictions net['mbox_loc'] = merge([ net['fc7_mbox_loc_flat'], net['conv6_2_mbox_loc_flat'], net['conv7_2_mbox_loc_flat'], net['conv8_2_mbox_loc_flat'], net['pool6_mbox_loc_flat']], mode='concat', concat_axis=1, name='mbox_loc') net['mbox_conf'] = merge([ net['fc7_mbox_conf_flat'], net['conv6_2_mbox_conf_flat'], net['conv7_2_mbox_conf_flat'], net['conv8_2_mbox_conf_flat'], net['pool6_mbox_conf_flat']], mode='concat', concat_axis=1, name='mbox_conf') net['mbox_priorbox'] = merge([ net['fc7_mbox_priorbox'], net['conv6_2_mbox_priorbox'], net['conv7_2_mbox_priorbox'], net['conv8_2_mbox_priorbox'], net['pool6_mbox_priorbox']], mode='concat', concat_axis=1, name='mbox_priorbox') if hasattr(net['mbox_loc'], '_keras_shape'): num_boxes = net['mbox_loc']._keras_shape[-1] // 4 elif hasattr(net['mbox_loc'], 'int_shape'): num_boxes = K.int_shape(net['mbox_loc'])[-1] // 4 net['mbox_loc'] = Reshape((num_boxes, 4), name='mbox_loc_final')(net['mbox_loc']) net['mbox_conf'] = Reshape((num_boxes, num_classes), name='mbox_conf_logits')(net['mbox_conf']) net['mbox_conf'] = Activation('softmax', name='mbox_conf_final')(net['mbox_conf']) net['predictions'] = merge([net['mbox_loc'], net['mbox_conf'], net['mbox_priorbox']], mode='concat', concat_axis=2, name='predictions') model = Model(net['input'], net['predictions']) return model
def SSD_Densenet(input_shape, num_classes=2): """SSD300 + Resnet50 architecture. # Arguments input_shape: Shape of the input image, expected to be either (300, 300, 3) or (3, 300, 300)(not tested). num_classes: Number of classes including background. # References https://arxiv.org/abs/1512.02325 """ net = {} img_size = (input_shape[1], input_shape[0]) depth = 100 nb_dense_block = 5 growth_rate = 12 nb_filter = -1 bottleneck = True reduction = 0.5 dropout_rate = 0.0 # 0.0 for data augmentation densenet_head = ssd.densenet.create_dense_net( nb_classes=2, img_dim=input_shape, depth=depth, nb_dense_block=nb_dense_block, growth_rate=growth_rate, nb_filter=nb_filter, bottleneck=bottleneck, reduction=reduction, dropout_rate=dropout_rate) densenet_head.layers.pop() densenet_head.layers.pop() densenet_head.load_weights( "/home/dmytro/data/DenseNet/weights/DenseNet-BC-100-12-CIFAR100-5.-0054-1.981.hdf5", by_name=True) # densenet_head.summary() # return densenet_head activation_layer_names = [] for layer in densenet_head.layers: # print(layer.name) if layer.outbound_nodes: net[layer.name] = layer.output layer.trainable = False # print(layer.name) if layer.name.startswith('activation'): print(len(activation_layer_names), layer.name) activation_layer_names.append(layer.name) # print(activation_layer_names) prev_size_layer_name = activation_layer_names[-34] print("prev size:", prev_size_layer_name) net['pool5'] = MaxPooling2D((3, 3), strides=(1, 1), border_mode='same', name='pool5')(net[activation_layer_names[-1]]) # FC6 net['fc6'] = AtrousConvolution2D(1024, 3, 3, atrous_rate=(6, 6), activation='relu', border_mode='same', name='fc6')(net['pool5']) # x = Dropout(0.5, name='drop6')(x) # FC7 net['fc7'] = Convolution2D(1024, 1, 1, activation='relu', border_mode='same', name='fc7')(net['fc6']) # x = Dropout(0.5, name='drop7')(x) # Block 6 net['conv6_1'] = wrap_with_bn_and_dropout(net, Convolution2D( 256, 1, 1, activation='linear', border_mode='same', name='conv6_1')(net['fc7']), name='6_1') net['conv6_2'] = Convolution2D(512, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv6_2')(net['conv6_1']) # Block 7 net['conv7_1'] = wrap_with_bn_and_dropout( net, Convolution2D(128, 1, 1, activation='linear', border_mode='same', name='conv7_1')(net['conv6_2']), name='7_1') net['conv7_2'] = ZeroPadding2D()(net['conv7_1']) net['conv7_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='valid', name='conv7_2')(net['conv7_2']) # Block 8 net['conv8_1'] = wrap_with_bn_and_dropout( net, Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv8_1')(net['conv7_2']), name='8_1') net['conv8_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv8_2')(net['conv8_1']) # Last Pool net['pool6'] = GlobalAveragePooling2D(name='pool6')(net['conv8_2']) # Prediction from conv4_3 net['conv4_3_norm'] = Normalize(20, name='conv4_3_norm')( net[prev_size_layer_name]) num_priors = 3 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv4_3_norm_mbox_loc')(net['conv4_3_norm']) net['conv4_3_norm_mbox_loc'] = x flatten = Flatten(name='conv4_3_norm_mbox_loc_flat') net['conv4_3_norm_mbox_loc_flat'] = flatten(net['conv4_3_norm_mbox_loc']) name = 'conv4_3_norm_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv4_3_norm']) net['conv4_3_norm_mbox_conf'] = x flatten = Flatten(name='conv4_3_norm_mbox_conf_flat') net['conv4_3_norm_mbox_conf_flat'] = flatten(net['conv4_3_norm_mbox_conf']) priorbox = PriorBox(img_size, 30.0, aspect_ratios=[2], variances=[0.1, 0.1, 0.2, 0.2], name='conv4_3_norm_mbox_priorbox') net['conv4_3_norm_mbox_priorbox'] = priorbox(net['conv4_3_norm']) # Prediction from fc7 num_priors = 6 net['fc7_mbox_loc'] = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='fc7_mbox_loc')(net['fc7']) flatten = Flatten(name='fc7_mbox_loc_flat') net['fc7_mbox_loc_flat'] = flatten(net['fc7_mbox_loc']) name = 'fc7_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) net['fc7_mbox_conf'] = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['fc7']) flatten = Flatten(name='fc7_mbox_conf_flat') net['fc7_mbox_conf_flat'] = flatten(net['fc7_mbox_conf']) priorbox = PriorBox(img_size, 60.0, max_size=114.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='fc7_mbox_priorbox') net['fc7_mbox_priorbox'] = priorbox(net['fc7']) # Prediction from conv6_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv6_2_mbox_loc')(net['conv6_2']) net['conv6_2_mbox_loc'] = x flatten = Flatten(name='conv6_2_mbox_loc_flat') net['conv6_2_mbox_loc_flat'] = flatten(net['conv6_2_mbox_loc']) name = 'conv6_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv6_2']) net['conv6_2_mbox_conf'] = x flatten = Flatten(name='conv6_2_mbox_conf_flat') net['conv6_2_mbox_conf_flat'] = flatten(net['conv6_2_mbox_conf']) priorbox = PriorBox(img_size, 114.0, max_size=168.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv6_2_mbox_priorbox') net['conv6_2_mbox_priorbox'] = priorbox(net['conv6_2']) # Prediction from conv7_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv7_2_mbox_loc')(net['conv7_2']) net['conv7_2_mbox_loc'] = x flatten = Flatten(name='conv7_2_mbox_loc_flat') net['conv7_2_mbox_loc_flat'] = flatten(net['conv7_2_mbox_loc']) name = 'conv7_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv7_2']) net['conv7_2_mbox_conf'] = x flatten = Flatten(name='conv7_2_mbox_conf_flat') net['conv7_2_mbox_conf_flat'] = flatten(net['conv7_2_mbox_conf']) priorbox = PriorBox(img_size, 168.0, max_size=222.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv7_2_mbox_priorbox') net['conv7_2_mbox_priorbox'] = priorbox(net['conv7_2']) # Prediction from conv8_2 num_priors = 6 x = Convolution2D(num_priors * 4, 3, 3, border_mode='same', name='conv8_2_mbox_loc')(net['conv8_2']) net['conv8_2_mbox_loc'] = x flatten = Flatten(name='conv8_2_mbox_loc_flat') net['conv8_2_mbox_loc_flat'] = flatten(net['conv8_2_mbox_loc']) name = 'conv8_2_mbox_conf' if num_classes != 21: name += '_{}'.format(num_classes) x = Convolution2D(num_priors * num_classes, 3, 3, border_mode='same', name=name)(net['conv8_2']) net['conv8_2_mbox_conf'] = x flatten = Flatten(name='conv8_2_mbox_conf_flat') net['conv8_2_mbox_conf_flat'] = flatten(net['conv8_2_mbox_conf']) priorbox = PriorBox(img_size, 222.0, max_size=276.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='conv8_2_mbox_priorbox') net['conv8_2_mbox_priorbox'] = priorbox(net['conv8_2']) # Prediction from pool6 num_priors = 6 x = Dense(num_priors * 4, name='pool6_mbox_loc_flat')(net['pool6']) net['pool6_mbox_loc_flat'] = x name = 'pool6_mbox_conf_flat' if num_classes != 21: name += '_{}'.format(num_classes) x = Dense(num_priors * num_classes, name=name)(net['pool6']) net['pool6_mbox_conf_flat'] = x priorbox = PriorBox(img_size, 276.0, max_size=330.0, aspect_ratios=[2, 3], variances=[0.1, 0.1, 0.2, 0.2], name='pool6_mbox_priorbox') if K.image_dim_ordering() == 'tf': target_shape = (1, 1, 256) else: target_shape = (256, 1, 1) net['pool6_reshaped'] = Reshape(target_shape, name='pool6_reshaped')(net['pool6']) net['pool6_mbox_priorbox'] = priorbox(net['pool6_reshaped']) # Gather all predictions net['mbox_loc'] = merge([ net['conv4_3_norm_mbox_loc_flat'], net['fc7_mbox_loc_flat'], net['conv6_2_mbox_loc_flat'], net['conv7_2_mbox_loc_flat'], net['conv8_2_mbox_loc_flat'], net['pool6_mbox_loc_flat'] ], mode='concat', concat_axis=1, name='mbox_loc') net['mbox_conf'] = merge([ net['conv4_3_norm_mbox_conf_flat'], net['fc7_mbox_conf_flat'], net['conv6_2_mbox_conf_flat'], net['conv7_2_mbox_conf_flat'], net['conv8_2_mbox_conf_flat'], net['pool6_mbox_conf_flat'] ], mode='concat', concat_axis=1, name='mbox_conf') net['mbox_priorbox'] = merge([ net['conv4_3_norm_mbox_priorbox'], net['fc7_mbox_priorbox'], net['conv6_2_mbox_priorbox'], net['conv7_2_mbox_priorbox'], net['conv8_2_mbox_priorbox'], net['pool6_mbox_priorbox'] ], mode='concat', concat_axis=1, name='mbox_priorbox') if hasattr(net['mbox_loc'], '_keras_shape'): num_boxes = net['mbox_loc']._keras_shape[-1] // 4 elif hasattr(net['mbox_loc'], 'int_shape'): num_boxes = K.int_shape(net['mbox_loc'])[-1] // 4 net['mbox_loc'] = Reshape((num_boxes, 4), name='mbox_loc_final')(net['mbox_loc']) net['mbox_conf'] = Reshape((num_boxes, num_classes), name='mbox_conf_logits')(net['mbox_conf']) net['mbox_conf'] = Activation('softmax', name='mbox_conf_final')(net['mbox_conf']) net['predictions'] = merge( [net['mbox_loc'], net['mbox_conf'], net['mbox_priorbox']], mode='concat', concat_axis=2, name='predictions') model = Model(net['input'], net['predictions']) return model
def get_unet(self): inputs = Input([None, None, 1]) conv1 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(inputs) print("conv1 shape:", conv1.shape) conv1 = AtrousConvolution2D(64, 3, atrous_rate=(2, 2), activation='relu', padding='same', kernel_initializer='he_normal')(conv1) #conv1 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1) print("conv1 shape:", conv1.shape) pool1 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(conv1) print("pool1 shape:", pool1.shape) conv2 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool1) print("conv2 shape:", conv2.shape) conv2 = AtrousConvolution2D(128, 3, atrous_rate=(2, 2), activation='relu', padding='same', kernel_initializer='he_normal')(conv2) #conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2) print("conv2 shape:", conv2.shape) pool2 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(conv2) print("pool2 shape:", pool2.shape) conv3 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool2) print("conv3 shape:", conv3.shape) conv3 = AtrousConvolution2D(256, 3, atrous_rate=(2, 2), activation='relu', padding='same', kernel_initializer='he_normal')(conv3) #conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3) print("conv3 shape:", conv3.shape) pool3 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(conv3) print("pool3 shape:", pool3.shape) conv4 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool3) conv4 = AtrousConvolution2D(512, 3, atrous_rate=(2, 2), activation='relu', padding='same', kernel_initializer='he_normal')(conv4) drop4 = Dropout(0.5)(conv4) pool4 = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid')(drop4) conv5_1 = AtrousConvolution2D(1024, 3, atrous_rate=(1, 1), activation='relu', padding='same', kernel_initializer='he_normal')(pool4) conv5_2 = AtrousConvolution2D(1024, 3, atrous_rate=(2, 2), activation='relu', padding='same', kernel_initializer='he_normal')(conv5_1) #drop5 = Dropout(0.5)(conv5_3) up6 = Conv2D(512, 2, activation='relu', padding='same', kernel_initializer='he_normal')( UpSampling2D(size=(2, 2))(conv5_2)) #merge6 = merge([drop4,up6], mode = 'concat', concat_axis = 3) merge6 = concatenate([drop4, up6], axis=3) conv6 = AtrousConvolution2D(512, 3, atrous_rate=(1, 1), activation='relu', padding='same', kernel_initializer='he_normal')(merge6) conv6 = AtrousConvolution2D(512, 3, atrous_rate=(2, 2), activation='relu', padding='same', kernel_initializer='he_normal')(conv6) up7 = Conv2D(256, 2, activation='relu', padding='same', kernel_initializer='he_normal')( UpSampling2D(size=(2, 2))(conv6)) # merge7 = merge([conv3,up7], mode = 'concat', concat_axis = 3) #merge7 = concatenate([conv3, up7],axis=3) conv7 = AtrousConvolution2D(256, 3, atrous_rate=(1, 1), activation='relu', padding='same', kernel_initializer='he_normal')(up7) conv7 = AtrousConvolution2D(256, 3, atrous_rate=(2, 2), activation='relu', padding='same', kernel_initializer='he_normal')(conv7) up8 = Conv2D(128, 2, activation='relu', padding='same', kernel_initializer='he_normal')( UpSampling2D(size=(2, 2))(conv7)) # merge8 = merge([conv2,up8], mode = 'concat', concat_axis = 3) #merge8 = concatenate([conv2,up8],axis=3) conv8 = AtrousConvolution2D(128, 3, atrous_rate=(1, 1), activation='relu', padding='same', kernel_initializer='he_normal')(up8) conv8 = AtrousConvolution2D(128, 3, atrous_rate=(2, 2), activation='relu', padding='same', kernel_initializer='he_normal')(conv8) up9 = Conv2D(64, 2, activation='relu', padding='same', kernel_initializer='he_normal')( UpSampling2D(size=(2, 2))(conv8)) # merge9 = merge([conv1,up9], mode = 'concat', concat_axis = 3) #merge9 = concatenate([conv1,up9],axis=3) conv9 = AtrousConvolution2D(64, 3, atrous_rate=(1, 1), activation='relu', padding='same', kernel_initializer='he_normal')(up9) conv9 = AtrousConvolution2D(64, 3, atrous_rate=(2, 2), activation='relu', padding='same', kernel_initializer='he_normal')(conv9) conv9 = AtrousConvolution2D(2, 3, atrous_rate=(1, 1), activation='relu', padding='same', kernel_initializer='he_normal')(conv9) conv10 = Conv2D(1, 1, activation='sigmoid')(conv9) model = Model(input=inputs, output=conv10) model.compile(optimizer=Adam(lr=1e-4), loss='binary_crossentropy', metrics=['accuracy']) return model
def get_dilation_model_camvid(input_shape, apply_softmax, input_tensor, classes): if input_tensor is None: model_in = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): model_in = Input(tensor=input_tensor, shape=input_shape) else: model_in = input_tensor h = Convolution2D(64, 3, 3, activation='relu', name='conv1_1')(model_in) h = Convolution2D(64, 3, 3, activation='relu', name='conv1_2')(h) h = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool1')(h) h = Convolution2D(128, 3, 3, activation='relu', name='conv2_1')(h) h = Convolution2D(128, 3, 3, activation='relu', name='conv2_2')(h) h = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool2')(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_1')(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_2')(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_3')(h) h = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool3')(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_1')(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_2')(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_3')(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_1')(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_2')(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_3')(h) h = AtrousConvolution2D(4096, 7, 7, atrous_rate=(4, 4), activation='relu', name='fc6')(h) h = Dropout(0.5, name='drop6')(h) h = Convolution2D(4096, 1, 1, activation='relu', name='fc7')(h) h = Dropout(0.5, name='drop7')(h) h = Convolution2D(classes, 1, 1, name='final')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(classes, 3, 3, activation='relu', name='ctx_conv1_1')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(classes, 3, 3, activation='relu', name='ctx_conv1_2')(h) h = ZeroPadding2D(padding=(2, 2))(h) h = AtrousConvolution2D(classes, 3, 3, atrous_rate=(2, 2), activation='relu', name='ctx_conv2_1')(h) h = ZeroPadding2D(padding=(4, 4))(h) h = AtrousConvolution2D(classes, 3, 3, atrous_rate=(4, 4), activation='relu', name='ctx_conv3_1')(h) h = ZeroPadding2D(padding=(8, 8))(h) h = AtrousConvolution2D(classes, 3, 3, atrous_rate=(8, 8), activation='relu', name='ctx_conv4_1')(h) h = ZeroPadding2D(padding=(16, 16))(h) h = AtrousConvolution2D(classes, 3, 3, atrous_rate=(16, 16), activation='relu', name='ctx_conv5_1')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(classes, 3, 3, activation='relu', name='ctx_fc1')(h) logits = Convolution2D(classes, 1, 1, name='ctx_final')(h) if apply_softmax: model_out = softmax(logits) else: model_out = logits model = Model(input=model_in, output=model_out, name='dilation_camvid') return model
def vgg16_tt100k_base_network(input_shape, pretrained_weigths): """VGG16 base model with pretrained weights `pretrained_weigths` is an hdf5 file with the weights from a VGG16 model pretrained on the TT100K dataset. NOTE: expected input_shape for the model must be (64, 64, 3). It is the input shape we used for our trained vgg16 models. `input_shape` is a tuple (height, width, channels). Return a dict with the layers to use from the base model. """ base_model = vgg.build_vgg(img_shape=(64, 64, 3), n_classes=45, n_layers=16, freeze_layers_from=None) base_model.load_weights(pretrained_weigths) net = {} net['input'] = base_model.input net['conv4_3'] = base_model.get_layer('block4_conv3').output net['pool5'] = base_model.get_layer('block5_pool').output # Block 6 net['conv6'] = AtrousConvolution2D(1024, 3, 3, atrous_rate=(6, 6), activation='relu', border_mode='same', name='conv6')(net['pool5']) # Block 7 net['conv7'] = Convolution2D(1024, 1, 1, activation='relu', border_mode='same', name='conv7')(net['conv6']) # Block 8 net['conv8_1'] = Convolution2D(256, 1, 1, activation='relu', border_mode='same', name='conv8_1')(net['conv7']) net['conv8_2'] = Convolution2D(512, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv8_2')(net['conv8_1']) # Block 9 net['conv9_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv9_1')(net['conv8_2']) net['conv9_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv9_2')(net['conv9_1']) # Block 10 net['conv10_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv10_1')(net['conv9_2']) net['conv10_2'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv10_2')(net['conv10_1']) # Block 11 net['conv11_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv11_1')(net['conv10_2']) net['conv11_2'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv11_2')(net['conv11_1']) # Add extra layer on top of conv4_3 to normalize its output according to # the paper net['conv4_3_norm'] = Normalize(20, name='conv4_3_norm')(net['conv4_3']) return net
def VGG16_Base(self, input_tensor): x = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(input_tensor) x = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(x) x = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(x) x = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(x) x = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(x) x = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(x) x = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(x) x = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(x) x = Convolution2D(256, 3, 3, activation='relu', border_mode='same')(x) x = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same')(x) CONV_4 = x x = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='MaxPool_4')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same')(net['pool4']) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same')(x) x = Convolution2D(512, 3, 3, activation='relu', border_mode='same')(x) x = MaxPooling2D((3, 3), strides=(1, 1), border_mode='same')(x) x = AtrousConvolution2D(1024, 3, 3, atrous_rate=(6, 6), activation='relu', border_mode='same')(x) x = Convolution2D(1024, 1, 1, activation='relu', border_mode='same')(x) FC_7 = x x = Convolution2D(256, 1, 1, activation='relu', border_mode='same')(x) x = Convolution2D(512, 3, 3, subsample=(2, 2), activation='relu', border_mode='same')(x) CONV_6 = x x = Convolution2D(128, 1, 1, activation='relu', border_mode='same')(x) x = ZeroPadding2D()(x) x = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='valid')(x) CONV_7 = x x = Convolution2D(128, 1, 1, activation='relu', border_mode='same')(x) x = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='same')(x) CONV_8 = x x = GlobalAveragePooling2D(name='GaPooling_6')(x) POOL_6 = x return x, CONV_4, FC_7, CONV_6, CONV_7, CONV_8, POOL_6
def init_params(options): params = OrderedDict() if not use_conv: params = get_layer('ff')[0](options, params, prefix='layer_1', nin=INPUT_SIZE, nout=args.dims[0], ortho=False) params = get_layer('ff')[0](options, params, prefix='layer_2', nin=args.dims[0], nout=args.dims[0], ortho=False) dilated_conv = False if dilated_conv: bn = True filter_size = 5 c1 = AtrousConvolution2D(128, filter_size, filter_size, atrous_rate=(1, 1), border_mode='same') c1.build((100, 3, 32, 32)) qw = c1.get_weights() params[_p('c1', 'w')] = qw[0] params[_p('c1', 'b')] = qw[1] if bn: params = bnorm_layer_init((100, 3, 32, 128), params, 'c1_bn') c2 = AtrousConvolution2D(128, filter_size, filter_size, atrous_rate=(2, 2), border_mode='same') c2.build((100, 3, 32, 128)) qw = c2.get_weights() params[_p('c2', 'w')] = qw[0] params[_p('c2', 'b')] = qw[1] if bn: params = bnorm_layer_init((100, 3, 32, 128), params, 'c2_bn') c3 = AtrousConvolution2D(128, filter_size, filter_size, atrous_rate=(4, 4), border_mode='same') c3.build((100, 3, 32, 128)) qw = c3.get_weights() params[_p('c3', 'w')] = qw[0] params[_p('c3', 'b')] = qw[1] if bn: params = bnorm_layer_init((100, 3, 32, 128), params, 'c3_bn') c4_mu = AtrousConvolution2D(128, filter_size, filter_size, atrous_rate=(4, 4), border_mode='same') c4_mu.build((100, 3, 32, 128)) qw = c4_mu.get_weights() params[_p('c4_mu', 'w')] = qw[0] params[_p('c4_mu', 'b')] = qw[1] if bn: params = bnorm_layer_init((100, 3, 32, 128), params, 'c4_mu_bn') c5_mu = AtrousConvolution2D(128, filter_size, filter_size, atrous_rate=(2, 2), border_mode='same') c5_mu.build((100, 3, 32, 128)) qw = c5_mu.get_weights() params[_p('c5_mu', 'w')] = qw[0] params[_p('c5_mu', 'b')] = qw[1] if bn: params = bnorm_layer_init((100, 3, 32, 128), params, 'c5_mu_bn') c6_mu = AtrousConvolution2D(32, filter_size, filter_size, atrous_rate=(1, 1), border_mode='same') c6_mu.build((100, 3, 32, 128)) qw = c6_mu.get_weights() params[_p('c6_mu', 'w')] = qw[0] params[_p('c6_mu', 'b')] = qw[1] c4_s = AtrousConvolution2D(128, filter_size, filter_size, atrous_rate=(4, 4), border_mode='same') c4_s.build((100, 3, 32, 128)) qw = c4_s.get_weights() params[_p('c4_s', 'w')] = qw[0] params[_p('c4_s', 'b')] = qw[1] if bn: params = bnorm_layer_init((100, 3, 32, 128), params, 'c4_s_bn') c5_s = AtrousConvolution2D(128, filter_size, filter_size, atrous_rate=(2, 2), border_mode='same') c5_s.build((100, 3, 32, 128)) qw = c5_s.get_weights() params[_p('c5_s', 'w')] = qw[0] params[_p('c5_s', 'b')] = qw[1] if bn: params = bnorm_layer_init((100, 3, 32, 128), params, 'c5_s_bn') c6_s = AtrousConvolution2D(32, filter_size, filter_size, atrous_rate=(1, 1), border_mode='same') c6_s.build((100, 3, 32, 128)) qw = c6_s.get_weights() params[_p('c6_s', 'w')] = qw[0] params[_p('c6_s', 'b')] = qw[1] if use_conv: bn = True params = ConvLayer(3, 64, 5, 2, params=params, prefix='conv_1', bn=bn) params = ConvLayer(64, 128, 5, 2, params=params, prefix='conv_2', bn=bn) params = ConvLayer(128, 256, 5, 2, params=params, prefix='conv_3', bn=bn) params = get_layer('ff')[0](options, params, prefix='layer_1', nin=4 * 4 * 256, nout=2048, ortho=False) params = get_layer('ff')[0](options, params, prefix='layer_2', nin=2048, nout=2048, ortho=False) params = get_layer('ff')[0](options, params, prefix='layer_3', nin=2048, nout=2048, ortho=False) params = get_layer('ff')[0](options, params, prefix='layer_4', nin=2048, nout=2048, ortho=False) params = get_layer('ff')[0](options, params, prefix='layer_5', nin=2048, nout=4 * 4 * 256, ortho=False) params = ConvLayer(256, 128, 5, -2, params=params, prefix='conv_4_mu', bn=bn) params = ConvLayer(128, 64, 5, -2, params=params, prefix='conv_5_mu', bn=bn) params = ConvLayer(64, 3, 5, -2, params=params, prefix='conv_6_mu') params = ConvLayer(256, 128, 5, -2, params=params, prefix='conv_4_s', bn=bn) params = ConvLayer(128, 64, 5, -2, params=params, prefix='conv_5_s', bn=bn) params = ConvLayer(64, 3, 5, -2, params=params, prefix='conv_6_s') else: #TODO: Ideally, only in the output layer, flag=True should be set. if len(args.dims) == 1: params = get_layer('ff')[0](options, params, prefix='mu_0', nin=args.dims[0], nout=INPUT_SIZE, ortho=False, flag=True) if args.noise == 'gaussian': params = get_layer('ff')[0](options, params, prefix='sigma_0', nin=args.dims[0], nout=INPUT_SIZE, ortho=False) for i in range(len(args.dims) - 1): params = get_layer('ff')[0](options, params, prefix='mu_' + str(i), nin=args.dims[i], nout=args.dims[i + 1], ortho=False) if args.noise == 'gaussian': params = get_layer('ff')[0](options, params, prefix='sigma_' + str(i), nin=args.dims[i], nout=args.dims[i + 1], ortho=False, flag=True) if len(args.dims) > 1: params = get_layer('ff')[0](options, params, prefix='mu_' + str(i + 1), nin=args.dims[i + 1], nout=INPUT_SIZE, ortho=False, flag=True) if args.noise == 'gaussian': params = get_layer('ff')[0](options, params, prefix='sigma_' + str(i + 1), nin=args.dims[i + 1], nout=INPUT_SIZE, ortho=False) return params
def vgg16_base_network(input_shape=None): """VGG16 base model used in SSD paper""" net = {} # Block 1 net['input'] = Input(shape=input_shape) net['conv1_1'] = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_1')(net['input']) net['conv1_2'] = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='conv1_2')(net['conv1_1']) net['pool1'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool1')(net['conv1_2']) # Block 2 net['conv2_1'] = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_1')(net['pool1']) net['conv2_2'] = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='conv2_2')(net['conv2_1']) net['pool2'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool2')(net['conv2_2']) # Block 3 net['conv3_1'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_1')(net['pool2']) net['conv3_2'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_2')(net['conv3_1']) net['conv3_3'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv3_3')(net['conv3_2']) net['pool3'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool3')(net['conv3_3']) # Block 4 net['conv4_1'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_1')(net['pool3']) net['conv4_2'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_2')(net['conv4_1']) net['conv4_3'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv4_3')(net['conv4_2']) net['pool4'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='pool4')(net['conv4_3']) # Block 5 net['conv5_1'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv5_1')(net['pool4']) net['conv5_2'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv5_2')(net['conv5_1']) net['conv5_3'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='conv5_3')(net['conv5_2']) net['pool5'] = MaxPooling2D((3, 3), strides=(1, 1), border_mode='same', name='pool5')(net['conv5_3']) # Block 6 net['conv6'] = AtrousConvolution2D(1024, 3, 3, atrous_rate=(6, 6), activation='relu', border_mode='same', name='conv6')(net['pool5']) # Block 7 net['conv7'] = Convolution2D(1024, 1, 1, activation='relu', border_mode='same', name='conv7')(net['conv6']) # Block 8 net['conv8_1'] = Convolution2D(256, 1, 1, activation='relu', border_mode='same', name='conv8_1')(net['conv7']) net['conv8_2'] = Convolution2D(512, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv8_2')(net['conv8_1']) # Block 9 net['conv9_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv9_1')(net['conv8_2']) net['conv9_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv9_2')(net['conv9_1']) # Block 10 net['conv10_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv10_1')(net['conv9_2']) net['conv10_2'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv10_2')(net['conv10_1']) # Block 11 net['conv11_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv11_1')(net['conv10_2']) net['conv11_2'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv11_2')(net['conv11_1']) # TODO: remove final GlobalAveragePooling2D? ## Global Average Pooling is not used in the original paper. ## Added here to obtain a final feature maps resolution of 1x1 when ## image size is > 300x300. # net['pool11'] = GlobalAveragePooling2D(name='pool11')(net['conv11_2']) # Add extra layer on top of conv4_3 to normalize its output according to # the paper net['conv4_3_norm'] = Normalize(20, name='conv4_3_norm')(net['conv4_3']) return net
def generator(img_size, n_filters, name='g'): """ generate network based on unet """ img_height, img_width = img_size[0], img_size[1] img_ch = 3 out_ch = 1 kernel_size = (3, 3) strides = (1, 1) padding = 'same' inputs = Input((img_height, img_width, img_ch)) conv1 = Conv2D(filters=n_filters, kernel_size=(3, 3), strides=(1, 1), padding='same')(inputs) # atrous_conv1 = AtrousConvolution2D(n_filters, 3,3, atrous_rate=(2,2), border_mode='same')(conv1) conv1 = Conv2D(filters=n_filters, kernel_size=(3, 3), strides=(1, 1), padding='same')(conv1) down1 = AveragePooling2D(pool_size=(2, 2))(conv1) conv2 = Conv2D(filters=n_filters * 2, kernel_size=kernel_size, strides=(1, 1), padding=padding)(down1) # conv2 = InstanceNormalization()(conv2) conv2 = GroupNormalization(groups=16, axis=3, scale=False)(conv2) # conv2 = BatchNormalization(scale=False, axis=3)(conv2) conv2 = LeakyReLU(0.2)(conv2) # atrous_conv2 = AtrousConvolution2D(n_filters*2, 3, 3, atrous_rate=(2, 2), border_mode='same')(conv2) conv2 = Conv2D(filters=n_filters * 2, kernel_size=kernel_size, strides=(1, 1), padding=padding)(conv2) # conv2 = InstanceNormalization()(conv2) conv2 = GroupNormalization(groups=16, axis=3, scale=False)(conv2) # conv2 = BatchNormalization(scale=False, axis=3)(conv2) conv2 = LeakyReLU(0.2)(conv2) # conv2 = Activation('relu')(conv2) down2 = AveragePooling2D(pool_size=(2, 2))(conv2) conv3 = Conv2D(filters=n_filters * 4, kernel_size=kernel_size, strides=(1, 1), padding=padding)(down2) # conv3 = InstanceNormalization()(conv3) conv3 = GroupNormalization(groups=16, axis=3, scale=False)(conv3) # conv3 = BatchNormalization(scale=False, axis=3)(conv3) conv3 = LeakyReLU(0.2)(conv3) atrous_conv3 = AtrousConvolution2D(n_filters * 4, 3, 3, atrous_rate=(2, 2), border_mode='same')(conv3) # conv3 = Conv2D(filters=n_filters * 4, kernel_size=kernel_size, strides=(1, 1), padding=padding)(conv3) # atrous_conv3 = InstanceNormalization()(atrous_conv3) atrous_conv3 = GroupNormalization(groups=16, axis=3, scale=False)(atrous_conv3) # conv3 = BatchNormalization(scale=False, axis=3)(conv3) atrous_conv3 = LeakyReLU(0.2)(atrous_conv3) # conv3 = Activation('relu')(conv3) # down3 = AveragePooling2D(pool_size=(2, 2))(conv3) conv4 = Conv2D(filters=n_filters * 4, kernel_size=kernel_size, strides=(1, 1), padding=padding)(atrous_conv3) # conv4 = InstanceNormalization()(conv4) conv4 = GroupNormalization(groups=16, axis=3, scale=False)(conv4) # conv4 = BatchNormalization(scale=False, axis=3)(conv4) conv4 = LeakyReLU(0.2)(conv4) # conv4 = Conv2D(filters=n_filters * 8, kernel_size=kernel_size, strides=(1, 1), padding=padding)(conv4) atrous_conv4 = AtrousConvolution2D(n_filters * 4, 3, 3, atrous_rate=(2, 2), border_mode='same')(conv4) # atrous_conv4 = InstanceNormalization()(atrous_conv4) atrous_conv4 = GroupNormalization(groups=16, axis=3, scale=False)(atrous_conv4) # conv4 = BatchNormalization(scale=False, axis=3)(conv4) atrous_conv4 = LeakyReLU(0.2)(atrous_conv4) # conv4 = Activation('relu')(conv4) # conv2 = res_block(down1, n_filters*2) # down2 = AveragePooling2D(pool_size=(2, 2))(conv2) # conv3 = res_block(down2, n_filters*4) # down3 = AveragePooling2D(pool_size=(2, 2))(conv3) # conv4 = res_block(down3, n_filters*8) att_g1 = attention_gate(x=atrous_conv3, signal=atrous_conv4, n_filters=n_filters * 4, padding='same') concat1 = Concatenate(axis=3)([conv3, att_g1]) up1 = Conv2D(filters=n_filters * 4, kernel_size=kernel_size, strides=(1, 1), padding=padding)(concat1) # up1 = InstanceNormalization()(up1) up1 = GroupNormalization(groups=16, axis=3, scale=False)(up1) # up1 = BatchNormalization(scale=False, axis=3)(up1) up1 = LeakyReLU(0.2)(up1) # up1 = Conv2D(filters=n_filters * 4, kernel_size=kernel_size, strides=(1, 1), padding=padding)(up1) up1 = AtrousConvolution2D(n_filters * 4, 3, 3, atrous_rate=(2, 2), border_mode='same')(up1) # up1 = InstanceNormalization()(up1) up1 = GroupNormalization(groups=16, axis=3, scale=False)(up1) # up1 = BatchNormalization(scale=False, axis=3)(up1) up1 = LeakyReLU(0.2)(up1) # up1 = Activation('relu')(up1) # up1 = res_block(concat1, n_filters*4) up2 = Conv2DTranspose(filters=n_filters * 2, kernel_size=(3, 3), strides=(2, 2), padding=padding)(up1) att_g2 = attention_gate(x=conv2, signal=up2, n_filters=n_filters * 2, padding='same') concat2 = Concatenate(axis=3)([up2, att_g2]) up2 = Conv2D(filters=n_filters * 2, kernel_size=kernel_size, strides=(1, 1), padding=padding)(concat2) # up2 = InstanceNormalization()(up2) up2 = GroupNormalization(groups=16, axis=3, scale=False)(up2) # up2 = BatchNormalization(scale=False, axis=3)(up2) up2 = LeakyReLU(0.2)(up2) # up2 = Conv2D(filters=n_filters * 2, kernel_size=kernel_size, strides=(1, 1), padding=padding)(up2) up2 = AtrousConvolution2D(n_filters * 2, 3, 3, atrous_rate=(2, 2), border_mode='same')(up2) # up2 = InstanceNormalization()(up2) up2 = GroupNormalization(groups=16, axis=3, scale=False)(up2) # up2 = BatchNormalization(scale=False, axis=3)(up2) up2 = LeakyReLU(0.2)(up2) # up2 = Activation('relu')(up2) # up2 = res_block(concat2, n_filters * 2) up3 = Conv2DTranspose(filters=n_filters, kernel_size=(3, 3), strides=(2, 2), padding=padding)(up2) att_g3 = attention_gate(x=conv1, signal=up3, n_filters=n_filters, padding='same') concat3 = Concatenate(axis=3)([up3, att_g3]) up3 = Conv2D(filters=n_filters, kernel_size=kernel_size, strides=(1, 1), padding=padding)(concat3) # up3 = InstanceNormalization()(up3) up3 = GroupNormalization(groups=16, axis=3, scale=False)(up3) # up3 = BatchNormalization(scale=False, axis=3)(up3) up3 = LeakyReLU(0.2)(up3) # up3 = Conv2D(filters=n_filters, kernel_size=kernel_size, strides=(1, 1), padding=padding)(up3) up3 = AtrousConvolution2D(n_filters, 3, 3, atrous_rate=(2, 2), border_mode='same')(up3) # up3 = InstanceNormalization()(up3) up3 = GroupNormalization(groups=16, axis=3, scale=False)(up3) # up3 = BatchNormalization(scale=False, axis=3)(up3) up3 = LeakyReLU(0.2)(up3) # up3 = Activation('relu')(up3) # up3 = res_block(concat3, n_filters) outputs = Conv2D(out_ch, kernel_size=(1, 1), strides=(1, 1), padding='same')(up3) outputs = Activation('sigmoid')(outputs) g = Model(inputs, outputs, name=name) return g
def TCN_net(): def conv_block(input, filters=32, kernel_size=(3, 3)): x = Conv2D(filters=filters, kernel_size=kernel_size, padding='same', kernel_initializer='he_normal')(input) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(filters=filters, kernel_size=kernel_size, padding='same', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Activation('relu')(x) return x def BandA(input): x = BatchNormalization()(input) x = Activation('relu')(x) return x inputs = Input((seq_length, image_rows_low, image_cols, channel_num)) # upscailing x0 = inputs # split in seq_length split_x = Lambda(tf.split, arguments={ 'axis': 1, 'num_or_size_splits': seq_length })(inputs) # shared parameteres layer up0 = Conv2DTranspose(filters=32, kernel_size=(3, 3), padding='same', strides=(2, 1), kernel_initializer='he_normal') up1 = Conv2DTranspose(filters=32, kernel_size=(3, 3), padding='same', strides=(2, 1), kernel_initializer='he_normal') output1 = [] # 0,1,2,3,4,5,6...15 for i in range(seq_length): slice = split_x[i] slice = Lambda(tf.squeeze, arguments={ 'axis': 1, })(slice) slice = up0(slice) slice = BatchNormalization()(slice) slice = Activation('relu')(slice) slice = up1(slice) slice = BatchNormalization()(slice) slice = Activation('relu')(slice) output1.append(slice) output2 = [] # 0,1,2,3,4,5,6,7 conv1 = Conv2D(filters=64, kernel_size=(3, 3), kernel_regularizer=regularizers.l2(0.01), dilation_rate=(2, 2), padding='same', kernel_initializer='he_normal') conv2 = AtrousConvolution2D(filters=64, kernel_size=(3, 3), kernel_regularizer=regularizers.l2(0.01), dilation_rate=(2, 2), strides=(2, 2), padding='same', kernel_initializer='he_normal') for i in range(int(seq_length / 2)): slice = concatenate([output1[2 * i], output1[2 * i + 1]], axis=3) slice = conv1(slice) slice = BatchNormalization()(slice) slice = Activation('relu')(slice) slice = conv2(slice) slice = BatchNormalization()(slice) slice = Activation('relu')(slice) output2.append(slice) output3 = [] # 0,1,2,3 conv3 = Conv2D(filters=128, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), dilation_rate=(2, 2), padding='same', kernel_initializer='he_normal') conv4 = AtrousConvolution2D(filters=128, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), dilation_rate=(2, 2), strides=(2, 2), padding='same', kernel_initializer='he_normal') for i in range(int(seq_length / 4)): slice = concatenate([output2[i * 2], output2[2 * i + 1]], axis=3) slice = conv3(slice) slice = BatchNormalization()(slice) slice = Activation('relu')(slice) slice = conv4(slice) slice = BatchNormalization()(slice) slice = Activation('relu')(slice) output3.append(slice) output4 = [] # 0,1 conv5 = Conv2D(filters=256, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), dilation_rate=(2, 2), padding='same', kernel_initializer='he_normal') conv6 = AtrousConvolution2D(filters=256, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), dilation_rate=(2, 2), strides=(2, 2), padding='same', kernel_initializer='he_normal') for i in range(int(seq_length / 8)): slice = concatenate([output3[i * 2], output3[2 * i + 1]], axis=3) slice = conv5(slice) slice = BatchNormalization()(slice) slice = Activation('relu')(slice) slice = conv6(slice) slice = BatchNormalization()(slice) slice = Activation('relu')(slice) output4.append(slice) output5 = [] # 0 conv7 = Conv2D(filters=512, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), dilation_rate=(2, 2), padding='same', kernel_initializer='he_normal') conv8 = AtrousConvolution2D(filters=512, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), dilation_rate=(2, 2), strides=(2, 2), padding='same', kernel_initializer='he_normal') for i in range(int(seq_length / 16)): slice = concatenate([output4[i * 2], output4[2 * i + 1]], axis=3) slice = conv7(slice) slice = BatchNormalization()(slice) slice = Activation('relu')(slice) slice = conv8(slice) slice = BatchNormalization()(slice) slice = Activation('relu')(slice) output5.append(slice) highest_map = output5[0] highest_map = Conv2D(filters=1024, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), padding='same', kernel_initializer='he_normal')(highest_map) highest_map = BatchNormalization()(highest_map) highest_map = Activation('relu')(highest_map) highest_map = Conv2D(filters=1024, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), padding='same', kernel_initializer='he_normal')(highest_map) highest_map = BatchNormalization()(highest_map) highest_map = Activation('relu')(highest_map) up1_map = Conv2DTranspose(filters=512, kernel_size=(3, 3), padding='same', strides=(2, 2), kernel_initializer='he_normal')(highest_map) up1_map = BandA(up1_map) up1_concat = concatenate([up1_map, output4[1]], axis=3) up1_concat = Conv2D(filters=512, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), padding='same', kernel_initializer='he_normal')(up1_concat) up1_concat = BandA(up1_concat) up1_concat = Conv2D(filters=512, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), padding='same', kernel_initializer='he_normal')(up1_concat) up1_concat = BandA(up1_concat) up2_map = Conv2DTranspose(filters=256, kernel_size=(3, 3), padding='same', strides=(2, 2), kernel_initializer='he_normal')(up1_concat) up2_map = BandA(up2_map) up2_concat = concatenate([up2_map, output3[3]], axis=3) up2_concat = Conv2D(filters=256, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), padding='same', kernel_initializer='he_normal')(up2_concat) up2_concat = BandA(up2_concat) up2_concat = Conv2D(filters=256, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), padding='same', kernel_initializer='he_normal')(up2_concat) up2_concat = BandA(up2_concat) up3_map = Conv2DTranspose(filters=128, kernel_size=(3, 3), padding='same', strides=(2, 2), kernel_initializer='he_normal')(up2_concat) up3_map = BandA(up3_map) up3_concat = concatenate([up3_map, output2[7]], axis=3) up3_concat = Conv2D(filters=128, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), padding='same', kernel_initializer='he_normal')(up3_concat) up3_concat = BandA(up3_concat) up3_concat = Conv2D(filters=128, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), padding='same', kernel_initializer='he_normal')(up3_concat) up3_concat = BandA(up3_concat) up4_map = Conv2DTranspose(filters=64, kernel_size=(3, 3), padding='same', strides=(2, 2), kernel_initializer='he_normal')(up3_concat) up4_map = BandA(up4_map) up4_concat = concatenate([up4_map, output1[15]], axis=3) up4_concat = Conv2D(filters=64, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), padding='same', kernel_initializer='he_normal')(up4_concat) up4_concat = BandA(up4_concat) up4_concat = Conv2D(filters=64, kernel_regularizer=regularizers.l2(0.01), kernel_size=(3, 3), padding='same', kernel_initializer='he_normal')(up4_concat) up4_concat = BandA(up4_concat) lowest_map = Conv2DTranspose(filters=64, kernel_size=(3, 3), padding='same', strides=(2, 2), kernel_initializer='he_normal')(up3_concat) print(K.int_shape(lowest_map)) gen_image = Conv2D(1, (1, 1), activation='relu')(lowest_map) print(K.int_shape(gen_image)) outputs = gen_image def PSNR(y_true, y_pred): max_pixel = 1.0 return -(10.0 * K.log( (max_pixel**2) / (K.mean(K.square(y_pred - y_true))))) / 2.303 from keras_contrib.losses import DSSIMObjective ssim_loss = DSSIMObjective() model = Model(inputs=inputs, outputs=outputs) model.compile(optimizer=Adam(lr=0.0001, decay=0.00001), loss='mse', metrics=['accuracy', 'mse', 'mae', PSNR, ssim_loss]) # model.compile(optimizer=Adam(lr=0.0001, decay=0.00001),loss=PSNR ,metrics =['accuracy', 'mse' , 'mae', PSNR, ssim_loss ] ) model.summary() return model
def get_dilation_model_cityscapes(input_shape, apply_softmax, input_tensor, classes): if input_tensor is None: model_in = Input(shape=input_shape) else: if not K.is_keras_tensor(input_tensor): model_in = Input(tensor=input_tensor, shape=input_shape) else: model_in = input_tensor h = Convolution2D(64, 3, 3, activation='relu', name='conv1_1')(model_in) h = Convolution2D(64, 3, 3, activation='relu', name='conv1_2')(h) h = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool1')(h) h = Convolution2D(128, 3, 3, activation='relu', name='conv2_1')(h) h = Convolution2D(128, 3, 3, activation='relu', name='conv2_2')(h) h = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool2')(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_1')(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_2')(h) h = Convolution2D(256, 3, 3, activation='relu', name='conv3_3')(h) h = MaxPooling2D(pool_size=(2, 2), strides=(2, 2), name='pool3')(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_1')(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_2')(h) h = Convolution2D(512, 3, 3, activation='relu', name='conv4_3')(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_1')(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_2')(h) h = AtrousConvolution2D(512, 3, 3, atrous_rate=(2, 2), activation='relu', name='conv5_3')(h) h = AtrousConvolution2D(4096, 7, 7, atrous_rate=(4, 4), activation='relu', name='fc6')(h) h = Dropout(0.5, name='drop6')(h) h = Convolution2D(4096, 1, 1, activation='relu', name='fc7')(h) h = Dropout(0.5, name='drop7')(h) h = Convolution2D(classes, 1, 1, name='final')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(classes, 3, 3, activation='relu', name='ctx_conv1_1')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(classes, 3, 3, activation='relu', name='ctx_conv1_2')(h) h = ZeroPadding2D(padding=(2, 2))(h) h = AtrousConvolution2D(classes, 3, 3, atrous_rate=(2, 2), activation='relu', name='ctx_conv2_1')(h) h = ZeroPadding2D(padding=(4, 4))(h) h = AtrousConvolution2D(classes, 3, 3, atrous_rate=(4, 4), activation='relu', name='ctx_conv3_1')(h) h = ZeroPadding2D(padding=(8, 8))(h) h = AtrousConvolution2D(classes, 3, 3, atrous_rate=(8, 8), activation='relu', name='ctx_conv4_1')(h) h = ZeroPadding2D(padding=(16, 16))(h) h = AtrousConvolution2D(classes, 3, 3, atrous_rate=(16, 16), activation='relu', name='ctx_conv5_1')(h) h = ZeroPadding2D(padding=(32, 32))(h) h = AtrousConvolution2D(classes, 3, 3, atrous_rate=(32, 32), activation='relu', name='ctx_conv6_1')(h) h = ZeroPadding2D(padding=(64, 64))(h) h = AtrousConvolution2D(classes, 3, 3, atrous_rate=(64, 64), activation='relu', name='ctx_conv7_1')(h) h = ZeroPadding2D(padding=(1, 1))(h) h = Convolution2D(classes, 3, 3, activation='relu', name='ctx_fc1')(h) h = Convolution2D(classes, 1, 1, name='ctx_final')(h) # the following two layers pretend to be a Deconvolution with grouping layer. # never managed to implement it in Keras # since it's just a gaussian upsampling trainable=False is recommended h = UpSampling2D(size=(8, 8))(h) logits = Convolution2D(classes, 16, 16, border_mode='same', bias=False, trainable=False, name='ctx_upsample')(h) if apply_softmax: model_out = softmax(logits) else: model_out = logits model = Model(input=model_in, output=model_out, name='dilation_cityscapes') return model
def vgg16_pretrained_base_network(input_shape): """VGG16 base model to use with pretrained weights In order to allow loading the weights, the vgg16 network is built with the layer names found in the vgg16 model provided by keras. `input_shape` must be a tuple (height, width, channels). Returns the layers in a dict which must be pass to the build_ssd() function. """ net = {} # Block 1 net['input'] = Input(shape=input_shape) net['block1_conv1'] = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv1')(net['input']) net['block1_conv2'] = Convolution2D(64, 3, 3, activation='relu', border_mode='same', name='block1_conv2')( net['block1_conv1']) net['block1_pool'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='block1_pool')(net['block1_conv2']) # Block 2 net['block2_conv1'] = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv1')( net['block1_pool']) net['block2_conv2'] = Convolution2D(128, 3, 3, activation='relu', border_mode='same', name='block2_conv2')( net['block2_conv1']) net['block2_pool'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='block2_pool')(net['block2_conv2']) # Block 3 net['block3_conv1'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv1')( net['block2_pool']) net['block3_conv2'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv2')( net['block3_conv1']) net['block3_conv3'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='block3_conv3')( net['block3_conv2']) net['block3_pool'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='block3_pool')(net['block3_conv3']) # Block 4 net['block4_conv1'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv1')( net['block3_pool']) net['block4_conv2'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv2')( net['block4_conv1']) net['block4_conv3'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block4_conv3')( net['block4_conv2']) net['block4_pool'] = MaxPooling2D((2, 2), strides=(2, 2), border_mode='same', name='block4_pool')(net['block4_conv3']) # Block 5 net['block5_conv1'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv1')( net['block4_pool']) net['block5_conv2'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv2')( net['block5_conv1']) net['block5_conv3'] = Convolution2D(512, 3, 3, activation='relu', border_mode='same', name='block5_conv3')( net['block5_conv2']) net['block5_pool'] = MaxPooling2D((3, 3), strides=(1, 1), border_mode='same', name='block5_pool')(net['block5_conv3']) # Block 6 net['conv6'] = AtrousConvolution2D(1024, 3, 3, atrous_rate=(6, 6), activation='relu', border_mode='same', name='conv6')(net['block5_pool']) # Block 7 net['conv7'] = Convolution2D(1024, 1, 1, activation='relu', border_mode='same', name='conv7')(net['conv6']) # Block 8 net['conv8_1'] = Convolution2D(256, 1, 1, activation='relu', border_mode='same', name='conv8_1')(net['conv7']) net['conv8_2'] = Convolution2D(512, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv8_2')(net['conv8_1']) # Block 9 net['conv9_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv9_1')(net['conv8_2']) net['conv9_2'] = Convolution2D(256, 3, 3, subsample=(2, 2), activation='relu', border_mode='same', name='conv9_2')(net['conv9_1']) # Block 10 net['conv10_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv10_1')(net['conv9_2']) net['conv10_2'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv10_2')(net['conv10_1']) # Block 11 net['conv11_1'] = Convolution2D(128, 1, 1, activation='relu', border_mode='same', name='conv11_1')(net['conv10_2']) net['conv11_2'] = Convolution2D(256, 3, 3, activation='relu', border_mode='same', name='conv11_2')(net['conv11_1']) # Add extra layer on top of conv4_3 to normalize its output according to # the paper net['conv4_3_norm'] = Normalize(20, name='conv4_3_norm')(net['block4_conv3']) return net
def atrous_unet(img_size, n_filters, name='atrous_unet'): k = 3 # kernel size s = 2 # stride img_ch = 3 # image channels out_ch = 1 # output channel img_height, img_width = img_size[0], img_size[1] padding = 'same' inputs = Input((img_height, img_width, img_ch)) conv1 = Conv2D(n_filters, (k, k), padding=padding)(inputs) pool1 = AveragePooling2D(pool_size=(s, s))(conv1) conv2 = Conv2D(2 * n_filters, (k, k), padding=padding)(pool1) # conv2 = InstanceNormalization(scale=False, axis=3)(conv2) conv2 = BatchNormalization(scale=False, axis=3)(conv2) # conv2 = LeakyReLU(0.2)(conv2) conv2 = Activation('relu')(conv2) conv2 = Conv2D(2 * n_filters, (k, k), padding=padding)(conv2) # conv2 = InstanceNormalization(scale=False, axis=3)(conv2) conv2 = BatchNormalization(scale=False, axis=3)(conv2) # conv2 = LeakyReLU(0.2)(conv2) conv2 = Activation('relu')(conv2) pool2 = AveragePooling2D(pool_size=(s, s))(conv2) conv3 = Conv2D(4 * n_filters, (k, k), padding=padding)(pool2) # conv3 = InstanceNormalization(scale=False, axis=3)(conv3) conv3 = BatchNormalization(scale=False, axis=3)(conv3) # conv3 = LeakyReLU(0.2)(conv3) conv3 = Activation('relu')(conv3) conv3 = Conv2D(4 * n_filters, (k, k), padding=padding)(conv3) # conv3 = InstanceNormalization(scale=False, axis=3)(conv3) # conv3 = LeakyReLU(0.2)(conv3) conv3 = BatchNormalization(scale=False, axis=3)(conv3) conv3 = Activation('relu')(conv3) pool3 = AveragePooling2D(pool_size=(s, s))(conv3) conv4 = Conv2D(8 * n_filters, (k, k), padding=padding)(pool3) # conv4 = InstanceNormalization(scale=False, axis=3)(conv4) # conv4 = LeakyReLU(0.2)(conv4) conv4 = BatchNormalization(scale=False, axis=3)(conv4) conv4 = Activation('relu')(conv4) conv4 = AtrousConvolution2D(8 * n_filters, k, k, atrous_rate=(2, 2), border_mode='same')(conv4) # conv4 = InstanceNormalization(scale=False, axis=3)(conv4) # conv4 = LeakyReLU(0.2)(conv4) conv4 = BatchNormalization(scale=False, axis=3)(conv4) conv4 = Activation('relu')(conv4) pool4 = AveragePooling2D(pool_size=(s, s))(conv4) conv5 = Conv2D(16 * n_filters, (k, k), padding=padding)(pool4) # conv5 = InstanceNormalization(scale=False, axis=3)(conv5) # conv5 = LeakyReLU(0.2)(conv5) conv5 = BatchNormalization(scale=False, axis=3)(conv5) conv5 = Activation('relu')(conv5) conv5 = AtrousConvolution2D(16 * n_filters, k, k, atrous_rate=(2, 2), border_mode='same')(conv5) # conv5 = InstanceNormalization(scale=False, axis=3)(conv5) # conv5 = LeakyReLU(0.2)(conv5) conv5 = BatchNormalization(scale=False, axis=3)(conv5) conv5 = Activation('relu')(conv5) up1 = Conv2DTranspose(filters=8 * n_filters, kernel_size=(4, 4), strides=(2, 2), padding=padding)(conv5) concat1 = Concatenate(axis=3)([up1, conv4]) conv6 = Conv2D(8 * n_filters, (k, k), padding=padding)(concat1) # conv6 = InstanceNormalization(scale=False, axis=3)(conv6) # conv6 = LeakyReLU(0.2)(conv6) conv6 = BatchNormalization(scale=False, axis=3)(conv6) conv6 = Activation('relu')(conv6) conv6 = AtrousConvolution2D(8 * n_filters, k, k, atrous_rate=(2, 2), border_mode='same')(conv6) # conv6 = InstanceNormalization(scale=False, axis=3)(conv6) # conv6 = LeakyReLU(0.2)(conv6) conv6 = BatchNormalization(scale=False, axis=3)(conv6) conv6 = Activation('relu')(conv6) up2 = Conv2DTranspose(filters=4 * n_filters, kernel_size=(4, 4), strides=(2, 2), padding=padding)(conv6) concat2 = Concatenate(axis=3)([up2, conv3]) conv7 = Conv2D(4 * n_filters, (k, k), padding=padding)(concat2) # conv7 = InstanceNormalization(scale=False, axis=3)(conv7) # conv7 = LeakyReLU(0.2)(conv7) conv7 = BatchNormalization(scale=False, axis=3)(conv7) conv7 = Activation('relu')(conv7) conv7 = AtrousConvolution2D(4 * n_filters, k, k, atrous_rate=(2, 2), border_mode='same')(conv7) # conv7 = InstanceNormalization(scale=False, axis=3)(conv7) # conv7 = LeakyReLU(0.2)(conv7) conv7 = BatchNormalization(scale=False, axis=3)(conv7) conv7 = Activation('relu')(conv7) up3 = Conv2DTranspose(filters=2 * n_filters, kernel_size=(4, 4), strides=(2, 2), padding=padding)(conv7) concat3 = Concatenate(axis=3)([up3, conv2]) conv8 = Conv2D(2 * n_filters, (k, k), padding=padding)(concat3) # conv8 = InstanceNormalization(scale=False, axis=3)(conv8) # conv8 = LeakyReLU(0.2)(conv8) conv8 = BatchNormalization(scale=False, axis=3)(conv8) conv8 = Activation('relu')(conv8) conv8 = AtrousConvolution2D(2 * n_filters, k, k, atrous_rate=(2, 2), border_mode='same')(conv8) # conv8 = InstanceNormalization(scale=False, axis=3)(conv8) # conv8 = LeakyReLU(0.2)(conv8) conv8 = BatchNormalization(scale=False, axis=3)(conv8) conv8 = Activation('relu')(conv8) up4 = Conv2DTranspose(filters=n_filters, kernel_size=(4, 4), strides=(2, 2), padding=padding)(conv8) concat4 = Concatenate(axis=3)([up4, conv1]) conv9 = Conv2D(n_filters, (k, k), padding=padding)(concat4) # conv9 = InstanceNormalization(scale=False, axis=3)(conv9) # conv9 = LeakyReLU(0.2)(conv9) conv9 = BatchNormalization(scale=False, axis=3)(conv9) conv9 = Activation('relu')(conv9) conv9 = AtrousConvolution2D(n_filters, k, k, atrous_rate=(2, 2), border_mode='same')(conv9) # conv9 = InstanceNormalization(scale=False, axis=3)(conv9) # conv9 = LeakyReLU(0.2)(conv9) conv9 = BatchNormalization(scale=False, axis=3)(conv9) conv9 = Activation('relu')(conv9) outputs = Conv2D(out_ch, (1, 1), padding=padding, activation='sigmoid')(conv9) g = Model(inputs, outputs, name=name) return g
def get_unet(): inputs = Input((1, running_img_rows, running_img_cols)) conv1 = AtrousConvolution2D(32, 3, 3, activation='relu', border_mode='same')(inputs) conv1 = AtrousConvolution2D(32, 3, 3, activation='relu', border_mode='same')(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) conv2 = AtrousConvolution2D(64, 3, 3, activation='relu', border_mode='same')(pool1) conv2 = AtrousConvolution2D(64, 3, 3, activation='relu', border_mode='same')(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) conv3 = AtrousConvolution2D(128, 3, 3, activation='relu', border_mode='same')(pool2) conv3 = AtrousConvolution2D(128, 3, 3, activation='relu', border_mode='same')(conv3) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) conv4 = AtrousConvolution2D(256, 3, 3, activation='relu', border_mode='same')(pool3) conv4 = AtrousConvolution2D(256, 3, 3, activation='relu', border_mode='same')(conv4) pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) conv5 = AtrousConvolution2D(512, 3, 3, activation='relu', border_mode='same')(pool4) conv5 = AtrousConvolution2D(512, 3, 3, activation='relu', border_mode='same')(conv5) up6 = merge([UpSampling2D(size=(2, 2))(conv5), conv4], mode='concat', concat_axis=1) conv6 = AtrousConvolution2D(256, 3, 3, activation='relu', border_mode='same')(up6) conv6 = AtrousConvolution2D(256, 3, 3, activation='relu', border_mode='same')(conv6) up7 = merge([UpSampling2D(size=(2, 2))(conv6), conv3], mode='concat', concat_axis=1) conv7 = AtrousConvolution2D(128, 3, 3, activation='relu', border_mode='same')(up7) conv7 = AtrousConvolution2D(128, 3, 3, activation='relu', border_mode='same')(conv7) up8 = merge([UpSampling2D(size=(2, 2))(conv7), conv2], mode='concat', concat_axis=1) conv8 = AtrousConvolution2D(64, 3, 3, activation='relu', border_mode='same')(up8) conv8 = AtrousConvolution2D(64, 3, 3, activation='relu', border_mode='same')(conv8) up9 = merge([UpSampling2D(size=(2, 2))(conv8), conv1], mode='concat', concat_axis=1) conv9 = AtrousConvolution2D(32, 3, 3, activation='relu', border_mode='same')(up9) conv9 = AtrousConvolution2D(32, 3, 3, activation='relu', border_mode='same')(conv9) conv10 =AtrousConvolution2D(1, 1, 1, activation='sigmoid')(conv9) model = Model(input=inputs, output=conv10) #改变learningrate原来是e-5,原定值最好。 model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef]) return model