@@ -0,0 +1,217 @@ | |||
# Repo-specific DockerIgnore ------------------------------------------------------------------------------------------- | |||
# .git | |||
.cache | |||
.idea | |||
runs | |||
output | |||
coco | |||
storage.googleapis.com | |||
data/samples/* | |||
!data/samples/zidane.jpg | |||
!data/samples/bus.jpg | |||
**/results*.txt | |||
*.jpg | |||
# Neural Network weights ----------------------------------------------------------------------------------------------- | |||
**/*.weights | |||
**/*.pt | |||
**/*.onnx | |||
**/*.mlmodel | |||
**/darknet53.conv.74 | |||
**/yolov3-tiny.conv.15 | |||
# Below Copied From .gitignore ----------------------------------------------------------------------------------------- | |||
# Below Copied From .gitignore ----------------------------------------------------------------------------------------- | |||
# GitHub Python GitIgnore ---------------------------------------------------------------------------------------------- | |||
# Byte-compiled / optimized / DLL files | |||
__pycache__/ | |||
*.py[cod] | |||
*$py.class | |||
# C extensions | |||
*.so | |||
# Distribution / packaging | |||
.Python | |||
env/ | |||
build/ | |||
develop-eggs/ | |||
dist/ | |||
downloads/ | |||
eggs/ | |||
.eggs/ | |||
lib/ | |||
lib64/ | |||
parts/ | |||
sdist/ | |||
var/ | |||
wheels/ | |||
*.egg-info/ | |||
.installed.cfg | |||
*.egg | |||
# PyInstaller | |||
# Usually these files are written by a python script from a template | |||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | |||
*.manifest | |||
*.spec | |||
# Installer logs | |||
pip-log.txt | |||
pip-delete-this-directory.txt | |||
# Unit test / coverage reports | |||
htmlcov/ | |||
.tox/ | |||
.coverage | |||
.coverage.* | |||
.cache | |||
nosetests.xml | |||
coverage.xml | |||
*.cover | |||
.hypothesis/ | |||
# Translations | |||
*.mo | |||
*.pot | |||
# Django stuff: | |||
*.log | |||
local_settings.py | |||
# Flask stuff: | |||
instance/ | |||
.webassets-cache | |||
# Scrapy stuff: | |||
.scrapy | |||
# Sphinx documentation | |||
docs/_build/ | |||
# PyBuilder | |||
target/ | |||
# Jupyter Notebook | |||
.ipynb_checkpoints | |||
# pyenv | |||
.python-version | |||
# celery beat schedule file | |||
celerybeat-schedule | |||
# SageMath parsed files | |||
*.sage.py | |||
# dotenv | |||
.env | |||
# virtualenv | |||
.venv | |||
venv/ | |||
ENV/ | |||
# Spyder project settings | |||
.spyderproject | |||
.spyproject | |||
# Rope project settings | |||
.ropeproject | |||
# mkdocs documentation | |||
/site | |||
# mypy | |||
.mypy_cache/ | |||
# https://github.com/github/gitignore/blob/master/Global/macOS.gitignore ----------------------------------------------- | |||
# General | |||
.DS_Store | |||
.AppleDouble | |||
.LSOverride | |||
# Icon must end with two \r | |||
Icon | |||
Icon? | |||
# Thumbnails | |||
._* | |||
# Files that might appear in the root of a volume | |||
.DocumentRevisions-V100 | |||
.fseventsd | |||
.Spotlight-V100 | |||
.TemporaryItems | |||
.Trashes | |||
.VolumeIcon.icns | |||
.com.apple.timemachine.donotpresent | |||
# Directories potentially created on remote AFP share | |||
.AppleDB | |||
.AppleDesktop | |||
Network Trash Folder | |||
Temporary Items | |||
.apdisk | |||
# https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore | |||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm | |||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | |||
# User-specific stuff: | |||
.idea/* | |||
.idea/**/workspace.xml | |||
.idea/**/tasks.xml | |||
.idea/dictionaries | |||
.html # Bokeh Plots | |||
.pg # TensorFlow Frozen Graphs | |||
.avi # videos | |||
# Sensitive or high-churn files: | |||
.idea/**/dataSources/ | |||
.idea/**/dataSources.ids | |||
.idea/**/dataSources.local.xml | |||
.idea/**/sqlDataSources.xml | |||
.idea/**/dynamic.xml | |||
.idea/**/uiDesigner.xml | |||
# Gradle: | |||
.idea/**/gradle.xml | |||
.idea/**/libraries | |||
# CMake | |||
cmake-build-debug/ | |||
cmake-build-release/ | |||
# Mongo Explorer plugin: | |||
.idea/**/mongoSettings.xml | |||
## File-based project format: | |||
*.iws | |||
## Plugin-specific files: | |||
# IntelliJ | |||
out/ | |||
# mpeltonen/sbt-idea plugin | |||
.idea_modules/ | |||
# JIRA plugin | |||
atlassian-ide-plugin.xml | |||
# Cursive Clojure plugin | |||
.idea/replstate.xml | |||
# Crashlytics plugin (for Android Studio and IntelliJ) | |||
com_crashlytics_export_strings.xml | |||
crashlytics.properties | |||
crashlytics-build.properties | |||
fabric.properties |
@@ -0,0 +1,41 @@ | |||
--- | |||
name: "\U0001F41BBug report" | |||
about: Create a report to help us improve | |||
title: '' | |||
labels: bug | |||
assignees: '' | |||
--- | |||
Before submitting a bug report, please ensure that you are using the latest versions of: | |||
- Python | |||
- PyTorch | |||
- This repository (run `git fetch && git status -uno` to check and `git pull` to update) | |||
**Your issue must be reproducible on a public dataset (i.e COCO) using the latest version of the repository, and you must supply code to reproduce, or we can not help you.** | |||
If this is a custom training question we suggest you include your `train*.jpg`, `test*.jpg` and `results.png` figures. | |||
## 🐛 Bug | |||
A clear and concise description of what the bug is. | |||
## To Reproduce | |||
**REQUIRED**: Code to reproduce your issue below | |||
``` | |||
python train.py ... | |||
``` | |||
## Expected behavior | |||
A clear and concise description of what you expected to happen. | |||
## Environment | |||
If applicable, add screenshots to help explain your problem. | |||
- OS: [e.g. Ubuntu] | |||
- GPU [e.g. 2080 Ti] | |||
## Additional context | |||
Add any other context about the problem here. |
@@ -0,0 +1,27 @@ | |||
--- | |||
name: "\U0001F680Feature request" | |||
about: Suggest an idea for this project | |||
title: '' | |||
labels: enhancement | |||
assignees: '' | |||
--- | |||
## 🚀 Feature | |||
<!-- A clear and concise description of the feature proposal --> | |||
## Motivation | |||
<!-- Please outline the motivation for the proposal. Is your feature request related to a problem? e.g., I'm always frustrated when [...]. If this is related to another GitHub issue, please link here too --> | |||
## Pitch | |||
<!-- A clear and concise description of what you want to happen. --> | |||
## Alternatives | |||
<!-- A clear and concise description of any alternative solutions or features you've considered, if any. --> | |||
## Additional context | |||
<!-- Add any other context or screenshots about the feature request here. --> |
@@ -0,0 +1,23 @@ | |||
name: Greetings | |||
on: [pull_request, issues] | |||
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 PR! We will respond as soon as possible.' | |||
issue-message: > | |||
Hello @${{ github.actor }}, thank you for your interest in our work! Please visit our [Custom Training Tutorial](https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data) to get started, and see our [Google Colab Notebook](https://github.com/ultralytics/yolov5/blob/master/tutorial.ipynb), [Docker Image](https://hub.docker.com/r/ultralytics/yolov5), and [GCP Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/GCP-Quickstart) for example environments. | |||
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 model or data training question, please note that Ultralytics does **not** provide free personal support. As a leader in vision ML and AI, we do offer professional consulting, from simple expert advice up to delivery of fully customized, end-to-end production solutions for our clients, such as: | |||
- **Cloud-based AI** surveillance systems operating on **hundreds of HD video streams in realtime.** | |||
- **Edge AI** integrated into custom iOS and Android apps for realtime **30 FPS video inference.** | |||
- **Custom data training**, hyperparameter evolution, and model exportation to any destination. | |||
To discuss your business requirements in greater detail please visit us at https://www.ultralytics.com. |
@@ -0,0 +1,17 @@ | |||
name: Close stale issues | |||
on: | |||
schedule: | |||
- cron: "0 0 * * *" | |||
jobs: | |||
stale: | |||
runs-on: ubuntu-latest | |||
steps: | |||
- uses: actions/stale@v1 | |||
with: | |||
repo-token: ${{ secrets.GITHUB_TOKEN }} | |||
stale-issue-message: 'This issue is stale because it has been open 30 days with no activity. Remove Stale label or comment or this will be closed in 5 days.' | |||
stale-pr-message: 'This pull request is stale because it has been open 30 days with no activity. Remove Stale label or comment or this will be closed in 5 days.' | |||
days-before-stale: 30 | |||
days-before-close: 5 | |||
exempt-issue-label: 'tutorial' |
@@ -0,0 +1,244 @@ | |||
# Repo-specific GitIgnore ---------------------------------------------------------------------------------------------- | |||
*.jpg | |||
*.jpeg | |||
*.png | |||
*.bmp | |||
*.tif | |||
*.tiff | |||
*.heic | |||
*.JPG | |||
*.JPEG | |||
*.PNG | |||
*.BMP | |||
*.TIF | |||
*.TIFF | |||
*.HEIC | |||
*.mp4 | |||
*.mov | |||
*.MOV | |||
*.avi | |||
*.data | |||
*.json | |||
*.cfg | |||
!cfg/yolov3*.cfg | |||
storage.googleapis.com | |||
runs/* | |||
data/* | |||
!data/samples/zidane.jpg | |||
!data/samples/bus.jpg | |||
!data/coco.names | |||
!data/coco_paper.names | |||
!data/coco.data | |||
!data/coco_*.data | |||
!data/coco_*.txt | |||
!data/trainvalno5k.shapes | |||
!data/*.sh | |||
pycocotools/* | |||
results*.txt | |||
gcp_test*.sh | |||
# MATLAB GitIgnore ----------------------------------------------------------------------------------------------------- | |||
*.m~ | |||
*.mat | |||
!targets*.mat | |||
# Neural Network weights ----------------------------------------------------------------------------------------------- | |||
*.weights | |||
*.pt | |||
*.onnx | |||
*.mlmodel | |||
darknet53.conv.74 | |||
yolov3-tiny.conv.15 | |||
# GitHub Python GitIgnore ---------------------------------------------------------------------------------------------- | |||
# Byte-compiled / optimized / DLL files | |||
__pycache__/ | |||
*.py[cod] | |||
*$py.class | |||
# C extensions | |||
*.so | |||
# Distribution / packaging | |||
.Python | |||
env/ | |||
build/ | |||
develop-eggs/ | |||
dist/ | |||
downloads/ | |||
eggs/ | |||
.eggs/ | |||
lib/ | |||
lib64/ | |||
parts/ | |||
sdist/ | |||
var/ | |||
wheels/ | |||
*.egg-info/ | |||
.installed.cfg | |||
*.egg | |||
# PyInstaller | |||
# Usually these files are written by a python script from a template | |||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | |||
*.manifest | |||
*.spec | |||
# Installer logs | |||
pip-log.txt | |||
pip-delete-this-directory.txt | |||
# Unit test / coverage reports | |||
htmlcov/ | |||
.tox/ | |||
.coverage | |||
.coverage.* | |||
.cache | |||
nosetests.xml | |||
coverage.xml | |||
*.cover | |||
.hypothesis/ | |||
# Translations | |||
*.mo | |||
*.pot | |||
# Django stuff: | |||
*.log | |||
local_settings.py | |||
# Flask stuff: | |||
instance/ | |||
.webassets-cache | |||
# Scrapy stuff: | |||
.scrapy | |||
# Sphinx documentation | |||
docs/_build/ | |||
# PyBuilder | |||
target/ | |||
# Jupyter Notebook | |||
.ipynb_checkpoints | |||
# pyenv | |||
.python-version | |||
# celery beat schedule file | |||
celerybeat-schedule | |||
# SageMath parsed files | |||
*.sage.py | |||
# dotenv | |||
.env | |||
# virtualenv | |||
.venv | |||
venv/ | |||
ENV/ | |||
# Spyder project settings | |||
.spyderproject | |||
.spyproject | |||
# Rope project settings | |||
.ropeproject | |||
# mkdocs documentation | |||
/site | |||
# mypy | |||
.mypy_cache/ | |||
# https://github.com/github/gitignore/blob/master/Global/macOS.gitignore ----------------------------------------------- | |||
# General | |||
.DS_Store | |||
.AppleDouble | |||
.LSOverride | |||
# Icon must end with two \r | |||
Icon | |||
Icon? | |||
# Thumbnails | |||
._* | |||
# Files that might appear in the root of a volume | |||
.DocumentRevisions-V100 | |||
.fseventsd | |||
.Spotlight-V100 | |||
.TemporaryItems | |||
.Trashes | |||
.VolumeIcon.icns | |||
.com.apple.timemachine.donotpresent | |||
# Directories potentially created on remote AFP share | |||
.AppleDB | |||
.AppleDesktop | |||
Network Trash Folder | |||
Temporary Items | |||
.apdisk | |||
# https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore | |||
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm | |||
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 | |||
# User-specific stuff: | |||
.idea/* | |||
.idea/**/workspace.xml | |||
.idea/**/tasks.xml | |||
.idea/dictionaries | |||
.html # Bokeh Plots | |||
.pg # TensorFlow Frozen Graphs | |||
.avi # videos | |||
# Sensitive or high-churn files: | |||
.idea/**/dataSources/ | |||
.idea/**/dataSources.ids | |||
.idea/**/dataSources.local.xml | |||
.idea/**/sqlDataSources.xml | |||
.idea/**/dynamic.xml | |||
.idea/**/uiDesigner.xml | |||
# Gradle: | |||
.idea/**/gradle.xml | |||
.idea/**/libraries | |||
# CMake | |||
cmake-build-debug/ | |||
cmake-build-release/ | |||
# Mongo Explorer plugin: | |||
.idea/**/mongoSettings.xml | |||
## File-based project format: | |||
*.iws | |||
## Plugin-specific files: | |||
# IntelliJ | |||
out/ | |||
# mpeltonen/sbt-idea plugin | |||
.idea_modules/ | |||
# JIRA plugin | |||
atlassian-ide-plugin.xml | |||
# Cursive Clojure plugin | |||
.idea/replstate.xml | |||
# Crashlytics plugin (for Android Studio and IntelliJ) | |||
com_crashlytics_export_strings.xml | |||
crashlytics.properties | |||
crashlytics-build.properties | |||
fabric.properties |
@@ -0,0 +1,49 @@ | |||
# Start FROM Nvidia PyTorch image https://ngc.nvidia.com/catalog/containers/nvidia:pytorch | |||
FROM nvcr.io/nvidia/pytorch:20.03-py3 | |||
# Install dependencies (pip or conda) | |||
RUN pip install -U gsutil thop | |||
# RUN pip install -U -r requirements.txt | |||
# Create working directory | |||
RUN mkdir -p /usr/src/app | |||
WORKDIR /usr/src/app | |||
# Copy contents | |||
COPY . /usr/src/app | |||
# Copy weights | |||
#RUN python3 -c "from models import *; \ | |||
#attempt_download('weights/yolov5s.pt'); \ | |||
#attempt_download('weights/yolov5m.pt'); \ | |||
#attempt_download('weights/yolov5l.pt')" | |||
# --------------------------------------------------- Extras Below --------------------------------------------------- | |||
# Build and Push | |||
# t=ultralytics/yolov5:latest && sudo docker build -t $t . && sudo docker push $t | |||
# Pull and Run | |||
# t=ultralytics/yolov5:latest && sudo docker pull $t && sudo docker run -it --ipc=host $t bash | |||
# Pull and Run with local directory access | |||
# t=ultralytics/yolov5:latest && sudo docker pull $t && sudo docker run -it --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t bash | |||
# Kill all | |||
# sudo docker kill "$(sudo docker ps -q)" | |||
# Kill all image-based | |||
# sudo docker kill $(sudo docker ps -a -q --filter ancestor=ultralytics/yolov5:latest) | |||
# Run bash for loop | |||
# sudo docker run --gpus all --ipc=host ultralytics/yolov5:latest while true; do python3 train.py --evolve; done | |||
# Bash in running container | |||
# sudo docker container exec -it 97919ad657de /bin/bash | |||
# Bash last stopped container | |||
# python -c "from utils.utils import *; create_backbone('weights/best.pt')" && gsutil cp weights/backbone.pt gs://ult/coco/yolov5s.pt | |||
# Clean up | |||
# docker system prune -a --volumes |
@@ -0,0 +1,674 @@ | |||
GNU GENERAL PUBLIC LICENSE | |||
Version 3, 29 June 2007 | |||
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/> | |||
Everyone is permitted to copy and distribute verbatim copies | |||
of this license document, but changing it is not allowed. | |||
Preamble | |||
The GNU General Public License is a free, copyleft license for | |||
software and other kinds of works. | |||
The licenses for most software and other practical works are designed | |||
to take away your freedom to share and change the works. By contrast, | |||
the GNU General Public License is intended to guarantee your freedom to | |||
share and change all versions of a program--to make sure it remains free | |||
software for all its users. We, the Free Software Foundation, use the | |||
GNU General Public License for most of our software; it applies also to | |||
any other work released this way by its authors. You can apply it to | |||
your programs, too. | |||
When we speak of free software, we are referring to freedom, not | |||
price. Our General Public Licenses are designed to make sure that you | |||
have the freedom to distribute copies of free software (and charge for | |||
them if you wish), that you receive source code or can get it if you | |||
want it, that you can change the software or use pieces of it in new | |||
free programs, and that you know you can do these things. | |||
To protect your rights, we need to prevent others from denying you | |||
these rights or asking you to surrender the rights. Therefore, you have | |||
certain responsibilities if you distribute copies of the software, or if | |||
you modify it: responsibilities to respect the freedom of others. | |||
For example, if you distribute copies of such a program, whether | |||
gratis or for a fee, you must pass on to the recipients the same | |||
freedoms that you received. You must make sure that they, too, receive | |||
or can get the source code. And you must show them these terms so they | |||
know their rights. | |||
Developers that use the GNU GPL protect your rights with two steps: | |||
(1) assert copyright on the software, and (2) offer you this License | |||
giving you legal permission to copy, distribute and/or modify it. | |||
For the developers' and authors' protection, the GPL clearly explains | |||
that there is no warranty for this free software. For both users' and | |||
authors' sake, the GPL requires that modified versions be marked as | |||
changed, so that their problems will not be attributed erroneously to | |||
authors of previous versions. | |||
Some devices are designed to deny users access to install or run | |||
modified versions of the software inside them, although the manufacturer | |||
can do so. This is fundamentally incompatible with the aim of | |||
protecting users' freedom to change the software. The systematic | |||
pattern of such abuse occurs in the area of products for individuals to | |||
use, which is precisely where it is most unacceptable. Therefore, we | |||
have designed this version of the GPL to prohibit the practice for those | |||
products. If such problems arise substantially in other domains, we | |||
stand ready to extend this provision to those domains in future versions | |||
of the GPL, as needed to protect the freedom of users. | |||
Finally, every program is threatened constantly by software patents. | |||
States should not allow patents to restrict development and use of | |||
software on general-purpose computers, but in those that do, we wish to | |||
avoid the special danger that patents applied to a free program could | |||
make it effectively proprietary. To prevent this, the GPL assures that | |||
patents cannot be used to render the program non-free. | |||
The precise terms and conditions for copying, distribution and | |||
modification follow. | |||
TERMS AND CONDITIONS | |||
0. Definitions. | |||
"This License" refers to version 3 of the GNU General Public License. | |||
"Copyright" also means copyright-like laws that apply to other kinds of | |||
works, such as semiconductor masks. | |||
"The Program" refers to any copyrightable work licensed under this | |||
License. Each licensee is addressed as "you". "Licensees" and | |||
"recipients" may be individuals or organizations. | |||
To "modify" a work means to copy from or adapt all or part of the work | |||
in a fashion requiring copyright permission, other than the making of an | |||
exact copy. The resulting work is called a "modified version" of the | |||
earlier work or a work "based on" the earlier work. | |||
A "covered work" means either the unmodified Program or a work based | |||
on the Program. | |||
To "propagate" a work means to do anything with it that, without | |||
permission, would make you directly or secondarily liable for | |||
infringement under applicable copyright law, except executing it on a | |||
computer or modifying a private copy. Propagation includes copying, | |||
distribution (with or without modification), making available to the | |||
public, and in some countries other activities as well. | |||
To "convey" a work means any kind of propagation that enables other | |||
parties to make or receive copies. Mere interaction with a user through | |||
a computer network, with no transfer of a copy, is not conveying. | |||
An interactive user interface displays "Appropriate Legal Notices" | |||
to the extent that it includes a convenient and prominently visible | |||
feature that (1) displays an appropriate copyright notice, and (2) | |||
tells the user that there is no warranty for the work (except to the | |||
extent that warranties are provided), that licensees may convey the | |||
work under this License, and how to view a copy of this License. If | |||
the interface presents a list of user commands or options, such as a | |||
menu, a prominent item in the list meets this criterion. | |||
1. Source Code. | |||
The "source code" for a work means the preferred form of the work | |||
for making modifications to it. "Object code" means any non-source | |||
form of a work. | |||
A "Standard Interface" means an interface that either is an official | |||
standard defined by a recognized standards body, or, in the case of | |||
interfaces specified for a particular programming language, one that | |||
is widely used among developers working in that language. | |||
The "System Libraries" of an executable work include anything, other | |||
than the work as a whole, that (a) is included in the normal form of | |||
packaging a Major Component, but which is not part of that Major | |||
Component, and (b) serves only to enable use of the work with that | |||
Major Component, or to implement a Standard Interface for which an | |||
implementation is available to the public in source code form. A | |||
"Major Component", in this context, means a major essential component | |||
(kernel, window system, and so on) of the specific operating system | |||
(if any) on which the executable work runs, or a compiler used to | |||
produce the work, or an object code interpreter used to run it. | |||
The "Corresponding Source" for a work in object code form means all | |||
the source code needed to generate, install, and (for an executable | |||
work) run the object code and to modify the work, including scripts to | |||
control those activities. However, it does not include the work's | |||
System Libraries, or general-purpose tools or generally available free | |||
programs which are used unmodified in performing those activities but | |||
which are not part of the work. For example, Corresponding Source | |||
includes interface definition files associated with source files for | |||
the work, and the source code for shared libraries and dynamically | |||
linked subprograms that the work is specifically designed to require, | |||
such as by intimate data communication or control flow between those | |||
subprograms and other parts of the work. | |||
The Corresponding Source need not include anything that users | |||
can regenerate automatically from other parts of the Corresponding | |||
Source. | |||
The Corresponding Source for a work in source code form is that | |||
same work. | |||
2. Basic Permissions. | |||
All rights granted under this License are granted for the term of | |||
copyright on the Program, and are irrevocable provided the stated | |||
conditions are met. This License explicitly affirms your unlimited | |||
permission to run the unmodified Program. The output from running a | |||
covered work is covered by this License only if the output, given its | |||
content, constitutes a covered work. This License acknowledges your | |||
rights of fair use or other equivalent, as provided by copyright law. | |||
You may make, run and propagate covered works that you do not | |||
convey, without conditions so long as your license otherwise remains | |||
in force. You may convey covered works to others for the sole purpose | |||
of having them make modifications exclusively for you, or provide you | |||
with facilities for running those works, provided that you comply with | |||
the terms of this License in conveying all material for which you do | |||
not control copyright. Those thus making or running the covered works | |||
for you must do so exclusively on your behalf, under your direction | |||
and control, on terms that prohibit them from making any copies of | |||
your copyrighted material outside their relationship with you. | |||
Conveying under any other circumstances is permitted solely under | |||
the conditions stated below. Sublicensing is not allowed; section 10 | |||
makes it unnecessary. | |||
3. Protecting Users' Legal Rights From Anti-Circumvention Law. | |||
No covered work shall be deemed part of an effective technological | |||
measure under any applicable law fulfilling obligations under article | |||
11 of the WIPO copyright treaty adopted on 20 December 1996, or | |||
similar laws prohibiting or restricting circumvention of such | |||
measures. | |||
When you convey a covered work, you waive any legal power to forbid | |||
circumvention of technological measures to the extent such circumvention | |||
is effected by exercising rights under this License with respect to | |||
the covered work, and you disclaim any intention to limit operation or | |||
modification of the work as a means of enforcing, against the work's | |||
users, your or third parties' legal rights to forbid circumvention of | |||
technological measures. | |||
4. Conveying Verbatim Copies. | |||
You may convey verbatim copies of the Program's source code as you | |||
receive it, in any medium, provided that you conspicuously and | |||
appropriately publish on each copy an appropriate copyright notice; | |||
keep intact all notices stating that this License and any | |||
non-permissive terms added in accord with section 7 apply to the code; | |||
keep intact all notices of the absence of any warranty; and give all | |||
recipients a copy of this License along with the Program. | |||
You may charge any price or no price for each copy that you convey, | |||
and you may offer support or warranty protection for a fee. | |||
5. Conveying Modified Source Versions. | |||
You may convey a work based on the Program, or the modifications to | |||
produce it from the Program, in the form of source code under the | |||
terms of section 4, provided that you also meet all of these conditions: | |||
a) The work must carry prominent notices stating that you modified | |||
it, and giving a relevant date. | |||
b) The work must carry prominent notices stating that it is | |||
released under this License and any conditions added under section | |||
7. This requirement modifies the requirement in section 4 to | |||
"keep intact all notices". | |||
c) You must license the entire work, as a whole, under this | |||
License to anyone who comes into possession of a copy. This | |||
License will therefore apply, along with any applicable section 7 | |||
additional terms, to the whole of the work, and all its parts, | |||
regardless of how they are packaged. This License gives no | |||
permission to license the work in any other way, but it does not | |||
invalidate such permission if you have separately received it. | |||
d) If the work has interactive user interfaces, each must display | |||
Appropriate Legal Notices; however, if the Program has interactive | |||
interfaces that do not display Appropriate Legal Notices, your | |||
work need not make them do so. | |||
A compilation of a covered work with other separate and independent | |||
works, which are not by their nature extensions of the covered work, | |||
and which are not combined with it such as to form a larger program, | |||
in or on a volume of a storage or distribution medium, is called an | |||
"aggregate" if the compilation and its resulting copyright are not | |||
used to limit the access or legal rights of the compilation's users | |||
beyond what the individual works permit. Inclusion of a covered work | |||
in an aggregate does not cause this License to apply to the other | |||
parts of the aggregate. | |||
6. Conveying Non-Source Forms. | |||
You may convey a covered work in object code form under the terms | |||
of sections 4 and 5, provided that you also convey the | |||
machine-readable Corresponding Source under the terms of this License, | |||
in one of these ways: | |||
a) Convey the object code in, or embodied in, a physical product | |||
(including a physical distribution medium), accompanied by the | |||
Corresponding Source fixed on a durable physical medium | |||
customarily used for software interchange. | |||
b) Convey the object code in, or embodied in, a physical product | |||
(including a physical distribution medium), accompanied by a | |||
written offer, valid for at least three years and valid for as | |||
long as you offer spare parts or customer support for that product | |||
model, to give anyone who possesses the object code either (1) a | |||
copy of the Corresponding Source for all the software in the | |||
product that is covered by this License, on a durable physical | |||
medium customarily used for software interchange, for a price no | |||
more than your reasonable cost of physically performing this | |||
conveying of source, or (2) access to copy the | |||
Corresponding Source from a network server at no charge. | |||
c) Convey individual copies of the object code with a copy of the | |||
written offer to provide the Corresponding Source. This | |||
alternative is allowed only occasionally and noncommercially, and | |||
only if you received the object code with such an offer, in accord | |||
with subsection 6b. | |||
d) Convey the object code by offering access from a designated | |||
place (gratis or for a charge), and offer equivalent access to the | |||
Corresponding Source in the same way through the same place at no | |||
further charge. You need not require recipients to copy the | |||
Corresponding Source along with the object code. If the place to | |||
copy the object code is a network server, the Corresponding Source | |||
may be on a different server (operated by you or a third party) | |||
that supports equivalent copying facilities, provided you maintain | |||
clear directions next to the object code saying where to find the | |||
Corresponding Source. Regardless of what server hosts the | |||
Corresponding Source, you remain obligated to ensure that it is | |||
available for as long as needed to satisfy these requirements. | |||
e) Convey the object code using peer-to-peer transmission, provided | |||
you inform other peers where the object code and Corresponding | |||
Source of the work are being offered to the general public at no | |||
charge under subsection 6d. | |||
A separable portion of the object code, whose source code is excluded | |||
from the Corresponding Source as a System Library, need not be | |||
included in conveying the object code work. | |||
A "User Product" is either (1) a "consumer product", which means any | |||
tangible personal property which is normally used for personal, family, | |||
or household purposes, or (2) anything designed or sold for incorporation | |||
into a dwelling. In determining whether a product is a consumer product, | |||
doubtful cases shall be resolved in favor of coverage. For a particular | |||
product received by a particular user, "normally used" refers to a | |||
typical or common use of that class of product, regardless of the status | |||
of the particular user or of the way in which the particular user | |||
actually uses, or expects or is expected to use, the product. A product | |||
is a consumer product regardless of whether the product has substantial | |||
commercial, industrial or non-consumer uses, unless such uses represent | |||
the only significant mode of use of the product. | |||
"Installation Information" for a User Product means any methods, | |||
procedures, authorization keys, or other information required to install | |||
and execute modified versions of a covered work in that User Product from | |||
a modified version of its Corresponding Source. The information must | |||
suffice to ensure that the continued functioning of the modified object | |||
code is in no case prevented or interfered with solely because | |||
modification has been made. | |||
If you convey an object code work under this section in, or with, or | |||
specifically for use in, a User Product, and the conveying occurs as | |||
part of a transaction in which the right of possession and use of the | |||
User Product is transferred to the recipient in perpetuity or for a | |||
fixed term (regardless of how the transaction is characterized), the | |||
Corresponding Source conveyed under this section must be accompanied | |||
by the Installation Information. But this requirement does not apply | |||
if neither you nor any third party retains the ability to install | |||
modified object code on the User Product (for example, the work has | |||
been installed in ROM). | |||
The requirement to provide Installation Information does not include a | |||
requirement to continue to provide support service, warranty, or updates | |||
for a work that has been modified or installed by the recipient, or for | |||
the User Product in which it has been modified or installed. Access to a | |||
network may be denied when the modification itself materially and | |||
adversely affects the operation of the network or violates the rules and | |||
protocols for communication across the network. | |||
Corresponding Source conveyed, and Installation Information provided, | |||
in accord with this section must be in a format that is publicly | |||
documented (and with an implementation available to the public in | |||
source code form), and must require no special password or key for | |||
unpacking, reading or copying. | |||
7. Additional Terms. | |||
"Additional permissions" are terms that supplement the terms of this | |||
License by making exceptions from one or more of its conditions. | |||
Additional permissions that are applicable to the entire Program shall | |||
be treated as though they were included in this License, to the extent | |||
that they are valid under applicable law. If additional permissions | |||
apply only to part of the Program, that part may be used separately | |||
under those permissions, but the entire Program remains governed by | |||
this License without regard to the additional permissions. | |||
When you convey a copy of a covered work, you may at your option | |||
remove any additional permissions from that copy, or from any part of | |||
it. (Additional permissions may be written to require their own | |||
removal in certain cases when you modify the work.) You may place | |||
additional permissions on material, added by you to a covered work, | |||
for which you have or can give appropriate copyright permission. | |||
Notwithstanding any other provision of this License, for material you | |||
add to a covered work, you may (if authorized by the copyright holders of | |||
that material) supplement the terms of this License with terms: | |||
a) Disclaiming warranty or limiting liability differently from the | |||
terms of sections 15 and 16 of this License; or | |||
b) Requiring preservation of specified reasonable legal notices or | |||
author attributions in that material or in the Appropriate Legal | |||
Notices displayed by works containing it; or | |||
c) Prohibiting misrepresentation of the origin of that material, or | |||
requiring that modified versions of such material be marked in | |||
reasonable ways as different from the original version; or | |||
d) Limiting the use for publicity purposes of names of licensors or | |||
authors of the material; or | |||
e) Declining to grant rights under trademark law for use of some | |||
trade names, trademarks, or service marks; or | |||
f) Requiring indemnification of licensors and authors of that | |||
material by anyone who conveys the material (or modified versions of | |||
it) with contractual assumptions of liability to the recipient, for | |||
any liability that these contractual assumptions directly impose on | |||
those licensors and authors. | |||
All other non-permissive additional terms are considered "further | |||
restrictions" within the meaning of section 10. If the Program as you | |||
received it, or any part of it, contains a notice stating that it is | |||
governed by this License along with a term that is a further | |||
restriction, you may remove that term. If a license document contains | |||
a further restriction but permits relicensing or conveying under this | |||
License, you may add to a covered work material governed by the terms | |||
of that license document, provided that the further restriction does | |||
not survive such relicensing or conveying. | |||
If you add terms to a covered work in accord with this section, you | |||
must place, in the relevant source files, a statement of the | |||
additional terms that apply to those files, or a notice indicating | |||
where to find the applicable terms. | |||
Additional terms, permissive or non-permissive, may be stated in the | |||
form of a separately written license, or stated as exceptions; | |||
the above requirements apply either way. | |||
8. Termination. | |||
You may not propagate or modify a covered work except as expressly | |||
provided under this License. Any attempt otherwise to propagate or | |||
modify it is void, and will automatically terminate your rights under | |||
this License (including any patent licenses granted under the third | |||
paragraph of section 11). | |||
However, if you cease all violation of this License, then your | |||
license from a particular copyright holder is reinstated (a) | |||
provisionally, unless and until the copyright holder explicitly and | |||
finally terminates your license, and (b) permanently, if the copyright | |||
holder fails to notify you of the violation by some reasonable means | |||
prior to 60 days after the cessation. | |||
Moreover, your license from a particular copyright holder is | |||
reinstated permanently if the copyright holder notifies you of the | |||
violation by some reasonable means, this is the first time you have | |||
received notice of violation of this License (for any work) from that | |||
copyright holder, and you cure the violation prior to 30 days after | |||
your receipt of the notice. | |||
Termination of your rights under this section does not terminate the | |||
licenses of parties who have received copies or rights from you under | |||
this License. If your rights have been terminated and not permanently | |||
reinstated, you do not qualify to receive new licenses for the same | |||
material under section 10. | |||
9. Acceptance Not Required for Having Copies. | |||
You are not required to accept this License in order to receive or | |||
run a copy of the Program. Ancillary propagation of a covered work | |||
occurring solely as a consequence of using peer-to-peer transmission | |||
to receive a copy likewise does not require acceptance. However, | |||
nothing other than this License grants you permission to propagate or | |||
modify any covered work. These actions infringe copyright if you do | |||
not accept this License. Therefore, by modifying or propagating a | |||
covered work, you indicate your acceptance of this License to do so. | |||
10. Automatic Licensing of Downstream Recipients. | |||
Each time you convey a covered work, the recipient automatically | |||
receives a license from the original licensors, to run, modify and | |||
propagate that work, subject to this License. You are not responsible | |||
for enforcing compliance by third parties with this License. | |||
An "entity transaction" is a transaction transferring control of an | |||
organization, or substantially all assets of one, or subdividing an | |||
organization, or merging organizations. If propagation of a covered | |||
work results from an entity transaction, each party to that | |||
transaction who receives a copy of the work also receives whatever | |||
licenses to the work the party's predecessor in interest had or could | |||
give under the previous paragraph, plus a right to possession of the | |||
Corresponding Source of the work from the predecessor in interest, if | |||
the predecessor has it or can get it with reasonable efforts. | |||
You may not impose any further restrictions on the exercise of the | |||
rights granted or affirmed under this License. For example, you may | |||
not impose a license fee, royalty, or other charge for exercise of | |||
rights granted under this License, and you may not initiate litigation | |||
(including a cross-claim or counterclaim in a lawsuit) alleging that | |||
any patent claim is infringed by making, using, selling, offering for | |||
sale, or importing the Program or any portion of it. | |||
11. Patents. | |||
A "contributor" is a copyright holder who authorizes use under this | |||
License of the Program or a work on which the Program is based. The | |||
work thus licensed is called the contributor's "contributor version". | |||
A contributor's "essential patent claims" are all patent claims | |||
owned or controlled by the contributor, whether already acquired or | |||
hereafter acquired, that would be infringed by some manner, permitted | |||
by this License, of making, using, or selling its contributor version, | |||
but do not include claims that would be infringed only as a | |||
consequence of further modification of the contributor version. For | |||
purposes of this definition, "control" includes the right to grant | |||
patent sublicenses in a manner consistent with the requirements of | |||
this License. | |||
Each contributor grants you a non-exclusive, worldwide, royalty-free | |||
patent license under the contributor's essential patent claims, to | |||
make, use, sell, offer for sale, import and otherwise run, modify and | |||
propagate the contents of its contributor version. | |||
In the following three paragraphs, a "patent license" is any express | |||
agreement or commitment, however denominated, not to enforce a patent | |||
(such as an express permission to practice a patent or covenant not to | |||
sue for patent infringement). To "grant" such a patent license to a | |||
party means to make such an agreement or commitment not to enforce a | |||
patent against the party. | |||
If you convey a covered work, knowingly relying on a patent license, | |||
and the Corresponding Source of the work is not available for anyone | |||
to copy, free of charge and under the terms of this License, through a | |||
publicly available network server or other readily accessible means, | |||
then you must either (1) cause the Corresponding Source to be so | |||
available, or (2) arrange to deprive yourself of the benefit of the | |||
patent license for this particular work, or (3) arrange, in a manner | |||
consistent with the requirements of this License, to extend the patent | |||
license to downstream recipients. "Knowingly relying" means you have | |||
actual knowledge that, but for the patent license, your conveying the | |||
covered work in a country, or your recipient's use of the covered work | |||
in a country, would infringe one or more identifiable patents in that | |||
country that you have reason to believe are valid. | |||
If, pursuant to or in connection with a single transaction or | |||
arrangement, you convey, or propagate by procuring conveyance of, a | |||
covered work, and grant a patent license to some of the parties | |||
receiving the covered work authorizing them to use, propagate, modify | |||
or convey a specific copy of the covered work, then the patent license | |||
you grant is automatically extended to all recipients of the covered | |||
work and works based on it. | |||
A patent license is "discriminatory" if it does not include within | |||
the scope of its coverage, prohibits the exercise of, or is | |||
conditioned on the non-exercise of one or more of the rights that are | |||
specifically granted under this License. You may not convey a covered | |||
work if you are a party to an arrangement with a third party that is | |||
in the business of distributing software, under which you make payment | |||
to the third party based on the extent of your activity of conveying | |||
the work, and under which the third party grants, to any of the | |||
parties who would receive the covered work from you, a discriminatory | |||
patent license (a) in connection with copies of the covered work | |||
conveyed by you (or copies made from those copies), or (b) primarily | |||
for and in connection with specific products or compilations that | |||
contain the covered work, unless you entered into that arrangement, | |||
or that patent license was granted, prior to 28 March 2007. | |||
Nothing in this License shall be construed as excluding or limiting | |||
any implied license or other defenses to infringement that may | |||
otherwise be available to you under applicable patent law. | |||
12. No Surrender of Others' Freedom. | |||
If conditions are imposed on you (whether by court order, agreement or | |||
otherwise) that contradict the conditions of this License, they do not | |||
excuse you from the conditions of this License. If you cannot convey a | |||
covered work so as to satisfy simultaneously your obligations under this | |||
License and any other pertinent obligations, then as a consequence you may | |||
not convey it at all. For example, if you agree to terms that obligate you | |||
to collect a royalty for further conveying from those to whom you convey | |||
the Program, the only way you could satisfy both those terms and this | |||
License would be to refrain entirely from conveying the Program. | |||
13. Use with the GNU Affero General Public License. | |||
Notwithstanding any other provision of this License, you have | |||
permission to link or combine any covered work with a work licensed | |||
under version 3 of the GNU Affero General Public License into a single | |||
combined work, and to convey the resulting work. The terms of this | |||
License will continue to apply to the part which is the covered work, | |||
but the special requirements of the GNU Affero General Public License, | |||
section 13, concerning interaction through a network will apply to the | |||
combination as such. | |||
14. Revised Versions of this License. | |||
The Free Software Foundation may publish revised and/or new versions of | |||
the GNU General Public License from time to time. Such new versions will | |||
be similar in spirit to the present version, but may differ in detail to | |||
address new problems or concerns. | |||
Each version is given a distinguishing version number. If the | |||
Program specifies that a certain numbered version of the GNU General | |||
Public License "or any later version" applies to it, you have the | |||
option of following the terms and conditions either of that numbered | |||
version or of any later version published by the Free Software | |||
Foundation. If the Program does not specify a version number of the | |||
GNU General Public License, you may choose any version ever published | |||
by the Free Software Foundation. | |||
If the Program specifies that a proxy can decide which future | |||
versions of the GNU General Public License can be used, that proxy's | |||
public statement of acceptance of a version permanently authorizes you | |||
to choose that version for the Program. | |||
Later license versions may give you additional or different | |||
permissions. However, no additional obligations are imposed on any | |||
author or copyright holder as a result of your choosing to follow a | |||
later version. | |||
15. Disclaimer of Warranty. | |||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY | |||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT | |||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY | |||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, | |||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM | |||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF | |||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION. | |||
16. Limitation of Liability. | |||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING | |||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS | |||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY | |||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE | |||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF | |||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD | |||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), | |||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF | |||
SUCH DAMAGES. | |||
17. Interpretation of Sections 15 and 16. | |||
If the disclaimer of warranty and limitation of liability provided | |||
above cannot be given local legal effect according to their terms, | |||
reviewing courts shall apply local law that most closely approximates | |||
an absolute waiver of all civil liability in connection with the | |||
Program, unless a warranty or assumption of liability accompanies a | |||
copy of the Program in return for a fee. | |||
END OF TERMS AND CONDITIONS | |||
How to Apply These Terms to Your New Programs | |||
If you develop a new program, and you want it to be of the greatest | |||
possible use to the public, the best way to achieve this is to make it | |||
free software which everyone can redistribute and change under these terms. | |||
To do so, attach the following notices to the program. It is safest | |||
to attach them to the start of each source file to most effectively | |||
state the exclusion of warranty; and each file should have at least | |||
the "copyright" line and a pointer to where the full notice is found. | |||
<one line to give the program's name and a brief idea of what it does.> | |||
Copyright (C) <year> <name of author> | |||
This program is free software: you can redistribute it and/or modify | |||
it under the terms of the GNU General Public License as published by | |||
the Free Software Foundation, either version 3 of the License, or | |||
(at your option) any later version. | |||
This program is distributed in the hope that it will be useful, | |||
but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
GNU General Public License for more details. | |||
You should have received a copy of the GNU General Public License | |||
along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
Also add information on how to contact you by electronic and paper mail. | |||
If the program does terminal interaction, make it output a short | |||
notice like this when it starts in an interactive mode: | |||
<program> Copyright (C) <year> <name of author> | |||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. | |||
This is free software, and you are welcome to redistribute it | |||
under certain conditions; type `show c' for details. | |||
The hypothetical commands `show w' and `show c' should show the appropriate | |||
parts of the General Public License. Of course, your program's commands | |||
might be different; for a GUI interface, you would use an "about box". | |||
You should also get your employer (if you work as a programmer) or school, | |||
if any, to sign a "copyright disclaimer" for the program, if necessary. | |||
For more information on this, and how to apply and follow the GNU GPL, see | |||
<http://www.gnu.org/licenses/>. | |||
The GNU General Public License does not permit incorporating your program | |||
into proprietary programs. If your program is a subroutine library, you | |||
may consider it more useful to permit linking proprietary applications with | |||
the library. If this is what you want to do, use the GNU Lesser General | |||
Public License instead of this License. But first, please read | |||
<http://www.gnu.org/philosophy/why-not-lgpl.html>. |
@@ -0,0 +1,110 @@ | |||
<a href="https://apps.apple.com/app/id1452689527" target="_blank"> | |||
<img src="https://user-images.githubusercontent.com/26833433/82944393-f7644d80-9f4f-11ea-8b87-1a5b04f555f1.jpg" width="1000"></a> | |||
  | |||
