Does not match the input value:

GRADED FUNCTION: conv_block

def conv_block(inputs=None, n_filters=32, dropout_prob=0, max_pooling=True):
“”"
Convolutional downsampling block

Arguments:
    inputs -- Input tensor
    n_filters -- Number of filters for the convolutional layers
    dropout_prob -- Dropout probability
    max_pooling -- Use MaxPooling2D to reduce the spatial dimensions of the output volume
Returns: 
    next_layer, skip_connection --  Next layer and skip connection outputs
"""
# conv = Conv2D(None, # Number of filters
#             None,   # Kernel size   
#             activation=None,
#             padding=None,
#             kernel_initializer=None)(inputs)
# conv = Conv2D(None, # Number of filters
#             None,   # Kernel size
#             activation=None,
#             padding=None,
#             kernel_initializer=None)(conv)
# YOUR CODE STARTS HERE

conv = Conv2D(32,3,activation='relu',padding='same',kernel_initializer='he_normal')(inputs)
conv = Conv2D(32,3,activation='relu',padding='same',kernel_initializer='he_normal')(conv)
# YOUR CODE ENDS HERE

# if dropout_prob > 0 add a dropout layer, with the variable dropout_prob as parameter
if dropout_prob > 0:
     # conv = None
     # YOUR CODE STARTS HERE
    
     
     # YOUR CODE ENDS HERE
     conv=Dropout(dropout_prob*0.5)(conv)
    
# if max_pooling is True add a MaxPooling2D with 2x2 pool_size
if max_pooling==True:
     
    
    # next_layer = None
    # YOUR CODE STARTS HERE
   
    next_layer =MaxPooling2D((2,2))(conv)
    # YOUR CODE ENDS HERE
    
else:
    next_layer = conv
    
skip_connection = conv

return next_layer, skip_connection

i have created this model but getting error in test so please help me to overcome this problem(Week 3 in CNN Program Assignments)

Hi shivraj,

Try to see where you have hard-coded parameter values, and replace them with parameter names.

Good luck!

1 Like

Hi reinoudbosch, yes issue resolved and in this assignment i strucked with onother lines of code which i will mention now so request you to help me in this

GRADED FUNCTION: unet_model

def unet_model(input_size=(96, 128, 3), n_filters=32, n_classes=23):

"""
Unet model

Arguments:
    input_size -- Input shape 
    n_filters -- Number of filters for the convolutional layers
    n_classes -- Number of output classes
Returns: 
    model -- tf.keras.Model
"""
inputs =Input(input_size)


# Contracting Path (encoding)
# Add a conv_block with the inputs of the unet_ model and n_filters
# cblock1 = conv_block(None, None)
# 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(None, None)
# cblock3 = conv_block(None, None)
# cblock4 = conv_block(None, None, dropout=None) # 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(None, None, dropout=None, max_pooling=None) 
# YOUR CODE STARTS HERE

cblock1 = conv_block(inputs, n_filters * 1)
cblock2 = conv_block(cblock1,n_filters*2)
cblock3 = conv_block(cblock2,n_filters*4)
cblock4 = conv_block(cblock3,n_filters*8,dropout_prob=0.3)
cblock5 = conv_block(cblock4,n_filters*16,dropout_prob=0.3,max_pooling=False)

# YOUR CODE ENDS 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
# ublock6 = upsampling_block(None, None,  None)

# 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(None, None,  None)
# ublock8 = upsampling_block(None, None,  None)
# ublock9 = upsampling_block(None, None,  None)
# YOUR CODE STARTS HERE
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

these line of code i have written but not output its showing the following error
ValueError: Layer conv2d_8 expects 1 inputs, but it received 2 input tensors. Inputs received: [<tf.Tensor ‘max_pooling2d_2/MaxPool:0’ shape=(None, 48, 64, 32) dtype=float32>, <tf.Tensor ‘conv2d_7/Relu:0’ shape=(None, 96, 128, 32) dtype=float32>]

I’ve got the same issue, you need to remember that conv_block get two parameters and one of them is the skip connection.
To solve it, in the cblock you need to refer to the first element.
cblock2 = conv_block(cblock1[0],n_filters*2)

2 Likes

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]

Hi dhanyashree,

Take a good look. What does the expansive_input of the upsampling_block for ublock7, 8, and 9 consist of?

2 Likes

i figured out point that we have to provide whole ublock like
ublock6 = upsampling_block(cblock5[0], cblock4[1], n_filters8)
ublock7 = upsampling_block(ublock6, cblock3[1], n_filters
4)
ublock8 = upsampling_block(ublock7, cblock2[1], n_filters2)
ublock9 = upsampling_block(ublock8, cblock1[1], n_filters
1)

its showing error :Expected value

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

does not match the input value:

[‘Conv2D’, (None, 12, 16, 32), 73760, ‘same’, ‘relu’, ‘HeNormal’]


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

~/work/W3A2/test_utils.py in comparator(learner, instructor)
17 “\n\n does not match the input value: \n\n”,
18 colored(f"{a}", “red”))
—> 19 raise AssertionError(“Error in test”)
20 print(colored(“All tests passed!”, “green”))
21

AssertionError: Error in test

hey,

if the code you have written here is exactly the one you are running in the jupyter notebook, i suppose it is the error in the value of filters. It seems like you have written n_filter8 instead of n_filters*8. I am guessing this is the problem.

If not, I believe it would give us opportunity to give more input if you are able to present here the entire code along with the error.

Hope this helps.

This is a very useful comment! The “first” word confused me. So it should be [0] instead of [1]

@reinoudbosch that definitely helped me, **the key for solving that is:
WHAT DOES THE ublock and convblok OUTPUT look like?

They do not output the samething but we are treating the output of those functions in the same way! We are setting expansive_input and contractive_input the same way. That’s the mistake.