|
1 |
| -# From Getting Started to Deployment tutorial with YOLOv5 |
| 1 | +# From getting started to deployment with YOLOv5 |
| 2 | + |
| 3 | +## Environment Setup |
| 4 | + |
| 5 | +Note: Since this repository uses OpenMMLab 2.0, please create a new conda virtual environment to prevent conflicts with your existing repositories and projects of OpenMMLab 1.0. |
| 6 | + |
| 7 | +```shell |
| 8 | +conda create -n open-mmlab python=3.8 -y |
| 9 | +conda activate open-mmlab |
| 10 | +conda install pytorch torchvision -c pytorch |
| 11 | +# conda install pytorch torchvision cpuonly -c pytorch |
| 12 | +pip install -U openmim |
| 13 | +mim install mmengine |
| 14 | +mim install "mmcv>=2.0.0rc1,<2.1.0" |
| 15 | +mim install "mmdet>=3.0.0rc0,<3.1.0" |
| 16 | +# for albumentations |
| 17 | +git clone https://github.com/open-mmlab/mmyolo.git |
| 18 | +cd mmyolo |
| 19 | +# Install albumentations |
| 20 | +pip install -r requirements/albu.txt |
| 21 | +# Install MMYOLO |
| 22 | +mim install -v -e . |
| 23 | +# "-v" means verbose, or more output |
| 24 | +# "-e" means install the project in editable mode, so any local modifications made to the code will take effect, eliminating the need to reinstall. |
| 25 | +``` |
| 26 | + |
| 27 | +For more detailed information about environment configuration, please refer to [get_started](../get_started.md). |
| 28 | + |
| 29 | +## Dataset Preparation |
| 30 | + |
| 31 | +In this tutorial, we provide the ballon dataset, which is less than 40MB, as the training dataset for MMYOLO. |
| 32 | + |
| 33 | +```shell |
| 34 | +python tools/misc/download_dataset.py --dataset-name balloon --save-dir data --unzip |
| 35 | +python tools/dataset_converters/balloon2coco.py |
| 36 | +``` |
| 37 | + |
| 38 | +After executing the above command, the balloon dataset will be downloaded in the `data` folder with the converted format we need. The `train.json` and `val.json` are the annotation files in the COCO format. |
| 39 | + |
| 40 | +<div align=center> |
| 41 | +<img src="https://cdn.vansin.top/img/20220912105312.png" alt="image"/> |
| 42 | +</div> |
| 43 | + |
| 44 | +## Config File Preparation |
| 45 | + |
| 46 | +Create a new file called the `yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py` configuration file in the `configs/yolov5` folder, and copy the following content into it. |
| 47 | + |
| 48 | +```python |
| 49 | +_base_ = './yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py' |
| 50 | + |
| 51 | +data_root = 'data/balloon/' |
| 52 | + |
| 53 | +train_batch_size_per_gpu = 4 |
| 54 | +train_num_workers = 2 |
| 55 | + |
| 56 | +metainfo = { |
| 57 | + 'CLASSES': ('balloon', ), |
| 58 | + 'PALETTE': [ |
| 59 | + (220, 20, 60), |
| 60 | + ] |
| 61 | +} |
| 62 | + |
| 63 | +train_dataloader = dict( |
| 64 | + batch_size=train_batch_size_per_gpu, |
| 65 | + num_workers=train_num_workers, |
| 66 | + dataset=dict( |
| 67 | + data_root=data_root, |
| 68 | + metainfo=metainfo, |
| 69 | + data_prefix=dict(img='train/'), |
| 70 | + ann_file='train.json')) |
| 71 | + |
| 72 | +val_dataloader = dict( |
| 73 | + dataset=dict( |
| 74 | + data_root=data_root, |
| 75 | + metainfo=metainfo, |
| 76 | + data_prefix=dict(img='val/'), |
| 77 | + ann_file='val.json')) |
| 78 | + |
| 79 | +test_dataloader = val_dataloader |
| 80 | + |
| 81 | +val_evaluator = dict(ann_file=data_root + 'val.json') |
| 82 | + |
| 83 | +test_evaluator = val_evaluator |
| 84 | + |
| 85 | +model = dict(bbox_head=dict(head_module=dict(num_classes=1))) |
| 86 | + |
| 87 | +default_hooks = dict(logger=dict(interval=1)) |
| 88 | +``` |
| 89 | + |
| 90 | +The above configuration is inherited from `./yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py`, and `data_root`, `metainfo`, `train_dataloader`, `val_dataloader`, `num_classes` and other configurations are updated according to the balloon data we are using. |
| 91 | +The reason why we set the `interval` of the logger to 1 is that the balloon data set we choose is relatively small, and if the `interval` is too large, we will not see the output of the loss-related log. Therefore, by setting the `interval` of the logger to 1 will ensure that each interval iteration will output a loss-related log. |
| 92 | + |
| 93 | +## Training |
| 94 | + |
| 95 | +```shell |
| 96 | +python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py |
| 97 | +``` |
| 98 | + |
| 99 | +After executing the above training command, the `work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon` folder will be automatically generated. Both the weight and the training configuration files will be saved in this folder. |
| 100 | + |
| 101 | +<div align=center> |
| 102 | +<img src="https://cdn.vansin.top/img/20220913213846.png" alt="image"/> |
| 103 | +</div> |
| 104 | + |
| 105 | +### Resume training after interruptions |
| 106 | + |
| 107 | +If training stops midway, add `--resume` at the end of the training command, and the program will automatically load the latest weight file from `work_dirs` to resume training. |
| 108 | + |
| 109 | +```shell |
| 110 | +python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py --resume |
| 111 | +``` |
| 112 | + |
| 113 | +### Fine-tune with loaded pretrained weights |
| 114 | + |
| 115 | +NOTICE: It is highly recommended that finetuning from large datasets, such as COCO, can significantly boost the performance of overall network. |
| 116 | +In this example, compared with training from scratch, finetuning the pretrained model outperforms with a significant margin. (Over 30+ mAP boost than training from scratch). |
| 117 | + |
| 118 | +1. Download the COCO dataset pre-trained weights |
| 119 | + |
| 120 | +```shell |
| 121 | +cd mmyolo |
| 122 | +wget https://download.openmmlab.com/mmyolo/v0/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco/yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth |
| 123 | +``` |
| 124 | + |
| 125 | +2. Load the pretrained model to train |
| 126 | + |
| 127 | +```shell |
| 128 | +cd mmyolo |
| 129 | +python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py \ |
| 130 | + --cfg-options load_from='yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth' custom_hooks=None |
| 131 | +``` |
| 132 | + |
| 133 | +Note: Ideally, the `strict_load` initialization parameter of `EMAHook` should be set to `False` during the fine-tuning phase, which makes the command `custom_hooks.0.strict_load=False`. However, because MMEngine v0.1.0 is the initial development version, there will be problems with this setting right now. Therefore, for the time being, you can only use the command `custom_hooks=None` to turn off `custom_hooks` to load the pre-training weights correctly. This issue is expected to be fixed in the next release. |
| 134 | + |
| 135 | +3. Freeze backbone to train |
| 136 | + |
| 137 | +Freeze the four backbone stages by setting `model.backbone.frozen_stages=4` in the config file or from the command line. |
| 138 | + |
| 139 | +```shell |
| 140 | +# Set model.backbone.frozen_stages=4 from the command line |
| 141 | +cd mmyolo |
| 142 | +python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py \ |
| 143 | + --cfg-options load_from='yolov5_s-v61_syncbn_fast_8xb16-300e_coco_20220918_084700-86e02187.pth' model.backbone.frozen_stages=4 custom_hooks=None |
| 144 | +``` |
| 145 | + |
| 146 | +### Visualization |
| 147 | + |
| 148 | +For `visualization` of `default_hooks` in `configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py`, we set `draw` to `True` and `interval` to `2`. |
| 149 | + |
| 150 | +```python |
| 151 | +default_hooks = dict( |
| 152 | + logger=dict(interval=1), |
| 153 | + visualization=dict(draw=True, interval=2), |
| 154 | +) |
| 155 | +``` |
| 156 | + |
| 157 | +Re-run the following training command. During the validation, each `interval` image will save a puzzle of the annotation and prediction results to `work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/{timestamp}/vis_data/vis_image` folder. |
| 158 | + |
| 159 | +```shell |
| 160 | +python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py |
| 161 | +``` |
| 162 | + |
| 163 | +<div align=center> |
| 164 | +<img src="https://moonstarimg.oss-cn-hangzhou.aliyuncs.com/img/20220920094007.png" alt="image"/> |
| 165 | +</div> |
| 166 | + |
| 167 | +#### Visualization Backend Support |
| 168 | + |
| 169 | +MMEngine supports various backends such as local, TensorBoard, and wandb. |
| 170 | + |
| 171 | +- wandb |
| 172 | + |
| 173 | +Register and get your wandb API key from the [official website](https://wandb.ai/settings). |
| 174 | + |
| 175 | +<div align=center> |
| 176 | +<img src="https://cdn.vansin.top/img/20220913212628.png" alt="image"/> |
| 177 | +</div> |
| 178 | + |
| 179 | +```shell |
| 180 | +pip install wandb |
| 181 | + |
| 182 | +wandb login |
| 183 | +# enter your API key, then you can see if you login successfully |
| 184 | +``` |
| 185 | + |
| 186 | +Add wandb configuration in `configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py`. |
| 187 | + |
| 188 | +```python |
| 189 | +visualizer = dict(vis_backends = [dict(type='LocalVisBackend'), dict(type='WandbVisBackend')]) |
| 190 | +``` |
| 191 | + |
| 192 | +Re-run the training command to check data visualization results such as loss, learning rate, and coco/bbox_mAP in the web link prompted on the command line. |
| 193 | + |
| 194 | +```shell |
| 195 | +python tools/train.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py |
| 196 | +``` |
| 197 | + |
| 198 | +<div align=center> |
| 199 | +<img src="https://cdn.vansin.top/img/20220913213221.png" alt="image"/> |
| 200 | +</div> |
| 201 | + |
| 202 | +- Tensorboard |
| 203 | + |
| 204 | +Install Tensorboard |
| 205 | + |
| 206 | +```shell |
| 207 | +pip install tensorboard |
| 208 | +``` |
| 209 | + |
| 210 | +Similar to wandb, we need to add Tensorboard configuration in `configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py`. |
| 211 | + |
| 212 | +```python |
| 213 | +visualizer = dict(vis_backends=[dict(type='LocalVisBackend'),dict(type='TensorboardVisBackend')]) |
| 214 | +``` |
| 215 | + |
| 216 | +Re-run the training command, a Tensorboard folder will be created in `work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/{timestamp}/vis_data`, You can get data visualization results such as loss, learning rate, and coco/bbox_mAP in the web link prompted on the command line with the following command: |
| 217 | + |
| 218 | +```shell |
| 219 | +tensorboard --logdir=work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon |
| 220 | +``` |
| 221 | + |
| 222 | +## Model Testing |
| 223 | + |
| 224 | +If you set `custom_hooks=None` during training, you still need to set `custom_hooks=None` during model testing as well. |
| 225 | + |
| 226 | +```shell |
| 227 | +python tools/test.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py \ |
| 228 | + work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/epoch_300.pth \ |
| 229 | + --show-dir show_results --cfg-options custom_hooks=None |
| 230 | +``` |
| 231 | + |
| 232 | +If you don't set `custom_hooks=None` during training, the test command should be as follows: |
| 233 | + |
| 234 | +```shell |
| 235 | +python tools/test.py configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py \ |
| 236 | + work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/epoch_300.pth \ |
| 237 | + --show-dir show_results |
| 238 | +``` |
| 239 | + |
| 240 | +Run the above command, the inference result picture will be automatically saved to the `work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/{timestamp}/show_results` folder. The following is one of the result pictures. The left one is the actual annotation, and the right is the model inference result. |
| 241 | + |
| 242 | +<div align=center> |
| 243 | +<img src="https://user-images.githubusercontent.com/27466624/190913272-f99709e5-c798-46b8-aede-30f4e91683a3.jpg" alt="result_img"/> |
| 244 | +</div> |
| 245 | + |
| 246 | +## Model Deployment |
| 247 | + |
| 248 | +Under development... |
0 commit comments