TransWikia.com

Unable to make CNN model with same output and input dimensions

Data Science Asked on March 26, 2021

I’m trying to develop a simple CNN model that takes in a RGB images and returns the same 3 channel as output.

Eg: 3 x 128 x 128 is what I’m giving as input

K.set_image_data_format('channels_first')
def resBlock(x, channels, kernel_size=[3, 3], scale=0.1):
    tmp = Conv2D(channels, kernel_size, kernel_initializer='he_uniform', padding='same')(x)
    tmp = Activation('relu')(tmp)
    tmp = Conv2D(channels, kernel_size, kernel_initializer='he_uniform', padding='same')(tmp)
    tmp = Lambda(lambda x: x * scale)(tmp)

    return Add()([x, tmp])

feature_size = 128

input_shape = (3, 128,128)
inputs = Input(shape=input_shape)

# x = Concatenate(axis=1)([inputs])
x = Conv2D(feature_size, kernel_size=(3,3), activation='relu', input_shape=input_shape, padding='same')(inputs)
for i in range(6):
    x = resBlock(x, feature_size)

x = Conv2D(feature_size, (3, 3), kernel_initializer='he_uniform', padding='same')(x)
x = Add()([x, inputs])
model = Model(inputs=inputs,  outputs = x)

This is what I have currently, however, it throws an error:

ValueError: Operands could not be broadcast together with shapes (128, 128, 128) (3, 128, 128)

It fails in this line: x = Add()([x, inputs])

One Answer

The error indicates that you are trying to add tensors of incompatible dimensions, as x has 128 channels and inputs has 3 channels.

The reason why x has 128 channels is just in the line above, where you pass feature_size with value 128 as the number of output channels. Change the number of output channels in that line to 3 and it should work, like this:

x = Conv2D(3, (3, 3), kernel_initializer='he_uniform', padding='same')(x)

Correct answer by noe on March 26, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP