# How can i prevent the model from predicting the same object twice?

**URL:** <https://discuss.roboflow.com/t/how-can-i-prevent-the-model-from-predicting-the-same-object-twice/492>\
**Category:** 🤝  Community Help\
**Created:** [May 25, 2022, 5:29pm UTC](https://discuss.roboflow.com/t/how-can-i-prevent-the-model-from-predicting-the-same-object-twice/492 "2022-05-25T17:29:29Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vshesh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/vshesh/32/332_2.png) [@vshesh](https://discuss.roboflow.com/u/vshesh)\
**Post date:** [May 25, 2022, 5:29pm UTC](https://discuss.roboflow.com/t/how-can-i-prevent-the-model-from-predicting-the-same-object-twice/492/1 "2022-05-25T17:29:29Z")

</div>

This category is for question related to accounts on [https://app.roboflow.com/](https://app.roboflow.com/)

Please share the following so we may better assist you:

1. Project type (Object detection)
2. The screenshot of the error being triggered in your browser’s developer tools/console.

 ![Screen Shot 2022-05-24 at 8.41.12 PM](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/b76a531d70b82a6589a1145ad9f8b509574f17d4.png)

I’m seeing a lot of “double predictions” (some correct, some incorrect) in my test set from the trained model. Is there a way to make the model not predict the same object twice? It’s seeing the same object with a smaller bounding box and a bigger bounding box.

One thing I will try is to space out the objects more (although I don’t understand why that would matter, since the bounding boxes in the annotations are tight around the object)

 ![Screen Shot 2022-05-24 at 8.45.42 PM](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/10f5bdaaf957b555dc981b1e7b6e8b0e4e4dbf65.png)  
those are the ground truth boxes

---

<div class="post-metadata">

**Author:** ![Mohamed](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/mohamed/32/62_2.png) [@Mohamed](https://discuss.roboflow.com/u/Mohamed)\
**Post date:** [May 26, 2022, 2:15pm UTC](https://discuss.roboflow.com/t/how-can-i-prevent-the-model-from-predicting-the-same-object-twice/492/2 "2022-05-26T14:15:32Z")

</div>

Hi, it isn’t an “error,” but rather from the model’s inference settings. You can update your `overlap` settings when running inference with the model.

For example, in the [Example Web App](https://docs.roboflow.com/inference/hosted-api#the-example-web-app), setting an `overlap` metric of 30% means that bounding boxes can only overlap up to 30% before they are “merged” and considered as a single box.

![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/61cadf2ba265fb5e91efefa4c5119345c55a3f7e.png)

---

<div class="post-metadata">

**Author:** ![vshesh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/vshesh/32/332_2.png) [@vshesh](https://discuss.roboflow.com/u/vshesh)\
**Post date:** [May 26, 2022, 3:08pm UTC](https://discuss.roboflow.com/t/how-can-i-prevent-the-model-from-predicting-the-same-object-twice/492/3 "2022-05-26T15:08:02Z")

</div>

Oh ok nice. Cool that I can change it to something else when I run detection.

After playing around with it for a bit, I’m finding the behavior of this setting perplexing. Would you help me understand what is going on in these cases?

### Case 1

 ![Screen Shot 2022-05-26 at 7.37.41 AM](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/b24553220616a6bd16e4cbb8b28009552d7df2dd.jpeg)

on the left there it feels like 100% overlap since the red box completely covers the blue one.

Through trial and error I found that 11% overlap and below makes this particular case of overlap go away and 12% and higher makes it show up. Why this is considered 11% overlap?  
(the only thing I could think of is that 11% is the overlap between the red large box and the second red box to it’s right)

* * *

Also when I did choose an overlap less than 11% I got:

 ![Screen Shot 2022-05-26 at 7.46.10 AM](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/c581db967e59a726ab8d11183d6c5beff70dc1da.png)  
(in this case the boxes are nearly axis aligned, so overlap zero is also working)

This is great! but how did the prediction change as a result of setting overlap differently? It’s now predicting a different size and shape of red box (notice the larger red box above)?

### Case 2

Now having learnt that 11% works for this one case, I tried a second at 11% and got this:

 ![Screen Shot 2022-05-26 at 8.00.13 AM](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/7c9be4aefcf95cbe1abc034d18cf365f02ee824d.jpeg)

At 11%, there are quite a few nearly overlapping cases still present. In the top center-right looks like low overlap removed the box around the circle with a bar in it rather than remove the overlapping boxes around the star with a dot in it.

Just to play around I decided to try 0% overlap .. and still got some doubled boxes:

 ![Screen Shot 2022-05-26 at 8.03.54 AM](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/bf26a382e867df218fe7c2e744ca9469d02f97e1.jpeg)

So now I’m confused… what happened here? I thought 0% overlap would remove the doubled boxes for sure.

* * *

Is there some way to set a different overlap when visualizing the dataset in the trained output? Then I could easily scan the images and make sure that overlap of 10-11% gets me the results I’m looking for.

I also wonder how precision/recall is calculated in this case and whether I could see a different precision/recall for this different overlap setting. That might also help me understand whether the setting improves the model or not…

Thanks for all your help, I’m just getting started with CV and I’m learning a lot from your answers.

---

<div class="post-metadata">

**Author:** ![vshesh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/vshesh/32/332_2.png) [@vshesh](https://discuss.roboflow.com/u/vshesh)\
**Post date:** [June 14, 2022, 4:10am UTC](https://discuss.roboflow.com/t/how-can-i-prevent-the-model-from-predicting-the-same-object-twice/492/4 "2022-06-14T04:10:29Z")

</div>

@Mohamed not sure if this is helpful… I was checking out how the yolov5 detector deals with this issue and they have a `--agnostic-nms` option that uses nms across classes.

> <https://github.com/ultralytics/yolov5/blob/master/detect.py#L227>

Is something like that what I need here? I noticed that the overlap removes predictions of the same class and leaves predictions of multiple classes alone … unless I’m just making that up hoping for a solution here.

---

<div class="post-metadata">

**Author:** ![Mohamed](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/mohamed/32/62_2.png) [@Mohamed](https://discuss.roboflow.com/u/Mohamed)\
**Post date:** [June 14, 2022, 5:55pm UTC](https://discuss.roboflow.com/t/how-can-i-prevent-the-model-from-predicting-the-same-object-twice/492/5 "2022-06-14T17:55:29Z")

</div>

Yes, that looks correct! Nms is the value of interest.

There’s more on it in our [Glossary from the Knowledge Base](https://help.roboflow.com/1339576-roboflow1-glossary)

 ![image](https://canada1.discourse-cdn.com/flex029/uploads/roboflow1/original/1X/c31592f45e815045ba316ad4ee9f9cf57b5ef5f2.png)

> **[Model Training: YOLOv5 Video Tutorial](https://help.roboflow.com/model-training-yolov5-video-tutorial)**
>
> Train a Custom YOLOv5 Model After generating a version of your labeled object detection dataset, we are ready to train a model. Here is a tutorial on train

> **[Run YOLOv5 on Your Webcam](https://help.roboflow.com/guides/run-yolov5-on-your-webcam)**
>
> Local Inference with YOLOv5 on a Webcam Clone the Ultralytics YOLOv5 Github repository to your computer and run detect.py after saving the model weights fr

---

<div class="post-metadata">

**Author:** ![vshesh](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/vshesh/32/332_2.png) [@vshesh](https://discuss.roboflow.com/u/vshesh)\
**Post date:** [June 14, 2022, 6:14pm UTC](https://discuss.roboflow.com/t/how-can-i-prevent-the-model-from-predicting-the-same-object-twice/492/6 "2022-06-14T18:14:49Z")

</div>

Hey @Mohamed

Now how do I set that option with the roboflow inference tool? I don’t see an “agnostic” option anywhere there to select.

If we can get this working I can hopefully try out the model!

vishesh

---

<div class="post-metadata">

**Author:** ![Mohamed](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/mohamed/32/62_2.png) [@Mohamed](https://discuss.roboflow.com/u/Mohamed)\
**Post date:** [June 14, 2022, 6:21pm UTC](https://discuss.roboflow.com/t/how-can-i-prevent-the-model-from-predicting-the-same-object-twice/492/7 "2022-06-14T18:21:19Z")

</div>

The current option in Roboflow is the `overlap` and `confidence` flags. I’ll forward this to my team as feature request feedback. #feedback

To have a more explicit nms-setting you’d want to train and use a custom YOLOv5 model and `detect.py` with the `--agnostic-nms` argument included when you run inference with `detect.py`.

This guide has examples of using `detect.py` with arguments:

> **[Run YOLOv5 on Your Webcam](https://help.roboflow.com/guides/run-yolov5-on-your-webcam)**
>
> Local Inference with YOLOv5 on a Webcam Clone the Ultralytics YOLOv5 Github repository to your computer and run detect.py after saving the model weights fr

---

<div class="post-metadata">

**Author:** ![Mohamed](https://yyz1.discourse-cdn.com/flex029/user_avatar/discuss.roboflow.com/mohamed/32/62_2.png) [@Mohamed](https://discuss.roboflow.com/u/Mohamed)\
**Post date:** [June 14, 2022, 6:34pm UTC](https://discuss.roboflow.com/t/how-can-i-prevent-the-model-from-predicting-the-same-object-twice/492/8 "2022-06-14T18:34:12Z")

</div>

Also, “class-agnostic nms” would set the nms-values’ results in your inference to operate in such a way that your model detects a bunch of objects “without knowing” what class they belong to.

A higher confidence setting and higher IOU (overlap) setting for inference helps to accomplish the “consolidation” of overlapping bounding boxes of the same class for the same object. So you should still be able to try this out/test with your Roboflow model with higher confidence and overlap settings, and [implement active learning](https://help.roboflow.com/guides/implementing-active-learning) to improve classes that don’t perform well. Start with lower confidence & overlap for an image and increase the values in increments of 3-5% with each test.

- Note: use the same image for the tests, and be sure it is not an image within your validation or test sets
- [Train, Validation, Test Split and Why You Need It](https://blog.roboflow.com/train-test-split/)
