Prediction threshold?

username: m3tobom_M
cooked: <p>Hello. I am working on this tutorial: <a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html#" class="inline-onebox" rel="noopener nofollow ugc">TorchVision Object Detection Finetuning Tutorial — PyTorch Tutorials 2.2.0+cu121 documentation</a>. I trained model and made predictions with py file below:</p>
<pre><code class="lang-auto">from PIL import Image
import torch
import numpy as np
from torchvision import transforms
from torch.autograd import Variable
import cv2


loader = transforms.Compose([transforms.ToTensor()])

def img_loader(img_path):
    img = Image.open(img_path)
    img = img.convert('RGB')
    img = loader(img).float()
    img = Variable(img, requires_grad=True)
    return img.cuda()
    
path = r't4.png'

model = torch.load('network')

model.eval()
img_arr = img_loader(path)
with torch.no_grad():
    pred = model([img_arr])

iter_num = len(pred[0]['boxes'])

img1 = cv2.imread(path)

#where I draw bboxes
for i in range(iter_num):
    tl =  (pred[0]['boxes'][i][0],pred[0]['boxes'][i][1])
    br = (pred[0]['boxes'][i][2],pred[0]['boxes'][i][3])
    img1 = cv2.rectangle(img1,tl,br, (0,0,255), 2)

output = Image.fromarray(pred[0]['masks'][0, 0].mul(255).byte().cpu().numpy())
print(output)
cv2_output = np.array(output)
cv2_output = cv2_output[:, :-1].copy()



cv2.imshow('out', img1)
cv2.imwrite('todiscuss.png', img1)
cv2.waitKey(0)
print('Done')

</code></pre>
<p>When I draw boundary boxes I got this result: <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/1/d14498166ba1aed2640fe003437071ea5c3a9616.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/d14498166ba1aed2640fe003437071ea5c3a9616" title="todiscuss"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d14498166ba1aed2640fe003437071ea5c3a9616_2_690x468.jpeg" alt="todiscuss" data-base62-sha1="tRgKIpnXpMwBbv3PnSJAdLm12ui" width="690" height="468" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/1/d14498166ba1aed2640fe003437071ea5c3a9616_2_690x468.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/d/1/d14498166ba1aed2640fe003437071ea5c3a9616.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/1/d14498166ba1aed2640fe003437071ea5c3a9616.jpeg 2x" data-dominant-color="A25E65"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">todiscuss</span><span class="informations">716×486 283 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>As it seen in image, pred = model(img) returns lots of bboxes. Is there any way to set a threshold for returned bboxes?</p>

username: KFrank
cooked: <p>Hi m3!</p>
<aside class="quote no-group" data-username="m3tobom_M" data-post="1" data-topic="80606" data-full="true">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/m3tobom_m/48/38207_2.png" class="avatar"> m3tobom_M:</div>
<blockquote>
<p>As it seen in image, pred = model(img) returns lots of bboxes. Is there any way to set a threshold for returned bboxes?</p>
</blockquote>
</aside>
<p>A common approach for this issue is <em>non-maximal suppression.</em>  I don’t<br>
know of a single, best reference for this, but it is discussed broadly.</p>
<p>Best.</p>
<p>K. Frank</p>

username: m3tobom_M
cooked: <p>Thanks a lot. I applied non-maximal supression and I had better results.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/1/6196d10d85f3d39f9b24a39aadf35a2ded646991.png" data-download-href="https://discuss.pytorch.org/uploads/default/6196d10d85f3d39f9b24a39aadf35a2ded646991" title="todiscuss"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/1/6196d10d85f3d39f9b24a39aadf35a2ded646991.png" alt="todiscuss" data-base62-sha1="dVjr8EHALZtBVtea58u8gKkEJDH" width="690" height="468" data-dominant-color="847F87"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">todiscuss</span><span class="informations">716×486 567 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>