IndexError: index 2 is out of bounds for dimension 1 with size 2

Malek_Aburaddaha: <p>Hello I am using torch metrics mAP linked here:<br>
<a href="https://torchmetrics.readthedocs.io/en/stable/detection/mean_average_precision.html#torchmetrics.detection.mean_ap.MeanAveragePrecision" class="onebox" target="_blank" rel="noopener nofollow ugc">https://torchmetrics.readthedocs.io/en/stable/detection/mean_average_precision.html#torchmetrics.detection.mean_ap.MeanAveragePrecision</a><br>
to evaluate my model, and I have been stuck for a while with this error.<br>
Here is the code that I am using to convert my bboxes into tensors along with scores and labeles, then I’m implementing the metrics provided in the link above.<br>
Here is the code:</p>
<pre><code class="lang-auto">from torch import IntTensor, Tensor
from torchmetrics.detection import MeanAveragePrecision
#%% Initialize lists of dictionaries for evaluation

preds = []
target = []

#%% A Function to add detections annotations
def add_dt_annotation(bboxes, scores, labeles):
    annotations = {
        'boxes': Tensor(bboxes),
        'scores': Tensor(scores),
        'labels': Tensor(labeles)
    }
    
    # Append the annotations to preds
    preds.append(annotations)

#%% A function to add ground truth annotations
def add_gt_annotation(bboxes, labeles):
    annotations = {
        'boxes': Tensor(bboxes),
        'labels': Tensor(labeles)
    }
    
    # Append the annotations to preds
    target.append(annotations)  

#%%
import glob, os

# Using glob to find all text files in the folder of the detections
text_files = glob.glob(os.path.join('abs dt', '*.txt'))

# Iterate over the files in the folder
for file in text_files:
    bboxes= []
    scores = []
    labeles = []
    # Open the file and read the text from each file
    with open(file, 'r') as text:
        lines = text.readlines()
        # Iterate over the lines of the text file
        for line in lines:
            # Extracting the coordinates
            coords= [float(coord) for coord in line.strip().split(' ')[0:]]
            bbox = coords[0:4]
            score = [coords[4]]
            label = [0]
            labeles.append(label)
            bboxes.append(bbox)
            scores.append(score)

        # Creating a dictionary for each line with a bbox using the function
        # For the detections
        add_dt_annotation(bboxes, scores, labeles)   

#%%
# Doing the same for the ground truth labels

# Using glob to find all text files in the folder of the ground truth
text_files = glob.glob(os.path.join('abs gt', '*.txt'))

# Iterate over the files in the folder
for file in text_files:
    bboxes= []
    labeles = []
    # Open the file and read the text from each file
    with open(file, 'r') as text:
        lines = text.readlines()
        # Iterate over the lines of the text file
        for line in lines:
            # Extracting the coordinates
            coords= [float(coord) for coord in line.strip().split(' ')[0:]]
            bbox = coords[0:4]
            label = [0]
            labeles.append(label)
            bboxes.append(bbox)
        # Creating a dictionary for each line with a bbox using the function
        # For the ground truth
        add_gt_annotation(bboxes, labeles) 



#%% 
#Evaluate
metric = MeanAveragePrecision(box_format='cxcywh', iou_type="bbox")
metric.update(preds, target)
from pprint import pprint
pprint(metric.compute())
</code></pre>
<p>And here is the first 3 samples of (preds) and (target)</p>
<p>preds:<br>
[{‘boxes’: tensor([[1788.0000, 1472.5000,   88.0000,   21.0000],<br>
[2272.5000, 1603.5000,  133.0000,   35.0000]]),<br>
‘scores’: tensor([[0.8689],<br>
[0.9038]]),<br>
‘labels’: tensor([[0.],<br>
[0.]])},<br>
{‘boxes’: tensor([[2282.5000, 1485.0000,   75.0000,   22.0000]]),<br>
‘scores’: tensor([[0.9018]]),<br>
‘labels’: tensor([[0.]])},<br>
{‘boxes’: tensor([[2046.5000, 1377.5000,   87.0000,   23.0000]]),<br>
‘scores’: tensor([[0.8657]]),<br>
‘labels’: tensor([[0.]])}]</p>
<p>target:<br>
[{‘boxes’: tensor([[2297., 1602.,  242.,   44.],<br>
[1779., 1471.,  106.,   26.]]),<br>
‘labels’: tensor([[0.],<br>
[0.]])},<br>
{‘boxes’: tensor([[2284., 1484.,   68.,   24.],<br>
[2220., 1441.,   56.,   18.],<br>
[2009., 1355.,   70.,   22.]]),<br>
‘labels’: tensor([[0.],<br>
[0.],<br>
[0.]])},<br>
{‘boxes’: tensor([[2534., 1612.,  120.,   40.],<br>
[2380., 1520.,   88.,   24.],<br>
[2037., 1379.,   90.,   18.]]),<br>
‘labels’: tensor([[0.],<br>
[0.],<br>
[0.]])}]</p>
<p>after all of that, I still recieve this error when it runs “pprint(metric.compute())”:<br>
IndexError: index 2 is out of bounds for dimension 1 with size 2</p>