File size: 6,318 Bytes
4a690bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
All In 1
========

:file:`yolo.lazy` is a packaged file that includes :guilabel:`training`, :guilabel:`validation`, and :guilabel:`inference` tasks.
For detailed function documentation, thercheck out the IPython notebooks to learn how to import and use these function
the following section will break down operation inside of lazy, also supporting directly import/call the function.

[TOC], setup, build, dataset, train, validation, inference
To train the model, you can run:

Train Model
----------


- batch size check / cuda
- training time / check
- build model / check
- dataset / check

.. code-block:: bash

    python yolo/lazy.py task=train

You can customize the training process by overriding the following common arguments:

- ``name``: :guilabel:`str`
  The experiment name.

- ``model``: :guilabel:`str`
  Model backbone, options include [model_zoo] v9-c, v7, v9-e, etc.

- ``cpu_num``: :guilabel:`int`
  Number of CPU workers (num_workers).

- ``out_path``: :guilabel:`Path`
  The output path for saving models and logs.

- ``weight``: :guilabel:`Path | bool | None`
  The path to pre-trained weights, False for training from scratch, None for default weights.

- ``use_wandb``: :guilabel:`bool`
  Whether to use Weights and Biases for experiment tracking.

- ``use_TensorBoard``: :guilabel:`bool`
  Whether to use TensorBoard for logging.

- ``image_size``: :guilabel:`int | [int, int]`
  The input image size.

- ``+quiet``: :guilabel:`bool`
  Optional, disable all output.

- ``task.epoch``: :guilabel:`int`
  Total number of training epochs.

- ``task.data.batch_size``: :guilabel:`int`
  The size of each batch (auto-batch sizing [WIP]).

Examples
~~~~~~~~

To train a model with a specific batch size and image size, you can run:

.. code-block:: bash

    python yolo/lazy.py task=train task.data.batch_size=12 image_size=1280

Multi-GPU Training with DDP
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

For multi-GPU training, we use Distributed Data Parallel (DDP) for efficient and scalable training.
DDP enable training model with mutliple GPU, even the GPUs aren't on the same machine. For more details, you can refer to the `DDP tutorial <https://pytorch.org/tutorials/intermediate/ddp_tutorial.html>`_.

To train on multiple GPUs, replace the ``python`` command with ``torchrun --nproc_per_node=[GPU_NUM]``. The ``nproc_per_node`` argument specifies the number of GPUs to use.


.. tabs::

   .. tab:: bash
    .. code-block:: bash

        torchrun --nproc_per_node=2 yolo/lazy.py task=train device=[0,1]

   .. tab:: zsh
    .. code-block:: bash

        torchrun --nproc_per_node=2 yolo/lazy.py task=train device=\[0,1\]


Training on a Custom Dataset
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To use the auto-download module, we suggest users construct the dataset config in the following format.
If the config files include `auto_download`, the model will automatically download the dataset when creating the dataloader.

Here is an example dataset config file:

.. literalinclude:: ../../yolo/config/dataset/dev.yaml
  :language: YAML

Both of the following formats are acceptable:

- ``path``: :guilabel:`str`
  The path to the dataset.

- ``train, validation``: :guilabel:`str`
  The training and validation directory names under `/images`. If using txt as ground truth, these should also be the names under `/labels/`.

- ``class_num``: :guilabel:`int`
  The number of dataset classes.

- ``class_list``: :guilabel:`List[str]`
  Optional, the list of class names, used only for visualizing the bounding box classes.

- ``auto_download``: :guilabel:`dict`
  Optional, whether to auto-download the dataset.

The dataset should include labels or annotations, preferably in JSON format for compatibility with pycocotools during inference:

.. code-block:: text

    DataSetName/
    β”œβ”€β”€ annotations
    β”‚   β”œβ”€β”€ train_json_name.json
    β”‚   └── val_json_name.json
    β”œβ”€β”€ labels/
    β”‚   β”œβ”€β”€ train/
    β”‚   β”‚   β”œβ”€β”€ AnyLabelName.txt
    β”‚   β”‚   └── ...
    β”‚   └── validation/
    β”‚       └── ...
    └── images/
        β”œβ”€β”€ train/
        β”‚   β”œβ”€β”€ AnyImageNameN.{png,jpg,jpeg}
        β”‚   └── ...
        └── validation/
            └── ...


Validation Model
----------------

During training, this block will be auto-executed. You may also run this task manually to generate a JSON file representing the predictions for a given validation dataset. If the validation set includes JSON annotations, it will run pycocotools for evaluation.

We recommend setting ``task.data.shuffle`` to False and turning off ``task.data.data_augment``.

You can customize the validation process by overriding the following arguments:

- ``task.nms.min_confidence``: :guilabel:`str`
  The minimum confidence of model prediction.

- ``task.nms.min_iou``: :guilabel:`str`
  The minimum IoU threshold for NMS (Non-Maximum Suppression).

Examples
~~~~~~~~

.. tabs::

   .. tab:: git-cloned
      .. code-block:: bash

         python yolo/lazy.py task=validation task.nms.min_iou=0.9

   .. tab:: PyPI
      .. code-block:: bash

         yolo task=validation task.nms.min_iou=0.9


Model Inference
---------------

.. note::
   The ``dataset`` parameter shouldn't be overridden because the model requires the ``class_num`` of the dataset. If the classes have names, please provide the ``class_list``.

You can customize the inference process by overriding the following arguments:

- ``task.fast_inference``: :guilabel:`str`
  Optional. Values can be `onnx`, `trt`, `deploy`, or `None`. `deploy` will detach the model auxiliary head.

- ``task.data.source``: :guilabel:`str | Path | int`
  This argument will be auto-resolved and could be a webcam ID, image folder path, video/image path.

- ``task.nms.min_confidence``: :guilabel:`str`
  The minimum confidence of model prediction.

- ``task.nms.min_iou``: :guilabel:`str`
  The minimum IoU threshold for NMS (Non-Maximum Suppression).

Examples
~~~~~~~~

.. tabs::

   .. tab:: git-cloned
      .. code-block:: bash

         python yolo/lazy.py model=v9-m task.nms.min_confidence=0.1 task.data.source=0 task.fast_inference=onnx

   .. tab:: PyPI
      .. code-block:: bash

         yolo model=v9-m task.nms.min_confidence=0.1 task.data.source=0 task.fast_inference=onnx