My first Object Detection proyect and bounding boxes problems

username: Miguel_Campos
cooked: <p>Hi all, firstly, sorry for my english, i am still learning.</p>
<p>I am trying my first object detection proyect. For this propose i am following a example from the official pytorch website but i decided to use my own images, concretly the dataset contains 2 airplanes:<br>
<a href="https://pytorch.org/docs/stable/torchvision/models.html#faster-r-cnn" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/torchvision/models.html#faster-r-cnn</a></p>
<p>This is my code, where as the official site, i dowload a pretrained model</p>
<blockquote>
<p>model = torchvision.models.detection.fasterrcnn_resnet50_fpn(pretrained=True)</p>
</blockquote>
<p>Then i prepared my dataset</p>
<blockquote>
<p>set_data = torchvision.datasets.ImageFolder(“/home/mjack3/Imágenes”,<br>
transform = transforms.Compose(<br>
[transforms.Resize([200, 200]),<br>
transforms.ToTensor(),<br>
]))</p>
</blockquote>
<blockquote>
<p>testloader = torch.utils.data.DataLoader(set_data, batch_size=2)<br>
img, label = next(iter(testloader))</p>
</blockquote>
<p>And finally i feed my model</p>
<blockquote>
<p>predictions = model(img)<br>
predictions</p>
</blockquote>
<p>Results for 2 images:</p>
<blockquote>
<p>[{‘boxes’: tensor([[  2.1480,  41.3460, 200.0000, 157.1328],<br>
[ 16.6714, 144.5215,  36.9665, 152.5237],<br>
[178.3848, 143.8716, 198.9536, 150.1973],<br>
[ 15.4439, 145.8342,  28.8368, 152.8024],<br>
[  0.4650, 144.6138,  14.9021, 152.9334],<br>
[ 38.4139, 144.2619,  47.7382, 152.0511],<br>
[ 26.9428, 144.4103,  37.6303, 151.7255],<br>
[ 29.1579, 143.9854,  55.7968, 152.4163],<br>
[  0.6116, 144.8602,  15.0447, 152.9558],<br>
[ 31.8172, 145.3369,  45.8835, 151.8564],<br>
[179.8206, 143.0551, 191.4160, 149.7137],<br>
[ 15.6531, 144.1850,  41.4935, 152.6716],<br>
[  0.0000,  65.7286, 110.4369, 149.1068],<br>
[ 32.3294, 145.6019,  39.6292, 151.9699],<br>
[ 34.2337, 142.1913,  50.6896, 151.5474],<br>
[ 35.4431, 145.8370,  40.0628, 151.6249],<br>
[ 14.8859, 145.5617,  22.5705, 152.6457],<br>
[ 93.2531, 144.5989, 108.6908, 151.4838],<br>
[ 30.6879, 145.0558,  36.9574, 151.1745],<br>
[ 10.0094, 144.1053,  51.3472, 152.9482],<br>
[ 35.6446, 143.2554,  48.3307, 151.7106],<br>
[ 20.3258, 146.4695,  26.9378, 152.7105],<br>
[145.7043, 118.8403, 156.8973, 149.5791]], grad_fn=),<br>
‘labels’: tensor([5, 3, 3, 3, 3, 3, 3, 3, 8, 3, 3, 8, 5, 3, 3, 3, 3, 3, 3, 3, 8, 3, 1]),<br>
‘scores’: tensor([0.9896, 0.8164, 0.6273, 0.4471, 0.4334, 0.3455, 0.1999, 0.1908, 0.1772,<br>
0.1532, 0.1505, 0.1308, 0.1236, 0.1062, 0.0980, 0.0979, 0.0968, 0.0916,<br>
0.0915, 0.0851, 0.0832, 0.0646, 0.0511], grad_fn=)},<br>
{‘boxes’: tensor([[136.1270, 120.9056, 147.9458, 131.6058],<br>
[136.2942, 120.7577, 144.5860, 128.0632],<br>
[139.5699, 123.0861, 146.5174, 131.7614]], grad_fn=),<br>
‘labels’: tensor([5, 5, 5]),<br>
‘scores’: tensor([0.9813, 0.5450, 0.2220], grad_fn=)}]</p>
</blockquote>
<p>So the idea is, getting the maximum score class index for getting the position of the label and the bounding box in this way:</p>
<blockquote>
<p>_, <strong>index_pred</strong> = torch.max(predictions[0][“scores”].data, 0)<br>
label_pred = predictions[0][“labels”][<strong>index_pred</strong>].item()<br>
bb1 = predictions[0][“boxes”][<strong>index_pred</strong>].data.numpy()</p>
</blockquote>
<p>Now is time of print results, this is what i tried:</p>
<blockquote>
<p>rect = patches.Rectangle((<strong>bb1</strong>[0], <strong>bb1</strong>[1]),<strong>bb1</strong>[2],<strong>bb1</strong>[3],linewidth=2,edgecolor=‘r’,facecolor=‘none’)</p>
</blockquote>
<p>This is what i get for each predicted image:</p>
<p>In all the cases, i get 5 as index, wich is in COCO the airplane but the bboxes are not shown correctly.<br>
What am i doing wrong?</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/6/16cf32bfe1234444c5f54aa7e508ebbd504a3324.png" alt="des" data-base62-sha1="3fMqjTYFnHZGXqcV9QeT82xwpa4" width="629" height="129"></p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/0/20e73faa405e841562312a16a38516c7b59cec76.png" alt="inf1" data-base62-sha1="4H4J4WaopaUaI9pzt2AxWFqFp3w" width="257" height="252"><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/7/8/782a247f6910fa2aabb89974298ce377b52f2130.png" alt="avion2" data-base62-sha1="h91Amtqmlwp0uRPdpVWp3LiGffy" width="257" height="252"></p>