Image segmentation Unet_v2

In unet decoding what should be mentioned in contractive block output. I’m kind of confused. Please help me to find the error


UnboundLocalError 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 # Note that you must use the second element of the contractive block i.e before the maxpooling layer.
37 # At each step, use half the number of filters of the previous block
—> 38 ublock7 = upsampling_block(ublock6[0], ublock7[0], n_filters * 4)
39 ublock8 = upsampling_block(ublock7[0], ublock8[0], n_filters * 2)
40 ublock9 = upsampling_block(ublock8[0], ublock9[0], n_filters)

UnboundLocalError: local variable ‘ublock7’ referenced before assignment

inputs = Input(input_size)
# Contracting Path (encoding)
# Add a conv_block with the inputs of the unet_ model and n_filters
### START CODE HERE
cblock1 = conv_block(inputs, n_filters)
# Chain the first element of the output of each block to be the input of the next conv_block.
# Double the number of filters at each new step
cblock2 = conv_block(cblock1[0], n_filters * 2)
cblock3 = conv_block(cblock2[0], n_filters * 4)
cblock4 = conv_block(cblock3[0], n_filters * 8, dropout_prob= 0.3) # Include a dropout of 0.3 for this layer
# Include a dropout of 0.3 for this layer, and avoid the max_pooling layer
cblock5 = conv_block(cblock4[0], n_filters * 16, dropout_prob= 0.3, max_pooling=False)

### END CODE HERE

# Expanding Path (decoding)
# Add the first upsampling_block.
# Use the cblock5[0] as expansive_input and cblock4[1] as contractive_input and n_filters * 8
### START CODE HERE
ublock6 = upsampling_block(cblock5[0], cblock4[1], n_filters * 8)
# Chain the output of the previous block as expansive_input and the corresponding contractive block output.
# Note that you must use the second element of the contractive block i.e before the maxpooling layer. 
# At each step, use half the number of filters of the previous block 
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)
### END CODE 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
### START CODE HERE
conv10 = Conv2D(n_classes, kernel_size = 1, padding='same')(conv9)
### END CODE HERE

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 # Note that you must use the second element of the contractive block i.e before the maxpooling layer.
37 # At each step, use half the number of filters of the previous block
—> 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)

in upsampling_block(expansive_input, contractive_input, n_filters)
17 kernel_size = (3,3), # Kernel size
18 strides=(2, 2),
—> 19 padding=‘same’)(expansive_input)
20
21 # 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_2 is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [12, 16, 256]

This is the error now im getting

In the ublock arguments:

  • you do not need the [0] addressing for the previous ublock.
  • you do need [1] addressing for the cblocks.
2 Likes