Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to output test.py metrics per class? #1829

Closed
kotagiripranay opened this issue Jan 4, 2021 · 17 comments · Fixed by #1869
Closed

How to output test.py metrics per class? #1829

kotagiripranay opened this issue Jan 4, 2021 · 17 comments · Fixed by #1869
Assignees
Labels
question Further information is requested

Comments

@kotagiripranay
Copy link

❔Question

Hi
can anyone help me out with how to check the performance of the yolov5 model on my new data
By using detect.py I can get the output detected.
But how to check its map and how to plot its matrix eg: training and Val we get map.5 and map.95

Additional context

@kotagiripranay kotagiripranay added the question Further information is requested label Jan 4, 2021
@github-actions
Copy link
Contributor

github-actions bot commented Jan 4, 2021

👋 Hello @kotagiripranay, thank you for your interest in 🚀 YOLOv5! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a 🐛 Bug Report, please provide screenshots and minimum viable code to reproduce your issue, otherwise we can not help you.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online W&B logging if available.

For business inquiries or professional support requests please visit https://www.ultralytics.com or email Glenn Jocher at glenn.jocher@ultralytics.com.

Requirements

Python 3.8 or later with all requirements.txt dependencies installed, including torch>=1.7. To install run:

$ pip install -r requirements.txt

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

CI CPU testing

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training (train.py), testing (test.py), inference (detect.py) and export (export.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.

@glenn-jocher
Copy link
Member

@kotagiripranay test.py computes metrics on trained models. See Colab notebook for examples.
https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb

@kotagiripranay
Copy link
Author

kotagiripranay commented Jan 6, 2021

Hi, @glenn-jocher I think you have understood the question in the wrong way. let me explain in detail so you can help me out in solving it.

  1. we use train.py to train the model in the YAML file we will be providing the path of training and testing data with its respective images and labels
  2. After training the data for 50 epochs on my own training data with a batch size of 5 I got this matrix
     Epoch   gpu_mem      GIoU       obj       cls     total   targets  img_size
     49/49     6.47G   0.01198    0.0143  0.002628    0.0289        77       640: 100% 1600/1600 [14:16<00:00,  1.87it/s]
               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 120/120 [00:19<00:00,  6.10it/s]
                 all         600    6.01e+03       0.578       0.788       0.598       0.497
  1. so here I have around 5 classes like ['figure', 'list', 'table', 'text', 'title']

  2. In the above matrix which I have shown, getting an overall performance of my training data map of 598 its good

  3. next I plotted the graph and understood everything that happing in the training data

  4. now I run this code ['pub-colab is a YAML file which I had provided same in train.py as well']

!python test.py --weights weights/best.pt --data pub_colab.yaml --img 640 --iou 0.65

so I got the output as :

Namespace(augment=False, batch_size=32, conf_thres=0.001, data='./data/pub_colab.yaml', device='', img_size=640, iou_thres=0.65, merge=False, save_json=False, single_cls=False, task='val', verbose=False, weights='weights/best.pt')
Using CUDA device0 _CudaDeviceProperties(name='Tesla P100-PCIE-16GB', total_memory=16280MB)

Model Summary: 335 layers, 4.74149e+07 parameters, 0 gradients
Fusing layers...
Model Summary: 236 layers, 4.73862e+07 parameters, 4.48868e+07 gradients
Caching labels /content/drive/MyDrive/pub_colab/pub_colab/labels/val (600 found, 0 missing, 0 empty, 1 duplicate, for 600 images): 100% 600/600 [03:23<00:00,  2.94it/s]
               Class      Images     Targets           P           R      mAP@.5  mAP@.5:.95: 100% 19/19 [01:48<00:00,  5.74s/it]
                 all         600    6.01e+03       0.487       0.777       0.608       0.507
Speed: 12.4/1.6/13.9 ms inference/NMS/total per 640x640 image at batch-size 32

  1. I am happy that my map is good.

I THINK ITS CLEAR TILL NOW

THE PROBLEM STATEMENT is :

  1. I said u earlier that I had 5 classes like ['figure', 'list', 'table', 'text', 'title']

  2. I want the individual map score for every class to check which class is detecting good .....so I can do some hyperparameter tuning for the classes which are not detecting properly

an example which I am expecting :

       Text             map.5 --->78  map.95 ---> 75
       Figure         map.5 --->67  map.95 ---> 68
       Title            map.5 --->90  map.95 ---> 87
       Table          map.5 --->50  map.95 ---> 55
        List            map.5 --->48  map.95 ---> 35

can u help me out with the way how can I get the map for individual classes on my test data?

@kotagiripranay kotagiripranay changed the title how to check the performance of new data in the from metrix and plot its results how to check the performance of test data for individual classes using matrix Jan 6, 2021
@glenn-jocher
Copy link
Member

@kotagiripranay use python test.py --verbose to output metrics per class.

@glenn-jocher glenn-jocher changed the title how to check the performance of test data for individual classes using matrix How to output test.py metrics per class? Jan 7, 2021
@glenn-jocher glenn-jocher self-assigned this Jan 7, 2021
@glenn-jocher
Copy link
Member

glenn-jocher commented Jan 7, 2021

TODO: automatic --verbose on criteria match 1) <20 classes AND 2) [called directly from if __name__ == '__main__' OR final training epoch]?

@kotagiripranay
Copy link
Author

kotagiripranay commented Jan 7, 2021

That's great it worked thankyou @glenn-jocher, Now I have one more query can u please me out in the same way as the above one

now I came to know the matrix for individual classes to testing data using test.py --verbose

THE NEW PROBLEM STATEMENT IS:

