Keras中VGG16的输入层

时间:2019-05-03 20:26:03

标签: python keras

我正在构建一个U-Net,我想在解码器部分使用预训练模型(VGG16)。

挑战在于我拥有灰度图像,而VGG与RGB兼容。

我发现了一个将其转换为RGB(通过级联)的函数:

from keras.layers import Layer
from keras import backend as K

class Gray2VGGInput(Layer):
    """Custom conversion layer"""
    def build(self, x):
        self.image_mean = K.variable(value=np.array([103.939, 116.779, 123.68]).reshape([1,1,1,3]).astype('float32'), 
                                     dtype='float32', 
                                     name='imageNet_mean' )
        self.built = True
        return
    def call(self, x):
        rgb_x = K.concatenate([x,x,x], axis=-1 )
        norm_x = rgb_x - self.image_mean
        return norm_x

    def compute_output_shape(self, input_shape):
        return input_shape[:3] + (3,)

但是我无法将其插入模型。 Gray2VGGInput是一层,因此我正在寻找一种方法将该层与VGG中的层连接。以下是我的尝试:

def UNET1_VGG16():
    ''' 
    UNET with pretrained layers from VGG16
    '''
    def upsampleLayer(in_layer, concat_layer, input_size):
        '''
        Upsampling (=Decoder) layer building block
        Parameters
        ----------
        in_layer: input layer
        concat_layer: layer with which to concatenate
        input_size: input size fot convolution
        '''
        upsample = Conv2DTranspose(input_size, (2, 2), strides=(2, 2), padding='same')(in_layer)    
        upsample = concatenate([upsample, concat_layer])
        conv = Conv2D(input_size, (1, 1), activation='relu', kernel_initializer='he_normal', padding='same')(upsample)
        conv = BatchNormalization()(conv)
        conv = Dropout(0.2)(conv)
        conv = Conv2D(input_size, (1, 1), activation='relu', kernel_initializer='he_normal', padding='same')(conv)
        conv = BatchNormalization()(conv)
        return conv

    img_rows = 864
    img_cols = 1232

    #--------
    #INPUT
    #--------
    #batch, height, width, channels
    inputs_1 = Input((img_rows, img_cols, 1))
    inputs_3 = Input((img_rows, img_cols, 3))

    #--------
    #VGG16 BASE
    #--------
    #Prepare net
    base_VGG16 = VGG16(input_tensor=inputs_3, 
                       include_top=False, 
                       weights='imagenet')

    #----------------
    #INPUT CONVERTER
    #----------------
    #This is the problematic part

    vgg_inputs_3 = Gray2VGGInput(name='gray_to_rgb')(inputs_1)

    model_input = Model(inputs=[inputs_1], outputs=[vgg_inputs_3])

    new_outputs = base_VGG16(model_input.output)
    new_inputs = Model(inputs_1, new_outputs)

    #--------
    #DECODER
    #--------
    c1 = base_VGG16.get_layer("block1_conv2").output #(None, 864, 1232, 64)
    c2 = base_VGG16.get_layer("block2_conv2").output #(None, 432, 616, 128) 
    c3 = base_VGG16.get_layer("block3_conv2").output #(None, 216, 308, 256) 
    c4 = base_VGG16.get_layer("block4_conv2").output #(None, 108, 154, 512) 

    #--------
    #BOTTLENECK
    #--------
    c5 = base_VGG16.get_layer("block5_conv2").output #(None, 54, 77, 512)

    #--------
    #ENCODER
    #--------    
    c6 = upsampleLayer(in_layer=c5, concat_layer=c4, input_size=512)
    c7 = upsampleLayer(in_layer=c6, concat_layer=c3, input_size=256)
    c8 = upsampleLayer(in_layer=c7, concat_layer=c2, input_size=128)
    c9 = upsampleLayer(in_layer=c8, concat_layer=c1, input_size=64)

    #--------
    #DENSE OUTPUT
    #--------
    outputs = Conv2D(1, (1, 1), activation='sigmoid')(c9)

    model = Model(inputs=[new_inputs.input], outputs=[outputs])

    #Freeze layers
    for layer in model.layers[:16]:
        layer.trainable = False

    print(model.summary())

    model.compile(optimizer='adam', 
                  loss=fr.diceCoefLoss, 
                  metrics=[fr.diceCoef])

    return model 

我收到以下错误:

  

ValueError:图表已断开连接:无法在“ input_14”层获取张量Tensor(“ input_14:0”,shape =(?, 864,1232,3),dtype = float32)的值。可以顺利访问以下先前的图层:[]

2 个答案:

答案 0 :(得分:1)

我认为您不需要多个输入,而是将Gray2VGGInput层的输出作为输入传递到VGG16模型。我认为您可以从VGG16模型中获得输出张量。我可以建议以下几点:

from keras.applications import VGG16


inputs_1 = Input(shape=(img_rows, img_cols, 1))
inputs_3 = Gray2VGGInput(name='gray_to_rgb')(inputs_1)  #shape=(img_rows, img_cols, 3)
base_VGG16 = VGG16(include_top=False, weights='imagenet', input_tensor=inputs_3)

#--------
#DECODER
#--------
c1 = base_VGG16.get_layer("block1_conv2").output #(None, 864, 1232, 64)
c2 = base_VGG16.get_layer("block2_conv2").output #(None, 432, 616, 128) 
c3 = base_VGG16.get_layer("block3_conv2").output #(None, 216, 308, 256) 
c4 = base_VGG16.get_layer("block4_conv2").output #(None, 108, 154, 512) 

#--------
#BOTTLENECK
#--------
c5 = base_VGG16.get_layer("block5_conv2").output #(None, 54, 77, 512)
... 
... and so on

该模型可以称为

model = Model(inputs=inputs_1, outputs=outputs)

您可以尝试一下,让我知道它是否有效。我没有测试过,所以可能会有错误。

答案 1 :(得分:0)

更改:

model_input = Model(inputs=[inputs_1], outputs=[vgg_inputs_3]) 

收件人

model_input = Model(inputs=[vgg_inputs_3] etc...