├── .dockerignore ├── .gitattributes ├── .github ├── ISSUE_TEMPLATE │ ├── bug-report.yml │ ├── config.yml │ ├── feature-request.yml │ └── question.yml ├── dependabot.yml └── workflows │ ├── ci-testing.yml │ ├── cla.yml │ ├── docker.yml │ ├── format.yml │ ├── links.yml │ ├── merge-main-into-prs.yml │ └── stale.yml ├── .gitignore ├── CITATION.cff ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── README.zh-CN.md ├── benchmarks.py ├── classify ├── predict.py ├── train.py ├── tutorial.ipynb └── val.py ├── data ├── Argoverse.yaml ├── GlobalWheat2020.yaml ├── ImageNet.yaml ├── ImageNet10.yaml ├── ImageNet100.yaml ├── ImageNet1000.yaml ├── Objects365.yaml ├── SKU-110K.yaml ├── VOC.yaml ├── VisDrone.yaml ├── coco.yaml ├── coco128-seg.yaml ├── coco128.yaml ├── hyps │ ├── hyp.Objects365.yaml │ ├── hyp.VOC.yaml │ ├── hyp.no-augmentation.yaml │ ├── hyp.scratch-high.yaml │ ├── hyp.scratch-low.yaml │ └── hyp.scratch-med.yaml ├── images │ ├── bus.jpg │ └── zidane.jpg ├── scripts │ ├── download_weights.sh │ ├── get_coco.sh │ ├── get_coco128.sh │ ├── get_imagenet.sh │ ├── get_imagenet10.sh │ ├── get_imagenet100.sh │ └── get_imagenet1000.sh └── xView.yaml ├── detect.py ├── export.py ├── hubconf.py ├── models ├── __init__.py ├── common.py ├── experimental.py ├── hub │ ├── anchors.yaml │ ├── yolov3-spp.yaml │ ├── yolov3-tiny.yaml │ ├── yolov3.yaml │ ├── yolov5-bifpn.yaml │ ├── yolov5-fpn.yaml │ ├── yolov5-p2.yaml │ ├── yolov5-p34.yaml │ ├── yolov5-p6.yaml │ ├── yolov5-p7.yaml │ ├── yolov5-panet.yaml │ ├── yolov5l6.yaml │ ├── yolov5m6.yaml │ ├── yolov5n6.yaml │ ├── yolov5s-LeakyReLU.yaml │ ├── yolov5s-ghost.yaml │ ├── yolov5s-transformer.yaml │ ├── yolov5s6.yaml │ └── yolov5x6.yaml ├── segment │ ├── yolov5l-seg.yaml │ ├── yolov5m-seg.yaml │ ├── yolov5n-seg.yaml │ ├── yolov5s-seg.yaml │ └── yolov5x-seg.yaml ├── tf.py ├── yolo.py ├── yolov5l.yaml ├── yolov5m.yaml ├── yolov5n.yaml ├── yolov5s.yaml └── yolov5x.yaml ├── pyproject.toml ├── requirements.txt ├── segment ├── predict.py ├── train.py ├── tutorial.ipynb └── val.py ├── train.py ├── tutorial.ipynb ├── utils ├── __init__.py ├── activations.py ├── augmentations.py ├── autoanchor.py ├── autobatch.py ├── aws │ ├── __init__.py │ ├── mime.sh │ ├── resume.py │ └── userdata.sh ├── callbacks.py ├── dataloaders.py ├── docker │ ├── Dockerfile │ ├── Dockerfile-arm64 │ └── Dockerfile-cpu ├── downloads.py ├── flask_rest_api │ ├── README.md │ ├── example_request.py │ └── restapi.py ├── general.py ├── google_app_engine │ ├── Dockerfile │ ├── additional_requirements.txt │ └── app.yaml ├── loggers │ ├── __init__.py │ ├── clearml │ │ ├── README.md │ │ ├── __init__.py │ │ ├── clearml_utils.py │ │ └── hpo.py │ ├── comet │ │ ├── README.md │ │ ├── __init__.py │ │ ├── comet_utils.py │ │ ├── hpo.py │ │ └── optimizer_config.json │ └── wandb │ │ ├── __init__.py │ │ └── wandb_utils.py ├── loss.py ├── metrics.py ├── plots.py ├── segment │ ├── __init__.py │ ├── augmentations.py │ ├── dataloaders.py │ ├── general.py │ ├── loss.py │ ├── metrics.py │ └── plots.py ├── torch_utils.py └── triton.py └── val.py /.dockerignore: -------------------------------------------------------------------------------- 1 | # Repo-specific DockerIgnore ------------------------------------------------------------------------------------------- 2 | .git 3 | .cache 4 | .idea 5 | runs 6 | output 7 | coco 8 | storage.googleapis.com 9 | 10 | data/samples/* 11 | **/results*.csv 12 | *.jpg 13 | 14 | # Neural Network weights ----------------------------------------------------------------------------------------------- 15 | **/*.pt 16 | **/*.pth 17 | **/*.onnx 18 | **/*.engine 19 | **/*.mlmodel 20 | **/*.torchscript 21 | **/*.torchscript.pt 22 | **/*.tflite 23 | **/*.h5 24 | **/*.pb 25 | *_saved_model/ 26 | *_web_model/ 27 | *_openvino_model/ 28 | 29 | # Below Copied From .gitignore ----------------------------------------------------------------------------------------- 30 | # Below Copied From .gitignore ----------------------------------------------------------------------------------------- 31 | 32 | 33 | # GitHub Python GitIgnore ---------------------------------------------------------------------------------------------- 34 | # Byte-compiled / optimized / DLL files 35 | __pycache__/ 36 | *.py[cod] 37 | *$py.class 38 | 39 | # C extensions 40 | *.so 41 | 42 | # Distribution / packaging 43 | .Python 44 | env/ 45 | build/ 46 | develop-eggs/ 47 | dist/ 48 | downloads/ 49 | eggs/ 50 | .eggs/ 51 | lib/ 52 | lib64/ 53 | parts/ 54 | sdist/ 55 | var/ 56 | wheels/ 57 | *.egg-info/ 58 | wandb/ 59 | .installed.cfg 60 | *.egg 61 | 62 | # PyInstaller 63 | # Usually these files are written by a python script from a template 64 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 65 | *.manifest 66 | *.spec 67 | 68 | # Installer logs 69 | pip-log.txt 70 | pip-delete-this-directory.txt 71 | 72 | # Unit test / coverage reports 73 | htmlcov/ 74 | .tox/ 75 | .coverage 76 | .coverage.* 77 | .cache 78 | nosetests.xml 79 | coverage.xml 80 | *.cover 81 | .hypothesis/ 82 | 83 | # Translations 84 | *.mo 85 | *.pot 86 | 87 | # Django stuff: 88 | *.log 89 | local_settings.py 90 | 91 | # Flask stuff: 92 | instance/ 93 | .webassets-cache 94 | 95 | # Scrapy stuff: 96 | .scrapy 97 | 98 | # Sphinx documentation 99 | docs/_build/ 100 | 101 | # PyBuilder 102 | target/ 103 | 104 | # Jupyter Notebook 105 | .ipynb_checkpoints 106 | 107 | # pyenv 108 | .python-version 109 | 110 | # celery beat schedule file 111 | celerybeat-schedule 112 | 113 | # SageMath parsed files 114 | *.sage.py 115 | 116 | # dotenv 117 | .env 118 | 119 | # virtualenv 120 | .venv* 121 | venv*/ 122 | ENV*/ 123 | 124 | # Spyder project settings 125 | .spyderproject 126 | .spyproject 127 | 128 | # Rope project settings 129 | .ropeproject 130 | 131 | # mkdocs documentation 132 | /site 133 | 134 | # mypy 135 | .mypy_cache/ 136 | 137 | 138 | # https://github.com/github/gitignore/blob/master/Global/macOS.gitignore ----------------------------------------------- 139 | 140 | # General 141 | .DS_Store 142 | .AppleDouble 143 | .LSOverride 144 | 145 | # Icon must end with two \r 146 | Icon 147 | Icon? 148 | 149 | # Thumbnails 150 | ._* 151 | 152 | # Files that might appear in the root of a volume 153 | .DocumentRevisions-V100 154 | .fseventsd 155 | .Spotlight-V100 156 | .TemporaryItems 157 | .Trashes 158 | .VolumeIcon.icns 159 | .com.apple.timemachine.donotpresent 160 | 161 | # Directories potentially created on remote AFP share 162 | .AppleDB 163 | .AppleDesktop 164 | Network Trash Folder 165 | Temporary Items 166 | .apdisk 167 | 168 | 169 | # https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore 170 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 171 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 172 | 173 | # User-specific stuff: 174 | .idea/* 175 | .idea/**/workspace.xml 176 | .idea/**/tasks.xml 177 | .idea/dictionaries 178 | .html # Bokeh Plots 179 | .pg # TensorFlow Frozen Graphs 180 | .avi # videos 181 | 182 | # Sensitive or high-churn files: 183 | .idea/**/dataSources/ 184 | .idea/**/dataSources.ids 185 | .idea/**/dataSources.local.xml 186 | .idea/**/sqlDataSources.xml 187 | .idea/**/dynamic.xml 188 | .idea/**/uiDesigner.xml 189 | 190 | # Gradle: 191 | .idea/**/gradle.xml 192 | .idea/**/libraries 193 | 194 | # CMake 195 | cmake-build-debug/ 196 | cmake-build-release/ 197 | 198 | # Mongo Explorer plugin: 199 | .idea/**/mongoSettings.xml 200 | 201 | ## File-based project format: 202 | *.iws 203 | 204 | ## Plugin-specific files: 205 | 206 | # IntelliJ 207 | out/ 208 | 209 | # mpeltonen/sbt-idea plugin 210 | .idea_modules/ 211 | 212 | # JIRA plugin 213 | atlassian-ide-plugin.xml 214 | 215 | # Cursive Clojure plugin 216 | .idea/replstate.xml 217 | 218 | # Crashlytics plugin (for Android Studio and IntelliJ) 219 | com_crashlytics_export_strings.xml 220 | crashlytics.properties 221 | crashlytics-build.properties 222 | fabric.properties 223 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # this drop notebooks from GitHub language stats 2 | *.ipynb linguist-vendored 3 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug-report.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | name: 🐛 Bug Report 4 | # title: " " 5 | description: Problems with YOLOv5 6 | labels: [bug, triage] 7 | body: 8 | - type: markdown 9 | attributes: 10 | value: | 11 | Thank you for submitting a YOLOv5 🐛 Bug Report! 12 | 13 | - type: checkboxes 14 | attributes: 15 | label: Search before asking 16 | description: > 17 | Please search the [issues](https://github.com/ultralytics/yolov5/issues) to see if a similar bug report already exists. 18 | options: 19 | - label: > 20 | I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar bug report. 21 | required: true 22 | 23 | - type: dropdown 24 | attributes: 25 | label: YOLOv5 Component 26 | description: | 27 | Please select the part of YOLOv5 where you found the bug. 28 | multiple: true 29 | options: 30 | - "Training" 31 | - "Validation" 32 | - "Detection" 33 | - "Export" 34 | - "PyTorch Hub" 35 | - "Multi-GPU" 36 | - "Evolution" 37 | - "Integrations" 38 | - "Other" 39 | validations: 40 | required: false 41 | 42 | - type: textarea 43 | attributes: 44 | label: Bug 45 | description: Provide console output with error messages and/or screenshots of the bug. 46 | placeholder: | 47 | 💡 ProTip! Include as much information as possible (screenshots, logs, tracebacks etc.) to receive the most helpful response. 48 | validations: 49 | required: true 50 | 51 | - type: textarea 52 | attributes: 53 | label: Environment 54 | description: Please specify the software and hardware you used to produce the bug. 55 | placeholder: | 56 | - YOLO: YOLOv5 🚀 v6.0-67-g60e42e1 torch 1.9.0+cu111 CUDA:0 (A100-SXM4-40GB, 40536MiB) 57 | - OS: Ubuntu 20.04 58 | - Python: 3.9.0 59 | validations: 60 | required: false 61 | 62 | - type: textarea 63 | attributes: 64 | label: Minimal Reproducible Example 65 | description: > 66 | When asking a question, people will be better able to provide help if you provide code that they can easily understand and use to **reproduce** the problem. 67 | This is referred to by community members as creating a [minimal reproducible example](https://docs.ultralytics.com/help/minimum_reproducible_example/). 68 | placeholder: | 69 | ``` 70 | # Code to reproduce your issue here 71 | ``` 72 | validations: 73 | required: false 74 | 75 | - type: textarea 76 | attributes: 77 | label: Additional 78 | description: Anything else you would like to share? 79 | 80 | - type: checkboxes 81 | attributes: 82 | label: Are you willing to submit a PR? 83 | description: > 84 | (Optional) We encourage you to submit a [Pull Request](https://github.com/ultralytics/yolov5/pulls) (PR) to help improve YOLOv5 for everyone, especially if you have a good understanding of how to implement a fix or feature. 85 | See the YOLOv5 [Contributing Guide](https://docs.ultralytics.com/help/contributing) to get started. 86 | options: 87 | - label: Yes I'd like to help by submitting a PR! 88 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | blank_issues_enabled: true 4 | contact_links: 5 | - name: 📄 Docs 6 | url: https://docs.ultralytics.com/yolov5 7 | about: View Ultralytics YOLOv5 Docs 8 | - name: 💬 Forum 9 | url: https://community.ultralytics.com/ 10 | about: Ask on Ultralytics Community Forum 11 | - name: 🎧 Discord 12 | url: https://ultralytics.com/discord 13 | about: Ask on Ultralytics Discord 14 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature-request.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | name: 🚀 Feature Request 4 | description: Suggest a YOLOv5 idea 5 | # title: " " 6 | labels: [enhancement] 7 | body: 8 | - type: markdown 9 | attributes: 10 | value: | 11 | Thank you for submitting a YOLOv5 🚀 Feature Request! 12 | 13 | - type: checkboxes 14 | attributes: 15 | label: Search before asking 16 | description: > 17 | Please search the [issues](https://github.com/ultralytics/yolov5/issues) to see if a similar feature request already exists. 18 | options: 19 | - label: > 20 | I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and found no similar feature requests. 21 | required: true 22 | 23 | - type: textarea 24 | attributes: 25 | label: Description 26 | description: A short description of your feature. 27 | placeholder: | 28 | What new feature would you like to see in YOLOv5? 29 | validations: 30 | required: true 31 | 32 | - type: textarea 33 | attributes: 34 | label: Use case 35 | description: | 36 | Describe the use case of your feature request. It will help us understand and prioritize the feature request. 37 | placeholder: | 38 | How would this feature be used, and who would use it? 39 | 40 | - type: textarea 41 | attributes: 42 | label: Additional 43 | description: Anything else you would like to share? 44 | 45 | - type: checkboxes 46 | attributes: 47 | label: Are you willing to submit a PR? 48 | description: > 49 | (Optional) We encourage you to submit a [Pull Request](https://github.com/ultralytics/yolov5/pulls) (PR) to help improve YOLOv5 for everyone, especially if you have a good understanding of how to implement a fix or feature. 50 | See the YOLOv5 [Contributing Guide](https://docs.ultralytics.com/help/contributing) to get started. 51 | options: 52 | - label: Yes I'd like to help by submitting a PR! 53 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/question.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | name: ❓ Question 4 | description: Ask a YOLOv5 question 5 | # title: " " 6 | labels: [question] 7 | body: 8 | - type: markdown 9 | attributes: 10 | value: | 11 | Thank you for asking a YOLOv5 ❓ Question! 12 | 13 | - type: checkboxes 14 | attributes: 15 | label: Search before asking 16 | description: > 17 | Please search the [issues](https://github.com/ultralytics/yolov5/issues) and [discussions](https://github.com/ultralytics/yolov5/discussions) to see if a similar question already exists. 18 | options: 19 | - label: > 20 | I have searched the YOLOv5 [issues](https://github.com/ultralytics/yolov5/issues) and [discussions](https://github.com/ultralytics/yolov5/discussions) and found no similar questions. 21 | required: true 22 | 23 | - type: textarea 24 | attributes: 25 | label: Question 26 | description: What is your question? 27 | placeholder: | 28 | 💡 ProTip! Include as much information as possible (screenshots, logs, tracebacks etc.) to receive the most helpful response. 29 | validations: 30 | required: true 31 | 32 | - type: textarea 33 | attributes: 34 | label: Additional 35 | description: Anything else you would like to share? 36 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Dependabot for package version updates 4 | # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates 5 | 6 | version: 2 7 | updates: 8 | - package-ecosystem: pip 9 | directory: "/" 10 | schedule: 11 | interval: weekly 12 | time: "04:00" 13 | open-pull-requests-limit: 10 14 | reviewers: 15 | - glenn-jocher 16 | labels: 17 | - dependencies 18 | 19 | - package-ecosystem: github-actions 20 | directory: "/.github/workflows" 21 | schedule: 22 | interval: weekly 23 | time: "04:00" 24 | open-pull-requests-limit: 5 25 | reviewers: 26 | - glenn-jocher 27 | labels: 28 | - dependencies 29 | -------------------------------------------------------------------------------- /.github/workflows/cla.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Ultralytics Contributor License Agreement (CLA) action https://docs.ultralytics.com/help/CLA 4 | # This workflow automatically requests Pull Requests (PR) authors to sign the Ultralytics CLA before PRs can be merged 5 | 6 | name: CLA Assistant 7 | on: 8 | issue_comment: 9 | types: 10 | - created 11 | pull_request_target: 12 | types: 13 | - reopened 14 | - opened 15 | - synchronize 16 | 17 | permissions: 18 | actions: write 19 | contents: write 20 | pull-requests: write 21 | statuses: write 22 | 23 | jobs: 24 | CLA: 25 | if: github.repository == 'ultralytics/yolov5' 26 | runs-on: ubuntu-latest 27 | steps: 28 | - name: CLA Assistant 29 | if: (github.event.comment.body == 'recheck' || github.event.comment.body == 'I have read the CLA Document and I sign the CLA') || github.event_name == 'pull_request_target' 30 | uses: contributor-assistant/github-action@v2.6.1 31 | env: 32 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 33 | # Must be repository secret PAT 34 | PERSONAL_ACCESS_TOKEN: ${{ secrets._GITHUB_TOKEN }} 35 | with: 36 | path-to-signatures: "signatures/version1/cla.json" 37 | path-to-document: "https://docs.ultralytics.com/help/CLA" # CLA document 38 | # Branch must not be protected 39 | branch: "cla-signatures" 40 | allowlist: dependabot[bot],github-actions,[pre-commit*,pre-commit*,bot* 41 | 42 | remote-organization-name: ultralytics 43 | remote-repository-name: cla 44 | custom-pr-sign-comment: "I have read the CLA Document and I sign the CLA" 45 | custom-allsigned-prcomment: All Contributors have signed the CLA. ✅ 46 | -------------------------------------------------------------------------------- /.github/workflows/docker.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Builds ultralytics/yolov5:latest images on DockerHub https://hub.docker.com/r/ultralytics/yolov5 4 | 5 | name: Publish Docker Images 6 | 7 | on: 8 | push: 9 | branches: [master] 10 | workflow_dispatch: 11 | 12 | jobs: 13 | docker: 14 | if: github.repository == 'ultralytics/yolov5' 15 | name: Push Docker image to Docker Hub 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: Checkout repo 19 | uses: actions/checkout@v4 20 | with: 21 | fetch-depth: 0 # copy full .git directory to access full git history in Docker images 22 | 23 | - name: Set up QEMU 24 | uses: docker/setup-qemu-action@v3 25 | 26 | - name: Set up Docker Buildx 27 | uses: docker/setup-buildx-action@v3 28 | 29 | - name: Login to Docker Hub 30 | uses: docker/login-action@v3 31 | with: 32 | username: ${{ secrets.DOCKERHUB_USERNAME }} 33 | password: ${{ secrets.DOCKERHUB_TOKEN }} 34 | 35 | - name: Build and push arm64 image 36 | uses: docker/build-push-action@v6 37 | continue-on-error: true 38 | with: 39 | context: . 40 | platforms: linux/arm64 41 | file: utils/docker/Dockerfile-arm64 42 | push: true 43 | tags: ultralytics/yolov5:latest-arm64 44 | 45 | - name: Build and push CPU image 46 | uses: docker/build-push-action@v6 47 | continue-on-error: true 48 | with: 49 | context: . 50 | file: utils/docker/Dockerfile-cpu 51 | push: true 52 | tags: ultralytics/yolov5:latest-cpu 53 | 54 | - name: Build and push GPU image 55 | uses: docker/build-push-action@v6 56 | continue-on-error: true 57 | with: 58 | context: . 59 | file: utils/docker/Dockerfile 60 | push: true 61 | tags: ultralytics/yolov5:latest 62 | -------------------------------------------------------------------------------- /.github/workflows/format.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Ultralytics Actions https://github.com/ultralytics/actions 4 | # This workflow formats code and documentation in PRs to Ultralytics standards 5 | 6 | name: Ultralytics Actions 7 | 8 | on: 9 | issues: 10 | types: [opened] 11 | pull_request: 12 | branches: [main, master] 13 | types: [opened, closed, synchronize, review_requested] 14 | 15 | permissions: 16 | contents: write # Modify code in PRs 17 | pull-requests: write # Add comments and labels to PRs 18 | issues: write # Add comments and labels to issues 19 | 20 | jobs: 21 | actions: 22 | runs-on: ubuntu-latest 23 | steps: 24 | - name: Run Ultralytics Actions 25 | uses: ultralytics/actions@main 26 | with: 27 | token: ${{ secrets._GITHUB_TOKEN || secrets.GITHUB_TOKEN }} # Auto-generated token 28 | labels: true # Auto-label issues/PRs using AI 29 | python: true # Format Python with Ruff and docformatter 30 | prettier: true # Format YAML, JSON, Markdown, CSS 31 | spelling: true # Check spelling with codespell 32 | links: false # Check broken links with Lychee 33 | summary: true # Generate AI-powered PR summaries 34 | openai_api_key: ${{ secrets.OPENAI_API_KEY }} # Powers PR summaries, labels and comments 35 | brave_api_key: ${{ secrets.BRAVE_API_KEY }} # Used for broken link resolution 36 | first_issue_response: | 37 | 👋 Hello @${{ github.actor }}, thank you for your interest in YOLOv5 🚀! Please visit our ⭐️ [Tutorials](https://docs.ultralytics.com/yolov5/) to get started, where you can find quickstart guides for simple tasks like [Custom Data Training](https://docs.ultralytics.com/yolov5/tutorials/train_custom_data/) all the way to advanced concepts like [Hyperparameter Evolution](https://docs.ultralytics.com/yolov5/tutorials/hyperparameter_evolution/). 38 | If this is a 🐛 Bug Report, please provide a **minimum reproducible example** to help us debug it. 39 | If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our [Tips for Best Training Results](https://docs.ultralytics.com/guides/model-training-tips/). 40 | ## Requirements 41 | [**Python>=3.8.0**](https://www.python.org/) with all [requirements.txt](https://github.com/ultralytics/yolov5/blob/master/requirements.txt) installed including [**PyTorch>=1.8**](https://pytorch.org/get-started/locally/). To get started: 42 | ```bash 43 | git clone https://github.com/ultralytics/yolov5 # clone 44 | cd yolov5 45 | pip install -r requirements.txt # install 46 | ``` 47 | ## Environments 48 | YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including [CUDA](https://developer.nvidia.com/cuda)/[CUDNN](https://developer.nvidia.com/cudnn), [Python](https://www.python.org/) and [PyTorch](https://pytorch.org/) preinstalled): 49 | - **Notebooks** with free GPU: Run on Gradient Open In Colab Open In Kaggle 50 | - **Google Cloud** Deep Learning VM. See [GCP Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/google_cloud_quickstart_tutorial/) 51 | - **Amazon** Deep Learning AMI. See [AWS Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/aws_quickstart_tutorial/) 52 | - **Docker Image**. See [Docker Quickstart Guide](https://docs.ultralytics.com/yolov5/environments/docker_image_quickstart_tutorial/) Docker Pulls 53 | ## Status 54 | YOLOv5 CI 55 | If this badge is green, all [YOLOv5 GitHub Actions](https://github.com/ultralytics/yolov5/actions) Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 [training](https://github.com/ultralytics/yolov5/blob/master/train.py), [validation](https://github.com/ultralytics/yolov5/blob/master/val.py), [inference](https://github.com/ultralytics/yolov5/blob/master/detect.py), [export](https://github.com/ultralytics/yolov5/blob/master/export.py) and [benchmarks](https://github.com/ultralytics/yolov5/blob/master/benchmarks.py) on macOS, Windows, and Ubuntu every 24 hours and on every commit. 56 | -------------------------------------------------------------------------------- /.github/workflows/links.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Continuous Integration (CI) GitHub Actions tests broken link checker using https://github.com/lycheeverse/lychee 4 | # Ignores the following status codes to reduce false positives: 5 | # - 403(OpenVINO, 'forbidden') 6 | # - 429(Instagram, 'too many requests') 7 | # - 500(Zenodo, 'cached') 8 | # - 502(Zenodo, 'bad gateway') 9 | # - 999(LinkedIn, 'unknown status code') 10 | 11 | name: Check Broken links 12 | 13 | on: 14 | workflow_dispatch: 15 | schedule: 16 | - cron: "0 0 * * *" # runs at 00:00 UTC every day 17 | 18 | jobs: 19 | Links: 20 | runs-on: ubuntu-latest 21 | steps: 22 | - uses: actions/checkout@v4 23 | 24 | - name: Download and install lychee 25 | run: | 26 | LYCHEE_URL=$(curl -s https://api.github.com/repos/lycheeverse/lychee/releases/latest | grep "browser_download_url" | grep "x86_64-unknown-linux-gnu.tar.gz" | cut -d '"' -f 4) 27 | curl -L $LYCHEE_URL | tar xz -C /usr/local/bin 28 | 29 | - name: Test Markdown and HTML links with retry 30 | uses: ultralytics/actions/retry@main 31 | with: 32 | timeout_minutes: 5 33 | retry_delay_seconds: 60 34 | retries: 2 35 | run: | 36 | lychee \ 37 | --scheme 'https' \ 38 | --timeout 60 \ 39 | --insecure \ 40 | --accept 403,429,500,502,999 \ 41 | --exclude-all-private \ 42 | --exclude 'https?://(www\.)?(linkedin\.com|twitter\.com|x\.com|instagram\.com|kaggle\.com|fonts\.gstatic\.com|url\.com)' \ 43 | --exclude-path '**/ci.yaml' \ 44 | --github-token ${{ secrets.GITHUB_TOKEN }} \ 45 | --header "User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.183 Safari/537.36" \ 46 | './**/*.md' \ 47 | './**/*.html' | tee -a $GITHUB_STEP_SUMMARY 48 | 49 | # Raise error if broken links found 50 | if ! grep -q "0 Errors" $GITHUB_STEP_SUMMARY; then 51 | exit 1 52 | fi 53 | 54 | - name: Test Markdown, HTML, YAML, Python and Notebook links with retry 55 | if: github.event_name == 'workflow_dispatch' 56 | uses: ultralytics/actions/retry@main 57 | with: 58 | timeout_minutes: 5 59 | retry_delay_seconds: 60 60 | retries: 2 61 | run: | 62 | lychee \ 63 | --scheme 'https' \ 64 | --timeout 60 \ 65 | --insecure \ 66 | --accept 429,999 \ 67 | --exclude-all-private \ 68 | --exclude 'https?://(www\.)?(linkedin\.com|twitter\.com|x\.com|instagram\.com|kaggle\.com|fonts\.gstatic\.com|url\.com)' \ 69 | --exclude-path '**/ci.yaml' \ 70 | --github-token ${{ secrets.GITHUB_TOKEN }} \ 71 | --header "User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.6478.183 Safari/537.36" \ 72 | './**/*.md' \ 73 | './**/*.html' \ 74 | './**/*.yml' \ 75 | './**/*.yaml' \ 76 | './**/*.py' \ 77 | './**/*.ipynb' | tee -a $GITHUB_STEP_SUMMARY 78 | 79 | # Raise error if broken links found 80 | if ! grep -q "0 Errors" $GITHUB_STEP_SUMMARY; then 81 | exit 1 82 | fi 83 | -------------------------------------------------------------------------------- /.github/workflows/merge-main-into-prs.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Automatically merges repository 'main' branch into all open PRs to keep them up-to-date 4 | # Action runs on updates to main branch so when one PR merges to main all others update 5 | 6 | name: Merge main into PRs 7 | 8 | on: 9 | workflow_dispatch: 10 | # push: 11 | # branches: 12 | # - ${{ github.event.repository.default_branch }} 13 | 14 | jobs: 15 | Merge: 16 | if: github.repository == 'ultralytics/yolov5' 17 | runs-on: ubuntu-latest 18 | steps: 19 | - name: Checkout repository 20 | uses: actions/checkout@v4 21 | with: 22 | fetch-depth: 0 23 | - uses: actions/setup-python@v5 24 | with: 25 | python-version: "3.x" 26 | cache: "pip" 27 | - name: Install requirements 28 | run: | 29 | pip install pygithub 30 | - name: Merge default branch into PRs 31 | shell: python 32 | run: | 33 | from github import Github 34 | import os 35 | 36 | g = Github(os.getenv('GITHUB_TOKEN')) 37 | repo = g.get_repo(os.getenv('GITHUB_REPOSITORY')) 38 | 39 | # Fetch the default branch name 40 | default_branch_name = repo.default_branch 41 | default_branch = repo.get_branch(default_branch_name) 42 | 43 | for pr in repo.get_pulls(state='open', sort='created'): 44 | try: 45 | # Get full names for repositories and branches 46 | base_repo_name = repo.full_name 47 | head_repo_name = pr.head.repo.full_name 48 | base_branch_name = pr.base.ref 49 | head_branch_name = pr.head.ref 50 | 51 | # Check if PR is behind the default branch 52 | comparison = repo.compare(default_branch.commit.sha, pr.head.sha) 53 | 54 | if comparison.behind_by > 0: 55 | print(f"⚠️ PR #{pr.number} ({head_repo_name}:{head_branch_name} -> {base_repo_name}:{base_branch_name}) is behind {default_branch_name} by {comparison.behind_by} commit(s).") 56 | 57 | # Attempt to update the branch 58 | try: 59 | success = pr.update_branch() 60 | assert success, "Branch update failed" 61 | print(f"✅ Successfully merged '{default_branch_name}' into PR #{pr.number} ({head_repo_name}:{head_branch_name} -> {base_repo_name}:{base_branch_name}).") 62 | except Exception as update_error: 63 | print(f"❌ Could not update PR #{pr.number} ({head_repo_name}:{head_branch_name} -> {base_repo_name}:{base_branch_name}): {update_error}") 64 | print(" This might be due to branch protection rules or insufficient permissions.") 65 | else: 66 | print(f"✅ PR #{pr.number} ({head_repo_name}:{head_branch_name} -> {base_repo_name}:{base_branch_name}) is up to date with {default_branch_name}.") 67 | except Exception as e: 68 | print(f"❌ Could not process PR #{pr.number}: {e}") 69 | 70 | env: 71 | GITHUB_TOKEN: ${{ secrets._GITHUB_TOKEN }} 72 | GITHUB_REPOSITORY: ${{ github.repository }} 73 | -------------------------------------------------------------------------------- /.github/workflows/stale.yml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | name: Close stale issues 4 | on: 5 | schedule: 6 | - cron: "0 0 * * *" # Runs at 00:00 UTC every day 7 | 8 | jobs: 9 | stale: 10 | runs-on: ubuntu-latest 11 | steps: 12 | - uses: actions/stale@v9 13 | with: 14 | repo-token: ${{ secrets.GITHUB_TOKEN }} 15 | 16 | stale-issue-message: | 17 | 👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help. 18 | 19 | For additional resources and information, please see the links below: 20 | 21 | - **Docs**: https://docs.ultralytics.com 22 | - **HUB**: https://hub.ultralytics.com 23 | - **Community**: https://community.ultralytics.com 24 | 25 | Feel free to inform us of any other **issues** you discover or **feature requests** that come to mind in the future. Pull Requests (PRs) are also always welcomed! 26 | 27 | Thank you for your contributions to YOLO 🚀 and Vision AI ⭐ 28 | 29 | stale-pr-message: | 30 | 👋 Hello there! We wanted to let you know that we've decided to close this pull request due to inactivity. We appreciate the effort you put into contributing to our project, but unfortunately, not all contributions are suitable or aligned with our product roadmap. 31 | 32 | We hope you understand our decision, and please don't let it discourage you from contributing to open source projects in the future. We value all of our community members and their contributions, and we encourage you to keep exploring new projects and ways to get involved. 33 | 34 | For additional resources and information, please see the links below: 35 | 36 | - **Docs**: https://docs.ultralytics.com 37 | - **HUB**: https://hub.ultralytics.com 38 | - **Community**: https://community.ultralytics.com 39 | 40 | Thank you for your contributions to YOLO 🚀 and Vision AI ⭐ 41 | 42 | days-before-issue-stale: 30 43 | days-before-issue-close: 10 44 | days-before-pr-stale: 90 45 | days-before-pr-close: 30 46 | exempt-issue-labels: "documentation,tutorial,TODO" 47 | operations-per-run: 300 # The maximum number of operations per run, used to control rate limiting. 48 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Repo-specific GitIgnore ---------------------------------------------------------------------------------------------- 2 | *.jpg 3 | *.jpeg 4 | *.png 5 | *.bmp 6 | *.tif 7 | *.tiff 8 | *.heic 9 | *.JPG 10 | *.JPEG 11 | *.PNG 12 | *.BMP 13 | *.TIF 14 | *.TIFF 15 | *.HEIC 16 | *.mp4 17 | *.mov 18 | *.MOV 19 | *.avi 20 | *.data 21 | *.json 22 | *.cfg 23 | !setup.cfg 24 | !cfg/yolov3*.cfg 25 | 26 | storage.googleapis.com 27 | runs/* 28 | data/* 29 | data/images/* 30 | !data/*.yaml 31 | !data/hyps 32 | !data/scripts 33 | !data/images 34 | !data/images/zidane.jpg 35 | !data/images/bus.jpg 36 | !data/*.sh 37 | 38 | results*.csv 39 | 40 | # Datasets ------------------------------------------------------------------------------------------------------------- 41 | coco/ 42 | coco128/ 43 | VOC/ 44 | 45 | # MATLAB GitIgnore ----------------------------------------------------------------------------------------------------- 46 | *.m~ 47 | *.mat 48 | !targets*.mat 49 | 50 | # Neural Network weights ----------------------------------------------------------------------------------------------- 51 | *.weights 52 | *.pt 53 | *.pb 54 | *.onnx 55 | *.engine 56 | *.mlmodel 57 | *.mlpackage 58 | *.torchscript 59 | *.tflite 60 | *.h5 61 | *_saved_model/ 62 | *_web_model/ 63 | *_openvino_model/ 64 | *_paddle_model/ 65 | darknet53.conv.74 66 | yolov3-tiny.conv.15 67 | 68 | # GitHub Python GitIgnore ---------------------------------------------------------------------------------------------- 69 | # Byte-compiled / optimized / DLL files 70 | __pycache__/ 71 | *.py[cod] 72 | *$py.class 73 | 74 | # C extensions 75 | *.so 76 | 77 | # Distribution / packaging 78 | .Python 79 | env/ 80 | build/ 81 | develop-eggs/ 82 | dist/ 83 | downloads/ 84 | eggs/ 85 | .eggs/ 86 | lib/ 87 | lib64/ 88 | parts/ 89 | sdist/ 90 | var/ 91 | wheels/ 92 | *.egg-info/ 93 | /wandb/ 94 | .installed.cfg 95 | *.egg 96 | 97 | 98 | # PyInstaller 99 | # Usually these files are written by a python script from a template 100 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 101 | *.manifest 102 | *.spec 103 | 104 | # Installer logs 105 | pip-log.txt 106 | pip-delete-this-directory.txt 107 | 108 | # Unit test / coverage reports 109 | htmlcov/ 110 | .tox/ 111 | .coverage 112 | .coverage.* 113 | .cache 114 | nosetests.xml 115 | coverage.xml 116 | *.cover 117 | .hypothesis/ 118 | 119 | # Translations 120 | *.mo 121 | *.pot 122 | 123 | # Django stuff: 124 | *.log 125 | local_settings.py 126 | 127 | # Flask stuff: 128 | instance/ 129 | .webassets-cache 130 | 131 | # Scrapy stuff: 132 | .scrapy 133 | 134 | # Sphinx documentation 135 | docs/_build/ 136 | 137 | # PyBuilder 138 | target/ 139 | 140 | # Jupyter Notebook 141 | .ipynb_checkpoints 142 | 143 | # pyenv 144 | .python-version 145 | 146 | # celery beat schedule file 147 | celerybeat-schedule 148 | 149 | # SageMath parsed files 150 | *.sage.py 151 | 152 | # dotenv 153 | .env 154 | 155 | # virtualenv 156 | .venv* 157 | venv*/ 158 | ENV*/ 159 | 160 | # Spyder project settings 161 | .spyderproject 162 | .spyproject 163 | 164 | # Rope project settings 165 | .ropeproject 166 | 167 | # mkdocs documentation 168 | /site 169 | 170 | # mypy 171 | .mypy_cache/ 172 | 173 | 174 | # https://github.com/github/gitignore/blob/master/Global/macOS.gitignore ----------------------------------------------- 175 | 176 | # General 177 | .DS_Store 178 | .AppleDouble 179 | .LSOverride 180 | 181 | # Icon must end with two \r 182 | Icon 183 | Icon? 184 | 185 | # Thumbnails 186 | ._* 187 | 188 | # Files that might appear in the root of a volume 189 | .DocumentRevisions-V100 190 | .fseventsd 191 | .Spotlight-V100 192 | .TemporaryItems 193 | .Trashes 194 | .VolumeIcon.icns 195 | .com.apple.timemachine.donotpresent 196 | 197 | # Directories potentially created on remote AFP share 198 | .AppleDB 199 | .AppleDesktop 200 | Network Trash Folder 201 | Temporary Items 202 | .apdisk 203 | 204 | 205 | # https://github.com/github/gitignore/blob/master/Global/JetBrains.gitignore 206 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 207 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 208 | 209 | # User-specific stuff: 210 | .idea/* 211 | .idea/**/workspace.xml 212 | .idea/**/tasks.xml 213 | .idea/dictionaries 214 | .html # Bokeh Plots 215 | .pg # TensorFlow Frozen Graphs 216 | .avi # videos 217 | 218 | # Sensitive or high-churn files: 219 | .idea/**/dataSources/ 220 | .idea/**/dataSources.ids 221 | .idea/**/dataSources.local.xml 222 | .idea/**/sqlDataSources.xml 223 | .idea/**/dynamic.xml 224 | .idea/**/uiDesigner.xml 225 | 226 | # Gradle: 227 | .idea/**/gradle.xml 228 | .idea/**/libraries 229 | 230 | # CMake 231 | cmake-build-debug/ 232 | cmake-build-release/ 233 | 234 | # Mongo Explorer plugin: 235 | .idea/**/mongoSettings.xml 236 | 237 | ## File-based project format: 238 | *.iws 239 | 240 | ## Plugin-specific files: 241 | 242 | # IntelliJ 243 | out/ 244 | 245 | # mpeltonen/sbt-idea plugin 246 | .idea_modules/ 247 | 248 | # JIRA plugin 249 | atlassian-ide-plugin.xml 250 | 251 | # Cursive Clojure plugin 252 | .idea/replstate.xml 253 | 254 | # Crashlytics plugin (for Android Studio and IntelliJ) 255 | com_crashlytics_export_strings.xml 256 | crashlytics.properties 257 | crashlytics-build.properties 258 | fabric.properties 259 | -------------------------------------------------------------------------------- /CITATION.cff: -------------------------------------------------------------------------------- 1 | cff-version: 1.2.0 2 | preferred-citation: 3 | type: software 4 | message: If you use YOLOv5, please cite it as below. 5 | authors: 6 | - family-names: Jocher 7 | given-names: Glenn 8 | orcid: "https://orcid.org/0000-0001-5950-6979" 9 | title: "YOLOv5 by Ultralytics" 10 | version: 7.0 11 | doi: 10.5281/zenodo.3908559 12 | date-released: 2020-5-29 13 | license: AGPL-3.0 14 | url: "https://github.com/ultralytics/yolov5" 15 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | Ultralytics logo 2 | 3 | # Contributing to YOLO 🚀 4 | 5 | We value your input and are committed to making contributing to YOLO as easy and transparent as possible. Whether you're: 6 | 7 | - Reporting a bug 8 | - Discussing the current state of the codebase 9 | - Submitting a fix 10 | - Proposing a new feature 11 | - Interested in becoming a maintainer 12 | 13 | Ultralytics YOLO thrives thanks to the collective efforts of our community. Every improvement you contribute helps push the boundaries of what's possible in AI! 😃 14 | 15 | ## 🛠️ Submitting a Pull Request (PR) 16 | 17 | Submitting a PR is straightforward! Here’s an example showing how to update `requirements.txt` in four simple steps: 18 | 19 | ### 1. Select the File to Update 20 | 21 | Click on `requirements.txt` in the GitHub repository. 22 | 23 |

