title: ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling3d_3/MaxPool3D'(op: 'MaxPool3D')with input shapes[?,1,60,60,128]
body: <p>I am building a keras UNET model for 3D image segmentation.</p>
  
  <p>Image shape  <code>240, 240, 150</code></p>
  
  <p>The input shape is <code>240, 240, 150, 4, 335</code> >> training data</p>
  
  <p>The output shape should be <code>240, 240, 150, 335</code> >> training labels</p>
  
  <p>I am using Conv3D, MaxPooling3D, Conv3DTranspose, and concatenate layers to build the model</p>
  
  <p>I am facing this error during the model building where I am doing upsampling </p>
  
  <p><code>ValueError: Negative dimension size caused by subtracting 2 from 1 for 'max_pooling3d_3/MaxPool3D' (op: 'MaxPool3D') with input shapes: [?,1,60,60,128].</code></p>
  
  <p>I searched for some solutions and found Layers <code>padding='same'</code> and <code>k.set_image_data_format('channels_last')</code></p>
  
  <p>with this I faced a new error when doing the concatination after the up sampling</p>
  
  <p><code>ValueError: A `Concatenate` layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 30, 30, 18, 256), (None, 30, 30, 19, 256)]</code></p>
  
  <p>I currently looping between those two errors and can't find what is exact issue to solve nether how to solve it</p>
  
  <p>Here is the code where I am building the model</p>
  
  <pre><code>def build_unet_model(input_shape):
      inputs = Input(input_shape)
  
      conv1 = create_shared_convolution(inputs, 32, config.KERNEL_SIZE)
      block1 = down_convolution(conv1, config.POOL_SIZE)
  
      conv2 = create_shared_convolution(block1, 64, config.KERNEL_SIZE)
      block2 = down_convolution(conv2, config.POOL_SIZE)
  
      conv3 = create_shared_convolution(block2, 128, config.KERNEL_SIZE)
      block3 = down_convolution(conv3, config.POOL_SIZE)
  
      conv4 = create_shared_convolution(block3, 256, config.KERNEL_SIZE)
      block4 = down_convolution(conv4, config.POOL_SIZE)
  
      conv5 = create_shared_convolution(block4, 512, config.KERNEL_SIZE)  # mid_con
      up1 = concatenate_layers(create_up_convolution(conv5, 256, config.STRIDE_SIZE), conv4)
  
      conv6 = create_shared_convolution(up1, 256, config.KERNEL_SIZE)
      up2 = concatenate_layers(create_up_convolution(conv6, 128, config.STRIDE_SIZE), conv3)
  
      conv7 = create_shared_convolution(up2, 128, config.KERNEL_SIZE)
      up3 = concatenate_layers(create_up_convolution(conv7, 64, config.STRIDE_SIZE), conv2)
  
      conv8 = create_shared_convolution(up3, 64, config.KERNEL_SIZE)
      up4 = concatenate_layers(create_up_convolution(conv8, 32, config.STRIDE_SIZE), conv1)
  
      conv9 = create_shared_convolution(up4, 32, config.KERNEL_SIZE)
      outputs = create_output_layer(conv9, 4, (1, 1, 1))
  
      model = Model(inputs=[inputs], outputs=[outputs])
      print(model.summary())
      return model.compile(optimizer=AdaBound(lr=1e-5, final_lr=1), loss=utils.ce_dl_loss, metrics=['accuracy'])
  </code></pre>
  
  <p>and those are the 5 functions used in the model building</p>
  
  <pre><code>def create_shared_convolution(input_layer, number_of_nets, kernel_size,
                                activation='relu', padding='same',
                                kernel_initializer=initializers.random_normal(stddev=0.01)):
      conv = Conv3D(number_of_nets, kernel_size, activation=activation, padding=padding,
                    kernel_initializer=kernel_initializer)(input_layer)
  
      conv = Conv3D(number_of_nets, kernel_size, activation=activation, padding=padding,
                    kernel_initializer=kernel_initializer)(conv)
      return conv
  
  def down_convolution(input_layer, pool_size):
      return MaxPooling3D(pool_size=pool_size)(input_layer)
  
  
  def create_up_convolution(input_layer, number_of_nets, stride_size, padding='same',
                            kernel_initializer=initializers.random_normal(stddev=0.01)):
      return Conv3DTranspose(number_of_nets, stride_size, strides=stride_size, padding=padding,
                             kernel_initializer=kernel_initializer)(input_layer)
  
  def concatenate_layers(layer1, layer2):
      return merge.concatenate([layer1, layer2])
  
  
  def create_output_layer(input_layer, number_of_nets, kernel_size, activation='relu',
                          kernel_initializer=initializers.random_normal(stddev=0.01)):
      conv = Conv3D(number_of_nets, kernel_size, activation=activation,
                    kernel_initializer=kernel_initializer)(input_layer)
  
      return Activation('softmax')(conv)
  </code></pre>