W3A1 YOLO assignment boolean masking

Hi there,

I am trying to use the filtering mask with tf.boolean_mask in the first coding window of week 3 assingment 1 . When I filter the box class scores, everything works fine. since the dimensions of box class scores and the filtering mask are the same. However, in trying to filter the boxes, I get Shapes (19, 19, 5, 4) and (19, 19, 5, 1) are incompatible. And obviously, a similar output for box classes. This doesn’t make sense to me, since I would assume that tf would “broadcast” the mask. Either way, I don’t really understand how to continue. Is there something I am missing?

Kind Regards,
James

1 Like

Have you used axis = -1 in box_classes and box_class_scores? Please share your full error.

Hi, no I used axis = None. I have pasted the error below. Please let me know if there is anything else you need.


ValueError Traceback (most recent call last)
in
4 boxes = tf.random.normal([19, 19, 5, 4], mean=1, stddev=4, seed = 1)
5 box_class_probs = tf.random.normal([19, 19, 5, 80], mean=1, stddev=4, seed = 1)
----> 6 scores, boxes, classes = yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold = 0.5)
7 print("scores[2] = " + str(scores[2].numpy()))
8 print("boxes[2] = " + str(boxes[2].numpy()))

in yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold)
41 ## (≈ 3 lines)
42 scores = tf.boolean_mask(box_class_scores, filtering_mask)
—> 43 boxes = tf.boolean_mask(boxes, filtering_mask, axis=None)
44 classes = tf.boolean_mask(box_classes, filtering_mask)
45 ### END CODE 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 (19, 19, 5, 4) and (19, 19, 5, 1) are incompatible

You have to use axis = - 1 for both, tf.math.argmax and tf.math.reduce_max.

I already do use axis = -1 for both argmax and reduce_max.

Sorry, I see my previous response was unclear. I use axis = -1 when obtaining box_classes and box_class_scores with argmax and reduce_max, I use axis = None when calling tf.boolean_mask

OK. I put the below two lines of code after filtering_mask (before the scores):

print(f"boxes shape: {boxes. Shape}")
print(f"filtering_mask shape: {filtering_mask.shape}")    

Then I re-ran that cell and the one below it. My output is:

boxes shape: (19, 19, 5, 4)
filtering_mask shape: (19, 19, 5)

What’s yours? If it’s not same as mine, then double-check your implementation of the step 2:

box_classes = None
box_class_scores = None

Ok I found my issue. I had keepdims set to true, so the dimensions of box_class_scores was (19, 19, 5, 1) instead of (19, 19, 5), which also caused my mask to be (19, 19 5, 1). Thank you!