PR_step1

24 | 25 | ### 2. Click 'Edit this file' 26 | 27 | Find the 'Edit this file' button in the top-right corner. 28 | 29 |

PR_step2

30 | 31 | ### 3. Make Your Changes 32 | 33 | For example, update the `matplotlib` version from `3.2.2` to `3.3`. 34 | 35 |

PR_step3

36 | 37 | ### 4. Preview Changes and Submit Your PR 38 | 39 | Click the **Preview changes** tab to review your updates. At the bottom, select 'Create a new branch for this commit', give your branch a descriptive name like `fix/matplotlib_version`, and click the green **Propose changes** button. Your PR is now submitted for review! 😃 40 | 41 |

PR_step4

42 | 43 | ### PR Best Practices 44 | 45 | To ensure your work is integrated smoothly, please: 46 | 47 | - ✅ Make sure your PR is **up-to-date** with the `ultralytics/yolov5` `master` branch. If your branch is behind, update it using the 'Update branch' button or by running `git pull` and `git merge master` locally. 48 | 49 |

Screenshot 2022-08-29 at 22 47 15

50 | 51 | - ✅ Ensure all YOLO Continuous Integration (CI) **checks are passing**. 52 | 53 |

Screenshot 2022-08-29 at 22 47 03

54 | 55 | - ✅ Limit your changes to the **minimum** required for your bug fix or feature. 56 | _"It is not daily increase but daily decrease, hack away the unessential. The closer to the source, the less wastage there is."_ — Bruce Lee 57 | 58 | ## 🐛 Submitting a Bug Report 59 | 60 | If you encounter an issue with YOLO, please submit a bug report! 61 | 62 | To help us investigate, we need to be able to reproduce the problem. Follow these guidelines to provide what we need to get started: 63 | 64 | When asking a question or reporting a bug, you'll get better help if you provide **code** that others can easily understand and use to **reproduce** the issue. This is known as a [minimum reproducible example](https://docs.ultralytics.com/help/minimum-reproducible-example/). Your code should be: 65 | 66 | - ✅ **Minimal** – Use as little code as possible that still produces the issue 67 | - ✅ **Complete** – Include all parts needed for someone else to reproduce the problem 68 | - ✅ **Reproducible** – Test your code to ensure it actually reproduces the issue 69 | 70 | Additionally, for [Ultralytics](https://www.ultralytics.com/) to assist you, your code should be: 71 | 72 | - ✅ **Current** – Ensure your code is up-to-date with the latest [master branch](https://github.com/ultralytics/yolov5/tree/master). Use `git pull` or `git clone` to get the latest version and confirm your issue hasn't already been fixed. 73 | - ✅ **Unmodified** – The problem must be reproducible without any custom modifications to the repository. [Ultralytics](https://www.ultralytics.com/) does not provide support for custom code ⚠️. 74 | 75 | If your issue meets these criteria, please close your current issue and open a new one using the 🐛 **Bug Report** [template](https://github.com/ultralytics/yolov5/issues/new/choose), including your [minimum reproducible example](https://docs.ultralytics.com/help/minimum-reproducible-example/) to help us diagnose your problem. 76 | 77 | ## 📄 License 78 | 79 | By contributing, you agree that your contributions will be licensed under the [AGPL-3.0 license](https://choosealicense.com/licenses/agpl-3.0/). 80 | 81 | --- 82 | 83 | For more details on contributing, check out the [Ultralytics open-source contributing guide](https://docs.ultralytics.com/help/contributing/), and explore our [Ultralytics blog](https://www.ultralytics.com/blog) for community highlights and best practices. 84 | 85 | We welcome your contributions—thank you for helping make Ultralytics YOLO better! 🚀 86 | 87 | [![Ultralytics open-source contributors](https://raw.githubusercontent.com/ultralytics/assets/main/im/image-contributors.png)](https://github.com/ultralytics/ultralytics/graphs/contributors) 88 | -------------------------------------------------------------------------------- /data/Argoverse.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Argoverse-HD dataset (ring-front-center camera) http://www.cs.cmu.edu/~mengtial/proj/streaming/ by Argo AI 4 | # Example usage: python train.py --data Argoverse.yaml 5 | # parent 6 | # ├── yolov5 7 | # └── datasets 8 | # └── Argoverse ← downloads here (31.3 GB) 9 | 10 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 11 | path: ../datasets/Argoverse # dataset root dir 12 | train: Argoverse-1.1/images/train/ # train images (relative to 'path') 39384 images 13 | val: Argoverse-1.1/images/val/ # val images (relative to 'path') 15062 images 14 | test: Argoverse-1.1/images/test/ # test images (optional) https://eval.ai/web/challenges/challenge-page/800/overview 15 | 16 | # Classes 17 | names: 18 | 0: person 19 | 1: bicycle 20 | 2: car 21 | 3: motorcycle 22 | 4: bus 23 | 5: truck 24 | 6: traffic_light 25 | 7: stop_sign 26 | 27 | # Download script/URL (optional) --------------------------------------------------------------------------------------- 28 | download: | 29 | import json 30 | 31 | from tqdm import tqdm 32 | from utils.general import download, Path 33 | 34 | 35 | def argoverse2yolo(set): 36 | labels = {} 37 | a = json.load(open(set, "rb")) 38 | for annot in tqdm(a['annotations'], desc=f"Converting {set} to YOLOv5 format..."): 39 | img_id = annot['image_id'] 40 | img_name = a['images'][img_id]['name'] 41 | img_label_name = f'{img_name[:-3]}txt' 42 | 43 | cls = annot['category_id'] # instance class id 44 | x_center, y_center, width, height = annot['bbox'] 45 | x_center = (x_center + width / 2) / 1920.0 # offset and scale 46 | y_center = (y_center + height / 2) / 1200.0 # offset and scale 47 | width /= 1920.0 # scale 48 | height /= 1200.0 # scale 49 | 50 | img_dir = set.parents[2] / 'Argoverse-1.1' / 'labels' / a['seq_dirs'][a['images'][annot['image_id']]['sid']] 51 | if not img_dir.exists(): 52 | img_dir.mkdir(parents=True, exist_ok=True) 53 | 54 | k = str(img_dir / img_label_name) 55 | if k not in labels: 56 | labels[k] = [] 57 | labels[k].append(f"{cls} {x_center} {y_center} {width} {height}\n") 58 | 59 | for k in labels: 60 | with open(k, "w") as f: 61 | f.writelines(labels[k]) 62 | 63 | 64 | # Download 65 | dir = Path(yaml['path']) # dataset root dir 66 | urls = ['https://argoverse-hd.s3.us-east-2.amazonaws.com/Argoverse-HD-Full.zip'] 67 | download(urls, dir=dir, delete=False) 68 | 69 | # Convert 70 | annotations_dir = 'Argoverse-HD/annotations/' 71 | (dir / 'Argoverse-1.1' / 'tracking').rename(dir / 'Argoverse-1.1' / 'images') # rename 'tracking' to 'images' 72 | for d in "train.json", "val.json": 73 | argoverse2yolo(dir / annotations_dir / d) # convert VisDrone annotations to YOLO labels 74 | -------------------------------------------------------------------------------- /data/GlobalWheat2020.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Global Wheat 2020 dataset http://www.global-wheat.com/ by University of Saskatchewan 4 | # Example usage: python train.py --data GlobalWheat2020.yaml 5 | # parent 6 | # ├── yolov5 7 | # └── datasets 8 | # └── GlobalWheat2020 ← downloads here (7.0 GB) 9 | 10 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 11 | path: ../datasets/GlobalWheat2020 # dataset root dir 12 | train: # train images (relative to 'path') 3422 images 13 | - images/arvalis_1 14 | - images/arvalis_2 15 | - images/arvalis_3 16 | - images/ethz_1 17 | - images/rres_1 18 | - images/inrae_1 19 | - images/usask_1 20 | val: # val images (relative to 'path') 748 images (WARNING: train set contains ethz_1) 21 | - images/ethz_1 22 | test: # test images (optional) 1276 images 23 | - images/utokyo_1 24 | - images/utokyo_2 25 | - images/nau_1 26 | - images/uq_1 27 | 28 | # Classes 29 | names: 30 | 0: wheat_head 31 | 32 | # Download script/URL (optional) --------------------------------------------------------------------------------------- 33 | download: | 34 | from utils.general import download, Path 35 | 36 | 37 | # Download 38 | dir = Path(yaml['path']) # dataset root dir 39 | urls = ['https://zenodo.org/record/4298502/files/global-wheat-codalab-official.zip', 40 | 'https://github.com/ultralytics/assets/releases/download/v0.0.0/GlobalWheat2020_labels.zip'] 41 | download(urls, dir=dir) 42 | 43 | # Make Directories 44 | for p in 'annotations', 'images', 'labels': 45 | (dir / p).mkdir(parents=True, exist_ok=True) 46 | 47 | # Move 48 | for p in 'arvalis_1', 'arvalis_2', 'arvalis_3', 'ethz_1', 'rres_1', 'inrae_1', 'usask_1', \ 49 | 'utokyo_1', 'utokyo_2', 'nau_1', 'uq_1': 50 | (dir / p).rename(dir / 'images' / p) # move to /images 51 | f = (dir / p).with_suffix('.json') # json file 52 | if f.exists(): 53 | f.rename((dir / 'annotations' / p).with_suffix('.json')) # move to /annotations 54 | -------------------------------------------------------------------------------- /data/ImageNet10.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # ImageNet-1k dataset https://www.image-net.org/index.php by Stanford University 4 | # Simplified class names from https://github.com/anishathalye/imagenet-simple-labels 5 | # Example usage: python classify/train.py --data imagenet 6 | # parent 7 | # ├── yolov5 8 | # └── datasets 9 | # └── imagenet10 ← downloads here 10 | 11 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 12 | path: ../datasets/imagenet10 # dataset root dir 13 | train: train # train images (relative to 'path') 1281167 images 14 | val: val # val images (relative to 'path') 50000 images 15 | test: # test images (optional) 16 | 17 | # Classes 18 | names: 19 | 0: tench 20 | 1: goldfish 21 | 2: great white shark 22 | 3: tiger shark 23 | 4: hammerhead shark 24 | 5: electric ray 25 | 6: stingray 26 | 7: cock 27 | 8: hen 28 | 9: ostrich 29 | 30 | # Download script/URL (optional) 31 | download: data/scripts/get_imagenet10.sh 32 | -------------------------------------------------------------------------------- /data/ImageNet100.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # ImageNet-1k dataset https://www.image-net.org/index.php by Stanford University 4 | # Simplified class names from https://github.com/anishathalye/imagenet-simple-labels 5 | # Example usage: python classify/train.py --data imagenet 6 | # parent 7 | # ├── yolov5 8 | # └── datasets 9 | # └── imagenet100 ← downloads here 10 | 11 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 12 | path: ../datasets/imagenet100 # dataset root dir 13 | train: train # train images (relative to 'path') 1281167 images 14 | val: val # val images (relative to 'path') 50000 images 15 | test: # test images (optional) 16 | 17 | # Classes 18 | names: 19 | 0: tench 20 | 1: goldfish 21 | 2: great white shark 22 | 3: tiger shark 23 | 4: hammerhead shark 24 | 5: electric ray 25 | 6: stingray 26 | 7: cock 27 | 8: hen 28 | 9: ostrich 29 | 10: brambling 30 | 11: goldfinch 31 | 12: house finch 32 | 13: junco 33 | 14: indigo bunting 34 | 15: American robin 35 | 16: bulbul 36 | 17: jay 37 | 18: magpie 38 | 19: chickadee 39 | 20: American dipper 40 | 21: kite 41 | 22: bald eagle 42 | 23: vulture 43 | 24: great grey owl 44 | 25: fire salamander 45 | 26: smooth newt 46 | 27: newt 47 | 28: spotted salamander 48 | 29: axolotl 49 | 30: American bullfrog 50 | 31: tree frog 51 | 32: tailed frog 52 | 33: loggerhead sea turtle 53 | 34: leatherback sea turtle 54 | 35: mud turtle 55 | 36: terrapin 56 | 37: box turtle 57 | 38: banded gecko 58 | 39: green iguana 59 | 40: Carolina anole 60 | 41: desert grassland whiptail lizard 61 | 42: agama 62 | 43: frilled-necked lizard 63 | 44: alligator lizard 64 | 45: Gila monster 65 | 46: European green lizard 66 | 47: chameleon 67 | 48: Komodo dragon 68 | 49: Nile crocodile 69 | 50: American alligator 70 | 51: triceratops 71 | 52: worm snake 72 | 53: ring-necked snake 73 | 54: eastern hog-nosed snake 74 | 55: smooth green snake 75 | 56: kingsnake 76 | 57: garter snake 77 | 58: water snake 78 | 59: vine snake 79 | 60: night snake 80 | 61: boa constrictor 81 | 62: African rock python 82 | 63: Indian cobra 83 | 64: green mamba 84 | 65: sea snake 85 | 66: Saharan horned viper 86 | 67: eastern diamondback rattlesnake 87 | 68: sidewinder 88 | 69: trilobite 89 | 70: harvestman 90 | 71: scorpion 91 | 72: yellow garden spider 92 | 73: barn spider 93 | 74: European garden spider 94 | 75: southern black widow 95 | 76: tarantula 96 | 77: wolf spider 97 | 78: tick 98 | 79: centipede 99 | 80: black grouse 100 | 81: ptarmigan 101 | 82: ruffed grouse 102 | 83: prairie grouse 103 | 84: peacock 104 | 85: quail 105 | 86: partridge 106 | 87: grey parrot 107 | 88: macaw 108 | 89: sulphur-crested cockatoo 109 | 90: lorikeet 110 | 91: coucal 111 | 92: bee eater 112 | 93: hornbill 113 | 94: hummingbird 114 | 95: jacamar 115 | 96: toucan 116 | 97: duck 117 | 98: red-breasted merganser 118 | 99: goose 119 | # Download script/URL (optional) 120 | download: data/scripts/get_imagenet100.sh 121 | -------------------------------------------------------------------------------- /data/SKU-110K.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # SKU-110K retail items dataset https://github.com/eg4000/SKU110K_CVPR19 by Trax Retail 4 | # Example usage: python train.py --data SKU-110K.yaml 5 | # parent 6 | # ├── yolov5 7 | # └── datasets 8 | # └── SKU-110K ← downloads here (13.6 GB) 9 | 10 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 11 | path: ../datasets/SKU-110K # dataset root dir 12 | train: train.txt # train images (relative to 'path') 8219 images 13 | val: val.txt # val images (relative to 'path') 588 images 14 | test: test.txt # test images (optional) 2936 images 15 | 16 | # Classes 17 | names: 18 | 0: object 19 | 20 | # Download script/URL (optional) --------------------------------------------------------------------------------------- 21 | download: | 22 | import shutil 23 | from tqdm import tqdm 24 | from utils.general import np, pd, Path, download, xyxy2xywh 25 | 26 | 27 | # Download 28 | dir = Path(yaml['path']) # dataset root dir 29 | parent = Path(dir.parent) # download dir 30 | urls = ['http://trax-geometry.s3.amazonaws.com/cvpr_challenge/SKU110K_fixed.tar.gz'] 31 | download(urls, dir=parent, delete=False) 32 | 33 | # Rename directories 34 | if dir.exists(): 35 | shutil.rmtree(dir) 36 | (parent / 'SKU110K_fixed').rename(dir) # rename dir 37 | (dir / 'labels').mkdir(parents=True, exist_ok=True) # create labels dir 38 | 39 | # Convert labels 40 | names = 'image', 'x1', 'y1', 'x2', 'y2', 'class', 'image_width', 'image_height' # column names 41 | for d in 'annotations_train.csv', 'annotations_val.csv', 'annotations_test.csv': 42 | x = pd.read_csv(dir / 'annotations' / d, names=names).values # annotations 43 | images, unique_images = x[:, 0], np.unique(x[:, 0]) 44 | with open((dir / d).with_suffix('.txt').__str__().replace('annotations_', ''), 'w') as f: 45 | f.writelines(f'./images/{s}\n' for s in unique_images) 46 | for im in tqdm(unique_images, desc=f'Converting {dir / d}'): 47 | cls = 0 # single-class dataset 48 | with open((dir / 'labels' / im).with_suffix('.txt'), 'a') as f: 49 | for r in x[images == im]: 50 | w, h = r[6], r[7] # image width, height 51 | xywh = xyxy2xywh(np.array([[r[1] / w, r[2] / h, r[3] / w, r[4] / h]]))[0] # instance 52 | f.write(f"{cls} {xywh[0]:.5f} {xywh[1]:.5f} {xywh[2]:.5f} {xywh[3]:.5f}\n") # write label 53 | -------------------------------------------------------------------------------- /data/VOC.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC by University of Oxford 4 | # Example usage: python train.py --data VOC.yaml 5 | # parent 6 | # ├── yolov5 7 | # └── datasets 8 | # └── VOC ← downloads here (2.8 GB) 9 | 10 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 11 | path: ../datasets/VOC 12 | train: # train images (relative to 'path') 16551 images 13 | - images/train2012 14 | - images/train2007 15 | - images/val2012 16 | - images/val2007 17 | val: # val images (relative to 'path') 4952 images 18 | - images/test2007 19 | test: # test images (optional) 20 | - images/test2007 21 | 22 | # Classes 23 | names: 24 | 0: aeroplane 25 | 1: bicycle 26 | 2: bird 27 | 3: boat 28 | 4: bottle 29 | 5: bus 30 | 6: car 31 | 7: cat 32 | 8: chair 33 | 9: cow 34 | 10: diningtable 35 | 11: dog 36 | 12: horse 37 | 13: motorbike 38 | 14: person 39 | 15: pottedplant 40 | 16: sheep 41 | 17: sofa 42 | 18: train 43 | 19: tvmonitor 44 | 45 | # Download script/URL (optional) --------------------------------------------------------------------------------------- 46 | download: | 47 | import xml.etree.ElementTree as ET 48 | 49 | from tqdm import tqdm 50 | from utils.general import download, Path 51 | 52 | 53 | def convert_label(path, lb_path, year, image_id): 54 | def convert_box(size, box): 55 | dw, dh = 1. / size[0], 1. / size[1] 56 | x, y, w, h = (box[0] + box[1]) / 2.0 - 1, (box[2] + box[3]) / 2.0 - 1, box[1] - box[0], box[3] - box[2] 57 | return x * dw, y * dh, w * dw, h * dh 58 | 59 | in_file = open(path / f'VOC{year}/Annotations/{image_id}.xml') 60 | out_file = open(lb_path, 'w') 61 | tree = ET.parse(in_file) 62 | root = tree.getroot() 63 | size = root.find('size') 64 | w = int(size.find('width').text) 65 | h = int(size.find('height').text) 66 | 67 | names = list(yaml['names'].values()) # names list 68 | for obj in root.iter('object'): 69 | cls = obj.find('name').text 70 | if cls in names and int(obj.find('difficult').text) != 1: 71 | xmlbox = obj.find('bndbox') 72 | bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')]) 73 | cls_id = names.index(cls) # class id 74 | out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n') 75 | 76 | 77 | # Download 78 | dir = Path(yaml['path']) # dataset root dir 79 | url = 'https://github.com/ultralytics/assets/releases/download/v0.0.0/' 80 | urls = [f'{url}VOCtrainval_06-Nov-2007.zip', # 446MB, 5012 images 81 | f'{url}VOCtest_06-Nov-2007.zip', # 438MB, 4953 images 82 | f'{url}VOCtrainval_11-May-2012.zip'] # 1.95GB, 17126 images 83 | download(urls, dir=dir / 'images', delete=False, curl=True, threads=3) 84 | 85 | # Convert 86 | path = dir / 'images/VOCdevkit' 87 | for year, image_set in ('2012', 'train'), ('2012', 'val'), ('2007', 'train'), ('2007', 'val'), ('2007', 'test'): 88 | imgs_path = dir / 'images' / f'{image_set}{year}' 89 | lbs_path = dir / 'labels' / f'{image_set}{year}' 90 | imgs_path.mkdir(exist_ok=True, parents=True) 91 | lbs_path.mkdir(exist_ok=True, parents=True) 92 | 93 | with open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt') as f: 94 | image_ids = f.read().strip().split() 95 | for id in tqdm(image_ids, desc=f'{image_set}{year}'): 96 | f = path / f'VOC{year}/JPEGImages/{id}.jpg' # old img path 97 | lb_path = (lbs_path / f.name).with_suffix('.txt') # new label path 98 | f.rename(imgs_path / f.name) # move image 99 | convert_label(path, lb_path, year, id) # convert labels to YOLO format 100 | -------------------------------------------------------------------------------- /data/VisDrone.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # VisDrone2019-DET dataset https://github.com/VisDrone/VisDrone-Dataset by Tianjin University 4 | # Example usage: python train.py --data VisDrone.yaml 5 | # parent 6 | # ├── yolov5 7 | # └── datasets 8 | # └── VisDrone ← downloads here (2.3 GB) 9 | 10 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 11 | path: ../datasets/VisDrone # dataset root dir 12 | train: VisDrone2019-DET-train/images # train images (relative to 'path') 6471 images 13 | val: VisDrone2019-DET-val/images # val images (relative to 'path') 548 images 14 | test: VisDrone2019-DET-test-dev/images # test images (optional) 1610 images 15 | 16 | # Classes 17 | names: 18 | 0: pedestrian 19 | 1: people 20 | 2: bicycle 21 | 3: car 22 | 4: van 23 | 5: truck 24 | 6: tricycle 25 | 7: awning-tricycle 26 | 8: bus 27 | 9: motor 28 | 29 | # Download script/URL (optional) --------------------------------------------------------------------------------------- 30 | download: | 31 | from utils.general import download, os, Path 32 | 33 | def visdrone2yolo(dir): 34 | from PIL import Image 35 | from tqdm import tqdm 36 | 37 | def convert_box(size, box): 38 | # Convert VisDrone box to YOLO xywh box 39 | dw = 1. / size[0] 40 | dh = 1. / size[1] 41 | return (box[0] + box[2] / 2) * dw, (box[1] + box[3] / 2) * dh, box[2] * dw, box[3] * dh 42 | 43 | (dir / 'labels').mkdir(parents=True, exist_ok=True) # make labels directory 44 | pbar = tqdm((dir / 'annotations').glob('*.txt'), desc=f'Converting {dir}') 45 | for f in pbar: 46 | img_size = Image.open((dir / 'images' / f.name).with_suffix('.jpg')).size 47 | lines = [] 48 | with open(f, 'r') as file: # read annotation.txt 49 | for row in [x.split(',') for x in file.read().strip().splitlines()]: 50 | if row[4] == '0': # VisDrone 'ignored regions' class 0 51 | continue 52 | cls = int(row[5]) - 1 53 | box = convert_box(img_size, tuple(map(int, row[:4]))) 54 | lines.append(f"{cls} {' '.join(f'{x:.6f}' for x in box)}\n") 55 | with open(str(f).replace(os.sep + 'annotations' + os.sep, os.sep + 'labels' + os.sep), 'w') as fl: 56 | fl.writelines(lines) # write label.txt 57 | 58 | 59 | # Download 60 | dir = Path(yaml['path']) # dataset root dir 61 | urls = ['https://github.com/ultralytics/assets/releases/download/v0.0.0/VisDrone2019-DET-train.zip', 62 | 'https://github.com/ultralytics/assets/releases/download/v0.0.0/VisDrone2019-DET-val.zip', 63 | 'https://github.com/ultralytics/assets/releases/download/v0.0.0/VisDrone2019-DET-test-dev.zip', 64 | 'https://github.com/ultralytics/assets/releases/download/v0.0.0/VisDrone2019-DET-test-challenge.zip'] 65 | download(urls, dir=dir, curl=True, threads=4) 66 | 67 | # Convert 68 | for d in 'VisDrone2019-DET-train', 'VisDrone2019-DET-val', 'VisDrone2019-DET-test-dev': 69 | visdrone2yolo(dir / d) # convert VisDrone annotations to YOLO labels 70 | -------------------------------------------------------------------------------- /data/coco.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # COCO 2017 dataset http://cocodataset.org by Microsoft 4 | # Example usage: python train.py --data coco.yaml 5 | # parent 6 | # ├── yolov5 7 | # └── datasets 8 | # └── coco ← downloads here (20.1 GB) 9 | 10 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 11 | path: ../datasets/coco # dataset root dir 12 | train: train2017.txt # train images (relative to 'path') 118287 images 13 | val: val2017.txt # val images (relative to 'path') 5000 images 14 | test: test-dev2017.txt # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794 15 | 16 | # Classes 17 | names: 18 | 0: person 19 | 1: bicycle 20 | 2: car 21 | 3: motorcycle 22 | 4: airplane 23 | 5: bus 24 | 6: train 25 | 7: truck 26 | 8: boat 27 | 9: traffic light 28 | 10: fire hydrant 29 | 11: stop sign 30 | 12: parking meter 31 | 13: bench 32 | 14: bird 33 | 15: cat 34 | 16: dog 35 | 17: horse 36 | 18: sheep 37 | 19: cow 38 | 20: elephant 39 | 21: bear 40 | 22: zebra 41 | 23: giraffe 42 | 24: backpack 43 | 25: umbrella 44 | 26: handbag 45 | 27: tie 46 | 28: suitcase 47 | 29: frisbee 48 | 30: skis 49 | 31: snowboard 50 | 32: sports ball 51 | 33: kite 52 | 34: baseball bat 53 | 35: baseball glove 54 | 36: skateboard 55 | 37: surfboard 56 | 38: tennis racket 57 | 39: bottle 58 | 40: wine glass 59 | 41: cup 60 | 42: fork 61 | 43: knife 62 | 44: spoon 63 | 45: bowl 64 | 46: banana 65 | 47: apple 66 | 48: sandwich 67 | 49: orange 68 | 50: broccoli 69 | 51: carrot 70 | 52: hot dog 71 | 53: pizza 72 | 54: donut 73 | 55: cake 74 | 56: chair 75 | 57: couch 76 | 58: potted plant 77 | 59: bed 78 | 60: dining table 79 | 61: toilet 80 | 62: tv 81 | 63: laptop 82 | 64: mouse 83 | 65: remote 84 | 66: keyboard 85 | 67: cell phone 86 | 68: microwave 87 | 69: oven 88 | 70: toaster 89 | 71: sink 90 | 72: refrigerator 91 | 73: book 92 | 74: clock 93 | 75: vase 94 | 76: scissors 95 | 77: teddy bear 96 | 78: hair drier 97 | 79: toothbrush 98 | 99 | # Download script/URL (optional) 100 | download: | 101 | from utils.general import download, Path 102 | 103 | 104 | # Download labels 105 | segments = False # segment or box labels 106 | dir = Path(yaml['path']) # dataset root dir 107 | url = 'https://github.com/ultralytics/assets/releases/download/v0.0.0/' 108 | urls = [url + ('coco2017labels-segments.zip' if segments else 'coco2017labels.zip')] # labels 109 | download(urls, dir=dir.parent) 110 | 111 | # Download data 112 | urls = ['http://images.cocodataset.org/zips/train2017.zip', # 19G, 118k images 113 | 'http://images.cocodataset.org/zips/val2017.zip', # 1G, 5k images 114 | 'http://images.cocodataset.org/zips/test2017.zip'] # 7G, 41k images (optional) 115 | download(urls, dir=dir / 'images', threads=3) 116 | -------------------------------------------------------------------------------- /data/coco128-seg.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # COCO128-seg dataset https://www.kaggle.com/datasets/ultralytics/coco128 (first 128 images from COCO train2017) by Ultralytics 4 | # Example usage: python train.py --data coco128.yaml 5 | # parent 6 | # ├── yolov5 7 | # └── datasets 8 | # └── coco128-seg ← downloads here (7 MB) 9 | 10 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 11 | path: ../datasets/coco128-seg # dataset root dir 12 | train: images/train2017 # train images (relative to 'path') 128 images 13 | val: images/train2017 # val images (relative to 'path') 128 images 14 | test: # test images (optional) 15 | 16 | # Classes 17 | names: 18 | 0: person 19 | 1: bicycle 20 | 2: car 21 | 3: motorcycle 22 | 4: airplane 23 | 5: bus 24 | 6: train 25 | 7: truck 26 | 8: boat 27 | 9: traffic light 28 | 10: fire hydrant 29 | 11: stop sign 30 | 12: parking meter 31 | 13: bench 32 | 14: bird 33 | 15: cat 34 | 16: dog 35 | 17: horse 36 | 18: sheep 37 | 19: cow 38 | 20: elephant 39 | 21: bear 40 | 22: zebra 41 | 23: giraffe 42 | 24: backpack 43 | 25: umbrella 44 | 26: handbag 45 | 27: tie 46 | 28: suitcase 47 | 29: frisbee 48 | 30: skis 49 | 31: snowboard 50 | 32: sports ball 51 | 33: kite 52 | 34: baseball bat 53 | 35: baseball glove 54 | 36: skateboard 55 | 37: surfboard 56 | 38: tennis racket 57 | 39: bottle 58 | 40: wine glass 59 | 41: cup 60 | 42: fork 61 | 43: knife 62 | 44: spoon 63 | 45: bowl 64 | 46: banana 65 | 47: apple 66 | 48: sandwich 67 | 49: orange 68 | 50: broccoli 69 | 51: carrot 70 | 52: hot dog 71 | 53: pizza 72 | 54: donut 73 | 55: cake 74 | 56: chair 75 | 57: couch 76 | 58: potted plant 77 | 59: bed 78 | 60: dining table 79 | 61: toilet 80 | 62: tv 81 | 63: laptop 82 | 64: mouse 83 | 65: remote 84 | 66: keyboard 85 | 67: cell phone 86 | 68: microwave 87 | 69: oven 88 | 70: toaster 89 | 71: sink 90 | 72: refrigerator 91 | 73: book 92 | 74: clock 93 | 75: vase 94 | 76: scissors 95 | 77: teddy bear 96 | 78: hair drier 97 | 79: toothbrush 98 | 99 | # Download script/URL (optional) 100 | download: https://github.com/ultralytics/assets/releases/download/v0.0.0/coco128-seg.zip 101 | -------------------------------------------------------------------------------- /data/coco128.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # COCO128 dataset https://www.kaggle.com/datasets/ultralytics/coco128 (first 128 images from COCO train2017) by Ultralytics 4 | # Example usage: python train.py --data coco128.yaml 5 | # parent 6 | # ├── yolov5 7 | # └── datasets 8 | # └── coco128 ← downloads here (7 MB) 9 | 10 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 11 | path: ../datasets/coco128 # dataset root dir 12 | train: images/train2017 # train images (relative to 'path') 128 images 13 | val: images/train2017 # val images (relative to 'path') 128 images 14 | test: # test images (optional) 15 | 16 | # Classes 17 | names: 18 | 0: person 19 | 1: bicycle 20 | 2: car 21 | 3: motorcycle 22 | 4: airplane 23 | 5: bus 24 | 6: train 25 | 7: truck 26 | 8: boat 27 | 9: traffic light 28 | 10: fire hydrant 29 | 11: stop sign 30 | 12: parking meter 31 | 13: bench 32 | 14: bird 33 | 15: cat 34 | 16: dog 35 | 17: horse 36 | 18: sheep 37 | 19: cow 38 | 20: elephant 39 | 21: bear 40 | 22: zebra 41 | 23: giraffe 42 | 24: backpack 43 | 25: umbrella 44 | 26: handbag 45 | 27: tie 46 | 28: suitcase 47 | 29: frisbee 48 | 30: skis 49 | 31: snowboard 50 | 32: sports ball 51 | 33: kite 52 | 34: baseball bat 53 | 35: baseball glove 54 | 36: skateboard 55 | 37: surfboard 56 | 38: tennis racket 57 | 39: bottle 58 | 40: wine glass 59 | 41: cup 60 | 42: fork 61 | 43: knife 62 | 44: spoon 63 | 45: bowl 64 | 46: banana 65 | 47: apple 66 | 48: sandwich 67 | 49: orange 68 | 50: broccoli 69 | 51: carrot 70 | 52: hot dog 71 | 53: pizza 72 | 54: donut 73 | 55: cake 74 | 56: chair 75 | 57: couch 76 | 58: potted plant 77 | 59: bed 78 | 60: dining table 79 | 61: toilet 80 | 62: tv 81 | 63: laptop 82 | 64: mouse 83 | 65: remote 84 | 66: keyboard 85 | 67: cell phone 86 | 68: microwave 87 | 69: oven 88 | 70: toaster 89 | 71: sink 90 | 72: refrigerator 91 | 73: book 92 | 74: clock 93 | 75: vase 94 | 76: scissors 95 | 77: teddy bear 96 | 78: hair drier 97 | 79: toothbrush 98 | 99 | # Download script/URL (optional) 100 | download: https://github.com/ultralytics/assets/releases/download/v0.0.0/coco128.zip 101 | -------------------------------------------------------------------------------- /data/hyps/hyp.Objects365.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Hyperparameters for Objects365 training 4 | # python train.py --weights yolov5m.pt --data Objects365.yaml --evolve 5 | # See Hyperparameter Evolution tutorial for details https://github.com/ultralytics/yolov5#tutorials 6 | 7 | lr0: 0.00258 8 | lrf: 0.17 9 | momentum: 0.779 10 | weight_decay: 0.00058 11 | warmup_epochs: 1.33 12 | warmup_momentum: 0.86 13 | warmup_bias_lr: 0.0711 14 | box: 0.0539 15 | cls: 0.299 16 | cls_pw: 0.825 17 | obj: 0.632 18 | obj_pw: 1.0 19 | iou_t: 0.2 20 | anchor_t: 3.44 21 | anchors: 3.2 22 | fl_gamma: 0.0 23 | hsv_h: 0.0188 24 | hsv_s: 0.704 25 | hsv_v: 0.36 26 | degrees: 0.0 27 | translate: 0.0902 28 | scale: 0.491 29 | shear: 0.0 30 | perspective: 0.0 31 | flipud: 0.0 32 | fliplr: 0.5 33 | mosaic: 1.0 34 | mixup: 0.0 35 | copy_paste: 0.0 36 | -------------------------------------------------------------------------------- /data/hyps/hyp.VOC.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Hyperparameters for VOC training 4 | # python train.py --batch 128 --weights yolov5m6.pt --data VOC.yaml --epochs 50 --img 512 --hyp hyp.scratch-med.yaml --evolve 5 | # See Hyperparameter Evolution tutorial for details https://github.com/ultralytics/yolov5#tutorials 6 | 7 | # YOLOv5 Hyperparameter Evolution Results 8 | # Best generation: 467 9 | # Last generation: 996 10 | # metrics/precision, metrics/recall, metrics/mAP_0.5, metrics/mAP_0.5:0.95, val/box_loss, val/obj_loss, val/cls_loss 11 | # 0.87729, 0.85125, 0.91286, 0.72664, 0.0076739, 0.0042529, 0.0013865 12 | 13 | lr0: 0.00334 14 | lrf: 0.15135 15 | momentum: 0.74832 16 | weight_decay: 0.00025 17 | warmup_epochs: 3.3835 18 | warmup_momentum: 0.59462 19 | warmup_bias_lr: 0.18657 20 | box: 0.02 21 | cls: 0.21638 22 | cls_pw: 0.5 23 | obj: 0.51728 24 | obj_pw: 0.67198 25 | iou_t: 0.2 26 | anchor_t: 3.3744 27 | fl_gamma: 0.0 28 | hsv_h: 0.01041 29 | hsv_s: 0.54703 30 | hsv_v: 0.27739 31 | degrees: 0.0 32 | translate: 0.04591 33 | scale: 0.75544 34 | shear: 0.0 35 | perspective: 0.0 36 | flipud: 0.0 37 | fliplr: 0.5 38 | mosaic: 0.85834 39 | mixup: 0.04266 40 | copy_paste: 0.0 41 | anchors: 3.412 42 | -------------------------------------------------------------------------------- /data/hyps/hyp.no-augmentation.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Hyperparameters when using Albumentations frameworks 4 | # python train.py --hyp hyp.no-augmentation.yaml 5 | # See https://github.com/ultralytics/yolov5/pull/3882 for YOLOv5 + Albumentations Usage examples 6 | 7 | lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3) 8 | lrf: 0.1 # final OneCycleLR learning rate (lr0 * lrf) 9 | momentum: 0.937 # SGD momentum/Adam beta1 10 | weight_decay: 0.0005 # optimizer weight decay 5e-4 11 | warmup_epochs: 3.0 # warmup epochs (fractions ok) 12 | warmup_momentum: 0.8 # warmup initial momentum 13 | warmup_bias_lr: 0.1 # warmup initial bias lr 14 | box: 0.05 # box loss gain 15 | cls: 0.3 # cls loss gain 16 | cls_pw: 1.0 # cls BCELoss positive_weight 17 | obj: 0.7 # obj loss gain (scale with pixels) 18 | obj_pw: 1.0 # obj BCELoss positive_weight 19 | iou_t: 0.20 # IoU training threshold 20 | anchor_t: 4.0 # anchor-multiple threshold 21 | # anchors: 3 # anchors per output layer (0 to ignore) 22 | # this parameters are all zero since we want to use albumentation framework 23 | fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5) 24 | hsv_h: 0 # image HSV-Hue augmentation (fraction) 25 | hsv_s: 0 # image HSV-Saturation augmentation (fraction) 26 | hsv_v: 0 # image HSV-Value augmentation (fraction) 27 | degrees: 0.0 # image rotation (+/- deg) 28 | translate: 0 # image translation (+/- fraction) 29 | scale: 0 # image scale (+/- gain) 30 | shear: 0 # image shear (+/- deg) 31 | perspective: 0.0 # image perspective (+/- fraction), range 0-0.001 32 | flipud: 0.0 # image flip up-down (probability) 33 | fliplr: 0.0 # image flip left-right (probability) 34 | mosaic: 0.0 # image mosaic (probability) 35 | mixup: 0.0 # image mixup (probability) 36 | copy_paste: 0.0 # segment copy-paste (probability) 37 | -------------------------------------------------------------------------------- /data/hyps/hyp.scratch-high.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Hyperparameters for high-augmentation COCO training from scratch 4 | # python train.py --batch 32 --cfg yolov5m6.yaml --weights '' --data coco.yaml --img 1280 --epochs 300 5 | # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials 6 | 7 | lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3) 8 | lrf: 0.1 # final OneCycleLR learning rate (lr0 * lrf) 9 | momentum: 0.937 # SGD momentum/Adam beta1 10 | weight_decay: 0.0005 # optimizer weight decay 5e-4 11 | warmup_epochs: 3.0 # warmup epochs (fractions ok) 12 | warmup_momentum: 0.8 # warmup initial momentum 13 | warmup_bias_lr: 0.1 # warmup initial bias lr 14 | box: 0.05 # box loss gain 15 | cls: 0.3 # cls loss gain 16 | cls_pw: 1.0 # cls BCELoss positive_weight 17 | obj: 0.7 # obj loss gain (scale with pixels) 18 | obj_pw: 1.0 # obj BCELoss positive_weight 19 | iou_t: 0.20 # IoU training threshold 20 | anchor_t: 4.0 # anchor-multiple threshold 21 | # anchors: 3 # anchors per output layer (0 to ignore) 22 | fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5) 23 | hsv_h: 0.015 # image HSV-Hue augmentation (fraction) 24 | hsv_s: 0.7 # image HSV-Saturation augmentation (fraction) 25 | hsv_v: 0.4 # image HSV-Value augmentation (fraction) 26 | degrees: 0.0 # image rotation (+/- deg) 27 | translate: 0.1 # image translation (+/- fraction) 28 | scale: 0.9 # image scale (+/- gain) 29 | shear: 0.0 # image shear (+/- deg) 30 | perspective: 0.0 # image perspective (+/- fraction), range 0-0.001 31 | flipud: 0.0 # image flip up-down (probability) 32 | fliplr: 0.5 # image flip left-right (probability) 33 | mosaic: 1.0 # image mosaic (probability) 34 | mixup: 0.1 # image mixup (probability) 35 | copy_paste: 0.1 # segment copy-paste (probability) 36 | -------------------------------------------------------------------------------- /data/hyps/hyp.scratch-low.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Hyperparameters for low-augmentation COCO training from scratch 4 | # python train.py --batch 64 --cfg yolov5n6.yaml --weights '' --data coco.yaml --img 640 --epochs 300 --linear 5 | # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials 6 | 7 | lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3) 8 | lrf: 0.01 # final OneCycleLR learning rate (lr0 * lrf) 9 | momentum: 0.937 # SGD momentum/Adam beta1 10 | weight_decay: 0.0005 # optimizer weight decay 5e-4 11 | warmup_epochs: 3.0 # warmup epochs (fractions ok) 12 | warmup_momentum: 0.8 # warmup initial momentum 13 | warmup_bias_lr: 0.1 # warmup initial bias lr 14 | box: 0.05 # box loss gain 15 | cls: 0.5 # cls loss gain 16 | cls_pw: 1.0 # cls BCELoss positive_weight 17 | obj: 1.0 # obj loss gain (scale with pixels) 18 | obj_pw: 1.0 # obj BCELoss positive_weight 19 | iou_t: 0.20 # IoU training threshold 20 | anchor_t: 4.0 # anchor-multiple threshold 21 | # anchors: 3 # anchors per output layer (0 to ignore) 22 | fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5) 23 | hsv_h: 0.015 # image HSV-Hue augmentation (fraction) 24 | hsv_s: 0.7 # image HSV-Saturation augmentation (fraction) 25 | hsv_v: 0.4 # image HSV-Value augmentation (fraction) 26 | degrees: 0.0 # image rotation (+/- deg) 27 | translate: 0.1 # image translation (+/- fraction) 28 | scale: 0.5 # image scale (+/- gain) 29 | shear: 0.0 # image shear (+/- deg) 30 | perspective: 0.0 # image perspective (+/- fraction), range 0-0.001 31 | flipud: 0.0 # image flip up-down (probability) 32 | fliplr: 0.5 # image flip left-right (probability) 33 | mosaic: 1.0 # image mosaic (probability) 34 | mixup: 0.0 # image mixup (probability) 35 | copy_paste: 0.0 # segment copy-paste (probability) 36 | -------------------------------------------------------------------------------- /data/hyps/hyp.scratch-med.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Hyperparameters for medium-augmentation COCO training from scratch 4 | # python train.py --batch 32 --cfg yolov5m6.yaml --weights '' --data coco.yaml --img 1280 --epochs 300 5 | # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials 6 | 7 | lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3) 8 | lrf: 0.1 # final OneCycleLR learning rate (lr0 * lrf) 9 | momentum: 0.937 # SGD momentum/Adam beta1 10 | weight_decay: 0.0005 # optimizer weight decay 5e-4 11 | warmup_epochs: 3.0 # warmup epochs (fractions ok) 12 | warmup_momentum: 0.8 # warmup initial momentum 13 | warmup_bias_lr: 0.1 # warmup initial bias lr 14 | box: 0.05 # box loss gain 15 | cls: 0.3 # cls loss gain 16 | cls_pw: 1.0 # cls BCELoss positive_weight 17 | obj: 0.7 # obj loss gain (scale with pixels) 18 | obj_pw: 1.0 # obj BCELoss positive_weight 19 | iou_t: 0.20 # IoU training threshold 20 | anchor_t: 4.0 # anchor-multiple threshold 21 | # anchors: 3 # anchors per output layer (0 to ignore) 22 | fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5) 23 | hsv_h: 0.015 # image HSV-Hue augmentation (fraction) 24 | hsv_s: 0.7 # image HSV-Saturation augmentation (fraction) 25 | hsv_v: 0.4 # image HSV-Value augmentation (fraction) 26 | degrees: 0.0 # image rotation (+/- deg) 27 | translate: 0.1 # image translation (+/- fraction) 28 | scale: 0.9 # image scale (+/- gain) 29 | shear: 0.0 # image shear (+/- deg) 30 | perspective: 0.0 # image perspective (+/- fraction), range 0-0.001 31 | flipud: 0.0 # image flip up-down (probability) 32 | fliplr: 0.5 # image flip left-right (probability) 33 | mosaic: 1.0 # image mosaic (probability) 34 | mixup: 0.1 # image mixup (probability) 35 | copy_paste: 0.0 # segment copy-paste (probability) 36 | -------------------------------------------------------------------------------- /data/images/bus.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultralytics/yolov5/cd44191ca5c1119ccd15c5395ef24418982c5a99/data/images/bus.jpg -------------------------------------------------------------------------------- /data/images/zidane.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ultralytics/yolov5/cd44191ca5c1119ccd15c5395ef24418982c5a99/data/images/zidane.jpg -------------------------------------------------------------------------------- /data/scripts/download_weights.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 3 | 4 | # Download latest models from https://github.com/ultralytics/yolov5/releases 5 | # Example usage: bash data/scripts/download_weights.sh 6 | # parent 7 | # └── yolov5 8 | # ├── yolov5s.pt ← downloads here 9 | # ├── yolov5m.pt 10 | # └── ... 11 | 12 | python - << EOF 13 | from utils.downloads import attempt_download 14 | 15 | p5 = list('nsmlx') # P5 models 16 | p6 = [f'{x}6' for x in p5] # P6 models 17 | cls = [f'{x}-cls' for x in p5] # classification models 18 | seg = [f'{x}-seg' for x in p5] # classification models 19 | 20 | for x in p5 + p6 + cls + seg: 21 | attempt_download(f'weights/yolov5{x}.pt') 22 | 23 | EOF 24 | -------------------------------------------------------------------------------- /data/scripts/get_coco.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 3 | 4 | # Download COCO 2017 dataset http://cocodataset.org 5 | # Example usage: bash data/scripts/get_coco.sh 6 | # parent 7 | # ├── yolov5 8 | # └── datasets 9 | # └── coco ← downloads here 10 | 11 | # Arguments (optional) Usage: bash data/scripts/get_coco.sh --train --val --test --segments 12 | if [ "$#" -gt 0 ]; then 13 | for opt in "$@"; do 14 | case "${opt}" in 15 | --train) train=true ;; 16 | --val) val=true ;; 17 | --test) test=true ;; 18 | --segments) segments=true ;; 19 | esac 20 | done 21 | else 22 | train=true 23 | val=true 24 | test=false 25 | segments=false 26 | fi 27 | 28 | # Download/unzip labels 29 | d='../datasets' # unzip directory 30 | url=https://github.com/ultralytics/yolov5/releases/download/v1.0/ 31 | if [ "$segments" == "true" ]; then 32 | f='coco2017labels-segments.zip' # 168 MB 33 | else 34 | f='coco2017labels.zip' # 46 MB 35 | fi 36 | echo 'Downloading' $url$f ' ...' 37 | curl -L $url$f -o $f -# && unzip -q $f -d $d && rm $f & 38 | 39 | # Download/unzip images 40 | d='../datasets/coco/images' # unzip directory 41 | url=http://images.cocodataset.org/zips/ 42 | if [ "$train" == "true" ]; then 43 | f='train2017.zip' # 19G, 118k images 44 | echo 'Downloading' $url$f '...' 45 | curl -L $url$f -o $f -# && unzip -q $f -d $d && rm $f & 46 | fi 47 | if [ "$val" == "true" ]; then 48 | f='val2017.zip' # 1G, 5k images 49 | echo 'Downloading' $url$f '...' 50 | curl -L $url$f -o $f -# && unzip -q $f -d $d && rm $f & 51 | fi 52 | if [ "$test" == "true" ]; then 53 | f='test2017.zip' # 7G, 41k images (optional) 54 | echo 'Downloading' $url$f '...' 55 | curl -L $url$f -o $f -# && unzip -q $f -d $d && rm $f & 56 | fi 57 | wait # finish background tasks 58 | -------------------------------------------------------------------------------- /data/scripts/get_coco128.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 3 | 4 | # Download COCO128 dataset https://www.kaggle.com/ultralytics/coco128 (first 128 images from COCO train2017) 5 | # Example usage: bash data/scripts/get_coco128.sh 6 | # parent 7 | # ├── yolov5 8 | # └── datasets 9 | # └── coco128 ← downloads here 10 | 11 | # Download/unzip images and labels 12 | d='../datasets' # unzip directory 13 | url=https://github.com/ultralytics/yolov5/releases/download/v1.0/ 14 | f='coco128.zip' # or 'coco128-segments.zip', 68 MB 15 | echo 'Downloading' $url$f ' ...' 16 | curl -L $url$f -o $f -# && unzip -q $f -d $d && rm $f & 17 | 18 | wait # finish background tasks 19 | -------------------------------------------------------------------------------- /data/scripts/get_imagenet.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 3 | 4 | # Download ILSVRC2012 ImageNet dataset https://image-net.org 5 | # Example usage: bash data/scripts/get_imagenet.sh 6 | # parent 7 | # ├── yolov5 8 | # └── datasets 9 | # └── imagenet ← downloads here 10 | 11 | # Arguments (optional) Usage: bash data/scripts/get_imagenet.sh --train --val 12 | if [ "$#" -gt 0 ]; then 13 | for opt in "$@"; do 14 | case "${opt}" in 15 | --train) train=true ;; 16 | --val) val=true ;; 17 | esac 18 | done 19 | else 20 | train=true 21 | val=true 22 | fi 23 | 24 | # Make dir 25 | d='../datasets/imagenet' # unzip directory 26 | mkdir -p $d && cd $d 27 | 28 | # Download/unzip train 29 | if [ "$train" == "true" ]; then 30 | wget https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_train.tar # download 138G, 1281167 images 31 | mkdir train && mv ILSVRC2012_img_train.tar train/ && cd train 32 | tar -xf ILSVRC2012_img_train.tar && rm -f ILSVRC2012_img_train.tar 33 | find . -name "*.tar" | while read NAME; do 34 | mkdir -p "${NAME%.tar}" 35 | tar -xf "${NAME}" -C "${NAME%.tar}" 36 | rm -f "${NAME}" 37 | done 38 | cd .. 39 | fi 40 | 41 | # Download/unzip val 42 | if [ "$val" == "true" ]; then 43 | wget https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_val.tar # download 6.3G, 50000 images 44 | mkdir val && mv ILSVRC2012_img_val.tar val/ && cd val && tar -xf ILSVRC2012_img_val.tar 45 | wget -qO- https://raw.githubusercontent.com/soumith/imagenetloader.torch/master/valprep.sh | bash # move into subdirs 46 | fi 47 | 48 | # Delete corrupted image (optional: PNG under JPEG name that may cause dataloaders to fail) 49 | # rm train/n04266014/n04266014_10835.JPEG 50 | 51 | # TFRecords (optional) 52 | # wget https://raw.githubusercontent.com/tensorflow/models/master/research/slim/datasets/imagenet_lsvrc_2015_synsets.txt 53 | -------------------------------------------------------------------------------- /data/scripts/get_imagenet10.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 3 | 4 | # Download ILSVRC2012 ImageNet dataset https://image-net.org 5 | # Example usage: bash data/scripts/get_imagenet.sh 6 | # parent 7 | # ├── yolov5 8 | # └── datasets 9 | # └── imagenet ← downloads here 10 | 11 | # Arguments (optional) Usage: bash data/scripts/get_imagenet.sh --train --val 12 | if [ "$#" -gt 0 ]; then 13 | for opt in "$@"; do 14 | case "${opt}" in 15 | --train) train=true ;; 16 | --val) val=true ;; 17 | esac 18 | done 19 | else 20 | train=true 21 | val=true 22 | fi 23 | 24 | # Make dir 25 | d='../datasets/imagenet10' # unzip directory 26 | mkdir -p $d && cd $d 27 | 28 | # Download/unzip train 29 | wget https://github.com/ultralytics/yolov5/releases/download/v1.0/imagenet10.zip 30 | unzip imagenet10.zip && rm imagenet10.zip 31 | -------------------------------------------------------------------------------- /data/scripts/get_imagenet100.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 3 | 4 | # Download ILSVRC2012 ImageNet dataset https://image-net.org 5 | # Example usage: bash data/scripts/get_imagenet.sh 6 | # parent 7 | # ├── yolov5 8 | # └── datasets 9 | # └── imagenet ← downloads here 10 | 11 | # Arguments (optional) Usage: bash data/scripts/get_imagenet.sh --train --val 12 | if [ "$#" -gt 0 ]; then 13 | for opt in "$@"; do 14 | case "${opt}" in 15 | --train) train=true ;; 16 | --val) val=true ;; 17 | esac 18 | done 19 | else 20 | train=true 21 | val=true 22 | fi 23 | 24 | # Make dir 25 | d='../datasets/imagenet100' # unzip directory 26 | mkdir -p $d && cd $d 27 | 28 | # Download/unzip train 29 | wget https://github.com/ultralytics/yolov5/releases/download/v1.0/imagenet100.zip 30 | unzip imagenet100.zip && rm imagenet100.zip 31 | -------------------------------------------------------------------------------- /data/scripts/get_imagenet1000.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 3 | 4 | # Download ILSVRC2012 ImageNet dataset https://image-net.org 5 | # Example usage: bash data/scripts/get_imagenet.sh 6 | # parent 7 | # ├── yolov5 8 | # └── datasets 9 | # └── imagenet ← downloads here 10 | 11 | # Arguments (optional) Usage: bash data/scripts/get_imagenet.sh --train --val 12 | if [ "$#" -gt 0 ]; then 13 | for opt in "$@"; do 14 | case "${opt}" in 15 | --train) train=true ;; 16 | --val) val=true ;; 17 | esac 18 | done 19 | else 20 | train=true 21 | val=true 22 | fi 23 | 24 | # Make dir 25 | d='../datasets/imagenet1000' # unzip directory 26 | mkdir -p $d && cd $d 27 | 28 | # Download/unzip train 29 | wget https://github.com/ultralytics/yolov5/releases/download/v1.0/imagenet1000.zip 30 | unzip imagenet1000.zip && rm imagenet1000.zip 31 | -------------------------------------------------------------------------------- /data/xView.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # DIUx xView 2018 Challenge https://challenge.xviewdataset.org by U.S. National Geospatial-Intelligence Agency (NGA) 4 | # -------- DOWNLOAD DATA MANUALLY and jar xf val_images.zip to 'datasets/xView' before running train command! -------- 5 | # Example usage: python train.py --data xView.yaml 6 | # parent 7 | # ├── yolov5 8 | # └── datasets 9 | # └── xView ← downloads here (20.7 GB) 10 | 11 | # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..] 12 | path: ../datasets/xView # dataset root dir 13 | train: images/autosplit_train.txt # train images (relative to 'path') 90% of 847 train images 14 | val: images/autosplit_val.txt # train images (relative to 'path') 10% of 847 train images 15 | 16 | # Classes 17 | names: 18 | 0: Fixed-wing Aircraft 19 | 1: Small Aircraft 20 | 2: Cargo Plane 21 | 3: Helicopter 22 | 4: Passenger Vehicle 23 | 5: Small Car 24 | 6: Bus 25 | 7: Pickup Truck 26 | 8: Utility Truck 27 | 9: Truck 28 | 10: Cargo Truck 29 | 11: Truck w/Box 30 | 12: Truck Tractor 31 | 13: Trailer 32 | 14: Truck w/Flatbed 33 | 15: Truck w/Liquid 34 | 16: Crane Truck 35 | 17: Railway Vehicle 36 | 18: Passenger Car 37 | 19: Cargo Car 38 | 20: Flat Car 39 | 21: Tank car 40 | 22: Locomotive 41 | 23: Maritime Vessel 42 | 24: Motorboat 43 | 25: Sailboat 44 | 26: Tugboat 45 | 27: Barge 46 | 28: Fishing Vessel 47 | 29: Ferry 48 | 30: Yacht 49 | 31: Container Ship 50 | 32: Oil Tanker 51 | 33: Engineering Vehicle 52 | 34: Tower crane 53 | 35: Container Crane 54 | 36: Reach Stacker 55 | 37: Straddle Carrier 56 | 38: Mobile Crane 57 | 39: Dump Truck 58 | 40: Haul Truck 59 | 41: Scraper/Tractor 60 | 42: Front loader/Bulldozer 61 | 43: Excavator 62 | 44: Cement Mixer 63 | 45: Ground Grader 64 | 46: Hut/Tent 65 | 47: Shed 66 | 48: Building 67 | 49: Aircraft Hangar 68 | 50: Damaged Building 69 | 51: Facility 70 | 52: Construction Site 71 | 53: Vehicle Lot 72 | 54: Helipad 73 | 55: Storage Tank 74 | 56: Shipping container lot 75 | 57: Shipping Container 76 | 58: Pylon 77 | 59: Tower 78 | 79 | # Download script/URL (optional) --------------------------------------------------------------------------------------- 80 | download: | 81 | import json 82 | import os 83 | from pathlib import Path 84 | 85 | import numpy as np 86 | from PIL import Image 87 | from tqdm import tqdm 88 | 89 | from utils.dataloaders import autosplit 90 | from utils.general import download, xyxy2xywhn 91 | 92 | 93 | def convert_labels(fname=Path('xView/xView_train.geojson')): 94 | # Convert xView geoJSON labels to YOLO format 95 | path = fname.parent 96 | with open(fname) as f: 97 | print(f'Loading {fname}...') 98 | data = json.load(f) 99 | 100 | # Make dirs 101 | labels = Path(path / 'labels' / 'train') 102 | os.system(f'rm -rf {labels}') 103 | labels.mkdir(parents=True, exist_ok=True) 104 | 105 | # xView classes 11-94 to 0-59 106 | xview_class2index = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, -1, 3, -1, 4, 5, 6, 7, 8, -1, 9, 10, 11, 107 | 12, 13, 14, 15, -1, -1, 16, 17, 18, 19, 20, 21, 22, -1, 23, 24, 25, -1, 26, 27, -1, 28, -1, 108 | 29, 30, 31, 32, 33, 34, 35, 36, 37, -1, 38, 39, 40, 41, 42, 43, 44, 45, -1, -1, -1, -1, 46, 109 | 47, 48, 49, -1, 50, 51, -1, 52, -1, -1, -1, 53, 54, -1, 55, -1, -1, 56, -1, 57, -1, 58, 59] 110 | 111 | shapes = {} 112 | for feature in tqdm(data['features'], desc=f'Converting {fname}'): 113 | p = feature['properties'] 114 | if p['bounds_imcoords']: 115 | id = p['image_id'] 116 | file = path / 'train_images' / id 117 | if file.exists(): # 1395.tif missing 118 | try: 119 | box = np.array([int(num) for num in p['bounds_imcoords'].split(",")]) 120 | assert box.shape[0] == 4, f'incorrect box shape {box.shape[0]}' 121 | cls = p['type_id'] 122 | cls = xview_class2index[int(cls)] # xView class to 0-60 123 | assert 59 >= cls >= 0, f'incorrect class index {cls}' 124 | 125 | # Write YOLO label 126 | if id not in shapes: 127 | shapes[id] = Image.open(file).size 128 | box = xyxy2xywhn(box[None].astype(np.float), w=shapes[id][0], h=shapes[id][1], clip=True) 129 | with open((labels / id).with_suffix('.txt'), 'a') as f: 130 | f.write(f"{cls} {' '.join(f'{x:.6f}' for x in box[0])}\n") # write label.txt 131 | except Exception as e: 132 | print(f'WARNING: skipping one label for {file}: {e}') 133 | 134 | 135 | # Download manually from https://challenge.xviewdataset.org 136 | dir = Path(yaml['path']) # dataset root dir 137 | # urls = ['https://d307kc0mrhucc3.cloudfront.net/train_labels.zip', # train labels 138 | # 'https://d307kc0mrhucc3.cloudfront.net/train_images.zip', # 15G, 847 train images 139 | # 'https://d307kc0mrhucc3.cloudfront.net/val_images.zip'] # 5G, 282 val images (no labels) 140 | # download(urls, dir=dir, delete=False) 141 | 142 | # Convert labels 143 | convert_labels(dir / 'xView_train.geojson') 144 | 145 | # Move images 146 | images = Path(dir / 'images') 147 | images.mkdir(parents=True, exist_ok=True) 148 | Path(dir / 'train_images').rename(dir / 'images' / 'train') 149 | Path(dir / 'val_images').rename(dir / 'images' / 'val') 150 | 151 | # Split 152 | autosplit(dir / 'images' / 'train') 153 | -------------------------------------------------------------------------------- /models/__init__.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | -------------------------------------------------------------------------------- /models/experimental.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | """Experimental modules.""" 3 | 4 | import math 5 | 6 | import numpy as np 7 | import torch 8 | import torch.nn as nn 9 | 10 | from utils.downloads import attempt_download 11 | 12 | 13 | class Sum(nn.Module): 14 | """Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070.""" 15 | 16 | def __init__(self, n, weight=False): 17 | """Initializes a module to sum outputs of layers with number of inputs `n` and optional weighting, supporting 2+ 18 | inputs. 19 | """ 20 | super().__init__() 21 | self.weight = weight # apply weights boolean 22 | self.iter = range(n - 1) # iter object 23 | if weight: 24 | self.w = nn.Parameter(-torch.arange(1.0, n) / 2, requires_grad=True) # layer weights 25 | 26 | def forward(self, x): 27 | """Processes input through a customizable weighted sum of `n` inputs, optionally applying learned weights.""" 28 | y = x[0] # no weight 29 | if self.weight: 30 | w = torch.sigmoid(self.w) * 2 31 | for i in self.iter: 32 | y = y + x[i + 1] * w[i] 33 | else: 34 | for i in self.iter: 35 | y = y + x[i + 1] 36 | return y 37 | 38 | 39 | class MixConv2d(nn.Module): 40 | """Mixed Depth-wise Conv https://arxiv.org/abs/1907.09595.""" 41 | 42 | def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): 43 | """Initializes MixConv2d with mixed depth-wise convolutional layers, taking input and output channels (c1, c2), 44 | kernel sizes (k), stride (s), and channel distribution strategy (equal_ch). 45 | """ 46 | super().__init__() 47 | n = len(k) # number of convolutions 48 | if equal_ch: # equal c_ per group 49 | i = torch.linspace(0, n - 1e-6, c2).floor() # c2 indices 50 | c_ = [(i == g).sum() for g in range(n)] # intermediate channels 51 | else: # equal weight.numel() per group 52 | b = [c2] + [0] * n 53 | a = np.eye(n + 1, n, k=-1) 54 | a -= np.roll(a, 1, axis=1) 55 | a *= np.array(k) ** 2 56 | a[0] = 1 57 | c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b 58 | 59 | self.m = nn.ModuleList( 60 | [nn.Conv2d(c1, int(c_), k, s, k // 2, groups=math.gcd(c1, int(c_)), bias=False) for k, c_ in zip(k, c_)] 61 | ) 62 | self.bn = nn.BatchNorm2d(c2) 63 | self.act = nn.SiLU() 64 | 65 | def forward(self, x): 66 | """Performs forward pass by applying SiLU activation on batch-normalized concatenated convolutional layer 67 | outputs. 68 | """ 69 | return self.act(self.bn(torch.cat([m(x) for m in self.m], 1))) 70 | 71 | 72 | class Ensemble(nn.ModuleList): 73 | """Ensemble of models.""" 74 | 75 | def __init__(self): 76 | """Initializes an ensemble of models to be used for aggregated predictions.""" 77 | super().__init__() 78 | 79 | def forward(self, x, augment=False, profile=False, visualize=False): 80 | """Performs forward pass aggregating outputs from an ensemble of models..""" 81 | y = [module(x, augment, profile, visualize)[0] for module in self] 82 | # y = torch.stack(y).max(0)[0] # max ensemble 83 | # y = torch.stack(y).mean(0) # mean ensemble 84 | y = torch.cat(y, 1) # nms ensemble 85 | return y, None # inference, train output 86 | 87 | 88 | def attempt_load(weights, device=None, inplace=True, fuse=True): 89 | """ 90 | Loads and fuses an ensemble or single YOLOv5 model from weights, handling device placement and model adjustments. 91 | 92 | Example inputs: weights=[a,b,c] or a single model weights=[a] or weights=a. 93 | """ 94 | from models.yolo import Detect, Model 95 | 96 | model = Ensemble() 97 | for w in weights if isinstance(weights, list) else [weights]: 98 | ckpt = torch.load(attempt_download(w), map_location="cpu") # load 99 | ckpt = (ckpt.get("ema") or ckpt["model"]).to(device).float() # FP32 model 100 | 101 | # Model compatibility updates 102 | if not hasattr(ckpt, "stride"): 103 | ckpt.stride = torch.tensor([32.0]) 104 | if hasattr(ckpt, "names") and isinstance(ckpt.names, (list, tuple)): 105 | ckpt.names = dict(enumerate(ckpt.names)) # convert to dict 106 | 107 | model.append(ckpt.fuse().eval() if fuse and hasattr(ckpt, "fuse") else ckpt.eval()) # model in eval mode 108 | 109 | # Module updates 110 | for m in model.modules(): 111 | t = type(m) 112 | if t in (nn.Hardswish, nn.LeakyReLU, nn.ReLU, nn.ReLU6, nn.SiLU, Detect, Model): 113 | m.inplace = inplace 114 | if t is Detect and not isinstance(m.anchor_grid, list): 115 | delattr(m, "anchor_grid") 116 | setattr(m, "anchor_grid", [torch.zeros(1)] * m.nl) 117 | elif t is nn.Upsample and not hasattr(m, "recompute_scale_factor"): 118 | m.recompute_scale_factor = None # torch 1.11.0 compatibility 119 | 120 | # Return model 121 | if len(model) == 1: 122 | return model[-1] 123 | 124 | # Return detection ensemble 125 | print(f"Ensemble created with {weights}\n") 126 | for k in "names", "nc", "yaml": 127 | setattr(model, k, getattr(model[0], k)) 128 | model.stride = model[torch.argmax(torch.tensor([m.stride.max() for m in model])).int()].stride # max stride 129 | assert all(model[0].nc == m.nc for m in model), f"Models have different class counts: {[m.nc for m in model]}" 130 | return model 131 | -------------------------------------------------------------------------------- /models/hub/anchors.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Default anchors for COCO data 4 | 5 | # P5 ------------------------------------------------------------------------------------------------------------------- 6 | # P5-640: 7 | anchors_p5_640: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # P6 ------------------------------------------------------------------------------------------------------------------- 13 | # P6-640: thr=0.25: 0.9964 BPR, 5.54 anchors past thr, n=12, img_size=640, metric_all=0.281/0.716-mean/best, past_thr=0.469-mean: 9,11, 21,19, 17,41, 43,32, 39,70, 86,64, 65,131, 134,130, 120,265, 282,180, 247,354, 512,387 14 | anchors_p6_640: 15 | - [9, 11, 21, 19, 17, 41] # P3/8 16 | - [43, 32, 39, 70, 86, 64] # P4/16 17 | - [65, 131, 134, 130, 120, 265] # P5/32 18 | - [282, 180, 247, 354, 512, 387] # P6/64 19 | 20 | # P6-1280: thr=0.25: 0.9950 BPR, 5.55 anchors past thr, n=12, img_size=1280, metric_all=0.281/0.714-mean/best, past_thr=0.468-mean: 19,27, 44,40, 38,94, 96,68, 86,152, 180,137, 140,301, 303,264, 238,542, 436,615, 739,380, 925,792 21 | anchors_p6_1280: 22 | - [19, 27, 44, 40, 38, 94] # P3/8 23 | - [96, 68, 86, 152, 180, 137] # P4/16 24 | - [140, 301, 303, 264, 238, 542] # P5/32 25 | - [436, 615, 739, 380, 925, 792] # P6/64 26 | 27 | # P6-1920: thr=0.25: 0.9950 BPR, 5.55 anchors past thr, n=12, img_size=1920, metric_all=0.281/0.714-mean/best, past_thr=0.468-mean: 28,41, 67,59, 57,141, 144,103, 129,227, 270,205, 209,452, 455,396, 358,812, 653,922, 1109,570, 1387,1187 28 | anchors_p6_1920: 29 | - [28, 41, 67, 59, 57, 141] # P3/8 30 | - [144, 103, 129, 227, 270, 205] # P4/16 31 | - [209, 452, 455, 396, 358, 812] # P5/32 32 | - [653, 922, 1109, 570, 1387, 1187] # P6/64 33 | 34 | # P7 ------------------------------------------------------------------------------------------------------------------- 35 | # P7-640: thr=0.25: 0.9962 BPR, 6.76 anchors past thr, n=15, img_size=640, metric_all=0.275/0.733-mean/best, past_thr=0.466-mean: 11,11, 13,30, 29,20, 30,46, 61,38, 39,92, 78,80, 146,66, 79,163, 149,150, 321,143, 157,303, 257,402, 359,290, 524,372 36 | anchors_p7_640: 37 | - [11, 11, 13, 30, 29, 20] # P3/8 38 | - [30, 46, 61, 38, 39, 92] # P4/16 39 | - [78, 80, 146, 66, 79, 163] # P5/32 40 | - [149, 150, 321, 143, 157, 303] # P6/64 41 | - [257, 402, 359, 290, 524, 372] # P7/128 42 | 43 | # P7-1280: thr=0.25: 0.9968 BPR, 6.71 anchors past thr, n=15, img_size=1280, metric_all=0.273/0.732-mean/best, past_thr=0.463-mean: 19,22, 54,36, 32,77, 70,83, 138,71, 75,173, 165,159, 148,334, 375,151, 334,317, 251,626, 499,474, 750,326, 534,814, 1079,818 44 | anchors_p7_1280: 45 | - [19, 22, 54, 36, 32, 77] # P3/8 46 | - [70, 83, 138, 71, 75, 173] # P4/16 47 | - [165, 159, 148, 334, 375, 151] # P5/32 48 | - [334, 317, 251, 626, 499, 474] # P6/64 49 | - [750, 326, 534, 814, 1079, 818] # P7/128 50 | 51 | # P7-1920: thr=0.25: 0.9968 BPR, 6.71 anchors past thr, n=15, img_size=1920, metric_all=0.273/0.732-mean/best, past_thr=0.463-mean: 29,34, 81,55, 47,115, 105,124, 207,107, 113,259, 247,238, 222,500, 563,227, 501,476, 376,939, 749,711, 1126,489, 801,1222, 1618,1227 52 | anchors_p7_1920: 53 | - [29, 34, 81, 55, 47, 115] # P3/8 54 | - [105, 124, 207, 107, 113, 259] # P4/16 55 | - [247, 238, 222, 500, 563, 227] # P5/32 56 | - [501, 476, 376, 939, 749, 711] # P6/64 57 | - [1126, 489, 801, 1222, 1618, 1227] # P7/128 58 | -------------------------------------------------------------------------------- /models/hub/yolov3-spp.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # darknet53 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [32, 3, 1]], # 0 17 | [-1, 1, Conv, [64, 3, 2]], # 1-P1/2 18 | [-1, 1, Bottleneck, [64]], 19 | [-1, 1, Conv, [128, 3, 2]], # 3-P2/4 20 | [-1, 2, Bottleneck, [128]], 21 | [-1, 1, Conv, [256, 3, 2]], # 5-P3/8 22 | [-1, 8, Bottleneck, [256]], 23 | [-1, 1, Conv, [512, 3, 2]], # 7-P4/16 24 | [-1, 8, Bottleneck, [512]], 25 | [-1, 1, Conv, [1024, 3, 2]], # 9-P5/32 26 | [-1, 4, Bottleneck, [1024]], # 10 27 | ] 28 | 29 | # YOLOv3-SPP head 30 | head: [ 31 | [-1, 1, Bottleneck, [1024, False]], 32 | [-1, 1, SPP, [512, [5, 9, 13]]], 33 | [-1, 1, Conv, [1024, 3, 1]], 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 1, Conv, [1024, 3, 1]], # 15 (P5/32-large) 36 | 37 | [-2, 1, Conv, [256, 1, 1]], 38 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 39 | [[-1, 8], 1, Concat, [1]], # cat backbone P4 40 | [-1, 1, Bottleneck, [512, False]], 41 | [-1, 1, Bottleneck, [512, False]], 42 | [-1, 1, Conv, [256, 1, 1]], 43 | [-1, 1, Conv, [512, 3, 1]], # 22 (P4/16-medium) 44 | 45 | [-2, 1, Conv, [128, 1, 1]], 46 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 47 | [[-1, 6], 1, Concat, [1]], # cat backbone P3 48 | [-1, 1, Bottleneck, [256, False]], 49 | [-1, 2, Bottleneck, [256, False]], # 27 (P3/8-small) 50 | 51 | [[27, 22, 15], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 52 | ] 53 | -------------------------------------------------------------------------------- /models/hub/yolov3-tiny.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 8 | - [10, 14, 23, 27, 37, 58] # P4/16 9 | - [81, 82, 135, 169, 344, 319] # P5/32 10 | 11 | # YOLOv3-tiny backbone 12 | backbone: 13 | # [from, number, module, args] 14 | [ 15 | [-1, 1, Conv, [16, 3, 1]], # 0 16 | [-1, 1, nn.MaxPool2d, [2, 2, 0]], # 1-P1/2 17 | [-1, 1, Conv, [32, 3, 1]], 18 | [-1, 1, nn.MaxPool2d, [2, 2, 0]], # 3-P2/4 19 | [-1, 1, Conv, [64, 3, 1]], 20 | [-1, 1, nn.MaxPool2d, [2, 2, 0]], # 5-P3/8 21 | [-1, 1, Conv, [128, 3, 1]], 22 | [-1, 1, nn.MaxPool2d, [2, 2, 0]], # 7-P4/16 23 | [-1, 1, Conv, [256, 3, 1]], 24 | [-1, 1, nn.MaxPool2d, [2, 2, 0]], # 9-P5/32 25 | [-1, 1, Conv, [512, 3, 1]], 26 | [-1, 1, nn.ZeroPad2d, [[0, 1, 0, 1]]], # 11 27 | [-1, 1, nn.MaxPool2d, [2, 1, 0]], # 12 28 | ] 29 | 30 | # YOLOv3-tiny head 31 | head: [ 32 | [-1, 1, Conv, [1024, 3, 1]], 33 | [-1, 1, Conv, [256, 1, 1]], 34 | [-1, 1, Conv, [512, 3, 1]], # 15 (P5/32-large) 35 | 36 | [-2, 1, Conv, [128, 1, 1]], 37 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 38 | [[-1, 8], 1, Concat, [1]], # cat backbone P4 39 | [-1, 1, Conv, [256, 3, 1]], # 19 (P4/16-medium) 40 | 41 | [[19, 15], 1, Detect, [nc, anchors]], # Detect(P4, P5) 42 | ] 43 | -------------------------------------------------------------------------------- /models/hub/yolov3.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # darknet53 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [32, 3, 1]], # 0 17 | [-1, 1, Conv, [64, 3, 2]], # 1-P1/2 18 | [-1, 1, Bottleneck, [64]], 19 | [-1, 1, Conv, [128, 3, 2]], # 3-P2/4 20 | [-1, 2, Bottleneck, [128]], 21 | [-1, 1, Conv, [256, 3, 2]], # 5-P3/8 22 | [-1, 8, Bottleneck, [256]], 23 | [-1, 1, Conv, [512, 3, 2]], # 7-P4/16 24 | [-1, 8, Bottleneck, [512]], 25 | [-1, 1, Conv, [1024, 3, 2]], # 9-P5/32 26 | [-1, 4, Bottleneck, [1024]], # 10 27 | ] 28 | 29 | # YOLOv3 head 30 | head: [ 31 | [-1, 1, Bottleneck, [1024, False]], 32 | [-1, 1, Conv, [512, 1, 1]], 33 | [-1, 1, Conv, [1024, 3, 1]], 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 1, Conv, [1024, 3, 1]], # 15 (P5/32-large) 36 | 37 | [-2, 1, Conv, [256, 1, 1]], 38 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 39 | [[-1, 8], 1, Concat, [1]], # cat backbone P4 40 | [-1, 1, Bottleneck, [512, False]], 41 | [-1, 1, Bottleneck, [512, False]], 42 | [-1, 1, Conv, [256, 1, 1]], 43 | [-1, 1, Conv, [512, 3, 1]], # 22 (P4/16-medium) 44 | 45 | [-2, 1, Conv, [128, 1, 1]], 46 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 47 | [[-1, 6], 1, Concat, [1]], # cat backbone P3 48 | [-1, 1, Bottleneck, [256, False]], 49 | [-1, 2, Bottleneck, [256, False]], # 27 (P3/8-small) 50 | 51 | [[27, 22, 15], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 52 | ] 53 | -------------------------------------------------------------------------------- /models/hub/yolov5-bifpn.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 BiFPN head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14, 6], 1, Concat, [1]], # cat P4 <--- BiFPN change 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/hub/yolov5-fpn.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 FPN head 29 | head: [ 30 | [-1, 3, C3, [1024, False]], # 10 (P5/32-large) 31 | 32 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 3, C3, [512, False]], # 14 (P4/16-medium) 36 | 37 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 38 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 39 | [-1, 1, Conv, [256, 1, 1]], 40 | [-1, 3, C3, [256, False]], # 18 (P3/8-small) 41 | 42 | [[18, 14, 10], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 43 | ] 44 | -------------------------------------------------------------------------------- /models/hub/yolov5-p2.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 3 # AutoAnchor evolves 3 anchors per P output layer 8 | 9 | # YOLOv5 v6.0 backbone 10 | backbone: 11 | # [from, number, module, args] 12 | [ 13 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 14 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 15 | [-1, 3, C3, [128]], 16 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 17 | [-1, 6, C3, [256]], 18 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 19 | [-1, 9, C3, [512]], 20 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 21 | [-1, 3, C3, [1024]], 22 | [-1, 1, SPPF, [1024, 5]], # 9 23 | ] 24 | 25 | # YOLOv5 v6.0 head with (P2, P3, P4, P5) outputs 26 | head: [ 27 | [-1, 1, Conv, [512, 1, 1]], 28 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 29 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 30 | [-1, 3, C3, [512, False]], # 13 31 | 32 | [-1, 1, Conv, [256, 1, 1]], 33 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 34 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 35 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 36 | 37 | [-1, 1, Conv, [128, 1, 1]], 38 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 39 | [[-1, 2], 1, Concat, [1]], # cat backbone P2 40 | [-1, 1, C3, [128, False]], # 21 (P2/4-xsmall) 41 | 42 | [-1, 1, Conv, [128, 3, 2]], 43 | [[-1, 18], 1, Concat, [1]], # cat head P3 44 | [-1, 3, C3, [256, False]], # 24 (P3/8-small) 45 | 46 | [-1, 1, Conv, [256, 3, 2]], 47 | [[-1, 14], 1, Concat, [1]], # cat head P4 48 | [-1, 3, C3, [512, False]], # 27 (P4/16-medium) 49 | 50 | [-1, 1, Conv, [512, 3, 2]], 51 | [[-1, 10], 1, Concat, [1]], # cat head P5 52 | [-1, 3, C3, [1024, False]], # 30 (P5/32-large) 53 | 54 | [[21, 24, 27, 30], 1, Detect, [nc, anchors]], # Detect(P2, P3, P4, P5) 55 | ] 56 | -------------------------------------------------------------------------------- /models/hub/yolov5-p34.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.33 # model depth multiple 6 | width_multiple: 0.50 # layer channel multiple 7 | anchors: 3 # AutoAnchor evolves 3 anchors per P output layer 8 | 9 | # YOLOv5 v6.0 backbone 10 | backbone: 11 | # [from, number, module, args] 12 | [ 13 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 14 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 15 | [-1, 3, C3, [128]], 16 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 17 | [-1, 6, C3, [256]], 18 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 19 | [-1, 9, C3, [512]], 20 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 21 | [-1, 3, C3, [1024]], 22 | [-1, 1, SPPF, [1024, 5]], # 9 23 | ] 24 | 25 | # YOLOv5 v6.0 head with (P3, P4) outputs 26 | head: [ 27 | [-1, 1, Conv, [512, 1, 1]], 28 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 29 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 30 | [-1, 3, C3, [512, False]], # 13 31 | 32 | [-1, 1, Conv, [256, 1, 1]], 33 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 34 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 35 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 36 | 37 | [-1, 1, Conv, [256, 3, 2]], 38 | [[-1, 14], 1, Concat, [1]], # cat head P4 39 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 40 | 41 | [[17, 20], 1, Detect, [nc, anchors]], # Detect(P3, P4) 42 | ] 43 | -------------------------------------------------------------------------------- /models/hub/yolov5-p6.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 3 # AutoAnchor evolves 3 anchors per P output layer 8 | 9 | # YOLOv5 v6.0 backbone 10 | backbone: 11 | # [from, number, module, args] 12 | [ 13 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 14 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 15 | [-1, 3, C3, [128]], 16 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 17 | [-1, 6, C3, [256]], 18 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 19 | [-1, 9, C3, [512]], 20 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32 21 | [-1, 3, C3, [768]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64 23 | [-1, 3, C3, [1024]], 24 | [-1, 1, SPPF, [1024, 5]], # 11 25 | ] 26 | 27 | # YOLOv5 v6.0 head with (P3, P4, P5, P6) outputs 28 | head: [ 29 | [-1, 1, Conv, [768, 1, 1]], 30 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 31 | [[-1, 8], 1, Concat, [1]], # cat backbone P5 32 | [-1, 3, C3, [768, False]], # 15 33 | 34 | [-1, 1, Conv, [512, 1, 1]], 35 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 36 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 37 | [-1, 3, C3, [512, False]], # 19 38 | 39 | [-1, 1, Conv, [256, 1, 1]], 40 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 41 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 42 | [-1, 3, C3, [256, False]], # 23 (P3/8-small) 43 | 44 | [-1, 1, Conv, [256, 3, 2]], 45 | [[-1, 20], 1, Concat, [1]], # cat head P4 46 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium) 47 | 48 | [-1, 1, Conv, [512, 3, 2]], 49 | [[-1, 16], 1, Concat, [1]], # cat head P5 50 | [-1, 3, C3, [768, False]], # 29 (P5/32-large) 51 | 52 | [-1, 1, Conv, [768, 3, 2]], 53 | [[-1, 12], 1, Concat, [1]], # cat head P6 54 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge) 55 | 56 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6) 57 | ] 58 | -------------------------------------------------------------------------------- /models/hub/yolov5-p7.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 3 # AutoAnchor evolves 3 anchors per P output layer 8 | 9 | # YOLOv5 v6.0 backbone 10 | backbone: 11 | # [from, number, module, args] 12 | [ 13 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 14 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 15 | [-1, 3, C3, [128]], 16 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 17 | [-1, 6, C3, [256]], 18 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 19 | [-1, 9, C3, [512]], 20 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32 21 | [-1, 3, C3, [768]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64 23 | [-1, 3, C3, [1024]], 24 | [-1, 1, Conv, [1280, 3, 2]], # 11-P7/128 25 | [-1, 3, C3, [1280]], 26 | [-1, 1, SPPF, [1280, 5]], # 13 27 | ] 28 | 29 | # YOLOv5 v6.0 head with (P3, P4, P5, P6, P7) outputs 30 | head: [ 31 | [-1, 1, Conv, [1024, 1, 1]], 32 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 33 | [[-1, 10], 1, Concat, [1]], # cat backbone P6 34 | [-1, 3, C3, [1024, False]], # 17 35 | 36 | [-1, 1, Conv, [768, 1, 1]], 37 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 38 | [[-1, 8], 1, Concat, [1]], # cat backbone P5 39 | [-1, 3, C3, [768, False]], # 21 40 | 41 | [-1, 1, Conv, [512, 1, 1]], 42 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 43 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 44 | [-1, 3, C3, [512, False]], # 25 45 | 46 | [-1, 1, Conv, [256, 1, 1]], 47 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 48 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 49 | [-1, 3, C3, [256, False]], # 29 (P3/8-small) 50 | 51 | [-1, 1, Conv, [256, 3, 2]], 52 | [[-1, 26], 1, Concat, [1]], # cat head P4 53 | [-1, 3, C3, [512, False]], # 32 (P4/16-medium) 54 | 55 | [-1, 1, Conv, [512, 3, 2]], 56 | [[-1, 22], 1, Concat, [1]], # cat head P5 57 | [-1, 3, C3, [768, False]], # 35 (P5/32-large) 58 | 59 | [-1, 1, Conv, [768, 3, 2]], 60 | [[-1, 18], 1, Concat, [1]], # cat head P6 61 | [-1, 3, C3, [1024, False]], # 38 (P6/64-xlarge) 62 | 63 | [-1, 1, Conv, [1024, 3, 2]], 64 | [[-1, 14], 1, Concat, [1]], # cat head P7 65 | [-1, 3, C3, [1280, False]], # 41 (P7/128-xxlarge) 66 | 67 | [[29, 32, 35, 38, 41], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6, P7) 68 | ] 69 | -------------------------------------------------------------------------------- /models/hub/yolov5-panet.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 PANet head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/hub/yolov5l6.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 8 | - [19, 27, 44, 40, 38, 94] # P3/8 9 | - [96, 68, 86, 152, 180, 137] # P4/16 10 | - [140, 301, 303, 264, 238, 542] # P5/32 11 | - [436, 615, 739, 380, 925, 792] # P6/64 12 | 13 | # YOLOv5 v6.0 backbone 14 | backbone: 15 | # [from, number, module, args] 16 | [ 17 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 18 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 19 | [-1, 3, C3, [128]], 20 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 21 | [-1, 6, C3, [256]], 22 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 23 | [-1, 9, C3, [512]], 24 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32 25 | [-1, 3, C3, [768]], 26 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64 27 | [-1, 3, C3, [1024]], 28 | [-1, 1, SPPF, [1024, 5]], # 11 29 | ] 30 | 31 | # YOLOv5 v6.0 head 32 | head: [ 33 | [-1, 1, Conv, [768, 1, 1]], 34 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 35 | [[-1, 8], 1, Concat, [1]], # cat backbone P5 36 | [-1, 3, C3, [768, False]], # 15 37 | 38 | [-1, 1, Conv, [512, 1, 1]], 39 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 40 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 41 | [-1, 3, C3, [512, False]], # 19 42 | 43 | [-1, 1, Conv, [256, 1, 1]], 44 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 45 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 46 | [-1, 3, C3, [256, False]], # 23 (P3/8-small) 47 | 48 | [-1, 1, Conv, [256, 3, 2]], 49 | [[-1, 20], 1, Concat, [1]], # cat head P4 50 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium) 51 | 52 | [-1, 1, Conv, [512, 3, 2]], 53 | [[-1, 16], 1, Concat, [1]], # cat head P5 54 | [-1, 3, C3, [768, False]], # 29 (P5/32-large) 55 | 56 | [-1, 1, Conv, [768, 3, 2]], 57 | [[-1, 12], 1, Concat, [1]], # cat head P6 58 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge) 59 | 60 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6) 61 | ] 62 | -------------------------------------------------------------------------------- /models/hub/yolov5m6.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.67 # model depth multiple 6 | width_multiple: 0.75 # layer channel multiple 7 | anchors: 8 | - [19, 27, 44, 40, 38, 94] # P3/8 9 | - [96, 68, 86, 152, 180, 137] # P4/16 10 | - [140, 301, 303, 264, 238, 542] # P5/32 11 | - [436, 615, 739, 380, 925, 792] # P6/64 12 | 13 | # YOLOv5 v6.0 backbone 14 | backbone: 15 | # [from, number, module, args] 16 | [ 17 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 18 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 19 | [-1, 3, C3, [128]], 20 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 21 | [-1, 6, C3, [256]], 22 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 23 | [-1, 9, C3, [512]], 24 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32 25 | [-1, 3, C3, [768]], 26 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64 27 | [-1, 3, C3, [1024]], 28 | [-1, 1, SPPF, [1024, 5]], # 11 29 | ] 30 | 31 | # YOLOv5 v6.0 head 32 | head: [ 33 | [-1, 1, Conv, [768, 1, 1]], 34 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 35 | [[-1, 8], 1, Concat, [1]], # cat backbone P5 36 | [-1, 3, C3, [768, False]], # 15 37 | 38 | [-1, 1, Conv, [512, 1, 1]], 39 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 40 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 41 | [-1, 3, C3, [512, False]], # 19 42 | 43 | [-1, 1, Conv, [256, 1, 1]], 44 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 45 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 46 | [-1, 3, C3, [256, False]], # 23 (P3/8-small) 47 | 48 | [-1, 1, Conv, [256, 3, 2]], 49 | [[-1, 20], 1, Concat, [1]], # cat head P4 50 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium) 51 | 52 | [-1, 1, Conv, [512, 3, 2]], 53 | [[-1, 16], 1, Concat, [1]], # cat head P5 54 | [-1, 3, C3, [768, False]], # 29 (P5/32-large) 55 | 56 | [-1, 1, Conv, [768, 3, 2]], 57 | [[-1, 12], 1, Concat, [1]], # cat head P6 58 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge) 59 | 60 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6) 61 | ] 62 | -------------------------------------------------------------------------------- /models/hub/yolov5n6.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.33 # model depth multiple 6 | width_multiple: 0.25 # layer channel multiple 7 | anchors: 8 | - [19, 27, 44, 40, 38, 94] # P3/8 9 | - [96, 68, 86, 152, 180, 137] # P4/16 10 | - [140, 301, 303, 264, 238, 542] # P5/32 11 | - [436, 615, 739, 380, 925, 792] # P6/64 12 | 13 | # YOLOv5 v6.0 backbone 14 | backbone: 15 | # [from, number, module, args] 16 | [ 17 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 18 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 19 | [-1, 3, C3, [128]], 20 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 21 | [-1, 6, C3, [256]], 22 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 23 | [-1, 9, C3, [512]], 24 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32 25 | [-1, 3, C3, [768]], 26 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64 27 | [-1, 3, C3, [1024]], 28 | [-1, 1, SPPF, [1024, 5]], # 11 29 | ] 30 | 31 | # YOLOv5 v6.0 head 32 | head: [ 33 | [-1, 1, Conv, [768, 1, 1]], 34 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 35 | [[-1, 8], 1, Concat, [1]], # cat backbone P5 36 | [-1, 3, C3, [768, False]], # 15 37 | 38 | [-1, 1, Conv, [512, 1, 1]], 39 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 40 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 41 | [-1, 3, C3, [512, False]], # 19 42 | 43 | [-1, 1, Conv, [256, 1, 1]], 44 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 45 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 46 | [-1, 3, C3, [256, False]], # 23 (P3/8-small) 47 | 48 | [-1, 1, Conv, [256, 3, 2]], 49 | [[-1, 20], 1, Concat, [1]], # cat head P4 50 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium) 51 | 52 | [-1, 1, Conv, [512, 3, 2]], 53 | [[-1, 16], 1, Concat, [1]], # cat head P5 54 | [-1, 3, C3, [768, False]], # 29 (P5/32-large) 55 | 56 | [-1, 1, Conv, [768, 3, 2]], 57 | [[-1, 12], 1, Concat, [1]], # cat head P6 58 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge) 59 | 60 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6) 61 | ] 62 | -------------------------------------------------------------------------------- /models/hub/yolov5s-LeakyReLU.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | activation: nn.LeakyReLU(0.1) # <----- Conv() activation used throughout entire YOLOv5 model 6 | depth_multiple: 0.33 # model depth multiple 7 | width_multiple: 0.50 # layer channel multiple 8 | anchors: 9 | - [10, 13, 16, 30, 33, 23] # P3/8 10 | - [30, 61, 62, 45, 59, 119] # P4/16 11 | - [116, 90, 156, 198, 373, 326] # P5/32 12 | 13 | # YOLOv5 v6.0 backbone 14 | backbone: 15 | # [from, number, module, args] 16 | [ 17 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 18 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 19 | [-1, 3, C3, [128]], 20 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 21 | [-1, 6, C3, [256]], 22 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 23 | [-1, 9, C3, [512]], 24 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 25 | [-1, 3, C3, [1024]], 26 | [-1, 1, SPPF, [1024, 5]], # 9 27 | ] 28 | 29 | # YOLOv5 v6.0 head 30 | head: [ 31 | [-1, 1, Conv, [512, 1, 1]], 32 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 33 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 34 | [-1, 3, C3, [512, False]], # 13 35 | 36 | [-1, 1, Conv, [256, 1, 1]], 37 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 38 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 39 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 40 | 41 | [-1, 1, Conv, [256, 3, 2]], 42 | [[-1, 14], 1, Concat, [1]], # cat head P4 43 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 44 | 45 | [-1, 1, Conv, [512, 3, 2]], 46 | [[-1, 10], 1, Concat, [1]], # cat head P5 47 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 48 | 49 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 50 | ] 51 | -------------------------------------------------------------------------------- /models/hub/yolov5s-ghost.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.33 # model depth multiple 6 | width_multiple: 0.50 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, GhostConv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3Ghost, [128]], 19 | [-1, 1, GhostConv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3Ghost, [256]], 21 | [-1, 1, GhostConv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3Ghost, [512]], 23 | [-1, 1, GhostConv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3Ghost, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, GhostConv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3Ghost, [512, False]], # 13 34 | 35 | [-1, 1, GhostConv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3Ghost, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, GhostConv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3Ghost, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, GhostConv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3Ghost, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/hub/yolov5s-transformer.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.33 # model depth multiple 6 | width_multiple: 0.50 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3TR, [1024]], # 9 <--- C3TR() Transformer module 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/hub/yolov5s6.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.33 # model depth multiple 6 | width_multiple: 0.50 # layer channel multiple 7 | anchors: 8 | - [19, 27, 44, 40, 38, 94] # P3/8 9 | - [96, 68, 86, 152, 180, 137] # P4/16 10 | - [140, 301, 303, 264, 238, 542] # P5/32 11 | - [436, 615, 739, 380, 925, 792] # P6/64 12 | 13 | # YOLOv5 v6.0 backbone 14 | backbone: 15 | # [from, number, module, args] 16 | [ 17 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 18 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 19 | [-1, 3, C3, [128]], 20 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 21 | [-1, 6, C3, [256]], 22 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 23 | [-1, 9, C3, [512]], 24 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32 25 | [-1, 3, C3, [768]], 26 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64 27 | [-1, 3, C3, [1024]], 28 | [-1, 1, SPPF, [1024, 5]], # 11 29 | ] 30 | 31 | # YOLOv5 v6.0 head 32 | head: [ 33 | [-1, 1, Conv, [768, 1, 1]], 34 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 35 | [[-1, 8], 1, Concat, [1]], # cat backbone P5 36 | [-1, 3, C3, [768, False]], # 15 37 | 38 | [-1, 1, Conv, [512, 1, 1]], 39 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 40 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 41 | [-1, 3, C3, [512, False]], # 19 42 | 43 | [-1, 1, Conv, [256, 1, 1]], 44 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 45 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 46 | [-1, 3, C3, [256, False]], # 23 (P3/8-small) 47 | 48 | [-1, 1, Conv, [256, 3, 2]], 49 | [[-1, 20], 1, Concat, [1]], # cat head P4 50 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium) 51 | 52 | [-1, 1, Conv, [512, 3, 2]], 53 | [[-1, 16], 1, Concat, [1]], # cat head P5 54 | [-1, 3, C3, [768, False]], # 29 (P5/32-large) 55 | 56 | [-1, 1, Conv, [768, 3, 2]], 57 | [[-1, 12], 1, Concat, [1]], # cat head P6 58 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge) 59 | 60 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6) 61 | ] 62 | -------------------------------------------------------------------------------- /models/hub/yolov5x6.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.33 # model depth multiple 6 | width_multiple: 1.25 # layer channel multiple 7 | anchors: 8 | - [19, 27, 44, 40, 38, 94] # P3/8 9 | - [96, 68, 86, 152, 180, 137] # P4/16 10 | - [140, 301, 303, 264, 238, 542] # P5/32 11 | - [436, 615, 739, 380, 925, 792] # P6/64 12 | 13 | # YOLOv5 v6.0 backbone 14 | backbone: 15 | # [from, number, module, args] 16 | [ 17 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 18 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 19 | [-1, 3, C3, [128]], 20 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 21 | [-1, 6, C3, [256]], 22 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 23 | [-1, 9, C3, [512]], 24 | [-1, 1, Conv, [768, 3, 2]], # 7-P5/32 25 | [-1, 3, C3, [768]], 26 | [-1, 1, Conv, [1024, 3, 2]], # 9-P6/64 27 | [-1, 3, C3, [1024]], 28 | [-1, 1, SPPF, [1024, 5]], # 11 29 | ] 30 | 31 | # YOLOv5 v6.0 head 32 | head: [ 33 | [-1, 1, Conv, [768, 1, 1]], 34 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 35 | [[-1, 8], 1, Concat, [1]], # cat backbone P5 36 | [-1, 3, C3, [768, False]], # 15 37 | 38 | [-1, 1, Conv, [512, 1, 1]], 39 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 40 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 41 | [-1, 3, C3, [512, False]], # 19 42 | 43 | [-1, 1, Conv, [256, 1, 1]], 44 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 45 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 46 | [-1, 3, C3, [256, False]], # 23 (P3/8-small) 47 | 48 | [-1, 1, Conv, [256, 3, 2]], 49 | [[-1, 20], 1, Concat, [1]], # cat head P4 50 | [-1, 3, C3, [512, False]], # 26 (P4/16-medium) 51 | 52 | [-1, 1, Conv, [512, 3, 2]], 53 | [[-1, 16], 1, Concat, [1]], # cat head P5 54 | [-1, 3, C3, [768, False]], # 29 (P5/32-large) 55 | 56 | [-1, 1, Conv, [768, 3, 2]], 57 | [[-1, 12], 1, Concat, [1]], # cat head P6 58 | [-1, 3, C3, [1024, False]], # 32 (P6/64-xlarge) 59 | 60 | [[23, 26, 29, 32], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5, P6) 61 | ] 62 | -------------------------------------------------------------------------------- /models/segment/yolov5l-seg.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/segment/yolov5m-seg.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.67 # model depth multiple 6 | width_multiple: 0.75 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/segment/yolov5n-seg.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.33 # model depth multiple 6 | width_multiple: 0.25 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/segment/yolov5s-seg.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.33 # model depth multiple 6 | width_multiple: 0.5 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/segment/yolov5x-seg.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.33 # model depth multiple 6 | width_multiple: 1.25 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Segment, [nc, anchors, 32, 256]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/yolov5l.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.0 # model depth multiple 6 | width_multiple: 1.0 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/yolov5m.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.67 # model depth multiple 6 | width_multiple: 0.75 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/yolov5n.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.33 # model depth multiple 6 | width_multiple: 0.25 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/yolov5s.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 0.33 # model depth multiple 6 | width_multiple: 0.50 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /models/yolov5x.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Parameters 4 | nc: 80 # number of classes 5 | depth_multiple: 1.33 # model depth multiple 6 | width_multiple: 1.25 # layer channel multiple 7 | anchors: 8 | - [10, 13, 16, 30, 33, 23] # P3/8 9 | - [30, 61, 62, 45, 59, 119] # P4/16 10 | - [116, 90, 156, 198, 373, 326] # P5/32 11 | 12 | # YOLOv5 v6.0 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [ 16 | [-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2 17 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 18 | [-1, 3, C3, [128]], 19 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 20 | [-1, 6, C3, [256]], 21 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 22 | [-1, 9, C3, [512]], 23 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 24 | [-1, 3, C3, [1024]], 25 | [-1, 1, SPPF, [1024, 5]], # 9 26 | ] 27 | 28 | # YOLOv5 v6.0 head 29 | head: [ 30 | [-1, 1, Conv, [512, 1, 1]], 31 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 3, C3, [512, False]], # 13 34 | 35 | [-1, 1, Conv, [256, 1, 1]], 36 | [-1, 1, nn.Upsample, [None, 2, "nearest"]], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 3, C3, [256, False]], # 17 (P3/8-small) 39 | 40 | [-1, 1, Conv, [256, 3, 2]], 41 | [[-1, 14], 1, Concat, [1]], # cat head P4 42 | [-1, 3, C3, [512, False]], # 20 (P4/16-medium) 43 | 44 | [-1, 1, Conv, [512, 3, 2]], 45 | [[-1, 10], 1, Concat, [1]], # cat head P5 46 | [-1, 3, C3, [1024, False]], # 23 (P5/32-large) 47 | 48 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 49 | ] 50 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Overview: 4 | # This pyproject.toml file manages the build, packaging, and distribution of the Ultralytics library. 5 | # It defines essential project metadata, dependencies, and settings used to develop and deploy the library. 6 | 7 | # Key Sections: 8 | # - [build-system]: Specifies the build requirements and backend (e.g., setuptools, wheel). 9 | # - [project]: Includes details like name, version, description, authors, dependencies and more. 10 | # - [project.optional-dependencies]: Provides additional, optional packages for extended features. 11 | # - [tool.*]: Configures settings for various tools (pytest, yapf, etc.) used in the project. 12 | 13 | # Installation: 14 | # The Ultralytics library can be installed using the command: 'pip install ultralytics' 15 | # For development purposes, you can install the package in editable mode with: 'pip install -e .' 16 | # This approach allows for real-time code modifications without the need for re-installation. 17 | 18 | # Documentation: 19 | # For comprehensive documentation and usage instructions, visit: https://docs.ultralytics.com 20 | 21 | [build-system] 22 | requires = ["setuptools>=43.0.0", "wheel"] 23 | build-backend = "setuptools.build_meta" 24 | 25 | # Project settings ----------------------------------------------------------------------------------------------------- 26 | [project] 27 | version = "7.0.0" 28 | name = "YOLOv5" 29 | description = "Ultralytics YOLOv5 for SOTA object detection, instance segmentation and image classification." 30 | readme = "README.md" 31 | requires-python = ">=3.8" 32 | license = { "text" = "AGPL-3.0" } 33 | keywords = ["machine-learning", "deep-learning", "computer-vision", "ML", "DL", "AI", "YOLO", "YOLOv3", "YOLOv5", "YOLOv8", "HUB", "Ultralytics"] 34 | authors = [ 35 | { name = "Glenn Jocher" }, 36 | { name = "Ayush Chaurasia" }, 37 | { name = "Jing Qiu" } 38 | ] 39 | maintainers = [ 40 | { name = "Glenn Jocher" }, 41 | { name = "Ayush Chaurasia" }, 42 | { name = "Jing Qiu" } 43 | ] 44 | classifiers = [ 45 | "Development Status :: 4 - Beta", 46 | "Intended Audience :: Developers", 47 | "Intended Audience :: Education", 48 | "Intended Audience :: Science/Research", 49 | "License :: OSI Approved :: GNU Affero General Public License v3 or later (AGPLv3+)", 50 | "Programming Language :: Python :: 3", 51 | "Programming Language :: Python :: 3.8", 52 | "Programming Language :: Python :: 3.9", 53 | "Programming Language :: Python :: 3.10", 54 | "Programming Language :: Python :: 3.11", 55 | "Topic :: Software Development", 56 | "Topic :: Scientific/Engineering", 57 | "Topic :: Scientific/Engineering :: Artificial Intelligence", 58 | "Topic :: Scientific/Engineering :: Image Recognition", 59 | "Operating System :: POSIX :: Linux", 60 | "Operating System :: MacOS", 61 | "Operating System :: Microsoft :: Windows", 62 | ] 63 | 64 | # Required dependencies ------------------------------------------------------------------------------------------------ 65 | dependencies = [ 66 | "matplotlib>=3.3.0", 67 | "numpy>=1.22.2", 68 | "opencv-python>=4.6.0", 69 | "pillow>=7.1.2", 70 | "pyyaml>=5.3.1", 71 | "requests>=2.23.0", 72 | "scipy>=1.4.1", 73 | "torch>=1.8.0", 74 | "torchvision>=0.9.0", 75 | "tqdm>=4.64.0", # progress bars 76 | "psutil", # system utilization 77 | "py-cpuinfo", # display CPU info 78 | "thop>=0.1.1", # FLOPs computation 79 | "pandas>=1.1.4", 80 | "seaborn>=0.11.0", # plotting 81 | "ultralytics>=8.1.47" 82 | ] 83 | 84 | # Optional dependencies ------------------------------------------------------------------------------------------------ 85 | [project.optional-dependencies] 86 | dev = [ 87 | "ipython", 88 | "check-manifest", 89 | "pytest", 90 | "pytest-cov", 91 | "coverage[toml]", 92 | "mkdocs-material", 93 | "mkdocstrings[python]", 94 | "mkdocs-ultralytics-plugin>=0.0.34", # for meta descriptions and images, dates and authors 95 | ] 96 | export = [ 97 | "onnx>=1.12.0", # ONNX export 98 | "coremltools>=7.0; platform_system != 'Windows'", # CoreML only supported on macOS and Linux 99 | "openvino-dev>=2023.0", # OpenVINO export 100 | "tensorflow>=2.0.0", # TF bug https://github.com/ultralytics/ultralytics/issues/5161 101 | "tensorflowjs>=3.9.0", # TF.js export, automatically installs tensorflow 102 | ] 103 | # tensorflow>=2.4.1,<=2.13.1 # TF exports (-cpu, -aarch64, -macos) 104 | # tflite-support # for TFLite model metadata 105 | # scikit-learn==0.19.2 # CoreML quantization 106 | # nvidia-pyindex # TensorRT export 107 | # nvidia-tensorrt # TensorRT export 108 | logging = [ 109 | "comet", # https://docs.ultralytics.com/integrations/comet/ 110 | "tensorboard>=2.13.0", 111 | "dvclive>=2.12.0", 112 | ] 113 | extra = [ 114 | "ipython", # interactive notebook 115 | "albumentations>=1.0.3", # training augmentations 116 | "pycocotools>=2.0.6", # COCO mAP 117 | ] 118 | 119 | [project.urls] 120 | "Bug Reports" = "https://github.com/ultralytics/yolov5/issues" 121 | "Funding" = "https://ultralytics.com" 122 | "Source" = "https://github.com/ultralytics/yolov5/" 123 | 124 | # Tools settings ------------------------------------------------------------------------------------------------------- 125 | [tool.pytest] 126 | norecursedirs = [".git", "dist", "build"] 127 | addopts = "--doctest-modules --durations=30 --color=yes" 128 | 129 | [tool.isort] 130 | line_length = 120 131 | multi_line_output = 0 132 | 133 | [tool.ruff] 134 | line-length = 120 135 | 136 | [tool.docformatter] 137 | wrap-summaries = 120 138 | wrap-descriptions = 120 139 | in-place = true 140 | pre-summary-newline = true 141 | close-quotes-on-newline = true 142 | 143 | [tool.codespell] 144 | ignore-words-list = "crate,nd,strack,dota,ane,segway,fo,gool,winn,commend" 145 | skip = '*.csv,*venv*,docs/??/,docs/mkdocs_??.yml' 146 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # YOLOv5 requirements 2 | # Usage: pip install -r requirements.txt 3 | 4 | # Base ------------------------------------------------------------------------ 5 | gitpython>=3.1.30 6 | matplotlib>=3.3 7 | numpy>=1.23.5 8 | opencv-python>=4.1.1 9 | pillow>=10.3.0 10 | psutil # system resources 11 | PyYAML>=5.3.1 12 | requests>=2.32.2 13 | scipy>=1.4.1 14 | thop>=0.1.1 # FLOPs computation 15 | torch>=1.8.0 # see https://pytorch.org/get-started/locally (recommended) 16 | torchvision>=0.9.0 17 | tqdm>=4.66.3 18 | ultralytics>=8.2.34 # https://ultralytics.com 19 | # protobuf<=3.20.1 # https://github.com/ultralytics/yolov5/issues/8012 20 | 21 | # Logging --------------------------------------------------------------------- 22 | # tensorboard>=2.4.1 23 | # clearml>=1.2.0 24 | # comet 25 | 26 | # Plotting -------------------------------------------------------------------- 27 | pandas>=1.1.4 28 | seaborn>=0.11.0 29 | 30 | # Export ---------------------------------------------------------------------- 31 | # coremltools>=6.0 # CoreML export 32 | # onnx>=1.10.0 # ONNX export 33 | # onnx-simplifier>=0.4.1 # ONNX simplifier 34 | # nvidia-pyindex # TensorRT export 35 | # nvidia-tensorrt # TensorRT export 36 | # scikit-learn<=1.1.2 # CoreML quantization 37 | # tensorflow>=2.4.0,<=2.13.1 # TF exports (-cpu, -aarch64, -macos) 38 | # tensorflowjs>=3.9.0 # TF.js export 39 | # openvino-dev>=2023.0 # OpenVINO export 40 | 41 | # Deploy ---------------------------------------------------------------------- 42 | setuptools>=70.0.0 # Snyk vulnerability fix 43 | # tritonclient[all]~=2.24.0 44 | 45 | # Extras ---------------------------------------------------------------------- 46 | # ipython # interactive notebook 47 | # mss # screenshots 48 | # albumentations>=1.0.3 49 | # pycocotools>=2.0.6 # COCO mAP 50 | -------------------------------------------------------------------------------- /utils/__init__.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | """utils/initialization.""" 3 | 4 | import contextlib 5 | import platform 6 | import threading 7 | 8 | 9 | def emojis(str=""): 10 | """Returns an emoji-safe version of a string, stripped of emojis on Windows platforms.""" 11 | return str.encode().decode("ascii", "ignore") if platform.system() == "Windows" else str 12 | 13 | 14 | class TryExcept(contextlib.ContextDecorator): 15 | """A context manager and decorator for error handling that prints an optional message with emojis on exception.""" 16 | 17 | def __init__(self, msg=""): 18 | """Initializes TryExcept with an optional message, used as a decorator or context manager for error handling.""" 19 | self.msg = msg 20 | 21 | def __enter__(self): 22 | """Enter the runtime context related to this object for error handling with an optional message.""" 23 | pass 24 | 25 | def __exit__(self, exc_type, value, traceback): 26 | """Context manager exit method that prints an error message with emojis if an exception occurred, always returns 27 | True. 28 | """ 29 | if value: 30 | print(emojis(f"{self.msg}{': ' if self.msg else ''}{value}")) 31 | return True 32 | 33 | 34 | def threaded(func): 35 | """Decorator @threaded to run a function in a separate thread, returning the thread instance.""" 36 | 37 | def wrapper(*args, **kwargs): 38 | """Runs the decorated function in a separate daemon thread and returns the thread instance.""" 39 | thread = threading.Thread(target=func, args=args, kwargs=kwargs, daemon=True) 40 | thread.start() 41 | return thread 42 | 43 | return wrapper 44 | 45 | 46 | def join_threads(verbose=False): 47 | """ 48 | Joins all daemon threads, optionally printing their names if verbose is True. 49 | 50 | Example: atexit.register(lambda: join_threads()) 51 | """ 52 | main_thread = threading.current_thread() 53 | for t in threading.enumerate(): 54 | if t is not main_thread: 55 | if verbose: 56 | print(f"Joining thread {t.name}") 57 | t.join() 58 | 59 | 60 | def notebook_init(verbose=True): 61 | """Initializes notebook environment by checking requirements, cleaning up, and displaying system info.""" 62 | print("Checking setup...") 63 | 64 | import os 65 | import shutil 66 | 67 | from ultralytics.utils.checks import check_requirements 68 | 69 | from utils.general import check_font, is_colab 70 | from utils.torch_utils import select_device # imports 71 | 72 | check_font() 73 | 74 | import psutil 75 | 76 | if check_requirements("wandb", install=False): 77 | os.system("pip uninstall -y wandb") # eliminate unexpected account creation prompt with infinite hang 78 | if is_colab(): 79 | shutil.rmtree("/content/sample_data", ignore_errors=True) # remove colab /sample_data directory 80 | 81 | # System info 82 | display = None 83 | if verbose: 84 | gb = 1 << 30 # bytes to GiB (1024 ** 3) 85 | ram = psutil.virtual_memory().total 86 | total, used, free = shutil.disk_usage("/") 87 | with contextlib.suppress(Exception): # clear display if ipython is installed 88 | from IPython import display 89 | 90 | display.clear_output() 91 | s = f"({os.cpu_count()} CPUs, {ram / gb:.1f} GB RAM, {(total - free) / gb:.1f}/{total / gb:.1f} GB disk)" 92 | else: 93 | s = "" 94 | 95 | select_device(newline=False) 96 | print(emojis(f"Setup complete ✅ {s}")) 97 | return display 98 | -------------------------------------------------------------------------------- /utils/activations.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | """Activation functions.""" 3 | 4 | import torch 5 | import torch.nn as nn 6 | import torch.nn.functional as F 7 | 8 | 9 | class SiLU(nn.Module): 10 | """Applies the Sigmoid-weighted Linear Unit (SiLU) activation function, also known as Swish.""" 11 | 12 | @staticmethod 13 | def forward(x): 14 | """ 15 | Applies the Sigmoid-weighted Linear Unit (SiLU) activation function. 16 | 17 | https://arxiv.org/pdf/1606.08415.pdf. 18 | """ 19 | return x * torch.sigmoid(x) 20 | 21 | 22 | class Hardswish(nn.Module): 23 | """Applies the Hardswish activation function, which is efficient for mobile and embedded devices.""" 24 | 25 | @staticmethod 26 | def forward(x): 27 | """ 28 | Applies the Hardswish activation function, compatible with TorchScript, CoreML, and ONNX. 29 | 30 | Equivalent to x * F.hardsigmoid(x) 31 | """ 32 | return x * F.hardtanh(x + 3, 0.0, 6.0) / 6.0 # for TorchScript, CoreML and ONNX 33 | 34 | 35 | class Mish(nn.Module): 36 | """Mish activation https://github.com/digantamisra98/Mish.""" 37 | 38 | @staticmethod 39 | def forward(x): 40 | """Applies the Mish activation function, a smooth alternative to ReLU.""" 41 | return x * F.softplus(x).tanh() 42 | 43 | 44 | class MemoryEfficientMish(nn.Module): 45 | """Efficiently applies the Mish activation function using custom autograd for reduced memory usage.""" 46 | 47 | class F(torch.autograd.Function): 48 | """Implements a custom autograd function for memory-efficient Mish activation.""" 49 | 50 | @staticmethod 51 | def forward(ctx, x): 52 | """Applies the Mish activation function, a smooth ReLU alternative, to the input tensor `x`.""" 53 | ctx.save_for_backward(x) 54 | return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x))) 55 | 56 | @staticmethod 57 | def backward(ctx, grad_output): 58 | """Computes the gradient of the Mish activation function with respect to input `x`.""" 59 | x = ctx.saved_tensors[0] 60 | sx = torch.sigmoid(x) 61 | fx = F.softplus(x).tanh() 62 | return grad_output * (fx + x * sx * (1 - fx * fx)) 63 | 64 | def forward(self, x): 65 | """Applies the Mish activation function to the input tensor `x`.""" 66 | return self.F.apply(x) 67 | 68 | 69 | class FReLU(nn.Module): 70 | """FReLU activation https://arxiv.org/abs/2007.11824.""" 71 | 72 | def __init__(self, c1, k=3): # ch_in, kernel 73 | """Initializes FReLU activation with channel `c1` and kernel size `k`.""" 74 | super().__init__() 75 | self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1, bias=False) 76 | self.bn = nn.BatchNorm2d(c1) 77 | 78 | def forward(self, x): 79 | """ 80 | Applies FReLU activation with max operation between input and BN-convolved input. 81 | 82 | https://arxiv.org/abs/2007.11824 83 | """ 84 | return torch.max(x, self.bn(self.conv(x))) 85 | 86 | 87 | class AconC(nn.Module): 88 | """ 89 | ACON activation (activate or not) function. 90 | 91 | AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter 92 | See "Activate or Not: Learning Customized Activation" https://arxiv.org/pdf/2009.04759.pdf. 93 | """ 94 | 95 | def __init__(self, c1): 96 | """Initializes AconC with learnable parameters p1, p2, and beta for channel-wise activation control.""" 97 | super().__init__() 98 | self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1)) 99 | self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1)) 100 | self.beta = nn.Parameter(torch.ones(1, c1, 1, 1)) 101 | 102 | def forward(self, x): 103 | """Applies AconC activation function with learnable parameters for channel-wise control on input tensor x.""" 104 | dpx = (self.p1 - self.p2) * x 105 | return dpx * torch.sigmoid(self.beta * dpx) + self.p2 * x 106 | 107 | 108 | class MetaAconC(nn.Module): 109 | """ 110 | ACON activation (activate or not) function. 111 | 112 | AconC: (p1*x-p2*x) * sigmoid(beta*(p1*x-p2*x)) + p2*x, beta is a learnable parameter 113 | See "Activate or Not: Learning Customized Activation" https://arxiv.org/pdf/2009.04759.pdf. 114 | """ 115 | 116 | def __init__(self, c1, k=1, s=1, r=16): 117 | """Initializes MetaAconC with params: channel_in (c1), kernel size (k=1), stride (s=1), reduction (r=16).""" 118 | super().__init__() 119 | c2 = max(r, c1 // r) 120 | self.p1 = nn.Parameter(torch.randn(1, c1, 1, 1)) 121 | self.p2 = nn.Parameter(torch.randn(1, c1, 1, 1)) 122 | self.fc1 = nn.Conv2d(c1, c2, k, s, bias=True) 123 | self.fc2 = nn.Conv2d(c2, c1, k, s, bias=True) 124 | # self.bn1 = nn.BatchNorm2d(c2) 125 | # self.bn2 = nn.BatchNorm2d(c1) 126 | 127 | def forward(self, x): 128 | """Applies a forward pass transforming input `x` using learnable parameters and sigmoid activation.""" 129 | y = x.mean(dim=2, keepdims=True).mean(dim=3, keepdims=True) 130 | # batch-size 1 bug/instabilities https://github.com/ultralytics/yolov5/issues/2891 131 | # beta = torch.sigmoid(self.bn2(self.fc2(self.bn1(self.fc1(y))))) # bug/unstable 132 | beta = torch.sigmoid(self.fc2(self.fc1(y))) # bug patch BN layers removed 133 | dpx = (self.p1 - self.p2) * x 134 | return dpx * torch.sigmoid(beta * dpx) + self.p2 * x 135 | -------------------------------------------------------------------------------- /utils/autobatch.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | """Auto-batch utils.""" 3 | 4 | from copy import deepcopy 5 | 6 | import numpy as np 7 | import torch 8 | 9 | from utils.general import LOGGER, colorstr 10 | from utils.torch_utils import profile 11 | 12 | 13 | def check_train_batch_size(model, imgsz=640, amp=True): 14 | """Checks and computes optimal training batch size for YOLOv5 model, given image size and AMP setting.""" 15 | with torch.cuda.amp.autocast(amp): 16 | return autobatch(deepcopy(model).train(), imgsz) # compute optimal batch size 17 | 18 | 19 | def autobatch(model, imgsz=640, fraction=0.8, batch_size=16): 20 | """Estimates optimal YOLOv5 batch size using `fraction` of CUDA memory.""" 21 | # Usage: 22 | # import torch 23 | # from utils.autobatch import autobatch 24 | # model = torch.hub.load('ultralytics/yolov5', 'yolov5s', autoshape=False) 25 | # print(autobatch(model)) 26 | 27 | # Check device 28 | prefix = colorstr("AutoBatch: ") 29 | LOGGER.info(f"{prefix}Computing optimal batch size for --imgsz {imgsz}") 30 | device = next(model.parameters()).device # get model device 31 | if device.type == "cpu": 32 | LOGGER.info(f"{prefix}CUDA not detected, using default CPU batch-size {batch_size}") 33 | return batch_size 34 | if torch.backends.cudnn.benchmark: 35 | LOGGER.info(f"{prefix} ⚠️ Requires torch.backends.cudnn.benchmark=False, using default batch-size {batch_size}") 36 | return batch_size 37 | 38 | # Inspect CUDA memory 39 | gb = 1 << 30 # bytes to GiB (1024 ** 3) 40 | d = str(device).upper() # 'CUDA:0' 41 | properties = torch.cuda.get_device_properties(device) # device properties 42 | t = properties.total_memory / gb # GiB total 43 | r = torch.cuda.memory_reserved(device) / gb # GiB reserved 44 | a = torch.cuda.memory_allocated(device) / gb # GiB allocated 45 | f = t - (r + a) # GiB free 46 | LOGGER.info(f"{prefix}{d} ({properties.name}) {t:.2f}G total, {r:.2f}G reserved, {a:.2f}G allocated, {f:.2f}G free") 47 | 48 | # Profile batch sizes 49 | batch_sizes = [1, 2, 4, 8, 16] 50 | try: 51 | img = [torch.empty(b, 3, imgsz, imgsz) for b in batch_sizes] 52 | results = profile(img, model, n=3, device=device) 53 | except Exception as e: 54 | LOGGER.warning(f"{prefix}{e}") 55 | 56 | # Fit a solution 57 | y = [x[2] for x in results if x] # memory [2] 58 | p = np.polyfit(batch_sizes[: len(y)], y, deg=1) # first degree polynomial fit 59 | b = int((f * fraction - p[1]) / p[0]) # y intercept (optimal batch size) 60 | if None in results: # some sizes failed 61 | i = results.index(None) # first fail index 62 | if b >= batch_sizes[i]: # y intercept above failure point 63 | b = batch_sizes[max(i - 1, 0)] # select prior safe point 64 | if b < 1 or b > 1024: # b outside of safe range 65 | b = batch_size 66 | LOGGER.warning(f"{prefix}WARNING ⚠️ CUDA anomaly detected, recommend restart environment and retry command.") 67 | 68 | fraction = (np.polyval(p, b) + r + a) / t # actual fraction predicted 69 | LOGGER.info(f"{prefix}Using batch-size {b} for {d} {t * fraction:.2f}G/{t:.2f}G ({fraction * 100:.0f}%) ✅") 70 | return b 71 | -------------------------------------------------------------------------------- /utils/aws/__init__.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | -------------------------------------------------------------------------------- /utils/aws/mime.sh: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # AWS EC2 instance startup 'MIME' script https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/ 4 | # This script will run on every instance restart, not only on first start 5 | # --- DO NOT COPY ABOVE COMMENTS WHEN PASTING INTO USERDATA --- 6 | 7 | Content-Type: multipart/mixed 8 | boundary="//" 9 | MIME-Version: 1.0 10 | 11 | --// 12 | Content-Type: text/cloud-config 13 | charset="us-ascii" 14 | MIME-Version: 1.0 15 | Content-Transfer-Encoding: 7bit 16 | Content-Disposition: attachment 17 | filename="cloud-config.txt" 18 | 19 | #cloud-config 20 | cloud_final_modules: 21 | - [scripts-user, always] 22 | 23 | --// 24 | Content-Type: text/x-shellscript 25 | charset="us-ascii" 26 | MIME-Version: 1.0 27 | Content-Transfer-Encoding: 7bit 28 | Content-Disposition: attachment 29 | filename="userdata.txt" 30 | 31 | #!/bin/bash 32 | # --- paste contents of userdata.sh here --- 33 | --// 34 | -------------------------------------------------------------------------------- /utils/aws/resume.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Resume all interrupted trainings in yolov5/ dir including DDP trainings 4 | # Usage: $ python utils/aws/resume.py 5 | 6 | import os 7 | import sys 8 | from pathlib import Path 9 | 10 | import torch 11 | import yaml 12 | 13 | FILE = Path(__file__).resolve() 14 | ROOT = FILE.parents[2] # YOLOv5 root directory 15 | if str(ROOT) not in sys.path: 16 | sys.path.append(str(ROOT)) # add ROOT to PATH 17 | 18 | port = 0 # --master_port 19 | path = Path("").resolve() 20 | for last in path.rglob("*/**/last.pt"): 21 | ckpt = torch.load(last) 22 | if ckpt["optimizer"] is None: 23 | continue 24 | 25 | # Load opt.yaml 26 | with open(last.parent.parent / "opt.yaml", errors="ignore") as f: 27 | opt = yaml.safe_load(f) 28 | 29 | # Get device count 30 | d = opt["device"].split(",") # devices 31 | nd = len(d) # number of devices 32 | ddp = nd > 1 or (nd == 0 and torch.cuda.device_count() > 1) # distributed data parallel 33 | 34 | if ddp: # multi-GPU 35 | port += 1 36 | cmd = f"python -m torch.distributed.run --nproc_per_node {nd} --master_port {port} train.py --resume {last}" 37 | else: # single-GPU 38 | cmd = f"python train.py --resume {last}" 39 | 40 | cmd += " > /dev/null 2>&1 &" # redirect output to dev/null and run in daemon thread 41 | print(cmd) 42 | os.system(cmd) 43 | -------------------------------------------------------------------------------- /utils/aws/userdata.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 3 | 4 | # AWS EC2 instance startup script https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html 5 | # This script will run only once on first instance start (for a re-start script see mime.sh) 6 | # /home/ubuntu (ubuntu) or /home/ec2-user (amazon-linux) is working dir 7 | # Use >300 GB SSD 8 | 9 | cd home/ubuntu 10 | if [ ! -d yolov5 ]; then 11 | echo "Running first-time script." # install dependencies, download COCO, pull Docker 12 | git clone https://github.com/ultralytics/yolov5 -b master && sudo chmod -R 777 yolov5 13 | cd yolov5 14 | bash data/scripts/get_coco.sh && echo "COCO done." & 15 | sudo docker pull ultralytics/yolov5:latest && echo "Docker done." & 16 | python -m pip install --upgrade pip && pip install -r requirements.txt && python detect.py && echo "Requirements done." & 17 | wait && echo "All tasks done." # finish background tasks 18 | else 19 | echo "Running re-start script." # resume interrupted runs 20 | i=0 21 | list=$(sudo docker ps -qa) # container list i.e. $'one\ntwo\nthree\nfour' 22 | while IFS= read -r id; do 23 | ((i++)) 24 | echo "restarting container $i: $id" 25 | sudo docker start $id 26 | # sudo docker exec -it $id python train.py --resume # single-GPU 27 | sudo docker exec -d $id python utils/aws/resume.py # multi-scenario 28 | done <<< "$list" 29 | fi 30 | -------------------------------------------------------------------------------- /utils/callbacks.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | """Callback utils.""" 3 | 4 | import threading 5 | 6 | 7 | class Callbacks: 8 | """Handles all registered callbacks for YOLOv5 Hooks.""" 9 | 10 | def __init__(self): 11 | """Initializes a Callbacks object to manage registered YOLOv5 training event hooks.""" 12 | self._callbacks = { 13 | "on_pretrain_routine_start": [], 14 | "on_pretrain_routine_end": [], 15 | "on_train_start": [], 16 | "on_train_epoch_start": [], 17 | "on_train_batch_start": [], 18 | "optimizer_step": [], 19 | "on_before_zero_grad": [], 20 | "on_train_batch_end": [], 21 | "on_train_epoch_end": [], 22 | "on_val_start": [], 23 | "on_val_batch_start": [], 24 | "on_val_image_end": [], 25 | "on_val_batch_end": [], 26 | "on_val_end": [], 27 | "on_fit_epoch_end": [], # fit = train + val 28 | "on_model_save": [], 29 | "on_train_end": [], 30 | "on_params_update": [], 31 | "teardown": [], 32 | } 33 | self.stop_training = False # set True to interrupt training 34 | 35 | def register_action(self, hook, name="", callback=None): 36 | """ 37 | Register a new action to a callback hook. 38 | 39 | Args: 40 | hook: The callback hook name to register the action to 41 | name: The name of the action for later reference 42 | callback: The callback to fire 43 | """ 44 | assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}" 45 | assert callable(callback), f"callback '{callback}' is not callable" 46 | self._callbacks[hook].append({"name": name, "callback": callback}) 47 | 48 | def get_registered_actions(self, hook=None): 49 | """ 50 | Returns all the registered actions by callback hook. 51 | 52 | Args: 53 | hook: The name of the hook to check, defaults to all 54 | """ 55 | return self._callbacks[hook] if hook else self._callbacks 56 | 57 | def run(self, hook, *args, thread=False, **kwargs): 58 | """ 59 | Loop through the registered actions and fire all callbacks on main thread. 60 | 61 | Args: 62 | hook: The name of the hook to check, defaults to all 63 | args: Arguments to receive from YOLOv5 64 | thread: (boolean) Run callbacks in daemon thread 65 | kwargs: Keyword Arguments to receive from YOLOv5 66 | """ 67 | assert hook in self._callbacks, f"hook '{hook}' not found in callbacks {self._callbacks}" 68 | for logger in self._callbacks[hook]: 69 | if thread: 70 | threading.Thread(target=logger["callback"], args=args, kwargs=kwargs, daemon=True).start() 71 | else: 72 | logger["callback"](*args, **kwargs) 73 | -------------------------------------------------------------------------------- /utils/docker/Dockerfile: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Builds ultralytics/yolov5:latest image on DockerHub https://hub.docker.com/r/ultralytics/yolov5 4 | # Image is CUDA-optimized for YOLOv5 single/multi-GPU training and inference 5 | 6 | # Start FROM PyTorch image https://hub.docker.com/r/pytorch/pytorch 7 | FROM pytorch/pytorch:2.0.0-cuda11.7-cudnn8-runtime 8 | 9 | # Downloads to user config dir 10 | ADD https://ultralytics.com/assets/Arial.ttf https://ultralytics.com/assets/Arial.Unicode.ttf /root/.config/Ultralytics/ 11 | 12 | # Install linux packages 13 | ENV DEBIAN_FRONTEND noninteractive 14 | RUN apt update 15 | RUN TZ=Etc/UTC apt install -y tzdata 16 | RUN apt install --no-install-recommends -y gcc git zip curl htop libgl1 libglib2.0-0 libpython3-dev gnupg 17 | # RUN alias python=python3 18 | 19 | # Security updates 20 | # https://security.snyk.io/vuln/SNYK-UBUNTU1804-OPENSSL-3314796 21 | RUN apt upgrade --no-install-recommends -y openssl 22 | 23 | # Create working directory 24 | RUN rm -rf /usr/src/app && mkdir -p /usr/src/app 25 | WORKDIR /usr/src/app 26 | 27 | # Copy contents 28 | COPY . /usr/src/app 29 | 30 | # Install pip packages 31 | COPY requirements.txt . 32 | RUN python3 -m pip install --upgrade pip wheel 33 | RUN pip install --no-cache -r requirements.txt albumentations comet gsutil notebook \ 34 | coremltools onnx onnx-simplifier onnxruntime 'openvino-dev>=2023.0' 35 | # tensorflow tensorflowjs \ 36 | 37 | # Set environment variables 38 | ENV OMP_NUM_THREADS=1 39 | 40 | # Cleanup 41 | ENV DEBIAN_FRONTEND teletype 42 | 43 | 44 | # Usage Examples ------------------------------------------------------------------------------------------------------- 45 | 46 | # Build and Push 47 | # t=ultralytics/yolov5:latest && sudo docker build -f utils/docker/Dockerfile -t $t . && sudo docker push $t 48 | 49 | # Pull and Run 50 | # t=ultralytics/yolov5:latest && sudo docker pull $t && sudo docker run -it --ipc=host --gpus all $t 51 | 52 | # Pull and Run with local directory access 53 | # t=ultralytics/yolov5:latest && sudo docker pull $t && sudo docker run -it --ipc=host --gpus all -v "$(pwd)"/datasets:/usr/src/datasets $t 54 | 55 | # Kill all 56 | # sudo docker kill $(sudo docker ps -q) 57 | 58 | # Kill all image-based 59 | # sudo docker kill $(sudo docker ps -qa --filter ancestor=ultralytics/yolov5:latest) 60 | 61 | # DockerHub tag update 62 | # t=ultralytics/yolov5:latest tnew=ultralytics/yolov5:v6.2 && sudo docker pull $t && sudo docker tag $t $tnew && sudo docker push $tnew 63 | 64 | # Clean up 65 | # sudo docker system prune -a --volumes 66 | 67 | # Update Ubuntu drivers 68 | # https://www.maketecheasier.com/install-nvidia-drivers-ubuntu/ 69 | 70 | # DDP test 71 | # python -m torch.distributed.run --nproc_per_node 2 --master_port 1 train.py --epochs 3 72 | 73 | # GCP VM from Image 74 | # docker.io/ultralytics/yolov5:latest 75 | -------------------------------------------------------------------------------- /utils/docker/Dockerfile-arm64: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Builds ultralytics/yolov5:latest-arm64 image on DockerHub https://hub.docker.com/r/ultralytics/yolov5 4 | # Image is aarch64-compatible for Apple M1 and other ARM architectures i.e. Jetson Nano and Raspberry Pi 5 | 6 | # Start FROM Ubuntu image https://hub.docker.com/_/ubuntu 7 | FROM arm64v8/ubuntu:22.10 8 | 9 | # Downloads to user config dir 10 | ADD https://ultralytics.com/assets/Arial.ttf https://ultralytics.com/assets/Arial.Unicode.ttf /root/.config/Ultralytics/ 11 | 12 | # Install linux packages 13 | ENV DEBIAN_FRONTEND noninteractive 14 | RUN apt update 15 | RUN TZ=Etc/UTC apt install -y tzdata 16 | RUN apt install --no-install-recommends -y python3-pip git zip curl htop gcc libgl1 libglib2.0-0 libpython3-dev 17 | # RUN alias python=python3 18 | 19 | # Install pip packages 20 | COPY requirements.txt . 21 | RUN python3 -m pip install --upgrade pip wheel 22 | RUN pip install --no-cache -r requirements.txt albumentations gsutil notebook \ 23 | coremltools onnx onnxruntime 24 | # tensorflow-aarch64 tensorflowjs \ 25 | 26 | # Create working directory 27 | RUN mkdir -p /usr/src/app 28 | WORKDIR /usr/src/app 29 | 30 | # Copy contents 31 | COPY . /usr/src/app 32 | ENV DEBIAN_FRONTEND teletype 33 | 34 | 35 | # Usage Examples ------------------------------------------------------------------------------------------------------- 36 | 37 | # Build and Push 38 | # t=ultralytics/yolov5:latest-arm64 && sudo docker build --platform linux/arm64 -f utils/docker/Dockerfile-arm64 -t $t . && sudo docker push $t 39 | 40 | # Pull and Run 41 | # t=ultralytics/yolov5:latest-arm64 && sudo docker pull $t && sudo docker run -it --ipc=host -v "$(pwd)"/datasets:/usr/src/datasets $t 42 | -------------------------------------------------------------------------------- /utils/docker/Dockerfile-cpu: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | # Builds ultralytics/yolov5:latest-cpu image on DockerHub https://hub.docker.com/r/ultralytics/yolov5 4 | # Image is CPU-optimized for ONNX, OpenVINO and PyTorch YOLOv5 deployments 5 | 6 | # Start FROM Ubuntu image https://hub.docker.com/_/ubuntu 7 | FROM ubuntu:23.10 8 | 9 | # Downloads to user config dir 10 | ADD https://ultralytics.com/assets/Arial.ttf https://ultralytics.com/assets/Arial.Unicode.ttf /root/.config/Ultralytics/ 11 | 12 | # Install linux packages 13 | # g++ required to build 'tflite_support' and 'lap' packages, libusb-1.0-0 required for 'tflite_support' package 14 | RUN apt update \ 15 | && apt install --no-install-recommends -y python3-pip git zip curl htop libgl1 libglib2.0-0 libpython3-dev gnupg g++ libusb-1.0-0 16 | # RUN alias python=python3 17 | 18 | # Remove python3.11/EXTERNALLY-MANAGED or use 'pip install --break-system-packages' avoid 'externally-managed-environment' Ubuntu nightly error 19 | RUN rm -rf /usr/lib/python3.11/EXTERNALLY-MANAGED 20 | 21 | # Install pip packages 22 | COPY requirements.txt . 23 | RUN python3 -m pip install --upgrade pip wheel 24 | RUN pip install --no-cache -r requirements.txt albumentations gsutil notebook \ 25 | coremltools onnx onnx-simplifier onnxruntime 'openvino-dev>=2023.0' \ 26 | # tensorflow tensorflowjs \ 27 | --extra-index-url https://download.pytorch.org/whl/cpu 28 | 29 | # Create working directory 30 | RUN mkdir -p /usr/src/app 31 | WORKDIR /usr/src/app 32 | 33 | # Copy contents 34 | COPY . /usr/src/app 35 | 36 | 37 | # Usage Examples ------------------------------------------------------------------------------------------------------- 38 | 39 | # Build and Push 40 | # t=ultralytics/yolov5:latest-cpu && sudo docker build -f utils/docker/Dockerfile-cpu -t $t . && sudo docker push $t 41 | 42 | # Pull and Run 43 | # t=ultralytics/yolov5:latest-cpu && sudo docker pull $t && sudo docker run -it --ipc=host -v "$(pwd)"/datasets:/usr/src/datasets $t 44 | -------------------------------------------------------------------------------- /utils/downloads.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | """Download utils.""" 3 | 4 | import logging 5 | import subprocess 6 | import urllib 7 | from pathlib import Path 8 | 9 | import requests 10 | import torch 11 | 12 | 13 | def is_url(url, check=True): 14 | """Determines if a string is a URL and optionally checks its existence online, returning a boolean.""" 15 | try: 16 | url = str(url) 17 | result = urllib.parse.urlparse(url) 18 | assert all([result.scheme, result.netloc]) # check if is url 19 | return (urllib.request.urlopen(url).getcode() == 200) if check else True # check if exists online 20 | except (AssertionError, urllib.request.HTTPError): 21 | return False 22 | 23 | 24 | def gsutil_getsize(url=""): 25 | """ 26 | Returns the size in bytes of a file at a Google Cloud Storage URL using `gsutil du`. 27 | 28 | Returns 0 if the command fails or output is empty. 29 | """ 30 | output = subprocess.check_output(["gsutil", "du", url], shell=True, encoding="utf-8") 31 | return int(output.split()[0]) if output else 0 32 | 33 | 34 | def url_getsize(url="https://ultralytics.com/images/bus.jpg"): 35 | """Returns the size in bytes of a downloadable file at a given URL; defaults to -1 if not found.""" 36 | response = requests.head(url, allow_redirects=True) 37 | return int(response.headers.get("content-length", -1)) 38 | 39 | 40 | def curl_download(url, filename, *, silent: bool = False) -> bool: 41 | """Download a file from a url to a filename using curl.""" 42 | silent_option = "sS" if silent else "" # silent 43 | proc = subprocess.run( 44 | [ 45 | "curl", 46 | "-#", 47 | f"-{silent_option}L", 48 | url, 49 | "--output", 50 | filename, 51 | "--retry", 52 | "9", 53 | "-C", 54 | "-", 55 | ] 56 | ) 57 | return proc.returncode == 0 58 | 59 | 60 | def safe_download(file, url, url2=None, min_bytes=1e0, error_msg=""): 61 | """ 62 | Downloads a file from a URL (or alternate URL) to a specified path if file is above a minimum size. 63 | 64 | Removes incomplete downloads. 65 | """ 66 | from utils.general import LOGGER 67 | 68 | file = Path(file) 69 | assert_msg = f"Downloaded file '{file}' does not exist or size is < min_bytes={min_bytes}" 70 | try: # url1 71 | LOGGER.info(f"Downloading {url} to {file}...") 72 | torch.hub.download_url_to_file(url, str(file), progress=LOGGER.level <= logging.INFO) 73 | assert file.exists() and file.stat().st_size > min_bytes, assert_msg # check 74 | except Exception as e: # url2 75 | if file.exists(): 76 | file.unlink() # remove partial downloads 77 | LOGGER.info(f"ERROR: {e}\nRe-attempting {url2 or url} to {file}...") 78 | # curl download, retry and resume on fail 79 | curl_download(url2 or url, file) 80 | finally: 81 | if not file.exists() or file.stat().st_size < min_bytes: # check 82 | if file.exists(): 83 | file.unlink() # remove partial downloads 84 | LOGGER.info(f"ERROR: {assert_msg}\n{error_msg}") 85 | LOGGER.info("") 86 | 87 | 88 | def attempt_download(file, repo="ultralytics/yolov5", release="v7.0"): 89 | """Downloads a file from GitHub release assets or via direct URL if not found locally, supporting backup 90 | versions. 91 | """ 92 | from utils.general import LOGGER 93 | 94 | def github_assets(repository, version="latest"): 95 | """Fetches GitHub repository release tag and asset names using the GitHub API.""" 96 | if version != "latest": 97 | version = f"tags/{version}" # i.e. tags/v7.0 98 | response = requests.get(f"https://api.github.com/repos/{repository}/releases/{version}").json() # github api 99 | return response["tag_name"], [x["name"] for x in response["assets"]] # tag, assets 100 | 101 | file = Path(str(file).strip().replace("'", "")) 102 | if not file.exists(): 103 | # URL specified 104 | name = Path(urllib.parse.unquote(str(file))).name # decode '%2F' to '/' etc. 105 | if str(file).startswith(("http:/", "https:/")): # download 106 | url = str(file).replace(":/", "://") # Pathlib turns :// -> :/ 107 | file = name.split("?")[0] # parse authentication https://url.com/file.txt?auth... 108 | if Path(file).is_file(): 109 | LOGGER.info(f"Found {url} locally at {file}") # file already exists 110 | else: 111 | safe_download(file=file, url=url, min_bytes=1e5) 112 | return file 113 | 114 | # GitHub assets 115 | assets = [f"yolov5{size}{suffix}.pt" for size in "nsmlx" for suffix in ("", "6", "-cls", "-seg")] # default 116 | try: 117 | tag, assets = github_assets(repo, release) 118 | except Exception: 119 | try: 120 | tag, assets = github_assets(repo) # latest release 121 | except Exception: 122 | try: 123 | tag = subprocess.check_output("git tag", shell=True, stderr=subprocess.STDOUT).decode().split()[-1] 124 | except Exception: 125 | tag = release 126 | 127 | if name in assets: 128 | file.parent.mkdir(parents=True, exist_ok=True) # make parent dir (if required) 129 | safe_download( 130 | file, 131 | url=f"https://github.com/{repo}/releases/download/{tag}/{name}", 132 | min_bytes=1e5, 133 | error_msg=f"{file} missing, try downloading from https://github.com/{repo}/releases/{tag}", 134 | ) 135 | 136 | return str(file) 137 | -------------------------------------------------------------------------------- /utils/flask_rest_api/README.md: -------------------------------------------------------------------------------- 1 | Ultralytics logo 2 | 3 | # Flask REST API for YOLOv5 4 | 5 | [Representational State Transfer (REST)](https://en.wikipedia.org/wiki/Representational_state_transfer) [Application Programming Interfaces (APIs)](https://en.wikipedia.org/wiki/API) provide a standardized way to expose [Machine Learning (ML)](https://www.ultralytics.com/glossary/machine-learning-ml) models for use by other services or applications. This directory contains an example REST API built with the [Flask](https://flask.palletsprojects.com/en/stable/) web framework to serve the [Ultralytics YOLOv5s](https://docs.ultralytics.com/models/yolov5/) model, loaded directly from [PyTorch Hub](https://pytorch.org/hub/ultralytics_yolov5/). This setup allows you to easily integrate YOLOv5 [object detection](https://docs.ultralytics.com/tasks/detect/) capabilities into your web applications or microservices, aligning with common [model deployment options](https://docs.ultralytics.com/guides/model-deployment-options/). 6 | 7 | ## 💻 Requirements 8 | 9 | The primary requirement is the [Flask](https://flask.palletsprojects.com/en/stable/) web framework. You can install it using pip: 10 | 11 | ```shell 12 | pip install Flask 13 | ``` 14 | 15 | You will also need `torch` and `yolov5`. These are implicitly handled by the script when it loads the model from PyTorch Hub. Ensure you have a functioning Python environment set up. 16 | 17 | ## ▶️ Run the API 18 | 19 | Once Flask is installed, you can start the API server using the following command: 20 | 21 | ```shell 22 | python restapi.py --port 5000 23 | ``` 24 | 25 | The server will begin listening on the specified port (defaulting to 5000). You can then send inference requests to the API endpoint using tools like [curl](https://curl.se/) or any other HTTP client. 26 | 27 | To test the API with a local image file (e.g., `zidane.jpg` located in the `yolov5/data/images` directory relative to the script): 28 | 29 | ```shell 30 | curl -X POST -F image=@../data/images/zidane.jpg 'http://localhost:5000/v1/object-detection/yolov5s' 31 | ``` 32 | 33 | The API processes the submitted image using the YOLOv5s model and returns the detection results in [JSON](https://www.json.org/json-en.html) format. Each object within the JSON array represents a detected item, including its class ID, confidence score, normalized [bounding box](https://www.ultralytics.com/glossary/bounding-box) coordinates (`xcenter`, `ycenter`, `width`, `height`), and class name. 34 | 35 | ```json 36 | [ 37 | { 38 | "class": 0, 39 | "confidence": 0.8900438547, 40 | "height": 0.9318675399, 41 | "name": "person", 42 | "width": 0.3264600933, 43 | "xcenter": 0.7438579798, 44 | "ycenter": 0.5207948685 45 | }, 46 | { 47 | "class": 0, 48 | "confidence": 0.8440024257, 49 | "height": 0.7155083418, 50 | "name": "person", 51 | "width": 0.6546785235, 52 | "xcenter": 0.427829951, 53 | "ycenter": 0.6334488392 54 | }, 55 | { 56 | "class": 27, 57 | "confidence": 0.3771208823, 58 | "height": 0.3902671337, 59 | "name": "tie", 60 | "width": 0.0696444362, 61 | "xcenter": 0.3675483763, 62 | "ycenter": 0.7991207838 63 | }, 64 | { 65 | "class": 27, 66 | "confidence": 0.3527112305, 67 | "height": 0.1540903747, 68 | "name": "tie", 69 | "width": 0.0336618312, 70 | "xcenter": 0.7814827561, 71 | "ycenter": 0.5065554976 72 | } 73 | ] 74 | ``` 75 | 76 | An example Python script, `example_request.py`, is included to demonstrate how to perform inference using the popular [requests](https://requests.readthedocs.io/en/latest/) library. This script offers a straightforward method for interacting with the running API programmatically. 77 | 78 | ## 🤝 Contribute 79 | 80 | Contributions to enhance this Flask API example are highly encouraged! Whether you're interested in adding support for different YOLO models, improving error handling, or implementing new features, please feel free to fork the repository, apply your changes, and submit a pull request. For more comprehensive contribution guidelines, please refer to the main [Ultralytics YOLOv5 repository](https://github.com/ultralytics/yolov5) and the general [Ultralytics documentation](https://docs.ultralytics.com/). 81 | -------------------------------------------------------------------------------- /utils/flask_rest_api/example_request.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | """Perform test request.""" 3 | 4 | import pprint 5 | 6 | import requests 7 | 8 | DETECTION_URL = "http://localhost:5000/v1/object-detection/yolov5s" 9 | IMAGE = "zidane.jpg" 10 | 11 | # Read image 12 | with open(IMAGE, "rb") as f: 13 | image_data = f.read() 14 | 15 | response = requests.post(DETECTION_URL, files={"image": image_data}).json() 16 | 17 | pprint.pprint(response) 18 | -------------------------------------------------------------------------------- /utils/flask_rest_api/restapi.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | """Run a Flask REST API exposing one or more YOLOv5s models.""" 3 | 4 | import argparse 5 | import io 6 | 7 | import torch 8 | from flask import Flask, request 9 | from PIL import Image 10 | 11 | app = Flask(__name__) 12 | models = {} 13 | 14 | DETECTION_URL = "/v1/object-detection/" 15 | 16 | 17 | @app.route(DETECTION_URL, methods=["POST"]) 18 | def predict(model): 19 | """Predict and return object detections in JSON format given an image and model name via a Flask REST API POST 20 | request. 21 | """ 22 | if request.method != "POST": 23 | return 24 | 25 | if request.files.get("image"): 26 | # Method 1 27 | # with request.files["image"] as f: 28 | # im = Image.open(io.BytesIO(f.read())) 29 | 30 | # Method 2 31 | im_file = request.files["image"] 32 | im_bytes = im_file.read() 33 | im = Image.open(io.BytesIO(im_bytes)) 34 | 35 | if model in models: 36 | results = models[model](im, size=640) # reduce size=320 for faster inference 37 | return results.pandas().xyxy[0].to_json(orient="records") 38 | 39 | 40 | if __name__ == "__main__": 41 | parser = argparse.ArgumentParser(description="Flask API exposing YOLOv5 model") 42 | parser.add_argument("--port", default=5000, type=int, help="port number") 43 | parser.add_argument("--model", nargs="+", default=["yolov5s"], help="model(s) to run, i.e. --model yolov5n yolov5s") 44 | opt = parser.parse_args() 45 | 46 | for m in opt.model: 47 | models[m] = torch.hub.load("ultralytics/yolov5", m, force_reload=True, skip_validation=True) 48 | 49 | app.run(host="0.0.0.0", port=opt.port) # debug=True causes Restarting with stat 50 | -------------------------------------------------------------------------------- /utils/google_app_engine/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM gcr.io/google-appengine/python 2 | 3 | # Create a virtualenv for dependencies. This isolates these packages from 4 | # system-level packages. 5 | # Use -p python3 or -p python3.7 to select python version. Default is version 2. 6 | RUN virtualenv /env -p python3 7 | 8 | # Setting these environment variables are the same as running 9 | # source /env/bin/activate. 10 | ENV VIRTUAL_ENV /env 11 | ENV PATH /env/bin:$PATH 12 | 13 | RUN apt-get update && apt-get install -y python-opencv 14 | 15 | # Copy the application's requirements.txt and run pip to install all 16 | # dependencies into the virtualenv. 17 | ADD requirements.txt /app/requirements.txt 18 | RUN pip install -r /app/requirements.txt 19 | 20 | # Add the application source code. 21 | ADD . /app 22 | 23 | # Run a WSGI server to serve the application. gunicorn must be declared as 24 | # a dependency in requirements.txt. 25 | CMD gunicorn -b :$PORT main:app 26 | -------------------------------------------------------------------------------- /utils/google_app_engine/additional_requirements.txt: -------------------------------------------------------------------------------- 1 | # add these requirements in your app on top of the existing ones 2 | pip==23.3 3 | Flask==2.3.2 4 | gunicorn==23.0.0 5 | werkzeug>=3.0.1 # not directly required, pinned by Snyk to avoid a vulnerability 6 | zipp>=3.19.1 # not directly required, pinned by Snyk to avoid a vulnerability 7 | -------------------------------------------------------------------------------- /utils/google_app_engine/app.yaml: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | runtime: custom 4 | env: flex 5 | 6 | service: yolov5app 7 | 8 | liveness_check: 9 | initial_delay_sec: 600 10 | 11 | manual_scaling: 12 | instances: 1 13 | resources: 14 | cpu: 1 15 | memory_gb: 4 16 | disk_size_gb: 20 17 | -------------------------------------------------------------------------------- /utils/loggers/clearml/__init__.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | -------------------------------------------------------------------------------- /utils/loggers/clearml/hpo.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | from clearml import Task 4 | 5 | # Connecting ClearML with the current process, 6 | # from here on everything is logged automatically 7 | from clearml.automation import HyperParameterOptimizer, UniformParameterRange 8 | from clearml.automation.optuna import OptimizerOptuna 9 | 10 | task = Task.init( 11 | project_name="Hyper-Parameter Optimization", 12 | task_name="YOLOv5", 13 | task_type=Task.TaskTypes.optimizer, 14 | reuse_last_task_id=False, 15 | ) 16 | 17 | # Example use case: 18 | optimizer = HyperParameterOptimizer( 19 | # This is the experiment we want to optimize 20 | base_task_id="", 21 | # here we define the hyper-parameters to optimize 22 | # Notice: The parameter name should exactly match what you see in the UI: / 23 | # For Example, here we see in the base experiment a section Named: "General" 24 | # under it a parameter named "batch_size", this becomes "General/batch_size" 25 | # If you have `argparse` for example, then arguments will appear under the "Args" section, 26 | # and you should instead pass "Args/batch_size" 27 | hyper_parameters=[ 28 | UniformParameterRange("Hyperparameters/lr0", min_value=1e-5, max_value=1e-1), 29 | UniformParameterRange("Hyperparameters/lrf", min_value=0.01, max_value=1.0), 30 | UniformParameterRange("Hyperparameters/momentum", min_value=0.6, max_value=0.98), 31 | UniformParameterRange("Hyperparameters/weight_decay", min_value=0.0, max_value=0.001), 32 | UniformParameterRange("Hyperparameters/warmup_epochs", min_value=0.0, max_value=5.0), 33 | UniformParameterRange("Hyperparameters/warmup_momentum", min_value=0.0, max_value=0.95), 34 | UniformParameterRange("Hyperparameters/warmup_bias_lr", min_value=0.0, max_value=0.2), 35 | UniformParameterRange("Hyperparameters/box", min_value=0.02, max_value=0.2), 36 | UniformParameterRange("Hyperparameters/cls", min_value=0.2, max_value=4.0), 37 | UniformParameterRange("Hyperparameters/cls_pw", min_value=0.5, max_value=2.0), 38 | UniformParameterRange("Hyperparameters/obj", min_value=0.2, max_value=4.0), 39 | UniformParameterRange("Hyperparameters/obj_pw", min_value=0.5, max_value=2.0), 40 | UniformParameterRange("Hyperparameters/iou_t", min_value=0.1, max_value=0.7), 41 | UniformParameterRange("Hyperparameters/anchor_t", min_value=2.0, max_value=8.0), 42 | UniformParameterRange("Hyperparameters/fl_gamma", min_value=0.0, max_value=4.0), 43 | UniformParameterRange("Hyperparameters/hsv_h", min_value=0.0, max_value=0.1), 44 | UniformParameterRange("Hyperparameters/hsv_s", min_value=0.0, max_value=0.9), 45 | UniformParameterRange("Hyperparameters/hsv_v", min_value=0.0, max_value=0.9), 46 | UniformParameterRange("Hyperparameters/degrees", min_value=0.0, max_value=45.0), 47 | UniformParameterRange("Hyperparameters/translate", min_value=0.0, max_value=0.9), 48 | UniformParameterRange("Hyperparameters/scale", min_value=0.0, max_value=0.9), 49 | UniformParameterRange("Hyperparameters/shear", min_value=0.0, max_value=10.0), 50 | UniformParameterRange("Hyperparameters/perspective", min_value=0.0, max_value=0.001), 51 | UniformParameterRange("Hyperparameters/flipud", min_value=0.0, max_value=1.0), 52 | UniformParameterRange("Hyperparameters/fliplr", min_value=0.0, max_value=1.0), 53 | UniformParameterRange("Hyperparameters/mosaic", min_value=0.0, max_value=1.0), 54 | UniformParameterRange("Hyperparameters/mixup", min_value=0.0, max_value=1.0), 55 | UniformParameterRange("Hyperparameters/copy_paste", min_value=0.0, max_value=1.0), 56 | ], 57 | # this is the objective metric we want to maximize/minimize 58 | objective_metric_title="metrics", 59 | objective_metric_series="mAP_0.5", 60 | # now we decide if we want to maximize it or minimize it (accuracy we maximize) 61 | objective_metric_sign="max", 62 | # let us limit the number of concurrent experiments, 63 | # this in turn will make sure we don't bombard the scheduler with experiments. 64 | # if we have an auto-scaler connected, this, by proxy, will limit the number of machine 65 | max_number_of_concurrent_tasks=1, 66 | # this is the optimizer class (actually doing the optimization) 67 | # Currently, we can choose from GridSearch, RandomSearch or OptimizerBOHB (Bayesian optimization Hyper-Band) 68 | optimizer_class=OptimizerOptuna, 69 | # If specified only the top K performing Tasks will be kept, the others will be automatically archived 70 | save_top_k_tasks_only=5, # 5, 71 | compute_time_limit=None, 72 | total_max_jobs=20, 73 | min_iteration_per_job=None, 74 | max_iteration_per_job=None, 75 | ) 76 | 77 | # report every 10 seconds, this is way too often, but we are testing here 78 | optimizer.set_report_period(10 / 60) 79 | # You can also use the line below instead to run all the optimizer tasks locally, without using queues or agent 80 | # an_optimizer.start_locally(job_complete_callback=job_complete_callback) 81 | # set the time limit for the optimization process (2 hours) 82 | optimizer.set_time_limit(in_minutes=120.0) 83 | # Start the optimization process in the local environment 84 | optimizer.start_locally() 85 | # wait until process is done (notice we are controlling the optimization process in the background) 86 | optimizer.wait() 87 | # make sure background optimization stopped 88 | optimizer.stop() 89 | 90 | print("We are done, good bye") 91 | -------------------------------------------------------------------------------- /utils/loggers/comet/comet_utils.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | 3 | import logging 4 | import os 5 | from urllib.parse import urlparse 6 | 7 | try: 8 | import comet_ml 9 | except ImportError: 10 | comet_ml = None 11 | 12 | import yaml 13 | 14 | logger = logging.getLogger(__name__) 15 | 16 | COMET_PREFIX = "comet://" 17 | COMET_MODEL_NAME = os.getenv("COMET_MODEL_NAME", "yolov5") 18 | COMET_DEFAULT_CHECKPOINT_FILENAME = os.getenv("COMET_DEFAULT_CHECKPOINT_FILENAME", "last.pt") 19 | 20 | 21 | def download_model_checkpoint(opt, experiment): 22 | """Downloads YOLOv5 model checkpoint from Comet ML experiment, updating `opt.weights` with download path.""" 23 | model_dir = f"{opt.project}/{experiment.name}" 24 | os.makedirs(model_dir, exist_ok=True) 25 | 26 | model_name = COMET_MODEL_NAME 27 | model_asset_list = experiment.get_model_asset_list(model_name) 28 | 29 | if len(model_asset_list) == 0: 30 | logger.error(f"COMET ERROR: No checkpoints found for model name : {model_name}") 31 | return 32 | 33 | model_asset_list = sorted( 34 | model_asset_list, 35 | key=lambda x: x["step"], 36 | reverse=True, 37 | ) 38 | logged_checkpoint_map = {asset["fileName"]: asset["assetId"] for asset in model_asset_list} 39 | 40 | resource_url = urlparse(opt.weights) 41 | checkpoint_filename = resource_url.query 42 | 43 | if checkpoint_filename: 44 | asset_id = logged_checkpoint_map.get(checkpoint_filename) 45 | else: 46 | asset_id = logged_checkpoint_map.get(COMET_DEFAULT_CHECKPOINT_FILENAME) 47 | checkpoint_filename = COMET_DEFAULT_CHECKPOINT_FILENAME 48 | 49 | if asset_id is None: 50 | logger.error(f"COMET ERROR: Checkpoint {checkpoint_filename} not found in the given Experiment") 51 | return 52 | 53 | try: 54 | logger.info(f"COMET INFO: Downloading checkpoint {checkpoint_filename}") 55 | asset_filename = checkpoint_filename 56 | 57 | model_binary = experiment.get_asset(asset_id, return_type="binary", stream=False) 58 | model_download_path = f"{model_dir}/{asset_filename}" 59 | with open(model_download_path, "wb") as f: 60 | f.write(model_binary) 61 | 62 | opt.weights = model_download_path 63 | 64 | except Exception as e: 65 | logger.warning("COMET WARNING: Unable to download checkpoint from Comet") 66 | logger.exception(e) 67 | 68 | 69 | def set_opt_parameters(opt, experiment): 70 | """ 71 | Update the opts Namespace with parameters from Comet's ExistingExperiment when resuming a run. 72 | 73 | Args: 74 | opt (argparse.Namespace): Namespace of command line options 75 | experiment (comet_ml.APIExperiment): Comet API Experiment object 76 | """ 77 | asset_list = experiment.get_asset_list() 78 | resume_string = opt.resume 79 | 80 | for asset in asset_list: 81 | if asset["fileName"] == "opt.yaml": 82 | asset_id = asset["assetId"] 83 | asset_binary = experiment.get_asset(asset_id, return_type="binary", stream=False) 84 | opt_dict = yaml.safe_load(asset_binary) 85 | for key, value in opt_dict.items(): 86 | setattr(opt, key, value) 87 | opt.resume = resume_string 88 | 89 | # Save hyperparameters to YAML file 90 | # Necessary to pass checks in training script 91 | save_dir = f"{opt.project}/{experiment.name}" 92 | os.makedirs(save_dir, exist_ok=True) 93 | 94 | hyp_yaml_path = f"{save_dir}/hyp.yaml" 95 | with open(hyp_yaml_path, "w") as f: 96 | yaml.dump(opt.hyp, f) 97 | opt.hyp = hyp_yaml_path 98 | 99 | 100 | def check_comet_weights(opt): 101 | """ 102 | Downloads model weights from Comet and updates the weights path to point to saved weights location. 103 | 104 | Args: 105 | opt (argparse.Namespace): Command Line arguments passed 106 | to YOLOv5 training script 107 | 108 | Returns: 109 | None/bool: Return True if weights are successfully downloaded 110 | else return None 111 | """ 112 | if comet_ml is None: 113 | return 114 | 115 | if isinstance(opt.weights, str) and opt.weights.startswith(COMET_PREFIX): 116 | api = comet_ml.API() 117 | resource = urlparse(opt.weights) 118 | experiment_path = f"{resource.netloc}{resource.path}" 119 | experiment = api.get(experiment_path) 120 | download_model_checkpoint(opt, experiment) 121 | return True 122 | 123 | return None 124 | 125 | 126 | def check_comet_resume(opt): 127 | """ 128 | Restores run parameters to its original state based on the model checkpoint and logged Experiment parameters. 129 | 130 | Args: 131 | opt (argparse.Namespace): Command Line arguments passed 132 | to YOLOv5 training script 133 | 134 | Returns: 135 | None/bool: Return True if the run is restored successfully 136 | else return None 137 | """ 138 | if comet_ml is None: 139 | return 140 | 141 | if isinstance(opt.resume, str) and opt.resume.startswith(COMET_PREFIX): 142 | api = comet_ml.API() 143 | resource = urlparse(opt.resume) 144 | experiment_path = f"{resource.netloc}{resource.path}" 145 | experiment = api.get(experiment_path) 146 | set_opt_parameters(opt, experiment) 147 | download_model_checkpoint(opt, experiment) 148 | 149 | return True 150 | 151 | return None 152 | -------------------------------------------------------------------------------- /utils/loggers/comet/optimizer_config.json: -------------------------------------------------------------------------------- 1 | { 2 | "algorithm": "random", 3 | "parameters": { 4 | "anchor_t": { 5 | "type": "discrete", 6 | "values": [2, 8] 7 | }, 8 | "batch_size": { 9 | "type": "discrete", 10 | "values": [16, 32, 64] 11 | }, 12 | "box": { 13 | "type": "discrete", 14 | "values": [0.02, 0.2] 15 | }, 16 | "cls": { 17 | "type": "discrete", 18 | "values": [0.2] 19 | }, 20 | "cls_pw": { 21 | "type": "discrete", 22 | "values": [0.5] 23 | }, 24 | "copy_paste": { 25 | "type": "discrete", 26 | "values": [1] 27 | }, 28 | "degrees": { 29 | "type": "discrete", 30 | "values": [0, 45] 31 | }, 32 | "epochs": { 33 | "type": "discrete", 34 | "values": [5] 35 | }, 36 | "fl_gamma": { 37 | "type": "discrete", 38 | "values": [0] 39 | }, 40 | "fliplr": { 41 | "type": "discrete", 42 | "values": [0] 43 | }, 44 | "flipud": { 45 | "type": "discrete", 46 | "values": [0] 47 | }, 48 | "hsv_h": { 49 | "type": "discrete", 50 | "values": [0] 51 | }, 52 | "hsv_s": { 53 | "type": "discrete", 54 | "values": [0] 55 | }, 56 | "hsv_v": { 57 | "type": "discrete", 58 | "values": [0] 59 | }, 60 | "iou_t": { 61 | "type": "discrete", 62 | "values": [0.7] 63 | }, 64 | "lr0": { 65 | "type": "discrete", 66 | "values": [1e-5, 0.1] 67 | }, 68 | "lrf": { 69 | "type": "discrete", 70 | "values": [0.01, 1] 71 | }, 72 | "mixup": { 73 | "type": "discrete", 74 | "values": [1] 75 | }, 76 | "momentum": { 77 | "type": "discrete", 78 | "values": [0.6] 79 | }, 80 | "mosaic": { 81 | "type": "discrete", 82 | "values": [0] 83 | }, 84 | "obj": { 85 | "type": "discrete", 86 | "values": [0.2] 87 | }, 88 | "obj_pw": { 89 | "type": "discrete", 90 | "values": [0.5] 91 | }, 92 | "optimizer": { 93 | "type": "categorical", 94 | "values": ["SGD", "Adam", "AdamW"] 95 | }, 96 | "perspective": { 97 | "type": "discrete", 98 | "values": [0] 99 | }, 100 | "scale": { 101 | "type": "discrete", 102 | "values": [0] 103 | }, 104 | "shear": { 105 | "type": "discrete", 106 | "values": [0] 107 | }, 108 | "translate": { 109 | "type": "discrete", 110 | "values": [0] 111 | }, 112 | "warmup_bias_lr": { 113 | "type": "discrete", 114 | "values": [0, 0.2] 115 | }, 116 | "warmup_epochs": { 117 | "type": "discrete", 118 | "values": [5] 119 | }, 120 | "warmup_momentum": { 121 | "type": "discrete", 122 | "values": [0, 0.95] 123 | }, 124 | "weight_decay": { 125 | "type": "discrete", 126 | "values": [0, 0.001] 127 | } 128 | }, 129 | "spec": { 130 | "maxCombo": 0, 131 | "metric": "metrics/mAP_0.5", 132 | "objective": "maximize" 133 | }, 134 | "trials": 1 135 | } 136 | -------------------------------------------------------------------------------- /utils/loggers/wandb/__init__.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | -------------------------------------------------------------------------------- /utils/segment/__init__.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | -------------------------------------------------------------------------------- /utils/segment/augmentations.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | """Image augmentation functions.""" 3 | 4 | import math 5 | import random 6 | 7 | import cv2 8 | import numpy as np 9 | 10 | from ..augmentations import box_candidates 11 | from ..general import resample_segments, segment2box 12 | 13 | 14 | def mixup(im, labels, segments, im2, labels2, segments2): 15 | """ 16 | Applies MixUp augmentation blending two images, labels, and segments with a random ratio. 17 | 18 | See https://arxiv.org/pdf/1710.09412.pdf 19 | """ 20 | r = np.random.beta(32.0, 32.0) # mixup ratio, alpha=beta=32.0 21 | im = (im * r + im2 * (1 - r)).astype(np.uint8) 22 | labels = np.concatenate((labels, labels2), 0) 23 | segments = np.concatenate((segments, segments2), 0) 24 | return im, labels, segments 25 | 26 | 27 | def random_perspective( 28 | im, targets=(), segments=(), degrees=10, translate=0.1, scale=0.1, shear=10, perspective=0.0, border=(0, 0) 29 | ): 30 | # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10)) 31 | # targets = [cls, xyxy] 32 | """Applies random perspective, rotation, scale, shear, and translation augmentations to an image and targets.""" 33 | height = im.shape[0] + border[0] * 2 # shape(h,w,c) 34 | width = im.shape[1] + border[1] * 2 35 | 36 | # Center 37 | C = np.eye(3) 38 | C[0, 2] = -im.shape[1] / 2 # x translation (pixels) 39 | C[1, 2] = -im.shape[0] / 2 # y translation (pixels) 40 | 41 | # Perspective 42 | P = np.eye(3) 43 | P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y) 44 | P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x) 45 | 46 | # Rotation and Scale 47 | R = np.eye(3) 48 | a = random.uniform(-degrees, degrees) 49 | # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations 50 | s = random.uniform(1 - scale, 1 + scale) 51 | # s = 2 ** random.uniform(-scale, scale) 52 | R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s) 53 | 54 | # Shear 55 | S = np.eye(3) 56 | S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg) 57 | S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg) 58 | 59 | # Translation 60 | T = np.eye(3) 61 | T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels) 62 | T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels) 63 | 64 | # Combined rotation matrix 65 | M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT 66 | if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed 67 | if perspective: 68 | im = cv2.warpPerspective(im, M, dsize=(width, height), borderValue=(114, 114, 114)) 69 | else: # affine 70 | im = cv2.warpAffine(im, M[:2], dsize=(width, height), borderValue=(114, 114, 114)) 71 | 72 | new_segments = [] 73 | if n := len(targets): 74 | new = np.zeros((n, 4)) 75 | segments = resample_segments(segments) # upsample 76 | for i, segment in enumerate(segments): 77 | xy = np.ones((len(segment), 3)) 78 | xy[:, :2] = segment 79 | xy = xy @ M.T # transform 80 | xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine 81 | 82 | # clip 83 | new[i] = segment2box(xy, width, height) 84 | new_segments.append(xy) 85 | 86 | # filter candidates 87 | i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01) 88 | targets = targets[i] 89 | targets[:, 1:5] = new[i] 90 | new_segments = np.array(new_segments)[i] 91 | 92 | return im, targets, new_segments 93 | -------------------------------------------------------------------------------- /utils/triton.py: -------------------------------------------------------------------------------- 1 | # Ultralytics 🚀 AGPL-3.0 License - https://ultralytics.com/license 2 | """Utils to interact with the Triton Inference Server.""" 3 | 4 | import typing 5 | from urllib.parse import urlparse 6 | 7 | import torch 8 | 9 | 10 | class TritonRemoteModel: 11 | """ 12 | A wrapper over a model served by the Triton Inference Server. 13 | 14 | It can be configured to communicate over GRPC or HTTP. It accepts Torch Tensors as input and returns them as 15 | outputs. 16 | """ 17 | 18 | def __init__(self, url: str): 19 | """ 20 | Keyword Arguments: 21 | url: Fully qualified address of the Triton server - for e.g. grpc://localhost:8000. 22 | """ 23 | parsed_url = urlparse(url) 24 | if parsed_url.scheme == "grpc": 25 | from tritonclient.grpc import InferenceServerClient, InferInput 26 | 27 | self.client = InferenceServerClient(parsed_url.netloc) # Triton GRPC client 28 | model_repository = self.client.get_model_repository_index() 29 | self.model_name = model_repository.models[0].name 30 | self.metadata = self.client.get_model_metadata(self.model_name, as_json=True) 31 | 32 | def create_input_placeholders() -> typing.List[InferInput]: 33 | return [ 34 | InferInput(i["name"], [int(s) for s in i["shape"]], i["datatype"]) for i in self.metadata["inputs"] 35 | ] 36 | 37 | else: 38 | from tritonclient.http import InferenceServerClient, InferInput 39 | 40 | self.client = InferenceServerClient(parsed_url.netloc) # Triton HTTP client 41 | model_repository = self.client.get_model_repository_index() 42 | self.model_name = model_repository[0]["name"] 43 | self.metadata = self.client.get_model_metadata(self.model_name) 44 | 45 | def create_input_placeholders() -> typing.List[InferInput]: 46 | return [ 47 | InferInput(i["name"], [int(s) for s in i["shape"]], i["datatype"]) for i in self.metadata["inputs"] 48 | ] 49 | 50 | self._create_input_placeholders_fn = create_input_placeholders 51 | 52 | @property 53 | def runtime(self): 54 | """Returns the model runtime.""" 55 | return self.metadata.get("backend", self.metadata.get("platform")) 56 | 57 | def __call__(self, *args, **kwargs) -> typing.Union[torch.Tensor, typing.Tuple[torch.Tensor, ...]]: 58 | """ 59 | Invokes the model. 60 | 61 | Parameters can be provided via args or kwargs. args, if provided, are assumed to match the order of inputs of 62 | the model. kwargs are matched with the model input names. 63 | """ 64 | inputs = self._create_inputs(*args, **kwargs) 65 | response = self.client.infer(model_name=self.model_name, inputs=inputs) 66 | result = [] 67 | for output in self.metadata["outputs"]: 68 | tensor = torch.as_tensor(response.as_numpy(output["name"])) 69 | result.append(tensor) 70 | return result[0] if len(result) == 1 else result 71 | 72 | def _create_inputs(self, *args, **kwargs): 73 | """Creates input tensors from args or kwargs, not both; raises error if none or both are provided.""" 74 | args_len, kwargs_len = len(args), len(kwargs) 75 | if not args_len and not kwargs_len: 76 | raise RuntimeError("No inputs provided.") 77 | if args_len and kwargs_len: 78 | raise RuntimeError("Cannot specify args and kwargs at the same time") 79 | 80 | placeholders = self._create_input_placeholders_fn() 81 | if args_len: 82 | if args_len != len(placeholders): 83 | raise RuntimeError(f"Expected {len(placeholders)} inputs, got {args_len}.") 84 | for input, value in zip(placeholders, args): 85 | input.set_data_from_numpy(value.cpu().numpy()) 86 | else: 87 | for input in placeholders: 88 | value = kwargs[input.name] 89 | input.set_data_from_numpy(value.cpu().numpy()) 90 | return placeholders 91 | --------------------------------------------------------------------------------