├── CONTRIBUTING.md
├── Dockerfile
├── LICENSE
├── README.md
├── VisDrone2YOLO_lable.py
├── data
├── Argoverse.yaml
├── GlobalWheat2020.yaml
├── Objects365.yaml
├── SKU-110K.yaml
├── UAVDT.yaml
├── VOC.yaml
├── VisDrone.yaml
├── coco.yaml
├── coco128.yaml
├── hyps
│ ├── hyp.UAVDT.yaml
│ ├── hyp.VisDrone.yaml
│ ├── hyp.finetune.yaml
│ ├── hyp.finetune_objects365.yaml
│ ├── hyp.scratch-high.yaml
│ ├── hyp.scratch-low.yaml
│ ├── hyp.scratch-med.yaml
│ └── hyp.scratch.yaml
├── images
│ ├── bus.jpg
│ └── zidane.jpg
├── scripts
│ ├── download_weights.sh
│ ├── get_coco.sh
│ └── get_coco128.sh
└── xView.yaml
├── detect.py
├── export.py
├── hubconf.py
├── images
├── README.md
├── result_in_UAVDT.png
└── result_in_VisDrone.png
├── models
├── __init__.py
├── common.py
├── experimental.py
├── hub
│ ├── anchors.yaml
│ ├── yolov3-spp.yaml
│ ├── yolov3-tiny.yaml
│ ├── yolov3.yaml
│ ├── yolov5-bifpn.yaml
│ ├── yolov5-fpn.yaml
│ ├── yolov5-p2.yaml
│ ├── yolov5-p6.yaml
│ ├── yolov5-p7.yaml
│ ├── yolov5-panet.yaml
│ ├── yolov5l6.yaml
│ ├── yolov5m6.yaml
│ ├── yolov5n6.yaml
│ ├── yolov5s-ghost.yaml
│ ├── yolov5s-transformer.yaml
│ ├── yolov5s6.yaml
│ └── yolov5x6.yaml
├── tf.py
├── yolo.py
├── yolov5l-tph-plus.yaml
├── yolov5l-xs-tph.yaml
├── yolov5l-xs-tr-cbam-spp-bifpn.yaml
├── yolov5l.yaml
├── yolov5m.yaml
├── yolov5n.yaml
├── yolov5s.yaml
└── yolov5x.yaml
├── requirements.txt
├── result.png
├── setup.cfg
├── train.png
├── train.py
├── tutorial.ipynb
├── utils
├── __init__.py
├── activations.py
├── augmentations.py
├── autoanchor.py
├── autobatch.py
├── aws
│ ├── __init__.py
│ ├── mime.sh
│ ├── resume.py
│ └── userdata.sh
├── callbacks.py
├── datasets.py
├── downloads.py
├── flask_rest_api
│ ├── README.md
│ ├── example_request.py
│ └── restapi.py
├── general.py
├── google_app_engine
│ ├── Dockerfile
│ ├── additional_requirements.txt
│ └── app.yaml
├── loggers
│ ├── __init__.py
│ └── wandb
│ │ ├── README.md
│ │ ├── __init__.py
│ │ ├── log_dataset.py
│ │ ├── sweep.py
│ │ ├── sweep.yaml
│ │ └── wandb_utils.py
├── loss.py
├── metrics.py
├── metrics_aIoU.py
├── plots.py
└── torch_utils.py
├── val.py
└── wbf.py
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | ## Contributing to YOLOv5 🚀
2 |
3 | We love your input! We want to make contributing to YOLOv5 as easy and transparent as possible, whether it's:
4 |
5 | - Reporting a bug
6 | - Discussing the current state of the code
7 | - Submitting a fix
8 | - Proposing a new feature
9 | - Becoming a maintainer
10 |
11 | YOLOv5 works so well due to our combined community effort, and for every small improvement you contribute you will be
12 | helping push the frontiers of what's possible in AI 😃!
13 |
14 | ## Submitting a Pull Request (PR) 🛠️
15 |
16 | Submitting a PR is easy! This example shows how to submit a PR for updating `requirements.txt` in 4 steps:
17 |
18 | ### 1. Select File to Update
19 |
20 | Select `requirements.txt` to update by clicking on it in GitHub.
21 |

