Problem in Unet_model()

{moderator edit - solution code removed}

import outputs
img_height = 96
img_width = 128
num_channels = 3

unet = unet_model((img_height, img_width, num_channels))
comparator(summary(unet), outputs.unet_model_output)

ValueError                                Traceback (most recent call last)
<ipython-input-24-4a1225a2a898> in <module>
      4 num_channels = 3
      5 
----> 6 unet = unet_model((img_height, img_width, num_channels))
      7 comparator(summary(unet), outputs.unet_model_output)

<ipython-input-23-4032af828372> in unet_model(input_size, n_filters, n_classes)
     19     # Chain the first element of the output of each block to be the input of the next conv_block.
     20     # Double the number of filters at each new step
---> 21     cblock2 = conv_block(cblock1, 2*n_filters)
     22     cblock3 = conv_block(cblock2,4*n_filters)
     23     cblock4 = conv_block(cblock3, 8*n_filters, dropout=0.3) # Include a dropout_prob of 0.3 for this layer

<ipython-input-15-12d650896d40> in conv_block(inputs, n_filters, dropout_prob, max_pooling)
     19                   activation='relu',
     20                   padding='same',
---> 21                   kernel_initializer= 'he_normal')(inputs)
     22     conv = Conv2D(n_filters, # Number of filters
     23                   3,   # Kernel size

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    924     if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
    925       return self._functional_construction_call(inputs, args, kwargs,
--> 926                                                 input_list)
    927 
    928     # Maintains info about the `Layer.call` stack.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
   1090       # TODO(reedwm): We should assert input compatibility after the inputs
   1091       # are casted, not before.
-> 1092       input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
   1093       graph = backend.get_graph()
   1094       # Use `self._name_scope()` to avoid auto-incrementing the name.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    156                      str(len(input_spec)) + ' inputs, '
    157                      'but it received ' + str(len(inputs)) +
--> 158                      ' input tensors. Inputs received: ' + str(inputs))
    159   for input_index, (x, spec) in enumerate(zip(inputs, input_spec)):
    160     if spec is None:

ValueError: Layer conv2d_29 expects 1 inputs, but it received 2 input tensors. Inputs received: [<tf.Tensor 'max_pooling2d_11/MaxPool:0' shape=(None, 48, 64, 32) dtype=float32>, <tf.Tensor 'conv2d_28/Relu:0' shape=(None, 96, 128, 32) dtype=float32>]

How to solve this error please help?

Note that the conv_block function returns two tensors, right? One to feed to the next step in the downsampling path and one that is the input for the “skip layer” that feeds over to the corresponding stage of the upsampling path. So you have to select which one you want for the particular purpose by indexing the output. Note that the upsampling blocks are different: they have only one output.

Also please note that we’re not supposed to just post our code publicly and say in effect “please fix it for me”. We could have provided the above help just from the error message. It’s better to start by just posting the full error trace. If we can’t figure out how to help from that, then we can ask for more details.

5 Likes
ValueError                                Traceback (most recent call last)
<ipython-input-56-4a1225a2a898> in <module>
      4 num_channels = 3
      5 
----> 6 unet = unet_model((img_height, img_width, num_channels))
      7 comparator(summary(unet), outputs.unet_model_output)

<ipython-input-55-a624af047b72> in unet_model(input_size, n_filters, n_classes)
     34     # Note that you must use the second element of the contractive block i.e before the maxpooling layer.
     35     # At each step, use half the number of filters of the previous block
---> 36     ublock7 = upsampling_block(ublock6[0],cblock3[1],n_filters * 4)
     37     ublock8 = upsampling_block(ublock7[0], cblock2[1],  n_filters * 2)
     38     ublock9 = upsampling_block(ublock8[0], cblock1[1], n_filters)

<ipython-input-39-f2fc6b166c01> in upsampling_block(expansive_input, contractive_input, n_filters)
     18                  3,    # Kernel size
     19                  strides=(2,2),
---> 20                  padding='same')(expansive_input)
     21 
     22     # Merge the previous output and the contractive_input

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, *args, **kwargs)
    924     if _in_functional_construction_mode(self, inputs, args, kwargs, input_list):
    925       return self._functional_construction_call(inputs, args, kwargs,
--> 926                                                 input_list)
    927 
    928     # Maintains info about the `Layer.call` stack.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _functional_construction_call(self, inputs, args, kwargs, input_list)
   1090       # TODO(reedwm): We should assert input compatibility after the inputs
   1091       # are casted, not before.
-> 1092       input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
   1093       graph = backend.get_graph()
   1094       # Use `self._name_scope()` to avoid auto-incrementing the name.

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
    194                          ', found ndim=' + str(ndim) +
    195                          '. Full shape received: ' +
--> 196                          str(x.shape.as_list()))
    197     # Check dtype.
    198     if spec.dtype is not None:

ValueError: Input 0 of layer conv2d_transpose_9 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [12, 16, 256]

@paulinpaloalto I have made some improvement but again the problem persist , what can I do next please suggest I stuck in this problem.

1 Like

It sounds like you fixed the problem on the downsampling path, but you missed the point that I mentioned about the upsampling path being different: those functions only output one tensor, so you don’t need to index off the one you want. But you did that, so you’ve removed one of the dimensions of the tensor, which is why it throws that error.

2 Likes

@paulinpaloalto Thank you soo much the problem is solved…