This repository represents Ultralytics open-source research into future object detection methods, and incorporates our lessons learned and best practices evolved over training thousands of models on custom client datasets with our previous YOLO repository https://github.com/ultralytics/yolov3. **All code and models are under active development, and are subject to modification or deletion without notice.** Use at your own risk. | |||
Updates: | |||
- **May 27, 2020**: Public release of repo. yolov3-spp (this repo) is SOTA among all known yolo implementations, yolov5 family will be undergoing architecture research and development over Q2/Q3 2020 to increase performance. Updates may include CSP bottlenecks from yolov4, as well as PANet or BiFPN head features. | |||
- **May 24, 2020**: Training yolov5s/x and yolov3-spp. yolov5m/l suffered early overfitting and also code 137 early docker terminations, cause unknown. yolov5l underperforms yolov3-spp due to earlier overfitting, cause unknown. | |||
- **April 1, 2020**: Begin development of a 100% pytorch scaleable yolov3/4-based group of future models, in small, medium, large and extra large sizes, collectively known as yolov5. Models will be defined by new user-friendly yaml-based configuration files for ease of construction and modification. Datasets will likewise use yaml configuration files. New training platform will be simpler use, harder to break, and more robust to training a wider variety of custom dataset. | |||
## Ultralytics Professional Support | |||
Ultralytics is a U.S.-based particle physics and AI startup with over 6 years of expertise supporting government, academic and business clients. We offer a wide range of vision AI services, spanning from simple expert advice up to delivery of fully customized, end-to-end production solutions, including: | |||
- **Cloud-based AI** surveillance systems operating on **hundreds of HD video streams in realtime.** | |||
- **Edge AI** integrated into custom iOS and Android apps for realtime **30 FPS video inference.** | |||
- **Custom data training**, hyperparameter evolution, and model exportation to any destination. | |||
For business inquiries and professional support requests please visit us at https://www.ultralytics.com. | |||
## Pretrained Checkpoints | |||
| Model | AP<sup>val</sup> | AP<sup>test</sup> | AP<sub>50</sub> | Latency<sub>GPU</sub> | FPS<sub>GPU</sub> | | params | FLOPs | | |||
|---------- |------ |------ |------ | -------- | ------| ------ |------ | :------: | | |||
| YOLOv5-s ([ckpt](https://drive.google.com/open?id=1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J)) | 33.1 | 33.0 | 53.3 | **3.3ms** | **303** | | 7.0B | 14.0 | |||
| YOLOv5-m ([ckpt](https://drive.google.com/open?id=1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J)) | 41.5 | 41.5 | 61.5 | 5.5ms | 182 | | 25.2B | 50.2 | |||
| YOLOv5-l ([ckpt](https://drive.google.com/open?id=1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J)) | 44.2 | 44.5 | 64.3 | 9.7ms | 103 | | 61.8B | 123.1 | |||
| YOLOv5-x ([ckpt](https://drive.google.com/open?id=1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J)) | **47.1** | **47.2** | **66.7** | 15.8ms | 63 | | 123.1B | 245.7 | |||
| YOLOv3-SPP ([ckpt](https://drive.google.com/open?id=1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J)) | 45.5 | 45.4 | 65.2 | 8.9ms | 112 | | 63.0B | 118.0 | |||
** AP<sup>test</sup> denotes COCO [test-dev2017](http://cocodataset.org/#upload) server results, all other AP results in the table denote val2017 accuracy. | |||
** All accuracy numbers are for single-model single-scale without ensemble or test-time augmentation. Reproduce by `python test.py --img-size 736 --conf_thres 0.001` | |||
** Latency<sub>GPU</sub> measures end-to-end latency per image averaged over 5000 COCO val2017 images using a V100 GPU and includes image preprocessing, inference, postprocessing and NMS. Average NMS time included in this chart is 1.6ms/image. Reproduce by `python test.py --img-size 640 --conf_thres 0.1 --batch-size 16` | |||
** All checkpoints are trained to 300 epochs with default settings and hyperparameters (no autoaugmentation). | |||
## Requirements | |||
Python 3.7 or later with all `requirements.txt` dependencies installed, including `torch >= 1.5`. To install run: | |||
```bash | |||
$ pip install -U -r requirements.txt | |||
``` | |||
## Tutorials | |||
* [Train Custom Data](https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data) | |||
* [Google Colab Notebook](https://colab.research.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb) with training, testing and testing examples | |||
* [GCP Quickstart](https://github.com/ultralytics/yolov5/wiki/GCP-Quickstart) | |||
* [Docker Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/Docker-Quickstart) | |||
## Inference | |||
Inference can be run on most common media formats. Model [checkpoints](https://drive.google.com/open?id=1Drs_Aiu7xx6S-ix95f9kNsA6ueKRpN2J) are downloaded automatically if available. Results are saved to `./inference/output`. | |||
```bash | |||
$ python detect.py --source file.jpg # image | |||
file.mp4 # video | |||
./dir # directory | |||
0 # webcam | |||
rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa # rtsp stream | |||
http://112.50.243.8/PLTV/88888888/224/3221225900/1.m3u8 # http stream | |||
``` | |||
To run inference on examples in the `./inference/images` folder: | |||
```bash | |||
$ python detect.py --source ./inference/images/ --weights yolov5s.pt --conf 0.4 | |||
Namespace(agnostic_nms=False, augment=False, classes=None, conf_thres=0.4, device='', fourcc='mp4v', half=False, img_size=640, iou_thres=0.5, output='inference/output', save_txt=False, source='./inference/images/', view_img=False, weights='yolov5s.pt') | |||
Using CUDA device0 _CudaDeviceProperties(name='Tesla P100-PCIE-16GB', total_memory=16280MB) | |||
Downloading https://drive.google.com/uc?export=download&id=1R5T6rIyy3lLwgFXNms8whc-387H0tMQO as yolov5s.pt... Done (2.6s) | |||
image 1/2 inference/images/bus.jpg: 640x512 3 persons, 1 buss, Done. (0.009s) | |||
image 2/2 inference/images/zidane.jpg: 384x640 2 persons, 2 ties, Done. (0.009s) | |||
Results saved to /content/yolov5/inference/output | |||
``` | |||
<img src="https://user-images.githubusercontent.com/26833433/83082816-59e54880-a039-11ea-8abe-ab90cc1ec4b0.jpeg" width="500"> | |||
## Reproduce Our Training | |||
Run commands below. Training takes a few days for yolov5s, to a few weeks for yolov5x on a 2080Ti GPU. | |||
```bash | |||
$ python train.py --data coco.yaml --cfg yolov5s.yaml --weights '' --batch-size 16 | |||
``` | |||
<img src="https://user-images.githubusercontent.com/26833433/82960433-5a191180-9f6f-11ea-85cc-c49dbd1555e1.png" width="900"> | |||
## Reproduce Our Environment | |||
To access an up-to-date working environment (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled), consider a: | |||
- **GCP** Deep Learning VM with $300 free credit offer: See our [GCP Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/GCP-Quickstart) | |||
- **Google Colab Notebook** with 12 hours of free GPU time: [Google Colab Notebook](https://colab.sandbox.google.com/github/ultralytics/yolov5/blob/master/tutorial.ipynb) | |||
- **Docker Image** from https://hub.docker.com/r/ultralytics/yolov5. See [Docker Quickstart Guide](https://github.com/ultralytics/yolov5/wiki/Docker-Quickstart) | |||
## Citation | |||
[![DOI](https://zenodo.org/badge/146165888.svg)](https://zenodo.org/badge/latestdoi/146165888) | |||
## Contact | |||
**Issues should be raised directly in the repository.** For business inquiries or professional support requests please visit us at https://www.ultralytics.com. |
@@ -0,0 +1,33 @@ | |||
# COCO 2017 dataset http://cocodataset.org | |||
# Download command: bash yolov5/data/get_coco2017.sh | |||
# Train command: python train.py --data ./data/coco.yaml | |||
# Dataset should be placed next to yolov5 folder: | |||
# /parent_folder | |||
# /coco | |||
# /yolov5 | |||
# train and val datasets (image directory or *.txt file with image paths) | |||
train: ../coco/train2017.txt # 118k images | |||
val: ../coco/val2017.txt # 5k images | |||
test: ../coco/test-dev2017.txt # 20k images for submission to https://competitions.codalab.org/competitions/20794 | |||
# number of classes | |||
nc: 80 | |||
# class names | |||
names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', | |||
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', | |||
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', | |||
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', | |||
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', | |||
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', | |||
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', | |||
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', | |||
'hair drier', 'toothbrush'] | |||
# Print classes | |||
# with open('data/coco.yaml') as f: | |||
# d = yaml.load(f, Loader=yaml.FullLoader) # dict | |||
# for i, x in enumerate(d['names']): | |||
# print(i, x) |
@@ -0,0 +1,26 @@ | |||
# COCO 2017 dataset http://cocodataset.org - first 128 training images | |||
# Download command: python -c "from yolov5.utils.google_utils import gdrive_download; gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')" | |||
# Train command: python train.py --data ./data/coco128.yaml | |||
# Dataset should be placed next to yolov5 folder: | |||
# /parent_folder | |||
# /coco128 | |||
# /yolov5 | |||
# train and val datasets (image directory or *.txt file with image paths) | |||
train: ../coco128/images/train2017/ | |||
val: ../coco128/images/train2017/ | |||
# number of classes | |||
nc: 80 | |||
# class names | |||
names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', | |||
'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', | |||
'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', | |||
'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', | |||
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', | |||
'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', | |||
'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', | |||
'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', | |||
'hair drier', 'toothbrush'] |
@@ -0,0 +1,25 @@ | |||
#!/bin/bash | |||
# Zip coco folder | |||
# zip -r coco.zip coco | |||
# tar -czvf coco.tar.gz coco | |||
# Download labels from Google Drive, accepting presented query | |||
filename="coco2017labels.zip" | |||
fileid="1cXZR_ckHki6nddOmcysCuuJFM--T-Q6L" | |||
curl -c ./cookie -s -L "https://drive.google.com/uc?export=download&id=${fileid}" > /dev/null | |||
curl -Lb ./cookie "https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=${fileid}" -o ${filename} | |||
rm ./cookie | |||
# Unzip labels | |||
unzip -q ${filename} # for coco.zip | |||
# tar -xzf ${filename} # for coco.tar.gz | |||
rm ${filename} | |||
# Download and unzip images | |||
cd coco/images | |||
f="train2017.zip" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f && rm $f # 19G, 118k images | |||
f="val2017.zip" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f && rm $f # 1G, 5k images | |||
# f="test2017.zip" && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f && rm $f # 7G, 41k images | |||
# cd out | |||
cd ../.. |
@@ -0,0 +1,182 @@ | |||
import argparse | |||
from utils.datasets import * | |||
from utils.utils import * | |||
ONNX_EXPORT = False | |||
def detect(save_img=False): | |||
imgsz = (320, 192) if ONNX_EXPORT else opt.img_size # (320, 192) or (416, 256) or (608, 352) for (height, width) | |||
out, source, weights, half, view_img, save_txt = opt.output, opt.source, opt.weights, opt.half, opt.view_img, opt.save_txt | |||
webcam = source == '0' or source.startswith('rtsp') or source.startswith('http') or source.endswith('.txt') | |||
# Initialize | |||
device = torch_utils.select_device(device='cpu' if ONNX_EXPORT else opt.device) | |||
if os.path.exists(out): | |||
shutil.rmtree(out) # delete output folder | |||
os.makedirs(out) # make new output folder | |||
# Load model | |||
google_utils.attempt_download(weights) | |||
model = torch.load(weights, map_location=device)['model'] | |||
# torch.save(torch.load(weights, map_location=device), weights) # update model if SourceChangeWarning | |||
# Second-stage classifier | |||
classify = False | |||
if classify: | |||
modelc = torch_utils.load_classifier(name='resnet101', n=2) # initialize | |||
modelc.load_state_dict(torch.load('weights/resnet101.pt', map_location=device)['model']) # load weights | |||
modelc.to(device).eval() | |||
# Eval mode | |||
model.to(device).eval() | |||
# Fuse Conv2d + BatchNorm2d layers | |||
# model.fuse() | |||
# Export mode | |||
if ONNX_EXPORT: | |||
model.fuse() | |||
img = torch.zeros((1, 3) + imgsz) # (1, 3, 320, 192) | |||
f = opt.weights.replace(opt.weights.split('.')[-1], 'onnx') # *.onnx filename | |||
torch.onnx.export(model, img, f, verbose=False, opset_version=11) | |||
# Validate exported model | |||
import onnx | |||
model = onnx.load(f) # Load the ONNX model | |||
onnx.checker.check_model(model) # Check that the IR is well formed | |||
print(onnx.helper.printable_graph(model.graph)) # Print a human readable representation of the graph | |||
return | |||
# Half precision | |||
half = half and device.type != 'cpu' # half precision only supported on CUDA | |||
if half: | |||
model.half() | |||
# Set Dataloader | |||
vid_path, vid_writer = None, None | |||
if webcam: | |||
view_img = True | |||
torch.backends.cudnn.benchmark = True # set True to speed up constant image size inference | |||
dataset = LoadStreams(source, img_size=imgsz) | |||
else: | |||
save_img = True | |||
dataset = LoadImages(source, img_size=imgsz) | |||
# Get names and colors | |||
names = model.names | |||
colors = [[random.randint(0, 255) for _ in range(3)] for _ in range(len(names))] | |||
# Run inference | |||
t0 = time.time() | |||
img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img | |||
_ = model(img.half() if half else img.float()) if device.type != 'cpu' else None # run once | |||
for path, img, im0s, vid_cap in dataset: | |||
img = torch.from_numpy(img).to(device) | |||
img = img.half() if half else img.float() # uint8 to fp16/32 | |||
img /= 255.0 # 0 - 255 to 0.0 - 1.0 | |||
if img.ndimension() == 3: | |||
img = img.unsqueeze(0) | |||
# Inference | |||
t1 = torch_utils.time_synchronized() | |||
pred = model(img, augment=opt.augment)[0] | |||
t2 = torch_utils.time_synchronized() | |||
# to float | |||
if half: | |||
pred = pred.float() | |||
# Apply NMS | |||
pred = non_max_suppression(pred, opt.conf_thres, opt.iou_thres, | |||
multi_label=False, classes=opt.classes, agnostic=opt.agnostic_nms) | |||
# Apply Classifier | |||
if classify: | |||
pred = apply_classifier(pred, modelc, img, im0s) | |||
# Process detections | |||
for i, det in enumerate(pred): # detections per image | |||
if webcam: # batch_size >= 1 | |||
p, s, im0 = path[i], '%g: ' % i, im0s[i] | |||
else: | |||
p, s, im0 = path, '', im0s | |||
save_path = str(Path(out) / Path(p).name) | |||
s += '%gx%g ' % img.shape[2:] # print string | |||
gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh | |||
if det is not None and len(det): | |||
# Rescale boxes from img_size to im0 size | |||
det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() | |||
# Print results | |||
for c in det[:, -1].unique(): | |||
n = (det[:, -1] == c).sum() # detections per class | |||
s += '%g %ss, ' % (n, names[int(c)]) # add to string | |||
# Write results | |||
for *xyxy, conf, cls in det: | |||
if save_txt: # Write to file | |||
xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh | |||
with open(save_path[:save_path.rfind('.')] + '.txt', 'a') as file: | |||
file.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format | |||
if save_img or view_img: # Add bbox to image | |||
label = '%s %.2f' % (names[int(cls)], conf) | |||
plot_one_box(xyxy, im0, label=label, color=colors[int(cls)], line_thickness=3) | |||
# Print time (inference + NMS) | |||
print('%sDone. (%.3fs)' % (s, t2 - t1)) | |||
# Stream results | |||
if view_img: | |||
cv2.imshow(p, im0) | |||
if cv2.waitKey(1) == ord('q'): # q to quit | |||
raise StopIteration | |||
# Save results (image with detections) | |||
if save_img: | |||
if dataset.mode == 'images': | |||
cv2.imwrite(save_path, im0) | |||
else: | |||
if vid_path != save_path: # new video | |||
vid_path = save_path | |||
if isinstance(vid_writer, cv2.VideoWriter): | |||
vid_writer.release() # release previous video writer | |||
fps = vid_cap.get(cv2.CAP_PROP_FPS) | |||
w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |||
h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |||
vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*opt.fourcc), fps, (w, h)) | |||
vid_writer.write(im0) | |||
if save_txt or save_img: | |||
print('Results saved to %s' % os.getcwd() + os.sep + out) | |||
if platform == 'darwin': # MacOS | |||
os.system('open ' + save_path) | |||
print('Done. (%.3fs)' % (time.time() - t0)) | |||
if __name__ == '__main__': | |||
parser = argparse.ArgumentParser() | |||
parser.add_argument('--weights', type=str, default='weights/yolov5s.pt', help='model.pt path') | |||
parser.add_argument('--source', type=str, default='inference/images', help='source') # file/folder, 0 for webcam | |||
parser.add_argument('--output', type=str, default='inference/output', help='output folder') # output folder | |||
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)') | |||
parser.add_argument('--conf-thres', type=float, default=0.4, help='object confidence threshold') | |||
parser.add_argument('--iou-thres', type=float, default=0.5, help='IOU threshold for NMS') | |||
parser.add_argument('--fourcc', type=str, default='mp4v', help='output video codec (verify ffmpeg support)') | |||
parser.add_argument('--half', action='store_true', help='half precision FP16 inference') | |||
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') | |||
parser.add_argument('--view-img', action='store_true', help='display results') | |||
parser.add_argument('--save-txt', action='store_true', help='save results to *.txt') | |||
parser.add_argument('--classes', nargs='+', type=int, help='filter by class') | |||
parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS') | |||
parser.add_argument('--augment', action='store_true', help='augmented inference') | |||
opt = parser.parse_args() | |||
print(opt) | |||
with torch.no_grad(): | |||
detect() |
@@ -0,0 +1,174 @@ | |||
# This file contains modules common to various models | |||
import torch.nn.functional as F | |||
from utils.utils import * | |||
def DWConv(c1, c2, k=1, s=1, act=True): # depthwise convolution | |||
return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act) | |||
class Conv(nn.Module): # standard convolution | |||
def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups | |||
super(Conv, self).__init__() | |||
self.conv = nn.Conv2d(c1, c2, k, s, k // 2, groups=g, bias=False) | |||
self.bn = nn.BatchNorm2d(c2) | |||
self.act = nn.LeakyReLU(0.1, inplace=True) if act else nn.Identity() | |||
def forward(self, x): | |||
return self.act(self.bn(self.conv(x))) | |||
class Bottleneck(nn.Module): | |||
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion | |||
super(Bottleneck, self).__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 BottleneckLight(nn.Module): | |||
def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion | |||
super(BottleneckLight, self).__init__() | |||
c_ = int(c2 * e) # hidden channels | |||
self.cv1 = Conv(c1, c_, 1, 1) | |||
self.cv2 = nn.Conv2d(c_, c2, 3, 1, 3 // 2, groups=g, bias=False) | |||
self.bn = nn.BatchNorm2d(c2) | |||
self.act = nn.LeakyReLU(0.1, inplace=True) | |||
self.add = shortcut and c1 == c2 | |||
def forward(self, x): | |||
return self.act(self.bn(x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)))) | |||
class BottleneckCSP(nn.Module): | |||
def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion | |||
super(BottleneckCSP, self).__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(c2, c2, 1, 1) | |||
self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) | |||
self.act = nn.LeakyReLU(0.1, inplace=True) | |||
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), dim=1)))) | |||
class BottleneckCSPF(nn.Module): | |||
def __init__(self, c1, c2, n=1, shortcut=False, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion | |||
super(BottleneckCSPF, self).__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(c2, c2, 1, 1) | |||
self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) | |||
self.act = nn.LeakyReLU(0.1, inplace=True) | |||
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), dim=1)))) | |||
class Narrow(nn.Module): | |||
def __init__(self, c1, c2, shortcut=True, g=1): # ch_in, ch_out, shortcut, groups | |||
super(Narrow, self).__init__() | |||
c_ = c2 // 2 # 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 Origami(nn.Module): # 5-side layering | |||
def forward(self, x): | |||
y = F.pad(x, [1, 1, 1, 1]) | |||
return torch.cat([x, y[..., :-2, 1:-1], y[..., 1:-1, :-2], y[..., 2:, 1:-1], y[..., 1:-1, 2:]], 1) | |||
class ConvPlus(nn.Module): # standard convolution | |||
def __init__(self, c1, c2, k=3, s=1, g=1, bias=True): # ch_in, ch_out, kernel, stride, groups | |||
super(ConvPlus, self).__init__() | |||
self.cv1 = nn.Conv2d(c1, c2, (k, 1), s, (k // 2, 0), groups=g, bias=bias) | |||
self.cv2 = nn.Conv2d(c1, c2, (1, k), s, (0, k // 2), groups=g, bias=bias) | |||
def forward(self, x): | |||
return self.cv1(x) + self.cv2(x) | |||
class SPP(nn.Module): # Spatial pyramid pooling layer used in YOLOv3-SPP | |||
def __init__(self, c1, c2, k=(5, 9, 13)): | |||
super(SPP, self).__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) | |||
return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1)) | |||
class Flatten(nn.Module): | |||
# Use after nn.AdaptiveAvgPool2d(1) to remove last 2 dimensions | |||
def forward(self, x): | |||
return x.view(x.size(0), -1) | |||
class Focus(nn.Module): | |||
# Focus wh information into c-space | |||
def __init__(self, c1, c2, k=1): | |||
super(Focus, self).__init__() | |||
self.conv = Conv(c1 * 4, c2, k, 1) | |||
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)) | |||
class Concat(nn.Module): | |||
# Concatenate a list of tensors along dimension | |||
def __init__(self, dimension=1): | |||
super(Concat, self).__init__() | |||
self.d = dimension | |||
def forward(self, x): | |||
return torch.cat(x, self.d) | |||
class MixConv2d(nn.Module): | |||
# Mixed Depthwise Conv https://arxiv.org/abs/1907.09595 | |||
def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): | |||
super(MixConv2d, self).__init__() | |||
groups = len(k) | |||
if equal_ch: # equal c_ per group | |||
i = torch.linspace(0, groups - 1E-6, c2).floor() # c2 indices | |||
c_ = [(i == g).sum() for g in range(groups)] # intermediate channels | |||
else: # equal weight.numel() per group | |||
b = [c2] + [0] * groups | |||
a = np.eye(groups + 1, groups, 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_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)]) | |||
self.bn = nn.BatchNorm2d(c2) | |||
self.act = nn.LeakyReLU(0.1, inplace=True) | |||
def forward(self, x): | |||
return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1))) |
@@ -0,0 +1,48 @@ | |||
from models.common import * | |||
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(Sum, self).__init__() | |||
self.weight = weight # apply weights boolean | |||
self.iter = range(n - 1) # iter object | |||
if weight: | |||
self.w = nn.Parameter(-torch.arange(1., 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 GhostConv(nn.Module): | |||
def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups | |||
super(GhostConv, self).__init__() | |||
c_ = c2 // 2 # hidden channels | |||
self.cv1 = Conv(c1, c_, k, s, g, act) | |||
self.cv2 = Conv(c_, c_, 5, 1, c_, act) | |||
def forward(self, x): | |||
y = self.cv1(x) | |||
return torch.cat([y, self.cv2(y)], 1) | |||
class GhostBottleneck(nn.Module): | |||
def __init__(self, c1, c2, k, s): | |||
super(GhostBottleneck, self).__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) |
@@ -0,0 +1,197 @@ | |||
import argparse | |||
import yaml | |||
from models.common import * | |||
class Detect(nn.Module): | |||
def __init__(self, nc=80, anchors=()): # detection layer | |||
super(Detect, self).__init__() | |||
self.stride = None # strides computed during build | |||
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.zeros(1)] * self.nl # init grid | |||
a = torch.tensor(anchors).float().view(self.nl, -1, 2) | |||
self.register_buffer('anchors', a) # shape(nl,na,2) | |||
self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) | |||
self.export = False # onnx export | |||
def forward(self, x): | |||
x = x.copy() | |||
z = [] # inference output | |||
self.training |= self.export | |||
for i in range(self.nl): | |||
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.grid[i].shape[2:4] != x[i].shape[2:4]: | |||
self.grid[i] = self._make_grid(nx, ny).to(x[i].device) | |||
y = x[i].sigmoid() | |||
y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i]) * self.stride[i] # xy | |||
y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh | |||
z.append(y.view(bs, -1, self.no)) | |||
return x if self.training else (torch.cat(z, 1), x) | |||
@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() | |||
class Model(nn.Module): | |||
def __init__(self, model_yaml='yolov5s.yaml'): # cfg, number of classes, depth-width gains | |||
super(Model, self).__init__() | |||
with open(model_yaml) as f: | |||
self.md = yaml.load(f, Loader=yaml.FullLoader) # model dict | |||
# Define model | |||
self.model, self.save, ch = parse_model(self.md, ch=[3]) # model, savelist, ch_out | |||
# print([x.shape for x in self.forward(torch.zeros(1, 3, 64, 64))]) | |||
# Build strides, anchors | |||
m = self.model[-1] # Detect() | |||
m.stride = torch.tensor([64 / x.shape[-2] for x in self.forward(torch.zeros(1, 3, 64, 64))]) # forward | |||
m.anchors /= m.stride.view(-1, 1, 1) | |||
self.stride = m.stride | |||
# Init weights, biases | |||
torch_utils.initialize_weights(self) | |||
self._initialize_biases() # only run once | |||
torch_utils.model_info(self) | |||
print('') | |||
def forward(self, x, augment=False, profile=False): | |||
y, ts = [], 0 # 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: | |||
import thop | |||
o = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 # FLOPS | |||
t = torch_utils.time_synchronized() | |||
for _ in range(10): | |||
_ = m(x) | |||
dt = torch_utils.time_synchronized() - t | |||
ts += dt | |||
print('%10.1f%10.0f%10.1fms %-40s' % (o, m.np, dt * 100, m.type)) | |||
x = m(x) # run | |||
y.append(x if m.i in self.save else None) # save output | |||
if profile: | |||
print(ts * 100) | |||
return x | |||
def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency | |||
# cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. | |||
m = self.model[-1] # Detect() module | |||
for f, s in zip(m.f, m.stride): # from | |||
mi = self.model[f % m.i] | |||
b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) | |||
b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) | |||
b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls | |||
mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) | |||
def _print_biases(self): | |||
m = self.model[-1] # Detect() module | |||
for f in sorted([x % m.i for x in m.f]): # from | |||
b = self.model[f].bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85) | |||
print(('%g Conv2d.bias:' + '%10.3g' * 6) % (f, *b[:5].mean(1).tolist(), b[5:].mean())) | |||
def parse_model(md, ch): # model_dict, input_channels(3) | |||
print('\n%3s%15s%3s%10s %-40s%-30s' % ('', 'from', 'n', 'params', 'module', 'arguments')) | |||
anchors, nc, gd, gw = md['anchors'], md['nc'], md['depth_multiple'], md['width_multiple'] | |||
na = (len(anchors[0]) // 2) # 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(md['backbone'] + md['head']): # from, number, module, args | |||
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: | |||
pass | |||
n = max(round(n * gd), 1) if n > 1 else n # depth gain | |||
if m in [nn.Conv2d, Conv, Bottleneck, SPP, DWConv, MixConv2d, Focus, ConvPlus, BottleneckCSP, BottleneckCSPF, BottleneckLight]: | |||
c1, c2 = ch[f], args[0] | |||
# Normal | |||
# if i > 0 and args[0] != no: # channel expansion factor | |||
# ex = 1.75 # exponential (default 2.0) | |||
# e = math.log(c2 / ch[1]) / math.log(2) | |||
# c2 = int(ch[1] * ex ** e) | |||
# if m != Focus: | |||
c2 = make_divisible(c2 * gw, 8) if c2 != no else c2 | |||
# Experimental | |||
# if i > 0 and args[0] != no: # channel expansion factor | |||
# ex = 1 + gw # exponential (default 2.0) | |||
# ch1 = 32 # ch[1] | |||
# e = math.log(c2 / ch1) / math.log(2) # level 1-n | |||
# c2 = int(ch1 * ex ** e) | |||
# if m != Focus: | |||
# c2 = make_divisible(c2, 8) if c2 != no else c2 | |||
args = [c1, c2, *args[1:]] | |||
if m in [BottleneckCSP, BottleneckCSPF]: | |||
args += [n] | |||
n = 1 | |||
elif m is nn.BatchNorm2d: | |||
args = [ch[f]] | |||
elif m is Concat: | |||
c2 = sum([ch[x] for x in f]) | |||
elif m is Origami: | |||
c2 = ch[f] * 5 | |||
elif m is Detect: | |||
f = f or list(reversed([(-1 if j == i else j - 1) for j, x in enumerate(ch) if x == no])) | |||
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 | |||
print('%3s%15s%3s%10.0f %-40s%-30s' % (i, f, n, np, t, args)) # 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 nn.Sequential(*layers), sorted(save), ch | |||
if __name__ == '__main__': | |||
parser = argparse.ArgumentParser() | |||
parser.add_argument('--cfg', type=str, default='yolov5s.yaml', help='model.yaml') | |||
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') | |||
opt = parser.parse_args() | |||
opt.cfg = glob.glob('./**/' + opt.cfg, recursive=True)[0] # find file | |||
device = torch_utils.select_device(opt.device) | |||
# Create model | |||
model = Model(opt.cfg).to(device) | |||
model.train() | |||
# Profile | |||
# img = torch.rand(8 if torch.cuda.is_available() else 1, 3, 640, 640).to(device) | |||
# y = model(img, profile=True) | |||
# print([y[0].shape] + [x.shape for x in y[1]]) | |||
# ONNX export | |||
# model.model[-1].export = True | |||
# torch.onnx.export(model, img, f.replace('.yaml', '.onnx'), verbose=True, opset_version=11) | |||
# Tensorboard | |||
# from torch.utils.tensorboard import SummaryWriter | |||
# tb_writer = SummaryWriter() | |||
# print("Run 'tensorboard --logdir=models/runs' to view tensorboard at http://localhost:6006/") | |||
# tb_writer.add_graph(model.model, img) # add model to tensorboard | |||
# tb_writer.add_image('test', img[0], dataformats='CWH') # add model to tensorboard |
@@ -0,0 +1,55 @@ | |||
# parameters | |||
nc: 80 # number of classes | |||
depth_multiple: 1.0 # expand model depth | |||
width_multiple: 1.0 # expand layer channels | |||
# anchors | |||
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 | |||
# na = len(anchors[0]) | |||
head: | |||
[[-1, 1, Bottleneck, [1024, False]], # 11 | |||
[-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]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1]], # 16 (P5/32-large) | |||
[-3, 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]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1]], # 24 (P4/16-medium) | |||
[-3, 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]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1]], # 30 (P3/8-small) | |||
[[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) | |||
] |
@@ -0,0 +1,55 @@ | |||
# parameters | |||
nc: 80 # number of classes | |||
depth_multiple: 1.0 # expand model depth | |||
width_multiple: 1.0 # expand layer channels | |||
# anchors | |||
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, BottleneckCSP, [64]], | |||
[-1, 1, Conv, [128, 3, 2]], # 3-P2/4 | |||
[-1, 2, BottleneckCSP, [128]], | |||
[-1, 1, Conv, [256, 3, 2]], # 5-P3/8 | |||
[-1, 8, BottleneckCSP, [256]], | |||
[-1, 1, Conv, [512, 3, 2]], # 7-P4/16 | |||
[-1, 8, BottleneckCSP, [512]], | |||
[-1, 1, Conv, [1024, 3, 2]], # 9-P5/32 | |||
[-1, 4, BottleneckCSP, [1024]], # 10 | |||
] | |||
# yolov3-spp head | |||
# na = len(anchors[0]) | |||
head: | |||
[[-1, 1, Bottleneck, [1024, False]], # 11 | |||
[-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]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1]], # 16 (P5/32-large) | |||
[-3, 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]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1]], # 24 (P4/16-medium) | |||
[-3, 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]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1]], # 30 (P3/8-small) | |||
[[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) | |||
] |
@@ -0,0 +1,45 @@ | |||
# parameters | |||
nc: 80 # number of classes | |||
depth_multiple: 1.0 # model depth multiple | |||
width_multiple: 1.0 # layer channel multiple | |||
# anchors | |||
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 backbone | |||
backbone: | |||
# [from, number, module, args] | |||
[[-1, 1, Focus, [64, 3]], # 1-P1/2 | |||
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4 | |||
[-1, 3, Bottleneck, [128]], | |||
[-1, 1, Conv, [256, 3, 2]], # 4-P3/8 | |||
[-1, 9, Bottleneck, [256]], | |||
[-1, 1, Conv, [512, 3, 2]], # 6-P4/16 | |||
[-1, 9, Bottleneck, [512]], | |||
[-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 | |||
[-1, 1, SPP, [1024, [5, 9, 13]]], | |||
[-1, 3, Bottleneck, [1024]], # 10 | |||
] | |||
# yolov5 head | |||
head: | |||
[[-1, 3, Bottleneck, [1024, False]], # 11 | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 6], 1, Concat, [1]], # cat backbone P4 | |||
[-1, 1, Conv, [512, 1, 1]], | |||
[-1, 3, Bottleneck, [512, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 4], 1, Concat, [1]], # cat backbone P3 | |||
[-1, 1, Conv, [256, 1, 1]], | |||
[-1, 3, Bottleneck, [256, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) | |||
[[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) | |||
] |
@@ -0,0 +1,46 @@ | |||
# parameters | |||
nc: 80 # number of classes | |||
depth_multiple: 0.67 # model depth multiple | |||
width_multiple: 0.75 # layer channel multiple | |||
# anchors | |||
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 backbone | |||
backbone: | |||
# [from, number, module, args] | |||
[[-1, 1, Focus, [64, 3]], # 1-P1/2 | |||
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4 | |||
[-1, 3, Bottleneck, [128]], | |||
[-1, 1, Conv, [256, 3, 2]], # 4-P3/8 | |||
[-1, 9, Bottleneck, [256]], | |||
[-1, 1, Conv, [512, 3, 2]], # 6-P4/16 | |||
[-1, 9, Bottleneck, [512]], | |||
[-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 | |||
[-1, 1, SPP, [1024, [5, 9, 13]]], | |||
[-1, 3, Bottleneck, [1024]], # 10 | |||
] | |||
# yolov5 head | |||
head: | |||
[[-1, 3, Bottleneck, [1024, False]], # 11 | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 6], 1, Concat, [1]], # cat backbone P4 | |||
[-1, 1, Conv, [512, 1, 1]], | |||
[-1, 3, Bottleneck, [512, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 4], 1, Concat, [1]], # cat backbone P3 | |||
[-1, 1, Conv, [256, 1, 1]], | |||
[-1, 3, Bottleneck, [256, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) | |||
[[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) | |||
] | |||
@@ -0,0 +1,45 @@ | |||
# parameters | |||
nc: 80 # number of classes | |||
depth_multiple: 0.33 # model depth multiple | |||
width_multiple: 0.50 # layer channel multiple | |||
# anchors | |||
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 backbone | |||
backbone: | |||
# [from, number, module, args] | |||
[[-1, 1, Focus, [64, 3]], # 1-P1/2 | |||
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4 | |||
[-1, 3, Bottleneck, [128]], | |||
[-1, 1, Conv, [256, 3, 2]], # 4-P3/8 | |||
[-1, 9, Bottleneck, [256]], | |||
[-1, 1, Conv, [512, 3, 2]], # 6-P4/16 | |||
[-1, 9, Bottleneck, [512]], | |||
[-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 | |||
[-1, 1, SPP, [1024, [5, 9, 13]]], | |||
[-1, 3, Bottleneck, [1024]], # 10 | |||
] | |||
# yolov5 head | |||
head: | |||
[[-1, 3, Bottleneck, [1024, False]], # 11 | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 6], 1, Concat, [1]], # cat backbone P4 | |||
[-1, 1, Conv, [512, 1, 1]], | |||
[-1, 3, Bottleneck, [512, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 4], 1, Concat, [1]], # cat backbone P3 | |||
[-1, 1, Conv, [256, 1, 1]], | |||
[-1, 3, Bottleneck, [256, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) | |||
[[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) | |||
] |
@@ -0,0 +1,46 @@ | |||
# parameters | |||
nc: 80 # number of classes | |||
depth_multiple: 0.33 # model depth multiple | |||
width_multiple: 0.50 # layer channel multiple | |||
# anchors | |||
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 backbone | |||
backbone: | |||
# [from, number, module, args] | |||
[[-1, 1, Focus, [64, 3]], # 1-P1/2 | |||
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4 | |||
[-1, 3, BottleneckCSP, [128]], | |||
[-1, 1, Conv, [256, 3, 2]], # 4-P3/8 | |||
[-1, 9, BottleneckCSP, [256]], | |||
[-1, 1, Conv, [512, 3, 2]], # 6-P4/16 | |||
[-1, 9, BottleneckCSP, [512]], | |||
[-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 | |||
[-1, 1, SPP, [1024, [5, 9, 13]]], | |||
[-1, 3, BottleneckCSP, [1024]], # 10 | |||
] | |||
# yolov5 head | |||
head: | |||
[[-1, 3, Bottleneck, [1024, False]], # 11 | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 6], 1, Concat, [1]], # cat backbone P4 | |||
[-1, 1, Conv, [512, 1, 1]], | |||
[-1, 3, Bottleneck, [512, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 4], 1, Concat, [1]], # cat backbone P3 | |||
[-1, 1, Conv, [256, 1, 1]], | |||
[-1, 3, Bottleneck, [256, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) | |||
[[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) | |||
] | |||
@@ -0,0 +1,46 @@ | |||
# parameters | |||
nc: 80 # number of classes | |||
depth_multiple: 0.33 # model depth multiple | |||
width_multiple: 0.50 # layer channel multiple | |||
# anchors | |||
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 backbone | |||
backbone: | |||
# [from, number, module, args] | |||
[[-1, 1, Focus, [64, 3]], # 1-P1/2 | |||
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4 | |||
[-1, 3, BottleneckCSP, [128]], | |||
[-1, 1, Conv, [256, 3, 2]], # 4-P3/8 | |||
[-1, 9, BottleneckCSP, [256]], | |||
[-1, 1, Conv, [512, 3, 2]], # 6-P4/16 | |||
[-1, 9, BottleneckCSP, [512]], | |||
[-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 | |||
[-1, 1, SPP, [1024, [5, 9, 13]]], | |||
[-1, 6, BottleneckCSP, [1024]], # 10 | |||
] | |||
# yolov5 head | |||
head: | |||
[[-1, 3, Bottleneck, [1024, False]], # 11 | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 6], 1, Concat, [1]], # cat backbone P4 | |||
[-1, 1, Conv, [512, 1, 1]], | |||
[-1, 3, Bottleneck, [512, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 4], 1, Concat, [1]], # cat backbone P3 | |||
[-1, 1, Conv, [256, 1, 1]], | |||
[-1, 3, Bottleneck, [256, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) | |||
[[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) | |||
] | |||
@@ -0,0 +1,46 @@ | |||
# parameters | |||
nc: 80 # number of classes | |||
depth_multiple: 0.33 # model depth multiple | |||
width_multiple: 0.50 # layer channel multiple | |||
# anchors | |||
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 backbone | |||
backbone: | |||
# [from, number, module, args] | |||
[[-1, 1, Focus, [64, 3]], # 1-P1/2 | |||
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4 | |||
[-1, 3, BottleneckCSP, [128]], | |||
[-1, 1, Conv, [256, 3, 2]], # 4-P3/8 | |||
[-1, 9, BottleneckCSP, [256]], | |||
[-1, 1, Conv, [512, 3, 2]], # 6-P4/16 | |||
[-1, 9, BottleneckCSP, [512]], | |||
[-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 | |||
[-1, 1, SPP, [1024, [5, 9, 13]]], | |||
[-1, 6, BottleneckCSP, [1024]], # 10 | |||
] | |||
# yolov5 head | |||
head: | |||
[[-1, 3, BottleneckCSPF, [1024]], # 11 | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 6], 1, Concat, [1]], # cat backbone P4 | |||
[-1, 1, Conv, [512, 1, 1]], | |||
[-1, 3, BottleneckCSPF, [512]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 4], 1, Concat, [1]], # cat backbone P3 | |||
[-1, 1, Conv, [256, 1, 1]], | |||
[-1, 3, BottleneckCSPF, [256]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) | |||
[[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) | |||
] | |||
@@ -0,0 +1,45 @@ | |||
# parameters | |||
nc: 80 # number of classes | |||
depth_multiple: 1.33 # model depth multiple | |||
width_multiple: 1.25 # layer channel multiple | |||
# anchors | |||
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 backbone | |||
backbone: | |||
# [from, number, module, args] | |||
[[-1, 1, Focus, [64, 3]], # 1-P1/2 | |||
[-1, 1, Conv, [128, 3, 2]], # 2-P2/4 | |||
[-1, 3, Bottleneck, [128]], | |||
[-1, 1, Conv, [256, 3, 2]], # 4-P3/8 | |||
[-1, 9, Bottleneck, [256]], | |||
[-1, 1, Conv, [512, 3, 2]], # 6-P4/16 | |||
[-1, 9, Bottleneck, [512]], | |||
[-1, 1, Conv, [1024, 3, 2]], # 8-P5/32 | |||
[-1, 1, SPP, [1024, [5, 9, 13]]], | |||
[-1, 3, Bottleneck, [1024]], # 10 | |||
] | |||
# yolov5 head | |||
head: | |||
[[-1, 3, Bottleneck, [1024]], # 11 | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 12 (P5/32-large) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 6], 1, Concat, [1]], # cat backbone P4 | |||
[-1, 1, Conv, [512, 1, 1]], | |||
[-1, 3, Bottleneck, [512, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 17 (P4/16-medium) | |||
[-2, 1, nn.Upsample, [None, 2, 'nearest']], | |||
[[-1, 4], 1, Concat, [1]], # cat backbone P3 | |||
[-1, 1, Conv, [256, 1, 1]], | |||
[-1, 3, Bottleneck, [256, False]], | |||
[-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]], # 22 (P3/8-small) | |||
[[], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) | |||
] |
@@ -0,0 +1,22 @@ | |||
# pip install -U -r requirements.txt | |||
numpy==1.17 | |||
opencv-python | |||
torch >= 1.5 | |||
matplotlib | |||
pycocotools | |||
tqdm | |||
pillow | |||
tensorboard | |||
pyyaml | |||
# Nvidia Apex (optional) for mixed precision training -------------------------- | |||
# git clone https://github.com/NVIDIA/apex && cd apex && pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" . --user && cd .. && rm -rf apex | |||
# Conda commands (in place of pip) --------------------------------------------- | |||
# conda update -yn base -c defaults conda | |||
# conda install -yc anaconda numpy opencv matplotlib tqdm pillow ipython | |||
# conda install -yc conda-forge scikit-image pycocotools tensorboard | |||
# conda install -yc spyder-ide spyder-line-profiler | |||
# conda install -yc pytorch pytorch torchvision | |||
# conda install -yc conda-forge protobuf numpy && pip install onnx # https://github.com/onnx/onnx#linux-and-macos |
@@ -0,0 +1,268 @@ | |||
import argparse | |||
import json | |||
import yaml | |||
from torch.utils.data import DataLoader | |||
from utils.datasets import * | |||
from utils.utils import * | |||
def test(data, | |||
weights=None, | |||
batch_size=16, | |||
imgsz=640, | |||
conf_thres=0.001, | |||
iou_thres=0.6, # for nms | |||
save_json=False, | |||
single_cls=False, | |||
augment=False, | |||
model=None, | |||
dataloader=None, | |||
multi_label=True, | |||
verbose=False): # 0 fast, 1 accurate | |||
# Initialize/load model and set device | |||
if model is None: | |||
device = torch_utils.select_device(opt.device, batch_size=batch_size) | |||
# Remove previous | |||
for f in glob.glob('test_batch*.jpg'): | |||
os.remove(f) | |||
# Load model | |||
google_utils.attempt_download(weights) | |||
model = torch.load(weights, map_location=device)['model'] | |||
torch_utils.model_info(model) | |||
# Fuse | |||
# model.fuse() | |||
model.to(device) | |||
if device.type != 'cpu' and torch.cuda.device_count() > 1: | |||
model = nn.DataParallel(model) | |||
training = False | |||
else: # called by train.py | |||
device = next(model.parameters()).device # get model device | |||
training = True | |||
# Configure run | |||
with open(data) as f: | |||
data = yaml.load(f, Loader=yaml.FullLoader) # model dict | |||
nc = 1 if single_cls else int(data['nc']) # number of classes | |||
iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for mAP@0.5:0.95 | |||
# iouv = iouv[0].view(1) # comment for mAP@0.5:0.95 | |||
niou = iouv.numel() | |||
# Dataloader | |||
if dataloader is None: | |||
path = data['test'] if opt.task == 'test' else data['val'] # path to val/test images | |||
dataset = LoadImagesAndLabels(path, imgsz, batch_size, rect=True, single_cls=opt.single_cls) | |||
batch_size = min(batch_size, len(dataset)) | |||
dataloader = DataLoader(dataset, | |||
batch_size=batch_size, | |||
num_workers=min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8]), | |||
pin_memory=True, | |||
collate_fn=dataset.collate_fn) | |||
seen = 0 | |||
model.eval() | |||
_ = model(torch.zeros((1, 3, imgsz, imgsz), device=device)) if device.type != 'cpu' else None # run once | |||
coco91class = coco80_to_coco91_class() | |||
s = ('%20s' + '%12s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', 'mAP@.5', 'mAP@.5:.95') | |||
p, r, f1, mp, mr, map50, map, t0, t1 = 0., 0., 0., 0., 0., 0., 0., 0., 0. | |||
loss = torch.zeros(3, device=device) | |||
jdict, stats, ap, ap_class = [], [], [], [] | |||
for batch_i, (imgs, targets, paths, shapes) in enumerate(tqdm(dataloader, desc=s)): | |||
imgs = imgs.to(device).float() / 255.0 # uint8 to float32, 0 - 255 to 0.0 - 1.0 | |||
targets = targets.to(device) | |||
nb, _, height, width = imgs.shape # batch size, channels, height, width | |||
whwh = torch.Tensor([width, height, width, height]).to(device) | |||
# Disable gradients | |||
with torch.no_grad(): | |||
# Run model | |||
t = torch_utils.time_synchronized() | |||
inf_out, train_out = model(imgs, augment=augment) # inference and training outputs | |||
t0 += torch_utils.time_synchronized() - t | |||
# Compute loss | |||
if training: # if model has loss hyperparameters | |||
loss += compute_loss(train_out, targets, model)[1][:3] # GIoU, obj, cls | |||
# Run NMS | |||
t = torch_utils.time_synchronized() | |||
output = non_max_suppression(inf_out, conf_thres=conf_thres, iou_thres=iou_thres, multi_label=multi_label) | |||
t1 += torch_utils.time_synchronized() - t | |||
# Statistics per image | |||
for si, pred in enumerate(output): | |||
labels = targets[targets[:, 0] == si, 1:] | |||
nl = len(labels) | |||
tcls = labels[:, 0].tolist() if nl else [] # target class | |||
seen += 1 | |||
if pred is None: | |||
if nl: | |||
stats.append((torch.zeros(0, niou, dtype=torch.bool), torch.Tensor(), torch.Tensor(), tcls)) | |||
continue | |||
# Append to text file | |||
# with open('test.txt', 'a') as file: | |||
# [file.write('%11.5g' * 7 % tuple(x) + '\n') for x in pred] | |||
# Clip boxes to image bounds | |||
clip_coords(pred, (height, width)) | |||
# Append to pycocotools JSON dictionary | |||
if save_json: | |||
# [{"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}, ... | |||
image_id = int(Path(paths[si]).stem.split('_')[-1]) | |||
box = pred[:, :4].clone() # xyxy | |||
scale_coords(imgs[si].shape[1:], box, shapes[si][0], shapes[si][1]) # to original shape | |||
box = xyxy2xywh(box) # xywh | |||
box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner | |||
for p, b in zip(pred.tolist(), box.tolist()): | |||
jdict.append({'image_id': image_id, | |||
'category_id': coco91class[int(p[5])], | |||
'bbox': [round(x, 3) for x in b], | |||
'score': round(p[4], 5)}) | |||
# Assign all predictions as incorrect | |||
correct = torch.zeros(pred.shape[0], niou, dtype=torch.bool, device=device) | |||
if nl: | |||
detected = [] # target indices | |||
tcls_tensor = labels[:, 0] | |||
# target boxes | |||
tbox = xywh2xyxy(labels[:, 1:5]) * whwh | |||
# Per target class | |||
for cls in torch.unique(tcls_tensor): | |||
ti = (cls == tcls_tensor).nonzero().view(-1) # prediction indices | |||
pi = (cls == pred[:, 5]).nonzero().view(-1) # target indices | |||
# Search for detections | |||
if pi.shape[0]: | |||
# Prediction to target ious | |||
ious, i = box_iou(pred[pi, :4], tbox[ti]).max(1) # best ious, indices | |||
# Append detections | |||
for j in (ious > iouv[0]).nonzero(): | |||
d = ti[i[j]] # detected target | |||
if d not in detected: | |||
detected.append(d) | |||
correct[pi[j]] = ious[j] > iouv # iou_thres is 1xn | |||
if len(detected) == nl: # all targets already located in image | |||
break | |||
# Append statistics (correct, conf, pcls, tcls) | |||
stats.append((correct.cpu(), pred[:, 4].cpu(), pred[:, 5].cpu(), tcls)) | |||
# Plot images | |||
if batch_i < 1: | |||
f = 'test_batch%g_gt.jpg' % batch_i # filename | |||
plot_images(imgs, targets, paths, f, model.names) # ground truth | |||
f = 'test_batch%g_pred.jpg' % batch_i | |||
plot_images(imgs, output_to_target(output, width, height), paths, f, model.names) # predictions | |||
# Compute statistics | |||
stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy | |||
if len(stats): | |||
p, r, ap, f1, ap_class = ap_per_class(*stats) | |||
p, r, ap50, ap = p[:, 0], r[:, 0], ap[:, 0], ap.mean(1) # [P, R, AP@0.5, AP@0.5:0.95] | |||
mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean() | |||
nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class | |||
else: | |||
nt = torch.zeros(1) | |||
# Print results | |||
pf = '%20s' + '%12.3g' * 6 # print format | |||
print(pf % ('all', seen, nt.sum(), mp, mr, map50, map)) | |||
# Print results per class | |||
if verbose and nc > 1 and len(stats): | |||
for i, c in enumerate(ap_class): | |||
print(pf % (model.names[c], seen, nt[c], p[i], r[i], ap50[i], ap[i])) | |||
# Print speeds | |||
t = tuple(x / seen * 1E3 for x in (t0, t1, t0 + t1)) + (imgsz, imgsz, batch_size) # tuple | |||
if not training: | |||
print('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t) | |||
# Save JSON | |||
if save_json and map50 and len(jdict): | |||
imgIds = [int(Path(x).stem.split('_')[-1]) for x in dataloader.dataset.img_files] | |||
f = 'detections_val2017_%s_results.json' % \ | |||
(weights.split(os.sep)[-1].replace('.pt', '') if weights else '') # filename | |||
print('\nCOCO mAP with pycocotools... saving %s...' % f) | |||
with open(f, 'w') as file: | |||
json.dump(jdict, file) | |||
try: | |||
from pycocotools.coco import COCO | |||
from pycocotools.cocoeval import COCOeval | |||
# https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb | |||
cocoGt = COCO(glob.glob('../coco/annotations/instances_val*.json')[0]) # initialize COCO ground truth api | |||
cocoDt = cocoGt.loadRes(f) # initialize COCO pred api | |||
cocoEval = COCOeval(cocoGt, cocoDt, 'bbox') | |||
cocoEval.params.imgIds = imgIds # [:32] # only evaluate these images | |||
cocoEval.evaluate() | |||
cocoEval.accumulate() | |||
cocoEval.summarize() | |||
map, map50 = cocoEval.stats[:2] # update to pycocotools results (mAP@0.5:0.95, mAP@0.5) | |||
except: | |||
print('WARNING: pycocotools must be installed with numpy==1.17 to run correctly. ' | |||
'See https://github.com/cocodataset/cocoapi/issues/356') | |||
# Return results | |||
maps = np.zeros(nc) + map | |||
for i, c in enumerate(ap_class): | |||
maps[c] = ap[i] | |||
return (mp, mr, map50, map, *(loss.cpu() / len(dataloader)).tolist()), maps, t | |||
if __name__ == '__main__': | |||
parser = argparse.ArgumentParser(prog='test.py') | |||
parser.add_argument('--weights', type=str, default='weights/yolov5s.pt', help='model.pt path') | |||
parser.add_argument('--data', type=str, default='data/coco.yaml', help='*.data path') | |||
parser.add_argument('--batch-size', type=int, default=16, help='size of each image batch') | |||
parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)') | |||
parser.add_argument('--conf-thres', type=float, default=0.001, help='object confidence threshold') | |||
parser.add_argument('--iou-thres', type=float, default=0.65, help='IOU threshold for NMS') | |||
parser.add_argument('--save-json', action='store_true', help='save a cocoapi-compatible JSON results file') | |||
parser.add_argument('--task', default='val', help="'val', 'test', 'study'") | |||
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') | |||
parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset') | |||
parser.add_argument('--augment', action='store_true', help='augmented inference') | |||
parser.add_argument('--verbose', action='store_true', help='report mAP by class') | |||
opt = parser.parse_args() | |||
opt.save_json = opt.save_json or opt.data.endswith(os.sep + 'coco.yaml') | |||
opt.data = glob.glob('./**/' + opt.data, recursive=True)[0] # find file | |||
print(opt) | |||
# task = 'val', 'test', 'study' | |||
if opt.task == 'val': # (default) run normally | |||
test(opt.data, | |||
opt.weights, | |||
opt.batch_size, | |||
opt.img_size, | |||
opt.conf_thres, | |||
opt.iou_thres, | |||
opt.save_json, | |||
opt.single_cls, | |||
opt.augment) | |||
elif opt.task == 'study': # run over a range of settings and save/plot | |||
for weights in ['yolov5s.pt', 'yolov5m.pt', 'yolovl.p5', 'yolov5x.pt', 'yolov3-spp.pt']: | |||
f = 'study_%s_%s.txt' % (Path(opt.data).stem, Path(weights).stem) # filename to save to | |||
x = list(range(256, 1024, 32)) # x axis | |||
y = [] # y axis | |||
for i in x: # img-size | |||
print('\nRunning %s point %s...' % (f, i)) | |||
r, _, t = test(opt.data, weights, opt.batch_size, i, opt.conf_thres, opt.iou_thres, opt.save_json) | |||
y.append(r + t) # results and times | |||
np.savetxt(f, y, fmt='%10.4g') # save | |||
plot_study_txt(f, x) # plot |
@@ -0,0 +1,443 @@ | |||
import argparse | |||
import torch.distributed as dist | |||
import torch.nn.functional as F | |||
import torch.optim as optim | |||
import torch.optim.lr_scheduler as lr_scheduler | |||
import yaml | |||
from torch.utils.tensorboard import SummaryWriter | |||
import test # import test.py to get mAP after each epoch | |||
from models.yolo import Model | |||
from utils.datasets import * | |||
from utils.utils import * | |||
mixed_precision = True | |||
try: # Mixed precision training https://github.com/NVIDIA/apex | |||
from apex import amp | |||
except: | |||
print('Apex recommended for faster mixed precision training: https://github.com/NVIDIA/apex') | |||
mixed_precision = False # not installed | |||
wdir = 'weights' + os.sep # weights dir | |||
last = wdir + 'last.pt' | |||
best = wdir + 'best.pt' | |||
results_file = 'results.txt' | |||
# Hyperparameters | |||
hyp = {'lr0': 0.01, # initial learning rate (SGD=1E-2, Adam=1E-3) | |||
'momentum': 0.937, # SGD momentum | |||
'weight_decay': 5e-4, # optimizer weight decay | |||
'giou': 0.05, # giou loss gain | |||
'cls': 0.58, # cls loss gain | |||
'cls_pw': 1.0, # cls BCELoss positive_weight | |||
'obj': 1.0, # obj loss gain (*=img_size/320 if img_size != 320) | |||
'obj_pw': 1.0, # obj BCELoss positive_weight | |||
'iou_t': 0.20, # iou training threshold | |||
'anchor_t': 4.0, # anchor-multiple threshold | |||
'fl_gamma': 0.0, # focal loss gamma (efficientDet default is gamma=1.5) | |||
'hsv_h': 0.014, # image HSV-Hue augmentation (fraction) | |||
'hsv_s': 0.68, # image HSV-Saturation augmentation (fraction) | |||
'hsv_v': 0.36, # image HSV-Value augmentation (fraction) | |||
'degrees': 0.0, # image rotation (+/- deg) | |||
'translate': 0.0, # image translation (+/- fraction) | |||
'scale': 0.5, # image scale (+/- gain) | |||
'shear': 0.0} # image shear (+/- deg) | |||
print(hyp) | |||
# Overwrite hyp with hyp*.txt (optional) | |||
f = glob.glob('hyp*.txt') | |||
if f: | |||
print('Using %s' % f[0]) | |||
for k, v in zip(hyp.keys(), np.loadtxt(f[0])): | |||
hyp[k] = v | |||
# Print focal loss if gamma > 0 | |||
if hyp['fl_gamma']: | |||
print('Using FocalLoss(gamma=%g)' % hyp['fl_gamma']) | |||
def train(hyp): | |||
epochs = opt.epochs # 300 | |||
batch_size = opt.batch_size # 64 | |||
weights = opt.weights # initial training weights | |||
# Configure | |||
init_seeds() | |||
with open(opt.data) as f: | |||
data_dict = yaml.load(f, Loader=yaml.FullLoader) # model dict | |||
train_path = data_dict['train'] | |||
test_path = data_dict['val'] | |||
nc = 1 if opt.single_cls else int(data_dict['nc']) # number of classes | |||
# Remove previous results | |||
for f in glob.glob('*_batch*.jpg') + glob.glob(results_file): | |||
os.remove(f) | |||
# Create model | |||
model = Model(opt.cfg).to(device) | |||
# Image sizes | |||
gs = int(max(model.stride)) # grid size (max stride) | |||
if any(x % gs != 0 for x in opt.img_size): | |||
print('WARNING: --img-size %g,%g must be multiple of %s max stride %g' % (*opt.img_size, opt.cfg, gs)) | |||
imgsz, imgsz_test = [make_divisible(x, gs) for x in opt.img_size] # image sizes (train, test) | |||
# Optimizer | |||
nbs = 64 # nominal batch size | |||
accumulate = max(round(nbs / batch_size), 1) # accumulate loss before optimizing | |||
hyp['weight_decay'] *= batch_size * accumulate / nbs # scale weight_decay | |||
pg0, pg1, pg2 = [], [], [] # optimizer parameter groups | |||
for k, v in model.named_parameters(): | |||
if v.requires_grad: | |||
if '.bias' in k: | |||
pg2.append(v) # biases | |||
elif '.weight' in k and '.bn' not in k: | |||
pg1.append(v) # apply weight decay | |||
else: | |||
pg0.append(v) # all else | |||
optimizer = optim.Adam(pg0, lr=hyp['lr0']) if opt.adam else \ | |||
optim.SGD(pg0, lr=hyp['lr0'], momentum=hyp['momentum'], nesterov=True) | |||
optimizer.add_param_group({'params': pg1, 'weight_decay': hyp['weight_decay']}) # add pg1 with weight_decay | |||
optimizer.add_param_group({'params': pg2}) # add pg2 (biases) | |||
print('Optimizer groups: %g .bias, %g conv.weight, %g other' % (len(pg2), len(pg1), len(pg0))) | |||
del pg0, pg1, pg2 | |||
# Load Model | |||
google_utils.attempt_download(weights) | |||
start_epoch, best_fitness = 0, 0.0 | |||
if weights.endswith('.pt'): # pytorch format | |||
chkpt = torch.load(weights, map_location=device) | |||
# load model | |||
try: | |||
chkpt['model'] = \ | |||
{k: v for k, v in chkpt['model'].state_dict().items() if model.state_dict()[k].numel() == v.numel()} | |||
model.load_state_dict(chkpt['model'], strict=False) | |||
except KeyError as e: | |||
s = "%s is not compatible with %s. Specify --weights '' or specify a --cfg compatible with %s." \ | |||
% (opt.weights, opt.cfg, opt.weights) | |||
raise KeyError(s) from e | |||
# load optimizer | |||
if chkpt['optimizer'] is not None: | |||
optimizer.load_state_dict(chkpt['optimizer']) | |||
best_fitness = chkpt['best_fitness'] | |||
# load results | |||
if chkpt.get('training_results') is not None: | |||
with open(results_file, 'w') as file: | |||
file.write(chkpt['training_results']) # write results.txt | |||
start_epoch = chkpt['epoch'] + 1 | |||
del chkpt | |||
# Mixed precision training https://github.com/NVIDIA/apex | |||
if mixed_precision: | |||
model, optimizer = amp.initialize(model, optimizer, opt_level='O1', verbosity=0) | |||
# Scheduler https://arxiv.org/pdf/1812.01187.pdf | |||
lf = lambda x: (((1 + math.cos(x * math.pi / epochs)) / 2) ** 1.0) * 0.9 + 0.1 # cosine | |||
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf) | |||
scheduler.last_epoch = start_epoch - 1 # do not move | |||
# https://discuss.pytorch.org/t/a-problem-occured-when-resuming-an-optimizer/28822 | |||
# plot_lr_scheduler(optimizer, scheduler, epochs) | |||
# Initialize distributed training | |||
if device.type != 'cpu' and torch.cuda.device_count() > 1 and torch.distributed.is_available(): | |||
dist.init_process_group(backend='nccl', # distributed backend | |||
init_method='tcp://127.0.0.1:9999', # init method | |||
world_size=1, # number of nodes | |||
rank=0) # node rank | |||
model = torch.nn.parallel.DistributedDataParallel(model) | |||
# Dataset | |||
dataset = LoadImagesAndLabels(train_path, imgsz, batch_size, | |||
augment=True, | |||
hyp=hyp, # augmentation hyperparameters | |||
rect=opt.rect, # rectangular training | |||
cache_images=opt.cache_images, | |||
single_cls=opt.single_cls) | |||
mlc = np.concatenate(dataset.labels, 0)[:, 0].max() # max label class | |||
assert mlc < nc, 'Label class %g exceeds nc=%g in %s. Correct your labels or your model.' % (mlc, nc, opt.cfg) | |||
# Dataloader | |||
batch_size = min(batch_size, len(dataset)) | |||
nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8]) # number of workers | |||
dataloader = torch.utils.data.DataLoader(dataset, | |||
batch_size=batch_size, | |||
num_workers=nw, | |||
shuffle=not opt.rect, # Shuffle=True unless rectangular training is used | |||
pin_memory=True, | |||
collate_fn=dataset.collate_fn) | |||
# Testloader | |||
testloader = torch.utils.data.DataLoader(LoadImagesAndLabels(test_path, imgsz_test, batch_size, | |||
hyp=hyp, | |||
rect=True, | |||
cache_images=opt.cache_images, | |||
single_cls=opt.single_cls), | |||
batch_size=batch_size, | |||
num_workers=nw, | |||
pin_memory=True, | |||
collate_fn=dataset.collate_fn) | |||
# Model parameters | |||
hyp['cls'] *= nc / 80. # scale coco-tuned hyp['cls'] to current dataset | |||
model.nc = nc # attach number of classes to model | |||
model.hyp = hyp # attach hyperparameters to model | |||
model.gr = 1.0 # giou loss ratio (obj_loss = 1.0 or giou) | |||
model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) # attach class weights | |||
model.names = data_dict['names'] | |||
# class frequency | |||
labels = np.concatenate(dataset.labels, 0) | |||
c = torch.tensor(labels[:, 0]) # classes | |||
# cf = torch.bincount(c.long(), minlength=nc) + 1. | |||
# model._initialize_biases(cf.to(device)) | |||
plot_labels(labels) | |||
tb_writer.add_histogram('classes', c, 0) | |||
# Exponential moving average | |||
ema = torch_utils.ModelEMA(model) | |||
# Start training | |||
t0 = time.time() | |||
nb = len(dataloader) # number of batches | |||
n_burn = max(3 * nb, 1e3) # burn-in iterations, max(3 epochs, 1k iterations) | |||
maps = np.zeros(nc) # mAP per class | |||
results = (0, 0, 0, 0, 0, 0, 0) # 'P', 'R', 'mAP', 'F1', 'val GIoU', 'val Objectness', 'val Classification' | |||
print('Image sizes %g train, %g test' % (imgsz, imgsz_test)) | |||
print('Using %g dataloader workers' % nw) | |||
print('Starting training for %g epochs...' % epochs) | |||
# torch.autograd.set_detect_anomaly(True) | |||
for epoch in range(start_epoch, epochs): # epoch ------------------------------------------------------------------ | |||
model.train() | |||
# Update image weights (optional) | |||
if dataset.image_weights: | |||
w = model.class_weights.cpu().numpy() * (1 - maps) ** 2 # class weights | |||
image_weights = labels_to_image_weights(dataset.labels, nc=nc, class_weights=w) | |||
dataset.indices = random.choices(range(dataset.n), weights=image_weights, k=dataset.n) # rand weighted idx | |||
mloss = torch.zeros(4, device=device) # mean losses | |||
print(('\n' + '%10s' * 8) % ('Epoch', 'gpu_mem', 'GIoU', 'obj', 'cls', 'total', 'targets', 'img_size')) | |||
pbar = tqdm(enumerate(dataloader), total=nb) # progress bar | |||
for i, (imgs, targets, paths, _) in pbar: # batch ------------------------------------------------------------- | |||
ni = i + nb * epoch # number integrated batches (since train start) | |||
imgs = imgs.to(device).float() / 255.0 # uint8 to float32, 0 - 255 to 0.0 - 1.0 | |||
# Burn-in | |||
if ni <= n_burn: | |||
xi = [0, n_burn] # x interp | |||
# model.gr = np.interp(ni, xi, [0.0, 1.0]) # giou loss ratio (obj_loss = 1.0 or giou) | |||
accumulate = max(1, np.interp(ni, xi, [1, nbs / batch_size]).round()) | |||
for j, x in enumerate(optimizer.param_groups): | |||
# bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0 | |||
x['lr'] = np.interp(ni, xi, [0.1 if j == 2 else 0.0, x['initial_lr'] * lf(epoch)]) | |||
if 'momentum' in x: | |||
x['momentum'] = np.interp(ni, xi, [0.9, hyp['momentum']]) | |||
# Multi-scale | |||
if True: | |||
imgsz = random.randrange(640, 640 + gs) // gs * gs | |||
sf = imgsz / max(imgs.shape[2:]) # scale factor | |||
if sf != 1: | |||
ns = [math.ceil(x * sf / gs) * gs for x in imgs.shape[2:]] # new shape (stretched to gs-multiple) | |||
imgs = F.interpolate(imgs, size=ns, mode='bilinear', align_corners=False) | |||
# Forward | |||
pred = model(imgs) | |||
# Loss | |||
loss, loss_items = compute_loss(pred, targets.to(device), model) | |||
if not torch.isfinite(loss): | |||
print('WARNING: non-finite loss, ending training ', loss_items) | |||
return results | |||
# Backward | |||
if mixed_precision: | |||
with amp.scale_loss(loss, optimizer) as scaled_loss: | |||
scaled_loss.backward() | |||
else: | |||
loss.backward() | |||
# Optimize | |||
if ni % accumulate == 0: | |||
optimizer.step() | |||
optimizer.zero_grad() | |||
ema.update(model) | |||
mloss = (mloss * i + loss_items) / (i + 1) # update mean losses | |||
mem = '%.3gG' % (torch.cuda.memory_cached() / 1E9 if torch.cuda.is_available() else 0) # (GB) | |||
s = ('%10s' * 2 + '%10.4g' * 6) % ('%g/%g' % (epoch, epochs - 1), mem, *mloss, targets.shape[0], imgsz) | |||
pbar.set_description(s) | |||
# Plot | |||
if ni < 3: | |||
f = 'train_batch%g.jpg' % i # filename | |||
res = plot_images(images=imgs, targets=targets, paths=paths, fname=f) | |||
if tb_writer: | |||
tb_writer.add_image(f, res, dataformats='HWC', global_step=epoch) | |||
# tb_writer.add_graph(model, imgs) # add model to tensorboard | |||
# end batch ------------------------------------------------------------------------------------------------ | |||
# Scheduler | |||
scheduler.step() | |||
# mAP | |||
ema.update_attr(model) | |||
final_epoch = epoch + 1 == epochs | |||
if not opt.notest or final_epoch: # Calculate mAP | |||
results, maps, times = test.test(opt.data, | |||
batch_size=batch_size, | |||
imgsz=imgsz_test, | |||
save_json=final_epoch and opt.data.endswith(os.sep + 'coco.yaml'), | |||
model=ema.ema, | |||
single_cls=opt.single_cls, | |||
dataloader=testloader, | |||
multi_label=ni > n_burn) | |||
# Write | |||
with open(results_file, 'a') as f: | |||
f.write(s + '%10.4g' * 7 % results + '\n') # P, R, mAP, F1, test_losses=(GIoU, obj, cls) | |||
if len(opt.name) and opt.bucket: | |||
os.system('gsutil cp results.txt gs://%s/results/results%s.txt' % (opt.bucket, opt.name)) | |||
# Tensorboard | |||
if tb_writer: | |||
tags = ['train/giou_loss', 'train/obj_loss', 'train/cls_loss', | |||
'metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/F1', | |||
'val/giou_loss', 'val/obj_loss', 'val/cls_loss'] | |||
for x, tag in zip(list(mloss[:-1]) + list(results), tags): | |||
tb_writer.add_scalar(tag, x, epoch) | |||
# Update best mAP | |||
fi = fitness(np.array(results).reshape(1, -1)) # fitness_i = weighted combination of [P, R, mAP, F1] | |||
if fi > best_fitness: | |||
best_fitness = fi | |||
# Save model | |||
save = (not opt.nosave) or (final_epoch and not opt.evolve) | |||
if save: | |||
with open(results_file, 'r') as f: # create checkpoint | |||
chkpt = {'epoch': epoch, | |||
'best_fitness': best_fitness, | |||
'training_results': f.read(), | |||
'model': ema.ema.module if hasattr(model, 'module') else ema.ema, | |||
'optimizer': None if final_epoch else optimizer.state_dict()} | |||
# Save last, best and delete | |||
torch.save(chkpt, last) | |||
if (best_fitness == fi) and not final_epoch: | |||
torch.save(chkpt, best) | |||
del chkpt | |||
# end epoch ---------------------------------------------------------------------------------------------------- | |||
# end training | |||
n = opt.name | |||
if len(n): | |||
n = '_' + n if not n.isnumeric() else n | |||
fresults, flast, fbest = 'results%s.txt' % n, wdir + 'last%s.pt' % n, wdir + 'best%s.pt' % n | |||
for f1, f2 in zip([wdir + 'last.pt', wdir + 'best.pt', 'results.txt'], [flast, fbest, fresults]): | |||
if os.path.exists(f1): | |||
os.rename(f1, f2) # rename | |||
ispt = f2.endswith('.pt') # is *.pt | |||
strip_optimizer(f2) if ispt else None # strip optimizer | |||
os.system('gsutil cp %s gs://%s/weights' % (f2, opt.bucket)) if opt.bucket and ispt else None # upload | |||
if not opt.evolve: | |||
plot_results() # save as results.png | |||
print('%g epochs completed in %.3f hours.\n' % (epoch - start_epoch + 1, (time.time() - t0) / 3600)) | |||
dist.destroy_process_group() if torch.cuda.device_count() > 1 else None | |||
torch.cuda.empty_cache() | |||
return results | |||
if __name__ == '__main__': | |||
parser = argparse.ArgumentParser() | |||
parser.add_argument('--epochs', type=int, default=300) | |||
parser.add_argument('--batch-size', type=int, default=16) | |||
parser.add_argument('--cfg', type=str, default='models/yolov5s.yaml', help='*.cfg path') | |||
parser.add_argument('--data', type=str, default='data/coco128.yaml', help='*.data path') | |||
parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='train,test sizes') | |||
parser.add_argument('--rect', action='store_true', help='rectangular training') | |||
parser.add_argument('--resume', action='store_true', help='resume training from last.pt') | |||
parser.add_argument('--nosave', action='store_true', help='only save final checkpoint') | |||
parser.add_argument('--notest', action='store_true', help='only test final epoch') | |||
parser.add_argument('--evolve', action='store_true', help='evolve hyperparameters') | |||
parser.add_argument('--bucket', type=str, default='', help='gsutil bucket') | |||
parser.add_argument('--cache-images', action='store_true', help='cache images for faster training') | |||
parser.add_argument('--weights', type=str, default='', help='initial weights path') | |||
parser.add_argument('--name', default='', help='renames results.txt to results_name.txt if supplied') | |||
parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') | |||
parser.add_argument('--adam', action='store_true', help='use adam optimizer') | |||
parser.add_argument('--single-cls', action='store_true', help='train as single-class dataset') | |||
opt = parser.parse_args() | |||
opt.weights = last if opt.resume else opt.weights | |||
opt.cfg = glob.glob('./**/' + opt.cfg, recursive=True)[0] # find file | |||
opt.data = glob.glob('./**/' + opt.data, recursive=True)[0] # find file | |||
print(opt) | |||
opt.img_size.extend([opt.img_size[-1]] * (2 - len(opt.img_size))) # extend to 2 sizes (train, test) | |||
device = torch_utils.select_device(opt.device, apex=mixed_precision, batch_size=opt.batch_size) | |||
# check_git_status() | |||
if device.type == 'cpu': | |||
mixed_precision = False | |||
# Train | |||
if not opt.evolve: | |||
tb_writer = SummaryWriter(comment=opt.name) | |||
print('Start Tensorboard with "tensorboard --logdir=runs", view at http://localhost:6006/') | |||
train(hyp) | |||
# Evolve hyperparameters (optional) | |||
else: | |||
tb_writer = None | |||
opt.notest, opt.nosave = True, True # only test/save final epoch | |||
if opt.bucket: | |||
os.system('gsutil cp gs://%s/evolve.txt .' % opt.bucket) # download evolve.txt if exists | |||
for _ in range(10): # generations to evolve | |||
if os.path.exists('evolve.txt'): # if evolve.txt exists: select best hyps and mutate | |||
# Select parent(s) | |||
parent = 'single' # parent selection method: 'single' or 'weighted' | |||
x = np.loadtxt('evolve.txt', ndmin=2) | |||
n = min(5, len(x)) # number of previous results to consider | |||
x = x[np.argsort(-fitness(x))][:n] # top n mutations | |||
w = fitness(x) - fitness(x).min() # weights | |||
if parent == 'single' or len(x) == 1: | |||
# x = x[random.randint(0, n - 1)] # random selection | |||
x = x[random.choices(range(n), weights=w)[0]] # weighted selection | |||
elif parent == 'weighted': | |||
x = (x * w.reshape(n, 1)).sum(0) / w.sum() # weighted combination | |||
# Mutate | |||
mp, s = 0.9, 0.2 # mutation probability, sigma | |||
npr = np.random | |||
npr.seed(int(time.time())) | |||
g = np.array([1, 1, 1, 1, 1, 1, 1, 0, .1, 1, 0, 1, 1, 1, 1, 1, 1, 1]) # gains | |||
ng = len(g) | |||
v = np.ones(ng) | |||
while all(v == 1): # mutate until a change occurs (prevent duplicates) | |||
v = (g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1).clip(0.3, 3.0) | |||
for i, k in enumerate(hyp.keys()): # plt.hist(v.ravel(), 300) | |||
hyp[k] = x[i + 7] * v[i] # mutate | |||
# Clip to limits | |||
keys = ['lr0', 'iou_t', 'momentum', 'weight_decay', 'hsv_s', 'hsv_v', 'translate', 'scale', 'fl_gamma'] | |||
limits = [(1e-5, 1e-2), (0.00, 0.70), (0.60, 0.98), (0, 0.001), (0, .9), (0, .9), (0, .9), (0, .9), (0, 3)] | |||
for k, v in zip(keys, limits): | |||
hyp[k] = np.clip(hyp[k], v[0], v[1]) | |||
# Train mutation | |||
results = train(hyp.copy()) | |||
# Write mutation results | |||
print_mutation(hyp, results, opt.bucket) | |||
# Plot results | |||
# plot_evolution_results(hyp) |
@@ -0,0 +1,62 @@ | |||
import torch | |||
import torch.functional as F | |||
import torch.nn as nn | |||
# Swish ------------------------------------------------------------------------ | |||
class SwishImplementation(torch.autograd.Function): | |||
@staticmethod | |||
def forward(ctx, x): | |||
ctx.save_for_backward(x) | |||
return x * torch.sigmoid(x) | |||
@staticmethod | |||
def backward(ctx, grad_output): | |||
x = ctx.saved_tensors[0] | |||
sx = torch.sigmoid(x) | |||
return grad_output * (sx * (1 + x * (1 - sx))) | |||
class MemoryEfficientSwish(nn.Module): | |||
@staticmethod | |||
def forward(x): | |||
return SwishImplementation.apply(x) | |||
class HardSwish(nn.Module): # https://arxiv.org/pdf/1905.02244.pdf | |||
@staticmethod | |||
def forward(x): | |||
return x * F.hardtanh(x + 3, 0., 6., True) / 6. | |||
class Swish(nn.Module): | |||
@staticmethod | |||
def forward(x): | |||
return x * torch.sigmoid(x) | |||
# Mish ------------------------------------------------------------------------ | |||
class MishImplementation(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)) | |||
class MemoryEfficientMish(nn.Module): | |||
@staticmethod | |||
def forward(x): | |||
return MishImplementation.apply(x) | |||
class Mish(nn.Module): # https://github.com/digantamisra98/Mish | |||
@staticmethod | |||
def forward(x): | |||
return x * F.softplus(x).tanh() |
@@ -0,0 +1,842 @@ | |||
import glob | |||
import math | |||
import os | |||
import random | |||
import shutil | |||
import time | |||
from pathlib import Path | |||
from threading import Thread | |||
import cv2 | |||
import numpy as np | |||
import torch | |||
from PIL import Image, ExifTags | |||
from torch.utils.data import Dataset | |||
from tqdm import tqdm | |||
from utils.utils import xyxy2xywh, xywh2xyxy | |||
help_url = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data' | |||
img_formats = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.dng'] | |||
vid_formats = ['.mov', '.avi', '.mp4'] | |||
# Get orientation exif tag | |||
for orientation in ExifTags.TAGS.keys(): | |||
if ExifTags.TAGS[orientation] == 'Orientation': | |||
break | |||
def exif_size(img): | |||
# Returns exif-corrected PIL size | |||
s = img.size # (width, height) | |||
try: | |||
rotation = dict(img._getexif().items())[orientation] | |||
if rotation == 6: # rotation 270 | |||
s = (s[1], s[0]) | |||
elif rotation == 8: # rotation 90 | |||
s = (s[1], s[0]) | |||
except: | |||
pass | |||
return s | |||
class LoadImages: # for inference | |||
def __init__(self, path, img_size=416): | |||
path = str(Path(path)) # os-agnostic | |||
files = [] | |||
if os.path.isdir(path): | |||
files = sorted(glob.glob(os.path.join(path, '*.*'))) | |||
elif os.path.isfile(path): | |||
files = [path] | |||
images = [x for x in files if os.path.splitext(x)[-1].lower() in img_formats] | |||
videos = [x for x in files if os.path.splitext(x)[-1].lower() in vid_formats] | |||
nI, nV = len(images), len(videos) | |||
self.img_size = img_size | |||
self.files = images + videos | |||
self.nF = nI + nV # number of files | |||
self.video_flag = [False] * nI + [True] * nV | |||
self.mode = 'images' | |||
if any(videos): | |||
self.new_video(videos[0]) # new video | |||
else: | |||
self.cap = None | |||
assert self.nF > 0, 'No images or videos found in ' + path | |||
def __iter__(self): | |||
self.count = 0 | |||
return self | |||
def __next__(self): | |||
if self.count == self.nF: | |||
raise StopIteration | |||
path = self.files[self.count] | |||
if self.video_flag[self.count]: | |||
# Read video | |||
self.mode = 'video' | |||
ret_val, img0 = self.cap.read() | |||
if not ret_val: | |||
self.count += 1 | |||
self.cap.release() | |||
if self.count == self.nF: # last video | |||
raise StopIteration | |||
else: | |||
path = self.files[self.count] | |||
self.new_video(path) | |||
ret_val, img0 = self.cap.read() | |||
self.frame += 1 | |||
print('video %g/%g (%g/%g) %s: ' % (self.count + 1, self.nF, self.frame, self.nframes, path), end='') | |||
else: | |||
# Read image | |||
self.count += 1 | |||
img0 = cv2.imread(path) # BGR | |||
assert img0 is not None, 'Image Not Found ' + path | |||
print('image %g/%g %s: ' % (self.count, self.nF, path), end='') | |||
# Padded resize | |||
img = letterbox(img0, new_shape=self.img_size)[0] | |||
# Convert | |||
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 | |||
img = np.ascontiguousarray(img) | |||
# cv2.imwrite(path + '.letterbox.jpg', 255 * img.transpose((1, 2, 0))[:, :, ::-1]) # save letterbox image | |||
return path, img, img0, self.cap | |||
def new_video(self, path): | |||
self.frame = 0 | |||
self.cap = cv2.VideoCapture(path) | |||
self.nframes = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT)) | |||
def __len__(self): | |||
return self.nF # number of files | |||
class LoadWebcam: # for inference | |||
def __init__(self, pipe=0, img_size=416): | |||
self.img_size = img_size | |||
if pipe == '0': | |||
pipe = 0 # local camera | |||
# pipe = 'rtsp://192.168.1.64/1' # IP camera | |||
# pipe = 'rtsp://username:password@192.168.1.64/1' # IP camera with login | |||
# pipe = 'rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa' # IP traffic camera | |||
# pipe = 'http://wmccpinetop.axiscam.net/mjpg/video.mjpg' # IP golf camera | |||
# https://answers.opencv.org/question/215996/changing-gstreamer-pipeline-to-opencv-in-pythonsolved/ | |||
# pipe = '"rtspsrc location="rtsp://username:password@192.168.1.64/1" latency=10 ! appsink' # GStreamer | |||
# https://answers.opencv.org/question/200787/video-acceleration-gstremer-pipeline-in-videocapture/ | |||
# https://stackoverflow.com/questions/54095699/install-gstreamer-support-for-opencv-python-package # install help | |||
# pipe = "rtspsrc location=rtsp://root:root@192.168.0.91:554/axis-media/media.amp?videocodec=h264&resolution=3840x2160 protocols=GST_RTSP_LOWER_TRANS_TCP ! rtph264depay ! queue ! vaapih264dec ! videoconvert ! appsink" # GStreamer | |||
self.pipe = pipe | |||
self.cap = cv2.VideoCapture(pipe) # video capture object | |||
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size | |||
def __iter__(self): | |||
self.count = -1 | |||
return self | |||
def __next__(self): | |||
self.count += 1 | |||
if cv2.waitKey(1) == ord('q'): # q to quit | |||
self.cap.release() | |||
cv2.destroyAllWindows() | |||
raise StopIteration | |||
# Read frame | |||
if self.pipe == 0: # local camera | |||
ret_val, img0 = self.cap.read() | |||
img0 = cv2.flip(img0, 1) # flip left-right | |||
else: # IP camera | |||
n = 0 | |||
while True: | |||
n += 1 | |||
self.cap.grab() | |||
if n % 30 == 0: # skip frames | |||
ret_val, img0 = self.cap.retrieve() | |||
if ret_val: | |||
break | |||
assert ret_val, 'Camera Error %s' % self.pipe | |||
img_path = 'webcam.jpg' | |||
print('webcam %g: ' % self.count, end='') | |||
# Padded resize | |||
img = letterbox(img0, new_shape=self.img_size)[0] | |||
# Convert | |||
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 | |||
img = np.ascontiguousarray(img) | |||
return img_path, img, img0, None | |||
def __len__(self): | |||
return 0 | |||
class LoadStreams: # multiple IP or RTSP cameras | |||
def __init__(self, sources='streams.txt', img_size=416): | |||
self.mode = 'images' | |||
self.img_size = img_size | |||
if os.path.isfile(sources): | |||
with open(sources, 'r') as f: | |||
sources = [x.strip() for x in f.read().splitlines() if len(x.strip())] | |||
else: | |||
sources = [sources] | |||
n = len(sources) | |||
self.imgs = [None] * n | |||
self.sources = sources | |||
for i, s in enumerate(sources): | |||
# Start the thread to read frames from the video stream | |||
print('%g/%g: %s... ' % (i + 1, n, s), end='') | |||
cap = cv2.VideoCapture(0 if s == '0' else s) | |||
assert cap.isOpened(), 'Failed to open %s' % s | |||
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) | |||
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |||
fps = cap.get(cv2.CAP_PROP_FPS) % 100 | |||
_, self.imgs[i] = cap.read() # guarantee first frame | |||
thread = Thread(target=self.update, args=([i, cap]), daemon=True) | |||
print(' success (%gx%g at %.2f FPS).' % (w, h, fps)) | |||
thread.start() | |||
print('') # newline | |||
# check for common shapes | |||
s = np.stack([letterbox(x, new_shape=self.img_size)[0].shape for x in self.imgs], 0) # inference shapes | |||
self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal | |||
if not self.rect: | |||
print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.') | |||
def update(self, index, cap): | |||
# Read next stream frame in a daemon thread | |||
n = 0 | |||
while cap.isOpened(): | |||
n += 1 | |||
# _, self.imgs[index] = cap.read() | |||
cap.grab() | |||
if n == 4: # read every 4th frame | |||
_, self.imgs[index] = cap.retrieve() | |||
n = 0 | |||
time.sleep(0.01) # wait time | |||
def __iter__(self): | |||
self.count = -1 | |||
return self | |||
def __next__(self): | |||
self.count += 1 | |||
img0 = self.imgs.copy() | |||
if cv2.waitKey(1) == ord('q'): # q to quit | |||
cv2.destroyAllWindows() | |||
raise StopIteration | |||
# Letterbox | |||
img = [letterbox(x, new_shape=self.img_size, auto=self.rect)[0] for x in img0] | |||
# Stack | |||
img = np.stack(img, 0) | |||
# Convert | |||
img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416 | |||
img = np.ascontiguousarray(img) | |||
return self.sources, img, img0, None | |||
def __len__(self): | |||
return 0 # 1E12 frames = 32 streams at 30 FPS for 30 years | |||
class LoadImagesAndLabels(Dataset): # for training/testing | |||
def __init__(self, path, img_size=416, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False, | |||
cache_images=False, single_cls=False): | |||
try: | |||
path = str(Path(path)) # os-agnostic | |||
parent = str(Path(path).parent) + os.sep | |||
if os.path.isfile(path): # file | |||
with open(path, 'r') as f: | |||
f = f.read().splitlines() | |||
f = [x.replace('./', parent) if x.startswith('./') else x for x in f] # local to global path | |||
elif os.path.isdir(path): # folder | |||
f = glob.iglob(path + os.sep + '*.*') | |||
else: | |||
raise Exception('%s does not exist' % path) | |||
self.img_files = [x.replace('/', os.sep) for x in f if os.path.splitext(x)[-1].lower() in img_formats] | |||
except: | |||
raise Exception('Error loading data from %s. See %s' % (path, help_url)) | |||
n = len(self.img_files) | |||
assert n > 0, 'No images found in %s. See %s' % (path, help_url) | |||
bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index | |||
nb = bi[-1] + 1 # number of batches | |||
self.n = n # number of images | |||
self.batch = bi # batch index of image | |||
self.img_size = img_size | |||
self.augment = augment | |||
self.hyp = hyp | |||
self.image_weights = image_weights | |||
self.rect = False if image_weights else rect | |||
self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training) | |||
# Define labels | |||
self.label_files = [x.replace('images', 'labels').replace(os.path.splitext(x)[-1], '.txt') | |||
for x in self.img_files] | |||
# Rectangular Training https://github.com/ultralytics/yolov3/issues/232 | |||
if self.rect: | |||
# Read image shapes (wh) | |||
sp = path.replace('.txt', '') + '.shapes' # shapefile path | |||
try: | |||
with open(sp, 'r') as f: # read existing shapefile | |||
s = [x.split() for x in f.read().splitlines()] | |||
assert len(s) == n, 'Shapefile out of sync' | |||
except: | |||
s = [exif_size(Image.open(f)) for f in tqdm(self.img_files, desc='Reading image shapes')] | |||
np.savetxt(sp, s, fmt='%g') # overwrites existing (if any) | |||
# Sort by aspect ratio | |||
s = np.array(s, dtype=np.float64) | |||
ar = s[:, 1] / s[:, 0] # aspect ratio | |||
irect = ar.argsort() | |||
self.img_files = [self.img_files[i] for i in irect] | |||
self.label_files = [self.label_files[i] for i in irect] | |||
self.shapes = s[irect] # wh | |||
ar = ar[irect] | |||
# Set training image shapes | |||
shapes = [[1, 1]] * nb | |||
for i in range(nb): | |||
ari = ar[bi == i] | |||
mini, maxi = ari.min(), ari.max() | |||
if maxi < 1: | |||
shapes[i] = [maxi, 1] | |||
elif mini > 1: | |||
shapes[i] = [1, 1 / mini] | |||
self.batch_shapes = np.ceil(np.array(shapes) * img_size / 64.).astype(np.int) * 64 | |||
# Cache labels | |||
self.imgs = [None] * n | |||
self.labels = [np.zeros((0, 5), dtype=np.float32)] * n | |||
create_datasubset, extract_bounding_boxes, labels_loaded = False, False, False | |||
nm, nf, ne, ns, nd = 0, 0, 0, 0, 0 # number missing, found, empty, datasubset, duplicate | |||
np_labels_path = str(Path(self.label_files[0]).parent) + '.npy' # saved labels in *.npy file | |||
if os.path.isfile(np_labels_path): | |||
s = np_labels_path # print string | |||
x = np.load(np_labels_path, allow_pickle=True) | |||
if len(x) == n: | |||
self.labels = x | |||
labels_loaded = True | |||
else: | |||
s = path.replace('images', 'labels') | |||
pbar = tqdm(self.label_files) | |||
for i, file in enumerate(pbar): | |||
if labels_loaded: | |||
l = self.labels[i] | |||
# np.savetxt(file, l, '%g') # save *.txt from *.npy file | |||
else: | |||
try: | |||
with open(file, 'r') as f: | |||
l = np.array([x.split() for x in f.read().splitlines()], dtype=np.float32) | |||
except: | |||
nm += 1 # print('missing labels for image %s' % self.img_files[i]) # file missing | |||
continue | |||
if l.shape[0]: | |||
assert l.shape[1] == 5, '> 5 label columns: %s' % file | |||
assert (l >= 0).all(), 'negative labels: %s' % file | |||
assert (l[:, 1:] <= 1).all(), 'non-normalized or out of bounds coordinate labels: %s' % file | |||
if np.unique(l, axis=0).shape[0] < l.shape[0]: # duplicate rows | |||
nd += 1 # print('WARNING: duplicate rows in %s' % self.label_files[i]) # duplicate rows | |||
if single_cls: | |||
l[:, 0] = 0 # force dataset into single-class mode | |||
self.labels[i] = l | |||
nf += 1 # file found | |||
# Create subdataset (a smaller dataset) | |||
if create_datasubset and ns < 1E4: | |||
if ns == 0: | |||
create_folder(path='./datasubset') | |||
os.makedirs('./datasubset/images') | |||
exclude_classes = 43 | |||
if exclude_classes not in l[:, 0]: | |||
ns += 1 | |||
# shutil.copy(src=self.img_files[i], dst='./datasubset/images/') # copy image | |||
with open('./datasubset/images.txt', 'a') as f: | |||
f.write(self.img_files[i] + '\n') | |||
# Extract object detection boxes for a second stage classifier | |||
if extract_bounding_boxes: | |||
p = Path(self.img_files[i]) | |||
img = cv2.imread(str(p)) | |||
h, w = img.shape[:2] | |||
for j, x in enumerate(l): | |||
f = '%s%sclassifier%s%g_%g_%s' % (p.parent.parent, os.sep, os.sep, x[0], j, p.name) | |||
if not os.path.exists(Path(f).parent): | |||
os.makedirs(Path(f).parent) # make new output folder | |||
b = x[1:] * [w, h, w, h] # box | |||
b[2:] = b[2:].max() # rectangle to square | |||
b[2:] = b[2:] * 1.3 + 30 # pad | |||
b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(np.int) | |||
b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image | |||
b[[1, 3]] = np.clip(b[[1, 3]], 0, h) | |||
assert cv2.imwrite(f, img[b[1]:b[3], b[0]:b[2]]), 'Failure extracting classifier boxes' | |||
else: | |||
ne += 1 # print('empty labels for image %s' % self.img_files[i]) # file empty | |||
# os.system("rm '%s' '%s'" % (self.img_files[i], self.label_files[i])) # remove | |||
pbar.desc = 'Caching labels %s (%g found, %g missing, %g empty, %g duplicate, for %g images)' % ( | |||
s, nf, nm, ne, nd, n) | |||
assert nf > 0 or n == 20288, 'No labels found in %s. See %s' % (os.path.dirname(file) + os.sep, help_url) | |||
if not labels_loaded and n > 1000: | |||
print('Saving labels to %s for faster future loading' % np_labels_path) | |||
np.save(np_labels_path, self.labels) # save for next time | |||
# Cache images into memory for faster training (WARNING: large datasets may exceed system RAM) | |||
if cache_images: # if training | |||
gb = 0 # Gigabytes of cached images | |||
pbar = tqdm(range(len(self.img_files)), desc='Caching images') | |||
self.img_hw0, self.img_hw = [None] * n, [None] * n | |||
for i in pbar: # max 10k images | |||
self.imgs[i], self.img_hw0[i], self.img_hw[i] = load_image(self, i) # img, hw_original, hw_resized | |||
gb += self.imgs[i].nbytes | |||
pbar.desc = 'Caching images (%.1fGB)' % (gb / 1E9) | |||
# Detect corrupted images https://medium.com/joelthchao/programmatically-detect-corrupted-image-8c1b2006c3d3 | |||
detect_corrupted_images = False | |||
if detect_corrupted_images: | |||
from skimage import io # conda install -c conda-forge scikit-image | |||
for file in tqdm(self.img_files, desc='Detecting corrupted images'): | |||
try: | |||
_ = io.imread(file) | |||
except: | |||
print('Corrupted image detected: %s' % file) | |||
def __len__(self): | |||
return len(self.img_files) | |||
# def __iter__(self): | |||
# self.count = -1 | |||
# print('ran dataset iter') | |||
# #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF) | |||
# return self | |||
def __getitem__(self, index): | |||
if self.image_weights: | |||
index = self.indices[index] | |||
hyp = self.hyp | |||
if self.mosaic: | |||
# Load mosaic | |||
img, labels = load_mosaic(self, index) | |||
shapes = None | |||
else: | |||
# Load image | |||
img, (h0, w0), (h, w) = load_image(self, index) | |||
# Letterbox | |||
shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape | |||
img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment) | |||
shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling | |||
# Load labels | |||
labels = [] | |||
x = self.labels[index] | |||
if x.size > 0: | |||
# Normalized xywh to pixel xyxy format | |||
labels = x.copy() | |||
labels[:, 1] = ratio[0] * w * (x[:, 1] - x[:, 3] / 2) + pad[0] # pad width | |||
labels[:, 2] = ratio[1] * h * (x[:, 2] - x[:, 4] / 2) + pad[1] # pad height | |||
labels[:, 3] = ratio[0] * w * (x[:, 1] + x[:, 3] / 2) + pad[0] | |||
labels[:, 4] = ratio[1] * h * (x[:, 2] + x[:, 4] / 2) + pad[1] | |||
if self.augment: | |||
# Augment imagespace | |||
if not self.mosaic: | |||
img, labels = random_affine(img, labels, | |||
degrees=hyp['degrees'], | |||
translate=hyp['translate'], | |||
scale=hyp['scale'], | |||
shear=hyp['shear']) | |||
# Augment colorspace | |||
augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v']) | |||
# Apply cutouts | |||
# if random.random() < 0.9: | |||
# labels = cutout(img, labels) | |||
nL = len(labels) # number of labels | |||
if nL: | |||
# convert xyxy to xywh | |||
labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) | |||
# Normalize coordinates 0 - 1 | |||
labels[:, [2, 4]] /= img.shape[0] # height | |||
labels[:, [1, 3]] /= img.shape[1] # width | |||
if self.augment: | |||
# random left-right flip | |||
lr_flip = True | |||
if lr_flip and random.random() < 0.5: | |||
img = np.fliplr(img) | |||
if nL: | |||
labels[:, 1] = 1 - labels[:, 1] | |||
# random up-down flip | |||
ud_flip = False | |||
if ud_flip and random.random() < 0.5: | |||
img = np.flipud(img) | |||
if nL: | |||
labels[:, 2] = 1 - labels[:, 2] | |||
labels_out = torch.zeros((nL, 6)) | |||
if nL: | |||
labels_out[:, 1:] = torch.from_numpy(labels) | |||
# Convert | |||
img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416 | |||
img = np.ascontiguousarray(img) | |||
return torch.from_numpy(img), labels_out, self.img_files[index], shapes | |||
@staticmethod | |||
def collate_fn(batch): | |||
img, label, path, shapes = zip(*batch) # transposed | |||
for i, l in enumerate(label): | |||
l[:, 0] = i # add target image index for build_targets() | |||
return torch.stack(img, 0), torch.cat(label, 0), path, shapes | |||
def load_image(self, index): | |||
# loads 1 image from dataset, returns img, original hw, resized hw | |||
img = self.imgs[index] | |||
if img is None: # not cached | |||
path = self.img_files[index] | |||
img = cv2.imread(path) # BGR | |||
assert img is not None, 'Image Not Found ' + path | |||
h0, w0 = img.shape[:2] # orig hw | |||
r = self.img_size / max(h0, w0) # resize image to img_size | |||
if r != 1: # always resize down, only resize up if training with augmentation | |||
interp = cv2.INTER_AREA if r < 1 and not self.augment else cv2.INTER_LINEAR | |||
img = cv2.resize(img, (int(w0 * r), int(h0 * r)), interpolation=interp) | |||
return img, (h0, w0), img.shape[:2] # img, hw_original, hw_resized | |||
else: | |||
return self.imgs[index], self.img_hw0[index], self.img_hw[index] # img, hw_original, hw_resized | |||
def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5): | |||
r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains | |||
hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV)) | |||
dtype = img.dtype # uint8 | |||
x = np.arange(0, 256, dtype=np.int16) | |||
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) | |||
img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype) | |||
cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed | |||
# Histogram equalization | |||
# if random.random() < 0.2: | |||
# for i in range(3): | |||
# img[:, :, i] = cv2.equalizeHist(img[:, :, i]) | |||
def load_mosaic(self, index): | |||
# loads images in a mosaic | |||
labels4 = [] | |||
s = self.img_size | |||
xc, yc = [int(random.uniform(s * 0.5, s * 1.5)) for _ in range(2)] # mosaic center x, y | |||
indices = [index] + [random.randint(0, len(self.labels) - 1) for _ in range(3)] # 3 additional image indices | |||
for i, index in enumerate(indices): | |||
# Load image | |||
img, _, (h, w) = load_image(self, index) | |||
# place img in img4 | |||
if i == 0: # top left | |||
img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles | |||
x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image) | |||
x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image) | |||
elif i == 1: # top right | |||
x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc | |||
x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h | |||
elif i == 2: # bottom left | |||
x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h) | |||
x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, max(xc, w), min(y2a - y1a, h) | |||
elif i == 3: # bottom right | |||
x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h) | |||
x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h) | |||
img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax] | |||
padw = x1a - x1b | |||
padh = y1a - y1b | |||
# Labels | |||
x = self.labels[index] | |||
labels = x.copy() | |||
if x.size > 0: # Normalized xywh to pixel xyxy format | |||
labels[:, 1] = w * (x[:, 1] - x[:, 3] / 2) + padw | |||
labels[:, 2] = h * (x[:, 2] - x[:, 4] / 2) + padh | |||
labels[:, 3] = w * (x[:, 1] + x[:, 3] / 2) + padw | |||
labels[:, 4] = h * (x[:, 2] + x[:, 4] / 2) + padh | |||
labels4.append(labels) | |||
# Concat/clip labels | |||
if len(labels4): | |||
labels4 = np.concatenate(labels4, 0) | |||
# np.clip(labels4[:, 1:] - s / 2, 0, s, out=labels4[:, 1:]) # use with center crop | |||
np.clip(labels4[:, 1:], 0, 2 * s, out=labels4[:, 1:]) # use with random_affine | |||
# Augment | |||
# img4 = img4[s // 2: int(s * 1.5), s // 2:int(s * 1.5)] # center crop (WARNING, requires box pruning) | |||
img4, labels4 = random_affine(img4, labels4, | |||
degrees=self.hyp['degrees'], | |||
translate=self.hyp['translate'], | |||
scale=self.hyp['scale'], | |||
shear=self.hyp['shear'], | |||
border=-s // 2) # border to remove | |||
return img4, labels4 | |||
def letterbox(img, new_shape=(416, 416), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True): | |||
# Resize image to a 32-pixel-multiple rectangle https://github.com/ultralytics/yolov3/issues/232 | |||
shape = img.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 test 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, 64), np.mod(dh, 64) # wh padding | |||
elif scaleFill: # stretch | |||
dw, dh = 0.0, 0.0 | |||
new_unpad = new_shape | |||
ratio = new_shape[0] / shape[1], new_shape[1] / shape[0] # width, height ratios | |||
dw /= 2 # divide padding into 2 sides | |||
dh /= 2 | |||
if shape[::-1] != new_unpad: # resize | |||
img = cv2.resize(img, 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)) | |||
img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border | |||
return img, ratio, (dw, dh) | |||
def random_affine(img, targets=(), degrees=10, translate=.1, scale=.1, shear=10, border=0): | |||
# torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10)) | |||
# https://medium.com/uruvideo/dataset-augmentation-with-random-homographies-a8f4b44830d4 | |||
# targets = [cls, xyxy] | |||
height = img.shape[0] + border * 2 | |||
width = img.shape[1] + border * 2 | |||
# 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=(img.shape[1] / 2, img.shape[0] / 2), scale=s) | |||
# Translation | |||
T = np.eye(3) | |||
T[0, 2] = random.uniform(-translate, translate) * img.shape[0] + border # x translation (pixels) | |||
T[1, 2] = random.uniform(-translate, translate) * img.shape[1] + border # y translation (pixels) | |||
# 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) | |||
# Combined rotation matrix | |||
M = S @ T @ R # ORDER IS IMPORTANT HERE!! | |||
if (border != 0) or (M != np.eye(3)).any(): # image changed | |||
img = cv2.warpAffine(img, M[:2], dsize=(width, height), flags=cv2.INTER_LINEAR, borderValue=(114, 114, 114)) | |||
# Transform label coordinates | |||
n = len(targets) | |||
if n: | |||
# warp points | |||
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)[:, :2].reshape(n, 8) | |||
# create new boxes | |||
x = xy[:, [0, 2, 4, 6]] | |||
y = xy[:, [1, 3, 5, 7]] | |||
xy = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T | |||
# # apply angle-based reduction of bounding boxes | |||
# radians = a * math.pi / 180 | |||
# reduction = max(abs(math.sin(radians)), abs(math.cos(radians))) ** 0.5 | |||
# x = (xy[:, 2] + xy[:, 0]) / 2 | |||
# y = (xy[:, 3] + xy[:, 1]) / 2 | |||
# w = (xy[:, 2] - xy[:, 0]) * reduction | |||
# h = (xy[:, 3] - xy[:, 1]) * reduction | |||
# xy = np.concatenate((x - w / 2, y - h / 2, x + w / 2, y + h / 2)).reshape(4, n).T | |||
# reject warped points outside of image | |||
xy[:, [0, 2]] = xy[:, [0, 2]].clip(0, width) | |||
xy[:, [1, 3]] = xy[:, [1, 3]].clip(0, height) | |||
w = xy[:, 2] - xy[:, 0] | |||
h = xy[:, 3] - xy[:, 1] | |||
area = w * h | |||
area0 = (targets[:, 3] - targets[:, 1]) * (targets[:, 4] - targets[:, 2]) | |||
ar = np.maximum(w / (h + 1e-16), h / (w + 1e-16)) # aspect ratio | |||
i = (w > 4) & (h > 4) & (area / (area0 * s + 1e-16) > 0.2) & (ar < 10) | |||
targets = targets[i] | |||
targets[:, 1:5] = xy[i] | |||
return img, targets | |||
def cutout(image, labels): | |||
# https://arxiv.org/abs/1708.04552 | |||
# https://github.com/hysts/pytorch_cutout/blob/master/dataloader.py | |||
# https://towardsdatascience.com/when-conventional-wisdom-fails-revisiting-data-augmentation-for-self-driving-cars-4831998c5509 | |||
h, w = image.shape[:2] | |||
def bbox_ioa(box1, box2): | |||
# Returns the intersection over box2 area given box1, box2. box1 is 4, box2 is nx4. boxes are x1y1x2y2 | |||
box2 = box2.transpose() | |||
# Get the coordinates of bounding boxes | |||
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3] | |||
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3] | |||
# Intersection area | |||
inter_area = (np.minimum(b1_x2, b2_x2) - np.maximum(b1_x1, b2_x1)).clip(0) * \ | |||
(np.minimum(b1_y2, b2_y2) - np.maximum(b1_y1, b2_y1)).clip(0) | |||
# box2 area | |||
box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + 1e-16 | |||
# Intersection over box2 area | |||
return inter_area / box2_area | |||
# create random masks | |||
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)) | |||
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 | |||
image[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, labels[:, 1:5]) # intersection over area | |||
labels = labels[ioa < 0.60] # remove >60% obscured labels | |||
return labels | |||
def reduce_img_size(path='../data/sm4/images', img_size=1024): # from utils.datasets import *; reduce_img_size() | |||
# creates a new ./images_reduced folder with reduced size images of maximum size img_size | |||
path_new = path + '_reduced' # reduced images path | |||
create_folder(path_new) | |||
for f in tqdm(glob.glob('%s/*.*' % path)): | |||
try: | |||
img = cv2.imread(f) | |||
h, w = img.shape[:2] | |||
r = img_size / max(h, w) # size ratio | |||
if r < 1.0: | |||
img = cv2.resize(img, (int(w * r), int(h * r)), interpolation=cv2.INTER_AREA) # _LINEAR fastest | |||
fnew = f.replace(path, path_new) # .replace(Path(f).suffix, '.jpg') | |||
cv2.imwrite(fnew, img) | |||
except: | |||
print('WARNING: image failure %s' % f) | |||
def convert_images2bmp(): # from utils.datasets import *; convert_images2bmp() | |||
# Save images | |||
formats = [x.lower() for x in img_formats] + [x.upper() for x in img_formats] | |||
# for path in ['../coco/images/val2014', '../coco/images/train2014']: | |||
for path in ['../data/sm4/images', '../data/sm4/background']: | |||
create_folder(path + 'bmp') | |||
for ext in formats: # ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.dng'] | |||
for f in tqdm(glob.glob('%s/*%s' % (path, ext)), desc='Converting %s' % ext): | |||
cv2.imwrite(f.replace(ext.lower(), '.bmp').replace(path, path + 'bmp'), cv2.imread(f)) | |||
# Save labels | |||
# for path in ['../coco/trainvalno5k.txt', '../coco/5k.txt']: | |||
for file in ['../data/sm4/out_train.txt', '../data/sm4/out_test.txt']: | |||
with open(file, 'r') as f: | |||
lines = f.read() | |||
# lines = f.read().replace('2014/', '2014bmp/') # coco | |||
lines = lines.replace('/images', '/imagesbmp') | |||
lines = lines.replace('/background', '/backgroundbmp') | |||
for ext in formats: | |||
lines = lines.replace(ext, '.bmp') | |||
with open(file.replace('.txt', 'bmp.txt'), 'w') as f: | |||
f.write(lines) | |||
def recursive_dataset2bmp(dataset='../data/sm4_bmp'): # from utils.datasets import *; recursive_dataset2bmp() | |||
# Converts dataset to bmp (for faster training) | |||
formats = [x.lower() for x in img_formats] + [x.upper() for x in img_formats] | |||
for a, b, files in os.walk(dataset): | |||
for file in tqdm(files, desc=a): | |||
p = a + '/' + file | |||
s = Path(file).suffix | |||
if s == '.txt': # replace text | |||
with open(p, 'r') as f: | |||
lines = f.read() | |||
for f in formats: | |||
lines = lines.replace(f, '.bmp') | |||
with open(p, 'w') as f: | |||
f.write(lines) | |||
elif s in formats: # replace image | |||
cv2.imwrite(p.replace(s, '.bmp'), cv2.imread(p)) | |||
if s != '.bmp': | |||
os.system("rm '%s'" % p) | |||
def imagelist2folder(path='data/coco_64img.txt'): # from utils.datasets import *; imagelist2folder() | |||
# Copies all the images in a text file (list of images) into a folder | |||
create_folder(path[:-4]) | |||
with open(path, 'r') as f: | |||
for line in f.read().splitlines(): | |||
os.system('cp "%s" %s' % (line, path[:-4])) | |||
print(line) | |||
def create_folder(path='./new_folder'): | |||
# Create folder | |||
if os.path.exists(path): | |||
shutil.rmtree(path) # delete output folder | |||
os.makedirs(path) # make new output folder |
@@ -0,0 +1,181 @@ | |||
#!/usr/bin/env bash | |||
# New VM | |||
if [ ! -d ./coco ] | |||
then | |||
echo "COCO folder not found. Running startup script." | |||
git clone https://github.com/ultralytics/yolov5 | |||
# git clone -b test --depth 1 https://github.com/ultralytics/yolov5 test # branch | |||
# sudo apt-get install zip | |||
# git clone https://github.com/NVIDIA/apex && cd apex && pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" . --user && cd .. && rm -rf apex | |||
# sudo conda install -yc conda-forge scikit-image pycocotools | |||
python3 -c "from yolov5.utils.google_utils import gdrive_download; gdrive_download('1rrL-Jbc68iHiGjXOYc8u9tKfFiOX21Tn','coco2017.zip')" | |||
python3 -c "from yolov5.utils.google_utils import gdrive_download; gdrive_download('1Y6Kou6kEB0ZEMCCpJSKStCor4KAReE43','coco2017.zip')" | |||
sudo docker pull ultralytics/coco:198 | |||
# Add 64GB swap | |||
sudo fallocate -l 64G /swapfile | |||
sudo chmod 600 /swapfile | |||
sudo mkswap /swapfile | |||
sudo swapon /swapfile | |||
free -h # check memory | |||
# sudo reboot now | |||
fi | |||
n=198 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s_csp2.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
# Evolve coco | |||
sudo -s | |||
t=ultralytics/yolov3:evolve | |||
# docker kill $(docker ps -a -q --filter ancestor=$t) | |||
for i in 0 1 6 7 | |||
do | |||
docker pull $t && docker run --gpus all -d --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t bash utils/evolve.sh $i | |||
sleep 30 | |||
done | |||
# kmean_anchors(path='../coco/train2017.txt', n=12, img_size=(256, 1024), thr=0.10, gen=10000) | |||
# 0.10 iou_thr: 1.000 best possible recall, 6.15 anchors > thr | |||
# n=12, img_size=(256, 1024), IoU_all=0.188/0.641-mean/best, IoU>thr=0.338-mean: 7,9, 13,17, 30,21, 18,38, 31,63, 54,38, 52,110, 90,69, 98,187, 164,116, 218,255, 448,414 | |||
# from yolov4l_10iou | |||
# computed with utils.kmean_anchors(path='../coco/train2017.txt', n=12, img_size=(320, 1024), thr=0.10, gen=1000) | |||
# Evolving anchors: 100%|█████████████████| 1000/1000 [39:57<00:00, 2.40s/it] | |||
# 0.10 iou_thr: 0.998 best possible recall, 6.07 anchors > thr | |||
# n=12, img_size=(320, 1024), IoU_all=0.187/0.635-mean/best, IoU>thr=0.339-mean: 9,13, 20,21, 21,50, 43,34, 44,89, 84,59, 76,164, 133,105, 216,176, 153,267, 312,331, 623,467 | |||
# from yolov4l_10iou_9anchors | |||
# computed with utils.kmean_anchors(path='../coco/train2017.txt', n=9, img_size=(320, 1024), thr=0.10, gen=1000) | |||
# Evolving anchors: 100%|█████████████████| 1000/1000 [31:26<00:00, 1.89s/it] | |||
# 0.10 iou_thr: 0.998 best possible recall, 4.60 anchors > thr | |||
# n=9, img_size=(320, 1024), IoU_all=0.190/0.604-mean/best, IoU>thr=0.342-mean: 9,13, 20,26, 29,58, 55,37, 57,115, 105,77, 133,165, 254,280, 508,450 | |||
# ar < 5 | |||
#Evolving anchors: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1000/1000 [14:56<00:00, 1.00it/s] | |||
#0.10 iou_thr: 0.999 best possible recall, 4.63 anchors > thr | |||
#n=9, img_size=(320, 1024), IoU_all=0.187/0.601-mean/best, IoU>thr=0.334-mean: 9,11, 15,35, 36,21, 34,61, 86,55, 66,145, 164,110, 185,262, 450,369 | |||
#Evolving anchors: 10%|████████████ | 1015/10000 [15:59<1:56:24, 1.29it/s] | |||
#5.00 iou_thr: 1.000 best possible recall, 4.79 anchors > thr | |||
#n=9, img_size=(320, 1024), IoU_all=9.193/1.535-mean/best, IoU>thr=2.745-mean: 8,8, 14,24, 41,21, 24,56, 64,57, 60,140, 154,102, 174,262, 443,340 | |||
#Evolving anchors: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10000/10000 [2:15:31<00:00, 1.25it/s] | |||
#5.00 iou_thr: 1.000 best possible recall, 4.80 anchors > thr | |||
#n=9, img_size=(320, 1024), IoU_all=9.106/1.531-mean/best, IoU>thr=2.744-mean: 8,8, 12,24, 33,19, 25,53, 64,48, 58,128, 145,93, 164,240, 419,349 | |||
Evolving anchors: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10000/10000 [21:39<00:00, 7.83it/s] | |||
0.20 iou_thr: 0.992 best possible recall, 3.44 anchors > thr | |||
n=9, img_size=(640, 640), IoU_all=0.199/0.614-mean/best, IoU>thr=0.421-mean: 9,11, 16,25, 38,25, 26,54, 70,50, 51,102, 113,109, 161,218, 366,343 | |||
Evolving anchors: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10000/10000 [16:34<00:00, 10.05it/s] | |||
0.10 iou_thr: 0.999 best possible recall, 4.87 anchors > thr | |||
n=9, img_size=(640, 640), IoU_all=0.200/0.614-mean/best, IoU>thr=0.342-mean: 10,12, 16,27, 37,25, 27,56, 69,51, 53,108, 116,107, 163,216, 364,343 | |||
- [10,13, 16,30, 33,23] # P3/8 | |||
- [30,61, 62,45, 59,119] # P4/16 | |||
- [116,90, 156,198, 373,326] # P5/32 | |||
# coco (small cancelled, large, medium, small, yolov3) | |||
n=129 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov4.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=130 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 32 --weights '' --cfg models/yolov4.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=133 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 16 --weights '' --cfg models/yolov4.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=134 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov4s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=135 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 32 --weights '' --cfg models/yolov3-spp.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=136 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 16 --weights '' --cfg models/yolov3-spp.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=138 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 24 --weights '' --cfg models/yolov3-spp.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=139 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 24 --weights '' --cfg models/yolov3-spp.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=140 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 24 --weights '' --cfg models/yolov3-spp.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=141 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=142 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=143 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=144 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=145 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 12 --weights '' --cfg models/yolov5l.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=146 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=147 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=148 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=149 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 832 --batch 32 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=150 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 32 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=151 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 320 640 --batch 32 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=152 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=154 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 32 --weights '' --cfg models/yolov5s_exp.yaml --bucket ult/coco --name $n && sudo shutdown | |||
n=153 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_focus1.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=155 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_focus2.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=156 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_exp_focus0.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=157 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_focus3.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=158 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_focus4.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=159 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_focus5.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=160 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5_final.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=161 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5_final.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=162 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5_final.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=163 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5_final.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=164 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5_final.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=165 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_origami.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=166 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_k3.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=167 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_plus.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=168 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=169 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=170 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 24 --weights '' --cfg models/yolov3-spp.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=171 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_k3_spp5913.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=172 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=173 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 24 --weights '' --cfg models/yolov3-spp.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=174 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 24 --weights '' --cfg models/yolov3-spp.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=178 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 96 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=179 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 48 --weights '' --cfg models/yolov5m.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=180 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 28 --weights '' --cfg models/yolov5l.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=181 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 16 --weights '' --cfg models/yolov5x.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=182 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 24 --weights '' --cfg models/yolov3-spp.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=183 && t=ultralytics/coco:v178 && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=184 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_csp.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=185 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 64 --weights '' --cfg models/yolov5s_csp.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=186 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=187 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s_csp.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=188 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s_csp.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=189 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s_focus.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=190 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=191 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=192 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=193 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=194 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s_csp.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=195 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s_csp.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=196 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s_csp.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=197 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s_csp1.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=198 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s_csp2.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=199 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --epochs 50 --batch 64 --weights '' --cfg models/yolov5s_csp2.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=201 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 48 --weights '' --cfg models/yolov5m_csp.yaml --bucket ult/coco --name $n --data data/coco.yaml | |||
n=204 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 24 --weights '' --cfg models/yolov3-spp.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=206 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 24 --weights '' --cfg models/yolov3-spp_csp.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=207 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t python3 train.py --img 640 640 --batch 24 --weights '' --cfg models/yolov3-spp_csp.yaml --bucket ult/coco --name $n --data data/coco.yaml && sudo shutdown | |||
n=205 && t=ultralytics/coco:v$n && sudo docker pull $t && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco $t bash |
@@ -0,0 +1,94 @@ | |||
# This file contains google utils: https://cloud.google.com/storage/docs/reference/libraries | |||
# pip install --upgrade google-cloud-storage | |||
# from google.cloud import storage | |||
import os | |||
import time | |||
from pathlib import Path | |||
def attempt_download(weights): | |||
# Attempt to download pretrained weights if not found locally | |||
weights = weights.strip() | |||
msg = weights + ' missing, try downloading from https://drive.google.com/open?id=1LezFG5g3BCW6iYaV89B2i64cqEUZD7e0' | |||
r = 1 | |||
if len(weights) > 0 and not os.path.isfile(weights): | |||
d = {'yolov3-spp.pt': '1mM67oNw4fZoIOL1c8M3hHmj66d8e-ni_', # yolov3-spp.yaml | |||
'yolov5s.pt': '1R5T6rIyy3lLwgFXNms8whc-387H0tMQO', # yolov5s.yaml | |||
'yolov5m.pt': '1vobuEExpWQVpXExsJ2w-Mbf3HJjWkQJr', # yolov5m.yaml | |||
'yolov5l.pt': '1hrlqD1Wdei7UT4OgT785BEk1JwnSvNEV', # yolov5l.yaml | |||
'yolov5x.pt': '1mM8aZJlWTxOg7BZJvNUMrTnA2AbeCVzS', # yolov5x.yaml | |||
} | |||
file = Path(weights).name | |||
if file in d: | |||
r = gdrive_download(id=d[file], name=weights) | |||
# Error check | |||
if not (r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6): # weights exist and > 1MB | |||
os.system('rm ' + weights) # remove partial downloads | |||
raise Exception(msg) | |||
def gdrive_download(id='1HaXkef9z6y5l4vUnCYgdmEAj61c6bfWO', name='coco.zip'): | |||
# https://gist.github.com/tanaikech/f0f2d122e05bf5f971611258c22c110f | |||
# Downloads a file from Google Drive, accepting presented query | |||
# from utils.google_utils import *; gdrive_download() | |||
t = time.time() | |||
print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='') | |||
os.remove(name) if os.path.exists(name) else None # remove existing | |||
os.remove('cookie') if os.path.exists('cookie') else None | |||
# Attempt file download | |||
os.system("curl -c ./cookie -s -L \"https://drive.google.com/uc?export=download&id=%s\" > /dev/null" % id) | |||
if os.path.exists('cookie'): # large file | |||
s = "curl -Lb ./cookie \"https://drive.google.com/uc?export=download&confirm=`awk '/download/ {print $NF}' ./cookie`&id=%s\" -o %s" % ( | |||
id, name) | |||
else: # small file | |||
s = "curl -s -L -o %s 'https://drive.google.com/uc?export=download&id=%s'" % (name, id) | |||
r = os.system(s) # execute, capture return values | |||
os.remove('cookie') if os.path.exists('cookie') else None | |||
# Error check | |||
if r != 0: | |||
os.remove(name) if os.path.exists(name) else None # remove partial | |||
print('Download error ') # raise Exception('Download error') | |||
return r | |||
# Unzip if archive | |||
if name.endswith('.zip'): | |||
print('unzipping... ', end='') | |||
os.system('unzip -q %s' % name) # unzip | |||
os.remove(name) # remove zip to free space | |||
print('Done (%.1fs)' % (time.time() - t)) | |||
return r | |||
# def upload_blob(bucket_name, source_file_name, destination_blob_name): | |||
# # Uploads a file to a bucket | |||
# # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python | |||
# | |||
# storage_client = storage.Client() | |||
# bucket = storage_client.get_bucket(bucket_name) | |||
# blob = bucket.blob(destination_blob_name) | |||
# | |||
# blob.upload_from_filename(source_file_name) | |||
# | |||
# print('File {} uploaded to {}.'.format( | |||
# source_file_name, | |||
# destination_blob_name)) | |||
# | |||
# | |||
# def download_blob(bucket_name, source_blob_name, destination_file_name): | |||
# # Uploads a blob from a bucket | |||
# storage_client = storage.Client() | |||
# bucket = storage_client.get_bucket(bucket_name) | |||
# blob = bucket.blob(source_blob_name) | |||
# | |||
# blob.download_to_filename(destination_file_name) | |||
# | |||
# print('Blob {} downloaded to {}.'.format( | |||
# source_blob_name, | |||
# destination_file_name)) |
@@ -0,0 +1,194 @@ | |||
import math | |||
import os | |||
import time | |||
from copy import deepcopy | |||
import torch | |||
import torch.backends.cudnn as cudnn | |||
import torch.nn as nn | |||
import torch.nn.functional as F | |||
def init_seeds(seed=0): | |||
torch.manual_seed(seed) | |||
# Reduce randomness (may be slower on Tesla GPUs) # https://pytorch.org/docs/stable/notes/randomness.html | |||
if seed == 0: | |||
cudnn.deterministic = False | |||
cudnn.benchmark = True | |||
def select_device(device='', apex=False, batch_size=None): | |||
# device = 'cpu' or '0' or '0,1,2,3' | |||
cpu_request = device.lower() == 'cpu' | |||
if device and not cpu_request: # if device requested other than 'cpu' | |||
os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable | |||
assert torch.cuda.is_available(), 'CUDA unavailable, invalid device %s requested' % device # check availablity | |||
cuda = False if cpu_request else torch.cuda.is_available() | |||
if cuda: | |||
c = 1024 ** 2 # bytes to MB | |||
ng = torch.cuda.device_count() | |||
if ng > 1 and batch_size: # check that batch_size is compatible with device_count | |||
assert batch_size % ng == 0, 'batch-size %g not multiple of GPU count %g' % (batch_size, ng) | |||
x = [torch.cuda.get_device_properties(i) for i in range(ng)] | |||
s = 'Using CUDA ' + ('Apex ' if apex else '') # apex for mixed precision https://github.com/NVIDIA/apex | |||
for i in range(0, ng): | |||
if i == 1: | |||
s = ' ' * len(s) | |||
print("%sdevice%g _CudaDeviceProperties(name='%s', total_memory=%dMB)" % | |||
(s, i, x[i].name, x[i].total_memory / c)) | |||
else: | |||
print('Using CPU') | |||
print('') # skip a line | |||
return torch.device('cuda:0' if cuda else 'cpu') | |||
def time_synchronized(): | |||
torch.cuda.synchronize() if torch.cuda.is_available() else None | |||
return time.time() | |||
def initialize_weights(model): | |||
for m in model.modules(): | |||
t = type(m) | |||
if t is nn.Conv2d: | |||
pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') | |||
elif t is nn.BatchNorm2d: | |||
m.eps = 1e-4 | |||
m.momentum = 0.03 | |||
elif t in [nn.LeakyReLU, nn.ReLU, nn.ReLU6]: | |||
m.inplace = True | |||
def find_modules(model, mclass=nn.Conv2d): | |||
# finds layer indices matching module class 'mclass' | |||
return [i for i, m in enumerate(model.module_list) if isinstance(m, mclass)] | |||
def fuse_conv_and_bn(conv, bn): | |||
# https://tehnokv.com/posts/fusing-batchnorm-and-conv/ | |||
with torch.no_grad(): | |||
# init | |||
fusedconv = torch.nn.Conv2d(conv.in_channels, | |||
conv.out_channels, | |||
kernel_size=conv.kernel_size, | |||
stride=conv.stride, | |||
padding=conv.padding, | |||
bias=True) | |||
# prepare filters | |||
w_conv = conv.weight.clone().view(conv.out_channels, -1) | |||
w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) | |||
fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.size())) | |||
# prepare spatial bias | |||
if conv.bias is not None: | |||
b_conv = conv.bias | |||
else: | |||
b_conv = torch.zeros(conv.weight.size(0)) | |||
b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps)) | |||
fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) | |||
return fusedconv | |||
def model_info(model, verbose=False): | |||
# Plots a line-by-line description of a PyTorch model | |||
n_p = sum(x.numel() for x in model.parameters()) # number parameters | |||
n_g = sum(x.numel() for x in model.parameters() if x.requires_grad) # number gradients | |||
if verbose: | |||
print('%5s %40s %9s %12s %20s %10s %10s' % ('layer', 'name', 'gradient', 'parameters', 'shape', 'mu', 'sigma')) | |||
for i, (name, p) in enumerate(model.named_parameters()): | |||
name = name.replace('module_list.', '') | |||
print('%5g %40s %9s %12g %20s %10.3g %10.3g' % | |||
(i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std())) | |||
try: # FLOPS | |||
from thop import profile | |||
macs, _ = profile(model, inputs=(torch.zeros(1, 3, 480, 640),), verbose=False) | |||
fs = ', %.1f GFLOPS' % (macs / 1E9 * 2) | |||
except: | |||
fs = '' | |||
print('Model Summary: %g layers, %g parameters, %g gradients%s' % (len(list(model.parameters())), n_p, n_g, fs)) | |||
def load_classifier(name='resnet101', n=2): | |||
# Loads a pretrained model reshaped to n-class output | |||
import pretrainedmodels # https://github.com/Cadene/pretrained-models.pytorch#torchvision | |||
model = pretrainedmodels.__dict__[name](num_classes=1000, pretrained='imagenet') | |||
# Display model properties | |||
for x in ['model.input_size', 'model.input_space', 'model.input_range', 'model.mean', 'model.std']: | |||
print(x + ' =', eval(x)) | |||
# Reshape output to n classes | |||
filters = model.last_linear.weight.shape[1] | |||
model.last_linear.bias = torch.nn.Parameter(torch.zeros(n)) | |||
model.last_linear.weight = torch.nn.Parameter(torch.zeros(n, filters)) | |||
model.last_linear.out_features = n | |||
return model | |||
def scale_img(img, ratio=1.0, same_shape=True): # img(16,3,256,416), r=ratio | |||
# scales img(bs,3,y,x) by ratio | |||
h, w = img.shape[2:] | |||
s = (int(h * ratio), int(w * ratio)) # new size | |||
img = F.interpolate(img, size=s, mode='bilinear', align_corners=False) # resize | |||
if not same_shape: # pad/crop img | |||
gs = 64 # (pixels) grid size | |||
h, w = [math.ceil(x * ratio / gs) * gs for x in (h, w)] | |||
return F.pad(img, [0, w - s[1], 0, h - s[0]], value=0.447) # value = imagenet mean | |||
class ModelEMA: | |||
""" Model Exponential Moving Average from https://github.com/rwightman/pytorch-image-models | |||
Keep a moving average of everything in the model state_dict (parameters and buffers). | |||
This is intended to allow functionality like | |||
https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage | |||
A smoothed version of the weights is necessary for some training schemes to perform well. | |||
E.g. Google's hyper-params for training MNASNet, MobileNet-V3, EfficientNet, etc that use | |||
RMSprop with a short 2.4-3 epoch decay period and slow LR decay rate of .96-.99 requires EMA | |||
smoothing of weights to match results. Pay attention to the decay constant you are using | |||
relative to your update count per epoch. | |||
To keep EMA from using GPU resources, set device='cpu'. This will save a bit of memory but | |||
disable validation of the EMA weights. Validation will have to be done manually in a separate | |||
process, or after the training stops converging. | |||
This class is sensitive where it is initialized in the sequence of model init, | |||
GPU assignment and distributed training wrappers. | |||
I've tested with the sequence in my own train.py for torch.DataParallel, apex.DDP, and single-GPU. | |||
""" | |||
def __init__(self, model, decay=0.9999, device=''): | |||
# make a copy of the model for accumulating moving average of weights | |||
self.ema = deepcopy(model) | |||
self.ema.eval() | |||
self.updates = 0 # number of EMA updates | |||
self.decay = lambda x: decay * (1 - math.exp(-x / 2000)) # decay exponential ramp (to help early epochs) | |||
self.device = device # perform ema on different device from model if set | |||
if device: | |||
self.ema.to(device=device) | |||
for p in self.ema.parameters(): | |||
p.requires_grad_(False) | |||
def update(self, model): | |||
self.updates += 1 | |||
d = self.decay(self.updates) | |||
with torch.no_grad(): | |||
if type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel): | |||
msd, esd = model.module.state_dict(), self.ema.module.state_dict() | |||
else: | |||
msd, esd = model.state_dict(), self.ema.state_dict() | |||
for k, v in esd.items(): | |||
if v.dtype.is_floating_point: | |||
v *= d | |||
v += (1. - d) * msd[k].detach() | |||
def update_attr(self, model): | |||
# Assign attributes (which may change during training) | |||
for k in model.__dict__.keys(): | |||
if not k.startswith('_'): | |||
setattr(self.ema, k, getattr(model, k)) |
@@ -0,0 +1,7 @@ | |||
#!/bin/bash | |||
# Download common models | |||
python3 -c "from models import *; | |||
attempt_download('weights/yolov5s.pt'); | |||
attempt_download('weights/yolov5m.pt'); | |||
attempt_download('weights/yolov5l.pt')" |