'how to add reshape layer before Conv3DTransposer in 3d unet?

ValueError: A Concatenate layer requires inputs with matching shapes except for the concatenation axis. Received: input_shape=[(None, 44, 44, 32, 128), (None, 28, 28, 16, 512)]

x = Input((128, 128,36,1)) 
inputs = x 
f = 64 # number of filters

layers = [] # Create a list to store the outputs from each box
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
layers.append(x) # append the output from each box to layers
x = MaxPooling3D() (x)

f = f * 2
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
layers.append(x) # append the output from each box to layers
x = MaxPooling3D() (x)

f = f * 2

x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = ZeroPadding3D(padding = (4,4,4))(x)     
layers.append(x) # append the output from each box to layers
x = MaxPooling3D() (x)
x = ZeroPadding3D(padding = (4,4,4))(x) 
f = f * 2
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
layers.append(x) # append the output from each box to layers
x = MaxPooling3D() (x)
x = ZeroPadding3D(padding = (4,4,4))(x) 

f = f * 2
ff2 = 128
 #bottleneck
j = len(layers) - 1 # 6 - 1 = 5 in our case
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3DTranspose(ff2, 2, strides = (2, 2,2), padding = 'valid') (x)
x = Concatenate(axis = -1)([x, layers[j]])

j = j -1

 #upsampling
ff2 = ff2//2
f = f // 2
x = Conv3D(f, 3, activation = 'relu', padding='same') (x)
x = Conv3D(f, 3, activation = 'relu', padding='same') (x)
x = Conv3DTranspose(ff2, 2, strides = (2, 2,2), padding = 'same') (x)
x = Concatenate(axis = -1)([x, layers[j]])
j = j - 1
outputs = Conv3D(1, 1, activation = 'sigmoid') (x)

 #model creation
model = Model(inputs = [inputs], outputs = [outputs])
model.summary(line_length=150)


Solution 1:[1]

I was able to reproduce the issue. So, I tried adding a few layers along with the Reshape layer. Please find the working code below.

x = Input((128, 128,36,1)) 
inputs = x 
f = 64 # number of filters

layers = [] # Create a list to store the outputs from each box
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
layers.append(x) # append the output from each box to layers
x = MaxPooling3D() (x)

f = f * 2
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
layers.append(x) # append the output from each box to layers
x = MaxPooling3D() (x)

f = f * 2
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = ZeroPadding3D(padding = (4,4,4))(x)     
layers.append(x) # append the output from each box to layers
x = MaxPooling3D() (x)
x = ZeroPadding3D(padding = (4,4,4))(x) 

f = f * 2
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
layers.append(x) # append the output from each box to layers
x = MaxPooling3D() (x)
x = ZeroPadding3D(padding = (4,4,4))(x) 


f = f * 2
ff2 = 128
 #bottleneck
j = len(layers) - 1 # 6 - 1 = 5 in our case
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
x = Conv3D(f, 3, activation = 'relu', padding = 'same') (x)
# Reshape layer 
x = Reshape((22, 22, 16, 1024))(x)
x = Conv3DTranspose(ff2, 1, strides = (1, 1, 1), padding = 'valid') (x) # Changed kernel size to 1 and strides to (1, 1, 1)
x = ZeroPadding3D(padding = (3,3,0))(x) 

x = Concatenate(axis = -1)([x, layers[j]])

ff2 = ff2//2
f = f // 2
x = Conv3D(f, 3, activation = 'relu', padding='same') (x)
x = Conv3D(f, 3, activation = 'relu', padding='same') (x)
x = Conv3DTranspose(ff2, 2, strides = (2, 2, 1), padding = 'same') (x) #Changed strides to (2, 2, 1)
# Added MaxPooling and ZeroPadding layers
x = MaxPooling3D(2,2) (x)
x = ZeroPadding3D(padding = (0,0,4))(x) 
x = Concatenate(axis = -1)([x, layers[j]])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1