Getting error in yolo_non_max_suppression function

Hello, @Kirthana,

It is learner’s job to solve any problems in their works, but we can explain the error to you and give you some idea.

There are three parts in the full traceback: (1) code in your notebook (2) 3rd party library’s code - here mostly tensorflow’s (3) the error message

We should first read (3), then (1). For your case, (3) tells us the error has to do with Op:GatherV2 and from (1) we know we have used tf.gather, so the error must be around it. So, how do we understand the rest of (3)?

If you run the following code, you will see the same kind of error:

params = tf.constant(['p0', 'p1', 'p2', 'p3', 'p4', 'p5'])

tf.gather(params, [13, ]).numpy()
InvalidArgumentError: indices[0] = 13 is not in [0, 6) [Op:GatherV2]

this is obviously problematic because params only has 6 elements but we are trying to gather the element number 13 from it. In other words, there is no element number 13. Therefore, we can read my error message like this: indices[0] = 13 means we are asking for the element number 13, and is not in [0, 6) means params only has 6 elements so we were expected to only ask for either element number 0, number 1, number 2, number 3, number 4 or number 5.

[0, 6) means “from 0 to 5”.

Now I think you can understand the error message you shared:

***InvalidArgumentError: indices[1] = 1 is not in [0, 1) [Op:GatherV2]***

Your code asked for element number 1 but the tensor has only element number 0 and it does not have element number 1.

[0, 1) means “from 0 to 0”.

Now, I can give you some ideas of how you can debug the error yourself:

The following part of your message indicates that you had failed the first test:

*      8 print(f"iou:    \t{iou(boxes[0], boxes[1])}")*
*      9 *
*---> 10 scores2, boxes2, classes2 = yolo_non_max_suppression(scores, boxes, classes, iou_threshold = 0.9)*
*     11 *
*     12 assert np.allclose(scores2.numpy(), [0.855, 0.828]), f"Wrong value on scores {scores2.numpy()}"*

The test supplies the following inputs to the function:

image

Now, if we read these inputs carefully, we notice that there are only two labels and they belong to two different classes. In other words, if we understand what non-max suppression is for, we should expect none of them to be suppressed.

Okay, so we can move on to the code:

I have added some tf.print so that I can keep track of what those variables receive. I recommend you to do the same but please remember to remove them after debugging to prevent your submission from failing.

Now, you need to fill in the blanks and if you fully understand what the filled code is supposed to do, then you will expect the following printouts in the first round of the for loop:

I am going to let you look into why these values are expected. You might need to read through the exercise’s description and the tensorflow documentation of those tensorflow functions used.

Now, I want to combine those values with the following line of your error message for how you use those values.

*---> 53             nms_indices.append(tf.gather(selected_indices,nms_indices_label,validate_indices=selected_indices, axis=None, batch_dims=0, name=None))*

If we replace selected_indices with [0] and nms_indices_label with another [0], this should not trigger the error you shared because we are gathering element number 0 of the tensor [0] which has element number 0. However, if nms_indices_label was [1], then that error should be triggered because then we would be asking for element number 1 from the tensor [0] which has only element number 0.

What you can do then is to add those tf.print and then inspect the printouts to see if any of them was off your expectation, then that should give you a lead and you can try to figure out why and fix it!

Good luck to you, @Kirthana.

Cheers,
Raymond

6 Likes