Hello I am using torch metrics mAP linked here:
https://torchmetrics.readthedocs.io/en/stable/detection/mean_average_precision.html#torchmetrics.detection.mean_ap.MeanAveragePrecision
to evaluate my model, and I have been stuck for a while with this error.
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.
Here is the code:
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())
And here is the first 3 samples of (preds) and (target)
preds:
[{‘boxes’: tensor([[1788.0000, 1472.5000, 88.0000, 21.0000],
[2272.5000, 1603.5000, 133.0000, 35.0000]]),
‘scores’: tensor([[0.8689],
[0.9038]]),
‘labels’: tensor([[0.],
[0.]])},
{‘boxes’: tensor([[2282.5000, 1485.0000, 75.0000, 22.0000]]),
‘scores’: tensor([[0.9018]]),
‘labels’: tensor([[0.]])},
{‘boxes’: tensor([[2046.5000, 1377.5000, 87.0000, 23.0000]]),
‘scores’: tensor([[0.8657]]),
‘labels’: tensor([[0.]])}]
target:
[{‘boxes’: tensor([[2297., 1602., 242., 44.],
[1779., 1471., 106., 26.]]),
‘labels’: tensor([[0.],
[0.]])},
{‘boxes’: tensor([[2284., 1484., 68., 24.],
[2220., 1441., 56., 18.],
[2009., 1355., 70., 22.]]),
‘labels’: tensor([[0.],
[0.],
[0.]])},
{‘boxes’: tensor([[2534., 1612., 120., 40.],
[2380., 1520., 88., 24.],
[2037., 1379., 90., 18.]]),
‘labels’: tensor([[0.],
[0.],
[0.]])}]
after all of that, I still recieve this error when it runs “pprint(metric.compute())”:
IndexError: index 2 is out of bounds for dimension 1 with size 2