Compare commits
7 Commits
c78f1d27df
...
1bb2ac9869
Author | SHA1 | Date |
---|---|---|
|
1bb2ac9869 | 2 years ago |
|
995cf382b4 | 2 years ago |
|
bb51d457ba | 2 years ago |
|
3c485e826b | 2 years ago |
|
e966ef0ae8 | 2 years ago |
|
55f559934c | 2 years ago |
|
1d9dfd8e44 | 2 years ago |
After Width: | Height: | Size: 89 KiB |
After Width: | Height: | Size: 48 KiB |
After Width: | Height: | Size: 42 KiB |
After Width: | Height: | Size: 85 KiB |
After Width: | Height: | Size: 80 KiB |
@ -0,0 +1,128 @@
|
|||||||
|
# YOLOv5 🚀 Contributor Covenant Code of Conduct
|
||||||
|
|
||||||
|
## Our Pledge
|
||||||
|
|
||||||
|
We as members, contributors, and leaders pledge to make participation in our
|
||||||
|
community a harassment-free experience for everyone, regardless of age, body
|
||||||
|
size, visible or invisible disability, ethnicity, sex characteristics, gender
|
||||||
|
identity and expression, level of experience, education, socio-economic status,
|
||||||
|
nationality, personal appearance, race, religion, or sexual identity
|
||||||
|
and orientation.
|
||||||
|
|
||||||
|
We pledge to act and interact in ways that contribute to an open, welcoming,
|
||||||
|
diverse, inclusive, and healthy community.
|
||||||
|
|
||||||
|
## Our Standards
|
||||||
|
|
||||||
|
Examples of behavior that contributes to a positive environment for our
|
||||||
|
community include:
|
||||||
|
|
||||||
|
- Demonstrating empathy and kindness toward other people
|
||||||
|
- Being respectful of differing opinions, viewpoints, and experiences
|
||||||
|
- Giving and gracefully accepting constructive feedback
|
||||||
|
- Accepting responsibility and apologizing to those affected by our mistakes,
|
||||||
|
and learning from the experience
|
||||||
|
- Focusing on what is best not just for us as individuals, but for the
|
||||||
|
overall community
|
||||||
|
|
||||||
|
Examples of unacceptable behavior include:
|
||||||
|
|
||||||
|
- The use of sexualized language or imagery, and sexual attention or
|
||||||
|
advances of any kind
|
||||||
|
- Trolling, insulting or derogatory comments, and personal or political attacks
|
||||||
|
- Public or private harassment
|
||||||
|
- Publishing others' private information, such as a physical or email
|
||||||
|
address, without their explicit permission
|
||||||
|
- Other conduct which could reasonably be considered inappropriate in a
|
||||||
|
professional setting
|
||||||
|
|
||||||
|
## Enforcement Responsibilities
|
||||||
|
|
||||||
|
Community leaders are responsible for clarifying and enforcing our standards of
|
||||||
|
acceptable behavior and will take appropriate and fair corrective action in
|
||||||
|
response to any behavior that they deem inappropriate, threatening, offensive,
|
||||||
|
or harmful.
|
||||||
|
|
||||||
|
Community leaders have the right and responsibility to remove, edit, or reject
|
||||||
|
comments, commits, code, wiki edits, issues, and other contributions that are
|
||||||
|
not aligned to this Code of Conduct, and will communicate reasons for moderation
|
||||||
|
decisions when appropriate.
|
||||||
|
|
||||||
|
## Scope
|
||||||
|
|
||||||
|
This Code of Conduct applies within all community spaces, and also applies when
|
||||||
|
an individual is officially representing the community in public spaces.
|
||||||
|
Examples of representing our community include using an official e-mail address,
|
||||||
|
posting via an official social media account, or acting as an appointed
|
||||||
|
representative at an online or offline event.
|
||||||
|
|
||||||
|
## Enforcement
|
||||||
|
|
||||||
|
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||||
|
reported to the community leaders responsible for enforcement at
|
||||||
|
hello@ultralytics.com.
|
||||||
|
All complaints will be reviewed and investigated promptly and fairly.
|
||||||
|
|
||||||
|
All community leaders are obligated to respect the privacy and security of the
|
||||||
|
reporter of any incident.
|
||||||
|
|
||||||
|
## Enforcement Guidelines
|
||||||
|
|
||||||
|
Community leaders will follow these Community Impact Guidelines in determining
|
||||||
|
the consequences for any action they deem in violation of this Code of Conduct:
|
||||||
|
|
||||||
|
### 1. Correction
|
||||||
|
|
||||||
|
**Community Impact**: Use of inappropriate language or other behavior deemed
|
||||||
|
unprofessional or unwelcome in the community.
|
||||||
|
|
||||||
|
**Consequence**: A private, written warning from community leaders, providing
|
||||||
|
clarity around the nature of the violation and an explanation of why the
|
||||||
|
behavior was inappropriate. A public apology may be requested.
|
||||||
|
|
||||||
|
### 2. Warning
|
||||||
|
|
||||||
|
**Community Impact**: A violation through a single incident or series
|
||||||
|
of actions.
|
||||||
|
|
||||||
|
**Consequence**: A warning with consequences for continued behavior. No
|
||||||
|
interaction with the people involved, including unsolicited interaction with
|
||||||
|
those enforcing the Code of Conduct, for a specified period of time. This
|
||||||
|
includes avoiding interactions in community spaces as well as external channels
|
||||||
|
like social media. Violating these terms may lead to a temporary or
|
||||||
|
permanent ban.
|
||||||
|
|
||||||
|
### 3. Temporary Ban
|
||||||
|
|
||||||
|
**Community Impact**: A serious violation of community standards, including
|
||||||
|
sustained inappropriate behavior.
|
||||||
|
|
||||||
|
**Consequence**: A temporary ban from any sort of interaction or public
|
||||||
|
communication with the community for a specified period of time. No public or
|
||||||
|
private interaction with the people involved, including unsolicited interaction
|
||||||
|
with those enforcing the Code of Conduct, is allowed during this period.
|
||||||
|
Violating these terms may lead to a permanent ban.
|
||||||
|
|
||||||
|
### 4. Permanent Ban
|
||||||
|
|
||||||
|
**Community Impact**: Demonstrating a pattern of violation of community
|
||||||
|
standards, including sustained inappropriate behavior, harassment of an
|
||||||
|
individual, or aggression toward or disparagement of classes of individuals.
|
||||||
|
|
||||||
|
**Consequence**: A permanent ban from any sort of public interaction within
|
||||||
|
the community.
|
||||||
|
|
||||||
|
## Attribution
|
||||||
|
|
||||||
|
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
||||||
|
version 2.0, available at
|
||||||
|
https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
||||||
|
|
||||||
|
Community Impact Guidelines were inspired by [Mozilla's code of conduct
|
||||||
|
enforcement ladder](https://github.com/mozilla/diversity).
|
||||||
|
|
||||||
|
For answers to common questions about this code of conduct, see the FAQ at
|
||||||
|
https://www.contributor-covenant.org/faq. Translations are available at
|
||||||
|
https://www.contributor-covenant.org/translations.
|
||||||
|
|
||||||
|
[homepage]: https://www.contributor-covenant.org
|
@ -0,0 +1,85 @@
|
|||||||
|
name: 🐛 Bug Report
|
||||||
|
# title: " "
|
||||||
|
description: Problems with YOLOv5
|
||||||
|
labels: [bug, triage]
|
||||||
|
body:
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
Thank you for submitting a YOLOv5 🐛 Bug Report!
|
||||||
|
|
||||||
|
- type: checkboxes
|
||||||
|
attributes:
|
||||||
|
label: Search before asking
|
||||||
|
description: >
|
||||||
|
Please search the [issues](https://github.com/ultralytics/yolov5/issues) to see if a similar bug report already exists.
|
||||||
|
options:
|
||||||
|
- label: >
|
||||||
|
I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar bug report.
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: dropdown
|
||||||
|
attributes:
|
||||||
|
label: YOLOv5 Component
|
||||||
|
description: |
|
||||||
|
Please select the part of YOLOv5 where you found the bug.
|
||||||
|
multiple: true
|
||||||
|
options:
|
||||||
|
- "Training"
|
||||||
|
- "Validation"
|
||||||
|
- "Detection"
|
||||||
|
- "Export"
|
||||||
|
- "PyTorch Hub"
|
||||||
|
- "Multi-GPU"
|
||||||
|
- "Evolution"
|
||||||
|
- "Integrations"
|
||||||
|
- "Other"
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Bug
|
||||||
|
description: Provide console output with error messages and/or screenshots of the bug.
|
||||||
|
placeholder: |
|
||||||
|
💡 ProTip! Include as much information as possible (screenshots, logs, tracebacks etc.) to receive the most helpful response.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Environment
|
||||||
|
description: Please specify the software and hardware you used to produce the bug.
|
||||||
|
placeholder: |
|
||||||
|
- YOLO: YOLOv5 🚀 v6.0-67-g60e42e1 torch 1.9.0+cu111 CUDA:0 (A100-SXM4-40GB, 40536MiB)
|
||||||
|
- OS: Ubuntu 20.04
|
||||||
|
- Python: 3.9.0
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Minimal Reproducible Example
|
||||||
|
description: >
|
||||||
|
When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to **reproduce** the problem.
|
||||||
|
This is referred to by community members as creating a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example).
|
||||||
|
placeholder: |
|
||||||
|
```
|
||||||
|
# Code to reproduce your issue here
|
||||||
|
```
|
||||||
|
validations:
|
||||||
|
required: false
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Additional
|
||||||
|
description: Anything else you would like to share?
|
||||||
|
|
||||||
|
- type: checkboxes
|
||||||
|
attributes:
|
||||||
|
label: Are you willing to submit a PR?
|
||||||
|
description: >
|
||||||
|
(Optional) We encourage you to submit a [Pull Request](https://github.com/ultralytics/yolov5/pulls) (PR) to help improve YOLOv5 for everyone, especially if you have a good understanding of how to implement a fix or feature.
|
||||||
|
See the YOLOv5 [Contributing Guide](https://github.com/ultralytics/yolov5/blob/master/CONTRIBUTING.md) to get started.
|
||||||
|
options:
|
||||||
|
- label: Yes I'd like to help by submitting a PR!
|
@ -0,0 +1,8 @@
|
|||||||
|
blank_issues_enabled: true
|
||||||
|
contact_links:
|
||||||
|
- name: 💬 Forum
|
||||||
|
url: https://community.ultralytics.com/
|
||||||
|
about: Ask on Ultralytics Community Forum
|
||||||
|
- name: Stack Overflow
|
||||||
|
url: https://stackoverflow.com/search?q=YOLOv5
|
||||||
|
about: Ask on Stack Overflow with 'YOLOv5' tag
|
@ -0,0 +1,50 @@
|
|||||||
|
name: 🚀 Feature Request
|
||||||
|
description: Suggest a YOLOv5 idea
|
||||||
|
# title: " "
|
||||||
|
labels: [enhancement]
|
||||||
|
body:
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
Thank you for submitting a YOLOv5 🚀 Feature Request!
|
||||||
|
|
||||||
|
- type: checkboxes
|
||||||
|
attributes:
|
||||||
|
label: Search before asking
|
||||||
|
description: >
|
||||||
|
Please search the [issues](https://github.com/ultralytics/yolov5/issues) to see if a similar feature request already exists.
|
||||||
|
options:
|
||||||
|
- label: >
|
||||||
|
I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar feature requests.
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Description
|
||||||
|
description: A short description of your feature.
|
||||||
|
placeholder: |
|
||||||
|
What new feature would you like to see in YOLOv5?
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Use case
|
||||||
|
description: |
|
||||||
|
Describe the use case of your feature request. It will help us understand and prioritize the feature request.
|
||||||
|
placeholder: |
|
||||||
|
How would this feature be used, and who would use it?
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Additional
|
||||||
|
description: Anything else you would like to share?
|
||||||
|
|
||||||
|
- type: checkboxes
|
||||||
|
attributes:
|
||||||
|
label: Are you willing to submit a PR?
|
||||||
|
description: >
|
||||||
|
(Optional) We encourage you to submit a [Pull Request](https://github.com/ultralytics/yolov5/pulls) (PR) to help improve YOLOv5 for everyone, especially if you have a good understanding of how to implement a fix or feature.
|
||||||
|
See the YOLOv5 [Contributing Guide](https://github.com/ultralytics/yolov5/blob/master/CONTRIBUTING.md) to get started.
|
||||||
|
options:
|
||||||
|
- label: Yes I'd like to help by submitting a PR!
|
@ -0,0 +1,33 @@
|
|||||||
|
name: ❓ Question
|
||||||
|
description: Ask a YOLOv5 question
|
||||||
|
# title: " "
|
||||||
|
labels: [question]
|
||||||
|
body:
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
Thank you for asking a YOLOv5 ❓ Question!
|
||||||
|
|
||||||
|
- type: checkboxes
|
||||||
|
attributes:
|
||||||
|
label: Search before asking
|
||||||
|
description: >
|
||||||
|
Please search the [issues](https://github.com/ultralytics/yolov5/issues) and [discussions](https://github.com/ultralytics/yolov5/discussions) to see if a similar question already exists.
|
||||||
|
options:
|
||||||
|
- label: >
|
||||||
|
I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and [discussions](https://github.com/ultralytics/yolov5/discussions) and found no similar questions.
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Question
|
||||||
|
description: What is your question?
|
||||||
|
placeholder: |
|
||||||
|
💡 ProTip! Include as much information as possible (screenshots, logs, tracebacks etc.) to receive the most helpful response.
|
||||||
|
validations:
|
||||||
|
required: true
|
||||||
|
|
||||||
|
- type: textarea
|
||||||
|
attributes:
|
||||||
|
label: Additional
|
||||||
|
description: Anything else you would like to share?
|
@ -0,0 +1,9 @@
|
|||||||
|
<!--
|
||||||
|
Thank you for submitting a YOLOv5 🚀 Pull Request! We want to make contributing to YOLOv5 as easy and transparent as possible. A few tips to get you started:
|
||||||
|
|
||||||
|
- Search existing YOLOv5 [PRs](https://github.com/ultralytics/yolov5/pull) to see if a similar PR already exists.
|
||||||
|
- Link this PR to a YOLOv5 [issue](https://github.com/ultralytics/yolov5/issues) to help us understand what bug fix or feature is being implemented.
|
||||||
|
- Provide before and after profiling/inference/training results to help us quantify the improvement your PR provides (if applicable).
|
||||||
|
|
||||||
|
Please see our ✅ [Contributing Guide](https://github.com/ultralytics/yolov5/blob/master/CONTRIBUTING.md) for more details.
|
||||||
|
-->
|
@ -0,0 +1,7 @@
|
|||||||
|
# Security Policy
|
||||||
|
|
||||||
|
We aim to make YOLOv5 🚀 as secure as possible! If you find potential vulnerabilities or have any concerns please let us know so we can investigate and take corrective action if needed.
|
||||||
|
|
||||||
|
### Reporting a Vulnerability
|
||||||
|
|
||||||
|
To report vulnerabilities please email us at hello@ultralytics.com or visit https://ultralytics.com/contact. Thank you!
|
@ -0,0 +1,23 @@
|
|||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: pip
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: weekly
|
||||||
|
time: "04:00"
|
||||||
|
open-pull-requests-limit: 10
|
||||||
|
reviewers:
|
||||||
|
- glenn-jocher
|
||||||
|
labels:
|
||||||
|
- dependencies
|
||||||
|
|
||||||
|
- package-ecosystem: github-actions
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: weekly
|
||||||
|
time: "04:00"
|
||||||
|
open-pull-requests-limit: 5
|
||||||
|
reviewers:
|
||||||
|
- glenn-jocher
|
||||||
|
labels:
|
||||||
|
- dependencies
|
@ -0,0 +1,167 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# YOLOv5 Continuous Integration (CI) GitHub Actions tests
|
||||||
|
|
||||||
|
name: YOLOv5 CI
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ master ]
|
||||||
|
schedule:
|
||||||
|
- cron: '0 0 * * *' # runs at 00:00 UTC every day
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
Benchmarks:
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
os: [ ubuntu-latest ]
|
||||||
|
python-version: [ '3.9' ] # requires python<=3.9
|
||||||
|
model: [ yolov5n ]
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
#- name: Cache pip
|
||||||
|
# uses: actions/cache@v3
|
||||||
|
# with:
|
||||||
|
# path: ~/.cache/pip
|
||||||
|
# key: ${{ runner.os }}-Benchmarks-${{ hashFiles('requirements.txt') }}
|
||||||
|
# restore-keys: ${{ runner.os }}-Benchmarks-
|
||||||
|
- name: Install requirements
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip wheel
|
||||||
|
pip install -r requirements.txt coremltools openvino-dev tensorflow-cpu --extra-index-url https://download.pytorch.org/whl/cpu
|
||||||
|
python --version
|
||||||
|
pip --version
|
||||||
|
pip list
|
||||||
|
- name: Benchmark DetectionModel
|
||||||
|
run: |
|
||||||
|
python benchmarks.py --data coco128.yaml --weights ${{ matrix.model }}.pt --img 320 --hard-fail 0.29
|
||||||
|
- name: Benchmark SegmentationModel
|
||||||
|
run: |
|
||||||
|
python benchmarks.py --data coco128-seg.yaml --weights ${{ matrix.model }}-seg.pt --img 320 --hard-fail 0.22
|
||||||
|
- name: Test predictions
|
||||||
|
run: |
|
||||||
|
python export.py --weights ${{ matrix.model }}-cls.pt --include onnx --img 224
|
||||||
|
python detect.py --weights ${{ matrix.model }}.onnx --img 320
|
||||||
|
python segment/predict.py --weights ${{ matrix.model }}-seg.onnx --img 320
|
||||||
|
python classify/predict.py --weights ${{ matrix.model }}-cls.onnx --img 224
|
||||||
|
|
||||||
|
Tests:
|
||||||
|
timeout-minutes: 60
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
os: [ ubuntu-latest, windows-latest ] # macos-latest bug https://github.com/ultralytics/yolov5/pull/9049
|
||||||
|
python-version: [ '3.10' ]
|
||||||
|
model: [ yolov5n ]
|
||||||
|
include:
|
||||||
|
- os: ubuntu-latest
|
||||||
|
python-version: '3.7' # '3.6.8' min
|
||||||
|
model: yolov5n
|
||||||
|
- os: ubuntu-latest
|
||||||
|
python-version: '3.8'
|
||||||
|
model: yolov5n
|
||||||
|
- os: ubuntu-latest
|
||||||
|
python-version: '3.9'
|
||||||
|
model: yolov5n
|
||||||
|
- os: ubuntu-latest
|
||||||
|
python-version: '3.8' # torch 1.7.0 requires python >=3.6, <=3.8
|
||||||
|
model: yolov5n
|
||||||
|
torch: '1.7.0' # min torch version CI https://pypi.org/project/torchvision/
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v3
|
||||||
|
- uses: actions/setup-python@v4
|
||||||
|
with:
|
||||||
|
python-version: ${{ matrix.python-version }}
|
||||||
|
- name: Get cache dir
|
||||||
|
# https://github.com/actions/cache/blob/master/examples.md#multiple-oss-in-a-workflow
|
||||||
|
id: pip-cache
|
||||||
|
run: echo "::set-output name=dir::$(pip cache dir)"
|
||||||
|
- name: Cache pip
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: ${{ steps.pip-cache.outputs.dir }}
|
||||||
|
key: ${{ runner.os }}-${{ matrix.python-version }}-pip-${{ hashFiles('requirements.txt') }}
|
||||||
|
restore-keys: ${{ runner.os }}-${{ matrix.python-version }}-pip-
|
||||||
|
- name: Install requirements
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip wheel
|
||||||
|
if [ "${{ matrix.torch }}" == "1.7.0" ]; then
|
||||||
|
pip install -r requirements.txt torch==1.7.0 torchvision==0.8.1 --extra-index-url https://download.pytorch.org/whl/cpu
|
||||||
|
else
|
||||||
|
pip install -r requirements.txt --extra-index-url https://download.pytorch.org/whl/cpu
|
||||||
|
fi
|
||||||
|
shell: bash # for Windows compatibility
|
||||||
|
- name: Check environment
|
||||||
|
run: |
|
||||||
|
python -c "import utils; utils.notebook_init()"
|
||||||
|
echo "RUNNER_OS is ${{ runner.os }}"
|
||||||
|
echo "GITHUB_EVENT_NAME is ${{ github.event_name }}"
|
||||||
|
echo "GITHUB_WORKFLOW is ${{ github.workflow }}"
|
||||||
|
echo "GITHUB_ACTOR is ${{ github.actor }}"
|
||||||
|
echo "GITHUB_REPOSITORY is ${{ github.repository }}"
|
||||||
|
echo "GITHUB_REPOSITORY_OWNER is ${{ github.repository_owner }}"
|
||||||
|
python --version
|
||||||
|
pip --version
|
||||||
|
pip list
|
||||||
|
- name: Test detection
|
||||||
|
shell: bash # for Windows compatibility
|
||||||
|
run: |
|
||||||
|
# export PYTHONPATH="$PWD" # to run '$ python *.py' files in subdirectories
|
||||||
|
m=${{ matrix.model }} # official weights
|
||||||
|
b=runs/train/exp/weights/best # best.pt checkpoint
|
||||||
|
python train.py --imgsz 64 --batch 32 --weights $m.pt --cfg $m.yaml --epochs 1 --device cpu # train
|
||||||
|
for d in cpu; do # devices
|
||||||
|
for w in $m $b; do # weights
|
||||||
|
python val.py --imgsz 64 --batch 32 --weights $w.pt --device $d # val
|
||||||
|
python detect.py --imgsz 64 --weights $w.pt --device $d # detect
|
||||||
|
done
|
||||||
|
done
|
||||||
|
python hubconf.py --model $m # hub
|
||||||
|
# python models/tf.py --weights $m.pt # build TF model
|
||||||
|
python models/yolo.py --cfg $m.yaml # build PyTorch model
|
||||||
|
python export.py --weights $m.pt --img 64 --include torchscript # export
|
||||||
|
python - <<EOF
|
||||||
|
import torch
|
||||||
|
im = torch.zeros([1, 3, 64, 64])
|
||||||
|
for path in '$m', '$b':
|
||||||
|
model = torch.hub.load('.', 'custom', path=path, source='local')
|
||||||
|
print(model('data/images/bus.jpg'))
|
||||||
|
model(im) # warmup, build grids for trace
|
||||||
|
torch.jit.trace(model, [im])
|
||||||
|
EOF
|
||||||
|
- name: Test segmentation
|
||||||
|
shell: bash # for Windows compatibility
|
||||||
|
run: |
|
||||||
|
m=${{ matrix.model }}-seg # official weights
|
||||||
|
b=runs/train-seg/exp/weights/best # best.pt checkpoint
|
||||||
|
python segment/train.py --imgsz 64 --batch 32 --weights $m.pt --cfg $m.yaml --epochs 1 --device cpu # train
|
||||||
|
python segment/train.py --imgsz 64 --batch 32 --weights '' --cfg $m.yaml --epochs 1 --device cpu # train
|
||||||
|
for d in cpu; do # devices
|
||||||
|
for w in $m $b; do # weights
|
||||||
|
python segment/val.py --imgsz 64 --batch 32 --weights $w.pt --device $d # val
|
||||||
|
python segment/predict.py --imgsz 64 --weights $w.pt --device $d # predict
|
||||||
|
python export.py --weights $w.pt --img 64 --include torchscript --device $d # export
|
||||||
|
done
|
||||||
|
done
|
||||||
|
- name: Test classification
|
||||||
|
shell: bash # for Windows compatibility
|
||||||
|
run: |
|
||||||
|
m=${{ matrix.model }}-cls.pt # official weights
|
||||||
|
b=runs/train-cls/exp/weights/best.pt # best.pt checkpoint
|
||||||
|
python classify/train.py --imgsz 32 --model $m --data mnist160 --epochs 1 # train
|
||||||
|
python classify/val.py --imgsz 32 --weights $b --data ../datasets/mnist160 # val
|
||||||
|
python classify/predict.py --imgsz 32 --weights $b --source ../datasets/mnist160/test/7/60.png # predict
|
||||||
|
python classify/predict.py --imgsz 32 --weights $m --source data/images/bus.jpg # predict
|
||||||
|
python export.py --weights $b --img 64 --include torchscript # export
|
||||||
|
python - <<EOF
|
||||||
|
import torch
|
||||||
|
for path in '$m', '$b':
|
||||||
|
model = torch.hub.load('.', 'custom', path=path, source='local')
|
||||||
|
EOF
|
@ -0,0 +1,57 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Builds ultralytics/yolov5:latest images on DockerHub https://hub.docker.com/r/ultralytics/yolov5
|
||||||
|
|
||||||
|
name: Publish Docker Images
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
docker:
|
||||||
|
if: github.repository == 'ultralytics/yolov5'
|
||||||
|
name: Push Docker image to Docker Hub
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout repo
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
|
||||||
|
- name: Login to Docker Hub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push arm64 image
|
||||||
|
uses: docker/build-push-action@v3
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
platforms: linux/arm64
|
||||||
|
file: utils/docker/Dockerfile-arm64
|
||||||
|
push: true
|
||||||
|
tags: ultralytics/yolov5:latest-arm64
|
||||||
|
|
||||||
|
- name: Build and push CPU image
|
||||||
|
uses: docker/build-push-action@v3
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: utils/docker/Dockerfile-cpu
|
||||||
|
push: true
|
||||||
|
tags: ultralytics/yolov5:latest-cpu
|
||||||
|
|
||||||
|
- name: Build and push GPU image
|
||||||
|
uses: docker/build-push-action@v3
|
||||||
|
continue-on-error: true
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: utils/docker/Dockerfile
|
||||||
|
push: true
|
||||||
|
tags: ultralytics/yolov5:latest
|
@ -0,0 +1,57 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
name: Greetings
|
||||||
|
|
||||||
|
on:
|
||||||
|
pull_request_target:
|
||||||
|
types: [opened]
|
||||||
|
issues:
|
||||||
|
types: [opened]
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
greeting:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/first-interaction@v1
|
||||||
|
with:
|
||||||
|
repo-token: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
pr-message: |
|
||||||
|
👋 Hello @${{ github.actor }}, thank you for submitting a YOLOv5 🚀 PR! To allow your work to be integrated as seamlessly as possible, we advise you to:
|
||||||
|
|
||||||
|
- ✅ Verify your PR is **up-to-date** with `ultralytics/yolov5` `master` branch. If your PR is behind you can update your code by clicking the 'Update branch' button or by running `git pull` and `git merge master` locally.
|
||||||
|
- ✅ Verify all YOLOv5 Continuous Integration (CI) **checks are passing**.
|
||||||
|
- ✅ Reduce changes to the absolute **minimum** required for your bug fix or feature addition. _"It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is."_ — Bruce Lee
|
||||||
|
|
||||||
|
issue-message: |
|
||||||
|
👋 Hello @${{ github.actor }}, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ [Tutorials](https://github.com/ultralytics/yolov5/wiki#tutorials) to get started, where you can find quickstart guides for simple tasks like [Custom Data Training](https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data) all the way to advanced concepts like [Hyperparameter Evolution](https://github.com/ultralytics/yolov5/issues/607).
|
||||||
|
|
||||||
|
If this is a 🐛 Bug Report, please provide screenshots and **minimum viable code to reproduce your issue**, otherwise we can not help you.
|
||||||
|
|
||||||
|
If this is a custom training ❓ Question, please provide as much information as possible, including dataset images, training logs, screenshots, and a public link to online [W&B logging](https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data#visualize) if available.
|
||||||
|
|
||||||
|
For business inquiries or professional support requests please visit https://ultralytics.com or email support@ultralytics.com.
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
|
||||||
|
[**Python>=3.7.0**](https://www.python.org/) with all [requirements.txt](https://github.com/ultralytics/yolov5/blob/master/requirements.txt) installed including [**PyTorch>=1.7**](https://pytorch.org/get-started/locally/). To get started:
|
||||||
|
```bash
|
||||||
|
git clone https://github.com/ultralytics/yolov5 # clone
|
||||||
|
cd yolov5
|
||||||
|
pip install -r requirements.txt # install
|
||||||
|
```
|
||||||
|
|
||||||
|
## Environments
|
||||||
|
|
||||||
|
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):
|
||||||
|
|
||||||
|
- **Notebooks** with free GPU: <a href="https://bit.ly/yolov5-paperspace-notebook"><img src="https://assets.paperspace.io/img/gradient-badge.svg" alt="Run on Gradient"></a> <a href="https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"></a> <a href="https://www.kaggle.com/ultralytics/yolov5"><img src="https://kaggle.com/static/images/open-in-kaggle.svg" alt="Open In Kaggle"></a>
|
||||||
|
- **Google Cloud** Deep Learning VM. See [GCP Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/GCP-Quickstart)
|
||||||
|
- **Amazon** Deep Learning AMI. See [AWS Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/AWS-Quickstart)
|
||||||
|
- **Docker Image**. See [Docker Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/Docker-Quickstart) <a href="https://hub.docker.com/r/ultralytics/yolov5"><img src="https://img.shields.io/docker/pulls/ultralytics/yolov5?logo=docker" alt="Docker Pulls"></a>
|
||||||
|
|
||||||
|
## Status
|
||||||
|
|
||||||
|
<a href="https://github.com/ultralytics/yolov5/actions/workflows/ci-testing.yml"><img src="https://github.com/ultralytics/yolov5/actions/workflows/ci-testing.yml/badge.svg" alt="YOLOv5 CI"></a>
|
||||||
|
|
||||||
|
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](https://github.com/ultralytics/yolov5/blob/master/train.py), [validation](https://github.com/ultralytics/yolov5/blob/master/val.py), [inference](https://github.com/ultralytics/yolov5/blob/master/detect.py), [export](https://github.com/ultralytics/yolov5/blob/master/export.py) and [benchmarks](https://github.com/ultralytics/yolov5/blob/master/benchmarks.py) on MacOS, Windows, and Ubuntu every 24 hours and on every commit.
|
||||||
|
|
@ -0,0 +1,74 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Argoverse-HD dataset (ring-front-center camera) http://www.cs.cmu.edu/~mengtial/proj/streaming/ by Argo AI
|
||||||
|
# Example usage: python train.py --data Argoverse.yaml
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── Argoverse ← downloads here (31.3 GB)
|
||||||
|
|
||||||
|
|
||||||
|
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
|
||||||
|
path: ../datasets/Argoverse # dataset root dir
|
||||||
|
train: Argoverse-1.1/images/train/ # train images (relative to 'path') 39384 images
|
||||||
|
val: Argoverse-1.1/images/val/ # val images (relative to 'path') 15062 images
|
||||||
|
test: Argoverse-1.1/images/test/ # test images (optional) https://eval.ai/web/challenges/challenge-page/800/overview
|
||||||
|
|
||||||
|
# Classes
|
||||||
|
names:
|
||||||
|
0: person
|
||||||
|
1: bicycle
|
||||||
|
2: car
|
||||||
|
3: motorcycle
|
||||||
|
4: bus
|
||||||
|
5: truck
|
||||||
|
6: traffic_light
|
||||||
|
7: stop_sign
|
||||||
|
|
||||||
|
|
||||||
|
# Download script/URL (optional) ---------------------------------------------------------------------------------------
|
||||||
|
download: |
|
||||||
|
import json
|
||||||
|
|
||||||
|
from tqdm import tqdm
|
||||||
|
from utils.general import download, Path
|
||||||
|
|
||||||
|
|
||||||
|
def argoverse2yolo(set):
|
||||||
|
labels = {}
|
||||||
|
a = json.load(open(set, "rb"))
|
||||||
|
for annot in tqdm(a['annotations'], desc=f"Converting {set} to YOLOv5 format..."):
|
||||||
|
img_id = annot['image_id']
|
||||||
|
img_name = a['images'][img_id]['name']
|
||||||
|
img_label_name = f'{img_name[:-3]}txt'
|
||||||
|
|
||||||
|
cls = annot['category_id'] # instance class id
|
||||||
|
x_center, y_center, width, height = annot['bbox']
|
||||||
|
x_center = (x_center + width / 2) / 1920.0 # offset and scale
|
||||||
|
y_center = (y_center + height / 2) / 1200.0 # offset and scale
|
||||||
|
width /= 1920.0 # scale
|
||||||
|
height /= 1200.0 # scale
|
||||||
|
|
||||||
|
img_dir = set.parents[2] / 'Argoverse-1.1' / 'labels' / a['seq_dirs'][a['images'][annot['image_id']]['sid']]
|
||||||
|
if not img_dir.exists():
|
||||||
|
img_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
k = str(img_dir / img_label_name)
|
||||||
|
if k not in labels:
|
||||||
|
labels[k] = []
|
||||||
|
labels[k].append(f"{cls} {x_center} {y_center} {width} {height}\n")
|
||||||
|
|
||||||
|
for k in labels:
|
||||||
|
with open(k, "w") as f:
|
||||||
|
f.writelines(labels[k])
|
||||||
|
|
||||||
|
|
||||||
|
# Download
|
||||||
|
dir = Path(yaml['path']) # dataset root dir
|
||||||
|
urls = ['https://argoverse-hd.s3.us-east-2.amazonaws.com/Argoverse-HD-Full.zip']
|
||||||
|
download(urls, dir=dir, delete=False)
|
||||||
|
|
||||||
|
# Convert
|
||||||
|
annotations_dir = 'Argoverse-HD/annotations/'
|
||||||
|
(dir / 'Argoverse-1.1' / 'tracking').rename(dir / 'Argoverse-1.1' / 'images') # rename 'tracking' to 'images'
|
||||||
|
for d in "train.json", "val.json":
|
||||||
|
argoverse2yolo(dir / annotations_dir / d) # convert VisDrone annotations to YOLO labels
|
@ -0,0 +1,54 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Global Wheat 2020 dataset http://www.global-wheat.com/ by University of Saskatchewan
|
||||||
|
# Example usage: python train.py --data GlobalWheat2020.yaml
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── GlobalWheat2020 ← downloads here (7.0 GB)
|
||||||
|
|
||||||
|
|
||||||
|
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
|
||||||
|
path: ../datasets/GlobalWheat2020 # dataset root dir
|
||||||
|
train: # train images (relative to 'path') 3422 images
|
||||||
|
- images/arvalis_1
|
||||||
|
- images/arvalis_2
|
||||||
|
- images/arvalis_3
|
||||||
|
- images/ethz_1
|
||||||
|
- images/rres_1
|
||||||
|
- images/inrae_1
|
||||||
|
- images/usask_1
|
||||||
|
val: # val images (relative to 'path') 748 images (WARNING: train set contains ethz_1)
|
||||||
|
- images/ethz_1
|
||||||
|
test: # test images (optional) 1276 images
|
||||||
|
- images/utokyo_1
|
||||||
|
- images/utokyo_2
|
||||||
|
- images/nau_1
|
||||||
|
- images/uq_1
|
||||||
|
|
||||||
|
# Classes
|
||||||
|
names:
|
||||||
|
0: wheat_head
|
||||||
|
|
||||||
|
|
||||||
|
# Download script/URL (optional) ---------------------------------------------------------------------------------------
|
||||||
|
download: |
|
||||||
|
from utils.general import download, Path
|
||||||
|
|
||||||
|
|
||||||
|
# Download
|
||||||
|
dir = Path(yaml['path']) # dataset root dir
|
||||||
|
urls = ['https://zenodo.org/record/4298502/files/global-wheat-codalab-official.zip',
|
||||||
|
'https://github.com/ultralytics/yolov5/releases/download/v1.0/GlobalWheat2020_labels.zip']
|
||||||
|
download(urls, dir=dir)
|
||||||
|
|
||||||
|
# Make Directories
|
||||||
|
for p in 'annotations', 'images', 'labels':
|
||||||
|
(dir / p).mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# Move
|
||||||
|
for p in 'arvalis_1', 'arvalis_2', 'arvalis_3', 'ethz_1', 'rres_1', 'inrae_1', 'usask_1', \
|
||||||
|
'utokyo_1', 'utokyo_2', 'nau_1', 'uq_1':
|
||||||
|
(dir / p).rename(dir / 'images' / p) # move to /images
|
||||||
|
f = (dir / p).with_suffix('.json') # json file
|
||||||
|
if f.exists():
|
||||||
|
f.rename((dir / 'annotations' / p).with_suffix('.json')) # move to /annotations
|
@ -0,0 +1,438 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Objects365 dataset https://www.objects365.org/ by Megvii
|
||||||
|
# Example usage: python train.py --data Objects365.yaml
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── Objects365 ← downloads here (712 GB = 367G data + 345G zips)
|
||||||
|
|
||||||
|
|
||||||
|
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
|
||||||
|
path: ../datasets/Objects365 # dataset root dir
|
||||||
|
train: images/train # train images (relative to 'path') 1742289 images
|
||||||
|
val: images/val # val images (relative to 'path') 80000 images
|
||||||
|
test: # test images (optional)
|
||||||
|
|
||||||
|
# Classes
|
||||||
|
names:
|
||||||
|
0: Person
|
||||||
|
1: Sneakers
|
||||||
|
2: Chair
|
||||||
|
3: Other Shoes
|
||||||
|
4: Hat
|
||||||
|
5: Car
|
||||||
|
6: Lamp
|
||||||
|
7: Glasses
|
||||||
|
8: Bottle
|
||||||
|
9: Desk
|
||||||
|
10: Cup
|
||||||
|
11: Street Lights
|
||||||
|
12: Cabinet/shelf
|
||||||
|
13: Handbag/Satchel
|
||||||
|
14: Bracelet
|
||||||
|
15: Plate
|
||||||
|
16: Picture/Frame
|
||||||
|
17: Helmet
|
||||||
|
18: Book
|
||||||
|
19: Gloves
|
||||||
|
20: Storage box
|
||||||
|
21: Boat
|
||||||
|
22: Leather Shoes
|
||||||
|
23: Flower
|
||||||
|
24: Bench
|
||||||
|
25: Potted Plant
|
||||||
|
26: Bowl/Basin
|
||||||
|
27: Flag
|
||||||
|
28: Pillow
|
||||||
|
29: Boots
|
||||||
|
30: Vase
|
||||||
|
31: Microphone
|
||||||
|
32: Necklace
|
||||||
|
33: Ring
|
||||||
|
34: SUV
|
||||||
|
35: Wine Glass
|
||||||
|
36: Belt
|
||||||
|
37: Monitor/TV
|
||||||
|
38: Backpack
|
||||||
|
39: Umbrella
|
||||||
|
40: Traffic Light
|
||||||
|
41: Speaker
|
||||||
|
42: Watch
|
||||||
|
43: Tie
|
||||||
|
44: Trash bin Can
|
||||||
|
45: Slippers
|
||||||
|
46: Bicycle
|
||||||
|
47: Stool
|
||||||
|
48: Barrel/bucket
|
||||||
|
49: Van
|
||||||
|
50: Couch
|
||||||
|
51: Sandals
|
||||||
|
52: Basket
|
||||||
|
53: Drum
|
||||||
|
54: Pen/Pencil
|
||||||
|
55: Bus
|
||||||
|
56: Wild Bird
|
||||||
|
57: High Heels
|
||||||
|
58: Motorcycle
|
||||||
|
59: Guitar
|
||||||
|
60: Carpet
|
||||||
|
61: Cell Phone
|
||||||
|
62: Bread
|
||||||
|
63: Camera
|
||||||
|
64: Canned
|
||||||
|
65: Truck
|
||||||
|
66: Traffic cone
|
||||||
|
67: Cymbal
|
||||||
|
68: Lifesaver
|
||||||
|
69: Towel
|
||||||
|
70: Stuffed Toy
|
||||||
|
71: Candle
|
||||||
|
72: Sailboat
|
||||||
|
73: Laptop
|
||||||
|
74: Awning
|
||||||
|
75: Bed
|
||||||
|
76: Faucet
|
||||||
|
77: Tent
|
||||||
|
78: Horse
|
||||||
|
79: Mirror
|
||||||
|
80: Power outlet
|
||||||
|
81: Sink
|
||||||
|
82: Apple
|
||||||
|
83: Air Conditioner
|
||||||
|
84: Knife
|
||||||
|
85: Hockey Stick
|
||||||
|
86: Paddle
|
||||||
|
87: Pickup Truck
|
||||||
|
88: Fork
|
||||||
|
89: Traffic Sign
|
||||||
|
90: Balloon
|
||||||
|
91: Tripod
|
||||||
|
92: Dog
|
||||||
|
93: Spoon
|
||||||
|
94: Clock
|
||||||
|
95: Pot
|
||||||
|
96: Cow
|
||||||
|
97: Cake
|
||||||
|
98: Dinning Table
|
||||||
|
99: Sheep
|
||||||
|
100: Hanger
|
||||||
|
101: Blackboard/Whiteboard
|
||||||
|
102: Napkin
|
||||||
|
103: Other Fish
|
||||||
|
104: Orange/Tangerine
|
||||||
|
105: Toiletry
|
||||||
|
106: Keyboard
|
||||||
|
107: Tomato
|
||||||
|
108: Lantern
|
||||||
|
109: Machinery Vehicle
|
||||||
|
110: Fan
|
||||||
|
111: Green Vegetables
|
||||||
|
112: Banana
|
||||||
|
113: Baseball Glove
|
||||||
|
114: Airplane
|
||||||
|
115: Mouse
|
||||||
|
116: Train
|
||||||
|
117: Pumpkin
|
||||||
|
118: Soccer
|
||||||
|
119: Skiboard
|
||||||
|
120: Luggage
|
||||||
|
121: Nightstand
|
||||||
|
122: Tea pot
|
||||||
|
123: Telephone
|
||||||
|
124: Trolley
|
||||||
|
125: Head Phone
|
||||||
|
126: Sports Car
|
||||||
|
127: Stop Sign
|
||||||
|
128: Dessert
|
||||||
|
129: Scooter
|
||||||
|
130: Stroller
|
||||||
|
131: Crane
|
||||||
|
132: Remote
|
||||||
|
133: Refrigerator
|
||||||
|
134: Oven
|
||||||
|
135: Lemon
|
||||||
|
136: Duck
|
||||||
|
137: Baseball Bat
|
||||||
|
138: Surveillance Camera
|
||||||
|
139: Cat
|
||||||
|
140: Jug
|
||||||
|
141: Broccoli
|
||||||
|
142: Piano
|
||||||
|
143: Pizza
|
||||||
|
144: Elephant
|
||||||
|
145: Skateboard
|
||||||
|
146: Surfboard
|
||||||
|
147: Gun
|
||||||
|
148: Skating and Skiing shoes
|
||||||
|
149: Gas stove
|
||||||
|
150: Donut
|
||||||
|
151: Bow Tie
|
||||||
|
152: Carrot
|
||||||
|
153: Toilet
|
||||||
|
154: Kite
|
||||||
|
155: Strawberry
|
||||||
|
156: Other Balls
|
||||||
|
157: Shovel
|
||||||
|
158: Pepper
|
||||||
|
159: Computer Box
|
||||||
|
160: Toilet Paper
|
||||||
|
161: Cleaning Products
|
||||||
|
162: Chopsticks
|
||||||
|
163: Microwave
|
||||||
|
164: Pigeon
|
||||||
|
165: Baseball
|
||||||
|
166: Cutting/chopping Board
|
||||||
|
167: Coffee Table
|
||||||
|
168: Side Table
|
||||||
|
169: Scissors
|
||||||
|
170: Marker
|
||||||
|
171: Pie
|
||||||
|
172: Ladder
|
||||||
|
173: Snowboard
|
||||||
|
174: Cookies
|
||||||
|
175: Radiator
|
||||||
|
176: Fire Hydrant
|
||||||
|
177: Basketball
|
||||||
|
178: Zebra
|
||||||
|
179: Grape
|
||||||
|
180: Giraffe
|
||||||
|
181: Potato
|
||||||
|
182: Sausage
|
||||||
|
183: Tricycle
|
||||||
|
184: Violin
|
||||||
|
185: Egg
|
||||||
|
186: Fire Extinguisher
|
||||||
|
187: Candy
|
||||||
|
188: Fire Truck
|
||||||
|
189: Billiards
|
||||||
|
190: Converter
|
||||||
|
191: Bathtub
|
||||||
|
192: Wheelchair
|
||||||
|
193: Golf Club
|
||||||
|
194: Briefcase
|
||||||
|
195: Cucumber
|
||||||
|
196: Cigar/Cigarette
|
||||||
|
197: Paint Brush
|
||||||
|
198: Pear
|
||||||
|
199: Heavy Truck
|
||||||
|
200: Hamburger
|
||||||
|
201: Extractor
|
||||||
|
202: Extension Cord
|
||||||
|
203: Tong
|
||||||
|
204: Tennis Racket
|
||||||
|
205: Folder
|
||||||
|
206: American Football
|
||||||
|
207: earphone
|
||||||
|
208: Mask
|
||||||
|
209: Kettle
|
||||||
|
210: Tennis
|
||||||
|
211: Ship
|
||||||
|
212: Swing
|
||||||
|
213: Coffee Machine
|
||||||
|
214: Slide
|
||||||
|
215: Carriage
|
||||||
|
216: Onion
|
||||||
|
217: Green beans
|
||||||
|
218: Projector
|
||||||
|
219: Frisbee
|
||||||
|
220: Washing Machine/Drying Machine
|
||||||
|
221: Chicken
|
||||||
|
222: Printer
|
||||||
|
223: Watermelon
|
||||||
|
224: Saxophone
|
||||||
|
225: Tissue
|
||||||
|
226: Toothbrush
|
||||||
|
227: Ice cream
|
||||||
|
228: Hot-air balloon
|
||||||
|
229: Cello
|
||||||
|
230: French Fries
|
||||||
|
231: Scale
|
||||||
|
232: Trophy
|
||||||
|
233: Cabbage
|
||||||
|
234: Hot dog
|
||||||
|
235: Blender
|
||||||
|
236: Peach
|
||||||
|
237: Rice
|
||||||
|
238: Wallet/Purse
|
||||||
|
239: Volleyball
|
||||||
|
240: Deer
|
||||||
|
241: Goose
|
||||||
|
242: Tape
|
||||||
|
243: Tablet
|
||||||
|
244: Cosmetics
|
||||||
|
245: Trumpet
|
||||||
|
246: Pineapple
|
||||||
|
247: Golf Ball
|
||||||
|
248: Ambulance
|
||||||
|
249: Parking meter
|
||||||
|
250: Mango
|
||||||
|
251: Key
|
||||||
|
252: Hurdle
|
||||||
|
253: Fishing Rod
|
||||||
|
254: Medal
|
||||||
|
255: Flute
|
||||||
|
256: Brush
|
||||||
|
257: Penguin
|
||||||
|
258: Megaphone
|
||||||
|
259: Corn
|
||||||
|
260: Lettuce
|
||||||
|
261: Garlic
|
||||||
|
262: Swan
|
||||||
|
263: Helicopter
|
||||||
|
264: Green Onion
|
||||||
|
265: Sandwich
|
||||||
|
266: Nuts
|
||||||
|
267: Speed Limit Sign
|
||||||
|
268: Induction Cooker
|
||||||
|
269: Broom
|
||||||
|
270: Trombone
|
||||||
|
271: Plum
|
||||||
|
272: Rickshaw
|
||||||
|
273: Goldfish
|
||||||
|
274: Kiwi fruit
|
||||||
|
275: Router/modem
|
||||||
|
276: Poker Card
|
||||||
|
277: Toaster
|
||||||
|
278: Shrimp
|
||||||
|
279: Sushi
|
||||||
|
280: Cheese
|
||||||
|
281: Notepaper
|
||||||
|
282: Cherry
|
||||||
|
283: Pliers
|
||||||
|
284: CD
|
||||||
|
285: Pasta
|
||||||
|
286: Hammer
|
||||||
|
287: Cue
|
||||||
|
288: Avocado
|
||||||
|
289: Hamimelon
|
||||||
|
290: Flask
|
||||||
|
291: Mushroom
|
||||||
|
292: Screwdriver
|
||||||
|
293: Soap
|
||||||
|
294: Recorder
|
||||||
|
295: Bear
|
||||||
|
296: Eggplant
|
||||||
|
297: Board Eraser
|
||||||
|
298: Coconut
|
||||||
|
299: Tape Measure/Ruler
|
||||||
|
300: Pig
|
||||||
|
301: Showerhead
|
||||||
|
302: Globe
|
||||||
|
303: Chips
|
||||||
|
304: Steak
|
||||||
|
305: Crosswalk Sign
|
||||||
|
306: Stapler
|
||||||
|
307: Camel
|
||||||
|
308: Formula 1
|
||||||
|
309: Pomegranate
|
||||||
|
310: Dishwasher
|
||||||
|
311: Crab
|
||||||
|
312: Hoverboard
|
||||||
|
313: Meat ball
|
||||||
|
314: Rice Cooker
|
||||||
|
315: Tuba
|
||||||
|
316: Calculator
|
||||||
|
317: Papaya
|
||||||
|
318: Antelope
|
||||||
|
319: Parrot
|
||||||
|
320: Seal
|
||||||
|
321: Butterfly
|
||||||
|
322: Dumbbell
|
||||||
|
323: Donkey
|
||||||
|
324: Lion
|
||||||
|
325: Urinal
|
||||||
|
326: Dolphin
|
||||||
|
327: Electric Drill
|
||||||
|
328: Hair Dryer
|
||||||
|
329: Egg tart
|
||||||
|
330: Jellyfish
|
||||||
|
331: Treadmill
|
||||||
|
332: Lighter
|
||||||
|
333: Grapefruit
|
||||||
|
334: Game board
|
||||||
|
335: Mop
|
||||||
|
336: Radish
|
||||||
|
337: Baozi
|
||||||
|
338: Target
|
||||||
|
339: French
|
||||||
|
340: Spring Rolls
|
||||||
|
341: Monkey
|
||||||
|
342: Rabbit
|
||||||
|
343: Pencil Case
|
||||||
|
344: Yak
|
||||||
|
345: Red Cabbage
|
||||||
|
346: Binoculars
|
||||||
|
347: Asparagus
|
||||||
|
348: Barbell
|
||||||
|
349: Scallop
|
||||||
|
350: Noddles
|
||||||
|
351: Comb
|
||||||
|
352: Dumpling
|
||||||
|
353: Oyster
|
||||||
|
354: Table Tennis paddle
|
||||||
|
355: Cosmetics Brush/Eyeliner Pencil
|
||||||
|
356: Chainsaw
|
||||||
|
357: Eraser
|
||||||
|
358: Lobster
|
||||||
|
359: Durian
|
||||||
|
360: Okra
|
||||||
|
361: Lipstick
|
||||||
|
362: Cosmetics Mirror
|
||||||
|
363: Curling
|
||||||
|
364: Table Tennis
|
||||||
|
|
||||||
|
|
||||||
|
# Download script/URL (optional) ---------------------------------------------------------------------------------------
|
||||||
|
download: |
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
from utils.general import Path, check_requirements, download, np, xyxy2xywhn
|
||||||
|
|
||||||
|
check_requirements(('pycocotools>=2.0',))
|
||||||
|
from pycocotools.coco import COCO
|
||||||
|
|
||||||
|
# Make Directories
|
||||||
|
dir = Path(yaml['path']) # dataset root dir
|
||||||
|
for p in 'images', 'labels':
|
||||||
|
(dir / p).mkdir(parents=True, exist_ok=True)
|
||||||
|
for q in 'train', 'val':
|
||||||
|
(dir / p / q).mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# Train, Val Splits
|
||||||
|
for split, patches in [('train', 50 + 1), ('val', 43 + 1)]:
|
||||||
|
print(f"Processing {split} in {patches} patches ...")
|
||||||
|
images, labels = dir / 'images' / split, dir / 'labels' / split
|
||||||
|
|
||||||
|
# Download
|
||||||
|
url = f"https://dorc.ks3-cn-beijing.ksyun.com/data-set/2020Objects365%E6%95%B0%E6%8D%AE%E9%9B%86/{split}/"
|
||||||
|
if split == 'train':
|
||||||
|
download([f'{url}zhiyuan_objv2_{split}.tar.gz'], dir=dir, delete=False) # annotations json
|
||||||
|
download([f'{url}patch{i}.tar.gz' for i in range(patches)], dir=images, curl=True, delete=False, threads=8)
|
||||||
|
elif split == 'val':
|
||||||
|
download([f'{url}zhiyuan_objv2_{split}.json'], dir=dir, delete=False) # annotations json
|
||||||
|
download([f'{url}images/v1/patch{i}.tar.gz' for i in range(15 + 1)], dir=images, curl=True, delete=False, threads=8)
|
||||||
|
download([f'{url}images/v2/patch{i}.tar.gz' for i in range(16, patches)], dir=images, curl=True, delete=False, threads=8)
|
||||||
|
|
||||||
|
# Move
|
||||||
|
for f in tqdm(images.rglob('*.jpg'), desc=f'Moving {split} images'):
|
||||||
|
f.rename(images / f.name) # move to /images/{split}
|
||||||
|
|
||||||
|
# Labels
|
||||||
|
coco = COCO(dir / f'zhiyuan_objv2_{split}.json')
|
||||||
|
names = [x["name"] for x in coco.loadCats(coco.getCatIds())]
|
||||||
|
for cid, cat in enumerate(names):
|
||||||
|
catIds = coco.getCatIds(catNms=[cat])
|
||||||
|
imgIds = coco.getImgIds(catIds=catIds)
|
||||||
|
for im in tqdm(coco.loadImgs(imgIds), desc=f'Class {cid + 1}/{len(names)} {cat}'):
|
||||||
|
width, height = im["width"], im["height"]
|
||||||
|
path = Path(im["file_name"]) # image filename
|
||||||
|
try:
|
||||||
|
with open(labels / path.with_suffix('.txt').name, 'a') as file:
|
||||||
|
annIds = coco.getAnnIds(imgIds=im["id"], catIds=catIds, iscrowd=None)
|
||||||
|
for a in coco.loadAnns(annIds):
|
||||||
|
x, y, w, h = a['bbox'] # bounding box in xywh (xy top-left corner)
|
||||||
|
xyxy = np.array([x, y, x + w, y + h])[None] # pixels(1,4)
|
||||||
|
x, y, w, h = xyxy2xywhn(xyxy, w=width, h=height, clip=True)[0] # normalized and clipped
|
||||||
|
file.write(f"{cid} {x:.5f} {y:.5f} {w:.5f} {h:.5f}\n")
|
||||||
|
except Exception as e:
|
||||||
|
print(e)
|
@ -0,0 +1,53 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# SKU-110K retail items dataset https://github.com/eg4000/SKU110K_CVPR19 by Trax Retail
|
||||||
|
# Example usage: python train.py --data SKU-110K.yaml
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── SKU-110K ← downloads here (13.6 GB)
|
||||||
|
|
||||||
|
|
||||||
|
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
|
||||||
|
path: ../datasets/SKU-110K # dataset root dir
|
||||||
|
train: train.txt # train images (relative to 'path') 8219 images
|
||||||
|
val: val.txt # val images (relative to 'path') 588 images
|
||||||
|
test: test.txt # test images (optional) 2936 images
|
||||||
|
|
||||||
|
# Classes
|
||||||
|
names:
|
||||||
|
0: object
|
||||||
|
|
||||||
|
|
||||||
|
# Download script/URL (optional) ---------------------------------------------------------------------------------------
|
||||||
|
download: |
|
||||||
|
import shutil
|
||||||
|
from tqdm import tqdm
|
||||||
|
from utils.general import np, pd, Path, download, xyxy2xywh
|
||||||
|
|
||||||
|
|
||||||
|
# Download
|
||||||
|
dir = Path(yaml['path']) # dataset root dir
|
||||||
|
parent = Path(dir.parent) # download dir
|
||||||
|
urls = ['http://trax-geometry.s3.amazonaws.com/cvpr_challenge/SKU110K_fixed.tar.gz']
|
||||||
|
download(urls, dir=parent, delete=False)
|
||||||
|
|
||||||
|
# Rename directories
|
||||||
|
if dir.exists():
|
||||||
|
shutil.rmtree(dir)
|
||||||
|
(parent / 'SKU110K_fixed').rename(dir) # rename dir
|
||||||
|
(dir / 'labels').mkdir(parents=True, exist_ok=True) # create labels dir
|
||||||
|
|
||||||
|
# Convert labels
|
||||||
|
names = 'image', 'x1', 'y1', 'x2', 'y2', 'class', 'image_width', 'image_height' # column names
|
||||||
|
for d in 'annotations_train.csv', 'annotations_val.csv', 'annotations_test.csv':
|
||||||
|
x = pd.read_csv(dir / 'annotations' / d, names=names).values # annotations
|
||||||
|
images, unique_images = x[:, 0], np.unique(x[:, 0])
|
||||||
|
with open((dir / d).with_suffix('.txt').__str__().replace('annotations_', ''), 'w') as f:
|
||||||
|
f.writelines(f'./images/{s}\n' for s in unique_images)
|
||||||
|
for im in tqdm(unique_images, desc=f'Converting {dir / d}'):
|
||||||
|
cls = 0 # single-class dataset
|
||||||
|
with open((dir / 'labels' / im).with_suffix('.txt'), 'a') as f:
|
||||||
|
for r in x[images == im]:
|
||||||
|
w, h = r[6], r[7] # image width, height
|
||||||
|
xywh = xyxy2xywh(np.array([[r[1] / w, r[2] / h, r[3] / w, r[4] / h]]))[0] # instance
|
||||||
|
f.write(f"{cls} {xywh[0]:.5f} {xywh[1]:.5f} {xywh[2]:.5f} {xywh[3]:.5f}\n") # write label
|
@ -0,0 +1,100 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC by University of Oxford
|
||||||
|
# Example usage: python train.py --data VOC.yaml
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── VOC ← downloads here (2.8 GB)
|
||||||
|
|
||||||
|
|
||||||
|
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
|
||||||
|
path: ../datasets/VOC
|
||||||
|
train: # train images (relative to 'path') 16551 images
|
||||||
|
- images/train2012
|
||||||
|
- images/train2007
|
||||||
|
- images/val2012
|
||||||
|
- images/val2007
|
||||||
|
val: # val images (relative to 'path') 4952 images
|
||||||
|
- images/test2007
|
||||||
|
test: # test images (optional)
|
||||||
|
- images/test2007
|
||||||
|
|
||||||
|
# Classes
|
||||||
|
names:
|
||||||
|
0: aeroplane
|
||||||
|
1: bicycle
|
||||||
|
2: bird
|
||||||
|
3: boat
|
||||||
|
4: bottle
|
||||||
|
5: bus
|
||||||
|
6: car
|
||||||
|
7: cat
|
||||||
|
8: chair
|
||||||
|
9: cow
|
||||||
|
10: diningtable
|
||||||
|
11: dog
|
||||||
|
12: horse
|
||||||
|
13: motorbike
|
||||||
|
14: person
|
||||||
|
15: pottedplant
|
||||||
|
16: sheep
|
||||||
|
17: sofa
|
||||||
|
18: train
|
||||||
|
19: tvmonitor
|
||||||
|
|
||||||
|
|
||||||
|
# Download script/URL (optional) ---------------------------------------------------------------------------------------
|
||||||
|
download: |
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
|
from tqdm import tqdm
|
||||||
|
from utils.general import download, Path
|
||||||
|
|
||||||
|
|
||||||
|
def convert_label(path, lb_path, year, image_id):
|
||||||
|
def convert_box(size, box):
|
||||||
|
dw, dh = 1. / size[0], 1. / size[1]
|
||||||
|
x, y, w, h = (box[0] + box[1]) / 2.0 - 1, (box[2] + box[3]) / 2.0 - 1, box[1] - box[0], box[3] - box[2]
|
||||||
|
return x * dw, y * dh, w * dw, h * dh
|
||||||
|
|
||||||
|
in_file = open(path / f'VOC{year}/Annotations/{image_id}.xml')
|
||||||
|
out_file = open(lb_path, 'w')
|
||||||
|
tree = ET.parse(in_file)
|
||||||
|
root = tree.getroot()
|
||||||
|
size = root.find('size')
|
||||||
|
w = int(size.find('width').text)
|
||||||
|
h = int(size.find('height').text)
|
||||||
|
|
||||||
|
names = list(yaml['names'].values()) # names list
|
||||||
|
for obj in root.iter('object'):
|
||||||
|
cls = obj.find('name').text
|
||||||
|
if cls in names and int(obj.find('difficult').text) != 1:
|
||||||
|
xmlbox = obj.find('bndbox')
|
||||||
|
bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')])
|
||||||
|
cls_id = names.index(cls) # class id
|
||||||
|
out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n')
|
||||||
|
|
||||||
|
|
||||||
|
# Download
|
||||||
|
dir = Path(yaml['path']) # dataset root dir
|
||||||
|
url = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/'
|
||||||
|
urls = [f'{url}VOCtrainval_06-Nov-2007.zip', # 446MB, 5012 images
|
||||||
|
f'{url}VOCtest_06-Nov-2007.zip', # 438MB, 4953 images
|
||||||
|
f'{url}VOCtrainval_11-May-2012.zip'] # 1.95GB, 17126 images
|
||||||
|
download(urls, dir=dir / 'images', delete=False, curl=True, threads=3)
|
||||||
|
|
||||||
|
# Convert
|
||||||
|
path = dir / 'images/VOCdevkit'
|
||||||
|
for year, image_set in ('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test'):
|
||||||
|
imgs_path = dir / 'images' / f'{image_set}{year}'
|
||||||
|
lbs_path = dir / 'labels' / f'{image_set}{year}'
|
||||||
|
imgs_path.mkdir(exist_ok=True, parents=True)
|
||||||
|
lbs_path.mkdir(exist_ok=True, parents=True)
|
||||||
|
|
||||||
|
with open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt') as f:
|
||||||
|
image_ids = f.read().strip().split()
|
||||||
|
for id in tqdm(image_ids, desc=f'{image_set}{year}'):
|
||||||
|
f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path
|
||||||
|
lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path
|
||||||
|
f.rename(imgs_path / f.name) # move image
|
||||||
|
convert_label(path, lb_path, year, id) # convert labels to YOLO format
|
@ -0,0 +1,70 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# VisDrone2019-DET dataset https://github.com/VisDrone/VisDrone-Dataset by Tianjin University
|
||||||
|
# Example usage: python train.py --data VisDrone.yaml
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── VisDrone ← downloads here (2.3 GB)
|
||||||
|
|
||||||
|
|
||||||
|
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
|
||||||
|
path: ../datasets/VisDrone # dataset root dir
|
||||||
|
train: VisDrone2019-DET-train/images # train images (relative to 'path') 6471 images
|
||||||
|
val: VisDrone2019-DET-val/images # val images (relative to 'path') 548 images
|
||||||
|
test: VisDrone2019-DET-test-dev/images # test images (optional) 1610 images
|
||||||
|
|
||||||
|
# Classes
|
||||||
|
names:
|
||||||
|
0: pedestrian
|
||||||
|
1: people
|
||||||
|
2: bicycle
|
||||||
|
3: car
|
||||||
|
4: van
|
||||||
|
5: truck
|
||||||
|
6: tricycle
|
||||||
|
7: awning-tricycle
|
||||||
|
8: bus
|
||||||
|
9: motor
|
||||||
|
|
||||||
|
|
||||||
|
# Download script/URL (optional) ---------------------------------------------------------------------------------------
|
||||||
|
download: |
|
||||||
|
from utils.general import download, os, Path
|
||||||
|
|
||||||
|
def visdrone2yolo(dir):
|
||||||
|
from PIL import Image
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
def convert_box(size, box):
|
||||||
|
# Convert VisDrone box to YOLO xywh box
|
||||||
|
dw = 1. / size[0]
|
||||||
|
dh = 1. / size[1]
|
||||||
|
return (box[0] + box[2] / 2) * dw, (box[1] + box[3] / 2) * dh, box[2] * dw, box[3] * dh
|
||||||
|
|
||||||
|
(dir / 'labels').mkdir(parents=True, exist_ok=True) # make labels directory
|
||||||
|
pbar = tqdm((dir / 'annotations').glob('*.txt'), desc=f'Converting {dir}')
|
||||||
|
for f in pbar:
|
||||||
|
img_size = Image.open((dir / 'images' / f.name).with_suffix('.jpg')).size
|
||||||
|
lines = []
|
||||||
|
with open(f, 'r') as file: # read annotation.txt
|
||||||
|
for row in [x.split(',') for x in file.read().strip().splitlines()]:
|
||||||
|
if row[4] == '0': # VisDrone 'ignored regions' class 0
|
||||||
|
continue
|
||||||
|
cls = int(row[5]) - 1
|
||||||
|
box = convert_box(img_size, tuple(map(int, row[:4])))
|
||||||
|
lines.append(f"{cls} {' '.join(f'{x:.6f}' for x in box)}\n")
|
||||||
|
with open(str(f).replace(os.sep + 'annotations' + os.sep, os.sep + 'labels' + os.sep), 'w') as fl:
|
||||||
|
fl.writelines(lines) # write label.txt
|
||||||
|
|
||||||
|
|
||||||
|
# Download
|
||||||
|
dir = Path(yaml['path']) # dataset root dir
|
||||||
|
urls = ['https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-train.zip',
|
||||||
|
'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-val.zip',
|
||||||
|
'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-dev.zip',
|
||||||
|
'https://github.com/ultralytics/yolov5/releases/download/v1.0/VisDrone2019-DET-test-challenge.zip']
|
||||||
|
download(urls, dir=dir, curl=True, threads=4)
|
||||||
|
|
||||||
|
# Convert
|
||||||
|
for d in 'VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev':
|
||||||
|
visdrone2yolo(dir / d) # convert VisDrone annotations to YOLO labels
|
@ -0,0 +1,116 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# COCO 2017 dataset http://cocodataset.org by Microsoft
|
||||||
|
# Example usage: python train.py --data coco.yaml
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── coco ← downloads here (20.1 GB)
|
||||||
|
|
||||||
|
|
||||||
|
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
|
||||||
|
path: ../datasets/coco # dataset root dir
|
||||||
|
train: train2017.txt # train images (relative to 'path') 118287 images
|
||||||
|
val: val2017.txt # val images (relative to 'path') 5000 images
|
||||||
|
test: test-dev2017.txt # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794
|
||||||
|
|
||||||
|
# Classes
|
||||||
|
names:
|
||||||
|
0: person
|
||||||
|
1: bicycle
|
||||||
|
2: car
|
||||||
|
3: motorcycle
|
||||||
|
4: airplane
|
||||||
|
5: bus
|
||||||
|
6: train
|
||||||
|
7: truck
|
||||||
|
8: boat
|
||||||
|
9: traffic light
|
||||||
|
10: fire hydrant
|
||||||
|
11: stop sign
|
||||||
|
12: parking meter
|
||||||
|
13: bench
|
||||||
|
14: bird
|
||||||
|
15: cat
|
||||||
|
16: dog
|
||||||
|
17: horse
|
||||||
|
18: sheep
|
||||||
|
19: cow
|
||||||
|
20: elephant
|
||||||
|
21: bear
|
||||||
|
22: zebra
|
||||||
|
23: giraffe
|
||||||
|
24: backpack
|
||||||
|
25: umbrella
|
||||||
|
26: handbag
|
||||||
|
27: tie
|
||||||
|
28: suitcase
|
||||||
|
29: frisbee
|
||||||
|
30: skis
|
||||||
|
31: snowboard
|
||||||
|
32: sports ball
|
||||||
|
33: kite
|
||||||
|
34: baseball bat
|
||||||
|
35: baseball glove
|
||||||
|
36: skateboard
|
||||||
|
37: surfboard
|
||||||
|
38: tennis racket
|
||||||
|
39: bottle
|
||||||
|
40: wine glass
|
||||||
|
41: cup
|
||||||
|
42: fork
|
||||||
|
43: knife
|
||||||
|
44: spoon
|
||||||
|
45: bowl
|
||||||
|
46: banana
|
||||||
|
47: apple
|
||||||
|
48: sandwich
|
||||||
|
49: orange
|
||||||
|
50: broccoli
|
||||||
|
51: carrot
|
||||||
|
52: hot dog
|
||||||
|
53: pizza
|
||||||
|
54: donut
|
||||||
|
55: cake
|
||||||
|
56: chair
|
||||||
|
57: couch
|
||||||
|
58: potted plant
|
||||||
|
59: bed
|
||||||
|
60: dining table
|
||||||
|
61: toilet
|
||||||
|
62: tv
|
||||||
|
63: laptop
|
||||||
|
64: mouse
|
||||||
|
65: remote
|
||||||
|
66: keyboard
|
||||||
|
67: cell phone
|
||||||
|
68: microwave
|
||||||
|
69: oven
|
||||||
|
70: toaster
|
||||||
|
71: sink
|
||||||
|
72: refrigerator
|
||||||
|
73: book
|
||||||
|
74: clock
|
||||||
|
75: vase
|
||||||
|
76: scissors
|
||||||
|
77: teddy bear
|
||||||
|
78: hair drier
|
||||||
|
79: toothbrush
|
||||||
|
|
||||||
|
|
||||||
|
# Download script/URL (optional)
|
||||||
|
download: |
|
||||||
|
from utils.general import download, Path
|
||||||
|
|
||||||
|
|
||||||
|
# Download labels
|
||||||
|
segments = False # segment or box labels
|
||||||
|
dir = Path(yaml['path']) # dataset root dir
|
||||||
|
url = 'https://github.com/ultralytics/yolov5/releases/download/v1.0/'
|
||||||
|
urls = [url + ('coco2017labels-segments.zip' if segments else 'coco2017labels.zip')] # labels
|
||||||
|
download(urls, dir=dir.parent)
|
||||||
|
|
||||||
|
# Download data
|
||||||
|
urls = ['http://images.cocodataset.org/zips/train2017.zip', # 19G, 118k images
|
||||||
|
'http://images.cocodataset.org/zips/val2017.zip', # 1G, 5k images
|
||||||
|
'http://images.cocodataset.org/zips/test2017.zip'] # 7G, 41k images (optional)
|
||||||
|
download(urls, dir=dir / 'images', threads=3)
|
@ -0,0 +1,101 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# COCO128-seg dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017) by Ultralytics
|
||||||
|
# Example usage: python train.py --data coco128.yaml
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── coco128-seg ← downloads here (7 MB)
|
||||||
|
|
||||||
|
|
||||||
|
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
|
||||||
|
path: ../datasets/coco128-seg # dataset root dir
|
||||||
|
train: images/train2017 # train images (relative to 'path') 128 images
|
||||||
|
val: images/train2017 # val images (relative to 'path') 128 images
|
||||||
|
test: # test images (optional)
|
||||||
|
|
||||||
|
# Classes
|
||||||
|
names:
|
||||||
|
0: person
|
||||||
|
1: bicycle
|
||||||
|
2: car
|
||||||
|
3: motorcycle
|
||||||
|
4: airplane
|
||||||
|
5: bus
|
||||||
|
6: train
|
||||||
|
7: truck
|
||||||
|
8: boat
|
||||||
|
9: traffic light
|
||||||
|
10: fire hydrant
|
||||||
|
11: stop sign
|
||||||
|
12: parking meter
|
||||||
|
13: bench
|
||||||
|
14: bird
|
||||||
|
15: cat
|
||||||
|
16: dog
|
||||||
|
17: horse
|
||||||
|
18: sheep
|
||||||
|
19: cow
|
||||||
|
20: elephant
|
||||||
|
21: bear
|
||||||
|
22: zebra
|
||||||
|
23: giraffe
|
||||||
|
24: backpack
|
||||||
|
25: umbrella
|
||||||
|
26: handbag
|
||||||
|
27: tie
|
||||||
|
28: suitcase
|
||||||
|
29: frisbee
|
||||||
|
30: skis
|
||||||
|
31: snowboard
|
||||||
|
32: sports ball
|
||||||
|
33: kite
|
||||||
|
34: baseball bat
|
||||||
|
35: baseball glove
|
||||||
|
36: skateboard
|
||||||
|
37: surfboard
|
||||||
|
38: tennis racket
|
||||||
|
39: bottle
|
||||||
|
40: wine glass
|
||||||
|
41: cup
|
||||||
|
42: fork
|
||||||
|
43: knife
|
||||||
|
44: spoon
|
||||||
|
45: bowl
|
||||||
|
46: banana
|
||||||
|
47: apple
|
||||||
|
48: sandwich
|
||||||
|
49: orange
|
||||||
|
50: broccoli
|
||||||
|
51: carrot
|
||||||
|
52: hot dog
|
||||||
|
53: pizza
|
||||||
|
54: donut
|
||||||
|
55: cake
|
||||||
|
56: chair
|
||||||
|
57: couch
|
||||||
|
58: potted plant
|
||||||
|
59: bed
|
||||||
|
60: dining table
|
||||||
|
61: toilet
|
||||||
|
62: tv
|
||||||
|
63: laptop
|
||||||
|
64: mouse
|
||||||
|
65: remote
|
||||||
|
66: keyboard
|
||||||
|
67: cell phone
|
||||||
|
68: microwave
|
||||||
|
69: oven
|
||||||
|
70: toaster
|
||||||
|
71: sink
|
||||||
|
72: refrigerator
|
||||||
|
73: book
|
||||||
|
74: clock
|
||||||
|
75: vase
|
||||||
|
76: scissors
|
||||||
|
77: teddy bear
|
||||||
|
78: hair drier
|
||||||
|
79: toothbrush
|
||||||
|
|
||||||
|
|
||||||
|
# Download script/URL (optional)
|
||||||
|
download: https://ultralytics.com/assets/coco128-seg.zip
|
@ -0,0 +1,101 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# COCO128 dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017) by Ultralytics
|
||||||
|
# Example usage: python train.py --data coco128.yaml
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── coco128 ← downloads here (7 MB)
|
||||||
|
|
||||||
|
|
||||||
|
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
|
||||||
|
path: ../datasets/coco128 # dataset root dir
|
||||||
|
train: images/train2017 # train images (relative to 'path') 128 images
|
||||||
|
val: images/train2017 # val images (relative to 'path') 128 images
|
||||||
|
test: # test images (optional)
|
||||||
|
|
||||||
|
# Classes
|
||||||
|
names:
|
||||||
|
0: person
|
||||||
|
1: bicycle
|
||||||
|
2: car
|
||||||
|
3: motorcycle
|
||||||
|
4: airplane
|
||||||
|
5: bus
|
||||||
|
6: train
|
||||||
|
7: truck
|
||||||
|
8: boat
|
||||||
|
9: traffic light
|
||||||
|
10: fire hydrant
|
||||||
|
11: stop sign
|
||||||
|
12: parking meter
|
||||||
|
13: bench
|
||||||
|
14: bird
|
||||||
|
15: cat
|
||||||
|
16: dog
|
||||||
|
17: horse
|
||||||
|
18: sheep
|
||||||
|
19: cow
|
||||||
|
20: elephant
|
||||||
|
21: bear
|
||||||
|
22: zebra
|
||||||
|
23: giraffe
|
||||||
|
24: backpack
|
||||||
|
25: umbrella
|
||||||
|
26: handbag
|
||||||
|
27: tie
|
||||||
|
28: suitcase
|
||||||
|
29: frisbee
|
||||||
|
30: skis
|
||||||
|
31: snowboard
|
||||||
|
32: sports ball
|
||||||
|
33: kite
|
||||||
|
34: baseball bat
|
||||||
|
35: baseball glove
|
||||||
|
36: skateboard
|
||||||
|
37: surfboard
|
||||||
|
38: tennis racket
|
||||||
|
39: bottle
|
||||||
|
40: wine glass
|
||||||
|
41: cup
|
||||||
|
42: fork
|
||||||
|
43: knife
|
||||||
|
44: spoon
|
||||||
|
45: bowl
|
||||||
|
46: banana
|
||||||
|
47: apple
|
||||||
|
48: sandwich
|
||||||
|
49: orange
|
||||||
|
50: broccoli
|
||||||
|
51: carrot
|
||||||
|
52: hot dog
|
||||||
|
53: pizza
|
||||||
|
54: donut
|
||||||
|
55: cake
|
||||||
|
56: chair
|
||||||
|
57: couch
|
||||||
|
58: potted plant
|
||||||
|
59: bed
|
||||||
|
60: dining table
|
||||||
|
61: toilet
|
||||||
|
62: tv
|
||||||
|
63: laptop
|
||||||
|
64: mouse
|
||||||
|
65: remote
|
||||||
|
66: keyboard
|
||||||
|
67: cell phone
|
||||||
|
68: microwave
|
||||||
|
69: oven
|
||||||
|
70: toaster
|
||||||
|
71: sink
|
||||||
|
72: refrigerator
|
||||||
|
73: book
|
||||||
|
74: clock
|
||||||
|
75: vase
|
||||||
|
76: scissors
|
||||||
|
77: teddy bear
|
||||||
|
78: hair drier
|
||||||
|
79: toothbrush
|
||||||
|
|
||||||
|
|
||||||
|
# Download script/URL (optional)
|
||||||
|
download: https://ultralytics.com/assets/coco128.zip
|
@ -0,0 +1,34 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Hyperparameters for Objects365 training
|
||||||
|
# python train.py --weights yolov5m.pt --data Objects365.yaml --evolve
|
||||||
|
# See Hyperparameter Evolution tutorial for details https://github.com/ultralytics/yolov5#tutorials
|
||||||
|
|
||||||
|
lr0: 0.00258
|
||||||
|
lrf: 0.17
|
||||||
|
momentum: 0.779
|
||||||
|
weight_decay: 0.00058
|
||||||
|
warmup_epochs: 1.33
|
||||||
|
warmup_momentum: 0.86
|
||||||
|
warmup_bias_lr: 0.0711
|
||||||
|
box: 0.0539
|
||||||
|
cls: 0.299
|
||||||
|
cls_pw: 0.825
|
||||||
|
obj: 0.632
|
||||||
|
obj_pw: 1.0
|
||||||
|
iou_t: 0.2
|
||||||
|
anchor_t: 3.44
|
||||||
|
anchors: 3.2
|
||||||
|
fl_gamma: 0.0
|
||||||
|
hsv_h: 0.0188
|
||||||
|
hsv_s: 0.704
|
||||||
|
hsv_v: 0.36
|
||||||
|
degrees: 0.0
|
||||||
|
translate: 0.0902
|
||||||
|
scale: 0.491
|
||||||
|
shear: 0.0
|
||||||
|
perspective: 0.0
|
||||||
|
flipud: 0.0
|
||||||
|
fliplr: 0.5
|
||||||
|
mosaic: 1.0
|
||||||
|
mixup: 0.0
|
||||||
|
copy_paste: 0.0
|
@ -0,0 +1,40 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Hyperparameters for VOC training
|
||||||
|
# python train.py --batch 128 --weights yolov5m6.pt --data VOC.yaml --epochs 50 --img 512 --hyp hyp.scratch-med.yaml --evolve
|
||||||
|
# See Hyperparameter Evolution tutorial for details https://github.com/ultralytics/yolov5#tutorials
|
||||||
|
|
||||||
|
# YOLOv5 Hyperparameter Evolution Results
|
||||||
|
# Best generation: 467
|
||||||
|
# Last generation: 996
|
||||||
|
# metrics/precision, metrics/recall, metrics/mAP_0.5, metrics/mAP_0.5:0.95, val/box_loss, val/obj_loss, val/cls_loss
|
||||||
|
# 0.87729, 0.85125, 0.91286, 0.72664, 0.0076739, 0.0042529, 0.0013865
|
||||||
|
|
||||||
|
lr0: 0.00334
|
||||||
|
lrf: 0.15135
|
||||||
|
momentum: 0.74832
|
||||||
|
weight_decay: 0.00025
|
||||||
|
warmup_epochs: 3.3835
|
||||||
|
warmup_momentum: 0.59462
|
||||||
|
warmup_bias_lr: 0.18657
|
||||||
|
box: 0.02
|
||||||
|
cls: 0.21638
|
||||||
|
cls_pw: 0.5
|
||||||
|
obj: 0.51728
|
||||||
|
obj_pw: 0.67198
|
||||||
|
iou_t: 0.2
|
||||||
|
anchor_t: 3.3744
|
||||||
|
fl_gamma: 0.0
|
||||||
|
hsv_h: 0.01041
|
||||||
|
hsv_s: 0.54703
|
||||||
|
hsv_v: 0.27739
|
||||||
|
degrees: 0.0
|
||||||
|
translate: 0.04591
|
||||||
|
scale: 0.75544
|
||||||
|
shear: 0.0
|
||||||
|
perspective: 0.0
|
||||||
|
flipud: 0.0
|
||||||
|
fliplr: 0.5
|
||||||
|
mosaic: 0.85834
|
||||||
|
mixup: 0.04266
|
||||||
|
copy_paste: 0.0
|
||||||
|
anchors: 3.412
|
@ -0,0 +1,34 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Hyperparameters for high-augmentation COCO training from scratch
|
||||||
|
# python train.py --batch 32 --cfg yolov5m6.yaml --weights '' --data coco.yaml --img 1280 --epochs 300
|
||||||
|
# See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials
|
||||||
|
|
||||||
|
lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
|
||||||
|
lrf: 0.1 # final OneCycleLR learning rate (lr0 * lrf)
|
||||||
|
momentum: 0.937 # SGD momentum/Adam beta1
|
||||||
|
weight_decay: 0.0005 # optimizer weight decay 5e-4
|
||||||
|
warmup_epochs: 3.0 # warmup epochs (fractions ok)
|
||||||
|
warmup_momentum: 0.8 # warmup initial momentum
|
||||||
|
warmup_bias_lr: 0.1 # warmup initial bias lr
|
||||||
|
box: 0.05 # box loss gain
|
||||||
|
cls: 0.3 # cls loss gain
|
||||||
|
cls_pw: 1.0 # cls BCELoss positive_weight
|
||||||
|
obj: 0.7 # obj loss gain (scale with pixels)
|
||||||
|
obj_pw: 1.0 # obj BCELoss positive_weight
|
||||||
|
iou_t: 0.20 # IoU training threshold
|
||||||
|
anchor_t: 4.0 # anchor-multiple threshold
|
||||||
|
# anchors: 3 # anchors per output layer (0 to ignore)
|
||||||
|
fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
|
||||||
|
hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
|
||||||
|
hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
|
||||||
|
hsv_v: 0.4 # image HSV-Value augmentation (fraction)
|
||||||
|
degrees: 0.0 # image rotation (+/- deg)
|
||||||
|
translate: 0.1 # image translation (+/- fraction)
|
||||||
|
scale: 0.9 # image scale (+/- gain)
|
||||||
|
shear: 0.0 # image shear (+/- deg)
|
||||||
|
perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
|
||||||
|
flipud: 0.0 # image flip up-down (probability)
|
||||||
|
fliplr: 0.5 # image flip left-right (probability)
|
||||||
|
mosaic: 1.0 # image mosaic (probability)
|
||||||
|
mixup: 0.1 # image mixup (probability)
|
||||||
|
copy_paste: 0.1 # segment copy-paste (probability)
|
@ -0,0 +1,34 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Hyperparameters for low-augmentation COCO training from scratch
|
||||||
|
# python train.py --batch 64 --cfg yolov5n6.yaml --weights '' --data coco.yaml --img 640 --epochs 300 --linear
|
||||||
|
# See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials
|
||||||
|
|
||||||
|
lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
|
||||||
|
lrf: 0.01 # final OneCycleLR learning rate (lr0 * lrf)
|
||||||
|
momentum: 0.937 # SGD momentum/Adam beta1
|
||||||
|
weight_decay: 0.0005 # optimizer weight decay 5e-4
|
||||||
|
warmup_epochs: 3.0 # warmup epochs (fractions ok)
|
||||||
|
warmup_momentum: 0.8 # warmup initial momentum
|
||||||
|
warmup_bias_lr: 0.1 # warmup initial bias lr
|
||||||
|
box: 0.05 # box loss gain
|
||||||
|
cls: 0.5 # cls loss gain
|
||||||
|
cls_pw: 1.0 # cls BCELoss positive_weight
|
||||||
|
obj: 1.0 # obj loss gain (scale with pixels)
|
||||||
|
obj_pw: 1.0 # obj BCELoss positive_weight
|
||||||
|
iou_t: 0.20 # IoU training threshold
|
||||||
|
anchor_t: 4.0 # anchor-multiple threshold
|
||||||
|
# anchors: 3 # anchors per output layer (0 to ignore)
|
||||||
|
fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
|
||||||
|
hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
|
||||||
|
hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
|
||||||
|
hsv_v: 0.4 # image HSV-Value augmentation (fraction)
|
||||||
|
degrees: 0.0 # image rotation (+/- deg)
|
||||||
|
translate: 0.1 # image translation (+/- fraction)
|
||||||
|
scale: 0.5 # image scale (+/- gain)
|
||||||
|
shear: 0.0 # image shear (+/- deg)
|
||||||
|
perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
|
||||||
|
flipud: 0.0 # image flip up-down (probability)
|
||||||
|
fliplr: 0.5 # image flip left-right (probability)
|
||||||
|
mosaic: 1.0 # image mosaic (probability)
|
||||||
|
mixup: 0.0 # image mixup (probability)
|
||||||
|
copy_paste: 0.0 # segment copy-paste (probability)
|
@ -0,0 +1,34 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Hyperparameters for medium-augmentation COCO training from scratch
|
||||||
|
# python train.py --batch 32 --cfg yolov5m6.yaml --weights '' --data coco.yaml --img 1280 --epochs 300
|
||||||
|
# See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials
|
||||||
|
|
||||||
|
lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3)
|
||||||
|
lrf: 0.1 # final OneCycleLR learning rate (lr0 * lrf)
|
||||||
|
momentum: 0.937 # SGD momentum/Adam beta1
|
||||||
|
weight_decay: 0.0005 # optimizer weight decay 5e-4
|
||||||
|
warmup_epochs: 3.0 # warmup epochs (fractions ok)
|
||||||
|
warmup_momentum: 0.8 # warmup initial momentum
|
||||||
|
warmup_bias_lr: 0.1 # warmup initial bias lr
|
||||||
|
box: 0.05 # box loss gain
|
||||||
|
cls: 0.3 # cls loss gain
|
||||||
|
cls_pw: 1.0 # cls BCELoss positive_weight
|
||||||
|
obj: 0.7 # obj loss gain (scale with pixels)
|
||||||
|
obj_pw: 1.0 # obj BCELoss positive_weight
|
||||||
|
iou_t: 0.20 # IoU training threshold
|
||||||
|
anchor_t: 4.0 # anchor-multiple threshold
|
||||||
|
# anchors: 3 # anchors per output layer (0 to ignore)
|
||||||
|
fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
|
||||||
|
hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
|
||||||
|
hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
|
||||||
|
hsv_v: 0.4 # image HSV-Value augmentation (fraction)
|
||||||
|
degrees: 0.0 # image rotation (+/- deg)
|
||||||
|
translate: 0.1 # image translation (+/- fraction)
|
||||||
|
scale: 0.9 # image scale (+/- gain)
|
||||||
|
shear: 0.0 # image shear (+/- deg)
|
||||||
|
perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
|
||||||
|
flipud: 0.0 # image flip up-down (probability)
|
||||||
|
fliplr: 0.5 # image flip left-right (probability)
|
||||||
|
mosaic: 1.0 # image mosaic (probability)
|
||||||
|
mixup: 0.1 # image mixup (probability)
|
||||||
|
copy_paste: 0.0 # segment copy-paste (probability)
|
After Width: | Height: | Size: 476 KiB |
After Width: | Height: | Size: 165 KiB |
@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Download latest models from https://github.com/ultralytics/yolov5/releases
|
||||||
|
# Example usage: bash data/scripts/download_weights.sh
|
||||||
|
# parent
|
||||||
|
# └── yolov5
|
||||||
|
# ├── yolov5s.pt ← downloads here
|
||||||
|
# ├── yolov5m.pt
|
||||||
|
# └── ...
|
||||||
|
|
||||||
|
python - <<EOF
|
||||||
|
from utils.downloads import attempt_download
|
||||||
|
|
||||||
|
p5 = list('nsmlx') # P5 models
|
||||||
|
p6 = [f'{x}6' for x in p5] # P6 models
|
||||||
|
cls = [f'{x}-cls' for x in p5] # classification models
|
||||||
|
seg = [f'{x}-seg' for x in p5] # classification models
|
||||||
|
|
||||||
|
for x in p5 + p6 + cls + seg:
|
||||||
|
attempt_download(f'weights/yolov5{x}.pt')
|
||||||
|
|
||||||
|
EOF
|
@ -0,0 +1,56 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Download COCO 2017 dataset http://cocodataset.org
|
||||||
|
# Example usage: bash data/scripts/get_coco.sh
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── coco ← downloads here
|
||||||
|
|
||||||
|
# Arguments (optional) Usage: bash data/scripts/get_coco.sh --train --val --test --segments
|
||||||
|
if [ "$#" -gt 0 ]; then
|
||||||
|
for opt in "$@"; do
|
||||||
|
case "${opt}" in
|
||||||
|
--train) train=true ;;
|
||||||
|
--val) val=true ;;
|
||||||
|
--test) test=true ;;
|
||||||
|
--segments) segments=true ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
else
|
||||||
|
train=true
|
||||||
|
val=true
|
||||||
|
test=false
|
||||||
|
segments=false
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Download/unzip labels
|
||||||
|
d='../datasets' # unzip directory
|
||||||
|
url=https://github.com/ultralytics/yolov5/releases/download/v1.0/
|
||||||
|
if [ "$segments" == "true" ]; then
|
||||||
|
f='coco2017labels-segments.zip' # 168 MB
|
||||||
|
else
|
||||||
|
f='coco2017labels.zip' # 46 MB
|
||||||
|
fi
|
||||||
|
echo 'Downloading' $url$f ' ...'
|
||||||
|
curl -L $url$f -o $f -# && unzip -q $f -d $d && rm $f &
|
||||||
|
|
||||||
|
# Download/unzip images
|
||||||
|
d='../datasets/coco/images' # unzip directory
|
||||||
|
url=http://images.cocodataset.org/zips/
|
||||||
|
if [ "$train" == "true" ]; then
|
||||||
|
f='train2017.zip' # 19G, 118k images
|
||||||
|
echo 'Downloading' $url$f '...'
|
||||||
|
curl -L $url$f -o $f -# && unzip -q $f -d $d && rm $f &
|
||||||
|
fi
|
||||||
|
if [ "$val" == "true" ]; then
|
||||||
|
f='val2017.zip' # 1G, 5k images
|
||||||
|
echo 'Downloading' $url$f '...'
|
||||||
|
curl -L $url$f -o $f -# && unzip -q $f -d $d && rm $f &
|
||||||
|
fi
|
||||||
|
if [ "$test" == "true" ]; then
|
||||||
|
f='test2017.zip' # 7G, 41k images (optional)
|
||||||
|
echo 'Downloading' $url$f '...'
|
||||||
|
curl -L $url$f -o $f -# && unzip -q $f -d $d && rm $f &
|
||||||
|
fi
|
||||||
|
wait # finish background tasks
|
@ -0,0 +1,17 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Download COCO128 dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017)
|
||||||
|
# Example usage: bash data/scripts/get_coco128.sh
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── coco128 ← downloads here
|
||||||
|
|
||||||
|
# Download/unzip images and labels
|
||||||
|
d='../datasets' # unzip directory
|
||||||
|
url=https://github.com/ultralytics/yolov5/releases/download/v1.0/
|
||||||
|
f='coco128.zip' # or 'coco128-segments.zip', 68 MB
|
||||||
|
echo 'Downloading' $url$f ' ...'
|
||||||
|
curl -L $url$f -o $f -# && unzip -q $f -d $d && rm $f &
|
||||||
|
|
||||||
|
wait # finish background tasks
|
@ -0,0 +1,51 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Download ILSVRC2012 ImageNet dataset https://image-net.org
|
||||||
|
# Example usage: bash data/scripts/get_imagenet.sh
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── imagenet ← downloads here
|
||||||
|
|
||||||
|
# Arguments (optional) Usage: bash data/scripts/get_imagenet.sh --train --val
|
||||||
|
if [ "$#" -gt 0 ]; then
|
||||||
|
for opt in "$@"; do
|
||||||
|
case "${opt}" in
|
||||||
|
--train) train=true ;;
|
||||||
|
--val) val=true ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
else
|
||||||
|
train=true
|
||||||
|
val=true
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Make dir
|
||||||
|
d='../datasets/imagenet' # unzip directory
|
||||||
|
mkdir -p $d && cd $d
|
||||||
|
|
||||||
|
# Download/unzip train
|
||||||
|
if [ "$train" == "true" ]; then
|
||||||
|
wget https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_train.tar # download 138G, 1281167 images
|
||||||
|
mkdir train && mv ILSVRC2012_img_train.tar train/ && cd train
|
||||||
|
tar -xf ILSVRC2012_img_train.tar && rm -f ILSVRC2012_img_train.tar
|
||||||
|
find . -name "*.tar" | while read NAME; do
|
||||||
|
mkdir -p "${NAME%.tar}"
|
||||||
|
tar -xf "${NAME}" -C "${NAME%.tar}"
|
||||||
|
rm -f "${NAME}"
|
||||||
|
done
|
||||||
|
cd ..
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Download/unzip val
|
||||||
|
if [ "$val" == "true" ]; then
|
||||||
|
wget https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_val.tar # download 6.3G, 50000 images
|
||||||
|
mkdir val && mv ILSVRC2012_img_val.tar val/ && cd val && tar -xf ILSVRC2012_img_val.tar
|
||||||
|
wget -qO- https://raw.githubusercontent.com/soumith/imagenetloader.torch/master/valprep.sh | bash # move into subdirs
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Delete corrupted image (optional: PNG under JPEG name that may cause dataloaders to fail)
|
||||||
|
# rm train/n04266014/n04266014_10835.JPEG
|
||||||
|
|
||||||
|
# TFRecords (optional)
|
||||||
|
# wget https://raw.githubusercontent.com/tensorflow/models/master/research/slim/datasets/imagenet_lsvrc_2015_synsets.txt
|
@ -0,0 +1,153 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# DIUx xView 2018 Challenge https://challenge.xviewdataset.org by U.S. National Geospatial-Intelligence Agency (NGA)
|
||||||
|
# -------- DOWNLOAD DATA MANUALLY and jar xf val_images.zip to 'datasets/xView' before running train command! --------
|
||||||
|
# Example usage: python train.py --data xView.yaml
|
||||||
|
# parent
|
||||||
|
# ├── yolov5
|
||||||
|
# └── datasets
|
||||||
|
# └── xView ← downloads here (20.7 GB)
|
||||||
|
|
||||||
|
|
||||||
|
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
|
||||||
|
path: ../datasets/xView # dataset root dir
|
||||||
|
train: images/autosplit_train.txt # train images (relative to 'path') 90% of 847 train images
|
||||||
|
val: images/autosplit_val.txt # train images (relative to 'path') 10% of 847 train images
|
||||||
|
|
||||||
|
# Classes
|
||||||
|
names:
|
||||||
|
0: Fixed-wing Aircraft
|
||||||
|
1: Small Aircraft
|
||||||
|
2: Cargo Plane
|
||||||
|
3: Helicopter
|
||||||
|
4: Passenger Vehicle
|
||||||
|
5: Small Car
|
||||||
|
6: Bus
|
||||||
|
7: Pickup Truck
|
||||||
|
8: Utility Truck
|
||||||
|
9: Truck
|
||||||
|
10: Cargo Truck
|
||||||
|
11: Truck w/Box
|
||||||
|
12: Truck Tractor
|
||||||
|
13: Trailer
|
||||||
|
14: Truck w/Flatbed
|
||||||
|
15: Truck w/Liquid
|
||||||
|
16: Crane Truck
|
||||||
|
17: Railway Vehicle
|
||||||
|
18: Passenger Car
|
||||||
|
19: Cargo Car
|
||||||
|
20: Flat Car
|
||||||
|
21: Tank car
|
||||||
|
22: Locomotive
|
||||||
|
23: Maritime Vessel
|
||||||
|
24: Motorboat
|
||||||
|
25: Sailboat
|
||||||
|
26: Tugboat
|
||||||
|
27: Barge
|
||||||
|
28: Fishing Vessel
|
||||||
|
29: Ferry
|
||||||
|
30: Yacht
|
||||||
|
31: Container Ship
|
||||||
|
32: Oil Tanker
|
||||||
|
33: Engineering Vehicle
|
||||||
|
34: Tower crane
|
||||||
|
35: Container Crane
|
||||||
|
36: Reach Stacker
|
||||||
|
37: Straddle Carrier
|
||||||
|
38: Mobile Crane
|
||||||
|
39: Dump Truck
|
||||||
|
40: Haul Truck
|
||||||
|
41: Scraper/Tractor
|
||||||
|
42: Front loader/Bulldozer
|
||||||
|
43: Excavator
|
||||||
|
44: Cement Mixer
|
||||||
|
45: Ground Grader
|
||||||
|
46: Hut/Tent
|
||||||
|
47: Shed
|
||||||
|
48: Building
|
||||||
|
49: Aircraft Hangar
|
||||||
|
50: Damaged Building
|
||||||
|
51: Facility
|
||||||
|
52: Construction Site
|
||||||
|
53: Vehicle Lot
|
||||||
|
54: Helipad
|
||||||
|
55: Storage Tank
|
||||||
|
56: Shipping container lot
|
||||||
|
57: Shipping Container
|
||||||
|
58: Pylon
|
||||||
|
59: Tower
|
||||||
|
|
||||||
|
|
||||||
|
# Download script/URL (optional) ---------------------------------------------------------------------------------------
|
||||||
|
download: |
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
from utils.dataloaders import autosplit
|
||||||
|
from utils.general import download, xyxy2xywhn
|
||||||
|
|
||||||
|
|
||||||
|
def convert_labels(fname=Path('xView/xView_train.geojson')):
|
||||||
|
# Convert xView geoJSON labels to YOLO format
|
||||||
|
path = fname.parent
|
||||||
|
with open(fname) as f:
|
||||||
|
print(f'Loading {fname}...')
|
||||||
|
data = json.load(f)
|
||||||
|
|
||||||
|
# Make dirs
|
||||||
|
labels = Path(path / 'labels' / 'train')
|
||||||
|
os.system(f'rm -rf {labels}')
|
||||||
|
labels.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
# xView classes 11-94 to 0-59
|
||||||
|
xview_class2index = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, -1, 4, 5, 6, 7, 8, -1, 9, 10, 11,
|
||||||
|
12, 13, 14, 15, -1, -1, 16, 17, 18, 19, 20, 21, 22, -1, 23, 24, 25, -1, 26, 27, -1, 28, -1,
|
||||||
|
29, 30, 31, 32, 33, 34, 35, 36, 37, -1, 38, 39, 40, 41, 42, 43, 44, 45, -1, -1, -1, -1, 46,
|
||||||
|
47, 48, 49, -1, 50, 51, -1, 52, -1, -1, -1, 53, 54, -1, 55, -1, -1, 56, -1, 57, -1, 58, 59]
|
||||||
|
|
||||||
|
shapes = {}
|
||||||
|
for feature in tqdm(data['features'], desc=f'Converting {fname}'):
|
||||||
|
p = feature['properties']
|
||||||
|
if p['bounds_imcoords']:
|
||||||
|
id = p['image_id']
|
||||||
|
file = path / 'train_images' / id
|
||||||
|
if file.exists(): # 1395.tif missing
|
||||||
|
try:
|
||||||
|
box = np.array([int(num) for num in p['bounds_imcoords'].split(",")])
|
||||||
|
assert box.shape[0] == 4, f'incorrect box shape {box.shape[0]}'
|
||||||
|
cls = p['type_id']
|
||||||
|
cls = xview_class2index[int(cls)] # xView class to 0-60
|
||||||
|
assert 59 >= cls >= 0, f'incorrect class index {cls}'
|
||||||
|
|
||||||
|
# Write YOLO label
|
||||||
|
if id not in shapes:
|
||||||
|
shapes[id] = Image.open(file).size
|
||||||
|
box = xyxy2xywhn(box[None].astype(np.float), w=shapes[id][0], h=shapes[id][1], clip=True)
|
||||||
|
with open((labels / id).with_suffix('.txt'), 'a') as f:
|
||||||
|
f.write(f"{cls} {' '.join(f'{x:.6f}' for x in box[0])}\n") # write label.txt
|
||||||
|
except Exception as e:
|
||||||
|
print(f'WARNING: skipping one label for {file}: {e}')
|
||||||
|
|
||||||
|
|
||||||
|
# Download manually from https://challenge.xviewdataset.org
|
||||||
|
dir = Path(yaml['path']) # dataset root dir
|
||||||
|
# urls = ['https://d307kc0mrhucc3.cloudfront.net/train_labels.zip', # train labels
|
||||||
|
# 'https://d307kc0mrhucc3.cloudfront.net/train_images.zip', # 15G, 847 train images
|
||||||
|
# 'https://d307kc0mrhucc3.cloudfront.net/val_images.zip'] # 5G, 282 val images (no labels)
|
||||||
|
# download(urls, dir=dir, delete=False)
|
||||||
|
|
||||||
|
# Convert labels
|
||||||
|
convert_labels(dir / 'xView_train.geojson')
|
||||||
|
|
||||||
|
# Move images
|
||||||
|
images = Path(dir / 'images')
|
||||||
|
images.mkdir(parents=True, exist_ok=True)
|
||||||
|
Path(dir / 'train_images').rename(dir / 'images' / 'train')
|
||||||
|
Path(dir / 'val_images').rename(dir / 'images' / 'val')
|
||||||
|
|
||||||
|
# Split
|
||||||
|
autosplit(dir / 'images' / 'train')
|
@ -0,0 +1,857 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
"""
|
||||||
|
Common modules
|
||||||
|
"""
|
||||||
|
|
||||||
|
import ast
|
||||||
|
import contextlib
|
||||||
|
import json
|
||||||
|
import math
|
||||||
|
import platform
|
||||||
|
import warnings
|
||||||
|
import zipfile
|
||||||
|
from collections import OrderedDict, namedtuple
|
||||||
|
from copy import copy
|
||||||
|
from pathlib import Path
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
import pandas as pd
|
||||||
|
import requests
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
from IPython.display import display
|
||||||
|
from PIL import Image
|
||||||
|
from torch.cuda import amp
|
||||||
|
|
||||||
|
from utils import TryExcept
|
||||||
|
from utils.dataloaders import exif_transpose, letterbox
|
||||||
|
from utils.general import (LOGGER, ROOT, Profile, check_requirements, check_suffix, check_version, colorstr,
|
||||||
|
increment_path, is_notebook, make_divisible, non_max_suppression, scale_boxes, xywh2xyxy,
|
||||||
|
xyxy2xywh, yaml_load)
|
||||||
|
from utils.plots import Annotator, colors, save_one_box
|
||||||
|
from utils.torch_utils import copy_attr, smart_inference_mode
|
||||||
|
|
||||||
|
|
||||||
|
def autopad(k, p=None, d=1): # kernel, padding, dilation
|
||||||
|
# Pad to 'same' shape outputs
|
||||||
|
if d > 1:
|
||||||
|
k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k] # actual kernel-size
|
||||||
|
if p is None:
|
||||||
|
p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad
|
||||||
|
return p
|
||||||
|
|
||||||
|
|
||||||
|
class Conv(nn.Module):
|
||||||
|
# Standard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation)
|
||||||
|
default_act = nn.SiLU() # default activation
|
||||||
|
|
||||||
|
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True):
|
||||||
|
super().__init__()
|
||||||
|
self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False)
|
||||||
|
self.bn = nn.BatchNorm2d(c2)
|
||||||
|
self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return self.act(self.bn(self.conv(x)))
|
||||||
|
|
||||||
|
def forward_fuse(self, x):
|
||||||
|
return self.act(self.conv(x))
|
||||||
|
|
||||||
|
|
||||||
|
class DWConv(Conv):
|
||||||
|
# Depth-wise convolution
|
||||||
|
def __init__(self, c1, c2, k=1, s=1, d=1, act=True): # ch_in, ch_out, kernel, stride, dilation, activation
|
||||||
|
super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), d=d, act=act)
|
||||||
|
|
||||||
|
|
||||||
|
class DWConvTranspose2d(nn.ConvTranspose2d):
|
||||||
|
# Depth-wise transpose convolution
|
||||||
|
def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0): # ch_in, ch_out, kernel, stride, padding, padding_out
|
||||||
|
super().__init__(c1, c2, k, s, p1, p2, groups=math.gcd(c1, c2))
|
||||||
|
|
||||||
|
|
||||||
|
class TransformerLayer(nn.Module):
|
||||||
|
# Transformer layer https://arxiv.org/abs/2010.11929 (LayerNorm layers removed for better performance)
|
||||||
|
def __init__(self, c, num_heads):
|
||||||
|
super().__init__()
|
||||||
|
self.q = nn.Linear(c, c, bias=False)
|
||||||
|
self.k = nn.Linear(c, c, bias=False)
|
||||||
|
self.v = nn.Linear(c, c, bias=False)
|
||||||
|
self.ma = nn.MultiheadAttention(embed_dim=c, num_heads=num_heads)
|
||||||
|
self.fc1 = nn.Linear(c, c, bias=False)
|
||||||
|
self.fc2 = nn.Linear(c, c, bias=False)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = self.ma(self.q(x), self.k(x), self.v(x))[0] + x
|
||||||
|
x = self.fc2(self.fc1(x)) + x
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
class TransformerBlock(nn.Module):
|
||||||
|
# Vision Transformer https://arxiv.org/abs/2010.11929
|
||||||
|
def __init__(self, c1, c2, num_heads, num_layers):
|
||||||
|
super().__init__()
|
||||||
|
self.conv = None
|
||||||
|
if c1 != c2:
|
||||||
|
self.conv = Conv(c1, c2)
|
||||||
|
self.linear = nn.Linear(c2, c2) # learnable position embedding
|
||||||
|
self.tr = nn.Sequential(*(TransformerLayer(c2, num_heads) for _ in range(num_layers)))
|
||||||
|
self.c2 = c2
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
if self.conv is not None:
|
||||||
|
x = self.conv(x)
|
||||||
|
b, _, w, h = x.shape
|
||||||
|
p = x.flatten(2).permute(2, 0, 1)
|
||||||
|
return self.tr(p + self.linear(p)).permute(1, 2, 0).reshape(b, self.c2, w, h)
|
||||||
|
|
||||||
|
|
||||||
|
class Bottleneck(nn.Module):
|
||||||
|
# Standard bottleneck
|
||||||
|
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion
|
||||||
|
super().__init__()
|
||||||
|
c_ = int(c2 * e) # hidden channels
|
||||||
|
self.cv1 = Conv(c1, c_, 1, 1)
|
||||||
|
self.cv2 = Conv(c_, c2, 3, 1, g=g)
|
||||||
|
self.add = shortcut and c1 == c2
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
|
||||||
|
|
||||||
|
|
||||||
|
class BottleneckCSP(nn.Module):
|
||||||
|
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
||||||
|
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
||||||
|
super().__init__()
|
||||||
|
c_ = int(c2 * e) # hidden channels
|
||||||
|
self.cv1 = Conv(c1, c_, 1, 1)
|
||||||
|
self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)
|
||||||
|
self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)
|
||||||
|
self.cv4 = Conv(2 * c_, c2, 1, 1)
|
||||||
|
self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3)
|
||||||
|
self.act = nn.SiLU()
|
||||||
|
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
y1 = self.cv3(self.m(self.cv1(x)))
|
||||||
|
y2 = self.cv2(x)
|
||||||
|
return self.cv4(self.act(self.bn(torch.cat((y1, y2), 1))))
|
||||||
|
|
||||||
|
|
||||||
|
class CrossConv(nn.Module):
|
||||||
|
# Cross Convolution Downsample
|
||||||
|
def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False):
|
||||||
|
# ch_in, ch_out, kernel, stride, groups, expansion, shortcut
|
||||||
|
super().__init__()
|
||||||
|
c_ = int(c2 * e) # hidden channels
|
||||||
|
self.cv1 = Conv(c1, c_, (1, k), (1, s))
|
||||||
|
self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g)
|
||||||
|
self.add = shortcut and c1 == c2
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))
|
||||||
|
|
||||||
|
|
||||||
|
class C3(nn.Module):
|
||||||
|
# CSP Bottleneck with 3 convolutions
|
||||||
|
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion
|
||||||
|
super().__init__()
|
||||||
|
c_ = int(c2 * e) # hidden channels
|
||||||
|
self.cv1 = Conv(c1, c_, 1, 1)
|
||||||
|
self.cv2 = Conv(c1, c_, 1, 1)
|
||||||
|
self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2)
|
||||||
|
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))
|
||||||
|
|
||||||
|
|
||||||
|
class C3x(C3):
|
||||||
|
# C3 module with cross-convolutions
|
||||||
|
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
|
||||||
|
super().__init__(c1, c2, n, shortcut, g, e)
|
||||||
|
c_ = int(c2 * e)
|
||||||
|
self.m = nn.Sequential(*(CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)))
|
||||||
|
|
||||||
|
|
||||||
|
class C3TR(C3):
|
||||||
|
# C3 module with TransformerBlock()
|
||||||
|
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
|
||||||
|
super().__init__(c1, c2, n, shortcut, g, e)
|
||||||
|
c_ = int(c2 * e)
|
||||||
|
self.m = TransformerBlock(c_, c_, 4, n)
|
||||||
|
|
||||||
|
|
||||||
|
class C3SPP(C3):
|
||||||
|
# C3 module with SPP()
|
||||||
|
def __init__(self, c1, c2, k=(5, 9, 13), n=1, shortcut=True, g=1, e=0.5):
|
||||||
|
super().__init__(c1, c2, n, shortcut, g, e)
|
||||||
|
c_ = int(c2 * e)
|
||||||
|
self.m = SPP(c_, c_, k)
|
||||||
|
|
||||||
|
|
||||||
|
class C3Ghost(C3):
|
||||||
|
# C3 module with GhostBottleneck()
|
||||||
|
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):
|
||||||
|
super().__init__(c1, c2, n, shortcut, g, e)
|
||||||
|
c_ = int(c2 * e) # hidden channels
|
||||||
|
self.m = nn.Sequential(*(GhostBottleneck(c_, c_) for _ in range(n)))
|
||||||
|
|
||||||
|
|
||||||
|
class SPP(nn.Module):
|
||||||
|
# Spatial Pyramid Pooling (SPP) layer https://arxiv.org/abs/1406.4729
|
||||||
|
def __init__(self, c1, c2, k=(5, 9, 13)):
|
||||||
|
super().__init__()
|
||||||
|
c_ = c1 // 2 # hidden channels
|
||||||
|
self.cv1 = Conv(c1, c_, 1, 1)
|
||||||
|
self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1)
|
||||||
|
self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = self.cv1(x)
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
|
||||||
|
return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))
|
||||||
|
|
||||||
|
|
||||||
|
class SPPF(nn.Module):
|
||||||
|
# Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher
|
||||||
|
def __init__(self, c1, c2, k=5): # equivalent to SPP(k=(5, 9, 13))
|
||||||
|
super().__init__()
|
||||||
|
c_ = c1 // 2 # hidden channels
|
||||||
|
self.cv1 = Conv(c1, c_, 1, 1)
|
||||||
|
self.cv2 = Conv(c_ * 4, c2, 1, 1)
|
||||||
|
self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = self.cv1(x)
|
||||||
|
with warnings.catch_warnings():
|
||||||
|
warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
|
||||||
|
y1 = self.m(x)
|
||||||
|
y2 = self.m(y1)
|
||||||
|
return self.cv2(torch.cat((x, y1, y2, self.m(y2)), 1))
|
||||||
|
|
||||||
|
|
||||||
|
class Focus(nn.Module):
|
||||||
|
# Focus wh information into c-space
|
||||||
|
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups
|
||||||
|
super().__init__()
|
||||||
|
self.conv = Conv(c1 * 4, c2, k, s, p, g, act=act)
|
||||||
|
# self.contract = Contract(gain=2)
|
||||||
|
|
||||||
|
def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2)
|
||||||
|
return self.conv(torch.cat((x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]), 1))
|
||||||
|
# return self.conv(self.contract(x))
|
||||||
|
|
||||||
|
|
||||||
|
class GhostConv(nn.Module):
|
||||||
|
# Ghost Convolution https://github.com/huawei-noah/ghostnet
|
||||||
|
def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups
|
||||||
|
super().__init__()
|
||||||
|
c_ = c2 // 2 # hidden channels
|
||||||
|
self.cv1 = Conv(c1, c_, k, s, None, g, act=act)
|
||||||
|
self.cv2 = Conv(c_, c_, 5, 1, None, c_, act=act)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
y = self.cv1(x)
|
||||||
|
return torch.cat((y, self.cv2(y)), 1)
|
||||||
|
|
||||||
|
|
||||||
|
class GhostBottleneck(nn.Module):
|
||||||
|
# Ghost Bottleneck https://github.com/huawei-noah/ghostnet
|
||||||
|
def __init__(self, c1, c2, k=3, s=1): # ch_in, ch_out, kernel, stride
|
||||||
|
super().__init__()
|
||||||
|
c_ = c2 // 2
|
||||||
|
self.conv = nn.Sequential(
|
||||||
|
GhostConv(c1, c_, 1, 1), # pw
|
||||||
|
DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw
|
||||||
|
GhostConv(c_, c2, 1, 1, act=False)) # pw-linear
|
||||||
|
self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False), Conv(c1, c2, 1, 1,
|
||||||
|
act=False)) if s == 2 else nn.Identity()
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return self.conv(x) + self.shortcut(x)
|
||||||
|
|
||||||
|
|
||||||
|
class Contract(nn.Module):
|
||||||
|
# Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40)
|
||||||
|
def __init__(self, gain=2):
|
||||||
|
super().__init__()
|
||||||
|
self.gain = gain
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
b, c, h, w = x.size() # assert (h / s == 0) and (W / s == 0), 'Indivisible gain'
|
||||||
|
s = self.gain
|
||||||
|
x = x.view(b, c, h // s, s, w // s, s) # x(1,64,40,2,40,2)
|
||||||
|
x = x.permute(0, 3, 5, 1, 2, 4).contiguous() # x(1,2,2,64,40,40)
|
||||||
|
return x.view(b, c * s * s, h // s, w // s) # x(1,256,40,40)
|
||||||
|
|
||||||
|
|
||||||
|
class Expand(nn.Module):
|
||||||
|
# Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160)
|
||||||
|
def __init__(self, gain=2):
|
||||||
|
super().__init__()
|
||||||
|
self.gain = gain
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
b, c, h, w = x.size() # assert C / s ** 2 == 0, 'Indivisible gain'
|
||||||
|
s = self.gain
|
||||||
|
x = x.view(b, s, s, c // s ** 2, h, w) # x(1,2,2,16,80,80)
|
||||||
|
x = x.permute(0, 3, 4, 1, 5, 2).contiguous() # x(1,16,80,2,80,2)
|
||||||
|
return x.view(b, c // s ** 2, h * s, w * s) # x(1,16,160,160)
|
||||||
|
|
||||||
|
|
||||||
|
class Concat(nn.Module):
|
||||||
|
# Concatenate a list of tensors along dimension
|
||||||
|
def __init__(self, dimension=1):
|
||||||
|
super().__init__()
|
||||||
|
self.d = dimension
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return torch.cat(x, self.d)
|
||||||
|
|
||||||
|
|
||||||
|
class DetectMultiBackend(nn.Module):
|
||||||
|
# YOLOv5 MultiBackend class for python inference on various backends
|
||||||
|
def __init__(self, weights='yolov5s.pt', device=torch.device('cpu'), dnn=False, data=None, fp16=False, fuse=True):
|
||||||
|
# Usage:
|
||||||
|
# PyTorch: weights = *.pt
|
||||||
|
# TorchScript: *.torchscript
|
||||||
|
# ONNX Runtime: *.onnx
|
||||||
|
# ONNX OpenCV DNN: *.onnx --dnn
|
||||||
|
# OpenVINO: *_openvino_model
|
||||||
|
# CoreML: *.mlmodel
|
||||||
|
# TensorRT: *.engine
|
||||||
|
# TensorFlow SavedModel: *_saved_model
|
||||||
|
# TensorFlow GraphDef: *.pb
|
||||||
|
# TensorFlow Lite: *.tflite
|
||||||
|
# TensorFlow Edge TPU: *_edgetpu.tflite
|
||||||
|
# PaddlePaddle: *_paddle_model
|
||||||
|
from models.experimental import attempt_download, attempt_load # scoped to avoid circular import
|
||||||
|
|
||||||
|
super().__init__()
|
||||||
|
w = str(weights[0] if isinstance(weights, list) else weights)
|
||||||
|
pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle, triton = self._model_type(w)
|
||||||
|
fp16 &= pt or jit or onnx or engine # FP16
|
||||||
|
nhwc = coreml or saved_model or pb or tflite or edgetpu # BHWC formats (vs torch BCWH)
|
||||||
|
stride = 32 # default stride
|
||||||
|
cuda = torch.cuda.is_available() and device.type != 'cpu' # use CUDA
|
||||||
|
if pt: # PyTorch
|
||||||
|
model = attempt_load(weights if isinstance(weights, list) else w, device=device, inplace=True, fuse=fuse)
|
||||||
|
stride = max(int(model.stride.max()), 32) # model stride
|
||||||
|
names = model.module.names if hasattr(model, 'module') else model.names # get class names
|
||||||
|
model.half() if fp16 else model.float()
|
||||||
|
self.model = model # explicitly assign for to(), cpu(), cuda(), half()
|
||||||
|
# elif jit: # TorchScript
|
||||||
|
# LOGGER.info(f'Loading {w} for TorchScript inference...')
|
||||||
|
# extra_files = {'config.txt': ''} # model metadata
|
||||||
|
# model = torch.jit.load(w, _extra_files=extra_files, map_location=device)
|
||||||
|
# model.half() if fp16 else model.float()
|
||||||
|
# if extra_files['config.txt']: # load metadata dict
|
||||||
|
# d = json.loads(extra_files['config.txt'],
|
||||||
|
# object_hook=lambda d: {int(k) if k.isdigit() else k: v
|
||||||
|
# for k, v in d.items()})
|
||||||
|
# stride, names = int(d['stride']), d['names']
|
||||||
|
# elif dnn: # ONNX OpenCV DNN
|
||||||
|
# LOGGER.info(f'Loading {w} for ONNX OpenCV DNN inference...')
|
||||||
|
# check_requirements('opencv-python>=4.5.4')
|
||||||
|
# net = cv2.dnn.readNetFromONNX(w)
|
||||||
|
# elif onnx: # ONNX Runtime
|
||||||
|
# LOGGER.info(f'Loading {w} for ONNX Runtime inference...')
|
||||||
|
# check_requirements(('onnx', 'onnxruntime-gpu' if cuda else 'onnxruntime'))
|
||||||
|
# import onnxruntime
|
||||||
|
# providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider']
|
||||||
|
# session = onnxruntime.InferenceSession(w, providers=providers)
|
||||||
|
# output_names = [x.name for x in session.get_outputs()]
|
||||||
|
# meta = session.get_modelmeta().custom_metadata_map # metadata
|
||||||
|
# if 'stride' in meta:
|
||||||
|
# stride, names = int(meta['stride']), eval(meta['names'])
|
||||||
|
# elif xml: # OpenVINO
|
||||||
|
# LOGGER.info(f'Loading {w} for OpenVINO inference...')
|
||||||
|
# check_requirements('openvino') # requires openvino-dev: https://pypi.org/project/openvino-dev/
|
||||||
|
# from openvino.runtime import Core, Layout, get_batch
|
||||||
|
# ie = Core()
|
||||||
|
# if not Path(w).is_file(): # if not *.xml
|
||||||
|
# w = next(Path(w).glob('*.xml')) # get *.xml file from *_openvino_model dir
|
||||||
|
# network = ie.read_model(model=w, weights=Path(w).with_suffix('.bin'))
|
||||||
|
# if network.get_parameters()[0].get_layout().empty:
|
||||||
|
# network.get_parameters()[0].set_layout(Layout("NCHW"))
|
||||||
|
# batch_dim = get_batch(network)
|
||||||
|
# if batch_dim.is_static:
|
||||||
|
# batch_size = batch_dim.get_length()
|
||||||
|
# executable_network = ie.compile_model(network, device_name="CPU") # device_name="MYRIAD" for Intel NCS2
|
||||||
|
# stride, names = self._load_metadata(Path(w).with_suffix('.yaml')) # load metadata
|
||||||
|
elif engine: # TensorRT
|
||||||
|
LOGGER.info(f'Loading {w} for TensorRT inference...')
|
||||||
|
import tensorrt as trt # https://developer.nvidia.com/nvidia-tensorrt-download
|
||||||
|
# check_version(trt.__version__, '7.0.0', hard=True) # require tensorrt>=7.0.0
|
||||||
|
# if device.type == 'cpu':
|
||||||
|
# device = torch.device('cuda:0')
|
||||||
|
Binding = namedtuple('Binding', ('name', 'dtype', 'shape', 'data', 'ptr'))
|
||||||
|
logger = trt.Logger(trt.Logger.INFO)
|
||||||
|
with open(w, 'rb') as f, trt.Runtime(logger) as runtime:
|
||||||
|
model = runtime.deserialize_cuda_engine(f.read())
|
||||||
|
context = model.create_execution_context()
|
||||||
|
bindings = OrderedDict()
|
||||||
|
output_names = []
|
||||||
|
fp16 = False # default updated below
|
||||||
|
dynamic = False
|
||||||
|
for i in range(model.num_bindings):
|
||||||
|
name = model.get_binding_name(i)
|
||||||
|
dtype = trt.nptype(model.get_binding_dtype(i))
|
||||||
|
if model.binding_is_input(i):
|
||||||
|
if -1 in tuple(model.get_binding_shape(i)): # dynamic
|
||||||
|
dynamic = True
|
||||||
|
context.set_binding_shape(i, tuple(model.get_profile_shape(0, i)[2]))
|
||||||
|
if dtype == np.float16:
|
||||||
|
fp16 = True
|
||||||
|
else: # output
|
||||||
|
output_names.append(name)
|
||||||
|
shape = tuple(context.get_binding_shape(i))
|
||||||
|
im = torch.from_numpy(np.empty(shape, dtype=dtype)).to(device)
|
||||||
|
bindings[name] = Binding(name, dtype, shape, im, int(im.data_ptr()))
|
||||||
|
binding_addrs = OrderedDict((n, d.ptr) for n, d in bindings.items())
|
||||||
|
batch_size = bindings['images'].shape[0] # if dynamic, this is instead max batch size
|
||||||
|
# elif coreml: # CoreML
|
||||||
|
# LOGGER.info(f'Loading {w} for CoreML inference...')
|
||||||
|
# import coremltools as ct
|
||||||
|
# model = ct.models.MLModel(w)
|
||||||
|
# elif saved_model: # TF SavedModel
|
||||||
|
# LOGGER.info(f'Loading {w} for TensorFlow SavedModel inference...')
|
||||||
|
# import tensorflow as tf
|
||||||
|
# keras = False # assume TF1 saved_model
|
||||||
|
# model = tf.keras.models.load_model(w) if keras else tf.saved_model.load(w)
|
||||||
|
# elif pb: # GraphDef https://www.tensorflow.org/guide/migrate#a_graphpb_or_graphpbtxt
|
||||||
|
# LOGGER.info(f'Loading {w} for TensorFlow GraphDef inference...')
|
||||||
|
# import tensorflow as tf
|
||||||
|
#
|
||||||
|
# def wrap_frozen_graph(gd, inputs, outputs):
|
||||||
|
# x = tf.compat.v1.wrap_function(lambda: tf.compat.v1.import_graph_def(gd, name=""), []) # wrapped
|
||||||
|
# ge = x.graph.as_graph_element
|
||||||
|
# return x.prune(tf.nest.map_structure(ge, inputs), tf.nest.map_structure(ge, outputs))
|
||||||
|
#
|
||||||
|
# def gd_outputs(gd):
|
||||||
|
# name_list, input_list = [], []
|
||||||
|
# for node in gd.node: # tensorflow.core.framework.node_def_pb2.NodeDef
|
||||||
|
# name_list.append(node.name)
|
||||||
|
# input_list.extend(node.input)
|
||||||
|
# return sorted(f'{x}:0' for x in list(set(name_list) - set(input_list)) if not x.startswith('NoOp'))
|
||||||
|
#
|
||||||
|
# gd = tf.Graph().as_graph_def() # TF GraphDef
|
||||||
|
# with open(w, 'rb') as f:
|
||||||
|
# gd.ParseFromString(f.read())
|
||||||
|
# frozen_func = wrap_frozen_graph(gd, inputs="x:0", outputs=gd_outputs(gd))
|
||||||
|
# elif tflite or edgetpu: # https://www.tensorflow.org/lite/guide/python#install_tensorflow_lite_for_python
|
||||||
|
# try: # https://coral.ai/docs/edgetpu/tflite-python/#update-existing-tf-lite-code-for-the-edge-tpu
|
||||||
|
# from tflite_runtime.interpreter import Interpreter, load_delegate
|
||||||
|
# except ImportError:
|
||||||
|
# import tensorflow as tf
|
||||||
|
# Interpreter, load_delegate = tf.lite.Interpreter, tf.lite.experimental.load_delegate,
|
||||||
|
# if edgetpu: # TF Edge TPU https://coral.ai/software/#edgetpu-runtime
|
||||||
|
# LOGGER.info(f'Loading {w} for TensorFlow Lite Edge TPU inference...')
|
||||||
|
# delegate = {
|
||||||
|
# 'Linux': 'libedgetpu.so.1',
|
||||||
|
# 'Darwin': 'libedgetpu.1.dylib',
|
||||||
|
# 'Windows': 'edgetpu.dll'}[platform.system()]
|
||||||
|
# interpreter = Interpreter(model_path=w, experimental_delegates=[load_delegate(delegate)])
|
||||||
|
# else: # TFLite
|
||||||
|
# LOGGER.info(f'Loading {w} for TensorFlow Lite inference...')
|
||||||
|
# interpreter = Interpreter(model_path=w) # load TFLite model
|
||||||
|
# interpreter.allocate_tensors() # allocate
|
||||||
|
# input_details = interpreter.get_input_details() # inputs
|
||||||
|
# output_details = interpreter.get_output_details() # outputs
|
||||||
|
# # load metadata
|
||||||
|
# with contextlib.suppress(zipfile.BadZipFile):
|
||||||
|
# with zipfile.ZipFile(w, "r") as model:
|
||||||
|
# meta_file = model.namelist()[0]
|
||||||
|
# meta = ast.literal_eval(model.read(meta_file).decode("utf-8"))
|
||||||
|
# stride, names = int(meta['stride']), meta['names']
|
||||||
|
# elif tfjs: # TF.js
|
||||||
|
# raise NotImplementedError('ERROR: YOLOv5 TF.js inference is not supported')
|
||||||
|
# elif paddle: # PaddlePaddle
|
||||||
|
# LOGGER.info(f'Loading {w} for PaddlePaddle inference...')
|
||||||
|
# check_requirements('paddlepaddle-gpu' if cuda else 'paddlepaddle')
|
||||||
|
# import paddle.inference as pdi
|
||||||
|
# if not Path(w).is_file(): # if not *.pdmodel
|
||||||
|
# w = next(Path(w).rglob('*.pdmodel')) # get *.pdmodel file from *_paddle_model dir
|
||||||
|
# weights = Path(w).with_suffix('.pdiparams')
|
||||||
|
# config = pdi.Config(str(w), str(weights))
|
||||||
|
# if cuda:
|
||||||
|
# config.enable_use_gpu(memory_pool_init_size_mb=2048, device_id=0)
|
||||||
|
# predictor = pdi.create_predictor(config)
|
||||||
|
# input_handle = predictor.get_input_handle(predictor.get_input_names()[0])
|
||||||
|
# output_names = predictor.get_output_names()
|
||||||
|
# elif triton: # NVIDIA Triton Inference Server
|
||||||
|
# LOGGER.info(f'Using {w} as Triton Inference Server...')
|
||||||
|
# check_requirements('tritonclient[all]')
|
||||||
|
# from utils.triton import TritonRemoteModel
|
||||||
|
# model = TritonRemoteModel(url=w)
|
||||||
|
# nhwc = model.runtime.startswith("tensorflow")
|
||||||
|
# else:
|
||||||
|
# raise NotImplementedError(f'ERROR: {w} is not a supported format')
|
||||||
|
#
|
||||||
|
# # class names
|
||||||
|
if 'names' not in locals():
|
||||||
|
names = yaml_load(data)['names'] if data else {i: f'class{i}' for i in range(999)}
|
||||||
|
if names[0] == 'n01440764' and len(names) == 1000: # ImageNet
|
||||||
|
names = yaml_load(ROOT / 'data/ImageNet.yaml')['names'] # human-readable names
|
||||||
|
|
||||||
|
self.__dict__.update(locals()) # assign all variables to self
|
||||||
|
|
||||||
|
def forward(self, im, augment=False, visualize=False):
|
||||||
|
# YOLOv5 MultiBackend inference
|
||||||
|
b, ch, h, w = im.shape # batch, channel, height, width
|
||||||
|
if self.fp16 and im.dtype != torch.float16:
|
||||||
|
im = im.half() # to FP16
|
||||||
|
if self.nhwc:
|
||||||
|
im = im.permute(0, 2, 3, 1) # torch BCHW to numpy BHWC shape(1,320,192,3)
|
||||||
|
|
||||||
|
if self.pt: # PyTorch
|
||||||
|
y = self.model(im, augment=augment, visualize=visualize) if augment or visualize else self.model(im)
|
||||||
|
# elif self.jit: # TorchScript
|
||||||
|
# y = self.model(im)
|
||||||
|
# elif self.dnn: # ONNX OpenCV DNN
|
||||||
|
# im = im.cpu().numpy() # torch to numpy
|
||||||
|
# self.net.setInput(im)
|
||||||
|
# y = self.net.forward()
|
||||||
|
# elif self.onnx: # ONNX Runtime
|
||||||
|
# im = im.cpu().numpy() # torch to numpy
|
||||||
|
# y = self.session.run(self.output_names, {self.session.get_inputs()[0].name: im})
|
||||||
|
# elif self.xml: # OpenVINO
|
||||||
|
# im = im.cpu().numpy() # FP32
|
||||||
|
# y = list(self.executable_network([im]).values())
|
||||||
|
elif self.engine: # TensorRT
|
||||||
|
if self.dynamic and im.shape != self.bindings['images'].shape:
|
||||||
|
i = self.model.get_binding_index('images')
|
||||||
|
self.context.set_binding_shape(i, im.shape) # reshape if dynamic
|
||||||
|
self.bindings['images'] = self.bindings['images']._replace(shape=im.shape)
|
||||||
|
for name in self.output_names:
|
||||||
|
i = self.model.get_binding_index(name)
|
||||||
|
self.bindings[name].data.resize_(tuple(self.context.get_binding_shape(i)))
|
||||||
|
s = self.bindings['images'].shape
|
||||||
|
assert im.shape == s, f"input size {im.shape} {'>' if self.dynamic else 'not equal to'} max model size {s}"
|
||||||
|
self.binding_addrs['images'] = int(im.data_ptr())
|
||||||
|
self.context.execute_v2(list(self.binding_addrs.values()))
|
||||||
|
y = [self.bindings[x].data for x in sorted(self.output_names)]
|
||||||
|
# elif self.coreml: # CoreML
|
||||||
|
# im = im.cpu().numpy()
|
||||||
|
# im = Image.fromarray((im[0] * 255).astype('uint8'))
|
||||||
|
# # im = im.resize((192, 320), Image.ANTIALIAS)
|
||||||
|
# y = self.model.predict({'image': im}) # coordinates are xywh normalized
|
||||||
|
# if 'confidence' in y:
|
||||||
|
# box = xywh2xyxy(y['coordinates'] * [[w, h, w, h]]) # xyxy pixels
|
||||||
|
# conf, cls = y['confidence'].max(1), y['confidence'].argmax(1).astype(np.float)
|
||||||
|
# y = np.concatenate((box, conf.reshape(-1, 1), cls.reshape(-1, 1)), 1)
|
||||||
|
# else:
|
||||||
|
# y = list(reversed(y.values())) # reversed for segmentation models (pred, proto)
|
||||||
|
# elif self.paddle: # PaddlePaddle
|
||||||
|
# im = im.cpu().numpy().astype(np.float32)
|
||||||
|
# self.input_handle.copy_from_cpu(im)
|
||||||
|
# self.predictor.run()
|
||||||
|
# y = [self.predictor.get_output_handle(x).copy_to_cpu() for x in self.output_names]
|
||||||
|
# elif self.triton: # NVIDIA Triton Inference Server
|
||||||
|
# y = self.model(im)
|
||||||
|
# else: # TensorFlow (SavedModel, GraphDef, Lite, Edge TPU)
|
||||||
|
# im = im.cpu().numpy()
|
||||||
|
# if self.saved_model: # SavedModel
|
||||||
|
# y = self.model(im, training=False) if self.keras else self.model(im)
|
||||||
|
# elif self.pb: # GraphDef
|
||||||
|
# y = self.frozen_func(x=self.tf.constant(im))
|
||||||
|
# else: # Lite or Edge TPU
|
||||||
|
# input = self.input_details[0]
|
||||||
|
# int8 = input['dtype'] == np.uint8 # is TFLite quantized uint8 model
|
||||||
|
# if int8:
|
||||||
|
# scale, zero_point = input['quantization']
|
||||||
|
# im = (im / scale + zero_point).astype(np.uint8) # de-scale
|
||||||
|
# self.interpreter.set_tensor(input['index'], im)
|
||||||
|
# self.interpreter.invoke()
|
||||||
|
# y = []
|
||||||
|
# for output in self.output_details:
|
||||||
|
# x = self.interpreter.get_tensor(output['index'])
|
||||||
|
# if int8:
|
||||||
|
# scale, zero_point = output['quantization']
|
||||||
|
# x = (x.astype(np.float32) - zero_point) * scale # re-scale
|
||||||
|
# y.append(x)
|
||||||
|
# y = [x if isinstance(x, np.ndarray) else x.numpy() for x in y]
|
||||||
|
# y[0][..., :4] *= [w, h, w, h] # xywh normalized to pixels
|
||||||
|
|
||||||
|
if isinstance(y, (list, tuple)):
|
||||||
|
return self.from_numpy(y[0]) if len(y) == 1 else [self.from_numpy(x) for x in y]
|
||||||
|
else:
|
||||||
|
return self.from_numpy(y)
|
||||||
|
|
||||||
|
def from_numpy(self, x):
|
||||||
|
return torch.from_numpy(x).to(self.device) if isinstance(x, np.ndarray) else x
|
||||||
|
|
||||||
|
def warmup(self, imgsz=(1, 3, 640, 640)):
|
||||||
|
# Warmup model by running inference once
|
||||||
|
warmup_types = self.pt, self.jit, self.onnx, self.engine, self.saved_model, self.pb, self.triton
|
||||||
|
if any(warmup_types) and (self.device.type != 'cpu' or self.triton):
|
||||||
|
im = torch.empty(*imgsz, dtype=torch.half if self.fp16 else torch.float, device=self.device) # input
|
||||||
|
for _ in range(2 if self.jit else 1): #
|
||||||
|
self.forward(im) # warmup
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _model_type(p='path/to/model.pt'):
|
||||||
|
# Return model type from model path, i.e. path='path/to/model.onnx' -> type=onnx
|
||||||
|
# types = [pt, jit, onnx, xml, engine, coreml, saved_model, pb, tflite, edgetpu, tfjs, paddle]
|
||||||
|
from export import export_formats
|
||||||
|
from utils.downloads import is_url
|
||||||
|
sf = list(export_formats().Suffix) # export suffixes
|
||||||
|
if not is_url(p, check=False):
|
||||||
|
check_suffix(p, sf) # checks
|
||||||
|
url = urlparse(p) # if url may be Triton inference server
|
||||||
|
types = [s in Path(p).name for s in sf]
|
||||||
|
types[8] &= not types[9] # tflite &= not edgetpu
|
||||||
|
triton = not any(types) and all([any(s in url.scheme for s in ["http", "grpc"]), url.netloc])
|
||||||
|
return types + [triton]
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _load_metadata(f=Path('path/to/meta.yaml')):
|
||||||
|
# Load metadata from meta.yaml if it exists
|
||||||
|
if f.exists():
|
||||||
|
d = yaml_load(f)
|
||||||
|
return d['stride'], d['names'] # assign stride, names
|
||||||
|
return None, None
|
||||||
|
|
||||||
|
|
||||||
|
class AutoShape(nn.Module):
|
||||||
|
# YOLOv5 input-robust model wrapper for passing cv2/np/PIL/torch inputs. Includes preprocessing, inference and NMS
|
||||||
|
conf = 0.25 # NMS confidence threshold
|
||||||
|
iou = 0.45 # NMS IoU threshold
|
||||||
|
agnostic = False # NMS class-agnostic
|
||||||
|
multi_label = False # NMS multiple labels per box
|
||||||
|
classes = None # (optional list) filter by class, i.e. = [0, 15, 16] for COCO persons, cats and dogs
|
||||||
|
max_det = 1000 # maximum number of detections per image
|
||||||
|
amp = False # Automatic Mixed Precision (AMP) inference
|
||||||
|
|
||||||
|
def __init__(self, model, verbose=True):
|
||||||
|
super().__init__()
|
||||||
|
if verbose:
|
||||||
|
LOGGER.info('Adding AutoShape... ')
|
||||||
|
copy_attr(self, model, include=('yaml', 'nc', 'hyp', 'names', 'stride', 'abc'), exclude=()) # copy attributes
|
||||||
|
self.dmb = isinstance(model, DetectMultiBackend) # DetectMultiBackend() instance
|
||||||
|
self.pt = not self.dmb or model.pt # PyTorch model
|
||||||
|
self.model = model.eval()
|
||||||
|
if self.pt:
|
||||||
|
m = self.model.model.model[-1] if self.dmb else self.model.model[-1] # Detect()
|
||||||
|
m.inplace = False # Detect.inplace=False for safe multithread inference
|
||||||
|
m.export = True # do not output loss values
|
||||||
|
|
||||||
|
def _apply(self, fn):
|
||||||
|
# Apply to(), cpu(), cuda(), half() to model tensors that are not parameters or registered buffers
|
||||||
|
self = super()._apply(fn)
|
||||||
|
if self.pt:
|
||||||
|
m = self.model.model.model[-1] if self.dmb else self.model.model[-1] # Detect()
|
||||||
|
m.stride = fn(m.stride)
|
||||||
|
m.grid = list(map(fn, m.grid))
|
||||||
|
if isinstance(m.anchor_grid, list):
|
||||||
|
m.anchor_grid = list(map(fn, m.anchor_grid))
|
||||||
|
return self
|
||||||
|
|
||||||
|
@smart_inference_mode()
|
||||||
|
def forward(self, ims, size=640, augment=False, profile=False):
|
||||||
|
# Inference from various sources. For size(height=640, width=1280), RGB images example inputs are:
|
||||||
|
# file: ims = 'data/images/zidane.jpg' # str or PosixPath
|
||||||
|
# URI: = 'https://ultralytics.com/images/zidane.jpg'
|
||||||
|
# OpenCV: = cv2.imread('image.jpg')[:,:,::-1] # HWC BGR to RGB x(640,1280,3)
|
||||||
|
# PIL: = Image.open('image.jpg') or ImageGrab.grab() # HWC x(640,1280,3)
|
||||||
|
# numpy: = np.zeros((640,1280,3)) # HWC
|
||||||
|
# torch: = torch.zeros(16,3,320,640) # BCHW (scaled to size=640, 0-1 values)
|
||||||
|
# multiple: = [Image.open('image1.jpg'), Image.open('image2.jpg'), ...] # list of images
|
||||||
|
|
||||||
|
dt = (Profile(), Profile(), Profile())
|
||||||
|
with dt[0]:
|
||||||
|
if isinstance(size, int): # expand
|
||||||
|
size = (size, size)
|
||||||
|
p = next(self.model.parameters()) if self.pt else torch.empty(1, device=self.model.device) # param
|
||||||
|
autocast = self.amp and (p.device.type != 'cpu') # Automatic Mixed Precision (AMP) inference
|
||||||
|
if isinstance(ims, torch.Tensor): # torch
|
||||||
|
with amp.autocast(autocast):
|
||||||
|
return self.model(ims.to(p.device).type_as(p), augment=augment) # inference
|
||||||
|
|
||||||
|
# Pre-process
|
||||||
|
n, ims = (len(ims), list(ims)) if isinstance(ims, (list, tuple)) else (1, [ims]) # number, list of images
|
||||||
|
shape0, shape1, files = [], [], [] # image and inference shapes, filenames
|
||||||
|
for i, im in enumerate(ims):
|
||||||
|
f = f'image{i}' # filename
|
||||||
|
if isinstance(im, (str, Path)): # filename or uri
|
||||||
|
im, f = Image.open(requests.get(im, stream=True).raw if str(im).startswith('http') else im), im
|
||||||
|
im = np.asarray(exif_transpose(im))
|
||||||
|
elif isinstance(im, Image.Image): # PIL Image
|
||||||
|
im, f = np.asarray(exif_transpose(im)), getattr(im, 'filename', f) or f
|
||||||
|
files.append(Path(f).with_suffix('.jpg').name)
|
||||||
|
if im.shape[0] < 5: # image in CHW
|
||||||
|
im = im.transpose((1, 2, 0)) # reverse dataloader .transpose(2, 0, 1)
|
||||||
|
im = im[..., :3] if im.ndim == 3 else cv2.cvtColor(im, cv2.COLOR_GRAY2BGR) # enforce 3ch input
|
||||||
|
s = im.shape[:2] # HWC
|
||||||
|
shape0.append(s) # image shape
|
||||||
|
g = max(size) / max(s) # gain
|
||||||
|
shape1.append([int(y * g) for y in s])
|
||||||
|
ims[i] = im if im.data.contiguous else np.ascontiguousarray(im) # update
|
||||||
|
shape1 = [make_divisible(x, self.stride) for x in np.array(shape1).max(0)] # inf shape
|
||||||
|
x = [letterbox(im, shape1, auto=False)[0] for im in ims] # pad
|
||||||
|
x = np.ascontiguousarray(np.array(x).transpose((0, 3, 1, 2))) # stack and BHWC to BCHW
|
||||||
|
x = torch.from_numpy(x).to(p.device).type_as(p) / 255 # uint8 to fp16/32
|
||||||
|
|
||||||
|
with amp.autocast(autocast):
|
||||||
|
# Inference
|
||||||
|
with dt[1]:
|
||||||
|
y = self.model(x, augment=augment) # forward
|
||||||
|
|
||||||
|
# Post-process
|
||||||
|
with dt[2]:
|
||||||
|
y = non_max_suppression(y if self.dmb else y[0],
|
||||||
|
self.conf,
|
||||||
|
self.iou,
|
||||||
|
self.classes,
|
||||||
|
self.agnostic,
|
||||||
|
self.multi_label,
|
||||||
|
max_det=self.max_det) # NMS
|
||||||
|
for i in range(n):
|
||||||
|
scale_boxes(shape1, y[i][:, :4], shape0[i])
|
||||||
|
|
||||||
|
return Detections(ims, y, files, dt, self.names, x.shape)
|
||||||
|
|
||||||
|
|
||||||
|
class Detections:
|
||||||
|
# YOLOv5 detections class for inference results
|
||||||
|
def __init__(self, ims, pred, files, times=(0, 0, 0), names=None, shape=None):
|
||||||
|
super().__init__()
|
||||||
|
d = pred[0].device # device
|
||||||
|
gn = [torch.tensor([*(im.shape[i] for i in [1, 0, 1, 0]), 1, 1], device=d) for im in ims] # normalizations
|
||||||
|
self.ims = ims # list of images as numpy arrays
|
||||||
|
self.pred = pred # list of tensors pred[0] = (xyxy, conf, cls)
|
||||||
|
self.names = names # class names
|
||||||
|
self.files = files # image filenames
|
||||||
|
self.times = times # profiling times
|
||||||
|
self.xyxy = pred # xyxy pixels
|
||||||
|
self.xywh = [xyxy2xywh(x) for x in pred] # xywh pixels
|
||||||
|
self.xyxyn = [x / g for x, g in zip(self.xyxy, gn)] # xyxy normalized
|
||||||
|
self.xywhn = [x / g for x, g in zip(self.xywh, gn)] # xywh normalized
|
||||||
|
self.n = len(self.pred) # number of images (batch size)
|
||||||
|
self.t = tuple(x.t / self.n * 1E3 for x in times) # timestamps (ms)
|
||||||
|
self.s = tuple(shape) # inference BCHW shape
|
||||||
|
|
||||||
|
def _run(self, pprint=False, show=False, save=False, crop=False, render=False, labels=True, save_dir=Path('')):
|
||||||
|
s, crops = '', []
|
||||||
|
for i, (im, pred) in enumerate(zip(self.ims, self.pred)):
|
||||||
|
s += f'\nimage {i + 1}/{len(self.pred)}: {im.shape[0]}x{im.shape[1]} ' # string
|
||||||
|
if pred.shape[0]:
|
||||||
|
for c in pred[:, -1].unique():
|
||||||
|
n = (pred[:, -1] == c).sum() # detections per class
|
||||||
|
s += f"{n} {self.names[int(c)]}{'s' * (n > 1)}, " # add to string
|
||||||
|
s = s.rstrip(', ')
|
||||||
|
if show or save or render or crop:
|
||||||
|
annotator = Annotator(im, example=str(self.names))
|
||||||
|
for *box, conf, cls in reversed(pred): # xyxy, confidence, class
|
||||||
|
label = f'{self.names[int(cls)]} {conf:.2f}'
|
||||||
|
if crop:
|
||||||
|
file = save_dir / 'crops' / self.names[int(cls)] / self.files[i] if save else None
|
||||||
|
crops.append({
|
||||||
|
'box': box,
|
||||||
|
'conf': conf,
|
||||||
|
'cls': cls,
|
||||||
|
'label': label,
|
||||||
|
'im': save_one_box(box, im, file=file, save=save)})
|
||||||
|
else: # all others
|
||||||
|
annotator.box_label(box, label if labels else '', color=colors(cls))
|
||||||
|
im = annotator.im
|
||||||
|
else:
|
||||||
|
s += '(no detections)'
|
||||||
|
|
||||||
|
im = Image.fromarray(im.astype(np.uint8)) if isinstance(im, np.ndarray) else im # from np
|
||||||
|
if show:
|
||||||
|
display(im) if is_notebook() else im.show(self.files[i])
|
||||||
|
if save:
|
||||||
|
f = self.files[i]
|
||||||
|
im.save(save_dir / f) # save
|
||||||
|
if i == self.n - 1:
|
||||||
|
LOGGER.info(f"Saved {self.n} image{'s' * (self.n > 1)} to {colorstr('bold', save_dir)}")
|
||||||
|
if render:
|
||||||
|
self.ims[i] = np.asarray(im)
|
||||||
|
if pprint:
|
||||||
|
s = s.lstrip('\n')
|
||||||
|
return f'{s}\nSpeed: %.1fms pre-process, %.1fms inference, %.1fms NMS per image at shape {self.s}' % self.t
|
||||||
|
if crop:
|
||||||
|
if save:
|
||||||
|
LOGGER.info(f'Saved results to {save_dir}\n')
|
||||||
|
return crops
|
||||||
|
|
||||||
|
@TryExcept('Showing images is not supported in this environment')
|
||||||
|
def show(self, labels=True):
|
||||||
|
self._run(show=True, labels=labels) # show results
|
||||||
|
|
||||||
|
def save(self, labels=True, save_dir='runs/detect/exp', exist_ok=False):
|
||||||
|
save_dir = increment_path(save_dir, exist_ok, mkdir=True) # increment save_dir
|
||||||
|
self._run(save=True, labels=labels, save_dir=save_dir) # save results
|
||||||
|
|
||||||
|
def crop(self, save=True, save_dir='runs/detect/exp', exist_ok=False):
|
||||||
|
save_dir = increment_path(save_dir, exist_ok, mkdir=True) if save else None
|
||||||
|
return self._run(crop=True, save=save, save_dir=save_dir) # crop results
|
||||||
|
|
||||||
|
def render(self, labels=True):
|
||||||
|
self._run(render=True, labels=labels) # render results
|
||||||
|
return self.ims
|
||||||
|
|
||||||
|
def pandas(self):
|
||||||
|
# return detections as pandas DataFrames, i.e. print(results.pandas().xyxy[0])
|
||||||
|
new = copy(self) # return copy
|
||||||
|
ca = 'xmin', 'ymin', 'xmax', 'ymax', 'confidence', 'class', 'name' # xyxy columns
|
||||||
|
cb = 'xcenter', 'ycenter', 'width', 'height', 'confidence', 'class', 'name' # xywh columns
|
||||||
|
for k, c in zip(['xyxy', 'xyxyn', 'xywh', 'xywhn'], [ca, ca, cb, cb]):
|
||||||
|
a = [[x[:5] + [int(x[5]), self.names[int(x[5])]] for x in x.tolist()] for x in getattr(self, k)] # update
|
||||||
|
setattr(new, k, [pd.DataFrame(x, columns=c) for x in a])
|
||||||
|
return new
|
||||||
|
|
||||||
|
def tolist(self):
|
||||||
|
# return a list of Detections objects, i.e. 'for result in results.tolist():'
|
||||||
|
r = range(self.n) # iterable
|
||||||
|
x = [Detections([self.ims[i]], [self.pred[i]], [self.files[i]], self.times, self.names, self.s) for i in r]
|
||||||
|
# for d in x:
|
||||||
|
# for k in ['ims', 'pred', 'xyxy', 'xyxyn', 'xywh', 'xywhn']:
|
||||||
|
# setattr(d, k, getattr(d, k)[0]) # pop out of list
|
||||||
|
return x
|
||||||
|
|
||||||
|
def print(self):
|
||||||
|
LOGGER.info(self.__str__())
|
||||||
|
|
||||||
|
def __len__(self): # override len(results)
|
||||||
|
return self.n
|
||||||
|
|
||||||
|
def __str__(self): # override print(results)
|
||||||
|
return self._run(pprint=True) # print results
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f'YOLOv5 {self.__class__} instance\n' + self.__str__()
|
||||||
|
|
||||||
|
|
||||||
|
class Proto(nn.Module):
|
||||||
|
# YOLOv5 mask Proto module for segmentation models
|
||||||
|
def __init__(self, c1, c_=256, c2=32): # ch_in, number of protos, number of masks
|
||||||
|
super().__init__()
|
||||||
|
self.cv1 = Conv(c1, c_, k=3)
|
||||||
|
self.upsample = nn.Upsample(scale_factor=2, mode='nearest')
|
||||||
|
self.cv2 = Conv(c_, c_, k=3)
|
||||||
|
self.cv3 = Conv(c_, c2)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return self.cv3(self.cv2(self.upsample(self.cv1(x))))
|
||||||
|
|
||||||
|
|
||||||
|
class Classify(nn.Module):
|
||||||
|
# YOLOv5 classification head, i.e. x(b,c1,20,20) to x(b,c2)
|
||||||
|
def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups
|
||||||
|
super().__init__()
|
||||||
|
c_ = 1280 # efficientnet_b0 size
|
||||||
|
self.conv = Conv(c1, c_, k, s, autopad(k, p), g)
|
||||||
|
self.pool = nn.AdaptiveAvgPool2d(1) # to x(b,c_,1,1)
|
||||||
|
self.drop = nn.Dropout(p=0.0, inplace=True)
|
||||||
|
self.linear = nn.Linear(c_, c2) # to x(b,c2)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
if isinstance(x, list):
|
||||||
|
x = torch.cat(x, 1)
|
||||||
|
return self.linear(self.drop(self.pool(self.conv(x)).flatten(1)))
|
@ -0,0 +1,111 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
"""
|
||||||
|
Experimental modules
|
||||||
|
"""
|
||||||
|
import math
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
|
||||||
|
from utils.downloads import attempt_download
|
||||||
|
|
||||||
|
|
||||||
|
class Sum(nn.Module):
|
||||||
|
# Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070
|
||||||
|
def __init__(self, n, weight=False): # n: number of inputs
|
||||||
|
super().__init__()
|
||||||
|
self.weight = weight # apply weights boolean
|
||||||
|
self.iter = range(n - 1) # iter object
|
||||||
|
if weight:
|
||||||
|
self.w = nn.Parameter(-torch.arange(1.0, n) / 2, requires_grad=True) # layer weights
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
y = x[0] # no weight
|
||||||
|
if self.weight:
|
||||||
|
w = torch.sigmoid(self.w) * 2
|
||||||
|
for i in self.iter:
|
||||||
|
y = y + x[i + 1] * w[i]
|
||||||
|
else:
|
||||||
|
for i in self.iter:
|
||||||
|
y = y + x[i + 1]
|
||||||
|
return y
|
||||||
|
|
||||||
|
|
||||||
|
class MixConv2d(nn.Module):
|
||||||
|
# Mixed Depth-wise Conv https://arxiv.org/abs/1907.09595
|
||||||
|
def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): # ch_in, ch_out, kernel, stride, ch_strategy
|
||||||
|
super().__init__()
|
||||||
|
n = len(k) # number of convolutions
|
||||||
|
if equal_ch: # equal c_ per group
|
||||||
|
i = torch.linspace(0, n - 1E-6, c2).floor() # c2 indices
|
||||||
|
c_ = [(i == g).sum() for g in range(n)] # intermediate channels
|
||||||
|
else: # equal weight.numel() per group
|
||||||
|
b = [c2] + [0] * n
|
||||||
|
a = np.eye(n + 1, n, k=-1)
|
||||||
|
a -= np.roll(a, 1, axis=1)
|
||||||
|
a *= np.array(k) ** 2
|
||||||
|
a[0] = 1
|
||||||
|
c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b
|
||||||
|
|
||||||
|
self.m = nn.ModuleList([
|
||||||
|
nn.Conv2d(c1, int(c_), k, s, k // 2, groups=math.gcd(c1, int(c_)), bias=False) for k, c_ in zip(k, c_)])
|
||||||
|
self.bn = nn.BatchNorm2d(c2)
|
||||||
|
self.act = nn.SiLU()
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return self.act(self.bn(torch.cat([m(x) for m in self.m], 1)))
|
||||||
|
|
||||||
|
|
||||||
|
class Ensemble(nn.ModuleList):
|
||||||
|
# Ensemble of models
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
def forward(self, x, augment=False, profile=False, visualize=False):
|
||||||
|
y = [module(x, augment, profile, visualize)[0] for module in self]
|
||||||
|
# y = torch.stack(y).max(0)[0] # max ensemble
|
||||||
|
# y = torch.stack(y).mean(0) # mean ensemble
|
||||||
|
y = torch.cat(y, 1) # nms ensemble
|
||||||
|
return y, None # inference, train output
|
||||||
|
|
||||||
|
|
||||||
|
def attempt_load(weights, device=None, inplace=True, fuse=True):
|
||||||
|
# Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a
|
||||||
|
from models.yolo import Detect, Model
|
||||||
|
|
||||||
|
model = Ensemble()
|
||||||
|
# for w in weights if isinstance(weights, list) else [weights]:
|
||||||
|
ckpt = torch.load(attempt_download(weights), map_location='cpu') # load
|
||||||
|
ckpt = (ckpt.get('ema') or ckpt['model']).to(device).float() # FP32 model
|
||||||
|
|
||||||
|
# Model compatibility updates
|
||||||
|
if not hasattr(ckpt, 'stride'):
|
||||||
|
ckpt.stride = torch.tensor([32.])
|
||||||
|
if hasattr(ckpt, 'names') and isinstance(ckpt.names, (list, tuple)):
|
||||||
|
ckpt.names = dict(enumerate(ckpt.names)) # convert to dict
|
||||||
|
|
||||||
|
model.append(ckpt.fuse().eval() if fuse and hasattr(ckpt, 'fuse') else ckpt.eval()) # model in eval mode
|
||||||
|
|
||||||
|
# Module compatibility updates
|
||||||
|
for m in model.modules():
|
||||||
|
t = type(m)
|
||||||
|
if t in (nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model):
|
||||||
|
m.inplace = inplace # torch 1.7.0 compatibility
|
||||||
|
if t is Detect and not isinstance(m.anchor_grid, list):
|
||||||
|
delattr(m, 'anchor_grid')
|
||||||
|
setattr(m, 'anchor_grid', [torch.zeros(1)] * m.nl)
|
||||||
|
elif t is nn.Upsample and not hasattr(m, 'recompute_scale_factor'):
|
||||||
|
m.recompute_scale_factor = None # torch 1.11.0 compatibility
|
||||||
|
|
||||||
|
# Return model
|
||||||
|
if len(model) == 1:
|
||||||
|
return model[-1]
|
||||||
|
|
||||||
|
# Return detection ensemble
|
||||||
|
print(f'Ensemble created with {weights}\n')
|
||||||
|
for k in 'names', 'nc', 'yaml':
|
||||||
|
setattr(model, k, getattr(model[0], k))
|
||||||
|
model.stride = model[torch.argmax(torch.tensor([m.stride.max() for m in model])).int()].stride # max stride
|
||||||
|
assert all(model[0].nc == m.nc for m in model), f'Models have different class counts: {[m.nc for m in model]}'
|
||||||
|
return model
|
@ -0,0 +1,59 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
# Default anchors for COCO data
|
||||||
|
|
||||||
|
|
||||||
|
# P5 -------------------------------------------------------------------------------------------------------------------
|
||||||
|
# P5-640:
|
||||||
|
anchors_p5_640:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
|
||||||
|
# P6 -------------------------------------------------------------------------------------------------------------------
|
||||||
|
# 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
|
||||||
|
anchors_p6_640:
|
||||||
|
- [9,11, 21,19, 17,41] # P3/8
|
||||||
|
- [43,32, 39,70, 86,64] # P4/16
|
||||||
|
- [65,131, 134,130, 120,265] # P5/32
|
||||||
|
- [282,180, 247,354, 512,387] # P6/64
|
||||||
|
|
||||||
|
# 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
|
||||||
|
anchors_p6_1280:
|
||||||
|
- [19,27, 44,40, 38,94] # P3/8
|
||||||
|
- [96,68, 86,152, 180,137] # P4/16
|
||||||
|
- [140,301, 303,264, 238,542] # P5/32
|
||||||
|
- [436,615, 739,380, 925,792] # P6/64
|
||||||
|
|
||||||
|
# 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
|
||||||
|
anchors_p6_1920:
|
||||||
|
- [28,41, 67,59, 57,141] # P3/8
|
||||||
|
- [144,103, 129,227, 270,205] # P4/16
|
||||||
|
- [209,452, 455,396, 358,812] # P5/32
|
||||||
|
- [653,922, 1109,570, 1387,1187] # P6/64
|
||||||
|
|
||||||
|
|
||||||
|
# P7 -------------------------------------------------------------------------------------------------------------------
|
||||||
|
# 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
|
||||||
|
anchors_p7_640:
|
||||||
|
- [11,11, 13,30, 29,20] # P3/8
|
||||||
|
- [30,46, 61,38, 39,92] # P4/16
|
||||||
|
- [78,80, 146,66, 79,163] # P5/32
|
||||||
|
- [149,150, 321,143, 157,303] # P6/64
|
||||||
|
- [257,402, 359,290, 524,372] # P7/128
|
||||||
|
|
||||||
|
# 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
|
||||||
|
anchors_p7_1280:
|
||||||
|
- [19,22, 54,36, 32,77] # P3/8
|
||||||
|
- [70,83, 138,71, 75,173] # P4/16
|
||||||
|
- [165,159, 148,334, 375,151] # P5/32
|
||||||
|
- [334,317, 251,626, 499,474] # P6/64
|
||||||
|
- [750,326, 534,814, 1079,818] # P7/128
|
||||||
|
|
||||||
|
# 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
|
||||||
|
anchors_p7_1920:
|
||||||
|
- [29,34, 81,55, 47,115] # P3/8
|
||||||
|
- [105,124, 207,107, 113,259] # P4/16
|
||||||
|
- [247,238, 222,500, 563,227] # P5/32
|
||||||
|
- [501,476, 376,939, 749,711] # P6/64
|
||||||
|
- [1126,489, 801,1222, 1618,1227] # P7/128
|
@ -0,0 +1,51 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# darknet53 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [32, 3, 1]], # 0
|
||||||
|
[-1, 1, Conv, [64, 3, 2]], # 1-P1/2
|
||||||
|
[-1, 1, Bottleneck, [64]],
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 3-P2/4
|
||||||
|
[-1, 2, Bottleneck, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 5-P3/8
|
||||||
|
[-1, 8, Bottleneck, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 7-P4/16
|
||||||
|
[-1, 8, Bottleneck, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 9-P5/32
|
||||||
|
[-1, 4, Bottleneck, [1024]], # 10
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv3-SPP head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Bottleneck, [1024, False]],
|
||||||
|
[-1, 1, SPP, [512, [5, 9, 13]]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 1]],
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 1]], # 15 (P5/32-large)
|
||||||
|
|
||||||
|
[-2, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 8], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 1, Bottleneck, [512, False]],
|
||||||
|
[-1, 1, Bottleneck, [512, False]],
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, Conv, [512, 3, 1]], # 22 (P4/16-medium)
|
||||||
|
|
||||||
|
[-2, 1, Conv, [128, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 1, Bottleneck, [256, False]],
|
||||||
|
[-1, 2, Bottleneck, [256, False]], # 27 (P3/8-small)
|
||||||
|
|
||||||
|
[[27, 22, 15], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,41 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,14, 23,27, 37,58] # P4/16
|
||||||
|
- [81,82, 135,169, 344,319] # P5/32
|
||||||
|
|
||||||
|
# YOLOv3-tiny backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [16, 3, 1]], # 0
|
||||||
|
[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 1-P1/2
|
||||||
|
[-1, 1, Conv, [32, 3, 1]],
|
||||||
|
[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 3-P2/4
|
||||||
|
[-1, 1, Conv, [64, 3, 1]],
|
||||||
|
[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 5-P3/8
|
||||||
|
[-1, 1, Conv, [128, 3, 1]],
|
||||||
|
[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 7-P4/16
|
||||||
|
[-1, 1, Conv, [256, 3, 1]],
|
||||||
|
[-1, 1, nn.MaxPool2d, [2, 2, 0]], # 9-P5/32
|
||||||
|
[-1, 1, Conv, [512, 3, 1]],
|
||||||
|
[-1, 1, nn.ZeroPad2d, [[0, 1, 0, 1]]], # 11
|
||||||
|
[-1, 1, nn.MaxPool2d, [2, 1, 0]], # 12
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv3-tiny head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [1024, 3, 1]],
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, Conv, [512, 3, 1]], # 15 (P5/32-large)
|
||||||
|
|
||||||
|
[-2, 1, Conv, [128, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 8], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 1, Conv, [256, 3, 1]], # 19 (P4/16-medium)
|
||||||
|
|
||||||
|
[[19, 15], 1, Detect, [nc, anchors]], # Detect(P4, P5)
|
||||||
|
]
|
@ -0,0 +1,51 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# darknet53 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [32, 3, 1]], # 0
|
||||||
|
[-1, 1, Conv, [64, 3, 2]], # 1-P1/2
|
||||||
|
[-1, 1, Bottleneck, [64]],
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 3-P2/4
|
||||||
|
[-1, 2, Bottleneck, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 5-P3/8
|
||||||
|
[-1, 8, Bottleneck, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 7-P4/16
|
||||||
|
[-1, 8, Bottleneck, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 9-P5/32
|
||||||
|
[-1, 4, Bottleneck, [1024]], # 10
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv3 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Bottleneck, [1024, False]],
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 1]],
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 1]], # 15 (P5/32-large)
|
||||||
|
|
||||||
|
[-2, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 8], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 1, Bottleneck, [512, False]],
|
||||||
|
[-1, 1, Bottleneck, [512, False]],
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, Conv, [512, 3, 1]], # 22 (P4/16-medium)
|
||||||
|
|
||||||
|
[-2, 1, Conv, [128, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 1, Bottleneck, [256, False]],
|
||||||
|
[-1, 2, Bottleneck, [256, False]], # 27 (P3/8-small)
|
||||||
|
|
||||||
|
[[27, 22, 15], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 BiFPN head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14, 6], 1, Concat, [1]], # cat P4 <--- BiFPN change
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,42 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 FPN head
|
||||||
|
head:
|
||||||
|
[[-1, 3, C3, [1024, False]], # 10 (P5/32-large)
|
||||||
|
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 3, C3, [512, False]], # 14 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 3, C3, [256, False]], # 18 (P3/8-small)
|
||||||
|
|
||||||
|
[[18, 14, 10], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,54 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors: 3 # AutoAnchor evolves 3 anchors per P output layer
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head with (P2, P3, P4, P5) outputs
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [128, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 2], 1, Concat, [1]], # cat backbone P2
|
||||||
|
[-1, 1, C3, [128, False]], # 21 (P2/4-xsmall)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [128, 3, 2]],
|
||||||
|
[[-1, 18], 1, Concat, [1]], # cat head P3
|
||||||
|
[-1, 3, C3, [256, False]], # 24 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 27 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 30 (P5/32-large)
|
||||||
|
|
||||||
|
[[21, 24, 27, 30], 1, Detect, [nc, anchors]], # Detect(P2, P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,41 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.33 # model depth multiple
|
||||||
|
width_multiple: 0.50 # layer channel multiple
|
||||||
|
anchors: 3 # AutoAnchor evolves 3 anchors per P output layer
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[ [ -1, 1, Conv, [ 64, 6, 2, 2 ] ], # 0-P1/2
|
||||||
|
[ -1, 1, Conv, [ 128, 3, 2 ] ], # 1-P2/4
|
||||||
|
[ -1, 3, C3, [ 128 ] ],
|
||||||
|
[ -1, 1, Conv, [ 256, 3, 2 ] ], # 3-P3/8
|
||||||
|
[ -1, 6, C3, [ 256 ] ],
|
||||||
|
[ -1, 1, Conv, [ 512, 3, 2 ] ], # 5-P4/16
|
||||||
|
[ -1, 9, C3, [ 512 ] ],
|
||||||
|
[ -1, 1, Conv, [ 1024, 3, 2 ] ], # 7-P5/32
|
||||||
|
[ -1, 3, C3, [ 1024 ] ],
|
||||||
|
[ -1, 1, SPPF, [ 1024, 5 ] ], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head with (P3, P4) outputs
|
||||||
|
head:
|
||||||
|
[ [ -1, 1, Conv, [ 512, 1, 1 ] ],
|
||||||
|
[ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ],
|
||||||
|
[ [ -1, 6 ], 1, Concat, [ 1 ] ], # cat backbone P4
|
||||||
|
[ -1, 3, C3, [ 512, False ] ], # 13
|
||||||
|
|
||||||
|
[ -1, 1, Conv, [ 256, 1, 1 ] ],
|
||||||
|
[ -1, 1, nn.Upsample, [ None, 2, 'nearest' ] ],
|
||||||
|
[ [ -1, 4 ], 1, Concat, [ 1 ] ], # cat backbone P3
|
||||||
|
[ -1, 3, C3, [ 256, False ] ], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[ -1, 1, Conv, [ 256, 3, 2 ] ],
|
||||||
|
[ [ -1, 14 ], 1, Concat, [ 1 ] ], # cat head P4
|
||||||
|
[ -1, 3, C3, [ 512, False ] ], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[ [ 17, 20 ], 1, Detect, [ nc, anchors ] ], # Detect(P3, P4)
|
||||||
|
]
|
@ -0,0 +1,56 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors: 3 # AutoAnchor evolves 3 anchors per P output layer
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [768, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [768]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 11
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head with (P3, P4, P5, P6) outputs
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [768, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
||||||
|
[-1, 3, C3, [768, False]], # 15
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 19
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 20], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 16], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [768, 3, 2]],
|
||||||
|
[[-1, 12], 1, Concat, [1]], # cat head P6
|
||||||
|
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
||||||
|
|
||||||
|
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
||||||
|
]
|
@ -0,0 +1,67 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors: 3 # AutoAnchor evolves 3 anchors per P output layer
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [768, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [768]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, Conv, [1280, 3, 2]], # 11-P7/128
|
||||||
|
[-1, 3, C3, [1280]],
|
||||||
|
[-1, 1, SPPF, [1280, 5]], # 13
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head with (P3, P4, P5, P6, P7) outputs
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [1024, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat backbone P6
|
||||||
|
[-1, 3, C3, [1024, False]], # 17
|
||||||
|
|
||||||
|
[-1, 1, Conv, [768, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
||||||
|
[-1, 3, C3, [768, False]], # 21
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 25
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 29 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 26], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 32 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 22], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [768, False]], # 35 (P5/32-large)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [768, 3, 2]],
|
||||||
|
[[-1, 18], 1, Concat, [1]], # cat head P6
|
||||||
|
[-1, 3, C3, [1024, False]], # 38 (P6/64-xlarge)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P7
|
||||||
|
[-1, 3, C3, [1280, False]], # 41 (P7/128-xxlarge)
|
||||||
|
|
||||||
|
[[29, 32, 35, 38, 41], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6, P7)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 PANet head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,60 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [19,27, 44,40, 38,94] # P3/8
|
||||||
|
- [96,68, 86,152, 180,137] # P4/16
|
||||||
|
- [140,301, 303,264, 238,542] # P5/32
|
||||||
|
- [436,615, 739,380, 925,792] # P6/64
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [768, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [768]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 11
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [768, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
||||||
|
[-1, 3, C3, [768, False]], # 15
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 19
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 20], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 16], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [768, 3, 2]],
|
||||||
|
[[-1, 12], 1, Concat, [1]], # cat head P6
|
||||||
|
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
||||||
|
|
||||||
|
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
||||||
|
]
|
@ -0,0 +1,60 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.67 # model depth multiple
|
||||||
|
width_multiple: 0.75 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [19,27, 44,40, 38,94] # P3/8
|
||||||
|
- [96,68, 86,152, 180,137] # P4/16
|
||||||
|
- [140,301, 303,264, 238,542] # P5/32
|
||||||
|
- [436,615, 739,380, 925,792] # P6/64
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [768, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [768]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 11
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [768, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
||||||
|
[-1, 3, C3, [768, False]], # 15
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 19
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 20], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 16], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [768, 3, 2]],
|
||||||
|
[[-1, 12], 1, Concat, [1]], # cat head P6
|
||||||
|
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
||||||
|
|
||||||
|
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
||||||
|
]
|
@ -0,0 +1,60 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.33 # model depth multiple
|
||||||
|
width_multiple: 0.25 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [19,27, 44,40, 38,94] # P3/8
|
||||||
|
- [96,68, 86,152, 180,137] # P4/16
|
||||||
|
- [140,301, 303,264, 238,542] # P5/32
|
||||||
|
- [436,615, 739,380, 925,792] # P6/64
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [768, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [768]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 11
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [768, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
||||||
|
[-1, 3, C3, [768, False]], # 15
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 19
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 20], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 16], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [768, 3, 2]],
|
||||||
|
[[-1, 12], 1, Concat, [1]], # cat head P6
|
||||||
|
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
||||||
|
|
||||||
|
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
||||||
|
]
|
@ -0,0 +1,49 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
activation: nn.LeakyReLU(0.1) # <----- Conv() activation used throughout entire YOLOv5 model
|
||||||
|
depth_multiple: 0.33 # model depth multiple
|
||||||
|
width_multiple: 0.50 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.33 # model depth multiple
|
||||||
|
width_multiple: 0.50 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, GhostConv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3Ghost, [128]],
|
||||||
|
[-1, 1, GhostConv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3Ghost, [256]],
|
||||||
|
[-1, 1, GhostConv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3Ghost, [512]],
|
||||||
|
[-1, 1, GhostConv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3Ghost, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, GhostConv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3Ghost, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, GhostConv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3Ghost, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, GhostConv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3Ghost, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, GhostConv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3Ghost, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.33 # model depth multiple
|
||||||
|
width_multiple: 0.50 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3TR, [1024]], # 9 <--- C3TR() Transformer module
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,60 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.33 # model depth multiple
|
||||||
|
width_multiple: 0.50 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [19,27, 44,40, 38,94] # P3/8
|
||||||
|
- [96,68, 86,152, 180,137] # P4/16
|
||||||
|
- [140,301, 303,264, 238,542] # P5/32
|
||||||
|
- [436,615, 739,380, 925,792] # P6/64
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [768, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [768]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 11
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [768, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
||||||
|
[-1, 3, C3, [768, False]], # 15
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 19
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 20], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 16], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [768, 3, 2]],
|
||||||
|
[[-1, 12], 1, Concat, [1]], # cat head P6
|
||||||
|
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
||||||
|
|
||||||
|
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
||||||
|
]
|
@ -0,0 +1,60 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.33 # model depth multiple
|
||||||
|
width_multiple: 1.25 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [19,27, 44,40, 38,94] # P3/8
|
||||||
|
- [96,68, 86,152, 180,137] # P4/16
|
||||||
|
- [140,301, 303,264, 238,542] # P5/32
|
||||||
|
- [436,615, 739,380, 925,792] # P6/64
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [768, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [768]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 9-P6/64
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 11
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [768, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 8], 1, Concat, [1]], # cat backbone P5
|
||||||
|
[-1, 3, C3, [768, False]], # 15
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 19
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 23 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 20], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 26 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 16], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [768, False]], # 29 (P5/32-large)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [768, 3, 2]],
|
||||||
|
[[-1, 12], 1, Concat, [1]], # cat head P6
|
||||||
|
[-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge)
|
||||||
|
|
||||||
|
[[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.67 # model depth multiple
|
||||||
|
width_multiple: 0.75 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.33 # model depth multiple
|
||||||
|
width_multiple: 0.25 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.33 # model depth multiple
|
||||||
|
width_multiple: 0.5 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.33 # model depth multiple
|
||||||
|
width_multiple: 1.25 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,608 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
"""
|
||||||
|
TensorFlow, Keras and TFLite versions of YOLOv5
|
||||||
|
Authored by https://github.com/zldrobit in PR https://github.com/ultralytics/yolov5/pull/1127
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
$ python models/tf.py --weights yolov5s.pt
|
||||||
|
|
||||||
|
Export:
|
||||||
|
$ python export.py --weights yolov5s.pt --include saved_model pb tflite tfjs
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
from copy import deepcopy
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
FILE = Path(__file__).resolve()
|
||||||
|
ROOT = FILE.parents[1] # YOLOv5 root directory
|
||||||
|
if str(ROOT) not in sys.path:
|
||||||
|
sys.path.append(str(ROOT)) # add ROOT to PATH
|
||||||
|
# ROOT = ROOT.relative_to(Path.cwd()) # relative
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import tensorflow as tf
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
from tensorflow import keras
|
||||||
|
|
||||||
|
from models.common import (C3, SPP, SPPF, Bottleneck, BottleneckCSP, C3x, Concat, Conv, CrossConv, DWConv,
|
||||||
|
DWConvTranspose2d, Focus, autopad)
|
||||||
|
from models.experimental import MixConv2d, attempt_load
|
||||||
|
from models.yolo import Detect, Segment
|
||||||
|
from utils.activations import SiLU
|
||||||
|
from utils.general import LOGGER, make_divisible, print_args
|
||||||
|
|
||||||
|
|
||||||
|
class TFBN(keras.layers.Layer):
|
||||||
|
# TensorFlow BatchNormalization wrapper
|
||||||
|
def __init__(self, w=None):
|
||||||
|
super().__init__()
|
||||||
|
self.bn = keras.layers.BatchNormalization(
|
||||||
|
beta_initializer=keras.initializers.Constant(w.bias.numpy()),
|
||||||
|
gamma_initializer=keras.initializers.Constant(w.weight.numpy()),
|
||||||
|
moving_mean_initializer=keras.initializers.Constant(w.running_mean.numpy()),
|
||||||
|
moving_variance_initializer=keras.initializers.Constant(w.running_var.numpy()),
|
||||||
|
epsilon=w.eps)
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return self.bn(inputs)
|
||||||
|
|
||||||
|
|
||||||
|
class TFPad(keras.layers.Layer):
|
||||||
|
# Pad inputs in spatial dimensions 1 and 2
|
||||||
|
def __init__(self, pad):
|
||||||
|
super().__init__()
|
||||||
|
if isinstance(pad, int):
|
||||||
|
self.pad = tf.constant([[0, 0], [pad, pad], [pad, pad], [0, 0]])
|
||||||
|
else: # tuple/list
|
||||||
|
self.pad = tf.constant([[0, 0], [pad[0], pad[0]], [pad[1], pad[1]], [0, 0]])
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return tf.pad(inputs, self.pad, mode='constant', constant_values=0)
|
||||||
|
|
||||||
|
|
||||||
|
class TFConv(keras.layers.Layer):
|
||||||
|
# Standard convolution
|
||||||
|
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True, w=None):
|
||||||
|
# ch_in, ch_out, weights, kernel, stride, padding, groups
|
||||||
|
super().__init__()
|
||||||
|
assert g == 1, "TF v2.2 Conv2D does not support 'groups' argument"
|
||||||
|
# TensorFlow convolution padding is inconsistent with PyTorch (e.g. k=3 s=2 'SAME' padding)
|
||||||
|
# see https://stackoverflow.com/questions/52975843/comparing-conv2d-with-padding-between-tensorflow-and-pytorch
|
||||||
|
conv = keras.layers.Conv2D(
|
||||||
|
filters=c2,
|
||||||
|
kernel_size=k,
|
||||||
|
strides=s,
|
||||||
|
padding='SAME' if s == 1 else 'VALID',
|
||||||
|
use_bias=not hasattr(w, 'bn'),
|
||||||
|
kernel_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()),
|
||||||
|
bias_initializer='zeros' if hasattr(w, 'bn') else keras.initializers.Constant(w.conv.bias.numpy()))
|
||||||
|
self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv])
|
||||||
|
self.bn = TFBN(w.bn) if hasattr(w, 'bn') else tf.identity
|
||||||
|
self.act = activations(w.act) if act else tf.identity
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return self.act(self.bn(self.conv(inputs)))
|
||||||
|
|
||||||
|
|
||||||
|
class TFDWConv(keras.layers.Layer):
|
||||||
|
# Depthwise convolution
|
||||||
|
def __init__(self, c1, c2, k=1, s=1, p=None, act=True, w=None):
|
||||||
|
# ch_in, ch_out, weights, kernel, stride, padding, groups
|
||||||
|
super().__init__()
|
||||||
|
assert c2 % c1 == 0, f'TFDWConv() output={c2} must be a multiple of input={c1} channels'
|
||||||
|
conv = keras.layers.DepthwiseConv2D(
|
||||||
|
kernel_size=k,
|
||||||
|
depth_multiplier=c2 // c1,
|
||||||
|
strides=s,
|
||||||
|
padding='SAME' if s == 1 else 'VALID',
|
||||||
|
use_bias=not hasattr(w, 'bn'),
|
||||||
|
depthwise_initializer=keras.initializers.Constant(w.conv.weight.permute(2, 3, 1, 0).numpy()),
|
||||||
|
bias_initializer='zeros' if hasattr(w, 'bn') else keras.initializers.Constant(w.conv.bias.numpy()))
|
||||||
|
self.conv = conv if s == 1 else keras.Sequential([TFPad(autopad(k, p)), conv])
|
||||||
|
self.bn = TFBN(w.bn) if hasattr(w, 'bn') else tf.identity
|
||||||
|
self.act = activations(w.act) if act else tf.identity
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return self.act(self.bn(self.conv(inputs)))
|
||||||
|
|
||||||
|
|
||||||
|
class TFDWConvTranspose2d(keras.layers.Layer):
|
||||||
|
# Depthwise ConvTranspose2d
|
||||||
|
def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0, w=None):
|
||||||
|
# ch_in, ch_out, weights, kernel, stride, padding, groups
|
||||||
|
super().__init__()
|
||||||
|
assert c1 == c2, f'TFDWConv() output={c2} must be equal to input={c1} channels'
|
||||||
|
assert k == 4 and p1 == 1, 'TFDWConv() only valid for k=4 and p1=1'
|
||||||
|
weight, bias = w.weight.permute(2, 3, 1, 0).numpy(), w.bias.numpy()
|
||||||
|
self.c1 = c1
|
||||||
|
self.conv = [
|
||||||
|
keras.layers.Conv2DTranspose(filters=1,
|
||||||
|
kernel_size=k,
|
||||||
|
strides=s,
|
||||||
|
padding='VALID',
|
||||||
|
output_padding=p2,
|
||||||
|
use_bias=True,
|
||||||
|
kernel_initializer=keras.initializers.Constant(weight[..., i:i + 1]),
|
||||||
|
bias_initializer=keras.initializers.Constant(bias[i])) for i in range(c1)]
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return tf.concat([m(x) for m, x in zip(self.conv, tf.split(inputs, self.c1, 3))], 3)[:, 1:-1, 1:-1]
|
||||||
|
|
||||||
|
|
||||||
|
class TFFocus(keras.layers.Layer):
|
||||||
|
# Focus wh information into c-space
|
||||||
|
def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True, w=None):
|
||||||
|
# ch_in, ch_out, kernel, stride, padding, groups
|
||||||
|
super().__init__()
|
||||||
|
self.conv = TFConv(c1 * 4, c2, k, s, p, g, act, w.conv)
|
||||||
|
|
||||||
|
def call(self, inputs): # x(b,w,h,c) -> y(b,w/2,h/2,4c)
|
||||||
|
# inputs = inputs / 255 # normalize 0-255 to 0-1
|
||||||
|
inputs = [inputs[:, ::2, ::2, :], inputs[:, 1::2, ::2, :], inputs[:, ::2, 1::2, :], inputs[:, 1::2, 1::2, :]]
|
||||||
|
return self.conv(tf.concat(inputs, 3))
|
||||||
|
|
||||||
|
|
||||||
|
class TFBottleneck(keras.layers.Layer):
|
||||||
|
# Standard bottleneck
|
||||||
|
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5, w=None): # ch_in, ch_out, shortcut, groups, expansion
|
||||||
|
super().__init__()
|
||||||
|
c_ = int(c2 * e) # hidden channels
|
||||||
|
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
||||||
|
self.cv2 = TFConv(c_, c2, 3, 1, g=g, w=w.cv2)
|
||||||
|
self.add = shortcut and c1 == c2
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return inputs + self.cv2(self.cv1(inputs)) if self.add else self.cv2(self.cv1(inputs))
|
||||||
|
|
||||||
|
|
||||||
|
class TFCrossConv(keras.layers.Layer):
|
||||||
|
# Cross Convolution
|
||||||
|
def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False, w=None):
|
||||||
|
super().__init__()
|
||||||
|
c_ = int(c2 * e) # hidden channels
|
||||||
|
self.cv1 = TFConv(c1, c_, (1, k), (1, s), w=w.cv1)
|
||||||
|
self.cv2 = TFConv(c_, c2, (k, 1), (s, 1), g=g, w=w.cv2)
|
||||||
|
self.add = shortcut and c1 == c2
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return inputs + self.cv2(self.cv1(inputs)) if self.add else self.cv2(self.cv1(inputs))
|
||||||
|
|
||||||
|
|
||||||
|
class TFConv2d(keras.layers.Layer):
|
||||||
|
# Substitution for PyTorch nn.Conv2D
|
||||||
|
def __init__(self, c1, c2, k, s=1, g=1, bias=True, w=None):
|
||||||
|
super().__init__()
|
||||||
|
assert g == 1, "TF v2.2 Conv2D does not support 'groups' argument"
|
||||||
|
self.conv = keras.layers.Conv2D(filters=c2,
|
||||||
|
kernel_size=k,
|
||||||
|
strides=s,
|
||||||
|
padding='VALID',
|
||||||
|
use_bias=bias,
|
||||||
|
kernel_initializer=keras.initializers.Constant(
|
||||||
|
w.weight.permute(2, 3, 1, 0).numpy()),
|
||||||
|
bias_initializer=keras.initializers.Constant(w.bias.numpy()) if bias else None)
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return self.conv(inputs)
|
||||||
|
|
||||||
|
|
||||||
|
class TFBottleneckCSP(keras.layers.Layer):
|
||||||
|
# CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks
|
||||||
|
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None):
|
||||||
|
# ch_in, ch_out, number, shortcut, groups, expansion
|
||||||
|
super().__init__()
|
||||||
|
c_ = int(c2 * e) # hidden channels
|
||||||
|
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
||||||
|
self.cv2 = TFConv2d(c1, c_, 1, 1, bias=False, w=w.cv2)
|
||||||
|
self.cv3 = TFConv2d(c_, c_, 1, 1, bias=False, w=w.cv3)
|
||||||
|
self.cv4 = TFConv(2 * c_, c2, 1, 1, w=w.cv4)
|
||||||
|
self.bn = TFBN(w.bn)
|
||||||
|
self.act = lambda x: keras.activations.swish(x)
|
||||||
|
self.m = keras.Sequential([TFBottleneck(c_, c_, shortcut, g, e=1.0, w=w.m[j]) for j in range(n)])
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
y1 = self.cv3(self.m(self.cv1(inputs)))
|
||||||
|
y2 = self.cv2(inputs)
|
||||||
|
return self.cv4(self.act(self.bn(tf.concat((y1, y2), axis=3))))
|
||||||
|
|
||||||
|
|
||||||
|
class TFC3(keras.layers.Layer):
|
||||||
|
# CSP Bottleneck with 3 convolutions
|
||||||
|
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None):
|
||||||
|
# ch_in, ch_out, number, shortcut, groups, expansion
|
||||||
|
super().__init__()
|
||||||
|
c_ = int(c2 * e) # hidden channels
|
||||||
|
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
||||||
|
self.cv2 = TFConv(c1, c_, 1, 1, w=w.cv2)
|
||||||
|
self.cv3 = TFConv(2 * c_, c2, 1, 1, w=w.cv3)
|
||||||
|
self.m = keras.Sequential([TFBottleneck(c_, c_, shortcut, g, e=1.0, w=w.m[j]) for j in range(n)])
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return self.cv3(tf.concat((self.m(self.cv1(inputs)), self.cv2(inputs)), axis=3))
|
||||||
|
|
||||||
|
|
||||||
|
class TFC3x(keras.layers.Layer):
|
||||||
|
# 3 module with cross-convolutions
|
||||||
|
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5, w=None):
|
||||||
|
# ch_in, ch_out, number, shortcut, groups, expansion
|
||||||
|
super().__init__()
|
||||||
|
c_ = int(c2 * e) # hidden channels
|
||||||
|
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
||||||
|
self.cv2 = TFConv(c1, c_, 1, 1, w=w.cv2)
|
||||||
|
self.cv3 = TFConv(2 * c_, c2, 1, 1, w=w.cv3)
|
||||||
|
self.m = keras.Sequential([
|
||||||
|
TFCrossConv(c_, c_, k=3, s=1, g=g, e=1.0, shortcut=shortcut, w=w.m[j]) for j in range(n)])
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return self.cv3(tf.concat((self.m(self.cv1(inputs)), self.cv2(inputs)), axis=3))
|
||||||
|
|
||||||
|
|
||||||
|
class TFSPP(keras.layers.Layer):
|
||||||
|
# Spatial pyramid pooling layer used in YOLOv3-SPP
|
||||||
|
def __init__(self, c1, c2, k=(5, 9, 13), w=None):
|
||||||
|
super().__init__()
|
||||||
|
c_ = c1 // 2 # hidden channels
|
||||||
|
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
||||||
|
self.cv2 = TFConv(c_ * (len(k) + 1), c2, 1, 1, w=w.cv2)
|
||||||
|
self.m = [keras.layers.MaxPool2D(pool_size=x, strides=1, padding='SAME') for x in k]
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
x = self.cv1(inputs)
|
||||||
|
return self.cv2(tf.concat([x] + [m(x) for m in self.m], 3))
|
||||||
|
|
||||||
|
|
||||||
|
class TFSPPF(keras.layers.Layer):
|
||||||
|
# Spatial pyramid pooling-Fast layer
|
||||||
|
def __init__(self, c1, c2, k=5, w=None):
|
||||||
|
super().__init__()
|
||||||
|
c_ = c1 // 2 # hidden channels
|
||||||
|
self.cv1 = TFConv(c1, c_, 1, 1, w=w.cv1)
|
||||||
|
self.cv2 = TFConv(c_ * 4, c2, 1, 1, w=w.cv2)
|
||||||
|
self.m = keras.layers.MaxPool2D(pool_size=k, strides=1, padding='SAME')
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
x = self.cv1(inputs)
|
||||||
|
y1 = self.m(x)
|
||||||
|
y2 = self.m(y1)
|
||||||
|
return self.cv2(tf.concat([x, y1, y2, self.m(y2)], 3))
|
||||||
|
|
||||||
|
|
||||||
|
class TFDetect(keras.layers.Layer):
|
||||||
|
# TF YOLOv5 Detect layer
|
||||||
|
def __init__(self, nc=80, anchors=(), ch=(), imgsz=(640, 640), w=None): # detection layer
|
||||||
|
super().__init__()
|
||||||
|
self.stride = tf.convert_to_tensor(w.stride.numpy(), dtype=tf.float32)
|
||||||
|
self.nc = nc # number of classes
|
||||||
|
self.no = nc + 5 # number of outputs per anchor
|
||||||
|
self.nl = len(anchors) # number of detection layers
|
||||||
|
self.na = len(anchors[0]) // 2 # number of anchors
|
||||||
|
self.grid = [tf.zeros(1)] * self.nl # init grid
|
||||||
|
self.anchors = tf.convert_to_tensor(w.anchors.numpy(), dtype=tf.float32)
|
||||||
|
self.anchor_grid = tf.reshape(self.anchors * tf.reshape(self.stride, [self.nl, 1, 1]), [self.nl, 1, -1, 1, 2])
|
||||||
|
self.m = [TFConv2d(x, self.no * self.na, 1, w=w.m[i]) for i, x in enumerate(ch)]
|
||||||
|
self.training = False # set to False after building model
|
||||||
|
self.imgsz = imgsz
|
||||||
|
for i in range(self.nl):
|
||||||
|
ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i]
|
||||||
|
self.grid[i] = self._make_grid(nx, ny)
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
z = [] # inference output
|
||||||
|
x = []
|
||||||
|
for i in range(self.nl):
|
||||||
|
x.append(self.m[i](inputs[i]))
|
||||||
|
# x(bs,20,20,255) to x(bs,3,20,20,85)
|
||||||
|
ny, nx = self.imgsz[0] // self.stride[i], self.imgsz[1] // self.stride[i]
|
||||||
|
x[i] = tf.reshape(x[i], [-1, ny * nx, self.na, self.no])
|
||||||
|
|
||||||
|
if not self.training: # inference
|
||||||
|
y = x[i]
|
||||||
|
grid = tf.transpose(self.grid[i], [0, 2, 1, 3]) - 0.5
|
||||||
|
anchor_grid = tf.transpose(self.anchor_grid[i], [0, 2, 1, 3]) * 4
|
||||||
|
xy = (tf.sigmoid(y[..., 0:2]) * 2 + grid) * self.stride[i] # xy
|
||||||
|
wh = tf.sigmoid(y[..., 2:4]) ** 2 * anchor_grid
|
||||||
|
# Normalize xywh to 0-1 to reduce calibration error
|
||||||
|
xy /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
|
||||||
|
wh /= tf.constant([[self.imgsz[1], self.imgsz[0]]], dtype=tf.float32)
|
||||||
|
y = tf.concat([xy, wh, tf.sigmoid(y[..., 4:5 + self.nc]), y[..., 5 + self.nc:]], -1)
|
||||||
|
z.append(tf.reshape(y, [-1, self.na * ny * nx, self.no]))
|
||||||
|
|
||||||
|
return tf.transpose(x, [0, 2, 1, 3]) if self.training else (tf.concat(z, 1),)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _make_grid(nx=20, ny=20):
|
||||||
|
# yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)])
|
||||||
|
# return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float()
|
||||||
|
xv, yv = tf.meshgrid(tf.range(nx), tf.range(ny))
|
||||||
|
return tf.cast(tf.reshape(tf.stack([xv, yv], 2), [1, 1, ny * nx, 2]), dtype=tf.float32)
|
||||||
|
|
||||||
|
|
||||||
|
class TFSegment(TFDetect):
|
||||||
|
# YOLOv5 Segment head for segmentation models
|
||||||
|
def __init__(self, nc=80, anchors=(), nm=32, npr=256, ch=(), imgsz=(640, 640), w=None):
|
||||||
|
super().__init__(nc, anchors, ch, imgsz, w)
|
||||||
|
self.nm = nm # number of masks
|
||||||
|
self.npr = npr # number of protos
|
||||||
|
self.no = 5 + nc + self.nm # number of outputs per anchor
|
||||||
|
self.m = [TFConv2d(x, self.no * self.na, 1, w=w.m[i]) for i, x in enumerate(ch)] # output conv
|
||||||
|
self.proto = TFProto(ch[0], self.npr, self.nm, w=w.proto) # protos
|
||||||
|
self.detect = TFDetect.call
|
||||||
|
|
||||||
|
def call(self, x):
|
||||||
|
p = self.proto(x[0])
|
||||||
|
# p = TFUpsample(None, scale_factor=4, mode='nearest')(self.proto(x[0])) # (optional) full-size protos
|
||||||
|
p = tf.transpose(p, [0, 3, 1, 2]) # from shape(1,160,160,32) to shape(1,32,160,160)
|
||||||
|
x = self.detect(self, x)
|
||||||
|
return (x, p) if self.training else (x[0], p)
|
||||||
|
|
||||||
|
|
||||||
|
class TFProto(keras.layers.Layer):
|
||||||
|
|
||||||
|
def __init__(self, c1, c_=256, c2=32, w=None):
|
||||||
|
super().__init__()
|
||||||
|
self.cv1 = TFConv(c1, c_, k=3, w=w.cv1)
|
||||||
|
self.upsample = TFUpsample(None, scale_factor=2, mode='nearest')
|
||||||
|
self.cv2 = TFConv(c_, c_, k=3, w=w.cv2)
|
||||||
|
self.cv3 = TFConv(c_, c2, w=w.cv3)
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return self.cv3(self.cv2(self.upsample(self.cv1(inputs))))
|
||||||
|
|
||||||
|
|
||||||
|
class TFUpsample(keras.layers.Layer):
|
||||||
|
# TF version of torch.nn.Upsample()
|
||||||
|
def __init__(self, size, scale_factor, mode, w=None): # warning: all arguments needed including 'w'
|
||||||
|
super().__init__()
|
||||||
|
assert scale_factor % 2 == 0, "scale_factor must be multiple of 2"
|
||||||
|
self.upsample = lambda x: tf.image.resize(x, (x.shape[1] * scale_factor, x.shape[2] * scale_factor), mode)
|
||||||
|
# self.upsample = keras.layers.UpSampling2D(size=scale_factor, interpolation=mode)
|
||||||
|
# with default arguments: align_corners=False, half_pixel_centers=False
|
||||||
|
# self.upsample = lambda x: tf.raw_ops.ResizeNearestNeighbor(images=x,
|
||||||
|
# size=(x.shape[1] * 2, x.shape[2] * 2))
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return self.upsample(inputs)
|
||||||
|
|
||||||
|
|
||||||
|
class TFConcat(keras.layers.Layer):
|
||||||
|
# TF version of torch.concat()
|
||||||
|
def __init__(self, dimension=1, w=None):
|
||||||
|
super().__init__()
|
||||||
|
assert dimension == 1, "convert only NCHW to NHWC concat"
|
||||||
|
self.d = 3
|
||||||
|
|
||||||
|
def call(self, inputs):
|
||||||
|
return tf.concat(inputs, self.d)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_model(d, ch, model, imgsz): # model_dict, input_channels(3)
|
||||||
|
LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}")
|
||||||
|
anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple']
|
||||||
|
na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors
|
||||||
|
no = na * (nc + 5) # number of outputs = anchors * (classes + 5)
|
||||||
|
|
||||||
|
layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out
|
||||||
|
for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args
|
||||||
|
m_str = m
|
||||||
|
m = eval(m) if isinstance(m, str) else m # eval strings
|
||||||
|
for j, a in enumerate(args):
|
||||||
|
try:
|
||||||
|
args[j] = eval(a) if isinstance(a, str) else a # eval strings
|
||||||
|
except NameError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
n = max(round(n * gd), 1) if n > 1 else n # depth gain
|
||||||
|
if m in [
|
||||||
|
nn.Conv2d, Conv, DWConv, DWConvTranspose2d, Bottleneck, SPP, SPPF, MixConv2d, Focus, CrossConv,
|
||||||
|
BottleneckCSP, C3, C3x]:
|
||||||
|
c1, c2 = ch[f], args[0]
|
||||||
|
c2 = make_divisible(c2 * gw, 8) if c2 != no else c2
|
||||||
|
|
||||||
|
args = [c1, c2, *args[1:]]
|
||||||
|
if m in [BottleneckCSP, C3, C3x]:
|
||||||
|
args.insert(2, n)
|
||||||
|
n = 1
|
||||||
|
elif m is nn.BatchNorm2d:
|
||||||
|
args = [ch[f]]
|
||||||
|
elif m is Concat:
|
||||||
|
c2 = sum(ch[-1 if x == -1 else x + 1] for x in f)
|
||||||
|
elif m in [Detect, Segment]:
|
||||||
|
args.append([ch[x + 1] for x in f])
|
||||||
|
if isinstance(args[1], int): # number of anchors
|
||||||
|
args[1] = [list(range(args[1] * 2))] * len(f)
|
||||||
|
if m is Segment:
|
||||||
|
args[3] = make_divisible(args[3] * gw, 8)
|
||||||
|
args.append(imgsz)
|
||||||
|
else:
|
||||||
|
c2 = ch[f]
|
||||||
|
|
||||||
|
tf_m = eval('TF' + m_str.replace('nn.', ''))
|
||||||
|
m_ = keras.Sequential([tf_m(*args, w=model.model[i][j]) for j in range(n)]) if n > 1 \
|
||||||
|
else tf_m(*args, w=model.model[i]) # module
|
||||||
|
|
||||||
|
torch_m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module
|
||||||
|
t = str(m)[8:-2].replace('__main__.', '') # module type
|
||||||
|
np = sum(x.numel() for x in torch_m_.parameters()) # number params
|
||||||
|
m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params
|
||||||
|
LOGGER.info(f'{i:>3}{str(f):>18}{str(n):>3}{np:>10} {t:<40}{str(args):<30}') # print
|
||||||
|
save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist
|
||||||
|
layers.append(m_)
|
||||||
|
ch.append(c2)
|
||||||
|
return keras.Sequential(layers), sorted(save)
|
||||||
|
|
||||||
|
|
||||||
|
class TFModel:
|
||||||
|
# TF YOLOv5 model
|
||||||
|
def __init__(self, cfg='yolov5s.yaml', ch=3, nc=None, model=None, imgsz=(640, 640)): # model, channels, classes
|
||||||
|
super().__init__()
|
||||||
|
if isinstance(cfg, dict):
|
||||||
|
self.yaml = cfg # model dict
|
||||||
|
else: # is *.yaml
|
||||||
|
import yaml # for torch hub
|
||||||
|
self.yaml_file = Path(cfg).name
|
||||||
|
with open(cfg) as f:
|
||||||
|
self.yaml = yaml.load(f, Loader=yaml.FullLoader) # model dict
|
||||||
|
|
||||||
|
# Define model
|
||||||
|
if nc and nc != self.yaml['nc']:
|
||||||
|
LOGGER.info(f"Overriding {cfg} nc={self.yaml['nc']} with nc={nc}")
|
||||||
|
self.yaml['nc'] = nc # override yaml value
|
||||||
|
self.model, self.savelist = parse_model(deepcopy(self.yaml), ch=[ch], model=model, imgsz=imgsz)
|
||||||
|
|
||||||
|
def predict(self,
|
||||||
|
inputs,
|
||||||
|
tf_nms=False,
|
||||||
|
agnostic_nms=False,
|
||||||
|
topk_per_class=100,
|
||||||
|
topk_all=100,
|
||||||
|
iou_thres=0.45,
|
||||||
|
conf_thres=0.25):
|
||||||
|
y = [] # outputs
|
||||||
|
x = inputs
|
||||||
|
for m in self.model.layers:
|
||||||
|
if m.f != -1: # if not from previous layer
|
||||||
|
x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
|
||||||
|
|
||||||
|
x = m(x) # run
|
||||||
|
y.append(x if m.i in self.savelist else None) # save output
|
||||||
|
|
||||||
|
# Add TensorFlow NMS
|
||||||
|
if tf_nms:
|
||||||
|
boxes = self._xywh2xyxy(x[0][..., :4])
|
||||||
|
probs = x[0][:, :, 4:5]
|
||||||
|
classes = x[0][:, :, 5:]
|
||||||
|
scores = probs * classes
|
||||||
|
if agnostic_nms:
|
||||||
|
nms = AgnosticNMS()((boxes, classes, scores), topk_all, iou_thres, conf_thres)
|
||||||
|
else:
|
||||||
|
boxes = tf.expand_dims(boxes, 2)
|
||||||
|
nms = tf.image.combined_non_max_suppression(boxes,
|
||||||
|
scores,
|
||||||
|
topk_per_class,
|
||||||
|
topk_all,
|
||||||
|
iou_thres,
|
||||||
|
conf_thres,
|
||||||
|
clip_boxes=False)
|
||||||
|
return (nms,)
|
||||||
|
return x # output [1,6300,85] = [xywh, conf, class0, class1, ...]
|
||||||
|
# x = x[0] # [x(1,6300,85), ...] to x(6300,85)
|
||||||
|
# xywh = x[..., :4] # x(6300,4) boxes
|
||||||
|
# conf = x[..., 4:5] # x(6300,1) confidences
|
||||||
|
# cls = tf.reshape(tf.cast(tf.argmax(x[..., 5:], axis=1), tf.float32), (-1, 1)) # x(6300,1) classes
|
||||||
|
# return tf.concat([conf, cls, xywh], 1)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _xywh2xyxy(xywh):
|
||||||
|
# Convert nx4 boxes from [x, y, w, h] to [x1, y1, x2, y2] where xy1=top-left, xy2=bottom-right
|
||||||
|
x, y, w, h = tf.split(xywh, num_or_size_splits=4, axis=-1)
|
||||||
|
return tf.concat([x - w / 2, y - h / 2, x + w / 2, y + h / 2], axis=-1)
|
||||||
|
|
||||||
|
|
||||||
|
class AgnosticNMS(keras.layers.Layer):
|
||||||
|
# TF Agnostic NMS
|
||||||
|
def call(self, input, topk_all, iou_thres, conf_thres):
|
||||||
|
# wrap map_fn to avoid TypeSpec related error https://stackoverflow.com/a/65809989/3036450
|
||||||
|
return tf.map_fn(lambda x: self._nms(x, topk_all, iou_thres, conf_thres),
|
||||||
|
input,
|
||||||
|
fn_output_signature=(tf.float32, tf.float32, tf.float32, tf.int32),
|
||||||
|
name='agnostic_nms')
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _nms(x, topk_all=100, iou_thres=0.45, conf_thres=0.25): # agnostic NMS
|
||||||
|
boxes, classes, scores = x
|
||||||
|
class_inds = tf.cast(tf.argmax(classes, axis=-1), tf.float32)
|
||||||
|
scores_inp = tf.reduce_max(scores, -1)
|
||||||
|
selected_inds = tf.image.non_max_suppression(boxes,
|
||||||
|
scores_inp,
|
||||||
|
max_output_size=topk_all,
|
||||||
|
iou_threshold=iou_thres,
|
||||||
|
score_threshold=conf_thres)
|
||||||
|
selected_boxes = tf.gather(boxes, selected_inds)
|
||||||
|
padded_boxes = tf.pad(selected_boxes,
|
||||||
|
paddings=[[0, topk_all - tf.shape(selected_boxes)[0]], [0, 0]],
|
||||||
|
mode="CONSTANT",
|
||||||
|
constant_values=0.0)
|
||||||
|
selected_scores = tf.gather(scores_inp, selected_inds)
|
||||||
|
padded_scores = tf.pad(selected_scores,
|
||||||
|
paddings=[[0, topk_all - tf.shape(selected_boxes)[0]]],
|
||||||
|
mode="CONSTANT",
|
||||||
|
constant_values=-1.0)
|
||||||
|
selected_classes = tf.gather(class_inds, selected_inds)
|
||||||
|
padded_classes = tf.pad(selected_classes,
|
||||||
|
paddings=[[0, topk_all - tf.shape(selected_boxes)[0]]],
|
||||||
|
mode="CONSTANT",
|
||||||
|
constant_values=-1.0)
|
||||||
|
valid_detections = tf.shape(selected_inds)[0]
|
||||||
|
return padded_boxes, padded_scores, padded_classes, valid_detections
|
||||||
|
|
||||||
|
|
||||||
|
def activations(act=nn.SiLU):
|
||||||
|
# Returns TF activation from input PyTorch activation
|
||||||
|
if isinstance(act, nn.LeakyReLU):
|
||||||
|
return lambda x: keras.activations.relu(x, alpha=0.1)
|
||||||
|
elif isinstance(act, nn.Hardswish):
|
||||||
|
return lambda x: x * tf.nn.relu6(x + 3) * 0.166666667
|
||||||
|
elif isinstance(act, (nn.SiLU, SiLU)):
|
||||||
|
return lambda x: keras.activations.swish(x)
|
||||||
|
else:
|
||||||
|
raise Exception(f'no matching TensorFlow activation found for PyTorch activation {act}')
|
||||||
|
|
||||||
|
|
||||||
|
def representative_dataset_gen(dataset, ncalib=100):
|
||||||
|
# Representative dataset generator for use with converter.representative_dataset, returns a generator of np arrays
|
||||||
|
for n, (path, img, im0s, vid_cap, string) in enumerate(dataset):
|
||||||
|
im = np.transpose(img, [1, 2, 0])
|
||||||
|
im = np.expand_dims(im, axis=0).astype(np.float32)
|
||||||
|
im /= 255
|
||||||
|
yield [im]
|
||||||
|
if n >= ncalib:
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
def run(
|
||||||
|
weights=ROOT / 'yolov5s.pt', # weights path
|
||||||
|
imgsz=(640, 640), # inference size h,w
|
||||||
|
batch_size=1, # batch size
|
||||||
|
dynamic=False, # dynamic batch size
|
||||||
|
):
|
||||||
|
# PyTorch model
|
||||||
|
im = torch.zeros((batch_size, 3, *imgsz)) # BCHW image
|
||||||
|
model = attempt_load(weights, device=torch.device('cpu'), inplace=True, fuse=False)
|
||||||
|
_ = model(im) # inference
|
||||||
|
model.info()
|
||||||
|
|
||||||
|
# TensorFlow model
|
||||||
|
im = tf.zeros((batch_size, *imgsz, 3)) # BHWC image
|
||||||
|
tf_model = TFModel(cfg=model.yaml, model=model, nc=model.nc, imgsz=imgsz)
|
||||||
|
_ = tf_model.predict(im) # inference
|
||||||
|
|
||||||
|
# Keras model
|
||||||
|
im = keras.Input(shape=(*imgsz, 3), batch_size=None if dynamic else batch_size)
|
||||||
|
keras_model = keras.Model(inputs=im, outputs=tf_model.predict(im))
|
||||||
|
keras_model.summary()
|
||||||
|
|
||||||
|
LOGGER.info('PyTorch, TensorFlow and Keras models successfully verified.\nUse export.py for TF model export.')
|
||||||
|
|
||||||
|
|
||||||
|
def parse_opt():
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--weights', type=str, default=ROOT / 'yolov5s.pt', help='weights path')
|
||||||
|
parser.add_argument('--imgsz', '--img', '--img-size', nargs='+', type=int, default=[640], help='inference size h,w')
|
||||||
|
parser.add_argument('--batch-size', type=int, default=1, help='batch size')
|
||||||
|
parser.add_argument('--dynamic', action='store_true', help='dynamic batch size')
|
||||||
|
opt = parser.parse_args()
|
||||||
|
opt.imgsz *= 2 if len(opt.imgsz) == 1 else 1 # expand
|
||||||
|
print_args(vars(opt))
|
||||||
|
return opt
|
||||||
|
|
||||||
|
|
||||||
|
def main(opt):
|
||||||
|
run(**vars(opt))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
opt = parse_opt()
|
||||||
|
main(opt)
|
@ -0,0 +1,391 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
"""
|
||||||
|
YOLO-specific modules
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
$ python models/yolo.py --cfg yolov5s.yaml
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import contextlib
|
||||||
|
import os
|
||||||
|
import platform
|
||||||
|
import sys
|
||||||
|
from copy import deepcopy
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
FILE = Path(__file__).resolve()
|
||||||
|
ROOT = FILE.parents[1] # YOLOv5 root directory
|
||||||
|
if str(ROOT) not in sys.path:
|
||||||
|
sys.path.append(str(ROOT)) # add ROOT to PATH
|
||||||
|
if platform.system() != 'Windows':
|
||||||
|
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
|
||||||
|
|
||||||
|
from models.common import *
|
||||||
|
from models.experimental import *
|
||||||
|
from utils.autoanchor import check_anchor_order
|
||||||
|
from utils.general import LOGGER, check_version, check_yaml, make_divisible, print_args
|
||||||
|
from utils.plots import feature_visualization
|
||||||
|
from utils.torch_utils import (fuse_conv_and_bn, initialize_weights, model_info, profile, scale_img, select_device,
|
||||||
|
time_sync)
|
||||||
|
|
||||||
|
try:
|
||||||
|
import thop # for FLOPs computation
|
||||||
|
except ImportError:
|
||||||
|
thop = None
|
||||||
|
|
||||||
|
|
||||||
|
class Detect(nn.Module):
|
||||||
|
# YOLOv5 Detect head for detection models
|
||||||
|
stride = None # strides computed during build
|
||||||
|
dynamic = False # force grid reconstruction
|
||||||
|
export = False # export mode
|
||||||
|
|
||||||
|
def __init__(self, nc=80, anchors=(), ch=(), inplace=True): # detection layer
|
||||||
|
super().__init__()
|
||||||
|
self.nc = nc # number of classes
|
||||||
|
self.no = nc + 5 # number of outputs per anchor
|
||||||
|
self.nl = len(anchors) # number of detection layers
|
||||||
|
self.na = len(anchors[0]) // 2 # number of anchors
|
||||||
|
self.grid = [torch.empty(0) for _ in range(self.nl)] # init grid
|
||||||
|
self.anchor_grid = [torch.empty(0) for _ in range(self.nl)] # init anchor grid
|
||||||
|
self.register_buffer('anchors', torch.tensor(anchors).float().view(self.nl, -1, 2)) # shape(nl,na,2)
|
||||||
|
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
|
||||||
|
self.inplace = inplace # use inplace ops (e.g. slice assignment)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
z = [] # inference output
|
||||||
|
for i in range(self.nl):
|
||||||
|
x[i] = self.m[i](x[i]) # conv
|
||||||
|
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
|
||||||
|
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
|
||||||
|
|
||||||
|
if not self.training: # inference
|
||||||
|
if self.dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
|
||||||
|
self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)
|
||||||
|
|
||||||
|
if isinstance(self, Segment): # (boxes + masks)
|
||||||
|
xy, wh, conf, mask = x[i].split((2, 2, self.nc + 1, self.no - self.nc - 5), 4)
|
||||||
|
xy = (xy.sigmoid() * 2 + self.grid[i]) * self.stride[i] # xy
|
||||||
|
wh = (wh.sigmoid() * 2) ** 2 * self.anchor_grid[i] # wh
|
||||||
|
y = torch.cat((xy, wh, conf.sigmoid(), mask), 4)
|
||||||
|
else: # Detect (boxes only)
|
||||||
|
xy, wh, conf = x[i].sigmoid().split((2, 2, self.nc + 1), 4)
|
||||||
|
xy = (xy * 2 + self.grid[i]) * self.stride[i] # xy
|
||||||
|
wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh
|
||||||
|
y = torch.cat((xy, wh, conf), 4)
|
||||||
|
z.append(y.view(bs, self.na * nx * ny, self.no))
|
||||||
|
|
||||||
|
return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x)
|
||||||
|
|
||||||
|
def _make_grid(self, nx=20, ny=20, i=0, torch_1_10=check_version(torch.__version__, '1.10.0')):
|
||||||
|
d = self.anchors[i].device
|
||||||
|
t = self.anchors[i].dtype
|
||||||
|
shape = 1, self.na, ny, nx, 2 # grid shape
|
||||||
|
y, x = torch.arange(ny, device=d, dtype=t), torch.arange(nx, device=d, dtype=t)
|
||||||
|
yv, xv = torch.meshgrid(y, x, indexing='ij') if torch_1_10 else torch.meshgrid(y, x) # torch>=0.7 compatibility
|
||||||
|
grid = torch.stack((xv, yv), 2).expand(shape) - 0.5 # add grid offset, i.e. y = 2.0 * x - 0.5
|
||||||
|
anchor_grid = (self.anchors[i] * self.stride[i]).view((1, self.na, 1, 1, 2)).expand(shape)
|
||||||
|
return grid, anchor_grid
|
||||||
|
|
||||||
|
|
||||||
|
class Segment(Detect):
|
||||||
|
# YOLOv5 Segment head for segmentation models
|
||||||
|
def __init__(self, nc=80, anchors=(), nm=32, npr=256, ch=(), inplace=True):
|
||||||
|
super().__init__(nc, anchors, ch, inplace)
|
||||||
|
self.nm = nm # number of masks
|
||||||
|
self.npr = npr # number of protos
|
||||||
|
self.no = 5 + nc + self.nm # number of outputs per anchor
|
||||||
|
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
|
||||||
|
self.proto = Proto(ch[0], self.npr, self.nm) # protos
|
||||||
|
self.detect = Detect.forward
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
p = self.proto(x[0])
|
||||||
|
x = self.detect(self, x)
|
||||||
|
return (x, p) if self.training else (x[0], p) if self.export else (x[0], p, x[1])
|
||||||
|
|
||||||
|
|
||||||
|
class BaseModel(nn.Module):
|
||||||
|
# YOLOv5 base model
|
||||||
|
def forward(self, x, profile=False, visualize=False):
|
||||||
|
return self._forward_once(x, profile, visualize) # single-scale inference, train
|
||||||
|
|
||||||
|
def _forward_once(self, x, profile=False, visualize=False):
|
||||||
|
y, dt = [], [] # outputs
|
||||||
|
for m in self.model:
|
||||||
|
if m.f != -1: # if not from previous layer
|
||||||
|
x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
|
||||||
|
if profile:
|
||||||
|
self._profile_one_layer(m, x, dt)
|
||||||
|
x = m(x) # run
|
||||||
|
y.append(x if m.i in self.save else None) # save output
|
||||||
|
if visualize:
|
||||||
|
feature_visualization(x, m.type, m.i, save_dir=visualize)
|
||||||
|
return x
|
||||||
|
|
||||||
|
def _profile_one_layer(self, m, x, dt):
|
||||||
|
c = m == self.model[-1] # is final layer, copy input as inplace fix
|
||||||
|
o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPs
|
||||||
|
t = time_sync()
|
||||||
|
for _ in range(10):
|
||||||
|
m(x.copy() if c else x)
|
||||||
|
dt.append((time_sync() - t) * 100)
|
||||||
|
if m == self.model[0]:
|
||||||
|
LOGGER.info(f"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} module")
|
||||||
|
LOGGER.info(f'{dt[-1]:10.2f} {o:10.2f} {m.np:10.0f} {m.type}')
|
||||||
|
if c:
|
||||||
|
LOGGER.info(f"{sum(dt):10.2f} {'-':>10s} {'-':>10s} Total")
|
||||||
|
|
||||||
|
def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers
|
||||||
|
LOGGER.info('Fusing layers... ')
|
||||||
|
for m in self.model.modules():
|
||||||
|
if isinstance(m, (Conv, DWConv)) and hasattr(m, 'bn'):
|
||||||
|
m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv
|
||||||
|
delattr(m, 'bn') # remove batchnorm
|
||||||
|
m.forward = m.forward_fuse # update forward
|
||||||
|
self.info()
|
||||||
|
return self
|
||||||
|
|
||||||
|
def info(self, verbose=False, img_size=640): # print model information
|
||||||
|
model_info(self, verbose, img_size)
|
||||||
|
|
||||||
|
def _apply(self, fn):
|
||||||
|
# Apply to(), cpu(), cuda(), half() to model tensors that are not parameters or registered buffers
|
||||||
|
self = super()._apply(fn)
|
||||||
|
m = self.model[-1] # Detect()
|
||||||
|
if isinstance(m, (Detect, Segment)):
|
||||||
|
m.stride = fn(m.stride)
|
||||||
|
m.grid = list(map(fn, m.grid))
|
||||||
|
if isinstance(m.anchor_grid, list):
|
||||||
|
m.anchor_grid = list(map(fn, m.anchor_grid))
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
class DetectionModel(BaseModel):
|
||||||
|
# YOLOv5 detection model
|
||||||
|
def __init__(self, cfg='yolov5s.yaml', ch=3, nc=None, anchors=None): # model, input channels, number of classes
|
||||||
|
super().__init__()
|
||||||
|
if isinstance(cfg, dict):
|
||||||
|
self.yaml = cfg # model dict
|
||||||
|
else: # is *.yaml
|
||||||
|
import yaml # for torch hub
|
||||||
|
self.yaml_file = Path(cfg).name
|
||||||
|
with open(cfg, encoding='ascii', errors='ignore') as f:
|
||||||
|
self.yaml = yaml.safe_load(f) # model dict
|
||||||
|
|
||||||
|
# Define model
|
||||||
|
ch = self.yaml['ch'] = self.yaml.get('ch', ch) # input channels
|
||||||
|
if nc and nc != self.yaml['nc']:
|
||||||
|
LOGGER.info(f"Overriding model.yaml nc={self.yaml['nc']} with nc={nc}")
|
||||||
|
self.yaml['nc'] = nc # override yaml value
|
||||||
|
if anchors:
|
||||||
|
LOGGER.info(f'Overriding model.yaml anchors with anchors={anchors}')
|
||||||
|
self.yaml['anchors'] = round(anchors) # override yaml value
|
||||||
|
self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist
|
||||||
|
self.names = [str(i) for i in range(self.yaml['nc'])] # default names
|
||||||
|
self.inplace = self.yaml.get('inplace', True)
|
||||||
|
|
||||||
|
# Build strides, anchors
|
||||||
|
m = self.model[-1] # Detect()
|
||||||
|
if isinstance(m, (Detect, Segment)):
|
||||||
|
s = 256 # 2x min stride
|
||||||
|
m.inplace = self.inplace
|
||||||
|
forward = lambda x: self.forward(x)[0] if isinstance(m, Segment) else self.forward(x)
|
||||||
|
m.stride = torch.tensor([s / x.shape[-2] for x in forward(torch.zeros(1, ch, s, s))]) # forward
|
||||||
|
check_anchor_order(m)
|
||||||
|
m.anchors /= m.stride.view(-1, 1, 1)
|
||||||
|
self.stride = m.stride
|
||||||
|
self._initialize_biases() # only run once
|
||||||
|
|
||||||
|
# Init weights, biases
|
||||||
|
initialize_weights(self)
|
||||||
|
self.info()
|
||||||
|
LOGGER.info('')
|
||||||
|
|
||||||
|
def forward(self, x, augment=False, profile=False, visualize=False):
|
||||||
|
if augment:
|
||||||
|
return self._forward_augment(x) # augmented inference, None
|
||||||
|
return self._forward_once(x, profile, visualize) # single-scale inference, train
|
||||||
|
|
||||||
|
def _forward_augment(self, x):
|
||||||
|
img_size = x.shape[-2:] # height, width
|
||||||
|
s = [1, 0.83, 0.67] # scales
|
||||||
|
f = [None, 3, None] # flips (2-ud, 3-lr)
|
||||||
|
y = [] # outputs
|
||||||
|
for si, fi in zip(s, f):
|
||||||
|
xi = scale_img(x.flip(fi) if fi else x, si, gs=int(self.stride.max()))
|
||||||
|
yi = self._forward_once(xi)[0] # forward
|
||||||
|
# cv2.imwrite(f'img_{si}.jpg', 255 * xi[0].cpu().numpy().transpose((1, 2, 0))[:, :, ::-1]) # save
|
||||||
|
yi = self._descale_pred(yi, fi, si, img_size)
|
||||||
|
y.append(yi)
|
||||||
|
y = self._clip_augmented(y) # clip augmented tails
|
||||||
|
return torch.cat(y, 1), None # augmented inference, train
|
||||||
|
|
||||||
|
def _descale_pred(self, p, flips, scale, img_size):
|
||||||
|
# de-scale predictions following augmented inference (inverse operation)
|
||||||
|
if self.inplace:
|
||||||
|
p[..., :4] /= scale # de-scale
|
||||||
|
if flips == 2:
|
||||||
|
p[..., 1] = img_size[0] - p[..., 1] # de-flip ud
|
||||||
|
elif flips == 3:
|
||||||
|
p[..., 0] = img_size[1] - p[..., 0] # de-flip lr
|
||||||
|
else:
|
||||||
|
x, y, wh = p[..., 0:1] / scale, p[..., 1:2] / scale, p[..., 2:4] / scale # de-scale
|
||||||
|
if flips == 2:
|
||||||
|
y = img_size[0] - y # de-flip ud
|
||||||
|
elif flips == 3:
|
||||||
|
x = img_size[1] - x # de-flip lr
|
||||||
|
p = torch.cat((x, y, wh, p[..., 4:]), -1)
|
||||||
|
return p
|
||||||
|
|
||||||
|
def _clip_augmented(self, y):
|
||||||
|
# Clip YOLOv5 augmented inference tails
|
||||||
|
nl = self.model[-1].nl # number of detection layers (P3-P5)
|
||||||
|
g = sum(4 ** x for x in range(nl)) # grid points
|
||||||
|
e = 1 # exclude layer count
|
||||||
|
i = (y[0].shape[1] // g) * sum(4 ** x for x in range(e)) # indices
|
||||||
|
y[0] = y[0][:, :-i] # large
|
||||||
|
i = (y[-1].shape[1] // g) * sum(4 ** (nl - 1 - x) for x in range(e)) # indices
|
||||||
|
y[-1] = y[-1][:, i:] # small
|
||||||
|
return y
|
||||||
|
|
||||||
|
def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency
|
||||||
|
# https://arxiv.org/abs/1708.02002 section 3.3
|
||||||
|
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1.
|
||||||
|
m = self.model[-1] # Detect() module
|
||||||
|
for mi, s in zip(m.m, m.stride): # from
|
||||||
|
b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85)
|
||||||
|
b.data[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
|
||||||
|
b.data[:, 5:5 + m.nc] += math.log(0.6 / (m.nc - 0.99999)) if cf is None else torch.log(cf / cf.sum()) # cls
|
||||||
|
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True)
|
||||||
|
|
||||||
|
|
||||||
|
Model = DetectionModel # retain YOLOv5 'Model' class for backwards compatibility
|
||||||
|
|
||||||
|
|
||||||
|
class SegmentationModel(DetectionModel):
|
||||||
|
# YOLOv5 segmentation model
|
||||||
|
def __init__(self, cfg='yolov5s-seg.yaml', ch=3, nc=None, anchors=None):
|
||||||
|
super().__init__(cfg, ch, nc, anchors)
|
||||||
|
|
||||||
|
|
||||||
|
class ClassificationModel(BaseModel):
|
||||||
|
# YOLOv5 classification model
|
||||||
|
def __init__(self, cfg=None, model=None, nc=1000, cutoff=10): # yaml, model, number of classes, cutoff index
|
||||||
|
super().__init__()
|
||||||
|
self._from_detection_model(model, nc, cutoff) if model is not None else self._from_yaml(cfg)
|
||||||
|
|
||||||
|
def _from_detection_model(self, model, nc=1000, cutoff=10):
|
||||||
|
# Create a YOLOv5 classification model from a YOLOv5 detection model
|
||||||
|
if isinstance(model, DetectMultiBackend):
|
||||||
|
model = model.model # unwrap DetectMultiBackend
|
||||||
|
model.model = model.model[:cutoff] # backbone
|
||||||
|
m = model.model[-1] # last layer
|
||||||
|
ch = m.conv.in_channels if hasattr(m, 'conv') else m.cv1.conv.in_channels # ch into module
|
||||||
|
c = Classify(ch, nc) # Classify()
|
||||||
|
c.i, c.f, c.type = m.i, m.f, 'models.common.Classify' # index, from, type
|
||||||
|
model.model[-1] = c # replace
|
||||||
|
self.model = model.model
|
||||||
|
self.stride = model.stride
|
||||||
|
self.save = []
|
||||||
|
self.nc = nc
|
||||||
|
|
||||||
|
def _from_yaml(self, cfg):
|
||||||
|
# Create a YOLOv5 classification model from a *.yaml file
|
||||||
|
self.model = None
|
||||||
|
|
||||||
|
|
||||||
|
def parse_model(d, ch): # model_dict, input_channels(3)
|
||||||
|
# Parse a YOLOv5 model.yaml dictionary
|
||||||
|
LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}")
|
||||||
|
anchors, nc, gd, gw, act = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'], d.get('activation')
|
||||||
|
if act:
|
||||||
|
Conv.default_act = eval(act) # redefine default activation, i.e. Conv.default_act = nn.SiLU()
|
||||||
|
LOGGER.info(f"{colorstr('activation:')} {act}") # print
|
||||||
|
na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors
|
||||||
|
no = na * (nc + 5) # number of outputs = anchors * (classes + 5)
|
||||||
|
|
||||||
|
layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out
|
||||||
|
for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args
|
||||||
|
m = eval(m) if isinstance(m, str) else m # eval strings
|
||||||
|
for j, a in enumerate(args):
|
||||||
|
with contextlib.suppress(NameError):
|
||||||
|
args[j] = eval(a) if isinstance(a, str) else a # eval strings
|
||||||
|
|
||||||
|
n = n_ = max(round(n * gd), 1) if n > 1 else n # depth gain
|
||||||
|
if m in {
|
||||||
|
Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,
|
||||||
|
BottleneckCSP, C3, C3TR, C3SPP, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x}:
|
||||||
|
c1, c2 = ch[f], args[0]
|
||||||
|
if c2 != no: # if not output
|
||||||
|
c2 = make_divisible(c2 * gw, 8)
|
||||||
|
|
||||||
|
args = [c1, c2, *args[1:]]
|
||||||
|
if m in {BottleneckCSP, C3, C3TR, C3Ghost, C3x}:
|
||||||
|
args.insert(2, n) # number of repeats
|
||||||
|
n = 1
|
||||||
|
elif m is nn.BatchNorm2d:
|
||||||
|
args = [ch[f]]
|
||||||
|
elif m is Concat:
|
||||||
|
c2 = sum(ch[x] for x in f)
|
||||||
|
# TODO: channel, gw, gd
|
||||||
|
elif m in {Detect, Segment}:
|
||||||
|
args.append([ch[x] for x in f])
|
||||||
|
if isinstance(args[1], int): # number of anchors
|
||||||
|
args[1] = [list(range(args[1] * 2))] * len(f)
|
||||||
|
if m is Segment:
|
||||||
|
args[3] = make_divisible(args[3] * gw, 8)
|
||||||
|
elif m is Contract:
|
||||||
|
c2 = ch[f] * args[0] ** 2
|
||||||
|
elif m is Expand:
|
||||||
|
c2 = ch[f] // args[0] ** 2
|
||||||
|
else:
|
||||||
|
c2 = ch[f]
|
||||||
|
|
||||||
|
m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module
|
||||||
|
t = str(m)[8:-2].replace('__main__.', '') # module type
|
||||||
|
np = sum(x.numel() for x in m_.parameters()) # number params
|
||||||
|
m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params
|
||||||
|
LOGGER.info(f'{i:>3}{str(f):>18}{n_:>3}{np:10.0f} {t:<40}{str(args):<30}') # print
|
||||||
|
save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist
|
||||||
|
layers.append(m_)
|
||||||
|
if i == 0:
|
||||||
|
ch = []
|
||||||
|
ch.append(c2)
|
||||||
|
return nn.Sequential(*layers), sorted(save)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument('--cfg', type=str, default='yolov5s.yaml', help='model.yaml')
|
||||||
|
parser.add_argument('--batch-size', type=int, default=1, help='total batch size for all GPUs')
|
||||||
|
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
|
||||||
|
parser.add_argument('--profile', action='store_true', help='profile model speed')
|
||||||
|
parser.add_argument('--line-profile', action='store_true', help='profile model speed layer by layer')
|
||||||
|
parser.add_argument('--test', action='store_true', help='test all yolo*.yaml')
|
||||||
|
opt = parser.parse_args()
|
||||||
|
opt.cfg = check_yaml(opt.cfg) # check YAML
|
||||||
|
print_args(vars(opt))
|
||||||
|
device = select_device(opt.device)
|
||||||
|
|
||||||
|
# Create model
|
||||||
|
im = torch.rand(opt.batch_size, 3, 640, 640).to(device)
|
||||||
|
model = Model(opt.cfg).to(device)
|
||||||
|
|
||||||
|
# Options
|
||||||
|
if opt.line_profile: # profile layer by layer
|
||||||
|
model(im, profile=True)
|
||||||
|
|
||||||
|
elif opt.profile: # profile forward-backward
|
||||||
|
results = profile(input=im, ops=[model], n=3)
|
||||||
|
|
||||||
|
elif opt.test: # test all models
|
||||||
|
for cfg in Path(ROOT / 'models').rglob('yolo*.yaml'):
|
||||||
|
try:
|
||||||
|
_ = Model(cfg)
|
||||||
|
except Exception as e:
|
||||||
|
print(f'Error in {cfg}: {e}')
|
||||||
|
|
||||||
|
else: # report fused model summary
|
||||||
|
model.fuse()
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.0 # model depth multiple
|
||||||
|
width_multiple: 1.0 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.67 # model depth multiple
|
||||||
|
width_multiple: 0.75 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.33 # model depth multiple
|
||||||
|
width_multiple: 0.25 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 0.33 # model depth multiple
|
||||||
|
width_multiple: 0.50 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,48 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
|
||||||
|
# Parameters
|
||||||
|
nc: 80 # number of classes
|
||||||
|
depth_multiple: 1.33 # model depth multiple
|
||||||
|
width_multiple: 1.25 # layer channel multiple
|
||||||
|
anchors:
|
||||||
|
- [10,13, 16,30, 33,23] # P3/8
|
||||||
|
- [30,61, 62,45, 59,119] # P4/16
|
||||||
|
- [116,90, 156,198, 373,326] # P5/32
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 backbone
|
||||||
|
backbone:
|
||||||
|
# [from, number, module, args]
|
||||||
|
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
|
||||||
|
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
|
||||||
|
[-1, 3, C3, [128]],
|
||||||
|
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
|
||||||
|
[-1, 6, C3, [256]],
|
||||||
|
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
|
||||||
|
[-1, 9, C3, [512]],
|
||||||
|
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
|
||||||
|
[-1, 3, C3, [1024]],
|
||||||
|
[-1, 1, SPPF, [1024, 5]], # 9
|
||||||
|
]
|
||||||
|
|
||||||
|
# YOLOv5 v6.0 head
|
||||||
|
head:
|
||||||
|
[[-1, 1, Conv, [512, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 6], 1, Concat, [1]], # cat backbone P4
|
||||||
|
[-1, 3, C3, [512, False]], # 13
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 1, 1]],
|
||||||
|
[-1, 1, nn.Upsample, [None, 2, 'nearest']],
|
||||||
|
[[-1, 4], 1, Concat, [1]], # cat backbone P3
|
||||||
|
[-1, 3, C3, [256, False]], # 17 (P3/8-small)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [256, 3, 2]],
|
||||||
|
[[-1, 14], 1, Concat, [1]], # cat head P4
|
||||||
|
[-1, 3, C3, [512, False]], # 20 (P4/16-medium)
|
||||||
|
|
||||||
|
[-1, 1, Conv, [512, 3, 2]],
|
||||||
|
[[-1, 10], 1, Concat, [1]], # cat head P5
|
||||||
|
[-1, 3, C3, [1024, False]], # 23 (P5/32-large)
|
||||||
|
|
||||||
|
[[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
|
||||||
|
]
|
@ -0,0 +1,80 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
"""
|
||||||
|
utils/initialization
|
||||||
|
"""
|
||||||
|
|
||||||
|
import contextlib
|
||||||
|
import platform
|
||||||
|
import threading
|
||||||
|
|
||||||
|
|
||||||
|
def emojis(str=''):
|
||||||
|
# Return platform-dependent emoji-safe version of string
|
||||||
|
return str.encode().decode('ascii', 'ignore') if platform.system() == 'Windows' else str
|
||||||
|
|
||||||
|
|
||||||
|
class TryExcept(contextlib.ContextDecorator):
|
||||||
|
# YOLOv5 TryExcept class. Usage: @TryExcept() decorator or 'with TryExcept():' context manager
|
||||||
|
def __init__(self, msg=''):
|
||||||
|
self.msg = msg
|
||||||
|
|
||||||
|
def __enter__(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __exit__(self, exc_type, value, traceback):
|
||||||
|
if value:
|
||||||
|
print(emojis(f"{self.msg}{': ' if self.msg else ''}{value}"))
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def threaded(func):
|
||||||
|
# Multi-threads a target function and returns thread. Usage: @threaded decorator
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
thread = threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True)
|
||||||
|
thread.start()
|
||||||
|
return thread
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def join_threads(verbose=False):
|
||||||
|
# Join all daemon threads, i.e. atexit.register(lambda: join_threads())
|
||||||
|
main_thread = threading.current_thread()
|
||||||
|
for t in threading.enumerate():
|
||||||
|
if t is not main_thread:
|
||||||
|
if verbose:
|
||||||
|
print(f'Joining thread {t.name}')
|
||||||
|
t.join()
|
||||||
|
|
||||||
|
|
||||||
|
def notebook_init(verbose=True):
|
||||||
|
# Check system software and hardware
|
||||||
|
print('Checking setup...')
|
||||||
|
|
||||||
|
import os
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
from utils.general import check_font, check_requirements, is_colab
|
||||||
|
from utils.torch_utils import select_device # imports
|
||||||
|
|
||||||
|
check_font()
|
||||||
|
|
||||||
|
import psutil
|
||||||
|
from IPython import display # to display images and clear console output
|
||||||
|
|
||||||
|
if is_colab():
|
||||||
|
shutil.rmtree('/content/sample_data', ignore_errors=True) # remove colab /sample_data directory
|
||||||
|
|
||||||
|
# System info
|
||||||
|
if verbose:
|
||||||
|
gb = 1 << 30 # bytes to GiB (1024 ** 3)
|
||||||
|
ram = psutil.virtual_memory().total
|
||||||
|
total, used, free = shutil.disk_usage("/")
|
||||||
|
display.clear_output()
|
||||||
|
s = f'({os.cpu_count()} CPUs, {ram / gb:.1f} GB RAM, {(total - free) / gb:.1f}/{total / gb:.1f} GB disk)'
|
||||||
|
else:
|
||||||
|
s = ''
|
||||||
|
|
||||||
|
select_device(newline=False)
|
||||||
|
print(emojis(f'Setup complete ✅ {s}'))
|
||||||
|
return display
|
@ -0,0 +1,103 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
"""
|
||||||
|
Activation functions
|
||||||
|
"""
|
||||||
|
|
||||||
|
import torch
|
||||||
|
import torch.nn as nn
|
||||||
|
import torch.nn.functional as F
|
||||||
|
|
||||||
|
|
||||||
|
class SiLU(nn.Module):
|
||||||
|
# SiLU activation https://arxiv.org/pdf/1606.08415.pdf
|
||||||
|
@staticmethod
|
||||||
|
def forward(x):
|
||||||
|
return x * torch.sigmoid(x)
|
||||||
|
|
||||||
|
|
||||||
|
class Hardswish(nn.Module):
|
||||||
|
# Hard-SiLU activation
|
||||||
|
@staticmethod
|
||||||
|
def forward(x):
|
||||||
|
# return x * F.hardsigmoid(x) # for TorchScript and CoreML
|
||||||
|
return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for TorchScript, CoreML and ONNX
|
||||||
|
|
||||||
|
|
||||||
|
class Mish(nn.Module):
|
||||||
|
# Mish activation https://github.com/digantamisra98/Mish
|
||||||
|
@staticmethod
|
||||||
|
def forward(x):
|
||||||
|
return x * F.softplus(x).tanh()
|
||||||
|
|
||||||
|
|
||||||
|
class MemoryEfficientMish(nn.Module):
|
||||||
|
# Mish activation memory-efficient
|
||||||
|
class F(torch.autograd.Function):
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def forward(ctx, x):
|
||||||
|
ctx.save_for_backward(x)
|
||||||
|
return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x)))
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def backward(ctx, grad_output):
|
||||||
|
x = ctx.saved_tensors[0]
|
||||||
|
sx = torch.sigmoid(x)
|
||||||
|
fx = F.softplus(x).tanh()
|
||||||
|
return grad_output * (fx + x * sx * (1 - fx * fx))
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return self.F.apply(x)
|
||||||
|
|
||||||
|
|
||||||
|
class FReLU(nn.Module):
|
||||||
|
# FReLU activation https://arxiv.org/abs/2007.11824
|
||||||
|
def __init__(self, c1, k=3): # ch_in, kernel
|
||||||
|
super().__init__()
|
||||||
|
self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False)
|
||||||
|
self.bn = nn.BatchNorm2d(c1)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return torch.max(x, self.bn(self.conv(x)))
|
||||||
|
|
||||||
|
|
||||||
|
class AconC(nn.Module):
|
||||||
|
r""" ACON activation (activate or not)
|
||||||
|
AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter
|
||||||
|
according to "Activate or Not: Learning Customized Activation" <https://arxiv.org/pdf/2009.04759.pdf>.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, c1):
|
||||||
|
super().__init__()
|
||||||
|
self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
||||||
|
self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
||||||
|
self.beta = nn.Parameter(torch.ones(1, c1, 1, 1))
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
dpx = (self.p1 - self.p2) * x
|
||||||
|
return dpx * torch.sigmoid(self.beta * dpx) + self.p2 * x
|
||||||
|
|
||||||
|
|
||||||
|
class MetaAconC(nn.Module):
|
||||||
|
r""" ACON activation (activate or not)
|
||||||
|
MetaAconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is generated by a small network
|
||||||
|
according to "Activate or Not: Learning Customized Activation" <https://arxiv.org/pdf/2009.04759.pdf>.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, c1, k=1, s=1, r=16): # ch_in, kernel, stride, r
|
||||||
|
super().__init__()
|
||||||
|
c2 = max(r, c1 // r)
|
||||||
|
self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
||||||
|
self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1))
|
||||||
|
self.fc1 = nn.Conv2d(c1, c2, k, s, bias=True)
|
||||||
|
self.fc2 = nn.Conv2d(c2, c1, k, s, bias=True)
|
||||||
|
# self.bn1 = nn.BatchNorm2d(c2)
|
||||||
|
# self.bn2 = nn.BatchNorm2d(c1)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
y = x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True)
|
||||||
|
# batch-size 1 bug/instabilities https://github.com/ultralytics/yolov5/issues/2891
|
||||||
|
# beta = torch.sigmoid(self.bn2(self.fc2(self.bn1(self.fc1(y))))) # bug/unstable
|
||||||
|
beta = torch.sigmoid(self.fc2(self.fc1(y))) # bug patch BN layers removed
|
||||||
|
dpx = (self.p1 - self.p2) * x
|
||||||
|
return dpx * torch.sigmoid(beta * dpx) + self.p2 * x
|
@ -0,0 +1,397 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
"""
|
||||||
|
Image augmentation functions
|
||||||
|
"""
|
||||||
|
|
||||||
|
import math
|
||||||
|
import random
|
||||||
|
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import torchvision.transforms as T
|
||||||
|
import torchvision.transforms.functional as TF
|
||||||
|
|
||||||
|
from utils.general import LOGGER, check_version, colorstr, resample_segments, segment2box, xywhn2xyxy
|
||||||
|
from utils.metrics import bbox_ioa
|
||||||
|
|
||||||
|
IMAGENET_MEAN = 0.485, 0.456, 0.406 # RGB mean
|
||||||
|
IMAGENET_STD = 0.229, 0.224, 0.225 # RGB standard deviation
|
||||||
|
|
||||||
|
|
||||||
|
class Albumentations:
|
||||||
|
# YOLOv5 Albumentations class (optional, only used if package is installed)
|
||||||
|
def __init__(self, size=640):
|
||||||
|
self.transform = None
|
||||||
|
prefix = colorstr('albumentations: ')
|
||||||
|
try:
|
||||||
|
import albumentations as A
|
||||||
|
check_version(A.__version__, '1.0.3', hard=True) # version requirement
|
||||||
|
|
||||||
|
T = [
|
||||||
|
A.RandomResizedCrop(height=size, width=size, scale=(0.8, 1.0), ratio=(0.9, 1.11), p=0.0),
|
||||||
|
A.Blur(p=0.01),
|
||||||
|
A.MedianBlur(p=0.01),
|
||||||
|
A.ToGray(p=0.01),
|
||||||
|
A.CLAHE(p=0.01),
|
||||||
|
A.RandomBrightnessContrast(p=0.0),
|
||||||
|
A.RandomGamma(p=0.0),
|
||||||
|
A.ImageCompression(quality_lower=75, p=0.0)] # transforms
|
||||||
|
self.transform = A.Compose(T, bbox_params=A.BboxParams(format='yolo', label_fields=['class_labels']))
|
||||||
|
|
||||||
|
LOGGER.info(prefix + ', '.join(f'{x}'.replace('always_apply=False, ', '') for x in T if x.p))
|
||||||
|
except ImportError: # package not installed, skip
|
||||||
|
pass
|
||||||
|
except Exception as e:
|
||||||
|
LOGGER.info(f'{prefix}{e}')
|
||||||
|
|
||||||
|
def __call__(self, im, labels, p=1.0):
|
||||||
|
if self.transform and random.random() < p:
|
||||||
|
new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0]) # transformed
|
||||||
|
im, labels = new['image'], np.array([[c, *b] for c, b in zip(new['class_labels'], new['bboxes'])])
|
||||||
|
return im, labels
|
||||||
|
|
||||||
|
|
||||||
|
def normalize(x, mean=IMAGENET_MEAN, std=IMAGENET_STD, inplace=False):
|
||||||
|
# Denormalize RGB images x per ImageNet stats in BCHW format, i.e. = (x - mean) / std
|
||||||
|
return TF.normalize(x, mean, std, inplace=inplace)
|
||||||
|
|
||||||
|
|
||||||
|
def denormalize(x, mean=IMAGENET_MEAN, std=IMAGENET_STD):
|
||||||
|
# Denormalize RGB images x per ImageNet stats in BCHW format, i.e. = x * std + mean
|
||||||
|
for i in range(3):
|
||||||
|
x[:, i] = x[:, i] * std[i] + mean[i]
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
def augment_hsv(im, hgain=0.5, sgain=0.5, vgain=0.5):
|
||||||
|
# HSV color-space augmentation
|
||||||
|
if hgain or sgain or vgain:
|
||||||
|
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
|
||||||
|
hue, sat, val = cv2.split(cv2.cvtColor(im, cv2.COLOR_BGR2HSV))
|
||||||
|
dtype = im.dtype # uint8
|
||||||
|
|
||||||
|
x = np.arange(0, 256, dtype=r.dtype)
|
||||||
|
lut_hue = ((x * r[0]) % 180).astype(dtype)
|
||||||
|
lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
|
||||||
|
lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
|
||||||
|
|
||||||
|
im_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val)))
|
||||||
|
cv2.cvtColor(im_hsv, cv2.COLOR_HSV2BGR, dst=im) # no return needed
|
||||||
|
|
||||||
|
|
||||||
|
def hist_equalize(im, clahe=True, bgr=False):
|
||||||
|
# Equalize histogram on BGR image 'im' with im.shape(n,m,3) and range 0-255
|
||||||
|
yuv = cv2.cvtColor(im, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV)
|
||||||
|
if clahe:
|
||||||
|
c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
|
||||||
|
yuv[:, :, 0] = c.apply(yuv[:, :, 0])
|
||||||
|
else:
|
||||||
|
yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0]) # equalize Y channel histogram
|
||||||
|
return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB) # convert YUV image to RGB
|
||||||
|
|
||||||
|
|
||||||
|
def replicate(im, labels):
|
||||||
|
# Replicate labels
|
||||||
|
h, w = im.shape[:2]
|
||||||
|
boxes = labels[:, 1:].astype(int)
|
||||||
|
x1, y1, x2, y2 = boxes.T
|
||||||
|
s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels)
|
||||||
|
for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices
|
||||||
|
x1b, y1b, x2b, y2b = boxes[i]
|
||||||
|
bh, bw = y2b - y1b, x2b - x1b
|
||||||
|
yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y
|
||||||
|
x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh]
|
||||||
|
im[y1a:y2a, x1a:x2a] = im[y1b:y2b, x1b:x2b] # im4[ymin:ymax, xmin:xmax]
|
||||||
|
labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0)
|
||||||
|
|
||||||
|
return im, labels
|
||||||
|
|
||||||
|
|
||||||
|
def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
|
||||||
|
# Resize and pad image while meeting stride-multiple constraints
|
||||||
|
shape = im.shape[:2] # current shape [height, width]
|
||||||
|
if isinstance(new_shape, int):
|
||||||
|
new_shape = (new_shape, new_shape)
|
||||||
|
|
||||||
|
# Scale ratio (new / old)
|
||||||
|
r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
|
||||||
|
if not scaleup: # only scale down, do not scale up (for better val mAP)
|
||||||
|
r = min(r, 1.0)
|
||||||
|
|
||||||
|
# Compute padding
|
||||||
|
ratio = r, r # width, height ratios
|
||||||
|
new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
|
||||||
|
dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
|
||||||
|
if auto: # minimum rectangle
|
||||||
|
dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding
|
||||||
|
elif scaleFill: # stretch
|
||||||
|
dw, dh = 0.0, 0.0
|
||||||
|
new_unpad = (new_shape[1], new_shape[0])
|
||||||
|
ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
|
||||||
|
|
||||||
|
dw /= 2 # divide padding into 2 sides
|
||||||
|
dh /= 2
|
||||||
|
|
||||||
|
if shape[::-1] != new_unpad: # resize
|
||||||
|
im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
|
||||||
|
top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
|
||||||
|
left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
|
||||||
|
im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
|
||||||
|
return im, ratio, (dw, dh)
|
||||||
|
|
||||||
|
|
||||||
|
def random_perspective(im,
|
||||||
|
targets=(),
|
||||||
|
segments=(),
|
||||||
|
degrees=10,
|
||||||
|
translate=.1,
|
||||||
|
scale=.1,
|
||||||
|
shear=10,
|
||||||
|
perspective=0.0,
|
||||||
|
border=(0, 0)):
|
||||||
|
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(0.1, 0.1), scale=(0.9, 1.1), shear=(-10, 10))
|
||||||
|
# targets = [cls, xyxy]
|
||||||
|
|
||||||
|
height = im.shape[0] + border[0] * 2 # shape(h,w,c)
|
||||||
|
width = im.shape[1] + border[1] * 2
|
||||||
|
|
||||||
|
# Center
|
||||||
|
C = np.eye(3)
|
||||||
|
C[0, 2] = -im.shape[1] / 2 # x translation (pixels)
|
||||||
|
C[1, 2] = -im.shape[0] / 2 # y translation (pixels)
|
||||||
|
|
||||||
|
# Perspective
|
||||||
|
P = np.eye(3)
|
||||||
|
P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y)
|
||||||
|
P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x)
|
||||||
|
|
||||||
|
# Rotation and Scale
|
||||||
|
R = np.eye(3)
|
||||||
|
a = random.uniform(-degrees, degrees)
|
||||||
|
# a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations
|
||||||
|
s = random.uniform(1 - scale, 1 + scale)
|
||||||
|
# s = 2 ** random.uniform(-scale, scale)
|
||||||
|
R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s)
|
||||||
|
|
||||||
|
# Shear
|
||||||
|
S = np.eye(3)
|
||||||
|
S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg)
|
||||||
|
S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg)
|
||||||
|
|
||||||
|
# Translation
|
||||||
|
T = np.eye(3)
|
||||||
|
T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels)
|
||||||
|
T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels)
|
||||||
|
|
||||||
|
# Combined rotation matrix
|
||||||
|
M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT
|
||||||
|
if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed
|
||||||
|
if perspective:
|
||||||
|
im = cv2.warpPerspective(im, M, dsize=(width, height), borderValue=(114, 114, 114))
|
||||||
|
else: # affine
|
||||||
|
im = cv2.warpAffine(im, M[:2], dsize=(width, height), borderValue=(114, 114, 114))
|
||||||
|
|
||||||
|
# Visualize
|
||||||
|
# import matplotlib.pyplot as plt
|
||||||
|
# ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel()
|
||||||
|
# ax[0].imshow(im[:, :, ::-1]) # base
|
||||||
|
# ax[1].imshow(im2[:, :, ::-1]) # warped
|
||||||
|
|
||||||
|
# Transform label coordinates
|
||||||
|
n = len(targets)
|
||||||
|
if n:
|
||||||
|
use_segments = any(x.any() for x in segments)
|
||||||
|
new = np.zeros((n, 4))
|
||||||
|
if use_segments: # warp segments
|
||||||
|
segments = resample_segments(segments) # upsample
|
||||||
|
for i, segment in enumerate(segments):
|
||||||
|
xy = np.ones((len(segment), 3))
|
||||||
|
xy[:, :2] = segment
|
||||||
|
xy = xy @ M.T # transform
|
||||||
|
xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine
|
||||||
|
|
||||||
|
# clip
|
||||||
|
new[i] = segment2box(xy, width, height)
|
||||||
|
|
||||||
|
else: # warp boxes
|
||||||
|
xy = np.ones((n * 4, 3))
|
||||||
|
xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1
|
||||||
|
xy = xy @ M.T # transform
|
||||||
|
xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]).reshape(n, 8) # perspective rescale or affine
|
||||||
|
|
||||||
|
# create new boxes
|
||||||
|
x = xy[:, [0, 2, 4, 6]]
|
||||||
|
y = xy[:, [1, 3, 5, 7]]
|
||||||
|
new = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T
|
||||||
|
|
||||||
|
# clip
|
||||||
|
new[:, [0, 2]] = new[:, [0, 2]].clip(0, width)
|
||||||
|
new[:, [1, 3]] = new[:, [1, 3]].clip(0, height)
|
||||||
|
|
||||||
|
# filter candidates
|
||||||
|
i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01 if use_segments else 0.10)
|
||||||
|
targets = targets[i]
|
||||||
|
targets[:, 1:5] = new[i]
|
||||||
|
|
||||||
|
return im, targets
|
||||||
|
|
||||||
|
|
||||||
|
def copy_paste(im, labels, segments, p=0.5):
|
||||||
|
# Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
|
||||||
|
n = len(segments)
|
||||||
|
if p and n:
|
||||||
|
h, w, c = im.shape # height, width, channels
|
||||||
|
im_new = np.zeros(im.shape, np.uint8)
|
||||||
|
for j in random.sample(range(n), k=round(p * n)):
|
||||||
|
l, s = labels[j], segments[j]
|
||||||
|
box = w - l[3], l[2], w - l[1], l[4]
|
||||||
|
ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
|
||||||
|
if (ioa < 0.30).all(): # allow 30% obscuration of existing labels
|
||||||
|
labels = np.concatenate((labels, [[l[0], *box]]), 0)
|
||||||
|
segments.append(np.concatenate((w - s[:, 0:1], s[:, 1:2]), 1))
|
||||||
|
cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (1, 1, 1), cv2.FILLED)
|
||||||
|
|
||||||
|
result = cv2.flip(im, 1) # augment segments (flip left-right)
|
||||||
|
i = cv2.flip(im_new, 1).astype(bool)
|
||||||
|
im[i] = result[i] # cv2.imwrite('debug.jpg', im) # debug
|
||||||
|
|
||||||
|
return im, labels, segments
|
||||||
|
|
||||||
|
|
||||||
|
def cutout(im, labels, p=0.5):
|
||||||
|
# Applies image cutout augmentation https://arxiv.org/abs/1708.04552
|
||||||
|
if random.random() < p:
|
||||||
|
h, w = im.shape[:2]
|
||||||
|
scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction
|
||||||
|
for s in scales:
|
||||||
|
mask_h = random.randint(1, int(h * s)) # create random masks
|
||||||
|
mask_w = random.randint(1, int(w * s))
|
||||||
|
|
||||||
|
# box
|
||||||
|
xmin = max(0, random.randint(0, w) - mask_w // 2)
|
||||||
|
ymin = max(0, random.randint(0, h) - mask_h // 2)
|
||||||
|
xmax = min(w, xmin + mask_w)
|
||||||
|
ymax = min(h, ymin + mask_h)
|
||||||
|
|
||||||
|
# apply random color mask
|
||||||
|
im[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)]
|
||||||
|
|
||||||
|
# return unobscured labels
|
||||||
|
if len(labels) and s > 0.03:
|
||||||
|
box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32)
|
||||||
|
ioa = bbox_ioa(box, xywhn2xyxy(labels[:, 1:5], w, h)) # intersection over area
|
||||||
|
labels = labels[ioa < 0.60] # remove >60% obscured labels
|
||||||
|
|
||||||
|
return labels
|
||||||
|
|
||||||
|
|
||||||
|
def mixup(im, labels, im2, labels2):
|
||||||
|
# Applies MixUp augmentation https://arxiv.org/pdf/1710.09412.pdf
|
||||||
|
r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0
|
||||||
|
im = (im * r + im2 * (1 - r)).astype(np.uint8)
|
||||||
|
labels = np.concatenate((labels, labels2), 0)
|
||||||
|
return im, labels
|
||||||
|
|
||||||
|
|
||||||
|
def box_candidates(box1, box2, wh_thr=2, ar_thr=100, area_thr=0.1, eps=1e-16): # box1(4,n), box2(4,n)
|
||||||
|
# Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
|
||||||
|
w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
|
||||||
|
w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
|
||||||
|
ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio
|
||||||
|
return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates
|
||||||
|
|
||||||
|
|
||||||
|
def classify_albumentations(
|
||||||
|
augment=True,
|
||||||
|
size=224,
|
||||||
|
scale=(0.08, 1.0),
|
||||||
|
ratio=(0.75, 1.0 / 0.75), # 0.75, 1.33
|
||||||
|
hflip=0.5,
|
||||||
|
vflip=0.0,
|
||||||
|
jitter=0.4,
|
||||||
|
mean=IMAGENET_MEAN,
|
||||||
|
std=IMAGENET_STD,
|
||||||
|
auto_aug=False):
|
||||||
|
# YOLOv5 classification Albumentations (optional, only used if package is installed)
|
||||||
|
prefix = colorstr('albumentations: ')
|
||||||
|
try:
|
||||||
|
import albumentations as A
|
||||||
|
from albumentations.pytorch import ToTensorV2
|
||||||
|
check_version(A.__version__, '1.0.3', hard=True) # version requirement
|
||||||
|
if augment: # Resize and crop
|
||||||
|
T = [A.RandomResizedCrop(height=size, width=size, scale=scale, ratio=ratio)]
|
||||||
|
if auto_aug:
|
||||||
|
# TODO: implement AugMix, AutoAug & RandAug in albumentation
|
||||||
|
LOGGER.info(f'{prefix}auto augmentations are currently not supported')
|
||||||
|
else:
|
||||||
|
if hflip > 0:
|
||||||
|
T += [A.HorizontalFlip(p=hflip)]
|
||||||
|
if vflip > 0:
|
||||||
|
T += [A.VerticalFlip(p=vflip)]
|
||||||
|
if jitter > 0:
|
||||||
|
color_jitter = (float(jitter),) * 3 # repeat value for brightness, contrast, satuaration, 0 hue
|
||||||
|
T += [A.ColorJitter(*color_jitter, 0)]
|
||||||
|
else: # Use fixed crop for eval set (reproducibility)
|
||||||
|
T = [A.SmallestMaxSize(max_size=size), A.CenterCrop(height=size, width=size)]
|
||||||
|
T += [A.Normalize(mean=mean, std=std), ToTensorV2()] # Normalize and convert to Tensor
|
||||||
|
LOGGER.info(prefix + ', '.join(f'{x}'.replace('always_apply=False, ', '') for x in T if x.p))
|
||||||
|
return A.Compose(T)
|
||||||
|
|
||||||
|
except ImportError: # package not installed, skip
|
||||||
|
LOGGER.warning(f'{prefix}⚠️ not found, install with `pip install albumentations` (recommended)')
|
||||||
|
except Exception as e:
|
||||||
|
LOGGER.info(f'{prefix}{e}')
|
||||||
|
|
||||||
|
|
||||||
|
def classify_transforms(size=224):
|
||||||
|
# Transforms to apply if albumentations not installed
|
||||||
|
assert isinstance(size, int), f'ERROR: classify_transforms size {size} must be integer, not (list, tuple)'
|
||||||
|
# T.Compose([T.ToTensor(), T.Resize(size), T.CenterCrop(size), T.Normalize(IMAGENET_MEAN, IMAGENET_STD)])
|
||||||
|
return T.Compose([CenterCrop(size), ToTensor(), T.Normalize(IMAGENET_MEAN, IMAGENET_STD)])
|
||||||
|
|
||||||
|
|
||||||
|
class LetterBox:
|
||||||
|
# YOLOv5 LetterBox class for image preprocessing, i.e. T.Compose([LetterBox(size), ToTensor()])
|
||||||
|
def __init__(self, size=(640, 640), auto=False, stride=32):
|
||||||
|
super().__init__()
|
||||||
|
self.h, self.w = (size, size) if isinstance(size, int) else size
|
||||||
|
self.auto = auto # pass max size integer, automatically solve for short side using stride
|
||||||
|
self.stride = stride # used with auto
|
||||||
|
|
||||||
|
def __call__(self, im): # im = np.array HWC
|
||||||
|
imh, imw = im.shape[:2]
|
||||||
|
r = min(self.h / imh, self.w / imw) # ratio of new/old
|
||||||
|
h, w = round(imh * r), round(imw * r) # resized image
|
||||||
|
hs, ws = (math.ceil(x / self.stride) * self.stride for x in (h, w)) if self.auto else self.h, self.w
|
||||||
|
top, left = round((hs - h) / 2 - 0.1), round((ws - w) / 2 - 0.1)
|
||||||
|
im_out = np.full((self.h, self.w, 3), 114, dtype=im.dtype)
|
||||||
|
im_out[top:top + h, left:left + w] = cv2.resize(im, (w, h), interpolation=cv2.INTER_LINEAR)
|
||||||
|
return im_out
|
||||||
|
|
||||||
|
|
||||||
|
class CenterCrop:
|
||||||
|
# YOLOv5 CenterCrop class for image preprocessing, i.e. T.Compose([CenterCrop(size), ToTensor()])
|
||||||
|
def __init__(self, size=640):
|
||||||
|
super().__init__()
|
||||||
|
self.h, self.w = (size, size) if isinstance(size, int) else size
|
||||||
|
|
||||||
|
def __call__(self, im): # im = np.array HWC
|
||||||
|
imh, imw = im.shape[:2]
|
||||||
|
m = min(imh, imw) # min dimension
|
||||||
|
top, left = (imh - m) // 2, (imw - m) // 2
|
||||||
|
return cv2.resize(im[top:top + m, left:left + m], (self.w, self.h), interpolation=cv2.INTER_LINEAR)
|
||||||
|
|
||||||
|
|
||||||
|
class ToTensor:
|
||||||
|
# YOLOv5 ToTensor class for image preprocessing, i.e. T.Compose([LetterBox(size), ToTensor()])
|
||||||
|
def __init__(self, half=False):
|
||||||
|
super().__init__()
|
||||||
|
self.half = half
|
||||||
|
|
||||||
|
def __call__(self, im): # im = np.array HWC in BGR order
|
||||||
|
im = np.ascontiguousarray(im.transpose((2, 0, 1))[::-1]) # HWC to CHW -> BGR to RGB -> contiguous
|
||||||
|
im = torch.from_numpy(im) # to torch
|
||||||
|
im = im.half() if self.half else im.float() # uint8 to fp16/32
|
||||||
|
im /= 255.0 # 0-255 to 0.0-1.0
|
||||||
|
return im
|
@ -0,0 +1,169 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
"""
|
||||||
|
AutoAnchor utils
|
||||||
|
"""
|
||||||
|
|
||||||
|
import random
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
import yaml
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
from utils import TryExcept
|
||||||
|
from utils.general import LOGGER, TQDM_BAR_FORMAT, colorstr
|
||||||
|
|
||||||
|
PREFIX = colorstr('AutoAnchor: ')
|
||||||
|
|
||||||
|
|
||||||
|
def check_anchor_order(m):
|
||||||
|
# Check anchor order against stride order for YOLOv5 Detect() module m, and correct if necessary
|
||||||
|
a = m.anchors.prod(-1).mean(-1).view(-1) # mean anchor area per output layer
|
||||||
|
da = a[-1] - a[0] # delta a
|
||||||
|
ds = m.stride[-1] - m.stride[0] # delta s
|
||||||
|
if da and (da.sign() != ds.sign()): # same order
|
||||||
|
LOGGER.info(f'{PREFIX}Reversing anchor order')
|
||||||
|
m.anchors[:] = m.anchors.flip(0)
|
||||||
|
|
||||||
|
|
||||||
|
@TryExcept(f'{PREFIX}ERROR')
|
||||||
|
def check_anchors(dataset, model, thr=4.0, imgsz=640):
|
||||||
|
# Check anchor fit to data, recompute if necessary
|
||||||
|
m = model.module.model[-1] if hasattr(model, 'module') else model.model[-1] # Detect()
|
||||||
|
shapes = imgsz * dataset.shapes / dataset.shapes.max(1, keepdims=True)
|
||||||
|
scale = np.random.uniform(0.9, 1.1, size=(shapes.shape[0], 1)) # augment scale
|
||||||
|
wh = torch.tensor(np.concatenate([l[:, 3:5] * s for s, l in zip(shapes * scale, dataset.labels)])).float() # wh
|
||||||
|
|
||||||
|
def metric(k): # compute metric
|
||||||
|
r = wh[:, None] / k[None]
|
||||||
|
x = torch.min(r, 1 / r).min(2)[0] # ratio metric
|
||||||
|
best = x.max(1)[0] # best_x
|
||||||
|
aat = (x > 1 / thr).float().sum(1).mean() # anchors above threshold
|
||||||
|
bpr = (best > 1 / thr).float().mean() # best possible recall
|
||||||
|
return bpr, aat
|
||||||
|
|
||||||
|
stride = m.stride.to(m.anchors.device).view(-1, 1, 1) # model strides
|
||||||
|
anchors = m.anchors.clone() * stride # current anchors
|
||||||
|
bpr, aat = metric(anchors.cpu().view(-1, 2))
|
||||||
|
s = f'\n{PREFIX}{aat:.2f} anchors/target, {bpr:.3f} Best Possible Recall (BPR). '
|
||||||
|
if bpr > 0.98: # threshold to recompute
|
||||||
|
LOGGER.info(f'{s}Current anchors are a good fit to dataset ✅')
|
||||||
|
else:
|
||||||
|
LOGGER.info(f'{s}Anchors are a poor fit to dataset ⚠️, attempting to improve...')
|
||||||
|
na = m.anchors.numel() // 2 # number of anchors
|
||||||
|
anchors = kmean_anchors(dataset, n=na, img_size=imgsz, thr=thr, gen=1000, verbose=False)
|
||||||
|
new_bpr = metric(anchors)[0]
|
||||||
|
if new_bpr > bpr: # replace anchors
|
||||||
|
anchors = torch.tensor(anchors, device=m.anchors.device).type_as(m.anchors)
|
||||||
|
m.anchors[:] = anchors.clone().view_as(m.anchors)
|
||||||
|
check_anchor_order(m) # must be in pixel-space (not grid-space)
|
||||||
|
m.anchors /= stride
|
||||||
|
s = f'{PREFIX}Done ✅ (optional: update model *.yaml to use these anchors in the future)'
|
||||||
|
else:
|
||||||
|
s = f'{PREFIX}Done ⚠️ (original anchors better than new anchors, proceeding with original anchors)'
|
||||||
|
LOGGER.info(s)
|
||||||
|
|
||||||
|
|
||||||
|
def kmean_anchors(dataset='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=1000, verbose=True):
|
||||||
|
""" Creates kmeans-evolved anchors from training dataset
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
dataset: path to data.yaml, or a loaded dataset
|
||||||
|
n: number of anchors
|
||||||
|
img_size: image size used for training
|
||||||
|
thr: anchor-label wh ratio threshold hyperparameter hyp['anchor_t'] used for training, default=4.0
|
||||||
|
gen: generations to evolve anchors using genetic algorithm
|
||||||
|
verbose: print all results
|
||||||
|
|
||||||
|
Return:
|
||||||
|
k: kmeans evolved anchors
|
||||||
|
|
||||||
|
Usage:
|
||||||
|
from utils.autoanchor import *; _ = kmean_anchors()
|
||||||
|
"""
|
||||||
|
from scipy.cluster.vq import kmeans
|
||||||
|
|
||||||
|
npr = np.random
|
||||||
|
thr = 1 / thr
|
||||||
|
|
||||||
|
def metric(k, wh): # compute metrics
|
||||||
|
r = wh[:, None] / k[None]
|
||||||
|
x = torch.min(r, 1 / r).min(2)[0] # ratio metric
|
||||||
|
# x = wh_iou(wh, torch.tensor(k)) # iou metric
|
||||||
|
return x, x.max(1)[0] # x, best_x
|
||||||
|
|
||||||
|
def anchor_fitness(k): # mutation fitness
|
||||||
|
_, best = metric(torch.tensor(k, dtype=torch.float32), wh)
|
||||||
|
return (best * (best > thr).float()).mean() # fitness
|
||||||
|
|
||||||
|
def print_results(k, verbose=True):
|
||||||
|
k = k[np.argsort(k.prod(1))] # sort small to large
|
||||||
|
x, best = metric(k, wh0)
|
||||||
|
bpr, aat = (best > thr).float().mean(), (x > thr).float().mean() * n # best possible recall, anch > thr
|
||||||
|
s = f'{PREFIX}thr={thr:.2f}: {bpr:.4f} best possible recall, {aat:.2f} anchors past thr\n' \
|
||||||
|
f'{PREFIX}n={n}, img_size={img_size}, metric_all={x.mean():.3f}/{best.mean():.3f}-mean/best, ' \
|
||||||
|
f'past_thr={x[x > thr].mean():.3f}-mean: '
|
||||||
|
for x in k:
|
||||||
|
s += '%i,%i, ' % (round(x[0]), round(x[1]))
|
||||||
|
if verbose:
|
||||||
|
LOGGER.info(s[:-2])
|
||||||
|
return k
|
||||||
|
|
||||||
|
if isinstance(dataset, str): # *.yaml file
|
||||||
|
with open(dataset, errors='ignore') as f:
|
||||||
|
data_dict = yaml.safe_load(f) # model dict
|
||||||
|
from utils.dataloaders import LoadImagesAndLabels
|
||||||
|
dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
|
||||||
|
|
||||||
|
# Get label wh
|
||||||
|
shapes = img_size * dataset.shapes / dataset.shapes.max(1, keepdims=True)
|
||||||
|
wh0 = np.concatenate([l[:, 3:5] * s for s, l in zip(shapes, dataset.labels)]) # wh
|
||||||
|
|
||||||
|
# Filter
|
||||||
|
i = (wh0 < 3.0).any(1).sum()
|
||||||
|
if i:
|
||||||
|
LOGGER.info(f'{PREFIX}WARNING ⚠️ Extremely small objects found: {i} of {len(wh0)} labels are <3 pixels in size')
|
||||||
|
wh = wh0[(wh0 >= 2.0).any(1)].astype(np.float32) # filter > 2 pixels
|
||||||
|
# wh = wh * (npr.rand(wh.shape[0], 1) * 0.9 + 0.1) # multiply by random scale 0-1
|
||||||
|
|
||||||
|
# Kmeans init
|
||||||
|
try:
|
||||||
|
LOGGER.info(f'{PREFIX}Running kmeans for {n} anchors on {len(wh)} points...')
|
||||||
|
assert n <= len(wh) # apply overdetermined constraint
|
||||||
|
s = wh.std(0) # sigmas for whitening
|
||||||
|
k = kmeans(wh / s, n, iter=30)[0] * s # points
|
||||||
|
assert n == len(k) # kmeans may return fewer points than requested if wh is insufficient or too similar
|
||||||
|
except Exception:
|
||||||
|
LOGGER.warning(f'{PREFIX}WARNING ⚠️ switching strategies from kmeans to random init')
|
||||||
|
k = np.sort(npr.rand(n * 2)).reshape(n, 2) * img_size # random init
|
||||||
|
wh, wh0 = (torch.tensor(x, dtype=torch.float32) for x in (wh, wh0))
|
||||||
|
k = print_results(k, verbose=False)
|
||||||
|
|
||||||
|
# Plot
|
||||||
|
# k, d = [None] * 20, [None] * 20
|
||||||
|
# for i in tqdm(range(1, 21)):
|
||||||
|
# k[i-1], d[i-1] = kmeans(wh / s, i) # points, mean distance
|
||||||
|
# fig, ax = plt.subplots(1, 2, figsize=(14, 7), tight_layout=True)
|
||||||
|
# ax = ax.ravel()
|
||||||
|
# ax[0].plot(np.arange(1, 21), np.array(d) ** 2, marker='.')
|
||||||
|
# fig, ax = plt.subplots(1, 2, figsize=(14, 7)) # plot wh
|
||||||
|
# ax[0].hist(wh[wh[:, 0]<100, 0],400)
|
||||||
|
# ax[1].hist(wh[wh[:, 1]<100, 1],400)
|
||||||
|
# fig.savefig('wh.png', dpi=200)
|
||||||
|
|
||||||
|
# Evolve
|
||||||
|
f, sh, mp, s = anchor_fitness(k), k.shape, 0.9, 0.1 # fitness, generations, mutation prob, sigma
|
||||||
|
pbar = tqdm(range(gen), bar_format=TQDM_BAR_FORMAT) # progress bar
|
||||||
|
for _ in pbar:
|
||||||
|
v = np.ones(sh)
|
||||||
|
while (v == 1).all(): # mutate until a change occurs (prevent duplicates)
|
||||||
|
v = ((npr.random(sh) < mp) * random.random() * npr.randn(*sh) * s + 1).clip(0.3, 3.0)
|
||||||
|
kg = (k.copy() * v).clip(min=2.0)
|
||||||
|
fg = anchor_fitness(kg)
|
||||||
|
if fg > f:
|
||||||
|
f, k = fg, kg.copy()
|
||||||
|
pbar.desc = f'{PREFIX}Evolving anchors with Genetic Algorithm: fitness = {f:.4f}'
|
||||||
|
if verbose:
|
||||||
|
print_results(k, verbose)
|
||||||
|
|
||||||
|
return print_results(k).astype(np.float32)
|
@ -0,0 +1,72 @@
|
|||||||
|
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
|
||||||
|
"""
|
||||||
|
Auto-batch utils
|
||||||
|
"""
|
||||||
|
|
||||||
|
from copy import deepcopy
|
||||||
|
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
|
||||||
|
from utils.general import LOGGER, colorstr
|
||||||
|
from utils.torch_utils import profile
|
||||||
|
|
||||||
|
|
||||||
|
def check_train_batch_size(model, imgsz=640, amp=True):
|
||||||
|
# Check YOLOv5 training batch size
|
||||||
|
with torch.cuda.amp.autocast(amp):
|
||||||
|
return autobatch(deepcopy(model).train(), imgsz) # compute optimal batch size
|
||||||
|
|
||||||
|
|
||||||
|
def autobatch(model, imgsz=640, fraction=0.8, batch_size=16):
|
||||||
|
# Automatically estimate best YOLOv5 batch size to use `fraction` of available CUDA memory
|
||||||
|
# Usage:
|
||||||
|
# import torch
|
||||||
|
# from utils.autobatch import autobatch
|
||||||
|
# model = torch.hub.load('ultralytics/yolov5', 'yolov5s', autoshape=False)
|
||||||
|
# print(autobatch(model))
|
||||||
|
|
||||||
|
# Check device
|
||||||
|
prefix = colorstr('AutoBatch: ')
|
||||||
|
LOGGER.info(f'{prefix}Computing optimal batch size for --imgsz {imgsz}')
|
||||||
|
device = next(model.parameters()).device # get model device
|
||||||
|
if device.type == 'cpu':
|
||||||
|
LOGGER.info(f'{prefix}CUDA not detected, using default CPU batch-size {batch_size}')
|
||||||
|
return batch_size
|
||||||
|
if torch.backends.cudnn.benchmark:
|
||||||
|
LOGGER.info(f'{prefix} ⚠️ Requires torch.backends.cudnn.benchmark=False, using default batch-size {batch_size}')
|
||||||
|
return batch_size
|
||||||
|
|
||||||
|
# Inspect CUDA memory
|
||||||
|
gb = 1 << 30 # bytes to GiB (1024 ** 3)
|
||||||
|
d = str(device).upper() # 'CUDA:0'
|
||||||
|
properties = torch.cuda.get_device_properties(device) # device properties
|
||||||
|
t = properties.total_memory / gb # GiB total
|
||||||
|
r = torch.cuda.memory_reserved(device) / gb # GiB reserved
|
||||||
|
a = torch.cuda.memory_allocated(device) / gb # GiB allocated
|
||||||
|
f = t - (r + a) # GiB free
|
||||||
|
LOGGER.info(f'{prefix}{d} ({properties.name}) {t:.2f}G total, {r:.2f}G reserved, {a:.2f}G allocated, {f:.2f}G free')
|
||||||
|
|
||||||
|
# Profile batch sizes
|
||||||
|
batch_sizes = [1, 2, 4, 8, 16]
|
||||||
|
try:
|
||||||
|
img = [torch.empty(b, 3, imgsz, imgsz) for b in batch_sizes]
|
||||||
|
results = profile(img, model, n=3, device=device)
|
||||||
|
except Exception as e:
|
||||||
|
LOGGER.warning(f'{prefix}{e}')
|
||||||
|
|
||||||
|
# Fit a solution
|
||||||
|
y = [x[2] for x in results if x] # memory [2]
|
||||||
|
p = np.polyfit(batch_sizes[:len(y)], y, deg=1) # first degree polynomial fit
|
||||||
|
b = int((f * fraction - p[1]) / p[0]) # y intercept (optimal batch size)
|
||||||
|
if None in results: # some sizes failed
|
||||||
|
i = results.index(None) # first fail index
|
||||||
|
if b >= batch_sizes[i]: # y intercept above failure point
|
||||||
|
b = batch_sizes[max(i - 1, 0)] # select prior safe point
|
||||||
|
if b < 1 or b > 1024: # b outside of safe range
|
||||||
|
b = batch_size
|
||||||
|
LOGGER.warning(f'{prefix}WARNING ⚠️ CUDA anomaly detected, recommend restart environment and retry command.')
|
||||||
|
|
||||||
|
fraction = (np.polyval(p, b) + r + a) / t # actual fraction predicted
|
||||||
|
LOGGER.info(f'{prefix}Using batch-size {b} for {d} {t * fraction:.2f}G/{t:.2f}G ({fraction * 100:.0f}%) ✅')
|
||||||
|
return b
|