U_net Model Course 4 Week 3

I’ve tried to this problem but it still doesn’t work.
This my prob:

ValueError Traceback (most recent call last)
in
3 num_channels = 3
4
----> 5 unet = unet_model((img_height, img_width, num_channels))

in unet_model(input_size, n_filters, n_classes)
30 # Use the cblock5[0] as expansive_input and cblock4[1] as contractive_input and n_filters * 8
31 ### START CODE HERE
—> 32 ublock6 = upsampling_block(cblock5[0], cblock4[1], n_filters*8)
33 # Chain the output of the previous block as expansive_input and the corresponding contractive block output.
34 # Note that you must use the second element of the contractive block i.e before the maxpooling layer.

in upsampling_block(expansive_input, contractive_input, n_filters)
21
22 # Merge the previous output and the contractive_input
—> 23 merge = concatenate([up, contractive_input], axis=3)
24 conv = Conv2D(n_filters, # Number of filters
25 3, # Kernel size

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in concatenate(inputs, axis, **kwargs)
929 A tensor, the concatenation of the inputs alongside axis axis.
930 “”"
→ 931 return Concatenate(axis=axis, **kwargs)(inputs)
932
933

/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)
1096 # Build layer if applicable (if the build method has been
1097 # overridden).
→ 1098 self._maybe_build(inputs)
1099 cast_inputs = self._maybe_cast_inputs(inputs, input_list)
1100

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
2641 # operations.
2642 with tf_utils.maybe_init_scope(self):
→ 2643 self.build(input_shapes) # pylint:disable=not-callable
2644 # We must set also ensure that the layer is marked as built, and the build
2645 # shape is stored since user defined build functions may not be calling

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/tf_utils.py in wrapper(instance, input_shape)
321 if input_shape is not None:
322 input_shape = convert_shapes(input_shape, to_tuples=True)
→ 323 output_shape = fn(instance, input_shape)
324 # Return shapes from fn as TensorShapes.
325 if output_shape is not None:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in build(self, input_shape)
517 shape[axis] for shape in shape_set if shape[axis] is not None)
518 if len(unique_dims) > 1:
→ 519 raise ValueError(err_msg)
520
521 def _merge_function(self, inputs):

ValueError: A Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 192, 256, 256), (None, 96, 128, 256)]

Hope to see your help.

There is an error in your call to “concatenate()”

I dont know what you mean.This is my code.

Sorry, I was reading the wrong line of code.

The error is detected in your “ublock6 =…” line. But the error may be in your code for cblock5 or cblock4.

2 Likes

That’s the error message you need to pay attention to.

2 Likes

In addition to the points from @TMosh, please note that only the conv block has two outputs. The upsampling block has only one output, so you don’t need to index off the appropriate element. That may be causing at least some of your problems.

5 Likes

This advice helped me realize my mistake!
Thanks!

1 Like

I had the same issue, thanks for your help Sir!

thanks , this also resolved my problem

I am facing this error:

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

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, cblock3[1], n_filters * 4)
37 ublock8 = upsampling_block(ublock7, cblock2[1], n_filters * 2)
38 ublock9 = upsampling_block(ublock8, cblock1[1], n_filters)

in upsampling_block(expansive_input, contractive_input, n_filters)
21
22 # Merge the previous output and the contractive_input
—> 23 merge = concatenate([up, contractive_input], axis=3)
24 conv = Conv2D(n_filters, # Number of filters
25 3, # Kernel size

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in concatenate(inputs, axis, **kwargs)
929 A tensor, the concatenation of the inputs alongside axis axis.
930 “”"
→ 931 return Concatenate(axis=axis, **kwargs)(inputs)
932
933

/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)
1096 # Build layer if applicable (if the build method has been
1097 # overridden).
→ 1098 self._maybe_build(inputs)
1099 cast_inputs = self._maybe_cast_inputs(inputs, input_list)
1100

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in _maybe_build(self, inputs)
2641 # operations.
2642 with tf_utils.maybe_init_scope(self):
→ 2643 self.build(input_shapes) # pylint:disable=not-callable
2644 # We must set also ensure that the layer is marked as built, and the build
2645 # shape is stored since user defined build functions may not be calling

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/utils/tf_utils.py in wrapper(instance, input_shape)
321 if input_shape is not None:
322 input_shape = convert_shapes(input_shape, to_tuples=True)
→ 323 output_shape = fn(instance, input_shape)
324 # Return shapes from fn as TensorShapes.
325 if output_shape is not None:

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/layers/merge.py in build(self, input_shape)
517 shape[axis] for shape in shape_set if shape[axis] is not None)
518 if len(unique_dims) > 1:
→ 519 raise ValueError(err_msg)
520
521 def _merge_function(self, inputs):

ValueError: A Concatenate layer requires inputs with matching shapes except for the concat axis. Got inputs shapes: [(None, 48, 64, 128), (None, 24, 32, 128)]

I dont know what is happening tbh. I have tried various changes but negative

My guess is that there is something wrong with your upsampling_block routine. Eg maybe you hardcoded the number of filters. Note that the error occurs the first time you actually use the output of that function.