Czorio
Czorio

Reputation: 115

Adding layer outputs with a shape mismatch

I am trying to build a V-Net, as described by Milletari et al., wherein Add layers need to be included. However, as the shape of the tensor from the Input layer is modified in the following Conv3D layer, the Add layer reports a ValueError as the shapes of the output tensors no longer match. Add requires these shapes to be the same.

In my case:

ValueError: Operands could not be broadcast together with shapes (3, 87, 512, 512) (16, 87, 512, 512)

Unfortunately, the Add layer has a generic enough term that online searches do not return relevant hits.

Example code:

# Assume imports are present

# Down Block 1
input_layer = Input(input_shape)
conv_1_1 = Conv3D(filters=16, kernel_size=(5, 5, 5), padding="same")(input_layer)
prelu_1_1 = PReLU()(conv_1_1)
add_1_1 = Add()([input_layer, prelu_1_1])
conv_1_2 = Conv3D(filters=16, kernel_size=(2, 2, 2), strides=(2, 2, 2)(add_1_1)
prelu_1_2 = PReLU()(conv_1_2)

# Down Block 2
# ...

keras.backend.tile is applied to the wrong dimension, and keras.backend.expand_dims only adds additional axes, not grow one. keras.layers.Reshape fails, as the new shape does not have the same amount of elements. A Concatenate layer could work, however, the article on V-Nets explicitly calls for an element-wise sum.

Other users have opted for the use of the merge() function, however, this function has been deprecated.

I would like to find a way to either grow the input_layer tensor to match the shape of the prelu_1_1 layer, or to find a function that lets me sum mismatched tensors.

Unfortunately, I have not been able to find any source code from the mentioned article, and all implementations that I have found seem to not clearly address my issue.

Upvotes: 0

Views: 154

Answers (1)

techytushar
techytushar

Reputation: 803

From the docs:

It takes as input a list of tensors, all of the same shape, and returns a single tensor (also of the same shape).

The shape of outputs of both input and prelu layer must be same. After applying conv on input the shape might have changed and that is why both layers can not be added together.

Upvotes: 0

Related Questions