0
votes

I am trying to build a U-Net model with a VGG16 encoder. This is the model code.

from keras.applications.vgg16 import VGG16
base_pretrained_model = VGG16(input_shape =  shape, include_top = False, weights = 'imagenet')
base_pretrained_model.trainable = False
base_pretrained_model.summary()
inp = Input(shape=shape,name = 'image_input')
output_vgg16_conv = base_pretrained_model(inp)

# output and start upsampling
conv_1  = Conv2D(512, (3,3), activation='relu', padding='same')(output_vgg16_conv)
up_conv = Conv2DTranspose(256, (3,3), strides=(2,2), activation='relu', padding='same')(conv_1)

# first concatenation block
concat_1 = concatenate([base_pretrained_model.get_layer('block5_conv3').output, up_conv], axis=-1, name='concat_1')
conv_2 = Conv2D(512, (3,3), activation='relu', padding='same')(concat_1)
up_conv_2 = Conv2DTranspose(256, (3,3), strides=(2,2), activation='relu', padding='same')(conv_2)

# second concatenation block
concat_2 = concatenate([up_conv_2, base_pretrained_model.get_layer('block4_conv3').output])
conv_3 = Conv2D(512, (3,3), activation='relu', padding='same')(concat_2)
up_conv_3 = Conv2DTranspose(128, (3,3), strides=(2,2), activation='relu', padding='same')(conv_3)

# third concatenation block
concat_3 = concatenate([up_conv_3, base_pretrained_model.get_layer('block3_conv3').output])
conv_4 = Conv2D(256, (3,3), activation='relu', padding='same')(concat_3)
up_conv_4 = Conv2DTranspose(64, (3,3), strides=(2,2), activation='relu', padding='same')(conv_4)

# fourth concatenation block
concat_4 = concatenate([up_conv_4, base_pretrained_model.get_layer('block2_conv2').output])
conv_5 = Conv2D(128, (3,3), activation='relu', padding='same')(concat_4)
up_conv_5 = Conv2DTranspose(32, (3,3), strides=(2,2), activation='relu', padding='same')(conv_5)

# fifth concatenation block
concat_4 = concatenate([up_conv_5, base_pretrained_model.get_layer('block1_conv2').output])
conv_6 = Conv2D(128, (3,3), activation='sigmoid', padding='same')(concat_4)

finalModel = Model(inp, output = conv_6)

I get the following error.

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 512, 512, 3), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []

Note: input_1 is the input layer to VGG16 model

1

1 Answers

1
votes

You don't need to define an additional Input layer since one Input layer is already there in the VGG16 model, which you can access by using

inp = base_pretrained_model.input

You also can get the output of VGG16 model by using

output_vgg16_conv = base_pretrained_model.output

Additionally, To keep the weights of base model VGG16 as it is, you can make each layer untrainable:

for layer in base_pretrained_model.layers:
    layer.trainable = False

Hope it will help.