├── .idea ├── .gitignore ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml └── yolov5-master.iml ├── Dockerfile ├── LICENSE ├── README.md ├── __pycache__ └── test.cpython-38.pyc ├── add_edge.py ├── convertor └── fold0 │ ├── images │ ├── train2017.shapes │ └── val2017.shapes │ └── labels │ └── train2017.npy ├── data ├── coco.yaml ├── coco128.yaml ├── hyp.finetune.yaml ├── hyp.scratch.yaml ├── scripts │ ├── get_coco.sh │ └── get_voc.sh ├── voc.yaml └── wheat0.yaml ├── detect.py ├── hubconf.py ├── inference └── images │ └── 1.tif ├── label_format.png ├── models ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-38.pyc │ ├── common.cpython-38.pyc │ ├── experimental.cpython-38.pyc │ └── yolo.cpython-38.pyc ├── common.py ├── experimental.py ├── export.py ├── hub │ ├── yolov3-spp.yaml │ ├── yolov5-fpn.yaml │ └── yolov5-panet.yaml ├── yolo.py ├── yolov5l.yaml ├── yolov5m.yaml ├── yolov5s.yaml └── yolov5x.yaml ├── rbox.png ├── requirements.txt ├── result.png ├── retanglelabel2mylabel.py ├── sotabench.py ├── test.py ├── test2.jpg ├── train.py ├── tutorial.ipynb ├── utils ├── __init__.py ├── __pycache__ │ ├── __init__.cpython-38.pyc │ ├── datasets.cpython-38.pyc │ ├── general.cpython-38.pyc │ ├── google_utils.cpython-38.pyc │ └── torch_utils.cpython-38.pyc ├── activations.py ├── datasets.py ├── evolve.sh ├── general.py ├── google_app_engine │ ├── Dockerfile │ ├── additional_requirements.txt │ └── app.yaml ├── google_utils.py ├── kmeans_for_anchors.py ├── torch_utils.py └── yolo_anchors.txt └── weights └── download_weights.sh /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | # Datasource local storage ignored files 5 | /dataSources/ 6 | /dataSources.local.xml 7 | # Editor-based HTTP Client requests 8 | /httpRequests/ 9 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 12 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.idea/yolov5-master.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 12 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Start FROM Nvidia PyTorch image https://ngc.nvidia.com/catalog/containers/nvidia:pytorch 2 | FROM nvcr.io/nvidia/pytorch:20.09-py3 3 | 4 | # Install dependencies 5 | RUN pip install --upgrade pip 6 | # COPY requirements.txt . 7 | # RUN pip install -r requirements.txt 8 | RUN pip install gsutil 9 | 10 | # Create working directory 11 | RUN mkdir -p /usr/src/app 12 | WORKDIR /usr/src/app 13 | 14 | # Copy contents 15 | COPY . /usr/src/app 16 | 17 | # Copy weights 18 | #RUN python3 -c "from models import *; \ 19 | #attempt_download('weights/yolov5s.pt'); \ 20 | #attempt_download('weights/yolov5m.pt'); \ 21 | #attempt_download('weights/yolov5l.pt')" 22 | 23 | 24 | # --------------------------------------------------- Extras Below --------------------------------------------------- 25 | 26 | # Build and Push 27 | # t=ultralytics/yolov5:latest && sudo docker build -t $t . && sudo docker push $t 28 | # for v in {300..303}; do t=ultralytics/coco:v$v && sudo docker build -t $t . && sudo docker push $t; done 29 | 30 | # Pull and Run 31 | # t=ultralytics/yolov5:latest && sudo docker pull $t && sudo docker run -it --ipc=host $t 32 | 33 | # Pull and Run with local directory access 34 | # t=ultralytics/yolov5:latest && sudo docker pull $t && sudo docker run -it --ipc=host --gpus all -v "$(pwd)"/coco:/usr/src/coco $t 35 | 36 | # Kill all 37 | # sudo docker kill $(sudo docker ps -q) 38 | 39 | # Kill all image-based 40 | # sudo docker kill $(sudo docker ps -a -q --filter ancestor=ultralytics/yolov5:latest) 41 | 42 | # Bash into running container 43 | # sudo docker container exec -it ba65811811ab bash 44 | 45 | # Bash into stopped container 46 | # sudo docker commit 092b16b25c5b usr/resume && sudo docker run -it --gpus all --ipc=host -v "$(pwd)"/coco:/usr/src/coco --entrypoint=sh usr/resume 47 | 48 | # Send weights to GCP 49 | # python -c "from utils.general import *; strip_optimizer('runs/exp0_*/weights/best.pt', 'tmp.pt')" && gsutil cp tmp.pt gs://*.pt 50 | 51 | # Clean up 52 | # docker system prune -a --volumes 53 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | the rotation detection 2 | # Requirement 3 | ```bash 4 | torch==1.6 5 | shapely==1.7.1 6 | opencv==4.2.0.34 7 | ``` 8 | # inference 9 | you can download the weights [BaiduYun](https://pan.baidu.com/s/1l7AwoT78tQEQ-K_vOJobQQ)(password is 4ud5) or [GoogleDrive](https://drive.google.com/drive/folders/1McWvzy_UAUCOBFmzjzawVqC0KroSLmEy?usp=sharing) for ship detection by my dataset(not DOTA) to test the demo. 10 | ```bash 11 | $ python detect.py 12 | ``` 13 | ![image](result.png) 14 | # train 15 | ## what format my model need 16 | Not much different from yolo dataset,just add an __angle__ and we define the box attribute w is always __longer__ than h! 17 | 18 | So wo define the box label is (cls, c_x, c_y, Longest side,short side, angle) 19 | 20 | Attention!we define angle is a classify question,so we define 180 classes for angle. 21 | 22 | For Example: 23 | ![image](rbox.png) 24 | Range for angle is [-90,90), so wo should __add__ __90__ in angle while make your dataset label and then your label's Range should be [0,179) 25 | ![image](label_format.png) 26 | ## modify yaml 27 | models/yolov5m.yaml: set nc to your dataset class num; 28 | data/wheat0.yaml:set nc to your dataset class num, and set names to your dataset class name; 29 | 30 | ```bash 31 | $ python train.py 32 | ``` 33 | # update 34 | 2021.1.4---correct some BUG for training 35 | 36 | 37 | # details 38 | If you have any question,welcome discuss with me by [This](https://zhuanlan.zhihu.com/p/270388743) or email to prozacliang@qq.com 39 | -------------------------------------------------------------------------------- /__pycache__/test.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/__pycache__/test.cpython-38.pyc -------------------------------------------------------------------------------- /add_edge.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | # from keras.application.vgg import VGG16 4 | import matplotlib.pyplot as plt 5 | if __name__ == '__main__': 6 | label_path = r'convertor\fold0\labels\train2017/' 7 | img_path = r'inference\test2' 8 | edge_size = 100 9 | 10 | for file in os.listdir(img_path): 11 | 12 | # print(os.path.join(img_path, (file.split('.')[0] + '.tif'))) 13 | img = cv2.imread(os.path.join(img_path, (file.split('.')[0] + '.tif'))) 14 | 15 | 16 | 17 | img = cv2.copyMakeBorder(img,edge_size,edge_size,edge_size,edge_size, cv2.BORDER_CONSTANT,value=[144,144,144]) 18 | cv2.imwrite(r'G:\hjj\yolov5\yolov5-ship\inference\edge_pic/{}'.format(file), img) 19 | 20 | # plt.imshow(img) 21 | # plt.show() 22 | # print(img) -------------------------------------------------------------------------------- /convertor/fold0/images/train2017.shapes: -------------------------------------------------------------------------------- 1 | 1024 1024 2 | 1024 1024 3 | 1024 1024 4 | 1024 1024 5 | 1024 1024 6 | 1024 1024 7 | 1024 1024 8 | 1024 1024 9 | 1024 1024 10 | 1024 1024 11 | 1024 1024 12 | 1024 1024 13 | 1024 1024 14 | 1024 1024 15 | 1024 1024 16 | 1024 1024 17 | 1024 1024 18 | 1024 1024 19 | 1024 1024 20 | 1024 1024 21 | 1024 1024 22 | 1024 1024 23 | 1024 1024 24 | 1024 1024 25 | 1024 1024 26 | 1024 1024 27 | 1024 1024 28 | 1024 1024 29 | 1024 1024 30 | 1024 1024 31 | 1024 1024 32 | 1024 1024 33 | 1024 1024 34 | 1024 1024 35 | 1024 1024 36 | 1024 1024 37 | 1024 1024 38 | 1024 1024 39 | 1024 1024 40 | 1024 1024 41 | 1024 1024 42 | 1024 1024 43 | 1024 1024 44 | 1024 1024 45 | 1024 1024 46 | 1024 1024 47 | 1024 1024 48 | 1024 1024 49 | 1024 1024 50 | 1024 1024 51 | 1024 1024 52 | 1024 1024 53 | 1024 1024 54 | 1024 1024 55 | 1024 1024 56 | 1024 1024 57 | 1024 1024 58 | 1024 1024 59 | 1024 1024 60 | 1024 1024 61 | 1024 1024 62 | 1024 1024 63 | 1024 1024 64 | 1024 1024 65 | 1024 1024 66 | 1024 1024 67 | 1024 1024 68 | 1024 1024 69 | 1024 1024 70 | 1024 1024 71 | 1024 1024 72 | 1024 1024 73 | 1024 1024 74 | 1024 1024 75 | 1024 1024 76 | 1024 1024 77 | 1024 1024 78 | 1024 1024 79 | 1024 1024 80 | 1024 1024 81 | 1024 1024 82 | 1024 1024 83 | 1024 1024 84 | 1024 1024 85 | 1024 1024 86 | 1024 1024 87 | 1024 1024 88 | 1024 1024 89 | 1024 1024 90 | 1024 1024 91 | 1024 1024 92 | 1024 1024 93 | 1024 1024 94 | 1024 1024 95 | 1024 1024 96 | 1024 1024 97 | 1024 1024 98 | 1024 1024 99 | 1024 1024 100 | 1024 1024 101 | 1024 1024 102 | 1024 1024 103 | 1024 1024 104 | 1024 1024 105 | 1024 1024 106 | 1024 1024 107 | 1024 1024 108 | 1024 1024 109 | 1024 1024 110 | 1024 1024 111 | 1024 1024 112 | 1024 1024 113 | 1024 1024 114 | 1024 1024 115 | 1024 1024 116 | 1024 1024 117 | 1024 1024 118 | 1024 1024 119 | 1024 1024 120 | 1024 1024 121 | 1024 1024 122 | 1024 1024 123 | 1024 1024 124 | 1024 1024 125 | 1024 1024 126 | 1024 1024 127 | 1024 1024 128 | 1024 1024 129 | 1024 1024 130 | 1024 1024 131 | 1024 1024 132 | 1024 1024 133 | 1024 1024 134 | 1024 1024 135 | 1024 1024 136 | 1024 1024 137 | 1024 1024 138 | 1024 1024 139 | 1024 1024 140 | 1024 1024 141 | 1024 1024 142 | 1024 1024 143 | 1024 1024 144 | 1024 1024 145 | 1024 1024 146 | 1024 1024 147 | 1024 1024 148 | 1024 1024 149 | 1024 1024 150 | 1024 1024 151 | 1024 1024 152 | 1024 1024 153 | 1024 1024 154 | 1024 1024 155 | 1024 1024 156 | 1024 1024 157 | 1024 1024 158 | 1024 1024 159 | 1024 1024 160 | 1024 1024 161 | 1024 1024 162 | 1024 1024 163 | 1024 1024 164 | 1024 1024 165 | 1024 1024 166 | 1024 1024 167 | 1024 1024 168 | 1024 1024 169 | 1024 1024 170 | 1024 1024 171 | 1024 1024 172 | 1024 1024 173 | 1024 1024 174 | 1024 1024 175 | 1024 1024 176 | 1024 1024 177 | 1024 1024 178 | 1024 1024 179 | 1024 1024 180 | 1024 1024 181 | 1024 1024 182 | 1024 1024 183 | 1024 1024 184 | 1024 1024 185 | 1024 1024 186 | 1024 1024 187 | 1024 1024 188 | 1024 1024 189 | 1024 1024 190 | 1024 1024 191 | 1024 1024 192 | 1024 1024 193 | 1024 1024 194 | 1024 1024 195 | 1024 1024 196 | 1024 1024 197 | 1024 1024 198 | 1024 1024 199 | 1024 1024 200 | 1024 1024 201 | 1024 1024 202 | 1024 1024 203 | 1024 1024 204 | 1024 1024 205 | 1024 1024 206 | 1024 1024 207 | 1024 1024 208 | 1024 1024 209 | 1024 1024 210 | 1024 1024 211 | 1024 1024 212 | 1024 1024 213 | 1024 1024 214 | 1024 1024 215 | 1024 1024 216 | 1024 1024 217 | 1024 1024 218 | 1024 1024 219 | 1024 1024 220 | 1024 1024 221 | 1024 1024 222 | 1024 1024 223 | 1024 1024 224 | 1024 1024 225 | 1024 1024 226 | 1024 1024 227 | 1024 1024 228 | 1024 1024 229 | 1024 1024 230 | 1024 1024 231 | 1024 1024 232 | 1024 1024 233 | 1024 1024 234 | 1024 1024 235 | 1024 1024 236 | 1024 1024 237 | 1024 1024 238 | 1024 1024 239 | 1024 1024 240 | 1024 1024 241 | 1024 1024 242 | 1024 1024 243 | 1024 1024 244 | 1024 1024 245 | 1024 1024 246 | 1024 1024 247 | 1024 1024 248 | 1024 1024 249 | 1024 1024 250 | 1024 1024 251 | 1024 1024 252 | 1024 1024 253 | 1024 1024 254 | 1024 1024 255 | 1024 1024 256 | 1024 1024 257 | 1024 1024 258 | 1024 1024 259 | 1024 1024 260 | 1024 1024 261 | 1024 1024 262 | 1024 1024 263 | 1024 1024 264 | 1024 1024 265 | 1024 1024 266 | 1024 1024 267 | 1024 1024 268 | 1024 1024 269 | 1024 1024 270 | 1024 1024 271 | 1024 1024 272 | 1024 1024 273 | 1024 1024 274 | 1024 1024 275 | 1024 1024 276 | 1024 1024 277 | 1024 1024 278 | 1024 1024 279 | 1024 1024 280 | 1024 1024 281 | 1024 1024 282 | 1024 1024 283 | 1024 1024 284 | 1024 1024 285 | 1024 1024 286 | 1024 1024 287 | 1024 1024 288 | 1024 1024 289 | 1024 1024 290 | 1024 1024 291 | 1024 1024 292 | 1024 1024 293 | 1024 1024 294 | 1024 1024 295 | 1024 1024 296 | 1024 1024 297 | 1024 1024 298 | 1024 1024 299 | 1024 1024 300 | 1024 1024 301 | 1024 1024 302 | 1024 1024 303 | 1024 1024 304 | 1024 1024 305 | 1024 1024 306 | 1024 1024 307 | 1024 1024 308 | 1024 1024 309 | 1024 1024 310 | 1024 1024 311 | 1024 1024 312 | 1024 1024 313 | 1024 1024 314 | 1024 1024 315 | 1024 1024 316 | 1024 1024 317 | 1024 1024 318 | 1024 1024 319 | 1024 1024 320 | 1024 1024 321 | 1024 1024 322 | 1024 1024 323 | 1024 1024 324 | 1024 1024 325 | 1024 1024 326 | 1024 1024 327 | 1024 1024 328 | 1024 1024 329 | 1024 1024 330 | 1024 1024 331 | 1024 1024 332 | 1024 1024 333 | 1024 1024 334 | 1024 1024 335 | 1024 1024 336 | 1024 1024 337 | 1024 1024 338 | 1024 1024 339 | 1024 1024 340 | 1024 1024 341 | 1024 1024 342 | 1024 1024 343 | 1024 1024 344 | 1024 1024 345 | 1024 1024 346 | 1024 1024 347 | 1024 1024 348 | 1024 1024 349 | 1024 1024 350 | 1024 1024 351 | 1024 1024 352 | 1024 1024 353 | 1024 1024 354 | 1024 1024 355 | 1024 1024 356 | 1024 1024 357 | 1024 1024 358 | 1024 1024 359 | 1024 1024 360 | 1024 1024 361 | 1024 1024 362 | 1024 1024 363 | 1024 1024 364 | 1024 1024 365 | 1024 1024 366 | 1024 1024 367 | 1024 1024 368 | 1024 1024 369 | 1024 1024 370 | 1024 1024 371 | 1024 1024 372 | 1024 1024 373 | 1024 1024 374 | 1024 1024 375 | 1024 1024 376 | 1024 1024 377 | 1024 1024 378 | 1024 1024 379 | 1024 1024 380 | 1024 1024 381 | 1024 1024 382 | 1024 1024 383 | 1024 1024 384 | 1024 1024 385 | 1024 1024 386 | 1024 1024 387 | 1024 1024 388 | 1024 1024 389 | 1024 1024 390 | 1024 1024 391 | 1024 1024 392 | 1024 1024 393 | 1024 1024 394 | 1024 1024 395 | 1024 1024 396 | 1024 1024 397 | 1024 1024 398 | 1024 1024 399 | 1024 1024 400 | 1024 1024 401 | 1024 1024 402 | 1024 1024 403 | 1024 1024 404 | 1024 1024 405 | 1024 1024 406 | 1024 1024 407 | 1024 1024 408 | 1024 1024 409 | 1024 1024 410 | 1024 1024 411 | 1024 1024 412 | 1024 1024 413 | 1024 1024 414 | 1024 1024 415 | 1024 1024 416 | 1024 1024 417 | 1024 1024 418 | 1024 1024 419 | 1024 1024 420 | 1024 1024 421 | 1024 1024 422 | 1024 1024 423 | 1024 1024 424 | 1024 1024 425 | 1024 1024 426 | 1024 1024 427 | 1024 1024 428 | 1024 1024 429 | 1024 1024 430 | 1024 1024 431 | 1024 1024 432 | 1024 1024 433 | 1024 1024 434 | 1024 1024 435 | 1024 1024 436 | 1024 1024 437 | 1024 1024 438 | 1024 1024 439 | 1024 1024 440 | 1024 1024 441 | 1024 1024 442 | 1024 1024 443 | 1024 1024 444 | 1024 1024 445 | 1024 1024 446 | 1024 1024 447 | 1024 1024 448 | 1024 1024 449 | 1024 1024 450 | 1024 1024 451 | 1024 1024 452 | 1024 1024 453 | 1024 1024 454 | 1024 1024 455 | 1024 1024 456 | 1024 1024 457 | 1024 1024 458 | 1024 1024 459 | 1024 1024 460 | 1024 1024 461 | 1024 1024 462 | 1024 1024 463 | 1024 1024 464 | 1024 1024 465 | 1024 1024 466 | 1024 1024 467 | 1024 1024 468 | 1024 1024 469 | 1024 1024 470 | 1024 1024 471 | 1024 1024 472 | 1024 1024 473 | 1024 1024 474 | 1024 1024 475 | 1024 1024 476 | 1024 1024 477 | 1024 1024 478 | 1024 1024 479 | 1024 1024 480 | 1024 1024 481 | 1024 1024 482 | 1024 1024 483 | 1024 1024 484 | 1024 1024 485 | 1024 1024 486 | 1024 1024 487 | 1024 1024 488 | 1024 1024 489 | 1024 1024 490 | 1024 1024 491 | 1024 1024 492 | 1024 1024 493 | 1024 1024 494 | 1024 1024 495 | 1024 1024 496 | 1024 1024 497 | 1024 1024 498 | 1024 1024 499 | 1024 1024 500 | 1024 1024 501 | 1024 1024 502 | 1024 1024 503 | 1024 1024 504 | 1024 1024 505 | 1024 1024 506 | 1024 1024 507 | 1024 1024 508 | 1024 1024 509 | 1024 1024 510 | 1024 1024 511 | 1024 1024 512 | 1024 1024 513 | 1024 1024 514 | 1024 1024 515 | 1024 1024 516 | 1024 1024 517 | 1024 1024 518 | 1024 1024 519 | 1024 1024 520 | 1024 1024 521 | 1024 1024 522 | 1024 1024 523 | 1024 1024 524 | 1024 1024 525 | 1024 1024 526 | 1024 1024 527 | 1024 1024 528 | 1024 1024 529 | 1024 1024 530 | 1024 1024 531 | 1024 1024 532 | 1024 1024 533 | 1024 1024 534 | 1024 1024 535 | 1024 1024 536 | 1024 1024 537 | 1024 1024 538 | 1024 1024 539 | 1024 1024 540 | 1024 1024 541 | 1024 1024 542 | 1024 1024 543 | 1024 1024 544 | 1024 1024 545 | 1024 1024 546 | 1024 1024 547 | 1024 1024 548 | 1024 1024 549 | 1024 1024 550 | 1024 1024 551 | 1024 1024 552 | 1024 1024 553 | 1024 1024 554 | 1024 1024 555 | 1024 1024 556 | 1024 1024 557 | 1024 1024 558 | 1024 1024 559 | 1024 1024 560 | 1024 1024 561 | 1024 1024 562 | 1024 1024 563 | 1024 1024 564 | 1024 1024 565 | 1024 1024 566 | 1024 1024 567 | 1024 1024 568 | 1024 1024 569 | 1024 1024 570 | 1024 1024 571 | 1024 1024 572 | 1024 1024 573 | 1024 1024 574 | 1024 1024 575 | 1024 1024 576 | 1024 1024 577 | 1024 1024 578 | 1024 1024 579 | 1024 1024 580 | 1024 1024 581 | 1024 1024 582 | 1024 1024 583 | 1024 1024 584 | 1024 1024 585 | 1024 1024 586 | 1024 1024 587 | 1024 1024 588 | 1024 1024 589 | 1024 1024 590 | 1024 1024 591 | 1024 1024 592 | 1024 1024 593 | 1024 1024 594 | 1024 1024 595 | 1024 1024 596 | 1024 1024 597 | 1024 1024 598 | 1024 1024 599 | 1024 1024 600 | 1024 1024 601 | 1024 1024 602 | 1024 1024 603 | 1024 1024 604 | 1024 1024 605 | 1024 1024 606 | 1024 1024 607 | 1024 1024 608 | 1024 1024 609 | 1024 1024 610 | 1024 1024 611 | 1024 1024 612 | 1024 1024 613 | 1024 1024 614 | 1024 1024 615 | 1024 1024 616 | 1024 1024 617 | 1024 1024 618 | 1024 1024 619 | 1024 1024 620 | 1024 1024 621 | 1024 1024 622 | 1024 1024 623 | 1024 1024 624 | 1024 1024 625 | 1024 1024 626 | 1024 1024 627 | 1024 1024 628 | 1024 1024 629 | 1024 1024 630 | 1024 1024 631 | 1024 1024 632 | 1024 1024 633 | 1024 1024 634 | 1024 1024 635 | 1024 1024 636 | 1024 1024 637 | 1024 1024 638 | 1024 1024 639 | 1024 1024 640 | 1024 1024 641 | 1024 1024 642 | 1024 1024 643 | 1024 1024 644 | 1024 1024 645 | 1024 1024 646 | 1024 1024 647 | 1024 1024 648 | 1024 1024 649 | 1024 1024 650 | 1024 1024 651 | 1024 1024 652 | 1024 1024 653 | 1024 1024 654 | 1024 1024 655 | 1024 1024 656 | 1024 1024 657 | 1024 1024 658 | 1024 1024 659 | 1024 1024 660 | 1024 1024 661 | 1024 1024 662 | 1024 1024 663 | 1024 1024 664 | 1024 1024 665 | 1024 1024 666 | 1024 1024 667 | 1024 1024 668 | 1024 1024 669 | 1024 1024 670 | 1024 1024 671 | 1024 1024 672 | 1024 1024 673 | 1024 1024 674 | 1024 1024 675 | 1024 1024 676 | 1024 1024 677 | 1024 1024 678 | 1024 1024 679 | 1024 1024 680 | 1024 1024 681 | 1024 1024 682 | 1024 1024 683 | 1024 1024 684 | 1024 1024 685 | 1024 1024 686 | 1024 1024 687 | 1024 1024 688 | 1024 1024 689 | 1024 1024 690 | 1024 1024 691 | 1024 1024 692 | 1024 1024 693 | 1024 1024 694 | 1024 1024 695 | 1024 1024 696 | 1024 1024 697 | 1024 1024 698 | 1024 1024 699 | 1024 1024 700 | 1024 1024 701 | -------------------------------------------------------------------------------- /convertor/fold0/images/val2017.shapes: -------------------------------------------------------------------------------- 1 | 1024 1024 2 | 1024 1024 3 | 1024 1024 4 | 1024 1024 5 | 1024 1024 6 | 1024 1024 7 | 1024 1024 8 | 1024 1024 9 | 1024 1024 10 | 1024 1024 11 | 1024 1024 12 | 1024 1024 13 | 1024 1024 14 | 1024 1024 15 | 1024 1024 16 | 1024 1024 17 | 1024 1024 18 | 1024 1024 19 | 1024 1024 20 | 1024 1024 21 | -------------------------------------------------------------------------------- /convertor/fold0/labels/train2017.npy: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/convertor/fold0/labels/train2017.npy -------------------------------------------------------------------------------- /data/coco.yaml: -------------------------------------------------------------------------------- 1 | # COCO 2017 dataset http://cocodataset.org 2 | # Train command: python train.py --data coco.yaml 3 | # Default dataset location is next to /yolov5: 4 | # /parent_folder 5 | # /coco 6 | # /yolov5 7 | 8 | 9 | # download command/URL (optional) 10 | download: bash data/scripts/get_coco.sh 11 | 12 | # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/] 13 | train: ../coco/train2017.txt # 118287 images 14 | val: ../coco/val2017.txt # 5000 images 15 | test: ../coco/test-dev2017.txt # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794 16 | 17 | # number of classes 18 | nc: 80 19 | 20 | # class names 21 | names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 22 | 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 23 | 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 24 | 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 25 | 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 26 | 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 27 | 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 28 | 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 29 | 'hair drier', 'toothbrush'] 30 | 31 | # Print classes 32 | # with open('data/coco.yaml') as f: 33 | # d = yaml.load(f, Loader=yaml.FullLoader) # dict 34 | # for i, x in enumerate(d['names']): 35 | # print(i, x) 36 | -------------------------------------------------------------------------------- /data/coco128.yaml: -------------------------------------------------------------------------------- 1 | # COCO 2017 dataset http://cocodataset.org - first 128 training images 2 | # Train command: python train.py --data coco128.yaml 3 | # Default dataset location is next to /yolov5: 4 | # /parent_folder 5 | # /coco128 6 | # /yolov5 7 | 8 | 9 | # download command/URL (optional) 10 | download: https://github.com/ultralytics/yolov5/releases/download/v1.0/coco128.zip 11 | 12 | # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/] 13 | train: ../coco128/images/train2017/ # 128 images 14 | val: ../coco128/images/train2017/ # 128 images 15 | 16 | # number of classes 17 | nc: 80 18 | 19 | # class names 20 | names: ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', 21 | 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 22 | 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 23 | 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', 24 | 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 25 | 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', 26 | 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 27 | 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 28 | 'hair drier', 'toothbrush'] 29 | -------------------------------------------------------------------------------- /data/hyp.finetune.yaml: -------------------------------------------------------------------------------- 1 | # Hyperparameters for VOC finetuning 2 | # python train.py --batch 64 --weights yolov5m.pt --data voc.yaml --img 512 --epochs 50 3 | # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials 4 | 5 | 6 | # Hyperparameter Evolution Results 7 | # Generations: 306 8 | # P R mAP.5 mAP.5:.95 box obj cls 9 | # Metrics: 0.6 0.936 0.896 0.684 0.0115 0.00805 0.00146 10 | 11 | lr0: 0.0032 12 | lrf: 0.12 13 | momentum: 0.843 14 | weight_decay: 0.00036 15 | warmup_epochs: 2.0 16 | warmup_momentum: 0.5 17 | warmup_bias_lr: 0.05 18 | box: 0.0296 19 | cls: 0.243 20 | cls_pw: 0.631 21 | obj: 0.301 22 | obj_pw: 0.911 23 | iou_t: 0.2 24 | anchor_t: 2.91 25 | # anchors: 3.63 26 | fl_gamma: 0.0 27 | hsv_h: 0.0138 28 | hsv_s: 0.664 29 | hsv_v: 0.464 30 | degrees: 0.373 31 | translate: 0.245 32 | scale: 0.898 33 | shear: 0.602 34 | perspective: 0.0 35 | flipud: 0.00856 36 | fliplr: 0.5 37 | mosaic: 1.0 38 | mixup: 0.243 39 | -------------------------------------------------------------------------------- /data/hyp.scratch.yaml: -------------------------------------------------------------------------------- 1 | # Hyperparameters for COCO training from scratch 2 | # python train.py --batch 40 --cfg yolov5m.yaml --weights '' --data coco.yaml --img 640 --epochs 300 3 | # See tutorials for hyperparameter evolution https://github.com/ultralytics/yolov5#tutorials 4 | 5 | 6 | lr0: 0.01 # initial learning rate (SGD=1E-2, Adam=1E-3) 7 | lrf: 0.2 # final OneCycleLR learning rate (lr0 * lrf) 8 | momentum: 0.937 # SGD momentum/Adam beta1 9 | weight_decay: 0.0005 # optimizer weight decay 5e-4 10 | warmup_epochs: 3.0 # warmup epochs (fractions ok) 11 | warmup_momentum: 0.8 # warmup initial momentum 12 | warmup_bias_lr: 0.1 # warmup initial bias lr 13 | box: 0.05 # box loss gain 14 | cls: 0.5 # cls loss gain 15 | cls_pw: 1.0 # cls BCELoss positive_weight 16 | obj: 1.0 # obj loss gain (scale with pixels) 17 | obj_pw: 1.0 # obj BCELoss positive_weight 18 | angle_pw: 0.5 19 | angle: 0.5 20 | iou_t: 0.20 # IoU training threshold 21 | anchor_t: 4.0 # anchor-multiple threshold 22 | # anchors: 0 # anchors per output grid (0 to ignore) 23 | fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5) 24 | hsv_h: 0.015 # image HSV-Hue augmentation (fraction) 25 | hsv_s: 0.7 # image HSV-Saturation augmentation (fraction) 26 | hsv_v: 0.4 # image HSV-Value augmentation (fraction) 27 | degrees: 0.0 # image rotation (+/- deg) 28 | translate: 0.1 # image translation (+/- fraction) 29 | scale: 0.5 # image scale (+/- gain) 30 | shear: 0.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.5 # image flip left-right (probability) 34 | mosaic: 1.0 # image mosaic (probability) 35 | mixup: 0.0 # image mixup (probability) 36 | -------------------------------------------------------------------------------- /data/scripts/get_coco.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # COCO 2017 dataset http://cocodataset.org 3 | # Download command: bash data/scripts/get_coco.sh 4 | # Train command: python train.py --data coco.yaml 5 | # Default dataset location is next to /yolov5: 6 | # /parent_folder 7 | # /coco 8 | # /yolov5 9 | 10 | # Download/unzip labels 11 | echo 'Downloading COCO 2017 labels ...' 12 | d='../' # unzip directory 13 | f='coco2017labels.zip' && curl -L https://github.com/ultralytics/yolov5/releases/download/v1.0/$f -o $f 14 | unzip -q $f -d $d && rm $f 15 | 16 | # Download/unzip images 17 | echo 'Downloading COCO 2017 images ...' 18 | d='../coco/images' # unzip directory 19 | f='train2017.zip' && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f -d $d && rm $f # 19G, 118k images 20 | f='val2017.zip' && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f -d $d && rm $f # 1G, 5k images 21 | # f='test2017.zip' && curl http://images.cocodataset.org/zips/$f -o $f && unzip -q $f -d $d && rm $f # 7G, 41k images 22 | -------------------------------------------------------------------------------- /data/scripts/get_voc.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC/ 3 | # Download command: bash data/scripts/get_voc.sh 4 | # Train command: python train.py --data voc.yaml 5 | # Default dataset location is next to /yolov5: 6 | # /parent_folder 7 | # /VOC 8 | # /yolov5 9 | 10 | start=$(date +%s) 11 | 12 | # handle optional download dir 13 | if [ -z "$1" ]; then 14 | # navigate to ~/tmp 15 | echo "navigating to ../tmp/ ..." 16 | mkdir -p ../tmp 17 | cd ../tmp/ 18 | else 19 | # check if is valid directory 20 | if [ ! -d $1 ]; then 21 | echo $1 "is not a valid directory" 22 | exit 0 23 | fi 24 | echo "navigating to" $1 "..." 25 | cd $1 26 | fi 27 | 28 | echo "Downloading VOC2007 trainval ..." 29 | # Download data 30 | curl -LO http://pjreddie.com/media/files/VOCtrainval_06-Nov-2007.tar 31 | echo "Downloading VOC2007 test data ..." 32 | curl -LO http://pjreddie.com/media/files/VOCtest_06-Nov-2007.tar 33 | echo "Done downloading." 34 | 35 | # Extract data 36 | echo "Extracting trainval ..." 37 | tar -xf VOCtrainval_06-Nov-2007.tar 38 | echo "Extracting test ..." 39 | tar -xf VOCtest_06-Nov-2007.tar 40 | echo "removing tars ..." 41 | rm VOCtrainval_06-Nov-2007.tar 42 | rm VOCtest_06-Nov-2007.tar 43 | 44 | end=$(date +%s) 45 | runtime=$((end - start)) 46 | 47 | echo "Completed in" $runtime "seconds" 48 | 49 | start=$(date +%s) 50 | 51 | # handle optional download dir 52 | if [ -z "$1" ]; then 53 | # navigate to ~/tmp 54 | echo "navigating to ../tmp/ ..." 55 | mkdir -p ../tmp 56 | cd ../tmp/ 57 | else 58 | # check if is valid directory 59 | if [ ! -d $1 ]; then 60 | echo $1 "is not a valid directory" 61 | exit 0 62 | fi 63 | echo "navigating to" $1 "..." 64 | cd $1 65 | fi 66 | 67 | echo "Downloading VOC2012 trainval ..." 68 | # Download data 69 | curl -LO http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar 70 | echo "Done downloading." 71 | 72 | # Extract data 73 | echo "Extracting trainval ..." 74 | tar -xf VOCtrainval_11-May-2012.tar 75 | echo "removing tar ..." 76 | rm VOCtrainval_11-May-2012.tar 77 | 78 | end=$(date +%s) 79 | runtime=$((end - start)) 80 | 81 | echo "Completed in" $runtime "seconds" 82 | 83 | cd ../tmp 84 | echo "Spliting dataset..." 85 | python3 - "$@" <train.txt 145 | cat 2007_train.txt 2007_val.txt 2007_test.txt 2012_train.txt 2012_val.txt >train.all.txt 146 | 147 | python3 - "$@" <= 1 89 | p, s, im0 = path[i], '%g: ' % i, im0s[i].copy() 90 | else: 91 | p, s, im0 = path, '', im0s 92 | 93 | save_path = str(Path(out) / Path(p).name) 94 | txt_path = str(Path(out) / Path(p).stem) + ('_%g' % dataset.frame if dataset.mode == 'video' else '') 95 | s += '%gx%g ' % img.shape[2:] # print string 96 | gn = torch.tensor(im0.shape)[[1, 0, 1, 0]] # normalization gain whwh 97 | if det is not None and len(det): 98 | # Rescale boxes from img_size to im0 size 99 | det[:, :4] = scale_coords(img.shape[2:], det[:, :4], im0.shape).round() 100 | 101 | # Print results 102 | for c in det[:, 6].unique(): 103 | n = (det[:, 6] == c).sum() # detections per class 104 | s += '%g %ss, ' % (n, names[int(c)]) # add to string 105 | 106 | # Write results 107 | for *xywh, conf, cls in reversed(det): 108 | # if save_txt: # Write to file 109 | # xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 110 | # with open(txt_path + '.txt', 'a') as f: 111 | # f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 112 | 113 | if save_img or view_img: # Add bbox to image 114 | label = '%s %.2f' % (names[int(cls)], conf) 115 | plot_one_box(xywh, im0, label=label, color=colors[int(cls)], line_thickness=3, path=path) 116 | 117 | # Print time (inference + NMS) 118 | print('%sDone. (%.3fs)' % (s, t2 - t1)) 119 | 120 | # Stream results 121 | if view_img: 122 | cv2.imshow(p, im0) 123 | if cv2.waitKey(1) == ord('q'): # q to quit 124 | raise StopIteration 125 | 126 | # Save results (image with detections) 127 | if save_img: 128 | if dataset.mode == 'images': 129 | cv2.imwrite(save_path, im0) 130 | else: 131 | if vid_path != save_path: # new video 132 | vid_path = save_path 133 | if isinstance(vid_writer, cv2.VideoWriter): 134 | vid_writer.release() # release previous video writer 135 | 136 | fourcc = 'mp4v' # output video codec 137 | fps = vid_cap.get(cv2.CAP_PROP_FPS) 138 | w = int(vid_cap.get(cv2.CAP_PROP_FRAME_WIDTH)) 139 | h = int(vid_cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) 140 | vid_writer = cv2.VideoWriter(save_path, cv2.VideoWriter_fourcc(*fourcc), fps, (w, h)) 141 | vid_writer.write(im0) 142 | 143 | if save_txt or save_img: 144 | print('Results saved to %s' % Path(out)) 145 | 146 | print('Done. (%.3fs)' % (time.time() - t0)) 147 | 148 | 149 | if __name__ == '__main__': 150 | 151 | parser = argparse.ArgumentParser() 152 | parser.add_argument('--weights', nargs='+', type=str, default='weights/m-70.pt', help='model.pt path(s)') 153 | parser.add_argument('--source', type=str, default='inference/images', help='source') # file/folder, 0 for webcam 154 | parser.add_argument('--output', type=str, default='inference/output', help='output folder') # output folder 155 | parser.add_argument('--img-size', type=int, default=1024, help='inference size (pixels)') 156 | parser.add_argument('--conf-thres', type=float, default=0.4, help='object confidence threshold') 157 | parser.add_argument('--iou-thres', type=float, default=0.5, help='IOU threshold for NMS') 158 | parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') 159 | parser.add_argument('--view-img', action='store_true', help='display results') 160 | parser.add_argument('--save-txt', action='store_true', help='save results to *.txt') 161 | parser.add_argument('--classes', nargs='+', type=int, help='filter by class: --class 0, or --class 0 2 3') 162 | parser.add_argument('--agnostic-nms', action='store_true', help='class-agnostic NMS') 163 | parser.add_argument('--augment', action='store_true', help='augmented inference') 164 | parser.add_argument('--update', action='store_true', help='update all models') 165 | opt = parser.parse_args() 166 | print(opt) 167 | 168 | with torch.no_grad(): 169 | if opt.update: # update all models (to fix SourceChangeWarning) 170 | for opt.weights in ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt']: 171 | detect() 172 | strip_optimizer(opt.weights) 173 | else: 174 | detect() -------------------------------------------------------------------------------- /hubconf.py: -------------------------------------------------------------------------------- 1 | """File for accessing YOLOv5 via PyTorch Hub https://pytorch.org/hub/ 2 | 3 | Usage: 4 | import torch 5 | model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True, channels=3, classes=80) 6 | """ 7 | 8 | dependencies = ['torch', 'yaml'] 9 | import os 10 | 11 | import torch 12 | 13 | from models.common import NMS 14 | from models.yolo import Model 15 | from utils.google_utils import attempt_download 16 | 17 | 18 | def create(name, pretrained, channels, classes): 19 | """Creates a specified YOLOv5 model 20 | 21 | Arguments: 22 | name (str): name of model, i.e. 'yolov5s' 23 | pretrained (bool): load pretrained weights into the model 24 | channels (int): number of input channels 25 | classes (int): number of model classes 26 | 27 | Returns: 28 | pytorch model 29 | """ 30 | config = os.path.join(os.path.dirname(__file__), 'models', '%s.yaml' % name) # model.yaml path 31 | try: 32 | model = Model(config, channels, classes) 33 | if pretrained: 34 | ckpt = '%s.pt' % name # checkpoint filename 35 | attempt_download(ckpt) # download if not found locally 36 | state_dict = torch.load(ckpt, map_location=torch.device('cpu'))['model'].float().state_dict() # to FP32 37 | state_dict = {k: v for k, v in state_dict.items() if model.state_dict()[k].shape == v.shape} # filter 38 | model.load_state_dict(state_dict, strict=False) # load 39 | 40 | model.add_nms() # add NMS module 41 | model.eval() 42 | return model 43 | 44 | except Exception as e: 45 | help_url = 'https://github.com/ultralytics/yolov5/issues/36' 46 | s = 'Cache maybe be out of date, deleting cache and retrying may solve this. See %s for help.' % help_url 47 | raise Exception(s) from e 48 | 49 | 50 | def yolov5s(pretrained=False, channels=3, classes=80): 51 | """YOLOv5-small model from https://github.com/ultralytics/yolov5 52 | 53 | Arguments: 54 | pretrained (bool): load pretrained weights into the model, default=False 55 | channels (int): number of input channels, default=3 56 | classes (int): number of model classes, default=80 57 | 58 | Returns: 59 | pytorch model 60 | """ 61 | return create('yolov5s', pretrained, channels, classes) 62 | 63 | 64 | def yolov5m(pretrained=False, channels=3, classes=80): 65 | """YOLOv5-medium model from https://github.com/ultralytics/yolov5 66 | 67 | Arguments: 68 | pretrained (bool): load pretrained weights into the model, default=False 69 | channels (int): number of input channels, default=3 70 | classes (int): number of model classes, default=80 71 | 72 | Returns: 73 | pytorch model 74 | """ 75 | return create('yolov5m', pretrained, channels, classes) 76 | 77 | 78 | def yolov5l(pretrained=False, channels=3, classes=80): 79 | """YOLOv5-large model from https://github.com/ultralytics/yolov5 80 | 81 | Arguments: 82 | pretrained (bool): load pretrained weights into the model, default=False 83 | channels (int): number of input channels, default=3 84 | classes (int): number of model classes, default=80 85 | 86 | Returns: 87 | pytorch model 88 | """ 89 | return create('yolov5l', pretrained, channels, classes) 90 | 91 | 92 | def yolov5x(pretrained=False, channels=3, classes=80): 93 | """YOLOv5-xlarge model from https://github.com/ultralytics/yolov5 94 | 95 | Arguments: 96 | pretrained (bool): load pretrained weights into the model, default=False 97 | channels (int): number of input channels, default=3 98 | classes (int): number of model classes, default=80 99 | 100 | Returns: 101 | pytorch model 102 | """ 103 | return create('yolov5x', pretrained, channels, classes) 104 | -------------------------------------------------------------------------------- /inference/images/1.tif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/inference/images/1.tif -------------------------------------------------------------------------------- /label_format.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/label_format.png -------------------------------------------------------------------------------- /models/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/models/__init__.py -------------------------------------------------------------------------------- /models/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/models/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /models/__pycache__/common.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/models/__pycache__/common.cpython-38.pyc -------------------------------------------------------------------------------- /models/__pycache__/experimental.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/models/__pycache__/experimental.cpython-38.pyc -------------------------------------------------------------------------------- /models/__pycache__/yolo.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/models/__pycache__/yolo.cpython-38.pyc -------------------------------------------------------------------------------- /models/common.py: -------------------------------------------------------------------------------- 1 | # This file contains modules common to various models 2 | import math 3 | 4 | import torch 5 | import torch.nn as nn 6 | from utils.general import non_max_suppression 7 | 8 | 9 | def autopad(k, p=None): # kernel, padding 10 | # Pad to 'same' 11 | if p is None: 12 | p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad 13 | return p 14 | 15 | 16 | def DWConv(c1, c2, k=1, s=1, act=True): 17 | # Depthwise convolution 18 | return Conv(c1, c2, k, s, g=math.gcd(c1, c2), act=act) 19 | 20 | 21 | class Conv(nn.Module): 22 | # Standard convolution 23 | def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups 24 | super(Conv, self).__init__() 25 | self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) 26 | self.bn = nn.BatchNorm2d(c2) 27 | self.act = nn.Hardswish() if act else nn.Identity() 28 | 29 | def forward(self, x): 30 | return self.act(self.bn(self.conv(x))) 31 | 32 | def fuseforward(self, x): 33 | return self.act(self.conv(x)) 34 | 35 | 36 | class Bottleneck(nn.Module): 37 | # Standard bottleneck 38 | def __init__(self, c1, c2, shortcut=True, g=1, e=0.5): # ch_in, ch_out, shortcut, groups, expansion 39 | super(Bottleneck, self).__init__() 40 | c_ = int(c2 * e) # hidden channels 41 | self.cv1 = Conv(c1, c_, 1, 1) 42 | self.cv2 = Conv(c_, c2, 3, 1, g=g) 43 | self.add = shortcut and c1 == c2 44 | 45 | def forward(self, x): 46 | return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) 47 | 48 | 49 | class BottleneckCSP(nn.Module): 50 | # CSP Bottleneck https://github.com/WongKinYiu/CrossStagePartialNetworks 51 | def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion 52 | super(BottleneckCSP, self).__init__() 53 | c_ = int(c2 * e) # hidden channels 54 | self.cv1 = Conv(c1, c_, 1, 1) 55 | self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) 56 | self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) 57 | self.cv4 = Conv(2 * c_, c2, 1, 1) 58 | self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) 59 | self.act = nn.LeakyReLU(0.1, inplace=True) 60 | self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)]) 61 | 62 | def forward(self, x): 63 | y1 = self.cv3(self.m(self.cv1(x))) 64 | y2 = self.cv2(x) 65 | return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) 66 | 67 | 68 | class SPP(nn.Module): 69 | # Spatial pyramid pooling layer used in YOLOv3-SPP 70 | def __init__(self, c1, c2, k=(5, 9, 13)): 71 | super(SPP, self).__init__() 72 | c_ = c1 // 2 # hidden channels 73 | self.cv1 = Conv(c1, c_, 1, 1) 74 | self.cv2 = Conv(c_ * (len(k) + 1), c2, 1, 1) 75 | self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k]) 76 | 77 | def forward(self, x): 78 | x = self.cv1(x) 79 | return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1)) 80 | 81 | 82 | class Focus(nn.Module): 83 | # Focus wh information into c-space 84 | def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True): # ch_in, ch_out, kernel, stride, padding, groups 85 | super(Focus, self).__init__() 86 | self.conv = Conv(c1 * 4, c2, k, s, p, g, act) 87 | 88 | def forward(self, x): # x(b,c,w,h) -> y(b,4c,w/2,h/2) 89 | return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1)) 90 | 91 | 92 | class Concat(nn.Module): 93 | # Concatenate a list of tensors along dimension 94 | def __init__(self, dimension=1): 95 | super(Concat, self).__init__() 96 | self.d = dimension 97 | 98 | def forward(self, x): 99 | return torch.cat(x, self.d) 100 | 101 | 102 | class NMS(nn.Module): 103 | # Non-Maximum Suppression (NMS) module 104 | conf = 0.3 # confidence threshold 105 | iou = 0.6 # IoU threshold 106 | classes = None # (optional list) filter by class 107 | 108 | def __init__(self, dimension=1): 109 | super(NMS, self).__init__() 110 | 111 | def forward(self, x): 112 | return non_max_suppression(x[0], conf_thres=self.conf, iou_thres=self.iou, classes=self.classes) 113 | 114 | 115 | class Flatten(nn.Module): 116 | # Use after nn.AdaptiveAvgPool2d(1) to remove last 2 dimensions 117 | @staticmethod 118 | def forward(x): 119 | return x.view(x.size(0), -1) 120 | 121 | 122 | class Classify(nn.Module): 123 | # Classification head, i.e. x(b,c1,20,20) to x(b,c2) 124 | def __init__(self, c1, c2, k=1, s=1, p=None, g=1): # ch_in, ch_out, kernel, stride, padding, groups 125 | super(Classify, self).__init__() 126 | self.aap = nn.AdaptiveAvgPool2d(1) # to x(b,c1,1,1) 127 | self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False) # to x(b,c2,1,1) 128 | self.flat = Flatten() 129 | 130 | def forward(self, x): 131 | z = torch.cat([self.aap(y) for y in (x if isinstance(x, list) else [x])], 1) # cat if list 132 | return self.flat(self.conv(z)) # flatten to x(b,c2) 133 | -------------------------------------------------------------------------------- /models/experimental.py: -------------------------------------------------------------------------------- 1 | # This file contains experimental modules 2 | 3 | import numpy as np 4 | import torch 5 | import torch.nn as nn 6 | 7 | from models.common import Conv, DWConv 8 | from utils.google_utils import attempt_download 9 | 10 | 11 | class CrossConv(nn.Module): 12 | # Cross Convolution Downsample 13 | def __init__(self, c1, c2, k=3, s=1, g=1, e=1.0, shortcut=False): 14 | # ch_in, ch_out, kernel, stride, groups, expansion, shortcut 15 | super(CrossConv, self).__init__() 16 | c_ = int(c2 * e) # hidden channels 17 | self.cv1 = Conv(c1, c_, (1, k), (1, s)) 18 | self.cv2 = Conv(c_, c2, (k, 1), (s, 1), g=g) 19 | self.add = shortcut and c1 == c2 20 | 21 | def forward(self, x): 22 | return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x)) 23 | 24 | 25 | class C3(nn.Module): 26 | # Cross Convolution CSP 27 | def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5): # ch_in, ch_out, number, shortcut, groups, expansion 28 | super(C3, self).__init__() 29 | c_ = int(c2 * e) # hidden channels 30 | self.cv1 = Conv(c1, c_, 1, 1) 31 | self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False) 32 | self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False) 33 | self.cv4 = Conv(2 * c_, c2, 1, 1) 34 | self.bn = nn.BatchNorm2d(2 * c_) # applied to cat(cv2, cv3) 35 | self.act = nn.LeakyReLU(0.1, inplace=True) 36 | self.m = nn.Sequential(*[CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)]) 37 | 38 | def forward(self, x): 39 | y1 = self.cv3(self.m(self.cv1(x))) 40 | y2 = self.cv2(x) 41 | return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1)))) 42 | 43 | 44 | class Sum(nn.Module): 45 | # Weighted sum of 2 or more layers https://arxiv.org/abs/1911.09070 46 | def __init__(self, n, weight=False): # n: number of inputs 47 | super(Sum, self).__init__() 48 | self.weight = weight # apply weights boolean 49 | self.iter = range(n - 1) # iter object 50 | if weight: 51 | self.w = nn.Parameter(-torch.arange(1., n) / 2, requires_grad=True) # layer weights 52 | 53 | def forward(self, x): 54 | y = x[0] # no weight 55 | if self.weight: 56 | w = torch.sigmoid(self.w) * 2 57 | for i in self.iter: 58 | y = y + x[i + 1] * w[i] 59 | else: 60 | for i in self.iter: 61 | y = y + x[i + 1] 62 | return y 63 | 64 | 65 | class GhostConv(nn.Module): 66 | # Ghost Convolution https://github.com/huawei-noah/ghostnet 67 | def __init__(self, c1, c2, k=1, s=1, g=1, act=True): # ch_in, ch_out, kernel, stride, groups 68 | super(GhostConv, self).__init__() 69 | c_ = c2 // 2 # hidden channels 70 | self.cv1 = Conv(c1, c_, k, s, g, act) 71 | self.cv2 = Conv(c_, c_, 5, 1, c_, act) 72 | 73 | def forward(self, x): 74 | y = self.cv1(x) 75 | return torch.cat([y, self.cv2(y)], 1) 76 | 77 | 78 | class GhostBottleneck(nn.Module): 79 | # Ghost Bottleneck https://github.com/huawei-noah/ghostnet 80 | def __init__(self, c1, c2, k, s): 81 | super(GhostBottleneck, self).__init__() 82 | c_ = c2 // 2 83 | self.conv = nn.Sequential(GhostConv(c1, c_, 1, 1), # pw 84 | DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(), # dw 85 | GhostConv(c_, c2, 1, 1, act=False)) # pw-linear 86 | self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False), 87 | Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity() 88 | 89 | def forward(self, x): 90 | return self.conv(x) + self.shortcut(x) 91 | 92 | 93 | class MixConv2d(nn.Module): 94 | # Mixed Depthwise Conv https://arxiv.org/abs/1907.09595 95 | def __init__(self, c1, c2, k=(1, 3), s=1, equal_ch=True): 96 | super(MixConv2d, self).__init__() 97 | groups = len(k) 98 | if equal_ch: # equal c_ per group 99 | i = torch.linspace(0, groups - 1E-6, c2).floor() # c2 indices 100 | c_ = [(i == g).sum() for g in range(groups)] # intermediate channels 101 | else: # equal weight.numel() per group 102 | b = [c2] + [0] * groups 103 | a = np.eye(groups + 1, groups, k=-1) 104 | a -= np.roll(a, 1, axis=1) 105 | a *= np.array(k) ** 2 106 | a[0] = 1 107 | c_ = np.linalg.lstsq(a, b, rcond=None)[0].round() # solve for equal weight indices, ax = b 108 | 109 | self.m = nn.ModuleList([nn.Conv2d(c1, int(c_[g]), k[g], s, k[g] // 2, bias=False) for g in range(groups)]) 110 | self.bn = nn.BatchNorm2d(c2) 111 | self.act = nn.LeakyReLU(0.1, inplace=True) 112 | 113 | def forward(self, x): 114 | return x + self.act(self.bn(torch.cat([m(x) for m in self.m], 1))) 115 | 116 | 117 | class Ensemble(nn.ModuleList): 118 | # Ensemble of models 119 | def __init__(self): 120 | super(Ensemble, self).__init__() 121 | 122 | def forward(self, x, augment=False): 123 | y = [] 124 | for module in self: 125 | y.append(module(x, augment)[0]) 126 | # y = torch.stack(y).max(0)[0] # max ensemble 127 | # y = torch.cat(y, 1) # nms ensemble 128 | y = torch.stack(y).mean(0) # mean ensemble 129 | return y, None # inference, train output 130 | 131 | 132 | def attempt_load(weights, map_location=None): 133 | # Loads an ensemble of models weights=[a,b,c] or a single model weights=[a] or weights=a 134 | model = Ensemble() 135 | for w in weights if isinstance(weights, list) else [weights]: 136 | attempt_download(w) 137 | model.append(torch.load(w, map_location=map_location)['model'].float().fuse().eval()) # load FP32 model 138 | 139 | if len(model) == 1: 140 | return model[-1] # return model 141 | else: 142 | print('Ensemble created with %s\n' % weights) 143 | for k in ['names', 'stride']: 144 | setattr(model, k, getattr(model[-1], k)) 145 | return model # return ensemble 146 | -------------------------------------------------------------------------------- /models/export.py: -------------------------------------------------------------------------------- 1 | """Exports a YOLOv5 *.pt model to ONNX and TorchScript formats 2 | 3 | Usage: 4 | $ export PYTHONPATH="$PWD" && python models/export.py --weights ./weights/yolov5s.pt --img 640 --batch 1 5 | """ 6 | 7 | import argparse 8 | import sys 9 | import time 10 | 11 | sys.path.append('./') # to run '$ python *.py' files in subdirectories 12 | 13 | import torch 14 | import torch.nn as nn 15 | 16 | import models 17 | from models.experimental import attempt_load 18 | from utils.activations import Hardswish 19 | from utils.general import set_logging, check_img_size 20 | 21 | if __name__ == '__main__': 22 | parser = argparse.ArgumentParser() 23 | parser.add_argument('--weights', type=str, default='./yolov5s.pt', help='weights path') # from yolov5/models/ 24 | parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image size') # height, width 25 | parser.add_argument('--batch-size', type=int, default=1, help='batch size') 26 | opt = parser.parse_args() 27 | opt.img_size *= 2 if len(opt.img_size) == 1 else 1 # expand 28 | print(opt) 29 | set_logging() 30 | t = time.time() 31 | 32 | # Load PyTorch model 33 | model = attempt_load(opt.weights, map_location=torch.device('cpu')) # load FP32 model 34 | labels = model.names 35 | 36 | # Checks 37 | gs = int(max(model.stride)) # grid size (max stride) 38 | opt.img_size = [check_img_size(x, gs) for x in opt.img_size] # verify img_size are gs-multiples 39 | 40 | # Input 41 | img = torch.zeros(opt.batch_size, 3, *opt.img_size) # image size(1,3,320,192) iDetection 42 | 43 | # Update model 44 | for k, m in model.named_modules(): 45 | m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatibility 46 | if isinstance(m, models.common.Conv) and isinstance(m.act, nn.Hardswish): 47 | m.act = Hardswish() # assign activation 48 | # if isinstance(m, models.yolo.Detect): 49 | # m.forward = m.forward_export # assign forward (optional) 50 | model.model[-1].export = True # set Detect() layer export=True 51 | y = model(img) # dry run 52 | 53 | # TorchScript export 54 | try: 55 | print('\nStarting TorchScript export with torch %s...' % torch.__version__) 56 | f = opt.weights.replace('.pt', '.torchscript.pt') # filename 57 | ts = torch.jit.trace(model, img) 58 | ts.save(f) 59 | print('TorchScript export success, saved as %s' % f) 60 | except Exception as e: 61 | print('TorchScript export failure: %s' % e) 62 | 63 | # ONNX export 64 | try: 65 | import onnx 66 | 67 | print('\nStarting ONNX export with onnx %s...' % onnx.__version__) 68 | f = opt.weights.replace('.pt', '.onnx') # filename 69 | torch.onnx.export(model, img, f, verbose=False, opset_version=12, input_names=['images'], 70 | output_names=['classes', 'boxes'] if y is None else ['output']) 71 | 72 | # Checks 73 | onnx_model = onnx.load(f) # load onnx model 74 | onnx.checker.check_model(onnx_model) # check onnx model 75 | # print(onnx.helper.printable_graph(onnx_model.graph)) # print a human readable model 76 | print('ONNX export success, saved as %s' % f) 77 | except Exception as e: 78 | print('ONNX export failure: %s' % e) 79 | 80 | # CoreML export 81 | try: 82 | import coremltools as ct 83 | 84 | print('\nStarting CoreML export with coremltools %s...' % ct.__version__) 85 | # convert model from torchscript and apply pixel scaling as per detect.py 86 | model = ct.convert(ts, inputs=[ct.ImageType(name='image', shape=img.shape, scale=1 / 255.0, bias=[0, 0, 0])]) 87 | f = opt.weights.replace('.pt', '.mlmodel') # filename 88 | model.save(f) 89 | print('CoreML export success, saved as %s' % f) 90 | except Exception as e: 91 | print('CoreML export failure: %s' % e) 92 | 93 | # Finish 94 | print('\nExport complete (%.2fs). Visualize with https://github.com/lutzroeder/netron.' % (time.time() - t)) 95 | -------------------------------------------------------------------------------- /models/hub/yolov3-spp.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 80 # number of classes 3 | depth_multiple: 1.0 # model depth multiple 4 | width_multiple: 1.0 # layer channel multiple 5 | 6 | # anchors 7 | anchors: 8 | - [10,13, 16,30, 33,23] # P3/8 9 | - [30,61, 62,45, 59,119] # P4/16 10 | - [116,90, 156,198, 373,326] # P5/32 11 | 12 | # darknet53 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [[-1, 1, Conv, [32, 3, 1]], # 0 16 | [-1, 1, Conv, [64, 3, 2]], # 1-P1/2 17 | [-1, 1, Bottleneck, [64]], 18 | [-1, 1, Conv, [128, 3, 2]], # 3-P2/4 19 | [-1, 2, Bottleneck, [128]], 20 | [-1, 1, Conv, [256, 3, 2]], # 5-P3/8 21 | [-1, 8, Bottleneck, [256]], 22 | [-1, 1, Conv, [512, 3, 2]], # 7-P4/16 23 | [-1, 8, Bottleneck, [512]], 24 | [-1, 1, Conv, [1024, 3, 2]], # 9-P5/32 25 | [-1, 4, Bottleneck, [1024]], # 10 26 | ] 27 | 28 | # YOLOv3-SPP head 29 | head: 30 | [[-1, 1, Bottleneck, [1024, False]], 31 | [-1, 1, SPP, [512, [5, 9, 13]]], 32 | [-1, 1, Conv, [1024, 3, 1]], 33 | [-1, 1, Conv, [512, 1, 1]], 34 | [-1, 1, Conv, [1024, 3, 1]], # 15 (P5/32-large) 35 | 36 | [-2, 1, Conv, [256, 1, 1]], 37 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 38 | [[-1, 8], 1, Concat, [1]], # cat backbone P4 39 | [-1, 1, Bottleneck, [512, False]], 40 | [-1, 1, Bottleneck, [512, False]], 41 | [-1, 1, Conv, [256, 1, 1]], 42 | [-1, 1, Conv, [512, 3, 1]], # 22 (P4/16-medium) 43 | 44 | [-2, 1, Conv, [128, 1, 1]], 45 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 46 | [[-1, 6], 1, Concat, [1]], # cat backbone P3 47 | [-1, 1, Bottleneck, [256, False]], 48 | [-1, 2, Bottleneck, [256, False]], # 27 (P3/8-small) 49 | 50 | [[27, 22, 15], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 51 | ] 52 | -------------------------------------------------------------------------------- /models/hub/yolov5-fpn.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 80 # number of classes 3 | depth_multiple: 1.0 # model depth multiple 4 | width_multiple: 1.0 # layer channel multiple 5 | 6 | # anchors 7 | anchors: 8 | - [10,13, 16,30, 33,23] # P3/8 9 | - [30,61, 62,45, 59,119] # P4/16 10 | - [116,90, 156,198, 373,326] # P5/32 11 | 12 | # YOLOv5 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 17 | [-1, 3, Bottleneck, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 6, BottleneckCSP, [1024]], # 9 25 | ] 26 | 27 | # YOLOv5 FPN head 28 | head: 29 | [[-1, 3, BottleneckCSP, [1024, False]], # 10 (P5/32-large) 30 | 31 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 32 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 33 | [-1, 1, Conv, [512, 1, 1]], 34 | [-1, 3, BottleneckCSP, [512, False]], # 14 (P4/16-medium) 35 | 36 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 37 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 38 | [-1, 1, Conv, [256, 1, 1]], 39 | [-1, 3, BottleneckCSP, [256, False]], # 18 (P3/8-small) 40 | 41 | [[18, 14, 10], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 42 | ] 43 | -------------------------------------------------------------------------------- /models/hub/yolov5-panet.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 80 # number of classes 3 | depth_multiple: 1.0 # model depth multiple 4 | width_multiple: 1.0 # layer channel multiple 5 | 6 | # anchors 7 | anchors: 8 | - [116,90, 156,198, 373,326] # P5/32 9 | - [30,61, 62,45, 59,119] # P4/16 10 | - [10,13, 16,30, 33,23] # P3/8 11 | 12 | # YOLOv5 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 17 | [-1, 3, BottleneckCSP, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 3, BottleneckCSP, [1024, False]], # 9 25 | ] 26 | 27 | # YOLOv5 PANet head 28 | head: 29 | [[-1, 1, Conv, [512, 1, 1]], 30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 32 | [-1, 3, BottleneckCSP, [512, False]], # 13 33 | 34 | [-1, 1, Conv, [256, 1, 1]], 35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 37 | [-1, 3, BottleneckCSP, [256, False]], # 17 (P3/8-small) 38 | 39 | [-1, 1, Conv, [256, 3, 2]], 40 | [[-1, 14], 1, Concat, [1]], # cat head P4 41 | [-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium) 42 | 43 | [-1, 1, Conv, [512, 3, 2]], 44 | [[-1, 10], 1, Concat, [1]], # cat head P5 45 | [-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large) 46 | 47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P5, P4, P3) 48 | ] 49 | -------------------------------------------------------------------------------- /models/yolo.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import logging 3 | import math 4 | import sys 5 | from copy import deepcopy 6 | from pathlib import Path 7 | 8 | sys.path.append('./') # to run '$ python *.py' files in subdirectories 9 | logger = logging.getLogger(__name__) 10 | 11 | import torch 12 | import torch.nn as nn 13 | 14 | from models.common import Conv, Bottleneck, SPP, DWConv, Focus, BottleneckCSP, Concat, NMS 15 | from models.experimental import MixConv2d, CrossConv, C3 16 | from utils.general import check_anchor_order, make_divisible, check_file, set_logging 17 | from utils.torch_utils import ( 18 | time_synchronized, fuse_conv_and_bn, model_info, scale_img, initialize_weights, select_device) 19 | 20 | 21 | class Detect(nn.Module): 22 | stride = None # strides computed during build 23 | export = False # onnx export 24 | 25 | def __init__(self, nc=80, anchors=(), ch=()): # detection layer 26 | super(Detect, self).__init__() 27 | self.nc = nc # number of classes 28 | self.angle = 180 29 | self.no = nc + 5 + self.angle # number of outputs per anchor 30 | self.nl = len(anchors) # number of detection layers 31 | self.na = len(anchors[0]) // 2 # number of anchors 32 | self.grid = [torch.zeros(1)] * self.nl # init grid 33 | a = torch.tensor(anchors).float().view(self.nl, -1, 2) 34 | self.register_buffer('anchors', a) # shape(nl,na,2) 35 | self.register_buffer('anchor_grid', a.clone().view(self.nl, 1, -1, 1, 1, 2)) # shape(nl,1,na,1,1,2) 36 | self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv 37 | 38 | def forward(self, x): 39 | # x = x.copy() # for profiling 40 | z = [] # inference output 41 | self.training |= self.export 42 | for i in range(self.nl): 43 | x[i] = self.m[i](x[i]) # conv 44 | bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) 45 | x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() 46 | 47 | if not self.training: # inference 48 | if self.grid[i].shape[2:4] != x[i].shape[2:4]: 49 | self.grid[i] = self._make_grid(nx, ny).to(x[i].device) 50 | 51 | y = x[i].sigmoid() 52 | y[..., 0:2] = (y[..., 0:2] * 2. - 0.5 + self.grid[i].to(x[i].device)) * self.stride[i] # xy 53 | y[..., 2:4] = (y[..., 2:4] * 2) ** 2 * self.anchor_grid[i] # wh 54 | z.append(y.view(bs, -1, self.no)) 55 | 56 | return x if self.training else (torch.cat(z, 1), x) 57 | 58 | @staticmethod 59 | def _make_grid(nx=20, ny=20): 60 | yv, xv = torch.meshgrid([torch.arange(ny), torch.arange(nx)]) 61 | return torch.stack((xv, yv), 2).view((1, 1, ny, nx, 2)).float() 62 | 63 | 64 | class Model(nn.Module): 65 | def __init__(self, cfg='yolov5s.yaml', ch=3, nc=None): # model, input channels, number of classes 66 | super(Model, self).__init__() 67 | if isinstance(cfg, dict): 68 | self.yaml = cfg # model dict 69 | else: # is *.yaml 70 | import yaml # for torch hub 71 | self.yaml_file = Path(cfg).name 72 | with open(cfg) as f: 73 | self.yaml = yaml.load(f, Loader=yaml.FullLoader) # model dict 74 | 75 | # Define model 76 | if nc and nc != self.yaml['nc']: 77 | print('Overriding model.yaml nc=%g with nc=%g' % (self.yaml['nc'], nc)) 78 | self.yaml['nc'] = nc # override yaml value 79 | self.model, self.save = parse_model(deepcopy(self.yaml), ch=[ch]) # model, savelist, ch_out 80 | # print([x.shape for x in self.forward(torch.zeros(1, ch, 64, 64))]) 81 | 82 | # Build strides, anchors 83 | m = self.model[-1] # Detect() 84 | if isinstance(m, Detect): 85 | s = 128 # 2x min stride 86 | m.stride = torch.tensor([s / x.shape[-2] for x in self.forward(torch.zeros(1, ch, s, s))]) # forward 87 | m.anchors /= m.stride.view(-1, 1, 1) 88 | check_anchor_order(m) 89 | self.stride = m.stride 90 | self._initialize_biases() # only run once 91 | # print('Strides: %s' % m.stride.tolist()) 92 | 93 | # Init weights, biases 94 | initialize_weights(self) 95 | self.info() 96 | print('') 97 | 98 | def forward(self, x, augment=False, profile=False): 99 | if augment: 100 | img_size = x.shape[-2:] # height, width 101 | s = [1, 0.83, 0.67] # scales 102 | f = [None, 3, None] # flips (2-ud, 3-lr) 103 | y = [] # outputs 104 | for si, fi in zip(s, f): 105 | xi = scale_img(x.flip(fi) if fi else x, si) 106 | yi = self.forward_once(xi)[0] # forward 107 | # cv2.imwrite('img%g.jpg' % s, 255 * xi[0].numpy().transpose((1, 2, 0))[:, :, ::-1]) # save 108 | yi[..., :4] /= si # de-scale 109 | if fi == 2: 110 | yi[..., 1] = img_size[0] - yi[..., 1] # de-flip ud 111 | elif fi == 3: 112 | yi[..., 0] = img_size[1] - yi[..., 0] # de-flip lr 113 | y.append(yi) 114 | return torch.cat(y, 1), None # augmented inference, train 115 | else: 116 | return self.forward_once(x, profile) # single-scale inference, train 117 | 118 | def forward_once(self, x, profile=False): 119 | y, dt = [], [] # outputs 120 | for m in self.model: 121 | if m.f != -1: # if not from previous layer 122 | x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers 123 | 124 | if profile: 125 | try: 126 | import thop 127 | o = thop.profile(m, inputs=(x,), verbose=False)[0] / 1E9 * 2 # FLOPS 128 | except: 129 | o = 0 130 | t = time_synchronized() 131 | for _ in range(10): 132 | _ = m(x) 133 | dt.append((time_synchronized() - t) * 100) 134 | print('%10.1f%10.0f%10.1fms %-40s' % (o, m.np, dt[-1], m.type)) 135 | 136 | x = m(x) # run 137 | y.append(x if m.i in self.save else None) # save output 138 | 139 | if profile: 140 | print('%.1fms total' % sum(dt)) 141 | return x 142 | 143 | def _initialize_biases(self, cf=None): # initialize biases into Detect(), cf is class frequency 144 | # cf = torch.bincount(torch.tensor(np.concatenate(dataset.labels, 0)[:, 0]).long(), minlength=nc) + 1. 145 | m = self.model[-1] # Detect() module 146 | for mi, s in zip(m.m, m.stride): # from 147 | b = mi.bias.view(m.na, -1) # conv.bias(255) to (3,85) 148 | b[:, 4] += math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image) 149 | b[:, 5:] += math.log(0.6 / (m.nc - 0.99)) if cf is None else torch.log(cf / cf.sum()) # cls 150 | mi.bias = torch.nn.Parameter(b.view(-1), requires_grad=True) 151 | 152 | def _print_biases(self): 153 | m = self.model[-1] # Detect() module 154 | for mi in m.m: # from 155 | b = mi.bias.detach().view(m.na, -1).T # conv.bias(255) to (3,85) 156 | print(('%6g Conv2d.bias:' + '%10.3g' * 6) % (mi.weight.shape[1], *b[:5].mean(1).tolist(), b[5:].mean())) 157 | 158 | # def _print_weights(self): 159 | # for m in self.model.modules(): 160 | # if type(m) is Bottleneck: 161 | # print('%10.3g' % (m.w.detach().sigmoid() * 2)) # shortcut weights 162 | 163 | def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers 164 | print('Fusing layers... ') 165 | for m in self.model.modules(): 166 | if type(m) is Conv and hasattr(m, 'bn'): 167 | m._non_persistent_buffers_set = set() # pytorch 1.6.0 compatability 168 | m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv 169 | delattr(m, 'bn') # remove batchnorm 170 | m.forward = m.fuseforward # update forward 171 | self.info() 172 | return self 173 | 174 | def add_nms(self): # fuse model Conv2d() + BatchNorm2d() layers 175 | if type(self.model[-1]) is not NMS: # if missing NMS 176 | print('Adding NMS module... ') 177 | m = NMS() # module 178 | m.f = -1 # from 179 | m.i = self.model[-1].i + 1 # index 180 | self.model.add_module(name='%s' % m.i, module=m) # add 181 | return self 182 | 183 | def info(self, verbose=False): # print model information 184 | model_info(self, verbose) 185 | 186 | 187 | def parse_model(d, ch): # model_dict, input_channels(3) 188 | logger.info('\n%3s%18s%3s%10s %-40s%-30s' % ('', 'from', 'n', 'params', 'module', 'arguments')) 189 | anchors, nc, gd, gw = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'] 190 | na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors 191 | no = na * (nc + 5 + 180) # number of outputs = anchors * (classes + 5) 192 | 193 | layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out 194 | for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args 195 | m = eval(m) if isinstance(m, str) else m # eval strings 196 | for j, a in enumerate(args): 197 | try: 198 | args[j] = eval(a) if isinstance(a, str) else a # eval strings 199 | except: 200 | pass 201 | 202 | n = max(round(n * gd), 1) if n > 1 else n # depth gain 203 | if m in [Conv, Bottleneck, SPP, DWConv, MixConv2d, Focus, CrossConv, BottleneckCSP, C3]: 204 | c1, c2 = ch[f], args[0] 205 | 206 | # Normal 207 | # if i > 0 and args[0] != no: # channel expansion factor 208 | # ex = 1.75 # exponential (default 2.0) 209 | # e = math.log(c2 / ch[1]) / math.log(2) 210 | # c2 = int(ch[1] * ex ** e) 211 | # if m != Focus: 212 | 213 | c2 = make_divisible(c2 * gw, 8) if c2 != no else c2 214 | 215 | # Experimental 216 | # if i > 0 and args[0] != no: # channel expansion factor 217 | # ex = 1 + gw # exponential (default 2.0) 218 | # ch1 = 32 # ch[1] 219 | # e = math.log(c2 / ch1) / math.log(2) # level 1-n 220 | # c2 = int(ch1 * ex ** e) 221 | # if m != Focus: 222 | # c2 = make_divisible(c2, 8) if c2 != no else c2 223 | 224 | args = [c1, c2, *args[1:]] 225 | if m in [BottleneckCSP, C3]: 226 | args.insert(2, n) 227 | n = 1 228 | elif m is nn.BatchNorm2d: 229 | args = [ch[f]] 230 | elif m is Concat: 231 | c2 = sum([ch[-1 if x == -1 else x + 1] for x in f]) 232 | elif m is Detect: 233 | args.append([ch[x + 1] for x in f]) 234 | if isinstance(args[1], int): # number of anchors 235 | args[1] = [list(range(args[1] * 2))] * len(f) 236 | else: 237 | c2 = ch[f] 238 | 239 | m_ = nn.Sequential(*[m(*args) for _ in range(n)]) if n > 1 else m(*args) # module 240 | t = str(m)[8:-2].replace('__main__.', '') # module type 241 | np = sum([x.numel() for x in m_.parameters()]) # number params 242 | m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params 243 | logger.info('%3s%18s%3s%10.0f %-40s%-30s' % (i, f, n, np, t, args)) # print 244 | save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist 245 | layers.append(m_) 246 | ch.append(c2) 247 | return nn.Sequential(*layers), sorted(save) 248 | 249 | 250 | if __name__ == '__main__': 251 | parser = argparse.ArgumentParser() 252 | parser.add_argument('--cfg', type=str, default='yolov5m.yaml', help='model.yaml') 253 | parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') 254 | opt = parser.parse_args() 255 | opt.cfg = check_file(opt.cfg) # check file 256 | set_logging() 257 | device = select_device(opt.device) 258 | img = torch.FloatTensor(torch.ones((1,3,640,640))).cuda() 259 | 260 | # Create model 261 | model = Model(opt.cfg).to(device) 262 | model.train() 263 | pre = model(img) 264 | # print(pre) 265 | 266 | # Profile 267 | # img = torch.rand(8 if torch.cuda.is_available() else 1, 3, 640, 640).to(device) 268 | # y = model(img, profile=True) 269 | 270 | # ONNX export 271 | # model.model[-1].export = True 272 | # torch.onnx.export(model, img, opt.cfg.replace('.yaml', '.onnx'), verbose=True, opset_version=11) 273 | 274 | # Tensorboard 275 | # from torch.utils.tensorboard import SummaryWriter 276 | # tb_writer = SummaryWriter() 277 | # print("Run 'tensorboard --logdir=models/runs' to view tensorboard at http://localhost:6006/") 278 | # tb_writer.add_graph(model.model, img) # add model to tensorboard 279 | # tb_writer.add_image('test', img[0], dataformats='CWH') # add model to tensorboard 280 | -------------------------------------------------------------------------------- /models/yolov5l.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 5 # number of classes 3 | depth_multiple: 1.0 # model depth multiple 4 | width_multiple: 1.0 # layer channel multiple 5 | 6 | # anchors 7 | anchors: 8 | - [24,9, 37,12, 52,15] # P3/8 9 | - [64,23, 81,19, 98,29] # P4/16 10 | - [137,27, 199,41, 342,65] # P5/32 11 | 12 | # YOLOv5 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 17 | [-1, 3, BottleneckCSP, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 3, BottleneckCSP, [1024, False]], # 9 25 | ] 26 | 27 | # YOLOv5 head 28 | head: 29 | [[-1, 1, Conv, [512, 1, 1]], 30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 32 | [-1, 3, BottleneckCSP, [512, False]], # 13 33 | 34 | [-1, 1, Conv, [256, 1, 1]], 35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 37 | [-1, 3, BottleneckCSP, [256, False]], # 17 (P3/8-small) 38 | 39 | [-1, 1, Conv, [256, 3, 2]], 40 | [[-1, 14], 1, Concat, [1]], # cat head P4 41 | [-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium) 42 | 43 | [-1, 1, Conv, [512, 3, 2]], 44 | [[-1, 10], 1, Concat, [1]], # cat head P5 45 | [-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large) 46 | 47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 48 | ] 49 | -------------------------------------------------------------------------------- /models/yolov5m.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 5 # number of classes 3 | depth_multiple: 0.67 # model depth multiple 4 | width_multiple: 0.75 # layer channel multiple 5 | 6 | # anchors 7 | anchors: 8 | - [24,9, 37,12, 52,15] # P3/8 9 | - [64,23, 81,19, 98,29] # P4/16 10 | - [137,27, 199,41, 342,65] # P5/32 11 | 12 | # YOLOv5 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 17 | [-1, 3, BottleneckCSP, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 3, BottleneckCSP, [1024, False]], # 9 25 | ] 26 | 27 | # YOLOv5 head 28 | head: 29 | [[-1, 1, Conv, [512, 1, 1]], 30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 32 | [-1, 3, BottleneckCSP, [512, False]], # 13 33 | 34 | [-1, 1, Conv, [256, 1, 1]], 35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 37 | [-1, 3, BottleneckCSP, [256, False]], # 17 (P3/8-small) 38 | 39 | [-1, 1, Conv, [256, 3, 2]], 40 | [[-1, 14], 1, Concat, [1]], # cat head P4 41 | [-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium) 42 | 43 | [-1, 1, Conv, [512, 3, 2]], 44 | [[-1, 10], 1, Concat, [1]], # cat head P5 45 | [-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large) 46 | 47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 48 | ] 49 | -------------------------------------------------------------------------------- /models/yolov5s.yaml: -------------------------------------------------------------------------------- 1 | # parameters 2 | nc: 5 # number of classes 3 | depth_multiple: 0.33 # model depth multiple 4 | width_multiple: 0.50 # layer channel multiple 5 | 6 | # anchors 7 | anchors: 8 | - [10,13, 16,30, 33,23] # P3/8 9 | - [30,61, 62,45, 59,119] # P4/16 10 | - [116,90, 156,198, 373,326] # P5/32 11 | 12 | # YOLOv5 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 17 | [-1, 3, BottleneckCSP, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 3, BottleneckCSP, [1024, False]], # 9 25 | ] 26 | 27 | # YOLOv5 head 28 | head: 29 | 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, BottleneckCSP, [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, BottleneckCSP, [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, BottleneckCSP, [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, BottleneckCSP, [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 | # parameters 2 | nc: 5 # number of classes 3 | depth_multiple: 1.33 # model depth multiple 4 | width_multiple: 1.25 # layer channel multiple 5 | 6 | # anchors 7 | anchors: 8 | - [24,9, 37,12, 52,15] # P3/8 9 | - [64,23, 81,19, 98,29] # P4/16 10 | - [137,27, 199,41, 342,65] # P5/32 11 | 12 | # YOLOv5 backbone 13 | backbone: 14 | # [from, number, module, args] 15 | [[-1, 1, Focus, [64, 3]], # 0-P1/2 16 | [-1, 1, Conv, [128, 3, 2]], # 1-P2/4 17 | [-1, 3, BottleneckCSP, [128]], 18 | [-1, 1, Conv, [256, 3, 2]], # 3-P3/8 19 | [-1, 9, BottleneckCSP, [256]], 20 | [-1, 1, Conv, [512, 3, 2]], # 5-P4/16 21 | [-1, 9, BottleneckCSP, [512]], 22 | [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32 23 | [-1, 1, SPP, [1024, [5, 9, 13]]], 24 | [-1, 3, BottleneckCSP, [1024, False]], # 9 25 | ] 26 | 27 | # YOLOv5 head 28 | head: 29 | [[-1, 1, Conv, [512, 1, 1]], 30 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 31 | [[-1, 6], 1, Concat, [1]], # cat backbone P4 32 | [-1, 3, BottleneckCSP, [512, False]], # 13 33 | 34 | [-1, 1, Conv, [256, 1, 1]], 35 | [-1, 1, nn.Upsample, [None, 2, 'nearest']], 36 | [[-1, 4], 1, Concat, [1]], # cat backbone P3 37 | [-1, 3, BottleneckCSP, [256, False]], # 17 (P3/8-small) 38 | 39 | [-1, 1, Conv, [256, 3, 2]], 40 | [[-1, 14], 1, Concat, [1]], # cat head P4 41 | [-1, 3, BottleneckCSP, [512, False]], # 20 (P4/16-medium) 42 | 43 | [-1, 1, Conv, [512, 3, 2]], 44 | [[-1, 10], 1, Concat, [1]], # cat head P5 45 | [-1, 3, BottleneckCSP, [1024, False]], # 23 (P5/32-large) 46 | 47 | [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5) 48 | ] 49 | -------------------------------------------------------------------------------- /rbox.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/rbox.png -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # pip install -r requirements.txt 2 | 3 | # base ---------------------------------------- 4 | Cython 5 | matplotlib>=3.2.2 6 | numpy>=1.18.5 7 | opencv-python>=4.1.2 8 | pillow 9 | PyYAML>=5.3 10 | scipy>=1.4.1 11 | tensorboard>=2.2 12 | tqdm>=4.41.0 13 | shapely 14 | # coco ---------------------------------------- 15 | # pycocotools>=2.0 16 | 17 | # export -------------------------------------- 18 | # packaging # for coremltools 19 | # coremltools==4.0 20 | # onnx>=1.7.0 21 | # scikit-learn==0.19.2 # for coreml quantization 22 | 23 | # extras -------------------------------------- 24 | # thop # FLOPS computation 25 | # seaborn # plotting 26 | -------------------------------------------------------------------------------- /result.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/result.png -------------------------------------------------------------------------------- /retanglelabel2mylabel.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | import numpy as np 4 | from shapely.geometry import Polygon, MultiPoint # 多边形 5 | import time 6 | import cv2 7 | import argparse 8 | 9 | from time import sleep 10 | def trans(file, line_, wh_list): 11 | # file = '1001.txt' 12 | path = label_path + '/' + file 13 | 14 | # line = '' + img_path + '/' + os.path.splitext(file)[0] + '.tif' 15 | line = '' 16 | # print(line) 17 | # print(path) 18 | f = open(path) 19 | label = f.read().split() 20 | # print(label) 21 | clss = [] 22 | xsets = [] 23 | ysets = [] 24 | sets = [] 25 | 26 | 27 | for i in range(0, len(label), 9): 28 | cls = float(label[i]) - 1 29 | if cls not in clss: 30 | clss.append(cls) 31 | data = np.array(label[i+1:i+9]).astype(int) 32 | data = data.reshape(4, 2) 33 | 34 | rect = cv2.minAreaRect(data) # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度) 35 | # print(rect) 36 | box = cv2.boxPoints(rect).astype(int) 37 | 38 | c_x = rect[0][0] 39 | c_y = rect[0][1] 40 | w = rect[1][0] 41 | h = rect[1][1] 42 | theta = rect[-1] 43 | 44 | 45 | if (theta < -90 or theta > 0) and h < w: 46 | print(w,h) 47 | print(file) 48 | print(theta) 49 | sleep(11111) 50 | 51 | if theta == 0 and w < h: 52 | theta = -90 53 | t = h 54 | h = w 55 | w = t 56 | 57 | if w > h: 58 | t = h 59 | h = w 60 | w = t 61 | 62 | 63 | else: 64 | if theta == 0: 65 | print('dfasd') 66 | theta = 0 67 | else: 68 | theta = 90 + theta 69 | 70 | if w > h : 71 | sleep(1111) 72 | 73 | 74 | # print(c_x, c_y, w, h, theta) 75 | # line = line + ' ' + str(c_x/1024) + ',' + str(c_y/1024) + ',' + str(h / 1024) + ',' + str(w / 1024) + ',' + str(int(theta)) + ',' + str(cls) + ' ' 76 | # line = line + ' ' + str(c_x - h / 2) + ',' + str(c_y - w / 2) + ',' + str(c_x + h / 2) + ',' + str(c_y + w / 2) + ',' + str(cls) + ',' + str(int(theta)+90) + ' ' 77 | line = line + str(cls) + ' ' + str(c_x / 1024) + ' ' + str(c_y / 1024) + ' ' + str(h / 1024) + ' ' + str(w / 1024) + ' ' + str(int(theta)+90) + '\n' 78 | # line = line + str(cls) + ' ' + str(c_x / 1024) + ' ' + str(c_y / 1024) + ' ' + str(h / 1024) + ' ' + str( 79 | # w / 1024) + ' ' + str(int(theta) + 90) + '\n' 80 | wh_list.append([h/1024, w/1024]) 81 | with open(opt.output_path+'/{}'.format(str(int(os.path.splitext(file)[0])+2008) + '.txt'), 82 | 'w+') as f: 83 | 84 | f.write(line) 85 | f.close() 86 | # line_ = line_ + line + '\n' 87 | 88 | # # print(data[:,0].shape) 89 | # # poly = Polygon(data).convex_hull 90 | # d_index = np.argmax(data[:, 0]) 91 | # c_index = np.argmax(data[:, 1]) 92 | # c_x = (max(data[:, 0]) + min(data[:, 0])) / 2 93 | # c_y = (max(data[:, 1]) + min(data[:, 1])) / 2 94 | # print(data[d_index],data[c_index]) 95 | # # print('len:',len(set(data[:,0]))) 96 | # if len(set(data[:, 0])) not in xsets: 97 | # xsets.append(len(set(data[:, 0]))) 98 | # if len(set(data[:, 1])) not in ysets: 99 | # ysets.append(len(set(data[:, 1]))) 100 | # if (len(set(data[:, 1]))*len(set(data[:, 0]))) not in sets: 101 | # sets.append((len(set(data[:, 1]))*len(set(data[:, 0])))) 102 | # if len(set(data[:,0])) < 4 or len(set(data[:,1])) < 4: 103 | # 104 | # if len(set(data[:,0])) == 2 and len(set(data[:,1])) == 2: 105 | # 106 | # print('正规矩形:') 107 | # theta = - np.pi / 2 108 | # right = np.where(data[:, 0]==max(data[:, 0])) 109 | # top = np.where(data[:, 1]==max(data[:, 1])) 110 | # # print(top[0], right[0]) 111 | # # h = np.abs(data[top[0][0]][0] - data[top[0][1]][0]) 112 | # # w = np.abs(data[right[0][0]][1] - data[right[0][1]][1]) 113 | # # 114 | # # print(w , h) 115 | # # if len(set(data[:,0])) == 3 or len(set(data[:,1])) == 3: 116 | # 117 | # 118 | # 119 | # else: 120 | # # print(1) 121 | # theta = - np.arctan((data[c_index][1] - data[d_index][1]) / (data[d_index][0] - data[c_index][0])) 122 | # 123 | # w = np.sqrt((data[c_index][1] - data[d_index][1])**2 + (data[d_index][0] - data[c_index][0])**2) 124 | # h = np.sqrt((data[d_index][0] - data[np.argmin(data[:, 1])][0])**2 +(data[d_index][1] - data[np.argmin(data[:, 1])][1])**2) 125 | # # print(theta) 126 | # 127 | # # print(c_x, c_y, w, h, theta) 128 | 129 | return path, rect, line_, int(theta) + 90, wh_list 130 | 131 | 132 | 133 | if __name__ == '__main__': 134 | parser = argparse.ArgumentParser() 135 | parser.add_argument('--label_path', type=str, 136 | help='label path') 137 | parser.add_argument('--img_path', type=str, 138 | help='images path') 139 | parser.add_argument('--output_path', type=str, default=r'convertor\fold0\labels_output/', 140 | help='label output path') 141 | opt = parser.parse_args() 142 | label_path = opt.label_path 143 | img_path = opt.img_path 144 | all_label = [] 145 | cls = [] 146 | xsets = [] 147 | ysets = [] 148 | sets = [] 149 | line_ = '' 150 | thetas = [] 151 | wh_list = [] 152 | for file in os.listdir(label_path): 153 | path, ret, line_, theta, wh_list = trans(file, line_, wh_list) 154 | if theta not in thetas: 155 | thetas.append(theta) 156 | print(len(wh_list)) 157 | print(wh_list) 158 | # print(len(thetas), max(thetas),min(thetas)) 159 | # with open(r'D:\hjj\yolo4/2007_train_ship_angle_1.txt', 'w+') as f: 160 | # 161 | # f.write(line_) 162 | # f.close() 163 | -------------------------------------------------------------------------------- /sotabench.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import glob 3 | import json 4 | import os 5 | import shutil 6 | from pathlib import Path 7 | 8 | import numpy as np 9 | import torch 10 | import yaml 11 | from tqdm import tqdm 12 | 13 | from models.experimental import attempt_load 14 | from utils.datasets import create_dataloader 15 | from utils.general import ( 16 | coco80_to_coco91_class, check_dataset, check_file, check_img_size, compute_loss, non_max_suppression, scale_coords, 17 | xyxy2xywh, clip_coords, plot_images, xywh2xyxy, box_iou, output_to_target, ap_per_class, set_logging) 18 | from utils.torch_utils import select_device, time_synchronized 19 | 20 | 21 | from sotabencheval.object_detection import COCOEvaluator 22 | from sotabencheval.utils import is_server 23 | 24 | DATA_ROOT = './.data/vision/coco' if is_server() else '../coco' # sotabench data dir 25 | 26 | 27 | def test(data, 28 | weights=None, 29 | batch_size=16, 30 | imgsz=640, 31 | conf_thres=0.001, 32 | iou_thres=0.6, # for NMS 33 | save_json=False, 34 | single_cls=False, 35 | augment=False, 36 | verbose=False, 37 | model=None, 38 | dataloader=None, 39 | save_dir='', 40 | merge=False, 41 | save_txt=False): 42 | # Initialize/load model and set device 43 | training = model is not None 44 | if training: # called by train.py 45 | device = next(model.parameters()).device # get model device 46 | 47 | else: # called directly 48 | set_logging() 49 | device = select_device(opt.device, batch_size=batch_size) 50 | merge, save_txt = opt.merge, opt.save_txt # use Merge NMS, save *.txt labels 51 | if save_txt: 52 | out = Path('inference/output') 53 | if os.path.exists(out): 54 | shutil.rmtree(out) # delete output folder 55 | os.makedirs(out) # make new output folder 56 | 57 | # Remove previous 58 | for f in glob.glob(str(Path(save_dir) / 'test_batch*.jpg')): 59 | os.remove(f) 60 | 61 | # Load model 62 | model = attempt_load(weights, map_location=device) # load FP32 model 63 | imgsz = check_img_size(imgsz, s=model.stride.max()) # check img_size 64 | 65 | # Multi-GPU disabled, incompatible with .half() https://github.com/ultralytics/yolov5/issues/99 66 | # if device.type != 'cpu' and torch.cuda.device_count() > 1: 67 | # model = nn.DataParallel(model) 68 | 69 | # Half 70 | half = device.type != 'cpu' # half precision only supported on CUDA 71 | if half: 72 | model.half() 73 | 74 | # Configure 75 | model.eval() 76 | with open(data) as f: 77 | data = yaml.load(f, Loader=yaml.FullLoader) # model dict 78 | check_dataset(data) # check 79 | nc = 1 if single_cls else int(data['nc']) # number of classes 80 | iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for mAP@0.5:0.95 81 | niou = iouv.numel() 82 | 83 | # Dataloader 84 | if not training: 85 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 86 | _ = model(img.half() if half else img) if device.type != 'cpu' else None # run once 87 | path = data['test'] if opt.task == 'test' else data['val'] # path to val/test images 88 | dataloader = create_dataloader(path, imgsz, batch_size, model.stride.max(), opt, 89 | hyp=None, augment=False, cache=True, pad=0.5, rect=True)[0] 90 | 91 | seen = 0 92 | names = model.names if hasattr(model, 'names') else model.module.names 93 | coco91class = coco80_to_coco91_class() 94 | s = ('%20s' + '%12s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', 'mAP@.5', 'mAP@.5:.95') 95 | p, r, f1, mp, mr, map50, map, t0, t1 = 0., 0., 0., 0., 0., 0., 0., 0., 0. 96 | loss = torch.zeros(3, device=device) 97 | jdict, stats, ap, ap_class = [], [], [], [] 98 | evaluator = COCOEvaluator(root=DATA_ROOT, model_name=opt.weights.replace('.pt', '')) 99 | for batch_i, (img, targets, paths, shapes) in enumerate(tqdm(dataloader, desc=s)): 100 | img = img.to(device, non_blocking=True) 101 | img = img.half() if half else img.float() # uint8 to fp16/32 102 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 103 | targets = targets.to(device) 104 | nb, _, height, width = img.shape # batch size, channels, height, width 105 | whwh = torch.Tensor([width, height, width, height]).to(device) 106 | 107 | # Disable gradients 108 | with torch.no_grad(): 109 | # Run model 110 | t = time_synchronized() 111 | inf_out, train_out = model(img, augment=augment) # inference and training outputs 112 | t0 += time_synchronized() - t 113 | 114 | # Compute loss 115 | if training: # if model has loss hyperparameters 116 | loss += compute_loss([x.float() for x in train_out], targets, model)[1][:3] # box, obj, cls 117 | 118 | # Run NMS 119 | t = time_synchronized() 120 | output = non_max_suppression(inf_out, conf_thres=conf_thres, iou_thres=iou_thres, merge=merge) 121 | t1 += time_synchronized() - t 122 | 123 | # Statistics per image 124 | for si, pred in enumerate(output): 125 | labels = targets[targets[:, 0] == si, 1:] 126 | nl = len(labels) 127 | tcls = labels[:, 0].tolist() if nl else [] # target class 128 | seen += 1 129 | 130 | if pred is None: 131 | if nl: 132 | stats.append((torch.zeros(0, niou, dtype=torch.bool), torch.Tensor(), torch.Tensor(), tcls)) 133 | continue 134 | 135 | # Append to text file 136 | if save_txt: 137 | gn = torch.tensor(shapes[si][0])[[1, 0, 1, 0]] # normalization gain whwh 138 | x = pred.clone() 139 | x[:, :4] = scale_coords(img[si].shape[1:], x[:, :4], shapes[si][0], shapes[si][1]) # to original 140 | for *xyxy, conf, cls in x: 141 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 142 | with open(str(out / Path(paths[si]).stem) + '.txt', 'a') as f: 143 | f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 144 | 145 | # Clip boxes to image bounds 146 | clip_coords(pred, (height, width)) 147 | 148 | # Append to pycocotools JSON dictionary 149 | if save_json: 150 | # [{"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}, ... 151 | image_id = Path(paths[si]).stem 152 | box = pred[:, :4].clone() # xyxy 153 | scale_coords(img[si].shape[1:], box, shapes[si][0], shapes[si][1]) # to original shape 154 | box = xyxy2xywh(box) # xywh 155 | box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner 156 | for p, b in zip(pred.tolist(), box.tolist()): 157 | result = {'image_id': int(image_id) if image_id.isnumeric() else image_id, 158 | 'category_id': coco91class[int(p[5])], 159 | 'bbox': [round(x, 3) for x in b], 160 | 'score': round(p[4], 5)} 161 | jdict.append(result) 162 | 163 | #evaluator.add([result]) 164 | #if evaluator.cache_exists: 165 | # break 166 | 167 | # # Assign all predictions as incorrect 168 | # correct = torch.zeros(pred.shape[0], niou, dtype=torch.bool, device=device) 169 | # if nl: 170 | # detected = [] # target indices 171 | # tcls_tensor = labels[:, 0] 172 | # 173 | # # target boxes 174 | # tbox = xywh2xyxy(labels[:, 1:5]) * whwh 175 | # 176 | # # Per target class 177 | # for cls in torch.unique(tcls_tensor): 178 | # ti = (cls == tcls_tensor).nonzero(as_tuple=False).view(-1) # prediction indices 179 | # pi = (cls == pred[:, 5]).nonzero(as_tuple=False).view(-1) # target indices 180 | # 181 | # # Search for detections 182 | # if pi.shape[0]: 183 | # # Prediction to target ious 184 | # ious, i = box_iou(pred[pi, :4], tbox[ti]).max(1) # best ious, indices 185 | # 186 | # # Append detections 187 | # detected_set = set() 188 | # for j in (ious > iouv[0]).nonzero(as_tuple=False): 189 | # d = ti[i[j]] # detected target 190 | # if d.item() not in detected_set: 191 | # detected_set.add(d.item()) 192 | # detected.append(d) 193 | # correct[pi[j]] = ious[j] > iouv # iou_thres is 1xn 194 | # if len(detected) == nl: # all targets already located in image 195 | # break 196 | # 197 | # # Append statistics (correct, conf, pcls, tcls) 198 | # stats.append((correct.cpu(), pred[:, 4].cpu(), pred[:, 5].cpu(), tcls)) 199 | 200 | # # Plot images 201 | # if batch_i < 1: 202 | # f = Path(save_dir) / ('test_batch%g_gt.jpg' % batch_i) # filename 203 | # plot_images(img, targets, paths, str(f), names) # ground truth 204 | # f = Path(save_dir) / ('test_batch%g_pred.jpg' % batch_i) 205 | # plot_images(img, output_to_target(output, width, height), paths, str(f), names) # predictions 206 | 207 | evaluator.add(jdict) 208 | evaluator.save() 209 | 210 | # # Compute statistics 211 | # stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy 212 | # if len(stats) and stats[0].any(): 213 | # p, r, ap, f1, ap_class = ap_per_class(*stats) 214 | # p, r, ap50, ap = p[:, 0], r[:, 0], ap[:, 0], ap.mean(1) # [P, R, AP@0.5, AP@0.5:0.95] 215 | # mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean() 216 | # nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class 217 | # else: 218 | # nt = torch.zeros(1) 219 | # 220 | # # Print results 221 | # pf = '%20s' + '%12.3g' * 6 # print format 222 | # print(pf % ('all', seen, nt.sum(), mp, mr, map50, map)) 223 | # 224 | # # Print results per class 225 | # if verbose and nc > 1 and len(stats): 226 | # for i, c in enumerate(ap_class): 227 | # print(pf % (names[c], seen, nt[c], p[i], r[i], ap50[i], ap[i])) 228 | # 229 | # # Print speeds 230 | # t = tuple(x / seen * 1E3 for x in (t0, t1, t0 + t1)) + (imgsz, imgsz, batch_size) # tuple 231 | # if not training: 232 | # print('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t) 233 | # 234 | # # Save JSON 235 | # if save_json and len(jdict): 236 | # f = 'detections_val2017_%s_results.json' % \ 237 | # (weights.split(os.sep)[-1].replace('.pt', '') if isinstance(weights, str) else '') # filename 238 | # print('\nCOCO mAP with pycocotools... saving %s...' % f) 239 | # with open(f, 'w') as file: 240 | # json.dump(jdict, file) 241 | # 242 | # try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb 243 | # from pycocotools.coco import COCO 244 | # from pycocotools.cocoeval import COCOeval 245 | # 246 | # imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files] 247 | # cocoGt = COCO(glob.glob('../coco/annotations/instances_val*.json')[0]) # initialize COCO ground truth api 248 | # cocoDt = cocoGt.loadRes(f) # initialize COCO pred api 249 | # cocoEval = COCOeval(cocoGt, cocoDt, 'bbox') 250 | # cocoEval.params.imgIds = imgIds # image IDs to evaluate 251 | # cocoEval.evaluate() 252 | # cocoEval.accumulate() 253 | # cocoEval.summarize() 254 | # map, map50 = cocoEval.stats[:2] # update results (mAP@0.5:0.95, mAP@0.5) 255 | # except Exception as e: 256 | # print('ERROR: pycocotools unable to run: %s' % e) 257 | # 258 | # # Return results 259 | # model.float() # for training 260 | # maps = np.zeros(nc) + map 261 | # for i, c in enumerate(ap_class): 262 | # maps[c] = ap[i] 263 | # return (mp, mr, map50, map, *(loss.cpu() / len(dataloader)).tolist()), maps, t 264 | 265 | 266 | if __name__ == '__main__': 267 | parser = argparse.ArgumentParser(prog='test.py') 268 | parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)') 269 | parser.add_argument('--data', type=str, default='data/coco.yaml', help='*.data path') 270 | parser.add_argument('--batch-size', type=int, default=32, help='size of each image batch') 271 | parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)') 272 | parser.add_argument('--conf-thres', type=float, default=0.001, help='object confidence threshold') 273 | parser.add_argument('--iou-thres', type=float, default=0.65, help='IOU threshold for NMS') 274 | parser.add_argument('--save-json', action='store_true', help='save a cocoapi-compatible JSON results file') 275 | parser.add_argument('--task', default='val', help="'val', 'test', 'study'") 276 | parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') 277 | parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset') 278 | parser.add_argument('--augment', action='store_true', help='augmented inference') 279 | parser.add_argument('--merge', action='store_true', help='use Merge NMS') 280 | parser.add_argument('--verbose', action='store_true', help='report mAP by class') 281 | parser.add_argument('--save-txt', action='store_true', help='save results to *.txt') 282 | opt = parser.parse_args() 283 | opt.save_json |= opt.data.endswith('coco.yaml') 284 | opt.data = check_file(opt.data) # check file 285 | print(opt) 286 | 287 | if opt.task in ['val', 'test']: # run normally 288 | test(opt.data, 289 | opt.weights, 290 | opt.batch_size, 291 | opt.img_size, 292 | opt.conf_thres, 293 | opt.iou_thres, 294 | opt.save_json, 295 | opt.single_cls, 296 | opt.augment, 297 | opt.verbose) 298 | 299 | elif opt.task == 'study': # run over a range of settings and save/plot 300 | for weights in ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt']: 301 | f = 'study_%s_%s.txt' % (Path(opt.data).stem, Path(weights).stem) # filename to save to 302 | x = list(range(320, 800, 64)) # x axis 303 | y = [] # y axis 304 | for i in x: # img-size 305 | print('\nRunning %s point %s...' % (f, i)) 306 | r, _, t = test(opt.data, weights, opt.batch_size, i, opt.conf_thres, opt.iou_thres, opt.save_json) 307 | y.append(r + t) # results and times 308 | np.savetxt(f, y, fmt='%10.4g') # save 309 | os.system('zip -r study.zip study_*.txt') 310 | # utils.general.plot_study_txt(f, x) # plot -------------------------------------------------------------------------------- /test.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import glob 3 | import json 4 | import os 5 | import shutil 6 | from pathlib import Path 7 | 8 | import numpy as np 9 | import torch 10 | import yaml 11 | from tqdm import tqdm 12 | 13 | from models.experimental import attempt_load 14 | from utils.datasets import create_dataloader 15 | from utils.general import ( 16 | coco80_to_coco91_class, check_dataset, check_file, check_img_size, compute_loss, non_max_suppression, scale_coords, 17 | xyxy2xywh, clip_coords, plot_images, xywh2xyxy, box_iou, output_to_target, ap_per_class, set_logging) 18 | from utils.torch_utils import select_device, time_synchronized 19 | 20 | 21 | def test(data, 22 | weights=None, 23 | batch_size=16, 24 | imgsz=640, 25 | conf_thres=0.001, 26 | iou_thres=0.6, # for NMS 27 | save_json=False, 28 | single_cls=False, 29 | augment=False, 30 | verbose=False, 31 | model=None, 32 | dataloader=None, 33 | save_dir=Path(''), # for saving images 34 | save_txt=False, # for auto-labelling 35 | plots=True): 36 | # Initialize/load model and set device 37 | training = model is not None 38 | if training: # called by train.py 39 | device = next(model.parameters()).device # get model device 40 | 41 | else: # called directly 42 | set_logging() 43 | device = select_device(opt.device, batch_size=batch_size) 44 | save_txt = opt.save_txt # save *.txt labels 45 | if save_txt: 46 | out = Path('inference/output') 47 | if os.path.exists(out): 48 | shutil.rmtree(out) # delete output folder 49 | os.makedirs(out) # make new output folder 50 | 51 | # Remove previous 52 | for f in glob.glob(str(save_dir / 'test_batch*.jpg')): 53 | os.remove(f) 54 | 55 | # Load model 56 | model = attempt_load(weights, map_location=device) # load FP32 model 57 | imgsz = check_img_size(imgsz, s=model.stride.max()) # check img_size 58 | 59 | # Multi-GPU disabled, incompatible with .half() https://github.com/ultralytics/yolov5/issues/99 60 | # if device.type != 'cpu' and torch.cuda.device_count() > 1: 61 | # model = nn.DataParallel(model) 62 | 63 | # Half 64 | half = device.type != 'cpu' # half precision only supported on CUDA 65 | if half: 66 | model.half() 67 | 68 | # Configure 69 | model.eval() 70 | with open(data) as f: 71 | data = yaml.load(f, Loader=yaml.FullLoader) # model dict 72 | check_dataset(data) # check 73 | nc = 1 if single_cls else int(data['nc']) # number of classes 74 | iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for mAP@0.5:0.95 75 | niou = iouv.numel() 76 | 77 | # Dataloader 78 | if not training: 79 | img = torch.zeros((1, 3, imgsz, imgsz), device=device) # init img 80 | _ = model(img.half() if half else img) if device.type != 'cpu' else None # run once 81 | path = data['test'] if opt.task == 'test' else data['val'] # path to val/test images 82 | dataloader = create_dataloader(path, imgsz, batch_size, model.stride.max(), opt, 83 | hyp=None, augment=False, cache=False, pad=0.5, rect=True)[0] 84 | 85 | seen = 0 86 | names = model.names if hasattr(model, 'names') else model.module.names 87 | coco91class = coco80_to_coco91_class() 88 | s = ('%20s' + '%12s' * 6) % ('Class', 'Images', 'Targets', 'P', 'R', 'mAP@.5', 'mAP@.5:.95') 89 | p, r, f1, mp, mr, map50, map, t0, t1 = 0., 0., 0., 0., 0., 0., 0., 0., 0. 90 | loss = torch.zeros(3, device=device) 91 | jdict, stats, ap, ap_class = [], [], [], [] 92 | for batch_i, (img, targets, paths, shapes) in enumerate(tqdm(dataloader, desc=s)): 93 | img = img.to(device, non_blocking=True) 94 | img = img.half() if half else img.float() # uint8 to fp16/32 95 | img /= 255.0 # 0 - 255 to 0.0 - 1.0 96 | targets = targets.to(device) 97 | nb, _, height, width = img.shape # batch size, channels, height, width 98 | whwh = torch.Tensor([width, height, width, height]).to(device) 99 | 100 | # Disable gradients 101 | with torch.no_grad(): 102 | # Run model 103 | t = time_synchronized() 104 | inf_out, train_out = model(img, augment=augment) # inference and training outputs 105 | t0 += time_synchronized() - t 106 | 107 | # Compute loss 108 | if training: # if model has loss hyperparameters 109 | loss += compute_loss([x.float() for x in train_out], targets, model)[1][:3] # box, obj, cls 110 | 111 | # Run NMS 112 | t = time_synchronized() 113 | output = non_max_suppression(inf_out, conf_thres=conf_thres, iou_thres=iou_thres) 114 | t1 += time_synchronized() - t 115 | 116 | # Statistics per image 117 | for si, pred in enumerate(output): 118 | labels = targets[targets[:, 0] == si, 1:] 119 | nl = len(labels) 120 | tcls = labels[:, 0].tolist() if nl else [] # target class 121 | seen += 1 122 | 123 | if pred is None: 124 | if nl: 125 | stats.append((torch.zeros(0, niou, dtype=torch.bool), torch.Tensor(), torch.Tensor(), tcls)) 126 | continue 127 | 128 | # Append to text file 129 | if save_txt: 130 | gn = torch.tensor(shapes[si][0])[[1, 0, 1, 0]] # normalization gain whwh 131 | x = pred.clone() 132 | x[:, :4] = scale_coords(img[si].shape[1:], x[:, :4], shapes[si][0], shapes[si][1]) # to original 133 | for *xyxy, conf, cls in x: 134 | xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh 135 | with open(str(out / Path(paths[si]).stem) + '.txt', 'a') as f: 136 | f.write(('%g ' * 5 + '\n') % (cls, *xywh)) # label format 137 | 138 | # Clip boxes to image bounds 139 | clip_coords(pred, (height, width)) 140 | 141 | # Append to pycocotools JSON dictionary 142 | if save_json: 143 | # [{"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}, ... 144 | image_id = Path(paths[si]).stem 145 | box = pred[:, :4].clone() # xyxy 146 | scale_coords(img[si].shape[1:], box, shapes[si][0], shapes[si][1]) # to original shape 147 | box = xyxy2xywh(box) # xywh 148 | box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner 149 | for p, b in zip(pred.tolist(), box.tolist()): 150 | jdict.append({'image_id': int(image_id) if image_id.isnumeric() else image_id, 151 | 'category_id': coco91class[int(p[5])], 152 | 'bbox': [round(x, 3) for x in b], 153 | 'score': round(p[4], 5)}) 154 | 155 | # Assign all predictions as incorrect 156 | correct = torch.zeros(pred.shape[0], niou, dtype=torch.bool, device=device) 157 | if nl: 158 | detected = [] # target indices 159 | tcls_tensor = labels[:, 0] 160 | 161 | # target boxes 162 | tbox = xywh2xyxy(labels[:, 1:5]) * whwh 163 | 164 | # Per target class 165 | for cls in torch.unique(tcls_tensor): 166 | ti = (cls == tcls_tensor).nonzero(as_tuple=False).view(-1) # prediction indices 167 | pi = (cls == pred[:, 5]).nonzero(as_tuple=False).view(-1) # target indices 168 | 169 | # Search for detections 170 | if pi.shape[0]: 171 | # Prediction to target ious 172 | ious, i = box_iou(pred[pi, :4], tbox[ti]).max(1) # best ious, indices 173 | 174 | # Append detections 175 | detected_set = set() 176 | for j in (ious > iouv[0]).nonzero(as_tuple=False): 177 | d = ti[i[j]] # detected target 178 | if d.item() not in detected_set: 179 | detected_set.add(d.item()) 180 | detected.append(d) 181 | correct[pi[j]] = ious[j] > iouv # iou_thres is 1xn 182 | if len(detected) == nl: # all targets already located in image 183 | break 184 | 185 | # Append statistics (correct, conf, pcls, tcls) 186 | stats.append((correct.cpu(), pred[:, 4].cpu(), pred[:, 5].cpu(), tcls)) 187 | 188 | # Plot images 189 | # if plots and batch_i < 1: 190 | # f = save_dir / ('test_batch%g_gt.jpg' % batch_i) # filename 191 | # plot_images(img, targets, paths, str(f), names) # ground truth 192 | # f = save_dir / ('test_batch%g_pred.jpg' % batch_i) 193 | # plot_images(img, output_to_target(output, width, height), paths, str(f), names) # predictions 194 | 195 | # Compute statistics 196 | stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy 197 | if len(stats) and stats[0].any(): 198 | p, r, ap, f1, ap_class = ap_per_class(*stats, plot=plots, fname=save_dir / 'precision-recall_curve.png') 199 | p, r, ap50, ap = p[:, 0], r[:, 0], ap[:, 0], ap.mean(1) # [P, R, AP@0.5, AP@0.5:0.95] 200 | mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean() 201 | nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class 202 | else: 203 | nt = torch.zeros(1) 204 | 205 | # Print results 206 | pf = '%20s' + '%12.3g' * 6 # print format 207 | print(pf % ('all', seen, nt.sum(), mp, mr, map50, map)) 208 | 209 | # Print results per class 210 | if verbose and nc > 1 and len(stats): 211 | for i, c in enumerate(ap_class): 212 | print(pf % (names[c], seen, nt[c], p[i], r[i], ap50[i], ap[i])) 213 | 214 | # Print speeds 215 | t = tuple(x / seen * 1E3 for x in (t0, t1, t0 + t1)) + (imgsz, imgsz, batch_size) # tuple 216 | if not training: 217 | print('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t) 218 | 219 | # Save JSON 220 | if save_json and len(jdict): 221 | f = 'detections_val2017_%s_results.json' % \ 222 | (weights.split(os.sep)[-1].replace('.pt', '') if isinstance(weights, str) else '') # filename 223 | print('\nCOCO mAP with pycocotools... saving %s...' % f) 224 | with open(f, 'w') as file: 225 | json.dump(jdict, file) 226 | 227 | try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb 228 | from pycocotools.coco import COCO 229 | from pycocotools.cocoeval import COCOeval 230 | 231 | imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files] 232 | cocoGt = COCO(glob.glob('../coco/annotations/instances_val*.json')[0]) # initialize COCO ground truth api 233 | cocoDt = cocoGt.loadRes(f) # initialize COCO pred api 234 | cocoEval = COCOeval(cocoGt, cocoDt, 'bbox') 235 | cocoEval.params.imgIds = imgIds # image IDs to evaluate 236 | cocoEval.evaluate() 237 | cocoEval.accumulate() 238 | cocoEval.summarize() 239 | map, map50 = cocoEval.stats[:2] # update results (mAP@0.5:0.95, mAP@0.5) 240 | except Exception as e: 241 | print('ERROR: pycocotools unable to run: %s' % e) 242 | 243 | # Return results 244 | model.float() # for training 245 | maps = np.zeros(nc) + map 246 | for i, c in enumerate(ap_class): 247 | maps[c] = ap[i] 248 | return (mp, mr, map50, map, *(loss.cpu() / len(dataloader)).tolist()), maps, t 249 | 250 | 251 | if __name__ == '__main__': 252 | parser = argparse.ArgumentParser(prog='test.py') 253 | parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)') 254 | parser.add_argument('--data', type=str, default='data/coco128.yaml', help='*.data path') 255 | parser.add_argument('--batch-size', type=int, default=32, help='size of each image batch') 256 | parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)') 257 | parser.add_argument('--conf-thres', type=float, default=0.001, help='object confidence threshold') 258 | parser.add_argument('--iou-thres', type=float, default=0.65, help='IOU threshold for NMS') 259 | parser.add_argument('--save-json', action='store_true', help='save a cocoapi-compatible JSON results file') 260 | parser.add_argument('--task', default='val', help="'val', 'test', 'study'") 261 | parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') 262 | parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset') 263 | parser.add_argument('--augment', action='store_true', help='augmented inference') 264 | parser.add_argument('--verbose', action='store_true', help='report mAP by class') 265 | parser.add_argument('--save-txt', action='store_true', help='save results to *.txt') 266 | opt = parser.parse_args() 267 | opt.save_json |= opt.data.endswith('coco.yaml') 268 | opt.data = check_file(opt.data) # check file 269 | print(opt) 270 | 271 | if opt.task in ['val', 'test']: # run normally 272 | test(opt.data, 273 | opt.weights, 274 | opt.batch_size, 275 | opt.img_size, 276 | opt.conf_thres, 277 | opt.iou_thres, 278 | opt.save_json, 279 | opt.single_cls, 280 | opt.augment, 281 | opt.verbose) 282 | 283 | elif opt.task == 'study': # run over a range of settings and save/plot 284 | for weights in ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt']: 285 | f = 'study_%s_%s.txt' % (Path(opt.data).stem, Path(weights).stem) # filename to save to 286 | x = list(range(320, 800, 64)) # x axis 287 | y = [] # y axis 288 | for i in x: # img-size 289 | print('\nRunning %s point %s...' % (f, i)) 290 | r, _, t = test(opt.data, weights, opt.batch_size, i, opt.conf_thres, opt.iou_thres, opt.save_json) 291 | y.append(r + t) # results and times 292 | np.savetxt(f, y, fmt='%10.4g') # save 293 | os.system('zip -r study.zip study_*.txt') 294 | # utils.general.plot_study_txt(f, x) # plot 295 | -------------------------------------------------------------------------------- /test2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/test2.jpg -------------------------------------------------------------------------------- /train.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import logging 3 | import math 4 | import os 5 | import random 6 | import shutil 7 | import time 8 | from pathlib import Path 9 | 10 | import numpy as np 11 | import torch.distributed as dist 12 | import torch.nn.functional as F 13 | import torch.optim as optim 14 | import torch.optim.lr_scheduler as lr_scheduler 15 | import torch.utils.data 16 | import yaml 17 | from torch.cuda import amp 18 | from torch.nn.parallel import DistributedDataParallel as DDP 19 | from torch.utils.tensorboard import SummaryWriter 20 | from tqdm import tqdm 21 | 22 | import test # import test.py to get mAP after each epoch 23 | from models.yolo import Model 24 | from utils.datasets import create_dataloader 25 | from utils.general import ( 26 | torch_distributed_zero_first, labels_to_class_weights, plot_labels, check_anchors, labels_to_image_weights, 27 | compute_loss, plot_images, fitness, strip_optimizer, plot_results, get_latest_run, check_dataset, check_file, 28 | check_git_status, check_img_size, increment_dir, print_mutation, plot_evolution, set_logging, init_seeds) 29 | from utils.google_utils import attempt_download 30 | from utils.torch_utils import ModelEMA, select_device, intersect_dicts 31 | 32 | logger = logging.getLogger(__name__) 33 | 34 | 35 | def train(hyp, opt, device, tb_writer=None): 36 | logger.info(f'Hyperparameters {hyp}') 37 | log_dir = Path(tb_writer.log_dir) if tb_writer else Path(opt.logdir) / 'evolve' # logging directory 38 | wdir = log_dir / 'weights' # weights directory 39 | os.makedirs(wdir, exist_ok=True) 40 | last = wdir / 'last.pt' 41 | best = wdir / 'best.pt' 42 | results_file = str(log_dir / 'results.txt') 43 | epochs, batch_size, total_batch_size, weights, rank = \ 44 | opt.epochs, opt.batch_size, opt.total_batch_size, opt.weights, opt.global_rank 45 | 46 | # Save run settings 47 | with open(log_dir / 'hyp.yaml', 'w') as f: 48 | yaml.dump(hyp, f, sort_keys=False) 49 | with open(log_dir / 'opt.yaml', 'w') as f: 50 | yaml.dump(vars(opt), f, sort_keys=False) 51 | 52 | # Configure 53 | cuda = device.type != 'cpu' 54 | init_seeds(2 + rank) 55 | with open(opt.data) as f: 56 | data_dict = yaml.load(f, Loader=yaml.FullLoader) # data dict 57 | with torch_distributed_zero_first(rank): 58 | check_dataset(data_dict) # check 59 | train_path = data_dict['train'] 60 | test_path = data_dict['val'] 61 | nc, names = (1, ['item']) if opt.single_cls else (int(data_dict['nc']), data_dict['names']) # number classes, names 62 | assert len(names) == nc, '%g names found for nc=%g dataset in %s' % (len(names), nc, opt.data) # check 63 | 64 | # Model 65 | pretrained = weights.endswith('.pt') 66 | if pretrained: 67 | with torch_distributed_zero_first(rank): 68 | attempt_download(weights) # download if not found locally 69 | ckpt = torch.load(weights, map_location=device) # load checkpoint 70 | if hyp.get('anchors'): 71 | ckpt['model'].yaml['anchors'] = round(hyp['anchors']) # force autoanchor 72 | model = Model(opt.cfg or ckpt['model'].yaml, ch=3, nc=nc).to(device) # create 73 | exclude = ['anchor'] if opt.cfg or hyp.get('anchors') else [] # exclude keys 74 | state_dict = ckpt['model'].float().state_dict() # to FP32 75 | state_dict = intersect_dicts(state_dict, model.state_dict(), exclude=exclude) # intersect 76 | model.load_state_dict(state_dict, strict=False) # load 77 | logger.info('Transferred %g/%g items from %s' % (len(state_dict), len(model.state_dict()), weights)) # report 78 | else: 79 | model = Model(opt.cfg, ch=3, nc=nc).to(device) # create 80 | 81 | # Freeze 82 | freeze = ['', ] # parameter names to freeze (full or partial) 83 | if any(freeze): 84 | for k, v in model.named_parameters(): 85 | if any(x in k for x in freeze): 86 | print('freezing %s' % k) 87 | v.requires_grad = False 88 | 89 | # Optimizer 90 | nbs = 64 # nominal batch size 91 | accumulate = max(round(nbs / total_batch_size), 1) # accumulate loss before optimizing 92 | hyp['weight_decay'] *= total_batch_size * accumulate / nbs # scale weight_decay 93 | 94 | pg0, pg1, pg2 = [], [], [] # optimizer parameter groups 95 | for k, v in model.named_parameters(): 96 | v.requires_grad = True 97 | if '.bias' in k: 98 | pg2.append(v) # biases 99 | elif '.weight' in k and '.bn' not in k: 100 | pg1.append(v) # apply weight decay 101 | else: 102 | pg0.append(v) # all else 103 | 104 | if opt.adam: 105 | optimizer = optim.Adam(pg0, lr=hyp['lr0'], betas=(hyp['momentum'], 0.999)) # adjust beta1 to momentum 106 | else: 107 | optimizer = optim.SGD(pg0, lr=hyp['lr0'], momentum=hyp['momentum'], nesterov=True) 108 | 109 | optimizer.add_param_group({'params': pg1, 'weight_decay': hyp['weight_decay']}) # add pg1 with weight_decay 110 | optimizer.add_param_group({'params': pg2}) # add pg2 (biases) 111 | logger.info('Optimizer groups: %g .bias, %g conv.weight, %g other' % (len(pg2), len(pg1), len(pg0))) 112 | del pg0, pg1, pg2 113 | 114 | # Scheduler https://arxiv.org/pdf/1812.01187.pdf 115 | # https://pytorch.org/docs/stable/_modules/torch/optim/lr_scheduler.html#OneCycleLR 116 | lf = lambda x: ((1 + math.cos(x * math.pi / epochs)) / 2) * (1 - hyp['lrf']) + hyp['lrf'] # cosine 117 | scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lf) 118 | # plot_lr_scheduler(optimizer, scheduler, epochs) 119 | 120 | # Resume 121 | start_epoch, best_fitness = 0, 0.0 122 | if pretrained: 123 | # Optimizer 124 | if ckpt['optimizer'] is not None: 125 | optimizer.load_state_dict(ckpt['optimizer']) 126 | best_fitness = ckpt['best_fitness'] 127 | 128 | # Results 129 | if ckpt.get('training_results') is not None: 130 | with open(results_file, 'w') as file: 131 | file.write(ckpt['training_results']) # write results.txt 132 | 133 | # Epochs 134 | start_epoch = ckpt['epoch'] + 1 135 | if opt.resume: 136 | assert start_epoch > 0, '%s training to %g epochs is finished, nothing to resume.' % (weights, epochs) 137 | shutil.copytree(wdir, wdir.parent / f'weights_backup_epoch{start_epoch - 1}') # save previous weights 138 | if epochs < start_epoch: 139 | logger.info('%s has been trained for %g epochs. Fine-tuning for %g additional epochs.' % 140 | (weights, ckpt['epoch'], epochs)) 141 | epochs += ckpt['epoch'] # finetune additional epochs 142 | 143 | del ckpt, state_dict 144 | 145 | # Image sizes 146 | gs = int(max(model.stride)) # grid size (max stride) 147 | imgsz, imgsz_test = [check_img_size(x, gs) for x in opt.img_size] # verify imgsz are gs-multiples 148 | 149 | # DP mode 150 | if cuda and rank == -1 and torch.cuda.device_count() > 1: 151 | model = torch.nn.DataParallel(model) 152 | 153 | # SyncBatchNorm 154 | if opt.sync_bn and cuda and rank != -1: 155 | model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model).to(device) 156 | logger.info('Using SyncBatchNorm()') 157 | 158 | # Exponential moving average 159 | ema = ModelEMA(model) if rank in [-1, 0] else None 160 | 161 | # DDP mode 162 | if cuda and rank != -1: 163 | model = DDP(model, device_ids=[opt.local_rank], output_device=opt.local_rank) 164 | 165 | # Trainloader 166 | print("train loader!") 167 | dataloader, dataset = create_dataloader(train_path, imgsz, batch_size, gs, opt, 168 | hyp=hyp, augment=True, cache=opt.cache_images, rect=opt.rect, 169 | rank=rank, world_size=opt.world_size, workers=opt.workers) 170 | mlc = np.concatenate(dataset.labels, 0)[:, 0].max() # max label class 171 | nb = len(dataloader) # number of batches 172 | assert mlc < nc, 'Label class %g exceeds nc=%g in %s. Possible class labels are 0-%g' % (mlc, nc, opt.data, nc - 1) 173 | 174 | # Process 0 175 | if rank in [-1, 0]: 176 | ema.updates = start_epoch * nb // accumulate # set EMA updates 177 | print("test loader!") 178 | testloader = create_dataloader(test_path, imgsz_test, total_batch_size, gs, opt, 179 | hyp=hyp, augment=False, cache=opt.cache_images and not opt.notest, rect=True, 180 | rank=-1, world_size=opt.world_size, workers=opt.workers)[0] # testloader 181 | 182 | if not opt.resume: 183 | labels = np.concatenate(dataset.labels, 0) 184 | c = torch.tensor(labels[:, 0]) # classes 185 | # cf = torch.bincount(c.long(), minlength=nc) + 1. # frequency 186 | # model._initialize_biases(cf.to(device)) 187 | plot_labels(labels, save_dir=log_dir) 188 | if tb_writer: 189 | # tb_writer.add_hparams(hyp, {}) # causes duplicate https://github.com/ultralytics/yolov5/pull/384 190 | tb_writer.add_histogram('classes', c, 0) 191 | 192 | # Anchors 193 | if not opt.noautoanchor: 194 | check_anchors(dataset, model=model, thr=hyp['anchor_t'], imgsz=imgsz) 195 | 196 | # Model parameters 197 | hyp['cls'] *= nc / 80. # scale coco-tuned hyp['cls'] to current dataset 198 | model.nc = nc # attach number of classes to model 199 | model.hyp = hyp # attach hyperparameters to model 200 | model.gr = 1.0 # iou loss ratio (obj_loss = 1.0 or iou) 201 | model.class_weights = labels_to_class_weights(dataset.labels, nc).to(device) # attach class weights 202 | model.names = names 203 | 204 | # Start training 205 | t0 = time.time() 206 | nw = max(round(hyp['warmup_epochs'] * nb), 1e3) # number of warmup iterations, max(3 epochs, 1k iterations) 207 | # nw = min(nw, (epochs - start_epoch) / 2 * nb) # limit warmup to < 1/2 of training 208 | maps = np.zeros(nc) # mAP per class 209 | results = (0, 0, 0, 0, 0, 0, 0) # P, R, mAP@.5, mAP@.5-.95, val_loss(box, obj, cls) 210 | scheduler.last_epoch = start_epoch - 1 # do not move 211 | scaler = amp.GradScaler(enabled=cuda) 212 | logger.info('Image sizes %g train, %g test\nUsing %g dataloader workers\nLogging results to %s\n' 213 | 'Starting training for %g epochs...' % (imgsz, imgsz_test, dataloader.num_workers, log_dir, epochs)) 214 | for epoch in range(start_epoch, epochs): # epoch ------------------------------------------------------------------ 215 | model.train() 216 | 217 | # Update image weights (optional) 218 | if opt.image_weights: 219 | # Generate indices 220 | if rank in [-1, 0]: 221 | cw = model.class_weights.cpu().numpy() * (1 - maps) ** 2 # class weights 222 | iw = labels_to_image_weights(dataset.labels, nc=nc, class_weights=cw) # image weights 223 | dataset.indices = random.choices(range(dataset.n), weights=iw, k=dataset.n) # rand weighted idx 224 | # Broadcast if DDP 225 | if rank != -1: 226 | indices = (torch.tensor(dataset.indices) if rank == 0 else torch.zeros(dataset.n)).int() 227 | dist.broadcast(indices, 0) 228 | if rank != 0: 229 | dataset.indices = indices.cpu().numpy() 230 | 231 | # Update mosaic border 232 | # b = int(random.uniform(0.25 * imgsz, 0.75 * imgsz + gs) // gs * gs) 233 | # dataset.mosaic_border = [b - imgsz, -b] # height, width borders 234 | 235 | mloss = torch.zeros(5, device=device) # mean losses 236 | if rank != -1: 237 | dataloader.sampler.set_epoch(epoch) 238 | pbar = enumerate(dataloader) 239 | logger.info(('\n' + '%10s' * 9) % ('Epoch', 'gpu_mem', 'box', 'obj', 'cls', 'angle', 'total', 'targets', 'img_size')) 240 | if rank in [-1, 0]: 241 | pbar = tqdm(pbar, total=nb) # progress bar 242 | optimizer.zero_grad() 243 | for i, (imgs, targets, paths, _) in pbar: # batch ------------------------------------------------------------- 244 | ni = i + nb * epoch # number integrated batches (since train start) 245 | imgs = imgs.to(device, non_blocking=True).float() / 255.0 # uint8 to float32, 0-255 to 0.0-1.0 246 | 247 | # Warmup 248 | if ni <= nw: 249 | xi = [0, nw] # x interp 250 | # model.gr = np.interp(ni, xi, [0.0, 1.0]) # iou loss ratio (obj_loss = 1.0 or iou) 251 | accumulate = max(1, np.interp(ni, xi, [1, nbs / total_batch_size]).round()) 252 | for j, x in enumerate(optimizer.param_groups): 253 | # bias lr falls from 0.1 to lr0, all other lrs rise from 0.0 to lr0 254 | x['lr'] = np.interp(ni, xi, [hyp['warmup_bias_lr'] if j == 2 else 0.0, x['initial_lr'] * lf(epoch)]) 255 | if 'momentum' in x: 256 | x['momentum'] = np.interp(ni, xi, [hyp['warmup_momentum'], hyp['momentum']]) 257 | 258 | # Multi-scale 259 | if opt.multi_scale: 260 | sz = random.randrange(imgsz * 0.5, imgsz * 1.5 + gs) // gs * gs # size 261 | sf = sz / max(imgs.shape[2:]) # scale factor 262 | if sf != 1: 263 | ns = [math.ceil(x * sf / gs) * gs for x in imgs.shape[2:]] # new shape (stretched to gs-multiple) 264 | imgs = F.interpolate(imgs, size=ns, mode='bilinear', align_corners=False) 265 | 266 | # Forward 267 | with amp.autocast(enabled=cuda): 268 | pred = model(imgs) # forward 269 | loss, loss_items = compute_loss(pred, targets.to(device), model) # loss scaled by batch_size 270 | if rank != -1: 271 | loss *= opt.world_size # gradient averaged between devices in DDP mode 272 | 273 | # Backward 274 | scaler.scale(loss).backward() 275 | 276 | # Optimize 277 | if ni % accumulate == 0: 278 | scaler.step(optimizer) # optimizer.step 279 | scaler.update() 280 | optimizer.zero_grad() 281 | if ema: 282 | ema.update(model) 283 | 284 | # Print 285 | if rank in [-1, 0]: 286 | mloss = (mloss * i + loss_items) / (i + 1) # update mean losses 287 | mem = '%.3gG' % (torch.cuda.memory_reserved() / 1E9 if torch.cuda.is_available() else 0) # (GB) 288 | s = ('%10s' * 2 + '%10.4g' * 7) % ( 289 | '%g/%g' % (epoch, epochs - 1), mem, *mloss, targets.shape[0], imgs.shape[-1]) 290 | pbar.set_description(s) 291 | 292 | # # Plot 293 | # if ni < 3: 294 | # f = str(log_dir / ('train_batch%g.jpg' % ni)) # filename 295 | # result = plot_images(images=imgs, targets=targets, paths=paths, fname=f) 296 | # if tb_writer and result is not None: 297 | # tb_writer.add_image(f, result, dataformats='HWC', global_step=epoch) 298 | # # tb_writer.add_graph(model, imgs) # add model to tensorboard 299 | 300 | # end batch ------------------------------------------------------------------------------------------------ 301 | 302 | # Scheduler 303 | lr = [x['lr'] for x in optimizer.param_groups] # for tensorboard 304 | scheduler.step() 305 | 306 | # DDP process 0 or single-GPU 307 | if rank in [-1, 0]: 308 | # mAP 309 | if ema: 310 | ema.update_attr(model, include=['yaml', 'nc', 'hyp', 'gr', 'names', 'stride']) 311 | final_epoch = epoch + 1 == epochs 312 | if not opt.notest or final_epoch: # Calculate mAP 313 | results, maps, times = test.test(opt.data, 314 | batch_size=total_batch_size, 315 | imgsz=imgsz_test, 316 | model=ema.ema, 317 | single_cls=opt.single_cls, 318 | dataloader=testloader, 319 | save_dir=log_dir, 320 | plots=epoch == 0 or final_epoch) # plot first and last 321 | 322 | # Write 323 | with open(results_file, 'a') as f: 324 | f.write(s + '%10.4g' * 7 % results + '\n') # P, R, mAP@.5, mAP@.5-.95, val_loss(box, obj, cls) 325 | if len(opt.name) and opt.bucket: 326 | os.system('gsutil cp %s gs://%s/results/results%s.txt' % (results_file, opt.bucket, opt.name)) 327 | 328 | # Tensorboard 329 | if tb_writer: 330 | tags = ['train/box_loss', 'train/obj_loss', 'train/cls_loss', # train loss 331 | 'metrics/precision', 'metrics/recall', 'metrics/mAP_0.5', 'metrics/mAP_0.5:0.95', 332 | 'val/box_loss', 'val/obj_loss', 'val/cls_loss', # val loss 333 | 'x/lr0', 'x/lr1', 'x/lr2'] # params 334 | for x, tag in zip(list(mloss[:-1]) + list(results) + lr, tags): 335 | tb_writer.add_scalar(tag, x, epoch) 336 | 337 | # Update best mAP 338 | fi = fitness(np.array(results).reshape(1, -1)) # weighted combination of [P, R, mAP@.5, mAP@.5-.95] 339 | if fi > best_fitness: 340 | best_fitness = fi 341 | 342 | # Save model 343 | save = (not opt.nosave) or (final_epoch and not opt.evolve) 344 | if save: 345 | with open(results_file, 'r') as f: # create checkpoint 346 | ckpt = {'epoch': epoch, 347 | 'best_fitness': best_fitness, 348 | 'training_results': f.read(), 349 | 'model': ema.ema, 350 | 'optimizer': None if final_epoch else optimizer.state_dict()} 351 | 352 | # Save last, best and delete 353 | torch.save(ckpt, last) 354 | if best_fitness == fi: 355 | torch.save(ckpt, best) 356 | del ckpt 357 | # end epoch ---------------------------------------------------------------------------------------------------- 358 | # end training 359 | 360 | if rank in [-1, 0]: 361 | # Strip optimizers 362 | n = opt.name if opt.name.isnumeric() else '' 363 | fresults, flast, fbest = log_dir / f'results{n}.txt', wdir / f'last{n}.pt', wdir / f'best{n}.pt' 364 | for f1, f2 in zip([wdir / 'last.pt', wdir / 'best.pt', results_file], [flast, fbest, fresults]): 365 | if os.path.exists(f1): 366 | os.rename(f1, f2) # rename 367 | if str(f2).endswith('.pt'): # is *.pt 368 | strip_optimizer(f2) # strip optimizer 369 | os.system('gsutil cp %s gs://%s/weights' % (f2, opt.bucket)) if opt.bucket else None # upload 370 | # Finish 371 | if not opt.evolve: 372 | plot_results(save_dir=log_dir) # save as results.png 373 | logger.info('%g epochs completed in %.3f hours.\n' % (epoch - start_epoch + 1, (time.time() - t0) / 3600)) 374 | 375 | dist.destroy_process_group() if rank not in [-1, 0] else None 376 | torch.cuda.empty_cache() 377 | return results 378 | 379 | 380 | if __name__ == '__main__': 381 | parser = argparse.ArgumentParser() 382 | parser.add_argument('--weights', type=str, default='weights/10.28-0.5angle.pt', help='initial weights path') 383 | parser.add_argument('--cfg', type=str, default='models/yolov5l.yaml', help='model.yaml path') 384 | parser.add_argument('--data', type=str, default='data/wheat0.yaml', help='data.yaml path') 385 | parser.add_argument('--hyp', type=str, default='data/hyp.scratch.yaml', help='hyperparameters path') 386 | parser.add_argument('--epochs', type=int, default=60) 387 | parser.add_argument('--batch-size', type=int, default=1, help='total batch size for all GPUs') 388 | parser.add_argument('--img-size', nargs='+', type=int, default=[1024, 1024], help='[train, test] image sizes') 389 | parser.add_argument('--rect', action='store_true', help='rectangular training') 390 | parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training') 391 | parser.add_argument('--nosave', action='store_true', help='only save final checkpoint') 392 | parser.add_argument('--notest', action='store_true', default=True, help='only test final epoch') 393 | parser.add_argument('--noautoanchor', action='store_true', help='disable autoanchor check') 394 | parser.add_argument('--evolve', action='store_true', help='evolve hyperparameters') 395 | parser.add_argument('--bucket', type=str, default='', help='gsutil bucket') 396 | parser.add_argument('--cache-images', action='store_true', help='cache images for faster training') 397 | parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training') 398 | parser.add_argument('--name', default='', help='renames results.txt to results_name.txt if supplied') 399 | parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') 400 | parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%') 401 | parser.add_argument('--single-cls', action='store_true', help='train as single-class dataset') 402 | parser.add_argument('--adam', action='store_true', help='use torch.optim.Adam() optimizer') 403 | parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode') 404 | parser.add_argument('--local_rank', type=int, default=-1, help='DDP parameter, do not modify') 405 | parser.add_argument('--logdir', type=str, default='runs/', help='logging directory') 406 | parser.add_argument('--workers', type=int, default=8, help='maximum number of dataloader workers') 407 | opt = parser.parse_args() 408 | 409 | # Set DDP variables 410 | opt.total_batch_size = opt.batch_size 411 | opt.world_size = int(os.environ['WORLD_SIZE']) if 'WORLD_SIZE' in os.environ else 1 412 | opt.global_rank = int(os.environ['RANK']) if 'RANK' in os.environ else -1 413 | set_logging(opt.global_rank) 414 | if opt.global_rank in [-1, 0]: 415 | check_git_status() 416 | 417 | # Resume 418 | if opt.resume: # resume an interrupted run 419 | ckpt = opt.resume if isinstance(opt.resume, str) else get_latest_run() # specified or most recent path 420 | log_dir = Path(ckpt).parent.parent # runs/exp0 421 | assert os.path.isfile(ckpt), 'ERROR: --resume checkpoint does not exist' 422 | with open(log_dir / 'opt.yaml') as f: 423 | opt = argparse.Namespace(**yaml.load(f, Loader=yaml.FullLoader)) # replace 424 | opt.cfg, opt.weights, opt.resume = '', ckpt, True 425 | logger.info('Resuming training from %s' % ckpt) 426 | 427 | else: 428 | # opt.hyp = opt.hyp or ('hyp.finetune.yaml' if opt.weights else 'hyp.scratch.yaml') 429 | opt.data, opt.cfg, opt.hyp = check_file(opt.data), check_file(opt.cfg), check_file(opt.hyp) # check files 430 | assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified' 431 | opt.img_size.extend([opt.img_size[-1]] * (2 - len(opt.img_size))) # extend to 2 sizes (train, test) 432 | log_dir = increment_dir(Path(opt.logdir) / 'exp', opt.name) # runs/exp1 433 | 434 | device = select_device(opt.device, batch_size=opt.batch_size) 435 | 436 | # DDP mode 437 | if opt.local_rank != -1: 438 | assert torch.cuda.device_count() > opt.local_rank 439 | torch.cuda.set_device(opt.local_rank) 440 | device = torch.device('cuda', opt.local_rank) 441 | dist.init_process_group(backend='nccl', init_method='env://') # distributed backend 442 | assert opt.batch_size % opt.world_size == 0, '--batch-size must be multiple of CUDA device count' 443 | opt.batch_size = opt.total_batch_size // opt.world_size 444 | 445 | logger.info(opt) 446 | with open(opt.hyp) as f: 447 | hyp = yaml.load(f, Loader=yaml.FullLoader) # load hyps 448 | 449 | # Train 450 | if not opt.evolve: 451 | tb_writer = None 452 | if opt.global_rank in [-1, 0]: 453 | logger.info('Start Tensorboard with "tensorboard --logdir %s", view at http://localhost:6006/' % opt.logdir) 454 | tb_writer = SummaryWriter(log_dir=log_dir) # runs/exp0 455 | 456 | train(hyp, opt, device, tb_writer) 457 | 458 | # Evolve hyperparameters (optional) 459 | else: 460 | # Hyperparameter evolution metadata (mutation scale 0-1, lower_limit, upper_limit) 461 | meta = {'lr0': (1, 1e-5, 1e-1), # initial learning rate (SGD=1E-2, Adam=1E-3) 462 | 'lrf': (1, 0.01, 1.0), # final OneCycleLR learning rate (lr0 * lrf) 463 | 'momentum': (0.3, 0.6, 0.98), # SGD momentum/Adam beta1 464 | 'weight_decay': (1, 0.0, 0.001), # optimizer weight decay 465 | 'warmup_epochs': (1, 0.0, 5.0), # warmup epochs (fractions ok) 466 | 'warmup_momentum': (1, 0.0, 0.95), # warmup initial momentum 467 | 'warmup_bias_lr': (1, 0.0, 0.2), # warmup initial bias lr 468 | 'box': (1, 0.02, 0.2), # box loss gain 469 | 'cls': (1, 0.2, 4.0), # cls loss gain 470 | 'cls_pw': (1, 0.5, 2.0), # cls BCELoss positive_weight 471 | 'obj': (1, 0.2, 4.0), # obj loss gain (scale with pixels) 472 | 'obj_pw': (1, 0.5, 2.0), # obj BCELoss positive_weight 473 | 'iou_t': (0, 0.1, 0.7), # IoU training threshold 474 | 'anchor_t': (1, 2.0, 8.0), # anchor-multiple threshold 475 | 'anchors': (2, 2.0, 10.0), # anchors per output grid (0 to ignore) 476 | 'fl_gamma': (0, 0.0, 2.0), # focal loss gamma (efficientDet default gamma=1.5) 477 | 'hsv_h': (1, 0.0, 0.1), # image HSV-Hue augmentation (fraction) 478 | 'hsv_s': (1, 0.0, 0.9), # image HSV-Saturation augmentation (fraction) 479 | 'hsv_v': (1, 0.0, 0.9), # image HSV-Value augmentation (fraction) 480 | 'degrees': (1, 0.0, 45.0), # image rotation (+/- deg) 481 | 'translate': (1, 0.0, 0.9), # image translation (+/- fraction) 482 | 'scale': (1, 0.0, 0.9), # image scale (+/- gain) 483 | 'shear': (1, 0.0, 10.0), # image shear (+/- deg) 484 | 'perspective': (0, 0.0, 0.001), # image perspective (+/- fraction), range 0-0.001 485 | 'flipud': (1, 0.0, 1.0), # image flip up-down (probability) 486 | 'fliplr': (0, 0.0, 1.0), # image flip left-right (probability) 487 | 'mosaic': (1, 0.0, 1.0), # image mixup (probability) 488 | 'mixup': (1, 0.0, 1.0)} # image mixup (probability) 489 | 490 | assert opt.local_rank == -1, 'DDP mode not implemented for --evolve' 491 | opt.notest, opt.nosave = True, True # only test/save final epoch 492 | # ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices 493 | yaml_file = Path('runs/evolve/hyp_evolved.yaml') # save best result here 494 | if opt.bucket: 495 | os.system('gsutil cp gs://%s/evolve.txt .' % opt.bucket) # download evolve.txt if exists 496 | 497 | for _ in range(300): # generations to evolve 498 | if os.path.exists('evolve.txt'): # if evolve.txt exists: select best hyps and mutate 499 | # Select parent(s) 500 | parent = 'single' # parent selection method: 'single' or 'weighted' 501 | x = np.loadtxt('evolve.txt', ndmin=2) 502 | n = min(5, len(x)) # number of previous results to consider 503 | x = x[np.argsort(-fitness(x))][:n] # top n mutations 504 | w = fitness(x) - fitness(x).min() # weights 505 | if parent == 'single' or len(x) == 1: 506 | # x = x[random.randint(0, n - 1)] # random selection 507 | x = x[random.choices(range(n), weights=w)[0]] # weighted selection 508 | elif parent == 'weighted': 509 | x = (x * w.reshape(n, 1)).sum(0) / w.sum() # weighted combination 510 | 511 | # Mutate 512 | mp, s = 0.8, 0.2 # mutation probability, sigma 513 | npr = np.random 514 | npr.seed(int(time.time())) 515 | g = np.array([x[0] for x in meta.values()]) # gains 0-1 516 | ng = len(meta) 517 | v = np.ones(ng) 518 | while all(v == 1): # mutate until a change occurs (prevent duplicates) 519 | v = (g * (npr.random(ng) < mp) * npr.randn(ng) * npr.random() * s + 1).clip(0.3, 3.0) 520 | for i, k in enumerate(hyp.keys()): # plt.hist(v.ravel(), 300) 521 | hyp[k] = float(x[i + 7] * v[i]) # mutate 522 | 523 | # Constrain to limits 524 | for k, v in meta.items(): 525 | hyp[k] = max(hyp[k], v[1]) # lower limit 526 | hyp[k] = min(hyp[k], v[2]) # upper limit 527 | hyp[k] = round(hyp[k], 5) # significant digits 528 | 529 | # Train mutation 530 | results = train(hyp.copy(), opt, device) 531 | 532 | # Write mutation results 533 | print_mutation(hyp.copy(), results, yaml_file, opt.bucket) 534 | 535 | # Plot results 536 | plot_evolution(yaml_file) 537 | print('Hyperparameter evolution complete. Best results saved as: %s\nCommand to train a new model with these ' 538 | 'hyperparameters: $ python train.py --hyp %s' % (yaml_file, yaml_file)) 539 | -------------------------------------------------------------------------------- /utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/utils/__init__.py -------------------------------------------------------------------------------- /utils/__pycache__/__init__.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/utils/__pycache__/__init__.cpython-38.pyc -------------------------------------------------------------------------------- /utils/__pycache__/datasets.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/utils/__pycache__/datasets.cpython-38.pyc -------------------------------------------------------------------------------- /utils/__pycache__/general.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/utils/__pycache__/general.cpython-38.pyc -------------------------------------------------------------------------------- /utils/__pycache__/google_utils.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/utils/__pycache__/google_utils.cpython-38.pyc -------------------------------------------------------------------------------- /utils/__pycache__/torch_utils.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/BossZard/rotation-yolov5/6d1303ef08c4fd1ee8e92b7ef88a9e1e3acdd2cc/utils/__pycache__/torch_utils.cpython-38.pyc -------------------------------------------------------------------------------- /utils/activations.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | 6 | # Swish https://arxiv.org/pdf/1905.02244.pdf --------------------------------------------------------------------------- 7 | class Swish(nn.Module): # 8 | @staticmethod 9 | def forward(x): 10 | return x * torch.sigmoid(x) 11 | 12 | 13 | class Hardswish(nn.Module): # export-friendly version of nn.Hardswish() 14 | @staticmethod 15 | def forward(x): 16 | # return x * F.hardsigmoid(x) # for torchscript and CoreML 17 | return x * F.hardtanh(x + 3, 0., 6.) / 6. # for torchscript, CoreML and ONNX 18 | 19 | 20 | class MemoryEfficientSwish(nn.Module): 21 | class F(torch.autograd.Function): 22 | @staticmethod 23 | def forward(ctx, x): 24 | ctx.save_for_backward(x) 25 | return x * torch.sigmoid(x) 26 | 27 | @staticmethod 28 | def backward(ctx, grad_output): 29 | x = ctx.saved_tensors[0] 30 | sx = torch.sigmoid(x) 31 | return grad_output * (sx * (1 + x * (1 - sx))) 32 | 33 | def forward(self, x): 34 | return self.F.apply(x) 35 | 36 | 37 | # Mish https://github.com/digantamisra98/Mish -------------------------------------------------------------------------- 38 | class Mish(nn.Module): 39 | @staticmethod 40 | def forward(x): 41 | return x * F.softplus(x).tanh() 42 | 43 | 44 | class MemoryEfficientMish(nn.Module): 45 | class F(torch.autograd.Function): 46 | @staticmethod 47 | def forward(ctx, x): 48 | ctx.save_for_backward(x) 49 | return x.mul(torch.tanh(F.softplus(x))) # x * tanh(ln(1 + exp(x))) 50 | 51 | @staticmethod 52 | def backward(ctx, grad_output): 53 | x = ctx.saved_tensors[0] 54 | sx = torch.sigmoid(x) 55 | fx = F.softplus(x).tanh() 56 | return grad_output * (fx + x * sx * (1 - fx * fx)) 57 | 58 | def forward(self, x): 59 | return self.F.apply(x) 60 | 61 | 62 | # FReLU https://arxiv.org/abs/2007.11824 ------------------------------------------------------------------------------- 63 | class FReLU(nn.Module): 64 | def __init__(self, c1, k=3): # ch_in, kernel 65 | super().__init__() 66 | self.conv = nn.Conv2d(c1, c1, k, 1, 1, groups=c1) 67 | self.bn = nn.BatchNorm2d(c1) 68 | 69 | def forward(self, x): 70 | return torch.max(x, self.bn(self.conv(x))) 71 | -------------------------------------------------------------------------------- /utils/evolve.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Hyperparameter evolution commands (avoids CUDA memory leakage issues) 3 | # Replaces train.py python generations 'for' loop with a bash 'for' loop 4 | 5 | # Start on 4-GPU machine 6 | #for i in 0 1 2 3; do 7 | # t=ultralytics/yolov5:evolve && sudo docker pull $t && sudo docker run -d --ipc=host --gpus all -v "$(pwd)"/VOC:/usr/src/VOC $t bash utils/evolve.sh $i 8 | # sleep 60 # avoid simultaneous evolve.txt read/write 9 | #done 10 | 11 | # Hyperparameter evolution commands 12 | while true; do 13 | # python train.py --batch 64 --weights yolov5m.pt --data voc.yaml --img 512 --epochs 50 --evolve --bucket ult/evolve/voc --device $1 14 | python train.py --batch 40 --weights yolov5m.pt --data coco.yaml --img 640 --epochs 30 --evolve --bucket ult/evolve/coco --device $1 15 | done 16 | -------------------------------------------------------------------------------- /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==18.1 3 | Flask==1.0.2 4 | gunicorn==19.9.0 5 | -------------------------------------------------------------------------------- /utils/google_app_engine/app.yaml: -------------------------------------------------------------------------------- 1 | runtime: custom 2 | env: flex 3 | 4 | service: yolov5app 5 | 6 | liveness_check: 7 | initial_delay_sec: 600 8 | 9 | manual_scaling: 10 | instances: 1 11 | resources: 12 | cpu: 1 13 | memory_gb: 4 14 | disk_size_gb: 20 -------------------------------------------------------------------------------- /utils/google_utils.py: -------------------------------------------------------------------------------- 1 | # This file contains google utils: https://cloud.google.com/storage/docs/reference/libraries 2 | # pip install --upgrade google-cloud-storage 3 | # from google.cloud import storage 4 | 5 | import os 6 | import platform 7 | import subprocess 8 | import time 9 | from pathlib import Path 10 | 11 | import torch 12 | 13 | 14 | def gsutil_getsize(url=''): 15 | # gs://bucket/file size https://cloud.google.com/storage/docs/gsutil/commands/du 16 | s = subprocess.check_output('gsutil du %s' % url, shell=True).decode('utf-8') 17 | return eval(s.split(' ')[0]) if len(s) else 0 # bytes 18 | 19 | 20 | def attempt_download(weights): 21 | # Attempt to download pretrained weights if not found locally 22 | weights = weights.strip().replace("'", '') 23 | file = Path(weights).name 24 | 25 | msg = weights + ' missing, try downloading from https://github.com/ultralytics/yolov5/releases/' 26 | models = ['yolov5s.pt', 'yolov5m.pt', 'yolov5l.pt', 'yolov5x.pt'] # available models 27 | 28 | if file in models and not os.path.isfile(weights): 29 | # Google Drive 30 | # d = {'yolov5s.pt': '1R5T6rIyy3lLwgFXNms8whc-387H0tMQO', 31 | # 'yolov5m.pt': '1vobuEExpWQVpXExsJ2w-Mbf3HJjWkQJr', 32 | # 'yolov5l.pt': '1hrlqD1Wdei7UT4OgT785BEk1JwnSvNEV', 33 | # 'yolov5x.pt': '1mM8aZJlWTxOg7BZJvNUMrTnA2AbeCVzS'} 34 | # r = gdrive_download(id=d[file], name=weights) if file in d else 1 35 | # if r == 0 and os.path.exists(weights) and os.path.getsize(weights) > 1E6: # check 36 | # return 37 | 38 | try: # GitHub 39 | url = 'https://github.com/ultralytics/yolov5/releases/download/v3.0/' + file 40 | print('Downloading %s to %s...' % (url, weights)) 41 | torch.hub.download_url_to_file(url, weights) 42 | assert os.path.exists(weights) and os.path.getsize(weights) > 1E6 # check 43 | except Exception as e: # GCP 44 | print('Download error: %s' % e) 45 | url = 'https://storage.googleapis.com/ultralytics/yolov5/ckpt/' + file 46 | print('Downloading %s to %s...' % (url, weights)) 47 | r = os.system('curl -L %s -o %s' % (url, weights)) # torch.hub.download_url_to_file(url, weights) 48 | finally: 49 | if not (os.path.exists(weights) and os.path.getsize(weights) > 1E6): # check 50 | os.remove(weights) if os.path.exists(weights) else None # remove partial downloads 51 | print('ERROR: Download failure: %s' % msg) 52 | print('') 53 | return 54 | 55 | 56 | def gdrive_download(id='1n_oKgR81BJtqk75b00eAjdv03qVCQn2f', name='coco128.zip'): 57 | # Downloads a file from Google Drive. from utils.google_utils import *; gdrive_download() 58 | t = time.time() 59 | 60 | print('Downloading https://drive.google.com/uc?export=download&id=%s as %s... ' % (id, name), end='') 61 | os.remove(name) if os.path.exists(name) else None # remove existing 62 | os.remove('cookie') if os.path.exists('cookie') else None 63 | 64 | # Attempt file download 65 | out = "NUL" if platform.system() == "Windows" else "/dev/null" 66 | os.system('curl -c ./cookie -s -L "drive.google.com/uc?export=download&id=%s" > %s ' % (id, out)) 67 | if os.path.exists('cookie'): # large file 68 | s = 'curl -Lb ./cookie "drive.google.com/uc?export=download&confirm=%s&id=%s" -o %s' % (get_token(), id, name) 69 | else: # small file 70 | s = 'curl -s -L -o %s "drive.google.com/uc?export=download&id=%s"' % (name, id) 71 | r = os.system(s) # execute, capture return 72 | os.remove('cookie') if os.path.exists('cookie') else None 73 | 74 | # Error check 75 | if r != 0: 76 | os.remove(name) if os.path.exists(name) else None # remove partial 77 | print('Download error ') # raise Exception('Download error') 78 | return r 79 | 80 | # Unzip if archive 81 | if name.endswith('.zip'): 82 | print('unzipping... ', end='') 83 | os.system('unzip -q %s' % name) # unzip 84 | os.remove(name) # remove zip to free space 85 | 86 | print('Done (%.1fs)' % (time.time() - t)) 87 | return r 88 | 89 | 90 | def get_token(cookie="./cookie"): 91 | with open(cookie) as f: 92 | for line in f: 93 | if "download" in line: 94 | return line.split()[-1] 95 | return "" 96 | 97 | # def upload_blob(bucket_name, source_file_name, destination_blob_name): 98 | # # Uploads a file to a bucket 99 | # # https://cloud.google.com/storage/docs/uploading-objects#storage-upload-object-python 100 | # 101 | # storage_client = storage.Client() 102 | # bucket = storage_client.get_bucket(bucket_name) 103 | # blob = bucket.blob(destination_blob_name) 104 | # 105 | # blob.upload_from_filename(source_file_name) 106 | # 107 | # print('File {} uploaded to {}.'.format( 108 | # source_file_name, 109 | # destination_blob_name)) 110 | # 111 | # 112 | # def download_blob(bucket_name, source_blob_name, destination_file_name): 113 | # # Uploads a blob from a bucket 114 | # storage_client = storage.Client() 115 | # bucket = storage_client.get_bucket(bucket_name) 116 | # blob = bucket.blob(source_blob_name) 117 | # 118 | # blob.download_to_filename(destination_file_name) 119 | # 120 | # print('Blob {} downloaded to {}.'.format( 121 | # source_blob_name, 122 | # destination_file_name)) 123 | -------------------------------------------------------------------------------- /utils/kmeans_for_anchors.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import xml.etree.ElementTree as ET 3 | import glob 4 | import random 5 | import cv2 6 | import os 7 | import numpy as np 8 | from shapely.geometry import Polygon, MultiPoint # 多边形 9 | import time 10 | import cv2 11 | import argparse 12 | 13 | from time import sleep 14 | def trans(file, line_, wh_list): 15 | # file = '1001.txt' 16 | path = opt.label_path + '/' + file 17 | 18 | # line = '' + img_path + '/' + os.path.splitext(file)[0] + '.tif' 19 | line = '' 20 | # print(line) 21 | # print(path) 22 | f = open(path) 23 | label = f.read().split() 24 | # print(label) 25 | clss = [] 26 | xsets = [] 27 | ysets = [] 28 | sets = [] 29 | 30 | 31 | for i in range(0, len(label), 9): 32 | cls = float(label[i]) - 1 33 | if cls not in clss: 34 | clss.append(cls) 35 | data = np.array(label[i+1:i+9]).astype(int) 36 | data = data.reshape(4, 2) 37 | 38 | rect = cv2.minAreaRect(data) # 得到最小外接矩形的(中心(x,y), (宽,高), 旋转角度) 39 | # print(rect) 40 | box = cv2.boxPoints(rect).astype(int) 41 | 42 | c_x = rect[0][0] 43 | c_y = rect[0][1] 44 | w = rect[1][0] 45 | h = rect[1][1] 46 | theta = rect[-1] 47 | 48 | 49 | if (theta < -90 or theta > 0) and h < w: 50 | print(w,h) 51 | print(file) 52 | print(theta) 53 | sleep(11111) 54 | 55 | if theta == 0 and w < h: 56 | theta = -90 57 | t = h 58 | h = w 59 | w = t 60 | 61 | if w > h: 62 | t = h 63 | h = w 64 | w = t 65 | 66 | 67 | else: 68 | if theta == 0: 69 | print('dfasd') 70 | theta = 0 71 | else: 72 | theta = 90 + theta 73 | 74 | if w > h : 75 | sleep(1111) 76 | 77 | 78 | # print(c_x, c_y, w, h, theta) 79 | # line = line + ' ' + str(c_x/1024) + ',' + str(c_y/1024) + ',' + str(h / 1024) + ',' + str(w / 1024) + ',' + str(int(theta)) + ',' + str(cls) + ' ' 80 | # line = line + ' ' + str(c_x - h / 2) + ',' + str(c_y - w / 2) + ',' + str(c_x + h / 2) + ',' + str(c_y + w / 2) + ',' + str(cls) + ',' + str(int(theta)+90) + ' ' 81 | line = line + str(cls) + ' ' + str(c_x / 1024) + ' ' + str(c_y / 1024) + ' ' + str(h / 1024) + ' ' + str(w / 1024) + ' ' + str(int(theta)+90) + '\n' 82 | # line = line + str(cls) + ' ' + str(c_x / 1024) + ' ' + str(c_y / 1024) + ' ' + str(h / 1024) + ' ' + str( 83 | # w / 1024) + ' ' + str(int(theta) + 90) + '\n' 84 | wh_list.append([h/1024, w/1024]) 85 | # with open(r'D:\hjj\yolov5-master\convertor\fold0\labels\theta\{}'.format(os.path.splitext(file)[0] + '.txt'), 86 | # 'w+') as f: 87 | # 88 | # f.write(line) 89 | # f.close() 90 | line_ = line_ + line + '\n' 91 | 92 | # # print(data[:,0].shape) 93 | # # poly = Polygon(data).convex_hull 94 | # d_index = np.argmax(data[:, 0]) 95 | # c_index = np.argmax(data[:, 1]) 96 | # c_x = (max(data[:, 0]) + min(data[:, 0])) / 2 97 | # c_y = (max(data[:, 1]) + min(data[:, 1])) / 2 98 | # print(data[d_index],data[c_index]) 99 | # # print('len:',len(set(data[:,0]))) 100 | # if len(set(data[:, 0])) not in xsets: 101 | # xsets.append(len(set(data[:, 0]))) 102 | # if len(set(data[:, 1])) not in ysets: 103 | # ysets.append(len(set(data[:, 1]))) 104 | # if (len(set(data[:, 1]))*len(set(data[:, 0]))) not in sets: 105 | # sets.append((len(set(data[:, 1]))*len(set(data[:, 0])))) 106 | # if len(set(data[:,0])) < 4 or len(set(data[:,1])) < 4: 107 | # 108 | # if len(set(data[:,0])) == 2 and len(set(data[:,1])) == 2: 109 | # 110 | # print('正规矩形:') 111 | # theta = - np.pi / 2 112 | # right = np.where(data[:, 0]==max(data[:, 0])) 113 | # top = np.where(data[:, 1]==max(data[:, 1])) 114 | # # print(top[0], right[0]) 115 | # # h = np.abs(data[top[0][0]][0] - data[top[0][1]][0]) 116 | # # w = np.abs(data[right[0][0]][1] - data[right[0][1]][1]) 117 | # # 118 | # # print(w , h) 119 | # # if len(set(data[:,0])) == 3 or len(set(data[:,1])) == 3: 120 | # 121 | # 122 | # 123 | # else: 124 | # # print(1) 125 | # theta = - np.arctan((data[c_index][1] - data[d_index][1]) / (data[d_index][0] - data[c_index][0])) 126 | # 127 | # w = np.sqrt((data[c_index][1] - data[d_index][1])**2 + (data[d_index][0] - data[c_index][0])**2) 128 | # h = np.sqrt((data[d_index][0] - data[np.argmin(data[:, 1])][0])**2 +(data[d_index][1] - data[np.argmin(data[:, 1])][1])**2) 129 | # # print(theta) 130 | # 131 | # # print(c_x, c_y, w, h, theta) 132 | 133 | return path, rect, line_, int(theta) + 90, wh_list 134 | 135 | 136 | 137 | 138 | def cas_iou(box,cluster): 139 | x = np.minimum(cluster[:,0],box[0]) 140 | y = np.minimum(cluster[:,1],box[1]) 141 | 142 | intersection = x * y 143 | area1 = box[0] * box[1] 144 | 145 | area2 = cluster[:,0] * cluster[:,1] 146 | iou = intersection / (area1 + area2 -intersection) 147 | 148 | return iou 149 | 150 | def avg_iou(box,cluster): 151 | return np.mean([np.max(cas_iou(box[i],cluster)) for i in range(box.shape[0])]) 152 | 153 | 154 | def kmeans(box,k): 155 | # 取出一共有多少框 156 | row = box.shape[0] 157 | 158 | # 每个框各个点的位置 159 | distance = np.empty((row,k)) 160 | 161 | # 最后的聚类位置 162 | last_clu = np.zeros((row,)) 163 | 164 | np.random.seed() 165 | 166 | # 随机选5个当聚类中心 167 | cluster = box[np.random.choice(row,k,replace = False)] 168 | # cluster = random.sample(row, k) 169 | while True: 170 | # 计算每一行距离五个点的iou情况。 171 | for i in range(row): 172 | distance[i] = 1 - cas_iou(box[i],cluster) 173 | 174 | # 取出最小点 175 | near = np.argmin(distance,axis=1) 176 | 177 | if (last_clu == near).all(): 178 | break 179 | 180 | # 求每一个类的中位点 181 | for j in range(k): 182 | cluster[j] = np.median( 183 | box[near == j],axis=0) 184 | 185 | last_clu = near 186 | 187 | return cluster 188 | 189 | def load_data(path): 190 | data = [] 191 | # 对于每一个xml都寻找box 192 | for xml_file in glob.glob('{}/*xml'.format(path)): 193 | tree = ET.parse(xml_file) 194 | height = int(tree.findtext('./size/height')) 195 | width = int(tree.findtext('./size/width')) 196 | # 对于每一个目标都获得它的宽高 197 | for obj in tree.iter('object'): 198 | xmin = int(float(obj.findtext('bndbox/xmin'))) / width 199 | ymin = int(float(obj.findtext('bndbox/ymin'))) / height 200 | xmax = int(float(obj.findtext('bndbox/xmax'))) / width 201 | ymax = int(float(obj.findtext('bndbox/ymax'))) / height 202 | 203 | xmin = np.float64(xmin) 204 | ymin = np.float64(ymin) 205 | xmax = np.float64(xmax) 206 | ymax = np.float64(ymax) 207 | # 得到宽高 208 | data.append([xmax-xmin,ymax-ymin]) 209 | return np.array(data) 210 | 211 | 212 | if __name__ == '__main__': 213 | parser = argparse.ArgumentParser() 214 | parser.add_argument('--label_path', type=str, default=r'D:\hjj\火箭军\科目四按图索骥\科目四初赛第一阶段\train\labels/', help='label path') 215 | opt = parser.parse_args() 216 | # label_path = r'D:\hjj\火箭军\科目四按图索骥\科目四初赛第一阶段\train\labels/' 217 | 218 | all_label = [] 219 | cls = [] 220 | xsets = [] 221 | ysets = [] 222 | sets = [] 223 | line_ = '' 224 | thetas = [] 225 | wh_list = [] 226 | for file in os.listdir(opt.label_path): 227 | path, ret, line_, theta, wh_list = trans(file, line_, wh_list) 228 | if theta not in thetas: 229 | thetas.append(theta) 230 | 231 | # 运行该程序会计算'./VOCdevkit/VOC2007/Annotations'的xml 232 | # 会生成yolo_anchors.txt 233 | SIZE = 1024 234 | anchors_num = 9 235 | # 载入数据集,可以使用VOC的xml 236 | path = r'./VOCdevkit/VOC2007/Annotations' 237 | 238 | # 载入所有的xml 239 | # 存储格式为转化为比例后的width,height 240 | # data = load_data(path) 241 | data = np.array(wh_list) 242 | 243 | # 使用k聚类算法 244 | out = kmeans(data,anchors_num) 245 | out = out[np.argsort(out[:,0])] 246 | print('acc:{:.2f}%'.format(avg_iou(data,out) * 100)) 247 | print(out*SIZE) 248 | data = out*SIZE 249 | f = open("yolo_anchors.txt", 'w') 250 | row = np.shape(data)[0] 251 | for i in range(row): 252 | if i == 0: 253 | x_y = "%d,%d" % (data[i][0], data[i][1]) 254 | else: 255 | x_y = ", %d,%d" % (data[i][0], data[i][1]) 256 | f.write(x_y) 257 | f.close() -------------------------------------------------------------------------------- /utils/torch_utils.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import math 3 | import os 4 | import time 5 | from copy import deepcopy 6 | 7 | import torch 8 | import torch.backends.cudnn as cudnn 9 | import torch.nn as nn 10 | import torch.nn.functional as F 11 | import torchvision 12 | 13 | logger = logging.getLogger(__name__) 14 | 15 | 16 | def init_torch_seeds(seed=0): 17 | torch.manual_seed(seed) 18 | 19 | # Speed-reproducibility tradeoff https://pytorch.org/docs/stable/notes/randomness.html 20 | if seed == 0: # slower, more reproducible 21 | cudnn.deterministic = True 22 | cudnn.benchmark = False 23 | else: # faster, less reproducible 24 | cudnn.deterministic = False 25 | cudnn.benchmark = True 26 | 27 | 28 | def select_device(device='', batch_size=None): 29 | # device = 'cpu' or '0' or '0,1,2,3' 30 | cpu_request = device.lower() == 'cpu' 31 | if device and not cpu_request: # if device requested other than 'cpu' 32 | os.environ['CUDA_VISIBLE_DEVICES'] = device # set environment variable 33 | assert torch.cuda.is_available(), 'CUDA unavailable, invalid device %s requested' % device # check availablity 34 | 35 | cuda = False if cpu_request else torch.cuda.is_available() 36 | if cuda: 37 | c = 1024 ** 2 # bytes to MB 38 | ng = torch.cuda.device_count() 39 | if ng > 1 and batch_size: # check that batch_size is compatible with device_count 40 | assert batch_size % ng == 0, 'batch-size %g not multiple of GPU count %g' % (batch_size, ng) 41 | x = [torch.cuda.get_device_properties(i) for i in range(ng)] 42 | s = 'Using CUDA ' 43 | for i in range(0, ng): 44 | if i == 1: 45 | s = ' ' * len(s) 46 | logger.info("%sdevice%g _CudaDeviceProperties(name='%s', total_memory=%dMB)" % 47 | (s, i, x[i].name, x[i].total_memory / c)) 48 | else: 49 | logger.info('Using CPU') 50 | 51 | logger.info('') # skip a line 52 | return torch.device('cuda:0' if cuda else 'cpu') 53 | 54 | 55 | def time_synchronized(): 56 | torch.cuda.synchronize() if torch.cuda.is_available() else None 57 | return time.time() 58 | 59 | 60 | def is_parallel(model): 61 | return type(model) in (nn.parallel.DataParallel, nn.parallel.DistributedDataParallel) 62 | 63 | 64 | def intersect_dicts(da, db, exclude=()): 65 | # Dictionary intersection of matching keys and shapes, omitting 'exclude' keys, using da values 66 | return {k: v for k, v in da.items() if k in db and not any(x in k for x in exclude) and v.shape == db[k].shape} 67 | 68 | 69 | def initialize_weights(model): 70 | for m in model.modules(): 71 | t = type(m) 72 | if t is nn.Conv2d: 73 | pass # nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') 74 | elif t is nn.BatchNorm2d: 75 | m.eps = 1e-3 76 | m.momentum = 0.03 77 | elif t in [nn.LeakyReLU, nn.ReLU, nn.ReLU6]: 78 | m.inplace = True 79 | 80 | 81 | def find_modules(model, mclass=nn.Conv2d): 82 | # Finds layer indices matching module class 'mclass' 83 | return [i for i, m in enumerate(model.module_list) if isinstance(m, mclass)] 84 | 85 | 86 | def sparsity(model): 87 | # Return global model sparsity 88 | a, b = 0., 0. 89 | for p in model.parameters(): 90 | a += p.numel() 91 | b += (p == 0).sum() 92 | return b / a 93 | 94 | 95 | def prune(model, amount=0.3): 96 | # Prune model to requested global sparsity 97 | import torch.nn.utils.prune as prune 98 | print('Pruning model... ', end='') 99 | for name, m in model.named_modules(): 100 | if isinstance(m, nn.Conv2d): 101 | prune.l1_unstructured(m, name='weight', amount=amount) # prune 102 | prune.remove(m, 'weight') # make permanent 103 | print(' %.3g global sparsity' % sparsity(model)) 104 | 105 | 106 | def fuse_conv_and_bn(conv, bn): 107 | # Fuse convolution and batchnorm layers https://tehnokv.com/posts/fusing-batchnorm-and-conv/ 108 | 109 | # init 110 | fusedconv = nn.Conv2d(conv.in_channels, 111 | conv.out_channels, 112 | kernel_size=conv.kernel_size, 113 | stride=conv.stride, 114 | padding=conv.padding, 115 | groups=conv.groups, 116 | bias=True).requires_grad_(False).to(conv.weight.device) 117 | 118 | # prepare filters 119 | w_conv = conv.weight.clone().view(conv.out_channels, -1) 120 | w_bn = torch.diag(bn.weight.div(torch.sqrt(bn.eps + bn.running_var))) 121 | fusedconv.weight.copy_(torch.mm(w_bn, w_conv).view(fusedconv.weight.size())) 122 | 123 | # prepare spatial bias 124 | b_conv = torch.zeros(conv.weight.size(0), device=conv.weight.device) if conv.bias is None else conv.bias 125 | b_bn = bn.bias - bn.weight.mul(bn.running_mean).div(torch.sqrt(bn.running_var + bn.eps)) 126 | fusedconv.bias.copy_(torch.mm(w_bn, b_conv.reshape(-1, 1)).reshape(-1) + b_bn) 127 | 128 | return fusedconv 129 | 130 | 131 | def model_info(model, verbose=False): 132 | # Plots a line-by-line description of a PyTorch model 133 | n_p = sum(x.numel() for x in model.parameters()) # number parameters 134 | n_g = sum(x.numel() for x in model.parameters() if x.requires_grad) # number gradients 135 | if verbose: 136 | print('%5s %40s %9s %12s %20s %10s %10s' % ('layer', 'name', 'gradient', 'parameters', 'shape', 'mu', 'sigma')) 137 | for i, (name, p) in enumerate(model.named_parameters()): 138 | name = name.replace('module_list.', '') 139 | print('%5g %40s %9s %12g %20s %10.3g %10.3g' % 140 | (i, name, p.requires_grad, p.numel(), list(p.shape), p.mean(), p.std())) 141 | 142 | try: # FLOPS 143 | from thop import profile 144 | flops = profile(deepcopy(model), inputs=(torch.zeros(1, 3, 64, 64),), verbose=False)[0] / 1E9 * 2 145 | fs = ', %.1f GFLOPS' % (flops * 100) # 640x640 FLOPS 146 | except: 147 | fs = '' 148 | 149 | logger.info( 150 | 'Model Summary: %g layers, %g parameters, %g gradients%s' % (len(list(model.parameters())), n_p, n_g, fs)) 151 | 152 | 153 | def load_classifier(name='resnet101', n=2): 154 | # Loads a pretrained model reshaped to n-class output 155 | model = torchvision.models.__dict__[name](pretrained=True) 156 | 157 | # ResNet model properties 158 | # input_size = [3, 224, 224] 159 | # input_space = 'RGB' 160 | # input_range = [0, 1] 161 | # mean = [0.485, 0.456, 0.406] 162 | # std = [0.229, 0.224, 0.225] 163 | 164 | # Reshape output to n classes 165 | filters = model.fc.weight.shape[1] 166 | model.fc.bias = nn.Parameter(torch.zeros(n), requires_grad=True) 167 | model.fc.weight = nn.Parameter(torch.zeros(n, filters), requires_grad=True) 168 | model.fc.out_features = n 169 | return model 170 | 171 | 172 | def scale_img(img, ratio=1.0, same_shape=False): # img(16,3,256,416), r=ratio 173 | # scales img(bs,3,y,x) by ratio 174 | if ratio == 1.0: 175 | return img 176 | else: 177 | h, w = img.shape[2:] 178 | s = (int(h * ratio), int(w * ratio)) # new size 179 | img = F.interpolate(img, size=s, mode='bilinear', align_corners=False) # resize 180 | if not same_shape: # pad/crop img 181 | gs = 32 # (pixels) grid size 182 | h, w = [math.ceil(x * ratio / gs) * gs for x in (h, w)] 183 | return F.pad(img, [0, w - s[1], 0, h - s[0]], value=0.447) # value = imagenet mean 184 | 185 | 186 | def copy_attr(a, b, include=(), exclude=()): 187 | # Copy attributes from b to a, options to only include [...] and to exclude [...] 188 | for k, v in b.__dict__.items(): 189 | if (len(include) and k not in include) or k.startswith('_') or k in exclude: 190 | continue 191 | else: 192 | setattr(a, k, v) 193 | 194 | 195 | class ModelEMA: 196 | """ Model Exponential Moving Average from https://github.com/rwightman/pytorch-image-models 197 | Keep a moving average of everything in the model state_dict (parameters and buffers). 198 | This is intended to allow functionality like 199 | https://www.tensorflow.org/api_docs/python/tf/train/ExponentialMovingAverage 200 | A smoothed version of the weights is necessary for some training schemes to perform well. 201 | This class is sensitive where it is initialized in the sequence of model init, 202 | GPU assignment and distributed training wrappers. 203 | """ 204 | 205 | def __init__(self, model, decay=0.9999, updates=0): 206 | # Create EMA 207 | self.ema = deepcopy(model.module if is_parallel(model) else model).eval() # FP32 EMA 208 | # if next(model.parameters()).device.type != 'cpu': 209 | # self.ema.half() # FP16 EMA 210 | self.updates = updates # number of EMA updates 211 | self.decay = lambda x: decay * (1 - math.exp(-x / 2000)) # decay exponential ramp (to help early epochs) 212 | for p in self.ema.parameters(): 213 | p.requires_grad_(False) 214 | 215 | def update(self, model): 216 | # Update EMA parameters 217 | with torch.no_grad(): 218 | self.updates += 1 219 | d = self.decay(self.updates) 220 | 221 | msd = model.module.state_dict() if is_parallel(model) else model.state_dict() # model state_dict 222 | for k, v in self.ema.state_dict().items(): 223 | if v.dtype.is_floating_point: 224 | v *= d 225 | v += (1. - d) * msd[k].detach() 226 | 227 | def update_attr(self, model, include=(), exclude=('process_group', 'reducer')): 228 | # Update EMA attributes 229 | copy_attr(self.ema, model, include, exclude) 230 | -------------------------------------------------------------------------------- /utils/yolo_anchors.txt: -------------------------------------------------------------------------------- 1 | 23,11, 28,8, 36,12, 52,14, 65,20, 86,24, 130,28, 203,41, 340,65 -------------------------------------------------------------------------------- /weights/download_weights.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Download common models 3 | 4 | python -c " 5 | from utils.google_utils import *; 6 | attempt_download('weights/yolov5s.pt'); 7 | attempt_download('weights/yolov5m.pt'); 8 | attempt_download('weights/yolov5l.pt'); 9 | attempt_download('weights/yolov5x.pt') 10 | " 11 | --------------------------------------------------------------------------------