Python – How to add skip joins between convolutional layers in Keras

How to add skip joins between convolutional layers in Keras… here is a solution to the problem.

How to add skip joins between convolutional layers in Keras

I want to add a skip connection between the residual blocks of keras. This is my current implementation and it doesn’t work because tensors have different shapes.

The function is as follows:

def build_res_blocks(net, x_in, num_res_blocks, res_block, num_filters, res_block_expansion, kernel_size, scaling):
    net_next_in = net
    for i in range(num_res_blocks):
        net = res_block(net_next_in, num_filters, res_block_expansion, kernel_size, scaling)
    
# net tensor shape: (None, None, 32)
        # x_in tensor shape: (None, None, 3)
        # Error here, net_next_in should be in the shape of (None, None, 32) to be fed into next layer
        net_next_in = Add()([net, x_in]) 

return net

But I see

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

How do I add or merge these tensors into the correct shape (None, None, 32)? If this is not the right approach, how can you achieve the desired result?

This is what res_block looks like:

def res_block(x_in, num_filters, expansion, kernel_size, scaling):
    x = Conv2D(num_filters * expansion, kernel_size, padding='same')(x_in)
    x = Activation('relu')(x)
    x = Conv2D(num_filters, kernel_size, padding='same')(x)
    x = Add()([x_in, x])
return x

Solution

You cannot add tensors of different shapes. You can concatenate them with keras.layers.Concatenate, but this will leave you with tensors of shape [None, None, 35].

Or, take a look
Resnet50 Implemented in Keras. Their residual block has 1x1xC convolution in the shortcut and is suitable for cases where the dimensions to be added are different.

Related Problems and Solutions