Course 4 Week 3 A1 - Shape incompatible for last test, with all earlier test passed

Hi,
I am currently “stuck” C4W3A1.

I have passed all the test for the “GRADED FUNCTION”, I have submit my assignment and get full marks. But when I run the last test with function predict(), it give error “ValueError: Shapes (1, 19, 19, 80) and (1, 19, 19, 5) are incompatible”.

The detailed error message is as following:

out_scores, out_boxes, out_classes = predict("test.jpg")

    ---------------------------------------------------------------------------
    
    ValueError                                Traceback (most recent call last)
    
    <ipython-input-15-43edc87fb9a8> in <module>
    ----> 1 out_scores, out_boxes, out_classes = predict("test.jpg")


    <ipython-input-14-85846e4b09d5> in predict(image_file)
         20     yolo_outputs = yolo_head(yolo_model_outputs, anchors, len(class_names))
         21 
    ---> 22     out_scores, out_boxes, out_classes = yolo_eval(yolo_outputs, [image.size[1],  image.size[0]], 10, 0.3, 0.5)
         23 
         24     # Print predictions info


    <ipython-input-9-d99906cf3e34> in yolo_eval(yolo_outputs, image_shape, max_boxes, score_threshold, iou_threshold)
         47 
         48     # Use one of the functions you've implemented to perform Score-filtering with a threshold of score_threshold (≈1 line)
    ---> 49     scores, boxes, classes = yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold = score_threshold)
         50 
         51     # Scale boxes back to original image shape.


    <ipython-input-2-0e90b3b3e798> in yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold)
         61     scores  = tf.boolean_mask(box_class_scores, filtering_mask)
         62     boxes   = tf.boolean_mask(boxes, filtering_mask)
    ---> 63     classes = tf.boolean_mask(box_classes, filtering_mask)
         64 
         65     # YOUR CODE ENDS HERE


    /opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
        199     """Call target, and fall back on dispatchers if there is a TypeError."""
        200     try:
    --> 201       return target(*args, **kwargs)
        202     except (TypeError, ValueError):
        203       # Note: convert_to_eager_tensor currently raises a ValueError, not a


    /opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py in boolean_mask_v2(tensor, mask, axis, name)
       1801   ```
       1802   """
    -> 1803   return boolean_mask(tensor, mask, name, axis)
       1804 
       1805 


    /opt/conda/lib/python3.7/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs)
        199     """Call target, and fall back on dispatchers if there is a TypeError."""
        200     try:
    --> 201       return target(*args, **kwargs)
        202     except (TypeError, ValueError):
        203       # Note: convert_to_eager_tensor currently raises a ValueError, not a


    /opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py in boolean_mask(tensor, mask, name, axis)
       1728     if axis_value is not None:
       1729       axis = axis_value
    -> 1730       shape_tensor[axis:axis + ndims_mask].assert_is_compatible_with(shape_mask)
       1731 
       1732     leading_size = gen_math_ops.prod(shape(tensor)[axis:axis + ndims_mask], [0])


    /opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/tensor_shape.py in assert_is_compatible_with(self, other)
       1132     """
       1133     if not self.is_compatible_with(other):
    -> 1134       raise ValueError("Shapes %s and %s are incompatible" % (self, other))
       1135 
       1136   def most_specific_compatible_shape(self, other):


    ValueError: Shapes (1, 19, 19, 80) and (1, 19, 19, 5) are incompatible

According to the error message, it seems to me that the error is occurring at LN63 of yolo_filter_boxes() where the filterting_mask is applied to the classes. But I am not sure the two shapes (1,19,19,80) and (1,19,19,5) refer to which vector.

And what puzzles me is that the module test for that particular function is all passed. When it is invoked in yolo_eval(), the test is also running fine.

I have no clue why this is happening. Any informatoin/suggestion/insight are welcome.

Thanks.

The grader always tests your functions with a totally different set of data. It might be a different size and shape than was used in the exercise test cases.

So start by verifying that your code doesn’t use any hard-coded values incorrectly.

Thanks for your advice.

I have double checked my code, there is no hard-coded values in all the function to be graded.

The problem I am facing now is:

  • All the function test in the notebook Section 1 & 2 passed with no issues.
  • The submitted assignment get full marks through the grader.
  • When I try to run the test in Section 3 Test YOLO Pre-trained Model on Images, the described issue appears.

But there is no new user-implemented code in Section 3, all user-implemented code used in Section 3 are from earlier graded functions. My code passes the grader test with no problem.

This really get me confused.

That confuses me also.
Perhaps another mentor or someone from the community can explain it.

I had the same issue. I solved it by noticing that earlier, in the yolo_filter_boxes function, when computing box_classes and box_classes_scores I hard coded axis=3 as the axis with respect to compute the argmax/max.
It turned out that the axis containing the scores is not always the third one but, more generally, it is the last one. By changing axis=3 into something else (hint: how can we refer to the last element of a list in python?) I solved my problem with the predict("test.jpg") call.

12 Likes

@pietro, thanks for your reply.

Hi, pietro
It is working fine after the correction.

I did test out axis=3 and axis=-1 when doing yolo_filter_box. Then I left the 3 there and proceeded with other assignment, did not register that in mind.

Thanks a lot for the reply.

2 Likes

I had the same problem, solved it, but still don’t know why it did not worked.

I was using the “>” sign to create the filtering_mask. It outputs a (1, 19, 19, 5) mask (incorrect). Then I changed the code to use the “greater” function of tf.math library and it worked… it outputs a correct (19,19,5) mask, as it should be.

Could someone explain why it does not work using the > sign??

Thanks bro
excellent solution!

I had the same issue. Thank you @pietro and folks for dealing with this, you saved me a lot of grief!