How can I calculate F1 score in object detection?


I’ve followed the object detection tutorial (TorchVision Object Detection Finetuning Tutorial — PyTorch Tutorials 1.10.1+cu102 documentation) and adapted the code for my problem.

I understand that the average precision and recalls are calculated when put through evaluation:

for epoch in range(num_epochs):
  # train for one epoch, printing every 10 iterations
  train_one_epoch(model, optimizer, train_data_loader, device, epoch, print_freq=10)
  # update the learning rate
  # evaluate on the test dataset
  evaluate(model, valid_data_loader, device=device)

How can I calculate the f1 score from these metrics? Would I need to extract additional information? I’m guessing I would need to also calculate the number of false negatives, false positives, true positives and true negatives based on a specific IoU threshold…

Is there an example that already exists?

You can use sklearn.metrics.f1_score if you don’t want to calculate f1 manually

right, was thinking about something else :slight_smile:
if you already have precision and recall, why don’t just directly calculate F1 = (2 * precision * recall)/(precision+recall) ?