question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

UResNet Error: ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis.

See original GitHub issue

I am trying to create an Unet with the following code:

img_size_ori = 101
img_size_target = 197
model = UResNet34((img_size_target, img_size_target, 3))

But then I got this error message:

Traceback (most recent call last):
  File ".\main_unet.py", line 24, in <module>
    model = UResNet34((img_size_target, img_size_target, 3))
  File "F:\Workspace\kaggle\Salt Identification\model\segmentation_models\unet\models.py", line 40, in UResNet34
    activation=activation, **kwargs)
  File "F:\Workspace\kaggle\Salt Identification\model\segmentation_models\unet\builder.py", line 38, in build_unet
    x = up_block(filters, i, upsample_rate=up_size, skip=skip, **kwargs)(x)
  File "F:\Workspace\kaggle\Salt Identification\model\segmentation_models\unet\blocks.py", line 27, in layer
    x = Concatenate()([x, skip])
  File "F:\Anaconda\lib\site-packages\keras\engine\base_layer.py", line 431, in __call__
    self.build(unpack_singleton(input_shapes))
  File "F:\Anaconda\lib\site-packages\keras\layers\merge.py", line 354, in build
    'Got inputs shapes: %s' % (input_shape))
ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 14, 14, 512), (None, 13, 13, 256)]

Thanks for this nice repository.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

29reactions
qubvelcommented, Sep 3, 2018

Height and width of input images should be divisible by 32 for all models

0reactions
tarunnithcommented, Nov 25, 2020

Hi @qubvel I am trying to train unet with efficientnnet and this is what I am using. and size of my images and masks is 256x256x3 . I get this error: A Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 32, 32, 64), (None, 1, 1, 8)]

when I run the below block of code:

K.clear_session()
img_size = 256
model = UEfficientNet(input_shape=(img_size, img_size,3),dropout_rate=0.25)

I get error on this line:

uconv4 = concatenate([deconv4, conv4])

def UEfficientNet(input_shape=(None, None,3),dropout_rate=0.1):
    print(input_shape)
    backbone = efn.EfficientNetB4(weights='imagenet',
                            include_top=False,
                            input_shape=input_shape)
    input = backbone.input
    
    start_neurons = 16

    conv4 = backbone.layers[342].output
    conv4 = LeakyReLU(alpha=0.1)(conv4)
    pool4 = MaxPooling2D((2, 2))(conv4)
    pool4 = Dropout(dropout_rate)(pool4)
    
     # Middle
    convm = Conv2D(start_neurons * 32, (3, 3), activation=None, padding="same")(pool4)
    convm = residual_block(convm,start_neurons * 32)
    convm = residual_block(convm,start_neurons * 32)
    convm = LeakyReLU(alpha=0.1)(convm)
    
    deconv4 = Conv2DTranspose(start_neurons * 16, (3, 3), strides=(2, 2), padding="same")(convm)
    **uconv4 = concatenate([deconv4, conv4])**
    uconv4 = Dropout(dropout_rate)(uconv4)
    
    uconv4 = Conv2D(start_neurons * 16, (3, 3), activation=None, padding="same")(uconv4)
    uconv4 = residual_block(uconv4,start_neurons * 16)
    uconv4 = residual_block(uconv4,start_neurons * 16)
    uconv4 = LeakyReLU(alpha=0.1)(uconv4)
    
    deconv3 = Conv2DTranspose(start_neurons * 8, (3, 3), strides=(2, 2), padding="same")(uconv4)
    conv3 = backbone.layers[154].output
    uconv3 = concatenate([deconv3, conv3])    
    uconv3 = Dropout(dropout_rate)(uconv3)
    
    uconv3 = Conv2D(start_neurons * 8, (3, 3), activation=None, padding="same")(uconv3)
    uconv3 = residual_block(uconv3,start_neurons * 8)
    uconv3 = residual_block(uconv3,start_neurons * 8)
    uconv3 = LeakyReLU(alpha=0.1)(uconv3)

    deconv2 = Conv2DTranspose(start_neurons * 4, (3, 3), strides=(2, 2), padding="same")(uconv3)
    conv2 = backbone.layers[92].output
    uconv2 = concatenate([deconv2, conv2])
        
    uconv2 = Dropout(0.1)(uconv2)
    uconv2 = Conv2D(start_neurons * 4, (3, 3), activation=None, padding="same")(uconv2)
    uconv2 = residual_block(uconv2,start_neurons * 4)
    uconv2 = residual_block(uconv2,start_neurons * 4)
    uconv2 = LeakyReLU(alpha=0.1)(uconv2)
    
    deconv1 = Conv2DTranspose(start_neurons * 2, (3, 3), strides=(2, 2), padding="same")(uconv2)
    conv1 = backbone.layers[30].output
    uconv1 = concatenate([deconv1, conv1])
    
    uconv1 = Dropout(0.1)(uconv1)
    uconv1 = Conv2D(start_neurons * 2, (3, 3), activation=None, padding="same")(uconv1)
    uconv1 = residual_block(uconv1,start_neurons * 2)
    uconv1 = residual_block(uconv1,start_neurons * 2)
    uconv1 = LeakyReLU(alpha=0.1)(uconv1)
    
    uconv0 = Conv2DTranspose(start_neurons * 1, (3, 3), strides=(2, 2), padding="same")(uconv1)   
    uconv0 = Dropout(0.1)(uconv0)
    uconv0 = Conv2D(start_neurons * 1, (3, 3), activation=None, padding="same")(uconv0)
    uconv0 = residual_block(uconv0,start_neurons * 1)
    uconv0 = residual_block(uconv0,start_neurons * 1)
    uconv0 = LeakyReLU(alpha=0.1)(uconv0)
    
    uconv0 = Dropout(dropout_rate/2)(uconv0)
    output_layer = Conv2D(1, (1,1), padding="same", activation="sigmoid")(uconv0)    
    
    model = Model(input, output_layer)
    model.name = 'u-xception'

    return model

@PallawiSinghal I am also getting this error. How do you resolve it?

Have you found the solution to the problem?

I think this is due to the wrong concatenation of layers. in model.summary() find out the correct layers to concatenate. I will post the correct code in my repository tomorrow or day after tomorrow…

Read more comments on GitHub >

github_iconTop Results From Across the Web

A `Concatenate` layer requires inputs with matching shapes ...
ValueError : A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 523, 523, 32), etc....
Read more >
A`Concatenate`layer requires inputs with matching shapes ...
Hello ValueError occurred when start learning tlt-yolo. Size of the training image is 1280 x 720. The below error occurred when setting ...
Read more >
Concatenate layer - Keras
Layer that concatenates a list of inputs. It takes as input a list of tensors, all of the same shape except for the...
Read more >
A 'Concatenate' Layer Requires Inputs With Matching Shapes
Ask questionsUResNet Error: ValueError: A Concatenate layer requires inputs with matching shapes except for the concat axis. I am trying to create an...
Read more >
"concat" mode can only merge layers with matching output ...
When concatenating two tensors along a specific axis, all other dimensions except the one being concatenated must be the same.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found