Spaces:
Configuration error
Configuration error
nn
commited on
Upload 123 files
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- README.md +81 -13
- United_States_000062.jpg +0 -0
- United_States_000502.jpg +0 -0
- YOLO9_run.ipynb +1123 -0
- panoptic/predict.py +246 -0
- panoptic/train.py +662 -0
- panoptic/val.py +597 -0
- requirements.txt +49 -0
- runs/detect/yolov9_c_640_detect/United_States_000062.jpg +0 -0
- runs/detect/yolov9_c_640_detect/United_States_000502.jpg +0 -0
- runs/detect/yolov9_c_640_detect/temp_image.jpg +0 -0
- scripts/get_coco.sh +22 -0
- segment/predict.py +246 -0
- segment/train.py +646 -0
- segment/train_dual.py +647 -0
- segment/val.py +457 -0
- segment/val_dual.py +458 -0
- streamlit.ipynb +0 -0
- temp_image.jpg +0 -0
- tools/reparameterization.ipynb +450 -0
- train.py +634 -0
- train_dual.py +644 -0
- train_triple.py +636 -0
- utils/__init__.py +75 -0
- utils/__pycache__/__init__.cpython-310.pyc +0 -0
- utils/__pycache__/__init__.cpython-311.pyc +0 -0
- utils/__pycache__/augmentations.cpython-310.pyc +0 -0
- utils/__pycache__/augmentations.cpython-311.pyc +0 -0
- utils/__pycache__/autoanchor.cpython-310.pyc +0 -0
- utils/__pycache__/autobatch.cpython-310.pyc +0 -0
- utils/__pycache__/callbacks.cpython-310.pyc +0 -0
- utils/__pycache__/dataloaders.cpython-310.pyc +0 -0
- utils/__pycache__/dataloaders.cpython-311.pyc +0 -0
- utils/__pycache__/downloads.cpython-310.pyc +0 -0
- utils/__pycache__/downloads.cpython-311.pyc +0 -0
- utils/__pycache__/general.cpython-310.pyc +0 -0
- utils/__pycache__/general.cpython-311.pyc +0 -0
- utils/__pycache__/lion.cpython-310.pyc +0 -0
- utils/__pycache__/lion.cpython-311.pyc +0 -0
- utils/__pycache__/loss_tal_dual.cpython-310.pyc +0 -0
- utils/__pycache__/metrics.cpython-310.pyc +0 -0
- utils/__pycache__/metrics.cpython-311.pyc +0 -0
- utils/__pycache__/plots.cpython-310.pyc +0 -0
- utils/__pycache__/plots.cpython-311.pyc +0 -0
- utils/__pycache__/torch_utils.cpython-310.pyc +0 -0
- utils/__pycache__/torch_utils.cpython-311.pyc +0 -0
- utils/activations.py +98 -0
- utils/augmentations.py +395 -0
- utils/autoanchor.py +164 -0
- utils/autobatch.py +67 -0
README.md
CHANGED
@@ -1,13 +1,81 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# YOLO9tr: Yolo9 with partial self attention
|
2 |
+
This is the repo for using yolov9 with partial self attention (PSA) \
|
3 |
+
This model was developed to be used in pavement damage detection based on YOLO9s Model.
|
4 |
+
### From paper
|
5 |
+
YOLO9tr: A Lightweight Model for Pavement Damage Detection Utilizing a Generalized Efficient Layer Aggregation Network and Attention Mechanism [Access](https://arxiv.org/abs/2406.11254)
|
6 |
+
|
7 |
+
## Authors
|
8 |
+
|
9 |
+
Authors: Dr. Sompote Youwai, Achitaphon Chaiyaphat and Pawarotorn Chaipetch
|
10 |
+
|
11 |
+
AI research Group \
|
12 |
+
Department of Civil Engineering\
|
13 |
+
King Mongkut's University of Technology Thonburi\
|
14 |
+
Thailand
|
15 |
+
|
16 |
+
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
<p align="center">
|
21 |
+
<img src="https://github.com/Sompote/YOLO9tr/assets/62241733/40d64fae-23ac-46a9-a62b-5f5eb99553a0" alt="Picture11223"/>
|
22 |
+
</p>
|
23 |
+
<p align="center">
|
24 |
+
<img src="https://github.com/Sompote/YOLO9tr/assets/62241733/851ad8f3-f92a-43af-a481-c7c83b6e6269" alt="Picture11"/>
|
25 |
+
</p>
|
26 |
+
<p align="center">
|
27 |
+
<img src="https://github.com/Sompote/YOLO9tr/assets/62241733/902aa180-73fd-422e-985f-28a09166f52f" alt="detect_result"/>
|
28 |
+
</p>
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
|
33 |
+
## Deployment
|
34 |
+
|
35 |
+
To deploy this project run
|
36 |
+
|
37 |
+
```bash
|
38 |
+
git clone https://github.com/Sompote/YOLO9tr
|
39 |
+
pip install -r requirements.txt
|
40 |
+
```
|
41 |
+
|
42 |
+
|
43 |
+
Reccomend to use weight for [YOLO9s](https://github.com/WongKinYiu/yolov9/releases/download/v0.1/yolov9-s.pt) as initial training
|
44 |
+
|
45 |
+
|
46 |
+
### Train with Single GPU
|
47 |
+
```bash
|
48 |
+
python train_dual.py --workers 8 --device 0 --batch 4 --data '/workspace/6400 images/data.yaml' --img 640 \
|
49 |
+
--cfg models/detect/yolov9tr.yaml --weights '../yolov9s' --name yolov9-tr --hyp hyp.scratch-high.yaml\
|
50 |
+
--min-items 0 --epochs 200 --close-mosaic 15
|
51 |
+
|
52 |
+
```
|
53 |
+
|
54 |
+
|
55 |
+
### Train with Dual GPU
|
56 |
+
```bash
|
57 |
+
torchrun --nproc_per_node 2 --master_port 9527 train_dual.py \
|
58 |
+
--workers 8 --device 0,1 --sync-bn --batch 30 --data '/workspace/road damage/data.yaml' \
|
59 |
+
--img 640 --cfg models/detect/yolov9tr.yaml --weights '../yolov9s' --name yolov9-c --hyp hyp.scratch-high.yaml \
|
60 |
+
--min-items 0 --epochs 200 --close-mosaic 15
|
61 |
+
```
|
62 |
+
|
63 |
+
|
64 |
+
|
65 |
+
### Evaluation
|
66 |
+
[YOLO9tr.pt](https://drive.google.com/file/d/1DtXXICCulTPN8DP4HbVLP3T3sk5BP5HI/view?usp=share_link)
|
67 |
+
```
|
68 |
+
python val_dual.py --data data/coco.yaml --img 640 --batch 32 --conf 0.001\
|
69 |
+
--iou 0.7 --device 0 --weights './yolov9tr.pt' \
|
70 |
+
--save-json --name yolov9_c_640_val
|
71 |
+
```
|
72 |
+
### Inference
|
73 |
+
```
|
74 |
+
python detect_dual.py --source './data/images/horses.jpg' --img 640 --device 0 \
|
75 |
+
--weights './yolov9tr.pt' --name yolov9_c_640_detect
|
76 |
+
```
|
77 |
+
The file format of data can be used the same as YOLOv8 in Roboflow
|
78 |
+
|
79 |
+
|
80 |
+
|
81 |
+
|
United_States_000062.jpg
ADDED
![]() |
United_States_000502.jpg
ADDED
![]() |
YOLO9_run.ipynb
ADDED
@@ -0,0 +1,1123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 1,
|
6 |
+
"metadata": {
|
7 |
+
"colab": {
|
8 |
+
"base_uri": "https://localhost:8080/"
|
9 |
+
},
|
10 |
+
"id": "Mw7RoF7eRJEG",
|
11 |
+
"outputId": "1aa46197-e525-4227-b78a-5068873ebd65"
|
12 |
+
},
|
13 |
+
"outputs": [
|
14 |
+
{
|
15 |
+
"name": "stdout",
|
16 |
+
"output_type": "stream",
|
17 |
+
"text": [
|
18 |
+
"Cloning into 'YOLO9_KMUTT'...\n",
|
19 |
+
"remote: Enumerating objects: 182, done.\u001b[K\n",
|
20 |
+
"remote: Counting objects: 100% (182/182), done.\u001b[K\n",
|
21 |
+
"remote: Compressing objects: 100% (132/132), done.\u001b[K\n",
|
22 |
+
"remote: Total 182 (delta 49), reused 179 (delta 46), pack-reused 0\u001b[K\n",
|
23 |
+
"Receiving objects: 100% (182/182), 2.27 MiB | 8.47 MiB/s, done.\n",
|
24 |
+
"Resolving deltas: 100% (49/49), done.\n"
|
25 |
+
]
|
26 |
+
}
|
27 |
+
],
|
28 |
+
"source": [
|
29 |
+
"!git clone https://github.com/Sompote/YOLO9_KMUTT"
|
30 |
+
]
|
31 |
+
},
|
32 |
+
{
|
33 |
+
"cell_type": "code",
|
34 |
+
"execution_count": 9,
|
35 |
+
"metadata": {
|
36 |
+
"colab": {
|
37 |
+
"base_uri": "https://localhost:8080/"
|
38 |
+
},
|
39 |
+
"id": "6KgJYQKfryz2",
|
40 |
+
"outputId": "f359b42d-f9d5-41ac-bbc6-d4c623672e92"
|
41 |
+
},
|
42 |
+
"outputs": [
|
43 |
+
{
|
44 |
+
"name": "stdout",
|
45 |
+
"output_type": "stream",
|
46 |
+
"text": [
|
47 |
+
"/workspace/YOLO9_KMUTT/yolov9\n"
|
48 |
+
]
|
49 |
+
}
|
50 |
+
],
|
51 |
+
"source": [
|
52 |
+
"%cd /workspace/YOLO9_KMUTT/yolov9/"
|
53 |
+
]
|
54 |
+
},
|
55 |
+
{
|
56 |
+
"cell_type": "code",
|
57 |
+
"execution_count": 10,
|
58 |
+
"metadata": {
|
59 |
+
"colab": {
|
60 |
+
"base_uri": "https://localhost:8080/"
|
61 |
+
},
|
62 |
+
"id": "AnohaOgqq5rQ",
|
63 |
+
"outputId": "d4a78b77-33f3-43dd-af6e-c1ef5e32a8f9"
|
64 |
+
},
|
65 |
+
"outputs": [
|
66 |
+
{
|
67 |
+
"name": "stdout",
|
68 |
+
"output_type": "stream",
|
69 |
+
"text": [
|
70 |
+
"Collecting gitpython (from -r requirements.txt (line 5))\n",
|
71 |
+
" Downloading GitPython-3.1.43-py3-none-any.whl.metadata (13 kB)\n",
|
72 |
+
"Requirement already satisfied: ipython in /opt/conda/lib/python3.10/site-packages (from -r requirements.txt (line 6)) (8.20.0)\n",
|
73 |
+
"Collecting matplotlib>=3.2.2 (from -r requirements.txt (line 7))\n",
|
74 |
+
" Downloading matplotlib-3.9.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (11 kB)\n",
|
75 |
+
"Requirement already satisfied: numpy>=1.18.5 in /opt/conda/lib/python3.10/site-packages (from -r requirements.txt (line 8)) (1.26.3)\n",
|
76 |
+
"Collecting opencv-python>=4.1.1 (from -r requirements.txt (line 9))\n",
|
77 |
+
" Downloading opencv_python-4.10.0.82-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (20 kB)\n",
|
78 |
+
"Requirement already satisfied: Pillow>=7.1.2 in /opt/conda/lib/python3.10/site-packages (from -r requirements.txt (line 10)) (10.0.1)\n",
|
79 |
+
"Requirement already satisfied: psutil in /opt/conda/lib/python3.10/site-packages (from -r requirements.txt (line 11)) (5.9.0)\n",
|
80 |
+
"Requirement already satisfied: PyYAML>=5.3.1 in /opt/conda/lib/python3.10/site-packages (from -r requirements.txt (line 12)) (6.0.1)\n",
|
81 |
+
"Requirement already satisfied: requests>=2.23.0 in /opt/conda/lib/python3.10/site-packages (from -r requirements.txt (line 13)) (2.31.0)\n",
|
82 |
+
"Collecting scipy>=1.4.1 (from -r requirements.txt (line 14))\n",
|
83 |
+
" Downloading scipy-1.13.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (60 kB)\n",
|
84 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m60.6/60.6 kB\u001b[0m \u001b[31m9.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
85 |
+
"\u001b[?25hCollecting thop>=0.1.1 (from -r requirements.txt (line 15))\n",
|
86 |
+
" Downloading thop-0.1.1.post2209072238-py3-none-any.whl.metadata (2.7 kB)\n",
|
87 |
+
"Requirement already satisfied: torch>=1.7.0 in /opt/conda/lib/python3.10/site-packages (from -r requirements.txt (line 16)) (2.2.0)\n",
|
88 |
+
"Requirement already satisfied: torchvision>=0.8.1 in /opt/conda/lib/python3.10/site-packages (from -r requirements.txt (line 17)) (0.17.0)\n",
|
89 |
+
"Requirement already satisfied: tqdm>=4.64.0 in /opt/conda/lib/python3.10/site-packages (from -r requirements.txt (line 18)) (4.65.0)\n",
|
90 |
+
"Collecting tensorboard>=2.4.1 (from -r requirements.txt (line 22))\n",
|
91 |
+
" Downloading tensorboard-2.17.0-py3-none-any.whl.metadata (1.6 kB)\n",
|
92 |
+
"Collecting pandas>=1.1.4 (from -r requirements.txt (line 27))\n",
|
93 |
+
" Downloading pandas-2.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (19 kB)\n",
|
94 |
+
"Collecting seaborn>=0.11.0 (from -r requirements.txt (line 28))\n",
|
95 |
+
" Downloading seaborn-0.13.2-py3-none-any.whl.metadata (5.4 kB)\n",
|
96 |
+
"Collecting albumentations>=1.0.3 (from -r requirements.txt (line 46))\n",
|
97 |
+
" Downloading albumentations-1.4.8-py3-none-any.whl.metadata (37 kB)\n",
|
98 |
+
"Collecting pycocotools>=2.0 (from -r requirements.txt (line 47))\n",
|
99 |
+
" Downloading pycocotools-2.0.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (1.1 kB)\n",
|
100 |
+
"Collecting gitdb<5,>=4.0.1 (from gitpython->-r requirements.txt (line 5))\n",
|
101 |
+
" Downloading gitdb-4.0.11-py3-none-any.whl.metadata (1.2 kB)\n",
|
102 |
+
"Requirement already satisfied: decorator in /opt/conda/lib/python3.10/site-packages (from ipython->-r requirements.txt (line 6)) (5.1.1)\n",
|
103 |
+
"Requirement already satisfied: jedi>=0.16 in /opt/conda/lib/python3.10/site-packages (from ipython->-r requirements.txt (line 6)) (0.18.1)\n",
|
104 |
+
"Requirement already satisfied: matplotlib-inline in /opt/conda/lib/python3.10/site-packages (from ipython->-r requirements.txt (line 6)) (0.1.6)\n",
|
105 |
+
"Requirement already satisfied: prompt-toolkit<3.1.0,>=3.0.41 in /opt/conda/lib/python3.10/site-packages (from ipython->-r requirements.txt (line 6)) (3.0.43)\n",
|
106 |
+
"Requirement already satisfied: pygments>=2.4.0 in /opt/conda/lib/python3.10/site-packages (from ipython->-r requirements.txt (line 6)) (2.15.1)\n",
|
107 |
+
"Requirement already satisfied: stack-data in /opt/conda/lib/python3.10/site-packages (from ipython->-r requirements.txt (line 6)) (0.2.0)\n",
|
108 |
+
"Requirement already satisfied: traitlets>=5 in /opt/conda/lib/python3.10/site-packages (from ipython->-r requirements.txt (line 6)) (5.7.1)\n",
|
109 |
+
"Requirement already satisfied: exceptiongroup in /opt/conda/lib/python3.10/site-packages (from ipython->-r requirements.txt (line 6)) (1.2.0)\n",
|
110 |
+
"Requirement already satisfied: pexpect>4.3 in /opt/conda/lib/python3.10/site-packages (from ipython->-r requirements.txt (line 6)) (4.8.0)\n",
|
111 |
+
"Collecting contourpy>=1.0.1 (from matplotlib>=3.2.2->-r requirements.txt (line 7))\n",
|
112 |
+
" Downloading contourpy-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (5.8 kB)\n",
|
113 |
+
"Collecting cycler>=0.10 (from matplotlib>=3.2.2->-r requirements.txt (line 7))\n",
|
114 |
+
" Downloading cycler-0.12.1-py3-none-any.whl.metadata (3.8 kB)\n",
|
115 |
+
"Collecting fonttools>=4.22.0 (from matplotlib>=3.2.2->-r requirements.txt (line 7))\n",
|
116 |
+
" Downloading fonttools-4.53.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (162 kB)\n",
|
117 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m162.2/162.2 kB\u001b[0m \u001b[31m22.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
118 |
+
"\u001b[?25hCollecting kiwisolver>=1.3.1 (from matplotlib>=3.2.2->-r requirements.txt (line 7))\n",
|
119 |
+
" Downloading kiwisolver-1.4.5-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.metadata (6.4 kB)\n",
|
120 |
+
"Requirement already satisfied: packaging>=20.0 in /opt/conda/lib/python3.10/site-packages (from matplotlib>=3.2.2->-r requirements.txt (line 7)) (23.1)\n",
|
121 |
+
"Collecting pyparsing>=2.3.1 (from matplotlib>=3.2.2->-r requirements.txt (line 7))\n",
|
122 |
+
" Downloading pyparsing-3.1.2-py3-none-any.whl.metadata (5.1 kB)\n",
|
123 |
+
"Requirement already satisfied: python-dateutil>=2.7 in /opt/conda/lib/python3.10/site-packages (from matplotlib>=3.2.2->-r requirements.txt (line 7)) (2.9.0.post0)\n",
|
124 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/conda/lib/python3.10/site-packages (from requests>=2.23.0->-r requirements.txt (line 13)) (2.0.4)\n",
|
125 |
+
"Requirement already satisfied: idna<4,>=2.5 in /opt/conda/lib/python3.10/site-packages (from requests>=2.23.0->-r requirements.txt (line 13)) (3.4)\n",
|
126 |
+
"Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/conda/lib/python3.10/site-packages (from requests>=2.23.0->-r requirements.txt (line 13)) (1.26.18)\n",
|
127 |
+
"Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/lib/python3.10/site-packages (from requests>=2.23.0->-r requirements.txt (line 13)) (2023.11.17)\n",
|
128 |
+
"Requirement already satisfied: filelock in /opt/conda/lib/python3.10/site-packages (from torch>=1.7.0->-r requirements.txt (line 16)) (3.13.1)\n",
|
129 |
+
"Requirement already satisfied: typing-extensions>=4.8.0 in /opt/conda/lib/python3.10/site-packages (from torch>=1.7.0->-r requirements.txt (line 16)) (4.9.0)\n",
|
130 |
+
"Requirement already satisfied: sympy in /opt/conda/lib/python3.10/site-packages (from torch>=1.7.0->-r requirements.txt (line 16)) (1.12)\n",
|
131 |
+
"Requirement already satisfied: networkx in /opt/conda/lib/python3.10/site-packages (from torch>=1.7.0->-r requirements.txt (line 16)) (3.1)\n",
|
132 |
+
"Requirement already satisfied: jinja2 in /opt/conda/lib/python3.10/site-packages (from torch>=1.7.0->-r requirements.txt (line 16)) (3.1.2)\n",
|
133 |
+
"Requirement already satisfied: fsspec in /opt/conda/lib/python3.10/site-packages (from torch>=1.7.0->-r requirements.txt (line 16)) (2023.12.2)\n",
|
134 |
+
"Collecting absl-py>=0.4 (from tensorboard>=2.4.1->-r requirements.txt (line 22))\n",
|
135 |
+
" Downloading absl_py-2.1.0-py3-none-any.whl.metadata (2.3 kB)\n",
|
136 |
+
"Collecting grpcio>=1.48.2 (from tensorboard>=2.4.1->-r requirements.txt (line 22))\n",
|
137 |
+
" Downloading grpcio-1.64.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (3.3 kB)\n",
|
138 |
+
"Collecting markdown>=2.6.8 (from tensorboard>=2.4.1->-r requirements.txt (line 22))\n",
|
139 |
+
" Downloading Markdown-3.6-py3-none-any.whl.metadata (7.0 kB)\n",
|
140 |
+
"Collecting protobuf!=4.24.0,<5.0.0,>=3.19.6 (from tensorboard>=2.4.1->-r requirements.txt (line 22))\n",
|
141 |
+
" Downloading protobuf-4.25.3-cp37-abi3-manylinux2014_x86_64.whl.metadata (541 bytes)\n",
|
142 |
+
"Requirement already satisfied: setuptools>=41.0.0 in /opt/conda/lib/python3.10/site-packages (from tensorboard>=2.4.1->-r requirements.txt (line 22)) (68.2.2)\n",
|
143 |
+
"Requirement already satisfied: six>1.9 in /opt/conda/lib/python3.10/site-packages (from tensorboard>=2.4.1->-r requirements.txt (line 22)) (1.16.0)\n",
|
144 |
+
"Collecting tensorboard-data-server<0.8.0,>=0.7.0 (from tensorboard>=2.4.1->-r requirements.txt (line 22))\n",
|
145 |
+
" Downloading tensorboard_data_server-0.7.2-py3-none-manylinux_2_31_x86_64.whl.metadata (1.1 kB)\n",
|
146 |
+
"Collecting werkzeug>=1.0.1 (from tensorboard>=2.4.1->-r requirements.txt (line 22))\n",
|
147 |
+
" Downloading werkzeug-3.0.3-py3-none-any.whl.metadata (3.7 kB)\n",
|
148 |
+
"Requirement already satisfied: pytz>=2020.1 in /opt/conda/lib/python3.10/site-packages (from pandas>=1.1.4->-r requirements.txt (line 27)) (2023.3.post1)\n",
|
149 |
+
"Collecting tzdata>=2022.7 (from pandas>=1.1.4->-r requirements.txt (line 27))\n",
|
150 |
+
" Downloading tzdata-2024.1-py2.py3-none-any.whl.metadata (1.4 kB)\n",
|
151 |
+
"Collecting scikit-image>=0.21.0 (from albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
152 |
+
" Downloading scikit_image-0.23.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (14 kB)\n",
|
153 |
+
"Collecting scikit-learn>=1.3.2 (from albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
154 |
+
" Downloading scikit_learn-1.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (11 kB)\n",
|
155 |
+
"Collecting pydantic>=2.7.0 (from albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
156 |
+
" Downloading pydantic-2.7.3-py3-none-any.whl.metadata (108 kB)\n",
|
157 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m109.0/109.0 kB\u001b[0m \u001b[31m31.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
158 |
+
"\u001b[?25hCollecting albucore>=0.0.4 (from albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
159 |
+
" Downloading albucore-0.0.9-py3-none-any.whl.metadata (3.1 kB)\n",
|
160 |
+
"Collecting opencv-python-headless>=4.9.0.80 (from albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
161 |
+
" Downloading opencv_python_headless-4.10.0.82-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (20 kB)\n",
|
162 |
+
"Requirement already satisfied: tomli>=2.0.1 in /opt/conda/lib/python3.10/site-packages (from albucore>=0.0.4->albumentations>=1.0.3->-r requirements.txt (line 46)) (2.0.1)\n",
|
163 |
+
"Collecting smmap<6,>=3.0.1 (from gitdb<5,>=4.0.1->gitpython->-r requirements.txt (line 5))\n",
|
164 |
+
" Downloading smmap-5.0.1-py3-none-any.whl.metadata (4.3 kB)\n",
|
165 |
+
"Requirement already satisfied: parso<0.9.0,>=0.8.0 in /opt/conda/lib/python3.10/site-packages (from jedi>=0.16->ipython->-r requirements.txt (line 6)) (0.8.3)\n",
|
166 |
+
"Requirement already satisfied: ptyprocess>=0.5 in /opt/conda/lib/python3.10/site-packages (from pexpect>4.3->ipython->-r requirements.txt (line 6)) (0.7.0)\n",
|
167 |
+
"Requirement already satisfied: wcwidth in /opt/conda/lib/python3.10/site-packages (from prompt-toolkit<3.1.0,>=3.0.41->ipython->-r requirements.txt (line 6)) (0.2.5)\n",
|
168 |
+
"Collecting annotated-types>=0.4.0 (from pydantic>=2.7.0->albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
169 |
+
" Downloading annotated_types-0.7.0-py3-none-any.whl.metadata (15 kB)\n",
|
170 |
+
"Collecting pydantic-core==2.18.4 (from pydantic>=2.7.0->albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
171 |
+
" Downloading pydantic_core-2.18.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (6.5 kB)\n",
|
172 |
+
"Collecting imageio>=2.33 (from scikit-image>=0.21.0->albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
173 |
+
" Downloading imageio-2.34.1-py3-none-any.whl.metadata (4.9 kB)\n",
|
174 |
+
"Collecting tifffile>=2022.8.12 (from scikit-image>=0.21.0->albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
175 |
+
" Downloading tifffile-2024.5.22-py3-none-any.whl.metadata (30 kB)\n",
|
176 |
+
"Collecting lazy-loader>=0.4 (from scikit-image>=0.21.0->albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
177 |
+
" Downloading lazy_loader-0.4-py3-none-any.whl.metadata (7.6 kB)\n",
|
178 |
+
"Collecting joblib>=1.2.0 (from scikit-learn>=1.3.2->albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
179 |
+
" Downloading joblib-1.4.2-py3-none-any.whl.metadata (5.4 kB)\n",
|
180 |
+
"Collecting threadpoolctl>=3.1.0 (from scikit-learn>=1.3.2->albumentations>=1.0.3->-r requirements.txt (line 46))\n",
|
181 |
+
" Downloading threadpoolctl-3.5.0-py3-none-any.whl.metadata (13 kB)\n",
|
182 |
+
"Requirement already satisfied: MarkupSafe>=2.1.1 in /opt/conda/lib/python3.10/site-packages (from werkzeug>=1.0.1->tensorboard>=2.4.1->-r requirements.txt (line 22)) (2.1.3)\n",
|
183 |
+
"Requirement already satisfied: executing in /opt/conda/lib/python3.10/site-packages (from stack-data->ipython->-r requirements.txt (line 6)) (0.8.3)\n",
|
184 |
+
"Requirement already satisfied: asttokens in /opt/conda/lib/python3.10/site-packages (from stack-data->ipython->-r requirements.txt (line 6)) (2.0.5)\n",
|
185 |
+
"Requirement already satisfied: pure-eval in /opt/conda/lib/python3.10/site-packages (from stack-data->ipython->-r requirements.txt (line 6)) (0.2.2)\n",
|
186 |
+
"Requirement already satisfied: mpmath>=0.19 in /opt/conda/lib/python3.10/site-packages (from sympy->torch>=1.7.0->-r requirements.txt (line 16)) (1.3.0)\n",
|
187 |
+
"Downloading GitPython-3.1.43-py3-none-any.whl (207 kB)\n",
|
188 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m207.3/207.3 kB\u001b[0m \u001b[31m35.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
189 |
+
"\u001b[?25hDownloading matplotlib-3.9.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (8.3 MB)\n",
|
190 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m8.3/8.3 MB\u001b[0m \u001b[31m127.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
|
191 |
+
"\u001b[?25hDownloading opencv_python-4.10.0.82-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (62.5 MB)\n",
|
192 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.5/62.5 MB\u001b[0m \u001b[31m89.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
|
193 |
+
"\u001b[?25hDownloading scipy-1.13.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (38.6 MB)\n",
|
194 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m38.6/38.6 MB\u001b[0m \u001b[31m100.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m00:01\u001b[0m\n",
|
195 |
+
"\u001b[?25hDownloading thop-0.1.1.post2209072238-py3-none-any.whl (15 kB)\n",
|
196 |
+
"Downloading tensorboard-2.17.0-py3-none-any.whl (5.5 MB)\n",
|
197 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.5/5.5 MB\u001b[0m \u001b[31m148.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m\n",
|
198 |
+
"\u001b[?25hDownloading pandas-2.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.0 MB)\n",
|
199 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.0/13.0 MB\u001b[0m \u001b[31m151.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m0:01\u001b[0m\n",
|
200 |
+
"\u001b[?25hDownloading seaborn-0.13.2-py3-none-any.whl (294 kB)\n",
|
201 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m294.9/294.9 kB\u001b[0m \u001b[31m50.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
202 |
+
"\u001b[?25hDownloading albumentations-1.4.8-py3-none-any.whl (156 kB)\n",
|
203 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m156.8/156.8 kB\u001b[0m \u001b[31m35.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
204 |
+
"\u001b[?25hDownloading pycocotools-2.0.7-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (426 kB)\n",
|
205 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m426.2/426.2 kB\u001b[0m \u001b[31m61.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
206 |
+
"\u001b[?25hDownloading absl_py-2.1.0-py3-none-any.whl (133 kB)\n",
|
207 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m133.7/133.7 kB\u001b[0m \u001b[31m33.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
208 |
+
"\u001b[?25hDownloading albucore-0.0.9-py3-none-any.whl (7.9 kB)\n",
|
209 |
+
"Downloading contourpy-1.2.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (305 kB)\n",
|
210 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m305.2/305.2 kB\u001b[0m \u001b[31m50.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
211 |
+
"\u001b[?25hDownloading cycler-0.12.1-py3-none-any.whl (8.3 kB)\n",
|
212 |
+
"Downloading fonttools-4.53.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.6 MB)\n",
|
213 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m4.6/4.6 MB\u001b[0m \u001b[31m141.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
214 |
+
"\u001b[?25hDownloading gitdb-4.0.11-py3-none-any.whl (62 kB)\n",
|
215 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m62.7/62.7 kB\u001b[0m \u001b[31m13.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
216 |
+
"\u001b[?25hDownloading grpcio-1.64.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (5.6 MB)\n",
|
217 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m5.6/5.6 MB\u001b[0m \u001b[31m140.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m00:01\u001b[0m\n",
|
218 |
+
"\u001b[?25hDownloading kiwisolver-1.4.5-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (1.6 MB)\n",
|
219 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.6/1.6 MB\u001b[0m \u001b[31m115.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
220 |
+
"\u001b[?25hDownloading Markdown-3.6-py3-none-any.whl (105 kB)\n",
|
221 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m105.4/105.4 kB\u001b[0m \u001b[31m20.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
222 |
+
"\u001b[?25hDownloading opencv_python_headless-4.10.0.82-cp37-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (49.9 MB)\n",
|
223 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m49.9/49.9 MB\u001b[0m \u001b[31m93.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m:00:01\u001b[0m00:01\u001b[0m\n",
|
224 |
+
"\u001b[?25hDownloading protobuf-4.25.3-cp37-abi3-manylinux2014_x86_64.whl (294 kB)\n",
|
225 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m294.6/294.6 kB\u001b[0m \u001b[31m49.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
226 |
+
"\u001b[?25hDownloading pydantic-2.7.3-py3-none-any.whl (409 kB)\n",
|
227 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m409.6/409.6 kB\u001b[0m \u001b[31m70.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
228 |
+
"\u001b[?25hDownloading pydantic_core-2.18.4-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.0 MB)\n",
|
229 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.0/2.0 MB\u001b[0m \u001b[31m117.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
230 |
+
"\u001b[?25hDownloading pyparsing-3.1.2-py3-none-any.whl (103 kB)\n",
|
231 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m103.2/103.2 kB\u001b[0m \u001b[31m21.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
232 |
+
"\u001b[?25hDownloading scikit_image-0.23.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (14.7 MB)\n",
|
233 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m14.7/14.7 MB\u001b[0m \u001b[31m145.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
|
234 |
+
"\u001b[?25hDownloading scikit_learn-1.5.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (13.3 MB)\n",
|
235 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m13.3/13.3 MB\u001b[0m \u001b[31m137.8 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m \u001b[36m0:00:01\u001b[0m\n",
|
236 |
+
"\u001b[?25hDownloading tensorboard_data_server-0.7.2-py3-none-manylinux_2_31_x86_64.whl (6.6 MB)\n",
|
237 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m6.6/6.6 MB\u001b[0m \u001b[31m96.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0mta \u001b[36m0:00:01\u001b[0m\n",
|
238 |
+
"\u001b[?25hDownloading tzdata-2024.1-py2.py3-none-any.whl (345 kB)\n",
|
239 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m345.4/345.4 kB\u001b[0m \u001b[31m51.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
240 |
+
"\u001b[?25hDownloading werkzeug-3.0.3-py3-none-any.whl (227 kB)\n",
|
241 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m227.3/227.3 kB\u001b[0m \u001b[31m42.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
242 |
+
"\u001b[?25hDownloading annotated_types-0.7.0-py3-none-any.whl (13 kB)\n",
|
243 |
+
"Downloading imageio-2.34.1-py3-none-any.whl (313 kB)\n",
|
244 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m313.5/313.5 kB\u001b[0m \u001b[31m56.0 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
245 |
+
"\u001b[?25hDownloading joblib-1.4.2-py3-none-any.whl (301 kB)\n",
|
246 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m301.8/301.8 kB\u001b[0m \u001b[31m49.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
247 |
+
"\u001b[?25hDownloading lazy_loader-0.4-py3-none-any.whl (12 kB)\n",
|
248 |
+
"Downloading smmap-5.0.1-py3-none-any.whl (24 kB)\n",
|
249 |
+
"Downloading threadpoolctl-3.5.0-py3-none-any.whl (18 kB)\n",
|
250 |
+
"Downloading tifffile-2024.5.22-py3-none-any.whl (225 kB)\n",
|
251 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m225.5/225.5 kB\u001b[0m \u001b[31m49.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
|
252 |
+
"\u001b[?25hInstalling collected packages: werkzeug, tzdata, tifffile, threadpoolctl, tensorboard-data-server, smmap, scipy, pyparsing, pydantic-core, protobuf, opencv-python-headless, opencv-python, markdown, lazy-loader, kiwisolver, joblib, imageio, grpcio, fonttools, cycler, contourpy, annotated-types, absl-py, tensorboard, scikit-learn, scikit-image, pydantic, pandas, matplotlib, gitdb, albucore, thop, seaborn, pycocotools, gitpython, albumentations\n",
|
253 |
+
"Successfully installed absl-py-2.1.0 albucore-0.0.9 albumentations-1.4.8 annotated-types-0.7.0 contourpy-1.2.1 cycler-0.12.1 fonttools-4.53.0 gitdb-4.0.11 gitpython-3.1.43 grpcio-1.64.1 imageio-2.34.1 joblib-1.4.2 kiwisolver-1.4.5 lazy-loader-0.4 markdown-3.6 matplotlib-3.9.0 opencv-python-4.10.0.82 opencv-python-headless-4.10.0.82 pandas-2.2.2 protobuf-4.25.3 pycocotools-2.0.7 pydantic-2.7.3 pydantic-core-2.18.4 pyparsing-3.1.2 scikit-image-0.23.2 scikit-learn-1.5.0 scipy-1.13.1 seaborn-0.13.2 smmap-5.0.1 tensorboard-2.17.0 tensorboard-data-server-0.7.2 thop-0.1.1.post2209072238 threadpoolctl-3.5.0 tifffile-2024.5.22 tzdata-2024.1 werkzeug-3.0.3\n",
|
254 |
+
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
|
255 |
+
"\u001b[0m"
|
256 |
+
]
|
257 |
+
}
|
258 |
+
],
|
259 |
+
"source": [
|
260 |
+
"!pip install -r requirements.txt"
|
261 |
+
]
|
262 |
+
},
|
263 |
+
{
|
264 |
+
"cell_type": "code",
|
265 |
+
"execution_count": 11,
|
266 |
+
"metadata": {},
|
267 |
+
"outputs": [
|
268 |
+
{
|
269 |
+
"name": "stdout",
|
270 |
+
"output_type": "stream",
|
271 |
+
"text": [
|
272 |
+
"Hit:1 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64 InRelease\n",
|
273 |
+
"Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease \n",
|
274 |
+
"Hit:3 http://security.ubuntu.com/ubuntu jammy-security InRelease\n",
|
275 |
+
"Hit:4 http://archive.ubuntu.com/ubuntu jammy-updates InRelease\n",
|
276 |
+
"Hit:5 http://archive.ubuntu.com/ubuntu jammy-backports InRelease\n",
|
277 |
+
"Reading package lists... Done\n",
|
278 |
+
"Reading package lists... Done\n",
|
279 |
+
"Building dependency tree... Done\n",
|
280 |
+
"Reading state information... Done\n",
|
281 |
+
"The following additional packages will be installed:\n",
|
282 |
+
" libdrm-amdgpu1 libdrm-common libdrm-intel1 libdrm-nouveau2 libdrm-radeon1\n",
|
283 |
+
" libdrm2 libgl1 libgl1-amber-dri libgl1-mesa-dri libglapi-mesa libglvnd0\n",
|
284 |
+
" libglx-mesa0 libglx0 libllvm15 libpciaccess0 libsensors-config libsensors5\n",
|
285 |
+
" libx11-6 libx11-data libx11-xcb1 libxau6 libxcb-dri2-0 libxcb-dri3-0\n",
|
286 |
+
" libxcb-glx0 libxcb-present0 libxcb-randr0 libxcb-shm0 libxcb-sync1\n",
|
287 |
+
" libxcb-xfixes0 libxcb1 libxdmcp6 libxext6 libxfixes3 libxshmfence1\n",
|
288 |
+
" libxxf86vm1\n",
|
289 |
+
"Suggested packages:\n",
|
290 |
+
" pciutils lm-sensors\n",
|
291 |
+
"The following NEW packages will be installed:\n",
|
292 |
+
" libdrm-amdgpu1 libdrm-common libdrm-intel1 libdrm-nouveau2 libdrm-radeon1\n",
|
293 |
+
" libdrm2 libgl1 libgl1-amber-dri libgl1-mesa-dri libgl1-mesa-glx\n",
|
294 |
+
" libglapi-mesa libglvnd0 libglx-mesa0 libglx0 libllvm15 libpciaccess0\n",
|
295 |
+
" libsensors-config libsensors5 libx11-6 libx11-data libx11-xcb1 libxau6\n",
|
296 |
+
" libxcb-dri2-0 libxcb-dri3-0 libxcb-glx0 libxcb-present0 libxcb-randr0\n",
|
297 |
+
" libxcb-shm0 libxcb-sync1 libxcb-xfixes0 libxcb1 libxdmcp6 libxext6\n",
|
298 |
+
" libxfixes3 libxshmfence1 libxxf86vm1\n",
|
299 |
+
"0 upgraded, 36 newly installed, 0 to remove and 56 not upgraded.\n",
|
300 |
+
"Need to get 40.1 MB of archives.\n",
|
301 |
+
"After this operation, 173 MB of additional disk space will be used.\n",
|
302 |
+
"Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libdrm-common all 2.4.113-2~ubuntu0.22.04.1 [5450 B]\n",
|
303 |
+
"Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libdrm2 amd64 2.4.113-2~ubuntu0.22.04.1 [38.1 kB]\n",
|
304 |
+
"Get:3 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxau6 amd64 1:1.0.9-1build5 [7634 B]\n",
|
305 |
+
"Get:4 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxdmcp6 amd64 1:1.1.3-0ubuntu5 [10.9 kB]\n",
|
306 |
+
"Get:5 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxcb1 amd64 1.14-3ubuntu3 [49.0 kB]\n",
|
307 |
+
"Get:6 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libx11-data all 2:1.7.5-1ubuntu0.3 [120 kB]\n",
|
308 |
+
"Get:7 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libx11-6 amd64 2:1.7.5-1ubuntu0.3 [667 kB]\n",
|
309 |
+
"Get:8 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxext6 amd64 2:1.3.4-1build1 [31.8 kB]\n",
|
310 |
+
"Get:9 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libdrm-amdgpu1 amd64 2.4.113-2~ubuntu0.22.04.1 [19.9 kB]\n",
|
311 |
+
"Get:10 http://archive.ubuntu.com/ubuntu jammy/main amd64 libpciaccess0 amd64 0.16-3 [19.1 kB]\n",
|
312 |
+
"Get:11 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libdrm-intel1 amd64 2.4.113-2~ubuntu0.22.04.1 [66.7 kB]\n",
|
313 |
+
"Get:12 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libdrm-nouveau2 amd64 2.4.113-2~ubuntu0.22.04.1 [17.5 kB]\n",
|
314 |
+
"Get:13 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libdrm-radeon1 amd64 2.4.113-2~ubuntu0.22.04.1 [21.6 kB]\n",
|
315 |
+
"Get:14 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libglapi-mesa amd64 23.2.1-1ubuntu3.1~22.04.2 [37.1 kB]\n",
|
316 |
+
"Get:15 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libgl1-amber-dri amd64 21.3.9-0ubuntu1~22.04.1 [4218 kB]\n",
|
317 |
+
"Get:16 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libllvm15 amd64 1:15.0.7-0ubuntu0.22.04.3 [25.4 MB]\n",
|
318 |
+
"Get:17 http://archive.ubuntu.com/ubuntu jammy/main amd64 libsensors-config all 1:3.6.0-7ubuntu1 [5274 B]\n",
|
319 |
+
"Get:18 http://archive.ubuntu.com/ubuntu jammy/main amd64 libsensors5 amd64 1:3.6.0-7ubuntu1 [26.3 kB]\n",
|
320 |
+
"Get:19 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxcb-dri3-0 amd64 1.14-3ubuntu3 [6968 B]\n",
|
321 |
+
"Get:20 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libgl1-mesa-dri amd64 23.2.1-1ubuntu3.1~22.04.2 [8860 kB]\n",
|
322 |
+
"Get:21 http://archive.ubuntu.com/ubuntu jammy/main amd64 libglvnd0 amd64 1.4.0-1 [73.6 kB]\n",
|
323 |
+
"Get:22 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libx11-xcb1 amd64 2:1.7.5-1ubuntu0.3 [7802 B]\n",
|
324 |
+
"Get:23 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxcb-dri2-0 amd64 1.14-3ubuntu3 [7206 B]\n",
|
325 |
+
"Get:24 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxcb-glx0 amd64 1.14-3ubuntu3 [25.9 kB]\n",
|
326 |
+
"Get:25 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxcb-present0 amd64 1.14-3ubuntu3 [5734 B]\n",
|
327 |
+
"Get:26 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxcb-randr0 amd64 1.14-3ubuntu3 [18.3 kB]\n",
|
328 |
+
"Get:27 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxcb-shm0 amd64 1.14-3ubuntu3 [5780 B]\n",
|
329 |
+
"Get:28 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxcb-sync1 amd64 1.14-3ubuntu3 [9416 B]\n",
|
330 |
+
"Get:29 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxcb-xfixes0 amd64 1.14-3ubuntu3 [9996 B]\n",
|
331 |
+
"Get:30 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxfixes3 amd64 1:6.0.0-1 [11.7 kB]\n",
|
332 |
+
"Get:31 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxshmfence1 amd64 1.3-1build4 [5394 B]\n",
|
333 |
+
"Get:32 http://archive.ubuntu.com/ubuntu jammy/main amd64 libxxf86vm1 amd64 1:1.1.4-1build3 [10.4 kB]\n",
|
334 |
+
"Get:33 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libglx-mesa0 amd64 23.2.1-1ubuntu3.1~22.04.2 [158 kB]\n",
|
335 |
+
"Get:34 http://archive.ubuntu.com/ubuntu jammy/main amd64 libglx0 amd64 1.4.0-1 [41.0 kB]\n",
|
336 |
+
"Get:35 http://archive.ubuntu.com/ubuntu jammy/main amd64 libgl1 amd64 1.4.0-1 [110 kB]\n",
|
337 |
+
"Get:36 http://archive.ubuntu.com/ubuntu jammy-updates/universe amd64 libgl1-mesa-glx amd64 23.0.4-0ubuntu1~22.04.1 [5584 B]\n",
|
338 |
+
"Fetched 40.1 MB in 3s (14.7 MB/s)\n",
|
339 |
+
"debconf: delaying package configuration, since apt-utils is not installed\n",
|
340 |
+
"Selecting previously unselected package libdrm-common.\n",
|
341 |
+
"(Reading database ... 22469 files and directories currently installed.)\n",
|
342 |
+
"Preparing to unpack .../00-libdrm-common_2.4.113-2~ubuntu0.22.04.1_all.deb ...\n",
|
343 |
+
"Unpacking libdrm-common (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
344 |
+
"Selecting previously unselected package libdrm2:amd64.\n",
|
345 |
+
"Preparing to unpack .../01-libdrm2_2.4.113-2~ubuntu0.22.04.1_amd64.deb ...\n",
|
346 |
+
"Unpacking libdrm2:amd64 (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
347 |
+
"Selecting previously unselected package libxau6:amd64.\n",
|
348 |
+
"Preparing to unpack .../02-libxau6_1%3a1.0.9-1build5_amd64.deb ...\n",
|
349 |
+
"Unpacking libxau6:amd64 (1:1.0.9-1build5) ...\n",
|
350 |
+
"Selecting previously unselected package libxdmcp6:amd64.\n",
|
351 |
+
"Preparing to unpack .../03-libxdmcp6_1%3a1.1.3-0ubuntu5_amd64.deb ...\n",
|
352 |
+
"Unpacking libxdmcp6:amd64 (1:1.1.3-0ubuntu5) ...\n",
|
353 |
+
"Selecting previously unselected package libxcb1:amd64.\n",
|
354 |
+
"Preparing to unpack .../04-libxcb1_1.14-3ubuntu3_amd64.deb ...\n",
|
355 |
+
"Unpacking libxcb1:amd64 (1.14-3ubuntu3) ...\n",
|
356 |
+
"Selecting previously unselected package libx11-data.\n",
|
357 |
+
"Preparing to unpack .../05-libx11-data_2%3a1.7.5-1ubuntu0.3_all.deb ...\n",
|
358 |
+
"Unpacking libx11-data (2:1.7.5-1ubuntu0.3) ...\n",
|
359 |
+
"Selecting previously unselected package libx11-6:amd64.\n",
|
360 |
+
"Preparing to unpack .../06-libx11-6_2%3a1.7.5-1ubuntu0.3_amd64.deb ...\n",
|
361 |
+
"Unpacking libx11-6:amd64 (2:1.7.5-1ubuntu0.3) ...\n",
|
362 |
+
"Selecting previously unselected package libxext6:amd64.\n",
|
363 |
+
"Preparing to unpack .../07-libxext6_2%3a1.3.4-1build1_amd64.deb ...\n",
|
364 |
+
"Unpacking libxext6:amd64 (2:1.3.4-1build1) ...\n",
|
365 |
+
"Selecting previously unselected package libdrm-amdgpu1:amd64.\n",
|
366 |
+
"Preparing to unpack .../08-libdrm-amdgpu1_2.4.113-2~ubuntu0.22.04.1_amd64.deb ...\n",
|
367 |
+
"Unpacking libdrm-amdgpu1:amd64 (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
368 |
+
"Selecting previously unselected package libpciaccess0:amd64.\n",
|
369 |
+
"Preparing to unpack .../09-libpciaccess0_0.16-3_amd64.deb ...\n",
|
370 |
+
"Unpacking libpciaccess0:amd64 (0.16-3) ...\n",
|
371 |
+
"Selecting previously unselected package libdrm-intel1:amd64.\n",
|
372 |
+
"Preparing to unpack .../10-libdrm-intel1_2.4.113-2~ubuntu0.22.04.1_amd64.deb ...\n",
|
373 |
+
"Unpacking libdrm-intel1:amd64 (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
374 |
+
"Selecting previously unselected package libdrm-nouveau2:amd64.\n",
|
375 |
+
"Preparing to unpack .../11-libdrm-nouveau2_2.4.113-2~ubuntu0.22.04.1_amd64.deb ...\n",
|
376 |
+
"Unpacking libdrm-nouveau2:amd64 (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
377 |
+
"Selecting previously unselected package libdrm-radeon1:amd64.\n",
|
378 |
+
"Preparing to unpack .../12-libdrm-radeon1_2.4.113-2~ubuntu0.22.04.1_amd64.deb ...\n",
|
379 |
+
"Unpacking libdrm-radeon1:amd64 (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
380 |
+
"Selecting previously unselected package libglapi-mesa:amd64.\n",
|
381 |
+
"Preparing to unpack .../13-libglapi-mesa_23.2.1-1ubuntu3.1~22.04.2_amd64.deb ...\n",
|
382 |
+
"Unpacking libglapi-mesa:amd64 (23.2.1-1ubuntu3.1~22.04.2) ...\n",
|
383 |
+
"Selecting previously unselected package libgl1-amber-dri:amd64.\n",
|
384 |
+
"Preparing to unpack .../14-libgl1-amber-dri_21.3.9-0ubuntu1~22.04.1_amd64.deb ...\n",
|
385 |
+
"Unpacking libgl1-amber-dri:amd64 (21.3.9-0ubuntu1~22.04.1) ...\n",
|
386 |
+
"Selecting previously unselected package libllvm15:amd64.\n",
|
387 |
+
"Preparing to unpack .../15-libllvm15_1%3a15.0.7-0ubuntu0.22.04.3_amd64.deb ...\n",
|
388 |
+
"Unpacking libllvm15:amd64 (1:15.0.7-0ubuntu0.22.04.3) ...\n",
|
389 |
+
"Selecting previously unselected package libsensors-config.\n",
|
390 |
+
"Preparing to unpack .../16-libsensors-config_1%3a3.6.0-7ubuntu1_all.deb ...\n",
|
391 |
+
"Unpacking libsensors-config (1:3.6.0-7ubuntu1) ...\n",
|
392 |
+
"Selecting previously unselected package libsensors5:amd64.\n",
|
393 |
+
"Preparing to unpack .../17-libsensors5_1%3a3.6.0-7ubuntu1_amd64.deb ...\n",
|
394 |
+
"Unpacking libsensors5:amd64 (1:3.6.0-7ubuntu1) ...\n",
|
395 |
+
"Selecting previously unselected package libxcb-dri3-0:amd64.\n",
|
396 |
+
"Preparing to unpack .../18-libxcb-dri3-0_1.14-3ubuntu3_amd64.deb ...\n",
|
397 |
+
"Unpacking libxcb-dri3-0:amd64 (1.14-3ubuntu3) ...\n",
|
398 |
+
"Selecting previously unselected package libgl1-mesa-dri:amd64.\n",
|
399 |
+
"Preparing to unpack .../19-libgl1-mesa-dri_23.2.1-1ubuntu3.1~22.04.2_amd64.deb ...\n",
|
400 |
+
"Unpacking libgl1-mesa-dri:amd64 (23.2.1-1ubuntu3.1~22.04.2) ...\n",
|
401 |
+
"Selecting previously unselected package libglvnd0:amd64.\n",
|
402 |
+
"Preparing to unpack .../20-libglvnd0_1.4.0-1_amd64.deb ...\n",
|
403 |
+
"Unpacking libglvnd0:amd64 (1.4.0-1) ...\n",
|
404 |
+
"Selecting previously unselected package libx11-xcb1:amd64.\n",
|
405 |
+
"Preparing to unpack .../21-libx11-xcb1_2%3a1.7.5-1ubuntu0.3_amd64.deb ...\n",
|
406 |
+
"Unpacking libx11-xcb1:amd64 (2:1.7.5-1ubuntu0.3) ...\n",
|
407 |
+
"Selecting previously unselected package libxcb-dri2-0:amd64.\n",
|
408 |
+
"Preparing to unpack .../22-libxcb-dri2-0_1.14-3ubuntu3_amd64.deb ...\n",
|
409 |
+
"Unpacking libxcb-dri2-0:amd64 (1.14-3ubuntu3) ...\n",
|
410 |
+
"Selecting previously unselected package libxcb-glx0:amd64.\n",
|
411 |
+
"Preparing to unpack .../23-libxcb-glx0_1.14-3ubuntu3_amd64.deb ...\n",
|
412 |
+
"Unpacking libxcb-glx0:amd64 (1.14-3ubuntu3) ...\n",
|
413 |
+
"Selecting previously unselected package libxcb-present0:amd64.\n",
|
414 |
+
"Preparing to unpack .../24-libxcb-present0_1.14-3ubuntu3_amd64.deb ...\n",
|
415 |
+
"Unpacking libxcb-present0:amd64 (1.14-3ubuntu3) ...\n",
|
416 |
+
"Selecting previously unselected package libxcb-randr0:amd64.\n",
|
417 |
+
"Preparing to unpack .../25-libxcb-randr0_1.14-3ubuntu3_amd64.deb ...\n",
|
418 |
+
"Unpacking libxcb-randr0:amd64 (1.14-3ubuntu3) ...\n",
|
419 |
+
"Selecting previously unselected package libxcb-shm0:amd64.\n",
|
420 |
+
"Preparing to unpack .../26-libxcb-shm0_1.14-3ubuntu3_amd64.deb ...\n",
|
421 |
+
"Unpacking libxcb-shm0:amd64 (1.14-3ubuntu3) ...\n",
|
422 |
+
"Selecting previously unselected package libxcb-sync1:amd64.\n",
|
423 |
+
"Preparing to unpack .../27-libxcb-sync1_1.14-3ubuntu3_amd64.deb ...\n",
|
424 |
+
"Unpacking libxcb-sync1:amd64 (1.14-3ubuntu3) ...\n",
|
425 |
+
"Selecting previously unselected package libxcb-xfixes0:amd64.\n",
|
426 |
+
"Preparing to unpack .../28-libxcb-xfixes0_1.14-3ubuntu3_amd64.deb ...\n",
|
427 |
+
"Unpacking libxcb-xfixes0:amd64 (1.14-3ubuntu3) ...\n",
|
428 |
+
"Selecting previously unselected package libxfixes3:amd64.\n",
|
429 |
+
"Preparing to unpack .../29-libxfixes3_1%3a6.0.0-1_amd64.deb ...\n",
|
430 |
+
"Unpacking libxfixes3:amd64 (1:6.0.0-1) ...\n",
|
431 |
+
"Selecting previously unselected package libxshmfence1:amd64.\n",
|
432 |
+
"Preparing to unpack .../30-libxshmfence1_1.3-1build4_amd64.deb ...\n",
|
433 |
+
"Unpacking libxshmfence1:amd64 (1.3-1build4) ...\n",
|
434 |
+
"Selecting previously unselected package libxxf86vm1:amd64.\n",
|
435 |
+
"Preparing to unpack .../31-libxxf86vm1_1%3a1.1.4-1build3_amd64.deb ...\n",
|
436 |
+
"Unpacking libxxf86vm1:amd64 (1:1.1.4-1build3) ...\n",
|
437 |
+
"Selecting previously unselected package libglx-mesa0:amd64.\n",
|
438 |
+
"Preparing to unpack .../32-libglx-mesa0_23.2.1-1ubuntu3.1~22.04.2_amd64.deb ...\n",
|
439 |
+
"Unpacking libglx-mesa0:amd64 (23.2.1-1ubuntu3.1~22.04.2) ...\n",
|
440 |
+
"Selecting previously unselected package libglx0:amd64.\n",
|
441 |
+
"Preparing to unpack .../33-libglx0_1.4.0-1_amd64.deb ...\n",
|
442 |
+
"Unpacking libglx0:amd64 (1.4.0-1) ...\n",
|
443 |
+
"Selecting previously unselected package libgl1:amd64.\n",
|
444 |
+
"Preparing to unpack .../34-libgl1_1.4.0-1_amd64.deb ...\n",
|
445 |
+
"Unpacking libgl1:amd64 (1.4.0-1) ...\n",
|
446 |
+
"Selecting previously unselected package libgl1-mesa-glx:amd64.\n",
|
447 |
+
"Preparing to unpack .../35-libgl1-mesa-glx_23.0.4-0ubuntu1~22.04.1_amd64.deb ...\n",
|
448 |
+
"Unpacking libgl1-mesa-glx:amd64 (23.0.4-0ubuntu1~22.04.1) ...\n",
|
449 |
+
"Setting up libpciaccess0:amd64 (0.16-3) ...\n",
|
450 |
+
"Setting up libxau6:amd64 (1:1.0.9-1build5) ...\n",
|
451 |
+
"Setting up libxdmcp6:amd64 (1:1.1.3-0ubuntu5) ...\n",
|
452 |
+
"Setting up libxcb1:amd64 (1.14-3ubuntu3) ...\n",
|
453 |
+
"Setting up libxcb-xfixes0:amd64 (1.14-3ubuntu3) ...\n",
|
454 |
+
"Setting up libglvnd0:amd64 (1.4.0-1) ...\n",
|
455 |
+
"Setting up libxcb-glx0:amd64 (1.14-3ubuntu3) ...\n",
|
456 |
+
"Setting up libsensors-config (1:3.6.0-7ubuntu1) ...\n",
|
457 |
+
"Setting up libxcb-shm0:amd64 (1.14-3ubuntu3) ...\n",
|
458 |
+
"Setting up libxcb-present0:amd64 (1.14-3ubuntu3) ...\n",
|
459 |
+
"Setting up libx11-data (2:1.7.5-1ubuntu0.3) ...\n",
|
460 |
+
"Setting up libxcb-sync1:amd64 (1.14-3ubuntu3) ...\n",
|
461 |
+
"Setting up libsensors5:amd64 (1:3.6.0-7ubuntu1) ...\n",
|
462 |
+
"Setting up libglapi-mesa:amd64 (23.2.1-1ubuntu3.1~22.04.2) ...\n",
|
463 |
+
"Setting up libxcb-dri2-0:amd64 (1.14-3ubuntu3) ...\n",
|
464 |
+
"Setting up libxshmfence1:amd64 (1.3-1build4) ...\n",
|
465 |
+
"Setting up libxcb-randr0:amd64 (1.14-3ubuntu3) ...\n",
|
466 |
+
"Setting up libllvm15:amd64 (1:15.0.7-0ubuntu0.22.04.3) ...\n",
|
467 |
+
"Setting up libx11-6:amd64 (2:1.7.5-1ubuntu0.3) ...\n",
|
468 |
+
"Setting up libdrm-common (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
469 |
+
"Setting up libxcb-dri3-0:amd64 (1.14-3ubuntu3) ...\n",
|
470 |
+
"Setting up libx11-xcb1:amd64 (2:1.7.5-1ubuntu0.3) ...\n",
|
471 |
+
"Setting up libxext6:amd64 (2:1.3.4-1build1) ...\n",
|
472 |
+
"Setting up libxxf86vm1:amd64 (1:1.1.4-1build3) ...\n",
|
473 |
+
"Setting up libxfixes3:amd64 (1:6.0.0-1) ...\n",
|
474 |
+
"Setting up libdrm2:amd64 (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
475 |
+
"Setting up libdrm-amdgpu1:amd64 (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
476 |
+
"Setting up libdrm-nouveau2:amd64 (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
477 |
+
"Setting up libdrm-radeon1:amd64 (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
478 |
+
"Setting up libdrm-intel1:amd64 (2.4.113-2~ubuntu0.22.04.1) ...\n",
|
479 |
+
"Setting up libgl1-mesa-dri:amd64 (23.2.1-1ubuntu3.1~22.04.2) ...\n",
|
480 |
+
"Setting up libgl1-amber-dri:amd64 (21.3.9-0ubuntu1~22.04.1) ...\n",
|
481 |
+
"Setting up libglx-mesa0:amd64 (23.2.1-1ubuntu3.1~22.04.2) ...\n",
|
482 |
+
"Setting up libglx0:amd64 (1.4.0-1) ...\n",
|
483 |
+
"Setting up libgl1:amd64 (1.4.0-1) ...\n",
|
484 |
+
"Setting up libgl1-mesa-glx:amd64 (23.0.4-0ubuntu1~22.04.1) ...\n",
|
485 |
+
"Processing triggers for libc-bin (2.35-0ubuntu3.4) ...\n",
|
486 |
+
"yes: standard output: Broken pipe\n"
|
487 |
+
]
|
488 |
+
}
|
489 |
+
],
|
490 |
+
"source": [
|
491 |
+
"!sudo apt-get update\n",
|
492 |
+
"!yes | sudo apt-get install libgl1-mesa-glx"
|
493 |
+
]
|
494 |
+
},
|
495 |
+
{
|
496 |
+
"cell_type": "code",
|
497 |
+
"execution_count": 9,
|
498 |
+
"metadata": {},
|
499 |
+
"outputs": [
|
500 |
+
{
|
501 |
+
"name": "stdout",
|
502 |
+
"output_type": "stream",
|
503 |
+
"text": [
|
504 |
+
"/workspace/yolov9\n"
|
505 |
+
]
|
506 |
+
}
|
507 |
+
],
|
508 |
+
"source": [
|
509 |
+
"%cd /workspace/yolov9"
|
510 |
+
]
|
511 |
+
},
|
512 |
+
{
|
513 |
+
"cell_type": "code",
|
514 |
+
"execution_count": 22,
|
515 |
+
"metadata": {},
|
516 |
+
"outputs": [
|
517 |
+
{
|
518 |
+
"name": "stdout",
|
519 |
+
"output_type": "stream",
|
520 |
+
"text": [
|
521 |
+
"Collecting Pillow==9.5.0\n",
|
522 |
+
" Downloading Pillow-9.5.0-cp310-cp310-manylinux_2_28_x86_64.whl.metadata (9.5 kB)\n",
|
523 |
+
"Downloading Pillow-9.5.0-cp310-cp310-manylinux_2_28_x86_64.whl (3.4 MB)\n",
|
524 |
+
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m3.4/3.4 MB\u001b[0m \u001b[31m54.5 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
|
525 |
+
"\u001b[?25hInstalling collected packages: Pillow\n",
|
526 |
+
" Attempting uninstall: Pillow\n",
|
527 |
+
" Found existing installation: Pillow 10.0.1\n",
|
528 |
+
" Uninstalling Pillow-10.0.1:\n",
|
529 |
+
" Successfully uninstalled Pillow-10.0.1\n",
|
530 |
+
"Successfully installed Pillow-9.5.0\n",
|
531 |
+
"\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
|
532 |
+
"\u001b[0mNote: you may need to restart the kernel to use updated packages.\n"
|
533 |
+
]
|
534 |
+
}
|
535 |
+
],
|
536 |
+
"source": [
|
537 |
+
"pip install Pillow==9.5.0"
|
538 |
+
]
|
539 |
+
},
|
540 |
+
{
|
541 |
+
"cell_type": "code",
|
542 |
+
"execution_count": 23,
|
543 |
+
"metadata": {
|
544 |
+
"colab": {
|
545 |
+
"base_uri": "https://localhost:8080/"
|
546 |
+
},
|
547 |
+
"id": "jjB9EA9yRCyR",
|
548 |
+
"outputId": "2e388fee-6ec5-492d-b698-55d2d1cec2f2"
|
549 |
+
},
|
550 |
+
"outputs": [
|
551 |
+
{
|
552 |
+
"name": "stdout",
|
553 |
+
"output_type": "stream",
|
554 |
+
"text": [
|
555 |
+
"\u001b[34m\u001b[1mtrain_dual: \u001b[0mweights=, cfg=/workspace/YOLO9_KMUTT/yolov9/models/detect/yolov9-kmuttX.yaml, data=/workspace/6400 images/data.yaml, hyp=hyp.scratch-high.yaml, epochs=500, batch_size=4, imgsz=640, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, noplots=False, evolve=None, bucket=, cache=None, image_weights=False, device=0, multi_scale=False, single_cls=False, optimizer=SGD, sync_bn=False, workers=8, project=runs/train, name=yolov9-c, exist_ok=False, quad=False, cos_lr=False, flat_cos_lr=False, fixed_lr=False, label_smoothing=0.0, patience=100, freeze=[0], save_period=-1, seed=0, local_rank=-1, min_items=0, close_mosaic=15, entity=None, upload_dataset=False, bbox_interval=-1, artifact_alias=latest\n",
|
556 |
+
"YOLO 🚀 2024-6-7 Python-3.10.13 torch-2.2.0 CUDA:0 (NVIDIA GeForce RTX 4090, 24210MiB)\n",
|
557 |
+
"\n",
|
558 |
+
"\u001b[34m\u001b[1mhyperparameters: \u001b[0mlr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, cls_pw=1.0, obj=0.7, obj_pw=1.0, dfl=1.5, iou_t=0.2, anchor_t=5.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.9, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.15, copy_paste=0.3\n",
|
559 |
+
"\u001b[34m\u001b[1mClearML: \u001b[0mrun 'pip install clearml' to automatically track, visualize and remotely train YOLO 🚀 in ClearML\n",
|
560 |
+
"\u001b[34m\u001b[1mComet: \u001b[0mrun 'pip install comet_ml' to automatically track and visualize YOLO 🚀 runs in Comet\n",
|
561 |
+
"\u001b[34m\u001b[1mTensorBoard: \u001b[0mStart with 'tensorboard --logdir runs/train', view at http://localhost:6006/\n",
|
562 |
+
"Overriding model.yaml nc=80 with nc=10\n",
|
563 |
+
"\n",
|
564 |
+
" from n params module arguments \n",
|
565 |
+
" 0 -1 1 0 models.common.Silence [] \n",
|
566 |
+
" 1 -1 1 1856 models.common.Conv [3, 64, 3, 2] \n",
|
567 |
+
" 2 -1 1 73984 models.common.Conv [64, 128, 3, 2] \n",
|
568 |
+
" 3 -1 1 252160 models.common.RepNCSPELAN4 [128, 256, 128, 64, 2] \n",
|
569 |
+
" 4 -1 1 164352 models.common.ADown [256, 256] \n",
|
570 |
+
" 5 -1 1 1004032 models.common.RepNCSPELAN4 [256, 512, 256, 128, 2] \n",
|
571 |
+
" 6 -1 1 656384 models.common.ADown [512, 512] \n",
|
572 |
+
" 7 -1 1 4006912 models.common.RepNCSPELAN4 [512, 1024, 512, 256, 2] \n",
|
573 |
+
" 8 -1 1 2623488 models.common.ADown [1024, 1024] \n",
|
574 |
+
" 9 -1 1 4269056 models.common.RepNCSPELAN4 [1024, 1024, 512, 256, 2] \n",
|
575 |
+
" 10 1 1 4160 models.common.CBLinear [64, [64]] \n",
|
576 |
+
" 11 3 1 49344 models.common.CBLinear [256, [64, 128]] \n",
|
577 |
+
" 12 5 1 229824 models.common.CBLinear [512, [64, 128, 256]] \n",
|
578 |
+
" 13 7 1 984000 models.common.CBLinear [1024, [64, 128, 256, 512]] \n",
|
579 |
+
" 14 9 1 2033600 models.common.CBLinear [1024, [64, 128, 256, 512, 1024]]\n",
|
580 |
+
" 15 0 1 1856 models.common.Conv [3, 64, 3, 2] \n",
|
581 |
+
" 16[10, 11, 12, 13, 14, -1] 1 0 models.common.CBFuse [[0, 0, 0, 0, 0]] \n",
|
582 |
+
" 17 -1 1 73984 models.common.Conv [64, 128, 3, 2] \n",
|
583 |
+
" 18[11, 12, 13, 14, -1] 1 0 models.common.CBFuse [[1, 1, 1, 1]] \n",
|
584 |
+
" 19 -1 1 252160 models.common.RepNCSPELAN4 [128, 256, 128, 64, 2] \n",
|
585 |
+
" 20 -1 1 164352 models.common.ADown [256, 256] \n",
|
586 |
+
" 21 [12, 13, 14, -1] 1 0 models.common.CBFuse [[2, 2, 2]] \n",
|
587 |
+
" 22 -1 1 1004032 models.common.RepNCSPELAN4 [256, 512, 256, 128, 2] \n",
|
588 |
+
" 23 -1 1 656384 models.common.ADown [512, 512] \n",
|
589 |
+
" 24 [13, 14, -1] 1 0 models.common.CBFuse [[3, 3]] \n",
|
590 |
+
" 25 -1 1 4006912 models.common.RepNCSPELAN4 [512, 1024, 512, 256, 2] \n",
|
591 |
+
" 26 -1 1 2623488 models.common.ADown [1024, 1024] \n",
|
592 |
+
" 27 [14, -1] 1 0 models.common.CBFuse [[4]] \n",
|
593 |
+
" 28 -1 1 4269056 models.common.RepNCSPELAN4 [1024, 1024, 512, 256, 2] \n",
|
594 |
+
" 29 9 1 787968 models.common.SPPELAN [1024, 512, 256] \n",
|
595 |
+
" 30 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n",
|
596 |
+
" 31 [-1, 7] 1 0 models.common.Concat [1] \n",
|
597 |
+
" 32 -1 1 4005888 models.common.RepNCSPELAN4 [1536, 512, 512, 256, 2] \n",
|
598 |
+
" 33 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n",
|
599 |
+
" 34 [-1, 5] 1 0 models.common.Concat [1] \n",
|
600 |
+
" 35 -1 1 1069056 models.common.RepNCSPELAN4 [1024, 256, 256, 128, 2] \n",
|
601 |
+
" 36 28 1 787968 models.common.SPPELAN [1024, 512, 256] \n",
|
602 |
+
" 37 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n",
|
603 |
+
" 38 [-1, 25] 1 0 models.common.Concat [1] \n",
|
604 |
+
" 39 -1 1 4005888 models.common.RepNCSPELAN4 [1536, 512, 512, 256, 2] \n",
|
605 |
+
" 40 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n",
|
606 |
+
" 41 [-1, 22] 1 0 models.common.Concat [1] \n",
|
607 |
+
" 42 -1 1 1069056 models.common.RepNCSPELAN4 [1024, 256, 256, 128, 2] \n",
|
608 |
+
" 43 -1 1 164352 models.common.ADown [256, 256] \n",
|
609 |
+
" 44 [-1, 39] 1 0 models.common.Concat [1] \n",
|
610 |
+
" 45 -1 1 3612672 models.common.RepNCSPELAN4 [768, 512, 512, 256, 2] \n",
|
611 |
+
" 46 -1 1 656384 models.common.ADown [512, 512] \n",
|
612 |
+
" 47 [-1, 36] 1 0 models.common.Concat [1] \n",
|
613 |
+
" 48 -1 1 13910016 models.common.RepNCSPELAN4 [1024, 1024, 1024, 512, 2] \n",
|
614 |
+
" 49 -1 1 3948032 models.common.PSA [1024, 1024] \n",
|
615 |
+
" 50 35 1 249728 models.common.PSA [256, 256] \n",
|
616 |
+
" 51 32 1 990976 models.common.PSA [512, 512] \n",
|
617 |
+
" 52 29 1 990976 models.common.PSA [512, 512] \n",
|
618 |
+
" 53 42 1 249728 models.common.PSA [256, 256] \n",
|
619 |
+
" 54 45 1 990976 models.common.PSA [512, 512] \n",
|
620 |
+
" 55[50, 51, 52, 53, 54, 49] 1 12471260 models.yolo.DualDDetect [10, [256, 512, 512, 256, 512, 1024]]\n",
|
621 |
+
"yolov9-kmuttX summary: 1643 layers, 79366300 parameters, 79366268 gradients, 263.8 GFLOPs\n",
|
622 |
+
"\n",
|
623 |
+
"\u001b[34m\u001b[1mAMP: \u001b[0mchecks passed ✅\n",
|
624 |
+
"\u001b[34m\u001b[1moptimizer:\u001b[0m SGD(lr=0.01) with parameter groups 398 weight(decay=0.0), 417 weight(decay=0.0005), 415 bias\n",
|
625 |
+
"\u001b[34m\u001b[1malbumentations: \u001b[0mBlur(p=0.01, blur_limit=(3, 7)), MedianBlur(p=0.01, blur_limit=(3, 7)), ToGray(p=0.01), CLAHE(p=0.01, clip_limit=(1, 4.0), tile_grid_size=(8, 8))\n",
|
626 |
+
"\u001b[34m\u001b[1mtrain: \u001b[0mScanning /workspace/6400 images/train/labels.cache... 5120 images, 7 back\u001b[0m\n",
|
627 |
+
"\u001b[34m\u001b[1mval: \u001b[0mScanning /workspace/6400 images/valid/labels.cache... 640 images, 0 backgro\u001b[0m\n",
|
628 |
+
"Plotting labels to runs/train/yolov9-c10/labels.jpg... \n",
|
629 |
+
"Image sizes 640 train, 640 val\n",
|
630 |
+
"Using 4 dataloader workers\n",
|
631 |
+
"Logging results to \u001b[1mruns/train/yolov9-c10\u001b[0m\n",
|
632 |
+
"Starting training for 500 epochs...\n",
|
633 |
+
"\n",
|
634 |
+
" Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size\n",
|
635 |
+
" 0/499 16.5G 4.939 7.156 5.232 11 640: WARNING ⚠️ TensorBoard graph visualization failure Only tensors, lists, tuples of tensors, or dictionary of tensors can be output from traced functions\n",
|
636 |
+
" 0/499 16.7G 5.236 7.764 5.234 23 640: ^C\n",
|
637 |
+
" 0/499 16.7G 5.236 7.764 5.234 23 640: \n",
|
638 |
+
"Traceback (most recent call last):\n",
|
639 |
+
" File \"/workspace/YOLO9_KMUTT/yolov9/train_dual.py\", line 644, in <module>\n",
|
640 |
+
" main(opt)\n",
|
641 |
+
" File \"/workspace/YOLO9_KMUTT/yolov9/train_dual.py\", line 538, in main\n",
|
642 |
+
" train(opt.hyp, opt, device, callbacks)\n",
|
643 |
+
" File \"/workspace/YOLO9_KMUTT/yolov9/train_dual.py\", line 314, in train\n",
|
644 |
+
" pred = model(imgs) # forward\n",
|
645 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
646 |
+
" return self._call_impl(*args, **kwargs)\n",
|
647 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
648 |
+
" return forward_call(*args, **kwargs)\n",
|
649 |
+
" File \"/workspace/YOLO9_KMUTT/yolov9/models/yolo.py\", line 633, in forward\n",
|
650 |
+
" return self._forward_once(x, profile, visualize) # single-scale inference, train\n",
|
651 |
+
" File \"/workspace/YOLO9_KMUTT/yolov9/models/yolo.py\", line 533, in _forward_once\n",
|
652 |
+
" x = m(x) # run\n",
|
653 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
654 |
+
" return self._call_impl(*args, **kwargs)\n",
|
655 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
656 |
+
" return forward_call(*args, **kwargs)\n",
|
657 |
+
" File \"/workspace/YOLO9_KMUTT/yolov9/models/common.py\", line 613, in forward\n",
|
658 |
+
" y.extend((m(y[-1])) for m in [self.cv2, self.cv3])\n",
|
659 |
+
" File \"/workspace/YOLO9_KMUTT/yolov9/models/common.py\", line 613, in <genexpr>\n",
|
660 |
+
" y.extend((m(y[-1])) for m in [self.cv2, self.cv3])\n",
|
661 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
662 |
+
" return self._call_impl(*args, **kwargs)\n",
|
663 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
664 |
+
" return forward_call(*args, **kwargs)\n",
|
665 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/container.py\", line 217, in forward\n",
|
666 |
+
" input = module(input)\n",
|
667 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
668 |
+
" return self._call_impl(*args, **kwargs)\n",
|
669 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
670 |
+
" return forward_call(*args, **kwargs)\n",
|
671 |
+
" File \"/workspace/YOLO9_KMUTT/yolov9/models/common.py\", line 384, in forward\n",
|
672 |
+
" return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))\n",
|
673 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
674 |
+
" return self._call_impl(*args, **kwargs)\n",
|
675 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
676 |
+
" return forward_call(*args, **kwargs)\n",
|
677 |
+
" File \"/workspace/YOLO9_KMUTT/yolov9/models/common.py\", line 54, in forward\n",
|
678 |
+
" return self.act(self.bn(self.conv(x)))\n",
|
679 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
680 |
+
" return self._call_impl(*args, **kwargs)\n",
|
681 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
682 |
+
" return forward_call(*args, **kwargs)\n",
|
683 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/batchnorm.py\", line 175, in forward\n",
|
684 |
+
" return F.batch_norm(\n",
|
685 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/functional.py\", line 2482, in batch_norm\n",
|
686 |
+
" return torch.batch_norm(\n",
|
687 |
+
"KeyboardInterrupt\n"
|
688 |
+
]
|
689 |
+
}
|
690 |
+
],
|
691 |
+
"source": [
|
692 |
+
"!python train_dual.py --workers 8 --device 0 --batch 4 --data '/workspace/6400 images/data.yaml' --img 640 --cfg /workspace/YOLO9_KMUTT/yolov9/models/detect/yolov9-kmuttX.yaml --weights '' --name yolov9-c --hyp hyp.scratch-high.yaml --min-items 0 --epochs 500 --close-mosaic 15"
|
693 |
+
]
|
694 |
+
},
|
695 |
+
{
|
696 |
+
"cell_type": "code",
|
697 |
+
"execution_count": 9,
|
698 |
+
"metadata": {
|
699 |
+
"id": "VRMBngDDsIok"
|
700 |
+
},
|
701 |
+
"outputs": [
|
702 |
+
{
|
703 |
+
"name": "stdout",
|
704 |
+
"output_type": "stream",
|
705 |
+
"text": [
|
706 |
+
"[2024-06-01 23:55:21,192] torch.distributed.run: [WARNING] \n",
|
707 |
+
"[2024-06-01 23:55:21,192] torch.distributed.run: [WARNING] *****************************************\n",
|
708 |
+
"[2024-06-01 23:55:21,192] torch.distributed.run: [WARNING] Setting OMP_NUM_THREADS environment variable for each process to be 1 in default, to avoid your system being overloaded, please further tune the variable for optimal performance in your application as needed. \n",
|
709 |
+
"[2024-06-01 23:55:21,192] torch.distributed.run: [WARNING] *****************************************\n",
|
710 |
+
"\u001b[34m\u001b[1mtrain_dual: \u001b[0mweights=, cfg=models/detect/yolov9-c.yaml, data=/workspace/norway/data.yaml, hyp=hyp.scratch-high.yaml, epochs=500, batch_size=30, imgsz=640, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, noplots=False, evolve=None, bucket=, cache=None, image_weights=False, device=0,1, multi_scale=False, single_cls=False, optimizer=SGD, sync_bn=True, workers=8, project=runs/train, name=yolov9-c, exist_ok=False, quad=False, cos_lr=False, flat_cos_lr=False, fixed_lr=False, label_smoothing=0.0, patience=100, freeze=[0], save_period=-1, seed=0, local_rank=-1, min_items=0, close_mosaic=15, entity=None, upload_dataset=False, bbox_interval=-1, artifact_alias=latest\n",
|
711 |
+
"Traceback (most recent call last):\n",
|
712 |
+
" File \"/workspace/yolov9/train_dual.py\", line 644, in <module>\n",
|
713 |
+
" main(opt)\n",
|
714 |
+
" File \"/workspace/yolov9/train_dual.py\", line 530, in main\n",
|
715 |
+
" assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'\n",
|
716 |
+
"AssertionError: --batch-size 30 must be multiple of WORLD_SIZE\n",
|
717 |
+
"Traceback (most recent call last):\n",
|
718 |
+
" File \"/workspace/yolov9/train_dual.py\", line 644, in <module>\n",
|
719 |
+
" main(opt)\n",
|
720 |
+
" File \"/workspace/yolov9/train_dual.py\", line 530, in main\n",
|
721 |
+
" assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'\n",
|
722 |
+
"AssertionError: --batch-size 30 must be multiple of WORLD_SIZE\n",
|
723 |
+
"Traceback (most recent call last):\n",
|
724 |
+
" File \"/workspace/yolov9/train_dual.py\", line 644, in <module>\n",
|
725 |
+
" main(opt)\n",
|
726 |
+
" File \"/workspace/yolov9/train_dual.py\", line 530, in main\n",
|
727 |
+
" assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'\n",
|
728 |
+
"AssertionError: --batch-size 30 must be multiple of WORLD_SIZE\n",
|
729 |
+
"YOLO 🚀 v0.1-94-gcd88016 Python-3.10.13 torch-2.2.0 CUDA:0 (NVIDIA GeForce RTX 4090, 24217MiB)\n",
|
730 |
+
" CUDA:1 (NVIDIA GeForce RTX 4090, 24217MiB)\n",
|
731 |
+
"\n",
|
732 |
+
"Traceback (most recent call last):\n",
|
733 |
+
" File \"/workspace/yolov9/train_dual.py\", line 644, in <module>\n",
|
734 |
+
" main(opt)\n",
|
735 |
+
" File \"/workspace/yolov9/train_dual.py\", line 530, in main\n",
|
736 |
+
" assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'\n",
|
737 |
+
"AssertionError: --batch-size 30 must be multiple of WORLD_SIZE\n",
|
738 |
+
"Traceback (most recent call last):\n",
|
739 |
+
" File \"/workspace/yolov9/train_dual.py\", line 644, in <module>\n",
|
740 |
+
" main(opt)\n",
|
741 |
+
" File \"/workspace/yolov9/train_dual.py\", line 530, in main\n",
|
742 |
+
" assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'\n",
|
743 |
+
"AssertionError: --batch-size 30 must be multiple of WORLD_SIZE\n",
|
744 |
+
"Traceback (most recent call last):\n",
|
745 |
+
" File \"/workspace/yolov9/train_dual.py\", line 644, in <module>\n",
|
746 |
+
" main(opt)\n",
|
747 |
+
" File \"/workspace/yolov9/train_dual.py\", line 530, in main\n",
|
748 |
+
" assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'\n",
|
749 |
+
"AssertionError: --batch-size 30 must be multiple of WORLD_SIZE\n",
|
750 |
+
"Traceback (most recent call last):\n",
|
751 |
+
" File \"/workspace/yolov9/train_dual.py\", line 644, in <module>\n",
|
752 |
+
" main(opt)\n",
|
753 |
+
" File \"/workspace/yolov9/train_dual.py\", line 530, in main\n",
|
754 |
+
" assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'\n",
|
755 |
+
"AssertionError: --batch-size 30 must be multiple of WORLD_SIZE\n",
|
756 |
+
"Traceback (most recent call last):\n",
|
757 |
+
" File \"/workspace/yolov9/train_dual.py\", line 644, in <module>\n",
|
758 |
+
" main(opt)\n",
|
759 |
+
" File \"/workspace/yolov9/train_dual.py\", line 530, in main\n",
|
760 |
+
" assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'\n",
|
761 |
+
"AssertionError: --batch-size 30 must be multiple of WORLD_SIZE\n",
|
762 |
+
"[2024-06-01 23:55:26,209] torch.distributed.elastic.multiprocessing.api: [ERROR] failed (exitcode: 1) local_rank: 0 (pid: 4360) of binary: /opt/conda/bin/python\n",
|
763 |
+
"Traceback (most recent call last):\n",
|
764 |
+
" File \"/opt/conda/bin/torchrun\", line 33, in <module>\n",
|
765 |
+
" sys.exit(load_entry_point('torch==2.2.0', 'console_scripts', 'torchrun')())\n",
|
766 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/distributed/elastic/multiprocessing/errors/__init__.py\", line 347, in wrapper\n",
|
767 |
+
" return f(*args, **kwargs)\n",
|
768 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/distributed/run.py\", line 812, in main\n",
|
769 |
+
" run(args)\n",
|
770 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/distributed/run.py\", line 803, in run\n",
|
771 |
+
" elastic_launch(\n",
|
772 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/distributed/launcher/api.py\", line 135, in __call__\n",
|
773 |
+
" return launch_agent(self._config, self._entrypoint, list(args))\n",
|
774 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/distributed/launcher/api.py\", line 268, in launch_agent\n",
|
775 |
+
" raise ChildFailedError(\n",
|
776 |
+
"torch.distributed.elastic.multiprocessing.errors.ChildFailedError: \n",
|
777 |
+
"============================================================\n",
|
778 |
+
"train_dual.py FAILED\n",
|
779 |
+
"------------------------------------------------------------\n",
|
780 |
+
"Failures:\n",
|
781 |
+
"[1]:\n",
|
782 |
+
" time : 2024-06-01_23:55:26\n",
|
783 |
+
" host : de3bed18af84\n",
|
784 |
+
" rank : 1 (local_rank: 1)\n",
|
785 |
+
" exitcode : 1 (pid: 4361)\n",
|
786 |
+
" error_file: <N/A>\n",
|
787 |
+
" traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html\n",
|
788 |
+
"[2]:\n",
|
789 |
+
" time : 2024-06-01_23:55:26\n",
|
790 |
+
" host : de3bed18af84\n",
|
791 |
+
" rank : 2 (local_rank: 2)\n",
|
792 |
+
" exitcode : 1 (pid: 4362)\n",
|
793 |
+
" error_file: <N/A>\n",
|
794 |
+
" traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html\n",
|
795 |
+
"[3]:\n",
|
796 |
+
" time : 2024-06-01_23:55:26\n",
|
797 |
+
" host : de3bed18af84\n",
|
798 |
+
" rank : 3 (local_rank: 3)\n",
|
799 |
+
" exitcode : 1 (pid: 4363)\n",
|
800 |
+
" error_file: <N/A>\n",
|
801 |
+
" traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html\n",
|
802 |
+
"[4]:\n",
|
803 |
+
" time : 2024-06-01_23:55:26\n",
|
804 |
+
" host : de3bed18af84\n",
|
805 |
+
" rank : 4 (local_rank: 4)\n",
|
806 |
+
" exitcode : 1 (pid: 4364)\n",
|
807 |
+
" error_file: <N/A>\n",
|
808 |
+
" traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html\n",
|
809 |
+
"[5]:\n",
|
810 |
+
" time : 2024-06-01_23:55:26\n",
|
811 |
+
" host : de3bed18af84\n",
|
812 |
+
" rank : 5 (local_rank: 5)\n",
|
813 |
+
" exitcode : 1 (pid: 4365)\n",
|
814 |
+
" error_file: <N/A>\n",
|
815 |
+
" traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html\n",
|
816 |
+
"[6]:\n",
|
817 |
+
" time : 2024-06-01_23:55:26\n",
|
818 |
+
" host : de3bed18af84\n",
|
819 |
+
" rank : 6 (local_rank: 6)\n",
|
820 |
+
" exitcode : 1 (pid: 4366)\n",
|
821 |
+
" error_file: <N/A>\n",
|
822 |
+
" traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html\n",
|
823 |
+
"[7]:\n",
|
824 |
+
" time : 2024-06-01_23:55:26\n",
|
825 |
+
" host : de3bed18af84\n",
|
826 |
+
" rank : 7 (local_rank: 7)\n",
|
827 |
+
" exitcode : 1 (pid: 4367)\n",
|
828 |
+
" error_file: <N/A>\n",
|
829 |
+
" traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html\n",
|
830 |
+
"------------------------------------------------------------\n",
|
831 |
+
"Root Cause (first observed failure):\n",
|
832 |
+
"[0]:\n",
|
833 |
+
" time : 2024-06-01_23:55:26\n",
|
834 |
+
" host : de3bed18af84\n",
|
835 |
+
" rank : 0 (local_rank: 0)\n",
|
836 |
+
" exitcode : 1 (pid: 4360)\n",
|
837 |
+
" error_file: <N/A>\n",
|
838 |
+
" traceback : To enable traceback see: https://pytorch.org/docs/stable/elastic/errors.html\n",
|
839 |
+
"============================================================\n"
|
840 |
+
]
|
841 |
+
}
|
842 |
+
],
|
843 |
+
"source": [
|
844 |
+
"!torchrun --nproc_per_node 2 --master_port 9527 train_dual.py \\\n",
|
845 |
+
"--workers 8 --device 0,1 --sync-bn --batch 30 --data '/workspace/norway/data.yaml' \\\n",
|
846 |
+
"--img 640 --cfg models/detect/yolov9-c.yaml --weights '' --name yolov9-c --hyp hyp.scratch-high.yaml \\\n",
|
847 |
+
"--min-items 0 --epochs 500 --close-mosaic 15"
|
848 |
+
]
|
849 |
+
},
|
850 |
+
{
|
851 |
+
"cell_type": "code",
|
852 |
+
"execution_count": 10,
|
853 |
+
"metadata": {},
|
854 |
+
"outputs": [
|
855 |
+
{
|
856 |
+
"name": "stdout",
|
857 |
+
"output_type": "stream",
|
858 |
+
"text": [
|
859 |
+
"[2024-06-02 02:12:23,563] torch.distributed.run: [WARNING] \n",
|
860 |
+
"[2024-06-02 02:12:23,563] torch.distributed.run: [WARNING] *****************************************\n",
|
861 |
+
"[2024-06-02 02:12:23,563] torch.distributed.run: [WARNING] Setting OMP_NUM_THREADS environment variable for each process to be 1 in default, to avoid your system being overloaded, please further tune the variable for optimal performance in your application as needed. \n",
|
862 |
+
"[2024-06-02 02:12:23,563] torch.distributed.run: [WARNING] *****************************************\n",
|
863 |
+
"\u001b[34m\u001b[1mtrain_dual: \u001b[0mweights=, cfg=models/detect/yolov9-c.yaml, data=/workspace/road damage/data.yaml, hyp=hyp.scratch-high.yaml, epochs=500, batch_size=30, imgsz=640, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, noplots=False, evolve=None, bucket=, cache=None, image_weights=False, device=0,1, multi_scale=False, single_cls=False, optimizer=SGD, sync_bn=True, workers=8, project=runs/train, name=yolov9-c, exist_ok=False, quad=False, cos_lr=False, flat_cos_lr=False, fixed_lr=False, label_smoothing=0.0, patience=100, freeze=[0], save_period=-1, seed=0, local_rank=-1, min_items=0, close_mosaic=15, entity=None, upload_dataset=False, bbox_interval=-1, artifact_alias=latest\n",
|
864 |
+
"YOLO 🚀 v0.1-94-gcd88016 Python-3.10.13 torch-2.2.0 CUDA:0 (NVIDIA GeForce RTX 4090, 24217MiB)\n",
|
865 |
+
" CUDA:1 (NVIDIA GeForce RTX 4090, 24217MiB)\n",
|
866 |
+
"\n",
|
867 |
+
"\u001b[34m\u001b[1mhyperparameters: \u001b[0mlr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=7.5, cls=0.5, cls_pw=1.0, obj=0.7, obj_pw=1.0, dfl=1.5, iou_t=0.2, anchor_t=5.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.9, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.15, copy_paste=0.3\n",
|
868 |
+
"\u001b[34m\u001b[1mClearML: \u001b[0mrun 'pip install clearml' to automatically track, visualize and remotely train YOLO 🚀 in ClearML\n",
|
869 |
+
"\u001b[34m\u001b[1mComet: \u001b[0mrun 'pip install comet_ml' to automatically track and visualize YOLO 🚀 runs in Comet\n",
|
870 |
+
"\u001b[34m\u001b[1mTensorBoard: \u001b[0mStart with 'tensorboard --logdir runs/train', view at http://localhost:6006/\n",
|
871 |
+
"Overriding model.yaml nc=80 with nc=7\n",
|
872 |
+
"\n",
|
873 |
+
" from n params module arguments \n",
|
874 |
+
" 0 -1 1 0 models.common.Silence [] \n",
|
875 |
+
" 1 -1 1 1856 models.common.Conv [3, 64, 3, 2] \n",
|
876 |
+
" 2 -1 1 73984 models.common.Conv [64, 128, 3, 2] \n",
|
877 |
+
" 3 -1 1 291456 models.common.RepNCSPELAN4 [128, 256, 128, 64, 3] \n",
|
878 |
+
" 4 -1 1 164352 models.common.ADown [256, 256] \n",
|
879 |
+
" 5 -1 1 1160448 models.common.RepNCSPELAN4 [256, 512, 256, 128, 3] \n",
|
880 |
+
" 6 -1 1 656384 models.common.ADown [512, 512] \n",
|
881 |
+
" 7 -1 1 4105728 models.common.RepNCSPELAN4 [512, 512, 512, 256, 3] \n",
|
882 |
+
" 8 -1 1 656384 models.common.ADown [512, 512] \n",
|
883 |
+
" 9 -1 1 4105728 models.common.RepNCSPELAN4 [512, 512, 512, 256, 3] \n",
|
884 |
+
" 10 -1 1 656896 models.common.SPPELAN [512, 512, 256] \n",
|
885 |
+
" 11 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n",
|
886 |
+
" 12 [-1, 7] 1 0 models.common.Concat [1] \n",
|
887 |
+
" 13 -1 1 3119616 models.common.RepNCSPELAN4 [1024, 512, 512, 256, 1] \n",
|
888 |
+
" 14 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest'] \n",
|
889 |
+
" 15 [-1, 5] 1 0 models.common.Concat [1] \n",
|
890 |
+
" 16 -1 1 912640 models.common.RepNCSPELAN4 [1024, 256, 256, 128, 1] \n",
|
891 |
+
" 17 -1 1 164352 models.common.ADown [256, 256] \n",
|
892 |
+
" 18 [-1, 13] 1 0 models.common.Concat [1] \n",
|
893 |
+
" 19 -1 1 2988544 models.common.RepNCSPELAN4 [768, 512, 512, 256, 1] \n",
|
894 |
+
" 20 -1 1 656384 models.common.ADown [512, 512] \n",
|
895 |
+
" 21 [-1, 10] 1 0 models.common.Concat [1] \n",
|
896 |
+
" 22 -1 1 3119616 models.common.RepNCSPELAN4 [1024, 512, 512, 256, 1] \n",
|
897 |
+
" 23 5 1 131328 models.common.CBLinear [512, [256]] \n",
|
898 |
+
" 24 7 1 393984 models.common.CBLinear [512, [256, 512]] \n",
|
899 |
+
" 25 9 1 656640 models.common.CBLinear [512, [256, 512, 512]] \n",
|
900 |
+
" 26 0 1 1856 models.common.Conv [3, 64, 3, 2] \n",
|
901 |
+
" 27 -1 1 73984 models.common.Conv [64, 128, 3, 2] \n",
|
902 |
+
" 28 -1 1 212864 models.common.RepNCSPELAN4 [128, 256, 128, 64, 1] \n",
|
903 |
+
" 29 -1 1 164352 models.common.ADown [256, 256] \n",
|
904 |
+
" 30 [23, 24, 25, -1] 1 0 models.common.CBFuse [[0, 0, 0]] \n",
|
905 |
+
" 31 -1 1 847616 models.common.RepNCSPELAN4 [256, 512, 256, 128, 1] \n",
|
906 |
+
" 32 -1 1 656384 models.common.ADown [512, 512] \n",
|
907 |
+
" 33 [24, 25, -1] 1 0 models.common.CBFuse [[1, 1]] \n",
|
908 |
+
" 34 -1 1 2857472 models.common.RepNCSPELAN4 [512, 512, 512, 256, 1] \n",
|
909 |
+
" 35 -1 1 656384 models.common.ADown [512, 512] \n",
|
910 |
+
" 36 [25, -1] 1 0 models.common.CBFuse [[2]] \n",
|
911 |
+
" 37 -1 1 2857472 models.common.RepNCSPELAN4 [512, 512, 512, 256, 1] \n",
|
912 |
+
" 38[31, 34, 37, 16, 19, 22] 1 21556682 models.yolo.DualDDetect [7, [512, 512, 512, 256, 512, 512]]\n",
|
913 |
+
"yolov9-c summary: 1170 layers, 53901386 parameters, 53901354 gradients, 252.0 GFLOPs\n",
|
914 |
+
"\n",
|
915 |
+
"\u001b[34m\u001b[1mAMP: \u001b[0mchecks passed ✅\n",
|
916 |
+
"\u001b[34m\u001b[1moptimizer:\u001b[0m SGD(lr=0.01) with parameter groups 286 weight(decay=0.0), 303 weight(decay=0.00046875), 301 bias\n",
|
917 |
+
"Using SyncBatchNorm()\n",
|
918 |
+
"\u001b[34m\u001b[1malbumentations: \u001b[0mBlur(p=0.01, blur_limit=(3, 7)), MedianBlur(p=0.01, blur_limit=(3, 7)), ToGray(p=0.01), CLAHE(p=0.01, clip_limit=(1, 4.0), tile_grid_size=(8, 8))\n",
|
919 |
+
"\u001b[34m\u001b[1mtrain: \u001b[0mScanning /workspace/road damage/train/labels... 4672 images, 345 backgrou\u001b[0m\n",
|
920 |
+
"\u001b[34m\u001b[1mtrain: \u001b[0mWARNING ⚠️ /workspace/road damage/train/images/Japan_006916_jpg.rf.5dc73c813f7053289379ab9cf4e81173.jpg: 1 duplicate labels removed\n",
|
921 |
+
"\u001b[34m\u001b[1mtrain: \u001b[0mNew cache created: /workspace/road damage/train/labels.cache\n",
|
922 |
+
"\u001b[34m\u001b[1mval: \u001b[0mScanning /workspace/road damage/valid/labels... 450 images, 34 backgrounds,\u001b[0m\n",
|
923 |
+
"\u001b[34m\u001b[1mval: \u001b[0mNew cache created: /workspace/road damage/valid/labels.cache\n",
|
924 |
+
"Plotting labels to runs/train/yolov9-c4/labels.jpg... \n",
|
925 |
+
"Image sizes 640 train, 640 val\n",
|
926 |
+
"Using 16 dataloader workers\n",
|
927 |
+
"Logging results to \u001b[1mruns/train/yolov9-c4\u001b[0m\n",
|
928 |
+
"Starting training for 500 epochs...\n",
|
929 |
+
"\n",
|
930 |
+
" Epoch GPU_mem box_loss cls_loss dfl_loss Instances Size\n",
|
931 |
+
" 0/499 20.4G 4.8 6.503 5.217 74 640: Exception in thread Thread-11 (plot_images):\n",
|
932 |
+
"Traceback (most recent call last):\n",
|
933 |
+
" File \"/opt/conda/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
|
934 |
+
" self.run()\n",
|
935 |
+
" File \"/opt/conda/lib/python3.10/threading.py\", line 953, in run\n",
|
936 |
+
" self._target(*self._args, **self._kwargs)\n",
|
937 |
+
" File \"/workspace/yolov9/utils/plots.py\", line 300, in plot_images\n",
|
938 |
+
" annotator.box_label(box, label, color=color)\n",
|
939 |
+
" File \"/workspace/yolov9/utils/plots.py\", line 86, in box_label\n",
|
940 |
+
" w, h = self.font.getsize(label) # text width, height\n",
|
941 |
+
"AttributeError: 'FreeTypeFont' object has no attribute 'getsize'\n",
|
942 |
+
" 0/499 20.4G 4.903 6.657 5.212 61 640: Exception in thread Thread-12 (plot_images):\n",
|
943 |
+
"Traceback (most recent call last):\n",
|
944 |
+
" File \"/opt/conda/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
|
945 |
+
" self.run()\n",
|
946 |
+
" File \"/opt/conda/lib/python3.10/threading.py\", line 953, in run\n",
|
947 |
+
" self._target(*self._args, **self._kwargs)\n",
|
948 |
+
" File \"/workspace/yolov9/utils/plots.py\", line 300, in plot_images\n",
|
949 |
+
" annotator.box_label(box, label, color=color)\n",
|
950 |
+
" File \"/workspace/yolov9/utils/plots.py\", line 86, in box_label\n",
|
951 |
+
" w, h = self.font.getsize(label) # text width, height\n",
|
952 |
+
"AttributeError: 'FreeTypeFont' object has no attribute 'getsize'\n",
|
953 |
+
" 0/499 20.4G 4.703 6.511 5.215 57 640: Exception in thread Thread-13 (plot_images):\n",
|
954 |
+
"Traceback (most recent call last):\n",
|
955 |
+
" File \"/opt/conda/lib/python3.10/threading.py\", line 1016, in _bootstrap_inner\n",
|
956 |
+
" self.run()\n",
|
957 |
+
" File \"/opt/conda/lib/python3.10/threading.py\", line 953, in run\n",
|
958 |
+
" self._target(*self._args, **self._kwargs)\n",
|
959 |
+
" File \"/workspace/yolov9/utils/plots.py\", line 300, in plot_images\n",
|
960 |
+
" annotator.box_label(box, label, color=color)\n",
|
961 |
+
" File \"/workspace/yolov9/utils/plots.py\", line 86, in box_label\n",
|
962 |
+
" w, h = self.font.getsize(label) # text width, height\n",
|
963 |
+
"AttributeError: 'FreeTypeFont' object has no attribute 'getsize'\n",
|
964 |
+
" 0/499 20.5G 4.674 6.804 5.251 49 640: ^C\n",
|
965 |
+
"[2024-06-02 02:12:52,085] torch.distributed.elastic.agent.server.api: [WARNING] Received Signals.SIGINT death signal, shutting down workers\n",
|
966 |
+
"[2024-06-02 02:12:52,086] torch.distributed.elastic.multiprocessing.api: [WARNING] Sending process 11363 closing signal SIGINT\n",
|
967 |
+
"[2024-06-02 02:12:52,086] torch.distributed.elastic.multiprocessing.api: [WARNING] Sending process 11364 closing signal SIGINT\n",
|
968 |
+
" 0/499 20.5G 4.674 6.804 5.251 49 640: \n",
|
969 |
+
"Traceback (most recent call last):\n",
|
970 |
+
" File \"/workspace/yolov9/train_dual.py\", line 644, in <module>\n",
|
971 |
+
" main(opt)\n",
|
972 |
+
" File \"/workspace/yolov9/train_dual.py\", line 538, in main\n",
|
973 |
+
" train(opt.hyp, opt, device, callbacks)\n",
|
974 |
+
" File \"/workspace/yolov9/train_dual.py\", line 314, in train\n",
|
975 |
+
" pred = model(imgs) # forward\n",
|
976 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
977 |
+
" return self._call_impl(*args, **kwargs)\n",
|
978 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
979 |
+
" return forward_call(*args, **kwargs)\n",
|
980 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/parallel/distributed.py\", line 1523, in forward\n",
|
981 |
+
" else self._run_ddp_forward(*inputs, **kwargs)\n",
|
982 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/parallel/distributed.py\", line 1359, in _run_ddp_forward\n",
|
983 |
+
" return self.module(*inputs, **kwargs) # type: ignore[index]\n",
|
984 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
985 |
+
" return self._call_impl(*args, **kwargs)\n",
|
986 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
987 |
+
" return forward_call(*args, **kwargs)\n",
|
988 |
+
" File \"/workspace/yolov9/models/yolo.py\", line 633, in forward\n",
|
989 |
+
" return self._forward_once(x, profile, visualize) # single-scale inference, train\n",
|
990 |
+
" File \"/workspace/yolov9/models/yolo.py\", line 533, in _forward_once\n",
|
991 |
+
" x = m(x) # run\n",
|
992 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
993 |
+
" return self._call_impl(*args, **kwargs)\n",
|
994 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
995 |
+
" return forward_call(*args, **kwargs)\n",
|
996 |
+
" File \"/workspace/yolov9/models/yolo.py\", line 225, in forward\n",
|
997 |
+
" d1.append(torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1))\n",
|
998 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
999 |
+
" return self._call_impl(*args, **kwargs)\n",
|
1000 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
1001 |
+
" return forward_call(*args, **kwargs)\n",
|
1002 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/container.py\", line 217, in forward\n",
|
1003 |
+
" input = module(input)\n",
|
1004 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
1005 |
+
" return self._call_impl(*args, **kwargs)\n",
|
1006 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
1007 |
+
" return forward_call(*args, **kwargs)\n",
|
1008 |
+
" File \"/workspace/yolov9/models/common.py\", line 54, in forward\n",
|
1009 |
+
" return self.act(self.bn(self.conv(x)))\n",
|
1010 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
1011 |
+
" return self._call_impl(*args, **kwargs)\n",
|
1012 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
1013 |
+
" return forward_call(*args, **kwargs)\n",
|
1014 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/batchnorm.py\", line 767, in forward\n",
|
1015 |
+
" return sync_batch_norm.apply(\n",
|
1016 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/autograd/function.py\", line 553, in apply\n",
|
1017 |
+
" return super().apply(*args, **kwargs) # type: ignore[misc]\n",
|
1018 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/_functions.py\", line 89, in forward\n",
|
1019 |
+
" counts = count_all.view(-1)\n",
|
1020 |
+
"KeyboardInterrupt\n",
|
1021 |
+
"Traceback (most recent call last):\n",
|
1022 |
+
" File \"/workspace/yolov9/train_dual.py\", line 644, in <module>\n",
|
1023 |
+
" main(opt)\n",
|
1024 |
+
" File \"/workspace/yolov9/train_dual.py\", line 538, in main\n",
|
1025 |
+
" train(opt.hyp, opt, device, callbacks)\n",
|
1026 |
+
" File \"/workspace/yolov9/train_dual.py\", line 314, in train\n",
|
1027 |
+
" pred = model(imgs) # forward\n",
|
1028 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
1029 |
+
" return self._call_impl(*args, **kwargs)\n",
|
1030 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
1031 |
+
" return forward_call(*args, **kwargs)\n",
|
1032 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/parallel/distributed.py\", line 1523, in forward\n",
|
1033 |
+
" else self._run_ddp_forward(*inputs, **kwargs)\n",
|
1034 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/parallel/distributed.py\", line 1359, in _run_ddp_forward\n",
|
1035 |
+
" return self.module(*inputs, **kwargs) # type: ignore[index]\n",
|
1036 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
1037 |
+
" return self._call_impl(*args, **kwargs)\n",
|
1038 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
1039 |
+
" return forward_call(*args, **kwargs)\n",
|
1040 |
+
" File \"/workspace/yolov9/models/yolo.py\", line 633, in forward\n",
|
1041 |
+
" return self._forward_once(x, profile, visualize) # single-scale inference, train\n",
|
1042 |
+
" File \"/workspace/yolov9/models/yolo.py\", line 533, in _forward_once\n",
|
1043 |
+
" x = m(x) # run\n",
|
1044 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
1045 |
+
" return self._call_impl(*args, **kwargs)\n",
|
1046 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
1047 |
+
" return forward_call(*args, **kwargs)\n",
|
1048 |
+
" File \"/workspace/yolov9/models/yolo.py\", line 225, in forward\n",
|
1049 |
+
" d1.append(torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1))\n",
|
1050 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
1051 |
+
" return self._call_impl(*args, **kwargs)\n",
|
1052 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
1053 |
+
" return forward_call(*args, **kwargs)\n",
|
1054 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/container.py\", line 217, in forward\n",
|
1055 |
+
" input = module(input)\n",
|
1056 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
1057 |
+
" return self._call_impl(*args, **kwargs)\n",
|
1058 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
1059 |
+
" return forward_call(*args, **kwargs)\n",
|
1060 |
+
" File \"/workspace/yolov9/models/common.py\", line 54, in forward\n",
|
1061 |
+
" return self.act(self.bn(self.conv(x)))\n",
|
1062 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1511, in _wrapped_call_impl\n",
|
1063 |
+
" return self._call_impl(*args, **kwargs)\n",
|
1064 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/module.py\", line 1520, in _call_impl\n",
|
1065 |
+
" return forward_call(*args, **kwargs)\n",
|
1066 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/batchnorm.py\", line 767, in forward\n",
|
1067 |
+
" return sync_batch_norm.apply(\n",
|
1068 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/autograd/function.py\", line 553, in apply\n",
|
1069 |
+
" return super().apply(*args, **kwargs) # type: ignore[misc]\n",
|
1070 |
+
" File \"/opt/conda/lib/python3.10/site-packages/torch/nn/modules/_functions.py\", line 89, in forward\n",
|
1071 |
+
" counts = count_all.view(-1)\n",
|
1072 |
+
"KeyboardInterrupt\n"
|
1073 |
+
]
|
1074 |
+
}
|
1075 |
+
],
|
1076 |
+
"source": [
|
1077 |
+
"!torchrun --nproc_per_node 2 --master_port 9527 train_dual.py \\\n",
|
1078 |
+
"--workers 8 --device 0,1 --sync-bn --batch 30 --data '/workspace/road damage/data.yaml' \\\n",
|
1079 |
+
"--img 640 --cfg models/detect/yolov9-c.yaml --weights '' --name yolov9-c --hyp hyp.scratch-high.yaml \\\n",
|
1080 |
+
"--min-items 0 --epochs 500 --close-mosaic 15"
|
1081 |
+
]
|
1082 |
+
},
|
1083 |
+
{
|
1084 |
+
"cell_type": "code",
|
1085 |
+
"execution_count": null,
|
1086 |
+
"metadata": {},
|
1087 |
+
"outputs": [],
|
1088 |
+
"source": [
|
1089 |
+
"!torchrun --nproc_per_node 2 --master_port 9527 train_dual.py \\\n",
|
1090 |
+
"--workers 8 --device 0,1 --sync-bn --batch 30 --data '/workspace/road damage/data.yaml' \\\n",
|
1091 |
+
"--img 640 --cfg models/detect/yolov9-c.yaml --weights '' --name yolov9-c --hyp hyp.scratch-high.yaml \\\n",
|
1092 |
+
"--min-items 0 --epochs 500 --close-mosaic 15"
|
1093 |
+
]
|
1094 |
+
}
|
1095 |
+
],
|
1096 |
+
"metadata": {
|
1097 |
+
"accelerator": "GPU",
|
1098 |
+
"colab": {
|
1099 |
+
"gpuType": "L4",
|
1100 |
+
"machine_shape": "hm",
|
1101 |
+
"provenance": []
|
1102 |
+
},
|
1103 |
+
"kernelspec": {
|
1104 |
+
"display_name": "Python 3 (ipykernel)",
|
1105 |
+
"language": "python",
|
1106 |
+
"name": "python3"
|
1107 |
+
},
|
1108 |
+
"language_info": {
|
1109 |
+
"codemirror_mode": {
|
1110 |
+
"name": "ipython",
|
1111 |
+
"version": 3
|
1112 |
+
},
|
1113 |
+
"file_extension": ".py",
|
1114 |
+
"mimetype": "text/x-python",
|
1115 |
+
"name": "python",
|
1116 |
+
"nbconvert_exporter": "python",
|
1117 |
+
"pygments_lexer": "ipython3",
|
1118 |
+
"version": "3.10.13"
|
1119 |
+
}
|
1120 |
+
},
|
1121 |
+
"nbformat": 4,
|
1122 |
+
"nbformat_minor": 4
|
1123 |
+
}
|
panoptic/predict.py
ADDED
@@ -0,0 +1,246 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import os
|
3 |
+
import platform
|
4 |
+
import sys
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
import torch
|
8 |
+
|
9 |
+
FILE = Path(__file__).resolve()
|
10 |
+
ROOT = FILE.parents[1] # YOLO root directory
|
11 |
+
if str(ROOT) not in sys.path:
|
12 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
13 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
14 |
+
|
15 |
+
from models.common import DetectMultiBackend
|
16 |
+
from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadScreenshots, LoadStreams
|
17 |
+
from utils.general import (LOGGER, Profile, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
|
18 |
+
increment_path, non_max_suppression, print_args, scale_boxes, scale_segments,
|
19 |
+
strip_optimizer, xyxy2xywh)
|
20 |
+
from utils.plots import Annotator, colors, save_one_box
|
21 |
+
from utils.segment.general import masks2segments, process_mask
|
22 |
+
from utils.torch_utils import select_device, smart_inference_mode
|
23 |
+
|
24 |
+
|
25 |
+
@smart_inference_mode()
|
26 |
+
def run(
|
27 |
+
weights=ROOT / 'yolo-pan.pt', # model.pt path(s)
|
28 |
+
source=ROOT / 'data/images', # file/dir/URL/glob/screen/0(webcam)
|
29 |
+
data=ROOT / 'data/coco128.yaml', # dataset.yaml path
|
30 |
+
imgsz=(640, 640), # inference size (height, width)
|
31 |
+
conf_thres=0.25, # confidence threshold
|
32 |
+
iou_thres=0.45, # NMS IOU threshold
|
33 |
+
max_det=1000, # maximum detections per image
|
34 |
+
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
|
35 |
+
view_img=False, # show results
|
36 |
+
save_txt=False, # save results to *.txt
|
37 |
+
save_conf=False, # save confidences in --save-txt labels
|
38 |
+
save_crop=False, # save cropped prediction boxes
|
39 |
+
nosave=False, # do not save images/videos
|
40 |
+
classes=None, # filter by class: --class 0, or --class 0 2 3
|
41 |
+
agnostic_nms=False, # class-agnostic NMS
|
42 |
+
augment=False, # augmented inference
|
43 |
+
visualize=False, # visualize features
|
44 |
+
update=False, # update all models
|
45 |
+
project=ROOT / 'runs/predict-seg', # save results to project/name
|
46 |
+
name='exp', # save results to project/name
|
47 |
+
exist_ok=False, # existing project/name ok, do not increment
|
48 |
+
line_thickness=3, # bounding box thickness (pixels)
|
49 |
+
hide_labels=False, # hide labels
|
50 |
+
hide_conf=False, # hide confidences
|
51 |
+
half=False, # use FP16 half-precision inference
|
52 |
+
dnn=False, # use OpenCV DNN for ONNX inference
|
53 |
+
vid_stride=1, # video frame-rate stride
|
54 |
+
retina_masks=False,
|
55 |
+
):
|
56 |
+
source = str(source)
|
57 |
+
save_img = not nosave and not source.endswith('.txt') # save inference images
|
58 |
+
is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
|
59 |
+
is_url = source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
|
60 |
+
webcam = source.isnumeric() or source.endswith('.txt') or (is_url and not is_file)
|
61 |
+
screenshot = source.lower().startswith('screen')
|
62 |
+
if is_url and is_file:
|
63 |
+
source = check_file(source) # download
|
64 |
+
|
65 |
+
# Directories
|
66 |
+
save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
|
67 |
+
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
|
68 |
+
|
69 |
+
# Load model
|
70 |
+
device = select_device(device)
|
71 |
+
model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
|
72 |
+
stride, names, pt = model.stride, model.names, model.pt
|
73 |
+
imgsz = check_img_size(imgsz, s=stride) # check image size
|
74 |
+
|
75 |
+
# Dataloader
|
76 |
+
bs = 1 # batch_size
|
77 |
+
if webcam:
|
78 |
+
view_img = check_imshow(warn=True)
|
79 |
+
dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
|
80 |
+
bs = len(dataset)
|
81 |
+
elif screenshot:
|
82 |
+
dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt)
|
83 |
+
else:
|
84 |
+
dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
|
85 |
+
vid_path, vid_writer = [None] * bs, [None] * bs
|
86 |
+
|
87 |
+
# Run inference
|
88 |
+
model.warmup(imgsz=(1 if pt else bs, 3, *imgsz)) # warmup
|
89 |
+
seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
|
90 |
+
for path, im, im0s, vid_cap, s in dataset:
|
91 |
+
with dt[0]:
|
92 |
+
im = torch.from_numpy(im).to(model.device)
|
93 |
+
im = im.half() if model.fp16 else im.float() # uint8 to fp16/32
|
94 |
+
im /= 255 # 0 - 255 to 0.0 - 1.0
|
95 |
+
if len(im.shape) == 3:
|
96 |
+
im = im[None] # expand for batch dim
|
97 |
+
|
98 |
+
# Inference
|
99 |
+
with dt[1]:
|
100 |
+
visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False
|
101 |
+
pred, proto = model(im, augment=augment, visualize=visualize)[:2]
|
102 |
+
|
103 |
+
# NMS
|
104 |
+
with dt[2]:
|
105 |
+
pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det, nm=32)
|
106 |
+
|
107 |
+
# Second-stage classifier (optional)
|
108 |
+
# pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)
|
109 |
+
|
110 |
+
# Process predictions
|
111 |
+
for i, det in enumerate(pred): # per image
|
112 |
+
seen += 1
|
113 |
+
if webcam: # batch_size >= 1
|
114 |
+
p, im0, frame = path[i], im0s[i].copy(), dataset.count
|
115 |
+
s += f'{i}: '
|
116 |
+
else:
|
117 |
+
p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
|
118 |
+
|
119 |
+
p = Path(p) # to Path
|
120 |
+
save_path = str(save_dir / p.name) # im.jpg
|
121 |
+
txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # im.txt
|
122 |
+
s += '%gx%g ' % im.shape[2:] # print string
|
123 |
+
imc = im0.copy() if save_crop else im0 # for save_crop
|
124 |
+
annotator = Annotator(im0, line_width=line_thickness, example=str(names))
|
125 |
+
if len(det):
|
126 |
+
masks = process_mask(proto[i], det[:, 6:], det[:, :4], im.shape[2:], upsample=True) # HWC
|
127 |
+
det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round() # rescale boxes to im0 size
|
128 |
+
|
129 |
+
# Segments
|
130 |
+
if save_txt:
|
131 |
+
segments = reversed(masks2segments(masks))
|
132 |
+
segments = [scale_segments(im.shape[2:], x, im0.shape, normalize=True) for x in segments]
|
133 |
+
|
134 |
+
# Print results
|
135 |
+
for c in det[:, 5].unique():
|
136 |
+
n = (det[:, 5] == c).sum() # detections per class
|
137 |
+
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
|
138 |
+
|
139 |
+
# Mask plotting
|
140 |
+
annotator.masks(masks,
|
141 |
+
colors=[colors(x, True) for x in det[:, 5]],
|
142 |
+
im_gpu=None if retina_masks else im[i])
|
143 |
+
|
144 |
+
# Write results
|
145 |
+
for j, (*xyxy, conf, cls) in enumerate(reversed(det[:, :6])):
|
146 |
+
if save_txt: # Write to file
|
147 |
+
segj = segments[j].reshape(-1) # (n,2) to (n*2)
|
148 |
+
line = (cls, *segj, conf) if save_conf else (cls, *segj) # label format
|
149 |
+
with open(f'{txt_path}.txt', 'a') as f:
|
150 |
+
f.write(('%g ' * len(line)).rstrip() % line + '\n')
|
151 |
+
|
152 |
+
if save_img or save_crop or view_img: # Add bbox to image
|
153 |
+
c = int(cls) # integer class
|
154 |
+
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
|
155 |
+
annotator.box_label(xyxy, label, color=colors(c, True))
|
156 |
+
# annotator.draw.polygon(segments[j], outline=colors(c, True), width=3)
|
157 |
+
if save_crop:
|
158 |
+
save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True)
|
159 |
+
|
160 |
+
# Stream results
|
161 |
+
im0 = annotator.result()
|
162 |
+
if view_img:
|
163 |
+
if platform.system() == 'Linux' and p not in windows:
|
164 |
+
windows.append(p)
|
165 |
+
cv2.namedWindow(str(p), cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux)
|
166 |
+
cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0])
|
167 |
+
cv2.imshow(str(p), im0)
|
168 |
+
if cv2.waitKey(1) == ord('q'): # 1 millisecond
|
169 |
+
exit()
|
170 |
+
|
171 |
+
# Save results (image with detections)
|
172 |
+
if save_img:
|
173 |
+
if dataset.mode == 'image':
|
174 |
+
cv2.imwrite(save_path, im0)
|
175 |
+
else: # 'video' or 'stream'
|
176 |
+
if vid_path[i] != save_path: # new video
|
177 |
+
vid_path[i] = save_path
|
178 |
+
if isinstance(vid_writer[i], cv2.VideoWriter):
|
179 |
+
vid_writer[i].release() # release previous video writer
|
180 |
+
if vid_cap: # video
|
181 |
+
fps = vid_cap.get(cv2.CAP_PROP_FPS)
|
182 |
+
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
183 |
+
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
184 |
+
else: # stream
|
185 |
+
fps, w, h = 30, im0.shape[1], im0.shape[0]
|
186 |
+
save_path = str(Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos
|
187 |
+
vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
|
188 |
+
vid_writer[i].write(im0)
|
189 |
+
|
190 |
+
# Print time (inference-only)
|
191 |
+
LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms")
|
192 |
+
|
193 |
+
# Print results
|
194 |
+
t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image
|
195 |
+
LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {(1, 3, *imgsz)}' % t)
|
196 |
+
if save_txt or save_img:
|
197 |
+
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
|
198 |
+
LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
|
199 |
+
if update:
|
200 |
+
strip_optimizer(weights[0]) # update model (to fix SourceChangeWarning)
|
201 |
+
|
202 |
+
|
203 |
+
def parse_opt():
|
204 |
+
parser = argparse.ArgumentParser()
|
205 |
+
parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolo-pan.pt', help='model path(s)')
|
206 |
+
parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob/screen/0(webcam)')
|
207 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')
|
208 |
+
parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
|
209 |
+
parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')
|
210 |
+
parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')
|
211 |
+
parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
|
212 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
213 |
+
parser.add_argument('--view-img', action='store_true', help='show results')
|
214 |
+
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
|
215 |
+
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
|
216 |
+
parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
|
217 |
+
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
|
218 |
+
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
|
219 |
+
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
|
220 |
+
parser.add_argument('--augment', action='store_true', help='augmented inference')
|
221 |
+
parser.add_argument('--visualize', action='store_true', help='visualize features')
|
222 |
+
parser.add_argument('--update', action='store_true', help='update all models')
|
223 |
+
parser.add_argument('--project', default=ROOT / 'runs/predict-seg', help='save results to project/name')
|
224 |
+
parser.add_argument('--name', default='exp', help='save results to project/name')
|
225 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
226 |
+
parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
|
227 |
+
parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
|
228 |
+
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
|
229 |
+
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
|
230 |
+
parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
|
231 |
+
parser.add_argument('--vid-stride', type=int, default=1, help='video frame-rate stride')
|
232 |
+
parser.add_argument('--retina-masks', action='store_true', help='whether to plot masks in native resolution')
|
233 |
+
opt = parser.parse_args()
|
234 |
+
opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand
|
235 |
+
print_args(vars(opt))
|
236 |
+
return opt
|
237 |
+
|
238 |
+
|
239 |
+
def main(opt):
|
240 |
+
check_requirements(exclude=('tensorboard', 'thop'))
|
241 |
+
run(**vars(opt))
|
242 |
+
|
243 |
+
|
244 |
+
if __name__ == "__main__":
|
245 |
+
opt = parse_opt()
|
246 |
+
main(opt)
|
panoptic/train.py
ADDED
@@ -0,0 +1,662 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import math
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import sys
|
6 |
+
import time
|
7 |
+
from copy import deepcopy
|
8 |
+
from datetime import datetime
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
import numpy as np
|
12 |
+
import torch
|
13 |
+
import torch.distributed as dist
|
14 |
+
import torch.nn as nn
|
15 |
+
import yaml
|
16 |
+
from torch.optim import lr_scheduler
|
17 |
+
from tqdm import tqdm
|
18 |
+
|
19 |
+
FILE = Path(__file__).resolve()
|
20 |
+
ROOT = FILE.parents[1] # YOLO root directory
|
21 |
+
if str(ROOT) not in sys.path:
|
22 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
23 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
24 |
+
|
25 |
+
import panoptic.val as validate # for end-of-epoch mAP
|
26 |
+
from models.experimental import attempt_load
|
27 |
+
from models.yolo import SegmentationModel
|
28 |
+
from utils.autoanchor import check_anchors
|
29 |
+
from utils.autobatch import check_train_batch_size
|
30 |
+
from utils.callbacks import Callbacks
|
31 |
+
from utils.downloads import attempt_download, is_url
|
32 |
+
from utils.general import (LOGGER, TQDM_BAR_FORMAT, check_amp, check_dataset, check_file, check_git_info,
|
33 |
+
check_git_status, check_img_size, check_requirements, check_suffix, check_yaml, colorstr,
|
34 |
+
get_latest_run, increment_path, init_seeds, intersect_dicts, labels_to_class_weights,
|
35 |
+
labels_to_image_weights, one_cycle, one_flat_cycle, print_args, print_mutation, strip_optimizer, yaml_save)
|
36 |
+
from utils.loggers import GenericLogger
|
37 |
+
from utils.plots import plot_evolve, plot_labels
|
38 |
+
from utils.panoptic.dataloaders import create_dataloader
|
39 |
+
from utils.panoptic.loss_tal import ComputeLoss
|
40 |
+
from utils.panoptic.metrics import KEYS, fitness
|
41 |
+
from utils.panoptic.plots import plot_images_and_masks, plot_results_with_masks
|
42 |
+
from utils.torch_utils import (EarlyStopping, ModelEMA, de_parallel, select_device, smart_DDP, smart_optimizer,
|
43 |
+
smart_resume, torch_distributed_zero_first)
|
44 |
+
|
45 |
+
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
|
46 |
+
RANK = int(os.getenv('RANK', -1))
|
47 |
+
WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))
|
48 |
+
GIT_INFO = None#check_git_info()
|
49 |
+
|
50 |
+
|
51 |
+
def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictionary
|
52 |
+
save_dir, epochs, batch_size, weights, single_cls, evolve, data, cfg, resume, noval, nosave, workers, freeze, mask_ratio = \
|
53 |
+
Path(opt.save_dir), opt.epochs, opt.batch_size, opt.weights, opt.single_cls, opt.evolve, opt.data, opt.cfg, \
|
54 |
+
opt.resume, opt.noval, opt.nosave, opt.workers, opt.freeze, opt.mask_ratio
|
55 |
+
# callbacks.run('on_pretrain_routine_start')
|
56 |
+
|
57 |
+
# Directories
|
58 |
+
w = save_dir / 'weights' # weights dir
|
59 |
+
(w.parent if evolve else w).mkdir(parents=True, exist_ok=True) # make dir
|
60 |
+
last, best = w / 'last.pt', w / 'best.pt'
|
61 |
+
|
62 |
+
# Hyperparameters
|
63 |
+
if isinstance(hyp, str):
|
64 |
+
with open(hyp, errors='ignore') as f:
|
65 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
66 |
+
LOGGER.info(colorstr('hyperparameters: ') + ', '.join(f'{k}={v}' for k, v in hyp.items()))
|
67 |
+
opt.hyp = hyp.copy() # for saving hyps to checkpoints
|
68 |
+
|
69 |
+
# Save run settings
|
70 |
+
if not evolve:
|
71 |
+
yaml_save(save_dir / 'hyp.yaml', hyp)
|
72 |
+
yaml_save(save_dir / 'opt.yaml', vars(opt))
|
73 |
+
|
74 |
+
# Loggers
|
75 |
+
data_dict = None
|
76 |
+
if RANK in {-1, 0}:
|
77 |
+
logger = GenericLogger(opt=opt, console_logger=LOGGER)
|
78 |
+
|
79 |
+
# Config
|
80 |
+
plots = not evolve and not opt.noplots # create plots
|
81 |
+
overlap = not opt.no_overlap
|
82 |
+
cuda = device.type != 'cpu'
|
83 |
+
init_seeds(opt.seed + 1 + RANK, deterministic=True)
|
84 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
85 |
+
data_dict = data_dict or check_dataset(data) # check if None
|
86 |
+
train_path, val_path = data_dict['train'], data_dict['val']
|
87 |
+
nc = 1 if single_cls else int(data_dict['nc']) # number of classes
|
88 |
+
names = {0: 'item'} if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
|
89 |
+
#is_coco = isinstance(val_path, str) and val_path.endswith('coco/val2017.txt') # COCO dataset
|
90 |
+
is_coco = isinstance(val_path, str) and val_path.endswith('val2017.txt') # COCO dataset
|
91 |
+
|
92 |
+
# Model
|
93 |
+
check_suffix(weights, '.pt') # check weights
|
94 |
+
pretrained = weights.endswith('.pt')
|
95 |
+
if pretrained:
|
96 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
97 |
+
weights = attempt_download(weights) # download if not found locally
|
98 |
+
ckpt = torch.load(weights, map_location='cpu') # load checkpoint to CPU to avoid CUDA memory leak
|
99 |
+
model = SegmentationModel(cfg or ckpt['model'].yaml, ch=3, nc=nc).to(device)
|
100 |
+
exclude = ['anchor'] if (cfg or hyp.get('anchors')) and not resume else [] # exclude keys
|
101 |
+
csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
|
102 |
+
csd = intersect_dicts(csd, model.state_dict(), exclude=exclude) # intersect
|
103 |
+
model.load_state_dict(csd, strict=False) # load
|
104 |
+
LOGGER.info(f'Transferred {len(csd)}/{len(model.state_dict())} items from {weights}') # report
|
105 |
+
else:
|
106 |
+
model = SegmentationModel(cfg, ch=3, nc=nc).to(device) # create
|
107 |
+
amp = check_amp(model) # check AMP
|
108 |
+
|
109 |
+
# Freeze
|
110 |
+
freeze = [f'model.{x}.' for x in (freeze if len(freeze) > 1 else range(freeze[0]))] # layers to freeze
|
111 |
+
for k, v in model.named_parameters():
|
112 |
+
#v.requires_grad = True # train all layers
|
113 |
+
# v.register_hook(lambda x: torch.nan_to_num(x)) # NaN to 0 (commented for erratic training results)
|
114 |
+
if any(x in k for x in freeze):
|
115 |
+
LOGGER.info(f'freezing {k}')
|
116 |
+
v.requires_grad = False
|
117 |
+
|
118 |
+
# Image size
|
119 |
+
gs = max(int(model.stride.max()), 32) # grid size (max stride)
|
120 |
+
imgsz = check_img_size(opt.imgsz, gs, floor=gs * 2) # verify imgsz is gs-multiple
|
121 |
+
|
122 |
+
# Batch size
|
123 |
+
if RANK == -1 and batch_size == -1: # single-GPU only, estimate best batch size
|
124 |
+
batch_size = check_train_batch_size(model, imgsz, amp)
|
125 |
+
logger.update_params({"batch_size": batch_size})
|
126 |
+
# loggers.on_params_update({"batch_size": batch_size})
|
127 |
+
|
128 |
+
# Optimizer
|
129 |
+
nbs = 64 # nominal batch size
|
130 |
+
accumulate = max(round(nbs / batch_size), 1) # accumulate loss before optimizing
|
131 |
+
hyp['weight_decay'] *= batch_size * accumulate / nbs # scale weight_decay
|
132 |
+
optimizer = smart_optimizer(model, opt.optimizer, hyp['lr0'], hyp['momentum'], hyp['weight_decay'])
|
133 |
+
|
134 |
+
# Scheduler
|
135 |
+
if opt.cos_lr:
|
136 |
+
lf = one_cycle(1, hyp['lrf'], epochs) # cosine 1->hyp['lrf']
|
137 |
+
elif opt.flat_cos_lr:
|
138 |
+
lf = one_flat_cycle(1, hyp['lrf'], epochs) # cosine 1->hyp['lrf']
|
139 |
+
elif opt.fixed_lr:
|
140 |
+
lf = lambda x: 1.0
|
141 |
+
elif opt.poly_lr:
|
142 |
+
power = 0.9
|
143 |
+
lf = lambda x: ((1 - (x / epochs)) ** power) * (1.0 - hyp['lrf']) + hyp['lrf']
|
144 |
+
else:
|
145 |
+
lf = lambda x: (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf'] # linear
|
146 |
+
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf) # plot_lr_scheduler(optimizer, scheduler, epochs)
|
147 |
+
|
148 |
+
# EMA
|
149 |
+
ema = ModelEMA(model) if RANK in {-1, 0} else None
|
150 |
+
|
151 |
+
# Resume
|
152 |
+
best_fitness, start_epoch = 0.0, 0
|
153 |
+
if pretrained:
|
154 |
+
if resume:
|
155 |
+
best_fitness, start_epoch, epochs = smart_resume(ckpt, optimizer, ema, weights, epochs, resume)
|
156 |
+
del ckpt, csd
|
157 |
+
|
158 |
+
# DP mode
|
159 |
+
if cuda and RANK == -1 and torch.cuda.device_count() > 1:
|
160 |
+
LOGGER.warning('WARNING ⚠️ DP not recommended, use torch.distributed.run for best DDP Multi-GPU results.')
|
161 |
+
model = torch.nn.DataParallel(model)
|
162 |
+
|
163 |
+
# SyncBatchNorm
|
164 |
+
if opt.sync_bn and cuda and RANK != -1:
|
165 |
+
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model).to(device)
|
166 |
+
LOGGER.info('Using SyncBatchNorm()')
|
167 |
+
|
168 |
+
# Trainloader
|
169 |
+
train_loader, dataset = create_dataloader(
|
170 |
+
train_path,
|
171 |
+
imgsz,
|
172 |
+
batch_size // WORLD_SIZE,
|
173 |
+
gs,
|
174 |
+
single_cls,
|
175 |
+
hyp=hyp,
|
176 |
+
augment=True,
|
177 |
+
cache=None if opt.cache == 'val' else opt.cache,
|
178 |
+
rect=opt.rect,
|
179 |
+
rank=LOCAL_RANK,
|
180 |
+
workers=workers,
|
181 |
+
image_weights=opt.image_weights,
|
182 |
+
close_mosaic=opt.close_mosaic != 0,
|
183 |
+
quad=opt.quad,
|
184 |
+
prefix=colorstr('train: '),
|
185 |
+
shuffle=True,
|
186 |
+
mask_downsample_ratio=mask_ratio,
|
187 |
+
overlap_mask=overlap,
|
188 |
+
)
|
189 |
+
labels = np.concatenate(dataset.labels, 0)
|
190 |
+
mlc = int(labels[:, 0].max()) # max label class
|
191 |
+
assert mlc < nc, f'Label class {mlc} exceeds nc={nc} in {data}. Possible class labels are 0-{nc - 1}'
|
192 |
+
|
193 |
+
# Process 0
|
194 |
+
if RANK in {-1, 0}:
|
195 |
+
val_loader = create_dataloader(val_path,
|
196 |
+
imgsz,
|
197 |
+
batch_size // WORLD_SIZE * 2,
|
198 |
+
gs,
|
199 |
+
single_cls,
|
200 |
+
hyp=hyp,
|
201 |
+
cache=None if noval else opt.cache,
|
202 |
+
rect=True,
|
203 |
+
rank=-1,
|
204 |
+
workers=workers * 2,
|
205 |
+
pad=0.5,
|
206 |
+
mask_downsample_ratio=mask_ratio,
|
207 |
+
overlap_mask=overlap,
|
208 |
+
prefix=colorstr('val: '))[0]
|
209 |
+
|
210 |
+
if not resume:
|
211 |
+
#if not opt.noautoanchor:
|
212 |
+
# check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz) # run AutoAnchor
|
213 |
+
model.half().float() # pre-reduce anchor precision
|
214 |
+
|
215 |
+
if plots:
|
216 |
+
plot_labels(labels, names, save_dir)
|
217 |
+
# callbacks.run('on_pretrain_routine_end', labels, names)
|
218 |
+
|
219 |
+
# DDP mode
|
220 |
+
if cuda and RANK != -1:
|
221 |
+
model = smart_DDP(model)
|
222 |
+
|
223 |
+
# Model attributes
|
224 |
+
nl = de_parallel(model).model[-1].nl # number of detection layers (to scale hyps)
|
225 |
+
#hyp['box'] *= 3 / nl # scale to layers
|
226 |
+
#hyp['cls'] *= nc / 80 * 3 / nl # scale to classes and layers
|
227 |
+
#hyp['obj'] *= (imgsz / 640) ** 2 * 3 / nl # scale to image size and layers
|
228 |
+
hyp['label_smoothing'] = opt.label_smoothing
|
229 |
+
model.nc = nc # attach number of classes to model
|
230 |
+
model.hyp = hyp # attach hyperparameters to model
|
231 |
+
model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) * nc # attach class weights
|
232 |
+
model.names = names
|
233 |
+
|
234 |
+
# Start training
|
235 |
+
t0 = time.time()
|
236 |
+
nb = len(train_loader) # number of batches
|
237 |
+
nw = max(round(hyp['warmup_epochs'] * nb), 100) # number of warmup iterations, max(3 epochs, 100 iterations)
|
238 |
+
# nw = min(nw, (epochs - start_epoch) / 2 * nb) # limit warmup to < 1/2 of training
|
239 |
+
last_opt_step = -1
|
240 |
+
maps = np.zeros(nc) # mAP per class
|
241 |
+
results = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) # P, R, [email protected], [email protected], val_loss(box, obj, cls)
|
242 |
+
scheduler.last_epoch = start_epoch - 1 # do not move
|
243 |
+
scaler = torch.cuda.amp.GradScaler(enabled=amp)
|
244 |
+
stopper, stop = EarlyStopping(patience=opt.patience), False
|
245 |
+
compute_loss = ComputeLoss(model, overlap=overlap) # init loss class
|
246 |
+
# callbacks.run('on_train_start')
|
247 |
+
LOGGER.info(f'Image sizes {imgsz} train, {imgsz} val\n'
|
248 |
+
f'Using {train_loader.num_workers * WORLD_SIZE} dataloader workers\n'
|
249 |
+
f"Logging results to {colorstr('bold', save_dir)}\n"
|
250 |
+
f'Starting training for {epochs} epochs...')
|
251 |
+
for epoch in range(start_epoch, epochs): # epoch ------------------------------------------------------------------
|
252 |
+
# callbacks.run('on_train_epoch_start')
|
253 |
+
model.train()
|
254 |
+
|
255 |
+
# Update image weights (optional, single-GPU only)
|
256 |
+
if opt.image_weights:
|
257 |
+
cw = model.class_weights.cpu().numpy() * (1 - maps) ** 2 / nc # class weights
|
258 |
+
iw = labels_to_image_weights(dataset.labels, nc=nc, class_weights=cw) # image weights
|
259 |
+
dataset.indices = random.choices(range(dataset.n), weights=iw, k=dataset.n) # rand weighted idx
|
260 |
+
if epoch == (epochs - opt.close_mosaic):
|
261 |
+
LOGGER.info("Closing dataloader mosaic")
|
262 |
+
dataset.mosaic = False
|
263 |
+
|
264 |
+
# Update mosaic border (optional)
|
265 |
+
# b = int(random.uniform(0.25 * imgsz, 0.75 * imgsz + gs) // gs * gs)
|
266 |
+
# dataset.mosaic_border = [b - imgsz, -b] # height, width borders
|
267 |
+
|
268 |
+
mloss = torch.zeros(6, device=device) # mean losses
|
269 |
+
if RANK != -1:
|
270 |
+
train_loader.sampler.set_epoch(epoch)
|
271 |
+
pbar = enumerate(train_loader)
|
272 |
+
LOGGER.info(('\n' + '%11s' * 10) %
|
273 |
+
('Epoch', 'GPU_mem', 'box_loss', 'seg_loss', 'cls_loss', 'dfl_loss', 'fcl_loss', 'dic_loss', 'Instances', 'Size'))
|
274 |
+
if RANK in {-1, 0}:
|
275 |
+
pbar = tqdm(pbar, total=nb, bar_format=TQDM_BAR_FORMAT) # progress bar
|
276 |
+
optimizer.zero_grad()
|
277 |
+
for i, (imgs, targets, paths, _, masks, semasks) in pbar: # batch ------------------------------------------------------
|
278 |
+
# callbacks.run('on_train_batch_start')
|
279 |
+
#print(imgs.shape)
|
280 |
+
#print(semasks.shape)
|
281 |
+
#print(masks.shape)
|
282 |
+
ni = i + nb * epoch # number integrated batches (since train start)
|
283 |
+
imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0
|
284 |
+
|
285 |
+
# Warmup
|
286 |
+
if ni <= nw:
|
287 |
+
xi = [0, nw] # x interp
|
288 |
+
# compute_loss.gr = np.interp(ni, xi, [0.0, 1.0]) # iou loss ratio (obj_loss = 1.0 or iou)
|
289 |
+
accumulate = max(1, np.interp(ni, xi, [1, nbs / batch_size]).round())
|
290 |
+
for j, x in enumerate(optimizer.param_groups):
|
291 |
+
# bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0
|
292 |
+
x['lr'] = np.interp(ni, xi, [hyp['warmup_bias_lr'] if j == 0 else 0.0, x['initial_lr'] * lf(epoch)])
|
293 |
+
if 'momentum' in x:
|
294 |
+
x['momentum'] = np.interp(ni, xi, [hyp['warmup_momentum'], hyp['momentum']])
|
295 |
+
|
296 |
+
# Multi-scale
|
297 |
+
if opt.multi_scale:
|
298 |
+
sz = random.randrange(imgsz * 0.5, imgsz * 1.5 + gs) // gs * gs # size
|
299 |
+
sf = sz / max(imgs.shape[2:]) # scale factor
|
300 |
+
if sf != 1:
|
301 |
+
ns = [math.ceil(x * sf / gs) * gs for x in imgs.shape[2:]] # new shape (stretched to gs-multiple)
|
302 |
+
imgs = nn.functional.interpolate(imgs, size=ns, mode='bilinear', align_corners=False)
|
303 |
+
|
304 |
+
# Forward
|
305 |
+
with torch.cuda.amp.autocast(amp):
|
306 |
+
pred = model(imgs) # forward
|
307 |
+
loss, loss_items = compute_loss(pred, targets.to(device), masks=masks.to(device).float(),
|
308 |
+
semasks=semasks.to(device).float())
|
309 |
+
if RANK != -1:
|
310 |
+
loss *= WORLD_SIZE # gradient averaged between devices in DDP mode
|
311 |
+
if opt.quad:
|
312 |
+
loss *= 4.
|
313 |
+
|
314 |
+
# Backward
|
315 |
+
torch.use_deterministic_algorithms(False)
|
316 |
+
scaler.scale(loss).backward()
|
317 |
+
|
318 |
+
# Optimize - https://pytorch.org/docs/master/notes/amp_examples.html
|
319 |
+
if ni - last_opt_step >= accumulate:
|
320 |
+
scaler.unscale_(optimizer) # unscale gradients
|
321 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=10.0) # clip gradients
|
322 |
+
scaler.step(optimizer) # optimizer.step
|
323 |
+
scaler.update()
|
324 |
+
optimizer.zero_grad()
|
325 |
+
if ema:
|
326 |
+
ema.update(model)
|
327 |
+
last_opt_step = ni
|
328 |
+
|
329 |
+
# Log
|
330 |
+
if RANK in {-1, 0}:
|
331 |
+
mloss = (mloss * i + loss_items) / (i + 1) # update mean losses
|
332 |
+
mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB)
|
333 |
+
pbar.set_description(('%11s' * 2 + '%11.4g' * 8) %
|
334 |
+
(f'{epoch}/{epochs - 1}', mem, *mloss, targets.shape[0], imgs.shape[-1]))
|
335 |
+
# callbacks.run('on_train_batch_end', model, ni, imgs, targets, paths)
|
336 |
+
# if callbacks.stop_training:
|
337 |
+
# return
|
338 |
+
|
339 |
+
# Mosaic plots
|
340 |
+
if plots:
|
341 |
+
if ni < 10:
|
342 |
+
plot_images_and_masks(imgs, targets, masks, semasks, paths, save_dir / f"train_batch{ni}.jpg")
|
343 |
+
if ni == 10:
|
344 |
+
files = sorted(save_dir.glob('train*.jpg'))
|
345 |
+
logger.log_images(files, "Mosaics", epoch)
|
346 |
+
# end batch ------------------------------------------------------------------------------------------------
|
347 |
+
|
348 |
+
# Scheduler
|
349 |
+
lr = [x['lr'] for x in optimizer.param_groups] # for loggers
|
350 |
+
scheduler.step()
|
351 |
+
|
352 |
+
if RANK in {-1, 0}:
|
353 |
+
# mAP
|
354 |
+
# callbacks.run('on_train_epoch_end', epoch=epoch)
|
355 |
+
ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'names', 'stride', 'class_weights'])
|
356 |
+
final_epoch = (epoch + 1 == epochs) or stopper.possible_stop
|
357 |
+
if not noval or final_epoch: # Calculate mAP
|
358 |
+
if (opt.save_period > 0 and epoch % opt.save_period == 0) or (epoch > (epochs - 2 * opt.close_mosaic)):
|
359 |
+
results, maps, _ = validate.run(data_dict,
|
360 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
361 |
+
imgsz=imgsz,
|
362 |
+
half=amp,
|
363 |
+
model=ema.ema,
|
364 |
+
single_cls=single_cls,
|
365 |
+
dataloader=val_loader,
|
366 |
+
save_dir=save_dir,
|
367 |
+
plots=False,
|
368 |
+
callbacks=callbacks,
|
369 |
+
compute_loss=compute_loss,
|
370 |
+
mask_downsample_ratio=mask_ratio,
|
371 |
+
overlap=overlap)
|
372 |
+
|
373 |
+
# Update best mAP
|
374 |
+
fi = fitness(np.array(results).reshape(1, -1)) # weighted combination of [P, R, [email protected], [email protected]]
|
375 |
+
stop = stopper(epoch=epoch, fitness=fi) # early stop check
|
376 |
+
if fi > best_fitness:
|
377 |
+
best_fitness = fi
|
378 |
+
log_vals = list(mloss) + list(results) + lr
|
379 |
+
# callbacks.run('on_fit_epoch_end', log_vals, epoch, best_fitness, fi)
|
380 |
+
# Log val metrics and media
|
381 |
+
metrics_dict = dict(zip(KEYS, log_vals))
|
382 |
+
logger.log_metrics(metrics_dict, epoch)
|
383 |
+
|
384 |
+
# Save model
|
385 |
+
if (not nosave) or (final_epoch and not evolve): # if save
|
386 |
+
ckpt = {
|
387 |
+
'epoch': epoch,
|
388 |
+
'best_fitness': best_fitness,
|
389 |
+
'model': deepcopy(de_parallel(model)).half(),
|
390 |
+
'ema': deepcopy(ema.ema).half(),
|
391 |
+
'updates': ema.updates,
|
392 |
+
'optimizer': optimizer.state_dict(),
|
393 |
+
'opt': vars(opt),
|
394 |
+
'git': GIT_INFO, # {remote, branch, commit} if a git repo
|
395 |
+
'date': datetime.now().isoformat()}
|
396 |
+
|
397 |
+
# Save last, best and delete
|
398 |
+
torch.save(ckpt, last)
|
399 |
+
if best_fitness == fi:
|
400 |
+
torch.save(ckpt, best)
|
401 |
+
if opt.save_period > 0 and epoch % opt.save_period == 0:
|
402 |
+
torch.save(ckpt, w / f'epoch{epoch}.pt')
|
403 |
+
logger.log_model(w / f'epoch{epoch}.pt')
|
404 |
+
del ckpt
|
405 |
+
# callbacks.run('on_model_save', last, epoch, final_epoch, best_fitness, fi)
|
406 |
+
|
407 |
+
# EarlyStopping
|
408 |
+
if RANK != -1: # if DDP training
|
409 |
+
broadcast_list = [stop if RANK == 0 else None]
|
410 |
+
dist.broadcast_object_list(broadcast_list, 0) # broadcast 'stop' to all ranks
|
411 |
+
if RANK != 0:
|
412 |
+
stop = broadcast_list[0]
|
413 |
+
if stop:
|
414 |
+
break # must break all DDP ranks
|
415 |
+
|
416 |
+
# end epoch ----------------------------------------------------------------------------------------------------
|
417 |
+
# end training -----------------------------------------------------------------------------------------------------
|
418 |
+
if RANK in {-1, 0}:
|
419 |
+
LOGGER.info(f'\n{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.')
|
420 |
+
for f in last, best:
|
421 |
+
if f.exists():
|
422 |
+
strip_optimizer(f) # strip optimizers
|
423 |
+
if f is best:
|
424 |
+
LOGGER.info(f'\nValidating {f}...')
|
425 |
+
results, _, _ = validate.run(
|
426 |
+
data_dict,
|
427 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
428 |
+
imgsz=imgsz,
|
429 |
+
model=attempt_load(f, device).half(),
|
430 |
+
iou_thres=0.65 if is_coco else 0.60, # best pycocotools at iou 0.65
|
431 |
+
single_cls=single_cls,
|
432 |
+
dataloader=val_loader,
|
433 |
+
save_dir=save_dir,
|
434 |
+
save_json=is_coco,
|
435 |
+
verbose=True,
|
436 |
+
plots=plots,
|
437 |
+
callbacks=callbacks,
|
438 |
+
compute_loss=compute_loss,
|
439 |
+
mask_downsample_ratio=mask_ratio,
|
440 |
+
overlap=overlap) # val best model with plots
|
441 |
+
if is_coco:
|
442 |
+
# callbacks.run('on_fit_epoch_end', list(mloss) + list(results) + lr, epoch, best_fitness, fi)
|
443 |
+
metrics_dict = dict(zip(KEYS, list(mloss) + list(results) + lr))
|
444 |
+
logger.log_metrics(metrics_dict, epoch)
|
445 |
+
|
446 |
+
# callbacks.run('on_train_end', last, best, epoch, results)
|
447 |
+
# on train end callback using genericLogger
|
448 |
+
logger.log_metrics(dict(zip(KEYS[6:22], results)), epochs)
|
449 |
+
if not opt.evolve:
|
450 |
+
logger.log_model(best, epoch)
|
451 |
+
if plots:
|
452 |
+
plot_results_with_masks(file=save_dir / 'results.csv') # save results.png
|
453 |
+
files = ['results.png', 'confusion_matrix.png', *(f'{x}_curve.png' for x in ('F1', 'PR', 'P', 'R'))]
|
454 |
+
files = [(save_dir / f) for f in files if (save_dir / f).exists()] # filter
|
455 |
+
LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}")
|
456 |
+
logger.log_images(files, "Results", epoch + 1)
|
457 |
+
logger.log_images(sorted(save_dir.glob('val*.jpg')), "Validation", epoch + 1)
|
458 |
+
torch.cuda.empty_cache()
|
459 |
+
return results
|
460 |
+
|
461 |
+
|
462 |
+
def parse_opt(known=False):
|
463 |
+
parser = argparse.ArgumentParser()
|
464 |
+
parser.add_argument('--weights', type=str, default=ROOT / 'yolo-pan.pt', help='initial weights path')
|
465 |
+
parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
|
466 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128-seg.yaml', help='dataset.yaml path')
|
467 |
+
parser.add_argument('--hyp', type=str, default=ROOT / 'data/hyps/hyp.scratch-low.yaml', help='hyperparameters path')
|
468 |
+
parser.add_argument('--epochs', type=int, default=100, help='total training epochs')
|
469 |
+
parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs, -1 for autobatch')
|
470 |
+
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='train, val image size (pixels)')
|
471 |
+
parser.add_argument('--rect', action='store_true', help='rectangular training')
|
472 |
+
parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')
|
473 |
+
parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
|
474 |
+
parser.add_argument('--noval', action='store_true', help='only validate final epoch')
|
475 |
+
parser.add_argument('--noautoanchor', action='store_true', help='disable AutoAnchor')
|
476 |
+
parser.add_argument('--noplots', action='store_true', help='save no plot files')
|
477 |
+
parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations')
|
478 |
+
parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
|
479 |
+
parser.add_argument('--cache', type=str, nargs='?', const='ram', help='image --cache ram/disk')
|
480 |
+
parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
|
481 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
482 |
+
parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
|
483 |
+
parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
|
484 |
+
parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'AdamW', 'LION'], default='SGD', help='optimizer')
|
485 |
+
parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
|
486 |
+
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
|
487 |
+
parser.add_argument('--project', default=ROOT / 'runs/train-pan', help='save to project/name')
|
488 |
+
parser.add_argument('--name', default='exp', help='save to project/name')
|
489 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
490 |
+
parser.add_argument('--quad', action='store_true', help='quad dataloader')
|
491 |
+
parser.add_argument('--cos-lr', action='store_true', help='cosine LR scheduler')
|
492 |
+
parser.add_argument('--flat-cos-lr', action='store_true', help='cosine LR scheduler')
|
493 |
+
parser.add_argument('--fixed-lr', action='store_true', help='fixed LR scheduler')
|
494 |
+
parser.add_argument('--poly-lr', action='store_true', help='fixed LR scheduler')
|
495 |
+
parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
|
496 |
+
parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)')
|
497 |
+
parser.add_argument('--freeze', nargs='+', type=int, default=[0], help='Freeze layers: backbone=10, first3=0 1 2')
|
498 |
+
parser.add_argument('--save-period', type=int, default=-1, help='Save checkpoint every x epochs (disabled if < 1)')
|
499 |
+
parser.add_argument('--seed', type=int, default=0, help='Global training seed')
|
500 |
+
parser.add_argument('--local_rank', type=int, default=-1, help='Automatic DDP Multi-GPU argument, do not modify')
|
501 |
+
parser.add_argument('--close-mosaic', type=int, default=0, help='Experimental')
|
502 |
+
|
503 |
+
# Instance Segmentation Args
|
504 |
+
parser.add_argument('--mask-ratio', type=int, default=4, help='Downsample the truth masks to saving memory')
|
505 |
+
parser.add_argument('--no-overlap', action='store_true', help='Overlap masks train faster at slightly less mAP')
|
506 |
+
|
507 |
+
return parser.parse_known_args()[0] if known else parser.parse_args()
|
508 |
+
|
509 |
+
|
510 |
+
def main(opt, callbacks=Callbacks()):
|
511 |
+
# Checks
|
512 |
+
if RANK in {-1, 0}:
|
513 |
+
print_args(vars(opt))
|
514 |
+
#check_git_status()
|
515 |
+
#check_requirements()
|
516 |
+
|
517 |
+
# Resume
|
518 |
+
if opt.resume and not opt.evolve: # resume from specified or most recent last.pt
|
519 |
+
last = Path(check_file(opt.resume) if isinstance(opt.resume, str) else get_latest_run())
|
520 |
+
opt_yaml = last.parent.parent / 'opt.yaml' # train options yaml
|
521 |
+
opt_data = opt.data # original dataset
|
522 |
+
if opt_yaml.is_file():
|
523 |
+
with open(opt_yaml, errors='ignore') as f:
|
524 |
+
d = yaml.safe_load(f)
|
525 |
+
else:
|
526 |
+
d = torch.load(last, map_location='cpu')['opt']
|
527 |
+
opt = argparse.Namespace(**d) # replace
|
528 |
+
opt.cfg, opt.weights, opt.resume = '', str(last), True # reinstate
|
529 |
+
if is_url(opt_data):
|
530 |
+
opt.data = check_file(opt_data) # avoid HUB resume auth timeout
|
531 |
+
else:
|
532 |
+
opt.data, opt.cfg, opt.hyp, opt.weights, opt.project = \
|
533 |
+
check_file(opt.data), check_yaml(opt.cfg), check_yaml(opt.hyp), str(opt.weights), str(opt.project) # checks
|
534 |
+
assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified'
|
535 |
+
if opt.evolve:
|
536 |
+
if opt.project == str(ROOT / 'runs/train'): # if default project name, rename to runs/evolve
|
537 |
+
opt.project = str(ROOT / 'runs/evolve')
|
538 |
+
opt.exist_ok, opt.resume = opt.resume, False # pass resume to exist_ok and disable resume
|
539 |
+
if opt.name == 'cfg':
|
540 |
+
opt.name = Path(opt.cfg).stem # use model.yaml as name
|
541 |
+
opt.save_dir = str(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok))
|
542 |
+
|
543 |
+
# DDP mode
|
544 |
+
device = select_device(opt.device, batch_size=opt.batch_size)
|
545 |
+
if LOCAL_RANK != -1:
|
546 |
+
msg = 'is not compatible with YOLO Multi-GPU DDP training'
|
547 |
+
assert not opt.image_weights, f'--image-weights {msg}'
|
548 |
+
assert not opt.evolve, f'--evolve {msg}'
|
549 |
+
assert opt.batch_size != -1, f'AutoBatch with --batch-size -1 {msg}, please pass a valid --batch-size'
|
550 |
+
assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'
|
551 |
+
assert torch.cuda.device_count() > LOCAL_RANK, 'insufficient CUDA devices for DDP command'
|
552 |
+
torch.cuda.set_device(LOCAL_RANK)
|
553 |
+
device = torch.device('cuda', LOCAL_RANK)
|
554 |
+
dist.init_process_group(backend="nccl" if dist.is_nccl_available() else "gloo")
|
555 |
+
|
556 |
+
# Train
|
557 |
+
if not opt.evolve:
|
558 |
+
train(opt.hyp, opt, device, callbacks)
|
559 |
+
|
560 |
+
# Evolve hyperparameters (optional)
|
561 |
+
else:
|
562 |
+
# Hyperparameter evolution metadata (mutation scale 0-1, lower_limit, upper_limit)
|
563 |
+
meta = {
|
564 |
+
'lr0': (1, 1e-5, 1e-1), # initial learning rate (SGD=1E-2, Adam=1E-3)
|
565 |
+
'lrf': (1, 0.01, 1.0), # final OneCycleLR learning rate (lr0 * lrf)
|
566 |
+
'momentum': (0.3, 0.6, 0.98), # SGD momentum/Adam beta1
|
567 |
+
'weight_decay': (1, 0.0, 0.001), # optimizer weight decay
|
568 |
+
'warmup_epochs': (1, 0.0, 5.0), # warmup epochs (fractions ok)
|
569 |
+
'warmup_momentum': (1, 0.0, 0.95), # warmup initial momentum
|
570 |
+
'warmup_bias_lr': (1, 0.0, 0.2), # warmup initial bias lr
|
571 |
+
'box': (1, 0.02, 0.2), # box loss gain
|
572 |
+
'cls': (1, 0.2, 4.0), # cls loss gain
|
573 |
+
'cls_pw': (1, 0.5, 2.0), # cls BCELoss positive_weight
|
574 |
+
'obj': (1, 0.2, 4.0), # obj loss gain (scale with pixels)
|
575 |
+
'obj_pw': (1, 0.5, 2.0), # obj BCELoss positive_weight
|
576 |
+
'iou_t': (0, 0.1, 0.7), # IoU training threshold
|
577 |
+
'anchor_t': (1, 2.0, 8.0), # anchor-multiple threshold
|
578 |
+
'anchors': (2, 2.0, 10.0), # anchors per output grid (0 to ignore)
|
579 |
+
'fl_gamma': (0, 0.0, 2.0), # focal loss gamma (efficientDet default gamma=1.5)
|
580 |
+
'hsv_h': (1, 0.0, 0.1), # image HSV-Hue augmentation (fraction)
|
581 |
+
'hsv_s': (1, 0.0, 0.9), # image HSV-Saturation augmentation (fraction)
|
582 |
+
'hsv_v': (1, 0.0, 0.9), # image HSV-Value augmentation (fraction)
|
583 |
+
'degrees': (1, 0.0, 45.0), # image rotation (+/- deg)
|
584 |
+
'translate': (1, 0.0, 0.9), # image translation (+/- fraction)
|
585 |
+
'scale': (1, 0.0, 0.9), # image scale (+/- gain)
|
586 |
+
'shear': (1, 0.0, 10.0), # image shear (+/- deg)
|
587 |
+
'perspective': (0, 0.0, 0.001), # image perspective (+/- fraction), range 0-0.001
|
588 |
+
'flipud': (1, 0.0, 1.0), # image flip up-down (probability)
|
589 |
+
'fliplr': (0, 0.0, 1.0), # image flip left-right (probability)
|
590 |
+
'mosaic': (1, 0.0, 1.0), # image mixup (probability)
|
591 |
+
'mixup': (1, 0.0, 1.0), # image mixup (probability)
|
592 |
+
'copy_paste': (1, 0.0, 1.0)} # segment copy-paste (probability)
|
593 |
+
|
594 |
+
with open(opt.hyp, errors='ignore') as f:
|
595 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
596 |
+
if 'anchors' not in hyp: # anchors commented in hyp.yaml
|
597 |
+
hyp['anchors'] = 3
|
598 |
+
if opt.noautoanchor:
|
599 |
+
del hyp['anchors'], meta['anchors']
|
600 |
+
opt.noval, opt.nosave, save_dir = True, True, Path(opt.save_dir) # only val/save final epoch
|
601 |
+
# ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
|
602 |
+
evolve_yaml, evolve_csv = save_dir / 'hyp_evolve.yaml', save_dir / 'evolve.csv'
|
603 |
+
if opt.bucket:
|
604 |
+
os.system(f'gsutil cp gs://{opt.bucket}/evolve.csv {evolve_csv}') # download evolve.csv if exists
|
605 |
+
|
606 |
+
for _ in range(opt.evolve): # generations to evolve
|
607 |
+
if evolve_csv.exists(): # if evolve.csv exists: select best hyps and mutate
|
608 |
+
# Select parent(s)
|
609 |
+
parent = 'single' # parent selection method: 'single' or 'weighted'
|
610 |
+
x = np.loadtxt(evolve_csv, ndmin=2, delimiter=',', skiprows=1)
|
611 |
+
n = min(5, len(x)) # number of previous results to consider
|
612 |
+
x = x[np.argsort(-fitness(x))][:n] # top n mutations
|
613 |
+
w = fitness(x) - fitness(x).min() + 1E-6 # weights (sum > 0)
|
614 |
+
if parent == 'single' or len(x) == 1:
|
615 |
+
# x = x[random.randint(0, n - 1)] # random selection
|
616 |
+
x = x[random.choices(range(n), weights=w)[0]] # weighted selection
|
617 |
+
elif parent == 'weighted':
|
618 |
+
x = (x * w.reshape(n, 1)).sum(0) / w.sum() # weighted combination
|
619 |
+
|
620 |
+
# Mutate
|
621 |
+
mp, s = 0.8, 0.2 # mutation probability, sigma
|
622 |
+
npr = np.random
|
623 |
+
npr.seed(int(time.time()))
|
624 |
+
g = np.array([meta[k][0] for k in hyp.keys()]) # gains 0-1
|
625 |
+
ng = len(meta)
|
626 |
+
v = np.ones(ng)
|
627 |
+
while all(v == 1): # mutate until a change occurs (prevent duplicates)
|
628 |
+
v = (g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1).clip(0.3, 3.0)
|
629 |
+
for i, k in enumerate(hyp.keys()): # plt.hist(v.ravel(), 300)
|
630 |
+
hyp[k] = float(x[i + 7] * v[i]) # mutate
|
631 |
+
|
632 |
+
# Constrain to limits
|
633 |
+
for k, v in meta.items():
|
634 |
+
hyp[k] = max(hyp[k], v[1]) # lower limit
|
635 |
+
hyp[k] = min(hyp[k], v[2]) # upper limit
|
636 |
+
hyp[k] = round(hyp[k], 5) # significant digits
|
637 |
+
|
638 |
+
# Train mutation
|
639 |
+
results = train(hyp.copy(), opt, device, callbacks)
|
640 |
+
callbacks = Callbacks()
|
641 |
+
# Write mutation results
|
642 |
+
print_mutation(KEYS, results, hyp.copy(), save_dir, opt.bucket)
|
643 |
+
|
644 |
+
# Plot results
|
645 |
+
plot_evolve(evolve_csv)
|
646 |
+
LOGGER.info(f'Hyperparameter evolution finished {opt.evolve} generations\n'
|
647 |
+
f"Results saved to {colorstr('bold', save_dir)}\n"
|
648 |
+
f'Usage example: $ python train.py --hyp {evolve_yaml}')
|
649 |
+
|
650 |
+
|
651 |
+
def run(**kwargs):
|
652 |
+
# Usage: import train; train.run(data='coco128.yaml', imgsz=320, weights='yolo.pt')
|
653 |
+
opt = parse_opt(True)
|
654 |
+
for k, v in kwargs.items():
|
655 |
+
setattr(opt, k, v)
|
656 |
+
main(opt)
|
657 |
+
return opt
|
658 |
+
|
659 |
+
|
660 |
+
if __name__ == "__main__":
|
661 |
+
opt = parse_opt()
|
662 |
+
main(opt)
|
panoptic/val.py
ADDED
@@ -0,0 +1,597 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
from multiprocessing.pool import ThreadPool
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import torch
|
10 |
+
from tqdm import tqdm
|
11 |
+
|
12 |
+
FILE = Path(__file__).resolve()
|
13 |
+
ROOT = FILE.parents[1] # YOLO root directory
|
14 |
+
if str(ROOT) not in sys.path:
|
15 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
16 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
17 |
+
|
18 |
+
import torch.nn.functional as F
|
19 |
+
import torchvision.transforms as transforms
|
20 |
+
from pycocotools import mask as maskUtils
|
21 |
+
from models.common import DetectMultiBackend
|
22 |
+
from models.yolo import SegmentationModel
|
23 |
+
from utils.callbacks import Callbacks
|
24 |
+
from utils.coco_utils import getCocoIds, getMappingId, getMappingIndex
|
25 |
+
from utils.general import (LOGGER, NUM_THREADS, TQDM_BAR_FORMAT, Profile, check_dataset, check_img_size,
|
26 |
+
check_requirements, check_yaml, coco80_to_coco91_class, colorstr, increment_path,
|
27 |
+
non_max_suppression, print_args, scale_boxes, xywh2xyxy, xyxy2xywh)
|
28 |
+
from utils.metrics import ConfusionMatrix, box_iou
|
29 |
+
from utils.plots import output_to_target, plot_val_study
|
30 |
+
from utils.panoptic.dataloaders import create_dataloader
|
31 |
+
from utils.panoptic.general import mask_iou, process_mask, process_mask_upsample, scale_image
|
32 |
+
from utils.panoptic.metrics import Metrics, ap_per_class_box_and_mask, Semantic_Metrics
|
33 |
+
from utils.panoptic.plots import plot_images_and_masks
|
34 |
+
from utils.torch_utils import de_parallel, select_device, smart_inference_mode
|
35 |
+
|
36 |
+
|
37 |
+
def save_one_txt(predn, save_conf, shape, file):
|
38 |
+
# Save one txt result
|
39 |
+
gn = torch.tensor(shape)[[1, 0, 1, 0]] # normalization gain whwh
|
40 |
+
for *xyxy, conf, cls in predn.tolist():
|
41 |
+
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
|
42 |
+
line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format
|
43 |
+
with open(file, 'a') as f:
|
44 |
+
f.write(('%g ' * len(line)).rstrip() % line + '\n')
|
45 |
+
|
46 |
+
|
47 |
+
def save_one_json(predn, jdict, path, class_map, pred_masks):
|
48 |
+
# Save one JSON result {"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}
|
49 |
+
from pycocotools.mask import encode
|
50 |
+
|
51 |
+
def single_encode(x):
|
52 |
+
rle = encode(np.asarray(x[:, :, None], order="F", dtype="uint8"))[0]
|
53 |
+
rle["counts"] = rle["counts"].decode("utf-8")
|
54 |
+
return rle
|
55 |
+
|
56 |
+
image_id = int(path.stem) if path.stem.isnumeric() else path.stem
|
57 |
+
box = xyxy2xywh(predn[:, :4]) # xywh
|
58 |
+
box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
|
59 |
+
pred_masks = np.transpose(pred_masks, (2, 0, 1))
|
60 |
+
with ThreadPool(NUM_THREADS) as pool:
|
61 |
+
rles = pool.map(single_encode, pred_masks)
|
62 |
+
for i, (p, b) in enumerate(zip(predn.tolist(), box.tolist())):
|
63 |
+
jdict.append({
|
64 |
+
'image_id': image_id,
|
65 |
+
'category_id': class_map[int(p[5])],
|
66 |
+
'bbox': [round(x, 3) for x in b],
|
67 |
+
'score': round(p[4], 5),
|
68 |
+
'segmentation': rles[i]})
|
69 |
+
|
70 |
+
|
71 |
+
def process_batch(detections, labels, iouv, pred_masks=None, gt_masks=None, overlap=False, masks=False):
|
72 |
+
"""
|
73 |
+
Return correct prediction matrix
|
74 |
+
Arguments:
|
75 |
+
detections (array[N, 6]), x1, y1, x2, y2, conf, class
|
76 |
+
labels (array[M, 5]), class, x1, y1, x2, y2
|
77 |
+
Returns:
|
78 |
+
correct (array[N, 10]), for 10 IoU levels
|
79 |
+
"""
|
80 |
+
if masks:
|
81 |
+
if overlap:
|
82 |
+
nl = len(labels)
|
83 |
+
index = torch.arange(nl, device=gt_masks.device).view(nl, 1, 1) + 1
|
84 |
+
gt_masks = gt_masks.repeat(nl, 1, 1) # shape(1,640,640) -> (n,640,640)
|
85 |
+
gt_masks = torch.where(gt_masks == index, 1.0, 0.0)
|
86 |
+
if gt_masks.shape[1:] != pred_masks.shape[1:]:
|
87 |
+
gt_masks = F.interpolate(gt_masks[None], pred_masks.shape[1:], mode="bilinear", align_corners=False)[0]
|
88 |
+
gt_masks = gt_masks.gt_(0.5)
|
89 |
+
iou = mask_iou(gt_masks.view(gt_masks.shape[0], -1), pred_masks.view(pred_masks.shape[0], -1))
|
90 |
+
else: # boxes
|
91 |
+
iou = box_iou(labels[:, 1:], detections[:, :4])
|
92 |
+
|
93 |
+
correct = np.zeros((detections.shape[0], iouv.shape[0])).astype(bool)
|
94 |
+
correct_class = labels[:, 0:1] == detections[:, 5]
|
95 |
+
for i in range(len(iouv)):
|
96 |
+
x = torch.where((iou >= iouv[i]) & correct_class) # IoU > threshold and classes match
|
97 |
+
if x[0].shape[0]:
|
98 |
+
matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy() # [label, detect, iou]
|
99 |
+
if x[0].shape[0] > 1:
|
100 |
+
matches = matches[matches[:, 2].argsort()[::-1]]
|
101 |
+
matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
|
102 |
+
# matches = matches[matches[:, 2].argsort()[::-1]]
|
103 |
+
matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
|
104 |
+
correct[matches[:, 1].astype(int), i] = True
|
105 |
+
return torch.tensor(correct, dtype=torch.bool, device=iouv.device)
|
106 |
+
|
107 |
+
|
108 |
+
@smart_inference_mode()
|
109 |
+
def run(
|
110 |
+
data,
|
111 |
+
weights=None, # model.pt path(s)
|
112 |
+
batch_size=32, # batch size
|
113 |
+
imgsz=640, # inference size (pixels)
|
114 |
+
conf_thres=0.001, # confidence threshold
|
115 |
+
iou_thres=0.6, # NMS IoU threshold
|
116 |
+
max_det=300, # maximum detections per image
|
117 |
+
task='val', # train, val, test, speed or study
|
118 |
+
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
|
119 |
+
workers=8, # max dataloader workers (per RANK in DDP mode)
|
120 |
+
single_cls=False, # treat as single-class dataset
|
121 |
+
augment=False, # augmented inference
|
122 |
+
verbose=False, # verbose output
|
123 |
+
save_txt=False, # save results to *.txt
|
124 |
+
save_hybrid=False, # save label+prediction hybrid results to *.txt
|
125 |
+
save_conf=False, # save confidences in --save-txt labels
|
126 |
+
save_json=False, # save a COCO-JSON results file
|
127 |
+
project=ROOT / 'runs/val-pan', # save to project/name
|
128 |
+
name='exp', # save to project/name
|
129 |
+
exist_ok=False, # existing project/name ok, do not increment
|
130 |
+
half=True, # use FP16 half-precision inference
|
131 |
+
dnn=False, # use OpenCV DNN for ONNX inference
|
132 |
+
model=None,
|
133 |
+
dataloader=None,
|
134 |
+
save_dir=Path(''),
|
135 |
+
plots=True,
|
136 |
+
overlap=False,
|
137 |
+
mask_downsample_ratio=1,
|
138 |
+
compute_loss=None,
|
139 |
+
callbacks=Callbacks(),
|
140 |
+
):
|
141 |
+
if save_json:
|
142 |
+
check_requirements(['pycocotools'])
|
143 |
+
process = process_mask_upsample # more accurate
|
144 |
+
else:
|
145 |
+
process = process_mask # faster
|
146 |
+
|
147 |
+
# Initialize/load model and set device
|
148 |
+
training = model is not None
|
149 |
+
if training: # called by train.py
|
150 |
+
device, pt, jit, engine = next(model.parameters()).device, True, False, False # get model device, PyTorch model
|
151 |
+
half &= device.type != 'cpu' # half precision only supported on CUDA
|
152 |
+
model.half() if half else model.float()
|
153 |
+
nm = de_parallel(model).model[-1].nm # number of masks
|
154 |
+
else: # called directly
|
155 |
+
device = select_device(device, batch_size=batch_size)
|
156 |
+
|
157 |
+
# Directories
|
158 |
+
save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
|
159 |
+
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
|
160 |
+
|
161 |
+
# Load model
|
162 |
+
model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
|
163 |
+
stride, pt, jit, engine = model.stride, model.pt, model.jit, model.engine
|
164 |
+
imgsz = check_img_size(imgsz, s=stride) # check image size
|
165 |
+
half = model.fp16 # FP16 supported on limited backends with CUDA
|
166 |
+
nm = de_parallel(model).model.model[-1].nm if isinstance(model, SegmentationModel) else 32 # number of masks
|
167 |
+
if engine:
|
168 |
+
batch_size = model.batch_size
|
169 |
+
else:
|
170 |
+
device = model.device
|
171 |
+
if not (pt or jit):
|
172 |
+
batch_size = 1 # export.py models default to batch-size 1
|
173 |
+
LOGGER.info(f'Forcing --batch-size 1 square inference (1,3,{imgsz},{imgsz}) for non-PyTorch models')
|
174 |
+
|
175 |
+
# Data
|
176 |
+
data = check_dataset(data) # check
|
177 |
+
|
178 |
+
# Configure
|
179 |
+
model.eval()
|
180 |
+
cuda = device.type != 'cpu'
|
181 |
+
#is_coco = isinstance(data.get('val'), str) and data['val'].endswith(f'coco{os.sep}val2017.txt') # COCO dataset
|
182 |
+
is_coco = isinstance(data.get('val'), str) and data['val'].endswith(f'val2017.txt') # COCO dataset
|
183 |
+
nc = 1 if single_cls else int(data['nc']) # number of classes
|
184 |
+
stuff_names = data.get('stuff_names', []) # names of stuff classes
|
185 |
+
stuff_nc = len(stuff_names) # number of stuff classes
|
186 |
+
iouv = torch.linspace(0.5, 0.95, 10, device=device) # iou vector for [email protected]:0.95
|
187 |
+
niou = iouv.numel()
|
188 |
+
|
189 |
+
# Semantic Segmentation
|
190 |
+
img_id_list = []
|
191 |
+
|
192 |
+
# Dataloader
|
193 |
+
if not training:
|
194 |
+
if pt and not single_cls: # check --weights are trained on --data
|
195 |
+
ncm = model.model.nc
|
196 |
+
assert ncm == nc, f'{weights} ({ncm} classes) trained on different --data than what you passed ({nc} ' \
|
197 |
+
f'classes). Pass correct combination of --weights and --data that are trained together.'
|
198 |
+
model.warmup(imgsz=(1 if pt else batch_size, 3, imgsz, imgsz)) # warmup
|
199 |
+
pad, rect = (0.0, False) if task == 'speed' else (0.5, pt) # square inference for benchmarks
|
200 |
+
task = task if task in ('train', 'val', 'test') else 'val' # path to train/val/test images
|
201 |
+
dataloader = create_dataloader(data[task],
|
202 |
+
imgsz,
|
203 |
+
batch_size,
|
204 |
+
stride,
|
205 |
+
single_cls,
|
206 |
+
pad=pad,
|
207 |
+
rect=rect,
|
208 |
+
workers=workers,
|
209 |
+
prefix=colorstr(f'{task}: '),
|
210 |
+
overlap_mask=overlap,
|
211 |
+
mask_downsample_ratio=mask_downsample_ratio)[0]
|
212 |
+
|
213 |
+
seen = 0
|
214 |
+
confusion_matrix = ConfusionMatrix(nc=nc)
|
215 |
+
names = model.names if hasattr(model, 'names') else model.module.names # get class names
|
216 |
+
if isinstance(names, (list, tuple)): # old format
|
217 |
+
names = dict(enumerate(names))
|
218 |
+
class_map = coco80_to_coco91_class() if is_coco else list(range(1000))
|
219 |
+
s = ('%22s' + '%11s' * 12) % ('Class', 'Images', 'Instances', 'Box(P', "R", "mAP50", "mAP50-95)", "Mask(P", "R",
|
220 |
+
"mAP50", "mAP50-95)", 'S(MIoU', 'FWIoU)')
|
221 |
+
dt = Profile(), Profile(), Profile()
|
222 |
+
metrics = Metrics()
|
223 |
+
semantic_metrics = Semantic_Metrics(nc = (nc + stuff_nc), device = device)
|
224 |
+
loss = torch.zeros(6, device=device)
|
225 |
+
jdict, stats = [], []
|
226 |
+
semantic_jdict = []
|
227 |
+
# callbacks.run('on_val_start')
|
228 |
+
pbar = tqdm(dataloader, desc=s, bar_format=TQDM_BAR_FORMAT) # progress bar
|
229 |
+
for batch_i, (im, targets, paths, shapes, masks, semasks) in enumerate(pbar):
|
230 |
+
# callbacks.run('on_val_batch_start')
|
231 |
+
with dt[0]:
|
232 |
+
if cuda:
|
233 |
+
im = im.to(device, non_blocking=True)
|
234 |
+
targets = targets.to(device)
|
235 |
+
masks = masks.to(device)
|
236 |
+
semasks = semasks.to(device)
|
237 |
+
masks = masks.float()
|
238 |
+
semasks = semasks.float()
|
239 |
+
im = im.half() if half else im.float() # uint8 to fp16/32
|
240 |
+
im /= 255 # 0 - 255 to 0.0 - 1.0
|
241 |
+
nb, _, height, width = im.shape # batch size, channels, height, width
|
242 |
+
|
243 |
+
# Inference
|
244 |
+
with dt[1]:
|
245 |
+
preds, train_out = model(im)# if compute_loss else (*model(im, augment=augment)[:2], None)
|
246 |
+
#train_out, preds, protos = p if len(p) == 3 else p[1]
|
247 |
+
#preds = p
|
248 |
+
#train_out = p[1][0] if len(p[1]) == 3 else p[0]
|
249 |
+
# protos = train_out[-1]
|
250 |
+
#print(preds.shape)
|
251 |
+
#print(train_out[0].shape)
|
252 |
+
#print(train_out[1].shape)
|
253 |
+
#print(train_out[2].shape)
|
254 |
+
_, pred_masks, protos, psemasks = train_out
|
255 |
+
|
256 |
+
# Loss
|
257 |
+
if compute_loss:
|
258 |
+
loss += compute_loss(train_out, targets, masks, semasks = semasks)[1] # box, obj, cls
|
259 |
+
|
260 |
+
# NMS
|
261 |
+
targets[:, 2:] *= torch.tensor((width, height, width, height), device=device) # to pixels
|
262 |
+
lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_hybrid else [] # for autolabelling
|
263 |
+
with dt[2]:
|
264 |
+
preds = non_max_suppression(preds,
|
265 |
+
conf_thres,
|
266 |
+
iou_thres,
|
267 |
+
labels=lb,
|
268 |
+
multi_label=True,
|
269 |
+
agnostic=single_cls,
|
270 |
+
max_det=max_det,
|
271 |
+
nm=nm)
|
272 |
+
|
273 |
+
# Metrics
|
274 |
+
plot_masks = [] # masks for plotting
|
275 |
+
plot_semasks = [] # masks for plotting
|
276 |
+
|
277 |
+
if training:
|
278 |
+
semantic_metrics.update(psemasks, semasks)
|
279 |
+
else:
|
280 |
+
_, _, smh, smw = semasks.shape
|
281 |
+
semantic_metrics.update(torch.nn.functional.interpolate(psemasks, size = (smh, smw), mode = 'bilinear', align_corners = False), semasks)
|
282 |
+
|
283 |
+
if plots and batch_i < 3:
|
284 |
+
plot_semasks.append(psemasks.clone().detach().cpu())
|
285 |
+
|
286 |
+
for si, (pred, proto, psemask) in enumerate(zip(preds, protos, psemasks)):
|
287 |
+
labels = targets[targets[:, 0] == si, 1:]
|
288 |
+
nl, npr = labels.shape[0], pred.shape[0] # number of labels, predictions
|
289 |
+
path, shape = Path(paths[si]), shapes[si][0]
|
290 |
+
image_id = path.stem
|
291 |
+
img_id_list.append(image_id)
|
292 |
+
correct_masks = torch.zeros(npr, niou, dtype=torch.bool, device=device) # init
|
293 |
+
correct_bboxes = torch.zeros(npr, niou, dtype=torch.bool, device=device) # init
|
294 |
+
seen += 1
|
295 |
+
|
296 |
+
if npr == 0:
|
297 |
+
if nl:
|
298 |
+
stats.append((correct_masks, correct_bboxes, *torch.zeros((2, 0), device=device), labels[:, 0]))
|
299 |
+
if plots:
|
300 |
+
confusion_matrix.process_batch(detections=None, labels=labels[:, 0])
|
301 |
+
else:
|
302 |
+
# Masks
|
303 |
+
midx = [si] if overlap else targets[:, 0] == si
|
304 |
+
gt_masks = masks[midx]
|
305 |
+
pred_masks = process(proto, pred[:, 6:], pred[:, :4], shape=im[si].shape[1:])
|
306 |
+
|
307 |
+
# Predictions
|
308 |
+
if single_cls:
|
309 |
+
pred[:, 5] = 0
|
310 |
+
predn = pred.clone()
|
311 |
+
scale_boxes(im[si].shape[1:], predn[:, :4], shape, shapes[si][1]) # native-space pred
|
312 |
+
|
313 |
+
# Evaluate
|
314 |
+
if nl:
|
315 |
+
tbox = xywh2xyxy(labels[:, 1:5]) # target boxes
|
316 |
+
scale_boxes(im[si].shape[1:], tbox, shape, shapes[si][1]) # native-space labels
|
317 |
+
labelsn = torch.cat((labels[:, 0:1], tbox), 1) # native-space labels
|
318 |
+
correct_bboxes = process_batch(predn, labelsn, iouv)
|
319 |
+
correct_masks = process_batch(predn, labelsn, iouv, pred_masks, gt_masks, overlap=overlap, masks=True)
|
320 |
+
if plots:
|
321 |
+
confusion_matrix.process_batch(predn, labelsn)
|
322 |
+
stats.append((correct_masks, correct_bboxes, pred[:, 4], pred[:, 5], labels[:, 0])) # (conf, pcls, tcls)
|
323 |
+
|
324 |
+
pred_masks = torch.as_tensor(pred_masks, dtype=torch.uint8)
|
325 |
+
if plots and batch_i < 3:
|
326 |
+
plot_masks.append(pred_masks[:15].cpu()) # filter top 15 to plot
|
327 |
+
|
328 |
+
# Save/log
|
329 |
+
if save_txt:
|
330 |
+
save_one_txt(predn, save_conf, shape, file=save_dir / 'labels' / f'{path.stem}.txt')
|
331 |
+
if save_json:
|
332 |
+
pred_masks = scale_image(im[si].shape[1:],
|
333 |
+
pred_masks.permute(1, 2, 0).contiguous().cpu().numpy(), shape, shapes[si][1])
|
334 |
+
save_one_json(predn, jdict, path, class_map, pred_masks) # append to COCO-JSON dictionary
|
335 |
+
# callbacks.run('on_val_image_end', pred, predn, path, names, im[si])
|
336 |
+
|
337 |
+
# Semantic Segmentation
|
338 |
+
h0, w0 = shape
|
339 |
+
|
340 |
+
# resize
|
341 |
+
_, mask_h, mask_w = psemask.shape
|
342 |
+
h_ratio = mask_h / h0
|
343 |
+
w_ratio = mask_w / w0
|
344 |
+
|
345 |
+
if h_ratio == w_ratio:
|
346 |
+
psemask = torch.nn.functional.interpolate(psemask[None, :], size = (h0, w0), mode = 'bilinear', align_corners = False)
|
347 |
+
else:
|
348 |
+
transform = transforms.CenterCrop((h0, w0))
|
349 |
+
|
350 |
+
if (1 != h_ratio) and (1 != w_ratio):
|
351 |
+
h_new = h0 if (h_ratio < w_ratio) else int(mask_h / w_ratio)
|
352 |
+
w_new = w0 if (h_ratio > w_ratio) else int(mask_w / h_ratio)
|
353 |
+
psemask = torch.nn.functional.interpolate(psemask[None, :], size = (h_new, w_new), mode = 'bilinear', align_corners = False)
|
354 |
+
|
355 |
+
psemask = transform(psemask)
|
356 |
+
|
357 |
+
psemask = torch.squeeze(psemask)
|
358 |
+
|
359 |
+
nc, h, w = psemask.shape
|
360 |
+
|
361 |
+
semantic_mask = torch.flatten(psemask, start_dim = 1).permute(1, 0) # class x h x w -> (h x w) x class
|
362 |
+
|
363 |
+
max_idx = semantic_mask.argmax(1)
|
364 |
+
output_masks = torch.zeros(semantic_mask.shape).scatter(1, max_idx.cpu().unsqueeze(1), 1.0) # one hot: (h x w) x class
|
365 |
+
output_masks = torch.reshape(output_masks.permute(1, 0), (nc, h, w)) # (h x w) x class -> class x h x w
|
366 |
+
psemask = output_masks.to(device = device)
|
367 |
+
|
368 |
+
# TODO: check is_coco
|
369 |
+
instances_ids = getCocoIds(name = 'instances')
|
370 |
+
stuff_mask = torch.zeros((h, w), device = device)
|
371 |
+
check_semantic_mask = False
|
372 |
+
for idx, pred_semantic_mask in enumerate(psemask):
|
373 |
+
category_id = int(getMappingId(idx))
|
374 |
+
if 183 == category_id:
|
375 |
+
# set all non-stuff pixels to other
|
376 |
+
pred_semantic_mask = (torch.logical_xor(stuff_mask, torch.ones((h, w), device = device))).int()
|
377 |
+
|
378 |
+
# ignore the classes which all zeros / unlabeled class
|
379 |
+
if (0 >= torch.max(pred_semantic_mask)) or (0 >= category_id):
|
380 |
+
continue
|
381 |
+
|
382 |
+
if category_id not in instances_ids:
|
383 |
+
# record all stuff mask
|
384 |
+
stuff_mask = torch.logical_or(stuff_mask, pred_semantic_mask)
|
385 |
+
|
386 |
+
if (category_id not in instances_ids):
|
387 |
+
rle = maskUtils.encode(np.asfortranarray(pred_semantic_mask.cpu(), dtype = np.uint8))
|
388 |
+
rle['counts'] = rle['counts'].decode('utf-8')
|
389 |
+
|
390 |
+
temp_d = {
|
391 |
+
'image_id': int(image_id) if image_id.isnumeric() else image_id,
|
392 |
+
'category_id': category_id,
|
393 |
+
'segmentation': rle,
|
394 |
+
'score': 1
|
395 |
+
}
|
396 |
+
|
397 |
+
semantic_jdict.append(temp_d)
|
398 |
+
check_semantic_mask = True
|
399 |
+
|
400 |
+
if not check_semantic_mask:
|
401 |
+
# append a other mask for evaluation if the image without any mask
|
402 |
+
other_mask = (torch.ones((h, w), device = device)).int()
|
403 |
+
|
404 |
+
rle = maskUtils.encode(np.asfortranarray(other_mask.cpu(), dtype = np.uint8))
|
405 |
+
rle['counts'] = rle['counts'].decode('utf-8')
|
406 |
+
|
407 |
+
temp_d = {
|
408 |
+
'image_id': int(image_id) if image_id.isnumeric() else image_id,
|
409 |
+
'category_id': 183,
|
410 |
+
'segmentation': rle,
|
411 |
+
'score': 1
|
412 |
+
}
|
413 |
+
|
414 |
+
semantic_jdict.append(temp_d)
|
415 |
+
|
416 |
+
# Plot images
|
417 |
+
if plots and batch_i < 3:
|
418 |
+
if len(plot_masks):
|
419 |
+
plot_masks = torch.cat(plot_masks, dim=0)
|
420 |
+
if len(plot_semasks):
|
421 |
+
plot_semasks = torch.cat(plot_semasks, dim = 0)
|
422 |
+
plot_images_and_masks(im, targets, masks, semasks, paths, save_dir / f'val_batch{batch_i}_labels.jpg', names)
|
423 |
+
plot_images_and_masks(im, output_to_target(preds, max_det=15), plot_masks, plot_semasks, paths,
|
424 |
+
save_dir / f'val_batch{batch_i}_pred.jpg', names) # pred
|
425 |
+
|
426 |
+
# callbacks.run('on_val_batch_end')
|
427 |
+
|
428 |
+
# Compute metrics
|
429 |
+
stats = [torch.cat(x, 0).cpu().numpy() for x in zip(*stats)] # to numpy
|
430 |
+
if len(stats) and stats[0].any():
|
431 |
+
results = ap_per_class_box_and_mask(*stats, plot=plots, save_dir=save_dir, names=names)
|
432 |
+
metrics.update(results)
|
433 |
+
nt = np.bincount(stats[4].astype(int), minlength=nc) # number of targets per class
|
434 |
+
|
435 |
+
# Print results
|
436 |
+
pf = '%22s' + '%11i' * 2 + '%11.3g' * 10 # print format
|
437 |
+
LOGGER.info(pf % ("all", seen, nt.sum(), *metrics.mean_results(), *semantic_metrics.results()))
|
438 |
+
if nt.sum() == 0:
|
439 |
+
LOGGER.warning(f'WARNING ⚠️ no labels found in {task} set, can not compute metrics without labels')
|
440 |
+
|
441 |
+
# Print results per class
|
442 |
+
if (verbose or (nc < 50 and not training)) and nc > 1 and len(stats):
|
443 |
+
for i, c in enumerate(metrics.ap_class_index):
|
444 |
+
LOGGER.info(pf % (names[c], seen, nt[c], *metrics.class_result(i), *semantic_metrics.results()))
|
445 |
+
|
446 |
+
# Print speeds
|
447 |
+
t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image
|
448 |
+
if not training:
|
449 |
+
shape = (batch_size, 3, imgsz, imgsz)
|
450 |
+
LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {shape}' % t)
|
451 |
+
|
452 |
+
# Plots
|
453 |
+
if plots:
|
454 |
+
confusion_matrix.plot(save_dir=save_dir, names=list(names.values()))
|
455 |
+
# callbacks.run('on_val_end')
|
456 |
+
|
457 |
+
mp_bbox, mr_bbox, map50_bbox, map_bbox, mp_mask, mr_mask, map50_mask, map_mask = metrics.mean_results()
|
458 |
+
miou_sem, fwiou_sem = semantic_metrics.results()
|
459 |
+
semantic_metrics.reset()
|
460 |
+
|
461 |
+
# Save JSON
|
462 |
+
if save_json and len(jdict):
|
463 |
+
w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights
|
464 |
+
anno_path = Path(data.get('path', '../coco'))
|
465 |
+
anno_json = str(anno_path / 'annotations/instances_val2017.json') # annotations json
|
466 |
+
pred_json = str(save_dir / f"{w}_predictions.json") # predictions json
|
467 |
+
LOGGER.info(f'\nEvaluating pycocotools mAP... saving {pred_json}...')
|
468 |
+
with open(pred_json, 'w') as f:
|
469 |
+
json.dump(jdict, f)
|
470 |
+
|
471 |
+
semantic_anno_json = str(anno_path / 'annotations/stuff_val2017.json') # annotations json
|
472 |
+
semantic_pred_json = str(save_dir / f"{w}_predictions_stuff.json") # predictions json
|
473 |
+
LOGGER.info(f'\nsaving {semantic_pred_json}...')
|
474 |
+
with open(semantic_pred_json, 'w') as f:
|
475 |
+
json.dump(semantic_jdict, f)
|
476 |
+
|
477 |
+
try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
|
478 |
+
from pycocotools.coco import COCO
|
479 |
+
from pycocotools.cocoeval import COCOeval
|
480 |
+
|
481 |
+
anno = COCO(anno_json) # init annotations api
|
482 |
+
pred = anno.loadRes(pred_json) # init predictions api
|
483 |
+
results = []
|
484 |
+
for eval in COCOeval(anno, pred, 'bbox'), COCOeval(anno, pred, 'segm'):
|
485 |
+
if is_coco:
|
486 |
+
eval.params.imgIds = [int(Path(x).stem) for x in dataloader.dataset.im_files] # img ID to evaluate
|
487 |
+
eval.evaluate()
|
488 |
+
eval.accumulate()
|
489 |
+
eval.summarize()
|
490 |
+
results.extend(eval.stats[:2]) # update results ([email protected]:0.95, [email protected])
|
491 |
+
map_bbox, map50_bbox, map_mask, map50_mask = results
|
492 |
+
|
493 |
+
# Semantic Segmentation
|
494 |
+
from utils.stuff_seg.cocostuffeval import COCOStuffeval
|
495 |
+
|
496 |
+
LOGGER.info(f'\nEvaluating pycocotools stuff... ')
|
497 |
+
imgIds = [int(x) for x in img_id_list]
|
498 |
+
|
499 |
+
stuffGt = COCO(semantic_anno_json) # initialize COCO ground truth api
|
500 |
+
stuffDt = stuffGt.loadRes(semantic_pred_json) # initialize COCO pred api
|
501 |
+
|
502 |
+
cocoStuffEval = COCOStuffeval(stuffGt, stuffDt)
|
503 |
+
cocoStuffEval.params.imgIds = imgIds # image IDs to evaluate
|
504 |
+
cocoStuffEval.evaluate()
|
505 |
+
stats, statsClass = cocoStuffEval.summarize()
|
506 |
+
stuffIds = getCocoIds(name = 'stuff')
|
507 |
+
title = ' {:<5} | {:^6} | {:^6} '.format('class', 'iou', 'macc') if (0 >= len(stuff_names)) else \
|
508 |
+
' {:<5} | {:<20} | {:^6} | {:^6} '.format('class', 'class name', 'iou', 'macc')
|
509 |
+
print(title)
|
510 |
+
for idx, (iou, macc) in enumerate(zip(statsClass['ious'], statsClass['maccs'])):
|
511 |
+
id = (idx + 1)
|
512 |
+
if id not in stuffIds:
|
513 |
+
continue
|
514 |
+
content = ' {:<5} | {:0.4f} | {:0.4f} '.format(str(id), iou, macc) if (0 >= len(stuff_names)) else \
|
515 |
+
' {:<5} | {:<20} | {:0.4f} | {:0.4f} '.format(str(id), str(stuff_names[getMappingIndex(id, name = 'stuff')]), iou, macc)
|
516 |
+
print(content)
|
517 |
+
|
518 |
+
except Exception as e:
|
519 |
+
LOGGER.info(f'pycocotools unable to run: {e}')
|
520 |
+
|
521 |
+
# Return results
|
522 |
+
model.float() # for training
|
523 |
+
if not training:
|
524 |
+
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
|
525 |
+
LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
|
526 |
+
final_metric = mp_bbox, mr_bbox, map50_bbox, map_bbox, mp_mask, mr_mask, map50_mask, map_mask, miou_sem, fwiou_sem
|
527 |
+
return (*final_metric, *(loss.cpu() / len(dataloader)).tolist()), metrics.get_maps(nc), t
|
528 |
+
|
529 |
+
|
530 |
+
def parse_opt():
|
531 |
+
parser = argparse.ArgumentParser()
|
532 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128-pan.yaml', help='dataset.yaml path')
|
533 |
+
parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolo-pan.pt', help='model path(s)')
|
534 |
+
parser.add_argument('--batch-size', type=int, default=32, help='batch size')
|
535 |
+
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='inference size (pixels)')
|
536 |
+
parser.add_argument('--conf-thres', type=float, default=0.001, help='confidence threshold')
|
537 |
+
parser.add_argument('--iou-thres', type=float, default=0.6, help='NMS IoU threshold')
|
538 |
+
parser.add_argument('--max-det', type=int, default=300, help='maximum detections per image')
|
539 |
+
parser.add_argument('--task', default='val', help='train, val, test, speed or study')
|
540 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
541 |
+
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
|
542 |
+
parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset')
|
543 |
+
parser.add_argument('--augment', action='store_true', help='augmented inference')
|
544 |
+
parser.add_argument('--verbose', action='store_true', help='report mAP by class')
|
545 |
+
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
|
546 |
+
parser.add_argument('--save-hybrid', action='store_true', help='save label+prediction hybrid results to *.txt')
|
547 |
+
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
|
548 |
+
parser.add_argument('--save-json', action='store_true', help='save a COCO-JSON results file')
|
549 |
+
parser.add_argument('--project', default=ROOT / 'runs/val-pan', help='save results to project/name')
|
550 |
+
parser.add_argument('--name', default='exp', help='save to project/name')
|
551 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
552 |
+
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
|
553 |
+
parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
|
554 |
+
opt = parser.parse_args()
|
555 |
+
opt.data = check_yaml(opt.data) # check YAML
|
556 |
+
# opt.save_json |= opt.data.endswith('coco.yaml')
|
557 |
+
opt.save_txt |= opt.save_hybrid
|
558 |
+
print_args(vars(opt))
|
559 |
+
return opt
|
560 |
+
|
561 |
+
|
562 |
+
def main(opt):
|
563 |
+
#check_requirements(requirements=ROOT / 'requirements.txt', exclude=('tensorboard', 'thop'))
|
564 |
+
|
565 |
+
if opt.task in ('train', 'val', 'test'): # run normally
|
566 |
+
if opt.conf_thres > 0.001: # https://github.com/
|
567 |
+
LOGGER.warning(f'WARNING ⚠️ confidence threshold {opt.conf_thres} > 0.001 produces invalid results')
|
568 |
+
if opt.save_hybrid:
|
569 |
+
LOGGER.warning('WARNING ⚠️ --save-hybrid returns high mAP from hybrid labels, not from predictions alone')
|
570 |
+
run(**vars(opt))
|
571 |
+
|
572 |
+
else:
|
573 |
+
weights = opt.weights if isinstance(opt.weights, list) else [opt.weights]
|
574 |
+
opt.half = torch.cuda.is_available() and opt.device != 'cpu' # FP16 for fastest results
|
575 |
+
if opt.task == 'speed': # speed benchmarks
|
576 |
+
# python val.py --task speed --data coco.yaml --batch 1 --weights yolo.pt...
|
577 |
+
opt.conf_thres, opt.iou_thres, opt.save_json = 0.25, 0.45, False
|
578 |
+
for opt.weights in weights:
|
579 |
+
run(**vars(opt), plots=False)
|
580 |
+
|
581 |
+
elif opt.task == 'study': # speed vs mAP benchmarks
|
582 |
+
# python val.py --task study --data coco.yaml --iou 0.7 --weights yolo.pt...
|
583 |
+
for opt.weights in weights:
|
584 |
+
f = f'study_{Path(opt.data).stem}_{Path(opt.weights).stem}.txt' # filename to save to
|
585 |
+
x, y = list(range(256, 1536 + 128, 128)), [] # x axis (image sizes), y axis
|
586 |
+
for opt.imgsz in x: # img-size
|
587 |
+
LOGGER.info(f'\nRunning {f} --imgsz {opt.imgsz}...')
|
588 |
+
r, _, t = run(**vars(opt), plots=False)
|
589 |
+
y.append(r + t) # results and times
|
590 |
+
np.savetxt(f, y, fmt='%10.4g') # save
|
591 |
+
os.system('zip -r study.zip study_*.txt')
|
592 |
+
plot_val_study(x=x) # plot
|
593 |
+
|
594 |
+
|
595 |
+
if __name__ == "__main__":
|
596 |
+
opt = parse_opt()
|
597 |
+
main(opt)
|
requirements.txt
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# requirements
|
2 |
+
# Usage: pip install -r requirements.txt
|
3 |
+
|
4 |
+
# Base ------------------------------------------------------------------------
|
5 |
+
gitpython
|
6 |
+
ipython
|
7 |
+
matplotlib>=3.2.2
|
8 |
+
numpy>=1.18.5
|
9 |
+
opencv-python>=4.1.1
|
10 |
+
Pillow>=7.1.2
|
11 |
+
psutil
|
12 |
+
PyYAML>=5.3.1
|
13 |
+
requests>=2.23.0
|
14 |
+
scipy>=1.4.1
|
15 |
+
thop>=0.1.1
|
16 |
+
opencv-python
|
17 |
+
opencv-contrib-python
|
18 |
+
torch
|
19 |
+
torchvision
|
20 |
+
tqdm>=4.64.0
|
21 |
+
# protobuf<=3.20.1
|
22 |
+
|
23 |
+
# Logging ---------------------------------------------------------------------
|
24 |
+
tensorboard>=2.4.1
|
25 |
+
# clearml>=1.2.0
|
26 |
+
# comet
|
27 |
+
|
28 |
+
# Plotting --------------------------------------------------------------------
|
29 |
+
pandas>=1.1.4
|
30 |
+
seaborn>=0.11.0
|
31 |
+
|
32 |
+
# Export ----------------------------------------------------------------------
|
33 |
+
# coremltools>=6.0
|
34 |
+
# onnx>=1.9.0
|
35 |
+
# onnx-simplifier>=0.4.1
|
36 |
+
# nvidia-pyindex
|
37 |
+
# nvidia-tensorrt
|
38 |
+
# scikit-learn<=1.1.2
|
39 |
+
# tensorflow>=2.4.1
|
40 |
+
# tensorflowjs>=3.9.0
|
41 |
+
# openvino-dev
|
42 |
+
|
43 |
+
# Deploy ----------------------------------------------------------------------
|
44 |
+
# tritonclient[all]~=2.24.0
|
45 |
+
|
46 |
+
# Extras ----------------------------------------------------------------------
|
47 |
+
# mss
|
48 |
+
albumentations>=1.0.3
|
49 |
+
pycocotools>=2.0
|
runs/detect/yolov9_c_640_detect/United_States_000062.jpg
ADDED
![]() |
runs/detect/yolov9_c_640_detect/United_States_000502.jpg
ADDED
![]() |
runs/detect/yolov9_c_640_detect/temp_image.jpg
ADDED
![]() |
scripts/get_coco.sh
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/bin/bash
|
2 |
+
# COCO 2017 dataset http://cocodataset.org
|
3 |
+
# Download command: bash ./scripts/get_coco.sh
|
4 |
+
|
5 |
+
# Download/unzip labels
|
6 |
+
d='./' # unzip directory
|
7 |
+
url=https://github.com/ultralytics/yolov5/releases/download/v1.0/
|
8 |
+
f='coco2017labels-segments.zip' # or 'coco2017labels.zip', 68 MB
|
9 |
+
echo 'Downloading' $url$f ' ...'
|
10 |
+
curl -L $url$f -o $f && unzip -q $f -d $d && rm $f & # download, unzip, remove in background
|
11 |
+
|
12 |
+
# Download/unzip images
|
13 |
+
d='./coco/images' # unzip directory
|
14 |
+
url=http://images.cocodataset.org/zips/
|
15 |
+
f1='train2017.zip' # 19G, 118k images
|
16 |
+
f2='val2017.zip' # 1G, 5k images
|
17 |
+
f3='test2017.zip' # 7G, 41k images (optional)
|
18 |
+
for f in $f1 $f2 $f3; do
|
19 |
+
echo 'Downloading' $url$f '...'
|
20 |
+
curl -L $url$f -o $f && unzip -q $f -d $d && rm $f & # download, unzip, remove in background
|
21 |
+
done
|
22 |
+
wait # finish background tasks
|
segment/predict.py
ADDED
@@ -0,0 +1,246 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import os
|
3 |
+
import platform
|
4 |
+
import sys
|
5 |
+
from pathlib import Path
|
6 |
+
|
7 |
+
import torch
|
8 |
+
|
9 |
+
FILE = Path(__file__).resolve()
|
10 |
+
ROOT = FILE.parents[1] # YOLO root directory
|
11 |
+
if str(ROOT) not in sys.path:
|
12 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
13 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
14 |
+
|
15 |
+
from models.common import DetectMultiBackend
|
16 |
+
from utils.dataloaders import IMG_FORMATS, VID_FORMATS, LoadImages, LoadScreenshots, LoadStreams
|
17 |
+
from utils.general import (LOGGER, Profile, check_file, check_img_size, check_imshow, check_requirements, colorstr, cv2,
|
18 |
+
increment_path, non_max_suppression, print_args, scale_boxes, scale_segments,
|
19 |
+
strip_optimizer, xyxy2xywh)
|
20 |
+
from utils.plots import Annotator, colors, save_one_box
|
21 |
+
from utils.segment.general import masks2segments, process_mask
|
22 |
+
from utils.torch_utils import select_device, smart_inference_mode
|
23 |
+
|
24 |
+
|
25 |
+
@smart_inference_mode()
|
26 |
+
def run(
|
27 |
+
weights=ROOT / 'yolo-seg.pt', # model.pt path(s)
|
28 |
+
source=ROOT / 'data/images', # file/dir/URL/glob/screen/0(webcam)
|
29 |
+
data=ROOT / 'data/coco.yaml', # dataset.yaml path
|
30 |
+
imgsz=(640, 640), # inference size (height, width)
|
31 |
+
conf_thres=0.25, # confidence threshold
|
32 |
+
iou_thres=0.45, # NMS IOU threshold
|
33 |
+
max_det=1000, # maximum detections per image
|
34 |
+
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
|
35 |
+
view_img=False, # show results
|
36 |
+
save_txt=False, # save results to *.txt
|
37 |
+
save_conf=False, # save confidences in --save-txt labels
|
38 |
+
save_crop=False, # save cropped prediction boxes
|
39 |
+
nosave=False, # do not save images/videos
|
40 |
+
classes=None, # filter by class: --class 0, or --class 0 2 3
|
41 |
+
agnostic_nms=False, # class-agnostic NMS
|
42 |
+
augment=False, # augmented inference
|
43 |
+
visualize=False, # visualize features
|
44 |
+
update=False, # update all models
|
45 |
+
project=ROOT / 'runs/predict-seg', # save results to project/name
|
46 |
+
name='exp', # save results to project/name
|
47 |
+
exist_ok=False, # existing project/name ok, do not increment
|
48 |
+
line_thickness=3, # bounding box thickness (pixels)
|
49 |
+
hide_labels=False, # hide labels
|
50 |
+
hide_conf=False, # hide confidences
|
51 |
+
half=False, # use FP16 half-precision inference
|
52 |
+
dnn=False, # use OpenCV DNN for ONNX inference
|
53 |
+
vid_stride=1, # video frame-rate stride
|
54 |
+
retina_masks=False,
|
55 |
+
):
|
56 |
+
source = str(source)
|
57 |
+
save_img = not nosave and not source.endswith('.txt') # save inference images
|
58 |
+
is_file = Path(source).suffix[1:] in (IMG_FORMATS + VID_FORMATS)
|
59 |
+
is_url = source.lower().startswith(('rtsp://', 'rtmp://', 'http://', 'https://'))
|
60 |
+
webcam = source.isnumeric() or source.endswith('.txt') or (is_url and not is_file)
|
61 |
+
screenshot = source.lower().startswith('screen')
|
62 |
+
if is_url and is_file:
|
63 |
+
source = check_file(source) # download
|
64 |
+
|
65 |
+
# Directories
|
66 |
+
save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
|
67 |
+
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
|
68 |
+
|
69 |
+
# Load model
|
70 |
+
device = select_device(device)
|
71 |
+
model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
|
72 |
+
stride, names, pt = model.stride, model.names, model.pt
|
73 |
+
imgsz = check_img_size(imgsz, s=stride) # check image size
|
74 |
+
|
75 |
+
# Dataloader
|
76 |
+
bs = 1 # batch_size
|
77 |
+
if webcam:
|
78 |
+
view_img = check_imshow(warn=True)
|
79 |
+
dataset = LoadStreams(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
|
80 |
+
bs = len(dataset)
|
81 |
+
elif screenshot:
|
82 |
+
dataset = LoadScreenshots(source, img_size=imgsz, stride=stride, auto=pt)
|
83 |
+
else:
|
84 |
+
dataset = LoadImages(source, img_size=imgsz, stride=stride, auto=pt, vid_stride=vid_stride)
|
85 |
+
vid_path, vid_writer = [None] * bs, [None] * bs
|
86 |
+
|
87 |
+
# Run inference
|
88 |
+
model.warmup(imgsz=(1 if pt else bs, 3, *imgsz)) # warmup
|
89 |
+
seen, windows, dt = 0, [], (Profile(), Profile(), Profile())
|
90 |
+
for path, im, im0s, vid_cap, s in dataset:
|
91 |
+
with dt[0]:
|
92 |
+
im = torch.from_numpy(im).to(model.device)
|
93 |
+
im = im.half() if model.fp16 else im.float() # uint8 to fp16/32
|
94 |
+
im /= 255 # 0 - 255 to 0.0 - 1.0
|
95 |
+
if len(im.shape) == 3:
|
96 |
+
im = im[None] # expand for batch dim
|
97 |
+
|
98 |
+
# Inference
|
99 |
+
with dt[1]:
|
100 |
+
visualize = increment_path(save_dir / Path(path).stem, mkdir=True) if visualize else False
|
101 |
+
pred, proto = model(im, augment=augment, visualize=visualize)[:2]
|
102 |
+
|
103 |
+
# NMS
|
104 |
+
with dt[2]:
|
105 |
+
pred = non_max_suppression(pred, conf_thres, iou_thres, classes, agnostic_nms, max_det=max_det, nm=32)
|
106 |
+
|
107 |
+
# Second-stage classifier (optional)
|
108 |
+
# pred = utils.general.apply_classifier(pred, classifier_model, im, im0s)
|
109 |
+
|
110 |
+
# Process predictions
|
111 |
+
for i, det in enumerate(pred): # per image
|
112 |
+
seen += 1
|
113 |
+
if webcam: # batch_size >= 1
|
114 |
+
p, im0, frame = path[i], im0s[i].copy(), dataset.count
|
115 |
+
s += f'{i}: '
|
116 |
+
else:
|
117 |
+
p, im0, frame = path, im0s.copy(), getattr(dataset, 'frame', 0)
|
118 |
+
|
119 |
+
p = Path(p) # to Path
|
120 |
+
save_path = str(save_dir / p.name) # im.jpg
|
121 |
+
txt_path = str(save_dir / 'labels' / p.stem) + ('' if dataset.mode == 'image' else f'_{frame}') # im.txt
|
122 |
+
s += '%gx%g ' % im.shape[2:] # print string
|
123 |
+
imc = im0.copy() if save_crop else im0 # for save_crop
|
124 |
+
annotator = Annotator(im0, line_width=line_thickness, example=str(names))
|
125 |
+
if len(det):
|
126 |
+
masks = process_mask(proto[i], det[:, 6:], det[:, :4], im.shape[2:], upsample=True) # HWC
|
127 |
+
det[:, :4] = scale_boxes(im.shape[2:], det[:, :4], im0.shape).round() # rescale boxes to im0 size
|
128 |
+
|
129 |
+
# Segments
|
130 |
+
if save_txt:
|
131 |
+
segments = reversed(masks2segments(masks))
|
132 |
+
segments = [scale_segments(im.shape[2:], x, im0.shape, normalize=True) for x in segments]
|
133 |
+
|
134 |
+
# Print results
|
135 |
+
for c in det[:, 5].unique():
|
136 |
+
n = (det[:, 5] == c).sum() # detections per class
|
137 |
+
s += f"{n} {names[int(c)]}{'s' * (n > 1)}, " # add to string
|
138 |
+
|
139 |
+
# Mask plotting
|
140 |
+
annotator.masks(masks,
|
141 |
+
colors=[colors(x, True) for x in det[:, 5]],
|
142 |
+
im_gpu=None if retina_masks else im[i])
|
143 |
+
|
144 |
+
# Write results
|
145 |
+
for j, (*xyxy, conf, cls) in enumerate(reversed(det[:, :6])):
|
146 |
+
if save_txt: # Write to file
|
147 |
+
segj = segments[j].reshape(-1) # (n,2) to (n*2)
|
148 |
+
line = (cls, *segj, conf) if save_conf else (cls, *segj) # label format
|
149 |
+
with open(f'{txt_path}.txt', 'a') as f:
|
150 |
+
f.write(('%g ' * len(line)).rstrip() % line + '\n')
|
151 |
+
|
152 |
+
if save_img or save_crop or view_img: # Add bbox to image
|
153 |
+
c = int(cls) # integer class
|
154 |
+
label = None if hide_labels else (names[c] if hide_conf else f'{names[c]} {conf:.2f}')
|
155 |
+
annotator.box_label(xyxy, label, color=colors(c, True))
|
156 |
+
# annotator.draw.polygon(segments[j], outline=colors(c, True), width=3)
|
157 |
+
if save_crop:
|
158 |
+
save_one_box(xyxy, imc, file=save_dir / 'crops' / names[c] / f'{p.stem}.jpg', BGR=True)
|
159 |
+
|
160 |
+
# Stream results
|
161 |
+
im0 = annotator.result()
|
162 |
+
if view_img:
|
163 |
+
if platform.system() == 'Linux' and p not in windows:
|
164 |
+
windows.append(p)
|
165 |
+
cv2.namedWindow(str(p), cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO) # allow window resize (Linux)
|
166 |
+
cv2.resizeWindow(str(p), im0.shape[1], im0.shape[0])
|
167 |
+
cv2.imshow(str(p), im0)
|
168 |
+
if cv2.waitKey(1) == ord('q'): # 1 millisecond
|
169 |
+
exit()
|
170 |
+
|
171 |
+
# Save results (image with detections)
|
172 |
+
if save_img:
|
173 |
+
if dataset.mode == 'image':
|
174 |
+
cv2.imwrite(save_path, im0)
|
175 |
+
else: # 'video' or 'stream'
|
176 |
+
if vid_path[i] != save_path: # new video
|
177 |
+
vid_path[i] = save_path
|
178 |
+
if isinstance(vid_writer[i], cv2.VideoWriter):
|
179 |
+
vid_writer[i].release() # release previous video writer
|
180 |
+
if vid_cap: # video
|
181 |
+
fps = vid_cap.get(cv2.CAP_PROP_FPS)
|
182 |
+
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
183 |
+
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
184 |
+
else: # stream
|
185 |
+
fps, w, h = 30, im0.shape[1], im0.shape[0]
|
186 |
+
save_path = str(Path(save_path).with_suffix('.mp4')) # force *.mp4 suffix on results videos
|
187 |
+
vid_writer[i] = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))
|
188 |
+
vid_writer[i].write(im0)
|
189 |
+
|
190 |
+
# Print time (inference-only)
|
191 |
+
LOGGER.info(f"{s}{'' if len(det) else '(no detections), '}{dt[1].dt * 1E3:.1f}ms")
|
192 |
+
|
193 |
+
# Print results
|
194 |
+
t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image
|
195 |
+
LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {(1, 3, *imgsz)}' % t)
|
196 |
+
if save_txt or save_img:
|
197 |
+
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
|
198 |
+
LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
|
199 |
+
if update:
|
200 |
+
strip_optimizer(weights[0]) # update model (to fix SourceChangeWarning)
|
201 |
+
|
202 |
+
|
203 |
+
def parse_opt():
|
204 |
+
parser = argparse.ArgumentParser()
|
205 |
+
parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolo-seg.pt', help='model path(s)')
|
206 |
+
parser.add_argument('--source', type=str, default=ROOT / 'data/images', help='file/dir/URL/glob/screen/0(webcam)')
|
207 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='(optional) dataset.yaml path')
|
208 |
+
parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
|
209 |
+
parser.add_argument('--conf-thres', type=float, default=0.25, help='confidence threshold')
|
210 |
+
parser.add_argument('--iou-thres', type=float, default=0.45, help='NMS IoU threshold')
|
211 |
+
parser.add_argument('--max-det', type=int, default=1000, help='maximum detections per image')
|
212 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
213 |
+
parser.add_argument('--view-img', action='store_true', help='show results')
|
214 |
+
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
|
215 |
+
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
|
216 |
+
parser.add_argument('--save-crop', action='store_true', help='save cropped prediction boxes')
|
217 |
+
parser.add_argument('--nosave', action='store_true', help='do not save images/videos')
|
218 |
+
parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --classes 0, or --classes 0 2 3')
|
219 |
+
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS')
|
220 |
+
parser.add_argument('--augment', action='store_true', help='augmented inference')
|
221 |
+
parser.add_argument('--visualize', action='store_true', help='visualize features')
|
222 |
+
parser.add_argument('--update', action='store_true', help='update all models')
|
223 |
+
parser.add_argument('--project', default=ROOT / 'runs/predict-seg', help='save results to project/name')
|
224 |
+
parser.add_argument('--name', default='exp', help='save results to project/name')
|
225 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
226 |
+
parser.add_argument('--line-thickness', default=3, type=int, help='bounding box thickness (pixels)')
|
227 |
+
parser.add_argument('--hide-labels', default=False, action='store_true', help='hide labels')
|
228 |
+
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
|
229 |
+
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
|
230 |
+
parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
|
231 |
+
parser.add_argument('--vid-stride', type=int, default=1, help='video frame-rate stride')
|
232 |
+
parser.add_argument('--retina-masks', action='store_true', help='whether to plot masks in native resolution')
|
233 |
+
opt = parser.parse_args()
|
234 |
+
opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand
|
235 |
+
print_args(vars(opt))
|
236 |
+
return opt
|
237 |
+
|
238 |
+
|
239 |
+
def main(opt):
|
240 |
+
check_requirements(exclude=('tensorboard', 'thop'))
|
241 |
+
run(**vars(opt))
|
242 |
+
|
243 |
+
|
244 |
+
if __name__ == "__main__":
|
245 |
+
opt = parse_opt()
|
246 |
+
main(opt)
|
segment/train.py
ADDED
@@ -0,0 +1,646 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import math
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import sys
|
6 |
+
import time
|
7 |
+
from copy import deepcopy
|
8 |
+
from datetime import datetime
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
import numpy as np
|
12 |
+
import torch
|
13 |
+
import torch.distributed as dist
|
14 |
+
import torch.nn as nn
|
15 |
+
import yaml
|
16 |
+
from torch.optim import lr_scheduler
|
17 |
+
from tqdm import tqdm
|
18 |
+
|
19 |
+
FILE = Path(__file__).resolve()
|
20 |
+
ROOT = FILE.parents[1] # YOLO root directory
|
21 |
+
if str(ROOT) not in sys.path:
|
22 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
23 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
24 |
+
|
25 |
+
import segment.val as validate # for end-of-epoch mAP
|
26 |
+
from models.experimental import attempt_load
|
27 |
+
from models.yolo import SegmentationModel
|
28 |
+
from utils.autoanchor import check_anchors
|
29 |
+
from utils.autobatch import check_train_batch_size
|
30 |
+
from utils.callbacks import Callbacks
|
31 |
+
from utils.downloads import attempt_download, is_url
|
32 |
+
from utils.general import (LOGGER, TQDM_BAR_FORMAT, check_amp, check_dataset, check_file, check_git_info,
|
33 |
+
check_git_status, check_img_size, check_requirements, check_suffix, check_yaml, colorstr,
|
34 |
+
get_latest_run, increment_path, init_seeds, intersect_dicts, labels_to_class_weights,
|
35 |
+
labels_to_image_weights, one_cycle, print_args, print_mutation, strip_optimizer, yaml_save)
|
36 |
+
from utils.loggers import GenericLogger
|
37 |
+
from utils.plots import plot_evolve, plot_labels
|
38 |
+
from utils.segment.dataloaders import create_dataloader
|
39 |
+
from utils.segment.loss_tal import ComputeLoss
|
40 |
+
from utils.segment.metrics import KEYS, fitness
|
41 |
+
from utils.segment.plots import plot_images_and_masks, plot_results_with_masks
|
42 |
+
from utils.torch_utils import (EarlyStopping, ModelEMA, de_parallel, select_device, smart_DDP, smart_optimizer,
|
43 |
+
smart_resume, torch_distributed_zero_first)
|
44 |
+
|
45 |
+
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
|
46 |
+
RANK = int(os.getenv('RANK', -1))
|
47 |
+
WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))
|
48 |
+
GIT_INFO = None#check_git_info()
|
49 |
+
|
50 |
+
|
51 |
+
def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictionary
|
52 |
+
save_dir, epochs, batch_size, weights, single_cls, evolve, data, cfg, resume, noval, nosave, workers, freeze, mask_ratio = \
|
53 |
+
Path(opt.save_dir), opt.epochs, opt.batch_size, opt.weights, opt.single_cls, opt.evolve, opt.data, opt.cfg, \
|
54 |
+
opt.resume, opt.noval, opt.nosave, opt.workers, opt.freeze, opt.mask_ratio
|
55 |
+
# callbacks.run('on_pretrain_routine_start')
|
56 |
+
|
57 |
+
# Directories
|
58 |
+
w = save_dir / 'weights' # weights dir
|
59 |
+
(w.parent if evolve else w).mkdir(parents=True, exist_ok=True) # make dir
|
60 |
+
last, best = w / 'last.pt', w / 'best.pt'
|
61 |
+
|
62 |
+
# Hyperparameters
|
63 |
+
if isinstance(hyp, str):
|
64 |
+
with open(hyp, errors='ignore') as f:
|
65 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
66 |
+
LOGGER.info(colorstr('hyperparameters: ') + ', '.join(f'{k}={v}' for k, v in hyp.items()))
|
67 |
+
opt.hyp = hyp.copy() # for saving hyps to checkpoints
|
68 |
+
|
69 |
+
# Save run settings
|
70 |
+
if not evolve:
|
71 |
+
yaml_save(save_dir / 'hyp.yaml', hyp)
|
72 |
+
yaml_save(save_dir / 'opt.yaml', vars(opt))
|
73 |
+
|
74 |
+
# Loggers
|
75 |
+
data_dict = None
|
76 |
+
if RANK in {-1, 0}:
|
77 |
+
logger = GenericLogger(opt=opt, console_logger=LOGGER)
|
78 |
+
|
79 |
+
# Config
|
80 |
+
plots = not evolve and not opt.noplots # create plots
|
81 |
+
overlap = not opt.no_overlap
|
82 |
+
cuda = device.type != 'cpu'
|
83 |
+
init_seeds(opt.seed + 1 + RANK, deterministic=True)
|
84 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
85 |
+
data_dict = data_dict or check_dataset(data) # check if None
|
86 |
+
train_path, val_path = data_dict['train'], data_dict['val']
|
87 |
+
nc = 1 if single_cls else int(data_dict['nc']) # number of classes
|
88 |
+
names = {0: 'item'} if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
|
89 |
+
#is_coco = isinstance(val_path, str) and val_path.endswith('coco/val2017.txt') # COCO dataset
|
90 |
+
is_coco = isinstance(val_path, str) and val_path.endswith('val2017.txt') # COCO dataset
|
91 |
+
|
92 |
+
# Model
|
93 |
+
check_suffix(weights, '.pt') # check weights
|
94 |
+
pretrained = weights.endswith('.pt')
|
95 |
+
if pretrained:
|
96 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
97 |
+
weights = attempt_download(weights) # download if not found locally
|
98 |
+
ckpt = torch.load(weights, map_location='cpu') # load checkpoint to CPU to avoid CUDA memory leak
|
99 |
+
model = SegmentationModel(cfg or ckpt['model'].yaml, ch=3, nc=nc).to(device)
|
100 |
+
exclude = ['anchor'] if (cfg or hyp.get('anchors')) and not resume else [] # exclude keys
|
101 |
+
csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
|
102 |
+
csd = intersect_dicts(csd, model.state_dict(), exclude=exclude) # intersect
|
103 |
+
model.load_state_dict(csd, strict=False) # load
|
104 |
+
LOGGER.info(f'Transferred {len(csd)}/{len(model.state_dict())} items from {weights}') # report
|
105 |
+
else:
|
106 |
+
model = SegmentationModel(cfg, ch=3, nc=nc).to(device) # create
|
107 |
+
amp = check_amp(model) # check AMP
|
108 |
+
|
109 |
+
# Freeze
|
110 |
+
freeze = [f'model.{x}.' for x in (freeze if len(freeze) > 1 else range(freeze[0]))] # layers to freeze
|
111 |
+
for k, v in model.named_parameters():
|
112 |
+
#v.requires_grad = True # train all layers
|
113 |
+
# v.register_hook(lambda x: torch.nan_to_num(x)) # NaN to 0 (commented for erratic training results)
|
114 |
+
if any(x in k for x in freeze):
|
115 |
+
LOGGER.info(f'freezing {k}')
|
116 |
+
v.requires_grad = False
|
117 |
+
|
118 |
+
# Image size
|
119 |
+
gs = max(int(model.stride.max()), 32) # grid size (max stride)
|
120 |
+
imgsz = check_img_size(opt.imgsz, gs, floor=gs * 2) # verify imgsz is gs-multiple
|
121 |
+
|
122 |
+
# Batch size
|
123 |
+
if RANK == -1 and batch_size == -1: # single-GPU only, estimate best batch size
|
124 |
+
batch_size = check_train_batch_size(model, imgsz, amp)
|
125 |
+
logger.update_params({"batch_size": batch_size})
|
126 |
+
# loggers.on_params_update({"batch_size": batch_size})
|
127 |
+
|
128 |
+
# Optimizer
|
129 |
+
nbs = 64 # nominal batch size
|
130 |
+
accumulate = max(round(nbs / batch_size), 1) # accumulate loss before optimizing
|
131 |
+
hyp['weight_decay'] *= batch_size * accumulate / nbs # scale weight_decay
|
132 |
+
optimizer = smart_optimizer(model, opt.optimizer, hyp['lr0'], hyp['momentum'], hyp['weight_decay'])
|
133 |
+
|
134 |
+
# Scheduler
|
135 |
+
if opt.cos_lr:
|
136 |
+
lf = one_cycle(1, hyp['lrf'], epochs) # cosine 1->hyp['lrf']
|
137 |
+
else:
|
138 |
+
lf = lambda x: (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf'] # linear
|
139 |
+
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf) # plot_lr_scheduler(optimizer, scheduler, epochs)
|
140 |
+
|
141 |
+
# EMA
|
142 |
+
ema = ModelEMA(model) if RANK in {-1, 0} else None
|
143 |
+
|
144 |
+
# Resume
|
145 |
+
best_fitness, start_epoch = 0.0, 0
|
146 |
+
if pretrained:
|
147 |
+
if resume:
|
148 |
+
best_fitness, start_epoch, epochs = smart_resume(ckpt, optimizer, ema, weights, epochs, resume)
|
149 |
+
del ckpt, csd
|
150 |
+
|
151 |
+
# DP mode
|
152 |
+
if cuda and RANK == -1 and torch.cuda.device_count() > 1:
|
153 |
+
LOGGER.warning('WARNING ⚠️ DP not recommended, use torch.distributed.run for best DDP Multi-GPU results.')
|
154 |
+
model = torch.nn.DataParallel(model)
|
155 |
+
|
156 |
+
# SyncBatchNorm
|
157 |
+
if opt.sync_bn and cuda and RANK != -1:
|
158 |
+
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model).to(device)
|
159 |
+
LOGGER.info('Using SyncBatchNorm()')
|
160 |
+
|
161 |
+
# Trainloader
|
162 |
+
train_loader, dataset = create_dataloader(
|
163 |
+
train_path,
|
164 |
+
imgsz,
|
165 |
+
batch_size // WORLD_SIZE,
|
166 |
+
gs,
|
167 |
+
single_cls,
|
168 |
+
hyp=hyp,
|
169 |
+
augment=True,
|
170 |
+
cache=None if opt.cache == 'val' else opt.cache,
|
171 |
+
rect=opt.rect,
|
172 |
+
rank=LOCAL_RANK,
|
173 |
+
workers=workers,
|
174 |
+
image_weights=opt.image_weights,
|
175 |
+
close_mosaic=opt.close_mosaic != 0,
|
176 |
+
quad=opt.quad,
|
177 |
+
prefix=colorstr('train: '),
|
178 |
+
shuffle=True,
|
179 |
+
mask_downsample_ratio=mask_ratio,
|
180 |
+
overlap_mask=overlap,
|
181 |
+
)
|
182 |
+
labels = np.concatenate(dataset.labels, 0)
|
183 |
+
mlc = int(labels[:, 0].max()) # max label class
|
184 |
+
assert mlc < nc, f'Label class {mlc} exceeds nc={nc} in {data}. Possible class labels are 0-{nc - 1}'
|
185 |
+
|
186 |
+
# Process 0
|
187 |
+
if RANK in {-1, 0}:
|
188 |
+
val_loader = create_dataloader(val_path,
|
189 |
+
imgsz,
|
190 |
+
batch_size // WORLD_SIZE * 2,
|
191 |
+
gs,
|
192 |
+
single_cls,
|
193 |
+
hyp=hyp,
|
194 |
+
cache=None if noval else opt.cache,
|
195 |
+
rect=True,
|
196 |
+
rank=-1,
|
197 |
+
workers=workers * 2,
|
198 |
+
pad=0.5,
|
199 |
+
mask_downsample_ratio=mask_ratio,
|
200 |
+
overlap_mask=overlap,
|
201 |
+
prefix=colorstr('val: '))[0]
|
202 |
+
|
203 |
+
if not resume:
|
204 |
+
#if not opt.noautoanchor:
|
205 |
+
# check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz) # run AutoAnchor
|
206 |
+
model.half().float() # pre-reduce anchor precision
|
207 |
+
|
208 |
+
if plots:
|
209 |
+
plot_labels(labels, names, save_dir)
|
210 |
+
# callbacks.run('on_pretrain_routine_end', labels, names)
|
211 |
+
|
212 |
+
# DDP mode
|
213 |
+
if cuda and RANK != -1:
|
214 |
+
model = smart_DDP(model)
|
215 |
+
|
216 |
+
# Model attributes
|
217 |
+
nl = de_parallel(model).model[-1].nl # number of detection layers (to scale hyps)
|
218 |
+
#hyp['box'] *= 3 / nl # scale to layers
|
219 |
+
#hyp['cls'] *= nc / 80 * 3 / nl # scale to classes and layers
|
220 |
+
#hyp['obj'] *= (imgsz / 640) ** 2 * 3 / nl # scale to image size and layers
|
221 |
+
hyp['label_smoothing'] = opt.label_smoothing
|
222 |
+
model.nc = nc # attach number of classes to model
|
223 |
+
model.hyp = hyp # attach hyperparameters to model
|
224 |
+
model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) * nc # attach class weights
|
225 |
+
model.names = names
|
226 |
+
|
227 |
+
# Start training
|
228 |
+
t0 = time.time()
|
229 |
+
nb = len(train_loader) # number of batches
|
230 |
+
nw = max(round(hyp['warmup_epochs'] * nb), 100) # number of warmup iterations, max(3 epochs, 100 iterations)
|
231 |
+
# nw = min(nw, (epochs - start_epoch) / 2 * nb) # limit warmup to < 1/2 of training
|
232 |
+
last_opt_step = -1
|
233 |
+
maps = np.zeros(nc) # mAP per class
|
234 |
+
results = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) # P, R, [email protected], [email protected], val_loss(box, obj, cls)
|
235 |
+
scheduler.last_epoch = start_epoch - 1 # do not move
|
236 |
+
scaler = torch.cuda.amp.GradScaler(enabled=amp)
|
237 |
+
stopper, stop = EarlyStopping(patience=opt.patience), False
|
238 |
+
compute_loss = ComputeLoss(model, overlap=overlap) # init loss class
|
239 |
+
# callbacks.run('on_train_start')
|
240 |
+
LOGGER.info(f'Image sizes {imgsz} train, {imgsz} val\n'
|
241 |
+
f'Using {train_loader.num_workers * WORLD_SIZE} dataloader workers\n'
|
242 |
+
f"Logging results to {colorstr('bold', save_dir)}\n"
|
243 |
+
f'Starting training for {epochs} epochs...')
|
244 |
+
for epoch in range(start_epoch, epochs): # epoch ------------------------------------------------------------------
|
245 |
+
# callbacks.run('on_train_epoch_start')
|
246 |
+
model.train()
|
247 |
+
|
248 |
+
# Update image weights (optional, single-GPU only)
|
249 |
+
if opt.image_weights:
|
250 |
+
cw = model.class_weights.cpu().numpy() * (1 - maps) ** 2 / nc # class weights
|
251 |
+
iw = labels_to_image_weights(dataset.labels, nc=nc, class_weights=cw) # image weights
|
252 |
+
dataset.indices = random.choices(range(dataset.n), weights=iw, k=dataset.n) # rand weighted idx
|
253 |
+
if epoch == (epochs - opt.close_mosaic):
|
254 |
+
LOGGER.info("Closing dataloader mosaic")
|
255 |
+
dataset.mosaic = False
|
256 |
+
|
257 |
+
# Update mosaic border (optional)
|
258 |
+
# b = int(random.uniform(0.25 * imgsz, 0.75 * imgsz + gs) // gs * gs)
|
259 |
+
# dataset.mosaic_border = [b - imgsz, -b] # height, width borders
|
260 |
+
|
261 |
+
mloss = torch.zeros(4, device=device) # mean losses
|
262 |
+
if RANK != -1:
|
263 |
+
train_loader.sampler.set_epoch(epoch)
|
264 |
+
pbar = enumerate(train_loader)
|
265 |
+
LOGGER.info(('\n' + '%11s' * 8) %
|
266 |
+
('Epoch', 'GPU_mem', 'box_loss', 'seg_loss', 'cls_loss', 'dfl_loss', 'Instances', 'Size'))
|
267 |
+
if RANK in {-1, 0}:
|
268 |
+
pbar = tqdm(pbar, total=nb, bar_format=TQDM_BAR_FORMAT) # progress bar
|
269 |
+
optimizer.zero_grad()
|
270 |
+
for i, (imgs, targets, paths, _, masks) in pbar: # batch ------------------------------------------------------
|
271 |
+
# callbacks.run('on_train_batch_start')
|
272 |
+
ni = i + nb * epoch # number integrated batches (since train start)
|
273 |
+
imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0
|
274 |
+
|
275 |
+
# Warmup
|
276 |
+
if ni <= nw:
|
277 |
+
xi = [0, nw] # x interp
|
278 |
+
# compute_loss.gr = np.interp(ni, xi, [0.0, 1.0]) # iou loss ratio (obj_loss = 1.0 or iou)
|
279 |
+
accumulate = max(1, np.interp(ni, xi, [1, nbs / batch_size]).round())
|
280 |
+
for j, x in enumerate(optimizer.param_groups):
|
281 |
+
# bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0
|
282 |
+
x['lr'] = np.interp(ni, xi, [hyp['warmup_bias_lr'] if j == 0 else 0.0, x['initial_lr'] * lf(epoch)])
|
283 |
+
if 'momentum' in x:
|
284 |
+
x['momentum'] = np.interp(ni, xi, [hyp['warmup_momentum'], hyp['momentum']])
|
285 |
+
|
286 |
+
# Multi-scale
|
287 |
+
if opt.multi_scale:
|
288 |
+
sz = random.randrange(imgsz * 0.5, imgsz * 1.5 + gs) // gs * gs # size
|
289 |
+
sf = sz / max(imgs.shape[2:]) # scale factor
|
290 |
+
if sf != 1:
|
291 |
+
ns = [math.ceil(x * sf / gs) * gs for x in imgs.shape[2:]] # new shape (stretched to gs-multiple)
|
292 |
+
imgs = nn.functional.interpolate(imgs, size=ns, mode='bilinear', align_corners=False)
|
293 |
+
|
294 |
+
# Forward
|
295 |
+
with torch.cuda.amp.autocast(amp):
|
296 |
+
pred = model(imgs) # forward
|
297 |
+
loss, loss_items = compute_loss(pred, targets.to(device), masks=masks.to(device).float())
|
298 |
+
if RANK != -1:
|
299 |
+
loss *= WORLD_SIZE # gradient averaged between devices in DDP mode
|
300 |
+
if opt.quad:
|
301 |
+
loss *= 4.
|
302 |
+
|
303 |
+
# Backward
|
304 |
+
scaler.scale(loss).backward()
|
305 |
+
|
306 |
+
# Optimize - https://pytorch.org/docs/master/notes/amp_examples.html
|
307 |
+
if ni - last_opt_step >= accumulate:
|
308 |
+
scaler.unscale_(optimizer) # unscale gradients
|
309 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=10.0) # clip gradients
|
310 |
+
scaler.step(optimizer) # optimizer.step
|
311 |
+
scaler.update()
|
312 |
+
optimizer.zero_grad()
|
313 |
+
if ema:
|
314 |
+
ema.update(model)
|
315 |
+
last_opt_step = ni
|
316 |
+
|
317 |
+
# Log
|
318 |
+
if RANK in {-1, 0}:
|
319 |
+
mloss = (mloss * i + loss_items) / (i + 1) # update mean losses
|
320 |
+
mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB)
|
321 |
+
pbar.set_description(('%11s' * 2 + '%11.4g' * 6) %
|
322 |
+
(f'{epoch}/{epochs - 1}', mem, *mloss, targets.shape[0], imgs.shape[-1]))
|
323 |
+
# callbacks.run('on_train_batch_end', model, ni, imgs, targets, paths)
|
324 |
+
# if callbacks.stop_training:
|
325 |
+
# return
|
326 |
+
|
327 |
+
# Mosaic plots
|
328 |
+
if plots:
|
329 |
+
if ni < 3:
|
330 |
+
plot_images_and_masks(imgs, targets, masks, paths, save_dir / f"train_batch{ni}.jpg")
|
331 |
+
if ni == 10:
|
332 |
+
files = sorted(save_dir.glob('train*.jpg'))
|
333 |
+
logger.log_images(files, "Mosaics", epoch)
|
334 |
+
# end batch ------------------------------------------------------------------------------------------------
|
335 |
+
|
336 |
+
# Scheduler
|
337 |
+
lr = [x['lr'] for x in optimizer.param_groups] # for loggers
|
338 |
+
scheduler.step()
|
339 |
+
|
340 |
+
if RANK in {-1, 0}:
|
341 |
+
# mAP
|
342 |
+
# callbacks.run('on_train_epoch_end', epoch=epoch)
|
343 |
+
ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'names', 'stride', 'class_weights'])
|
344 |
+
final_epoch = (epoch + 1 == epochs) or stopper.possible_stop
|
345 |
+
if not noval or final_epoch: # Calculate mAP
|
346 |
+
results, maps, _ = validate.run(data_dict,
|
347 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
348 |
+
imgsz=imgsz,
|
349 |
+
half=amp,
|
350 |
+
model=ema.ema,
|
351 |
+
single_cls=single_cls,
|
352 |
+
dataloader=val_loader,
|
353 |
+
save_dir=save_dir,
|
354 |
+
plots=False,
|
355 |
+
callbacks=callbacks,
|
356 |
+
compute_loss=compute_loss,
|
357 |
+
mask_downsample_ratio=mask_ratio,
|
358 |
+
overlap=overlap)
|
359 |
+
|
360 |
+
# Update best mAP
|
361 |
+
fi = fitness(np.array(results).reshape(1, -1)) # weighted combination of [P, R, [email protected], [email protected]]
|
362 |
+
stop = stopper(epoch=epoch, fitness=fi) # early stop check
|
363 |
+
if fi > best_fitness:
|
364 |
+
best_fitness = fi
|
365 |
+
log_vals = list(mloss) + list(results) + lr
|
366 |
+
# callbacks.run('on_fit_epoch_end', log_vals, epoch, best_fitness, fi)
|
367 |
+
# Log val metrics and media
|
368 |
+
metrics_dict = dict(zip(KEYS, log_vals))
|
369 |
+
logger.log_metrics(metrics_dict, epoch)
|
370 |
+
|
371 |
+
# Save model
|
372 |
+
if (not nosave) or (final_epoch and not evolve): # if save
|
373 |
+
ckpt = {
|
374 |
+
'epoch': epoch,
|
375 |
+
'best_fitness': best_fitness,
|
376 |
+
'model': deepcopy(de_parallel(model)).half(),
|
377 |
+
'ema': deepcopy(ema.ema).half(),
|
378 |
+
'updates': ema.updates,
|
379 |
+
'optimizer': optimizer.state_dict(),
|
380 |
+
'opt': vars(opt),
|
381 |
+
'git': GIT_INFO, # {remote, branch, commit} if a git repo
|
382 |
+
'date': datetime.now().isoformat()}
|
383 |
+
|
384 |
+
# Save last, best and delete
|
385 |
+
torch.save(ckpt, last)
|
386 |
+
if best_fitness == fi:
|
387 |
+
torch.save(ckpt, best)
|
388 |
+
if opt.save_period > 0 and epoch % opt.save_period == 0:
|
389 |
+
torch.save(ckpt, w / f'epoch{epoch}.pt')
|
390 |
+
logger.log_model(w / f'epoch{epoch}.pt')
|
391 |
+
del ckpt
|
392 |
+
# callbacks.run('on_model_save', last, epoch, final_epoch, best_fitness, fi)
|
393 |
+
|
394 |
+
# EarlyStopping
|
395 |
+
if RANK != -1: # if DDP training
|
396 |
+
broadcast_list = [stop if RANK == 0 else None]
|
397 |
+
dist.broadcast_object_list(broadcast_list, 0) # broadcast 'stop' to all ranks
|
398 |
+
if RANK != 0:
|
399 |
+
stop = broadcast_list[0]
|
400 |
+
if stop:
|
401 |
+
break # must break all DDP ranks
|
402 |
+
|
403 |
+
# end epoch ----------------------------------------------------------------------------------------------------
|
404 |
+
# end training -----------------------------------------------------------------------------------------------------
|
405 |
+
if RANK in {-1, 0}:
|
406 |
+
LOGGER.info(f'\n{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.')
|
407 |
+
for f in last, best:
|
408 |
+
if f.exists():
|
409 |
+
strip_optimizer(f) # strip optimizers
|
410 |
+
if f is best:
|
411 |
+
LOGGER.info(f'\nValidating {f}...')
|
412 |
+
results, _, _ = validate.run(
|
413 |
+
data_dict,
|
414 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
415 |
+
imgsz=imgsz,
|
416 |
+
model=attempt_load(f, device).half(),
|
417 |
+
iou_thres=0.65 if is_coco else 0.60, # best pycocotools at iou 0.65
|
418 |
+
single_cls=single_cls,
|
419 |
+
dataloader=val_loader,
|
420 |
+
save_dir=save_dir,
|
421 |
+
save_json=is_coco,
|
422 |
+
verbose=True,
|
423 |
+
plots=plots,
|
424 |
+
callbacks=callbacks,
|
425 |
+
compute_loss=compute_loss,
|
426 |
+
mask_downsample_ratio=mask_ratio,
|
427 |
+
overlap=overlap) # val best model with plots
|
428 |
+
if is_coco:
|
429 |
+
# callbacks.run('on_fit_epoch_end', list(mloss) + list(results) + lr, epoch, best_fitness, fi)
|
430 |
+
metrics_dict = dict(zip(KEYS, list(mloss) + list(results) + lr))
|
431 |
+
logger.log_metrics(metrics_dict, epoch)
|
432 |
+
|
433 |
+
# callbacks.run('on_train_end', last, best, epoch, results)
|
434 |
+
# on train end callback using genericLogger
|
435 |
+
logger.log_metrics(dict(zip(KEYS[4:16], results)), epochs)
|
436 |
+
if not opt.evolve:
|
437 |
+
logger.log_model(best, epoch)
|
438 |
+
if plots:
|
439 |
+
plot_results_with_masks(file=save_dir / 'results.csv') # save results.png
|
440 |
+
files = ['results.png', 'confusion_matrix.png', *(f'{x}_curve.png' for x in ('F1', 'PR', 'P', 'R'))]
|
441 |
+
files = [(save_dir / f) for f in files if (save_dir / f).exists()] # filter
|
442 |
+
LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}")
|
443 |
+
logger.log_images(files, "Results", epoch + 1)
|
444 |
+
logger.log_images(sorted(save_dir.glob('val*.jpg')), "Validation", epoch + 1)
|
445 |
+
torch.cuda.empty_cache()
|
446 |
+
return results
|
447 |
+
|
448 |
+
|
449 |
+
def parse_opt(known=False):
|
450 |
+
parser = argparse.ArgumentParser()
|
451 |
+
parser.add_argument('--weights', type=str, default=ROOT / 'yolo-seg.pt', help='initial weights path')
|
452 |
+
parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
|
453 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128-seg.yaml', help='dataset.yaml path')
|
454 |
+
parser.add_argument('--hyp', type=str, default=ROOT / 'data/hyps/hyp.scratch-low.yaml', help='hyperparameters path')
|
455 |
+
parser.add_argument('--epochs', type=int, default=100, help='total training epochs')
|
456 |
+
parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs, -1 for autobatch')
|
457 |
+
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='train, val image size (pixels)')
|
458 |
+
parser.add_argument('--rect', action='store_true', help='rectangular training')
|
459 |
+
parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')
|
460 |
+
parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
|
461 |
+
parser.add_argument('--noval', action='store_true', help='only validate final epoch')
|
462 |
+
parser.add_argument('--noautoanchor', action='store_true', help='disable AutoAnchor')
|
463 |
+
parser.add_argument('--noplots', action='store_true', help='save no plot files')
|
464 |
+
parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations')
|
465 |
+
parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
|
466 |
+
parser.add_argument('--cache', type=str, nargs='?', const='ram', help='image --cache ram/disk')
|
467 |
+
parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
|
468 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
469 |
+
parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
|
470 |
+
parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
|
471 |
+
parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'AdamW', 'LION'], default='SGD', help='optimizer')
|
472 |
+
parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
|
473 |
+
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
|
474 |
+
parser.add_argument('--project', default=ROOT / 'runs/train-seg', help='save to project/name')
|
475 |
+
parser.add_argument('--name', default='exp', help='save to project/name')
|
476 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
477 |
+
parser.add_argument('--quad', action='store_true', help='quad dataloader')
|
478 |
+
parser.add_argument('--cos-lr', action='store_true', help='cosine LR scheduler')
|
479 |
+
parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
|
480 |
+
parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)')
|
481 |
+
parser.add_argument('--freeze', nargs='+', type=int, default=[0], help='Freeze layers: backbone=10, first3=0 1 2')
|
482 |
+
parser.add_argument('--save-period', type=int, default=-1, help='Save checkpoint every x epochs (disabled if < 1)')
|
483 |
+
parser.add_argument('--seed', type=int, default=0, help='Global training seed')
|
484 |
+
parser.add_argument('--local_rank', type=int, default=-1, help='Automatic DDP Multi-GPU argument, do not modify')
|
485 |
+
parser.add_argument('--close-mosaic', type=int, default=0, help='Experimental')
|
486 |
+
|
487 |
+
# Instance Segmentation Args
|
488 |
+
parser.add_argument('--mask-ratio', type=int, default=4, help='Downsample the truth masks to saving memory')
|
489 |
+
parser.add_argument('--no-overlap', action='store_true', help='Overlap masks train faster at slightly less mAP')
|
490 |
+
|
491 |
+
return parser.parse_known_args()[0] if known else parser.parse_args()
|
492 |
+
|
493 |
+
|
494 |
+
def main(opt, callbacks=Callbacks()):
|
495 |
+
# Checks
|
496 |
+
if RANK in {-1, 0}:
|
497 |
+
print_args(vars(opt))
|
498 |
+
#check_git_status()
|
499 |
+
#check_requirements()
|
500 |
+
|
501 |
+
# Resume
|
502 |
+
if opt.resume and not opt.evolve: # resume from specified or most recent last.pt
|
503 |
+
last = Path(check_file(opt.resume) if isinstance(opt.resume, str) else get_latest_run())
|
504 |
+
opt_yaml = last.parent.parent / 'opt.yaml' # train options yaml
|
505 |
+
opt_data = opt.data # original dataset
|
506 |
+
if opt_yaml.is_file():
|
507 |
+
with open(opt_yaml, errors='ignore') as f:
|
508 |
+
d = yaml.safe_load(f)
|
509 |
+
else:
|
510 |
+
d = torch.load(last, map_location='cpu')['opt']
|
511 |
+
opt = argparse.Namespace(**d) # replace
|
512 |
+
opt.cfg, opt.weights, opt.resume = '', str(last), True # reinstate
|
513 |
+
if is_url(opt_data):
|
514 |
+
opt.data = check_file(opt_data) # avoid HUB resume auth timeout
|
515 |
+
else:
|
516 |
+
opt.data, opt.cfg, opt.hyp, opt.weights, opt.project = \
|
517 |
+
check_file(opt.data), check_yaml(opt.cfg), check_yaml(opt.hyp), str(opt.weights), str(opt.project) # checks
|
518 |
+
assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified'
|
519 |
+
if opt.evolve:
|
520 |
+
if opt.project == str(ROOT / 'runs/train'): # if default project name, rename to runs/evolve
|
521 |
+
opt.project = str(ROOT / 'runs/evolve')
|
522 |
+
opt.exist_ok, opt.resume = opt.resume, False # pass resume to exist_ok and disable resume
|
523 |
+
if opt.name == 'cfg':
|
524 |
+
opt.name = Path(opt.cfg).stem # use model.yaml as name
|
525 |
+
opt.save_dir = str(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok))
|
526 |
+
|
527 |
+
# DDP mode
|
528 |
+
device = select_device(opt.device, batch_size=opt.batch_size)
|
529 |
+
if LOCAL_RANK != -1:
|
530 |
+
msg = 'is not compatible with YOLO Multi-GPU DDP training'
|
531 |
+
assert not opt.image_weights, f'--image-weights {msg}'
|
532 |
+
assert not opt.evolve, f'--evolve {msg}'
|
533 |
+
assert opt.batch_size != -1, f'AutoBatch with --batch-size -1 {msg}, please pass a valid --batch-size'
|
534 |
+
assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'
|
535 |
+
assert torch.cuda.device_count() > LOCAL_RANK, 'insufficient CUDA devices for DDP command'
|
536 |
+
torch.cuda.set_device(LOCAL_RANK)
|
537 |
+
device = torch.device('cuda', LOCAL_RANK)
|
538 |
+
dist.init_process_group(backend="nccl" if dist.is_nccl_available() else "gloo")
|
539 |
+
|
540 |
+
# Train
|
541 |
+
if not opt.evolve:
|
542 |
+
train(opt.hyp, opt, device, callbacks)
|
543 |
+
|
544 |
+
# Evolve hyperparameters (optional)
|
545 |
+
else:
|
546 |
+
# Hyperparameter evolution metadata (mutation scale 0-1, lower_limit, upper_limit)
|
547 |
+
meta = {
|
548 |
+
'lr0': (1, 1e-5, 1e-1), # initial learning rate (SGD=1E-2, Adam=1E-3)
|
549 |
+
'lrf': (1, 0.01, 1.0), # final OneCycleLR learning rate (lr0 * lrf)
|
550 |
+
'momentum': (0.3, 0.6, 0.98), # SGD momentum/Adam beta1
|
551 |
+
'weight_decay': (1, 0.0, 0.001), # optimizer weight decay
|
552 |
+
'warmup_epochs': (1, 0.0, 5.0), # warmup epochs (fractions ok)
|
553 |
+
'warmup_momentum': (1, 0.0, 0.95), # warmup initial momentum
|
554 |
+
'warmup_bias_lr': (1, 0.0, 0.2), # warmup initial bias lr
|
555 |
+
'box': (1, 0.02, 0.2), # box loss gain
|
556 |
+
'cls': (1, 0.2, 4.0), # cls loss gain
|
557 |
+
'cls_pw': (1, 0.5, 2.0), # cls BCELoss positive_weight
|
558 |
+
'obj': (1, 0.2, 4.0), # obj loss gain (scale with pixels)
|
559 |
+
'obj_pw': (1, 0.5, 2.0), # obj BCELoss positive_weight
|
560 |
+
'iou_t': (0, 0.1, 0.7), # IoU training threshold
|
561 |
+
'anchor_t': (1, 2.0, 8.0), # anchor-multiple threshold
|
562 |
+
'anchors': (2, 2.0, 10.0), # anchors per output grid (0 to ignore)
|
563 |
+
'fl_gamma': (0, 0.0, 2.0), # focal loss gamma (efficientDet default gamma=1.5)
|
564 |
+
'hsv_h': (1, 0.0, 0.1), # image HSV-Hue augmentation (fraction)
|
565 |
+
'hsv_s': (1, 0.0, 0.9), # image HSV-Saturation augmentation (fraction)
|
566 |
+
'hsv_v': (1, 0.0, 0.9), # image HSV-Value augmentation (fraction)
|
567 |
+
'degrees': (1, 0.0, 45.0), # image rotation (+/- deg)
|
568 |
+
'translate': (1, 0.0, 0.9), # image translation (+/- fraction)
|
569 |
+
'scale': (1, 0.0, 0.9), # image scale (+/- gain)
|
570 |
+
'shear': (1, 0.0, 10.0), # image shear (+/- deg)
|
571 |
+
'perspective': (0, 0.0, 0.001), # image perspective (+/- fraction), range 0-0.001
|
572 |
+
'flipud': (1, 0.0, 1.0), # image flip up-down (probability)
|
573 |
+
'fliplr': (0, 0.0, 1.0), # image flip left-right (probability)
|
574 |
+
'mosaic': (1, 0.0, 1.0), # image mixup (probability)
|
575 |
+
'mixup': (1, 0.0, 1.0), # image mixup (probability)
|
576 |
+
'copy_paste': (1, 0.0, 1.0)} # segment copy-paste (probability)
|
577 |
+
|
578 |
+
with open(opt.hyp, errors='ignore') as f:
|
579 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
580 |
+
if 'anchors' not in hyp: # anchors commented in hyp.yaml
|
581 |
+
hyp['anchors'] = 3
|
582 |
+
if opt.noautoanchor:
|
583 |
+
del hyp['anchors'], meta['anchors']
|
584 |
+
opt.noval, opt.nosave, save_dir = True, True, Path(opt.save_dir) # only val/save final epoch
|
585 |
+
# ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
|
586 |
+
evolve_yaml, evolve_csv = save_dir / 'hyp_evolve.yaml', save_dir / 'evolve.csv'
|
587 |
+
if opt.bucket:
|
588 |
+
os.system(f'gsutil cp gs://{opt.bucket}/evolve.csv {evolve_csv}') # download evolve.csv if exists
|
589 |
+
|
590 |
+
for _ in range(opt.evolve): # generations to evolve
|
591 |
+
if evolve_csv.exists(): # if evolve.csv exists: select best hyps and mutate
|
592 |
+
# Select parent(s)
|
593 |
+
parent = 'single' # parent selection method: 'single' or 'weighted'
|
594 |
+
x = np.loadtxt(evolve_csv, ndmin=2, delimiter=',', skiprows=1)
|
595 |
+
n = min(5, len(x)) # number of previous results to consider
|
596 |
+
x = x[np.argsort(-fitness(x))][:n] # top n mutations
|
597 |
+
w = fitness(x) - fitness(x).min() + 1E-6 # weights (sum > 0)
|
598 |
+
if parent == 'single' or len(x) == 1:
|
599 |
+
# x = x[random.randint(0, n - 1)] # random selection
|
600 |
+
x = x[random.choices(range(n), weights=w)[0]] # weighted selection
|
601 |
+
elif parent == 'weighted':
|
602 |
+
x = (x * w.reshape(n, 1)).sum(0) / w.sum() # weighted combination
|
603 |
+
|
604 |
+
# Mutate
|
605 |
+
mp, s = 0.8, 0.2 # mutation probability, sigma
|
606 |
+
npr = np.random
|
607 |
+
npr.seed(int(time.time()))
|
608 |
+
g = np.array([meta[k][0] for k in hyp.keys()]) # gains 0-1
|
609 |
+
ng = len(meta)
|
610 |
+
v = np.ones(ng)
|
611 |
+
while all(v == 1): # mutate until a change occurs (prevent duplicates)
|
612 |
+
v = (g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1).clip(0.3, 3.0)
|
613 |
+
for i, k in enumerate(hyp.keys()): # plt.hist(v.ravel(), 300)
|
614 |
+
hyp[k] = float(x[i + 7] * v[i]) # mutate
|
615 |
+
|
616 |
+
# Constrain to limits
|
617 |
+
for k, v in meta.items():
|
618 |
+
hyp[k] = max(hyp[k], v[1]) # lower limit
|
619 |
+
hyp[k] = min(hyp[k], v[2]) # upper limit
|
620 |
+
hyp[k] = round(hyp[k], 5) # significant digits
|
621 |
+
|
622 |
+
# Train mutation
|
623 |
+
results = train(hyp.copy(), opt, device, callbacks)
|
624 |
+
callbacks = Callbacks()
|
625 |
+
# Write mutation results
|
626 |
+
print_mutation(KEYS, results, hyp.copy(), save_dir, opt.bucket)
|
627 |
+
|
628 |
+
# Plot results
|
629 |
+
plot_evolve(evolve_csv)
|
630 |
+
LOGGER.info(f'Hyperparameter evolution finished {opt.evolve} generations\n'
|
631 |
+
f"Results saved to {colorstr('bold', save_dir)}\n"
|
632 |
+
f'Usage example: $ python train.py --hyp {evolve_yaml}')
|
633 |
+
|
634 |
+
|
635 |
+
def run(**kwargs):
|
636 |
+
# Usage: import train; train.run(data='coco128.yaml', imgsz=320, weights='yolo.pt')
|
637 |
+
opt = parse_opt(True)
|
638 |
+
for k, v in kwargs.items():
|
639 |
+
setattr(opt, k, v)
|
640 |
+
main(opt)
|
641 |
+
return opt
|
642 |
+
|
643 |
+
|
644 |
+
if __name__ == "__main__":
|
645 |
+
opt = parse_opt()
|
646 |
+
main(opt)
|
segment/train_dual.py
ADDED
@@ -0,0 +1,647 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import math
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import sys
|
6 |
+
import time
|
7 |
+
from copy import deepcopy
|
8 |
+
from datetime import datetime
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
import numpy as np
|
12 |
+
import torch
|
13 |
+
import torch.distributed as dist
|
14 |
+
import torch.nn as nn
|
15 |
+
import yaml
|
16 |
+
from torch.optim import lr_scheduler
|
17 |
+
from tqdm import tqdm
|
18 |
+
|
19 |
+
FILE = Path(__file__).resolve()
|
20 |
+
ROOT = FILE.parents[1] # YOLO root directory
|
21 |
+
if str(ROOT) not in sys.path:
|
22 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
23 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
24 |
+
|
25 |
+
import segment.val_dual as validate # for end-of-epoch mAP
|
26 |
+
from models.experimental import attempt_load
|
27 |
+
from models.yolo import SegmentationModel
|
28 |
+
from utils.autoanchor import check_anchors
|
29 |
+
from utils.autobatch import check_train_batch_size
|
30 |
+
from utils.callbacks import Callbacks
|
31 |
+
from utils.downloads import attempt_download, is_url
|
32 |
+
from utils.general import (LOGGER, TQDM_BAR_FORMAT, check_amp, check_dataset, check_file, check_git_info,
|
33 |
+
check_git_status, check_img_size, check_requirements, check_suffix, check_yaml, colorstr,
|
34 |
+
get_latest_run, increment_path, init_seeds, intersect_dicts, labels_to_class_weights,
|
35 |
+
labels_to_image_weights, one_cycle, print_args, print_mutation, strip_optimizer, yaml_save)
|
36 |
+
from utils.loggers import GenericLogger
|
37 |
+
from utils.plots import plot_evolve, plot_labels
|
38 |
+
from utils.segment.dataloaders import create_dataloader
|
39 |
+
from utils.segment.loss_tal_dual import ComputeLoss
|
40 |
+
#from utils.segment.loss_tal_dual import ComputeLossLH as ComputeLoss
|
41 |
+
from utils.segment.metrics import KEYS, fitness
|
42 |
+
from utils.segment.plots import plot_images_and_masks, plot_results_with_masks
|
43 |
+
from utils.torch_utils import (EarlyStopping, ModelEMA, de_parallel, select_device, smart_DDP, smart_optimizer,
|
44 |
+
smart_resume, torch_distributed_zero_first)
|
45 |
+
|
46 |
+
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
|
47 |
+
RANK = int(os.getenv('RANK', -1))
|
48 |
+
WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))
|
49 |
+
GIT_INFO = None#check_git_info()
|
50 |
+
|
51 |
+
|
52 |
+
def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictionary
|
53 |
+
save_dir, epochs, batch_size, weights, single_cls, evolve, data, cfg, resume, noval, nosave, workers, freeze, mask_ratio = \
|
54 |
+
Path(opt.save_dir), opt.epochs, opt.batch_size, opt.weights, opt.single_cls, opt.evolve, opt.data, opt.cfg, \
|
55 |
+
opt.resume, opt.noval, opt.nosave, opt.workers, opt.freeze, opt.mask_ratio
|
56 |
+
# callbacks.run('on_pretrain_routine_start')
|
57 |
+
|
58 |
+
# Directories
|
59 |
+
w = save_dir / 'weights' # weights dir
|
60 |
+
(w.parent if evolve else w).mkdir(parents=True, exist_ok=True) # make dir
|
61 |
+
last, best = w / 'last.pt', w / 'best.pt'
|
62 |
+
|
63 |
+
# Hyperparameters
|
64 |
+
if isinstance(hyp, str):
|
65 |
+
with open(hyp, errors='ignore') as f:
|
66 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
67 |
+
LOGGER.info(colorstr('hyperparameters: ') + ', '.join(f'{k}={v}' for k, v in hyp.items()))
|
68 |
+
opt.hyp = hyp.copy() # for saving hyps to checkpoints
|
69 |
+
|
70 |
+
# Save run settings
|
71 |
+
if not evolve:
|
72 |
+
yaml_save(save_dir / 'hyp.yaml', hyp)
|
73 |
+
yaml_save(save_dir / 'opt.yaml', vars(opt))
|
74 |
+
|
75 |
+
# Loggers
|
76 |
+
data_dict = None
|
77 |
+
if RANK in {-1, 0}:
|
78 |
+
logger = GenericLogger(opt=opt, console_logger=LOGGER)
|
79 |
+
|
80 |
+
# Config
|
81 |
+
plots = not evolve and not opt.noplots # create plots
|
82 |
+
overlap = not opt.no_overlap
|
83 |
+
cuda = device.type != 'cpu'
|
84 |
+
init_seeds(opt.seed + 1 + RANK, deterministic=True)
|
85 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
86 |
+
data_dict = data_dict or check_dataset(data) # check if None
|
87 |
+
train_path, val_path = data_dict['train'], data_dict['val']
|
88 |
+
nc = 1 if single_cls else int(data_dict['nc']) # number of classes
|
89 |
+
names = {0: 'item'} if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
|
90 |
+
#is_coco = isinstance(val_path, str) and val_path.endswith('coco/val2017.txt') # COCO dataset
|
91 |
+
is_coco = isinstance(val_path, str) and val_path.endswith('val2017.txt') # COCO dataset
|
92 |
+
|
93 |
+
# Model
|
94 |
+
check_suffix(weights, '.pt') # check weights
|
95 |
+
pretrained = weights.endswith('.pt')
|
96 |
+
if pretrained:
|
97 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
98 |
+
weights = attempt_download(weights) # download if not found locally
|
99 |
+
ckpt = torch.load(weights, map_location='cpu') # load checkpoint to CPU to avoid CUDA memory leak
|
100 |
+
model = SegmentationModel(cfg or ckpt['model'].yaml, ch=3, nc=nc).to(device)
|
101 |
+
exclude = ['anchor'] if (cfg or hyp.get('anchors')) and not resume else [] # exclude keys
|
102 |
+
csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
|
103 |
+
csd = intersect_dicts(csd, model.state_dict(), exclude=exclude) # intersect
|
104 |
+
model.load_state_dict(csd, strict=False) # load
|
105 |
+
LOGGER.info(f'Transferred {len(csd)}/{len(model.state_dict())} items from {weights}') # report
|
106 |
+
else:
|
107 |
+
model = SegmentationModel(cfg, ch=3, nc=nc).to(device) # create
|
108 |
+
amp = check_amp(model) # check AMP
|
109 |
+
|
110 |
+
# Freeze
|
111 |
+
freeze = [f'model.{x}.' for x in (freeze if len(freeze) > 1 else range(freeze[0]))] # layers to freeze
|
112 |
+
for k, v in model.named_parameters():
|
113 |
+
#v.requires_grad = True # train all layers
|
114 |
+
# v.register_hook(lambda x: torch.nan_to_num(x)) # NaN to 0 (commented for erratic training results)
|
115 |
+
if any(x in k for x in freeze):
|
116 |
+
LOGGER.info(f'freezing {k}')
|
117 |
+
v.requires_grad = False
|
118 |
+
|
119 |
+
# Image size
|
120 |
+
gs = max(int(model.stride.max()), 32) # grid size (max stride)
|
121 |
+
imgsz = check_img_size(opt.imgsz, gs, floor=gs * 2) # verify imgsz is gs-multiple
|
122 |
+
|
123 |
+
# Batch size
|
124 |
+
if RANK == -1 and batch_size == -1: # single-GPU only, estimate best batch size
|
125 |
+
batch_size = check_train_batch_size(model, imgsz, amp)
|
126 |
+
logger.update_params({"batch_size": batch_size})
|
127 |
+
# loggers.on_params_update({"batch_size": batch_size})
|
128 |
+
|
129 |
+
# Optimizer
|
130 |
+
nbs = 64 # nominal batch size
|
131 |
+
accumulate = max(round(nbs / batch_size), 1) # accumulate loss before optimizing
|
132 |
+
hyp['weight_decay'] *= batch_size * accumulate / nbs # scale weight_decay
|
133 |
+
optimizer = smart_optimizer(model, opt.optimizer, hyp['lr0'], hyp['momentum'], hyp['weight_decay'])
|
134 |
+
|
135 |
+
# Scheduler
|
136 |
+
if opt.cos_lr:
|
137 |
+
lf = one_cycle(1, hyp['lrf'], epochs) # cosine 1->hyp['lrf']
|
138 |
+
else:
|
139 |
+
lf = lambda x: (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf'] # linear
|
140 |
+
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf) # plot_lr_scheduler(optimizer, scheduler, epochs)
|
141 |
+
|
142 |
+
# EMA
|
143 |
+
ema = ModelEMA(model) if RANK in {-1, 0} else None
|
144 |
+
|
145 |
+
# Resume
|
146 |
+
best_fitness, start_epoch = 0.0, 0
|
147 |
+
if pretrained:
|
148 |
+
if resume:
|
149 |
+
best_fitness, start_epoch, epochs = smart_resume(ckpt, optimizer, ema, weights, epochs, resume)
|
150 |
+
del ckpt, csd
|
151 |
+
|
152 |
+
# DP mode
|
153 |
+
if cuda and RANK == -1 and torch.cuda.device_count() > 1:
|
154 |
+
LOGGER.warning('WARNING ⚠️ DP not recommended, use torch.distributed.run for best DDP Multi-GPU results.')
|
155 |
+
model = torch.nn.DataParallel(model)
|
156 |
+
|
157 |
+
# SyncBatchNorm
|
158 |
+
if opt.sync_bn and cuda and RANK != -1:
|
159 |
+
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model).to(device)
|
160 |
+
LOGGER.info('Using SyncBatchNorm()')
|
161 |
+
|
162 |
+
# Trainloader
|
163 |
+
train_loader, dataset = create_dataloader(
|
164 |
+
train_path,
|
165 |
+
imgsz,
|
166 |
+
batch_size // WORLD_SIZE,
|
167 |
+
gs,
|
168 |
+
single_cls,
|
169 |
+
hyp=hyp,
|
170 |
+
augment=True,
|
171 |
+
cache=None if opt.cache == 'val' else opt.cache,
|
172 |
+
rect=opt.rect,
|
173 |
+
rank=LOCAL_RANK,
|
174 |
+
workers=workers,
|
175 |
+
image_weights=opt.image_weights,
|
176 |
+
close_mosaic=opt.close_mosaic != 0,
|
177 |
+
quad=opt.quad,
|
178 |
+
prefix=colorstr('train: '),
|
179 |
+
shuffle=True,
|
180 |
+
mask_downsample_ratio=mask_ratio,
|
181 |
+
overlap_mask=overlap,
|
182 |
+
)
|
183 |
+
labels = np.concatenate(dataset.labels, 0)
|
184 |
+
mlc = int(labels[:, 0].max()) # max label class
|
185 |
+
assert mlc < nc, f'Label class {mlc} exceeds nc={nc} in {data}. Possible class labels are 0-{nc - 1}'
|
186 |
+
|
187 |
+
# Process 0
|
188 |
+
if RANK in {-1, 0}:
|
189 |
+
val_loader = create_dataloader(val_path,
|
190 |
+
imgsz,
|
191 |
+
batch_size // WORLD_SIZE * 2,
|
192 |
+
gs,
|
193 |
+
single_cls,
|
194 |
+
hyp=hyp,
|
195 |
+
cache=None if noval else opt.cache,
|
196 |
+
rect=True,
|
197 |
+
rank=-1,
|
198 |
+
workers=workers * 2,
|
199 |
+
pad=0.5,
|
200 |
+
mask_downsample_ratio=mask_ratio,
|
201 |
+
overlap_mask=overlap,
|
202 |
+
prefix=colorstr('val: '))[0]
|
203 |
+
|
204 |
+
if not resume:
|
205 |
+
#if not opt.noautoanchor:
|
206 |
+
# check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz) # run AutoAnchor
|
207 |
+
model.half().float() # pre-reduce anchor precision
|
208 |
+
|
209 |
+
if plots:
|
210 |
+
plot_labels(labels, names, save_dir)
|
211 |
+
# callbacks.run('on_pretrain_routine_end', labels, names)
|
212 |
+
|
213 |
+
# DDP mode
|
214 |
+
if cuda and RANK != -1:
|
215 |
+
model = smart_DDP(model)
|
216 |
+
|
217 |
+
# Model attributes
|
218 |
+
nl = de_parallel(model).model[-1].nl # number of detection layers (to scale hyps)
|
219 |
+
#hyp['box'] *= 3 / nl # scale to layers
|
220 |
+
#hyp['cls'] *= nc / 80 * 3 / nl # scale to classes and layers
|
221 |
+
#hyp['obj'] *= (imgsz / 640) ** 2 * 3 / nl # scale to image size and layers
|
222 |
+
hyp['label_smoothing'] = opt.label_smoothing
|
223 |
+
model.nc = nc # attach number of classes to model
|
224 |
+
model.hyp = hyp # attach hyperparameters to model
|
225 |
+
model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) * nc # attach class weights
|
226 |
+
model.names = names
|
227 |
+
|
228 |
+
# Start training
|
229 |
+
t0 = time.time()
|
230 |
+
nb = len(train_loader) # number of batches
|
231 |
+
nw = max(round(hyp['warmup_epochs'] * nb), 100) # number of warmup iterations, max(3 epochs, 100 iterations)
|
232 |
+
# nw = min(nw, (epochs - start_epoch) / 2 * nb) # limit warmup to < 1/2 of training
|
233 |
+
last_opt_step = -1
|
234 |
+
maps = np.zeros(nc) # mAP per class
|
235 |
+
results = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0) # P, R, [email protected], [email protected], val_loss(box, obj, cls)
|
236 |
+
scheduler.last_epoch = start_epoch - 1 # do not move
|
237 |
+
scaler = torch.cuda.amp.GradScaler(enabled=amp)
|
238 |
+
stopper, stop = EarlyStopping(patience=opt.patience), False
|
239 |
+
compute_loss = ComputeLoss(model, overlap=overlap) # init loss class
|
240 |
+
# callbacks.run('on_train_start')
|
241 |
+
LOGGER.info(f'Image sizes {imgsz} train, {imgsz} val\n'
|
242 |
+
f'Using {train_loader.num_workers * WORLD_SIZE} dataloader workers\n'
|
243 |
+
f"Logging results to {colorstr('bold', save_dir)}\n"
|
244 |
+
f'Starting training for {epochs} epochs...')
|
245 |
+
for epoch in range(start_epoch, epochs): # epoch ------------------------------------------------------------------
|
246 |
+
# callbacks.run('on_train_epoch_start')
|
247 |
+
model.train()
|
248 |
+
|
249 |
+
# Update image weights (optional, single-GPU only)
|
250 |
+
if opt.image_weights:
|
251 |
+
cw = model.class_weights.cpu().numpy() * (1 - maps) ** 2 / nc # class weights
|
252 |
+
iw = labels_to_image_weights(dataset.labels, nc=nc, class_weights=cw) # image weights
|
253 |
+
dataset.indices = random.choices(range(dataset.n), weights=iw, k=dataset.n) # rand weighted idx
|
254 |
+
if epoch == (epochs - opt.close_mosaic):
|
255 |
+
LOGGER.info("Closing dataloader mosaic")
|
256 |
+
dataset.mosaic = False
|
257 |
+
|
258 |
+
# Update mosaic border (optional)
|
259 |
+
# b = int(random.uniform(0.25 * imgsz, 0.75 * imgsz + gs) // gs * gs)
|
260 |
+
# dataset.mosaic_border = [b - imgsz, -b] # height, width borders
|
261 |
+
|
262 |
+
mloss = torch.zeros(4, device=device) # mean losses
|
263 |
+
if RANK != -1:
|
264 |
+
train_loader.sampler.set_epoch(epoch)
|
265 |
+
pbar = enumerate(train_loader)
|
266 |
+
LOGGER.info(('\n' + '%11s' * 8) %
|
267 |
+
('Epoch', 'GPU_mem', 'box_loss', 'seg_loss', 'cls_loss', 'dfl_loss', 'Instances', 'Size'))
|
268 |
+
if RANK in {-1, 0}:
|
269 |
+
pbar = tqdm(pbar, total=nb, bar_format=TQDM_BAR_FORMAT) # progress bar
|
270 |
+
optimizer.zero_grad()
|
271 |
+
for i, (imgs, targets, paths, _, masks) in pbar: # batch ------------------------------------------------------
|
272 |
+
# callbacks.run('on_train_batch_start')
|
273 |
+
ni = i + nb * epoch # number integrated batches (since train start)
|
274 |
+
imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0
|
275 |
+
|
276 |
+
# Warmup
|
277 |
+
if ni <= nw:
|
278 |
+
xi = [0, nw] # x interp
|
279 |
+
# compute_loss.gr = np.interp(ni, xi, [0.0, 1.0]) # iou loss ratio (obj_loss = 1.0 or iou)
|
280 |
+
accumulate = max(1, np.interp(ni, xi, [1, nbs / batch_size]).round())
|
281 |
+
for j, x in enumerate(optimizer.param_groups):
|
282 |
+
# bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0
|
283 |
+
x['lr'] = np.interp(ni, xi, [hyp['warmup_bias_lr'] if j == 0 else 0.0, x['initial_lr'] * lf(epoch)])
|
284 |
+
if 'momentum' in x:
|
285 |
+
x['momentum'] = np.interp(ni, xi, [hyp['warmup_momentum'], hyp['momentum']])
|
286 |
+
|
287 |
+
# Multi-scale
|
288 |
+
if opt.multi_scale:
|
289 |
+
sz = random.randrange(imgsz * 0.5, imgsz * 1.5 + gs) // gs * gs # size
|
290 |
+
sf = sz / max(imgs.shape[2:]) # scale factor
|
291 |
+
if sf != 1:
|
292 |
+
ns = [math.ceil(x * sf / gs) * gs for x in imgs.shape[2:]] # new shape (stretched to gs-multiple)
|
293 |
+
imgs = nn.functional.interpolate(imgs, size=ns, mode='bilinear', align_corners=False)
|
294 |
+
|
295 |
+
# Forward
|
296 |
+
with torch.cuda.amp.autocast(amp):
|
297 |
+
pred = model(imgs) # forward
|
298 |
+
loss, loss_items = compute_loss(pred, targets.to(device), masks=masks.to(device).float())
|
299 |
+
if RANK != -1:
|
300 |
+
loss *= WORLD_SIZE # gradient averaged between devices in DDP mode
|
301 |
+
if opt.quad:
|
302 |
+
loss *= 4.
|
303 |
+
|
304 |
+
# Backward
|
305 |
+
scaler.scale(loss).backward()
|
306 |
+
|
307 |
+
# Optimize - https://pytorch.org/docs/master/notes/amp_examples.html
|
308 |
+
if ni - last_opt_step >= accumulate:
|
309 |
+
scaler.unscale_(optimizer) # unscale gradients
|
310 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=10.0) # clip gradients
|
311 |
+
scaler.step(optimizer) # optimizer.step
|
312 |
+
scaler.update()
|
313 |
+
optimizer.zero_grad()
|
314 |
+
if ema:
|
315 |
+
ema.update(model)
|
316 |
+
last_opt_step = ni
|
317 |
+
|
318 |
+
# Log
|
319 |
+
if RANK in {-1, 0}:
|
320 |
+
mloss = (mloss * i + loss_items) / (i + 1) # update mean losses
|
321 |
+
mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB)
|
322 |
+
pbar.set_description(('%11s' * 2 + '%11.4g' * 6) %
|
323 |
+
(f'{epoch}/{epochs - 1}', mem, *mloss, targets.shape[0], imgs.shape[-1]))
|
324 |
+
# callbacks.run('on_train_batch_end', model, ni, imgs, targets, paths)
|
325 |
+
# if callbacks.stop_training:
|
326 |
+
# return
|
327 |
+
|
328 |
+
# Mosaic plots
|
329 |
+
if plots:
|
330 |
+
if ni < 3:
|
331 |
+
plot_images_and_masks(imgs, targets, masks, paths, save_dir / f"train_batch{ni}.jpg")
|
332 |
+
if ni == 10:
|
333 |
+
files = sorted(save_dir.glob('train*.jpg'))
|
334 |
+
logger.log_images(files, "Mosaics", epoch)
|
335 |
+
# end batch ------------------------------------------------------------------------------------------------
|
336 |
+
|
337 |
+
# Scheduler
|
338 |
+
lr = [x['lr'] for x in optimizer.param_groups] # for loggers
|
339 |
+
scheduler.step()
|
340 |
+
|
341 |
+
if RANK in {-1, 0}:
|
342 |
+
# mAP
|
343 |
+
# callbacks.run('on_train_epoch_end', epoch=epoch)
|
344 |
+
ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'names', 'stride', 'class_weights'])
|
345 |
+
final_epoch = (epoch + 1 == epochs) or stopper.possible_stop
|
346 |
+
if not noval or final_epoch: # Calculate mAP
|
347 |
+
results, maps, _ = validate.run(data_dict,
|
348 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
349 |
+
imgsz=imgsz,
|
350 |
+
half=amp,
|
351 |
+
model=ema.ema,
|
352 |
+
single_cls=single_cls,
|
353 |
+
dataloader=val_loader,
|
354 |
+
save_dir=save_dir,
|
355 |
+
plots=False,
|
356 |
+
callbacks=callbacks,
|
357 |
+
compute_loss=compute_loss,
|
358 |
+
mask_downsample_ratio=mask_ratio,
|
359 |
+
overlap=overlap)
|
360 |
+
|
361 |
+
# Update best mAP
|
362 |
+
fi = fitness(np.array(results).reshape(1, -1)) # weighted combination of [P, R, [email protected], [email protected]]
|
363 |
+
stop = stopper(epoch=epoch, fitness=fi) # early stop check
|
364 |
+
if fi > best_fitness:
|
365 |
+
best_fitness = fi
|
366 |
+
log_vals = list(mloss) + list(results) + lr
|
367 |
+
# callbacks.run('on_fit_epoch_end', log_vals, epoch, best_fitness, fi)
|
368 |
+
# Log val metrics and media
|
369 |
+
metrics_dict = dict(zip(KEYS, log_vals))
|
370 |
+
logger.log_metrics(metrics_dict, epoch)
|
371 |
+
|
372 |
+
# Save model
|
373 |
+
if (not nosave) or (final_epoch and not evolve): # if save
|
374 |
+
ckpt = {
|
375 |
+
'epoch': epoch,
|
376 |
+
'best_fitness': best_fitness,
|
377 |
+
'model': deepcopy(de_parallel(model)).half(),
|
378 |
+
'ema': deepcopy(ema.ema).half(),
|
379 |
+
'updates': ema.updates,
|
380 |
+
'optimizer': optimizer.state_dict(),
|
381 |
+
'opt': vars(opt),
|
382 |
+
'git': GIT_INFO, # {remote, branch, commit} if a git repo
|
383 |
+
'date': datetime.now().isoformat()}
|
384 |
+
|
385 |
+
# Save last, best and delete
|
386 |
+
torch.save(ckpt, last)
|
387 |
+
if best_fitness == fi:
|
388 |
+
torch.save(ckpt, best)
|
389 |
+
if opt.save_period > 0 and epoch % opt.save_period == 0:
|
390 |
+
torch.save(ckpt, w / f'epoch{epoch}.pt')
|
391 |
+
logger.log_model(w / f'epoch{epoch}.pt')
|
392 |
+
del ckpt
|
393 |
+
# callbacks.run('on_model_save', last, epoch, final_epoch, best_fitness, fi)
|
394 |
+
|
395 |
+
# EarlyStopping
|
396 |
+
if RANK != -1: # if DDP training
|
397 |
+
broadcast_list = [stop if RANK == 0 else None]
|
398 |
+
dist.broadcast_object_list(broadcast_list, 0) # broadcast 'stop' to all ranks
|
399 |
+
if RANK != 0:
|
400 |
+
stop = broadcast_list[0]
|
401 |
+
if stop:
|
402 |
+
break # must break all DDP ranks
|
403 |
+
|
404 |
+
# end epoch ----------------------------------------------------------------------------------------------------
|
405 |
+
# end training -----------------------------------------------------------------------------------------------------
|
406 |
+
if RANK in {-1, 0}:
|
407 |
+
LOGGER.info(f'\n{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.')
|
408 |
+
for f in last, best:
|
409 |
+
if f.exists():
|
410 |
+
strip_optimizer(f) # strip optimizers
|
411 |
+
if f is best:
|
412 |
+
LOGGER.info(f'\nValidating {f}...')
|
413 |
+
results, _, _ = validate.run(
|
414 |
+
data_dict,
|
415 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
416 |
+
imgsz=imgsz,
|
417 |
+
model=attempt_load(f, device).half(),
|
418 |
+
iou_thres=0.65 if is_coco else 0.60, # best pycocotools at iou 0.65
|
419 |
+
single_cls=single_cls,
|
420 |
+
dataloader=val_loader,
|
421 |
+
save_dir=save_dir,
|
422 |
+
save_json=is_coco,
|
423 |
+
verbose=True,
|
424 |
+
plots=plots,
|
425 |
+
callbacks=callbacks,
|
426 |
+
compute_loss=compute_loss,
|
427 |
+
mask_downsample_ratio=mask_ratio,
|
428 |
+
overlap=overlap) # val best model with plots
|
429 |
+
if is_coco:
|
430 |
+
# callbacks.run('on_fit_epoch_end', list(mloss) + list(results) + lr, epoch, best_fitness, fi)
|
431 |
+
metrics_dict = dict(zip(KEYS, list(mloss) + list(results) + lr))
|
432 |
+
logger.log_metrics(metrics_dict, epoch)
|
433 |
+
|
434 |
+
# callbacks.run('on_train_end', last, best, epoch, results)
|
435 |
+
# on train end callback using genericLogger
|
436 |
+
logger.log_metrics(dict(zip(KEYS[4:16], results)), epochs)
|
437 |
+
if not opt.evolve:
|
438 |
+
logger.log_model(best, epoch)
|
439 |
+
if plots:
|
440 |
+
plot_results_with_masks(file=save_dir / 'results.csv') # save results.png
|
441 |
+
files = ['results.png', 'confusion_matrix.png', *(f'{x}_curve.png' for x in ('F1', 'PR', 'P', 'R'))]
|
442 |
+
files = [(save_dir / f) for f in files if (save_dir / f).exists()] # filter
|
443 |
+
LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}")
|
444 |
+
logger.log_images(files, "Results", epoch + 1)
|
445 |
+
logger.log_images(sorted(save_dir.glob('val*.jpg')), "Validation", epoch + 1)
|
446 |
+
torch.cuda.empty_cache()
|
447 |
+
return results
|
448 |
+
|
449 |
+
|
450 |
+
def parse_opt(known=False):
|
451 |
+
parser = argparse.ArgumentParser()
|
452 |
+
parser.add_argument('--weights', type=str, default=ROOT / 'yolo-seg.pt', help='initial weights path')
|
453 |
+
parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
|
454 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128-seg.yaml', help='dataset.yaml path')
|
455 |
+
parser.add_argument('--hyp', type=str, default=ROOT / 'data/hyps/hyp.scratch-low.yaml', help='hyperparameters path')
|
456 |
+
parser.add_argument('--epochs', type=int, default=100, help='total training epochs')
|
457 |
+
parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs, -1 for autobatch')
|
458 |
+
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='train, val image size (pixels)')
|
459 |
+
parser.add_argument('--rect', action='store_true', help='rectangular training')
|
460 |
+
parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')
|
461 |
+
parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
|
462 |
+
parser.add_argument('--noval', action='store_true', help='only validate final epoch')
|
463 |
+
parser.add_argument('--noautoanchor', action='store_true', help='disable AutoAnchor')
|
464 |
+
parser.add_argument('--noplots', action='store_true', help='save no plot files')
|
465 |
+
parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations')
|
466 |
+
parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
|
467 |
+
parser.add_argument('--cache', type=str, nargs='?', const='ram', help='image --cache ram/disk')
|
468 |
+
parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
|
469 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
470 |
+
parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
|
471 |
+
parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
|
472 |
+
parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'AdamW', 'LION'], default='SGD', help='optimizer')
|
473 |
+
parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
|
474 |
+
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
|
475 |
+
parser.add_argument('--project', default=ROOT / 'runs/train-seg', help='save to project/name')
|
476 |
+
parser.add_argument('--name', default='exp', help='save to project/name')
|
477 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
478 |
+
parser.add_argument('--quad', action='store_true', help='quad dataloader')
|
479 |
+
parser.add_argument('--cos-lr', action='store_true', help='cosine LR scheduler')
|
480 |
+
parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
|
481 |
+
parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)')
|
482 |
+
parser.add_argument('--freeze', nargs='+', type=int, default=[0], help='Freeze layers: backbone=10, first3=0 1 2')
|
483 |
+
parser.add_argument('--save-period', type=int, default=-1, help='Save checkpoint every x epochs (disabled if < 1)')
|
484 |
+
parser.add_argument('--seed', type=int, default=0, help='Global training seed')
|
485 |
+
parser.add_argument('--local_rank', type=int, default=-1, help='Automatic DDP Multi-GPU argument, do not modify')
|
486 |
+
parser.add_argument('--close-mosaic', type=int, default=0, help='Experimental')
|
487 |
+
|
488 |
+
# Instance Segmentation Args
|
489 |
+
parser.add_argument('--mask-ratio', type=int, default=4, help='Downsample the truth masks to saving memory')
|
490 |
+
parser.add_argument('--no-overlap', action='store_true', help='Overlap masks train faster at slightly less mAP')
|
491 |
+
|
492 |
+
return parser.parse_known_args()[0] if known else parser.parse_args()
|
493 |
+
|
494 |
+
|
495 |
+
def main(opt, callbacks=Callbacks()):
|
496 |
+
# Checks
|
497 |
+
if RANK in {-1, 0}:
|
498 |
+
print_args(vars(opt))
|
499 |
+
#check_git_status()
|
500 |
+
#check_requirements()
|
501 |
+
|
502 |
+
# Resume
|
503 |
+
if opt.resume and not opt.evolve: # resume from specified or most recent last.pt
|
504 |
+
last = Path(check_file(opt.resume) if isinstance(opt.resume, str) else get_latest_run())
|
505 |
+
opt_yaml = last.parent.parent / 'opt.yaml' # train options yaml
|
506 |
+
opt_data = opt.data # original dataset
|
507 |
+
if opt_yaml.is_file():
|
508 |
+
with open(opt_yaml, errors='ignore') as f:
|
509 |
+
d = yaml.safe_load(f)
|
510 |
+
else:
|
511 |
+
d = torch.load(last, map_location='cpu')['opt']
|
512 |
+
opt = argparse.Namespace(**d) # replace
|
513 |
+
opt.cfg, opt.weights, opt.resume = '', str(last), True # reinstate
|
514 |
+
if is_url(opt_data):
|
515 |
+
opt.data = check_file(opt_data) # avoid HUB resume auth timeout
|
516 |
+
else:
|
517 |
+
opt.data, opt.cfg, opt.hyp, opt.weights, opt.project = \
|
518 |
+
check_file(opt.data), check_yaml(opt.cfg), check_yaml(opt.hyp), str(opt.weights), str(opt.project) # checks
|
519 |
+
assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified'
|
520 |
+
if opt.evolve:
|
521 |
+
if opt.project == str(ROOT / 'runs/train'): # if default project name, rename to runs/evolve
|
522 |
+
opt.project = str(ROOT / 'runs/evolve')
|
523 |
+
opt.exist_ok, opt.resume = opt.resume, False # pass resume to exist_ok and disable resume
|
524 |
+
if opt.name == 'cfg':
|
525 |
+
opt.name = Path(opt.cfg).stem # use model.yaml as name
|
526 |
+
opt.save_dir = str(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok))
|
527 |
+
|
528 |
+
# DDP mode
|
529 |
+
device = select_device(opt.device, batch_size=opt.batch_size)
|
530 |
+
if LOCAL_RANK != -1:
|
531 |
+
msg = 'is not compatible with YOLO Multi-GPU DDP training'
|
532 |
+
assert not opt.image_weights, f'--image-weights {msg}'
|
533 |
+
assert not opt.evolve, f'--evolve {msg}'
|
534 |
+
assert opt.batch_size != -1, f'AutoBatch with --batch-size -1 {msg}, please pass a valid --batch-size'
|
535 |
+
assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'
|
536 |
+
assert torch.cuda.device_count() > LOCAL_RANK, 'insufficient CUDA devices for DDP command'
|
537 |
+
torch.cuda.set_device(LOCAL_RANK)
|
538 |
+
device = torch.device('cuda', LOCAL_RANK)
|
539 |
+
dist.init_process_group(backend="nccl" if dist.is_nccl_available() else "gloo")
|
540 |
+
|
541 |
+
# Train
|
542 |
+
if not opt.evolve:
|
543 |
+
train(opt.hyp, opt, device, callbacks)
|
544 |
+
|
545 |
+
# Evolve hyperparameters (optional)
|
546 |
+
else:
|
547 |
+
# Hyperparameter evolution metadata (mutation scale 0-1, lower_limit, upper_limit)
|
548 |
+
meta = {
|
549 |
+
'lr0': (1, 1e-5, 1e-1), # initial learning rate (SGD=1E-2, Adam=1E-3)
|
550 |
+
'lrf': (1, 0.01, 1.0), # final OneCycleLR learning rate (lr0 * lrf)
|
551 |
+
'momentum': (0.3, 0.6, 0.98), # SGD momentum/Adam beta1
|
552 |
+
'weight_decay': (1, 0.0, 0.001), # optimizer weight decay
|
553 |
+
'warmup_epochs': (1, 0.0, 5.0), # warmup epochs (fractions ok)
|
554 |
+
'warmup_momentum': (1, 0.0, 0.95), # warmup initial momentum
|
555 |
+
'warmup_bias_lr': (1, 0.0, 0.2), # warmup initial bias lr
|
556 |
+
'box': (1, 0.02, 0.2), # box loss gain
|
557 |
+
'cls': (1, 0.2, 4.0), # cls loss gain
|
558 |
+
'cls_pw': (1, 0.5, 2.0), # cls BCELoss positive_weight
|
559 |
+
'obj': (1, 0.2, 4.0), # obj loss gain (scale with pixels)
|
560 |
+
'obj_pw': (1, 0.5, 2.0), # obj BCELoss positive_weight
|
561 |
+
'iou_t': (0, 0.1, 0.7), # IoU training threshold
|
562 |
+
'anchor_t': (1, 2.0, 8.0), # anchor-multiple threshold
|
563 |
+
'anchors': (2, 2.0, 10.0), # anchors per output grid (0 to ignore)
|
564 |
+
'fl_gamma': (0, 0.0, 2.0), # focal loss gamma (efficientDet default gamma=1.5)
|
565 |
+
'hsv_h': (1, 0.0, 0.1), # image HSV-Hue augmentation (fraction)
|
566 |
+
'hsv_s': (1, 0.0, 0.9), # image HSV-Saturation augmentation (fraction)
|
567 |
+
'hsv_v': (1, 0.0, 0.9), # image HSV-Value augmentation (fraction)
|
568 |
+
'degrees': (1, 0.0, 45.0), # image rotation (+/- deg)
|
569 |
+
'translate': (1, 0.0, 0.9), # image translation (+/- fraction)
|
570 |
+
'scale': (1, 0.0, 0.9), # image scale (+/- gain)
|
571 |
+
'shear': (1, 0.0, 10.0), # image shear (+/- deg)
|
572 |
+
'perspective': (0, 0.0, 0.001), # image perspective (+/- fraction), range 0-0.001
|
573 |
+
'flipud': (1, 0.0, 1.0), # image flip up-down (probability)
|
574 |
+
'fliplr': (0, 0.0, 1.0), # image flip left-right (probability)
|
575 |
+
'mosaic': (1, 0.0, 1.0), # image mixup (probability)
|
576 |
+
'mixup': (1, 0.0, 1.0), # image mixup (probability)
|
577 |
+
'copy_paste': (1, 0.0, 1.0)} # segment copy-paste (probability)
|
578 |
+
|
579 |
+
with open(opt.hyp, errors='ignore') as f:
|
580 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
581 |
+
if 'anchors' not in hyp: # anchors commented in hyp.yaml
|
582 |
+
hyp['anchors'] = 3
|
583 |
+
if opt.noautoanchor:
|
584 |
+
del hyp['anchors'], meta['anchors']
|
585 |
+
opt.noval, opt.nosave, save_dir = True, True, Path(opt.save_dir) # only val/save final epoch
|
586 |
+
# ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
|
587 |
+
evolve_yaml, evolve_csv = save_dir / 'hyp_evolve.yaml', save_dir / 'evolve.csv'
|
588 |
+
if opt.bucket:
|
589 |
+
os.system(f'gsutil cp gs://{opt.bucket}/evolve.csv {evolve_csv}') # download evolve.csv if exists
|
590 |
+
|
591 |
+
for _ in range(opt.evolve): # generations to evolve
|
592 |
+
if evolve_csv.exists(): # if evolve.csv exists: select best hyps and mutate
|
593 |
+
# Select parent(s)
|
594 |
+
parent = 'single' # parent selection method: 'single' or 'weighted'
|
595 |
+
x = np.loadtxt(evolve_csv, ndmin=2, delimiter=',', skiprows=1)
|
596 |
+
n = min(5, len(x)) # number of previous results to consider
|
597 |
+
x = x[np.argsort(-fitness(x))][:n] # top n mutations
|
598 |
+
w = fitness(x) - fitness(x).min() + 1E-6 # weights (sum > 0)
|
599 |
+
if parent == 'single' or len(x) == 1:
|
600 |
+
# x = x[random.randint(0, n - 1)] # random selection
|
601 |
+
x = x[random.choices(range(n), weights=w)[0]] # weighted selection
|
602 |
+
elif parent == 'weighted':
|
603 |
+
x = (x * w.reshape(n, 1)).sum(0) / w.sum() # weighted combination
|
604 |
+
|
605 |
+
# Mutate
|
606 |
+
mp, s = 0.8, 0.2 # mutation probability, sigma
|
607 |
+
npr = np.random
|
608 |
+
npr.seed(int(time.time()))
|
609 |
+
g = np.array([meta[k][0] for k in hyp.keys()]) # gains 0-1
|
610 |
+
ng = len(meta)
|
611 |
+
v = np.ones(ng)
|
612 |
+
while all(v == 1): # mutate until a change occurs (prevent duplicates)
|
613 |
+
v = (g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1).clip(0.3, 3.0)
|
614 |
+
for i, k in enumerate(hyp.keys()): # plt.hist(v.ravel(), 300)
|
615 |
+
hyp[k] = float(x[i + 7] * v[i]) # mutate
|
616 |
+
|
617 |
+
# Constrain to limits
|
618 |
+
for k, v in meta.items():
|
619 |
+
hyp[k] = max(hyp[k], v[1]) # lower limit
|
620 |
+
hyp[k] = min(hyp[k], v[2]) # upper limit
|
621 |
+
hyp[k] = round(hyp[k], 5) # significant digits
|
622 |
+
|
623 |
+
# Train mutation
|
624 |
+
results = train(hyp.copy(), opt, device, callbacks)
|
625 |
+
callbacks = Callbacks()
|
626 |
+
# Write mutation results
|
627 |
+
print_mutation(KEYS, results, hyp.copy(), save_dir, opt.bucket)
|
628 |
+
|
629 |
+
# Plot results
|
630 |
+
plot_evolve(evolve_csv)
|
631 |
+
LOGGER.info(f'Hyperparameter evolution finished {opt.evolve} generations\n'
|
632 |
+
f"Results saved to {colorstr('bold', save_dir)}\n"
|
633 |
+
f'Usage example: $ python train.py --hyp {evolve_yaml}')
|
634 |
+
|
635 |
+
|
636 |
+
def run(**kwargs):
|
637 |
+
# Usage: import train; train.run(data='coco128.yaml', imgsz=320, weights='yolo.pt')
|
638 |
+
opt = parse_opt(True)
|
639 |
+
for k, v in kwargs.items():
|
640 |
+
setattr(opt, k, v)
|
641 |
+
main(opt)
|
642 |
+
return opt
|
643 |
+
|
644 |
+
|
645 |
+
if __name__ == "__main__":
|
646 |
+
opt = parse_opt()
|
647 |
+
main(opt)
|
segment/val.py
ADDED
@@ -0,0 +1,457 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
from multiprocessing.pool import ThreadPool
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import torch
|
10 |
+
from tqdm import tqdm
|
11 |
+
|
12 |
+
FILE = Path(__file__).resolve()
|
13 |
+
ROOT = FILE.parents[1] # YOLO root directory
|
14 |
+
if str(ROOT) not in sys.path:
|
15 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
16 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
17 |
+
|
18 |
+
import torch.nn.functional as F
|
19 |
+
|
20 |
+
from models.common import DetectMultiBackend
|
21 |
+
from models.yolo import SegmentationModel
|
22 |
+
from utils.callbacks import Callbacks
|
23 |
+
from utils.general import (LOGGER, NUM_THREADS, TQDM_BAR_FORMAT, Profile, check_dataset, check_img_size,
|
24 |
+
check_requirements, check_yaml, coco80_to_coco91_class, colorstr, increment_path,
|
25 |
+
non_max_suppression, print_args, scale_boxes, xywh2xyxy, xyxy2xywh)
|
26 |
+
from utils.metrics import ConfusionMatrix, box_iou
|
27 |
+
from utils.plots import output_to_target, plot_val_study
|
28 |
+
from utils.segment.dataloaders import create_dataloader
|
29 |
+
from utils.segment.general import mask_iou, process_mask, process_mask_upsample, scale_image
|
30 |
+
from utils.segment.metrics import Metrics, ap_per_class_box_and_mask
|
31 |
+
from utils.segment.plots import plot_images_and_masks
|
32 |
+
from utils.torch_utils import de_parallel, select_device, smart_inference_mode
|
33 |
+
|
34 |
+
|
35 |
+
def save_one_txt(predn, save_conf, shape, file):
|
36 |
+
# Save one txt result
|
37 |
+
gn = torch.tensor(shape)[[1, 0, 1, 0]] # normalization gain whwh
|
38 |
+
for *xyxy, conf, cls in predn.tolist():
|
39 |
+
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
|
40 |
+
line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format
|
41 |
+
with open(file, 'a') as f:
|
42 |
+
f.write(('%g ' * len(line)).rstrip() % line + '\n')
|
43 |
+
|
44 |
+
|
45 |
+
def save_one_json(predn, jdict, path, class_map, pred_masks):
|
46 |
+
# Save one JSON result {"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}
|
47 |
+
from pycocotools.mask import encode
|
48 |
+
|
49 |
+
def single_encode(x):
|
50 |
+
rle = encode(np.asarray(x[:, :, None], order="F", dtype="uint8"))[0]
|
51 |
+
rle["counts"] = rle["counts"].decode("utf-8")
|
52 |
+
return rle
|
53 |
+
|
54 |
+
image_id = int(path.stem) if path.stem.isnumeric() else path.stem
|
55 |
+
box = xyxy2xywh(predn[:, :4]) # xywh
|
56 |
+
box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
|
57 |
+
pred_masks = np.transpose(pred_masks, (2, 0, 1))
|
58 |
+
with ThreadPool(NUM_THREADS) as pool:
|
59 |
+
rles = pool.map(single_encode, pred_masks)
|
60 |
+
for i, (p, b) in enumerate(zip(predn.tolist(), box.tolist())):
|
61 |
+
jdict.append({
|
62 |
+
'image_id': image_id,
|
63 |
+
'category_id': class_map[int(p[5])],
|
64 |
+
'bbox': [round(x, 3) for x in b],
|
65 |
+
'score': round(p[4], 5),
|
66 |
+
'segmentation': rles[i]})
|
67 |
+
|
68 |
+
|
69 |
+
def process_batch(detections, labels, iouv, pred_masks=None, gt_masks=None, overlap=False, masks=False):
|
70 |
+
"""
|
71 |
+
Return correct prediction matrix
|
72 |
+
Arguments:
|
73 |
+
detections (array[N, 6]), x1, y1, x2, y2, conf, class
|
74 |
+
labels (array[M, 5]), class, x1, y1, x2, y2
|
75 |
+
Returns:
|
76 |
+
correct (array[N, 10]), for 10 IoU levels
|
77 |
+
"""
|
78 |
+
if masks:
|
79 |
+
if overlap:
|
80 |
+
nl = len(labels)
|
81 |
+
index = torch.arange(nl, device=gt_masks.device).view(nl, 1, 1) + 1
|
82 |
+
gt_masks = gt_masks.repeat(nl, 1, 1) # shape(1,640,640) -> (n,640,640)
|
83 |
+
gt_masks = torch.where(gt_masks == index, 1.0, 0.0)
|
84 |
+
if gt_masks.shape[1:] != pred_masks.shape[1:]:
|
85 |
+
gt_masks = F.interpolate(gt_masks[None], pred_masks.shape[1:], mode="bilinear", align_corners=False)[0]
|
86 |
+
gt_masks = gt_masks.gt_(0.5)
|
87 |
+
iou = mask_iou(gt_masks.view(gt_masks.shape[0], -1), pred_masks.view(pred_masks.shape[0], -1))
|
88 |
+
else: # boxes
|
89 |
+
iou = box_iou(labels[:, 1:], detections[:, :4])
|
90 |
+
|
91 |
+
correct = np.zeros((detections.shape[0], iouv.shape[0])).astype(bool)
|
92 |
+
correct_class = labels[:, 0:1] == detections[:, 5]
|
93 |
+
for i in range(len(iouv)):
|
94 |
+
x = torch.where((iou >= iouv[i]) & correct_class) # IoU > threshold and classes match
|
95 |
+
if x[0].shape[0]:
|
96 |
+
matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy() # [label, detect, iou]
|
97 |
+
if x[0].shape[0] > 1:
|
98 |
+
matches = matches[matches[:, 2].argsort()[::-1]]
|
99 |
+
matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
|
100 |
+
# matches = matches[matches[:, 2].argsort()[::-1]]
|
101 |
+
matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
|
102 |
+
correct[matches[:, 1].astype(int), i] = True
|
103 |
+
return torch.tensor(correct, dtype=torch.bool, device=iouv.device)
|
104 |
+
|
105 |
+
|
106 |
+
@smart_inference_mode()
|
107 |
+
def run(
|
108 |
+
data,
|
109 |
+
weights=None, # model.pt path(s)
|
110 |
+
batch_size=32, # batch size
|
111 |
+
imgsz=640, # inference size (pixels)
|
112 |
+
conf_thres=0.001, # confidence threshold
|
113 |
+
iou_thres=0.6, # NMS IoU threshold
|
114 |
+
max_det=300, # maximum detections per image
|
115 |
+
task='val', # train, val, test, speed or study
|
116 |
+
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
|
117 |
+
workers=8, # max dataloader workers (per RANK in DDP mode)
|
118 |
+
single_cls=False, # treat as single-class dataset
|
119 |
+
augment=False, # augmented inference
|
120 |
+
verbose=False, # verbose output
|
121 |
+
save_txt=False, # save results to *.txt
|
122 |
+
save_hybrid=False, # save label+prediction hybrid results to *.txt
|
123 |
+
save_conf=False, # save confidences in --save-txt labels
|
124 |
+
save_json=False, # save a COCO-JSON results file
|
125 |
+
project=ROOT / 'runs/val-seg', # save to project/name
|
126 |
+
name='exp', # save to project/name
|
127 |
+
exist_ok=False, # existing project/name ok, do not increment
|
128 |
+
half=True, # use FP16 half-precision inference
|
129 |
+
dnn=False, # use OpenCV DNN for ONNX inference
|
130 |
+
model=None,
|
131 |
+
dataloader=None,
|
132 |
+
save_dir=Path(''),
|
133 |
+
plots=True,
|
134 |
+
overlap=False,
|
135 |
+
mask_downsample_ratio=1,
|
136 |
+
compute_loss=None,
|
137 |
+
callbacks=Callbacks(),
|
138 |
+
):
|
139 |
+
if save_json:
|
140 |
+
check_requirements(['pycocotools'])
|
141 |
+
process = process_mask_upsample # more accurate
|
142 |
+
else:
|
143 |
+
process = process_mask # faster
|
144 |
+
|
145 |
+
# Initialize/load model and set device
|
146 |
+
training = model is not None
|
147 |
+
if training: # called by train.py
|
148 |
+
device, pt, jit, engine = next(model.parameters()).device, True, False, False # get model device, PyTorch model
|
149 |
+
half &= device.type != 'cpu' # half precision only supported on CUDA
|
150 |
+
model.half() if half else model.float()
|
151 |
+
nm = de_parallel(model).model[-1].nm # number of masks
|
152 |
+
else: # called directly
|
153 |
+
device = select_device(device, batch_size=batch_size)
|
154 |
+
|
155 |
+
# Directories
|
156 |
+
save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
|
157 |
+
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
|
158 |
+
|
159 |
+
# Load model
|
160 |
+
model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
|
161 |
+
stride, pt, jit, engine = model.stride, model.pt, model.jit, model.engine
|
162 |
+
imgsz = check_img_size(imgsz, s=stride) # check image size
|
163 |
+
half = model.fp16 # FP16 supported on limited backends with CUDA
|
164 |
+
nm = de_parallel(model).model.model[-1].nm if isinstance(model, SegmentationModel) else 32 # number of masks
|
165 |
+
if engine:
|
166 |
+
batch_size = model.batch_size
|
167 |
+
else:
|
168 |
+
device = model.device
|
169 |
+
if not (pt or jit):
|
170 |
+
batch_size = 1 # export.py models default to batch-size 1
|
171 |
+
LOGGER.info(f'Forcing --batch-size 1 square inference (1,3,{imgsz},{imgsz}) for non-PyTorch models')
|
172 |
+
|
173 |
+
# Data
|
174 |
+
data = check_dataset(data) # check
|
175 |
+
|
176 |
+
# Configure
|
177 |
+
model.eval()
|
178 |
+
cuda = device.type != 'cpu'
|
179 |
+
#is_coco = isinstance(data.get('val'), str) and data['val'].endswith(f'coco{os.sep}val2017.txt') # COCO dataset
|
180 |
+
is_coco = isinstance(data.get('val'), str) and data['val'].endswith(f'val2017.txt') # COCO dataset
|
181 |
+
nc = 1 if single_cls else int(data['nc']) # number of classes
|
182 |
+
iouv = torch.linspace(0.5, 0.95, 10, device=device) # iou vector for [email protected]:0.95
|
183 |
+
niou = iouv.numel()
|
184 |
+
|
185 |
+
# Dataloader
|
186 |
+
if not training:
|
187 |
+
if pt and not single_cls: # check --weights are trained on --data
|
188 |
+
ncm = model.model.nc
|
189 |
+
assert ncm == nc, f'{weights} ({ncm} classes) trained on different --data than what you passed ({nc} ' \
|
190 |
+
f'classes). Pass correct combination of --weights and --data that are trained together.'
|
191 |
+
model.warmup(imgsz=(1 if pt else batch_size, 3, imgsz, imgsz)) # warmup
|
192 |
+
pad, rect = (0.0, False) if task == 'speed' else (0.5, pt) # square inference for benchmarks
|
193 |
+
task = task if task in ('train', 'val', 'test') else 'val' # path to train/val/test images
|
194 |
+
dataloader = create_dataloader(data[task],
|
195 |
+
imgsz,
|
196 |
+
batch_size,
|
197 |
+
stride,
|
198 |
+
single_cls,
|
199 |
+
pad=pad,
|
200 |
+
rect=rect,
|
201 |
+
workers=workers,
|
202 |
+
prefix=colorstr(f'{task}: '),
|
203 |
+
overlap_mask=overlap,
|
204 |
+
mask_downsample_ratio=mask_downsample_ratio)[0]
|
205 |
+
|
206 |
+
seen = 0
|
207 |
+
confusion_matrix = ConfusionMatrix(nc=nc)
|
208 |
+
names = model.names if hasattr(model, 'names') else model.module.names # get class names
|
209 |
+
if isinstance(names, (list, tuple)): # old format
|
210 |
+
names = dict(enumerate(names))
|
211 |
+
class_map = coco80_to_coco91_class() if is_coco else list(range(1000))
|
212 |
+
s = ('%22s' + '%11s' * 10) % ('Class', 'Images', 'Instances', 'Box(P', "R", "mAP50", "mAP50-95)", "Mask(P", "R",
|
213 |
+
"mAP50", "mAP50-95)")
|
214 |
+
dt = Profile(), Profile(), Profile()
|
215 |
+
metrics = Metrics()
|
216 |
+
loss = torch.zeros(4, device=device)
|
217 |
+
jdict, stats = [], []
|
218 |
+
# callbacks.run('on_val_start')
|
219 |
+
pbar = tqdm(dataloader, desc=s, bar_format=TQDM_BAR_FORMAT) # progress bar
|
220 |
+
for batch_i, (im, targets, paths, shapes, masks) in enumerate(pbar):
|
221 |
+
# callbacks.run('on_val_batch_start')
|
222 |
+
with dt[0]:
|
223 |
+
if cuda:
|
224 |
+
im = im.to(device, non_blocking=True)
|
225 |
+
targets = targets.to(device)
|
226 |
+
masks = masks.to(device)
|
227 |
+
masks = masks.float()
|
228 |
+
im = im.half() if half else im.float() # uint8 to fp16/32
|
229 |
+
im /= 255 # 0 - 255 to 0.0 - 1.0
|
230 |
+
nb, _, height, width = im.shape # batch size, channels, height, width
|
231 |
+
|
232 |
+
# Inference
|
233 |
+
with dt[1]:
|
234 |
+
preds, train_out = model(im)# if compute_loss else (*model(im, augment=augment)[:2], None)
|
235 |
+
#train_out, preds, protos = p if len(p) == 3 else p[1]
|
236 |
+
#preds = p
|
237 |
+
#train_out = p[1][0] if len(p[1]) == 3 else p[0]
|
238 |
+
protos = train_out[-1]
|
239 |
+
#print(preds.shape)
|
240 |
+
#print(train_out[0].shape)
|
241 |
+
#print(train_out[1].shape)
|
242 |
+
#print(train_out[2].shape)
|
243 |
+
|
244 |
+
# Loss
|
245 |
+
if compute_loss:
|
246 |
+
loss += compute_loss(train_out, targets, masks)[1] # box, obj, cls
|
247 |
+
|
248 |
+
# NMS
|
249 |
+
targets[:, 2:] *= torch.tensor((width, height, width, height), device=device) # to pixels
|
250 |
+
lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_hybrid else [] # for autolabelling
|
251 |
+
with dt[2]:
|
252 |
+
preds = non_max_suppression(preds,
|
253 |
+
conf_thres,
|
254 |
+
iou_thres,
|
255 |
+
labels=lb,
|
256 |
+
multi_label=True,
|
257 |
+
agnostic=single_cls,
|
258 |
+
max_det=max_det,
|
259 |
+
nm=nm)
|
260 |
+
|
261 |
+
# Metrics
|
262 |
+
plot_masks = [] # masks for plotting
|
263 |
+
for si, (pred, proto) in enumerate(zip(preds, protos)):
|
264 |
+
labels = targets[targets[:, 0] == si, 1:]
|
265 |
+
nl, npr = labels.shape[0], pred.shape[0] # number of labels, predictions
|
266 |
+
path, shape = Path(paths[si]), shapes[si][0]
|
267 |
+
correct_masks = torch.zeros(npr, niou, dtype=torch.bool, device=device) # init
|
268 |
+
correct_bboxes = torch.zeros(npr, niou, dtype=torch.bool, device=device) # init
|
269 |
+
seen += 1
|
270 |
+
|
271 |
+
if npr == 0:
|
272 |
+
if nl:
|
273 |
+
stats.append((correct_masks, correct_bboxes, *torch.zeros((2, 0), device=device), labels[:, 0]))
|
274 |
+
if plots:
|
275 |
+
confusion_matrix.process_batch(detections=None, labels=labels[:, 0])
|
276 |
+
continue
|
277 |
+
|
278 |
+
# Masks
|
279 |
+
midx = [si] if overlap else targets[:, 0] == si
|
280 |
+
gt_masks = masks[midx]
|
281 |
+
pred_masks = process(proto, pred[:, 6:], pred[:, :4], shape=im[si].shape[1:])
|
282 |
+
|
283 |
+
# Predictions
|
284 |
+
if single_cls:
|
285 |
+
pred[:, 5] = 0
|
286 |
+
predn = pred.clone()
|
287 |
+
scale_boxes(im[si].shape[1:], predn[:, :4], shape, shapes[si][1]) # native-space pred
|
288 |
+
|
289 |
+
# Evaluate
|
290 |
+
if nl:
|
291 |
+
tbox = xywh2xyxy(labels[:, 1:5]) # target boxes
|
292 |
+
scale_boxes(im[si].shape[1:], tbox, shape, shapes[si][1]) # native-space labels
|
293 |
+
labelsn = torch.cat((labels[:, 0:1], tbox), 1) # native-space labels
|
294 |
+
correct_bboxes = process_batch(predn, labelsn, iouv)
|
295 |
+
correct_masks = process_batch(predn, labelsn, iouv, pred_masks, gt_masks, overlap=overlap, masks=True)
|
296 |
+
if plots:
|
297 |
+
confusion_matrix.process_batch(predn, labelsn)
|
298 |
+
stats.append((correct_masks, correct_bboxes, pred[:, 4], pred[:, 5], labels[:, 0])) # (conf, pcls, tcls)
|
299 |
+
|
300 |
+
pred_masks = torch.as_tensor(pred_masks, dtype=torch.uint8)
|
301 |
+
if plots and batch_i < 3:
|
302 |
+
plot_masks.append(pred_masks[:15].cpu()) # filter top 15 to plot
|
303 |
+
|
304 |
+
# Save/log
|
305 |
+
if save_txt:
|
306 |
+
save_one_txt(predn, save_conf, shape, file=save_dir / 'labels' / f'{path.stem}.txt')
|
307 |
+
if save_json:
|
308 |
+
pred_masks = scale_image(im[si].shape[1:],
|
309 |
+
pred_masks.permute(1, 2, 0).contiguous().cpu().numpy(), shape, shapes[si][1])
|
310 |
+
save_one_json(predn, jdict, path, class_map, pred_masks) # append to COCO-JSON dictionary
|
311 |
+
# callbacks.run('on_val_image_end', pred, predn, path, names, im[si])
|
312 |
+
|
313 |
+
# Plot images
|
314 |
+
if plots and batch_i < 3:
|
315 |
+
if len(plot_masks):
|
316 |
+
plot_masks = torch.cat(plot_masks, dim=0)
|
317 |
+
plot_images_and_masks(im, targets, masks, paths, save_dir / f'val_batch{batch_i}_labels.jpg', names)
|
318 |
+
plot_images_and_masks(im, output_to_target(preds, max_det=15), plot_masks, paths,
|
319 |
+
save_dir / f'val_batch{batch_i}_pred.jpg', names) # pred
|
320 |
+
|
321 |
+
# callbacks.run('on_val_batch_end')
|
322 |
+
|
323 |
+
# Compute metrics
|
324 |
+
stats = [torch.cat(x, 0).cpu().numpy() for x in zip(*stats)] # to numpy
|
325 |
+
if len(stats) and stats[0].any():
|
326 |
+
results = ap_per_class_box_and_mask(*stats, plot=plots, save_dir=save_dir, names=names)
|
327 |
+
metrics.update(results)
|
328 |
+
nt = np.bincount(stats[4].astype(int), minlength=nc) # number of targets per class
|
329 |
+
|
330 |
+
# Print results
|
331 |
+
pf = '%22s' + '%11i' * 2 + '%11.3g' * 8 # print format
|
332 |
+
LOGGER.info(pf % ("all", seen, nt.sum(), *metrics.mean_results()))
|
333 |
+
if nt.sum() == 0:
|
334 |
+
LOGGER.warning(f'WARNING ⚠️ no labels found in {task} set, can not compute metrics without labels')
|
335 |
+
|
336 |
+
# Print results per class
|
337 |
+
if (verbose or (nc < 50 and not training)) and nc > 1 and len(stats):
|
338 |
+
for i, c in enumerate(metrics.ap_class_index):
|
339 |
+
LOGGER.info(pf % (names[c], seen, nt[c], *metrics.class_result(i)))
|
340 |
+
|
341 |
+
# Print speeds
|
342 |
+
t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image
|
343 |
+
if not training:
|
344 |
+
shape = (batch_size, 3, imgsz, imgsz)
|
345 |
+
LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {shape}' % t)
|
346 |
+
|
347 |
+
# Plots
|
348 |
+
if plots:
|
349 |
+
confusion_matrix.plot(save_dir=save_dir, names=list(names.values()))
|
350 |
+
# callbacks.run('on_val_end')
|
351 |
+
|
352 |
+
mp_bbox, mr_bbox, map50_bbox, map_bbox, mp_mask, mr_mask, map50_mask, map_mask = metrics.mean_results()
|
353 |
+
|
354 |
+
# Save JSON
|
355 |
+
if save_json and len(jdict):
|
356 |
+
w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights
|
357 |
+
anno_json = str(Path(data.get('path', '../coco')) / 'annotations/instances_val2017.json') # annotations json
|
358 |
+
pred_json = str(save_dir / f"{w}_predictions.json") # predictions json
|
359 |
+
LOGGER.info(f'\nEvaluating pycocotools mAP... saving {pred_json}...')
|
360 |
+
with open(pred_json, 'w') as f:
|
361 |
+
json.dump(jdict, f)
|
362 |
+
|
363 |
+
try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
|
364 |
+
from pycocotools.coco import COCO
|
365 |
+
from pycocotools.cocoeval import COCOeval
|
366 |
+
|
367 |
+
anno = COCO(anno_json) # init annotations api
|
368 |
+
pred = anno.loadRes(pred_json) # init predictions api
|
369 |
+
results = []
|
370 |
+
for eval in COCOeval(anno, pred, 'bbox'), COCOeval(anno, pred, 'segm'):
|
371 |
+
if is_coco:
|
372 |
+
eval.params.imgIds = [int(Path(x).stem) for x in dataloader.dataset.im_files] # img ID to evaluate
|
373 |
+
eval.evaluate()
|
374 |
+
eval.accumulate()
|
375 |
+
eval.summarize()
|
376 |
+
results.extend(eval.stats[:2]) # update results ([email protected]:0.95, [email protected])
|
377 |
+
map_bbox, map50_bbox, map_mask, map50_mask = results
|
378 |
+
except Exception as e:
|
379 |
+
LOGGER.info(f'pycocotools unable to run: {e}')
|
380 |
+
|
381 |
+
# Return results
|
382 |
+
model.float() # for training
|
383 |
+
if not training:
|
384 |
+
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
|
385 |
+
LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
|
386 |
+
final_metric = mp_bbox, mr_bbox, map50_bbox, map_bbox, mp_mask, mr_mask, map50_mask, map_mask
|
387 |
+
return (*final_metric, *(loss.cpu() / len(dataloader)).tolist()), metrics.get_maps(nc), t
|
388 |
+
|
389 |
+
|
390 |
+
def parse_opt():
|
391 |
+
parser = argparse.ArgumentParser()
|
392 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128-seg.yaml', help='dataset.yaml path')
|
393 |
+
parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolo-seg.pt', help='model path(s)')
|
394 |
+
parser.add_argument('--batch-size', type=int, default=32, help='batch size')
|
395 |
+
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='inference size (pixels)')
|
396 |
+
parser.add_argument('--conf-thres', type=float, default=0.001, help='confidence threshold')
|
397 |
+
parser.add_argument('--iou-thres', type=float, default=0.6, help='NMS IoU threshold')
|
398 |
+
parser.add_argument('--max-det', type=int, default=300, help='maximum detections per image')
|
399 |
+
parser.add_argument('--task', default='val', help='train, val, test, speed or study')
|
400 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
401 |
+
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
|
402 |
+
parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset')
|
403 |
+
parser.add_argument('--augment', action='store_true', help='augmented inference')
|
404 |
+
parser.add_argument('--verbose', action='store_true', help='report mAP by class')
|
405 |
+
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
|
406 |
+
parser.add_argument('--save-hybrid', action='store_true', help='save label+prediction hybrid results to *.txt')
|
407 |
+
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
|
408 |
+
parser.add_argument('--save-json', action='store_true', help='save a COCO-JSON results file')
|
409 |
+
parser.add_argument('--project', default=ROOT / 'runs/val-seg', help='save results to project/name')
|
410 |
+
parser.add_argument('--name', default='exp', help='save to project/name')
|
411 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
412 |
+
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
|
413 |
+
parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
|
414 |
+
opt = parser.parse_args()
|
415 |
+
opt.data = check_yaml(opt.data) # check YAML
|
416 |
+
# opt.save_json |= opt.data.endswith('coco.yaml')
|
417 |
+
opt.save_txt |= opt.save_hybrid
|
418 |
+
print_args(vars(opt))
|
419 |
+
return opt
|
420 |
+
|
421 |
+
|
422 |
+
def main(opt):
|
423 |
+
#check_requirements(requirements=ROOT / 'requirements.txt', exclude=('tensorboard', 'thop'))
|
424 |
+
|
425 |
+
if opt.task in ('train', 'val', 'test'): # run normally
|
426 |
+
if opt.conf_thres > 0.001: # https://github.com/ultralytics/yolov5/issues/1466
|
427 |
+
LOGGER.warning(f'WARNING ⚠️ confidence threshold {opt.conf_thres} > 0.001 produces invalid results')
|
428 |
+
if opt.save_hybrid:
|
429 |
+
LOGGER.warning('WARNING ⚠️ --save-hybrid returns high mAP from hybrid labels, not from predictions alone')
|
430 |
+
run(**vars(opt))
|
431 |
+
|
432 |
+
else:
|
433 |
+
weights = opt.weights if isinstance(opt.weights, list) else [opt.weights]
|
434 |
+
opt.half = torch.cuda.is_available() and opt.device != 'cpu' # FP16 for fastest results
|
435 |
+
if opt.task == 'speed': # speed benchmarks
|
436 |
+
# python val.py --task speed --data coco.yaml --batch 1 --weights yolo.pt...
|
437 |
+
opt.conf_thres, opt.iou_thres, opt.save_json = 0.25, 0.45, False
|
438 |
+
for opt.weights in weights:
|
439 |
+
run(**vars(opt), plots=False)
|
440 |
+
|
441 |
+
elif opt.task == 'study': # speed vs mAP benchmarks
|
442 |
+
# python val.py --task study --data coco.yaml --iou 0.7 --weights yolo.pt...
|
443 |
+
for opt.weights in weights:
|
444 |
+
f = f'study_{Path(opt.data).stem}_{Path(opt.weights).stem}.txt' # filename to save to
|
445 |
+
x, y = list(range(256, 1536 + 128, 128)), [] # x axis (image sizes), y axis
|
446 |
+
for opt.imgsz in x: # img-size
|
447 |
+
LOGGER.info(f'\nRunning {f} --imgsz {opt.imgsz}...')
|
448 |
+
r, _, t = run(**vars(opt), plots=False)
|
449 |
+
y.append(r + t) # results and times
|
450 |
+
np.savetxt(f, y, fmt='%10.4g') # save
|
451 |
+
os.system('zip -r study.zip study_*.txt')
|
452 |
+
plot_val_study(x=x) # plot
|
453 |
+
|
454 |
+
|
455 |
+
if __name__ == "__main__":
|
456 |
+
opt = parse_opt()
|
457 |
+
main(opt)
|
segment/val_dual.py
ADDED
@@ -0,0 +1,458 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
from multiprocessing.pool import ThreadPool
|
6 |
+
from pathlib import Path
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import torch
|
10 |
+
from tqdm import tqdm
|
11 |
+
|
12 |
+
FILE = Path(__file__).resolve()
|
13 |
+
ROOT = FILE.parents[1] # YOLO root directory
|
14 |
+
if str(ROOT) not in sys.path:
|
15 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
16 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
17 |
+
|
18 |
+
import torch.nn.functional as F
|
19 |
+
|
20 |
+
from models.common import DetectMultiBackend
|
21 |
+
from models.yolo import SegmentationModel
|
22 |
+
from utils.callbacks import Callbacks
|
23 |
+
from utils.general import (LOGGER, NUM_THREADS, TQDM_BAR_FORMAT, Profile, check_dataset, check_img_size,
|
24 |
+
check_requirements, check_yaml, coco80_to_coco91_class, colorstr, increment_path,
|
25 |
+
non_max_suppression, print_args, scale_boxes, xywh2xyxy, xyxy2xywh)
|
26 |
+
from utils.metrics import ConfusionMatrix, box_iou
|
27 |
+
from utils.plots import output_to_target, plot_val_study
|
28 |
+
from utils.segment.dataloaders import create_dataloader
|
29 |
+
from utils.segment.general import mask_iou, process_mask, process_mask_upsample, scale_image
|
30 |
+
from utils.segment.metrics import Metrics, ap_per_class_box_and_mask
|
31 |
+
from utils.segment.plots import plot_images_and_masks
|
32 |
+
from utils.torch_utils import de_parallel, select_device, smart_inference_mode
|
33 |
+
|
34 |
+
|
35 |
+
def save_one_txt(predn, save_conf, shape, file):
|
36 |
+
# Save one txt result
|
37 |
+
gn = torch.tensor(shape)[[1, 0, 1, 0]] # normalization gain whwh
|
38 |
+
for *xyxy, conf, cls in predn.tolist():
|
39 |
+
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
|
40 |
+
line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format
|
41 |
+
with open(file, 'a') as f:
|
42 |
+
f.write(('%g ' * len(line)).rstrip() % line + '\n')
|
43 |
+
|
44 |
+
|
45 |
+
def save_one_json(predn, jdict, path, class_map, pred_masks):
|
46 |
+
# Save one JSON result {"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}
|
47 |
+
from pycocotools.mask import encode
|
48 |
+
|
49 |
+
def single_encode(x):
|
50 |
+
rle = encode(np.asarray(x[:, :, None], order="F", dtype="uint8"))[0]
|
51 |
+
rle["counts"] = rle["counts"].decode("utf-8")
|
52 |
+
return rle
|
53 |
+
|
54 |
+
image_id = int(path.stem) if path.stem.isnumeric() else path.stem
|
55 |
+
box = xyxy2xywh(predn[:, :4]) # xywh
|
56 |
+
box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
|
57 |
+
pred_masks = np.transpose(pred_masks, (2, 0, 1))
|
58 |
+
with ThreadPool(NUM_THREADS) as pool:
|
59 |
+
rles = pool.map(single_encode, pred_masks)
|
60 |
+
for i, (p, b) in enumerate(zip(predn.tolist(), box.tolist())):
|
61 |
+
jdict.append({
|
62 |
+
'image_id': image_id,
|
63 |
+
'category_id': class_map[int(p[5])],
|
64 |
+
'bbox': [round(x, 3) for x in b],
|
65 |
+
'score': round(p[4], 5),
|
66 |
+
'segmentation': rles[i]})
|
67 |
+
|
68 |
+
|
69 |
+
def process_batch(detections, labels, iouv, pred_masks=None, gt_masks=None, overlap=False, masks=False):
|
70 |
+
"""
|
71 |
+
Return correct prediction matrix
|
72 |
+
Arguments:
|
73 |
+
detections (array[N, 6]), x1, y1, x2, y2, conf, class
|
74 |
+
labels (array[M, 5]), class, x1, y1, x2, y2
|
75 |
+
Returns:
|
76 |
+
correct (array[N, 10]), for 10 IoU levels
|
77 |
+
"""
|
78 |
+
if masks:
|
79 |
+
if overlap:
|
80 |
+
nl = len(labels)
|
81 |
+
index = torch.arange(nl, device=gt_masks.device).view(nl, 1, 1) + 1
|
82 |
+
gt_masks = gt_masks.repeat(nl, 1, 1) # shape(1,640,640) -> (n,640,640)
|
83 |
+
gt_masks = torch.where(gt_masks == index, 1.0, 0.0)
|
84 |
+
if gt_masks.shape[1:] != pred_masks.shape[1:]:
|
85 |
+
gt_masks = F.interpolate(gt_masks[None], pred_masks.shape[1:], mode="bilinear", align_corners=False)[0]
|
86 |
+
gt_masks = gt_masks.gt_(0.5)
|
87 |
+
iou = mask_iou(gt_masks.view(gt_masks.shape[0], -1), pred_masks.view(pred_masks.shape[0], -1))
|
88 |
+
else: # boxes
|
89 |
+
iou = box_iou(labels[:, 1:], detections[:, :4])
|
90 |
+
|
91 |
+
correct = np.zeros((detections.shape[0], iouv.shape[0])).astype(bool)
|
92 |
+
correct_class = labels[:, 0:1] == detections[:, 5]
|
93 |
+
for i in range(len(iouv)):
|
94 |
+
x = torch.where((iou >= iouv[i]) & correct_class) # IoU > threshold and classes match
|
95 |
+
if x[0].shape[0]:
|
96 |
+
matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy() # [label, detect, iou]
|
97 |
+
if x[0].shape[0] > 1:
|
98 |
+
matches = matches[matches[:, 2].argsort()[::-1]]
|
99 |
+
matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
|
100 |
+
# matches = matches[matches[:, 2].argsort()[::-1]]
|
101 |
+
matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
|
102 |
+
correct[matches[:, 1].astype(int), i] = True
|
103 |
+
return torch.tensor(correct, dtype=torch.bool, device=iouv.device)
|
104 |
+
|
105 |
+
|
106 |
+
@smart_inference_mode()
|
107 |
+
def run(
|
108 |
+
data,
|
109 |
+
weights=None, # model.pt path(s)
|
110 |
+
batch_size=32, # batch size
|
111 |
+
imgsz=640, # inference size (pixels)
|
112 |
+
conf_thres=0.001, # confidence threshold
|
113 |
+
iou_thres=0.6, # NMS IoU threshold
|
114 |
+
max_det=300, # maximum detections per image
|
115 |
+
task='val', # train, val, test, speed or study
|
116 |
+
device='', # cuda device, i.e. 0 or 0,1,2,3 or cpu
|
117 |
+
workers=8, # max dataloader workers (per RANK in DDP mode)
|
118 |
+
single_cls=False, # treat as single-class dataset
|
119 |
+
augment=False, # augmented inference
|
120 |
+
verbose=False, # verbose output
|
121 |
+
save_txt=False, # save results to *.txt
|
122 |
+
save_hybrid=False, # save label+prediction hybrid results to *.txt
|
123 |
+
save_conf=False, # save confidences in --save-txt labels
|
124 |
+
save_json=False, # save a COCO-JSON results file
|
125 |
+
project=ROOT / 'runs/val-seg', # save to project/name
|
126 |
+
name='exp', # save to project/name
|
127 |
+
exist_ok=False, # existing project/name ok, do not increment
|
128 |
+
half=True, # use FP16 half-precision inference
|
129 |
+
dnn=False, # use OpenCV DNN for ONNX inference
|
130 |
+
model=None,
|
131 |
+
dataloader=None,
|
132 |
+
save_dir=Path(''),
|
133 |
+
plots=True,
|
134 |
+
overlap=False,
|
135 |
+
mask_downsample_ratio=1,
|
136 |
+
compute_loss=None,
|
137 |
+
callbacks=Callbacks(),
|
138 |
+
):
|
139 |
+
if save_json:
|
140 |
+
check_requirements(['pycocotools'])
|
141 |
+
process = process_mask_upsample # more accurate
|
142 |
+
else:
|
143 |
+
process = process_mask # faster
|
144 |
+
|
145 |
+
# Initialize/load model and set device
|
146 |
+
training = model is not None
|
147 |
+
if training: # called by train.py
|
148 |
+
device, pt, jit, engine = next(model.parameters()).device, True, False, False # get model device, PyTorch model
|
149 |
+
half &= device.type != 'cpu' # half precision only supported on CUDA
|
150 |
+
model.half() if half else model.float()
|
151 |
+
nm = de_parallel(model).model[-1].nm # number of masks
|
152 |
+
else: # called directly
|
153 |
+
device = select_device(device, batch_size=batch_size)
|
154 |
+
|
155 |
+
# Directories
|
156 |
+
save_dir = increment_path(Path(project) / name, exist_ok=exist_ok) # increment run
|
157 |
+
(save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
|
158 |
+
|
159 |
+
# Load model
|
160 |
+
model = DetectMultiBackend(weights, device=device, dnn=dnn, data=data, fp16=half)
|
161 |
+
stride, pt, jit, engine = model.stride, model.pt, model.jit, model.engine
|
162 |
+
imgsz = check_img_size(imgsz, s=stride) # check image size
|
163 |
+
half = model.fp16 # FP16 supported on limited backends with CUDA
|
164 |
+
nm = de_parallel(model).model.model[-1].nm if isinstance(model, SegmentationModel) else 32 # number of masks
|
165 |
+
if engine:
|
166 |
+
batch_size = model.batch_size
|
167 |
+
else:
|
168 |
+
device = model.device
|
169 |
+
if not (pt or jit):
|
170 |
+
batch_size = 1 # export.py models default to batch-size 1
|
171 |
+
LOGGER.info(f'Forcing --batch-size 1 square inference (1,3,{imgsz},{imgsz}) for non-PyTorch models')
|
172 |
+
|
173 |
+
# Data
|
174 |
+
data = check_dataset(data) # check
|
175 |
+
|
176 |
+
# Configure
|
177 |
+
model.eval()
|
178 |
+
cuda = device.type != 'cpu'
|
179 |
+
#is_coco = isinstance(data.get('val'), str) and data['val'].endswith(f'coco{os.sep}val2017.txt') # COCO dataset
|
180 |
+
is_coco = isinstance(data.get('val'), str) and data['val'].endswith(f'val2017.txt') # COCO dataset
|
181 |
+
nc = 1 if single_cls else int(data['nc']) # number of classes
|
182 |
+
iouv = torch.linspace(0.5, 0.95, 10, device=device) # iou vector for [email protected]:0.95
|
183 |
+
niou = iouv.numel()
|
184 |
+
|
185 |
+
# Dataloader
|
186 |
+
if not training:
|
187 |
+
if pt and not single_cls: # check --weights are trained on --data
|
188 |
+
ncm = model.model.nc
|
189 |
+
assert ncm == nc, f'{weights} ({ncm} classes) trained on different --data than what you passed ({nc} ' \
|
190 |
+
f'classes). Pass correct combination of --weights and --data that are trained together.'
|
191 |
+
model.warmup(imgsz=(1 if pt else batch_size, 3, imgsz, imgsz)) # warmup
|
192 |
+
pad, rect = (0.0, False) if task == 'speed' else (0.5, pt) # square inference for benchmarks
|
193 |
+
task = task if task in ('train', 'val', 'test') else 'val' # path to train/val/test images
|
194 |
+
dataloader = create_dataloader(data[task],
|
195 |
+
imgsz,
|
196 |
+
batch_size,
|
197 |
+
stride,
|
198 |
+
single_cls,
|
199 |
+
pad=pad,
|
200 |
+
rect=rect,
|
201 |
+
workers=workers,
|
202 |
+
prefix=colorstr(f'{task}: '),
|
203 |
+
overlap_mask=overlap,
|
204 |
+
mask_downsample_ratio=mask_downsample_ratio)[0]
|
205 |
+
|
206 |
+
seen = 0
|
207 |
+
confusion_matrix = ConfusionMatrix(nc=nc)
|
208 |
+
names = model.names if hasattr(model, 'names') else model.module.names # get class names
|
209 |
+
if isinstance(names, (list, tuple)): # old format
|
210 |
+
names = dict(enumerate(names))
|
211 |
+
class_map = coco80_to_coco91_class() if is_coco else list(range(1000))
|
212 |
+
s = ('%22s' + '%11s' * 10) % ('Class', 'Images', 'Instances', 'Box(P', "R", "mAP50", "mAP50-95)", "Mask(P", "R",
|
213 |
+
"mAP50", "mAP50-95)")
|
214 |
+
dt = Profile(), Profile(), Profile()
|
215 |
+
metrics = Metrics()
|
216 |
+
loss = torch.zeros(4, device=device)
|
217 |
+
jdict, stats = [], []
|
218 |
+
# callbacks.run('on_val_start')
|
219 |
+
pbar = tqdm(dataloader, desc=s, bar_format=TQDM_BAR_FORMAT) # progress bar
|
220 |
+
for batch_i, (im, targets, paths, shapes, masks) in enumerate(pbar):
|
221 |
+
# callbacks.run('on_val_batch_start')
|
222 |
+
with dt[0]:
|
223 |
+
if cuda:
|
224 |
+
im = im.to(device, non_blocking=True)
|
225 |
+
targets = targets.to(device)
|
226 |
+
masks = masks.to(device)
|
227 |
+
masks = masks.float()
|
228 |
+
im = im.half() if half else im.float() # uint8 to fp16/32
|
229 |
+
im /= 255 # 0 - 255 to 0.0 - 1.0
|
230 |
+
nb, _, height, width = im.shape # batch size, channels, height, width
|
231 |
+
|
232 |
+
# Inference
|
233 |
+
with dt[1]:
|
234 |
+
preds, train_out = model(im)# if compute_loss else (*model(im, augment=augment)[:2], None)
|
235 |
+
#preds = preds[1]
|
236 |
+
#train_out, preds, protos = p if len(p) == 3 else p[1]
|
237 |
+
#preds = p
|
238 |
+
#train_out = p[1][0] if len(p[1]) == 3 else p[0]
|
239 |
+
protos = train_out[-1]
|
240 |
+
#print(preds.shape)
|
241 |
+
#print(train_out[0].shape)
|
242 |
+
#print(train_out[1].shape)
|
243 |
+
#print(train_out[2].shape)
|
244 |
+
|
245 |
+
# Loss
|
246 |
+
#if compute_loss:
|
247 |
+
# loss += compute_loss(train_out, targets, masks)[1] # box, obj, cls
|
248 |
+
|
249 |
+
# NMS
|
250 |
+
targets[:, 2:] *= torch.tensor((width, height, width, height), device=device) # to pixels
|
251 |
+
lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_hybrid else [] # for autolabelling
|
252 |
+
with dt[2]:
|
253 |
+
preds = non_max_suppression(preds,
|
254 |
+
conf_thres,
|
255 |
+
iou_thres,
|
256 |
+
labels=lb,
|
257 |
+
multi_label=True,
|
258 |
+
agnostic=single_cls,
|
259 |
+
max_det=max_det,
|
260 |
+
nm=nm)
|
261 |
+
|
262 |
+
# Metrics
|
263 |
+
plot_masks = [] # masks for plotting
|
264 |
+
for si, (pred, proto) in enumerate(zip(preds, protos)):
|
265 |
+
labels = targets[targets[:, 0] == si, 1:]
|
266 |
+
nl, npr = labels.shape[0], pred.shape[0] # number of labels, predictions
|
267 |
+
path, shape = Path(paths[si]), shapes[si][0]
|
268 |
+
correct_masks = torch.zeros(npr, niou, dtype=torch.bool, device=device) # init
|
269 |
+
correct_bboxes = torch.zeros(npr, niou, dtype=torch.bool, device=device) # init
|
270 |
+
seen += 1
|
271 |
+
|
272 |
+
if npr == 0:
|
273 |
+
if nl:
|
274 |
+
stats.append((correct_masks, correct_bboxes, *torch.zeros((2, 0), device=device), labels[:, 0]))
|
275 |
+
if plots:
|
276 |
+
confusion_matrix.process_batch(detections=None, labels=labels[:, 0])
|
277 |
+
continue
|
278 |
+
|
279 |
+
# Masks
|
280 |
+
midx = [si] if overlap else targets[:, 0] == si
|
281 |
+
gt_masks = masks[midx]
|
282 |
+
pred_masks = process(proto, pred[:, 6:], pred[:, :4], shape=im[si].shape[1:])
|
283 |
+
|
284 |
+
# Predictions
|
285 |
+
if single_cls:
|
286 |
+
pred[:, 5] = 0
|
287 |
+
predn = pred.clone()
|
288 |
+
scale_boxes(im[si].shape[1:], predn[:, :4], shape, shapes[si][1]) # native-space pred
|
289 |
+
|
290 |
+
# Evaluate
|
291 |
+
if nl:
|
292 |
+
tbox = xywh2xyxy(labels[:, 1:5]) # target boxes
|
293 |
+
scale_boxes(im[si].shape[1:], tbox, shape, shapes[si][1]) # native-space labels
|
294 |
+
labelsn = torch.cat((labels[:, 0:1], tbox), 1) # native-space labels
|
295 |
+
correct_bboxes = process_batch(predn, labelsn, iouv)
|
296 |
+
correct_masks = process_batch(predn, labelsn, iouv, pred_masks, gt_masks, overlap=overlap, masks=True)
|
297 |
+
if plots:
|
298 |
+
confusion_matrix.process_batch(predn, labelsn)
|
299 |
+
stats.append((correct_masks, correct_bboxes, pred[:, 4], pred[:, 5], labels[:, 0])) # (conf, pcls, tcls)
|
300 |
+
|
301 |
+
pred_masks = torch.as_tensor(pred_masks, dtype=torch.uint8)
|
302 |
+
if plots and batch_i < 3:
|
303 |
+
plot_masks.append(pred_masks[:15].cpu()) # filter top 15 to plot
|
304 |
+
|
305 |
+
# Save/log
|
306 |
+
if save_txt:
|
307 |
+
save_one_txt(predn, save_conf, shape, file=save_dir / 'labels' / f'{path.stem}.txt')
|
308 |
+
if save_json:
|
309 |
+
pred_masks = scale_image(im[si].shape[1:],
|
310 |
+
pred_masks.permute(1, 2, 0).contiguous().cpu().numpy(), shape, shapes[si][1])
|
311 |
+
save_one_json(predn, jdict, path, class_map, pred_masks) # append to COCO-JSON dictionary
|
312 |
+
# callbacks.run('on_val_image_end', pred, predn, path, names, im[si])
|
313 |
+
|
314 |
+
# Plot images
|
315 |
+
if plots and batch_i < 3:
|
316 |
+
if len(plot_masks):
|
317 |
+
plot_masks = torch.cat(plot_masks, dim=0)
|
318 |
+
plot_images_and_masks(im, targets, masks, paths, save_dir / f'val_batch{batch_i}_labels.jpg', names)
|
319 |
+
plot_images_and_masks(im, output_to_target(preds, max_det=15), plot_masks, paths,
|
320 |
+
save_dir / f'val_batch{batch_i}_pred.jpg', names) # pred
|
321 |
+
|
322 |
+
# callbacks.run('on_val_batch_end')
|
323 |
+
|
324 |
+
# Compute metrics
|
325 |
+
stats = [torch.cat(x, 0).cpu().numpy() for x in zip(*stats)] # to numpy
|
326 |
+
if len(stats) and stats[0].any():
|
327 |
+
results = ap_per_class_box_and_mask(*stats, plot=plots, save_dir=save_dir, names=names)
|
328 |
+
metrics.update(results)
|
329 |
+
nt = np.bincount(stats[4].astype(int), minlength=nc) # number of targets per class
|
330 |
+
|
331 |
+
# Print results
|
332 |
+
pf = '%22s' + '%11i' * 2 + '%11.3g' * 8 # print format
|
333 |
+
LOGGER.info(pf % ("all", seen, nt.sum(), *metrics.mean_results()))
|
334 |
+
if nt.sum() == 0:
|
335 |
+
LOGGER.warning(f'WARNING ⚠️ no labels found in {task} set, can not compute metrics without labels')
|
336 |
+
|
337 |
+
# Print results per class
|
338 |
+
if (verbose or (nc < 50 and not training)) and nc > 1 and len(stats):
|
339 |
+
for i, c in enumerate(metrics.ap_class_index):
|
340 |
+
LOGGER.info(pf % (names[c], seen, nt[c], *metrics.class_result(i)))
|
341 |
+
|
342 |
+
# Print speeds
|
343 |
+
t = tuple(x.t / seen * 1E3 for x in dt) # speeds per image
|
344 |
+
if not training:
|
345 |
+
shape = (batch_size, 3, imgsz, imgsz)
|
346 |
+
LOGGER.info(f'Speed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {shape}' % t)
|
347 |
+
|
348 |
+
# Plots
|
349 |
+
if plots:
|
350 |
+
confusion_matrix.plot(save_dir=save_dir, names=list(names.values()))
|
351 |
+
# callbacks.run('on_val_end')
|
352 |
+
|
353 |
+
mp_bbox, mr_bbox, map50_bbox, map_bbox, mp_mask, mr_mask, map50_mask, map_mask = metrics.mean_results()
|
354 |
+
|
355 |
+
# Save JSON
|
356 |
+
if save_json and len(jdict):
|
357 |
+
w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights
|
358 |
+
anno_json = str(Path(data.get('path', '../coco')) / 'annotations/instances_val2017.json') # annotations json
|
359 |
+
pred_json = str(save_dir / f"{w}_predictions.json") # predictions json
|
360 |
+
LOGGER.info(f'\nEvaluating pycocotools mAP... saving {pred_json}...')
|
361 |
+
with open(pred_json, 'w') as f:
|
362 |
+
json.dump(jdict, f)
|
363 |
+
|
364 |
+
try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
|
365 |
+
from pycocotools.coco import COCO
|
366 |
+
from pycocotools.cocoeval import COCOeval
|
367 |
+
|
368 |
+
anno = COCO(anno_json) # init annotations api
|
369 |
+
pred = anno.loadRes(pred_json) # init predictions api
|
370 |
+
results = []
|
371 |
+
for eval in COCOeval(anno, pred, 'bbox'), COCOeval(anno, pred, 'segm'):
|
372 |
+
if is_coco:
|
373 |
+
eval.params.imgIds = [int(Path(x).stem) for x in dataloader.dataset.im_files] # img ID to evaluate
|
374 |
+
eval.evaluate()
|
375 |
+
eval.accumulate()
|
376 |
+
eval.summarize()
|
377 |
+
results.extend(eval.stats[:2]) # update results ([email protected]:0.95, [email protected])
|
378 |
+
map_bbox, map50_bbox, map_mask, map50_mask = results
|
379 |
+
except Exception as e:
|
380 |
+
LOGGER.info(f'pycocotools unable to run: {e}')
|
381 |
+
|
382 |
+
# Return results
|
383 |
+
model.float() # for training
|
384 |
+
if not training:
|
385 |
+
s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
|
386 |
+
LOGGER.info(f"Results saved to {colorstr('bold', save_dir)}{s}")
|
387 |
+
final_metric = mp_bbox, mr_bbox, map50_bbox, map_bbox, mp_mask, mr_mask, map50_mask, map_mask
|
388 |
+
return (*final_metric, *(loss.cpu() / len(dataloader)).tolist()), metrics.get_maps(nc), t
|
389 |
+
|
390 |
+
|
391 |
+
def parse_opt():
|
392 |
+
parser = argparse.ArgumentParser()
|
393 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128-seg.yaml', help='dataset.yaml path')
|
394 |
+
parser.add_argument('--weights', nargs='+', type=str, default=ROOT / 'yolo-seg.pt', help='model path(s)')
|
395 |
+
parser.add_argument('--batch-size', type=int, default=32, help='batch size')
|
396 |
+
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='inference size (pixels)')
|
397 |
+
parser.add_argument('--conf-thres', type=float, default=0.001, help='confidence threshold')
|
398 |
+
parser.add_argument('--iou-thres', type=float, default=0.6, help='NMS IoU threshold')
|
399 |
+
parser.add_argument('--max-det', type=int, default=300, help='maximum detections per image')
|
400 |
+
parser.add_argument('--task', default='val', help='train, val, test, speed or study')
|
401 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
402 |
+
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
|
403 |
+
parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset')
|
404 |
+
parser.add_argument('--augment', action='store_true', help='augmented inference')
|
405 |
+
parser.add_argument('--verbose', action='store_true', help='report mAP by class')
|
406 |
+
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
|
407 |
+
parser.add_argument('--save-hybrid', action='store_true', help='save label+prediction hybrid results to *.txt')
|
408 |
+
parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
|
409 |
+
parser.add_argument('--save-json', action='store_true', help='save a COCO-JSON results file')
|
410 |
+
parser.add_argument('--project', default=ROOT / 'runs/val-seg', help='save results to project/name')
|
411 |
+
parser.add_argument('--name', default='exp', help='save to project/name')
|
412 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
413 |
+
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
|
414 |
+
parser.add_argument('--dnn', action='store_true', help='use OpenCV DNN for ONNX inference')
|
415 |
+
opt = parser.parse_args()
|
416 |
+
opt.data = check_yaml(opt.data) # check YAML
|
417 |
+
# opt.save_json |= opt.data.endswith('coco.yaml')
|
418 |
+
opt.save_txt |= opt.save_hybrid
|
419 |
+
print_args(vars(opt))
|
420 |
+
return opt
|
421 |
+
|
422 |
+
|
423 |
+
def main(opt):
|
424 |
+
#check_requirements(requirements=ROOT / 'requirements.txt', exclude=('tensorboard', 'thop'))
|
425 |
+
|
426 |
+
if opt.task in ('train', 'val', 'test'): # run normally
|
427 |
+
if opt.conf_thres > 0.001: # https://github.com/ultralytics/yolov5/issues/1466
|
428 |
+
LOGGER.warning(f'WARNING ⚠️ confidence threshold {opt.conf_thres} > 0.001 produces invalid results')
|
429 |
+
if opt.save_hybrid:
|
430 |
+
LOGGER.warning('WARNING ⚠️ --save-hybrid returns high mAP from hybrid labels, not from predictions alone')
|
431 |
+
run(**vars(opt))
|
432 |
+
|
433 |
+
else:
|
434 |
+
weights = opt.weights if isinstance(opt.weights, list) else [opt.weights]
|
435 |
+
opt.half = torch.cuda.is_available() and opt.device != 'cpu' # FP16 for fastest results
|
436 |
+
if opt.task == 'speed': # speed benchmarks
|
437 |
+
# python val.py --task speed --data coco.yaml --batch 1 --weights yolo.pt...
|
438 |
+
opt.conf_thres, opt.iou_thres, opt.save_json = 0.25, 0.45, False
|
439 |
+
for opt.weights in weights:
|
440 |
+
run(**vars(opt), plots=False)
|
441 |
+
|
442 |
+
elif opt.task == 'study': # speed vs mAP benchmarks
|
443 |
+
# python val.py --task study --data coco.yaml --iou 0.7 --weights yolo.pt...
|
444 |
+
for opt.weights in weights:
|
445 |
+
f = f'study_{Path(opt.data).stem}_{Path(opt.weights).stem}.txt' # filename to save to
|
446 |
+
x, y = list(range(256, 1536 + 128, 128)), [] # x axis (image sizes), y axis
|
447 |
+
for opt.imgsz in x: # img-size
|
448 |
+
LOGGER.info(f'\nRunning {f} --imgsz {opt.imgsz}...')
|
449 |
+
r, _, t = run(**vars(opt), plots=False)
|
450 |
+
y.append(r + t) # results and times
|
451 |
+
np.savetxt(f, y, fmt='%10.4g') # save
|
452 |
+
os.system('zip -r study.zip study_*.txt')
|
453 |
+
plot_val_study(x=x) # plot
|
454 |
+
|
455 |
+
|
456 |
+
if __name__ == "__main__":
|
457 |
+
opt = parse_opt()
|
458 |
+
main(opt)
|
streamlit.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
temp_image.jpg
ADDED
![]() |
tools/reparameterization.ipynb
ADDED
@@ -0,0 +1,450 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": null,
|
6 |
+
"id": "4beac401",
|
7 |
+
"metadata": {},
|
8 |
+
"outputs": [],
|
9 |
+
"source": [
|
10 |
+
"import torch\n",
|
11 |
+
"from models.yolo import Model"
|
12 |
+
]
|
13 |
+
},
|
14 |
+
{
|
15 |
+
"cell_type": "markdown",
|
16 |
+
"id": "d1a8399f",
|
17 |
+
"metadata": {},
|
18 |
+
"source": [
|
19 |
+
"## Convert YOLOv9-S"
|
20 |
+
]
|
21 |
+
},
|
22 |
+
{
|
23 |
+
"cell_type": "code",
|
24 |
+
"execution_count": null,
|
25 |
+
"id": "c7a40f10",
|
26 |
+
"metadata": {},
|
27 |
+
"outputs": [],
|
28 |
+
"source": [
|
29 |
+
"device = torch.device(\"cpu\")\n",
|
30 |
+
"cfg = \"./models/detect/gelan-s.yaml\"\n",
|
31 |
+
"model = Model(cfg, ch=3, nc=80, anchors=3)\n",
|
32 |
+
"#model = model.half()\n",
|
33 |
+
"model = model.to(device)\n",
|
34 |
+
"_ = model.eval()\n",
|
35 |
+
"ckpt = torch.load('./yolov9-s.pt', map_location='cpu')\n",
|
36 |
+
"model.names = ckpt['model'].names\n",
|
37 |
+
"model.nc = ckpt['model'].nc"
|
38 |
+
]
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"cell_type": "code",
|
42 |
+
"execution_count": null,
|
43 |
+
"id": "3b046bb2",
|
44 |
+
"metadata": {},
|
45 |
+
"outputs": [],
|
46 |
+
"source": [
|
47 |
+
"idx = 0\n",
|
48 |
+
"for k, v in model.state_dict().items():\n",
|
49 |
+
" if \"model.{}.\".format(idx) in k:\n",
|
50 |
+
" if idx < 22:\n",
|
51 |
+
" kr = k.replace(\"model.{}.\".format(idx), \"model.{}.\".format(idx))\n",
|
52 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
53 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
54 |
+
" print(k, \"perfectly matched!!\")\n",
|
55 |
+
" elif \"model.{}.cv2.\".format(idx) in k:\n",
|
56 |
+
" kr = k.replace(\"model.{}.cv2.\".format(idx), \"model.{}.cv4.\".format(idx+7))\n",
|
57 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
58 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
59 |
+
" print(k, \"perfectly matched!!\")\n",
|
60 |
+
" elif \"model.{}.cv3.\".format(idx) in k:\n",
|
61 |
+
" kr = k.replace(\"model.{}.cv3.\".format(idx), \"model.{}.cv5.\".format(idx+7))\n",
|
62 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
63 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
64 |
+
" print(k, \"perfectly matched!!\")\n",
|
65 |
+
" elif \"model.{}.dfl.\".format(idx) in k:\n",
|
66 |
+
" kr = k.replace(\"model.{}.dfl.\".format(idx), \"model.{}.dfl2.\".format(idx+7))\n",
|
67 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
68 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
69 |
+
" print(k, \"perfectly matched!!\")\n",
|
70 |
+
" else:\n",
|
71 |
+
" while True:\n",
|
72 |
+
" idx += 1\n",
|
73 |
+
" if \"model.{}.\".format(idx) in k:\n",
|
74 |
+
" break\n",
|
75 |
+
" if idx < 22:\n",
|
76 |
+
" kr = k.replace(\"model.{}.\".format(idx), \"model.{}.\".format(idx))\n",
|
77 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
78 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
79 |
+
" print(k, \"perfectly matched!!\")\n",
|
80 |
+
" elif \"model.{}.cv2.\".format(idx) in k:\n",
|
81 |
+
" kr = k.replace(\"model.{}.cv2.\".format(idx), \"model.{}.cv4.\".format(idx+7))\n",
|
82 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
83 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
84 |
+
" print(k, \"perfectly matched!!\")\n",
|
85 |
+
" elif \"model.{}.cv3.\".format(idx) in k:\n",
|
86 |
+
" kr = k.replace(\"model.{}.cv3.\".format(idx), \"model.{}.cv5.\".format(idx+7))\n",
|
87 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
88 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
89 |
+
" print(k, \"perfectly matched!!\")\n",
|
90 |
+
" elif \"model.{}.dfl.\".format(idx) in k:\n",
|
91 |
+
" kr = k.replace(\"model.{}.dfl.\".format(idx), \"model.{}.dfl2.\".format(idx+7))\n",
|
92 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
93 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
94 |
+
" print(k, \"perfectly matched!!\")\n",
|
95 |
+
"_ = model.eval()"
|
96 |
+
]
|
97 |
+
},
|
98 |
+
{
|
99 |
+
"cell_type": "code",
|
100 |
+
"execution_count": null,
|
101 |
+
"id": "07eb0cde",
|
102 |
+
"metadata": {},
|
103 |
+
"outputs": [],
|
104 |
+
"source": [
|
105 |
+
"m_ckpt = {'model': model.half(),\n",
|
106 |
+
" 'optimizer': None,\n",
|
107 |
+
" 'best_fitness': None,\n",
|
108 |
+
" 'ema': None,\n",
|
109 |
+
" 'updates': None,\n",
|
110 |
+
" 'opt': None,\n",
|
111 |
+
" 'git': None,\n",
|
112 |
+
" 'date': None,\n",
|
113 |
+
" 'epoch': -1}\n",
|
114 |
+
"torch.save(m_ckpt, \"./yolov9-s-converted.pt\")"
|
115 |
+
]
|
116 |
+
},
|
117 |
+
{
|
118 |
+
"cell_type": "markdown",
|
119 |
+
"id": "ba87d10f",
|
120 |
+
"metadata": {},
|
121 |
+
"source": [
|
122 |
+
"## Convert YOLOv9-M"
|
123 |
+
]
|
124 |
+
},
|
125 |
+
{
|
126 |
+
"cell_type": "code",
|
127 |
+
"execution_count": null,
|
128 |
+
"id": "cc41b027",
|
129 |
+
"metadata": {},
|
130 |
+
"outputs": [],
|
131 |
+
"source": [
|
132 |
+
"device = torch.device(\"cpu\")\n",
|
133 |
+
"cfg = \"./models/detect/gelan-m.yaml\"\n",
|
134 |
+
"model = Model(cfg, ch=3, nc=80, anchors=3)\n",
|
135 |
+
"#model = model.half()\n",
|
136 |
+
"model = model.to(device)\n",
|
137 |
+
"_ = model.eval()\n",
|
138 |
+
"ckpt = torch.load('./yolov9-m.pt', map_location='cpu')\n",
|
139 |
+
"model.names = ckpt['model'].names\n",
|
140 |
+
"model.nc = ckpt['model'].nc"
|
141 |
+
]
|
142 |
+
},
|
143 |
+
{
|
144 |
+
"cell_type": "code",
|
145 |
+
"execution_count": null,
|
146 |
+
"id": "cf7c3978",
|
147 |
+
"metadata": {},
|
148 |
+
"outputs": [],
|
149 |
+
"source": [
|
150 |
+
"idx = 0\n",
|
151 |
+
"for k, v in model.state_dict().items():\n",
|
152 |
+
" if \"model.{}.\".format(idx) in k:\n",
|
153 |
+
" if idx < 22:\n",
|
154 |
+
" kr = k.replace(\"model.{}.\".format(idx), \"model.{}.\".format(idx+1))\n",
|
155 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
156 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
157 |
+
" print(k, \"perfectly matched!!\")\n",
|
158 |
+
" elif \"model.{}.cv2.\".format(idx) in k:\n",
|
159 |
+
" kr = k.replace(\"model.{}.cv2.\".format(idx), \"model.{}.cv4.\".format(idx+16))\n",
|
160 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
161 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
162 |
+
" print(k, \"perfectly matched!!\")\n",
|
163 |
+
" elif \"model.{}.cv3.\".format(idx) in k:\n",
|
164 |
+
" kr = k.replace(\"model.{}.cv3.\".format(idx), \"model.{}.cv5.\".format(idx+16))\n",
|
165 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
166 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
167 |
+
" print(k, \"perfectly matched!!\")\n",
|
168 |
+
" elif \"model.{}.dfl.\".format(idx) in k:\n",
|
169 |
+
" kr = k.replace(\"model.{}.dfl.\".format(idx), \"model.{}.dfl2.\".format(idx+16))\n",
|
170 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
171 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
172 |
+
" print(k, \"perfectly matched!!\")\n",
|
173 |
+
" else:\n",
|
174 |
+
" while True:\n",
|
175 |
+
" idx += 1\n",
|
176 |
+
" if \"model.{}.\".format(idx) in k:\n",
|
177 |
+
" break\n",
|
178 |
+
" if idx < 22:\n",
|
179 |
+
" kr = k.replace(\"model.{}.\".format(idx), \"model.{}.\".format(idx+1))\n",
|
180 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
181 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
182 |
+
" print(k, \"perfectly matched!!\")\n",
|
183 |
+
" elif \"model.{}.cv2.\".format(idx) in k:\n",
|
184 |
+
" kr = k.replace(\"model.{}.cv2.\".format(idx), \"model.{}.cv4.\".format(idx+16))\n",
|
185 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
186 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
187 |
+
" print(k, \"perfectly matched!!\")\n",
|
188 |
+
" elif \"model.{}.cv3.\".format(idx) in k:\n",
|
189 |
+
" kr = k.replace(\"model.{}.cv3.\".format(idx), \"model.{}.cv5.\".format(idx+16))\n",
|
190 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
191 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
192 |
+
" print(k, \"perfectly matched!!\")\n",
|
193 |
+
" elif \"model.{}.dfl.\".format(idx) in k:\n",
|
194 |
+
" kr = k.replace(\"model.{}.dfl.\".format(idx), \"model.{}.dfl2.\".format(idx+16))\n",
|
195 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
196 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
197 |
+
" print(k, \"perfectly matched!!\")\n",
|
198 |
+
"_ = model.eval()"
|
199 |
+
]
|
200 |
+
},
|
201 |
+
{
|
202 |
+
"cell_type": "code",
|
203 |
+
"execution_count": null,
|
204 |
+
"id": "00a92a45",
|
205 |
+
"metadata": {},
|
206 |
+
"outputs": [],
|
207 |
+
"source": [
|
208 |
+
"m_ckpt = {'model': model.half(),\n",
|
209 |
+
" 'optimizer': None,\n",
|
210 |
+
" 'best_fitness': None,\n",
|
211 |
+
" 'ema': None,\n",
|
212 |
+
" 'updates': None,\n",
|
213 |
+
" 'opt': None,\n",
|
214 |
+
" 'git': None,\n",
|
215 |
+
" 'date': None,\n",
|
216 |
+
" 'epoch': -1}\n",
|
217 |
+
"torch.save(m_ckpt, \"./yolov9-m-converted.pt\")"
|
218 |
+
]
|
219 |
+
},
|
220 |
+
{
|
221 |
+
"cell_type": "markdown",
|
222 |
+
"id": "8680f822",
|
223 |
+
"metadata": {},
|
224 |
+
"source": [
|
225 |
+
"## Convert YOLOv9-C"
|
226 |
+
]
|
227 |
+
},
|
228 |
+
{
|
229 |
+
"cell_type": "code",
|
230 |
+
"execution_count": null,
|
231 |
+
"id": "59f0198d",
|
232 |
+
"metadata": {},
|
233 |
+
"outputs": [],
|
234 |
+
"source": [
|
235 |
+
"device = torch.device(\"cpu\")\n",
|
236 |
+
"cfg = \"./models/detect/gelan-c.yaml\"\n",
|
237 |
+
"model = Model(cfg, ch=3, nc=80, anchors=3)\n",
|
238 |
+
"#model = model.half()\n",
|
239 |
+
"model = model.to(device)\n",
|
240 |
+
"_ = model.eval()\n",
|
241 |
+
"ckpt = torch.load('./yolov9-c.pt', map_location='cpu')\n",
|
242 |
+
"model.names = ckpt['model'].names\n",
|
243 |
+
"model.nc = ckpt['model'].nc"
|
244 |
+
]
|
245 |
+
},
|
246 |
+
{
|
247 |
+
"cell_type": "code",
|
248 |
+
"execution_count": null,
|
249 |
+
"id": "2de7e1be",
|
250 |
+
"metadata": {},
|
251 |
+
"outputs": [],
|
252 |
+
"source": [
|
253 |
+
"idx = 0\n",
|
254 |
+
"for k, v in model.state_dict().items():\n",
|
255 |
+
" if \"model.{}.\".format(idx) in k:\n",
|
256 |
+
" if idx < 22:\n",
|
257 |
+
" kr = k.replace(\"model.{}.\".format(idx), \"model.{}.\".format(idx+1))\n",
|
258 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
259 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
260 |
+
" elif \"model.{}.cv2.\".format(idx) in k:\n",
|
261 |
+
" kr = k.replace(\"model.{}.cv2.\".format(idx), \"model.{}.cv4.\".format(idx+16))\n",
|
262 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
263 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
264 |
+
" elif \"model.{}.cv3.\".format(idx) in k:\n",
|
265 |
+
" kr = k.replace(\"model.{}.cv3.\".format(idx), \"model.{}.cv5.\".format(idx+16))\n",
|
266 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
267 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
268 |
+
" elif \"model.{}.dfl.\".format(idx) in k:\n",
|
269 |
+
" kr = k.replace(\"model.{}.dfl.\".format(idx), \"model.{}.dfl2.\".format(idx+16))\n",
|
270 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
271 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
272 |
+
" else:\n",
|
273 |
+
" while True:\n",
|
274 |
+
" idx += 1\n",
|
275 |
+
" if \"model.{}.\".format(idx) in k:\n",
|
276 |
+
" break\n",
|
277 |
+
" if idx < 22:\n",
|
278 |
+
" kr = k.replace(\"model.{}.\".format(idx), \"model.{}.\".format(idx+1))\n",
|
279 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
280 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
281 |
+
" elif \"model.{}.cv2.\".format(idx) in k:\n",
|
282 |
+
" kr = k.replace(\"model.{}.cv2.\".format(idx), \"model.{}.cv4.\".format(idx+16))\n",
|
283 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
284 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
285 |
+
" elif \"model.{}.cv3.\".format(idx) in k:\n",
|
286 |
+
" kr = k.replace(\"model.{}.cv3.\".format(idx), \"model.{}.cv5.\".format(idx+16))\n",
|
287 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
288 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
289 |
+
" elif \"model.{}.dfl.\".format(idx) in k:\n",
|
290 |
+
" kr = k.replace(\"model.{}.dfl.\".format(idx), \"model.{}.dfl2.\".format(idx+16))\n",
|
291 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
292 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
293 |
+
"_ = model.eval()"
|
294 |
+
]
|
295 |
+
},
|
296 |
+
{
|
297 |
+
"cell_type": "code",
|
298 |
+
"execution_count": null,
|
299 |
+
"id": "960796e3",
|
300 |
+
"metadata": {},
|
301 |
+
"outputs": [],
|
302 |
+
"source": [
|
303 |
+
"m_ckpt = {'model': model.half(),\n",
|
304 |
+
" 'optimizer': None,\n",
|
305 |
+
" 'best_fitness': None,\n",
|
306 |
+
" 'ema': None,\n",
|
307 |
+
" 'updates': None,\n",
|
308 |
+
" 'opt': None,\n",
|
309 |
+
" 'git': None,\n",
|
310 |
+
" 'date': None,\n",
|
311 |
+
" 'epoch': -1}\n",
|
312 |
+
"torch.save(m_ckpt, \"./yolov9-c-converted.pt\")"
|
313 |
+
]
|
314 |
+
},
|
315 |
+
{
|
316 |
+
"cell_type": "markdown",
|
317 |
+
"id": "47c6e6ae",
|
318 |
+
"metadata": {},
|
319 |
+
"source": [
|
320 |
+
"## Convert YOLOv9-E"
|
321 |
+
]
|
322 |
+
},
|
323 |
+
{
|
324 |
+
"cell_type": "code",
|
325 |
+
"execution_count": null,
|
326 |
+
"id": "801a1b7c",
|
327 |
+
"metadata": {},
|
328 |
+
"outputs": [],
|
329 |
+
"source": [
|
330 |
+
"device = torch.device(\"cpu\")\n",
|
331 |
+
"cfg = \"./models/detect/gelan-e.yaml\"\n",
|
332 |
+
"model = Model(cfg, ch=3, nc=80, anchors=3)\n",
|
333 |
+
"#model = model.half()\n",
|
334 |
+
"model = model.to(device)\n",
|
335 |
+
"_ = model.eval()\n",
|
336 |
+
"ckpt = torch.load('./yolov9-e.pt', map_location='cpu')\n",
|
337 |
+
"model.names = ckpt['model'].names\n",
|
338 |
+
"model.nc = ckpt['model'].nc"
|
339 |
+
]
|
340 |
+
},
|
341 |
+
{
|
342 |
+
"cell_type": "code",
|
343 |
+
"execution_count": null,
|
344 |
+
"id": "a2ef4fe6",
|
345 |
+
"metadata": {},
|
346 |
+
"outputs": [],
|
347 |
+
"source": [
|
348 |
+
"idx = 0\n",
|
349 |
+
"for k, v in model.state_dict().items():\n",
|
350 |
+
" if \"model.{}.\".format(idx) in k:\n",
|
351 |
+
" if idx < 29:\n",
|
352 |
+
" kr = k.replace(\"model.{}.\".format(idx), \"model.{}.\".format(idx))\n",
|
353 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
354 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
355 |
+
" print(k, \"perfectly matched!!\")\n",
|
356 |
+
" elif idx < 42:\n",
|
357 |
+
" kr = k.replace(\"model.{}.\".format(idx), \"model.{}.\".format(idx+7))\n",
|
358 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
359 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
360 |
+
" print(k, \"perfectly matched!!\")\n",
|
361 |
+
" elif \"model.{}.cv2.\".format(idx) in k:\n",
|
362 |
+
" kr = k.replace(\"model.{}.cv2.\".format(idx), \"model.{}.cv4.\".format(idx+7))\n",
|
363 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
364 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
365 |
+
" print(k, \"perfectly matched!!\")\n",
|
366 |
+
" elif \"model.{}.cv3.\".format(idx) in k:\n",
|
367 |
+
" kr = k.replace(\"model.{}.cv3.\".format(idx), \"model.{}.cv5.\".format(idx+7))\n",
|
368 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
369 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
370 |
+
" print(k, \"perfectly matched!!\")\n",
|
371 |
+
" elif \"model.{}.dfl.\".format(idx) in k:\n",
|
372 |
+
" kr = k.replace(\"model.{}.dfl.\".format(idx), \"model.{}.dfl2.\".format(idx+7))\n",
|
373 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
374 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
375 |
+
" print(k, \"perfectly matched!!\")\n",
|
376 |
+
" else:\n",
|
377 |
+
" while True:\n",
|
378 |
+
" idx += 1\n",
|
379 |
+
" if \"model.{}.\".format(idx) in k:\n",
|
380 |
+
" break\n",
|
381 |
+
" if idx < 29:\n",
|
382 |
+
" kr = k.replace(\"model.{}.\".format(idx), \"model.{}.\".format(idx))\n",
|
383 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
384 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
385 |
+
" print(k, \"perfectly matched!!\")\n",
|
386 |
+
" elif idx < 42:\n",
|
387 |
+
" kr = k.replace(\"model.{}.\".format(idx), \"model.{}.\".format(idx+7))\n",
|
388 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
389 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
390 |
+
" print(k, \"perfectly matched!!\")\n",
|
391 |
+
" elif \"model.{}.cv2.\".format(idx) in k:\n",
|
392 |
+
" kr = k.replace(\"model.{}.cv2.\".format(idx), \"model.{}.cv4.\".format(idx+7))\n",
|
393 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
394 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
395 |
+
" print(k, \"perfectly matched!!\")\n",
|
396 |
+
" elif \"model.{}.cv3.\".format(idx) in k:\n",
|
397 |
+
" kr = k.replace(\"model.{}.cv3.\".format(idx), \"model.{}.cv5.\".format(idx+7))\n",
|
398 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
399 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
400 |
+
" print(k, \"perfectly matched!!\")\n",
|
401 |
+
" elif \"model.{}.dfl.\".format(idx) in k:\n",
|
402 |
+
" kr = k.replace(\"model.{}.dfl.\".format(idx), \"model.{}.dfl2.\".format(idx+7))\n",
|
403 |
+
" model.state_dict()[k] -= model.state_dict()[k]\n",
|
404 |
+
" model.state_dict()[k] += ckpt['model'].state_dict()[kr]\n",
|
405 |
+
" print(k, \"perfectly matched!!\")\n",
|
406 |
+
"_ = model.eval()"
|
407 |
+
]
|
408 |
+
},
|
409 |
+
{
|
410 |
+
"cell_type": "code",
|
411 |
+
"execution_count": null,
|
412 |
+
"id": "27bc1869",
|
413 |
+
"metadata": {},
|
414 |
+
"outputs": [],
|
415 |
+
"source": [
|
416 |
+
"m_ckpt = {'model': model.half(),\n",
|
417 |
+
" 'optimizer': None,\n",
|
418 |
+
" 'best_fitness': None,\n",
|
419 |
+
" 'ema': None,\n",
|
420 |
+
" 'updates': None,\n",
|
421 |
+
" 'opt': None,\n",
|
422 |
+
" 'git': None,\n",
|
423 |
+
" 'date': None,\n",
|
424 |
+
" 'epoch': -1}\n",
|
425 |
+
"torch.save(m_ckpt, \"./yolov9-e-converted.pt\")"
|
426 |
+
]
|
427 |
+
}
|
428 |
+
],
|
429 |
+
"metadata": {
|
430 |
+
"kernelspec": {
|
431 |
+
"display_name": "Python 3 (ipykernel)",
|
432 |
+
"language": "python",
|
433 |
+
"name": "python3"
|
434 |
+
},
|
435 |
+
"language_info": {
|
436 |
+
"codemirror_mode": {
|
437 |
+
"name": "ipython",
|
438 |
+
"version": 3
|
439 |
+
},
|
440 |
+
"file_extension": ".py",
|
441 |
+
"mimetype": "text/x-python",
|
442 |
+
"name": "python",
|
443 |
+
"nbconvert_exporter": "python",
|
444 |
+
"pygments_lexer": "ipython3",
|
445 |
+
"version": "3.8.12"
|
446 |
+
}
|
447 |
+
},
|
448 |
+
"nbformat": 4,
|
449 |
+
"nbformat_minor": 5
|
450 |
+
}
|
train.py
ADDED
@@ -0,0 +1,634 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import math
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import sys
|
6 |
+
import time
|
7 |
+
from copy import deepcopy
|
8 |
+
from datetime import datetime
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
import numpy as np
|
12 |
+
import torch
|
13 |
+
import torch.distributed as dist
|
14 |
+
import torch.nn as nn
|
15 |
+
import yaml
|
16 |
+
from torch.optim import lr_scheduler
|
17 |
+
from tqdm import tqdm
|
18 |
+
|
19 |
+
FILE = Path(__file__).resolve()
|
20 |
+
ROOT = FILE.parents[0] # root directory
|
21 |
+
if str(ROOT) not in sys.path:
|
22 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
23 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
24 |
+
|
25 |
+
import val as validate # for end-of-epoch mAP
|
26 |
+
from models.experimental import attempt_load
|
27 |
+
from models.yolo import Model
|
28 |
+
from utils.autoanchor import check_anchors
|
29 |
+
from utils.autobatch import check_train_batch_size
|
30 |
+
from utils.callbacks import Callbacks
|
31 |
+
from utils.dataloaders import create_dataloader
|
32 |
+
from utils.downloads import attempt_download, is_url
|
33 |
+
from utils.general import (LOGGER, TQDM_BAR_FORMAT, check_amp, check_dataset, check_file, check_img_size,
|
34 |
+
check_suffix, check_yaml, colorstr, get_latest_run, increment_path, init_seeds,
|
35 |
+
intersect_dicts, labels_to_class_weights, labels_to_image_weights, methods,
|
36 |
+
one_cycle, one_flat_cycle, print_args, print_mutation, strip_optimizer, yaml_save)
|
37 |
+
from utils.loggers import Loggers
|
38 |
+
from utils.loggers.comet.comet_utils import check_comet_resume
|
39 |
+
from utils.loss_tal import ComputeLoss
|
40 |
+
from utils.metrics import fitness
|
41 |
+
from utils.plots import plot_evolve
|
42 |
+
from utils.torch_utils import (EarlyStopping, ModelEMA, de_parallel, select_device, smart_DDP,
|
43 |
+
smart_optimizer, smart_resume, torch_distributed_zero_first)
|
44 |
+
|
45 |
+
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
|
46 |
+
RANK = int(os.getenv('RANK', -1))
|
47 |
+
WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))
|
48 |
+
GIT_INFO = None
|
49 |
+
|
50 |
+
|
51 |
+
def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictionary
|
52 |
+
save_dir, epochs, batch_size, weights, single_cls, evolve, data, cfg, resume, noval, nosave, workers, freeze = \
|
53 |
+
Path(opt.save_dir), opt.epochs, opt.batch_size, opt.weights, opt.single_cls, opt.evolve, opt.data, opt.cfg, \
|
54 |
+
opt.resume, opt.noval, opt.nosave, opt.workers, opt.freeze
|
55 |
+
callbacks.run('on_pretrain_routine_start')
|
56 |
+
|
57 |
+
# Directories
|
58 |
+
w = save_dir / 'weights' # weights dir
|
59 |
+
(w.parent if evolve else w).mkdir(parents=True, exist_ok=True) # make dir
|
60 |
+
last, best = w / 'last.pt', w / 'best.pt'
|
61 |
+
last_striped, best_striped = w / 'last_striped.pt', w / 'best_striped.pt'
|
62 |
+
|
63 |
+
# Hyperparameters
|
64 |
+
if isinstance(hyp, str):
|
65 |
+
with open(hyp, errors='ignore') as f:
|
66 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
67 |
+
LOGGER.info(colorstr('hyperparameters: ') + ', '.join(f'{k}={v}' for k, v in hyp.items()))
|
68 |
+
hyp['anchor_t'] = 5.0
|
69 |
+
opt.hyp = hyp.copy() # for saving hyps to checkpoints
|
70 |
+
|
71 |
+
# Save run settings
|
72 |
+
if not evolve:
|
73 |
+
yaml_save(save_dir / 'hyp.yaml', hyp)
|
74 |
+
yaml_save(save_dir / 'opt.yaml', vars(opt))
|
75 |
+
|
76 |
+
# Loggers
|
77 |
+
data_dict = None
|
78 |
+
if RANK in {-1, 0}:
|
79 |
+
loggers = Loggers(save_dir, weights, opt, hyp, LOGGER) # loggers instance
|
80 |
+
|
81 |
+
# Register actions
|
82 |
+
for k in methods(loggers):
|
83 |
+
callbacks.register_action(k, callback=getattr(loggers, k))
|
84 |
+
|
85 |
+
# Process custom dataset artifact link
|
86 |
+
data_dict = loggers.remote_dataset
|
87 |
+
if resume: # If resuming runs from remote artifact
|
88 |
+
weights, epochs, hyp, batch_size = opt.weights, opt.epochs, opt.hyp, opt.batch_size
|
89 |
+
|
90 |
+
# Config
|
91 |
+
plots = not evolve and not opt.noplots # create plots
|
92 |
+
cuda = device.type != 'cpu'
|
93 |
+
init_seeds(opt.seed + 1 + RANK, deterministic=True)
|
94 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
95 |
+
data_dict = data_dict or check_dataset(data) # check if None
|
96 |
+
train_path, val_path = data_dict['train'], data_dict['val']
|
97 |
+
nc = 1 if single_cls else int(data_dict['nc']) # number of classes
|
98 |
+
names = {0: 'item'} if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
|
99 |
+
#is_coco = isinstance(val_path, str) and val_path.endswith('coco/val2017.txt') # COCO dataset
|
100 |
+
is_coco = isinstance(val_path, str) and val_path.endswith('val2017.txt') # COCO dataset
|
101 |
+
|
102 |
+
# Model
|
103 |
+
check_suffix(weights, '.pt') # check weights
|
104 |
+
pretrained = weights.endswith('.pt')
|
105 |
+
if pretrained:
|
106 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
107 |
+
weights = attempt_download(weights) # download if not found locally
|
108 |
+
ckpt = torch.load(weights, map_location='cpu') # load checkpoint to CPU to avoid CUDA memory leak
|
109 |
+
model = Model(cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
|
110 |
+
exclude = ['anchor'] if (cfg or hyp.get('anchors')) and not resume else [] # exclude keys
|
111 |
+
csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
|
112 |
+
csd = intersect_dicts(csd, model.state_dict(), exclude=exclude) # intersect
|
113 |
+
model.load_state_dict(csd, strict=False) # load
|
114 |
+
LOGGER.info(f'Transferred {len(csd)}/{len(model.state_dict())} items from {weights}') # report
|
115 |
+
else:
|
116 |
+
model = Model(cfg, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
|
117 |
+
amp = check_amp(model) # check AMP
|
118 |
+
|
119 |
+
# Freeze
|
120 |
+
freeze = [f'model.{x}.' for x in (freeze if len(freeze) > 1 else range(freeze[0]))] # layers to freeze
|
121 |
+
for k, v in model.named_parameters():
|
122 |
+
# v.requires_grad = True # train all layers TODO: uncomment this line as in master
|
123 |
+
# v.register_hook(lambda x: torch.nan_to_num(x)) # NaN to 0 (commented for erratic training results)
|
124 |
+
if any(x in k for x in freeze):
|
125 |
+
LOGGER.info(f'freezing {k}')
|
126 |
+
v.requires_grad = False
|
127 |
+
|
128 |
+
# Image size
|
129 |
+
gs = max(int(model.stride.max()), 32) # grid size (max stride)
|
130 |
+
imgsz = check_img_size(opt.imgsz, gs, floor=gs * 2) # verify imgsz is gs-multiple
|
131 |
+
|
132 |
+
# Batch size
|
133 |
+
if RANK == -1 and batch_size == -1: # single-GPU only, estimate best batch size
|
134 |
+
batch_size = check_train_batch_size(model, imgsz, amp)
|
135 |
+
loggers.on_params_update({"batch_size": batch_size})
|
136 |
+
|
137 |
+
# Optimizer
|
138 |
+
nbs = 64 # nominal batch size
|
139 |
+
accumulate = max(round(nbs / batch_size), 1) # accumulate loss before optimizing
|
140 |
+
hyp['weight_decay'] *= batch_size * accumulate / nbs # scale weight_decay
|
141 |
+
optimizer = smart_optimizer(model, opt.optimizer, hyp['lr0'], hyp['momentum'], hyp['weight_decay'])
|
142 |
+
|
143 |
+
# Scheduler
|
144 |
+
if opt.cos_lr:
|
145 |
+
lf = one_cycle(1, hyp['lrf'], epochs) # cosine 1->hyp['lrf']
|
146 |
+
elif opt.flat_cos_lr:
|
147 |
+
lf = one_flat_cycle(1, hyp['lrf'], epochs) # flat cosine 1->hyp['lrf']
|
148 |
+
elif opt.fixed_lr:
|
149 |
+
lf = lambda x: 1.0
|
150 |
+
else:
|
151 |
+
lf = lambda x: (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf'] # linear
|
152 |
+
|
153 |
+
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf)
|
154 |
+
# from utils.plots import plot_lr_scheduler; plot_lr_scheduler(optimizer, scheduler, epochs)
|
155 |
+
|
156 |
+
# EMA
|
157 |
+
ema = ModelEMA(model) if RANK in {-1, 0} else None
|
158 |
+
|
159 |
+
# Resume
|
160 |
+
best_fitness, start_epoch = 0.0, 0
|
161 |
+
if pretrained:
|
162 |
+
if resume:
|
163 |
+
best_fitness, start_epoch, epochs = smart_resume(ckpt, optimizer, ema, weights, epochs, resume)
|
164 |
+
del ckpt, csd
|
165 |
+
|
166 |
+
# DP mode
|
167 |
+
if cuda and RANK == -1 and torch.cuda.device_count() > 1:
|
168 |
+
LOGGER.warning('WARNING ⚠️ DP not recommended, use torch.distributed.run for best DDP Multi-GPU results.')
|
169 |
+
model = torch.nn.DataParallel(model)
|
170 |
+
|
171 |
+
# SyncBatchNorm
|
172 |
+
if opt.sync_bn and cuda and RANK != -1:
|
173 |
+
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model).to(device)
|
174 |
+
LOGGER.info('Using SyncBatchNorm()')
|
175 |
+
|
176 |
+
# Trainloader
|
177 |
+
train_loader, dataset = create_dataloader(train_path,
|
178 |
+
imgsz,
|
179 |
+
batch_size // WORLD_SIZE,
|
180 |
+
gs,
|
181 |
+
single_cls,
|
182 |
+
hyp=hyp,
|
183 |
+
augment=True,
|
184 |
+
cache=None if opt.cache == 'val' else opt.cache,
|
185 |
+
rect=opt.rect,
|
186 |
+
rank=LOCAL_RANK,
|
187 |
+
workers=workers,
|
188 |
+
image_weights=opt.image_weights,
|
189 |
+
close_mosaic=opt.close_mosaic != 0,
|
190 |
+
quad=opt.quad,
|
191 |
+
prefix=colorstr('train: '),
|
192 |
+
shuffle=True,
|
193 |
+
min_items=opt.min_items)
|
194 |
+
labels = np.concatenate(dataset.labels, 0)
|
195 |
+
mlc = int(labels[:, 0].max()) # max label class
|
196 |
+
assert mlc < nc, f'Label class {mlc} exceeds nc={nc} in {data}. Possible class labels are 0-{nc - 1}'
|
197 |
+
|
198 |
+
# Process 0
|
199 |
+
if RANK in {-1, 0}:
|
200 |
+
val_loader = create_dataloader(val_path,
|
201 |
+
imgsz,
|
202 |
+
batch_size // WORLD_SIZE * 2,
|
203 |
+
gs,
|
204 |
+
single_cls,
|
205 |
+
hyp=hyp,
|
206 |
+
cache=None if noval else opt.cache,
|
207 |
+
rect=True,
|
208 |
+
rank=-1,
|
209 |
+
workers=workers * 2,
|
210 |
+
pad=0.5,
|
211 |
+
prefix=colorstr('val: '))[0]
|
212 |
+
|
213 |
+
if not resume:
|
214 |
+
# if not opt.noautoanchor:
|
215 |
+
# check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz) # run AutoAnchor
|
216 |
+
model.half().float() # pre-reduce anchor precision
|
217 |
+
|
218 |
+
callbacks.run('on_pretrain_routine_end', labels, names)
|
219 |
+
|
220 |
+
# DDP mode
|
221 |
+
if cuda and RANK != -1:
|
222 |
+
model = smart_DDP(model)
|
223 |
+
|
224 |
+
# Model attributes
|
225 |
+
nl = de_parallel(model).model[-1].nl # number of detection layers (to scale hyps)
|
226 |
+
#hyp['box'] *= 3 / nl # scale to layers
|
227 |
+
#hyp['cls'] *= nc / 80 * 3 / nl # scale to classes and layers
|
228 |
+
#hyp['obj'] *= (imgsz / 640) ** 2 * 3 / nl # scale to image size and layers
|
229 |
+
hyp['label_smoothing'] = opt.label_smoothing
|
230 |
+
model.nc = nc # attach number of classes to model
|
231 |
+
model.hyp = hyp # attach hyperparameters to model
|
232 |
+
model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) * nc # attach class weights
|
233 |
+
model.names = names
|
234 |
+
|
235 |
+
# Start training
|
236 |
+
t0 = time.time()
|
237 |
+
nb = len(train_loader) # number of batches
|
238 |
+
nw = max(round(hyp['warmup_epochs'] * nb), 100) # number of warmup iterations, max(3 epochs, 100 iterations)
|
239 |
+
# nw = min(nw, (epochs - start_epoch) / 2 * nb) # limit warmup to < 1/2 of training
|
240 |
+
last_opt_step = -1
|
241 |
+
maps = np.zeros(nc) # mAP per class
|
242 |
+
results = (0, 0, 0, 0, 0, 0, 0) # P, R, [email protected], [email protected], val_loss(box, obj, cls)
|
243 |
+
scheduler.last_epoch = start_epoch - 1 # do not move
|
244 |
+
scaler = torch.cuda.amp.GradScaler(enabled=amp)
|
245 |
+
stopper, stop = EarlyStopping(patience=opt.patience), False
|
246 |
+
compute_loss = ComputeLoss(model) # init loss class
|
247 |
+
callbacks.run('on_train_start')
|
248 |
+
LOGGER.info(f'Image sizes {imgsz} train, {imgsz} val\n'
|
249 |
+
f'Using {train_loader.num_workers * WORLD_SIZE} dataloader workers\n'
|
250 |
+
f"Logging results to {colorstr('bold', save_dir)}\n"
|
251 |
+
f'Starting training for {epochs} epochs...')
|
252 |
+
for epoch in range(start_epoch, epochs): # epoch ------------------------------------------------------------------
|
253 |
+
callbacks.run('on_train_epoch_start')
|
254 |
+
model.train()
|
255 |
+
|
256 |
+
# Update image weights (optional, single-GPU only)
|
257 |
+
if opt.image_weights:
|
258 |
+
cw = model.class_weights.cpu().numpy() * (1 - maps) ** 2 / nc # class weights
|
259 |
+
iw = labels_to_image_weights(dataset.labels, nc=nc, class_weights=cw) # image weights
|
260 |
+
dataset.indices = random.choices(range(dataset.n), weights=iw, k=dataset.n) # rand weighted idx
|
261 |
+
if epoch == (epochs - opt.close_mosaic):
|
262 |
+
LOGGER.info("Closing dataloader mosaic")
|
263 |
+
dataset.mosaic = False
|
264 |
+
|
265 |
+
# Update mosaic border (optional)
|
266 |
+
# b = int(random.uniform(0.25 * imgsz, 0.75 * imgsz + gs) // gs * gs)
|
267 |
+
# dataset.mosaic_border = [b - imgsz, -b] # height, width borders
|
268 |
+
|
269 |
+
mloss = torch.zeros(3, device=device) # mean losses
|
270 |
+
if RANK != -1:
|
271 |
+
train_loader.sampler.set_epoch(epoch)
|
272 |
+
pbar = enumerate(train_loader)
|
273 |
+
LOGGER.info(('\n' + '%11s' * 7) % ('Epoch', 'GPU_mem', 'box_loss', 'cls_loss', 'dfl_loss', 'Instances', 'Size'))
|
274 |
+
if RANK in {-1, 0}:
|
275 |
+
pbar = tqdm(pbar, total=nb, bar_format=TQDM_BAR_FORMAT) # progress bar
|
276 |
+
optimizer.zero_grad()
|
277 |
+
for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
|
278 |
+
callbacks.run('on_train_batch_start')
|
279 |
+
ni = i + nb * epoch # number integrated batches (since train start)
|
280 |
+
imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0
|
281 |
+
|
282 |
+
# Warmup
|
283 |
+
if ni <= nw:
|
284 |
+
xi = [0, nw] # x interp
|
285 |
+
# compute_loss.gr = np.interp(ni, xi, [0.0, 1.0]) # iou loss ratio (obj_loss = 1.0 or iou)
|
286 |
+
accumulate = max(1, np.interp(ni, xi, [1, nbs / batch_size]).round())
|
287 |
+
for j, x in enumerate(optimizer.param_groups):
|
288 |
+
# bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0
|
289 |
+
x['lr'] = np.interp(ni, xi, [hyp['warmup_bias_lr'] if j == 0 else 0.0, x['initial_lr'] * lf(epoch)])
|
290 |
+
if 'momentum' in x:
|
291 |
+
x['momentum'] = np.interp(ni, xi, [hyp['warmup_momentum'], hyp['momentum']])
|
292 |
+
|
293 |
+
# Multi-scale
|
294 |
+
if opt.multi_scale:
|
295 |
+
sz = random.randrange(imgsz * 0.5, imgsz * 1.5 + gs) // gs * gs # size
|
296 |
+
sf = sz / max(imgs.shape[2:]) # scale factor
|
297 |
+
if sf != 1:
|
298 |
+
ns = [math.ceil(x * sf / gs) * gs for x in imgs.shape[2:]] # new shape (stretched to gs-multiple)
|
299 |
+
imgs = nn.functional.interpolate(imgs, size=ns, mode='bilinear', align_corners=False)
|
300 |
+
|
301 |
+
# Forward
|
302 |
+
with torch.cuda.amp.autocast(amp):
|
303 |
+
pred = model(imgs) # forward
|
304 |
+
loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size
|
305 |
+
if RANK != -1:
|
306 |
+
loss *= WORLD_SIZE # gradient averaged between devices in DDP mode
|
307 |
+
if opt.quad:
|
308 |
+
loss *= 4.
|
309 |
+
|
310 |
+
# Backward
|
311 |
+
scaler.scale(loss).backward()
|
312 |
+
|
313 |
+
# Optimize - https://pytorch.org/docs/master/notes/amp_examples.html
|
314 |
+
if ni - last_opt_step >= accumulate:
|
315 |
+
scaler.unscale_(optimizer) # unscale gradients
|
316 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=10.0) # clip gradients
|
317 |
+
scaler.step(optimizer) # optimizer.step
|
318 |
+
scaler.update()
|
319 |
+
optimizer.zero_grad()
|
320 |
+
if ema:
|
321 |
+
ema.update(model)
|
322 |
+
last_opt_step = ni
|
323 |
+
|
324 |
+
# Log
|
325 |
+
if RANK in {-1, 0}:
|
326 |
+
mloss = (mloss * i + loss_items) / (i + 1) # update mean losses
|
327 |
+
mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB)
|
328 |
+
pbar.set_description(('%11s' * 2 + '%11.4g' * 5) %
|
329 |
+
(f'{epoch}/{epochs - 1}', mem, *mloss, targets.shape[0], imgs.shape[-1]))
|
330 |
+
callbacks.run('on_train_batch_end', model, ni, imgs, targets, paths, list(mloss))
|
331 |
+
if callbacks.stop_training:
|
332 |
+
return
|
333 |
+
# end batch ------------------------------------------------------------------------------------------------
|
334 |
+
|
335 |
+
# Scheduler
|
336 |
+
lr = [x['lr'] for x in optimizer.param_groups] # for loggers
|
337 |
+
scheduler.step()
|
338 |
+
|
339 |
+
if RANK in {-1, 0}:
|
340 |
+
# mAP
|
341 |
+
callbacks.run('on_train_epoch_end', epoch=epoch)
|
342 |
+
ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'names', 'stride', 'class_weights'])
|
343 |
+
final_epoch = (epoch + 1 == epochs) or stopper.possible_stop
|
344 |
+
if not noval or final_epoch: # Calculate mAP
|
345 |
+
results, maps, _ = validate.run(data_dict,
|
346 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
347 |
+
imgsz=imgsz,
|
348 |
+
half=amp,
|
349 |
+
model=ema.ema,
|
350 |
+
single_cls=single_cls,
|
351 |
+
dataloader=val_loader,
|
352 |
+
save_dir=save_dir,
|
353 |
+
plots=False,
|
354 |
+
callbacks=callbacks,
|
355 |
+
compute_loss=compute_loss)
|
356 |
+
|
357 |
+
# Update best mAP
|
358 |
+
fi = fitness(np.array(results).reshape(1, -1)) # weighted combination of [P, R, [email protected], [email protected]]
|
359 |
+
stop = stopper(epoch=epoch, fitness=fi) # early stop check
|
360 |
+
if fi > best_fitness:
|
361 |
+
best_fitness = fi
|
362 |
+
log_vals = list(mloss) + list(results) + lr
|
363 |
+
callbacks.run('on_fit_epoch_end', log_vals, epoch, best_fitness, fi)
|
364 |
+
|
365 |
+
# Save model
|
366 |
+
if (not nosave) or (final_epoch and not evolve): # if save
|
367 |
+
ckpt = {
|
368 |
+
'epoch': epoch,
|
369 |
+
'best_fitness': best_fitness,
|
370 |
+
'model': deepcopy(de_parallel(model)).half(),
|
371 |
+
'ema': deepcopy(ema.ema).half(),
|
372 |
+
'updates': ema.updates,
|
373 |
+
'optimizer': optimizer.state_dict(),
|
374 |
+
'opt': vars(opt),
|
375 |
+
'git': GIT_INFO, # {remote, branch, commit} if a git repo
|
376 |
+
'date': datetime.now().isoformat()}
|
377 |
+
|
378 |
+
# Save last, best and delete
|
379 |
+
torch.save(ckpt, last)
|
380 |
+
if best_fitness == fi:
|
381 |
+
torch.save(ckpt, best)
|
382 |
+
if opt.save_period > 0 and epoch % opt.save_period == 0:
|
383 |
+
torch.save(ckpt, w / f'epoch{epoch}.pt')
|
384 |
+
del ckpt
|
385 |
+
callbacks.run('on_model_save', last, epoch, final_epoch, best_fitness, fi)
|
386 |
+
|
387 |
+
# EarlyStopping
|
388 |
+
if RANK != -1: # if DDP training
|
389 |
+
broadcast_list = [stop if RANK == 0 else None]
|
390 |
+
dist.broadcast_object_list(broadcast_list, 0) # broadcast 'stop' to all ranks
|
391 |
+
if RANK != 0:
|
392 |
+
stop = broadcast_list[0]
|
393 |
+
if stop:
|
394 |
+
break # must break all DDP ranks
|
395 |
+
|
396 |
+
# end epoch ----------------------------------------------------------------------------------------------------
|
397 |
+
# end training -----------------------------------------------------------------------------------------------------
|
398 |
+
if RANK in {-1, 0}:
|
399 |
+
LOGGER.info(f'\n{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.')
|
400 |
+
for f in last, best:
|
401 |
+
if f.exists():
|
402 |
+
if f is last:
|
403 |
+
strip_optimizer(f, last_striped) # strip optimizers
|
404 |
+
else:
|
405 |
+
strip_optimizer(f, best_striped) # strip optimizers
|
406 |
+
if f is best:
|
407 |
+
LOGGER.info(f'\nValidating {f}...')
|
408 |
+
results, _, _ = validate.run(
|
409 |
+
data_dict,
|
410 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
411 |
+
imgsz=imgsz,
|
412 |
+
model=attempt_load(f, device).half(),
|
413 |
+
single_cls=single_cls,
|
414 |
+
dataloader=val_loader,
|
415 |
+
save_dir=save_dir,
|
416 |
+
save_json=is_coco,
|
417 |
+
verbose=True,
|
418 |
+
plots=plots,
|
419 |
+
callbacks=callbacks,
|
420 |
+
compute_loss=compute_loss) # val best model with plots
|
421 |
+
if is_coco:
|
422 |
+
callbacks.run('on_fit_epoch_end', list(mloss) + list(results) + lr, epoch, best_fitness, fi)
|
423 |
+
|
424 |
+
callbacks.run('on_train_end', last, best, epoch, results)
|
425 |
+
|
426 |
+
torch.cuda.empty_cache()
|
427 |
+
return results
|
428 |
+
|
429 |
+
|
430 |
+
def parse_opt(known=False):
|
431 |
+
parser = argparse.ArgumentParser()
|
432 |
+
# parser.add_argument('--weights', type=str, default=ROOT / 'yolo.pt', help='initial weights path')
|
433 |
+
# parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
|
434 |
+
parser.add_argument('--weights', type=str, default='', help='initial weights path')
|
435 |
+
parser.add_argument('--cfg', type=str, default='yolo.yaml', help='model.yaml path')
|
436 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco128.yaml', help='dataset.yaml path')
|
437 |
+
parser.add_argument('--hyp', type=str, default=ROOT / 'data/hyps/hyp.scratch-low.yaml', help='hyperparameters path')
|
438 |
+
parser.add_argument('--epochs', type=int, default=100, help='total training epochs')
|
439 |
+
parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs, -1 for autobatch')
|
440 |
+
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='train, val image size (pixels)')
|
441 |
+
parser.add_argument('--rect', action='store_true', help='rectangular training')
|
442 |
+
parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')
|
443 |
+
parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
|
444 |
+
parser.add_argument('--noval', action='store_true', help='only validate final epoch')
|
445 |
+
parser.add_argument('--noautoanchor', action='store_true', help='disable AutoAnchor')
|
446 |
+
parser.add_argument('--noplots', action='store_true', help='save no plot files')
|
447 |
+
parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations')
|
448 |
+
parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
|
449 |
+
parser.add_argument('--cache', type=str, nargs='?', const='ram', help='image --cache ram/disk')
|
450 |
+
parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
|
451 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
452 |
+
parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
|
453 |
+
parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
|
454 |
+
parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'AdamW', 'LION'], default='SGD', help='optimizer')
|
455 |
+
parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
|
456 |
+
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
|
457 |
+
parser.add_argument('--project', default=ROOT / 'runs/train', help='save to project/name')
|
458 |
+
parser.add_argument('--name', default='exp', help='save to project/name')
|
459 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
460 |
+
parser.add_argument('--quad', action='store_true', help='quad dataloader')
|
461 |
+
parser.add_argument('--cos-lr', action='store_true', help='cosine LR scheduler')
|
462 |
+
parser.add_argument('--flat-cos-lr', action='store_true', help='flat cosine LR scheduler')
|
463 |
+
parser.add_argument('--fixed-lr', action='store_true', help='fixed LR scheduler')
|
464 |
+
parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
|
465 |
+
parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)')
|
466 |
+
parser.add_argument('--freeze', nargs='+', type=int, default=[0], help='Freeze layers: backbone=10, first3=0 1 2')
|
467 |
+
parser.add_argument('--save-period', type=int, default=-1, help='Save checkpoint every x epochs (disabled if < 1)')
|
468 |
+
parser.add_argument('--seed', type=int, default=0, help='Global training seed')
|
469 |
+
parser.add_argument('--local_rank', type=int, default=-1, help='Automatic DDP Multi-GPU argument, do not modify')
|
470 |
+
parser.add_argument('--min-items', type=int, default=0, help='Experimental')
|
471 |
+
parser.add_argument('--close-mosaic', type=int, default=0, help='Experimental')
|
472 |
+
|
473 |
+
# Logger arguments
|
474 |
+
parser.add_argument('--entity', default=None, help='Entity')
|
475 |
+
parser.add_argument('--upload_dataset', nargs='?', const=True, default=False, help='Upload data, "val" option')
|
476 |
+
parser.add_argument('--bbox_interval', type=int, default=-1, help='Set bounding-box image logging interval')
|
477 |
+
parser.add_argument('--artifact_alias', type=str, default='latest', help='Version of dataset artifact to use')
|
478 |
+
|
479 |
+
return parser.parse_known_args()[0] if known else parser.parse_args()
|
480 |
+
|
481 |
+
|
482 |
+
def main(opt, callbacks=Callbacks()):
|
483 |
+
# Checks
|
484 |
+
if RANK in {-1, 0}:
|
485 |
+
print_args(vars(opt))
|
486 |
+
|
487 |
+
# Resume (from specified or most recent last.pt)
|
488 |
+
if opt.resume and not check_comet_resume(opt) and not opt.evolve:
|
489 |
+
last = Path(check_file(opt.resume) if isinstance(opt.resume, str) else get_latest_run())
|
490 |
+
opt_yaml = last.parent.parent / 'opt.yaml' # train options yaml
|
491 |
+
opt_data = opt.data # original dataset
|
492 |
+
if opt_yaml.is_file():
|
493 |
+
with open(opt_yaml, errors='ignore') as f:
|
494 |
+
d = yaml.safe_load(f)
|
495 |
+
else:
|
496 |
+
d = torch.load(last, map_location='cpu')['opt']
|
497 |
+
opt = argparse.Namespace(**d) # replace
|
498 |
+
opt.cfg, opt.weights, opt.resume = '', str(last), True # reinstate
|
499 |
+
if is_url(opt_data):
|
500 |
+
opt.data = check_file(opt_data) # avoid HUB resume auth timeout
|
501 |
+
else:
|
502 |
+
opt.data, opt.cfg, opt.hyp, opt.weights, opt.project = \
|
503 |
+
check_file(opt.data), check_yaml(opt.cfg), check_yaml(opt.hyp), str(opt.weights), str(opt.project) # checks
|
504 |
+
assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified'
|
505 |
+
if opt.evolve:
|
506 |
+
if opt.project == str(ROOT / 'runs/train'): # if default project name, rename to runs/evolve
|
507 |
+
opt.project = str(ROOT / 'runs/evolve')
|
508 |
+
opt.exist_ok, opt.resume = opt.resume, False # pass resume to exist_ok and disable resume
|
509 |
+
if opt.name == 'cfg':
|
510 |
+
opt.name = Path(opt.cfg).stem # use model.yaml as name
|
511 |
+
opt.save_dir = str(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok))
|
512 |
+
|
513 |
+
# DDP mode
|
514 |
+
device = select_device(opt.device, batch_size=opt.batch_size)
|
515 |
+
if LOCAL_RANK != -1:
|
516 |
+
msg = 'is not compatible with YOLO Multi-GPU DDP training'
|
517 |
+
assert not opt.image_weights, f'--image-weights {msg}'
|
518 |
+
assert not opt.evolve, f'--evolve {msg}'
|
519 |
+
assert opt.batch_size != -1, f'AutoBatch with --batch-size -1 {msg}, please pass a valid --batch-size'
|
520 |
+
assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'
|
521 |
+
assert torch.cuda.device_count() > LOCAL_RANK, 'insufficient CUDA devices for DDP command'
|
522 |
+
torch.cuda.set_device(LOCAL_RANK)
|
523 |
+
device = torch.device('cuda', LOCAL_RANK)
|
524 |
+
dist.init_process_group(backend="nccl" if dist.is_nccl_available() else "gloo")
|
525 |
+
|
526 |
+
# Train
|
527 |
+
if not opt.evolve:
|
528 |
+
train(opt.hyp, opt, device, callbacks)
|
529 |
+
|
530 |
+
# Evolve hyperparameters (optional)
|
531 |
+
else:
|
532 |
+
# Hyperparameter evolution metadata (mutation scale 0-1, lower_limit, upper_limit)
|
533 |
+
meta = {
|
534 |
+
'lr0': (1, 1e-5, 1e-1), # initial learning rate (SGD=1E-2, Adam=1E-3)
|
535 |
+
'lrf': (1, 0.01, 1.0), # final OneCycleLR learning rate (lr0 * lrf)
|
536 |
+
'momentum': (0.3, 0.6, 0.98), # SGD momentum/Adam beta1
|
537 |
+
'weight_decay': (1, 0.0, 0.001), # optimizer weight decay
|
538 |
+
'warmup_epochs': (1, 0.0, 5.0), # warmup epochs (fractions ok)
|
539 |
+
'warmup_momentum': (1, 0.0, 0.95), # warmup initial momentum
|
540 |
+
'warmup_bias_lr': (1, 0.0, 0.2), # warmup initial bias lr
|
541 |
+
'box': (1, 0.02, 0.2), # box loss gain
|
542 |
+
'cls': (1, 0.2, 4.0), # cls loss gain
|
543 |
+
'cls_pw': (1, 0.5, 2.0), # cls BCELoss positive_weight
|
544 |
+
'obj': (1, 0.2, 4.0), # obj loss gain (scale with pixels)
|
545 |
+
'obj_pw': (1, 0.5, 2.0), # obj BCELoss positive_weight
|
546 |
+
'iou_t': (0, 0.1, 0.7), # IoU training threshold
|
547 |
+
'anchor_t': (1, 2.0, 8.0), # anchor-multiple threshold
|
548 |
+
'anchors': (2, 2.0, 10.0), # anchors per output grid (0 to ignore)
|
549 |
+
'fl_gamma': (0, 0.0, 2.0), # focal loss gamma (efficientDet default gamma=1.5)
|
550 |
+
'hsv_h': (1, 0.0, 0.1), # image HSV-Hue augmentation (fraction)
|
551 |
+
'hsv_s': (1, 0.0, 0.9), # image HSV-Saturation augmentation (fraction)
|
552 |
+
'hsv_v': (1, 0.0, 0.9), # image HSV-Value augmentation (fraction)
|
553 |
+
'degrees': (1, 0.0, 45.0), # image rotation (+/- deg)
|
554 |
+
'translate': (1, 0.0, 0.9), # image translation (+/- fraction)
|
555 |
+
'scale': (1, 0.0, 0.9), # image scale (+/- gain)
|
556 |
+
'shear': (1, 0.0, 10.0), # image shear (+/- deg)
|
557 |
+
'perspective': (0, 0.0, 0.001), # image perspective (+/- fraction), range 0-0.001
|
558 |
+
'flipud': (1, 0.0, 1.0), # image flip up-down (probability)
|
559 |
+
'fliplr': (0, 0.0, 1.0), # image flip left-right (probability)
|
560 |
+
'mosaic': (1, 0.0, 1.0), # image mixup (probability)
|
561 |
+
'mixup': (1, 0.0, 1.0), # image mixup (probability)
|
562 |
+
'copy_paste': (1, 0.0, 1.0)} # segment copy-paste (probability)
|
563 |
+
|
564 |
+
with open(opt.hyp, errors='ignore') as f:
|
565 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
566 |
+
if 'anchors' not in hyp: # anchors commented in hyp.yaml
|
567 |
+
hyp['anchors'] = 3
|
568 |
+
if opt.noautoanchor:
|
569 |
+
del hyp['anchors'], meta['anchors']
|
570 |
+
opt.noval, opt.nosave, save_dir = True, True, Path(opt.save_dir) # only val/save final epoch
|
571 |
+
# ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
|
572 |
+
evolve_yaml, evolve_csv = save_dir / 'hyp_evolve.yaml', save_dir / 'evolve.csv'
|
573 |
+
if opt.bucket:
|
574 |
+
os.system(f'gsutil cp gs://{opt.bucket}/evolve.csv {evolve_csv}') # download evolve.csv if exists
|
575 |
+
|
576 |
+
for _ in range(opt.evolve): # generations to evolve
|
577 |
+
if evolve_csv.exists(): # if evolve.csv exists: select best hyps and mutate
|
578 |
+
# Select parent(s)
|
579 |
+
parent = 'single' # parent selection method: 'single' or 'weighted'
|
580 |
+
x = np.loadtxt(evolve_csv, ndmin=2, delimiter=',', skiprows=1)
|
581 |
+
n = min(5, len(x)) # number of previous results to consider
|
582 |
+
x = x[np.argsort(-fitness(x))][:n] # top n mutations
|
583 |
+
w = fitness(x) - fitness(x).min() + 1E-6 # weights (sum > 0)
|
584 |
+
if parent == 'single' or len(x) == 1:
|
585 |
+
# x = x[random.randint(0, n - 1)] # random selection
|
586 |
+
x = x[random.choices(range(n), weights=w)[0]] # weighted selection
|
587 |
+
elif parent == 'weighted':
|
588 |
+
x = (x * w.reshape(n, 1)).sum(0) / w.sum() # weighted combination
|
589 |
+
|
590 |
+
# Mutate
|
591 |
+
mp, s = 0.8, 0.2 # mutation probability, sigma
|
592 |
+
npr = np.random
|
593 |
+
npr.seed(int(time.time()))
|
594 |
+
g = np.array([meta[k][0] for k in hyp.keys()]) # gains 0-1
|
595 |
+
ng = len(meta)
|
596 |
+
v = np.ones(ng)
|
597 |
+
while all(v == 1): # mutate until a change occurs (prevent duplicates)
|
598 |
+
v = (g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1).clip(0.3, 3.0)
|
599 |
+
for i, k in enumerate(hyp.keys()): # plt.hist(v.ravel(), 300)
|
600 |
+
hyp[k] = float(x[i + 7] * v[i]) # mutate
|
601 |
+
|
602 |
+
# Constrain to limits
|
603 |
+
for k, v in meta.items():
|
604 |
+
hyp[k] = max(hyp[k], v[1]) # lower limit
|
605 |
+
hyp[k] = min(hyp[k], v[2]) # upper limit
|
606 |
+
hyp[k] = round(hyp[k], 5) # significant digits
|
607 |
+
|
608 |
+
# Train mutation
|
609 |
+
results = train(hyp.copy(), opt, device, callbacks)
|
610 |
+
callbacks = Callbacks()
|
611 |
+
# Write mutation results
|
612 |
+
keys = ('metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95', 'val/box_loss',
|
613 |
+
'val/obj_loss', 'val/cls_loss')
|
614 |
+
print_mutation(keys, results, hyp.copy(), save_dir, opt.bucket)
|
615 |
+
|
616 |
+
# Plot results
|
617 |
+
plot_evolve(evolve_csv)
|
618 |
+
LOGGER.info(f'Hyperparameter evolution finished {opt.evolve} generations\n'
|
619 |
+
f"Results saved to {colorstr('bold', save_dir)}\n"
|
620 |
+
f'Usage example: $ python train.py --hyp {evolve_yaml}')
|
621 |
+
|
622 |
+
|
623 |
+
def run(**kwargs):
|
624 |
+
# Usage: import train; train.run(data='coco128.yaml', imgsz=320, weights='yolo.pt')
|
625 |
+
opt = parse_opt(True)
|
626 |
+
for k, v in kwargs.items():
|
627 |
+
setattr(opt, k, v)
|
628 |
+
main(opt)
|
629 |
+
return opt
|
630 |
+
|
631 |
+
|
632 |
+
if __name__ == "__main__":
|
633 |
+
opt = parse_opt()
|
634 |
+
main(opt)
|
train_dual.py
ADDED
@@ -0,0 +1,644 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import math
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import sys
|
6 |
+
import time
|
7 |
+
from copy import deepcopy
|
8 |
+
from datetime import datetime
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
import numpy as np
|
12 |
+
import torch
|
13 |
+
import torch.distributed as dist
|
14 |
+
import torch.nn as nn
|
15 |
+
import yaml
|
16 |
+
from torch.optim import lr_scheduler
|
17 |
+
from tqdm import tqdm
|
18 |
+
|
19 |
+
FILE = Path(__file__).resolve()
|
20 |
+
ROOT = FILE.parents[0] # YOLO root directory
|
21 |
+
if str(ROOT) not in sys.path:
|
22 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
23 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
24 |
+
|
25 |
+
import val_dual as validate # for end-of-epoch mAP
|
26 |
+
from models.experimental import attempt_load
|
27 |
+
from models.yolo import Model
|
28 |
+
from utils.autoanchor import check_anchors
|
29 |
+
from utils.autobatch import check_train_batch_size
|
30 |
+
from utils.callbacks import Callbacks
|
31 |
+
from utils.dataloaders import create_dataloader
|
32 |
+
from utils.downloads import attempt_download, is_url
|
33 |
+
from utils.general import (LOGGER, TQDM_BAR_FORMAT, check_amp, check_dataset, check_file, check_git_info,
|
34 |
+
check_git_status, check_img_size, check_requirements, check_suffix, check_yaml, colorstr,
|
35 |
+
get_latest_run, increment_path, init_seeds, intersect_dicts, labels_to_class_weights,
|
36 |
+
labels_to_image_weights, methods, one_cycle, print_args, print_mutation, strip_optimizer,
|
37 |
+
yaml_save, one_flat_cycle)
|
38 |
+
from utils.loggers import Loggers
|
39 |
+
from utils.loggers.comet.comet_utils import check_comet_resume
|
40 |
+
from utils.loss_tal_dual import ComputeLoss
|
41 |
+
#from utils.loss_tal_dual import ComputeLossLH as ComputeLoss
|
42 |
+
#from utils.loss_tal_dual import ComputeLossLHCF as ComputeLoss
|
43 |
+
from utils.metrics import fitness
|
44 |
+
from utils.plots import plot_evolve
|
45 |
+
from utils.torch_utils import (EarlyStopping, ModelEMA, de_parallel, select_device, smart_DDP, smart_optimizer,
|
46 |
+
smart_resume, torch_distributed_zero_first)
|
47 |
+
|
48 |
+
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
|
49 |
+
RANK = int(os.getenv('RANK', -1))
|
50 |
+
WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))
|
51 |
+
GIT_INFO = None#check_git_info()
|
52 |
+
|
53 |
+
|
54 |
+
def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictionary
|
55 |
+
save_dir, epochs, batch_size, weights, single_cls, evolve, data, cfg, resume, noval, nosave, workers, freeze = \
|
56 |
+
Path(opt.save_dir), opt.epochs, opt.batch_size, opt.weights, opt.single_cls, opt.evolve, opt.data, opt.cfg, \
|
57 |
+
opt.resume, opt.noval, opt.nosave, opt.workers, opt.freeze
|
58 |
+
callbacks.run('on_pretrain_routine_start')
|
59 |
+
|
60 |
+
# Directories
|
61 |
+
w = save_dir / 'weights' # weights dir
|
62 |
+
(w.parent if evolve else w).mkdir(parents=True, exist_ok=True) # make dir
|
63 |
+
last, best = w / 'last.pt', w / 'best.pt'
|
64 |
+
|
65 |
+
# Hyperparameters
|
66 |
+
if isinstance(hyp, str):
|
67 |
+
with open(hyp, errors='ignore') as f:
|
68 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
69 |
+
LOGGER.info(colorstr('hyperparameters: ') + ', '.join(f'{k}={v}' for k, v in hyp.items()))
|
70 |
+
hyp['anchor_t'] = 5.0
|
71 |
+
opt.hyp = hyp.copy() # for saving hyps to checkpoints
|
72 |
+
|
73 |
+
# Save run settings
|
74 |
+
if not evolve:
|
75 |
+
yaml_save(save_dir / 'hyp.yaml', hyp)
|
76 |
+
yaml_save(save_dir / 'opt.yaml', vars(opt))
|
77 |
+
|
78 |
+
# Loggers
|
79 |
+
data_dict = None
|
80 |
+
if RANK in {-1, 0}:
|
81 |
+
loggers = Loggers(save_dir, weights, opt, hyp, LOGGER) # loggers instance
|
82 |
+
|
83 |
+
# Register actions
|
84 |
+
for k in methods(loggers):
|
85 |
+
callbacks.register_action(k, callback=getattr(loggers, k))
|
86 |
+
|
87 |
+
# Process custom dataset artifact link
|
88 |
+
data_dict = loggers.remote_dataset
|
89 |
+
if resume: # If resuming runs from remote artifact
|
90 |
+
weights, epochs, hyp, batch_size = opt.weights, opt.epochs, opt.hyp, opt.batch_size
|
91 |
+
|
92 |
+
# Config
|
93 |
+
plots = not evolve and not opt.noplots # create plots
|
94 |
+
cuda = device.type != 'cpu'
|
95 |
+
init_seeds(opt.seed + 1 + RANK, deterministic=True)
|
96 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
97 |
+
data_dict = data_dict or check_dataset(data) # check if None
|
98 |
+
train_path, val_path = data_dict['train'], data_dict['val']
|
99 |
+
nc = 1 if single_cls else int(data_dict['nc']) # number of classes
|
100 |
+
names = {0: 'item'} if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
|
101 |
+
#is_coco = isinstance(val_path, str) and val_path.endswith('coco/val2017.txt') # COCO dataset
|
102 |
+
is_coco = isinstance(val_path, str) and val_path.endswith('val2017.txt') # COCO dataset
|
103 |
+
|
104 |
+
# Model
|
105 |
+
check_suffix(weights, '.pt') # check weights
|
106 |
+
pretrained = weights.endswith('.pt')
|
107 |
+
if pretrained:
|
108 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
109 |
+
weights = attempt_download(weights) # download if not found locally
|
110 |
+
ckpt = torch.load(weights, map_location='cpu') # load checkpoint to CPU to avoid CUDA memory leak
|
111 |
+
model = Model(cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
|
112 |
+
exclude = ['anchor'] if (cfg or hyp.get('anchors')) and not resume else [] # exclude keys
|
113 |
+
csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
|
114 |
+
csd = intersect_dicts(csd, model.state_dict(), exclude=exclude) # intersect
|
115 |
+
model.load_state_dict(csd, strict=False) # load
|
116 |
+
LOGGER.info(f'Transferred {len(csd)}/{len(model.state_dict())} items from {weights}') # report
|
117 |
+
else:
|
118 |
+
model = Model(cfg, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
|
119 |
+
amp = check_amp(model) # check AMP
|
120 |
+
|
121 |
+
# Freeze
|
122 |
+
freeze = [f'model.{x}.' for x in (freeze if len(freeze) > 1 else range(freeze[0]))] # layers to freeze
|
123 |
+
for k, v in model.named_parameters():
|
124 |
+
# v.requires_grad = True # train all layers TODO: uncomment this line as in master
|
125 |
+
# v.register_hook(lambda x: torch.nan_to_num(x)) # NaN to 0 (commented for erratic training results)
|
126 |
+
if any(x in k for x in freeze):
|
127 |
+
LOGGER.info(f'freezing {k}')
|
128 |
+
v.requires_grad = False
|
129 |
+
|
130 |
+
# Image size
|
131 |
+
gs = max(int(model.stride.max()), 32) # grid size (max stride)
|
132 |
+
imgsz = check_img_size(opt.imgsz, gs, floor=gs * 2) # verify imgsz is gs-multiple
|
133 |
+
|
134 |
+
# Batch size
|
135 |
+
if RANK == -1 and batch_size == -1: # single-GPU only, estimate best batch size
|
136 |
+
batch_size = check_train_batch_size(model, imgsz, amp)
|
137 |
+
loggers.on_params_update({"batch_size": batch_size})
|
138 |
+
|
139 |
+
# Optimizer
|
140 |
+
nbs = 64 # nominal batch size
|
141 |
+
accumulate = max(round(nbs / batch_size), 1) # accumulate loss before optimizing
|
142 |
+
hyp['weight_decay'] *= batch_size * accumulate / nbs # scale weight_decay
|
143 |
+
optimizer = smart_optimizer(model, opt.optimizer, hyp['lr0'], hyp['momentum'], hyp['weight_decay'])
|
144 |
+
|
145 |
+
# Scheduler
|
146 |
+
if opt.cos_lr:
|
147 |
+
lf = one_cycle(1, hyp['lrf'], epochs) # cosine 1->hyp['lrf']
|
148 |
+
elif opt.flat_cos_lr:
|
149 |
+
lf = one_flat_cycle(1, hyp['lrf'], epochs) # flat cosine 1->hyp['lrf']
|
150 |
+
elif opt.fixed_lr:
|
151 |
+
lf = lambda x: 1.0
|
152 |
+
else:
|
153 |
+
lf = lambda x: (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf'] # linear
|
154 |
+
|
155 |
+
# def lf(x): # saw
|
156 |
+
# return (1 - (x % 30) / 30) * (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf']
|
157 |
+
#
|
158 |
+
# def lf(x): # triangle start at min
|
159 |
+
# return 2 * abs(x / 30 - math.floor(x / 30 + 1 / 2)) * (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf']
|
160 |
+
#
|
161 |
+
# def lf(x): # triangle start at max
|
162 |
+
# return 2 * abs(x / 32 + .5 - math.floor(x / 32 + 1)) * (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf']
|
163 |
+
|
164 |
+
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf)
|
165 |
+
# from utils.plots import plot_lr_scheduler; plot_lr_scheduler(optimizer, scheduler, epochs)
|
166 |
+
|
167 |
+
# EMA
|
168 |
+
ema = ModelEMA(model) if RANK in {-1, 0} else None
|
169 |
+
|
170 |
+
# Resume
|
171 |
+
best_fitness, start_epoch = 0.0, 0
|
172 |
+
if pretrained:
|
173 |
+
if resume:
|
174 |
+
best_fitness, start_epoch, epochs = smart_resume(ckpt, optimizer, ema, weights, epochs, resume)
|
175 |
+
del ckpt, csd
|
176 |
+
|
177 |
+
# DP mode
|
178 |
+
if cuda and RANK == -1 and torch.cuda.device_count() > 1:
|
179 |
+
LOGGER.warning('WARNING ⚠️ DP not recommended, use torch.distributed.run for best DDP Multi-GPU results.')
|
180 |
+
model = torch.nn.DataParallel(model)
|
181 |
+
|
182 |
+
# SyncBatchNorm
|
183 |
+
if opt.sync_bn and cuda and RANK != -1:
|
184 |
+
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model).to(device)
|
185 |
+
LOGGER.info('Using SyncBatchNorm()')
|
186 |
+
|
187 |
+
# Trainloader
|
188 |
+
train_loader, dataset = create_dataloader(train_path,
|
189 |
+
imgsz,
|
190 |
+
batch_size // WORLD_SIZE,
|
191 |
+
gs,
|
192 |
+
single_cls,
|
193 |
+
hyp=hyp,
|
194 |
+
augment=True,
|
195 |
+
cache=None if opt.cache == 'val' else opt.cache,
|
196 |
+
rect=opt.rect,
|
197 |
+
rank=LOCAL_RANK,
|
198 |
+
workers=workers,
|
199 |
+
image_weights=opt.image_weights,
|
200 |
+
close_mosaic=opt.close_mosaic != 0,
|
201 |
+
quad=opt.quad,
|
202 |
+
prefix=colorstr('train: '),
|
203 |
+
shuffle=True,
|
204 |
+
min_items=opt.min_items)
|
205 |
+
labels = np.concatenate(dataset.labels, 0)
|
206 |
+
mlc = int(labels[:, 0].max()) # max label class
|
207 |
+
assert mlc < nc, f'Label class {mlc} exceeds nc={nc} in {data}. Possible class labels are 0-{nc - 1}'
|
208 |
+
|
209 |
+
# Process 0
|
210 |
+
if RANK in {-1, 0}:
|
211 |
+
val_loader = create_dataloader(val_path,
|
212 |
+
imgsz,
|
213 |
+
batch_size // WORLD_SIZE * 2,
|
214 |
+
gs,
|
215 |
+
single_cls,
|
216 |
+
hyp=hyp,
|
217 |
+
cache=None if noval else opt.cache,
|
218 |
+
rect=True,
|
219 |
+
rank=-1,
|
220 |
+
workers=workers * 2,
|
221 |
+
pad=0.5,
|
222 |
+
prefix=colorstr('val: '))[0]
|
223 |
+
|
224 |
+
if not resume:
|
225 |
+
# if not opt.noautoanchor:
|
226 |
+
# check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz) # run AutoAnchor
|
227 |
+
model.half().float() # pre-reduce anchor precision
|
228 |
+
|
229 |
+
callbacks.run('on_pretrain_routine_end', labels, names)
|
230 |
+
|
231 |
+
# DDP mode
|
232 |
+
if cuda and RANK != -1:
|
233 |
+
model = smart_DDP(model)
|
234 |
+
|
235 |
+
# Model attributes
|
236 |
+
nl = de_parallel(model).model[-1].nl # number of detection layers (to scale hyps)
|
237 |
+
#hyp['box'] *= 3 / nl # scale to layers
|
238 |
+
#hyp['cls'] *= nc / 80 * 3 / nl # scale to classes and layers
|
239 |
+
#hyp['obj'] *= (imgsz / 640) ** 2 * 3 / nl # scale to image size and layers
|
240 |
+
hyp['label_smoothing'] = opt.label_smoothing
|
241 |
+
model.nc = nc # attach number of classes to model
|
242 |
+
model.hyp = hyp # attach hyperparameters to model
|
243 |
+
model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) * nc # attach class weights
|
244 |
+
model.names = names
|
245 |
+
|
246 |
+
# Start training
|
247 |
+
t0 = time.time()
|
248 |
+
nb = len(train_loader) # number of batches
|
249 |
+
nw = max(round(hyp['warmup_epochs'] * nb), 100) # number of warmup iterations, max(3 epochs, 100 iterations)
|
250 |
+
# nw = min(nw, (epochs - start_epoch) / 2 * nb) # limit warmup to < 1/2 of training
|
251 |
+
last_opt_step = -1
|
252 |
+
maps = np.zeros(nc) # mAP per class
|
253 |
+
results = (0, 0, 0, 0, 0, 0, 0) # P, R, [email protected], [email protected], val_loss(box, obj, cls)
|
254 |
+
scheduler.last_epoch = start_epoch - 1 # do not move
|
255 |
+
scaler = torch.cuda.amp.GradScaler(enabled=amp)
|
256 |
+
stopper, stop = EarlyStopping(patience=opt.patience), False
|
257 |
+
compute_loss = ComputeLoss(model) # init loss class
|
258 |
+
callbacks.run('on_train_start')
|
259 |
+
LOGGER.info(f'Image sizes {imgsz} train, {imgsz} val\n'
|
260 |
+
f'Using {train_loader.num_workers * WORLD_SIZE} dataloader workers\n'
|
261 |
+
f"Logging results to {colorstr('bold', save_dir)}\n"
|
262 |
+
f'Starting training for {epochs} epochs...')
|
263 |
+
for epoch in range(start_epoch, epochs): # epoch ------------------------------------------------------------------
|
264 |
+
callbacks.run('on_train_epoch_start')
|
265 |
+
model.train()
|
266 |
+
|
267 |
+
# Update image weights (optional, single-GPU only)
|
268 |
+
if opt.image_weights:
|
269 |
+
cw = model.class_weights.cpu().numpy() * (1 - maps) ** 2 / nc # class weights
|
270 |
+
iw = labels_to_image_weights(dataset.labels, nc=nc, class_weights=cw) # image weights
|
271 |
+
dataset.indices = random.choices(range(dataset.n), weights=iw, k=dataset.n) # rand weighted idx
|
272 |
+
if epoch == (epochs - opt.close_mosaic):
|
273 |
+
LOGGER.info("Closing dataloader mosaic")
|
274 |
+
dataset.mosaic = False
|
275 |
+
|
276 |
+
# Update mosaic border (optional)
|
277 |
+
# b = int(random.uniform(0.25 * imgsz, 0.75 * imgsz + gs) // gs * gs)
|
278 |
+
# dataset.mosaic_border = [b - imgsz, -b] # height, width borders
|
279 |
+
|
280 |
+
mloss = torch.zeros(3, device=device) # mean losses
|
281 |
+
if RANK != -1:
|
282 |
+
train_loader.sampler.set_epoch(epoch)
|
283 |
+
pbar = enumerate(train_loader)
|
284 |
+
LOGGER.info(('\n' + '%11s' * 7) % ('Epoch', 'GPU_mem', 'box_loss', 'cls_loss', 'dfl_loss', 'Instances', 'Size'))
|
285 |
+
if RANK in {-1, 0}:
|
286 |
+
pbar = tqdm(pbar, total=nb, bar_format=TQDM_BAR_FORMAT) # progress bar
|
287 |
+
optimizer.zero_grad()
|
288 |
+
for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
|
289 |
+
callbacks.run('on_train_batch_start')
|
290 |
+
ni = i + nb * epoch # number integrated batches (since train start)
|
291 |
+
imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0
|
292 |
+
|
293 |
+
# Warmup
|
294 |
+
if ni <= nw:
|
295 |
+
xi = [0, nw] # x interp
|
296 |
+
# compute_loss.gr = np.interp(ni, xi, [0.0, 1.0]) # iou loss ratio (obj_loss = 1.0 or iou)
|
297 |
+
accumulate = max(1, np.interp(ni, xi, [1, nbs / batch_size]).round())
|
298 |
+
for j, x in enumerate(optimizer.param_groups):
|
299 |
+
# bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0
|
300 |
+
x['lr'] = np.interp(ni, xi, [hyp['warmup_bias_lr'] if j == 0 else 0.0, x['initial_lr'] * lf(epoch)])
|
301 |
+
if 'momentum' in x:
|
302 |
+
x['momentum'] = np.interp(ni, xi, [hyp['warmup_momentum'], hyp['momentum']])
|
303 |
+
|
304 |
+
# Multi-scale
|
305 |
+
if opt.multi_scale:
|
306 |
+
sz = random.randrange(imgsz * 0.5, imgsz * 1.5 + gs) // gs * gs # size
|
307 |
+
sf = sz / max(imgs.shape[2:]) # scale factor
|
308 |
+
if sf != 1:
|
309 |
+
ns = [math.ceil(x * sf / gs) * gs for x in imgs.shape[2:]] # new shape (stretched to gs-multiple)
|
310 |
+
imgs = nn.functional.interpolate(imgs, size=ns, mode='bilinear', align_corners=False)
|
311 |
+
|
312 |
+
# Forward
|
313 |
+
with torch.cuda.amp.autocast(amp):
|
314 |
+
pred = model(imgs) # forward
|
315 |
+
loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size
|
316 |
+
if RANK != -1:
|
317 |
+
loss *= WORLD_SIZE # gradient averaged between devices in DDP mode
|
318 |
+
if opt.quad:
|
319 |
+
loss *= 4.
|
320 |
+
|
321 |
+
# Backward
|
322 |
+
scaler.scale(loss).backward()
|
323 |
+
|
324 |
+
# Optimize - https://pytorch.org/docs/master/notes/amp_examples.html
|
325 |
+
if ni - last_opt_step >= accumulate:
|
326 |
+
scaler.unscale_(optimizer) # unscale gradients
|
327 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=10.0) # clip gradients
|
328 |
+
scaler.step(optimizer) # optimizer.step
|
329 |
+
scaler.update()
|
330 |
+
optimizer.zero_grad()
|
331 |
+
if ema:
|
332 |
+
ema.update(model)
|
333 |
+
last_opt_step = ni
|
334 |
+
|
335 |
+
# Log
|
336 |
+
if RANK in {-1, 0}:
|
337 |
+
mloss = (mloss * i + loss_items) / (i + 1) # update mean losses
|
338 |
+
mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB)
|
339 |
+
pbar.set_description(('%11s' * 2 + '%11.4g' * 5) %
|
340 |
+
(f'{epoch}/{epochs - 1}', mem, *mloss, targets.shape[0], imgs.shape[-1]))
|
341 |
+
callbacks.run('on_train_batch_end', model, ni, imgs, targets, paths, list(mloss))
|
342 |
+
if callbacks.stop_training:
|
343 |
+
return
|
344 |
+
# end batch ------------------------------------------------------------------------------------------------
|
345 |
+
|
346 |
+
# Scheduler
|
347 |
+
lr = [x['lr'] for x in optimizer.param_groups] # for loggers
|
348 |
+
scheduler.step()
|
349 |
+
|
350 |
+
if RANK in {-1, 0}:
|
351 |
+
# mAP
|
352 |
+
callbacks.run('on_train_epoch_end', epoch=epoch)
|
353 |
+
ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'names', 'stride', 'class_weights'])
|
354 |
+
final_epoch = (epoch + 1 == epochs) or stopper.possible_stop
|
355 |
+
if not noval or final_epoch: # Calculate mAP
|
356 |
+
results, maps, _ = validate.run(data_dict,
|
357 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
358 |
+
imgsz=imgsz,
|
359 |
+
half=amp,
|
360 |
+
model=ema.ema,
|
361 |
+
single_cls=single_cls,
|
362 |
+
dataloader=val_loader,
|
363 |
+
save_dir=save_dir,
|
364 |
+
plots=False,
|
365 |
+
callbacks=callbacks,
|
366 |
+
compute_loss=compute_loss)
|
367 |
+
|
368 |
+
# Update best mAP
|
369 |
+
fi = fitness(np.array(results).reshape(1, -1)) # weighted combination of [P, R, [email protected], [email protected]]
|
370 |
+
stop = stopper(epoch=epoch, fitness=fi) # early stop check
|
371 |
+
if fi > best_fitness:
|
372 |
+
best_fitness = fi
|
373 |
+
log_vals = list(mloss) + list(results) + lr
|
374 |
+
callbacks.run('on_fit_epoch_end', log_vals, epoch, best_fitness, fi)
|
375 |
+
|
376 |
+
# Save model
|
377 |
+
if (not nosave) or (final_epoch and not evolve): # if save
|
378 |
+
ckpt = {
|
379 |
+
'epoch': epoch,
|
380 |
+
'best_fitness': best_fitness,
|
381 |
+
'model': deepcopy(de_parallel(model)).half(),
|
382 |
+
'ema': deepcopy(ema.ema).half(),
|
383 |
+
'updates': ema.updates,
|
384 |
+
'optimizer': optimizer.state_dict(),
|
385 |
+
'opt': vars(opt),
|
386 |
+
'git': GIT_INFO, # {remote, branch, commit} if a git repo
|
387 |
+
'date': datetime.now().isoformat()}
|
388 |
+
|
389 |
+
# Save last, best and delete
|
390 |
+
torch.save(ckpt, last)
|
391 |
+
if best_fitness == fi:
|
392 |
+
torch.save(ckpt, best)
|
393 |
+
if opt.save_period > 0 and epoch % opt.save_period == 0:
|
394 |
+
torch.save(ckpt, w / f'epoch{epoch}.pt')
|
395 |
+
del ckpt
|
396 |
+
callbacks.run('on_model_save', last, epoch, final_epoch, best_fitness, fi)
|
397 |
+
|
398 |
+
# EarlyStopping
|
399 |
+
if RANK != -1: # if DDP training
|
400 |
+
broadcast_list = [stop if RANK == 0 else None]
|
401 |
+
dist.broadcast_object_list(broadcast_list, 0) # broadcast 'stop' to all ranks
|
402 |
+
if RANK != 0:
|
403 |
+
stop = broadcast_list[0]
|
404 |
+
if stop:
|
405 |
+
break # must break all DDP ranks
|
406 |
+
|
407 |
+
# end epoch ----------------------------------------------------------------------------------------------------
|
408 |
+
# end training -----------------------------------------------------------------------------------------------------
|
409 |
+
if RANK in {-1, 0}:
|
410 |
+
LOGGER.info(f'\n{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.')
|
411 |
+
for f in last, best:
|
412 |
+
if f.exists():
|
413 |
+
strip_optimizer(f) # strip optimizers
|
414 |
+
if f is best:
|
415 |
+
LOGGER.info(f'\nValidating {f}...')
|
416 |
+
results, _, _ = validate.run(
|
417 |
+
data_dict,
|
418 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
419 |
+
imgsz=imgsz,
|
420 |
+
model=attempt_load(f, device).half(),
|
421 |
+
single_cls=single_cls,
|
422 |
+
dataloader=val_loader,
|
423 |
+
save_dir=save_dir,
|
424 |
+
save_json=is_coco,
|
425 |
+
verbose=True,
|
426 |
+
plots=plots,
|
427 |
+
callbacks=callbacks,
|
428 |
+
compute_loss=compute_loss) # val best model with plots
|
429 |
+
if is_coco:
|
430 |
+
callbacks.run('on_fit_epoch_end', list(mloss) + list(results) + lr, epoch, best_fitness, fi)
|
431 |
+
|
432 |
+
callbacks.run('on_train_end', last, best, epoch, results)
|
433 |
+
|
434 |
+
torch.cuda.empty_cache()
|
435 |
+
return results
|
436 |
+
|
437 |
+
|
438 |
+
def parse_opt(known=False):
|
439 |
+
parser = argparse.ArgumentParser()
|
440 |
+
# parser.add_argument('--weights', type=str, default=ROOT / 'yolo.pt', help='initial weights path')
|
441 |
+
# parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
|
442 |
+
parser.add_argument('--weights', type=str, default='', help='initial weights path')
|
443 |
+
parser.add_argument('--cfg', type=str, default='yolo.yaml', help='model.yaml path')
|
444 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco.yaml', help='dataset.yaml path')
|
445 |
+
parser.add_argument('--hyp', type=str, default=ROOT / 'data/hyps/hyp.scratch-high.yaml', help='hyperparameters path')
|
446 |
+
parser.add_argument('--epochs', type=int, default=100, help='total training epochs')
|
447 |
+
parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs, -1 for autobatch')
|
448 |
+
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='train, val image size (pixels)')
|
449 |
+
parser.add_argument('--rect', action='store_true', help='rectangular training')
|
450 |
+
parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')
|
451 |
+
parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
|
452 |
+
parser.add_argument('--noval', action='store_true', help='only validate final epoch')
|
453 |
+
parser.add_argument('--noautoanchor', action='store_true', help='disable AutoAnchor')
|
454 |
+
parser.add_argument('--noplots', action='store_true', help='save no plot files')
|
455 |
+
parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations')
|
456 |
+
parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
|
457 |
+
parser.add_argument('--cache', type=str, nargs='?', const='ram', help='image --cache ram/disk')
|
458 |
+
parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
|
459 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
460 |
+
parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
|
461 |
+
parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
|
462 |
+
parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'AdamW', 'LION'], default='SGD', help='optimizer')
|
463 |
+
parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
|
464 |
+
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
|
465 |
+
parser.add_argument('--project', default=ROOT / 'runs/train', help='save to project/name')
|
466 |
+
parser.add_argument('--name', default='exp', help='save to project/name')
|
467 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
468 |
+
parser.add_argument('--quad', action='store_true', help='quad dataloader')
|
469 |
+
parser.add_argument('--cos-lr', action='store_true', help='cosine LR scheduler')
|
470 |
+
parser.add_argument('--flat-cos-lr', action='store_true', help='flat cosine LR scheduler')
|
471 |
+
parser.add_argument('--fixed-lr', action='store_true', help='fixed LR scheduler')
|
472 |
+
parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
|
473 |
+
parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)')
|
474 |
+
parser.add_argument('--freeze', nargs='+', type=int, default=[0], help='Freeze layers: backbone=10, first3=0 1 2')
|
475 |
+
parser.add_argument('--save-period', type=int, default=-1, help='Save checkpoint every x epochs (disabled if < 1)')
|
476 |
+
parser.add_argument('--seed', type=int, default=0, help='Global training seed')
|
477 |
+
parser.add_argument('--local_rank', type=int, default=-1, help='Automatic DDP Multi-GPU argument, do not modify')
|
478 |
+
parser.add_argument('--min-items', type=int, default=0, help='Experimental')
|
479 |
+
parser.add_argument('--close-mosaic', type=int, default=0, help='Experimental')
|
480 |
+
|
481 |
+
# Logger arguments
|
482 |
+
parser.add_argument('--entity', default=None, help='Entity')
|
483 |
+
parser.add_argument('--upload_dataset', nargs='?', const=True, default=False, help='Upload data, "val" option')
|
484 |
+
parser.add_argument('--bbox_interval', type=int, default=-1, help='Set bounding-box image logging interval')
|
485 |
+
parser.add_argument('--artifact_alias', type=str, default='latest', help='Version of dataset artifact to use')
|
486 |
+
|
487 |
+
return parser.parse_known_args()[0] if known else parser.parse_args()
|
488 |
+
|
489 |
+
|
490 |
+
def main(opt, callbacks=Callbacks()):
|
491 |
+
# Checks
|
492 |
+
if RANK in {-1, 0}:
|
493 |
+
print_args(vars(opt))
|
494 |
+
#check_git_status()
|
495 |
+
#check_requirements()
|
496 |
+
|
497 |
+
# Resume (from specified or most recent last.pt)
|
498 |
+
if opt.resume and not check_comet_resume(opt) and not opt.evolve:
|
499 |
+
last = Path(check_file(opt.resume) if isinstance(opt.resume, str) else get_latest_run())
|
500 |
+
opt_yaml = last.parent.parent / 'opt.yaml' # train options yaml
|
501 |
+
opt_data = opt.data # original dataset
|
502 |
+
if opt_yaml.is_file():
|
503 |
+
with open(opt_yaml, errors='ignore') as f:
|
504 |
+
d = yaml.safe_load(f)
|
505 |
+
else:
|
506 |
+
d = torch.load(last, map_location='cpu')['opt']
|
507 |
+
opt = argparse.Namespace(**d) # replace
|
508 |
+
opt.cfg, opt.weights, opt.resume = '', str(last), True # reinstate
|
509 |
+
if is_url(opt_data):
|
510 |
+
opt.data = check_file(opt_data) # avoid HUB resume auth timeout
|
511 |
+
else:
|
512 |
+
opt.data, opt.cfg, opt.hyp, opt.weights, opt.project = \
|
513 |
+
check_file(opt.data), check_yaml(opt.cfg), check_yaml(opt.hyp), str(opt.weights), str(opt.project) # checks
|
514 |
+
assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified'
|
515 |
+
if opt.evolve:
|
516 |
+
if opt.project == str(ROOT / 'runs/train'): # if default project name, rename to runs/evolve
|
517 |
+
opt.project = str(ROOT / 'runs/evolve')
|
518 |
+
opt.exist_ok, opt.resume = opt.resume, False # pass resume to exist_ok and disable resume
|
519 |
+
if opt.name == 'cfg':
|
520 |
+
opt.name = Path(opt.cfg).stem # use model.yaml as name
|
521 |
+
opt.save_dir = str(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok))
|
522 |
+
|
523 |
+
# DDP mode
|
524 |
+
device = select_device(opt.device, batch_size=opt.batch_size)
|
525 |
+
if LOCAL_RANK != -1:
|
526 |
+
msg = 'is not compatible with YOLO Multi-GPU DDP training'
|
527 |
+
assert not opt.image_weights, f'--image-weights {msg}'
|
528 |
+
assert not opt.evolve, f'--evolve {msg}'
|
529 |
+
assert opt.batch_size != -1, f'AutoBatch with --batch-size -1 {msg}, please pass a valid --batch-size'
|
530 |
+
assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'
|
531 |
+
assert torch.cuda.device_count() > LOCAL_RANK, 'insufficient CUDA devices for DDP command'
|
532 |
+
torch.cuda.set_device(LOCAL_RANK)
|
533 |
+
device = torch.device('cuda', LOCAL_RANK)
|
534 |
+
dist.init_process_group(backend="nccl" if dist.is_nccl_available() else "gloo")
|
535 |
+
|
536 |
+
# Train
|
537 |
+
if not opt.evolve:
|
538 |
+
train(opt.hyp, opt, device, callbacks)
|
539 |
+
|
540 |
+
# Evolve hyperparameters (optional)
|
541 |
+
else:
|
542 |
+
# Hyperparameter evolution metadata (mutation scale 0-1, lower_limit, upper_limit)
|
543 |
+
meta = {
|
544 |
+
'lr0': (1, 1e-5, 1e-1), # initial learning rate (SGD=1E-2, Adam=1E-3)
|
545 |
+
'lrf': (1, 0.01, 1.0), # final OneCycleLR learning rate (lr0 * lrf)
|
546 |
+
'momentum': (0.3, 0.6, 0.98), # SGD momentum/Adam beta1
|
547 |
+
'weight_decay': (1, 0.0, 0.001), # optimizer weight decay
|
548 |
+
'warmup_epochs': (1, 0.0, 5.0), # warmup epochs (fractions ok)
|
549 |
+
'warmup_momentum': (1, 0.0, 0.95), # warmup initial momentum
|
550 |
+
'warmup_bias_lr': (1, 0.0, 0.2), # warmup initial bias lr
|
551 |
+
'box': (1, 0.02, 0.2), # box loss gain
|
552 |
+
'cls': (1, 0.2, 4.0), # cls loss gain
|
553 |
+
'cls_pw': (1, 0.5, 2.0), # cls BCELoss positive_weight
|
554 |
+
'obj': (1, 0.2, 4.0), # obj loss gain (scale with pixels)
|
555 |
+
'obj_pw': (1, 0.5, 2.0), # obj BCELoss positive_weight
|
556 |
+
'iou_t': (0, 0.1, 0.7), # IoU training threshold
|
557 |
+
'anchor_t': (1, 2.0, 8.0), # anchor-multiple threshold
|
558 |
+
'anchors': (2, 2.0, 10.0), # anchors per output grid (0 to ignore)
|
559 |
+
'fl_gamma': (0, 0.0, 2.0), # focal loss gamma (efficientDet default gamma=1.5)
|
560 |
+
'hsv_h': (1, 0.0, 0.1), # image HSV-Hue augmentation (fraction)
|
561 |
+
'hsv_s': (1, 0.0, 0.9), # image HSV-Saturation augmentation (fraction)
|
562 |
+
'hsv_v': (1, 0.0, 0.9), # image HSV-Value augmentation (fraction)
|
563 |
+
'degrees': (1, 0.0, 45.0), # image rotation (+/- deg)
|
564 |
+
'translate': (1, 0.0, 0.9), # image translation (+/- fraction)
|
565 |
+
'scale': (1, 0.0, 0.9), # image scale (+/- gain)
|
566 |
+
'shear': (1, 0.0, 10.0), # image shear (+/- deg)
|
567 |
+
'perspective': (0, 0.0, 0.001), # image perspective (+/- fraction), range 0-0.001
|
568 |
+
'flipud': (1, 0.0, 1.0), # image flip up-down (probability)
|
569 |
+
'fliplr': (0, 0.0, 1.0), # image flip left-right (probability)
|
570 |
+
'mosaic': (1, 0.0, 1.0), # image mixup (probability)
|
571 |
+
'mixup': (1, 0.0, 1.0), # image mixup (probability)
|
572 |
+
'copy_paste': (1, 0.0, 1.0)} # segment copy-paste (probability)
|
573 |
+
|
574 |
+
with open(opt.hyp, errors='ignore') as f:
|
575 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
576 |
+
if 'anchors' not in hyp: # anchors commented in hyp.yaml
|
577 |
+
hyp['anchors'] = 3
|
578 |
+
if opt.noautoanchor:
|
579 |
+
del hyp['anchors'], meta['anchors']
|
580 |
+
opt.noval, opt.nosave, save_dir = True, True, Path(opt.save_dir) # only val/save final epoch
|
581 |
+
# ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
|
582 |
+
evolve_yaml, evolve_csv = save_dir / 'hyp_evolve.yaml', save_dir / 'evolve.csv'
|
583 |
+
if opt.bucket:
|
584 |
+
os.system(f'gsutil cp gs://{opt.bucket}/evolve.csv {evolve_csv}') # download evolve.csv if exists
|
585 |
+
|
586 |
+
for _ in range(opt.evolve): # generations to evolve
|
587 |
+
if evolve_csv.exists(): # if evolve.csv exists: select best hyps and mutate
|
588 |
+
# Select parent(s)
|
589 |
+
parent = 'single' # parent selection method: 'single' or 'weighted'
|
590 |
+
x = np.loadtxt(evolve_csv, ndmin=2, delimiter=',', skiprows=1)
|
591 |
+
n = min(5, len(x)) # number of previous results to consider
|
592 |
+
x = x[np.argsort(-fitness(x))][:n] # top n mutations
|
593 |
+
w = fitness(x) - fitness(x).min() + 1E-6 # weights (sum > 0)
|
594 |
+
if parent == 'single' or len(x) == 1:
|
595 |
+
# x = x[random.randint(0, n - 1)] # random selection
|
596 |
+
x = x[random.choices(range(n), weights=w)[0]] # weighted selection
|
597 |
+
elif parent == 'weighted':
|
598 |
+
x = (x * w.reshape(n, 1)).sum(0) / w.sum() # weighted combination
|
599 |
+
|
600 |
+
# Mutate
|
601 |
+
mp, s = 0.8, 0.2 # mutation probability, sigma
|
602 |
+
npr = np.random
|
603 |
+
npr.seed(int(time.time()))
|
604 |
+
g = np.array([meta[k][0] for k in hyp.keys()]) # gains 0-1
|
605 |
+
ng = len(meta)
|
606 |
+
v = np.ones(ng)
|
607 |
+
while all(v == 1): # mutate until a change occurs (prevent duplicates)
|
608 |
+
v = (g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1).clip(0.3, 3.0)
|
609 |
+
for i, k in enumerate(hyp.keys()): # plt.hist(v.ravel(), 300)
|
610 |
+
hyp[k] = float(x[i + 7] * v[i]) # mutate
|
611 |
+
|
612 |
+
# Constrain to limits
|
613 |
+
for k, v in meta.items():
|
614 |
+
hyp[k] = max(hyp[k], v[1]) # lower limit
|
615 |
+
hyp[k] = min(hyp[k], v[2]) # upper limit
|
616 |
+
hyp[k] = round(hyp[k], 5) # significant digits
|
617 |
+
|
618 |
+
# Train mutation
|
619 |
+
results = train(hyp.copy(), opt, device, callbacks)
|
620 |
+
callbacks = Callbacks()
|
621 |
+
# Write mutation results
|
622 |
+
keys = ('metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95', 'val/box_loss',
|
623 |
+
'val/obj_loss', 'val/cls_loss')
|
624 |
+
print_mutation(keys, results, hyp.copy(), save_dir, opt.bucket)
|
625 |
+
|
626 |
+
# Plot results
|
627 |
+
plot_evolve(evolve_csv)
|
628 |
+
LOGGER.info(f'Hyperparameter evolution finished {opt.evolve} generations\n'
|
629 |
+
f"Results saved to {colorstr('bold', save_dir)}\n"
|
630 |
+
f'Usage example: $ python train.py --hyp {evolve_yaml}')
|
631 |
+
|
632 |
+
|
633 |
+
def run(**kwargs):
|
634 |
+
# Usage: import train; train.run(data='coco128.yaml', imgsz=320, weights='yolo.pt')
|
635 |
+
opt = parse_opt(True)
|
636 |
+
for k, v in kwargs.items():
|
637 |
+
setattr(opt, k, v)
|
638 |
+
main(opt)
|
639 |
+
return opt
|
640 |
+
|
641 |
+
|
642 |
+
if __name__ == "__main__":
|
643 |
+
opt = parse_opt()
|
644 |
+
main(opt)
|
train_triple.py
ADDED
@@ -0,0 +1,636 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import math
|
3 |
+
import os
|
4 |
+
import random
|
5 |
+
import sys
|
6 |
+
import time
|
7 |
+
from copy import deepcopy
|
8 |
+
from datetime import datetime
|
9 |
+
from pathlib import Path
|
10 |
+
|
11 |
+
import numpy as np
|
12 |
+
import torch
|
13 |
+
import torch.distributed as dist
|
14 |
+
import torch.nn as nn
|
15 |
+
import yaml
|
16 |
+
from torch.optim import lr_scheduler
|
17 |
+
from tqdm import tqdm
|
18 |
+
|
19 |
+
FILE = Path(__file__).resolve()
|
20 |
+
ROOT = FILE.parents[0] # YOLO root directory
|
21 |
+
if str(ROOT) not in sys.path:
|
22 |
+
sys.path.append(str(ROOT)) # add ROOT to PATH
|
23 |
+
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
24 |
+
|
25 |
+
import val_triple as validate # for end-of-epoch mAP
|
26 |
+
from models.experimental import attempt_load
|
27 |
+
from models.yolo import Model
|
28 |
+
from utils.autoanchor import check_anchors
|
29 |
+
from utils.autobatch import check_train_batch_size
|
30 |
+
from utils.callbacks import Callbacks
|
31 |
+
from utils.dataloaders import create_dataloader
|
32 |
+
from utils.downloads import attempt_download, is_url
|
33 |
+
from utils.general import (LOGGER, TQDM_BAR_FORMAT, check_amp, check_dataset, check_file, check_git_info,
|
34 |
+
check_git_status, check_img_size, check_requirements, check_suffix, check_yaml, colorstr,
|
35 |
+
get_latest_run, increment_path, init_seeds, intersect_dicts, labels_to_class_weights,
|
36 |
+
labels_to_image_weights, methods, one_cycle, print_args, print_mutation, strip_optimizer,
|
37 |
+
yaml_save)
|
38 |
+
from utils.loggers import Loggers
|
39 |
+
from utils.loggers.comet.comet_utils import check_comet_resume
|
40 |
+
from utils.loss_tal_triple import ComputeLoss
|
41 |
+
from utils.metrics import fitness
|
42 |
+
from utils.plots import plot_evolve
|
43 |
+
from utils.torch_utils import (EarlyStopping, ModelEMA, de_parallel, select_device, smart_DDP, smart_optimizer,
|
44 |
+
smart_resume, torch_distributed_zero_first)
|
45 |
+
|
46 |
+
LOCAL_RANK = int(os.getenv('LOCAL_RANK', -1)) # https://pytorch.org/docs/stable/elastic/run.html
|
47 |
+
RANK = int(os.getenv('RANK', -1))
|
48 |
+
WORLD_SIZE = int(os.getenv('WORLD_SIZE', 1))
|
49 |
+
GIT_INFO = None#check_git_info()
|
50 |
+
|
51 |
+
|
52 |
+
def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictionary
|
53 |
+
save_dir, epochs, batch_size, weights, single_cls, evolve, data, cfg, resume, noval, nosave, workers, freeze = \
|
54 |
+
Path(opt.save_dir), opt.epochs, opt.batch_size, opt.weights, opt.single_cls, opt.evolve, opt.data, opt.cfg, \
|
55 |
+
opt.resume, opt.noval, opt.nosave, opt.workers, opt.freeze
|
56 |
+
callbacks.run('on_pretrain_routine_start')
|
57 |
+
|
58 |
+
# Directories
|
59 |
+
w = save_dir / 'weights' # weights dir
|
60 |
+
(w.parent if evolve else w).mkdir(parents=True, exist_ok=True) # make dir
|
61 |
+
last, best = w / 'last.pt', w / 'best.pt'
|
62 |
+
|
63 |
+
# Hyperparameters
|
64 |
+
if isinstance(hyp, str):
|
65 |
+
with open(hyp, errors='ignore') as f:
|
66 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
67 |
+
LOGGER.info(colorstr('hyperparameters: ') + ', '.join(f'{k}={v}' for k, v in hyp.items()))
|
68 |
+
hyp['anchor_t'] = 5.0
|
69 |
+
opt.hyp = hyp.copy() # for saving hyps to checkpoints
|
70 |
+
|
71 |
+
# Save run settings
|
72 |
+
if not evolve:
|
73 |
+
yaml_save(save_dir / 'hyp.yaml', hyp)
|
74 |
+
yaml_save(save_dir / 'opt.yaml', vars(opt))
|
75 |
+
|
76 |
+
# Loggers
|
77 |
+
data_dict = None
|
78 |
+
if RANK in {-1, 0}:
|
79 |
+
loggers = Loggers(save_dir, weights, opt, hyp, LOGGER) # loggers instance
|
80 |
+
|
81 |
+
# Register actions
|
82 |
+
for k in methods(loggers):
|
83 |
+
callbacks.register_action(k, callback=getattr(loggers, k))
|
84 |
+
|
85 |
+
# Process custom dataset artifact link
|
86 |
+
data_dict = loggers.remote_dataset
|
87 |
+
if resume: # If resuming runs from remote artifact
|
88 |
+
weights, epochs, hyp, batch_size = opt.weights, opt.epochs, opt.hyp, opt.batch_size
|
89 |
+
|
90 |
+
# Config
|
91 |
+
plots = not evolve and not opt.noplots # create plots
|
92 |
+
cuda = device.type != 'cpu'
|
93 |
+
init_seeds(opt.seed + 1 + RANK, deterministic=True)
|
94 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
95 |
+
data_dict = data_dict or check_dataset(data) # check if None
|
96 |
+
train_path, val_path = data_dict['train'], data_dict['val']
|
97 |
+
nc = 1 if single_cls else int(data_dict['nc']) # number of classes
|
98 |
+
names = {0: 'item'} if single_cls and len(data_dict['names']) != 1 else data_dict['names'] # class names
|
99 |
+
#is_coco = isinstance(val_path, str) and val_path.endswith('coco/val2017.txt') # COCO dataset
|
100 |
+
is_coco = isinstance(val_path, str) and val_path.endswith('val2017.txt') # COCO dataset
|
101 |
+
|
102 |
+
# Model
|
103 |
+
check_suffix(weights, '.pt') # check weights
|
104 |
+
pretrained = weights.endswith('.pt')
|
105 |
+
if pretrained:
|
106 |
+
with torch_distributed_zero_first(LOCAL_RANK):
|
107 |
+
weights = attempt_download(weights) # download if not found locally
|
108 |
+
ckpt = torch.load(weights, map_location='cpu') # load checkpoint to CPU to avoid CUDA memory leak
|
109 |
+
model = Model(cfg or ckpt['model'].yaml, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
|
110 |
+
exclude = ['anchor'] if (cfg or hyp.get('anchors')) and not resume else [] # exclude keys
|
111 |
+
csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
|
112 |
+
csd = intersect_dicts(csd, model.state_dict(), exclude=exclude) # intersect
|
113 |
+
model.load_state_dict(csd, strict=False) # load
|
114 |
+
LOGGER.info(f'Transferred {len(csd)}/{len(model.state_dict())} items from {weights}') # report
|
115 |
+
else:
|
116 |
+
model = Model(cfg, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
|
117 |
+
amp = check_amp(model) # check AMP
|
118 |
+
|
119 |
+
# Freeze
|
120 |
+
freeze = [f'model.{x}.' for x in (freeze if len(freeze) > 1 else range(freeze[0]))] # layers to freeze
|
121 |
+
for k, v in model.named_parameters():
|
122 |
+
# v.requires_grad = True # train all layers TODO: uncomment this line as in master
|
123 |
+
# v.register_hook(lambda x: torch.nan_to_num(x)) # NaN to 0 (commented for erratic training results)
|
124 |
+
if any(x in k for x in freeze):
|
125 |
+
LOGGER.info(f'freezing {k}')
|
126 |
+
v.requires_grad = False
|
127 |
+
|
128 |
+
# Image size
|
129 |
+
gs = max(int(model.stride.max()), 32) # grid size (max stride)
|
130 |
+
imgsz = check_img_size(opt.imgsz, gs, floor=gs * 2) # verify imgsz is gs-multiple
|
131 |
+
|
132 |
+
# Batch size
|
133 |
+
if RANK == -1 and batch_size == -1: # single-GPU only, estimate best batch size
|
134 |
+
batch_size = check_train_batch_size(model, imgsz, amp)
|
135 |
+
loggers.on_params_update({"batch_size": batch_size})
|
136 |
+
|
137 |
+
# Optimizer
|
138 |
+
nbs = 64 # nominal batch size
|
139 |
+
accumulate = max(round(nbs / batch_size), 1) # accumulate loss before optimizing
|
140 |
+
hyp['weight_decay'] *= batch_size * accumulate / nbs # scale weight_decay
|
141 |
+
optimizer = smart_optimizer(model, opt.optimizer, hyp['lr0'], hyp['momentum'], hyp['weight_decay'])
|
142 |
+
|
143 |
+
# Scheduler
|
144 |
+
if opt.cos_lr:
|
145 |
+
lf = one_cycle(1, hyp['lrf'], epochs) # cosine 1->hyp['lrf']
|
146 |
+
else:
|
147 |
+
lf = lambda x: (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf'] # linear
|
148 |
+
|
149 |
+
# def lf(x): # saw
|
150 |
+
# return (1 - (x % 30) / 30) * (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf']
|
151 |
+
#
|
152 |
+
# def lf(x): # triangle start at min
|
153 |
+
# return 2 * abs(x / 30 - math.floor(x / 30 + 1 / 2)) * (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf']
|
154 |
+
#
|
155 |
+
# def lf(x): # triangle start at max
|
156 |
+
# return 2 * abs(x / 32 + .5 - math.floor(x / 32 + 1)) * (1 - x / epochs) * (1.0 - hyp['lrf']) + hyp['lrf']
|
157 |
+
|
158 |
+
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf)
|
159 |
+
# from utils.plots import plot_lr_scheduler; plot_lr_scheduler(optimizer, scheduler, epochs)
|
160 |
+
|
161 |
+
# EMA
|
162 |
+
ema = ModelEMA(model) if RANK in {-1, 0} else None
|
163 |
+
|
164 |
+
# Resume
|
165 |
+
best_fitness, start_epoch = 0.0, 0
|
166 |
+
if pretrained:
|
167 |
+
if resume:
|
168 |
+
best_fitness, start_epoch, epochs = smart_resume(ckpt, optimizer, ema, weights, epochs, resume)
|
169 |
+
del ckpt, csd
|
170 |
+
|
171 |
+
# DP mode
|
172 |
+
if cuda and RANK == -1 and torch.cuda.device_count() > 1:
|
173 |
+
LOGGER.warning('WARNING ⚠️ DP not recommended, use torch.distributed.run for best DDP Multi-GPU results.')
|
174 |
+
model = torch.nn.DataParallel(model)
|
175 |
+
|
176 |
+
# SyncBatchNorm
|
177 |
+
if opt.sync_bn and cuda and RANK != -1:
|
178 |
+
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model).to(device)
|
179 |
+
LOGGER.info('Using SyncBatchNorm()')
|
180 |
+
|
181 |
+
# Trainloader
|
182 |
+
train_loader, dataset = create_dataloader(train_path,
|
183 |
+
imgsz,
|
184 |
+
batch_size // WORLD_SIZE,
|
185 |
+
gs,
|
186 |
+
single_cls,
|
187 |
+
hyp=hyp,
|
188 |
+
augment=True,
|
189 |
+
cache=None if opt.cache == 'val' else opt.cache,
|
190 |
+
rect=opt.rect,
|
191 |
+
rank=LOCAL_RANK,
|
192 |
+
workers=workers,
|
193 |
+
image_weights=opt.image_weights,
|
194 |
+
close_mosaic=opt.close_mosaic != 0,
|
195 |
+
quad=opt.quad,
|
196 |
+
prefix=colorstr('train: '),
|
197 |
+
shuffle=True,
|
198 |
+
min_items=opt.min_items)
|
199 |
+
labels = np.concatenate(dataset.labels, 0)
|
200 |
+
mlc = int(labels[:, 0].max()) # max label class
|
201 |
+
assert mlc < nc, f'Label class {mlc} exceeds nc={nc} in {data}. Possible class labels are 0-{nc - 1}'
|
202 |
+
|
203 |
+
# Process 0
|
204 |
+
if RANK in {-1, 0}:
|
205 |
+
val_loader = create_dataloader(val_path,
|
206 |
+
imgsz,
|
207 |
+
batch_size // WORLD_SIZE * 2,
|
208 |
+
gs,
|
209 |
+
single_cls,
|
210 |
+
hyp=hyp,
|
211 |
+
cache=None if noval else opt.cache,
|
212 |
+
rect=True,
|
213 |
+
rank=-1,
|
214 |
+
workers=workers * 2,
|
215 |
+
pad=0.5,
|
216 |
+
prefix=colorstr('val: '))[0]
|
217 |
+
|
218 |
+
if not resume:
|
219 |
+
# if not opt.noautoanchor:
|
220 |
+
# check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz) # run AutoAnchor
|
221 |
+
model.half().float() # pre-reduce anchor precision
|
222 |
+
|
223 |
+
callbacks.run('on_pretrain_routine_end', labels, names)
|
224 |
+
|
225 |
+
# DDP mode
|
226 |
+
if cuda and RANK != -1:
|
227 |
+
model = smart_DDP(model)
|
228 |
+
|
229 |
+
# Model attributes
|
230 |
+
nl = de_parallel(model).model[-1].nl # number of detection layers (to scale hyps)
|
231 |
+
#hyp['box'] *= 3 / nl # scale to layers
|
232 |
+
#hyp['cls'] *= nc / 80 * 3 / nl # scale to classes and layers
|
233 |
+
#hyp['obj'] *= (imgsz / 640) ** 2 * 3 / nl # scale to image size and layers
|
234 |
+
hyp['label_smoothing'] = opt.label_smoothing
|
235 |
+
model.nc = nc # attach number of classes to model
|
236 |
+
model.hyp = hyp # attach hyperparameters to model
|
237 |
+
model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) * nc # attach class weights
|
238 |
+
model.names = names
|
239 |
+
|
240 |
+
# Start training
|
241 |
+
t0 = time.time()
|
242 |
+
nb = len(train_loader) # number of batches
|
243 |
+
nw = max(round(hyp['warmup_epochs'] * nb), 100) # number of warmup iterations, max(3 epochs, 100 iterations)
|
244 |
+
# nw = min(nw, (epochs - start_epoch) / 2 * nb) # limit warmup to < 1/2 of training
|
245 |
+
last_opt_step = -1
|
246 |
+
maps = np.zeros(nc) # mAP per class
|
247 |
+
results = (0, 0, 0, 0, 0, 0, 0) # P, R, [email protected], [email protected], val_loss(box, obj, cls)
|
248 |
+
scheduler.last_epoch = start_epoch - 1 # do not move
|
249 |
+
scaler = torch.cuda.amp.GradScaler(enabled=amp)
|
250 |
+
stopper, stop = EarlyStopping(patience=opt.patience), False
|
251 |
+
compute_loss = ComputeLoss(model) # init loss class
|
252 |
+
callbacks.run('on_train_start')
|
253 |
+
LOGGER.info(f'Image sizes {imgsz} train, {imgsz} val\n'
|
254 |
+
f'Using {train_loader.num_workers * WORLD_SIZE} dataloader workers\n'
|
255 |
+
f"Logging results to {colorstr('bold', save_dir)}\n"
|
256 |
+
f'Starting training for {epochs} epochs...')
|
257 |
+
for epoch in range(start_epoch, epochs): # epoch ------------------------------------------------------------------
|
258 |
+
callbacks.run('on_train_epoch_start')
|
259 |
+
model.train()
|
260 |
+
|
261 |
+
# Update image weights (optional, single-GPU only)
|
262 |
+
if opt.image_weights:
|
263 |
+
cw = model.class_weights.cpu().numpy() * (1 - maps) ** 2 / nc # class weights
|
264 |
+
iw = labels_to_image_weights(dataset.labels, nc=nc, class_weights=cw) # image weights
|
265 |
+
dataset.indices = random.choices(range(dataset.n), weights=iw, k=dataset.n) # rand weighted idx
|
266 |
+
if epoch == (epochs - opt.close_mosaic):
|
267 |
+
LOGGER.info("Closing dataloader mosaic")
|
268 |
+
dataset.mosaic = False
|
269 |
+
|
270 |
+
# Update mosaic border (optional)
|
271 |
+
# b = int(random.uniform(0.25 * imgsz, 0.75 * imgsz + gs) // gs * gs)
|
272 |
+
# dataset.mosaic_border = [b - imgsz, -b] # height, width borders
|
273 |
+
|
274 |
+
mloss = torch.zeros(3, device=device) # mean losses
|
275 |
+
if RANK != -1:
|
276 |
+
train_loader.sampler.set_epoch(epoch)
|
277 |
+
pbar = enumerate(train_loader)
|
278 |
+
LOGGER.info(('\n' + '%11s' * 7) % ('Epoch', 'GPU_mem', 'box_loss', 'cls_loss', 'dfl_loss', 'Instances', 'Size'))
|
279 |
+
if RANK in {-1, 0}:
|
280 |
+
pbar = tqdm(pbar, total=nb, bar_format=TQDM_BAR_FORMAT) # progress bar
|
281 |
+
optimizer.zero_grad()
|
282 |
+
for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
|
283 |
+
callbacks.run('on_train_batch_start')
|
284 |
+
ni = i + nb * epoch # number integrated batches (since train start)
|
285 |
+
imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0
|
286 |
+
|
287 |
+
# Warmup
|
288 |
+
if ni <= nw:
|
289 |
+
xi = [0, nw] # x interp
|
290 |
+
# compute_loss.gr = np.interp(ni, xi, [0.0, 1.0]) # iou loss ratio (obj_loss = 1.0 or iou)
|
291 |
+
accumulate = max(1, np.interp(ni, xi, [1, nbs / batch_size]).round())
|
292 |
+
for j, x in enumerate(optimizer.param_groups):
|
293 |
+
# bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0
|
294 |
+
x['lr'] = np.interp(ni, xi, [hyp['warmup_bias_lr'] if j == 0 else 0.0, x['initial_lr'] * lf(epoch)])
|
295 |
+
if 'momentum' in x:
|
296 |
+
x['momentum'] = np.interp(ni, xi, [hyp['warmup_momentum'], hyp['momentum']])
|
297 |
+
|
298 |
+
# Multi-scale
|
299 |
+
if opt.multi_scale:
|
300 |
+
sz = random.randrange(imgsz * 0.5, imgsz * 1.5 + gs) // gs * gs # size
|
301 |
+
sf = sz / max(imgs.shape[2:]) # scale factor
|
302 |
+
if sf != 1:
|
303 |
+
ns = [math.ceil(x * sf / gs) * gs for x in imgs.shape[2:]] # new shape (stretched to gs-multiple)
|
304 |
+
imgs = nn.functional.interpolate(imgs, size=ns, mode='bilinear', align_corners=False)
|
305 |
+
|
306 |
+
# Forward
|
307 |
+
with torch.cuda.amp.autocast(amp):
|
308 |
+
pred = model(imgs) # forward
|
309 |
+
loss, loss_items = compute_loss(pred, targets.to(device)) # loss scaled by batch_size
|
310 |
+
if RANK != -1:
|
311 |
+
loss *= WORLD_SIZE # gradient averaged between devices in DDP mode
|
312 |
+
if opt.quad:
|
313 |
+
loss *= 4.
|
314 |
+
|
315 |
+
# Backward
|
316 |
+
scaler.scale(loss).backward()
|
317 |
+
|
318 |
+
# Optimize - https://pytorch.org/docs/master/notes/amp_examples.html
|
319 |
+
if ni - last_opt_step >= accumulate:
|
320 |
+
scaler.unscale_(optimizer) # unscale gradients
|
321 |
+
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=10.0) # clip gradients
|
322 |
+
scaler.step(optimizer) # optimizer.step
|
323 |
+
scaler.update()
|
324 |
+
optimizer.zero_grad()
|
325 |
+
if ema:
|
326 |
+
ema.update(model)
|
327 |
+
last_opt_step = ni
|
328 |
+
|
329 |
+
# Log
|
330 |
+
if RANK in {-1, 0}:
|
331 |
+
mloss = (mloss * i + loss_items) / (i + 1) # update mean losses
|
332 |
+
mem = f'{torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0:.3g}G' # (GB)
|
333 |
+
pbar.set_description(('%11s' * 2 + '%11.4g' * 5) %
|
334 |
+
(f'{epoch}/{epochs - 1}', mem, *mloss, targets.shape[0], imgs.shape[-1]))
|
335 |
+
callbacks.run('on_train_batch_end', model, ni, imgs, targets, paths, list(mloss))
|
336 |
+
if callbacks.stop_training:
|
337 |
+
return
|
338 |
+
# end batch ------------------------------------------------------------------------------------------------
|
339 |
+
|
340 |
+
# Scheduler
|
341 |
+
lr = [x['lr'] for x in optimizer.param_groups] # for loggers
|
342 |
+
scheduler.step()
|
343 |
+
|
344 |
+
if RANK in {-1, 0}:
|
345 |
+
# mAP
|
346 |
+
callbacks.run('on_train_epoch_end', epoch=epoch)
|
347 |
+
ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'names', 'stride', 'class_weights'])
|
348 |
+
final_epoch = (epoch + 1 == epochs) or stopper.possible_stop
|
349 |
+
if not noval or final_epoch: # Calculate mAP
|
350 |
+
results, maps, _ = validate.run(data_dict,
|
351 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
352 |
+
imgsz=imgsz,
|
353 |
+
half=amp,
|
354 |
+
model=ema.ema,
|
355 |
+
single_cls=single_cls,
|
356 |
+
dataloader=val_loader,
|
357 |
+
save_dir=save_dir,
|
358 |
+
plots=False,
|
359 |
+
callbacks=callbacks,
|
360 |
+
compute_loss=compute_loss)
|
361 |
+
|
362 |
+
# Update best mAP
|
363 |
+
fi = fitness(np.array(results).reshape(1, -1)) # weighted combination of [P, R, [email protected], [email protected]]
|
364 |
+
stop = stopper(epoch=epoch, fitness=fi) # early stop check
|
365 |
+
if fi > best_fitness:
|
366 |
+
best_fitness = fi
|
367 |
+
log_vals = list(mloss) + list(results) + lr
|
368 |
+
callbacks.run('on_fit_epoch_end', log_vals, epoch, best_fitness, fi)
|
369 |
+
|
370 |
+
# Save model
|
371 |
+
if (not nosave) or (final_epoch and not evolve): # if save
|
372 |
+
ckpt = {
|
373 |
+
'epoch': epoch,
|
374 |
+
'best_fitness': best_fitness,
|
375 |
+
'model': deepcopy(de_parallel(model)).half(),
|
376 |
+
'ema': deepcopy(ema.ema).half(),
|
377 |
+
'updates': ema.updates,
|
378 |
+
'optimizer': optimizer.state_dict(),
|
379 |
+
'opt': vars(opt),
|
380 |
+
'git': GIT_INFO, # {remote, branch, commit} if a git repo
|
381 |
+
'date': datetime.now().isoformat()}
|
382 |
+
|
383 |
+
# Save last, best and delete
|
384 |
+
torch.save(ckpt, last)
|
385 |
+
if best_fitness == fi:
|
386 |
+
torch.save(ckpt, best)
|
387 |
+
if opt.save_period > 0 and epoch % opt.save_period == 0:
|
388 |
+
torch.save(ckpt, w / f'epoch{epoch}.pt')
|
389 |
+
del ckpt
|
390 |
+
callbacks.run('on_model_save', last, epoch, final_epoch, best_fitness, fi)
|
391 |
+
|
392 |
+
# EarlyStopping
|
393 |
+
if RANK != -1: # if DDP training
|
394 |
+
broadcast_list = [stop if RANK == 0 else None]
|
395 |
+
dist.broadcast_object_list(broadcast_list, 0) # broadcast 'stop' to all ranks
|
396 |
+
if RANK != 0:
|
397 |
+
stop = broadcast_list[0]
|
398 |
+
if stop:
|
399 |
+
break # must break all DDP ranks
|
400 |
+
|
401 |
+
# end epoch ----------------------------------------------------------------------------------------------------
|
402 |
+
# end training -----------------------------------------------------------------------------------------------------
|
403 |
+
if RANK in {-1, 0}:
|
404 |
+
LOGGER.info(f'\n{epoch - start_epoch + 1} epochs completed in {(time.time() - t0) / 3600:.3f} hours.')
|
405 |
+
for f in last, best:
|
406 |
+
if f.exists():
|
407 |
+
strip_optimizer(f) # strip optimizers
|
408 |
+
if f is best:
|
409 |
+
LOGGER.info(f'\nValidating {f}...')
|
410 |
+
results, _, _ = validate.run(
|
411 |
+
data_dict,
|
412 |
+
batch_size=batch_size // WORLD_SIZE * 2,
|
413 |
+
imgsz=imgsz,
|
414 |
+
model=attempt_load(f, device).half(),
|
415 |
+
single_cls=single_cls,
|
416 |
+
dataloader=val_loader,
|
417 |
+
save_dir=save_dir,
|
418 |
+
save_json=is_coco,
|
419 |
+
verbose=True,
|
420 |
+
plots=plots,
|
421 |
+
callbacks=callbacks,
|
422 |
+
compute_loss=compute_loss) # val best model with plots
|
423 |
+
if is_coco:
|
424 |
+
callbacks.run('on_fit_epoch_end', list(mloss) + list(results) + lr, epoch, best_fitness, fi)
|
425 |
+
|
426 |
+
callbacks.run('on_train_end', last, best, epoch, results)
|
427 |
+
|
428 |
+
torch.cuda.empty_cache()
|
429 |
+
return results
|
430 |
+
|
431 |
+
|
432 |
+
def parse_opt(known=False):
|
433 |
+
parser = argparse.ArgumentParser()
|
434 |
+
# parser.add_argument('--weights', type=str, default=ROOT / 'yolo.pt', help='initial weights path')
|
435 |
+
# parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
|
436 |
+
parser.add_argument('--weights', type=str, default='', help='initial weights path')
|
437 |
+
parser.add_argument('--cfg', type=str, default='yolo.yaml', help='model.yaml path')
|
438 |
+
parser.add_argument('--data', type=str, default=ROOT / 'data/coco.yaml', help='dataset.yaml path')
|
439 |
+
parser.add_argument('--hyp', type=str, default=ROOT / 'data/hyps/hyp.scratch-high.yaml', help='hyperparameters path')
|
440 |
+
parser.add_argument('--epochs', type=int, default=100, help='total training epochs')
|
441 |
+
parser.add_argument('--batch-size', type=int, default=16, help='total batch size for all GPUs, -1 for autobatch')
|
442 |
+
parser.add_argument('--imgsz', '--img', '--img-size', type=int, default=640, help='train, val image size (pixels)')
|
443 |
+
parser.add_argument('--rect', action='store_true', help='rectangular training')
|
444 |
+
parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training')
|
445 |
+
parser.add_argument('--nosave', action='store_true', help='only save final checkpoint')
|
446 |
+
parser.add_argument('--noval', action='store_true', help='only validate final epoch')
|
447 |
+
parser.add_argument('--noautoanchor', action='store_true', help='disable AutoAnchor')
|
448 |
+
parser.add_argument('--noplots', action='store_true', help='save no plot files')
|
449 |
+
parser.add_argument('--evolve', type=int, nargs='?', const=300, help='evolve hyperparameters for x generations')
|
450 |
+
parser.add_argument('--bucket', type=str, default='', help='gsutil bucket')
|
451 |
+
parser.add_argument('--cache', type=str, nargs='?', const='ram', help='image --cache ram/disk')
|
452 |
+
parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training')
|
453 |
+
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
454 |
+
parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%')
|
455 |
+
parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class')
|
456 |
+
parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'AdamW', 'LION'], default='SGD', help='optimizer')
|
457 |
+
parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
|
458 |
+
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
|
459 |
+
parser.add_argument('--project', default=ROOT / 'runs/train', help='save to project/name')
|
460 |
+
parser.add_argument('--name', default='exp', help='save to project/name')
|
461 |
+
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
|
462 |
+
parser.add_argument('--quad', action='store_true', help='quad dataloader')
|
463 |
+
parser.add_argument('--cos-lr', action='store_true', help='cosine LR scheduler')
|
464 |
+
parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon')
|
465 |
+
parser.add_argument('--patience', type=int, default=100, help='EarlyStopping patience (epochs without improvement)')
|
466 |
+
parser.add_argument('--freeze', nargs='+', type=int, default=[0], help='Freeze layers: backbone=10, first3=0 1 2')
|
467 |
+
parser.add_argument('--save-period', type=int, default=-1, help='Save checkpoint every x epochs (disabled if < 1)')
|
468 |
+
parser.add_argument('--seed', type=int, default=0, help='Global training seed')
|
469 |
+
parser.add_argument('--local_rank', type=int, default=-1, help='Automatic DDP Multi-GPU argument, do not modify')
|
470 |
+
parser.add_argument('--min-items', type=int, default=0, help='Experimental')
|
471 |
+
parser.add_argument('--close-mosaic', type=int, default=0, help='Experimental')
|
472 |
+
|
473 |
+
# Logger arguments
|
474 |
+
parser.add_argument('--entity', default=None, help='Entity')
|
475 |
+
parser.add_argument('--upload_dataset', nargs='?', const=True, default=False, help='Upload data, "val" option')
|
476 |
+
parser.add_argument('--bbox_interval', type=int, default=-1, help='Set bounding-box image logging interval')
|
477 |
+
parser.add_argument('--artifact_alias', type=str, default='latest', help='Version of dataset artifact to use')
|
478 |
+
|
479 |
+
return parser.parse_known_args()[0] if known else parser.parse_args()
|
480 |
+
|
481 |
+
|
482 |
+
def main(opt, callbacks=Callbacks()):
|
483 |
+
# Checks
|
484 |
+
if RANK in {-1, 0}:
|
485 |
+
print_args(vars(opt))
|
486 |
+
#check_git_status()
|
487 |
+
#check_requirements()
|
488 |
+
|
489 |
+
# Resume (from specified or most recent last.pt)
|
490 |
+
if opt.resume and not check_comet_resume(opt) and not opt.evolve:
|
491 |
+
last = Path(check_file(opt.resume) if isinstance(opt.resume, str) else get_latest_run())
|
492 |
+
opt_yaml = last.parent.parent / 'opt.yaml' # train options yaml
|
493 |
+
opt_data = opt.data # original dataset
|
494 |
+
if opt_yaml.is_file():
|
495 |
+
with open(opt_yaml, errors='ignore') as f:
|
496 |
+
d = yaml.safe_load(f)
|
497 |
+
else:
|
498 |
+
d = torch.load(last, map_location='cpu')['opt']
|
499 |
+
opt = argparse.Namespace(**d) # replace
|
500 |
+
opt.cfg, opt.weights, opt.resume = '', str(last), True # reinstate
|
501 |
+
if is_url(opt_data):
|
502 |
+
opt.data = check_file(opt_data) # avoid HUB resume auth timeout
|
503 |
+
else:
|
504 |
+
opt.data, opt.cfg, opt.hyp, opt.weights, opt.project = \
|
505 |
+
check_file(opt.data), check_yaml(opt.cfg), check_yaml(opt.hyp), str(opt.weights), str(opt.project) # checks
|
506 |
+
assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified'
|
507 |
+
if opt.evolve:
|
508 |
+
if opt.project == str(ROOT / 'runs/train'): # if default project name, rename to runs/evolve
|
509 |
+
opt.project = str(ROOT / 'runs/evolve')
|
510 |
+
opt.exist_ok, opt.resume = opt.resume, False # pass resume to exist_ok and disable resume
|
511 |
+
if opt.name == 'cfg':
|
512 |
+
opt.name = Path(opt.cfg).stem # use model.yaml as name
|
513 |
+
opt.save_dir = str(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok))
|
514 |
+
|
515 |
+
# DDP mode
|
516 |
+
device = select_device(opt.device, batch_size=opt.batch_size)
|
517 |
+
if LOCAL_RANK != -1:
|
518 |
+
msg = 'is not compatible with YOLO Multi-GPU DDP training'
|
519 |
+
assert not opt.image_weights, f'--image-weights {msg}'
|
520 |
+
assert not opt.evolve, f'--evolve {msg}'
|
521 |
+
assert opt.batch_size != -1, f'AutoBatch with --batch-size -1 {msg}, please pass a valid --batch-size'
|
522 |
+
assert opt.batch_size % WORLD_SIZE == 0, f'--batch-size {opt.batch_size} must be multiple of WORLD_SIZE'
|
523 |
+
assert torch.cuda.device_count() > LOCAL_RANK, 'insufficient CUDA devices for DDP command'
|
524 |
+
torch.cuda.set_device(LOCAL_RANK)
|
525 |
+
device = torch.device('cuda', LOCAL_RANK)
|
526 |
+
dist.init_process_group(backend="nccl" if dist.is_nccl_available() else "gloo")
|
527 |
+
|
528 |
+
# Train
|
529 |
+
if not opt.evolve:
|
530 |
+
train(opt.hyp, opt, device, callbacks)
|
531 |
+
|
532 |
+
# Evolve hyperparameters (optional)
|
533 |
+
else:
|
534 |
+
# Hyperparameter evolution metadata (mutation scale 0-1, lower_limit, upper_limit)
|
535 |
+
meta = {
|
536 |
+
'lr0': (1, 1e-5, 1e-1), # initial learning rate (SGD=1E-2, Adam=1E-3)
|
537 |
+
'lrf': (1, 0.01, 1.0), # final OneCycleLR learning rate (lr0 * lrf)
|
538 |
+
'momentum': (0.3, 0.6, 0.98), # SGD momentum/Adam beta1
|
539 |
+
'weight_decay': (1, 0.0, 0.001), # optimizer weight decay
|
540 |
+
'warmup_epochs': (1, 0.0, 5.0), # warmup epochs (fractions ok)
|
541 |
+
'warmup_momentum': (1, 0.0, 0.95), # warmup initial momentum
|
542 |
+
'warmup_bias_lr': (1, 0.0, 0.2), # warmup initial bias lr
|
543 |
+
'box': (1, 0.02, 0.2), # box loss gain
|
544 |
+
'cls': (1, 0.2, 4.0), # cls loss gain
|
545 |
+
'cls_pw': (1, 0.5, 2.0), # cls BCELoss positive_weight
|
546 |
+
'obj': (1, 0.2, 4.0), # obj loss gain (scale with pixels)
|
547 |
+
'obj_pw': (1, 0.5, 2.0), # obj BCELoss positive_weight
|
548 |
+
'iou_t': (0, 0.1, 0.7), # IoU training threshold
|
549 |
+
'anchor_t': (1, 2.0, 8.0), # anchor-multiple threshold
|
550 |
+
'anchors': (2, 2.0, 10.0), # anchors per output grid (0 to ignore)
|
551 |
+
'fl_gamma': (0, 0.0, 2.0), # focal loss gamma (efficientDet default gamma=1.5)
|
552 |
+
'hsv_h': (1, 0.0, 0.1), # image HSV-Hue augmentation (fraction)
|
553 |
+
'hsv_s': (1, 0.0, 0.9), # image HSV-Saturation augmentation (fraction)
|
554 |
+
'hsv_v': (1, 0.0, 0.9), # image HSV-Value augmentation (fraction)
|
555 |
+
'degrees': (1, 0.0, 45.0), # image rotation (+/- deg)
|
556 |
+
'translate': (1, 0.0, 0.9), # image translation (+/- fraction)
|
557 |
+
'scale': (1, 0.0, 0.9), # image scale (+/- gain)
|
558 |
+
'shear': (1, 0.0, 10.0), # image shear (+/- deg)
|
559 |
+
'perspective': (0, 0.0, 0.001), # image perspective (+/- fraction), range 0-0.001
|
560 |
+
'flipud': (1, 0.0, 1.0), # image flip up-down (probability)
|
561 |
+
'fliplr': (0, 0.0, 1.0), # image flip left-right (probability)
|
562 |
+
'mosaic': (1, 0.0, 1.0), # image mixup (probability)
|
563 |
+
'mixup': (1, 0.0, 1.0), # image mixup (probability)
|
564 |
+
'copy_paste': (1, 0.0, 1.0)} # segment copy-paste (probability)
|
565 |
+
|
566 |
+
with open(opt.hyp, errors='ignore') as f:
|
567 |
+
hyp = yaml.safe_load(f) # load hyps dict
|
568 |
+
if 'anchors' not in hyp: # anchors commented in hyp.yaml
|
569 |
+
hyp['anchors'] = 3
|
570 |
+
if opt.noautoanchor:
|
571 |
+
del hyp['anchors'], meta['anchors']
|
572 |
+
opt.noval, opt.nosave, save_dir = True, True, Path(opt.save_dir) # only val/save final epoch
|
573 |
+
# ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
|
574 |
+
evolve_yaml, evolve_csv = save_dir / 'hyp_evolve.yaml', save_dir / 'evolve.csv'
|
575 |
+
if opt.bucket:
|
576 |
+
os.system(f'gsutil cp gs://{opt.bucket}/evolve.csv {evolve_csv}') # download evolve.csv if exists
|
577 |
+
|
578 |
+
for _ in range(opt.evolve): # generations to evolve
|
579 |
+
if evolve_csv.exists(): # if evolve.csv exists: select best hyps and mutate
|
580 |
+
# Select parent(s)
|
581 |
+
parent = 'single' # parent selection method: 'single' or 'weighted'
|
582 |
+
x = np.loadtxt(evolve_csv, ndmin=2, delimiter=',', skiprows=1)
|
583 |
+
n = min(5, len(x)) # number of previous results to consider
|
584 |
+
x = x[np.argsort(-fitness(x))][:n] # top n mutations
|
585 |
+
w = fitness(x) - fitness(x).min() + 1E-6 # weights (sum > 0)
|
586 |
+
if parent == 'single' or len(x) == 1:
|
587 |
+
# x = x[random.randint(0, n - 1)] # random selection
|
588 |
+
x = x[random.choices(range(n), weights=w)[0]] # weighted selection
|
589 |
+
elif parent == 'weighted':
|
590 |
+
x = (x * w.reshape(n, 1)).sum(0) / w.sum() # weighted combination
|
591 |
+
|
592 |
+
# Mutate
|
593 |
+
mp, s = 0.8, 0.2 # mutation probability, sigma
|
594 |
+
npr = np.random
|
595 |
+
npr.seed(int(time.time()))
|
596 |
+
g = np.array([meta[k][0] for k in hyp.keys()]) # gains 0-1
|
597 |
+
ng = len(meta)
|
598 |
+
v = np.ones(ng)
|
599 |
+
while all(v == 1): # mutate until a change occurs (prevent duplicates)
|
600 |
+
v = (g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1).clip(0.3, 3.0)
|
601 |
+
for i, k in enumerate(hyp.keys()): # plt.hist(v.ravel(), 300)
|
602 |
+
hyp[k] = float(x[i + 7] * v[i]) # mutate
|
603 |
+
|
604 |
+
# Constrain to limits
|
605 |
+
for k, v in meta.items():
|
606 |
+
hyp[k] = max(hyp[k], v[1]) # lower limit
|
607 |
+
hyp[k] = min(hyp[k], v[2]) # upper limit
|
608 |
+
hyp[k] = round(hyp[k], 5) # significant digits
|
609 |
+
|
610 |
+
# Train mutation
|
611 |
+
results = train(hyp.copy(), opt, device, callbacks)
|
612 |
+
callbacks = Callbacks()
|
613 |
+
# Write mutation results
|
614 |
+
keys = ('metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95', 'val/box_loss',
|
615 |
+
'val/obj_loss', 'val/cls_loss')
|
616 |
+
print_mutation(keys, results, hyp.copy(), save_dir, opt.bucket)
|
617 |
+
|
618 |
+
# Plot results
|
619 |
+
plot_evolve(evolve_csv)
|
620 |
+
LOGGER.info(f'Hyperparameter evolution finished {opt.evolve} generations\n'
|
621 |
+
f"Results saved to {colorstr('bold', save_dir)}\n"
|
622 |
+
f'Usage example: $ python train.py --hyp {evolve_yaml}')
|
623 |
+
|
624 |
+
|
625 |
+
def run(**kwargs):
|
626 |
+
# Usage: import train; train.run(data='coco128.yaml', imgsz=320, weights='yolo.pt')
|
627 |
+
opt = parse_opt(True)
|
628 |
+
for k, v in kwargs.items():
|
629 |
+
setattr(opt, k, v)
|
630 |
+
main(opt)
|
631 |
+
return opt
|
632 |
+
|
633 |
+
|
634 |
+
if __name__ == "__main__":
|
635 |
+
opt = parse_opt()
|
636 |
+
main(opt)
|
utils/__init__.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import contextlib
|
2 |
+
import platform
|
3 |
+
import threading
|
4 |
+
|
5 |
+
|
6 |
+
def emojis(str=''):
|
7 |
+
# Return platform-dependent emoji-safe version of string
|
8 |
+
return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str
|
9 |
+
|
10 |
+
|
11 |
+
class TryExcept(contextlib.ContextDecorator):
|
12 |
+
# YOLOv5 TryExcept class. Usage: @TryExcept() decorator or 'with TryExcept():' context manager
|
13 |
+
def __init__(self, msg=''):
|
14 |
+
self.msg = msg
|
15 |
+
|
16 |
+
def __enter__(self):
|
17 |
+
pass
|
18 |
+
|
19 |
+
def __exit__(self, exc_type, value, traceback):
|
20 |
+
if value:
|
21 |
+
print(emojis(f"{self.msg}{': ' if self.msg else ''}{value}"))
|
22 |
+
return True
|
23 |
+
|
24 |
+
|
25 |
+
def threaded(func):
|
26 |
+
# Multi-threads a target function and returns thread. Usage: @threaded decorator
|
27 |
+
def wrapper(*args, **kwargs):
|
28 |
+
thread = threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True)
|
29 |
+
thread.start()
|
30 |
+
return thread
|
31 |
+
|
32 |
+
return wrapper
|
33 |
+
|
34 |
+
|
35 |
+
def join_threads(verbose=False):
|
36 |
+
# Join all daemon threads, i.e. atexit.register(lambda: join_threads())
|
37 |
+
main_thread = threading.current_thread()
|
38 |
+
for t in threading.enumerate():
|
39 |
+
if t is not main_thread:
|
40 |
+
if verbose:
|
41 |
+
print(f'Joining thread {t.name}')
|
42 |
+
t.join()
|
43 |
+
|
44 |
+
|
45 |
+
def notebook_init(verbose=True):
|
46 |
+
# Check system software and hardware
|
47 |
+
print('Checking setup...')
|
48 |
+
|
49 |
+
import os
|
50 |
+
import shutil
|
51 |
+
|
52 |
+
from utils.general import check_font, check_requirements, is_colab
|
53 |
+
from utils.torch_utils import select_device # imports
|
54 |
+
|
55 |
+
check_font()
|
56 |
+
|
57 |
+
import psutil
|
58 |
+
from IPython import display # to display images and clear console output
|
59 |
+
|
60 |
+
if is_colab():
|
61 |
+
shutil.rmtree('/content/sample_data', ignore_errors=True) # remove colab /sample_data directory
|
62 |
+
|
63 |
+
# System info
|
64 |
+
if verbose:
|
65 |
+
gb = 1 << 30 # bytes to GiB (1024 ** 3)
|
66 |
+
ram = psutil.virtual_memory().total
|
67 |
+
total, used, free = shutil.disk_usage("/")
|
68 |
+
display.clear_output()
|
69 |
+
s = f'({os.cpu_count()} CPUs, {ram / gb:.1f} GB RAM, {(total - free) / gb:.1f}/{total / gb:.1f} GB disk)'
|
70 |
+
else:
|
71 |
+
s = ''
|
72 |
+
|
73 |
+
select_device(newline=False)
|
74 |
+
print(emojis(f'Setup complete ✅ {s}'))
|
75 |
+
return display
|
utils/__pycache__/__init__.cpython-310.pyc
ADDED
Binary file (2.49 kB). View file
|
|
utils/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (4.04 kB). View file
|
|
utils/__pycache__/augmentations.cpython-310.pyc
ADDED
Binary file (13.7 kB). View file
|
|
utils/__pycache__/augmentations.cpython-311.pyc
ADDED
Binary file (28.3 kB). View file
|
|
utils/__pycache__/autoanchor.cpython-310.pyc
ADDED
Binary file (6.43 kB). View file
|
|
utils/__pycache__/autobatch.cpython-310.pyc
ADDED
Binary file (2.48 kB). View file
|
|
utils/__pycache__/callbacks.cpython-310.pyc
ADDED
Binary file (2.68 kB). View file
|
|
utils/__pycache__/dataloaders.cpython-310.pyc
ADDED
Binary file (43.1 kB). View file
|
|
utils/__pycache__/dataloaders.cpython-311.pyc
ADDED
Binary file (90.3 kB). View file
|
|
utils/__pycache__/downloads.cpython-310.pyc
ADDED
Binary file (3.97 kB). View file
|
|
utils/__pycache__/downloads.cpython-311.pyc
ADDED
Binary file (8.11 kB). View file
|
|
utils/__pycache__/general.cpython-310.pyc
ADDED
Binary file (38.8 kB). View file
|
|
utils/__pycache__/general.cpython-311.pyc
ADDED
Binary file (78.9 kB). View file
|
|
utils/__pycache__/lion.cpython-310.pyc
ADDED
Binary file (2.34 kB). View file
|
|
utils/__pycache__/lion.cpython-311.pyc
ADDED
Binary file (3.89 kB). View file
|
|
utils/__pycache__/loss_tal_dual.cpython-310.pyc
ADDED
Binary file (10.5 kB). View file
|
|
utils/__pycache__/metrics.cpython-310.pyc
ADDED
Binary file (12.7 kB). View file
|
|
utils/__pycache__/metrics.cpython-311.pyc
ADDED
Binary file (25.8 kB). View file
|
|
utils/__pycache__/plots.cpython-310.pyc
ADDED
Binary file (21.6 kB). View file
|
|
utils/__pycache__/plots.cpython-311.pyc
ADDED
Binary file (46.5 kB). View file
|
|
utils/__pycache__/torch_utils.cpython-310.pyc
ADDED
Binary file (18.2 kB). View file
|
|
utils/__pycache__/torch_utils.cpython-311.pyc
ADDED
Binary file (38.8 kB). View file
|
|
utils/activations.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
|
5 |
+
|
6 |
+
class SiLU(nn.Module):
|
7 |
+
# SiLU activation https://arxiv.org/pdf/1606.08415.pdf
|
8 |
+
@staticmethod
|
9 |
+
def forward(x):
|
10 |
+
return x * torch.sigmoid(x)
|
11 |
+
|
12 |
+
|
13 |
+
class Hardswish(nn.Module):
|
14 |
+
# Hard-SiLU activation
|
15 |
+
@staticmethod
|
16 |
+
def forward(x):
|
17 |
+
# return x * F.hardsigmoid(x) # for TorchScript and CoreML
|
18 |
+
return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for TorchScript, CoreML and ONNX
|
19 |
+
|
20 |
+
|
21 |
+
class Mish(nn.Module):
|
22 |
+
# Mish activation https://github.com/digantamisra98/Mish
|
23 |
+
@staticmethod
|
24 |
+
def forward(x):
|
25 |
+
return x * F.softplus(x).tanh()
|
26 |
+
|
27 |
+
|
28 |
+
class MemoryEfficientMish(nn.Module):
|
29 |
+
# Mish activation memory-efficient
|
30 |
+
class F(torch.autograd.Function):
|
31 |
+
|
32 |
+
@staticmethod
|
33 |
+
def forward(ctx, x):
|
34 |
+
ctx.save_for_backward(x)
|
35 |
+
return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x)))
|
36 |
+
|
37 |
+
@staticmethod
|
38 |
+
def backward(ctx, grad_output):
|
39 |
+
x = ctx.saved_tensors[0]
|
40 |
+
sx = torch.sigmoid(x)
|
41 |
+
fx = F.softplus(x).tanh()
|
42 |
+
return grad_output * (fx + x * sx * (1 - fx * fx))
|
43 |
+
|
44 |
+
def forward(self, x):
|
45 |
+
return self.F.apply(x)
|
46 |
+
|
47 |
+
|
48 |
+
class FReLU(nn.Module):
|
49 |
+
# FReLU activation https://arxiv.org/abs/2007.11824
|
50 |
+
def __init__(self, c1, k=3): # ch_in, kernel
|
51 |
+
super().__init__()
|
52 |
+
self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False)
|
53 |
+
self.bn = nn.BatchNorm2d(c1)
|
54 |
+
|
55 |
+
def forward(self, x):
|
56 |
+
return torch.max(x, self.bn(self.conv(x)))
|
57 |
+
|
58 |
+
|
59 |
+
class AconC(nn.Module):
|
60 |
+
r""" ACON activation (activate or not)
|
61 |
+
AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter
|
62 |
+
according to "Activate or Not: Learning Customized Activation" <https://arxiv.org/pdf/2009.04759.pdf>.
|
63 |
+
"""
|
64 |
+
|
65 |
+
def __init__(self, c1):
|
66 |
+
super().__init__()
|
67 |
+
self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
68 |
+
self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
69 |
+
self.beta = nn.Parameter(torch.ones(1, c1, 1, 1))
|
70 |
+
|
71 |
+
def forward(self, x):
|
72 |
+
dpx = (self.p1 - self.p2) * x
|
73 |
+
return dpx * torch.sigmoid(self.beta * dpx) + self.p2 * x
|
74 |
+
|
75 |
+
|
76 |
+
class MetaAconC(nn.Module):
|
77 |
+
r""" ACON activation (activate or not)
|
78 |
+
MetaAconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is generated by a small network
|
79 |
+
according to "Activate or Not: Learning Customized Activation" <https://arxiv.org/pdf/2009.04759.pdf>.
|
80 |
+
"""
|
81 |
+
|
82 |
+
def __init__(self, c1, k=1, s=1, r=16): # ch_in, kernel, stride, r
|
83 |
+
super().__init__()
|
84 |
+
c2 = max(r, c1 // r)
|
85 |
+
self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
86 |
+
self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
87 |
+
self.fc1 = nn.Conv2d(c1, c2, k, s, bias=True)
|
88 |
+
self.fc2 = nn.Conv2d(c2, c1, k, s, bias=True)
|
89 |
+
# self.bn1 = nn.BatchNorm2d(c2)
|
90 |
+
# self.bn2 = nn.BatchNorm2d(c1)
|
91 |
+
|
92 |
+
def forward(self, x):
|
93 |
+
y = x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True)
|
94 |
+
# batch-size 1 bug/instabilities https://github.com/ultralytics/yolov5/issues/2891
|
95 |
+
# beta = torch.sigmoid(self.bn2(self.fc2(self.bn1(self.fc1(y))))) # bug/unstable
|
96 |
+
beta = torch.sigmoid(self.fc2(self.fc1(y))) # bug patch BN layers removed
|
97 |
+
dpx = (self.p1 - self.p2) * x
|
98 |
+
return dpx * torch.sigmoid(beta * dpx) + self.p2 * x
|
utils/augmentations.py
ADDED
@@ -0,0 +1,395 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import math
|
2 |
+
import random
|
3 |
+
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
import torch
|
7 |
+
import torchvision.transforms as T
|
8 |
+
import torchvision.transforms.functional as TF
|
9 |
+
|
10 |
+
from utils.general import LOGGER, check_version, colorstr, resample_segments, segment2box, xywhn2xyxy
|
11 |
+
from utils.metrics import bbox_ioa
|
12 |
+
|
13 |
+
IMAGENET_MEAN = 0.485, 0.456, 0.406 # RGB mean
|
14 |
+
IMAGENET_STD = 0.229, 0.224, 0.225 # RGB standard deviation
|
15 |
+
|
16 |
+
|
17 |
+
class Albumentations:
|
18 |
+
# YOLOv5 Albumentations class (optional, only used if package is installed)
|
19 |
+
def __init__(self, size=640):
|
20 |
+
self.transform = None
|
21 |
+
prefix = colorstr('albumentations: ')
|
22 |
+
try:
|
23 |
+
import albumentations as A
|
24 |
+
check_version(A.__version__, '1.0.3', hard=True) # version requirement
|
25 |
+
|
26 |
+
T = [
|
27 |
+
A.RandomResizedCrop(height=size, width=size, scale=(0.8, 1.0), ratio=(0.9, 1.11), p=0.0),
|
28 |
+
A.Blur(p=0.01),
|
29 |
+
A.MedianBlur(p=0.01),
|
30 |
+
A.ToGray(p=0.01),
|
31 |
+
A.CLAHE(p=0.01),
|
32 |
+
A.RandomBrightnessContrast(p=0.0),
|
33 |
+
A.RandomGamma(p=0.0),
|
34 |
+
A.ImageCompression(quality_lower=75, p=0.0)] # transforms
|
35 |
+
self.transform = A.Compose(T, bbox_params=A.BboxParams(format='yolo', label_fields=['class_labels']))
|
36 |
+
|
37 |
+
LOGGER.info(prefix + ', '.join(f'{x}'.replace('always_apply=False, ', '') for x in T if x.p))
|
38 |
+
except ImportError: # package not installed, skip
|
39 |
+
pass
|
40 |
+
except Exception as e:
|
41 |
+
LOGGER.info(f'{prefix}{e}')
|
42 |
+
|
43 |
+
def __call__(self, im, labels, p=1.0):
|
44 |
+
if self.transform and random.random() < p:
|
45 |
+
new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0]) # transformed
|
46 |
+
im, labels = new['image'], np.array([[c, *b] for c, b in zip(new['class_labels'], new['bboxes'])])
|
47 |
+
return im, labels
|
48 |
+
|
49 |
+
|
50 |
+
def normalize(x, mean=IMAGENET_MEAN, std=IMAGENET_STD, inplace=False):
|
51 |
+
# Denormalize RGB images x per ImageNet stats in BCHW format, i.e. = (x - mean) / std
|
52 |
+
return TF.normalize(x, mean, std, inplace=inplace)
|
53 |
+
|
54 |
+
|
55 |
+
def denormalize(x, mean=IMAGENET_MEAN, std=IMAGENET_STD):
|
56 |
+
# Denormalize RGB images x per ImageNet stats in BCHW format, i.e. = x * std + mean
|
57 |
+
for i in range(3):
|
58 |
+
x[:, i] = x[:, i] * std[i] + mean[i]
|
59 |
+
return x
|
60 |
+
|
61 |
+
|
62 |
+
def augment_hsv(im, hgain=0.5, sgain=0.5, vgain=0.5):
|
63 |
+
# HSV color-space augmentation
|
64 |
+
if hgain or sgain or vgain:
|
65 |
+
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
|
66 |
+
hue, sat, val = cv2.split(cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
|
67 |
+
dtype = im.dtype # uint8
|
68 |
+
|
69 |
+
x = np.arange(0, 256, dtype=r.dtype)
|
70 |
+
lut_hue = ((x * r[0]) % 180).astype(dtype)
|
71 |
+
lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
|
72 |
+
lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
|
73 |
+
|
74 |
+
im_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val)))
|
75 |
+
cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR, dst=im) # no return needed
|
76 |
+
|
77 |
+
|
78 |
+
def hist_equalize(im, clahe=True, bgr=False):
|
79 |
+
# Equalize histogram on BGR image 'im' with im.shape(n,m,3) and range 0-255
|
80 |
+
yuv = cv2.cvtColor(im, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV)
|
81 |
+
if clahe:
|
82 |
+
c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
83 |
+
yuv[:, :, 0] = c.apply(yuv[:, :, 0])
|
84 |
+
else:
|
85 |
+
yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0]) # equalize Y channel histogram
|
86 |
+
return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB) # convert YUV image to RGB
|
87 |
+
|
88 |
+
|
89 |
+
def replicate(im, labels):
|
90 |
+
# Replicate labels
|
91 |
+
h, w = im.shape[:2]
|
92 |
+
boxes = labels[:, 1:].astype(int)
|
93 |
+
x1, y1, x2, y2 = boxes.T
|
94 |
+
s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels)
|
95 |
+
for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices
|
96 |
+
x1b, y1b, x2b, y2b = boxes[i]
|
97 |
+
bh, bw = y2b - y1b, x2b - x1b
|
98 |
+
yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y
|
99 |
+
x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh]
|
100 |
+
im[y1a:y2a, x1a:x2a] = im[y1b:y2b, x1b:x2b] # im4[ymin:ymax, xmin:xmax]
|
101 |
+
labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0)
|
102 |
+
|
103 |
+
return im, labels
|
104 |
+
|
105 |
+
|
106 |
+
def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
|
107 |
+
# Resize and pad image while meeting stride-multiple constraints
|
108 |
+
shape = im.shape[:2] # current shape [height, width]
|
109 |
+
if isinstance(new_shape, int):
|
110 |
+
new_shape = (new_shape, new_shape)
|
111 |
+
|
112 |
+
# Scale ratio (new / old)
|
113 |
+
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
|
114 |
+
if not scaleup: # only scale down, do not scale up (for better val mAP)
|
115 |
+
r = min(r, 1.0)
|
116 |
+
|
117 |
+
# Compute padding
|
118 |
+
ratio = r, r # width, height ratios
|
119 |
+
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
|
120 |
+
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
|
121 |
+
if auto: # minimum rectangle
|
122 |
+
dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding
|
123 |
+
elif scaleFill: # stretch
|
124 |
+
dw, dh = 0.0, 0.0
|
125 |
+
new_unpad = (new_shape[1], new_shape[0])
|
126 |
+
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
|
127 |
+
|
128 |
+
dw /= 2 # divide padding into 2 sides
|
129 |
+
dh /= 2
|
130 |
+
|
131 |
+
if shape[::-1] != new_unpad: # resize
|
132 |
+
im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
|
133 |
+
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
|
134 |
+
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
|
135 |
+
im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
|
136 |
+
return im, ratio, (dw, dh)
|
137 |
+
|
138 |
+
|
139 |
+
def random_perspective(im,
|
140 |
+
targets=(),
|
141 |
+
segments=(),
|
142 |
+
degrees=10,
|
143 |
+
translate=.1,
|
144 |
+
scale=.1,
|
145 |
+
shear=10,
|
146 |
+
perspective=0.0,
|
147 |
+
border=(0, 0)):
|
148 |
+
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(0.1, 0.1), scale=(0.9, 1.1), shear=(-10, 10))
|
149 |
+
# targets = [cls, xyxy]
|
150 |
+
|
151 |
+
height = im.shape[0] + border[0] * 2 # shape(h,w,c)
|
152 |
+
width = im.shape[1] + border[1] * 2
|
153 |
+
|
154 |
+
# Center
|
155 |
+
C = np.eye(3)
|
156 |
+
C[0, 2] = -im.shape[1] / 2 # x translation (pixels)
|
157 |
+
C[1, 2] = -im.shape[0] / 2 # y translation (pixels)
|
158 |
+
|
159 |
+
# Perspective
|
160 |
+
P = np.eye(3)
|
161 |
+
P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y)
|
162 |
+
P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x)
|
163 |
+
|
164 |
+
# Rotation and Scale
|
165 |
+
R = np.eye(3)
|
166 |
+
a = random.uniform(-degrees, degrees)
|
167 |
+
# a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations
|
168 |
+
s = random.uniform(1 - scale, 1 + scale)
|
169 |
+
# s = 2 ** random.uniform(-scale, scale)
|
170 |
+
R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s)
|
171 |
+
|
172 |
+
# Shear
|
173 |
+
S = np.eye(3)
|
174 |
+
S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg)
|
175 |
+
S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg)
|
176 |
+
|
177 |
+
# Translation
|
178 |
+
T = np.eye(3)
|
179 |
+
T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels)
|
180 |
+
T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels)
|
181 |
+
|
182 |
+
# Combined rotation matrix
|
183 |
+
M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT
|
184 |
+
if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed
|
185 |
+
if perspective:
|
186 |
+
im = cv2.warpPerspective(im, M, dsize=(width, height), borderValue=(114, 114, 114))
|
187 |
+
else: # affine
|
188 |
+
im = cv2.warpAffine(im, M[:2], dsize=(width, height), borderValue=(114, 114, 114))
|
189 |
+
|
190 |
+
# Visualize
|
191 |
+
# import matplotlib.pyplot as plt
|
192 |
+
# ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel()
|
193 |
+
# ax[0].imshow(im[:, :, ::-1]) # base
|
194 |
+
# ax[1].imshow(im2[:, :, ::-1]) # warped
|
195 |
+
|
196 |
+
# Transform label coordinates
|
197 |
+
n = len(targets)
|
198 |
+
if n:
|
199 |
+
use_segments = any(x.any() for x in segments)
|
200 |
+
new = np.zeros((n, 4))
|
201 |
+
if use_segments: # warp segments
|
202 |
+
segments = resample_segments(segments) # upsample
|
203 |
+
for i, segment in enumerate(segments):
|
204 |
+
xy = np.ones((len(segment), 3))
|
205 |
+
xy[:, :2] = segment
|
206 |
+
xy = xy @ M.T # transform
|
207 |
+
xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine
|
208 |
+
|
209 |
+
# clip
|
210 |
+
new[i] = segment2box(xy, width, height)
|
211 |
+
|
212 |
+
else: # warp boxes
|
213 |
+
xy = np.ones((n * 4, 3))
|
214 |
+
xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1
|
215 |
+
xy = xy @ M.T # transform
|
216 |
+
xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]).reshape(n, 8) # perspective rescale or affine
|
217 |
+
|
218 |
+
# create new boxes
|
219 |
+
x = xy[:, [0, 2, 4, 6]]
|
220 |
+
y = xy[:, [1, 3, 5, 7]]
|
221 |
+
new = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T
|
222 |
+
|
223 |
+
# clip
|
224 |
+
new[:, [0, 2]] = new[:, [0, 2]].clip(0, width)
|
225 |
+
new[:, [1, 3]] = new[:, [1, 3]].clip(0, height)
|
226 |
+
|
227 |
+
# filter candidates
|
228 |
+
i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01 if use_segments else 0.10)
|
229 |
+
targets = targets[i]
|
230 |
+
targets[:, 1:5] = new[i]
|
231 |
+
|
232 |
+
return im, targets
|
233 |
+
|
234 |
+
|
235 |
+
def copy_paste(im, labels, segments, p=0.5):
|
236 |
+
# Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
|
237 |
+
n = len(segments)
|
238 |
+
if p and n:
|
239 |
+
h, w, c = im.shape # height, width, channels
|
240 |
+
im_new = np.zeros(im.shape, np.uint8)
|
241 |
+
|
242 |
+
# calculate ioa first then select indexes randomly
|
243 |
+
boxes = np.stack([w - labels[:, 3], labels[:, 2], w - labels[:, 1], labels[:, 4]], axis=-1) # (n, 4)
|
244 |
+
ioa = bbox_ioa(boxes, labels[:, 1:5]) # intersection over area
|
245 |
+
indexes = np.nonzero((ioa < 0.30).all(1))[0] # (N, )
|
246 |
+
n = len(indexes)
|
247 |
+
for j in random.sample(list(indexes), k=round(p * n)):
|
248 |
+
l, box, s = labels[j], boxes[j], segments[j]
|
249 |
+
labels = np.concatenate((labels, [[l[0], *box]]), 0)
|
250 |
+
segments.append(np.concatenate((w - s[:, 0:1], s[:, 1:2]), 1))
|
251 |
+
cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (1, 1, 1), cv2.FILLED)
|
252 |
+
|
253 |
+
result = cv2.flip(im, 1) # augment segments (flip left-right)
|
254 |
+
i = cv2.flip(im_new, 1).astype(bool)
|
255 |
+
im[i] = result[i] # cv2.imwrite('debug.jpg', im) # debug
|
256 |
+
|
257 |
+
return im, labels, segments
|
258 |
+
|
259 |
+
|
260 |
+
def cutout(im, labels, p=0.5):
|
261 |
+
# Applies image cutout augmentation https://arxiv.org/abs/1708.04552
|
262 |
+
if random.random() < p:
|
263 |
+
h, w = im.shape[:2]
|
264 |
+
scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction
|
265 |
+
for s in scales:
|
266 |
+
mask_h = random.randint(1, int(h * s)) # create random masks
|
267 |
+
mask_w = random.randint(1, int(w * s))
|
268 |
+
|
269 |
+
# box
|
270 |
+
xmin = max(0, random.randint(0, w) - mask_w // 2)
|
271 |
+
ymin = max(0, random.randint(0, h) - mask_h // 2)
|
272 |
+
xmax = min(w, xmin + mask_w)
|
273 |
+
ymax = min(h, ymin + mask_h)
|
274 |
+
|
275 |
+
# apply random color mask
|
276 |
+
im[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)]
|
277 |
+
|
278 |
+
# return unobscured labels
|
279 |
+
if len(labels) and s > 0.03:
|
280 |
+
box = np.array([[xmin, ymin, xmax, ymax]], dtype=np.float32)
|
281 |
+
ioa = bbox_ioa(box, xywhn2xyxy(labels[:, 1:5], w, h))[0] # intersection over area
|
282 |
+
labels = labels[ioa < 0.60] # remove >60% obscured labels
|
283 |
+
|
284 |
+
return labels
|
285 |
+
|
286 |
+
|
287 |
+
def mixup(im, labels, im2, labels2):
|
288 |
+
# Applies MixUp augmentation https://arxiv.org/pdf/1710.09412.pdf
|
289 |
+
r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0
|
290 |
+
im = (im * r + im2 * (1 - r)).astype(np.uint8)
|
291 |
+
labels = np.concatenate((labels, labels2), 0)
|
292 |
+
return im, labels
|
293 |
+
|
294 |
+
|
295 |
+
def box_candidates(box1, box2, wh_thr=2, ar_thr=100, area_thr=0.1, eps=1e-16): # box1(4,n), box2(4,n)
|
296 |
+
# Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
|
297 |
+
w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
|
298 |
+
w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
|
299 |
+
ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio
|
300 |
+
return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates
|
301 |
+
|
302 |
+
|
303 |
+
def classify_albumentations(
|
304 |
+
augment=True,
|
305 |
+
size=224,
|
306 |
+
scale=(0.08, 1.0),
|
307 |
+
ratio=(0.75, 1.0 / 0.75), # 0.75, 1.33
|
308 |
+
hflip=0.5,
|
309 |
+
vflip=0.0,
|
310 |
+
jitter=0.4,
|
311 |
+
mean=IMAGENET_MEAN,
|
312 |
+
std=IMAGENET_STD,
|
313 |
+
auto_aug=False):
|
314 |
+
# YOLOv5 classification Albumentations (optional, only used if package is installed)
|
315 |
+
prefix = colorstr('albumentations: ')
|
316 |
+
try:
|
317 |
+
import albumentations as A
|
318 |
+
from albumentations.pytorch import ToTensorV2
|
319 |
+
check_version(A.__version__, '1.0.3', hard=True) # version requirement
|
320 |
+
if augment: # Resize and crop
|
321 |
+
T = [A.RandomResizedCrop(height=size, width=size, scale=scale, ratio=ratio)]
|
322 |
+
if auto_aug:
|
323 |
+
# TODO: implement AugMix, AutoAug & RandAug in albumentation
|
324 |
+
LOGGER.info(f'{prefix}auto augmentations are currently not supported')
|
325 |
+
else:
|
326 |
+
if hflip > 0:
|
327 |
+
T += [A.HorizontalFlip(p=hflip)]
|
328 |
+
if vflip > 0:
|
329 |
+
T += [A.VerticalFlip(p=vflip)]
|
330 |
+
if jitter > 0:
|
331 |
+
color_jitter = (float(jitter),) * 3 # repeat value for brightness, contrast, satuaration, 0 hue
|
332 |
+
T += [A.ColorJitter(*color_jitter, 0)]
|
333 |
+
else: # Use fixed crop for eval set (reproducibility)
|
334 |
+
T = [A.SmallestMaxSize(max_size=size), A.CenterCrop(height=size, width=size)]
|
335 |
+
T += [A.Normalize(mean=mean, std=std), ToTensorV2()] # Normalize and convert to Tensor
|
336 |
+
LOGGER.info(prefix + ', '.join(f'{x}'.replace('always_apply=False, ', '') for x in T if x.p))
|
337 |
+
return A.Compose(T)
|
338 |
+
|
339 |
+
except ImportError: # package not installed, skip
|
340 |
+
LOGGER.warning(f'{prefix}⚠️ not found, install with `pip install albumentations` (recommended)')
|
341 |
+
except Exception as e:
|
342 |
+
LOGGER.info(f'{prefix}{e}')
|
343 |
+
|
344 |
+
|
345 |
+
def classify_transforms(size=224):
|
346 |
+
# Transforms to apply if albumentations not installed
|
347 |
+
assert isinstance(size, int), f'ERROR: classify_transforms size {size} must be integer, not (list, tuple)'
|
348 |
+
# T.Compose([T.ToTensor(), T.Resize(size), T.CenterCrop(size), T.Normalize(IMAGENET_MEAN, IMAGENET_STD)])
|
349 |
+
return T.Compose([CenterCrop(size), ToTensor(), T.Normalize(IMAGENET_MEAN, IMAGENET_STD)])
|
350 |
+
|
351 |
+
|
352 |
+
class LetterBox:
|
353 |
+
# YOLOv5 LetterBox class for image preprocessing, i.e. T.Compose([LetterBox(size), ToTensor()])
|
354 |
+
def __init__(self, size=(640, 640), auto=False, stride=32):
|
355 |
+
super().__init__()
|
356 |
+
self.h, self.w = (size, size) if isinstance(size, int) else size
|
357 |
+
self.auto = auto # pass max size integer, automatically solve for short side using stride
|
358 |
+
self.stride = stride # used with auto
|
359 |
+
|
360 |
+
def __call__(self, im): # im = np.array HWC
|
361 |
+
imh, imw = im.shape[:2]
|
362 |
+
r = min(self.h / imh, self.w / imw) # ratio of new/old
|
363 |
+
h, w = round(imh * r), round(imw * r) # resized image
|
364 |
+
hs, ws = (math.ceil(x / self.stride) * self.stride for x in (h, w)) if self.auto else self.h, self.w
|
365 |
+
top, left = round((hs - h) / 2 - 0.1), round((ws - w) / 2 - 0.1)
|
366 |
+
im_out = np.full((self.h, self.w, 3), 114, dtype=im.dtype)
|
367 |
+
im_out[top:top + h, left:left + w] = cv2.resize(im, (w, h), interpolation=cv2.INTER_LINEAR)
|
368 |
+
return im_out
|
369 |
+
|
370 |
+
|
371 |
+
class CenterCrop:
|
372 |
+
# YOLOv5 CenterCrop class for image preprocessing, i.e. T.Compose([CenterCrop(size), ToTensor()])
|
373 |
+
def __init__(self, size=640):
|
374 |
+
super().__init__()
|
375 |
+
self.h, self.w = (size, size) if isinstance(size, int) else size
|
376 |
+
|
377 |
+
def __call__(self, im): # im = np.array HWC
|
378 |
+
imh, imw = im.shape[:2]
|
379 |
+
m = min(imh, imw) # min dimension
|
380 |
+
top, left = (imh - m) // 2, (imw - m) // 2
|
381 |
+
return cv2.resize(im[top:top + m, left:left + m], (self.w, self.h), interpolation=cv2.INTER_LINEAR)
|
382 |
+
|
383 |
+
|
384 |
+
class ToTensor:
|
385 |
+
# YOLOv5 ToTensor class for image preprocessing, i.e. T.Compose([LetterBox(size), ToTensor()])
|
386 |
+
def __init__(self, half=False):
|
387 |
+
super().__init__()
|
388 |
+
self.half = half
|
389 |
+
|
390 |
+
def __call__(self, im): # im = np.array HWC in BGR order
|
391 |
+
im = np.ascontiguousarray(im.transpose((2, 0, 1))[::-1]) # HWC to CHW -> BGR to RGB -> contiguous
|
392 |
+
im = torch.from_numpy(im) # to torch
|
393 |
+
im = im.half() if self.half else im.float() # uint8 to fp16/32
|
394 |
+
im /= 255.0 # 0-255 to 0.0-1.0
|
395 |
+
return im
|
utils/autoanchor.py
ADDED
@@ -0,0 +1,164 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
import yaml
|
6 |
+
from tqdm import tqdm
|
7 |
+
|
8 |
+
from utils import TryExcept
|
9 |
+
from utils.general import LOGGER, TQDM_BAR_FORMAT, colorstr
|
10 |
+
|
11 |
+
PREFIX = colorstr('AutoAnchor: ')
|
12 |
+
|
13 |
+
|
14 |
+
def check_anchor_order(m):
|
15 |
+
# Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary
|
16 |
+
a = m.anchors.prod(-1).mean(-1).view(-1) # mean anchor area per output layer
|
17 |
+
da = a[-1] - a[0] # delta a
|
18 |
+
ds = m.stride[-1] - m.stride[0] # delta s
|
19 |
+
if da and (da.sign() != ds.sign()): # same order
|
20 |
+
LOGGER.info(f'{PREFIX}Reversing anchor order')
|
21 |
+
m.anchors[:] = m.anchors.flip(0)
|
22 |
+
|
23 |
+
|
24 |
+
@TryExcept(f'{PREFIX}ERROR')
|
25 |
+
def check_anchors(dataset, model, thr=4.0, imgsz=640):
|
26 |
+
# Check anchor fit to data, recompute if necessary
|
27 |
+
m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1] # Detect()
|
28 |
+
shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True)
|
29 |
+
scale = np.random.uniform(0.9, 1.1, size=(shapes.shape[0], 1)) # augment scale
|
30 |
+
wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes * scale, dataset.labels)])).float() # wh
|
31 |
+
|
32 |
+
def metric(k): # compute metric
|
33 |
+
r = wh[:, None] / k[None]
|
34 |
+
x = torch.min(r, 1 / r).min(2)[0] # ratio metric
|
35 |
+
best = x.max(1)[0] # best_x
|
36 |
+
aat = (x > 1 / thr).float().sum(1).mean() # anchors above threshold
|
37 |
+
bpr = (best > 1 / thr).float().mean() # best possible recall
|
38 |
+
return bpr, aat
|
39 |
+
|
40 |
+
stride = m.stride.to(m.anchors.device).view(-1, 1, 1) # model strides
|
41 |
+
anchors = m.anchors.clone() * stride # current anchors
|
42 |
+
bpr, aat = metric(anchors.cpu().view(-1, 2))
|
43 |
+
s = f'\n{PREFIX}{aat:.2f} anchors/target, {bpr:.3f} Best Possible Recall (BPR). '
|
44 |
+
if bpr > 0.98: # threshold to recompute
|
45 |
+
LOGGER.info(f'{s}Current anchors are a good fit to dataset ✅')
|
46 |
+
else:
|
47 |
+
LOGGER.info(f'{s}Anchors are a poor fit to dataset ⚠️, attempting to improve...')
|
48 |
+
na = m.anchors.numel() // 2 # number of anchors
|
49 |
+
anchors = kmean_anchors(dataset, n=na, img_size=imgsz, thr=thr, gen=1000, verbose=False)
|
50 |
+
new_bpr = metric(anchors)[0]
|
51 |
+
if new_bpr > bpr: # replace anchors
|
52 |
+
anchors = torch.tensor(anchors, device=m.anchors.device).type_as(m.anchors)
|
53 |
+
m.anchors[:] = anchors.clone().view_as(m.anchors)
|
54 |
+
check_anchor_order(m) # must be in pixel-space (not grid-space)
|
55 |
+
m.anchors /= stride
|
56 |
+
s = f'{PREFIX}Done ✅ (optional: update model *.yaml to use these anchors in the future)'
|
57 |
+
else:
|
58 |
+
s = f'{PREFIX}Done ⚠️ (original anchors better than new anchors, proceeding with original anchors)'
|
59 |
+
LOGGER.info(s)
|
60 |
+
|
61 |
+
|
62 |
+
def kmean_anchors(dataset='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=1000, verbose=True):
|
63 |
+
""" Creates kmeans-evolved anchors from training dataset
|
64 |
+
|
65 |
+
Arguments:
|
66 |
+
dataset: path to data.yaml, or a loaded dataset
|
67 |
+
n: number of anchors
|
68 |
+
img_size: image size used for training
|
69 |
+
thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0
|
70 |
+
gen: generations to evolve anchors using genetic algorithm
|
71 |
+
verbose: print all results
|
72 |
+
|
73 |
+
Return:
|
74 |
+
k: kmeans evolved anchors
|
75 |
+
|
76 |
+
Usage:
|
77 |
+
from utils.autoanchor import *; _ = kmean_anchors()
|
78 |
+
"""
|
79 |
+
from scipy.cluster.vq import kmeans
|
80 |
+
|
81 |
+
npr = np.random
|
82 |
+
thr = 1 / thr
|
83 |
+
|
84 |
+
def metric(k, wh): # compute metrics
|
85 |
+
r = wh[:, None] / k[None]
|
86 |
+
x = torch.min(r, 1 / r).min(2)[0] # ratio metric
|
87 |
+
# x = wh_iou(wh, torch.tensor(k)) # iou metric
|
88 |
+
return x, x.max(1)[0] # x, best_x
|
89 |
+
|
90 |
+
def anchor_fitness(k): # mutation fitness
|
91 |
+
_, best = metric(torch.tensor(k, dtype=torch.float32), wh)
|
92 |
+
return (best * (best > thr).float()).mean() # fitness
|
93 |
+
|
94 |
+
def print_results(k, verbose=True):
|
95 |
+
k = k[np.argsort(k.prod(1))] # sort small to large
|
96 |
+
x, best = metric(k, wh0)
|
97 |
+
bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr
|
98 |
+
s = f'{PREFIX}thr={thr:.2f}: {bpr:.4f} best possible recall, {aat:.2f} anchors past thr\n' \
|
99 |
+
f'{PREFIX}n={n}, img_size={img_size}, metric_all={x.mean():.3f}/{best.mean():.3f}-mean/best, ' \
|
100 |
+
f'past_thr={x[x > thr].mean():.3f}-mean: '
|
101 |
+
for x in k:
|
102 |
+
s += '%i,%i, ' % (round(x[0]), round(x[1]))
|
103 |
+
if verbose:
|
104 |
+
LOGGER.info(s[:-2])
|
105 |
+
return k
|
106 |
+
|
107 |
+
if isinstance(dataset, str): # *.yaml file
|
108 |
+
with open(dataset, errors='ignore') as f:
|
109 |
+
data_dict = yaml.safe_load(f) # model dict
|
110 |
+
from utils.dataloaders import LoadImagesAndLabels
|
111 |
+
dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
|
112 |
+
|
113 |
+
# Get label wh
|
114 |
+
shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True)
|
115 |
+
wh0 = np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)]) # wh
|
116 |
+
|
117 |
+
# Filter
|
118 |
+
i = (wh0 < 3.0).any(1).sum()
|
119 |
+
if i:
|
120 |
+
LOGGER.info(f'{PREFIX}WARNING ⚠️ Extremely small objects found: {i} of {len(wh0)} labels are <3 pixels in size')
|
121 |
+
wh = wh0[(wh0 >= 2.0).any(1)].astype(np.float32) # filter > 2 pixels
|
122 |
+
# wh = wh * (npr.rand(wh.shape[0], 1) * 0.9 + 0.1) # multiply by random scale 0-1
|
123 |
+
|
124 |
+
# Kmeans init
|
125 |
+
try:
|
126 |
+
LOGGER.info(f'{PREFIX}Running kmeans for {n} anchors on {len(wh)} points...')
|
127 |
+
assert n <= len(wh) # apply overdetermined constraint
|
128 |
+
s = wh.std(0) # sigmas for whitening
|
129 |
+
k = kmeans(wh / s, n, iter=30)[0] * s # points
|
130 |
+
assert n == len(k) # kmeans may return fewer points than requested if wh is insufficient or too similar
|
131 |
+
except Exception:
|
132 |
+
LOGGER.warning(f'{PREFIX}WARNING ⚠️ switching strategies from kmeans to random init')
|
133 |
+
k = np.sort(npr.rand(n * 2)).reshape(n, 2) * img_size # random init
|
134 |
+
wh, wh0 = (torch.tensor(x, dtype=torch.float32) for x in (wh, wh0))
|
135 |
+
k = print_results(k, verbose=False)
|
136 |
+
|
137 |
+
# Plot
|
138 |
+
# k, d = [None] * 20, [None] * 20
|
139 |
+
# for i in tqdm(range(1, 21)):
|
140 |
+
# k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance
|
141 |
+
# fig, ax = plt.subplots(1, 2, figsize=(14, 7), tight_layout=True)
|
142 |
+
# ax = ax.ravel()
|
143 |
+
# ax[0].plot(np.arange(1, 21), np.array(d) ** 2, marker='.')
|
144 |
+
# fig, ax = plt.subplots(1, 2, figsize=(14, 7)) # plot wh
|
145 |
+
# ax[0].hist(wh[wh[:, 0]<100, 0],400)
|
146 |
+
# ax[1].hist(wh[wh[:, 1]<100, 1],400)
|
147 |
+
# fig.savefig('wh.png', dpi=200)
|
148 |
+
|
149 |
+
# Evolve
|
150 |
+
f, sh, mp, s = anchor_fitness(k), k.shape, 0.9, 0.1 # fitness, generations, mutation prob, sigma
|
151 |
+
pbar = tqdm(range(gen), bar_format=TQDM_BAR_FORMAT) # progress bar
|
152 |
+
for _ in pbar:
|
153 |
+
v = np.ones(sh)
|
154 |
+
while (v == 1).all(): # mutate until a change occurs (prevent duplicates)
|
155 |
+
v = ((npr.random(sh) < mp) * random.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0)
|
156 |
+
kg = (k.copy() * v).clip(min=2.0)
|
157 |
+
fg = anchor_fitness(kg)
|
158 |
+
if fg > f:
|
159 |
+
f, k = fg, kg.copy()
|
160 |
+
pbar.desc = f'{PREFIX}Evolving anchors with Genetic Algorithm: fitness = {f:.4f}'
|
161 |
+
if verbose:
|
162 |
+
print_results(k, verbose)
|
163 |
+
|
164 |
+
return print_results(k).astype(np.float32)
|
utils/autobatch.py
ADDED
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from copy import deepcopy
|
2 |
+
|
3 |
+
import numpy as np
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from utils.general import LOGGER, colorstr
|
7 |
+
from utils.torch_utils import profile
|
8 |
+
|
9 |
+
|
10 |
+
def check_train_batch_size(model, imgsz=640, amp=True):
|
11 |
+
# Check YOLOv5 training batch size
|
12 |
+
with torch.cuda.amp.autocast(amp):
|
13 |
+
return autobatch(deepcopy(model).train(), imgsz) # compute optimal batch size
|
14 |
+
|
15 |
+
|
16 |
+
def autobatch(model, imgsz=640, fraction=0.8, batch_size=16):
|
17 |
+
# Automatically estimate best YOLOv5 batch size to use `fraction` of available CUDA memory
|
18 |
+
# Usage:
|
19 |
+
# import torch
|
20 |
+
# from utils.autobatch import autobatch
|
21 |
+
# model = torch.hub.load('ultralytics/yolov5', 'yolov5s', autoshape=False)
|
22 |
+
# print(autobatch(model))
|
23 |
+
|
24 |
+
# Check device
|
25 |
+
prefix = colorstr('AutoBatch: ')
|
26 |
+
LOGGER.info(f'{prefix}Computing optimal batch size for --imgsz {imgsz}')
|
27 |
+
device = next(model.parameters()).device # get model device
|
28 |
+
if device.type == 'cpu':
|
29 |
+
LOGGER.info(f'{prefix}CUDA not detected, using default CPU batch-size {batch_size}')
|
30 |
+
return batch_size
|
31 |
+
if torch.backends.cudnn.benchmark:
|
32 |
+
LOGGER.info(f'{prefix} ⚠️ Requires torch.backends.cudnn.benchmark=False, using default batch-size {batch_size}')
|
33 |
+
return batch_size
|
34 |
+
|
35 |
+
# Inspect CUDA memory
|
36 |
+
gb = 1 << 30 # bytes to GiB (1024 ** 3)
|
37 |
+
d = str(device).upper() # 'CUDA:0'
|
38 |
+
properties = torch.cuda.get_device_properties(device) # device properties
|
39 |
+
t = properties.total_memory / gb # GiB total
|
40 |
+
r = torch.cuda.memory_reserved(device) / gb # GiB reserved
|
41 |
+
a = torch.cuda.memory_allocated(device) / gb # GiB allocated
|
42 |
+
f = t - (r + a) # GiB free
|
43 |
+
LOGGER.info(f'{prefix}{d} ({properties.name}) {t:.2f}G total, {r:.2f}G reserved, {a:.2f}G allocated, {f:.2f}G free')
|
44 |
+
|
45 |
+
# Profile batch sizes
|
46 |
+
batch_sizes = [1, 2, 4, 8, 16]
|
47 |
+
try:
|
48 |
+
img = [torch.empty(b, 3, imgsz, imgsz) for b in batch_sizes]
|
49 |
+
results = profile(img, model, n=3, device=device)
|
50 |
+
except Exception as e:
|
51 |
+
LOGGER.warning(f'{prefix}{e}')
|
52 |
+
|
53 |
+
# Fit a solution
|
54 |
+
y = [x[2] for x in results if x] # memory [2]
|
55 |
+
p = np.polyfit(batch_sizes[:len(y)], y, deg=1) # first degree polynomial fit
|
56 |
+
b = int((f * fraction - p[1]) / p[0]) # y intercept (optimal batch size)
|
57 |
+
if None in results: # some sizes failed
|
58 |
+
i = results.index(None) # first fail index
|
59 |
+
if b >= batch_sizes[i]: # y intercept above failure point
|
60 |
+
b = batch_sizes[max(i - 1, 0)] # select prior safe point
|
61 |
+
if b < 1 or b > 1024: # b outside of safe range
|
62 |
+
b = batch_size
|
63 |
+
LOGGER.warning(f'{prefix}WARNING ⚠️ CUDA anomaly detected, recommend restart environment and retry command.')
|
64 |
+
|
65 |
+
fraction = (np.polyval(p, b) + r + a) / t # actual fraction predicted
|
66 |
+
LOGGER.info(f'{prefix}Using batch-size {b} for {d} {t * fraction:.2f}G/{t:.2f}G ({fraction * 100:.0f}%) ✅')
|
67 |
+
return b
|