Skip to content

tf.image.combined_non_max_suppression drops detections scoring below -1 and returns equal scores in unspecified order #128926

Closed

Description

@teerthsharma

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

No. Reproduced on the 2.21.0 release wheel. non_max_suppression_op.cc is unchanged between 7b92f54 and master b5d39d2.

Source

binary

TensorFlow version

2.21.0

Custom code

Yes

OS platform and distribution

Windows 11

Python version

3.11.9

Current behavior?

tf.image.combined_non_max_suppression has two problems, both in SelectResultPerBatch / DoNMSPerClass in tensorflow/core/kernels/image/non_max_suppression_op.cc.

  1. The cross-class result vector is prefilled with padding (box_index -1, score -1.0, class -1) and then sorted by score only. A real detection scoring below -1.0 sorts after the padding, so padding takes its place in the top max_total_size.
  2. DoNMSPerClass pops equal scores in std::priority_queue heap order, which is unspecified, so equal-score boxes come back in a different order than tf.image.non_max_suppression returns them (lowest index first).

Standalone code to reproduce the issue

import numpy as np
import tensorflow as tf

# Box 2 duplicates box 0 and is suppressed. The two real detections score below -1.
boxes = tf.constant([[[[0, 0, .1, .1]], [[.2, .2, .3, .3]], [[0, 0, .1, .1]]]], tf.float32)
scores = tf.constant([[[-2.0], [-3.0], [-4.0]]], tf.float32)
b, s, c, v = tf.image.combined_non_max_suppression(
    boxes, scores, max_output_size_per_class=3, max_total_size=2,
    iou_threshold=0.5, score_threshold=-1e30)
print("scores ", s.numpy()[0], "classes", c.numpy()[0], "valid", v.numpy()[0])

# Eight disjoint boxes with equal scores, one class.
bx = np.array([[[[2 * i, 0, 2 * i + 1, 1]] for i in range(8)]], np.float32)
sc = np.full((1, 8, 1), 0.5, np.float32)
b, s, c, v = tf.image.combined_non_max_suppression(
    bx, sc, 8, 8, 0.5, 0.0, clip_boxes=False)
print("combined   ", [int(r[0] // 2) for r in b.numpy()[0]])
print("single-class", tf.image.non_max_suppression(bx[0, :, 0], sc[0, :, 0], 8, 0.5, 0.0).numpy().tolist())

Relevant log output

scores  [-1. -2.] classes [-1.  0.] valid 2      # expected scores [-2. -3.] classes [0. 0.]
combined    [0, 2, 6, 5, 7, 4, 1, 3]
single-class [0, 1, 2, 3, 4, 5, 6, 7]

I have a small patch (a total order in both comparators, plus three kernel tests) and will open a PR that references this issue.

Activity

  1. added
    comp:core
    2.21.0
    on Oct 12, 2026
  2. Kayyuri commented on Oct 12, 2026

    @Kayyuri

    Hi @teerthsharma, Thank you for reporting this issue!
    I see you have already taken the initiative and submitted a PR to fix this issue. Once PR is merged, the issue will be resolved.

    Thanks again for your patience and for contributing a fix to TensorFlow!

  3. google-ml-butler commented on Nov 12, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

  4. added a commit that references this issue on Nov 12, 2026
    8da2382
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.21.0awaiting PR mergecomp:coretype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions