def build_encoder_decoder(): # Encoder input_tensor = Input(shape=(320, 320, 4)) x = ZeroPadding2D((1, 1))(input_tensor) x = Conv2D(64, (3, 3), activation='relu', name='conv1_1')(x) x = BatchNormalization()(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(64, (3, 3), activation='relu', name='conv1_2')(x) x = BatchNormalization()(x) orig_1 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', name='conv2_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', name='conv2_2')(x) orig_2 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_3')(x) orig_3 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) inputs_size = x.get_shape()[1:3] conv_4_1x1 = Conv2D(512, (1, 1), activation='relu', padding='same', name='conv4_1x1')(x) conv_4_3x3_1 = Conv2D(512, (3, 3), activation='relu', padding='same', dilation_rate=ATROUS_RATES[0], name='conv4_3x3_1')(x) conv_4_3x3_2 = Conv2D(512, (3, 3), activation='relu', padding='same', dilation_rate=ATROUS_RATES[1], name='conv4_3x3_2')(x) conv_4_3x3_3 = Conv2D(512, (3, 3), activation='relu', padding='same', dilation_rate=ATROUS_RATES[2], name='conv4_3x3_3')(x) # Image average pooling image_level_features = Lambda( lambda x: tf.reduce_mean(x, [1, 2], keepdims=True), name='global_average_pooling')(x) image_level_features = Conv2D( 512, (1, 1), activation='relu', padding='same', name='image_level_features_conv_1x1')(image_level_features) image_level_features = Lambda(lambda x: tf.image.resize(x, inputs_size), name='upsample_1')(image_level_features) # Concat x = Concatenate(axis=3)([ conv_4_1x1, conv_4_3x3_1, conv_4_3x3_2, conv_4_3x3_3, image_level_features ]) x = Conv2D(512, (1, 1), activation='relu', padding='same', name='conv_1x1_1_concat')(x) x = Conv2D(512, (1, 1), activation='relu', padding='same', name='conv_1x1_2_concat')(x) orig_4 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_3')(x) orig_5 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) # Decoder # x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_5) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_5) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='deconv5_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='deconv5_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(512, (3, 3), activation='relu', padding='same', name='deconv5_3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_4) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_4) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='deconv4_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='deconv4_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='deconv4_3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_3) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_3) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='deconv3_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='deconv3_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='deconv3_3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_2) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_2) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='deconv2_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='deconv2_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_1) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_1) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='deconv1_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='deconv1_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(1, (3, 3), activation='sigmoid', padding='same', name='pred', kernel_initializer='he_normal', bias_initializer='zeros')(x) model = Model(inputs=input_tensor, outputs=x) return model
def build_encoder_decoder(): num_labels = 8 kernel = 3 # Encoder input_tensor = Input(shape=(320, 320, 3)) x = ZeroPadding2D((1, 1))(input_tensor) x = Conv2D(64, (kernel, kernel), activation='relu', name='conv1_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(64, (kernel, kernel), activation='relu', name='conv1_2')(x) orig_1 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (kernel, kernel), activation='relu', name='conv2_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (kernel, kernel), activation='relu', name='conv2_2')(x) orig_2 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (kernel, kernel), activation='relu', name='conv3_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (kernel, kernel), activation='relu', name='conv3_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (kernel, kernel), activation='relu', name='conv3_3')(x) orig_3 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (kernel, kernel), activation='relu', name='conv4_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (kernel, kernel), activation='relu', name='conv4_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (kernel, kernel), activation='relu', name='conv4_3')(x) orig_4 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (kernel, kernel), activation='relu', name='conv5_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (kernel, kernel), activation='relu', name='conv5_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (kernel, kernel), activation='relu', name='conv5_3')(x) orig_5 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) # Decoder x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_5) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_5) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv5_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv5_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv5_3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_4) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_4) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(256, (kernel, kernel), activation='relu', padding='same', name='deconv4_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(256, (kernel, kernel), activation='relu', padding='same', name='deconv4_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(256, (kernel, kernel), activation='relu', padding='same', name='deconv4_3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_3) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_3) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(128, (kernel, kernel), activation='relu', padding='same', name='deconv3_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(128, (kernel, kernel), activation='relu', padding='same', name='deconv3_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(128, (kernel, kernel), activation='relu', padding='same', name='deconv3_3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_2) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_2) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (kernel, kernel), activation='relu', padding='same', name='deconv2_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(64, (kernel, kernel), activation='relu', padding='same', name='deconv2_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_1) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_1) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (kernel, kernel), activation='relu', padding='same', name='deconv1_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(64, (kernel, kernel), activation='relu', padding='same', name='deconv1_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(num_labels, (1, 1), activation='softmax', padding='valid', name='pred', kernel_initializer='he_normal', bias_initializer='zeros')(x) model = Model(inputs=input_tensor, outputs=x) return model
def build_encoder_decoder(): # Encoder input_tensor = Input(shape=(320, 320, 4)) x = ZeroPadding2D((1, 1))(input_tensor) x = Conv2D(64, (3, 3), activation='relu', name='conv1_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(64, (3, 3), activation='relu', name='conv1_2')(x) orig_1 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', name='conv2_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', name='conv2_2')(x) orig_2 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_3')(x) orig_3 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv4_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv4_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv4_3')(x) orig_4 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_3')(x) orig_5 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) # Decoder # x = Conv2D(4096, (7, 7), activation='relu', padding='valid', name='conv6')(x) # x = BatchNormalization()(x) # x = UpSampling2D(size=(7, 7))(x) x = Conv2D(512, (1, 1), activation='relu', padding='same', name='deconv6', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_5) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_5) # print('origReshaped.shape: ' + str(K.int_shape(origReshaped))) xReshaped = Reshape(shape)(x) # print('xReshaped.shape: ' + str(K.int_shape(xReshaped))) together = Concatenate(axis=1)([origReshaped, xReshaped]) # print('together.shape: ' + str(K.int_shape(together))) x = Unpooling()(together) x = Conv2D(512, (5, 5), activation='relu', padding='same', name='deconv5', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_4) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_4) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(256, (5, 5), activation='relu', padding='same', name='deconv4', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_3) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_3) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(128, (5, 5), activation='relu', padding='same', name='deconv3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_2) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_2) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (5, 5), activation='relu', padding='same', name='deconv2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_1) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_1) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (5, 5), activation='relu', padding='same', name='deconv1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='pred', kernel_initializer='he_normal', bias_initializer='zeros')(x) model = Model(inputs=input_tensor, outputs=x) return model
def build_encoder_decoder(): kernel = 3 # Encoder # input_tensor = Input(shape=(320, 320, 4)) input_tensor_shape = input_tensor.get_shape()[1:3] # Entry flow x = Conv2D(32, (3, 3), padding="same")(input_tensor) x = BatchNormalization()(x) x = Activation("relu")(x) x = Conv2D(64, (3, 3), padding="same")(x) orig_1 = BatchNormalization()(x) x = Activation("relu")(orig_1) x = x = Conv2D(64, (3, 3), strides=(2, 2), padding="same")(x) x = BatchNormalization()(x) x = Activation("relu")(x) x, orig_2 = res_downsample_xception_block(x, 128) x, orig_3 = res_downsample_xception_block(x, 256, top_relu=True) x, orig_4 = res_downsample_xception_block(x, 728, top_relu=True) # Middle flow for i in range(8): x = res_xception_block(x, 728) # Exit flow res = Conv2D(1024, (1, 1), padding="same")(x) res = BatchNormalization()(res) x = Activation("relu")(x) x = DepthwiseConv2D((3, 3), padding="same")(x) x = BatchNormalization()(x) x = Conv2D(728, (1, 1), padding="same")(x) x = BatchNormalization()(x) x = Activation("relu")(x) x = DepthwiseConv2D((3, 3), padding="same")(x) x = BatchNormalization()(x) x = Conv2D(1024, (1, 1), padding="same")(x) x = BatchNormalization()(x) x = Activation("relu")(x) x = DepthwiseConv2D((3, 3), padding="same")(x) x = BatchNormalization()(x) x = Conv2D(1024, (1, 1), padding="same")(x) x = BatchNormalization()(x) x = Add()([x, res]) x = DepthwiseConv2D((3, 3), padding="same")(x) x = BatchNormalization()(x) x = Conv2D(1536, (1, 1), padding="same")(x) x = BatchNormalization()(x) x = Activation("relu")(x) x = DepthwiseConv2D((3, 3), padding="same")(x) x = BatchNormalization()(x) x = Conv2D(1536, (1, 1), padding="same")(x) x = BatchNormalization()(x) x = Activation("relu")(x) x = DepthwiseConv2D((3, 3), padding="same")(x) x = BatchNormalization()(x) x = Conv2D(2048, (1, 1), padding="same")(x) x = BatchNormalization()(x) x = Activation("relu")(x) inputs_size = x.get_shape()[1:3] # Atrous convolution conv_4_1x1 = SeparableConv2D(256, (1, 1), activation='relu', padding='same', name='conv4_1x1')(x) conv_4_3x3_1 = SeparableConv2D(256, (kernel, kernel), activation='relu', padding='same', dilation_rate=ATROUS_RATES[0], name='conv4_3x3_1')(x) conv_4_3x3_2 = SeparableConv2D(256, (kernel, kernel), activation='relu', padding='same', dilation_rate=ATROUS_RATES[1], name='conv4_3x3_2')(x) conv_4_3x3_3 = SeparableConv2D(256, (kernel, kernel), activation='relu', padding='same', dilation_rate=ATROUS_RATES[2], name='conv4_3x3_3')(x) # Image average pooling image_level_features = Lambda( lambda x: tf.reduce_mean(x, [1, 2], keepdims=True), name='global_average_pooling')(x) image_level_features = Conv2D( 256, (1, 1), activation='relu', padding='same', name='image_level_features_conv_1x1')(image_level_features) image_level_features = Lambda( lambda x: tf.image.resize_bilinear(x, inputs_size), name='upsample_1')(image_level_features) # Concat x = Concatenate(axis=3)([ conv_4_1x1, conv_4_3x3_1, conv_4_3x3_2, conv_4_3x3_3, image_level_features ]) x = Conv2D(256, (1, 1), activation='relu', padding='same', name='conv_1x1_1_concat')(x) x = Conv2D(728, (1, 1), activation='relu', padding='same', name='conv_1x1_2_concat')(x) # Decoderg # x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_4) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_4) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='deconv4_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='deconv4_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(256, (3, 3), activation='relu', padding='same', name='deconv4_3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_3) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_3) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='deconv3_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='deconv3_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(128, (3, 3), activation='relu', padding='same', name='deconv3_3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_2) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_2) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='deconv2_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='deconv2_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_1) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_1) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='deconv1_1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(64, (3, 3), activation='relu', padding='same', name='deconv1_2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(1, (3, 3), activation='sigmoid', padding='same', name='pred', kernel_initializer='he_normal', bias_initializer='zeros')(x) model = Model(inputs=input_tensor, outputs=x) return model
def create_model(): # Encoder input_tensor = Input(shape=(320, 320, 4)) x = ZeroPadding2D((1, 1))(input_tensor) x = Conv2D(64, (3, 3), activation='relu', name='conv1_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(64, (3, 3), activation='relu', name='conv1_2')(x) orig_1 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', name='conv2_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', name='conv2_2')(x) orig_2 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='conv3_3')(x) orig_3 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv4_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv4_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv4_3')(x) orig_4 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='conv5_3')(x) orig_5 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) # Decoder # x = Conv2D(4096, (7, 7), activation='relu', padding='valid', name='conv6')(x) # x = BatchNormalization()(x) # x = UpSampling2D(size=(7, 7))(x) x = Conv2D(512, (1, 1), activation='relu', padding='same', name='deconv6', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) x = Unpooling(orig_5, (20, 20, 512))(x) x = Conv2D(512, (5, 5), activation='relu', padding='same', name='deconv5', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) x = Unpooling(orig_4, (40, 40, 512))(x) x = Conv2D(256, (5, 5), activation='relu', padding='same', name='deconv4', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) x = Unpooling(orig_3, (80, 80, 256))(x) x = Conv2D(128, (5, 5), activation='relu', padding='same', name='deconv3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) x = Unpooling(orig_2, (160, 160, 128))(x) x = Conv2D(64, (5, 5), activation='relu', padding='same', name='deconv2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) x = Unpooling(orig_1, (320, 320, 64))(x) x = Conv2D(64, (5, 5), activation='relu', padding='same', name='deconv1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='pred', kernel_initializer='he_normal', bias_initializer='zeros')(x) model = Model(inputs=input_tensor, outputs=x) return model
def build_model(): # Encoder img_input = Input(shape=(img_rows, img_cols, channel)) x = ZeroPadding2D((1, 1))(img_input) x = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv1_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(64, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv1_2')(x) orig_1 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv2_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv2_2')(x) orig_2 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv3_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv3_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv3_3')(x) orig_3 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv4_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv4_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv4_3')(x) orig_4 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv5_1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv5_2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', kernel_initializer='he_normal', name='conv5_3')(x) orig_5 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) model = Model(img_input, x) # Loads ImageNet pre-trained data ensure_vgg16_weights() weights_path = 'models/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' model.load_weights(weights_path) # Decoder x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_5) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_5) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv5_1', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv5_2', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv5_3', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_4) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_4) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv4_1', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv4_2', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Conv2D(256, (kernel, kernel), activation='relu', padding='same', name='deconv4_3', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_3) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_3) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(256, (kernel, kernel), activation='relu', padding='same', name='deconv3_1', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Conv2D(256, (kernel, kernel), activation='relu', padding='same', name='deconv3_2', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Conv2D(128, (kernel, kernel), activation='relu', padding='same', name='deconv3_3', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_2) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_2) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(128, (kernel, kernel), activation='relu', padding='same', name='deconv2_1', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Conv2D(64, (kernel, kernel), activation='relu', padding='same', name='deconv2_2', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_1) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_1) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(64, (kernel, kernel), activation='relu', padding='same', name='deconv1_1', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) x = Conv2D(64, (kernel, kernel), activation='relu', padding='same', name='deconv1_2', kernel_initializer='he_normal')(x) x = BatchNormalization()(x) outputs = Conv2D(num_classes, (1, 1), activation='softmax', padding='valid', name='pred', kernel_initializer='he_normal')(x) model = Model(inputs=img_input, outputs=outputs, name="SegNet") return model
def build_encoder_decoder(hh, ww): # Encoder base_model = VGG16(include_top=False, weights=None, input_tensor=None, input_shape=(hh, ww, 4), pooling=None) migrate_model(base_model) x = base_model.output orig_5 = base_model.get_layer('block5_pool').output orig_4 = base_model.get_layer('block4_pool').output orig_3 = base_model.get_layer('block3_pool').output orig_2 = base_model.get_layer('block2_pool').output orig_1 = base_model.get_layer('block1_pool').output # Decoder x = Conv2D(512, (1, 1), activation='relu', padding='same', name='deconv6', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) high_feature = x x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 512))(x) x = Conv2D(512, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) # low_feature = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal',bias_initializer='zeros')(orig_5) attention_feature = multiply([orig_5, x]) #concate the_shape = K.int_shape(attention_feature) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(attention_feature) xReshaped = Reshape(shape)(high_feature) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) # x = UpSampling2D(size=(2, 2))(x) #add scale transfer x = Conv2D(2048, (1, 1), padding='same', use_bias=False)(x) x = pixel_shuffler((2, 2))(x) #block_2 x = Conv2D(512, (5, 5), activation='relu', padding='same', name='deconv5', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) high_feature = x x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 512))(x) x = Conv2D(512, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) # low_feature = Conv2D(512, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', # bias_initializer='zeros')(orig_4) attention_feature = multiply([orig_4, x]) the_shape = K.int_shape(attention_feature) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(attention_feature) xReshaped = Reshape(shape)(high_feature) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) # x = UpSampling2D(size=(2, 2))(x) x = pixel_shuffler((2, 2))(x) # block_3 x = Conv2D(256, (5, 5), activation='relu', padding='same', name='deconv4', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) high_feature = x x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 256))(x) x = Conv2D(256, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) # low_feature = Conv2D(256, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', # bias_initializer='zeros')(orig_3) attention_feature = multiply([orig_3, x]) the_shape = K.int_shape(attention_feature) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(attention_feature) xReshaped = Reshape(shape)(high_feature) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) # x = UpSampling2D(size=(2, 2))(x) x = pixel_shuffler((2, 2))(x) # block_4 x = Conv2D(128, (5, 5), activation='relu', padding='same', name='deconv3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) high_feature = x x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 128))(x) x = Conv2D(128, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) # low_feature = Conv2D(128, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', # bias_initializer='zeros')(orig_2) attention_feature = multiply([orig_2, x]) the_shape = K.int_shape(attention_feature) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(attention_feature) xReshaped = Reshape(shape)(high_feature) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) # x = UpSampling2D(size=(2, 2))(x) x = pixel_shuffler((2, 2))(x) # block_5 x = Conv2D(64, (5, 5), activation='relu', padding='same', name='deconv2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) high_feature = x x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 64))(x) x = Conv2D(64, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) # low_feature = Conv2D(64, (3, 3), activation='relu', padding='same', kernel_initializer='he_normal', # bias_initializer='zeros')(orig_1) attention_feature = multiply([orig_1, x]) the_shape = K.int_shape(attention_feature) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(attention_feature) xReshaped = Reshape(shape)(high_feature) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) # x = UpSampling2D(size=(2, 2))(x) x = Conv2D(256, (1, 1), padding='same', use_bias=False)(x) x = pixel_shuffler((2, 2))(x) # block_6 x = Conv2D(64, (5, 5), activation='relu', padding='same', name='deconv1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) x = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='pred', kernel_initializer='he_normal', bias_initializer='zeros')(x) model = Model(inputs=base_model.input, outputs=x) return model
def build_model(): # Encoder img_input = Input(shape=(img_rows, img_cols, channel)) x = ZeroPadding2D((1, 1))(img_input) x = Conv2D(64, (3, 3), activation='relu', name='block1_conv1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(64, (3, 3), activation='relu', name='block1_conv2')(x) orig_1 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', name='block2_conv1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(128, (3, 3), activation='relu', name='block2_conv2')(x) orig_2 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='block3_conv1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='block3_conv2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='block3_conv3')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(256, (3, 3), activation='relu', name='block3_conv4')(x) orig_3 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='block4_conv1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='block4_conv2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='block4_conv3')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='block4_conv4')(x) orig_4 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='block5_conv1')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='block5_conv2')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='block5_conv3')(x) x = ZeroPadding2D((1, 1))(x) x = Conv2D(512, (3, 3), activation='relu', name='block5_conv4')(x) orig_5 = x x = MaxPooling2D((2, 2), strides=(2, 2))(x) # Decoder x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_5) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_5) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv5_1', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv5_2', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv5_3', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv5_4', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_4) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_4) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv4_1', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(512, (kernel, kernel), activation='relu', padding='same', name='deconv4_2', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(256, (kernel, kernel), activation='relu', padding='same', name='deconv4_3', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(256, (kernel, kernel), activation='relu', padding='same', name='deconv4_4', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_3) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_3) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(256, (kernel, kernel), activation='relu', padding='same', name='deconv3_1', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(256, (kernel, kernel), activation='relu', padding='same', name='deconv3_2', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(128, (kernel, kernel), activation='relu', padding='same', name='deconv3_3', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(128, (kernel, kernel), activation='relu', padding='same', name='deconv3_4', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = UpSampling2D(size=(2, 2))(x) the_shape = K.int_shape(orig_2) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(orig_2) xReshaped = Reshape(shape)(x) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(128, (kernel, kernel), activation='relu', padding='same', name='deconv2_1', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(64, (kernel, kernel), activation='relu', padding='same', name='deconv2_2', kernel_initializer='he_uniform')(x) x = BatchNormalization()(x) x = Conv2D(3, (1, 1), padding='same', name='pred', activation='sigmoid', kernel_initializer='he_uniform')(x) outputs = x model = Model(inputs=img_input, outputs=outputs) ensure_vgg_weights() model.load_weights('models/vgg19_notop.h5', by_name=True) return model
def build_encoder_decoder(hh,ww): # encoder resNet = ResNet50(include_top=False,weights= None, input_tensor=None, input_shape=(hh,ww,4), pooling=None) # migrate_model(resNet) x = resNet.output orig_5 = resNet.get_layer('activation_49').output orig_5 = Conv2D(512, (1, 1), padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(orig_5) orig_4 = resNet.get_layer('activation_40').output orig_4 = Conv2D(512, (1, 1), padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(orig_4) orig_3 = resNet.get_layer('activation_22').output orig_3 = Conv2D(256, (1, 1), padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(orig_3) orig_2 = resNet.get_layer('activation_10').output orig_2 = Conv2D(128, (1, 1), padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(orig_2) orig_1 = resNet.get_layer('activation_1').output orig_1 = Conv2D(64, (1, 1), padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(orig_1) #decoder #stage_one x = Conv2D(512, (1, 1), activation='relu', padding='same', name='deconv6', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) high_feature = x x = GlobalAveragePooling2D()(x) x = Reshape((1,1,512))(x) x = Conv2D(512, (1, 1), activation='relu', padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) attention_feature = multiply([orig_5, x]) #concate the_shape = K.int_shape(attention_feature) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(attention_feature) xReshaped = Reshape(shape)(high_feature) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) #add scale transfer x = Conv2D(2048, (1,1),padding='same',use_bias=False)(x) x = pixel_shuffler((2,2))(x) #stage_two x = Conv2D(512, (5, 5), activation='relu', padding='same', name='deconv5', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) high_feature = x x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 512))(x) x = Conv2D(512, (1,1), activation='relu', padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) attention_feature = multiply([orig_4, x]) the_shape = K.int_shape(attention_feature) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(attention_feature) xReshaped = Reshape(shape)(high_feature) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = pixel_shuffler((2, 2))(x) # stage_three x = Conv2D(256, (5, 5), activation='relu', padding='same', name='deconv4', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) high_feature = x x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 256))(x) x = Conv2D(256, (1,1), activation='relu', padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) attention_feature = multiply([orig_3, x]) the_shape = K.int_shape(attention_feature) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(attention_feature) xReshaped = Reshape(shape)(high_feature) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = pixel_shuffler((2, 2))(x) # stage_four x = Conv2D(128, (5, 5), activation='relu', padding='same', name='deconv3', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) high_feature = x x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 128))(x) x = Conv2D(128, (1,1), activation='relu', padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) attention_feature = multiply([orig_2, x]) the_shape = K.int_shape(attention_feature) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(attention_feature) xReshaped = Reshape(shape)(high_feature) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = pixel_shuffler((2, 2))(x) # stage_five x = Conv2D(64, (5, 5), activation='relu', padding='same', name='deconv2', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) high_feature = x x = GlobalAveragePooling2D()(x) x = Reshape((1, 1, 64))(x) x = Conv2D(64, (1,1), activation='relu', padding='same', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) attention_feature = multiply([orig_1, x]) the_shape = K.int_shape(attention_feature) shape = (1, the_shape[1], the_shape[2], the_shape[3]) origReshaped = Reshape(shape)(attention_feature) xReshaped = Reshape(shape)(high_feature) together = Concatenate(axis=1)([origReshaped, xReshaped]) x = Unpooling()(together) x = Conv2D(256, (1,1),padding='same',use_bias=False)(x) x = pixel_shuffler((2, 2))(x) # stage_six x = Conv2D(64, (5, 5), activation='relu', padding='same', name='deconv1', kernel_initializer='he_normal', bias_initializer='zeros')(x) x = BatchNormalization()(x) #prediction layer x = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='pred', kernel_initializer='he_normal', bias_initializer='zeros')(x) model = Model(inputs=resNet.input, outputs=x) return model