Switch to unified view

a b/utils/loggers/comet/README.md
1
<img src="https://cdn.comet.ml/img/notebook_logo.png">
2
3
# YOLOv5 with Comet
4
5
This guide will cover how to use YOLOv5 with [Comet](https://bit.ly/yolov5-readme-comet2)
6
7
# About Comet
8
9
Comet builds tools that help data scientists, engineers, and team leaders accelerate and optimize machine learning and deep learning models.
10
11
Track and visualize model metrics in real time, save your hyperparameters, datasets, and model checkpoints, and visualize your model predictions with [Comet Custom Panels](https://www.comet.com/docs/v2/guides/comet-dashboard/code-panels/about-panels/?utm_source=yolov5&utm_medium=partner&utm_campaign=partner_yolov5_2022&utm_content=github)!
12
Comet makes sure you never lose track of your work and makes it easy to share results and collaborate across teams of all sizes!
13
14
# Getting Started
15
16
## Install Comet
17
18
```shell
19
pip install comet_ml
20
```
21
22
## Configure Comet Credentials
23
24
There are two ways to configure Comet with YOLOv5.
25
26
You can either set your credentials through environment variables
27
28
**Environment Variables**
29
30
```shell
31
export COMET_API_KEY=<Your Comet API Key>
32
export COMET_PROJECT_NAME=<Your Comet Project Name> # This will default to 'yolov5'
33
```
34
35
Or create a `.comet.config` file in your working directory and set your credentials there.
36
37
**Comet Configuration File**
38
39
```
40
[comet]
41
api_key=<Your Comet API Key>
42
project_name=<Your Comet Project Name> # This will default to 'yolov5'
43
```
44
45
## Run the Training Script
46
47
```shell
48
# Train YOLOv5s on COCO128 for 5 epochs
49
python train.py --img 640 --batch 16 --epochs 5 --data coco128.yaml --weights yolov5s.pt
50
```
51
52
That's it! Comet will automatically log your hyperparameters, command line arguments, training and validation metrics. You can visualize and analyze your runs in the Comet UI
53
54
<img width="1920" alt="yolo-ui" src="https://user-images.githubusercontent.com/26833433/202851203-164e94e1-2238-46dd-91f8-de020e9d6b41.png">
55
56
# Try out an Example!
57
58
Check out an example of a [completed run here](https://www.comet.com/examples/comet-example-yolov5/a0e29e0e9b984e4a822db2a62d0cb357?experiment-tab=chart&showOutliers=true&smoothing=0&transformY=smoothing&xAxis=step&utm_source=yolov5&utm_medium=partner&utm_campaign=partner_yolov5_2022&utm_content=github)
59
60
Or better yet, try it out yourself in this Colab Notebook
61
62
[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/comet-ml/comet-examples/blob/master/integrations/model-training/yolov5/notebooks/Comet_and_YOLOv5.ipynb)
63
64
# Log automatically
65
66
By default, Comet will log the following items
67
68
## Metrics
69
70
- Box Loss, Object Loss, Classification Loss for the training and validation data
71
- mAP_0.5, mAP_0.5:0.95 metrics for the validation data.
72
- Precision and Recall for the validation data
73
74
## Parameters
75
76
- Model Hyperparameters
77
- All parameters passed through the command line options
78
79
## Visualizations
80
81
- Confusion Matrix of the model predictions on the validation data
82
- Plots for the PR and F1 curves across all classes
83
- Correlogram of the Class Labels
84
85
# Configure Comet Logging
86
87
Comet can be configured to log additional data either through command line flags passed to the training script
88
or through environment variables.
89
90
```shell
91
export COMET_MODE=online # Set whether to run Comet in 'online' or 'offline' mode. Defaults to online
92
export COMET_MODEL_NAME=<your model name> #Set the name for the saved model. Defaults to yolov5
93
export COMET_LOG_CONFUSION_MATRIX=false # Set to disable logging a Comet Confusion Matrix. Defaults to true
94
export COMET_MAX_IMAGE_UPLOADS=<number of allowed images to upload to Comet> # Controls how many total image predictions to log to Comet. Defaults to 100.
95
export COMET_LOG_PER_CLASS_METRICS=true # Set to log evaluation metrics for each detected class at the end of training. Defaults to false
96
export COMET_DEFAULT_CHECKPOINT_FILENAME=<your checkpoint filename> # Set this if you would like to resume training from a different checkpoint. Defaults to 'last.pt'
97
export COMET_LOG_BATCH_LEVEL_METRICS=true # Set this if you would like to log training metrics at the batch level. Defaults to false.
98
export COMET_LOG_PREDICTIONS=true # Set this to false to disable logging model predictions
99
```
100
101
## Logging Checkpoints with Comet
102
103
Logging Models to Comet is disabled by default. To enable it, pass the `save-period` argument to the training script. This will save the
104
logged checkpoints to Comet based on the interval value provided by `save-period`
105
106
```shell
107
python train.py \
108
--img 640 \
109
--batch 16 \
110
--epochs 5 \
111
--data coco128.yaml \
112
--weights yolov5s.pt \
113
--save-period 1
114
```
115
116
## Logging Model Predictions
117
118
By default, model predictions (images, ground truth labels and bounding boxes) will be logged to Comet.
119
120
You can control the frequency of logged predictions and the associated images by passing the `bbox_interval` command line argument. Predictions can be visualized using Comet's Object Detection Custom Panel. This frequency corresponds to every Nth batch of data per epoch. In the example below, we are logging every 2nd batch of data for each epoch.
121
122
**Note:** The YOLOv5 validation dataloader will default to a batch size of 32, so you will have to set the logging frequency accordingly.
123
124
Here is an [example project using the Panel](https://www.comet.com/examples/comet-example-yolov5?shareable=YcwMiJaZSXfcEXpGOHDD12vA1&utm_source=yolov5&utm_medium=partner&utm_campaign=partner_yolov5_2022&utm_content=github)
125
126
```shell
127
python train.py \
128
--img 640 \
129
--batch 16 \
130
--epochs 5 \
131
--data coco128.yaml \
132
--weights yolov5s.pt \
133
--bbox_interval 2
134
```
135
136
### Controlling the number of Prediction Images logged to Comet
137
138
When logging predictions from YOLOv5, Comet will log the images associated with each set of predictions. By default a maximum of 100 validation images are logged. You can increase or decrease this number using the `COMET_MAX_IMAGE_UPLOADS` environment variable.
139
140
```shell
141
env COMET_MAX_IMAGE_UPLOADS=200 python train.py \
142
--img 640 \
143
--batch 16 \
144
--epochs 5 \
145
--data coco128.yaml \
146
--weights yolov5s.pt \
147
--bbox_interval 1
148
```
149
150
### Logging Class Level Metrics
151
152
Use the `COMET_LOG_PER_CLASS_METRICS` environment variable to log mAP, precision, recall, f1 for each class.
153
154
```shell
155
env COMET_LOG_PER_CLASS_METRICS=true python train.py \
156
--img 640 \
157
--batch 16 \
158
--epochs 5 \
159
--data coco128.yaml \
160
--weights yolov5s.pt
161
```
162
163
## Uploading a Dataset to Comet Artifacts
164
165
If you would like to store your data using [Comet Artifacts](https://www.comet.com/docs/v2/guides/data-management/using-artifacts/#learn-more?utm_source=yolov5&utm_medium=partner&utm_campaign=partner_yolov5_2022&utm_content=github), you can do so using the `upload_dataset` flag.
166
167
The dataset be organized in the way described in the [YOLOv5 documentation](https://docs.ultralytics.com/yolov5/tutorials/train_custom_data/). The dataset config `yaml` file must follow the same format as that of the `coco128.yaml` file.
168
169
```shell
170
python train.py \
171
--img 640 \
172
--batch 16 \
173
--epochs 5 \
174
--data coco128.yaml \
175
--weights yolov5s.pt \
176
--upload_dataset
177
```
178
179
You can find the uploaded dataset in the Artifacts tab in your Comet Workspace
180
<img width="1073" alt="artifact-1" src="https://user-images.githubusercontent.com/7529846/186929193-162718bf-ec7b-4eb9-8c3b-86b3763ef8ea.png">
181
182
You can preview the data directly in the Comet UI.
183
<img width="1082" alt="artifact-2" src="https://user-images.githubusercontent.com/7529846/186929215-432c36a9-c109-4eb0-944b-84c2786590d6.png">
184
185
Artifacts are versioned and also support adding metadata about the dataset. Comet will automatically log the metadata from your dataset `yaml` file
186
<img width="963" alt="artifact-3" src="https://user-images.githubusercontent.com/7529846/186929256-9d44d6eb-1a19-42de-889a-bcbca3018f2e.png">
187
188
### Using a saved Artifact
189
190
If you would like to use a dataset from Comet Artifacts, set the `path` variable in your dataset `yaml` file to point to the following Artifact resource URL.
191
192
```
193
# contents of artifact.yaml file
194
path: "comet://<workspace name>/<artifact name>:<artifact version or alias>"
195
```
196
197
Then pass this file to your training script in the following way
198
199
```shell
200
python train.py \
201
--img 640 \
202
--batch 16 \
203
--epochs 5 \
204
--data artifact.yaml \
205
--weights yolov5s.pt
206
```
207
208
Artifacts also allow you to track the lineage of data as it flows through your Experimentation workflow. Here you can see a graph that shows you all the experiments that have used your uploaded dataset.
209
<img width="1391" alt="artifact-4" src="https://user-images.githubusercontent.com/7529846/186929264-4c4014fa-fe51-4f3c-a5c5-f6d24649b1b4.png">
210
211
## Resuming a Training Run
212
213
If your training run is interrupted for any reason, e.g. disrupted internet connection, you can resume the run using the `resume` flag and the Comet Run Path.
214
215
The Run Path has the following format `comet://<your workspace name>/<your project name>/<experiment id>`.
216
217
This will restore the run to its state before the interruption, which includes restoring the  model from a checkpoint, restoring all hyperparameters and training arguments and downloading Comet dataset Artifacts if they were used in the original run. The resumed run will continue logging to the existing Experiment in the Comet UI
218
219
```shell
220
python train.py \
221
--resume "comet://<your run path>"
222
```
223
224
## Hyperparameter Search with the Comet Optimizer
225
226
YOLOv5 is also integrated with Comet's Optimizer, making is simple to visualize hyperparameter sweeps in the Comet UI.
227
228
### Configuring an Optimizer Sweep
229
230
To configure the Comet Optimizer, you will have to create a JSON file with the information about the sweep. An example file has been provided in `utils/loggers/comet/optimizer_config.json`
231
232
```shell
233
python utils/loggers/comet/hpo.py \
234
  --comet_optimizer_config "utils/loggers/comet/optimizer_config.json"
235
```
236
237
The `hpo.py` script accepts the same arguments as `train.py`. If you wish to pass additional arguments to your sweep simply add them after
238
the script.
239
240
```shell
241
python utils/loggers/comet/hpo.py \
242
  --comet_optimizer_config "utils/loggers/comet/optimizer_config.json" \
243
  --save-period 1 \
244
  --bbox_interval 1
245
```
246
247
### Running a Sweep in Parallel
248
249
```shell
250
comet optimizer -j <set number of workers> utils/loggers/comet/hpo.py \
251
  utils/loggers/comet/optimizer_config.json"
252
```
253
254
### Visualizing Results
255
256
Comet provides a number of ways to visualize the results of your sweep. Take a look at a [project with a completed sweep here](https://www.comet.com/examples/comet-example-yolov5/view/PrlArHGuuhDTKC1UuBmTtOSXD/panels?utm_source=yolov5&utm_medium=partner&utm_campaign=partner_yolov5_2022&utm_content=github)
257
258
<img width="1626" alt="hyperparameter-yolo" src="https://user-images.githubusercontent.com/7529846/186914869-7dc1de14-583f-4323-967b-c9a66a29e495.png">