Course4,week3,ass2(u-net)

Hi,

I have been stuck in unet_model function. I have already read all the related topics but could not find an answer. Would really appreciate if I get some help.

conv_block and unsampling block have already passed the tests and this is how I write the unet_model.

cblock1 is the output of conv_block for inputs and n_filters, and up to cblock5 I feed cblock(previous)[0] and double the n_filter to conv_block. ublock6 is also done as explained. Rest of the ublocks are obtained using ublock(previous) and cblock(corresponding)[1] and half of the n-filters. ex: ublock7 = upsampling_block(ublock6, cblock3[1], n_filters * 4)

I get this error on ublock6:

Could you help me figure out where I’m going wrong?
Thanks

Did you set the maxpooling for cblock5 as hinted in code comments?

1 Like

yes, dropout_prob=0.3, max_pooling=‘False’ for cblock5 and dropout_prob=0.3 for cblock4.

Hi,

Could you help me here please. I have been trying to solve it since yesterday and stuck on this assignment. @paulinpaloalto

Hey, just solved it. :slight_smile:
@paulinpaloalto

1 Like

It’s great to hear that you found the solution under your own power! Thanks for confirming.

I am getting the same error…but I am not able understand it… help please!

Please show us the actual output you are getting: not just the error message, the complete exception trace.

Ok, that does look like the same error. The solution was hinted at earlier on this thread. Your code for ublock6 looks correct, but one of the inputs is the wrong shape. Notice that the first one has h and w dimensions half of the second one. Did you perhaps miss the point that cblock5 is different than all the other contracting blocks in that you were not supposed to apply max pooling? The instructions in the comments in the template code were pretty clear on that point.

I have mentioned cblock5 = conv_block(cblock4[0], n_filters*16, dropout_prob=0.3, max_pooling=‘False’)

That is the wrong syntax for specifying the max_pooling value: it is a Boolean, but you’ve given it a python String value. Since the value is not zero, it ends up being the equivalent of True when you coerce it to a Boolean.

1 Like

ohhh…Gotcha… Thanks!!!

Thank you so much, it seems silly but I was stuck on the same problem for a long time too.

Hi Naeimeh.A, I followed carefully the instructions and still get the following: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)
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, n_filters2)
22 cblock3 = conv_block(cblock2, n_filters
4)
23 cblock4 = conv_block(cblock3, n_filters*8, dropout=0.3) # Include a dropout_prob of 0.3 for this layer

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_43 expects 1 inputs, but it received 2 input tensors. Inputs received: [<tf.Tensor ‘max_pooling2d_13/MaxPool:0’ shape=(None, 48, 64, 32) dtype=float32>, <tf.Tensor ‘conv2d_42/Relu:0’ shape=(None, 96, 128, 32) dtype=float32>]

Maybe you can direct me to my fault? thanks

1 Like

Hi, did you solve your problem? I’m stuck in the same problem rigth now, and didn’t find any tread to help me with.

What is “the same problem”? Please show us the error output you are getting. Not the code, please, just the output.

It would also probably be better in general to start new thread when you have a problem. This thread hasn’t had any activity in almost 2 years, so it’s just lucky that I happen to be “following” it …

1 Like

Hi, @paulinpaloalto. Thanks for quick response. Sorry, I didn’t notice that this tread was so old. I’ll open another tread next time. So, this is my error:


ValueError Traceback (most recent call last)
Input In [19], in <cell line: 6>()
3 img_width = 128
4 num_channels = 3
----> 6 unet = unet_model((img_height, img_width, num_channels))
7 comparator(summary(unet), outputs.unet_model_output)

Input In [18], in unet_model(input_size, n_filters, n_classes)
18 cblock1 = conv_block(inputs, n_filters)
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, n_filters2)
22 cblock3 = conv_block(cblock2, n_filters
4)
23 cblock4 = conv_block(cblock3, n_filters*8, dropout=0.3) # Include a dropout_prob of 0.3 for this layer

Input In [8], in conv_block(inputs, n_filters, dropout_prob, max_pooling)
4 “”"
5 Convolutional downsampling block
6
(…)
13 next_layer, skip_connection – Next layer and skip connection outputs
14 “”"
16 ### START CODE HERE
—> 17 conv = Conv2D(n_filters, # Number of filters
18 3, # Kernel size
19 activation=“relu”,
20 padding=“same”,
21 kernel_initializer=‘he_normal’)(inputs)
22 conv = Conv2D(n_filters, # Number of filters
23 3, # Kernel size
24 activation=“relu”,
25 padding=“same”,
26 # set ‘kernel_initializer’ same as above
27 kernel_initializer=‘he_normal’)(conv)
28 ### END CODE HERE
29
30 # if dropout_prob > 0 add a dropout layer, with the variable dropout_prob as parameter

File /usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py:67, in filter_traceback..error_handler(*args, **kwargs)
65 except Exception as e: # pylint: disable=broad-except
66 filtered_tb = _process_traceback_frames(e.traceback)
—> 67 raise e.with_traceback(filtered_tb) from None
68 finally:
69 del filtered_tb

File /usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py:200, in assert_input_compatibility(input_spec, inputs, layer_name)
197 raise TypeError(f’Inputs to a layer should be tensors. Got: {x}‘)
199 if len(inputs) != len(input_spec):
→ 200 raise ValueError(f’Layer “{layer_name}” expects {len(input_spec)} input(s),’
201 f’ but it received {len(inputs)} input tensors. ’
202 f’Inputs received: {inputs}')
203 for input_index, (x, spec) in enumerate(zip(inputs, input_spec)):
204 if spec is None:

ValueError: Layer “conv2d_17” expects 1 input(s), but it received 2 input tensors. Inputs received: [<tf.Tensor ‘Placeholder:0’ shape=(None, 48, 64, 32) dtype=float32>, <tf.Tensor ‘Placeholder_1:0’ shape=(None, 96, 128, 32) dtype=float32>]

Notice that the bug occurs on your second call to conv_block and you’re passing the output (return value) of the first call verbatim as the first argument. What is the return value of the conv_block function? Have a look at the definition: it returns two values, right? Which one do you want to pass?