22 |
23 | ### 2. Click 'Edit this file'
24 |
25 | Button is in top-right corner.
26 | 
27 |
28 | ### 3. Make Changes
29 |
30 | Change `matplotlib` version from `3.2.2` to `3.3`.
31 | 
32 |
33 | ### 4. Preview Changes and Submit PR
34 |
35 | Click on the **Preview changes** tab to verify your updates. At the bottom of the screen select 'Create a **new branch**
36 | for this commit', assign your branch a descriptive name such as `fix/matplotlib_version` and click the green **Propose
37 | changes** button. All done, your PR is now submitted to YOLOv5 for review and approval 😃!
38 | 
39 |
40 | ### PR recommendations
41 |
42 | To allow your work to be integrated as seamlessly as possible, we advise you to:
43 |
44 | - ✅ Verify your PR is **up-to-date with upstream/master.** If your PR is behind upstream/master an
45 | automatic [GitHub actions](https://github.com/ultralytics/yolov5/blob/master/.github/workflows/rebase.yml) rebase may
46 | be attempted by including the /rebase command in a comment body, or by running the following code, replacing 'feature'
47 | with the name of your local branch:
48 |
49 | ```bash
50 | git remote add upstream https://github.com/ultralytics/yolov5.git
51 | git fetch upstream
52 | git checkout feature # <----- replace 'feature' with local branch name
53 | git merge upstream/master
54 | git push -u origin -f
55 | ```
56 |
57 | - ✅ Verify all Continuous Integration (CI) **checks are passing**.
58 | - ✅ Reduce changes to the absolute **minimum** required for your bug fix or feature addition. _"It is not daily increase
59 | but daily decrease, hack away the unessential. The closer to the source, the less wastage there is."_ — Bruce Lee
60 |
61 | ## Submitting a Bug Report 🐛
62 |
63 | If you spot a problem with YOLOv5 please submit a Bug Report!
64 |
65 | For us to start investigating a possible problem we need to be able to reproduce it ourselves first. We've created a few
66 | short guidelines below to help users provide what we need in order to get started.
67 |
68 | When asking a question, people will be better able to provide help if you provide **code** that they can easily
69 | understand and use to **reproduce** the problem. This is referred to by community members as creating
70 | a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Your code that reproduces
71 | the problem should be:
72 |
73 | * ✅ **Minimal** – Use as little code as possible that still produces the same problem
74 | * ✅ **Complete** – Provide **all** parts someone else needs to reproduce your problem in the question itself
75 | * ✅ **Reproducible** – Test the code you're about to provide to make sure it reproduces the problem
76 |
77 | In addition to the above requirements, for [Ultralytics](https://ultralytics.com/) to provide assistance your code
78 | should be:
79 |
80 | * ✅ **Current** – Verify that your code is up-to-date with current
81 | GitHub [master](https://github.com/ultralytics/yolov5/tree/master), and if necessary `git pull` or `git clone` a new
82 | copy to ensure your problem has not already been resolved by previous commits.
83 | * ✅ **Unmodified** – Your problem must be reproducible without any modifications to the codebase in this
84 | repository. [Ultralytics](https://ultralytics.com/) does not provide support for custom code ⚠️.
85 |
86 | If you believe your problem meets all of the above criteria, please close this issue and raise a new one using the 🐛 **
87 | Bug Report** [template](https://github.com/ultralytics/yolov5/issues/new/choose) and providing
88 | a [minimum reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) to help us better
89 | understand and diagnose your problem.
90 |
91 | ## License
92 |
93 | By contributing, you agree that your contributions will be licensed under
94 | the [GPL-3.0 license](https://choosealicense.com/licenses/gpl-3.0/)
95 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Start FROM Nvidia PyTorch image https://ngc.nvidia.com/catalog/containers/nvidia:pytorch
4 | FROM nvcr.io/nvidia/pytorch:21.05-py3
5 |
6 | # Install linux packages
7 | RUN apt update && apt install -y zip htop screen libgl1-mesa-glx
8 |
9 | # Install python dependencies
10 | COPY requirements.txt .
11 | RUN python -m pip install --upgrade pip
12 | RUN pip uninstall -y nvidia-tensorboard nvidia-tensorboard-plugin-dlprof
13 | RUN pip install --no-cache -r requirements.txt coremltools onnx gsutil notebook wandb>=0.12.2
14 | RUN pip install --no-cache -U torch torchvision numpy
15 | # RUN pip install --no-cache torch==1.9.1+cu111 torchvision==0.10.1+cu111 -f https://download.pytorch.org/whl/torch_stable.html
16 |
17 | # Create working directory
18 | RUN mkdir -p /usr/src/app
19 | WORKDIR /usr/src/app
20 |
21 | # Copy contents
22 | COPY . /usr/src/app
23 |
24 | # Downloads to user config dir
25 | ADD https://ultralytics.com/assets/Arial.ttf /root/.config/Ultralytics/
26 |
27 | # Set environment variables
28 | # ENV HOME=/usr/src/app
29 |
30 |
31 | # Usage Examples -------------------------------------------------------------------------------------------------------
32 |
33 | # Build and Push
34 | # t=ultralytics/yolov5:latest && sudo docker build -t $t . && sudo docker push $t
35 |
36 | # Pull and Run
37 | # t=ultralytics/yolov5:latest && sudo docker pull $t && sudo docker run -it --ipc=host --gpus all $t
38 |
39 | # Pull and Run with local directory access
40 | # t=ultralytics/yolov5:latest && sudo docker pull $t && sudo docker run -it --ipc=host --gpus all -v "$(pwd)"/datasets:/usr/src/datasets $t
41 |
42 | # Kill all
43 | # sudo docker kill $(sudo docker ps -q)
44 |
45 | # Kill all image-based
46 | # sudo docker kill $(sudo docker ps -qa --filter ancestor=ultralytics/yolov5:latest)
47 |
48 | # Bash into running container
49 | # sudo docker exec -it 5a9b5863d93d bash
50 |
51 | # Bash into stopped container
52 | # id=$(sudo docker ps -qa) && sudo docker start $id && sudo docker exec -it $id bash
53 |
54 | # Clean up
55 | # docker system prune -a --volumes
56 |
57 | # Update Ubuntu drivers
58 | # https://www.maketecheasier.com/install-nvidia-drivers-ubuntu/
59 |
60 | # DDP test
61 | # python -m torch.distributed.run --nproc_per_node 2 --master_port 1 train.py --epochs 3
62 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # TPH-YOLOv5
2 | This repo is the implementation of ["TPH-YOLOv5: Improved YOLOv5 Based on Transformer Prediction Head for Object Detection on Drone-Captured Scenarios"](https://openaccess.thecvf.com/content/ICCV2021W/VisDrone/html/Zhu_TPH-YOLOv5_Improved_YOLOv5_Based_on_Transformer_Prediction_Head_for_Object_ICCVW_2021_paper.html) and ["TPH-YOLOv5++: Boosting Object Detection on Drone-Captured Scenarios with Cross-Layer Asymmetric Transformer"](https://www.mdpi.com/2072-4292/15/6/1687).
3 | On [VisDrone Challenge 2021](http://aiskyeye.com/), TPH-YOLOv5 wins 4th place and achieves well-matched results with 1st place model.
4 | 
5 | You can get [VisDrone-DET2021: The Vision Meets Drone Object Detection Challenge Results](https://openaccess.thecvf.com/content/ICCV2021W/VisDrone/html/Cao_VisDrone-DET2021_The_Vision_Meets_Drone_Object_Detection_Challenge_Results_ICCVW_2021_paper.html) for more information. The TPH-YOLOv5++, as an improved version, significantly improves inference efficiency and reduces computational costs while maintaining detection performance compared to TPH-YOLOv5.
6 |
7 | # Install
8 | ```bash
9 | $ git clone https://github.com/cv516Buaa/tph-yolov5
10 | $ cd tph-yolov5
11 | $ pip install -r requirements.txt
12 | ```
13 | # Convert labels
14 | VisDrone2YOLO_lable.py transfer VisDrone annotiations to yolo labels.
15 | You should set the path of VisDrone dataset in VisDrone2YOLO_lable.py first.
16 | ```bash
17 | $ python VisDrone2YOLO_lable.py
18 | ```
19 |
20 | # Inference
21 | * `Datasets` : [VisDrone](http://aiskyeye.com/download/object-detection-2/), [UAVDT](https://sites.google.com/view/grli-uavdt/%E9%A6%96%E9%A1%B5)
22 | * `Weights` (PyTorch
23 | v1.10):
24 | * `yolov5l-xs-1.pt`: | [Baidu Drive(pw: vibe)](https://pan.baidu.com/s/1APETgMoeCOvZi1GsBZERrg). | [Google Drive](https://drive.google.com/file/d/1nGeKl3qOa26v3haGSDmLjeA0cjDD9p61/view?usp=sharing) |
25 | * `yolov5l-xs-2.pt`: | [Baidu Drive(pw: vffz)](https://pan.baidu.com/s/19S84EevP86yJIvnv9KYXDA). | [Google Drive](https://drive.google.com/file/d/1VmORvxNtvMVMvmY7cCwvp0BoL6L3RGiq/view?usp=sharing) |
26 |
27 | val.py runs inference on VisDrone2019-DET-val, using weights trained with TPH-YOLOv5.
28 | (We provide two weights trained by two different models based on YOLOv5l.)
29 |
30 | ```bash
31 | $ python val.py --weights ./weights/yolov5l-xs-1.pt --img 1996 --data ./data/VisDrone.yaml
32 | yolov5l-xs-2.pt
33 | --augment --save-txt --save-conf --task val --batch-size 8 --verbose --name v5l-xs
34 | ```
35 | 
36 | Inference on UAVDT is similar and results of TPH-YOLOv5++ on UAVDT are as follow:
37 | 
38 |
39 | # Ensemble
40 | If you inference dataset with different models, then you can ensemble the result by weighted boxes fusion using wbf.py.
41 | You should set img path and txt path in wbf.py.
42 | ```bash
43 | $ python wbf.py
44 | ```
45 |
46 | # Train
47 | train.py allows you to train new model from strach.
48 | ```bash
49 | $ python train.py --img 1536 --adam --batch 4 --epochs 80 --data ./data/VisDrone.yaml --weights yolov5l.pt --hy data/hyps/hyp.VisDrone.yaml --cfg models/yolov5l-xs-tph.yaml --name v5l-xs-tph
50 | $ python train.py --img 1536 --adam --batch 4 --epochs 80 --data ./data/VisDrone.yaml --weights yolov5l.pt --hy data/hyps/hyp.VisDrone.yaml --cfg models/yolov5l-tph-plus.yaml --name v5l-tph-plus
51 | ```
52 | 
53 |
54 | # Description of TPH-YOLOv5, TPH-YOLOv5++ and citations
55 | - https://arxiv.org/abs/2108.11539
56 | - https://openaccess.thecvf.com/content/ICCV2021W/VisDrone/html/Zhu_TPH-YOLOv5_Improved_YOLOv5_Based_on_Transformer_Prediction_Head_for_Object_ICCVW_2021_paper.html
57 | - https://www.mdpi.com/2072-4292/15/6/1687
58 |
59 | If you have any question, please discuss with me by sending email to lyushuchang@buaa.edu.cn or liubinghao@buaa.edu.cn
60 | If you find this code useful please cite:
61 | ```
62 | @InProceedings{Zhu_2021_ICCV,
63 | author = {Zhu, Xingkui and Lyu, Shuchang and Wang, Xu and Zhao, Qi},
64 | title = {TPH-YOLOv5: Improved YOLOv5 Based on Transformer Prediction Head for Object Detection on Drone-Captured Scenarios},
65 | booktitle = {Proceedings of the IEEE/CVF International Conference on Computer Vision (ICCV) Workshops},
66 | month = {October},
67 | year = {2021},
68 | pages = {2778-2788}
69 | }
70 |
71 | @Article{rs15061687,
72 | AUTHOR = {Zhao, Qi and Liu, Binghao and Lyu, Shuchang and Wang, Chunlei and Zhang, Hong},
73 | TITLE = {TPH-YOLOv5++: Boosting Object Detection on Drone-Captured Scenarios with Cross-Layer Asymmetric Transformer},
74 | JOURNAL = {Remote Sensing},
75 | VOLUME = {15},
76 | YEAR = {2023},
77 | NUMBER = {6},
78 | ARTICLE-NUMBER = {1687},
79 | URL = {https://www.mdpi.com/2072-4292/15/6/1687},
80 | ISSN = {2072-4292},
81 | DOI = {10.3390/rs15061687}
82 | }
83 | ```
84 |
85 | # References
86 | Thanks to their great works
87 | * [ultralytics/yolov5](https://github.com/ultralytics/yolov5)
88 | * [SwinTransformer](https://github.com/microsoft/Swin-Transformer)
89 | * [WBF](https://github.com/ZFTurbo/Weighted-Boxes-Fusion)
90 |
--------------------------------------------------------------------------------
/VisDrone2YOLO_lable.py:
--------------------------------------------------------------------------------
1 | import os
2 | import pandas as pd
3 | from PIL import Image
4 |
5 | YOLO_LABELS_PATH = "../datasets/VisDrone/VisDrone2019-DET-val/labels"
6 | VISANN_PATH = "../datasets/VisDrone/VisDrone2019-DET-val/annotations/"
7 | VISIMG_PATH = "../datasets//VisDrone/VisDrone2019-DET-val/images/"
8 |
9 | def convert(bbox, img_size):
10 | #将标注visDrone数据集标注转为yolov5
11 | #bbox top_left_x top_left_y width height
12 | dw = 1/(img_size[0])
13 | dh = 1/(img_size[1])
14 | x = bbox[0] + bbox[2]/2
15 | y = bbox[1] + bbox[3]/2
16 | x = x * dw
17 | y = y * dh
18 | w = bbox[2] * dw
19 | h = bbox[3] * dh
20 | return (x,y,w,h)
21 |
22 | def ChangeToYolo5():
23 | if not os.path.exists(YOLO_LABELS_PATH):
24 | os.makedirs(YOLO_LABELS_PATH)
25 | print(len(os.listdir(VISANN_PATH)))
26 | for file in os.listdir(VISANN_PATH):
27 | image_path = VISIMG_PATH + '/' + file.replace('txt', 'jpg')
28 | ann_file = VISANN_PATH + '/' + file
29 | out_file = open(YOLO_LABELS_PATH + '/' + file, 'w')
30 | bbox = pd.read_csv(ann_file,header=None).values
31 | img = Image.open(image_path)
32 | img_size = img.size
33 | for row in bbox:
34 | if(row[4]==1 and 0= cls >= 0, f'incorrect class index {cls}'
74 |
75 | # Write YOLO label
76 | if id not in shapes:
77 | shapes[id] = Image.open(file).size
78 | box = xyxy2xywhn(box[None].astype(np.float), w=shapes[id][0], h=shapes[id][1], clip=True)
79 | with open((labels / id).with_suffix('.txt'), 'a') as f:
80 | f.write(f"{cls} {' '.join(f'{x:.6f}' for x in box[0])}\n") # write label.txt
81 | except Exception as e:
82 | print(f'WARNING: skipping one label for {file}: {e}')
83 |
84 |
85 | # Download manually from https://challenge.xviewdataset.org
86 | dir = Path(yaml['path']) # dataset root dir
87 | # urls = ['https://d307kc0mrhucc3.cloudfront.net/train_labels.zip', # train labels
88 | # 'https://d307kc0mrhucc3.cloudfront.net/train_images.zip', # 15G, 847 train images
89 | # 'https://d307kc0mrhucc3.cloudfront.net/val_images.zip'] # 5G, 282 val images (no labels)
90 | # download(urls, dir=dir, delete=False)
91 |
92 | # Convert labels
93 | convert_labels(dir / 'xView_train.geojson')
94 |
95 | # Move images
96 | images = Path(dir / 'images')
97 | images.mkdir(parents=True, exist_ok=True)
98 | Path(dir / 'train_images').rename(dir / 'images' / 'train')
99 | Path(dir / 'val_images').rename(dir / 'images' / 'val')
100 |
101 | # Split
102 | autosplit(dir / 'images' / 'train')
103 |
--------------------------------------------------------------------------------
/hubconf.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | PyTorch Hub models https://pytorch.org/hub/ultralytics_yolov5/
4 |
5 | Usage:
6 | import torch
7 | model = torch.hub.load('ultralytics/yolov5', 'yolov5s')
8 | """
9 |
10 | import torch
11 |
12 |
13 | def _create(name, pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
14 | """Creates a specified YOLOv5 model
15 |
16 | Arguments:
17 | name (str): name of model, i.e. 'yolov5s'
18 | pretrained (bool): load pretrained weights into the model
19 | channels (int): number of input channels
20 | classes (int): number of model classes
21 | autoshape (bool): apply YOLOv5 .autoshape() wrapper to model
22 | verbose (bool): print all information to screen
23 | device (str, torch.device, None): device to use for model parameters
24 |
25 | Returns:
26 | YOLOv5 pytorch model
27 | """
28 | from pathlib import Path
29 |
30 | from models.experimental import attempt_load
31 | from models.yolo import Model
32 | from utils.downloads import attempt_download
33 | from utils.general import check_requirements, set_logging
34 | from utils.torch_utils import select_device
35 |
36 | file = Path(__file__).resolve()
37 | check_requirements(exclude=('tensorboard', 'thop', 'opencv-python'))
38 | set_logging(verbose=verbose)
39 |
40 | save_dir = Path('') if str(name).endswith('.pt') else file.parent
41 | path = (save_dir / name).with_suffix('.pt') # checkpoint path
42 | try:
43 | device = select_device(('0' if torch.cuda.is_available() else 'cpu') if device is None else device)
44 |
45 | if pretrained and channels == 3 and classes == 80:
46 | model = attempt_load(path, map_location=device) # download/load FP32 model
47 | else:
48 | cfg = list((Path(__file__).parent / 'models').rglob(f'{name}.yaml'))[0] # model.yaml path
49 | model = Model(cfg, channels, classes) # create model
50 | if pretrained:
51 | ckpt = torch.load(attempt_download(path), map_location=device) # load
52 | msd = model.state_dict() # model state_dict
53 | csd = ckpt['model'].float().state_dict() # checkpoint state_dict as FP32
54 | csd = {k: v for k, v in csd.items() if msd[k].shape == v.shape} # filter
55 | model.load_state_dict(csd, strict=False) # load
56 | if len(ckpt['model'].names) == classes:
57 | model.names = ckpt['model'].names # set class names attribute
58 | if autoshape:
59 | model = model.autoshape() # for file/URI/PIL/cv2/np inputs and NMS
60 | return model.to(device)
61 |
62 | except Exception as e:
63 | help_url = 'https://github.com/ultralytics/yolov5/issues/36'
64 | s = 'Cache may be out of date, try `force_reload=True`. See %s for help.' % help_url
65 | raise Exception(s) from e
66 |
67 |
68 | def custom(path='path/to/model.pt', autoshape=True, verbose=True, device=None):
69 | # YOLOv5 custom or local model
70 | return _create(path, autoshape=autoshape, verbose=verbose, device=device)
71 |
72 |
73 | def yolov5n(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
74 | # YOLOv5-nano model https://github.com/ultralytics/yolov5
75 | return _create('yolov5n', pretrained, channels, classes, autoshape, verbose, device)
76 |
77 |
78 | def yolov5s(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
79 | # YOLOv5-small model https://github.com/ultralytics/yolov5
80 | return _create('yolov5s', pretrained, channels, classes, autoshape, verbose, device)
81 |
82 |
83 | def yolov5m(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
84 | # YOLOv5-medium model https://github.com/ultralytics/yolov5
85 | return _create('yolov5m', pretrained, channels, classes, autoshape, verbose, device)
86 |
87 |
88 | def yolov5l(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
89 | # YOLOv5-large model https://github.com/ultralytics/yolov5
90 | return _create('yolov5l', pretrained, channels, classes, autoshape, verbose, device)
91 |
92 |
93 | def yolov5x(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
94 | # YOLOv5-xlarge model https://github.com/ultralytics/yolov5
95 | return _create('yolov5x', pretrained, channels, classes, autoshape, verbose, device)
96 |
97 |
98 | def yolov5n6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
99 | # YOLOv5-nano-P6 model https://github.com/ultralytics/yolov5
100 | return _create('yolov5n6', pretrained, channels, classes, autoshape, verbose, device)
101 |
102 |
103 | def yolov5s6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
104 | # YOLOv5-small-P6 model https://github.com/ultralytics/yolov5
105 | return _create('yolov5s6', pretrained, channels, classes, autoshape, verbose, device)
106 |
107 |
108 | def yolov5m6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
109 | # YOLOv5-medium-P6 model https://github.com/ultralytics/yolov5
110 | return _create('yolov5m6', pretrained, channels, classes, autoshape, verbose, device)
111 |
112 |
113 | def yolov5l6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
114 | # YOLOv5-large-P6 model https://github.com/ultralytics/yolov5
115 | return _create('yolov5l6', pretrained, channels, classes, autoshape, verbose, device)
116 |
117 |
118 | def yolov5x6(pretrained=True, channels=3, classes=80, autoshape=True, verbose=True, device=None):
119 | # YOLOv5-xlarge-P6 model https://github.com/ultralytics/yolov5
120 | return _create('yolov5x6', pretrained, channels, classes, autoshape, verbose, device)
121 |
122 |
123 | if __name__ == '__main__':
124 | model = _create(name='yolov5s', pretrained=True, channels=3, classes=80, autoshape=True, verbose=True) # pretrained
125 | # model = custom(path='path/to/model.pt') # custom
126 |
127 | # Verify inference
128 | from pathlib import Path
129 |
130 | import cv2
131 | import numpy as np
132 | from PIL import Image
133 |
134 | imgs = ['data/images/zidane.jpg', # filename
135 | Path('data/images/zidane.jpg'), # Path
136 | 'https://ultralytics.com/images/zidane.jpg', # URI
137 | cv2.imread('data/images/bus.jpg')[:, :, ::-1], # OpenCV
138 | Image.open('data/images/bus.jpg'), # PIL
139 | np.zeros((320, 640, 3))] # numpy
140 |
141 | results = model(imgs) # batched inference
142 | results.print()
143 | results.save()
144 |
--------------------------------------------------------------------------------
/images/README.md:
--------------------------------------------------------------------------------
1 | # Some detection results of our methods on VisDrone and UAVDT
2 |
--------------------------------------------------------------------------------
/images/result_in_UAVDT.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cv516Buaa/tph-yolov5/052dfeb375e51756f17e9ca4f96b7e3e3a7cf3c4/images/result_in_UAVDT.png
--------------------------------------------------------------------------------
/images/result_in_VisDrone.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cv516Buaa/tph-yolov5/052dfeb375e51756f17e9ca4f96b7e3e3a7cf3c4/images/result_in_VisDrone.png
--------------------------------------------------------------------------------
/models/__init__.py:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/models/experimental.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Experimental modules
4 | """
5 | import math
6 |
7 | import numpy as np
8 | import torch
9 | import torch.nn as nn
10 |
11 | from models.common import Conv
12 | from utils.downloads import attempt_download
13 |
14 |
15 | class CrossConv(nn.Module):
16 | # Cross Convolution Downsample
17 | def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False):
18 | # ch_in, ch_out, kernel, stride, groups, expansion, shortcut
19 | super().__init__()
20 | c_ = int(c2 * e) # hidden channels
21 | self.cv1 = Conv(c1, c_, (1, k), (1, s))
22 | self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g)
23 | self.add = shortcut and c1 == c2
24 |
25 | def forward(self, x):
26 | return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
27 |
28 |
29 | class Sum(nn.Module):
30 | # Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070
31 | def __init__(self, n, weight=False): # n: number of inputs
32 | super().__init__()
33 | self.weight = weight # apply weights boolean
34 | self.iter = range(n - 1) # iter object
35 | if weight:
36 | self.w = nn.Parameter(-torch.arange(1.0, n) / 2, requires_grad=True) # layer weights
37 |
38 | def forward(self, x):
39 | y = x[0] # no weight
40 | if self.weight:
41 | w = torch.sigmoid(self.w) * 2
42 | for i in self.iter:
43 | y = y + x[i + 1] * w[i]
44 | else:
45 | for i in self.iter:
46 | y = y + x[i + 1]
47 | return y
48 |
49 |
50 | class MixConv2d(nn.Module):
51 | # Mixed Depth-wise Conv https://arxiv.org/abs/1907.09595
52 | def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): # ch_in, ch_out, kernel, stride, ch_strategy
53 | super().__init__()
54 | n = len(k) # number of convolutions
55 | if equal_ch: # equal c_ per group
56 | i = torch.linspace(0, n - 1E-6, c2).floor() # c2 indices
57 | c_ = [(i == g).sum() for g in range(n)] # intermediate channels
58 | else: # equal weight.numel() per group
59 | b = [c2] + [0] * n
60 | a = np.eye(n + 1, n, k=-1)
61 | a -= np.roll(a, 1, axis=1)
62 | a *= np.array(k) ** 2
63 | a[0] = 1
64 | c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b
65 |
66 | self.m = nn.ModuleList(
67 | [nn.Conv2d(c1, int(c_), k, s, k // 2, groups=math.gcd(c1, int(c_)), bias=False) for k, c_ in zip(k, c_)])
68 | self.bn = nn.BatchNorm2d(c2)
69 | self.act = nn.SiLU()
70 |
71 | def forward(self, x):
72 | return self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))
73 |
74 |
75 | class Ensemble(nn.ModuleList):
76 | # Ensemble of models
77 | def __init__(self):
78 | super().__init__()
79 |
80 | def forward(self, x, augment=False, profile=False, visualize=False):
81 | y = []
82 | for module in self:
83 | y.append(module(x, augment, profile, visualize)[0])
84 | # y = torch.stack(y).max(0)[0] # max ensemble
85 | # y = torch.stack(y).mean(0) # mean ensemble
86 | y = torch.cat(y, 1) # nms ensemble
87 | return y, None # inference, train output
88 |
89 |
90 | def attempt_load(weights, map_location=None, inplace=True, fuse=True):
91 | from models.yolo import Detect, Model
92 |
93 | # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
94 | model = Ensemble()
95 | for w in weights if isinstance(weights, list) else [weights]:
96 | ckpt = torch.load(attempt_download(w), map_location=map_location) # load
97 | if fuse:
98 | model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().fuse().eval()) # FP32 model
99 | else:
100 | model.append(ckpt['ema' if ckpt.get('ema') else 'model'].float().eval()) # without layer fuse
101 |
102 | # Compatibility updates
103 | for m in model.modules():
104 | if type(m) in [nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model]:
105 | m.inplace = inplace # pytorch 1.7.0 compatibility
106 | if type(m) is Detect:
107 | if not isinstance(m.anchor_grid, list): # new Detect Layer compatibility
108 | delattr(m, 'anchor_grid')
109 | setattr(m, 'anchor_grid', [torch.zeros(1)] * m.nl)
110 | elif type(m) is Conv:
111 | m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility
112 |
113 | if len(model) == 1:
114 | return model[-1] # return model
115 | else:
116 | print(f'Ensemble created with {weights}\n')
117 | for k in ['names']:
118 | setattr(model, k, getattr(model[-1], k))
119 | model.stride = model[torch.argmax(torch.tensor([m.stride.max() for m in model])).int()].stride # max stride
120 | return model # return ensemble
121 |
--------------------------------------------------------------------------------
/models/hub/anchors.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | # Default anchors for COCO data
3 |
4 |
5 | # P5 -------------------------------------------------------------------------------------------------------------------
6 | # P5-640:
7 | anchors_p5_640:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 |
13 | # P6 -------------------------------------------------------------------------------------------------------------------
14 | # P6-640: thr=0.25: 0.9964 BPR, 5.54 anchors past thr, n=12, img_size=640, metric_all=0.281/0.716-mean/best, past_thr=0.469-mean: 9,11, 21,19, 17,41, 43,32, 39,70, 86,64, 65,131, 134,130, 120,265, 282,180, 247,354, 512,387
15 | anchors_p6_640:
16 | - [9,11, 21,19, 17,41] # P3/8
17 | - [43,32, 39,70, 86,64] # P4/16
18 | - [65,131, 134,130, 120,265] # P5/32
19 | - [282,180, 247,354, 512,387] # P6/64
20 |
21 | # P6-1280: thr=0.25: 0.9950 BPR, 5.55 anchors past thr, n=12, img_size=1280, metric_all=0.281/0.714-mean/best, past_thr=0.468-mean: 19,27, 44,40, 38,94, 96,68, 86,152, 180,137, 140,301, 303,264, 238,542, 436,615, 739,380, 925,792
22 | anchors_p6_1280:
23 | - [19,27, 44,40, 38,94] # P3/8
24 | - [96,68, 86,152, 180,137] # P4/16
25 | - [140,301, 303,264, 238,542] # P5/32
26 | - [436,615, 739,380, 925,792] # P6/64
27 |
28 | # P6-1920: thr=0.25: 0.9950 BPR, 5.55 anchors past thr, n=12, img_size=1920, metric_all=0.281/0.714-mean/best, past_thr=0.468-mean: 28,41, 67,59, 57,141, 144,103, 129,227, 270,205, 209,452, 455,396, 358,812, 653,922, 1109,570, 1387,1187
29 | anchors_p6_1920:
30 | - [28,41, 67,59, 57,141] # P3/8
31 | - [144,103, 129,227, 270,205] # P4/16
32 | - [209,452, 455,396, 358,812] # P5/32
33 | - [653,922, 1109,570, 1387,1187] # P6/64
34 |
35 |
36 | # P7 -------------------------------------------------------------------------------------------------------------------
37 | # P7-640: thr=0.25: 0.9962 BPR, 6.76 anchors past thr, n=15, img_size=640, metric_all=0.275/0.733-mean/best, past_thr=0.466-mean: 11,11, 13,30, 29,20, 30,46, 61,38, 39,92, 78,80, 146,66, 79,163, 149,150, 321,143, 157,303, 257,402, 359,290, 524,372
38 | anchors_p7_640:
39 | - [11,11, 13,30, 29,20] # P3/8
40 | - [30,46, 61,38, 39,92] # P4/16
41 | - [78,80, 146,66, 79,163] # P5/32
42 | - [149,150, 321,143, 157,303] # P6/64
43 | - [257,402, 359,290, 524,372] # P7/128
44 |
45 | # P7-1280: thr=0.25: 0.9968 BPR, 6.71 anchors past thr, n=15, img_size=1280, metric_all=0.273/0.732-mean/best, past_thr=0.463-mean: 19,22, 54,36, 32,77, 70,83, 138,71, 75,173, 165,159, 148,334, 375,151, 334,317, 251,626, 499,474, 750,326, 534,814, 1079,818
46 | anchors_p7_1280:
47 | - [19,22, 54,36, 32,77] # P3/8
48 | - [70,83, 138,71, 75,173] # P4/16
49 | - [165,159, 148,334, 375,151] # P5/32
50 | - [334,317, 251,626, 499,474] # P6/64
51 | - [750,326, 534,814, 1079,818] # P7/128
52 |
53 | # P7-1920: thr=0.25: 0.9968 BPR, 6.71 anchors past thr, n=15, img_size=1920, metric_all=0.273/0.732-mean/best, past_thr=0.463-mean: 29,34, 81,55, 47,115, 105,124, 207,107, 113,259, 247,238, 222,500, 563,227, 501,476, 376,939, 749,711, 1126,489, 801,1222, 1618,1227
54 | anchors_p7_1920:
55 | - [29,34, 81,55, 47,115] # P3/8
56 | - [105,124, 207,107, 113,259] # P4/16
57 | - [247,238, 222,500, 563,227] # P5/32
58 | - [501,476, 376,939, 749,711] # P6/64
59 | - [1126,489, 801,1222, 1618,1227] # P7/128
60 |
--------------------------------------------------------------------------------
/models/hub/yolov3-spp.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # darknet53 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Conv, [32, 3, 1]], # 0
16 | [-1, 1, Conv, [64, 3, 2]], # 1-P1/2
17 | [-1, 1, Bottleneck, [64]],
18 | [-1, 1, Conv, [128, 3, 2]], # 3-P2/4
19 | [-1, 2, Bottleneck, [128]],
20 | [-1, 1, Conv, [256, 3, 2]], # 5-P3/8
21 | [-1, 8, Bottleneck, [256]],
22 | [-1, 1, Conv, [512, 3, 2]], # 7-P4/16
23 | [-1, 8, Bottleneck, [512]],
24 | [-1, 1, Conv, [1024, 3, 2]], # 9-P5/32
25 | [-1, 4, Bottleneck, [1024]], # 10
26 | ]
27 |
28 | # YOLOv3-SPP head
29 | head:
30 | [[-1, 1, Bottleneck, [1024, False]],
31 | [-1, 1, SPP, [512, [5, 9, 13]]],
32 | [-1, 1, Conv, [1024, 3, 1]],
33 | [-1, 1, Conv, [512, 1, 1]],
34 | [-1, 1, Conv, [1024, 3, 1]], # 15 (P5/32-large)
35 |
36 | [-2, 1, Conv, [256, 1, 1]],
37 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
38 | [[-1, 8], 1, Concat, [1]], # cat backbone P4
39 | [-1, 1, Bottleneck, [512, False]],
40 | [-1, 1, Bottleneck, [512, False]],
41 | [-1, 1, Conv, [256, 1, 1]],
42 | [-1, 1, Conv, [512, 3, 1]], # 22 (P4/16-medium)
43 |
44 | [-2, 1, Conv, [128, 1, 1]],
45 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
46 | [[-1, 6], 1, Concat, [1]], # cat backbone P3
47 | [-1, 1, Bottleneck, [256, False]],
48 | [-1, 2, Bottleneck, [256, False]], # 27 (P3/8-small)
49 |
50 | [[27, 22, 15], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
51 | ]
52 |
--------------------------------------------------------------------------------
/models/hub/yolov3-tiny.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors:
8 | - [10,14, 23,27, 37,58] # P4/16
9 | - [81,82, 135,169, 344,319] # P5/32
10 |
11 | # YOLOv3-tiny backbone
12 | backbone:
13 | # [from, number, module, args]
14 | [[-1, 1, Conv, [16, 3, 1]], # 0
15 | [-1, 1, nn.MaxPool2d, [2, 2, 0]], # 1-P1/2
16 | [-1, 1, Conv, [32, 3, 1]],
17 | [-1, 1, nn.MaxPool2d, [2, 2, 0]], # 3-P2/4
18 | [-1, 1, Conv, [64, 3, 1]],
19 | [-1, 1, nn.MaxPool2d, [2, 2, 0]], # 5-P3/8
20 | [-1, 1, Conv, [128, 3, 1]],
21 | [-1, 1, nn.MaxPool2d, [2, 2, 0]], # 7-P4/16
22 | [-1, 1, Conv, [256, 3, 1]],
23 | [-1, 1, nn.MaxPool2d, [2, 2, 0]], # 9-P5/32
24 | [-1, 1, Conv, [512, 3, 1]],
25 | [-1, 1, nn.ZeroPad2d, [[0, 1, 0, 1]]], # 11
26 | [-1, 1, nn.MaxPool2d, [2, 1, 0]], # 12
27 | ]
28 |
29 | # YOLOv3-tiny head
30 | head:
31 | [[-1, 1, Conv, [1024, 3, 1]],
32 | [-1, 1, Conv, [256, 1, 1]],
33 | [-1, 1, Conv, [512, 3, 1]], # 15 (P5/32-large)
34 |
35 | [-2, 1, Conv, [128, 1, 1]],
36 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
37 | [[-1, 8], 1, Concat, [1]], # cat backbone P4
38 | [-1, 1, Conv, [256, 3, 1]], # 19 (P4/16-medium)
39 |
40 | [[19, 15], 1, Detect, [nc, anchors]], # Detect(P4, P5)
41 | ]
42 |
--------------------------------------------------------------------------------
/models/hub/yolov3.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # darknet53 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Conv, [32, 3, 1]], # 0
16 | [-1, 1, Conv, [64, 3, 2]], # 1-P1/2
17 | [-1, 1, Bottleneck, [64]],
18 | [-1, 1, Conv, [128, 3, 2]], # 3-P2/4
19 | [-1, 2, Bottleneck, [128]],
20 | [-1, 1, Conv, [256, 3, 2]], # 5-P3/8
21 | [-1, 8, Bottleneck, [256]],
22 | [-1, 1, Conv, [512, 3, 2]], # 7-P4/16
23 | [-1, 8, Bottleneck, [512]],
24 | [-1, 1, Conv, [1024, 3, 2]], # 9-P5/32
25 | [-1, 4, Bottleneck, [1024]], # 10
26 | ]
27 |
28 | # YOLOv3 head
29 | head:
30 | [[-1, 1, Bottleneck, [1024, False]],
31 | [-1, 1, Conv, [512, [1, 1]]],
32 | [-1, 1, Conv, [1024, 3, 1]],
33 | [-1, 1, Conv, [512, 1, 1]],
34 | [-1, 1, Conv, [1024, 3, 1]], # 15 (P5/32-large)
35 |
36 | [-2, 1, Conv, [256, 1, 1]],
37 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
38 | [[-1, 8], 1, Concat, [1]], # cat backbone P4
39 | [-1, 1, Bottleneck, [512, False]],
40 | [-1, 1, Bottleneck, [512, False]],
41 | [-1, 1, Conv, [256, 1, 1]],
42 | [-1, 1, Conv, [512, 3, 1]], # 22 (P4/16-medium)
43 |
44 | [-2, 1, Conv, [128, 1, 1]],
45 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
46 | [[-1, 6], 1, Concat, [1]], # cat backbone P3
47 | [-1, 1, Bottleneck, [256, False]],
48 | [-1, 2, Bottleneck, [256, False]], # 27 (P3/8-small)
49 |
50 | [[27, 22, 15], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
51 | ]
52 |
--------------------------------------------------------------------------------
/models/hub/yolov5-bifpn.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 9, C3, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 1, SPP, [1024, [5, 9, 13]]],
24 | [-1, 3, C3, [1024, False]], # 9
25 | ]
26 |
27 | # YOLOv5 BiFPN head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, Conv, [256, 3, 2]],
40 | [[-1, 14, 6], 1, Concat, [1]], # cat P4
41 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, Conv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/models/hub/yolov5-fpn.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, Bottleneck, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 9, BottleneckCSP, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, BottleneckCSP, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 1, SPP, [1024, [5, 9, 13]]],
24 | [-1, 6, BottleneckCSP, [1024]], # 9
25 | ]
26 |
27 | # YOLOv5 FPN head
28 | head:
29 | [[-1, 3, BottleneckCSP, [1024, False]], # 10 (P5/32-large)
30 |
31 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
33 | [-1, 1, Conv, [512, 1, 1]],
34 | [-1, 3, BottleneckCSP, [512, False]], # 14 (P4/16-medium)
35 |
36 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
38 | [-1, 1, Conv, [256, 1, 1]],
39 | [-1, 3, BottleneckCSP, [256, False]], # 18 (P3/8-small)
40 |
41 | [[18, 14, 10], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
42 | ]
43 |
--------------------------------------------------------------------------------
/models/hub/yolov5-p2.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors: 3
8 |
9 | # YOLOv5 backbone
10 | backbone:
11 | # [from, number, module, args]
12 | [[-1, 1, Focus, [64, 3]], # 0-P1/2
13 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
14 | [-1, 3, C3, [128]],
15 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
16 | [-1, 9, C3, [256]],
17 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
18 | [-1, 9, C3, [512]],
19 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
20 | [-1, 1, SPP, [1024, [5, 9, 13]]],
21 | [-1, 3, C3, [1024, False]], # 9
22 | ]
23 |
24 | # YOLOv5 head
25 | head:
26 | [[-1, 1, Conv, [512, 1, 1]],
27 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
28 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
29 | [-1, 3, C3, [512, False]], # 13
30 |
31 | [-1, 1, Conv, [256, 1, 1]],
32 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
33 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
34 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
35 |
36 | [-1, 1, Conv, [128, 1, 1]],
37 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
38 | [[-1, 2], 1, Concat, [1]], # cat backbone P2
39 | [-1, 1, C3, [128, False]], # 21 (P2/4-xsmall)
40 |
41 | [-1, 1, Conv, [128, 3, 2]],
42 | [[-1, 18], 1, Concat, [1]], # cat head P3
43 | [-1, 3, C3, [256, False]], # 24 (P3/8-small)
44 |
45 | [-1, 1, Conv, [256, 3, 2]],
46 | [[-1, 14], 1, Concat, [1]], # cat head P4
47 | [-1, 3, C3, [512, False]], # 27 (P4/16-medium)
48 |
49 | [-1, 1, Conv, [512, 3, 2]],
50 | [[-1, 10], 1, Concat, [1]], # cat head P5
51 | [-1, 3, C3, [1024, False]], # 30 (P5/32-large)
52 |
53 | [[21, 24, 27, 30], 1, Detect, [nc, anchors]], # Detect(P2, P3, P4, P5)
54 | ]
55 |
--------------------------------------------------------------------------------
/models/hub/yolov5-p6.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors: 3
8 |
9 | # YOLOv5 backbone
10 | backbone:
11 | # [from, number, module, args]
12 | [[-1, 1, Focus, [64, 3]], # 0-P1/2
13 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
14 | [-1, 3, C3, [128]],
15 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
16 | [-1, 9, C3, [256]],
17 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
18 | [-1, 9, C3, [512]],
19 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32
20 | [-1, 3, C3, [768]],
21 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
22 | [-1, 1, SPP, [1024, [3, 5, 7]]],
23 | [-1, 3, C3, [1024, False]], # 11
24 | ]
25 |
26 | # YOLOv5 head
27 | head:
28 | [[-1, 1, Conv, [768, 1, 1]],
29 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
30 | [[-1, 8], 1, Concat, [1]], # cat backbone P5
31 | [-1, 3, C3, [768, False]], # 15
32 |
33 | [-1, 1, Conv, [512, 1, 1]],
34 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
35 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
36 | [-1, 3, C3, [512, False]], # 19
37 |
38 | [-1, 1, Conv, [256, 1, 1]],
39 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
40 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
41 | [-1, 3, C3, [256, False]], # 23 (P3/8-small)
42 |
43 | [-1, 1, Conv, [256, 3, 2]],
44 | [[-1, 20], 1, Concat, [1]], # cat head P4
45 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium)
46 |
47 | [-1, 1, Conv, [512, 3, 2]],
48 | [[-1, 16], 1, Concat, [1]], # cat head P5
49 | [-1, 3, C3, [768, False]], # 29 (P5/32-large)
50 |
51 | [-1, 1, Conv, [768, 3, 2]],
52 | [[-1, 12], 1, Concat, [1]], # cat head P6
53 | [-1, 3, C3, [1024, False]], # 32 (P5/64-xlarge)
54 |
55 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
56 | ]
57 |
--------------------------------------------------------------------------------
/models/hub/yolov5-p7.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors: 3
8 |
9 | # YOLOv5 backbone
10 | backbone:
11 | # [from, number, module, args]
12 | [[-1, 1, Focus, [64, 3]], # 0-P1/2
13 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
14 | [-1, 3, C3, [128]],
15 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
16 | [-1, 9, C3, [256]],
17 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
18 | [-1, 9, C3, [512]],
19 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32
20 | [-1, 3, C3, [768]],
21 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
22 | [-1, 3, C3, [1024]],
23 | [-1, 1, Conv, [1280, 3, 2]], # 11-P7/128
24 | [-1, 1, SPP, [1280, [3, 5]]],
25 | [-1, 3, C3, [1280, False]], # 13
26 | ]
27 |
28 | # YOLOv5 head
29 | head:
30 | [[-1, 1, Conv, [1024, 1, 1]],
31 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
32 | [[-1, 10], 1, Concat, [1]], # cat backbone P6
33 | [-1, 3, C3, [1024, False]], # 17
34 |
35 | [-1, 1, Conv, [768, 1, 1]],
36 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
37 | [[-1, 8], 1, Concat, [1]], # cat backbone P5
38 | [-1, 3, C3, [768, False]], # 21
39 |
40 | [-1, 1, Conv, [512, 1, 1]],
41 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
42 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
43 | [-1, 3, C3, [512, False]], # 25
44 |
45 | [-1, 1, Conv, [256, 1, 1]],
46 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
47 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
48 | [-1, 3, C3, [256, False]], # 29 (P3/8-small)
49 |
50 | [-1, 1, Conv, [256, 3, 2]],
51 | [[-1, 26], 1, Concat, [1]], # cat head P4
52 | [-1, 3, C3, [512, False]], # 32 (P4/16-medium)
53 |
54 | [-1, 1, Conv, [512, 3, 2]],
55 | [[-1, 22], 1, Concat, [1]], # cat head P5
56 | [-1, 3, C3, [768, False]], # 35 (P5/32-large)
57 |
58 | [-1, 1, Conv, [768, 3, 2]],
59 | [[-1, 18], 1, Concat, [1]], # cat head P6
60 | [-1, 3, C3, [1024, False]], # 38 (P6/64-xlarge)
61 |
62 | [-1, 1, Conv, [1024, 3, 2]],
63 | [[-1, 14], 1, Concat, [1]], # cat head P7
64 | [-1, 3, C3, [1280, False]], # 41 (P7/128-xxlarge)
65 |
66 | [[29, 32, 35, 38, 41], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6, P7)
67 | ]
68 |
--------------------------------------------------------------------------------
/models/hub/yolov5-panet.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, BottleneckCSP, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 9, BottleneckCSP, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, BottleneckCSP, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 1, SPP, [1024, [5, 9, 13]]],
24 | [-1, 3, BottleneckCSP, [1024, False]], # 9
25 | ]
26 |
27 | # YOLOv5 PANet head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, BottleneckCSP, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, BottleneckCSP, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, Conv, [256, 3, 2]],
40 | [[-1, 14], 1, Concat, [1]], # cat head P4
41 | [-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, Conv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/models/hub/yolov5l6.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors:
8 | - [19,27, 44,40, 38,94] # P3/8
9 | - [96,68, 86,152, 180,137] # P4/16
10 | - [140,301, 303,264, 238,542] # P5/32
11 | - [436,615, 739,380, 925,792] # P6/64
12 |
13 | # YOLOv5 v6.0 backbone
14 | backbone:
15 | # [from, number, module, args]
16 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18 | [-1, 3, C3, [128]],
19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20 | [-1, 6, C3, [256]],
21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22 | [-1, 9, C3, [512]],
23 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32
24 | [-1, 3, C3, [768]],
25 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
26 | [-1, 3, C3, [1024]],
27 | [-1, 1, SPPF, [1024, 5]], # 11
28 | ]
29 |
30 | # YOLOv5 v6.0 head
31 | head:
32 | [[-1, 1, Conv, [768, 1, 1]],
33 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
34 | [[-1, 8], 1, Concat, [1]], # cat backbone P5
35 | [-1, 3, C3, [768, False]], # 15
36 |
37 | [-1, 1, Conv, [512, 1, 1]],
38 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
39 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
40 | [-1, 3, C3, [512, False]], # 19
41 |
42 | [-1, 1, Conv, [256, 1, 1]],
43 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
44 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
45 | [-1, 3, C3, [256, False]], # 23 (P3/8-small)
46 |
47 | [-1, 1, Conv, [256, 3, 2]],
48 | [[-1, 20], 1, Concat, [1]], # cat head P4
49 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium)
50 |
51 | [-1, 1, Conv, [512, 3, 2]],
52 | [[-1, 16], 1, Concat, [1]], # cat head P5
53 | [-1, 3, C3, [768, False]], # 29 (P5/32-large)
54 |
55 | [-1, 1, Conv, [768, 3, 2]],
56 | [[-1, 12], 1, Concat, [1]], # cat head P6
57 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
58 |
59 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
60 | ]
61 |
--------------------------------------------------------------------------------
/models/hub/yolov5m6.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 0.67 # model depth multiple
6 | width_multiple: 0.75 # layer channel multiple
7 | anchors:
8 | - [19,27, 44,40, 38,94] # P3/8
9 | - [96,68, 86,152, 180,137] # P4/16
10 | - [140,301, 303,264, 238,542] # P5/32
11 | - [436,615, 739,380, 925,792] # P6/64
12 |
13 | # YOLOv5 v6.0 backbone
14 | backbone:
15 | # [from, number, module, args]
16 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18 | [-1, 3, C3, [128]],
19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20 | [-1, 6, C3, [256]],
21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22 | [-1, 9, C3, [512]],
23 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32
24 | [-1, 3, C3, [768]],
25 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
26 | [-1, 3, C3, [1024]],
27 | [-1, 1, SPPF, [1024, 5]], # 11
28 | ]
29 |
30 | # YOLOv5 v6.0 head
31 | head:
32 | [[-1, 1, Conv, [768, 1, 1]],
33 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
34 | [[-1, 8], 1, Concat, [1]], # cat backbone P5
35 | [-1, 3, C3, [768, False]], # 15
36 |
37 | [-1, 1, Conv, [512, 1, 1]],
38 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
39 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
40 | [-1, 3, C3, [512, False]], # 19
41 |
42 | [-1, 1, Conv, [256, 1, 1]],
43 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
44 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
45 | [-1, 3, C3, [256, False]], # 23 (P3/8-small)
46 |
47 | [-1, 1, Conv, [256, 3, 2]],
48 | [[-1, 20], 1, Concat, [1]], # cat head P4
49 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium)
50 |
51 | [-1, 1, Conv, [512, 3, 2]],
52 | [[-1, 16], 1, Concat, [1]], # cat head P5
53 | [-1, 3, C3, [768, False]], # 29 (P5/32-large)
54 |
55 | [-1, 1, Conv, [768, 3, 2]],
56 | [[-1, 12], 1, Concat, [1]], # cat head P6
57 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
58 |
59 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
60 | ]
61 |
--------------------------------------------------------------------------------
/models/hub/yolov5n6.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 0.33 # model depth multiple
6 | width_multiple: 0.25 # layer channel multiple
7 | anchors:
8 | - [19,27, 44,40, 38,94] # P3/8
9 | - [96,68, 86,152, 180,137] # P4/16
10 | - [140,301, 303,264, 238,542] # P5/32
11 | - [436,615, 739,380, 925,792] # P6/64
12 |
13 | # YOLOv5 v6.0 backbone
14 | backbone:
15 | # [from, number, module, args]
16 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18 | [-1, 3, C3, [128]],
19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20 | [-1, 6, C3, [256]],
21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22 | [-1, 9, C3, [512]],
23 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32
24 | [-1, 3, C3, [768]],
25 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
26 | [-1, 3, C3, [1024]],
27 | [-1, 1, SPPF, [1024, 5]], # 11
28 | ]
29 |
30 | # YOLOv5 v6.0 head
31 | head:
32 | [[-1, 1, Conv, [768, 1, 1]],
33 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
34 | [[-1, 8], 1, Concat, [1]], # cat backbone P5
35 | [-1, 3, C3, [768, False]], # 15
36 |
37 | [-1, 1, Conv, [512, 1, 1]],
38 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
39 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
40 | [-1, 3, C3, [512, False]], # 19
41 |
42 | [-1, 1, Conv, [256, 1, 1]],
43 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
44 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
45 | [-1, 3, C3, [256, False]], # 23 (P3/8-small)
46 |
47 | [-1, 1, Conv, [256, 3, 2]],
48 | [[-1, 20], 1, Concat, [1]], # cat head P4
49 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium)
50 |
51 | [-1, 1, Conv, [512, 3, 2]],
52 | [[-1, 16], 1, Concat, [1]], # cat head P5
53 | [-1, 3, C3, [768, False]], # 29 (P5/32-large)
54 |
55 | [-1, 1, Conv, [768, 3, 2]],
56 | [[-1, 12], 1, Concat, [1]], # cat head P6
57 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
58 |
59 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
60 | ]
61 |
--------------------------------------------------------------------------------
/models/hub/yolov5s-ghost.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 0.33 # model depth multiple
6 | width_multiple: 0.50 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2
16 | [-1, 1, GhostConv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3Ghost, [128]],
18 | [-1, 1, GhostConv, [256, 3, 2]], # 3-P3/8
19 | [-1, 9, C3Ghost, [256]],
20 | [-1, 1, GhostConv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3Ghost, [512]],
22 | [-1, 1, GhostConv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 1, SPP, [1024, [5, 9, 13]]],
24 | [-1, 3, C3Ghost, [1024, False]], # 9
25 | ]
26 |
27 | # YOLOv5 head
28 | head:
29 | [[-1, 1, GhostConv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3Ghost, [512, False]], # 13
33 |
34 | [-1, 1, GhostConv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3Ghost, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, GhostConv, [256, 3, 2]],
40 | [[-1, 14], 1, Concat, [1]], # cat head P4
41 | [-1, 3, C3Ghost, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, GhostConv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 3, C3Ghost, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/models/hub/yolov5s-transformer.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 0.33 # model depth multiple
6 | width_multiple: 0.50 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 9, C3, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 1, SPP, [1024, [5, 9, 13]]],
24 | [-1, 3, C3TR, [1024, False]], # 9 <-------- C3TR() Transformer module
25 | ]
26 |
27 | # YOLOv5 head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, Conv, [256, 3, 2]],
40 | [[-1, 14], 1, Concat, [1]], # cat head P4
41 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, Conv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/models/hub/yolov5s6.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 0.33 # model depth multiple
6 | width_multiple: 0.50 # layer channel multiple
7 | anchors:
8 | - [19,27, 44,40, 38,94] # P3/8
9 | - [96,68, 86,152, 180,137] # P4/16
10 | - [140,301, 303,264, 238,542] # P5/32
11 | - [436,615, 739,380, 925,792] # P6/64
12 |
13 | # YOLOv5 v6.0 backbone
14 | backbone:
15 | # [from, number, module, args]
16 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18 | [-1, 3, C3, [128]],
19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20 | [-1, 6, C3, [256]],
21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22 | [-1, 9, C3, [512]],
23 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32
24 | [-1, 3, C3, [768]],
25 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
26 | [-1, 3, C3, [1024]],
27 | [-1, 1, SPPF, [1024, 5]], # 11
28 | ]
29 |
30 | # YOLOv5 v6.0 head
31 | head:
32 | [[-1, 1, Conv, [768, 1, 1]],
33 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
34 | [[-1, 8], 1, Concat, [1]], # cat backbone P5
35 | [-1, 3, C3, [768, False]], # 15
36 |
37 | [-1, 1, Conv, [512, 1, 1]],
38 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
39 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
40 | [-1, 3, C3, [512, False]], # 19
41 |
42 | [-1, 1, Conv, [256, 1, 1]],
43 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
44 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
45 | [-1, 3, C3, [256, False]], # 23 (P3/8-small)
46 |
47 | [-1, 1, Conv, [256, 3, 2]],
48 | [[-1, 20], 1, Concat, [1]], # cat head P4
49 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium)
50 |
51 | [-1, 1, Conv, [512, 3, 2]],
52 | [[-1, 16], 1, Concat, [1]], # cat head P5
53 | [-1, 3, C3, [768, False]], # 29 (P5/32-large)
54 |
55 | [-1, 1, Conv, [768, 3, 2]],
56 | [[-1, 12], 1, Concat, [1]], # cat head P6
57 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
58 |
59 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
60 | ]
61 |
--------------------------------------------------------------------------------
/models/hub/yolov5x6.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.33 # model depth multiple
6 | width_multiple: 1.25 # layer channel multiple
7 | anchors:
8 | - [19,27, 44,40, 38,94] # P3/8
9 | - [96,68, 86,152, 180,137] # P4/16
10 | - [140,301, 303,264, 238,542] # P5/32
11 | - [436,615, 739,380, 925,792] # P6/64
12 |
13 | # YOLOv5 v6.0 backbone
14 | backbone:
15 | # [from, number, module, args]
16 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
18 | [-1, 3, C3, [128]],
19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
20 | [-1, 6, C3, [256]],
21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
22 | [-1, 9, C3, [512]],
23 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32
24 | [-1, 3, C3, [768]],
25 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
26 | [-1, 3, C3, [1024]],
27 | [-1, 1, SPPF, [1024, 5]], # 11
28 | ]
29 |
30 | # YOLOv5 v6.0 head
31 | head:
32 | [[-1, 1, Conv, [768, 1, 1]],
33 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
34 | [[-1, 8], 1, Concat, [1]], # cat backbone P5
35 | [-1, 3, C3, [768, False]], # 15
36 |
37 | [-1, 1, Conv, [512, 1, 1]],
38 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
39 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
40 | [-1, 3, C3, [512, False]], # 19
41 |
42 | [-1, 1, Conv, [256, 1, 1]],
43 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
44 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
45 | [-1, 3, C3, [256, False]], # 23 (P3/8-small)
46 |
47 | [-1, 1, Conv, [256, 3, 2]],
48 | [[-1, 20], 1, Concat, [1]], # cat head P4
49 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium)
50 |
51 | [-1, 1, Conv, [512, 3, 2]],
52 | [[-1, 16], 1, Concat, [1]], # cat head P5
53 | [-1, 3, C3, [768, False]], # 29 (P5/32-large)
54 |
55 | [-1, 1, Conv, [768, 3, 2]],
56 | [[-1, 12], 1, Concat, [1]], # cat head P6
57 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
58 |
59 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
60 | ]
61 |
--------------------------------------------------------------------------------
/models/yolov5l-tph-plus.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors: 3
8 | # - [10,13, 16,30, 33,23] # P3/8
9 | # - [30,61, 62,45, 59,119] # P4/16
10 | # - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 v6.0 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 6, C3, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 3, C3, [1024]],
24 | [-1, 1, SPPF, [1024, 5]], # 9
25 | ]
26 |
27 | # YOLOv5 v6.0 head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, Conv, [256, 3, 2]],
40 | [[-1, 14], 1, Concat, [1]], # cat head P4
41 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, Conv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 1, C3STR, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[2, 17, 20, 23], 1, CLLADetect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/models/yolov5l-xs-tph.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors: 4
8 | # - [10,13, 16,30, 33,23] # P3/8
9 | # - [30,61, 62,45, 59,119] # P4/16
10 | # - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 v6.0 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 6, C3, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 3, C3, [1024]],
24 | [-1, 1, SPPF, [1024, 5]], # 9
25 | ]
26 |
27 | # YOLOv5 v6.0 head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
38 |
39 | [ -1, 1, Conv, [ 128, 1, 1 ] ],
40 | [ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ],
41 | [ [ -1, 2 ], 1, Concat, [ 1 ] ], # cat backbone P2
42 | [ -1, 2, C3STR, [ 128, False ] ], # 21 (P2/4-xsmall)
43 |
44 | [ -1, 1, Conv, [ 128, 3, 2 ] ],
45 | [ [ -1, 18, 4], 1, Concat, [ 1 ] ], # cat head P3
46 | [ -1, 2, C3STR, [ 256, False ] ], # 24 (P3/8-small)
47 |
48 | [-1, 1, Conv, [256, 3, 2]],
49 | [[-1, 14, 6], 1, Concat, [1]], # cat head P4
50 | [-1, 2, C3STR, [512, False]], # 27 (P4/16-medium)
51 |
52 | [-1, 1, Conv, [512, 3, 2]],
53 | [[-1, 10], 1, Concat, [1]], # cat head P5
54 | [-1, 2, C3STR, [1024, False]], # 30 (P5/32-large)
55 |
56 | [[21, 24, 27, 30], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
57 | ]
58 |
--------------------------------------------------------------------------------
/models/yolov5l-xs-tr-cbam-spp-bifpn.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors: 4
8 | # - [10,13, 16,30, 33,23] # P3/8
9 | # - [30,61, 62,45, 59,119] # P4/16
10 | # - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 v6.0 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 6, C3, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 3, C3TR, [1024]],
24 | [-1, 1, SPPF, [1024, 5]], # 9
25 | ]
26 |
27 | # YOLOv5 v6.0 head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
38 |
39 | [ -1, 1, Conv, [ 128, 1, 1 ] ],
40 | [ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ],
41 | [ [ -1, 2 ], 1, Concat, [ 1 ] ], # cat backbone P2
42 | [-1, 1, SPP, [128, [5, 9, 13]]],
43 | [ -1, 3, C3, [ 128, False ] ], # (P2/4-xsmall)
44 | [-1, 1, CBAM, [128]], # 23
45 |
46 | [ -1, 1, Conv, [ 128, 3, 2 ] ],
47 | [ [ -1, 18, 4], 1, Concat, [ 1 ] ], # cat head P3
48 | [-1, 1, SPP, [256, [5, 9, 13]]],
49 | [ -1, 3, C3, [ 256, False ] ], # (P3/8-small)
50 | [-1, 1, CBAM, [256]], # 28
51 |
52 | [-1, 1, Conv, [256, 3, 2]],
53 | [[-1, 14, 6], 1, Concat, [1]], # cat head P4
54 | [-1, 1, SPP, [512, [3, 7, 11]]],
55 | [-1, 3, C3, [512, False]], # (P4/16-medium)
56 | [-1, 1, CBAM, [512]], # 33
57 |
58 | [-1, 1, Conv, [512, 3, 2]],
59 | [[-1, 10], 1, Concat, [1]], # cat head P5
60 | [-1, 1, SPP, [1024, [3, 5, 7]]],
61 | [-1, 3, C3TR, [1024, False]], # (P5/32-large)
62 | [-1, 1, CBAM, [1024]], # 38
63 |
64 | [[23, 28, 33, 38], 1, Detect, [nc,anchors]], # Detect(P2, P3, P4, P5)
65 | ]
66 |
--------------------------------------------------------------------------------
/models/yolov5l.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.0 # model depth multiple
6 | width_multiple: 1.0 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 v6.0 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 6, C3, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 3, C3, [1024]],
24 | [-1, 1, SPPF, [1024, 5]], # 9
25 | ]
26 |
27 | # YOLOv5 v6.0 head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, Conv, [256, 3, 2]],
40 | [[-1, 14], 1, Concat, [1]], # cat head P4
41 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, Conv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/models/yolov5m.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 0.67 # model depth multiple
6 | width_multiple: 0.75 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 v6.0 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 6, C3, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 3, C3, [1024]],
24 | [-1, 1, SPPF, [1024, 5]], # 9
25 | ]
26 |
27 | # YOLOv5 v6.0 head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, Conv, [256, 3, 2]],
40 | [[-1, 14], 1, Concat, [1]], # cat head P4
41 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, Conv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/models/yolov5n.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 0.33 # model depth multiple
6 | width_multiple: 0.25 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 v6.0 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 6, C3, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 3, C3, [1024]],
24 | [-1, 1, SPPF, [1024, 5]], # 9
25 | ]
26 |
27 | # YOLOv5 v6.0 head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, Conv, [256, 3, 2]],
40 | [[-1, 14], 1, Concat, [1]], # cat head P4
41 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, Conv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/models/yolov5s.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 0.33 # model depth multiple
6 | width_multiple: 0.50 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 v6.0 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 6, C3, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 3, C3, [1024]],
24 | [-1, 1, SPPF, [1024, 5]], # 9
25 | ]
26 |
27 | # YOLOv5 v6.0 head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, Conv, [256, 3, 2]],
40 | [[-1, 14], 1, Concat, [1]], # cat head P4
41 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, Conv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/models/yolov5x.yaml:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 |
3 | # Parameters
4 | nc: 80 # number of classes
5 | depth_multiple: 1.33 # model depth multiple
6 | width_multiple: 1.25 # layer channel multiple
7 | anchors:
8 | - [10,13, 16,30, 33,23] # P3/8
9 | - [30,61, 62,45, 59,119] # P4/16
10 | - [116,90, 156,198, 373,326] # P5/32
11 |
12 | # YOLOv5 v6.0 backbone
13 | backbone:
14 | # [from, number, module, args]
15 | [[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4
17 | [-1, 3, C3, [128]],
18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
19 | [-1, 6, C3, [256]],
20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
21 | [-1, 9, C3, [512]],
22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
23 | [-1, 3, C3, [1024]],
24 | [-1, 1, SPPF, [1024, 5]], # 9
25 | ]
26 |
27 | # YOLOv5 v6.0 head
28 | head:
29 | [[-1, 1, Conv, [512, 1, 1]],
30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4
32 | [-1, 3, C3, [512, False]], # 13
33 |
34 | [-1, 1, Conv, [256, 1, 1]],
35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']],
36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3
37 | [-1, 3, C3, [256, False]], # 17 (P3/8-small)
38 |
39 | [-1, 1, Conv, [256, 3, 2]],
40 | [[-1, 14], 1, Concat, [1]], # cat head P4
41 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
42 |
43 | [-1, 1, Conv, [512, 3, 2]],
44 | [[-1, 10], 1, Concat, [1]], # cat head P5
45 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
46 |
47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
48 | ]
49 |
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | # pip install -r requirements.txt
2 |
3 | # Base ----------------------------------------
4 | matplotlib>=3.2.2
5 | numpy>=1.18.5
6 | opencv-python>=4.1.2
7 | Pillow>=7.1.2
8 | PyYAML>=5.3.1
9 | requests>=2.23.0
10 | scipy>=1.4.1
11 | torch>=1.7.0
12 | torchvision>=0.8.1
13 | tqdm>=4.41.0
14 |
15 | # Logging -------------------------------------
16 | tensorboard>=2.4.1
17 | wandb
18 |
19 | # Plotting ------------------------------------
20 | pandas>=1.1.4
21 | seaborn>=0.11.0
22 |
23 | # Export --------------------------------------
24 | # coremltools>=4.1 # CoreML export
25 | # onnx>=1.9.0 # ONNX export
26 | # onnx-simplifier>=0.3.6 # ONNX simplifier
27 | # scikit-learn==0.19.2 # CoreML quantization
28 | # tensorflow>=2.4.1 # TFLite export
29 | # tensorflowjs>=3.9.0 # TF.js export
30 |
31 | # Extras --------------------------------------
32 | albumentations>=1.0.3
33 | # Cython # for pycocotools https://github.com/cocodataset/cocoapi/issues/172
34 | # pycocotools>=2.0 # COCO mAP
35 | # roboflow
36 | thop # FLOPs computation
37 | ensemble_boxes
--------------------------------------------------------------------------------
/result.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cv516Buaa/tph-yolov5/052dfeb375e51756f17e9ca4f96b7e3e3a7cf3c4/result.png
--------------------------------------------------------------------------------
/setup.cfg:
--------------------------------------------------------------------------------
1 | # Project-wide configuration file, can be used for package metadata and other toll configurations
2 | # Example usage: global configuration for PEP8 (via flake8) setting or default pytest arguments
3 |
4 | [metadata]
5 | license_file = LICENSE
6 | description-file = README.md
7 |
8 |
9 | [tool:pytest]
10 | norecursedirs =
11 | .git
12 | dist
13 | build
14 | addopts =
15 | --doctest-modules
16 | --durations=25
17 | --color=yes
18 |
19 |
20 | [flake8]
21 | max-line-length = 120
22 | exclude = .tox,*.egg,build,temp
23 | select = E,W,F
24 | doctests = True
25 | verbose = 2
26 | # https://pep8.readthedocs.io/en/latest/intro.html#error-codes
27 | format = pylint
28 | # see: https://www.flake8rules.com/
29 | ignore =
30 | E731 # Do not assign a lambda expression, use a def
31 | F405
32 | E402
33 | F841
34 | E741
35 | F821
36 | E722
37 | F401
38 | W504
39 | E127
40 | W504
41 | E231
42 | E501
43 | F403
44 | E302
45 | F541
46 |
47 |
48 | [isort]
49 | # https://pycqa.github.io/isort/docs/configuration/options.html
50 | line_length = 120
51 | multi_line_output = 0
52 |
--------------------------------------------------------------------------------
/train.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cv516Buaa/tph-yolov5/052dfeb375e51756f17e9ca4f96b7e3e3a7cf3c4/train.png
--------------------------------------------------------------------------------
/utils/__init__.py:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/utils/activations.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Activation functions
4 | """
5 |
6 | import torch
7 | import torch.nn as nn
8 | import torch.nn.functional as F
9 |
10 |
11 | # SiLU https://arxiv.org/pdf/1606.08415.pdf ----------------------------------------------------------------------------
12 | class SiLU(nn.Module): # export-friendly version of nn.SiLU()
13 | @staticmethod
14 | def forward(x):
15 | return x * torch.sigmoid(x)
16 |
17 |
18 | class Hardswish(nn.Module): # export-friendly version of nn.Hardswish()
19 | @staticmethod
20 | def forward(x):
21 | # return x * F.hardsigmoid(x) # for torchscript and CoreML
22 | return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for torchscript, CoreML and ONNX
23 |
24 |
25 | # Mish https://github.com/digantamisra98/Mish --------------------------------------------------------------------------
26 | class Mish(nn.Module):
27 | @staticmethod
28 | def forward(x):
29 | return x * F.softplus(x).tanh()
30 |
31 |
32 | class MemoryEfficientMish(nn.Module):
33 | class F(torch.autograd.Function):
34 | @staticmethod
35 | def forward(ctx, x):
36 | ctx.save_for_backward(x)
37 | return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x)))
38 |
39 | @staticmethod
40 | def backward(ctx, grad_output):
41 | x = ctx.saved_tensors[0]
42 | sx = torch.sigmoid(x)
43 | fx = F.softplus(x).tanh()
44 | return grad_output * (fx + x * sx * (1 - fx * fx))
45 |
46 | def forward(self, x):
47 | return self.F.apply(x)
48 |
49 |
50 | # FReLU https://arxiv.org/abs/2007.11824 -------------------------------------------------------------------------------
51 | class FReLU(nn.Module):
52 | def __init__(self, c1, k=3): # ch_in, kernel
53 | super().__init__()
54 | self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False)
55 | self.bn = nn.BatchNorm2d(c1)
56 |
57 | def forward(self, x):
58 | return torch.max(x, self.bn(self.conv(x)))
59 |
60 |
61 | # ACON https://arxiv.org/pdf/2009.04759.pdf ----------------------------------------------------------------------------
62 | class AconC(nn.Module):
63 | r""" ACON activation (activate or not).
64 | AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter
65 | according to "Activate or Not: Learning Customized Activation" .
66 | """
67 |
68 | def __init__(self, c1):
69 | super().__init__()
70 | self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
71 | self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
72 | self.beta = nn.Parameter(torch.ones(1, c1, 1, 1))
73 |
74 | def forward(self, x):
75 | dpx = (self.p1 - self.p2) * x
76 | return dpx * torch.sigmoid(self.beta * dpx) + self.p2 * x
77 |
78 |
79 | class MetaAconC(nn.Module):
80 | r""" ACON activation (activate or not).
81 | MetaAconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is generated by a small network
82 | according to "Activate or Not: Learning Customized Activation" .
83 | """
84 |
85 | def __init__(self, c1, k=1, s=1, r=16): # ch_in, kernel, stride, r
86 | super().__init__()
87 | c2 = max(r, c1 // r)
88 | self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
89 | self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
90 | self.fc1 = nn.Conv2d(c1, c2, k, s, bias=True)
91 | self.fc2 = nn.Conv2d(c2, c1, k, s, bias=True)
92 | # self.bn1 = nn.BatchNorm2d(c2)
93 | # self.bn2 = nn.BatchNorm2d(c1)
94 |
95 | def forward(self, x):
96 | y = x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True)
97 | # batch-size 1 bug/instabilities https://github.com/ultralytics/yolov5/issues/2891
98 | # beta = torch.sigmoid(self.bn2(self.fc2(self.bn1(self.fc1(y))))) # bug/unstable
99 | beta = torch.sigmoid(self.fc2(self.fc1(y))) # bug patch BN layers removed
100 | dpx = (self.p1 - self.p2) * x
101 | return dpx * torch.sigmoid(beta * dpx) + self.p2 * x
102 |
--------------------------------------------------------------------------------
/utils/augmentations.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Image augmentation functions
4 | """
5 |
6 | import logging
7 | import math
8 | import random
9 |
10 | import cv2
11 | import numpy as np
12 |
13 | from utils.general import check_version, colorstr, resample_segments, segment2box
14 | from utils.metrics import bbox_ioa
15 |
16 |
17 | class Albumentations:
18 | # YOLOv5 Albumentations class (optional, only used if package is installed)
19 | def __init__(self):
20 | self.transform = None
21 | try:
22 | import albumentations as A
23 | check_version(A.__version__, '1.0.3', hard=True) # version requirement
24 |
25 | self.transform = A.Compose([
26 | A.Blur(p=0.01),
27 | A.MedianBlur(p=0.3),
28 | A.ToGray(p=0.01),
29 | A.CLAHE(p=0.3),
30 | A.RandomBrightnessContrast(p=0.3),
31 | A.RandomGamma(p=0.0),
32 | A.ImageCompression(quality_lower=75, p=0.0)],
33 | bbox_params=A.BboxParams(format='yolo', label_fields=['class_labels']))
34 |
35 | logging.info(colorstr('albumentations: ') + ', '.join(f'{x}' for x in self.transform.transforms if x.p))
36 | except ImportError: # package not installed, skip
37 | pass
38 | except Exception as e:
39 | logging.info(colorstr('albumentations: ') + f'{e}')
40 |
41 | def __call__(self, im, labels, p=1.0):
42 | if self.transform and random.random() < p:
43 | new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0]) # transformed
44 | im, labels = new['image'], np.array([[c, *b] for c, b in zip(new['class_labels'], new['bboxes'])])
45 | return im, labels
46 |
47 |
48 | def augment_hsv(im, hgain=0.5, sgain=0.5, vgain=0.5):
49 | # HSV color-space augmentation
50 | if hgain or sgain or vgain:
51 | r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
52 | hue, sat, val = cv2.split(cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
53 | dtype = im.dtype # uint8
54 |
55 | x = np.arange(0, 256, dtype=r.dtype)
56 | lut_hue = ((x * r[0]) % 180).astype(dtype)
57 | lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
58 | lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
59 |
60 | im_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val)))
61 | cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR, dst=im) # no return needed
62 |
63 |
64 | def hist_equalize(im, clahe=True, bgr=False):
65 | # Equalize histogram on BGR image 'im' with im.shape(n,m,3) and range 0-255
66 | yuv = cv2.cvtColor(im, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV)
67 | if clahe:
68 | c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
69 | yuv[:, :, 0] = c.apply(yuv[:, :, 0])
70 | else:
71 | yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0]) # equalize Y channel histogram
72 | return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB) # convert YUV image to RGB
73 |
74 |
75 | def replicate(im, labels):
76 | # Replicate labels
77 | h, w = im.shape[:2]
78 | boxes = labels[:, 1:].astype(int)
79 | x1, y1, x2, y2 = boxes.T
80 | s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels)
81 | for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices
82 | x1b, y1b, x2b, y2b = boxes[i]
83 | bh, bw = y2b - y1b, x2b - x1b
84 | yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y
85 | x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh]
86 | im[y1a:y2a, x1a:x2a] = im[y1b:y2b, x1b:x2b] # im4[ymin:ymax, xmin:xmax]
87 | labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0)
88 |
89 | return im, labels
90 |
91 |
92 | def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
93 | # Resize and pad image while meeting stride-multiple constraints
94 | shape = im.shape[:2] # current shape [height, width]
95 | if isinstance(new_shape, int):
96 | new_shape = (new_shape, new_shape)
97 |
98 | # Scale ratio (new / old)
99 | r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
100 | if not scaleup: # only scale down, do not scale up (for better val mAP)
101 | r = min(r, 1.0)
102 |
103 | # Compute padding
104 | ratio = r, r # width, height ratios
105 | new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
106 | dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
107 | if auto: # minimum rectangle
108 | dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding
109 | elif scaleFill: # stretch
110 | dw, dh = 0.0, 0.0
111 | new_unpad = (new_shape[1], new_shape[0])
112 | ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
113 |
114 | dw /= 2 # divide padding into 2 sides
115 | dh /= 2
116 |
117 | if shape[::-1] != new_unpad: # resize
118 | im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
119 | top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
120 | left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
121 | im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
122 | return im, ratio, (dw, dh)
123 |
124 |
125 | def random_perspective(im, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0,
126 | border=(0, 0)):
127 | # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(0.1, 0.1), scale=(0.9, 1.1), shear=(-10, 10))
128 | # targets = [cls, xyxy]
129 |
130 | height = im.shape[0] + border[0] * 2 # shape(h,w,c)
131 | width = im.shape[1] + border[1] * 2
132 |
133 | # Center
134 | C = np.eye(3)
135 | C[0, 2] = -im.shape[1] / 2 # x translation (pixels)
136 | C[1, 2] = -im.shape[0] / 2 # y translation (pixels)
137 |
138 | # Perspective
139 | P = np.eye(3)
140 | P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y)
141 | P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x)
142 |
143 | # Rotation and Scale
144 | R = np.eye(3)
145 | a = random.uniform(-degrees, degrees)
146 | # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations
147 | s = random.uniform(1 - scale, 1 + scale)
148 | # s = 2 ** random.uniform(-scale, scale)
149 | R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s)
150 |
151 | # Shear
152 | S = np.eye(3)
153 | S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg)
154 | S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg)
155 |
156 | # Translation
157 | T = np.eye(3)
158 | T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels)
159 | T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels)
160 |
161 | # Combined rotation matrix
162 | M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT
163 | if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed
164 | if perspective:
165 | im = cv2.warpPerspective(im, M, dsize=(width, height), borderValue=(114, 114, 114))
166 | else: # affine
167 | im = cv2.warpAffine(im, M[:2], dsize=(width, height), borderValue=(114, 114, 114))
168 |
169 | # Visualize
170 | # import matplotlib.pyplot as plt
171 | # ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel()
172 | # ax[0].imshow(im[:, :, ::-1]) # base
173 | # ax[1].imshow(im2[:, :, ::-1]) # warped
174 |
175 | # Transform label coordinates
176 | n = len(targets)
177 | if n:
178 | use_segments = any(x.any() for x in segments)
179 | new = np.zeros((n, 4))
180 | if use_segments: # warp segments
181 | segments = resample_segments(segments) # upsample
182 | for i, segment in enumerate(segments):
183 | xy = np.ones((len(segment), 3))
184 | xy[:, :2] = segment
185 | xy = xy @ M.T # transform
186 | xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine
187 |
188 | # clip
189 | new[i] = segment2box(xy, width, height)
190 |
191 | else: # warp boxes
192 | xy = np.ones((n * 4, 3))
193 | xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1
194 | xy = xy @ M.T # transform
195 | xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]).reshape(n, 8) # perspective rescale or affine
196 |
197 | # create new boxes
198 | x = xy[:, [0, 2, 4, 6]]
199 | y = xy[:, [1, 3, 5, 7]]
200 | new = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T
201 |
202 | # clip
203 | new[:, [0, 2]] = new[:, [0, 2]].clip(0, width)
204 | new[:, [1, 3]] = new[:, [1, 3]].clip(0, height)
205 |
206 | # filter candidates
207 | i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01 if use_segments else 0.10)
208 | targets = targets[i]
209 | targets[:, 1:5] = new[i]
210 |
211 | return im, targets
212 |
213 |
214 | def copy_paste(im, labels, segments, p=0.5):
215 | # Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
216 | n = len(segments)
217 | if p and n:
218 | h, w, c = im.shape # height, width, channels
219 | im_new = np.zeros(im.shape, np.uint8)
220 | for j in random.sample(range(n), k=round(p * n)):
221 | l, s = labels[j], segments[j]
222 | box = w - l[3], l[2], w - l[1], l[4]
223 | ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
224 | if (ioa < 0.30).all(): # allow 30% obscuration of existing labels
225 | labels = np.concatenate((labels, [[l[0], *box]]), 0)
226 | segments.append(np.concatenate((w - s[:, 0:1], s[:, 1:2]), 1))
227 | cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED)
228 |
229 | result = cv2.bitwise_and(src1=im, src2=im_new)
230 | result = cv2.flip(result, 1) # augment segments (flip left-right)
231 | i = result > 0 # pixels to replace
232 | # i[:, :] = result.max(2).reshape(h, w, 1) # act over ch
233 | im[i] = result[i] # cv2.imwrite('debug.jpg', im) # debug
234 |
235 | return im, labels, segments
236 |
237 |
238 | def cutout(im, labels, p=0.5):
239 | # Applies image cutout augmentation https://arxiv.org/abs/1708.04552
240 | if random.random() < p:
241 | h, w = im.shape[:2]
242 | scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction
243 | for s in scales:
244 | mask_h = random.randint(1, int(h * s)) # create random masks
245 | mask_w = random.randint(1, int(w * s))
246 |
247 | # box
248 | xmin = max(0, random.randint(0, w) - mask_w // 2)
249 | ymin = max(0, random.randint(0, h) - mask_h // 2)
250 | xmax = min(w, xmin + mask_w)
251 | ymax = min(h, ymin + mask_h)
252 |
253 | # apply random color mask
254 | im[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)]
255 |
256 | # return unobscured labels
257 | if len(labels) and s > 0.03:
258 | box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32)
259 | ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
260 | labels = labels[ioa < 0.60] # remove >60% obscured labels
261 |
262 | return labels
263 |
264 |
265 | def mixup(im, labels, im2, labels2):
266 | # Applies MixUp augmentation https://arxiv.org/pdf/1710.09412.pdf
267 | r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0
268 | im = (im * r + im2 * (1 - r)).astype(np.uint8)
269 | labels = np.concatenate((labels, labels2), 0)
270 | return im, labels
271 |
272 |
273 | def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, eps=1e-16): # box1(4,n), box2(4,n)
274 | # Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
275 | w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
276 | w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
277 | ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio
278 | return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates
279 |
--------------------------------------------------------------------------------
/utils/autoanchor.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Auto-anchor utils
4 | """
5 |
6 | import random
7 |
8 | import numpy as np
9 | import torch
10 | import yaml
11 | from tqdm import tqdm
12 |
13 | from utils.general import colorstr
14 |
15 |
16 | def check_anchor_order(m):
17 | # Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary
18 | a = m.anchors.prod(-1).view(-1) # anchor area
19 | da = a[-1] - a[0] # delta a
20 | ds = m.stride[-1] - m.stride[0] # delta s
21 | if da.sign() != ds.sign(): # same order
22 | print('Reversing anchor order')
23 | m.anchors[:] = m.anchors.flip(0)
24 |
25 |
26 | def check_anchors(dataset, model, thr=4.0, imgsz=640):
27 | # Check anchor fit to data, recompute if necessary
28 | prefix = colorstr('autoanchor: ')
29 | print(f'\n{prefix}Analyzing anchors... ', end='')
30 | m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1] # Detect()
31 | shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True)
32 | scale = np.random.uniform(0.9, 1.1, size=(shapes.shape[0], 1)) # augment scale
33 | wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes * scale, dataset.labels)])).float() # wh
34 |
35 | def metric(k): # compute metric
36 | r = wh[:, None] / k[None]
37 | x = torch.min(r, 1 / r).min(2)[0] # ratio metric
38 | best = x.max(1)[0] # best_x
39 | aat = (x > 1 / thr).float().sum(1).mean() # anchors above threshold
40 | bpr = (best > 1 / thr).float().mean() # best possible recall
41 | return bpr, aat
42 |
43 | anchors = m.anchors.clone() * m.stride.to(m.anchors.device).view(-1, 1, 1) # current anchors
44 | bpr, aat = metric(anchors.cpu().view(-1, 2))
45 | print(f'anchors/target = {aat:.2f}, Best Possible Recall (BPR) = {bpr:.4f}', end='')
46 | if bpr < 0.98: # threshold to recompute
47 | print('. Attempting to improve anchors, please wait...')
48 | na = m.anchors.numel() // 2 # number of anchors
49 | try:
50 | anchors = kmean_anchors(dataset, n=na, img_size=imgsz, thr=thr, gen=1000, verbose=False)
51 | except Exception as e:
52 | print(f'{prefix}ERROR: {e}')
53 | new_bpr = metric(anchors)[0]
54 | if new_bpr > bpr: # replace anchors
55 | anchors = torch.tensor(anchors, device=m.anchors.device).type_as(m.anchors)
56 | m.anchors[:] = anchors.clone().view_as(m.anchors) / m.stride.to(m.anchors.device).view(-1, 1, 1) # loss
57 | check_anchor_order(m)
58 | print(f'{prefix}New anchors saved to model. Update model *.yaml to use these anchors in the future.')
59 | else:
60 | print(f'{prefix}Original anchors better than new anchors. Proceeding with original anchors.')
61 | print('') # newline
62 |
63 |
64 | def kmean_anchors(dataset='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=1000, verbose=True):
65 | """ Creates kmeans-evolved anchors from training dataset
66 |
67 | Arguments:
68 | dataset: path to data.yaml, or a loaded dataset
69 | n: number of anchors
70 | img_size: image size used for training
71 | thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0
72 | gen: generations to evolve anchors using genetic algorithm
73 | verbose: print all results
74 |
75 | Return:
76 | k: kmeans evolved anchors
77 |
78 | Usage:
79 | from utils.autoanchor import *; _ = kmean_anchors()
80 | """
81 | from scipy.cluster.vq import kmeans
82 |
83 | thr = 1 / thr
84 | prefix = colorstr('autoanchor: ')
85 |
86 | def metric(k, wh): # compute metrics
87 | r = wh[:, None] / k[None]
88 | x = torch.min(r, 1 / r).min(2)[0] # ratio metric
89 | # x = wh_iou(wh, torch.tensor(k)) # iou metric
90 | return x, x.max(1)[0] # x, best_x
91 |
92 | def anchor_fitness(k): # mutation fitness
93 | _, best = metric(torch.tensor(k, dtype=torch.float32), wh)
94 | return (best * (best > thr).float()).mean() # fitness
95 |
96 | def print_results(k):
97 | k = k[np.argsort(k.prod(1))] # sort small to large
98 | x, best = metric(k, wh0)
99 | bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr
100 | print(f'{prefix}thr={thr:.2f}: {bpr:.4f} best possible recall, {aat:.2f} anchors past thr')
101 | print(f'{prefix}n={n}, img_size={img_size}, metric_all={x.mean():.3f}/{best.mean():.3f}-mean/best, '
102 | f'past_thr={x[x > thr].mean():.3f}-mean: ', end='')
103 | for i, x in enumerate(k):
104 | print('%i,%i' % (round(x[0]), round(x[1])), end=', ' if i < len(k) - 1 else '\n') # use in *.cfg
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.datasets 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 | print(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)] # filter > 2 pixels
122 | # wh = wh * (np.random.rand(wh.shape[0], 1) * 0.9 + 0.1) # multiply by random scale 0-1
123 |
124 | # Kmeans calculation
125 | print(f'{prefix}Running kmeans for {n} anchors on {len(wh)} points...')
126 | s = wh.std(0) # sigmas for whitening
127 | k, dist = kmeans(wh / s, n, iter=30) # points, mean distance
128 | assert len(k) == n, f'{prefix}ERROR: scipy.cluster.vq.kmeans requested {n} points but returned only {len(k)}'
129 | k *= s
130 | wh = torch.tensor(wh, dtype=torch.float32) # filtered
131 | wh0 = torch.tensor(wh0, dtype=torch.float32) # unfiltered
132 | k = print_results(k)
133 |
134 | # Plot
135 | # k, d = [None] * 20, [None] * 20
136 | # for i in tqdm(range(1, 21)):
137 | # k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance
138 | # fig, ax = plt.subplots(1, 2, figsize=(14, 7), tight_layout=True)
139 | # ax = ax.ravel()
140 | # ax[0].plot(np.arange(1, 21), np.array(d) ** 2, marker='.')
141 | # fig, ax = plt.subplots(1, 2, figsize=(14, 7)) # plot wh
142 | # ax[0].hist(wh[wh[:, 0]<100, 0],400)
143 | # ax[1].hist(wh[wh[:, 1]<100, 1],400)
144 | # fig.savefig('wh.png', dpi=200)
145 |
146 | # Evolve
147 | npr = np.random
148 | f, sh, mp, s = anchor_fitness(k), k.shape, 0.9, 0.1 # fitness, generations, mutation prob, sigma
149 | pbar = tqdm(range(gen), desc=f'{prefix}Evolving anchors with Genetic Algorithm:') # progress bar
150 | for _ in pbar:
151 | v = np.ones(sh)
152 | while (v == 1).all(): # mutate until a change occurs (prevent duplicates)
153 | v = ((npr.random(sh) < mp) * random.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0)
154 | kg = (k.copy() * v).clip(min=2.0)
155 | fg = anchor_fitness(kg)
156 | if fg > f:
157 | f, k = fg, kg.copy()
158 | pbar.desc = f'{prefix}Evolving anchors with Genetic Algorithm: fitness = {f:.4f}'
159 | if verbose:
160 | print_results(k)
161 |
162 | return print_results(k)
163 |
--------------------------------------------------------------------------------
/utils/autobatch.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Auto-batch utils
4 | """
5 |
6 | from copy import deepcopy
7 |
8 | import numpy as np
9 | import torch
10 | from torch.cuda import amp
11 |
12 | from utils.general import colorstr
13 | from utils.torch_utils import profile
14 |
15 |
16 | def check_train_batch_size(model, imgsz=640):
17 | # Check YOLOv5 training batch size
18 | with amp.autocast():
19 | return autobatch(deepcopy(model).train(), imgsz) # compute optimal batch size
20 |
21 |
22 | def autobatch(model, imgsz=640, fraction=0.9, batch_size=16):
23 | # Automatically estimate best batch size to use `fraction` of available CUDA memory
24 | # Usage:
25 | # import torch
26 | # from utils.autobatch import autobatch
27 | # model = torch.hub.load('ultralytics/yolov5', 'yolov5s', autoshape=False)
28 | # print(autobatch(model))
29 |
30 | prefix = colorstr('autobatch: ')
31 | print(f'{prefix}Computing optimal batch size for --imgsz {imgsz}')
32 | device = next(model.parameters()).device # get model device
33 | if device.type == 'cpu':
34 | print(f'{prefix}CUDA not detected, using default CPU batch-size {batch_size}')
35 | return batch_size
36 |
37 | d = str(device).upper() # 'CUDA:0'
38 | t = torch.cuda.get_device_properties(device).total_memory / 1024 ** 3 # (GB)
39 | r = torch.cuda.memory_reserved(device) / 1024 ** 3 # (GB)
40 | a = torch.cuda.memory_allocated(device) / 1024 ** 3 # (GB)
41 | f = t - (r + a) # free inside reserved
42 | print(f'{prefix}{d} {t:.3g}G total, {r:.3g}G reserved, {a:.3g}G allocated, {f:.3g}G free')
43 |
44 | batch_sizes = [1, 2, 4, 8, 16]
45 | try:
46 | img = [torch.zeros(b, 3, imgsz, imgsz) for b in batch_sizes]
47 | y = profile(img, model, n=3, device=device)
48 | except Exception as e:
49 | print(f'{prefix}{e}')
50 |
51 | y = [x[2] for x in y if x] # memory [2]
52 | batch_sizes = batch_sizes[:len(y)]
53 | p = np.polyfit(batch_sizes, y, deg=1) # first degree polynomial fit
54 | b = int((f * fraction - p[1]) / p[0]) # y intercept (optimal batch size)
55 | print(f'{prefix}Using colorstr(batch-size {b}) for {d} {t * fraction:.3g}G/{t:.3g}G ({fraction * 100:.0f}%)')
56 | return b
57 |
--------------------------------------------------------------------------------
/utils/aws/__init__.py:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/utils/aws/mime.sh:
--------------------------------------------------------------------------------
1 | # AWS EC2 instance startup 'MIME' script https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/
2 | # This script will run on every instance restart, not only on first start
3 | # --- DO NOT COPY ABOVE COMMENTS WHEN PASTING INTO USERDATA ---
4 |
5 | Content-Type: multipart/mixed; boundary="//"
6 | MIME-Version: 1.0
7 |
8 | --//
9 | Content-Type: text/cloud-config; charset="us-ascii"
10 | MIME-Version: 1.0
11 | Content-Transfer-Encoding: 7bit
12 | Content-Disposition: attachment; filename="cloud-config.txt"
13 |
14 | #cloud-config
15 | cloud_final_modules:
16 | - [scripts-user, always]
17 |
18 | --//
19 | Content-Type: text/x-shellscript; charset="us-ascii"
20 | MIME-Version: 1.0
21 | Content-Transfer-Encoding: 7bit
22 | Content-Disposition: attachment; filename="userdata.txt"
23 |
24 | #!/bin/bash
25 | # --- paste contents of userdata.sh here ---
26 | --//
27 |
--------------------------------------------------------------------------------
/utils/aws/resume.py:
--------------------------------------------------------------------------------
1 | # Resume all interrupted trainings in yolov5/ dir including DDP trainings
2 | # Usage: $ python utils/aws/resume.py
3 |
4 | import os
5 | import sys
6 | from pathlib import Path
7 |
8 | import torch
9 | import yaml
10 |
11 | FILE = Path(__file__).resolve()
12 | ROOT = FILE.parents[2] # YOLOv5 root directory
13 | if str(ROOT) not in sys.path:
14 | sys.path.append(str(ROOT)) # add ROOT to PATH
15 |
16 | port = 0 # --master_port
17 | path = Path('').resolve()
18 | for last in path.rglob('*/**/last.pt'):
19 | ckpt = torch.load(last)
20 | if ckpt['optimizer'] is None:
21 | continue
22 |
23 | # Load opt.yaml
24 | with open(last.parent.parent / 'opt.yaml', errors='ignore') as f:
25 | opt = yaml.safe_load(f)
26 |
27 | # Get device count
28 | d = opt['device'].split(',') # devices
29 | nd = len(d) # number of devices
30 | ddp = nd > 1 or (nd == 0 and torch.cuda.device_count() > 1) # distributed data parallel
31 |
32 | if ddp: # multi-GPU
33 | port += 1
34 | cmd = f'python -m torch.distributed.run --nproc_per_node {nd} --master_port {port} train.py --resume {last}'
35 | else: # single-GPU
36 | cmd = f'python train.py --resume {last}'
37 |
38 | cmd += ' > /dev/null 2>&1 &' # redirect output to dev/null and run in daemon thread
39 | print(cmd)
40 | os.system(cmd)
41 |
--------------------------------------------------------------------------------
/utils/aws/userdata.sh:
--------------------------------------------------------------------------------
1 | #!/bin/bash
2 | # AWS EC2 instance startup script https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
3 | # This script will run only once on first instance start (for a re-start script see mime.sh)
4 | # /home/ubuntu (ubuntu) or /home/ec2-user (amazon-linux) is working dir
5 | # Use >300 GB SSD
6 |
7 | cd home/ubuntu
8 | if [ ! -d yolov5 ]; then
9 | echo "Running first-time script." # install dependencies, download COCO, pull Docker
10 | git clone https://github.com/ultralytics/yolov5 -b master && sudo chmod -R 777 yolov5
11 | cd yolov5
12 | bash data/scripts/get_coco.sh && echo "COCO done." &
13 | sudo docker pull ultralytics/yolov5:latest && echo "Docker done." &
14 | python -m pip install --upgrade pip && pip install -r requirements.txt && python detect.py && echo "Requirements done." &
15 | wait && echo "All tasks done." # finish background tasks
16 | else
17 | echo "Running re-start script." # resume interrupted runs
18 | i=0
19 | list=$(sudo docker ps -qa) # container list i.e. $'one\ntwo\nthree\nfour'
20 | while IFS= read -r id; do
21 | ((i++))
22 | echo "restarting container $i: $id"
23 | sudo docker start $id
24 | # sudo docker exec -it $id python train.py --resume # single-GPU
25 | sudo docker exec -d $id python utils/aws/resume.py # multi-scenario
26 | done <<<"$list"
27 | fi
28 |
--------------------------------------------------------------------------------
/utils/callbacks.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Callback utils
4 | """
5 |
6 |
7 | class Callbacks:
8 | """"
9 | Handles all registered callbacks for YOLOv5 Hooks
10 | """
11 |
12 | # Define the available callbacks
13 | _callbacks = {
14 | 'on_pretrain_routine_start': [],
15 | 'on_pretrain_routine_end': [],
16 |
17 | 'on_train_start': [],
18 | 'on_train_epoch_start': [],
19 | 'on_train_batch_start': [],
20 | 'optimizer_step': [],
21 | 'on_before_zero_grad': [],
22 | 'on_train_batch_end': [],
23 | 'on_train_epoch_end': [],
24 |
25 | 'on_val_start': [],
26 | 'on_val_batch_start': [],
27 | 'on_val_image_end': [],
28 | 'on_val_batch_end': [],
29 | 'on_val_end': [],
30 |
31 | 'on_fit_epoch_end': [], # fit = train + val
32 | 'on_model_save': [],
33 | 'on_train_end': [],
34 |
35 | 'teardown': [],
36 | }
37 |
38 | def register_action(self, hook, name='', callback=None):
39 | """
40 | Register a new action to a callback hook
41 |
42 | Args:
43 | hook The callback hook name to register the action to
44 | name The name of the action for later reference
45 | callback The callback to fire
46 | """
47 | assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}"
48 | assert callable(callback), f"callback '{callback}' is not callable"
49 | self._callbacks[hook].append({'name': name, 'callback': callback})
50 |
51 | def get_registered_actions(self, hook=None):
52 | """"
53 | Returns all the registered actions by callback hook
54 |
55 | Args:
56 | hook The name of the hook to check, defaults to all
57 | """
58 | if hook:
59 | return self._callbacks[hook]
60 | else:
61 | return self._callbacks
62 |
63 | def run(self, hook, *args, **kwargs):
64 | """
65 | Loop through the registered actions and fire all callbacks
66 |
67 | Args:
68 | hook The name of the hook to check, defaults to all
69 | args Arguments to receive from YOLOv5
70 | kwargs Keyword Arguments to receive from YOLOv5
71 | """
72 |
73 | assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}"
74 |
75 | for logger in self._callbacks[hook]:
76 | logger['callback'](*args, **kwargs)
77 |
--------------------------------------------------------------------------------
/utils/downloads.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Download utils
4 | """
5 |
6 | import os
7 | import platform
8 | import subprocess
9 | import time
10 | import urllib
11 | from pathlib import Path
12 | from zipfile import ZipFile
13 |
14 | import requests
15 | import torch
16 |
17 |
18 | def gsutil_getsize(url=''):
19 | # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du
20 | s = subprocess.check_output(f'gsutil du {url}', shell=True).decode('utf-8')
21 | return eval(s.split(' ')[0]) if len(s) else 0 # bytes
22 |
23 |
24 | def safe_download(file, url, url2=None, min_bytes=1E0, error_msg=''):
25 | # Attempts to download file from url or url2, checks and removes incomplete downloads < min_bytes
26 | file = Path(file)
27 | assert_msg = f"Downloaded file '{file}' does not exist or size is < min_bytes={min_bytes}"
28 | try: # url1
29 | print(f'Downloading {url} to {file}...')
30 | torch.hub.download_url_to_file(url, str(file))
31 | assert file.exists() and file.stat().st_size > min_bytes, assert_msg # check
32 | except Exception as e: # url2
33 | file.unlink(missing_ok=True) # remove partial downloads
34 | print(f'ERROR: {e}\nRe-attempting {url2 or url} to {file}...')
35 | os.system(f"curl -L '{url2 or url}' -o '{file}' --retry 3 -C -") # curl download, retry and resume on fail
36 | finally:
37 | if not file.exists() or file.stat().st_size < min_bytes: # check
38 | file.unlink(missing_ok=True) # remove partial downloads
39 | print(f"ERROR: {assert_msg}\n{error_msg}")
40 | print('')
41 |
42 |
43 | def attempt_download(file, repo='ultralytics/yolov5'): # from utils.downloads import *; attempt_download()
44 | # Attempt file download if does not exist
45 | file = Path(str(file).strip().replace("'", ''))
46 |
47 | if not file.exists():
48 | # URL specified
49 | name = Path(urllib.parse.unquote(str(file))).name # decode '%2F' to '/' etc.
50 | if str(file).startswith(('http:/', 'https:/')): # download
51 | url = str(file).replace(':/', '://') # Pathlib turns :// -> :/
52 | name = name.split('?')[0] # parse authentication https://url.com/file.txt?auth...
53 | safe_download(file=name, url=url, min_bytes=1E5)
54 | return name
55 |
56 | # GitHub assets
57 | file.parent.mkdir(parents=True, exist_ok=True) # make parent dir (if required)
58 | try:
59 | response = requests.get(f'https://api.github.com/repos/{repo}/releases/latest').json() # github api
60 | assets = [x['name'] for x in response['assets']] # release assets, i.e. ['yolov5s.pt', 'yolov5m.pt', ...]
61 | tag = response['tag_name'] # i.e. 'v1.0'
62 | except: # fallback plan
63 | assets = ['yolov5n.pt', 'yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt',
64 | 'yolov5n6.pt', 'yolov5s6.pt', 'yolov5m6.pt', 'yolov5l6.pt', 'yolov5x6.pt']
65 | try:
66 | tag = subprocess.check_output('git tag', shell=True, stderr=subprocess.STDOUT).decode().split()[-1]
67 | except:
68 | tag = 'v6.0' # current release
69 |
70 | if name in assets:
71 | safe_download(file,
72 | url=f'https://github.com/{repo}/releases/download/{tag}/{name}',
73 | # url2=f'https://storage.googleapis.com/{repo}/ckpt/{name}', # backup url (optional)
74 | min_bytes=1E5,
75 | error_msg=f'{file} missing, try downloading from https://github.com/{repo}/releases/')
76 |
77 | return str(file)
78 |
79 |
80 | def gdrive_download(id='16TiPfZj7htmTyhntwcZyEEAejOUxuT6m', file='tmp.zip'):
81 | # Downloads a file from Google Drive. from yolov5.utils.downloads import *; gdrive_download()
82 | t = time.time()
83 | file = Path(file)
84 | cookie = Path('cookie') # gdrive cookie
85 | print(f'Downloading https://drive.google.com/uc?export=download&id={id} as {file}... ', end='')
86 | file.unlink(missing_ok=True) # remove existing file
87 | cookie.unlink(missing_ok=True) # remove existing cookie
88 |
89 | # Attempt file download
90 | out = "NUL" if platform.system() == "Windows" else "/dev/null"
91 | os.system(f'curl -c ./cookie -s -L "drive.google.com/uc?export=download&id={id}" > {out}')
92 | if os.path.exists('cookie'): # large file
93 | s = f'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm={get_token()}&id={id}" -o {file}'
94 | else: # small file
95 | s = f'curl -s -L -o {file} "drive.google.com/uc?export=download&id={id}"'
96 | r = os.system(s) # execute, capture return
97 | cookie.unlink(missing_ok=True) # remove existing cookie
98 |
99 | # Error check
100 | if r != 0:
101 | file.unlink(missing_ok=True) # remove partial
102 | print('Download error ') # raise Exception('Download error')
103 | return r
104 |
105 | # Unzip if archive
106 | if file.suffix == '.zip':
107 | print('unzipping... ', end='')
108 | ZipFile(file).extractall(path=file.parent) # unzip
109 | file.unlink() # remove zip
110 |
111 | print(f'Done ({time.time() - t:.1f}s)')
112 | return r
113 |
114 |
115 | def get_token(cookie="./cookie"):
116 | with open(cookie) as f:
117 | for line in f:
118 | if "download" in line:
119 | return line.split()[-1]
120 | return ""
121 |
122 | # Google utils: https://cloud.google.com/storage/docs/reference/libraries ----------------------------------------------
123 | #
124 | #
125 | # def upload_blob(bucket_name, source_file_name, destination_blob_name):
126 | # # Uploads a file to a bucket
127 | # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python
128 | #
129 | # storage_client = storage.Client()
130 | # bucket = storage_client.get_bucket(bucket_name)
131 | # blob = bucket.blob(destination_blob_name)
132 | #
133 | # blob.upload_from_filename(source_file_name)
134 | #
135 | # print('File {} uploaded to {}.'.format(
136 | # source_file_name,
137 | # destination_blob_name))
138 | #
139 | #
140 | # def download_blob(bucket_name, source_blob_name, destination_file_name):
141 | # # Uploads a blob from a bucket
142 | # storage_client = storage.Client()
143 | # bucket = storage_client.get_bucket(bucket_name)
144 | # blob = bucket.blob(source_blob_name)
145 | #
146 | # blob.download_to_filename(destination_file_name)
147 | #
148 | # print('Blob {} downloaded to {}.'.format(
149 | # source_blob_name,
150 | # destination_file_name))
151 |
--------------------------------------------------------------------------------
/utils/flask_rest_api/README.md:
--------------------------------------------------------------------------------
1 | # Flask REST API
2 |
3 | [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) [API](https://en.wikipedia.org/wiki/API)s are
4 | commonly used to expose Machine Learning (ML) models to other services. This folder contains an example REST API
5 | created using Flask to expose the YOLOv5s model from [PyTorch Hub](https://pytorch.org/hub/ultralytics_yolov5/).
6 |
7 | ## Requirements
8 |
9 | [Flask](https://palletsprojects.com/p/flask/) is required. Install with:
10 |
11 | ```shell
12 | $ pip install Flask
13 | ```
14 |
15 | ## Run
16 |
17 | After Flask installation run:
18 |
19 | ```shell
20 | $ python3 restapi.py --port 5000
21 | ```
22 |
23 | Then use [curl](https://curl.se/) to perform a request:
24 |
25 | ```shell
26 | $ curl -X POST -F image=@zidane.jpg 'http://localhost:5000/v1/object-detection/yolov5s'
27 | ```
28 |
29 | The model inference results are returned as a JSON response:
30 |
31 | ```json
32 | [
33 | {
34 | "class": 0,
35 | "confidence": 0.8900438547,
36 | "height": 0.9318675399,
37 | "name": "person",
38 | "width": 0.3264600933,
39 | "xcenter": 0.7438579798,
40 | "ycenter": 0.5207948685
41 | },
42 | {
43 | "class": 0,
44 | "confidence": 0.8440024257,
45 | "height": 0.7155083418,
46 | "name": "person",
47 | "width": 0.6546785235,
48 | "xcenter": 0.427829951,
49 | "ycenter": 0.6334488392
50 | },
51 | {
52 | "class": 27,
53 | "confidence": 0.3771208823,
54 | "height": 0.3902671337,
55 | "name": "tie",
56 | "width": 0.0696444362,
57 | "xcenter": 0.3675483763,
58 | "ycenter": 0.7991207838
59 | },
60 | {
61 | "class": 27,
62 | "confidence": 0.3527112305,
63 | "height": 0.1540903747,
64 | "name": "tie",
65 | "width": 0.0336618312,
66 | "xcenter": 0.7814827561,
67 | "ycenter": 0.5065554976
68 | }
69 | ]
70 | ```
71 |
72 | An example python script to perform inference using [requests](https://docs.python-requests.org/en/master/) is given
73 | in `example_request.py`
74 |
--------------------------------------------------------------------------------
/utils/flask_rest_api/example_request.py:
--------------------------------------------------------------------------------
1 | """Perform test request"""
2 | import pprint
3 |
4 | import requests
5 |
6 | DETECTION_URL = "http://localhost:5000/v1/object-detection/yolov5s"
7 | TEST_IMAGE = "zidane.jpg"
8 |
9 | image_data = open(TEST_IMAGE, "rb").read()
10 |
11 | response = requests.post(DETECTION_URL, files={"image": image_data}).json()
12 |
13 | pprint.pprint(response)
14 |
--------------------------------------------------------------------------------
/utils/flask_rest_api/restapi.py:
--------------------------------------------------------------------------------
1 | """
2 | Run a rest API exposing the yolov5s object detection model
3 | """
4 | import argparse
5 | import io
6 |
7 | import torch
8 | from flask import Flask, request
9 | from PIL import Image
10 |
11 | app = Flask(__name__)
12 |
13 | DETECTION_URL = "/v1/object-detection/yolov5s"
14 |
15 |
16 | @app.route(DETECTION_URL, methods=["POST"])
17 | def predict():
18 | if not request.method == "POST":
19 | return
20 |
21 | if request.files.get("image"):
22 | image_file = request.files["image"]
23 | image_bytes = image_file.read()
24 |
25 | img = Image.open(io.BytesIO(image_bytes))
26 |
27 | results = model(img, size=640) # reduce size=320 for faster inference
28 | return results.pandas().xyxy[0].to_json(orient="records")
29 |
30 |
31 | if __name__ == "__main__":
32 | parser = argparse.ArgumentParser(description="Flask API exposing YOLOv5 model")
33 | parser.add_argument("--port", default=5000, type=int, help="port number")
34 | args = parser.parse_args()
35 |
36 | model = torch.hub.load("ultralytics/yolov5", "yolov5s", force_reload=True) # force_reload to recache
37 | app.run(host="0.0.0.0", port=args.port) # debug=True causes Restarting with stat
38 |
--------------------------------------------------------------------------------
/utils/google_app_engine/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM gcr.io/google-appengine/python
2 |
3 | # Create a virtualenv for dependencies. This isolates these packages from
4 | # system-level packages.
5 | # Use -p python3 or -p python3.7 to select python version. Default is version 2.
6 | RUN virtualenv /env -p python3
7 |
8 | # Setting these environment variables are the same as running
9 | # source /env/bin/activate.
10 | ENV VIRTUAL_ENV /env
11 | ENV PATH /env/bin:$PATH
12 |
13 | RUN apt-get update && apt-get install -y python-opencv
14 |
15 | # Copy the application's requirements.txt and run pip to install all
16 | # dependencies into the virtualenv.
17 | ADD requirements.txt /app/requirements.txt
18 | RUN pip install -r /app/requirements.txt
19 |
20 | # Add the application source code.
21 | ADD . /app
22 |
23 | # Run a WSGI server to serve the application. gunicorn must be declared as
24 | # a dependency in requirements.txt.
25 | CMD gunicorn -b :$PORT main:app
26 |
--------------------------------------------------------------------------------
/utils/google_app_engine/additional_requirements.txt:
--------------------------------------------------------------------------------
1 | # add these requirements in your app on top of the existing ones
2 | pip==19.2
3 | Flask==1.0.2
4 | gunicorn==19.9.0
5 |
--------------------------------------------------------------------------------
/utils/google_app_engine/app.yaml:
--------------------------------------------------------------------------------
1 | runtime: custom
2 | env: flex
3 |
4 | service: yolov5app
5 |
6 | liveness_check:
7 | initial_delay_sec: 600
8 |
9 | manual_scaling:
10 | instances: 1
11 | resources:
12 | cpu: 1
13 | memory_gb: 4
14 | disk_size_gb: 20
15 |
--------------------------------------------------------------------------------
/utils/loggers/__init__.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Logging utils
4 | """
5 |
6 | import os
7 | import warnings
8 | from threading import Thread
9 |
10 | import pkg_resources as pkg
11 | import torch
12 | from torch.utils.tensorboard import SummaryWriter
13 |
14 | from utils.general import colorstr, emojis
15 | from utils.loggers.wandb.wandb_utils import WandbLogger
16 | from utils.plots import plot_images, plot_results
17 | from utils.torch_utils import de_parallel
18 |
19 | LOGGERS = ('csv', 'tb', 'wandb') # text-file, TensorBoard, Weights & Biases
20 | RANK = int(os.getenv('RANK', -1))
21 |
22 | try:
23 | import wandb
24 |
25 | assert hasattr(wandb, '__version__') # verify package import not local dir
26 | if pkg.parse_version(wandb.__version__) >= pkg.parse_version('0.12.2') and RANK in [0, -1]:
27 | wandb_login_success = wandb.login(timeout=30)
28 | if not wandb_login_success:
29 | wandb = None
30 | except (ImportError, AssertionError):
31 | wandb = None
32 |
33 |
34 | class Loggers():
35 | # YOLOv5 Loggers class
36 | def __init__(self, save_dir=None, weights=None, opt=None, hyp=None, logger=None, include=LOGGERS):
37 | self.save_dir = save_dir
38 | self.weights = weights
39 | self.opt = opt
40 | self.hyp = hyp
41 | self.logger = logger # for printing results to console
42 | self.include = include
43 | self.keys = ['train/box_loss', 'train/obj_loss', 'train/cls_loss', # train loss
44 | 'metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95', # metrics
45 | 'val/box_loss', 'val/obj_loss', 'val/cls_loss', # val loss
46 | 'x/lr0', 'x/lr1', 'x/lr2'] # params
47 | for k in LOGGERS:
48 | setattr(self, k, None) # init empty logger dictionary
49 | self.csv = True # always log to csv
50 |
51 | # Message
52 | if not wandb:
53 | prefix = colorstr('Weights & Biases: ')
54 | s = f"{prefix}run 'pip install wandb' to automatically track and visualize YOLOv5 🚀 runs (RECOMMENDED)"
55 | print(emojis(s))
56 |
57 | # TensorBoard
58 | s = self.save_dir
59 | if 'tb' in self.include and not self.opt.evolve:
60 | prefix = colorstr('TensorBoard: ')
61 | self.logger.info(f"{prefix}Start with 'tensorboard --logdir {s.parent}', view at http://localhost:6006/")
62 | self.tb = SummaryWriter(str(s))
63 |
64 | # W&B
65 | if wandb and 'wandb' in self.include:
66 | wandb_artifact_resume = isinstance(self.opt.resume, str) and self.opt.resume.startswith('wandb-artifact://')
67 | run_id = torch.load(self.weights).get('wandb_id') if self.opt.resume and not wandb_artifact_resume else None
68 | self.opt.hyp = self.hyp # add hyperparameters
69 | self.wandb = WandbLogger(self.opt, run_id)
70 | else:
71 | self.wandb = None
72 |
73 | def on_pretrain_routine_end(self):
74 | # Callback runs on pre-train routine end
75 | paths = self.save_dir.glob('*labels*.jpg') # training labels
76 | if self.wandb:
77 | self.wandb.log({"Labels": [wandb.Image(str(x), caption=x.name) for x in paths]})
78 |
79 | def on_train_batch_end(self, ni, model, imgs, targets, paths, plots, sync_bn):
80 | # Callback runs on train batch end
81 | if plots:
82 | if ni == 0:
83 | if not sync_bn: # tb.add_graph() --sync known issue https://github.com/ultralytics/yolov5/issues/3754
84 | with warnings.catch_warnings():
85 | warnings.simplefilter('ignore') # suppress jit trace warning
86 | self.tb.add_graph(torch.jit.trace(de_parallel(model), imgs[0:1], strict=False), [])
87 | if ni < 3:
88 | f = self.save_dir / f'train_batch{ni}.jpg' # filename
89 | Thread(target=plot_images, args=(imgs, targets, paths, f), daemon=True).start()
90 | if self.wandb and ni == 10:
91 | files = sorted(self.save_dir.glob('train*.jpg'))
92 | self.wandb.log({'Mosaics': [wandb.Image(str(f), caption=f.name) for f in files if f.exists()]})
93 |
94 | def on_train_epoch_end(self, epoch):
95 | # Callback runs on train epoch end
96 | if self.wandb:
97 | self.wandb.current_epoch = epoch + 1
98 |
99 | def on_val_image_end(self, pred, predn, path, names, im):
100 | # Callback runs on val image end
101 | if self.wandb:
102 | self.wandb.val_one_image(pred, predn, path, names, im)
103 |
104 | def on_val_end(self):
105 | # Callback runs on val end
106 | if self.wandb:
107 | files = sorted(self.save_dir.glob('val*.jpg'))
108 | self.wandb.log({"Validation": [wandb.Image(str(f), caption=f.name) for f in files]})
109 |
110 | def on_fit_epoch_end(self, vals, epoch, best_fitness, fi):
111 | # Callback runs at the end of each fit (train+val) epoch
112 | x = {k: v for k, v in zip(self.keys, vals)} # dict
113 | if self.csv:
114 | file = self.save_dir / 'results.csv'
115 | n = len(x) + 1 # number of cols
116 | s = '' if file.exists() else (('%20s,' * n % tuple(['epoch'] + self.keys)).rstrip(',') + '\n') # add header
117 | with open(file, 'a') as f:
118 | f.write(s + ('%20.5g,' * n % tuple([epoch] + vals)).rstrip(',') + '\n')
119 |
120 | if self.tb:
121 | for k, v in x.items():
122 | self.tb.add_scalar(k, v, epoch)
123 |
124 | if self.wandb:
125 | self.wandb.log(x)
126 | self.wandb.end_epoch(best_result=best_fitness == fi)
127 |
128 | def on_model_save(self, last, epoch, final_epoch, best_fitness, fi):
129 | # Callback runs on model save event
130 | if self.wandb:
131 | if ((epoch + 1) % self.opt.save_period == 0 and not final_epoch) and self.opt.save_period != -1:
132 | self.wandb.log_model(last.parent, self.opt, epoch, fi, best_model=best_fitness == fi)
133 |
134 | def on_train_end(self, last, best, plots, epoch, results):
135 | # Callback runs on training end
136 | if plots:
137 | plot_results(file=self.save_dir / 'results.csv') # save results.png
138 | files = ['results.png', 'confusion_matrix.png', *(f'{x}_curve.png' for x in ('F1', 'PR', 'P', 'R'))]
139 | files = [(self.save_dir / f) for f in files if (self.save_dir / f).exists()] # filter
140 |
141 | if self.tb:
142 | import cv2
143 | for f in files:
144 | self.tb.add_image(f.stem, cv2.imread(str(f))[..., ::-1], epoch, dataformats='HWC')
145 |
146 | if self.wandb:
147 | self.wandb.log({"Results": [wandb.Image(str(f), caption=f.name) for f in files]})
148 | # Calling wandb.log. TODO: Refactor this into WandbLogger.log_model
149 | if not self.opt.evolve:
150 | wandb.log_artifact(str(best if best.exists() else last), type='model',
151 | name='run_' + self.wandb.wandb_run.id + '_model',
152 | aliases=['latest', 'best', 'stripped'])
153 | self.wandb.finish_run()
154 | else:
155 | self.wandb.finish_run()
156 | self.wandb = WandbLogger(self.opt)
157 |
--------------------------------------------------------------------------------
/utils/loggers/wandb/README.md:
--------------------------------------------------------------------------------
1 | 📚 This guide explains how to use **Weights & Biases** (W&B) with YOLOv5 🚀. UPDATED 29 September 2021.
2 | * [About Weights & Biases](#about-weights-&-biases)
3 | * [First-Time Setup](#first-time-setup)
4 | * [Viewing runs](#viewing-runs)
5 | * [Advanced Usage: Dataset Versioning and Evaluation](#advanced-usage)
6 | * [Reports: Share your work with the world!](#reports)
7 |
8 | ## About Weights & Biases
9 | Think of [W&B](https://wandb.ai/site?utm_campaign=repo_yolo_wandbtutorial) like GitHub for machine learning models. With a few lines of code, save everything you need to debug, compare and reproduce your models — architecture, hyperparameters, git commits, model weights, GPU usage, and even datasets and predictions.
10 |
11 | Used by top researchers including teams at OpenAI, Lyft, Github, and MILA, W&B is part of the new standard of best practices for machine learning. How W&B can help you optimize your machine learning workflows:
12 |
13 | * [Debug](https://wandb.ai/wandb/getting-started/reports/Visualize-Debug-Machine-Learning-Models--VmlldzoyNzY5MDk#Free-2) model performance in real time
14 | * [GPU usage](https://wandb.ai/wandb/getting-started/reports/Visualize-Debug-Machine-Learning-Models--VmlldzoyNzY5MDk#System-4) visualized automatically
15 | * [Custom charts](https://wandb.ai/wandb/customizable-charts/reports/Powerful-Custom-Charts-To-Debug-Model-Peformance--VmlldzoyNzY4ODI) for powerful, extensible visualization
16 | * [Share insights](https://wandb.ai/wandb/getting-started/reports/Visualize-Debug-Machine-Learning-Models--VmlldzoyNzY5MDk#Share-8) interactively with collaborators
17 | * [Optimize hyperparameters](https://docs.wandb.com/sweeps) efficiently
18 | * [Track](https://docs.wandb.com/artifacts) datasets, pipelines, and production models
19 |
20 | ## First-Time Setup
21 |
22 | Toggle Details
23 | When you first train, W&B will prompt you to create a new account and will generate an **API key** for you. If you are an existing user you can retrieve your key from https://wandb.ai/authorize. This key is used to tell W&B where to log your data. You only need to supply your key once, and then it is remembered on the same device.
24 |
25 | W&B will create a cloud **project** (default is 'YOLOv5') for your training runs, and each new training run will be provided a unique run **name** within that project as project/name. You can also manually set your project and run name as:
26 |
27 | ```shell
28 | $ python train.py --project ... --name ...
29 | ```
30 |
31 | YOLOv5 notebook example:
32 |
33 |
34 |
35 |
36 |
37 | ## Viewing Runs
38 |
39 | Toggle Details
40 | Run information streams from your environment to the W&B cloud console as you train. This allows you to monitor and even cancel runs in realtime . All important information is logged:
41 |
42 | * Training & Validation losses
43 | * Metrics: Precision, Recall, mAP@0.5, mAP@0.5:0.95
44 | * Learning Rate over time
45 | * A bounding box debugging panel, showing the training progress over time
46 | * GPU: Type, **GPU Utilization**, power, temperature, **CUDA memory usage**
47 | * System: Disk I/0, CPU utilization, RAM memory usage
48 | * Your trained model as W&B Artifact
49 | * Environment: OS and Python types, Git repository and state, **training command**
50 |
51 | 
52 |
53 |
54 |
55 |
56 | ## Advanced Usage
57 | You can leverage W&B artifacts and Tables integration to easily visualize and manage your datasets, models and training evaluations. Here are some quick examples to get you started.
58 |
59 | 1. Visualize and Version Datasets
60 | Log, visualize, dynamically query, and understand your data with W&B Tables. You can use the following command to log your dataset as a W&B Table. This will generate a {dataset}_wandb.yaml
file which can be used to train from dataset artifact.
61 |
62 | Usage
63 | Code $ python utils/logger/wandb/log_dataset.py --project ... --name ... --data ..
64 |
65 | 
66 |
67 |
68 | 2: Train and Log Evaluation simultaneousy
69 | This is an extension of the previous section, but it'll also training after uploading the dataset. This also evaluation Table
70 | Evaluation table compares your predictions and ground truths across the validation set for each epoch. It uses the references to the already uploaded datasets,
71 | so no images will be uploaded from your system more than once.
72 |
73 | Usage
74 | Code $ python utils/logger/wandb/log_dataset.py --data .. --upload_data
75 |
76 | 
77 |
78 |
79 | 3: Train using dataset artifact
80 | When you upload a dataset as described in the first section, you get a new config file with an added `_wandb` to its name. This file contains the information that
81 | can be used to train a model directly from the dataset artifact. This also logs evaluation
82 |
83 | Usage
84 | Code $ python utils/logger/wandb/log_dataset.py --data {data}_wandb.yaml
85 |
86 | 
87 |
88 |
89 | 4: Save model checkpoints as artifacts
90 | To enable saving and versioning checkpoints of your experiment, pass `--save_period n` with the base cammand, where `n` represents checkpoint interval.
91 | You can also log both the dataset and model checkpoints simultaneously. If not passed, only the final model will be logged
92 |
93 |
94 | Usage
95 | Code $ python train.py --save_period 1
96 |
97 | 
98 |
99 |
100 |
101 |
102 | 5: Resume runs from checkpoint artifacts.
103 | Any run can be resumed using artifacts if the --resume
argument starts with wandb-artifact://
prefix followed by the run path, i.e, wandb-artifact://username/project/runid
. This doesn't require the model checkpoint to be present on the local system.
104 |
105 |
106 | Usage
107 | Code $ python train.py --resume wandb-artifact://{run_path}
108 |
109 | 
110 |
111 |
112 | 6: Resume runs from dataset artifact & checkpoint artifacts.
113 | Local dataset or model checkpoints are not required. This can be used to resume runs directly on a different device
114 | The syntax is same as the previous section, but you'll need to lof both the dataset and model checkpoints as artifacts, i.e, set bot --upload_dataset
or
115 | train from _wandb.yaml
file and set --save_period
116 |
117 |
118 | Usage
119 | Code $ python train.py --resume wandb-artifact://{run_path}
120 |
121 | 
122 |
123 |
124 |
125 |
126 |
127 | Reports
128 | W&B Reports can be created from your saved runs for sharing online. Once a report is created you will receive a link you can use to publically share your results. Here is an example report created from the COCO128 tutorial trainings of all four YOLOv5 models ([link](https://wandb.ai/glenn-jocher/yolov5_tutorial/reports/YOLOv5-COCO128-Tutorial-Results--VmlldzozMDI5OTY)).
129 |
130 |
131 |
132 |
133 | ## Environments
134 |
135 | YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including [CUDA](https://developer.nvidia.com/cuda)/[CUDNN](https://developer.nvidia.com/cudnn), [Python](https://www.python.org/) and [PyTorch](https://pytorch.org/) preinstalled):
136 |
137 | - **Google Colab and Kaggle** notebooks with free GPU:
138 | - **Google Cloud** Deep Learning VM. See [GCP Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/GCP-Quickstart)
139 | - **Amazon** Deep Learning AMI. See [AWS Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/AWS-Quickstart)
140 | - **Docker Image**. See [Docker Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/Docker-Quickstart)
141 |
142 |
143 | ## Status
144 |
145 | 
146 |
147 | If this badge is green, all [YOLOv5 GitHub Actions](https://github.com/ultralytics/yolov5/actions) Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training ([train.py](https://github.com/ultralytics/yolov5/blob/master/train.py)), validation ([val.py](https://github.com/ultralytics/yolov5/blob/master/val.py)), inference ([detect.py](https://github.com/ultralytics/yolov5/blob/master/detect.py)) and export ([export.py](https://github.com/ultralytics/yolov5/blob/master/export.py)) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.
148 |
--------------------------------------------------------------------------------
/utils/loggers/wandb/__init__.py:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/utils/loggers/wandb/log_dataset.py:
--------------------------------------------------------------------------------
1 | import argparse
2 |
3 | from wandb_utils import WandbLogger
4 |
5 | WANDB_ARTIFACT_PREFIX = 'wandb-artifact://'
6 |
7 |
8 | def create_dataset_artifact(opt):
9 | logger = WandbLogger(opt, None, job_type='Dataset Creation') # TODO: return value unused
10 |
11 |
12 | if __name__ == '__main__':
13 | parser = argparse.ArgumentParser()
14 | parser.add_argument('--data', type=str, default='data/coco128.yaml', help='data.yaml path')
15 | parser.add_argument('--single-cls', action='store_true', help='train as single-class dataset')
16 | parser.add_argument('--project', type=str, default='YOLOv5', help='name of W&B Project')
17 | parser.add_argument('--entity', default=None, help='W&B entity')
18 | parser.add_argument('--name', type=str, default='log dataset', help='name of W&B run')
19 |
20 | opt = parser.parse_args()
21 | opt.resume = False # Explicitly disallow resume check for dataset upload job
22 |
23 | create_dataset_artifact(opt)
24 |
--------------------------------------------------------------------------------
/utils/loggers/wandb/sweep.py:
--------------------------------------------------------------------------------
1 | import sys
2 | from pathlib import Path
3 |
4 | import wandb
5 |
6 | FILE = Path(__file__).resolve()
7 | ROOT = FILE.parents[3] # YOLOv5 root directory
8 | if str(ROOT) not in sys.path:
9 | sys.path.append(str(ROOT)) # add ROOT to PATH
10 |
11 | from train import parse_opt, train
12 | from utils.callbacks import Callbacks
13 | from utils.general import increment_path
14 | from utils.torch_utils import select_device
15 |
16 |
17 | def sweep():
18 | wandb.init()
19 | # Get hyp dict from sweep agent
20 | hyp_dict = vars(wandb.config).get("_items")
21 |
22 | # Workaround: get necessary opt args
23 | opt = parse_opt(known=True)
24 | opt.batch_size = hyp_dict.get("batch_size")
25 | opt.save_dir = str(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok or opt.evolve))
26 | opt.epochs = hyp_dict.get("epochs")
27 | opt.nosave = True
28 | opt.data = hyp_dict.get("data")
29 | opt.weights = str(opt.weights)
30 | opt.cfg = str(opt.cfg)
31 | opt.data = str(opt.data)
32 | opt.hyp = str(opt.hyp)
33 | opt.project = str(opt.project)
34 | device = select_device(opt.device, batch_size=opt.batch_size)
35 |
36 | # train
37 | train(hyp_dict, opt, device, callbacks=Callbacks())
38 |
39 |
40 | if __name__ == "__main__":
41 | sweep()
42 |
--------------------------------------------------------------------------------
/utils/loggers/wandb/sweep.yaml:
--------------------------------------------------------------------------------
1 | # Hyperparameters for training
2 | # To set range-
3 | # Provide min and max values as:
4 | # parameter:
5 | #
6 | # min: scalar
7 | # max: scalar
8 | # OR
9 | #
10 | # Set a specific list of search space-
11 | # parameter:
12 | # values: [scalar1, scalar2, scalar3...]
13 | #
14 | # You can use grid, bayesian and hyperopt search strategy
15 | # For more info on configuring sweeps visit - https://docs.wandb.ai/guides/sweeps/configuration
16 |
17 | program: utils/loggers/wandb/sweep.py
18 | method: random
19 | metric:
20 | name: metrics/mAP_0.5
21 | goal: maximize
22 |
23 | parameters:
24 | # hyperparameters: set either min, max range or values list
25 | data:
26 | value: "data/coco128.yaml"
27 | batch_size:
28 | values: [64]
29 | epochs:
30 | values: [10]
31 |
32 | lr0:
33 | distribution: uniform
34 | min: 1e-5
35 | max: 1e-1
36 | lrf:
37 | distribution: uniform
38 | min: 0.01
39 | max: 1.0
40 | momentum:
41 | distribution: uniform
42 | min: 0.6
43 | max: 0.98
44 | weight_decay:
45 | distribution: uniform
46 | min: 0.0
47 | max: 0.001
48 | warmup_epochs:
49 | distribution: uniform
50 | min: 0.0
51 | max: 5.0
52 | warmup_momentum:
53 | distribution: uniform
54 | min: 0.0
55 | max: 0.95
56 | warmup_bias_lr:
57 | distribution: uniform
58 | min: 0.0
59 | max: 0.2
60 | box:
61 | distribution: uniform
62 | min: 0.02
63 | max: 0.2
64 | cls:
65 | distribution: uniform
66 | min: 0.2
67 | max: 4.0
68 | cls_pw:
69 | distribution: uniform
70 | min: 0.5
71 | max: 2.0
72 | obj:
73 | distribution: uniform
74 | min: 0.2
75 | max: 4.0
76 | obj_pw:
77 | distribution: uniform
78 | min: 0.5
79 | max: 2.0
80 | iou_t:
81 | distribution: uniform
82 | min: 0.1
83 | max: 0.7
84 | anchor_t:
85 | distribution: uniform
86 | min: 2.0
87 | max: 8.0
88 | fl_gamma:
89 | distribution: uniform
90 | min: 0.0
91 | max: 0.1
92 | hsv_h:
93 | distribution: uniform
94 | min: 0.0
95 | max: 0.1
96 | hsv_s:
97 | distribution: uniform
98 | min: 0.0
99 | max: 0.9
100 | hsv_v:
101 | distribution: uniform
102 | min: 0.0
103 | max: 0.9
104 | degrees:
105 | distribution: uniform
106 | min: 0.0
107 | max: 45.0
108 | translate:
109 | distribution: uniform
110 | min: 0.0
111 | max: 0.9
112 | scale:
113 | distribution: uniform
114 | min: 0.0
115 | max: 0.9
116 | shear:
117 | distribution: uniform
118 | min: 0.0
119 | max: 10.0
120 | perspective:
121 | distribution: uniform
122 | min: 0.0
123 | max: 0.001
124 | flipud:
125 | distribution: uniform
126 | min: 0.0
127 | max: 1.0
128 | fliplr:
129 | distribution: uniform
130 | min: 0.0
131 | max: 1.0
132 | mosaic:
133 | distribution: uniform
134 | min: 0.0
135 | max: 1.0
136 | mixup:
137 | distribution: uniform
138 | min: 0.0
139 | max: 1.0
140 | copy_paste:
141 | distribution: uniform
142 | min: 0.0
143 | max: 1.0
144 |
--------------------------------------------------------------------------------
/utils/loss.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Loss functions
4 | """
5 |
6 | import torch
7 | import torch.nn as nn
8 |
9 | from utils.metrics import bbox_iou
10 | from utils.torch_utils import is_parallel
11 |
12 |
13 | def smooth_BCE(eps=0.1): # https://github.com/ultralytics/yolov3/issues/238#issuecomment-598028441
14 | # return positive, negative label smoothing BCE targets
15 | return 1.0 - 0.5 * eps, 0.5 * eps
16 |
17 |
18 | class BCEBlurWithLogitsLoss(nn.Module):
19 | # BCEwithLogitLoss() with reduced missing label effects.
20 | def __init__(self, alpha=0.05):
21 | super().__init__()
22 | self.loss_fcn = nn.BCEWithLogitsLoss(reduction='none') # must be nn.BCEWithLogitsLoss()
23 | self.alpha = alpha
24 |
25 | def forward(self, pred, true):
26 | loss = self.loss_fcn(pred, true)
27 | pred = torch.sigmoid(pred) # prob from logits
28 | dx = pred - true # reduce only missing label effects
29 | # dx = (pred - true).abs() # reduce missing label and false label effects
30 | alpha_factor = 1 - torch.exp((dx - 1) / (self.alpha + 1e-4))
31 | loss *= alpha_factor
32 | return loss.mean()
33 |
34 |
35 | class FocalLoss(nn.Module):
36 | # Wraps focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)
37 | def __init__(self, loss_fcn, gamma=1.5, alpha=0.25):
38 | super().__init__()
39 | self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss()
40 | self.gamma = gamma
41 | self.alpha = alpha
42 | self.reduction = loss_fcn.reduction
43 | self.loss_fcn.reduction = 'none' # required to apply FL to each element
44 |
45 | def forward(self, pred, true):
46 | loss = self.loss_fcn(pred, true)
47 | # p_t = torch.exp(-loss)
48 | # loss *= self.alpha * (1.000001 - p_t) ** self.gamma # non-zero power for gradient stability
49 |
50 | # TF implementation https://github.com/tensorflow/addons/blob/v0.7.1/tensorflow_addons/losses/focal_loss.py
51 | pred_prob = torch.sigmoid(pred) # prob from logits
52 | p_t = true * pred_prob + (1 - true) * (1 - pred_prob)
53 | alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha)
54 | modulating_factor = (1.0 - p_t) ** self.gamma
55 | loss *= alpha_factor * modulating_factor
56 |
57 | if self.reduction == 'mean':
58 | return loss.mean()
59 | elif self.reduction == 'sum':
60 | return loss.sum()
61 | else: # 'none'
62 | return loss
63 |
64 | class VFLoss(nn.Module):
65 | def __init__(self, loss_fcn, gamma=1.5, alpha=0.25):
66 | super(VFLoss, self).__init__()
67 | # 传递 nn.BCEWithLogitsLoss() 损失函数 must be nn.BCEWithLogitsLoss()
68 | self.loss_fcn = loss_fcn #
69 | self.gamma = gamma
70 | self.alpha = alpha
71 | self.reduction = loss_fcn.reduction
72 | self.loss_fcn.reduction = 'mean' # required to apply VFL to each element
73 |
74 | def forward(self, pred, true):
75 | loss = self.loss_fcn(pred, true)
76 | pred_prob = torch.sigmoid(pred) # prob from logits
77 | focal_weight = true * (true > 0.0).float() + self.alpha * (pred_prob - true).abs().pow(self.gamma) * (true <= 0.0).float()
78 | loss *= focal_weight
79 | if self.reduction == 'mean':
80 | return loss.mean()
81 | elif self.reduction == 'sum':
82 | return loss.sum()
83 | else:
84 | return loss
85 |
86 |
87 | class QFocalLoss(nn.Module):
88 | # Wraps Quality focal loss around existing loss_fcn(), i.e. criteria = FocalLoss(nn.BCEWithLogitsLoss(), gamma=1.5)
89 | def __init__(self, loss_fcn, gamma=1.5, alpha=0.25):
90 | super().__init__()
91 | self.loss_fcn = loss_fcn # must be nn.BCEWithLogitsLoss()
92 | self.gamma = gamma
93 | self.alpha = alpha
94 | self.reduction = loss_fcn.reduction
95 | self.loss_fcn.reduction = 'none' # required to apply FL to each element
96 |
97 | def forward(self, pred, true):
98 | loss = self.loss_fcn(pred, true)
99 |
100 | pred_prob = torch.sigmoid(pred) # prob from logits
101 | alpha_factor = true * self.alpha + (1 - true) * (1 - self.alpha)
102 | modulating_factor = torch.abs(true - pred_prob) ** self.gamma
103 | loss *= alpha_factor * modulating_factor
104 |
105 | if self.reduction == 'mean':
106 | return loss.mean()
107 | elif self.reduction == 'sum':
108 | return loss.sum()
109 | else: # 'none'
110 | return loss
111 |
112 |
113 | class ComputeLoss:
114 | # Compute losses
115 | def __init__(self, model, autobalance=False):
116 | self.sort_obj_iou = False
117 | device = next(model.parameters()).device # get model device
118 | h = model.hyp # hyperparameters
119 |
120 | # Define criteria
121 | BCEcls = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['cls_pw']], device=device))
122 | BCEobj = nn.BCEWithLogitsLoss(pos_weight=torch.tensor([h['obj_pw']], device=device))
123 |
124 | # Class label smoothing https://arxiv.org/pdf/1902.04103.pdf eqn 3
125 | self.cp, self.cn = smooth_BCE(eps=h.get('label_smoothing', 0.0)) # positive, negative BCE targets
126 |
127 | # Focal loss
128 | g = h['fl_gamma'] # focal loss gamma
129 | if g > 0:
130 | BCEcls, BCEobj = FocalLoss(BCEcls, g), FocalLoss(BCEobj, g)
131 |
132 | det = model.module.model[-1] if is_parallel(model) else model.model[-1] # Detect() module
133 | self.balance = {3: [4.0, 1.0, 0.4]}.get(det.nl, [4.0, 1.0, 0.25, 0.06, 0.02]) # P3-P7
134 | self.ssi = list(det.stride).index(16) if autobalance else 0 # stride 16 index
135 | self.BCEcls, self.BCEobj, self.gr, self.hyp, self.autobalance = BCEcls, BCEobj, 1.0, h, autobalance
136 | for k in 'na', 'nc', 'nl', 'anchors':
137 | setattr(self, k, getattr(det, k))
138 |
139 | def __call__(self, p, targets): # predictions, targets, model
140 | device = targets.device
141 | lcls, lbox, lobj = torch.zeros(1, device=device), torch.zeros(1, device=device), torch.zeros(1, device=device)
142 | tcls, tbox, indices, anchors = self.build_targets(p, targets) # targets
143 |
144 | # Losses
145 | for i, pi in enumerate(p): # layer index, layer predictions
146 | b, a, gj, gi = indices[i] # image, anchor, gridy, gridx
147 | tobj = torch.zeros_like(pi[..., 0], device=device) # target obj
148 |
149 | n = b.shape[0] # number of targets
150 | if n:
151 | ps = pi[b, a, gj, gi] # prediction subset corresponding to targets
152 |
153 | # Regression
154 | pxy = ps[:, :2].sigmoid() * 2 - 0.5
155 | pwh = (ps[:, 2:4].sigmoid() * 2) ** 2 * anchors[i]
156 | pbox = torch.cat((pxy, pwh), 1) # predicted box
157 | iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True) # iou(prediction, target)
158 | lbox += (1.0 - iou).mean() # iou loss
159 |
160 | # Objectness
161 | score_iou = iou.detach().clamp(0).type(tobj.dtype)
162 | # if self.sort_obj_iou:
163 | if True:
164 | sort_id = torch.argsort(score_iou)
165 | b, a, gj, gi, score_iou = b[sort_id], a[sort_id], gj[sort_id], gi[sort_id], score_iou[sort_id]
166 | tobj[b, a, gj, gi] = (1.0 - self.gr) + self.gr * score_iou # iou ratio
167 |
168 | # Classification
169 | if self.nc > 1: # cls loss (only if multiple classes)
170 | t = torch.full_like(ps[:, 5:], self.cn, device=device) # targets
171 | t[range(n), tcls[i]] = self.cp
172 | lcls += self.BCEcls(ps[:, 5:], t) # BCE
173 |
174 | # Append targets to text file
175 | # with open('targets.txt', 'a') as file:
176 | # [file.write('%11.5g ' * 4 % tuple(x) + '\n') for x in torch.cat((txy[i], twh[i]), 1)]
177 |
178 | obji = self.BCEobj(pi[..., 4], tobj)
179 | lobj += obji * self.balance[i] # obj loss
180 | if self.autobalance:
181 | self.balance[i] = self.balance[i] * 0.9999 + 0.0001 / obji.detach().item()
182 |
183 | if self.autobalance:
184 | self.balance = [x / self.balance[self.ssi] for x in self.balance]
185 | lbox *= self.hyp['box']
186 | lobj *= self.hyp['obj']
187 | lcls *= self.hyp['cls']
188 | bs = tobj.shape[0] # batch size
189 |
190 | return (lbox + lobj + lcls) * bs, torch.cat((lbox, lobj, lcls)).detach()
191 |
192 | def build_targets(self, p, targets):
193 | # Build targets for compute_loss(), input targets(image,class,x,y,w,h)
194 | na, nt = self.na, targets.shape[0] # number of anchors, targets
195 | tcls, tbox, indices, anch = [], [], [], []
196 | gain = torch.ones(7, device=targets.device) # normalized to gridspace gain
197 | ai = torch.arange(na, device=targets.device).float().view(na, 1).repeat(1, nt) # same as .repeat_interleave(nt)
198 | targets = torch.cat((targets.repeat(na, 1, 1), ai[:, :, None]), 2) # append anchor indices
199 |
200 | g = 0.5 # bias
201 | off = torch.tensor([[0, 0],
202 | [1, 0], [0, 1], [-1, 0], [0, -1], # j,k,l,m
203 | # [1, 1], [1, -1], [-1, 1], [-1, -1], # jk,jm,lk,lm
204 | ], device=targets.device).float() * g # offsets
205 |
206 | for i in range(self.nl):
207 | anchors = self.anchors[i]
208 | gain[2:6] = torch.tensor(p[i].shape)[[3, 2, 3, 2]] # xyxy gain
209 |
210 | # Match targets to anchors
211 | t = targets * gain
212 | if nt:
213 | # Matches
214 | r = t[:, :, 4:6] / anchors[:, None] # wh ratio
215 | j = torch.max(r, 1 / r).max(2)[0] < self.hyp['anchor_t'] # compare
216 | # j = wh_iou(anchors, t[:, 4:6]) > model.hyp['iou_t'] # iou(3,n)=wh_iou(anchors(3,2), gwh(n,2))
217 | t = t[j] # filter
218 |
219 | # Offsets
220 | gxy = t[:, 2:4] # grid xy
221 | gxi = gain[[2, 3]] - gxy # inverse
222 | j, k = ((gxy % 1 < g) & (gxy > 1)).T
223 | l, m = ((gxi % 1 < g) & (gxi > 1)).T
224 | j = torch.stack((torch.ones_like(j), j, k, l, m))
225 | t = t.repeat((5, 1, 1))[j]
226 | offsets = (torch.zeros_like(gxy)[None] + off[:, None])[j]
227 | else:
228 | t = targets[0]
229 | offsets = 0
230 |
231 | # Define
232 | b, c = t[:, :2].long().T # image, class
233 | gxy = t[:, 2:4] # grid xy
234 | gwh = t[:, 4:6] # grid wh
235 | gij = (gxy - offsets).long()
236 | gi, gj = gij.T # grid xy indices
237 |
238 | # Append
239 | a = t[:, 6].long() # anchor indices
240 | indices.append((b, a, gj.clamp_(0, gain[3] - 1), gi.clamp_(0, gain[2] - 1))) # image, anchor, grid indices
241 | tbox.append(torch.cat((gxy - gij, gwh), 1)) # box
242 | anch.append(anchors[a]) # anchors
243 | tcls.append(c) # class
244 |
245 | return tcls, tbox, indices, anch
246 |
--------------------------------------------------------------------------------
/utils/metrics.py:
--------------------------------------------------------------------------------
1 | # YOLOv5 🚀 by Ultralytics, GPL-3.0 license
2 | """
3 | Model validation metrics
4 | """
5 |
6 | import math
7 | import warnings
8 | from pathlib import Path
9 |
10 | import matplotlib.pyplot as plt
11 | import numpy as np
12 | import torch
13 |
14 |
15 | def fitness(x):
16 | # Model fitness as a weighted combination of metrics
17 | w = [0.0, 0.0, 0.1, 0.9] # weights for [P, R, mAP@0.5, mAP@0.5:0.95]
18 | return (x[:, :4] * w).sum(1)
19 |
20 |
21 | def ap_per_class(tp, conf, pred_cls, target_cls, plot=False, save_dir='.', names=()):
22 | """ Compute the average precision, given the recall and precision curves.
23 | Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
24 | # Arguments
25 | tp: True positives (nparray, nx1 or nx10).
26 | conf: Objectness value from 0-1 (nparray).
27 | pred_cls: Predicted object classes (nparray).
28 | target_cls: True object classes (nparray).
29 | plot: Plot precision-recall curve at mAP@0.5
30 | save_dir: Plot save directory
31 | # Returns
32 | The average precision as computed in py-faster-rcnn.
33 | """
34 |
35 | # Sort by objectness
36 | i = np.argsort(-conf)
37 | tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
38 |
39 | # Find unique classes
40 | unique_classes = np.unique(target_cls)
41 | nc = unique_classes.shape[0] # number of classes, number of detections
42 |
43 | # Create Precision-Recall curve and compute AP for each class
44 | px, py = np.linspace(0, 1, 1000), [] # for plotting
45 | ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000))
46 | for ci, c in enumerate(unique_classes):
47 | i = pred_cls == c
48 | n_l = (target_cls == c).sum() # number of labels
49 | n_p = i.sum() # number of predictions
50 |
51 | if n_p == 0 or n_l == 0:
52 | continue
53 | else:
54 | # Accumulate FPs and TPs
55 | fpc = (1 - tp[i]).cumsum(0)
56 | tpc = tp[i].cumsum(0)
57 |
58 | # Recall
59 | recall = tpc / (n_l + 1e-16) # recall curve
60 | r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases
61 |
62 | # Precision
63 | precision = tpc / (tpc + fpc) # precision curve
64 | p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # p at pr_score
65 |
66 | # AP from recall-precision curve
67 | for j in range(tp.shape[1]):
68 | ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j])
69 | if plot and j == 0:
70 | py.append(np.interp(px, mrec, mpre)) # precision at mAP@0.5
71 |
72 | # Compute F1 (harmonic mean of precision and recall)
73 | f1 = 2 * p * r / (p + r + 1e-16)
74 | names = [v for k, v in names.items() if k in unique_classes] # list: only classes that have data
75 | names = {i: v for i, v in enumerate(names)} # to dict
76 | if plot:
77 | plot_pr_curve(px, py, ap, Path(save_dir) / 'PR_curve.png', names)
78 | plot_mc_curve(px, f1, Path(save_dir) / 'F1_curve.png', names, ylabel='F1')
79 | plot_mc_curve(px, p, Path(save_dir) / 'P_curve.png', names, ylabel='Precision')
80 | plot_mc_curve(px, r, Path(save_dir) / 'R_curve.png', names, ylabel='Recall')
81 |
82 | i = f1.mean(0).argmax() # max F1 index
83 | return p[:, i], r[:, i], ap, f1[:, i], unique_classes.astype('int32')
84 |
85 |
86 | def compute_ap(recall, precision):
87 | """ Compute the average precision, given the recall and precision curves
88 | # Arguments
89 | recall: The recall curve (list)
90 | precision: The precision curve (list)
91 | # Returns
92 | Average precision, precision curve, recall curve
93 | """
94 |
95 | # Append sentinel values to beginning and end
96 | mrec = np.concatenate(([0.0], recall, [1.0]))
97 | mpre = np.concatenate(([1.0], precision, [0.0]))
98 |
99 | # Compute the precision envelope
100 | mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))
101 |
102 | # Integrate area under curve
103 | method = 'interp' # methods: 'continuous', 'interp'
104 | if method == 'interp':
105 | x = np.linspace(0, 1, 101) # 101-point interp (COCO)
106 | ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate
107 | else: # 'continuous'
108 | i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes
109 | ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve
110 |
111 | return ap, mpre, mrec
112 |
113 |
114 | class ConfusionMatrix:
115 | # Updated version of https://github.com/kaanakan/object_detection_confusion_matrix
116 | def __init__(self, nc, conf=0.25, iou_thres=0.45):
117 | self.matrix = np.zeros((nc + 1, nc + 1))
118 | self.nc = nc # number of classes
119 | self.conf = conf
120 | self.iou_thres = iou_thres
121 |
122 | def process_batch(self, detections, labels):
123 | """
124 | Return intersection-over-union (Jaccard index) of boxes.
125 | Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
126 | Arguments:
127 | detections (Array[N, 6]), x1, y1, x2, y2, conf, class
128 | labels (Array[M, 5]), class, x1, y1, x2, y2
129 | Returns:
130 | None, updates confusion matrix accordingly
131 | """
132 | detections = detections[detections[:, 4] > self.conf]
133 | gt_classes = labels[:, 0].int()
134 | detection_classes = detections[:, 5].int()
135 | iou = box_iou(labels[:, 1:], detections[:, :4])
136 |
137 | x = torch.where(iou > self.iou_thres)
138 | if x[0].shape[0]:
139 | matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy()
140 | if x[0].shape[0] > 1:
141 | matches = matches[matches[:, 2].argsort()[::-1]]
142 | matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
143 | matches = matches[matches[:, 2].argsort()[::-1]]
144 | matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
145 | else:
146 | matches = np.zeros((0, 3))
147 |
148 | n = matches.shape[0] > 0
149 | m0, m1, _ = matches.transpose().astype(np.int16)
150 | for i, gc in enumerate(gt_classes):
151 | j = m0 == i
152 | if n and sum(j) == 1:
153 | self.matrix[detection_classes[m1[j]], gc] += 1 # correct
154 | else:
155 | self.matrix[self.nc, gc] += 1 # background FP
156 |
157 | if n:
158 | for i, dc in enumerate(detection_classes):
159 | if not any(m1 == i):
160 | self.matrix[dc, self.nc] += 1 # background FN
161 |
162 | def matrix(self):
163 | return self.matrix
164 |
165 | def plot(self, normalize=True, save_dir='', names=()):
166 | try:
167 | import seaborn as sn
168 |
169 | array = self.matrix / ((self.matrix.sum(0).reshape(1, -1) + 1E-6) if normalize else 1) # normalize columns
170 | array[array < 0.005] = np.nan # don't annotate (would appear as 0.00)
171 |
172 | fig = plt.figure(figsize=(12, 9), tight_layout=True)
173 | sn.set(font_scale=1.0 if self.nc < 50 else 0.8) # for label size
174 | labels = (0 < len(names) < 99) and len(names) == self.nc # apply names to ticklabels
175 | with warnings.catch_warnings():
176 | warnings.simplefilter('ignore') # suppress empty matrix RuntimeWarning: All-NaN slice encountered
177 | sn.heatmap(array, annot=self.nc < 30, annot_kws={"size": 8}, cmap='Blues', fmt='.2f', square=True,
178 | xticklabels=names + ['background FP'] if labels else "auto",
179 | yticklabels=names + ['background FN'] if labels else "auto").set_facecolor((1, 1, 1))
180 | fig.axes[0].set_xlabel('True')
181 | fig.axes[0].set_ylabel('Predicted')
182 | fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250)
183 | plt.close()
184 | except Exception as e:
185 | print(f'WARNING: ConfusionMatrix plot failure: {e}')
186 |
187 | def print(self):
188 | for i in range(self.nc + 1):
189 | print(' '.join(map(str, self.matrix[i])))
190 |
191 |
192 | def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7):
193 | # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
194 | box2 = box2.T
195 |
196 | # Get the coordinates of bounding boxes
197 | if x1y1x2y2: # x1, y1, x2, y2 = box1
198 | b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
199 | b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
200 | else: # transform from xywh to xyxy
201 | b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
202 | b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
203 | b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
204 | b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2
205 |
206 | # Intersection area
207 | inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
208 | (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
209 |
210 | # Union Area
211 | w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
212 | w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
213 | union = w1 * h1 + w2 * h2 - inter + eps
214 |
215 | iou = inter / union
216 | if GIoU or DIoU or CIoU:
217 | cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
218 | ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
219 | if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
220 | c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
221 | rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
222 | (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
223 | if DIoU:
224 | return iou - rho2 / c2 # DIoU
225 | elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
226 | v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
227 | with torch.no_grad():
228 | alpha = v / (v - iou + (1 + eps))
229 | return iou - (rho2 / c2 + v * alpha) # CIoU
230 | else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
231 | c_area = cw * ch + eps # convex area
232 | return iou - (c_area - union) / c_area # GIoU
233 | else:
234 | return iou # IoU
235 |
236 |
237 | def box_iou(box1, box2):
238 | # https://github.com/pytorch/vision/blob/master/torchvision/ops/boxes.py
239 | """
240 | Return intersection-over-union (Jaccard index) of boxes.
241 | Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
242 | Arguments:
243 | box1 (Tensor[N, 4])
244 | box2 (Tensor[M, 4])
245 | Returns:
246 | iou (Tensor[N, M]): the NxM matrix containing the pairwise
247 | IoU values for every element in boxes1 and boxes2
248 | """
249 |
250 | def box_area(box):
251 | # box = 4xn
252 | return (box[2] - box[0]) * (box[3] - box[1])
253 |
254 | area1 = box_area(box1.T)
255 | area2 = box_area(box2.T)
256 |
257 | # inter(N,M) = (rb(N,M,2) - lt(N,M,2)).clamp(0).prod(2)
258 | inter = (torch.min(box1[:, None, 2:], box2[:, 2:]) - torch.max(box1[:, None, :2], box2[:, :2])).clamp(0).prod(2)
259 | return inter / (area1[:, None] + area2 - inter) # iou = inter / (area1 + area2 - inter)
260 |
261 |
262 | def bbox_ioa(box1, box2, eps=1E-7):
263 | """ Returns the intersection over box2 area given box1, box2. Boxes are x1y1x2y2
264 | box1: np.array of shape(4)
265 | box2: np.array of shape(nx4)
266 | returns: np.array of shape(n)
267 | """
268 |
269 | box2 = box2.transpose()
270 |
271 | # Get the coordinates of bounding boxes
272 | b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
273 | b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
274 |
275 | # Intersection area
276 | inter_area = (np.minimum(b1_x2, b2_x2) - np.maximum(b1_x1, b2_x1)).clip(0) * \
277 | (np.minimum(b1_y2, b2_y2) - np.maximum(b1_y1, b2_y1)).clip(0)
278 |
279 | # box2 area
280 | box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + eps
281 |
282 | # Intersection over box2 area
283 | return inter_area / box2_area
284 |
285 |
286 | def wh_iou(wh1, wh2):
287 | # Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2
288 | wh1 = wh1[:, None] # [N,1,2]
289 | wh2 = wh2[None] # [1,M,2]
290 | inter = torch.min(wh1, wh2).prod(2) # [N,M]
291 | return inter / (wh1.prod(2) + wh2.prod(2) - inter) # iou = inter / (area1 + area2 - inter)
292 |
293 |
294 | # Plots ----------------------------------------------------------------------------------------------------------------
295 |
296 | def plot_pr_curve(px, py, ap, save_dir='pr_curve.png', names=()):
297 | # Precision-recall curve
298 | fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
299 | py = np.stack(py, axis=1)
300 |
301 | if 0 < len(names) < 21: # display per-class legend if < 21 classes
302 | for i, y in enumerate(py.T):
303 | ax.plot(px, y, linewidth=1, label=f'{names[i]} {ap[i, 0]:.3f}') # plot(recall, precision)
304 | else:
305 | ax.plot(px, py, linewidth=1, color='grey') # plot(recall, precision)
306 |
307 | ax.plot(px, py.mean(1), linewidth=3, color='blue', label='all classes %.3f mAP@0.5' % ap[:, 0].mean())
308 | ax.set_xlabel('Recall')
309 | ax.set_ylabel('Precision')
310 | ax.set_xlim(0, 1)
311 | ax.set_ylim(0, 1)
312 | plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
313 | fig.savefig(Path(save_dir), dpi=250)
314 | plt.close()
315 |
316 |
317 | def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence', ylabel='Metric'):
318 | # Metric-confidence curve
319 | fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
320 |
321 | if 0 < len(names) < 21: # display per-class legend if < 21 classes
322 | for i, y in enumerate(py):
323 | ax.plot(px, y, linewidth=1, label=f'{names[i]}') # plot(confidence, metric)
324 | else:
325 | ax.plot(px, py.T, linewidth=1, color='grey') # plot(confidence, metric)
326 |
327 | y = py.mean(0)
328 | ax.plot(px, y, linewidth=3, color='blue', label=f'all classes {y.max():.2f} at {px[y.argmax()]:.3f}')
329 | ax.set_xlabel(xlabel)
330 | ax.set_ylabel(ylabel)
331 | ax.set_xlim(0, 1)
332 | ax.set_ylim(0, 1)
333 | plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
334 | fig.savefig(Path(save_dir), dpi=250)
335 | plt.close()
336 |
--------------------------------------------------------------------------------
/wbf.py:
--------------------------------------------------------------------------------
1 | import numpy as np
2 | import pandas as pd
3 | import os
4 | from PIL import Image
5 | from tqdm import tqdm
6 | from ensemble_boxes import *
7 |
8 | def xywh2x1y1x2y2(bbox):
9 | x1 = bbox[0] - bbox[2]/2
10 | x2 = bbox[0] + bbox[2]/2
11 | y1 = bbox[1] - bbox[3]/2
12 | y2 = bbox[1] + bbox[3]/2
13 | return ([x1,y1,x2,y2])
14 |
15 | def x1y1x2y22xywh(bbox):
16 | x = (bbox[0] + bbox[2])/2
17 | y = (bbox[1] + bbox[3])/2
18 | w = bbox[2] - bbox[0]
19 | h = bbox[3] - bbox[1]
20 | return ([x,y,w,h])
21 |
22 | IMG_PATH = '/VisDrone2019-DET-test-challenge/images/'
23 | TXT_PATH = './runs/val/'
24 |
25 | OUT_PATH = './runs/wbf_labels/'
26 |
27 |
28 | MODEL_NAME = os.listdir(TXT_PATH)
29 | # MODEL_NAME = ['test1','test2']
30 |
31 | # ===============================
32 | # Default WBF config (you can change these)
33 | iou_thr = 0.67 #0.67
34 | skip_box_thr = 0.01
35 | # skip_box_thr = 0.0001
36 | sigma = 0.1
37 | # boxes_list, scores_list, labels_list, weights=weights,
38 | # ===============================
39 |
40 | image_ids = os.listdir(IMG_PATH)
41 | for image_id in tqdm(image_ids, total=len(image_ids)):
42 | boxes_list = []
43 | scores_list = []
44 | labels_list = []
45 | weights = []
46 | for name in MODEL_NAME:
47 | box_list = []
48 | score_list = []
49 | label_list = []
50 | txt_file = TXT_PATH + name + '/labels/' + image_id.replace('jpg', 'txt')
51 | if os.path.exists(txt_file):
52 | # if os.path.getsize(txt_file) > 0:
53 | txt_df = pd.read_csv(txt_file,header=None,sep=' ').values
54 |
55 | for row in txt_df:
56 | box_list.append(xywh2x1y1x2y2(row[1:5]))
57 | score_list.append(row[5])
58 | label_list.append(int(row[0]))
59 | boxes_list.append(box_list)
60 | scores_list.append(score_list)
61 | labels_list.append(label_list)
62 | weights.append(1.0)
63 | else:
64 | continue
65 | # print(txt_file)
66 |
67 | boxes, scores, labels = weighted_boxes_fusion(boxes_list, scores_list, labels_list, weights=weights, iou_thr=iou_thr, skip_box_thr=skip_box_thr)
68 | if not os.path.exists(OUT_PATH):
69 | os.makedirs(OUT_PATH)
70 | out_file = open(OUT_PATH + image_id.replace('jpg', 'txt'), 'w')
71 |
72 | for i,row in enumerate(boxes):
73 | img = Image.open(IMG_PATH + image_id)
74 | img_size = img.size
75 | bbox = x1y1x2y22xywh(row)
76 | out_file.write(str(int(labels[i]+1)) + ' ' +" ".join(str(x) for x in bbox) + " " + str(round(scores[i],6)) + '\n')
77 | out_file.close()
--------------------------------------------------------------------------------