Search code examples
keraskeras-layer

keras pre-trained ResNet50 target shape


I am trying to use ResNet50 Pretrained network for segmentation problem. I remove the last layer and add my desired layer. But when I try to fit, I get the following error:

ValueError: Error when checking target: expected conv2d_1 to have shape (16, 16, 1) but got array with shape (512, 512, 1)

I have two folders: images and masks. images are RGB and masks are in grayscale. The shape is 512x512 for all images. I can not figure in which part am I doing wrong.

Any help will be appreciated.

from keras.applications.resnet50 import ResNet50
image_input=Input(shape=(512, 512, 3))
model = ResNet50(input_tensor=image_input,weights='imagenet',include_top=False)
x = model.output
x = Conv2D(1, (1,1), padding="same", activation="sigmoid")(x)
model = Model(inputs=model.input, outputs=x)
model.summary()

conv2d_1 (Conv2D)           (None, 16, 16, 1)    2049 activation_49[0][0]              

for layer in model.layers[:-1]:
    layer.trainable = False

for layer in model.layers[-1:]:
    layer.trainable = True
model.compile(optimizer=Adam(), loss='binary_crossentropy', metrics=['accuracy'])

Solution

  • Your network gives an output of shape (16, 16, 1) but your y (target) has shape (512, 512, 1)

    Run the following to see this.

    from keras.applications.resnet50 import ResNet50
    from keras.layers import Input
    
    image_input=Input(shape=(512, 512, 3))
    model = ResNet50(input_tensor=image_input,weights='imagenet',include_top=False)
    model.summary()
    
    # Output shows that the ResNet50 network has output of shape (16,16,2048)
    
    from keras.layers import Conv2D
    
    conv2d = Conv2D(1, (1,1), padding="same", activation="sigmoid")
    conv2d.compute_output_shape((None, 16, 16, 2048))
    
    # Output shows the shape your network's output will have.
    

    Either your y or the way you use ResNet50 has to change. Read about ResNet50 to see what you are missing.