now I will upload some new jpg files WITHOUT ANY LABELS in yolov5/inference/images folder

By using the code : !python detect.py --weights weights/last.pt --img 640 --conf 0.4 the output is :

Namespace(agnostic_nms=False, augment=False, classes=None, conf_thres=0.4, device='', fourcc='mp4v', img_size=640, iou_thres=0.5, output='inference/output', save_txt=False, source='inference/images', view_img=False, weights='weights/best.pt')
Using CUDA device0 _CudaDeviceProperties(name='Tesla T4', total_memory=15079MB)

image 1/50 inference/images/PMC1599750_00010.jpg: 640x512 2 figures, 5 texts, 1 titles, Done. (0.028s)
image 2/50 inference/images/PMC1601951_00002.jpg: 640x512 10 texts, 3 titles, Done. (0.026s)
image 3/50 inference/images/PMC1601951_00008.jpg: 640x512 2 figures, 2 texts, 1 titles, Done. (0.024s)
image 4/50 inference/images/PMC1601952_00000.jpg: 640x512 3 texts, 3 titles, Done. (0.024s)
image 5/50 inference/images/PMC1601955_00002.jpg: 640x512 1 tables, 7 texts, 5 titles, Done. (0.025s)
image 6/50 inference/images/PMC1617106_00002.jpg: 640x512 7 texts, 5 titles, Done. (0.024s)
image 7/50 inference/images/PMC1617125_00005.jpg: 640x512 2 figures, 8 texts, 3 titles, Done. (0.023s)
image 8/50 inference/images/PMC1618838_00004.jpg: 640x512 6 texts, 2 titles, Done. (0.024s)
image 9/50 inference/images/PMC1624833_00001.jpg: 640x512 8 texts, 2 titles, Done. (0.023s)
image 10/50 inference/images/PMC1626074_00005.jpg: 640x512 10 texts, 1 titles, Done. (0.023s)

so here new images are created in the output folder by detecting their respective classes as shown above

QUESTION :

how to calculate the map of new data for individual classes as we did for the above testing data which I shared in yolov5/inference/images folder

@glenn-jocher
Copy link
Member

@kotagiripranay mAP computation requires labels naturally.

Therefore asking how to compute mAP on unlabelled data is completely contrary to the purpose of the metric.

@kotagiripranay
Copy link
Author

kotagiripranay commented Jan 7, 2021

@glenn-jocher I am asking is there any way to calculate map on the predicted images which are stored in yolov5/inference/output folder

this is the image which predicted by using the code :

!python detect.py --weights weights/last.pt --img 640 --conf 0.4

output :

Namespace(agnostic_nms=False, augment=False, classes=None, conf_thres=0.4, device='', fourcc='mp4v', img_size=640, iou_thres=0.5, output='inference/output', save_txt=False, source='inference/images', view_img=False, weights='weights/best.pt')
Using CUDA device0 _CudaDeviceProperties(name='Tesla T4', total_memory=15079MB)

image 1/1 inference/images/PMC1781957_00028.jpg: 640x512 7 texts, 4 titles, Done. (0.028s)

here the model has detected 7 texts and 4 titles, and the predicted file is saved in yolov5/inference/output folder

PMC1781957_00028

I want to calculate the map or accuracy for this set of predicted files that are present in the total folder...

can u help me out with it...:)

@glenn-jocher
Copy link
Member

@kotagiripranay its very simple:

  • test.py computes mAP on labelled data.
  • detect.py runs inference on unlabelled data.

@kotagiripranay
Copy link
Author

kotagiripranay commented Jan 7, 2021

@glenn-jocher as I new to object detection ...out of curiosity asking one doubt ..
what if someone asks what is your yolov5 model performance on new data.. can u please explain through matrix..
what is the answer then...?

test.py is for calculating map on test data....but what if I provided completely new data that has no labels..?
hope ur getting my point

I tried a new data in test.py -- data/pub_colab.yaml --img 640

but in pub_colab.yaml ...I provided train data images and their respective labels but in test data, I provided only images but no labels as usually it has thrown an error: cannot find the labels.

answer to this question...?

@glenn-jocher
Copy link
Member

@kotagiripranay metrics are computed on labelled data only.

@kotagiripranay
Copy link
Author

@glenn-jocher thanks buddy....now it's clear ...

@glenn-jocher glenn-jocher reopened this Jan 7, 2021
@glenn-jocher glenn-jocher linked a pull request Jan 7, 2021 that will close this issue
@glenn-jocher glenn-jocher removed the TODO label Jan 7, 2021
@glenn-jocher
Copy link
Member

@kotagiripranay PR #1869 implements --verbose automatically now when calling test.py directly with a dataset smaller than 20 classes.

@AnshKetchum
Copy link

@glenn-jocher Is there some sort of file that's created or method from which we can save the per-class metrics outputted (the ones at the end of training) using val.py or within train.py itself?

@glenn-jocher
Copy link
Member

@AnshKetchum try --verbose:

python val.py --verbose

@AnshKetchum
Copy link

AnshKetchum commented Dec 23, 2022

@glenn-jocher I've tried running val.py on verbose, and I see the per-class metrics that are outputted. But I want to save these metrics locally to some sort of a .txt or .csv file.

python val.py --verbose displays the metrics, but when I check runs/val/exp, I can't find any file that has those class metrics saved.

@glenn-jocher
Copy link
Member

@AnshKetchum Sorry for the confusion. Currently, there isn't a built-in feature to save the per-class metrics to a file directly in YOLOv5. You can manually save the verbose output to a file using Linux tee command or Windows PowerShell Out-File. Hope this helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants