Wrong shapes in yolo_filter_boxes

I am struck here in getting yolo_filter_boxes right. The unit tests fails with the wrong shapes

(1789, 80)
scores[2] = 9.270486
boxes[2] = [ -6.947019 0.06495993 -0.09231107 1.9229256 -8.933988
4.266996 -2.5233626 -1.0357457 9.270486 -6.093267
-14.030916 -4.884537 5.704059 -4.1289454 -6.1082263
1.7578557 4.355549 -6.43128 -2.4484224 3.2742531
-1.786569 -4.6719904 -3.7101617 2.0244038 -0.21229683
0.7752132 4.1586823 -3.4750829 5.502074 0.05799035
3.4069664 -3.757799 4.006846 -2.3703334 -3.342284
-4.293213 -2.5573728 -1.8982053 -1.1549497 -0.8022735
-1.8169211 1.3455609 -6.429533 -2.5119228 2.8919446
-5.360439 -1.5676502 -4.3315964 -6.0917664 -4.5209737
-2.929725 -1.7455465 -3.1875036 1.1328714 -1.5746784
0.0923316 1.9777262 0.86132324 7.4806495 2.619351
-1.4033728 -1.739711 0.254833 -0.38542864 -2.9211323
7.1019955 -6.438033 3.5228815 -2.1012435 -4.932968
-2.1174386 4.462003 -4.8623567 -1.3148922 -1.9493929
-4.421925 3.70371 -0.9842584 3.7049224 1.8417823 ]
classes[2] = 8
scores.shape = (1789,)
boxes.shape = (1789, 80)
classes.shape = (1789,)

AssertionError Traceback (most recent call last)
in
17
18 assert scores.shape == (1789,), “Wrong shape in scores”
—> 19 assert boxes.shape == (1789, 4), “Wrong shape in boxes”
20 assert classes.shape == (1789,), “Wrong shape in classes”
21

AssertionError: Wrong shape in boxes

def yolo_filter_boxes(boxes, box_confidence, box_class_probs, threshold = .5):
Here the boxes dimension is [19, 19, 5, 4]
box_confidence: [19, 19, 5, 1]
box_class_probs : [19, 19, 5, 80]

box_scores is computed as follows

box_scores = box_confidence * box_class_probs

This yields box_scores with a dimension of [19,19,5,80]

filtering_mask is [19,19,5]
boxes = tf.boolean_mask(box_scores,filtering_mask)

Here are the shapes I see from the yolo_filter_boxes test cell:

boxes.shape (19, 19, 5, 4)
boxes.dtype <dtype: 'float32'>
box_scores.shape (19, 19, 5, 80)
box_scores.dtype <dtype: 'float32'>
box_classes.shape (19, 19, 5)
box_classes.dtype <dtype: 'int64'>
box_class_scores.shape (19, 19, 5)
box_class_scores.dtype <dtype: 'float32'>
filtering_mask.shape (19, 19, 5)
filtering_mask.dtype <dtype: 'bool'>
scores[2] = 9.270486
boxes[2] = [ 4.6399336  3.2303846  4.431282  -2.202031 ]
classes[2] = 8
scores.shape = (1789,)
boxes.shape = (1789, 4)
classes.shape = (1789,)
 All tests passed!

It looks like all the shapes you show are correct other than the final output from the filtering operation. My theory is that means the problem is that the contents of your filter mask are what is incorrect, which probably means that the contents of box_class_scores are incorrect. You ended up with 80 entries selected instead of 4. So how could that happen?

1 Like

box_scores got through by doing element wise multiplication with box_confidence and box_class_probs

box_scores.shape = 19, 19, 5, 80

and then reduce the box_score to obtain maximum score
box_class_scores = tf.math.reduce_max(box_scores,axis = -1)

box_class_scores.shape = 19,19,5

filtering_mask = box_class_scores >= threshold
filtering_mask.shape = 19,19,5

threshold is set to 0.5

The problem is that your input to the final computation of boxes is wrong: you’re not supposed to be filtering against box_scores, but against the original boxes, which is 19 x 19 x 5 x 4, instead of 19 x 19 x 5 x 80.

1 Like