Getting error in yolo_non_max_suppression function

I am getting an error in tf.gather(nms_indices_label,selected_indices)
InvalidArgumentError: indices[0] = 1 is not in [0, 1) [Op:GatherV2]
what can be the possible reason?

boxes and scores contain information across all classes. Since we’re performing non-max suppression per label (see here to understand the motivation), the for loop should use boxes and scores for a single label during each pass. Please read the markdown again and look closely at the loop specific variables for invoking non_max_suppression correctly.

As far as tf.gather is concerned, see this as well:

# Append the resulting boxes into the partial result
# Use tf.gather() with 'selected_indices' and `nms_indices_label` 

Posting solution code in a public topic goes against our Community Forum Code of Conduct. It’s okay to share stacktrace on a public post and send code to a mentor via direct message.

Please edit your post.

Here’s a guide on how to edit a post.

2 Likes

Hi,

I am having the same problem and trying to resolve it for past 24 hours.

You mentioned

the for loop should use boxes and scores for a single label during each pass.

How to select boxes and scores for single label? Since, I am new to TF syntax.

They already wrote the logic for you to select one label in each iteration of the loop. Here’s is the first few lines of the template code:

    boxes = tf.cast(boxes, dtype=tf.float32)
    scores = tf.cast(scores, dtype=tf.float32)

    nms_indices = []
    classes_labels = tf.unique(classes)[0] # Get unique classes
    
    for label in classes_labels:
        filtering_mask = classes == label
    
    #### START CODE HERE

So within the loop, the variable label is the current single label value that you are working with and filtering_mask is boolean mask tensor which will be True in the positions that match the current label. Then they give you a pretty big hint in the template code about which TF function to use:

        # Get boxes for this class
        # Use tf.boolean_mask() with 'boxes' and `filtering_mask`
        boxes_label = None
        
        # Get scores for this class
        # Use tf.boolean_mask() with 'scores' and `filtering_mask`
        scores_label = None

Have you read the documentation for TF boolean_mask and gather?

1 Like

Hi, I have the same error
InvalidArgumentError: indices[0] = 1 is not in [0, 1) [Op:GatherV2]

After reading the solution here, I have adjusted the code to reflect the boxes_label and scores_label but will got the same issue. As i can’t put the code here, is there anyway that a mentor could help me please ?
Thanks so much

I have figured it out that I used the gather function wrongly. all good now thank you

1 Like

Glad to hear you found the error! Yes, it sounds like you probably reversed the arguments in the “gather” call.

---------------------------------------------------------------------------
*InvalidArgumentError                      Traceback (most recent call last)*
*<ipython-input-12-11f8efabc036> in <module>*
*      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()}"*

*<ipython-input-11-cff628b38fb4> in yolo_non_max_suppression(scores, boxes, classes, max_boxes, iou_threshold)*
*     51             # Append the resulting boxes into the partial result*
*     52             # Use tf.gather() with 'selected_indices' and `nms_indices_label`*
*---> 53             nms_indices.append(tf.gather(selected_indices,nms_indices_label,validate_indices=selected_indices, axis=None, batch_dims=0, name=None))*
*     54 *
*     55     # Flatten the list of indices and concatenate*

*/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 gather_v2(params, indices, validate_indices, axis, batch_dims, name)*
*   4693       name=name,*
*   4694       axis=axis,*
*-> 4695       batch_dims=batch_dims)*
*   4696 *
*   4697 *

*/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 gather(***failed resolving arguments***)*
*   4676     return params.sparse_read(indices, name=name)*
*   4677   except AttributeError:*
*-> 4678     return gen_array_ops.gather_v2(params, indices, axis, name=name)*
*   4679 *
*   4680 *

*/opt/conda/lib/python3.7/site-packages/tensorflow/python/ops/gen_array_ops.py in gather_v2(params, indices, axis, batch_dims, name)*
*   3843       return _result*
*   3844     except _core._NotOkStatusException as e:*
*-> 3845       _ops.raise_from_not_ok_status(e, name)*
*   3846     except _core._FallbackException:*
*   3847       pass*

*/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/ops.py in raise_from_not_ok_status(e, name)*
*   6841   message = e.message + (" name: " + name if name is not None else "")*
*   6842   # pylint: disable=protected-access*
*-> 6843   six.raise_from(core._status_to_exception(e.code, message), None)*
*   6844   # pylint: enable=protected-access*
*   6845 *

*/opt/conda/lib/python3.7/site-packages/six.py in raise_from(value, from_value)*

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

I am getting this error. Can I get a mentor to help me solve this? I do not have much idea, on solving it. Please suggest a mentor or looking forward to a mentor.

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

3 Likes