Course 4 week 3 Unet_model

my unet_model is as follows:
def unet_model(input_size=(96, 128, 3), n_filters=32, n_classes=23):
inputs = Input(input_size)
cblock1 = conv_block(inputs, n_filters)
cblock2 = conv_block(cblock1[0], n_filters2)
cblock3 = conv_block(cblock2[0], n_filters
4)
cblock4 = conv_block(cblock3[0], n_filters8, dropout_prob=0.3)
cblock5 = conv_block(cblock4[0], n_filters
16, dropout_prob=0.3, max_pooling=False)

ublock6 = upsampling_block(cblock5[0], cblock4[1],  n_filters * 8)
ublock7 = upsampling_block(ublock6[:,:,:, :int(ublock6.shape[-1]/2)], cblock3[1],  n_filters * 4)
ublock8 = upsampling_block(ublock7[:,:,:, :int(ublock7.shape[-1]/2)], cblock2[1],  n_filters * 2)
ublock9 = upsampling_block(ublock8[:,:,:, :int(ublock8.shape[-1]/2)], cblock1[1],  n_filters)

conv9 = Conv2D(n_filters,3,activation='relu',padding='same',kernel_initializer='HeNormal')(ublock9)

conv10 = Conv2D(n_classes, 1, padding='same')(conv9)

model = tf.keras.Model(inputs=inputs, outputs=conv10)

return model

And the error is
Test failed
Expected value

[‘Conv2DTranspose’, (None, 24, 32, 128), 295040]

does not match the input value:

[‘TensorFlowOpLayer’, [(None, 12, 16, 128)], 0]

I don’t know how to modified my fun to make it correct. thank you in advance

I want to guess this error is for the concatenation layer. If so, check all dimensions in the encoding part, to see that they match the U-net diagram. Also, check the skip connection dimensions to see if they match their destination on the decoding side. There is an extra divide by 2 in your model you need to track down

I removed those division by 2 things, and it works well now. Thank you for your reply.

@shivraj I’m pretty sure that asking for the solution would violate the course Honor Code

Yeah I apologizes for the inconvenience caused and so I have deleted all the posts and will not repeat the same again
This time it happened because of my mistake
Thanks and Regards
Shivaraj Kumara H C

Really sorry from my end while solving the assignments i was tensed so posted but now onwards will not post sorry for the inconvenience caused from my side.

im getting error as follows :Input 0 of layer conv2d_transpose_117 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [12, 16, 256]
could someone please explain whats gone wrong here ? line:ublock 7

ublock6 = upsampling_block(cblock5[0], cblock4[1],n_filters * 8)
ublock7 = upsampling_block(ublock6[0], cblock3[1],n_filters * 4)
ublock8 = upsampling_block(ublock7[0], cblock2[1],n_filters * 2)
ublock9 = upsampling_block(ublock8[0], cblock1[1],n_filters * 1)

YOUR CODE ENDS HERE*

conv9 = Conv2D(n_filters,
         3,
         activation='relu',
         padding='same',
         kernel_initializer='he_normal')(ublock9)

Add a Conv2D layer with n_classes filter, kernel size of 1 and a ‘same’ padding

conv10 = Conv2D(None, None, padding=None)(conv9)

YOUR CODE STARTS HERE

conv10 = Conv2D(n_classes ,kernel_size=1, padding='same')(conv9)

YOUR CODE ENDS HERE

model = tf.keras.Model(inputs=inputs, outputs=conv10)

return model

complete 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)
36 # YOUR CODE STARTS HERE
37 ublock6 = upsampling_block(cblock5[0], cblock4[1],n_filters * 8)
—> 38 ublock7 = upsampling_block(ublock6[0], cblock3[1],n_filters * 4)
39 ublock8 = upsampling_block(ublock7[0], cblock2[1],n_filters * 2)
40 ublock9 = upsampling_block(ublock8[0], cblock1[1],n_filters * 1)

in upsampling_block(expansive_input, contractive_input, n_filters)
18 kernel_size=3, # Kernel size
19 strides=2,
—> 20 padding=‘same’)(expansive_input)
21
22 # Merge the previous output and the contractive_input

/opt/conda/lib/python3.7/site-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.

/opt/conda/lib/python3.7/site-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.

/opt/conda/lib/python3.7/site-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_117 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [12, 16, 256]

1 Like

Hey,

Please note that you have to concatenate the entire output from the encoder block

Reference from the jupyter notebook:
" For the second half:
Chain the output of the previous block as expansive_input and the corresponding contractive block output."

So instead of using ublock6[0], you have to use ublock6 i.e. the entire output.

Hope this helps.

10 Likes

heya ! i figured out what you told and solved that part but Im getting error Does not match the input value: - #8 by dhanyashree
could you please check this out

Thank you so much ! This helped my code run perfectly!

Thank you, I modified the parameter from ubblock6[0] to ubblock6, solved.