diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..9cecc1d --- /dev/null +++ b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + {one line to give the program's name and a brief idea of what it does.} + Copyright (C) {year} {name of author} + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + {project} Copyright (C) {year} {fullname} + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +. diff --git a/README.md b/README.md index d46d5a8..beb1cf8 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,277 @@ -# YOLO_Object_Detection -This is the code for "YOLO Object Detection" by Siraj Raval on Youtube +## Intro + +[![Build Status](https://travis-ci.org/thtrieu/darkflow.svg?branch=master)](https://travis-ci.org/thtrieu/darkflow) [![codecov](https://codecov.io/gh/thtrieu/darkflow/branch/master/graph/badge.svg)](https://codecov.io/gh/thtrieu/darkflow) + +Real-time object detection and classification. Paper: [version 1](https://arxiv.org/pdf/1506.02640.pdf), [version 2](https://arxiv.org/pdf/1612.08242.pdf). + +Read more about YOLO (in darknet) and download weight files [here](http://pjreddie.com/darknet/yolo/). In case the weight file cannot be found, I uploaded some of mine [here](https://drive.google.com/drive/folders/0B1tW_VtY7onidEwyQ2FtQVplWEU), which include `yolo-full` and `yolo-tiny` of v1.0, `tiny-yolo-v1.1` of v1.1 and `yolo`, `tiny-yolo-voc` of v2. + + +Click on this image to see demo from yolov2: + +[![img](preview.png)](http://i.imgur.com/EyZZKAA.gif) + +## Dependencies + +Python3, tensorflow 1.0, numpy, opencv 3. + +### Getting started + +You can choose _one_ of the following three ways to get started with darkflow. + +1. Just build the Cython extensions in place. NOTE: If installing this way you will have to use `./flow` in the cloned darkflow directory instead of `flow` as darkflow is not installed globally. + ``` + python3 setup.py build_ext --inplace + ``` + +2. Let pip install darkflow globally in dev mode (still globally accessible, but changes to the code immediately take effect) + ``` + pip install -e . + ``` + +3. Install with pip globally + ``` + pip install . + ``` + +## Update + +**Android demo on Tensorflow's** [here](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/android/src/org/tensorflow/demo/TensorFlowYoloDetector.java) + +**I am looking for help:** + - `help wanted` labels in issue track + +## Parsing the annotations + +Skip this if you are not training or fine-tuning anything (you simply want to forward flow a trained net) + +For example, if you want to work with only 3 classes `tvmonitor`, `person`, `pottedplant`; edit `labels.txt` as follows + +``` +tvmonitor +person +pottedplant +``` + +And that's it. `darkflow` will take care of the rest. You can also set darkflow to load from a custom labels file with the `--labels` flag (i.e. `--labels myOtherLabelsFile.txt`). This can be helpful when working with multiple models with different sets of output labels. When this flag is not set, darkflow will load from `labels.txt` by default (unless you are using one of the recognized `.cfg` files designed for the COCO or VOC dataset - then the labels file will be ignored and the COCO or VOC labels will be loaded). + +## Design the net + +Skip this if you are working with one of the original configurations since they are already there. Otherwise, see the following example: + +```python +... + +[convolutional] +batch_normalize = 1 +size = 3 +stride = 1 +pad = 1 +activation = leaky + +[maxpool] + +[connected] +output = 4096 +activation = linear + +... +``` + +## Flowing the graph using `flow` + +```bash +# Have a look at its options +flow --h +``` + +First, let's take a closer look at one of a very useful option `--load` + +```bash +# 1. Load yolo-tiny.weights +flow --model cfg/yolo-tiny.cfg --load bin/yolo-tiny.weights + +# 2. To completely initialize a model, leave the --load option +flow --model cfg/yolo-new.cfg + +# 3. It is useful to reuse the first identical layers of tiny for `yolo-new` +flow --model cfg/yolo-new.cfg --load bin/yolo-tiny.weights +# this will print out which layers are reused, which are initialized +``` + +All input images from default folder `sample_img/` are flowed through the net and predictions are put in `sample_img/out/`. We can always specify more parameters for such forward passes, such as detection threshold, batch size, images folder, etc. + +```bash +# Forward all images in sample_img/ using tiny yolo and 100% GPU usage +flow --imgdir sample_img/ --model cfg/yolo-tiny.cfg --load bin/yolo-tiny.weights --gpu 1.0 +``` +json output can be generated with descriptions of the pixel location of each bounding box and the pixel location. Each prediction is stored in the `sample_img/out` folder by default. An example json array is shown below. +```bash +# Forward all images in sample_img/ using tiny yolo and JSON output. +flow --imgdir sample_img/ --model cfg/yolo-tiny.cfg --load bin/yolo-tiny.weights --json +``` +JSON output: +```json +[{"label":"person", "confidence": 0.56, "topleft": {"x": 184, "y": 101}, "bottomright": {"x": 274, "y": 382}}, +{"label": "dog", "confidence": 0.32, "topleft": {"x": 71, "y": 263}, "bottomright": {"x": 193, "y": 353}}, +{"label": "horse", "confidence": 0.76, "topleft": {"x": 412, "y": 109}, "bottomright": {"x": 592,"y": 337}}] +``` + - label: self explanatory + - confidence: somewhere between 0 and 1 (how confident yolo is about that detection) + - topleft: pixel coordinate of top left corner of box. + - bottomright: pixel coordinate of bottom right corner of box. + +## Training new model + +Training is simple as you only have to add option `--train`. Training set and annotation will be parsed if this is the first time a new configuration is trained. To point to training set and annotations, use option `--dataset` and `--annotation`. A few examples: + +```bash +# Initialize yolo-new from yolo-tiny, then train the net on 100% GPU: +flow --model cfg/yolo-new.cfg --load bin/yolo-tiny.weights --train --gpu 1.0 + +# Completely initialize yolo-new and train it with ADAM optimizer +flow --model cfg/yolo-new.cfg --train --trainer adam +``` + +During training, the script will occasionally save intermediate results into Tensorflow checkpoints, stored in `ckpt/`. To resume to any checkpoint before performing training/testing, use `--load [checkpoint_num]` option, if `checkpoint_num < 0`, `darkflow` will load the most recent save by parsing `ckpt/checkpoint`. + +```bash +# Resume the most recent checkpoint for training +flow --train --model cfg/yolo-new.cfg --load -1 + +# Test with checkpoint at step 1500 +flow --model cfg/yolo-new.cfg --load 1500 + +# Fine tuning yolo-tiny from the original one +flow --train --model cfg/yolo-tiny.cfg --load bin/yolo-tiny.weights +``` + +Example of training on Pascal VOC 2007: +```bash +# Download the Pascal VOC dataset: +curl -O https://pjreddie.com/media/files/VOCtest_06-Nov-2007.tar +tar xf VOCtest_06-Nov-2007.tar + +# An example of the Pascal VOC annotation format: +vim VOCdevkit/VOC2007/Annotations/000001.xml + +# Train the net on the Pascal dataset: +flow --model cfg/yolo-new.cfg --train --dataset "~/VOCdevkit/VOC2007/JPEGImages" --annotation "~/VOCdevkit/VOC2007/Annotations" +``` + +### Training on your own dataset + +*The steps below assume we want to use tiny YOLO and our dataset has 3 classes* + +1. Create a copy of the configuration file `tiny-yolo-voc.cfg` and rename it according to your preference `tiny-yolo-voc-3c.cfg` (It is crucial that you leave the original `tiny-yolo-voc.cfg` file unchanged, see below for explanation). + +2. In `tiny-yolo-voc-3c.cfg`, change classes in the [region] layer (the last layer) to the number of classes you are going to train for. In our case, classes are set to 3. + + ```python + ... + + [region] + anchors = 1.08,1.19, 3.42,4.41, 6.63,11.38, 9.42,5.11, 16.62,10.52 + bias_match=1 + classes=3 + coords=4 + num=5 + softmax=1 + + ... + ``` + +3. In `tiny-yolo-voc-3c.cfg`, change filters in the [convolutional] layer (the second to last layer) to num * (classes + 5). In our case, num is 5 and classes are 3 so 5 * (3 + 5) = 40 therefore filters are set to 40. + + ```python + ... + + [convolutional] + size=1 + stride=1 + pad=1 + filters=40 + activation=linear + + [region] + anchors = 1.08,1.19, 3.42,4.41, 6.63,11.38, 9.42,5.11, 16.62,10.52 + + ... + ``` + +4. Change `labels.txt` to include the label(s) you want to train on (number of labels should be the same as the number of classes you set in `tiny-yolo-voc-3c.cfg` file). In our case, `labels.txt` will contain 3 labels. + + ``` + label1 + label2 + label3 + ``` +5. Reference the `tiny-yolo-voc-3c.cfg` model when you train. + + `flow --model cfg/tiny-yolo-voc-3c.cfg --load bin/tiny-yolo-voc.weights --train --annotation train/Annotations --dataset train/Images` + + +* Why should I leave the original `tiny-yolo-voc.cfg` file unchanged? + + When darkflow sees you are loading `tiny-yolo-voc.weights` it will look for `tiny-yolo-voc.cfg` in your cfg/ folder and compare that configuration file to the new one you have set with `--model cfg/tiny-yolo-voc-3c.cfg`. In this case, every layer will have the same exact number of weights except for the last two, so it will load the weights into all layers up to the last two because they now contain different number of weights. + + +## Camera/video file demo + +For a demo that entirely runs on the CPU: + +```bash +flow --model cfg/yolo-new.cfg --load bin/yolo-new.weights --demo videofile.avi +``` + +For a demo that runs 100% on the GPU: + +```bash +flow --model cfg/yolo-new.cfg --load bin/yolo-new.weights --demo videofile.avi --gpu 1.0 +``` + +To use your webcam/camera, simply replace `videofile.avi` with keyword `camera`. + +To save a video with predicted bounding box, add `--saveVideo` option. + +## Using darkflow from another python application + +Please note that `return_predict(img)` must take an `numpy.ndarray`. Your image must be loaded beforehand and passed to `return_predict(img)`. Passing the file path won't work. + +Result from `return_predict(img)` will be a list of dictionaries representing each detected object's values in the same format as the JSON output listed above. + +```python +from darkflow.net.build import TFNet +import cv2 + +options = {"model": "cfg/yolo.cfg", "load": "bin/yolo.weights", "threshold": 0.1} + +tfnet = TFNet(options) + +imgcv = cv2.imread("./sample_img/dog.jpg") +result = tfnet.return_predict(imgcv) +print(result) +``` + + +## Save the built graph to a protobuf file (`.pb`) + +```bash +## Saving the lastest checkpoint to protobuf file +flow --model cfg/yolo-new.cfg --load -1 --savepb + +## Saving graph and weights to protobuf file +flow --model cfg/yolo.cfg --load bin/yolo.weights --savepb +``` +When saving the `.pb` file, a `.meta` file will also be generated alongside it. This `.meta` file is a JSON dump of everything in the `meta` dictionary that contains information nessecary for post-processing such as `anchors` and `labels`. This way, everything you need to make predictions from the graph and do post processing is contained in those two files - no need to have the `.cfg` or any labels file tagging along. + +The created `.pb` file can be used to migrate the graph to mobile devices (JAVA / C++ / Objective-C++). The name of input tensor and output tensor are respectively `'input'` and `'output'`. For further usage of this protobuf file, please refer to the official documentation of `Tensorflow` on C++ API [_here_](https://www.tensorflow.org/versions/r0.9/api_docs/cc/index.html). To run it on, say, iOS application, simply add the file to Bundle Resources and update the path to this file inside source code. + +Also, darkflow supports loading from a `.pb` and `.meta` file for generating predictions (instead of loading from a `.cfg` and checkpoint or `.weights`). +```bash +## Forward images in sample_img for predictions based on protobuf file +flow --pbLoad built_graph/yolo.pb --metaLoad built_graph/yolo.meta --imgdir sample_img/ +``` +If you'd like to load a `.pb` and `.meta` file when using `return_predict()` you can set the `"pbLoad"` and `"metaLoad"` options in place of the `"model"` and `"load"` options you would normally set. + +That's all. diff --git a/cfg/coco.names b/cfg/coco.names new file mode 100644 index 0000000..ca76c80 --- /dev/null +++ b/cfg/coco.names @@ -0,0 +1,80 @@ +person +bicycle +car +motorbike +aeroplane +bus +train +truck +boat +traffic light +fire hydrant +stop sign +parking meter +bench +bird +cat +dog +horse +sheep +cow +elephant +bear +zebra +giraffe +backpack +umbrella +handbag +tie +suitcase +frisbee +skis +snowboard +sports ball +kite +baseball bat +baseball glove +skateboard +surfboard +tennis racket +bottle +wine glass +cup +fork +knife +spoon +bowl +banana +apple +sandwich +orange +broccoli +carrot +hot dog +pizza +donut +cake +chair +sofa +pottedplant +bed +diningtable +toilet +tvmonitor +laptop +mouse +remote +keyboard +cell phone +microwave +oven +toaster +sink +refrigerator +book +clock +vase +scissors +teddy bear +hair drier +toothbrush diff --git a/cfg/extraction.cfg b/cfg/extraction.cfg new file mode 100644 index 0000000..94e1067 --- /dev/null +++ b/cfg/extraction.cfg @@ -0,0 +1,206 @@ +[net] +batch=128 +subdivisions=1 +height=224 +width=224 +max_crop=320 +channels=3 +momentum=0.9 +decay=0.0005 + +learning_rate=0.1 +policy=poly +power=4 +max_batches=1600000 + +[convolutional] +batch_normalize=1 +filters=64 +size=7 +stride=2 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=192 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1000 +size=1 +stride=1 +pad=1 +activation=leaky + +[avgpool] + +[softmax] +groups=1 + +[cost] +type=sse + diff --git a/cfg/extraction.conv.cfg b/cfg/extraction.conv.cfg new file mode 100644 index 0000000..2a7d09e --- /dev/null +++ b/cfg/extraction.conv.cfg @@ -0,0 +1,179 @@ +[net] +batch=1 +subdivisions=1 +height=256 +width=256 +channels=3 +momentum=0.9 +decay=0.0005 + +learning_rate=0.5 +policy=poly +power=6 +max_batches=500000 + +[convolutional] +filters=64 +size=7 +stride=2 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=192 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[avgpool] + +[connected] +output=1000 +activation=leaky + +[softmax] +groups=1 + diff --git a/cfg/tiny-yolo-4c.cfg b/cfg/tiny-yolo-4c.cfg new file mode 100644 index 0000000..881d9bf --- /dev/null +++ b/cfg/tiny-yolo-4c.cfg @@ -0,0 +1,134 @@ +[net] +batch=64 +subdivisions=8 +width=416 +height=416 +channels=3 +momentum=0.9 +decay=0.0005 +angle=0 +saturation = 1.5 +exposure = 1.5 +hue=.1 + +learning_rate=0.001 +max_batches = 40100 +policy=steps +steps=-1,100,20000,30000 +scales=.1,10,.1,.1 + +[convolutional] +batch_normalize=1 +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=1 + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +########### + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=1 +stride=1 +pad=1 +filters=45 +activation=linear + +[region] +anchors = 1.08,1.19, 3.42,4.41, 6.63,11.38, 9.42,5.11, 16.62,10.52 +bias_match=1 +classes=4 +coords=4 +num=5 +softmax=1 +jitter=.2 +rescore=1 + +object_scale=5 +noobject_scale=1 +class_scale=1 +coord_scale=1 + +absolute=1 +thresh=.6 +random=1 diff --git a/cfg/tiny-yolo-voc.cfg b/cfg/tiny-yolo-voc.cfg new file mode 100644 index 0000000..032b747 --- /dev/null +++ b/cfg/tiny-yolo-voc.cfg @@ -0,0 +1,134 @@ +[net] +batch=64 +subdivisions=8 +width=416 +height=416 +channels=3 +momentum=0.9 +decay=0.0005 +angle=0 +saturation = 1.5 +exposure = 1.5 +hue=.1 + +learning_rate=0.001 +max_batches = 40100 +policy=steps +steps=-1,100,20000,30000 +scales=.1,10,.1,.1 + +[convolutional] +batch_normalize=1 +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=1 + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +########### + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=1 +stride=1 +pad=1 +filters=125 +activation=linear + +[region] +anchors = 1.08,1.19, 3.42,4.41, 6.63,11.38, 9.42,5.11, 16.62,10.52 +bias_match=1 +classes=20 +coords=4 +num=5 +softmax=1 +jitter=.2 +rescore=1 + +object_scale=5 +noobject_scale=1 +class_scale=1 +coord_scale=1 + +absolute=1 +thresh = .5 +random=1 diff --git a/cfg/tiny-yolo.cfg b/cfg/tiny-yolo.cfg new file mode 100644 index 0000000..5580098 --- /dev/null +++ b/cfg/tiny-yolo.cfg @@ -0,0 +1,134 @@ +[net] +batch=64 +subdivisions=8 +width=416 +height=416 +channels=3 +momentum=0.9 +decay=0.0005 +angle=0 +saturation = 1.5 +exposure = 1.5 +hue=.1 + +learning_rate=0.001 +max_batches = 120000 +policy=steps +steps=-1,100,80000,100000 +scales=.1,10,.1,.1 + +[convolutional] +batch_normalize=1 +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=1 + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +########### + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=1 +stride=1 +pad=1 +filters=425 +activation=linear + +[region] +anchors = 0.738768,0.874946, 2.42204,2.65704, 4.30971,7.04493, 10.246,4.59428, 12.6868,11.8741 +bias_match=1 +classes=80 +coords=4 +num=5 +softmax=1 +jitter=.2 +rescore=1 + +object_scale=5 +noobject_scale=1 +class_scale=1 +coord_scale=1 + +absolute=1 +thresh = .6 +random=1 diff --git a/cfg/v1.1/person-bottle.cfg b/cfg/v1.1/person-bottle.cfg new file mode 100644 index 0000000..e5c0a25 --- /dev/null +++ b/cfg/v1.1/person-bottle.cfg @@ -0,0 +1,128 @@ +[net] +batch=64 +subdivisions=2 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +saturation=.75 +exposure=.75 +hue = .1 + +learning_rate=0.0005 +policy=steps +steps=200,400,600,800,20000,30000 +scales=2.5,2,2,2,.1,.1 +max_batches = 40000 + +[convolutional] +batch_normalize=1 +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=256 +activation=leaky + +[select] +old_output=1470 +keep=4,14/20 +bins=49 +output=588 +activation=linear + +[detection] +classes=2 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 + +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 \ No newline at end of file diff --git a/cfg/v1.1/tiny-coco.cfg b/cfg/v1.1/tiny-coco.cfg new file mode 100644 index 0000000..e58c73a --- /dev/null +++ b/cfg/v1.1/tiny-coco.cfg @@ -0,0 +1,125 @@ +[net] +batch=64 +subdivisions=2 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +hue = .1 +saturation=.75 +exposure=.75 + +learning_rate=0.0005 +policy=steps +steps=200,400,600,800,100000,150000 +scales=2.5,2,2,2,.1,.1 +max_batches = 200000 + +[convolutional] +batch_normalize=1 +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=256 +activation=leaky + +[connected] +output= 4655 +activation=linear + +[detection] +classes=80 +coords=4 +rescore=1 +side=7 +num=3 +softmax=0 +sqrt=1 +jitter=.2 + +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 diff --git a/cfg/v1.1/tiny-yolo-4c.cfg b/cfg/v1.1/tiny-yolo-4c.cfg new file mode 100644 index 0000000..22d862e --- /dev/null +++ b/cfg/v1.1/tiny-yolo-4c.cfg @@ -0,0 +1,128 @@ +[net] +batch=64 +subdivisions=2 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +saturation=.75 +exposure=.75 +hue = .1 + +learning_rate=0.0005 +policy=steps +steps=200,400,600,800,20000,30000 +scales=2.5,2,2,2,.1,.1 +max_batches = 40000 + +[convolutional] +batch_normalize=1 +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=256 +activation=leaky + +[select] +old_output=1470 +keep=8,14,15,19/20 +bins=49 +output=686 +activation=linear + +[detection] +classes=4 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 + +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 \ No newline at end of file diff --git a/cfg/v1.1/tiny-yolov1.cfg b/cfg/v1.1/tiny-yolov1.cfg new file mode 100644 index 0000000..ac4b346 --- /dev/null +++ b/cfg/v1.1/tiny-yolov1.cfg @@ -0,0 +1,126 @@ +[net] +batch=64 +subdivisions=2 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +saturation=.75 +exposure=.75 +hue = .1 + +learning_rate=0.0005 +policy=steps +steps=200,400,600,800,20000,30000 +scales=2.5,2,2,2,.1,.1 +max_batches = 40000 + +[convolutional] +batch_normalize=1 +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=256 +activation=leaky + +[connected] +output= 1470 +activation=linear + +[detection] +classes=20 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 + +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 + diff --git a/cfg/v1.1/yolo-coco.cfg b/cfg/v1.1/yolo-coco.cfg new file mode 100644 index 0000000..ed3f2d6 --- /dev/null +++ b/cfg/v1.1/yolo-coco.cfg @@ -0,0 +1,255 @@ +[net] +batch=64 +subdivisions=4 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +hue = .1 +saturation=.75 +exposure=.75 + +learning_rate=0.0005 +policy=steps +steps=200,400,600,800,100000,150000 +scales=2.5,2,2,2,.1,.1 +max_batches = 200000 + +[convolutional] +batch_normalize=1 +filters=64 +size=7 +stride=2 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=192 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +####### + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=2 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[local] +size=3 +stride=1 +pad=1 +filters=256 +activation=leaky + +[connected] +output= 4655 +activation=linear + +[detection] +classes=80 +coords=4 +rescore=1 +side=7 +num=3 +softmax=0 +sqrt=1 +jitter=.2 + +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 + diff --git a/cfg/v1.1/yolov1.cfg b/cfg/v1.1/yolov1.cfg new file mode 100644 index 0000000..c4f415c --- /dev/null +++ b/cfg/v1.1/yolov1.cfg @@ -0,0 +1,257 @@ +[net] +batch=1 +subdivisions=1 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 +saturation=1.5 +exposure=1.5 +hue=.1 + +learning_rate=0.0005 +policy=steps +steps=200,400,600,20000,30000 +scales=2.5,2,2,.1,.1 +max_batches = 40000 + +[convolutional] +batch_normalize=1 +filters=64 +size=7 +stride=2 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=192 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +####### + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=2 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[local] +size=3 +stride=1 +pad=1 +filters=256 +activation=leaky + +[dropout] +probability=.5 + +[connected] +output= 1715 +activation=linear + +[detection] +classes=20 +coords=4 +rescore=1 +side=7 +num=3 +softmax=0 +sqrt=1 +jitter=.2 + +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 + diff --git a/cfg/v1/tiny-old.profile b/cfg/v1/tiny-old.profile new file mode 100644 index 0000000..0799061 Binary files /dev/null and b/cfg/v1/tiny-old.profile differ diff --git a/cfg/v1/tiny.profile b/cfg/v1/tiny.profile new file mode 100644 index 0000000..e6635ae --- /dev/null +++ b/cfg/v1/tiny.profile @@ -0,0 +1 @@ +€]q]qa. \ No newline at end of file diff --git a/cfg/v1/yolo-2c.cfg b/cfg/v1/yolo-2c.cfg new file mode 100644 index 0000000..bde5bce --- /dev/null +++ b/cfg/v1/yolo-2c.cfg @@ -0,0 +1,141 @@ +[net] +batch=64 +subdivisions=64 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +learning_rate=0.0001 +policy=steps +steps=20,40,60,80,20000,30000 +scales=5,5,2,2,.1,.1 +max_batches = 40000 + +[crop] +crop_width=448 +crop_height=448 +flip=0 +angle=0 +saturation = 1.5 +exposure = 1.5 + +[convolutional] +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[connected] +output=256 +activation=linear + +[connected] +output=4096 +activation=leaky + +[dropout] +probability=.5 + +[select] +old_output=1470 +keep=14,19/20 +bins=49 +output=588 +activation=linear + +[detection] +classes=2 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 \ No newline at end of file diff --git a/cfg/v1/yolo-4c.cfg b/cfg/v1/yolo-4c.cfg new file mode 100644 index 0000000..ecf46d2 --- /dev/null +++ b/cfg/v1/yolo-4c.cfg @@ -0,0 +1,237 @@ +[net] +batch=64 +subdivisions=64 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +learning_rate=0.001 +policy=steps +steps=200,400,600,20000,30000 +scales=2.5,2,2,.1,.1 +max_batches = 40000 + +[crop] +crop_width=448 +crop_height=448 +flip=0 +angle=0 +saturation = 1.5 +exposure = 1.5 + +[convolutional] +filters=64 +size=7 +stride=2 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=192 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +####### + +[convolutional] +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=3 +stride=2 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[connected] +output=4096 +activation=leaky + +[dropout] +probability=.5 + +[select] +old_output=1470 +keep=8,14,15,19/20 +bins=49 +output=686 +activation=linear + +[detection] +classes=4 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 + +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 \ No newline at end of file diff --git a/cfg/v1/yolo-full.cfg b/cfg/v1/yolo-full.cfg new file mode 100644 index 0000000..9eb08d9 --- /dev/null +++ b/cfg/v1/yolo-full.cfg @@ -0,0 +1,234 @@ +[net] +batch=64 +subdivisions=64 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +learning_rate=0.001 +policy=steps +steps=200,400,600,20000,30000 +scales=2.5,2,2,.1,.1 +max_batches = 40000 + +[crop] +crop_width=448 +crop_height=448 +flip=0 +angle=0 +saturation = 1.5 +exposure = 1.5 + +[convolutional] +filters=64 +size=7 +stride=2 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=192 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +####### + +[convolutional] +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=3 +stride=2 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[connected] +output=4096 +activation=leaky + +[dropout] +probability=.5 + +[connected] +output= 1470 +activation=linear + +[detection] +classes=20 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 + +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 \ No newline at end of file diff --git a/cfg/v1/yolo-small.cfg b/cfg/v1/yolo-small.cfg new file mode 100644 index 0000000..2a84485 --- /dev/null +++ b/cfg/v1/yolo-small.cfg @@ -0,0 +1,239 @@ +[net] +batch=64 +subdivisions=64 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +learning_rate=0.001 +policy=steps +steps=200,400,600,20000,30000 +scales=2.5,2,2,.1,.1 +max_batches = 40000 + +[crop] +crop_width=448 +crop_height=448 +flip=0 +angle=0 +saturation = 1.5 +exposure = 1.5 + +[convolutional] +filters=64 +size=7 +stride=2 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=192 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +####### + +[convolutional] +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=3 +stride=2 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[connected] +output=512 +activation=leaky + +[connected] +output=4096 +activation=leaky + +[dropout] +probability=.5 + +[connected] +output= 1470 +activation=linear + +[detection] +classes=20 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 + +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 + diff --git a/cfg/v1/yolo-tiny-extract.cfg b/cfg/v1/yolo-tiny-extract.cfg new file mode 100644 index 0000000..cddf222 --- /dev/null +++ b/cfg/v1/yolo-tiny-extract.cfg @@ -0,0 +1,175 @@ +[net] +batch=64 +subdivisions=64 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +learning_rate=0.0001 +policy=steps +steps=20,40,60,80,20000,30000 +scales=5,5,2,2,.1,.1 +max_batches = 40000 + +[crop] +crop_width=448 +crop_height=448 +flip=0 +angle=0 +saturation = 1.5 +exposure = 1.5 + +[conv-extract] +profile=cfg/v1/tiny.profile +input=-1 +output=0 +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny.profile +input=0 +output=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny.profile +input=1 +output=2 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny.profile +input=2 +output=3 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny.profile +input=3 +output=4 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny.profile +input=4 +output=5 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny.profile +input=5 +output=6 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[conv-extract] +profile=cfg/v1/tiny.profile +input=6 +output=7 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[conv-extract] +profile=cfg/v1/tiny.profile +input=7 +output=8 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[extract] +profile=cfg/v1/tiny.profile +input=8 +output=9 +old=7,7,1024,256 +activation=linear + +[extract] +profile=cfg/v1/tiny.profile +input=9 +output=10 +old=256,4096 +activation=leaky + +[dropout] +probability=1. + +[select] +input=cfg/v1/tiny.profile,10 +old_output=1470 +keep=8,14,15,19/20 +bins=49 +output=686 +activation=linear + +[detection] +classes=4 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 \ No newline at end of file diff --git a/cfg/v1/yolo-tiny-extract_.cfg b/cfg/v1/yolo-tiny-extract_.cfg new file mode 100644 index 0000000..21e250e --- /dev/null +++ b/cfg/v1/yolo-tiny-extract_.cfg @@ -0,0 +1,177 @@ +[net] +batch=64 +subdivisions=64 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +learning_rate=0.0001 +policy=steps +steps=20,40,60,80,20000,30000 +scales=5,5,2,2,.1,.1 +max_batches = 40000 + +[crop] +crop_width=448 +crop_height=448 +flip=0 +angle=0 +saturation = 1.5 +exposure = 1.5 + +[conv-extract] +profile=cfg/v1/tiny-old.profile +input=-1 +output=0 +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny-old.profile +input=0 +output=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny-old.profile +input=1 +output=2 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny-old.profile +input=2 +output=3 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny-old.profile +input=3 +output=4 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny-old.profile +input=4 +output=5 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[conv-extract] +profile=cfg/v1/tiny-old.profile +input=5 +output=6 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[conv-extract] +profile=cfg/v1/tiny-old.profile +input=6 +output=7 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[conv-extract] +profile=cfg/v1/tiny-old.profile +input=7 +output=8 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[extract] +profile=cfg/v1/tiny-old.profile +input=8 +output=9 +old=7,7,1024,256 +activation=linear + +[extract] +profile=cfg/v1/tiny-old.profile +input=9 +output=10 +old=256,4096 +activation=leaky + +[dropout] +probability=1. + +[select] +input=cfg/v1/tiny-old.profile,10 +old_output=1470 +keep=8,14,15,19/20 +bins=49 +output=686 +activation=linear + +[detection] +classes=4 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 +object_scale=2.5 +noobject_scale=2 +class_scale=2.5 +coord_scale=5 + +save=11250 \ No newline at end of file diff --git a/cfg/v1/yolo-tiny.cfg b/cfg/v1/yolo-tiny.cfg new file mode 100644 index 0000000..8d139e8 --- /dev/null +++ b/cfg/v1/yolo-tiny.cfg @@ -0,0 +1,138 @@ +[net] +batch=64 +subdivisions=64 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +learning_rate=0.0001 +policy=steps +steps=20,40,60,80,20000,30000 +scales=5,5,2,2,.1,.1 +max_batches = 40000 + +[crop] +crop_width=448 +crop_height=448 +flip=0 +angle=0 +saturation = 1.5 +exposure = 1.5 + +[convolutional] +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[connected] +output=256 +activation=linear + +[connected] +output=4096 +activation=leaky + +[dropout] +probability=.5 + +[connected] +output= 1470 +activation=linear + +[detection] +classes=20 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 \ No newline at end of file diff --git a/cfg/v1/yolo-tiny4c.cfg b/cfg/v1/yolo-tiny4c.cfg new file mode 100644 index 0000000..21357ac --- /dev/null +++ b/cfg/v1/yolo-tiny4c.cfg @@ -0,0 +1,141 @@ +[net] +batch=64 +subdivisions=64 +height=448 +width=448 +channels=3 +momentum=0.9 +decay=0.0005 + +learning_rate=0.0001 +policy=steps +steps=20,40,60,80,20000,30000 +scales=5,5,2,2,.1,.1 +max_batches = 40000 + +[crop] +crop_width=448 +crop_height=448 +flip=0 +angle=0 +saturation = 1.5 +exposure = 1.5 + +[convolutional] +filters=16 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[connected] +output=256 +activation=linear + +[connected] +output=4096 +activation=leaky + +[dropout] +probability=.5 + +[select] +old_output=1470 +keep=8,14,15,19/20 +bins=49 +output=686 +activation=linear + +[detection] +classes=4 +coords=4 +rescore=1 +side=7 +num=2 +softmax=0 +sqrt=1 +jitter=.2 +object_scale=1 +noobject_scale=.5 +class_scale=1 +coord_scale=5 \ No newline at end of file diff --git a/cfg/yolo-voc.cfg b/cfg/yolo-voc.cfg new file mode 100644 index 0000000..ceb3f2a --- /dev/null +++ b/cfg/yolo-voc.cfg @@ -0,0 +1,244 @@ +[net] +batch=64 +subdivisions=8 +height=416 +width=416 +channels=3 +momentum=0.9 +decay=0.0005 +angle=0 +saturation = 1.5 +exposure = 1.5 +hue=.1 + +learning_rate=0.0001 +max_batches = 45000 +policy=steps +steps=100,25000,35000 +scales=10,.1,.1 + +[convolutional] +batch_normalize=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=64 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + + +####### + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[route] +layers=-9 + +[reorg] +stride=2 + +[route] +layers=-1,-3 + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=1 +stride=1 +pad=1 +filters=125 +activation=linear + +[region] +anchors = 1.08,1.19, 3.42,4.41, 6.63,11.38, 9.42,5.11, 16.62,10.52 +bias_match=1 +classes=20 +coords=4 +num=5 +softmax=1 +jitter=.2 +rescore=1 + +object_scale=5 +noobject_scale=1 +class_scale=1 +coord_scale=1 + +absolute=1 +thresh = .6 +random=0 diff --git a/cfg/yolo.cfg b/cfg/yolo.cfg new file mode 100644 index 0000000..343bb25 --- /dev/null +++ b/cfg/yolo.cfg @@ -0,0 +1,258 @@ +[net] +# Testing +batch=1 +subdivisions=1 +# Training +# batch=64 +# subdivisions=8 +width=608 +height=608 +channels=3 +momentum=0.9 +decay=0.0005 +angle=0 +saturation = 1.5 +exposure = 1.5 +hue=.1 + +learning_rate=0.001 +burn_in=1000 +max_batches = 500200 +policy=steps +steps=400000,450000 +scales=.1,.1 + +[convolutional] +batch_normalize=1 +filters=32 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=64 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=64 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=128 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=128 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=256 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=3 +stride=1 +pad=1 +activation=leaky + +[maxpool] +size=2 +stride=2 + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=512 +size=1 +stride=1 +pad=1 +activation=leaky + +[convolutional] +batch_normalize=1 +filters=1024 +size=3 +stride=1 +pad=1 +activation=leaky + + +####### + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[route] +layers=-9 + +[convolutional] +batch_normalize=1 +size=1 +stride=1 +pad=1 +filters=64 +activation=leaky + +[reorg] +stride=2 + +[route] +layers=-1,-4 + +[convolutional] +batch_normalize=1 +size=3 +stride=1 +pad=1 +filters=1024 +activation=leaky + +[convolutional] +size=1 +stride=1 +pad=1 +filters=425 +activation=linear + + +[region] +anchors = 0.57273, 0.677385, 1.87446, 2.06253, 3.33843, 5.47434, 7.88282, 3.52778, 9.77052, 9.16828 +bias_match=1 +classes=80 +coords=4 +num=5 +softmax=1 +jitter=.3 +rescore=1 + +object_scale=5 +noobject_scale=1 +class_scale=1 +coord_scale=1 + +absolute=1 +thresh = .1 +random=1 diff --git a/darkflow/cli.py b/darkflow/cli.py new file mode 100644 index 0000000..6d13d0a --- /dev/null +++ b/darkflow/cli.py @@ -0,0 +1,37 @@ +from .defaults import argHandler #Import the default arguments +import os +from .net.build import TFNet + +def cliHandler(args): + FLAGS = argHandler() + FLAGS.setDefaults() + FLAGS.parseArgs(args) + + # make sure all necessary dirs exist + def _get_dir(dirs): + for d in dirs: + this = os.path.abspath(os.path.join(os.path.curdir, d)) + if not os.path.exists(this): os.makedirs(this) + _get_dir([FLAGS.imgdir, FLAGS.binary, FLAGS.backup, + os.path.join(FLAGS.imgdir,'out'), FLAGS.summary]) + + # fix FLAGS.load to appropriate type + try: FLAGS.load = int(FLAGS.load) + except: pass + + tfnet = TFNet(FLAGS) + + if FLAGS.demo: + tfnet.camera() + exit('Demo stopped, exit.') + + if FLAGS.train: + print('Enter training ...'); tfnet.train() + if not FLAGS.savepb: + exit('Training finished, exit.') + + if FLAGS.savepb: + print('Rebuild a constant version ...') + tfnet.savepb(); exit('Done') + + tfnet.predict() diff --git a/darkflow/cython_utils/cy_yolo2_findboxes.pyx b/darkflow/cython_utils/cy_yolo2_findboxes.pyx new file mode 100644 index 0000000..299add0 --- /dev/null +++ b/darkflow/cython_utils/cy_yolo2_findboxes.pyx @@ -0,0 +1,97 @@ +import numpy as np +cimport numpy as np +cimport cython +ctypedef np.float_t DTYPE_t +from libc.math cimport exp +from ..utils.box import BoundBox +from nms cimport NMS + +#expit +@cython.boundscheck(False) # turn off bounds-checking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function +@cython.cdivision(True) +cdef float expit_c(float x): + cdef float y= 1/(1+exp(-x)) + return y + +#MAX +@cython.boundscheck(False) # turn off bounds-checking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function +@cython.cdivision(True) +cdef float max_c(float a, float b): + if(a>b): + return a + return b + +""" +#SOFTMAX! +@cython.cdivision(True) +@cython.boundscheck(False) # turn off bounds-checking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function +cdef void _softmax_c(float* x, int classes): + cdef: + float sum = 0 + np.intp_t k + float arr_max = 0 + for k in range(classes): + arr_max = max(arr_max,x[k]) + + for k in range(classes): + x[k] = exp(x[k]-arr_max) + sum += x[k] + + for k in range(classes): + x[k] = x[k]/sum +""" + + + +#BOX CONSTRUCTOR +@cython.cdivision(True) +@cython.boundscheck(False) # turn off bounds-checking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function +def box_constructor(meta,np.ndarray[float,ndim=3] net_out_in): + cdef: + np.intp_t H, W, _, C, B, row, col, box_loop, class_loop + np.intp_t row1, col1, box_loop1,index,index2 + float threshold = meta['thresh'] + float tempc,arr_max=0,sum=0 + double[:] anchors = np.asarray(meta['anchors']) + list boxes = list() + + H, W, _ = meta['out_size'] + C = meta['classes'] + B = meta['num'] + + cdef: + float[:, :, :, ::1] net_out = net_out_in.reshape([H, W, B, net_out_in.shape[2]/B]) + float[:, :, :, ::1] Classes = net_out[:, :, :, 5:] + float[:, :, :, ::1] Bbox_pred = net_out[:, :, :, :5] + float[:, :, :, ::1] probs = np.zeros((H, W, B, C), dtype=np.float32) + + for row in range(H): + for col in range(W): + for box_loop in range(B): + arr_max=0 + sum=0; + Bbox_pred[row, col, box_loop, 4] = expit_c(Bbox_pred[row, col, box_loop, 4]) + Bbox_pred[row, col, box_loop, 0] = (col + expit_c(Bbox_pred[row, col, box_loop, 0])) / W + Bbox_pred[row, col, box_loop, 1] = (row + expit_c(Bbox_pred[row, col, box_loop, 1])) / H + Bbox_pred[row, col, box_loop, 2] = exp(Bbox_pred[row, col, box_loop, 2]) * anchors[2 * box_loop + 0] / W + Bbox_pred[row, col, box_loop, 3] = exp(Bbox_pred[row, col, box_loop, 3]) * anchors[2 * box_loop + 1] / H + #SOFTMAX BLOCK, no more pointer juggling + for class_loop in range(C): + arr_max=max_c(arr_max,Classes[row,col,box_loop,class_loop]) + + for class_loop in range(C): + Classes[row,col,box_loop,class_loop]=exp(Classes[row,col,box_loop,class_loop]-arr_max) + sum+=Classes[row,col,box_loop,class_loop] + + for class_loop in range(C): + tempc = Classes[row, col, box_loop, class_loop] * Bbox_pred[row, col, box_loop, 4]/sum + if(tempc > threshold): + probs[row, col, box_loop, class_loop] = tempc + + + #NMS + return NMS(np.ascontiguousarray(probs).reshape(H*W*B,C), np.ascontiguousarray(Bbox_pred).reshape(H*B*W,5)) diff --git a/darkflow/cython_utils/cy_yolo_findboxes.pyx b/darkflow/cython_utils/cy_yolo_findboxes.pyx new file mode 100644 index 0000000..1f65408 --- /dev/null +++ b/darkflow/cython_utils/cy_yolo_findboxes.pyx @@ -0,0 +1,51 @@ +import numpy as np +cimport numpy as np +cimport cython +ctypedef np.float_t DTYPE_t +from libc.math cimport exp +from ..utils.box import BoundBox +from nms cimport NMS + + + +@cython.cdivision(True) +@cython.boundscheck(False) # turn off bounds-checking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function +def yolo_box_constructor(meta,np.ndarray[float] net_out, float threshold): + + cdef: + float sqrt + int C,B,S + int SS,prob_size,conf_size + int grid, b + int class_loop + + + sqrt = meta['sqrt'] + 1 + C, B, S = meta['classes'], meta['num'], meta['side'] + boxes = [] + SS = S * S # number of grid cells + prob_size = SS * C # class probabilities + conf_size = SS * B # confidences for each grid cell + + cdef: + float [:,::1] probs = np.ascontiguousarray(net_out[0 : prob_size]).reshape([SS,C]) + float [:,::1] confs = np.ascontiguousarray(net_out[prob_size : (prob_size + conf_size)]).reshape([SS,B]) + float [: , : ,::1] coords = np.ascontiguousarray(net_out[(prob_size + conf_size) : ]).reshape([SS, B, 4]) + float [:,:,::1] final_probs = np.zeros([SS,B,C],dtype=np.float32) + + + for grid in range(SS): + for b in range(B): + coords[grid, b, 0] = (coords[grid, b, 0] + grid % S) / S + coords[grid, b, 1] = (coords[grid, b, 1] + grid // S) / S + coords[grid, b, 2] = coords[grid, b, 2] ** sqrt + coords[grid, b, 3] = coords[grid, b, 3] ** sqrt + for class_loop in range(C): + probs[grid, class_loop] = probs[grid, class_loop] * confs[grid, b] + #print("PROBS",probs[grid,class_loop]) + if(probs[grid,class_loop] > threshold ): + final_probs[grid, b, class_loop] = probs[grid, class_loop] + + + return NMS(np.ascontiguousarray(final_probs).reshape(SS*B, C) , np.ascontiguousarray(coords).reshape(SS*B, 4)) diff --git a/darkflow/cython_utils/nms.pxd b/darkflow/cython_utils/nms.pxd new file mode 100644 index 0000000..018365e --- /dev/null +++ b/darkflow/cython_utils/nms.pxd @@ -0,0 +1,11 @@ +import numpy as np +cimport numpy as np +cimport cython +ctypedef np.float_t DTYPE_t +from libc.math cimport exp +from utils.box import BoundBox + + +cdef NMS(float[:, ::1] , float[:, ::1] ) + + diff --git a/darkflow/cython_utils/nms.pyx b/darkflow/cython_utils/nms.pyx new file mode 100644 index 0000000..cf264d6 --- /dev/null +++ b/darkflow/cython_utils/nms.pyx @@ -0,0 +1,131 @@ +import numpy as np +cimport numpy as np +cimport cython +from libc.math cimport exp +from ..utils.box import BoundBox + + + +#OVERLAP +@cython.boundscheck(False) # turn off bounds-checking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function +@cython.cdivision(True) +cdef float overlap_c(float x1, float w1 , float x2 , float w2): + cdef: + float l1,l2,left,right + l1 = x1 - w1 /2. + l2 = x2 - w2 /2. + left = max(l1,l2) + r1 = x1 + w1 /2. + r2 = x2 + w2 /2. + right = min(r1, r2) + return right - left; + +#BOX INTERSECTION +@cython.boundscheck(False) # turn off bounds-checking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function +@cython.cdivision(True) +cdef float box_intersection_c(float ax, float ay, float aw, float ah, float bx, float by, float bw, float bh): + cdef: + float w,h,area + w = overlap_c(ax, aw, bx, bw) + h = overlap_c(ay, ah, by, bh) + if w < 0 or h < 0: return 0 + area = w * h + return area + +#BOX UNION +@cython.boundscheck(False) # turn off bounds-checking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function +@cython.cdivision(True) +cdef float box_union_c(float ax, float ay, float aw, float ah, float bx, float by, float bw, float bh): + cdef: + float i,u + i = box_intersection_c(ax, ay, aw, ah, bx, by, bw, bh) + u = aw * ah + bw * bh -i + return u + + +#BOX IOU +@cython.boundscheck(False) # turn off bounds-checking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function +@cython.cdivision(True) +cdef float box_iou_c(float ax, float ay, float aw, float ah, float bx, float by, float bw, float bh): + return box_intersection_c(ax, ay, aw, ah, bx, by, bw, bh) / box_union_c(ax, ay, aw, ah, bx, by, bw, bh); + + + + +#NMS +@cython.boundscheck(False) # turn off bounds-checking for entire function +@cython.wraparound(False) # turn off negative index wrapping for entire function +@cython.cdivision(True) +cdef NMS(float[:, ::1] final_probs , float[:, ::1] final_bbox): + cdef list boxes = list() + cdef set indices = set() + cdef: + np.intp_t pred_length,class_length,class_loop,index,index2 + + + pred_length = final_bbox.shape[0] + class_length = final_probs.shape[1] + for class_loop in range(class_length): + for index in range(pred_length): + if final_probs[index,class_loop] == 0: continue + for index2 in range(index+1,pred_length): + if final_probs[index2,class_loop] == 0: continue + if index==index2 : continue + if box_iou_c(final_bbox[index,0],final_bbox[index,1],final_bbox[index,2],final_bbox[index,3],final_bbox[index2,0],final_bbox[index2,1],final_bbox[index2,2],final_bbox[index2,3]) >= 0.4: + if final_probs[index2,class_loop] > final_probs[index, class_loop] : + final_probs[index, class_loop] =0 + break + final_probs[index2,class_loop]=0 + + if index not in indices: + bb=BoundBox(class_length) + bb.x = final_bbox[index, 0] + bb.y = final_bbox[index, 1] + bb.w = final_bbox[index, 2] + bb.h = final_bbox[index, 3] + bb.c = final_bbox[index, 4] + bb.probs = np.asarray(final_probs[index,:]) + boxes.append(bb) + indices.add(index) + return boxes + +# cdef NMS(float[:, ::1] final_probs , float[:, ::1] final_bbox): +# cdef list boxes = list() +# cdef: +# np.intp_t pred_length,class_length,class_loop,index,index2, i, j + + +# pred_length = final_bbox.shape[0] +# class_length = final_probs.shape[1] + +# for class_loop in range(class_length): +# order = np.argsort(final_probs[:,class_loop])[::-1] +# # First box +# for i in range(pred_length): +# index = order[i] +# if final_probs[index, class_loop] == 0.: +# continue +# # Second box +# for j in range(i+1, pred_length): +# index2 = order[j] +# if box_iou_c( +# final_bbox[index,0],final_bbox[index,1], +# final_bbox[index,2],final_bbox[index,3], +# final_bbox[index2,0],final_bbox[index2,1], +# final_bbox[index2,2],final_bbox[index2,3]) >= 0.4: +# final_probs[index2, class_loop] = 0. + +# bb = BoundBox(class_length) +# bb.x = final_bbox[index, 0] +# bb.y = final_bbox[index, 1] +# bb.w = final_bbox[index, 2] +# bb.h = final_bbox[index, 3] +# bb.c = final_bbox[index, 4] +# bb.probs = np.asarray(final_probs[index,:]) +# boxes.append(bb) + +# return boxes diff --git a/darkflow/dark/connected.py b/darkflow/dark/connected.py new file mode 100644 index 0000000..d1eb37d --- /dev/null +++ b/darkflow/dark/connected.py @@ -0,0 +1,111 @@ +from .layer import Layer +import numpy as np + +class extract_layer(Layer): + def setup(self, old_inp, old_out, + activation, inp, out): + if inp is None: inp = range(old_inp) + self.activation = activation + self.old_inp = old_inp + self.old_out = old_out + self.inp = inp + self.out = out + self.wshape = { + 'biases': [len(self.out)], + 'weights': [len(self.inp), len(self.out)] + } + + @property + def signature(self): + sig = ['connected'] + sig += self._signature[1:-2] + return sig + + def present(self): + args = self.signature + self.presenter = connected_layer(*args) + + def recollect(self, val): + w = val['weights'] + b = val['biases'] + if w is None: self.w = val; return + w = np.take(w, self.inp, 0) + w = np.take(w, self.out, 1) + b = np.take(b, self.out) + assert1 = w.shape == tuple(self.wshape['weights']) + assert2 = b.shape == tuple(self.wshape['biases']) + assert assert1 and assert2, \ + 'Dimension does not match in {} recollect'.format( + self._signature) + + self.w['weights'] = w + self.w['biases'] = b + + + +class select_layer(Layer): + def setup(self, inp, old, + activation, inp_idx, + out, keep, train): + self.old = old + self.keep = keep + self.train = train + self.inp_idx = inp_idx + self.activation = activation + inp_dim = inp + if inp_idx is not None: + inp_dim = len(inp_idx) + self.inp = inp_dim + self.out = out + self.wshape = { + 'biases': [out], + 'weights': [inp_dim, out] + } + + @property + def signature(self): + sig = ['connected'] + sig += self._signature[1:-4] + return sig + + def present(self): + args = self.signature + self.presenter = connected_layer(*args) + + def recollect(self, val): + w = val['weights'] + b = val['biases'] + if w is None: self.w = val; return + if self.inp_idx is not None: + w = np.take(w, self.inp_idx, 0) + + keep_b = np.take(b, self.keep) + keep_w = np.take(w, self.keep, 1) + train_b = b[self.train:] + train_w = w[:, self.train:] + self.w['biases'] = np.concatenate( + (keep_b, train_b), axis = 0) + self.w['weights'] = np.concatenate( + (keep_w, train_w), axis = 1) + + +class connected_layer(Layer): + def setup(self, input_size, + output_size, activation): + self.activation = activation + self.inp = input_size + self.out = output_size + self.wshape = { + 'biases': [self.out], + 'weights': [self.inp, self.out] + } + + def finalize(self, transpose): + weights = self.w['weights'] + if weights is None: return + shp = self.wshape['weights'] + if not transpose: + weights = weights.reshape(shp[::-1]) + weights = weights.transpose([1,0]) + else: weights = weights.reshape(shp) + self.w['weights'] = weights \ No newline at end of file diff --git a/darkflow/dark/convolution.py b/darkflow/dark/convolution.py new file mode 100644 index 0000000..9e7141e --- /dev/null +++ b/darkflow/dark/convolution.py @@ -0,0 +1,156 @@ +from .layer import Layer +import numpy as np + +class local_layer(Layer): + def setup(self, ksize, c, n, stride, + pad, w_, h_, activation): + self.pad = pad * int(ksize / 2) + self.activation = activation + self.stride = stride + self.ksize = ksize + self.h_out = h_ + self.w_out = w_ + + self.dnshape = [h_ * w_, n, c, ksize, ksize] + self.wshape = dict({ + 'biases': [h_ * w_ * n], + 'kernels': [h_ * w_, ksize, ksize, c, n] + }) + + def finalize(self, _): + weights = self.w['kernels'] + if weights is None: return + weights = weights.reshape(self.dnshape) + weights = weights.transpose([0,3,4,2,1]) + self.w['kernels'] = weights + +class conv_extract_layer(Layer): + def setup(self, ksize, c, n, stride, + pad, batch_norm, activation, + inp, out): + if inp is None: inp = range(c) + self.activation = activation + self.batch_norm = batch_norm + self.stride = stride + self.ksize = ksize + self.pad = pad + self.inp = inp + self.out = out + self.wshape = dict({ + 'biases': [len(out)], + 'kernel': [ksize, ksize, len(inp), len(out)] + }) + + @property + def signature(self): + sig = ['convolutional'] + sig += self._signature[1:-2] + return sig + + def present(self): + args = self.signature + self.presenter = convolutional_layer(*args) + + def recollect(self, w): + if w is None: + self.w = w + return + k = w['kernel'] + b = w['biases'] + k = np.take(k, self.inp, 2) + k = np.take(k, self.out, 3) + b = np.take(b, self.out) + assert1 = k.shape == tuple(self.wshape['kernel']) + assert2 = b.shape == tuple(self.wshape['biases']) + assert assert1 and assert2, \ + 'Dimension not matching in {} recollect'.format( + self._signature) + self.w['kernel'] = k + self.w['biases'] = b + + +class conv_select_layer(Layer): + def setup(self, ksize, c, n, stride, + pad, batch_norm, activation, + keep_idx, real_n): + self.batch_norm = bool(batch_norm) + self.activation = activation + self.keep_idx = keep_idx + self.stride = stride + self.ksize = ksize + self.pad = pad + self.wshape = dict({ + 'biases': [real_n], + 'kernel': [ksize, ksize, c, real_n] + }) + if self.batch_norm: + self.wshape.update({ + 'moving_variance' : [real_n], + 'moving_mean': [real_n], + 'gamma' : [real_n] + }) + self.h['is_training'] = { + 'shape': (), + 'feed': True, + 'dfault': False + } + + @property + def signature(self): + sig = ['convolutional'] + sig += self._signature[1:-2] + return sig + + def present(self): + args = self.signature + self.presenter = convolutional_layer(*args) + + def recollect(self, w): + if w is None: + self.w = w + return + idx = self.keep_idx + k = w['kernel'] + b = w['biases'] + self.w['kernel'] = np.take(k, idx, 3) + self.w['biases'] = np.take(b, idx) + if self.batch_norm: + m = w['moving_mean'] + v = w['moving_variance'] + g = w['gamma'] + self.w['moving_mean'] = np.take(m, idx) + self.w['moving_variance'] = np.take(v, idx) + self.w['gamma'] = np.take(g, idx) + +class convolutional_layer(Layer): + def setup(self, ksize, c, n, stride, + pad, batch_norm, activation): + self.batch_norm = bool(batch_norm) + self.activation = activation + self.stride = stride + self.ksize = ksize + self.pad = pad + self.dnshape = [n, c, ksize, ksize] # darknet shape + self.wshape = dict({ + 'biases': [n], + 'kernel': [ksize, ksize, c, n] + }) + if self.batch_norm: + self.wshape.update({ + 'moving_variance' : [n], + 'moving_mean': [n], + 'gamma' : [n] + }) + self.h['is_training'] = { + 'feed': True, + 'dfault': False, + 'shape': () + } + + def finalize(self, _): + """deal with darknet""" + kernel = self.w['kernel'] + if kernel is None: return + kernel = kernel.reshape(self.dnshape) + kernel = kernel.transpose([2,3,1,0]) + self.w['kernel'] = kernel \ No newline at end of file diff --git a/darkflow/dark/darknet.py b/darkflow/dark/darknet.py new file mode 100644 index 0000000..8412456 --- /dev/null +++ b/darkflow/dark/darknet.py @@ -0,0 +1,86 @@ +from ..utils.process import cfg_yielder +from .darkop import create_darkop +from ..utils import loader +import warnings +import time +import os + +class Darknet(object): + + _EXT = '.weights' + + def __init__(self, FLAGS): + self.get_weight_src(FLAGS) + self.modify = False + + print('Parsing {}'.format(self.src_cfg)) + src_parsed = self.parse_cfg(self.src_cfg, FLAGS) + self.src_meta, self.src_layers = src_parsed + + if self.src_cfg == FLAGS.model: + self.meta, self.layers = src_parsed + else: + print('Parsing {}'.format(FLAGS.model)) + des_parsed = self.parse_cfg(FLAGS.model, FLAGS) + self.meta, self.layers = des_parsed + + self.load_weights() + + def get_weight_src(self, FLAGS): + """ + analyse FLAGS.load to know where is the + source binary and what is its config. + can be: None, FLAGS.model, or some other + """ + self.src_bin = FLAGS.model + self._EXT + self.src_bin = FLAGS.binary + self.src_bin + self.src_bin = os.path.abspath(self.src_bin) + exist = os.path.isfile(self.src_bin) + + if FLAGS.load == str(): FLAGS.load = int() + if type(FLAGS.load) is int: + self.src_cfg = FLAGS.model + if FLAGS.load: self.src_bin = None + elif not exist: self.src_bin = None + else: + assert os.path.isfile(FLAGS.load), \ + '{} not found'.format(FLAGS.load) + self.src_bin = FLAGS.load + name = loader.model_name(FLAGS.load) + cfg_path = os.path.join(FLAGS.config, name + '.cfg') + if not os.path.isfile(cfg_path): + warnings.warn( + '{} not found, use {} instead'.format( + cfg_path, FLAGS.model)) + cfg_path = FLAGS.model + self.src_cfg = cfg_path + FLAGS.load = int() + + + def parse_cfg(self, model, FLAGS): + """ + return a list of `layers` objects (darkop.py) + given path to binaries/ and configs/ + """ + args = [model, FLAGS.binary] + cfg_layers = cfg_yielder(*args) + meta = dict(); layers = list() + for i, info in enumerate(cfg_layers): + if i == 0: meta = info; continue + else: new = create_darkop(*info) + layers.append(new) + return meta, layers + + def load_weights(self): + """ + Use `layers` and Loader to load .weights file + """ + print('Loading {} ...'.format(self.src_bin)) + start = time.time() + + args = [self.src_bin, self.src_layers] + wgts_loader = loader.create_loader(*args) + for layer in self.layers: layer.load(wgts_loader) + + stop = time.time() + print('Finished in {}s'.format(stop - start)) \ No newline at end of file diff --git a/darkflow/dark/darkop.py b/darkflow/dark/darkop.py new file mode 100644 index 0000000..bcde00a --- /dev/null +++ b/darkflow/dark/darkop.py @@ -0,0 +1,60 @@ +from .layer import Layer +from .convolution import * +from .connected import * + +class avgpool_layer(Layer): + pass + +class crop_layer(Layer): + pass + +class maxpool_layer(Layer): + def setup(self, ksize, stride, pad): + self.stride = stride + self.ksize = ksize + self.pad = pad + +class softmax_layer(Layer): + def setup(self, groups): + self.groups = groups + +class dropout_layer(Layer): + def setup(self, p): + self.h['pdrop'] = dict({ + 'feed': p, # for training + 'dfault': 1.0, # for testing + 'shape': () + }) + +class route_layer(Layer): + def setup(self, routes): + self.routes = routes + +class reorg_layer(Layer): + def setup(self, stride): + self.stride = stride + +""" +Darkop Factory +""" + +darkops = { + 'dropout': dropout_layer, + 'connected': connected_layer, + 'maxpool': maxpool_layer, + 'convolutional': convolutional_layer, + 'avgpool': avgpool_layer, + 'softmax': softmax_layer, + 'crop': crop_layer, + 'local': local_layer, + 'select': select_layer, + 'route': route_layer, + 'reorg': reorg_layer, + 'conv-select': conv_select_layer, + 'conv-extract': conv_extract_layer, + 'extract': extract_layer +} + +def create_darkop(ltype, num, *args): + op_class = darkops.get(ltype, Layer) + return op_class(ltype, num, *args) \ No newline at end of file diff --git a/darkflow/dark/layer.py b/darkflow/dark/layer.py new file mode 100644 index 0000000..08dee9a --- /dev/null +++ b/darkflow/dark/layer.py @@ -0,0 +1,71 @@ +from ..utils import loader +import numpy as np + +class Layer(object): + + def __init__(self, *args): + self._signature = list(args) + self.type = list(args)[0] + self.number = list(args)[1] + + self.w = dict() # weights + self.h = dict() # placeholders + self.wshape = dict() # weight shape + self.wsize = dict() # weight size + self.setup(*args[2:]) # set attr up + self.present() + for var in self.wshape: + shp = self.wshape[var] + size = np.prod(shp) + self.wsize[var] = size + + def load(self, src_loader): + var_lay = src_loader.VAR_LAYER + if self.type not in var_lay: return + + src_type = type(src_loader) + if src_type is loader.weights_loader: + wdict = self.load_weights(src_loader) + else: + wdict = self.load_ckpt(src_loader) + if wdict is not None: + self.recollect(wdict) + + def load_weights(self, src_loader): + val = src_loader([self.presenter]) + if val is None: return None + else: return val.w + + def load_ckpt(self, src_loader): + result = dict() + presenter = self.presenter + for var in presenter.wshape: + name = presenter.varsig(var) + shape = presenter.wshape[var] + key = [name, shape] + val = src_loader(key) + result[var] = val + return result + + @property + def signature(self): + return self._signature + + # For comparing two layers + def __eq__(self, other): + return self.signature == other.signature + def __ne__(self, other): + return not self.__eq__(other) + + def varsig(self, var): + if var not in self.wshape: + return None + sig = str(self.number) + sig += '-' + self.type + sig += '/' + var + return sig + + def recollect(self, w): self.w = w + def present(self): self.presenter = self + def setup(self, *args): pass + def finalize(self): pass \ No newline at end of file diff --git a/darkflow/defaults.py b/darkflow/defaults.py new file mode 100644 index 0000000..566bea0 --- /dev/null +++ b/darkflow/defaults.py @@ -0,0 +1,96 @@ +class argHandler(dict): + #A super duper fancy custom made CLI argument handler!! + __getattr__ = dict.get + __setattr__ = dict.__setitem__ + __delattr__ = dict.__delitem__ + _descriptions = {'help, --h, -h': 'show this super helpful message and exit'} + + def setDefaults(self): + self.define('imgdir', './sample_img/', 'path to testing directory with images') + self.define('binary', './bin/', 'path to .weights directory') + self.define('config', './cfg/', 'path to .cfg directory') + self.define('dataset', '../pascal/VOCdevkit/IMG/', 'path to dataset directory') + self.define('labels', 'labels.txt', 'path to labels file') + self.define('backup', './ckpt/', 'path to backup folder') + self.define('summary', './summary/', 'path to TensorBoard summaries directory') + self.define('annotation', '../pascal/VOCdevkit/ANN/', 'path to annotation directory') + self.define('threshold', -0.1, 'detection threshold') + self.define('model', '', 'configuration of choice') + self.define('trainer', 'rmsprop', 'training algorithm') + self.define('momentum', 0.0, 'applicable for rmsprop and momentum optimizers') + self.define('verbalise', True, 'say out loud while building graph') + self.define('train', False, 'train the whole net') + self.define('load', '', 'how to initialize the net? Either from .weights or a checkpoint, or even from scratch') + self.define('savepb', False, 'save net and weight to a .pb file') + self.define('gpu', 0.0, 'how much gpu (from 0.0 to 1.0)') + self.define('gpuName', '/gpu:0', 'GPU device name') + self.define('lr', 1e-5, 'learning rate') + self.define('keep',20,'Number of most recent training results to save') + self.define('batch', 16, 'batch size') + self.define('epoch', 1000, 'number of epoch') + self.define('save', 2000, 'save checkpoint every ? training examples') + self.define('demo', '', 'demo on webcam') + self.define('queue', 1, 'process demo in batch') + self.define('json', False, 'Outputs bounding box information in json format.') + self.define('saveVideo', False, 'Records video from input video or camera') + self.define('pbLoad', '', 'path to .pb protobuf file (metaLoad must also be specified)') + self.define('metaLoad', '', 'path to .meta file generated during --savepb that corresponds to .pb file') + + def define(self, argName, default, description): + self[argName] = default + self._descriptions[argName] = description + + def help(self): + print('Example usage: flow --imgdir sample_img/ --model cfg/yolo.cfg --load bin/yolo.weights') + print('') + print('Arguments:') + spacing = max([len(i) for i in self._descriptions.keys()]) + 2 + for item in self._descriptions: + currentSpacing = spacing - len(item) + print(' --' + item + (' ' * currentSpacing) + self._descriptions[item]) + print('') + exit() + + def parseArgs(self, args): + print('') + i = 1 + while i < len(args): + if args[i] == '-h' or args[i] == '--h' or args[i] == '--help': + self.help() #Time for some self help! :) + if len(args[i]) < 2: + print('ERROR - Invalid argument: ' + args[i]) + print('Try running flow --help') + exit() + argumentName = args[i][2:] + if isinstance(self.get(argumentName), bool): + if not (i + 1) >= len(args) and (args[i + 1].lower() != 'false' and args[i + 1].lower() != 'true') and not args[i + 1].startswith('--'): + print('ERROR - Expected boolean value (or no value) following argument: ' + args[i]) + print('Try running flow --help') + exit() + elif not (i + 1) >= len(args) and (args[i + 1].lower() == 'false' or args[i + 1].lower() == 'true'): + self[argumentName] = (args[i + 1].lower() == 'true') + i += 1 + else: + self[argumentName] = True + elif args[i].startswith('--') and not (i + 1) >= len(args) and not args[i + 1].startswith('--') and argumentName in self: + if isinstance(self[argumentName], float): + try: + args[i + 1] = float(args[i + 1]) + except: + print('ERROR - Expected float for argument: ' + args[i]) + print('Try running flow --help') + exit() + elif isinstance(self[argumentName], int): + try: + args[i + 1] = int(args[i + 1]) + except: + print('ERROR - Expected int for argument: ' + args[i]) + print('Try running flow --help') + exit() + self[argumentName] = args[i + 1] + i += 1 + else: + print('ERROR - Invalid argument: ' + args[i]) + print('Try running flow --help') + exit() + i += 1 diff --git a/darkflow/net/build.py b/darkflow/net/build.py new file mode 100644 index 0000000..f416161 --- /dev/null +++ b/darkflow/net/build.py @@ -0,0 +1,177 @@ +import tensorflow as tf +import time +from . import help +from . import flow +from .ops import op_create, identity +from .ops import HEADER, LINE +from .framework import create_framework +from ..dark.darknet import Darknet +import json +import os + +class TFNet(object): + + _TRAINER = dict({ + 'rmsprop': tf.train.RMSPropOptimizer, + 'adadelta': tf.train.AdadeltaOptimizer, + 'adagrad': tf.train.AdagradOptimizer, + 'adagradDA': tf.train.AdagradDAOptimizer, + 'momentum': tf.train.MomentumOptimizer, + 'adam': tf.train.AdamOptimizer, + 'ftrl': tf.train.FtrlOptimizer, + 'sgd': tf.train.GradientDescentOptimizer + }) + + # imported methods + _get_fps = help._get_fps + say = help.say + train = flow.train + camera = help.camera + predict = flow.predict + return_predict = flow.return_predict + to_darknet = help.to_darknet + build_train_op = help.build_train_op + load_from_ckpt = help.load_from_ckpt + + def __init__(self, FLAGS, darknet = None): + self.ntrain = 0 + + if isinstance(FLAGS, dict): + from ..defaults import argHandler + newFLAGS = argHandler() + newFLAGS.setDefaults() + newFLAGS.update(FLAGS) + FLAGS = newFLAGS + + self.FLAGS = FLAGS + if self.FLAGS.pbLoad and self.FLAGS.metaLoad: + self.say('\nLoading from .pb and .meta') + self.graph = tf.Graph() + device_name = FLAGS.gpuName \ + if FLAGS.gpu > 0.0 else None + with tf.device(device_name): + with self.graph.as_default() as g: + self.build_from_pb() + return + + if darknet is None: + darknet = Darknet(FLAGS) + self.ntrain = len(darknet.layers) + + self.darknet = darknet + args = [darknet.meta, FLAGS] + self.num_layer = len(darknet.layers) + self.framework = create_framework(*args) + + self.meta = darknet.meta + + self.say('\nBuilding net ...') + start = time.time() + self.graph = tf.Graph() + device_name = FLAGS.gpuName \ + if FLAGS.gpu > 0.0 else None + with tf.device(device_name): + with self.graph.as_default() as g: + self.build_forward() + self.setup_meta_ops() + self.say('Finished in {}s\n'.format( + time.time() - start)) + + def build_from_pb(self): + with tf.gfile.FastGFile(self.FLAGS.pbLoad, "rb") as f: + graph_def = tf.GraphDef() + graph_def.ParseFromString(f.read()) + + tf.import_graph_def( + graph_def, + name="" + ) + with open(self.FLAGS.metaLoad, 'r') as fp: + self.meta = json.load(fp) + self.framework = create_framework(self.meta, self.FLAGS) + + # Placeholders + self.inp = tf.get_default_graph().get_tensor_by_name('input:0') + self.feed = dict() # other placeholders + self.out = tf.get_default_graph().get_tensor_by_name('output:0') + + self.setup_meta_ops() + + def build_forward(self): + verbalise = self.FLAGS.verbalise + + # Placeholders + inp_size = [None] + self.meta['inp_size'] + self.inp = tf.placeholder(tf.float32, inp_size, 'input') + self.feed = dict() # other placeholders + + # Build the forward pass + state = identity(self.inp) + roof = self.num_layer - self.ntrain + self.say(HEADER, LINE) + for i, layer in enumerate(self.darknet.layers): + scope = '{}-{}'.format(str(i),layer.type) + args = [layer, state, i, roof, self.feed] + state = op_create(*args) + mess = state.verbalise() + self.say(mess) + self.say(LINE) + + self.top = state + self.out = tf.identity(state.out, name='output') + + def setup_meta_ops(self): + cfg = dict({ + 'allow_soft_placement': False, + 'log_device_placement': False + }) + + utility = min(self.FLAGS.gpu, 1.) + if utility > 0.0: + self.say('GPU mode with {} usage'.format(utility)) + cfg['gpu_options'] = tf.GPUOptions( + per_process_gpu_memory_fraction = utility) + cfg['allow_soft_placement'] = True + else: + self.say('Running entirely on CPU') + cfg['device_count'] = {'GPU': 0} + + if self.FLAGS.train: self.build_train_op() + + if self.FLAGS.summary is not None: + self.summary_op = tf.summary.merge_all() + self.writer = tf.summary.FileWriter(self.FLAGS.summary + 'train') + + self.sess = tf.Session(config = tf.ConfigProto(**cfg)) + self.sess.run(tf.global_variables_initializer()) + + if not self.ntrain: return + self.saver = tf.train.Saver(tf.global_variables(), + max_to_keep = self.FLAGS.keep) + if self.FLAGS.load != 0: self.load_from_ckpt() + + if self.FLAGS.summary is not None: + self.writer.add_graph(self.sess.graph) + + def savepb(self): + """ + Create a standalone const graph def that + C++ can load and run. + """ + darknet_pb = self.to_darknet() + flags_pb = self.FLAGS + flags_pb.verbalise = False + + flags_pb.train = False + # rebuild another tfnet. all const. + tfnet_pb = TFNet(flags_pb, darknet_pb) + tfnet_pb.sess = tf.Session(graph = tfnet_pb.graph) + # tfnet_pb.predict() # uncomment for unit testing + name = 'built_graph/{}.pb'.format(self.meta['name']) + os.makedirs(os.path.dirname(name), exist_ok=True) + #Save dump of everything in meta + with open('built_graph/{}.meta'.format(self.meta['name']), 'w') as fp: + json.dump(self.meta, fp) + self.say('Saving const graph def to {}'.format(name)) + graph_def = tfnet_pb.sess.graph_def + tf.train.write_graph(graph_def,'./', name, False) \ No newline at end of file diff --git a/darkflow/net/flow.py b/darkflow/net/flow.py new file mode 100644 index 0000000..aa1d8f4 --- /dev/null +++ b/darkflow/net/flow.py @@ -0,0 +1,148 @@ +import os +import time +import numpy as np +import tensorflow as tf +import pickle +from multiprocessing.pool import ThreadPool + +train_stats = ( + 'Training statistics: \n' + '\tLearning rate : {}\n' + '\tBatch size : {}\n' + '\tEpoch number : {}\n' + '\tBackup every : {}' +) +pool = ThreadPool() + +def _save_ckpt(self, step, loss_profile): + file = '{}-{}{}' + model = self.meta['name'] + + profile = file.format(model, step, '.profile') + profile = os.path.join(self.FLAGS.backup, profile) + with open(profile, 'wb') as profile_ckpt: + pickle.dump(loss_profile, profile_ckpt) + + ckpt = file.format(model, step, '') + ckpt = os.path.join(self.FLAGS.backup, ckpt) + self.say('Checkpoint at step {}'.format(step)) + self.saver.save(self.sess, ckpt) + + +def train(self): + loss_ph = self.framework.placeholders + loss_mva = None; profile = list() + + batches = self.framework.shuffle() + loss_op = self.framework.loss + + for i, (x_batch, datum) in enumerate(batches): + if not i: self.say(train_stats.format( + self.FLAGS.lr, self.FLAGS.batch, + self.FLAGS.epoch, self.FLAGS.save + )) + + feed_dict = { + loss_ph[key]: datum[key] + for key in loss_ph } + feed_dict[self.inp] = x_batch + feed_dict.update(self.feed) + + fetches = [self.train_op, loss_op, self.summary_op] + fetched = self.sess.run(fetches, feed_dict) + loss = fetched[1] + + if loss_mva is None: loss_mva = loss + loss_mva = .9 * loss_mva + .1 * loss + step_now = self.FLAGS.load + i + 1 + + self.writer.add_summary(fetched[2], step_now) + + form = 'step {} - loss {} - moving ave loss {}' + self.say(form.format(step_now, loss, loss_mva)) + profile += [(loss, loss_mva)] + + ckpt = (i+1) % (self.FLAGS.save // self.FLAGS.batch) + args = [step_now, profile] + if not ckpt: _save_ckpt(self, *args) + + if ckpt: _save_ckpt(self, *args) + +def return_predict(self, im): + assert isinstance(im, np.ndarray), \ + 'Image is not a np.ndarray' + h, w, _ = im.shape + im = self.framework.resize_input(im) + this_inp = np.expand_dims(im, 0) + feed_dict = {self.inp : this_inp} + + out = self.sess.run(self.out, feed_dict)[0] + boxes = self.framework.findboxes(out) + threshold = self.FLAGS.threshold + boxesInfo = list() + for box in boxes: + tmpBox = self.framework.process_box(box, h, w, threshold) + if tmpBox is None: + continue + boxesInfo.append({ + "label": tmpBox[4], + "confidence": tmpBox[6], + "topleft": { + "x": tmpBox[0], + "y": tmpBox[2]}, + "bottomright": { + "x": tmpBox[1], + "y": tmpBox[3]} + }) + return boxesInfo + +import math + +def predict(self): + inp_path = self.FLAGS.imgdir + all_inps = os.listdir(inp_path) + all_inps = [i for i in all_inps if self.framework.is_inp(i)] + if not all_inps: + msg = 'Failed to find any images in {} .' + exit('Error: {}'.format(msg.format(inp_path))) + + batch = min(self.FLAGS.batch, len(all_inps)) + + # predict in batches + n_batch = int(math.ceil(len(all_inps) / batch)) + for j in range(n_batch): + from_idx = j * batch + to_idx = min(from_idx + batch, len(all_inps)) + + # collect images input in the batch + inp_feed = list(); new_all = list() + this_batch = all_inps[from_idx:to_idx] + for inp in this_batch: + new_all += [inp] + this_inp = os.path.join(inp_path, inp) + this_inp = self.framework.preprocess(this_inp) + expanded = np.expand_dims(this_inp, 0) + inp_feed.append(expanded) + this_batch = new_all + + # Feed to the net + feed_dict = {self.inp : np.concatenate(inp_feed, 0)} + self.say('Forwarding {} inputs ...'.format(len(inp_feed))) + start = time.time() + out = self.sess.run(self.out, feed_dict) + stop = time.time(); last = stop - start + self.say('Total time = {}s / {} inps = {} ips'.format( + last, len(inp_feed), len(inp_feed) / last)) + + # Post processing + self.say('Post processing {} inputs ...'.format(len(inp_feed))) + start = time.time() + pool.map(lambda p: (lambda i, prediction: + self.framework.postprocess( + prediction, os.path.join(inp_path, this_batch[i])))(*p), + enumerate(out)) + stop = time.time(); last = stop - start + + # Timing + self.say('Total time = {}s / {} inps = {} ips'.format( + last, len(inp_feed), len(inp_feed) / last)) diff --git a/darkflow/net/framework.py b/darkflow/net/framework.py new file mode 100644 index 0000000..627629e --- /dev/null +++ b/darkflow/net/framework.py @@ -0,0 +1,59 @@ +from . import yolo +from . import yolov2 +from . import vanilla +from os.path import basename + +class framework(object): + constructor = vanilla.constructor + loss = vanilla.train.loss + + def __init__(self, meta, FLAGS): + model = basename(meta['model']) + model = '.'.join(model.split('.')[:-1]) + meta['name'] = model + + self.constructor(meta, FLAGS) + + def is_inp(self, file_name): + return True + +class YOLO(framework): + constructor = yolo.constructor + parse = yolo.data.parse + shuffle = yolo.data.shuffle + preprocess = yolo.predict.preprocess + postprocess = yolo.predict.postprocess + loss = yolo.train.loss + is_inp = yolo.misc.is_inp + profile = yolo.misc.profile + _batch = yolo.data._batch + resize_input = yolo.predict.resize_input + findboxes = yolo.predict.findboxes + process_box = yolo.predict.process_box + +class YOLOv2(framework): + constructor = yolo.constructor + parse = yolo.data.parse + shuffle = yolov2.data.shuffle + preprocess = yolo.predict.preprocess + loss = yolov2.train.loss + is_inp = yolo.misc.is_inp + postprocess = yolov2.predict.postprocess + _batch = yolov2.data._batch + resize_input = yolo.predict.resize_input + findboxes = yolov2.predict.findboxes + process_box = yolo.predict.process_box + +""" +framework factory +""" + +types = { + '[detection]': YOLO, + '[region]': YOLOv2 +} + +def create_framework(meta, FLAGS): + net_type = meta['type'] + this = types.get(net_type, framework) + return this(meta, FLAGS) \ No newline at end of file diff --git a/darkflow/net/help.py b/darkflow/net/help.py new file mode 100644 index 0000000..24f1614 --- /dev/null +++ b/darkflow/net/help.py @@ -0,0 +1,171 @@ +""" +tfnet secondary (helper) methods +""" +from ..utils.loader import create_loader +from time import time as timer +import tensorflow as tf +import numpy as np +import sys +import cv2 +import os + +old_graph_msg = 'Resolving old graph def {} (no guarantee)' + +def build_train_op(self): + self.framework.loss(self.out) + self.say('Building {} train op'.format(self.meta['model'])) + optimizer = self._TRAINER[self.FLAGS.trainer](self.FLAGS.lr) + gradients = optimizer.compute_gradients(self.framework.loss) + self.train_op = optimizer.apply_gradients(gradients) + +def load_from_ckpt(self): + if self.FLAGS.load < 0: # load lastest ckpt + with open(self.FLAGS.backup + 'checkpoint', 'r') as f: + last = f.readlines()[-1].strip() + load_point = last.split(' ')[1] + load_point = load_point.split('"')[1] + load_point = load_point.split('-')[-1] + self.FLAGS.load = int(load_point) + + load_point = os.path.join(self.FLAGS.backup, self.meta['name']) + load_point = '{}-{}'.format(load_point, self.FLAGS.load) + self.say('Loading from {}'.format(load_point)) + try: self.saver.restore(self.sess, load_point) + except: load_old_graph(self, load_point) + +def say(self, *msgs): + if not self.FLAGS.verbalise: + return + msgs = list(msgs) + for msg in msgs: + if msg is None: continue + print(msg) + +def load_old_graph(self, ckpt): + ckpt_loader = create_loader(ckpt) + self.say(old_graph_msg.format(ckpt)) + + for var in tf.global_variables(): + name = var.name.split(':')[0] + args = [name, var.get_shape()] + val = ckpt_loader(args) + assert val is not None, \ + 'Cannot find and load {}'.format(var.name) + shp = val.shape + plh = tf.placeholder(tf.float32, shp) + op = tf.assign(var, plh) + self.sess.run(op, {plh: val}) + +def _get_fps(self, frame): + elapsed = int() + start = timer() + preprocessed = self.framework.preprocess(frame) + feed_dict = {self.inp: [preprocessed]} + net_out = self.sess.run(self.out, feed_dict)[0] + processed = self.framework.postprocess(net_out, frame, False) + return timer() - start + +def camera(self): + file = self.FLAGS.demo + SaveVideo = self.FLAGS.saveVideo + + if file == 'camera': + file = 0 + else: + assert os.path.isfile(file), \ + 'file {} does not exist'.format(file) + + camera = cv2.VideoCapture(file) + + if file == 0: + self.say('Press [ESC] to quit demo') + + assert camera.isOpened(), \ + 'Cannot capture source' + + if file == 0:#camera window + cv2.namedWindow('', 0) + _, frame = camera.read() + height, width, _ = frame.shape + cv2.resizeWindow('', width, height) + else: + _, frame = camera.read() + height, width, _ = frame.shape + + if SaveVideo: + fourcc = cv2.VideoWriter_fourcc(*'XVID') + if file == 0:#camera window + fps = 1 / self._get_fps(frame) + if fps < 1: + fps = 1 + else: + fps = round(camera.get(cv2.CAP_PROP_FPS)) + videoWriter = cv2.VideoWriter( + 'video.avi', fourcc, fps, (width, height)) + + # buffers for demo in batch + buffer_inp = list() + buffer_pre = list() + + elapsed = int() + start = timer() + self.say('Press [ESC] to quit demo') + # Loop through frames + while camera.isOpened(): + elapsed += 1 + _, frame = camera.read() + if frame is None: + print ('\nEnd of Video') + break + preprocessed = self.framework.preprocess(frame) + buffer_inp.append(frame) + buffer_pre.append(preprocessed) + + # Only process and imshow when queue is full + if elapsed % self.FLAGS.queue == 0: + feed_dict = {self.inp: buffer_pre} + net_out = self.sess.run(self.out, feed_dict) + for img, single_out in zip(buffer_inp, net_out): + postprocessed = self.framework.postprocess( + single_out, img, False) + if SaveVideo: + videoWriter.write(postprocessed) + if file == 0: #camera window + cv2.imshow('', postprocessed) + # Clear Buffers + buffer_inp = list() + buffer_pre = list() + + if elapsed % 5 == 0: + sys.stdout.write('\r') + sys.stdout.write('{0:3.3f} FPS'.format( + elapsed / (timer() - start))) + sys.stdout.flush() + if file == 0: #camera window + choice = cv2.waitKey(1) + if choice == 27: break + + sys.stdout.write('\n') + if SaveVideo: + videoWriter.release() + camera.release() + if file == 0: #camera window + cv2.destroyAllWindows() + +def to_darknet(self): + darknet_ckpt = self.darknet + + with self.graph.as_default() as g: + for var in tf.global_variables(): + name = var.name.split(':')[0] + var_name = name.split('-') + l_idx = int(var_name[0]) + w_sig = var_name[1].split('/')[-1] + l = darknet_ckpt.layers[l_idx] + l.w[w_sig] = var.eval(self.sess) + + for layer in darknet_ckpt.layers: + for ph in layer.h: + layer.h[ph] = None + + return darknet_ckpt diff --git a/darkflow/net/ops/__init__.py b/darkflow/net/ops/__init__.py new file mode 100644 index 0000000..d687cb3 --- /dev/null +++ b/darkflow/net/ops/__init__.py @@ -0,0 +1,27 @@ +from .simple import * +from .convolution import * +from .baseop import HEADER, LINE + +op_types = { + 'convolutional': convolutional, + 'conv-select': conv_select, + 'connected': connected, + 'maxpool': maxpool, + 'leaky': leaky, + 'dropout': dropout, + 'flatten': flatten, + 'avgpool': avgpool, + 'softmax': softmax, + 'identity': identity, + 'crop': crop, + 'local': local, + 'select': select, + 'route': route, + 'reorg': reorg, + 'conv-extract': conv_extract, + 'extract': extract +} + +def op_create(*args): + layer_type = list(args)[0].type + return op_types[layer_type](*args) \ No newline at end of file diff --git a/darkflow/net/ops/baseop.py b/darkflow/net/ops/baseop.py new file mode 100644 index 0000000..8992aca --- /dev/null +++ b/darkflow/net/ops/baseop.py @@ -0,0 +1,100 @@ +import tensorflow as tf +import numpy as np + +FORM = '{:>6} | {:>6} | {:<32} | {}' +FORM_ = '{}+{}+{}+{}' +LINE = FORM_.format('-'*7, '-'*8, '-'*34, '-'*15) +HEADER = FORM.format( + 'Source', 'Train?','Layer description', 'Output size') + +def _shape(tensor): # work for both tf.Tensor & np.ndarray + if type(tensor) in [tf.Variable, tf.Tensor]: + return tensor.get_shape() + else: return tensor.shape + +def _name(tensor): + return tensor.name.split(':')[0] + +class BaseOp(object): + """ + BaseOp objects initialise with a darknet's `layer` object + and input tensor of that layer `inp`, it calculates the + output of this layer and place the result in self.out + """ + + # let slim take care of the following vars + _SLIM = ['gamma', 'moving_mean', 'moving_variance'] + + def __init__(self, layer, inp, num, roof, feed): + self.inp = inp # BaseOp + self.num = num # int + self.out = None # tf.Tensor + self.lay = layer + + self.scope = '{}-{}'.format( + str(self.num), self.lay.type) + self.gap = roof - self.num + self.var = not self.gap > 0 + self.act = 'Load ' + self.convert(feed) + if self.var: self.train_msg = 'Yep! ' + else: self.train_msg = 'Nope ' + self.forward() + + def convert(self, feed): + """convert self.lay to variables & placeholders""" + for var in self.lay.wshape: + self.wrap_variable(var) + for ph in self.lay.h: + self.wrap_pholder(ph, feed) + + def wrap_variable(self, var): + """wrap layer.w into variables""" + val = self.lay.w.get(var, None) + if val is None: + shape = self.lay.wshape[var] + args = [0., 1e-2, shape] + if 'moving_mean' in var: + val = np.zeros(shape) + elif 'moving_variance' in var: + val = np.ones(shape) + else: + val = np.random.normal(*args) + self.lay.w[var] = val.astype(np.float32) + self.act = 'Init ' + if not self.var: return + + val = self.lay.w[var] + self.lay.w[var] = tf.constant_initializer(val) + if var in self._SLIM: return + with tf.variable_scope(self.scope): + self.lay.w[var] = tf.get_variable(var, + shape = self.lay.wshape[var], + dtype = tf.float32, + initializer = self.lay.w[var]) + + def wrap_pholder(self, ph, feed): + """wrap layer.h into placeholders""" + phtype = type(self.lay.h[ph]) + if phtype is not dict: return + + sig = '{}/{}'.format(self.scope, ph) + val = self.lay.h[ph] + + self.lay.h[ph] = tf.placeholder_with_default( + val['dfault'], val['shape'], name = sig) + feed[self.lay.h[ph]] = val['feed'] + + def verbalise(self): # console speaker + msg = str() + inp = _name(self.inp.out) + if inp == 'input': \ + msg = FORM.format( + '', '', 'input', + _shape(self.inp.out)) + '\n' + if not self.act: return msg + return msg + FORM.format( + self.act, self.train_msg, + self.speak(), _shape(self.out)) + + def speak(self): pass \ No newline at end of file diff --git a/darkflow/net/ops/convolution.py b/darkflow/net/ops/convolution.py new file mode 100644 index 0000000..167b0fd --- /dev/null +++ b/darkflow/net/ops/convolution.py @@ -0,0 +1,116 @@ +import tensorflow.contrib.slim as slim +from .baseop import BaseOp +import tensorflow as tf +import numpy as np + +class reorg(BaseOp): + def _forward(self): + inp = self.inp.out + shape = inp.get_shape().as_list() + _, h, w, c = shape + s = self.lay.stride + out = list() + for i in range(int(h/s)): + row_i = list() + for j in range(int(w/s)): + si, sj = s * i, s * j + boxij = inp[:, si: si+s, sj: sj+s,:] + flatij = tf.reshape(boxij, [-1,1,1,c*s*s]) + row_i += [flatij] + out += [tf.concat(row_i, 2)] + + self.out = tf.concat(out, 1) + + def forward(self): + inp = self.inp.out + s = self.lay.stride + self.out = tf.extract_image_patches( + inp, [1,s,s,1], [1,s,s,1], [1,1,1,1], 'VALID') + + def speak(self): + args = [self.lay.stride] * 2 + msg = 'local flatten {}x{}' + return msg.format(*args) + + +class local(BaseOp): + def forward(self): + pad = [[self.lay.pad, self.lay.pad]] * 2; + temp = tf.pad(self.inp.out, [[0, 0]] + pad + [[0, 0]]) + + k = self.lay.w['kernels'] + ksz = self.lay.ksize + half = int(ksz / 2) + out = list() + for i in range(self.lay.h_out): + row_i = list() + for j in range(self.lay.w_out): + kij = k[i * self.lay.w_out + j] + i_, j_ = i + 1 - half, j + 1 - half + tij = temp[:, i_ : i_ + ksz, j_ : j_ + ksz,:] + row_i.append( + tf.nn.conv2d(tij, kij, + padding = 'VALID', + strides = [1] * 4)) + out += [tf.concat(row_i, 2)] + + self.out = tf.concat(out, 1) + + def speak(self): + l = self.lay + args = [l.ksize] * 2 + [l.pad] + [l.stride] + args += [l.activation] + msg = 'loca {}x{}p{}_{} {}'.format(*args) + return msg + +class convolutional(BaseOp): + def forward(self): + pad = [[self.lay.pad, self.lay.pad]] * 2; + temp = tf.pad(self.inp.out, [[0, 0]] + pad + [[0, 0]]) + temp = tf.nn.conv2d(temp, self.lay.w['kernel'], padding = 'VALID', + name = self.scope, strides = [1] + [self.lay.stride] * 2 + [1]) + if self.lay.batch_norm: + temp = self.batchnorm(self.lay, temp) + self.out = tf.nn.bias_add(temp, self.lay.w['biases']) + + def batchnorm(self, layer, inp): + if not self.var: + temp = (inp - layer.w['moving_mean']) + temp /= (np.sqrt(layer.w['moving_variance']) + 1e-5) + temp *= layer.w['gamma'] + return temp + else: + args = dict({ + 'center' : False, 'scale' : True, + 'epsilon': 1e-5, 'scope' : self.scope, + 'updates_collections' : None, + 'is_training': layer.h['is_training'], + 'param_initializers': layer.w + }) + return slim.batch_norm(inp, **args) + + def speak(self): + l = self.lay + args = [l.ksize] * 2 + [l.pad] + [l.stride] + args += [l.batch_norm * '+bnorm'] + args += [l.activation] + msg = 'conv {}x{}p{}_{} {} {}'.format(*args) + return msg + +class conv_select(convolutional): + def speak(self): + l = self.lay + args = [l.ksize] * 2 + [l.pad] + [l.stride] + args += [l.batch_norm * '+bnorm'] + args += [l.activation] + msg = 'sele {}x{}p{}_{} {} {}'.format(*args) + return msg + +class conv_extract(convolutional): + def speak(self): + l = self.lay + args = [l.ksize] * 2 + [l.pad] + [l.stride] + args += [l.batch_norm * '+bnorm'] + args += [l.activation] + msg = 'extr {}x{}p{}_{} {} {}'.format(*args) + return msg \ No newline at end of file diff --git a/darkflow/net/ops/simple.py b/darkflow/net/ops/simple.py new file mode 100644 index 0000000..01e28ce --- /dev/null +++ b/darkflow/net/ops/simple.py @@ -0,0 +1,133 @@ +import tensorflow.contrib.slim as slim +from .baseop import BaseOp +import tensorflow as tf +from distutils.version import StrictVersion + +class route(BaseOp): + def forward(self): + routes = self.lay.routes + routes_out = list() + for r in routes: + this = self.inp + while this.lay.number != r: + this = this.inp + assert this is not None, \ + 'Routing to non-existence {}'.format(r) + routes_out += [this.out] + self.out = tf.concat(routes_out, 3) + + def speak(self): + msg = 'concat {}' + return msg.format(self.lay.routes) + +class connected(BaseOp): + def forward(self): + self.out = tf.nn.xw_plus_b( + self.inp.out, + self.lay.w['weights'], + self.lay.w['biases'], + name = self.scope) + + def speak(self): + layer = self.lay + args = [layer.inp, layer.out] + args += [layer.activation] + msg = 'full {} x {} {}' + return msg.format(*args) + +class select(connected): + """a weird connected layer""" + def speak(self): + layer = self.lay + args = [layer.inp, layer.out] + args += [layer.activation] + msg = 'sele {} x {} {}' + return msg.format(*args) + +class extract(connected): + """a weird connected layer""" + def speak(self): + layer = self.lay + args = [len(layer.inp), len(layer.out)] + args += [layer.activation] + msg = 'extr {} x {} {}' + return msg.format(*args) + +class flatten(BaseOp): + def forward(self): + temp = tf.transpose( + self.inp.out, [0,3,1,2]) + self.out = slim.flatten( + temp, scope = self.scope) + + def speak(self): return 'flat' + + +class softmax(BaseOp): + def forward(self): + self.out = tf.nn.softmax(self.inp.out) + + def speak(self): return 'softmax()' + + +class avgpool(BaseOp): + def forward(self): + self.out = tf.reduce_mean( + self.inp.out, [1, 2], + name = self.scope + ) + + def speak(self): return 'avgpool()' + + +class dropout(BaseOp): + def forward(self): + if self.lay.h['pdrop'] is None: + self.lay.h['pdrop'] = 1.0 + self.out = tf.nn.dropout( + self.inp.out, + self.lay.h['pdrop'], + name = self.scope + ) + + def speak(self): return 'drop' + + +class crop(BaseOp): + def forward(self): + self.out = self.inp.out * 2. - 1. + + def speak(self): + return 'scale to (-1, 1)' + + +class maxpool(BaseOp): + def forward(self): + self.out = tf.nn.max_pool( + self.inp.out, padding = 'SAME', + ksize = [1] + [self.lay.ksize]*2 + [1], + strides = [1] + [self.lay.stride]*2 + [1], + name = self.scope + ) + + def speak(self): + l = self.lay + return 'maxp {}x{}p{}_{}'.format( + l.ksize, l.ksize, l.pad, l.stride) + + +class leaky(BaseOp): + def forward(self): + self.out = tf.maximum( + .1 * self.inp.out, + self.inp.out, + name = self.scope + ) + + def verbalise(self): pass + + +class identity(BaseOp): + def __init__(self, inp): + self.inp = None + self.out = inp diff --git a/darkflow/net/vanilla/__init__.py b/darkflow/net/vanilla/__init__.py new file mode 100644 index 0000000..b189372 --- /dev/null +++ b/darkflow/net/vanilla/__init__.py @@ -0,0 +1,4 @@ +from . import train + +def constructor(self, meta, FLAGS): + self.meta, self.FLAGS = meta, FLAGS \ No newline at end of file diff --git a/darkflow/net/vanilla/train.py b/darkflow/net/vanilla/train.py new file mode 100644 index 0000000..1785d04 --- /dev/null +++ b/darkflow/net/vanilla/train.py @@ -0,0 +1,42 @@ +_LOSS_TYPE = ['sse','l2', 'smooth', + 'sparse', 'l1', 'softmax', + 'svm', 'fisher'] + +def loss(self, net_out): + m = self.meta + loss_type = self.meta['type'] + assert loss_type in _LOSS_TYPE, \ + 'Loss type {} not implemented'.format(loss_type) + + out = net_out + out_shape = out.get_shape() + out_dtype = out.dtype.base_dtype + _truth = tf.placeholders(out_dtype, out_shape) + + self.placeholders = dict({ + 'truth': _truth + }) + + diff = _truth - out + if loss_type in ['sse','12']: + loss = tf.nn.l2_loss(diff) + + elif loss_type == ['smooth']: + small = tf.cast(diff < 1, tf.float32) + large = 1. - small + l1_loss = tf.nn.l1_loss(tf.multiply(diff, large)) + l2_loss = tf.nn.l2_loss(tf.multiply(diff, small)) + loss = l1_loss + l2_loss + + elif loss_type in ['sparse', 'l1']: + loss = l1_loss(diff) + + elif loss_type == 'softmax': + loss = tf.nn.softmax_cross_entropy_with_logits(logits, y) + loss = tf.reduce_mean(loss) + + elif loss_type == 'svm': + assert 'train_size' in m, \ + 'Must specify' + size = m['train_size'] + self.nu = tf.Variable(tf.ones([train_size, num_classes])) \ No newline at end of file diff --git a/darkflow/net/yolo/__init__.py b/darkflow/net/yolo/__init__.py new file mode 100644 index 0000000..1ad7d54 --- /dev/null +++ b/darkflow/net/yolo/__init__.py @@ -0,0 +1,37 @@ +from . import train +from . import predict +from . import data +from . import misc +import numpy as np + + +""" YOLO framework __init__ equivalent""" + +def constructor(self, meta, FLAGS): + + def _to_color(indx, base): + """ return (b, r, g) tuple""" + base2 = base * base + b = 2 - indx / base2 + r = 2 - (indx % base2) / base + g = 2 - (indx % base2) % base + return (b * 127, r * 127, g * 127) + if 'labels' not in meta: + misc.labels(meta, FLAGS) #We're not loading from a .pb so we do need to load the labels + assert len(meta['labels']) == meta['classes'], ( + 'labels.txt and {} indicate' + ' ' + 'inconsistent class numbers' + ).format(meta['model']) + + # assign a color for each label + colors = list() + base = int(np.ceil(pow(meta['classes'], 1./3))) + for x in range(len(meta['labels'])): + colors += [_to_color(x, base)] + meta['colors'] = colors + self.fetch = list() + self.meta, self.FLAGS = meta, FLAGS + + # over-ride the threshold in meta if FLAGS has it. + if FLAGS.threshold > 0.0: + self.meta['thresh'] = FLAGS.threshold \ No newline at end of file diff --git a/darkflow/net/yolo/data.py b/darkflow/net/yolo/data.py new file mode 100644 index 0000000..f216e7f --- /dev/null +++ b/darkflow/net/yolo/data.py @@ -0,0 +1,130 @@ +from ...utils.pascal_voc_clean_xml import pascal_voc_clean_xml +from numpy.random import permutation as perm +from .predict import preprocess +# from .misc import show +from copy import deepcopy +import pickle +import numpy as np +import os + +def parse(self, exclusive = False): + meta = self.meta + ext = '.parsed' + ann = self.FLAGS.annotation + if not os.path.isdir(ann): + msg = 'Annotation directory not found {} .' + exit('Error: {}'.format(msg.format(ann))) + print('\n{} parsing {}'.format(meta['model'], ann)) + dumps = pascal_voc_clean_xml(ann, meta['labels'], exclusive) + return dumps + + +def _batch(self, chunk): + """ + Takes a chunk of parsed annotations + returns value for placeholders of net's + input & loss layer correspond to this chunk + """ + meta = self.meta + S, B = meta['side'], meta['num'] + C, labels = meta['classes'], meta['labels'] + + # preprocess + jpg = chunk[0]; w, h, allobj_ = chunk[1] + allobj = deepcopy(allobj_) + path = os.path.join(self.FLAGS.dataset, jpg) + img = self.preprocess(path, allobj) + + # Calculate regression target + cellx = 1. * w / S + celly = 1. * h / S + for obj in allobj: + centerx = .5*(obj[1]+obj[3]) #xmin, xmax + centery = .5*(obj[2]+obj[4]) #ymin, ymax + cx = centerx / cellx + cy = centery / celly + if cx >= S or cy >= S: return None, None + obj[3] = float(obj[3]-obj[1]) / w + obj[4] = float(obj[4]-obj[2]) / h + obj[3] = np.sqrt(obj[3]) + obj[4] = np.sqrt(obj[4]) + obj[1] = cx - np.floor(cx) # centerx + obj[2] = cy - np.floor(cy) # centery + obj += [int(np.floor(cy) * S + np.floor(cx))] + + # show(im, allobj, S, w, h, cellx, celly) # unit test + + # Calculate placeholders' values + probs = np.zeros([S*S,C]) + confs = np.zeros([S*S,B]) + coord = np.zeros([S*S,B,4]) + proid = np.zeros([S*S,C]) + prear = np.zeros([S*S,4]) + for obj in allobj: + probs[obj[5], :] = [0.] * C + probs[obj[5], labels.index(obj[0])] = 1. + proid[obj[5], :] = [1] * C + coord[obj[5], :, :] = [obj[1:5]] * B + prear[obj[5],0] = obj[1] - obj[3]**2 * .5 * S # xleft + prear[obj[5],1] = obj[2] - obj[4]**2 * .5 * S # yup + prear[obj[5],2] = obj[1] + obj[3]**2 * .5 * S # xright + prear[obj[5],3] = obj[2] + obj[4]**2 * .5 * S # ybot + confs[obj[5], :] = [1.] * B + + # Finalise the placeholders' values + upleft = np.expand_dims(prear[:,0:2], 1) + botright = np.expand_dims(prear[:,2:4], 1) + wh = botright - upleft; + area = wh[:,:,0] * wh[:,:,1] + upleft = np.concatenate([upleft] * B, 1) + botright = np.concatenate([botright] * B, 1) + areas = np.concatenate([area] * B, 1) + + # value for placeholder at input layer + inp_feed_val = img + # value for placeholder at loss layer + loss_feed_val = { + 'probs': probs, 'confs': confs, + 'coord': coord, 'proid': proid, + 'areas': areas, 'upleft': upleft, + 'botright': botright + } + + return inp_feed_val, loss_feed_val + +def shuffle(self): + batch = self.FLAGS.batch + data = self.parse() + size = len(data) + + print('Dataset of {} instance(s)'.format(size)) + if batch > size: self.FLAGS.batch = batch = size + batch_per_epoch = int(size / batch) + + for i in range(self.FLAGS.epoch): + shuffle_idx = perm(np.arange(size)) + for b in range(batch_per_epoch): + # yield these + x_batch = list() + feed_batch = dict() + + for j in range(b*batch, b*batch+batch): + train_instance = data[shuffle_idx[j]] + inp, new_feed = self._batch(train_instance) + + if inp is None: continue + x_batch += [np.expand_dims(inp, 0)] + + for key in new_feed: + new = new_feed[key] + old_feed = feed_batch.get(key, + np.zeros((0,) + new.shape)) + feed_batch[key] = np.concatenate([ + old_feed, [new] + ]) + + x_batch = np.concatenate(x_batch, 0) + yield x_batch, feed_batch + + print('Finish {} epoch(es)'.format(i + 1)) + diff --git a/darkflow/net/yolo/misc.py b/darkflow/net/yolo/misc.py new file mode 100644 index 0000000..c7aeae4 --- /dev/null +++ b/darkflow/net/yolo/misc.py @@ -0,0 +1,130 @@ +import pickle +import numpy as np +import cv2 +import os + +labels20 = ["aeroplane", "bicycle", "bird", "boat", "bottle", + "bus", "car", "cat", "chair", "cow", "diningtable", "dog", + "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", + "train", "tvmonitor"] + +# 8, 14, 15, 19 + +voc_models = ['yolo-full', 'yolo-tiny', 'yolo-small', # <- v1 + 'yolov1', 'tiny-yolov1', # <- v1.1 + 'tiny-yolo-voc', 'yolo-voc'] # <- v2 + +coco_models = ['tiny-coco', 'yolo-coco', # <- v1.1 + 'yolo', 'tiny-yolo'] # <- v2 + +coco_names = 'coco.names' +nine_names = '9k.names' + +def labels(meta, FLAGS): + model = os.path.basename(meta['name']) + if model in voc_models: + print("Model has a VOC model name, loading VOC labels.") + meta['labels'] = labels20 + else: + file = FLAGS.labels + if model in coco_models: + print("Model has a coco model name, loading coco labels.") + file = os.path.join(FLAGS.config, coco_names) + elif model == 'yolo9000': + print("Model has name yolo9000, loading yolo9000 labels.") + file = os.path.join(FLAGS.config, nine_names) + with open(file, 'r') as f: + meta['labels'] = list() + labs = [l.strip() for l in f.readlines()] + for lab in labs: + if lab == '----': break + meta['labels'] += [lab] + if len(meta['labels']) == 0: + meta['labels'] = labels20 + +def is_inp(self, name): + return name[-4:] in ['.jpg','.JPG', '.jpeg', '.JPEG', '.png', '.PNG'] + +def show(im, allobj, S, w, h, cellx, celly): + for obj in allobj: + a = obj[5] % S + b = obj[5] // S + cx = a + obj[1] + cy = b + obj[2] + centerx = cx * cellx + centery = cy * celly + ww = obj[3]**2 * w + hh = obj[4]**2 * h + cv2.rectangle(im, + (int(centerx - ww/2), int(centery - hh/2)), + (int(centerx + ww/2), int(centery + hh/2)), + (0,0,255), 2) + cv2.imshow("result", im) + cv2.waitKey() + cv2.destroyAllWindows() + +def show2(im, allobj): + for obj in allobj: + cv2.rectangle(im, + (obj[1], obj[2]), + (obj[3], obj[4]), + (0,0,255),2) + cv2.imshow('result', im) + cv2.waitKey() + cv2.destroyAllWindows() + + +_MVA = .05 + +def profile(self, net): + pass +# data = self.parse(exclusive = True) +# size = len(data); batch = self.FLAGS.batch +# all_inp_ = [x[0] for x in data] +# net.say('Will cycle through {} examples {} times'.format( +# len(all_inp_), net.FLAGS.epoch)) + +# fetch = list(); mvave = list(); names = list(); +# this = net.top +# conv_lay = ['convolutional', 'connected', 'local', 'conv-select'] +# while this.inp is not None: +# if this.lay.type in conv_lay: +# fetch = [this.out] + fetch +# names = [this.lay.signature] + names +# mvave = [None] + mvave +# this = this.inp +# print(names) + +# total = int(); allofthem = len(all_inp_) * net.FLAGS.epoch +# batch = min(net.FLAGS.batch, len(all_inp_)) +# for count in range(net.FLAGS.epoch): +# net.say('EPOCH {}'.format(count)) +# for j in range(len(all_inp_)/batch): +# inp_feed = list(); new_all = list() +# all_inp = all_inp_[j*batch: (j*batch+batch)] +# for inp in all_inp: +# new_all += [inp] +# this_inp = os.path.join(net.FLAGS.dataset, inp) +# this_inp = net.framework.preprocess(this_inp) +# expanded = np.expand_dims(this_inp, 0) +# inp_feed.append(expanded) +# all_inp = new_all +# feed_dict = {net.inp : np.concatenate(inp_feed, 0)} +# out = net.sess.run(fetch, feed_dict) + +# for i, o in enumerate(out): +# oi = out[i]; +# dim = len(oi.shape) - 1 +# ai = mvave[i]; +# mi = np.mean(oi, tuple(range(dim))) +# vi = np.var(oi, tuple(range(dim))) +# if ai is None: mvave[i] = [mi, vi] +# elif 'banana ninja yada yada': +# ai[0] = (1 - _MVA) * ai[0] + _MVA * mi +# ai[1] = (1 - _MVA) * ai[1] + _MVA * vi +# total += len(inp_feed) +# net.say('{} / {} = {}%'.format( +# total, allofthem, 100. * total / allofthem)) + +# with open('profile', 'wb') as f: +# pickle.dump([mvave], f, protocol = -1) diff --git a/darkflow/net/yolo/predict.py b/darkflow/net/yolo/predict.py new file mode 100644 index 0000000..de3f789 --- /dev/null +++ b/darkflow/net/yolo/predict.py @@ -0,0 +1,123 @@ +from ...utils.im_transform import imcv2_recolor, imcv2_affine_trans +from ...utils.box import BoundBox, box_iou, prob_compare +import numpy as np +import cv2 +import os +import json +from ...cython_utils.cy_yolo_findboxes import yolo_box_constructor + +def _fix(obj, dims, scale, offs): + for i in range(1, 5): + dim = dims[(i + 1) % 2] + off = offs[(i + 1) % 2] + obj[i] = int(obj[i] * scale - off) + obj[i] = max(min(obj[i], dim), 0) + +def resize_input(self, im): + h, w, c = self.meta['inp_size'] + imsz = cv2.resize(im, (w, h)) + imsz = imsz / 255. + imsz = imsz[:,:,::-1] + return imsz + +def process_box(self, b, h, w, threshold): + max_indx = np.argmax(b.probs) + max_prob = b.probs[max_indx] + label = self.meta['labels'][max_indx] + if max_prob > threshold: + left = int ((b.x - b.w/2.) * w) + right = int ((b.x + b.w/2.) * w) + top = int ((b.y - b.h/2.) * h) + bot = int ((b.y + b.h/2.) * h) + if left < 0 : left = 0 + if right > w - 1: right = w - 1 + if top < 0 : top = 0 + if bot > h - 1: bot = h - 1 + mess = '{}'.format(label) + return (left, right, top, bot, mess, max_indx, max_prob) + return None + +def findboxes(self, net_out): + meta, FLAGS = self.meta, self.FLAGS + threshold = FLAGS.threshold + + boxes = [] + boxes = yolo_box_constructor(meta, net_out, threshold) + + return boxes + +def preprocess(self, im, allobj = None): + """ + Takes an image, return it as a numpy tensor that is readily + to be fed into tfnet. If there is an accompanied annotation (allobj), + meaning this preprocessing is serving the train process, then this + image will be transformed with random noise to augment training data, + using scale, translation, flipping and recolor. The accompanied + parsed annotation (allobj) will also be modified accordingly. + """ + if type(im) is not np.ndarray: + im = cv2.imread(im) + + if allobj is not None: # in training mode + result = imcv2_affine_trans(im) + im, dims, trans_param = result + scale, offs, flip = trans_param + for obj in allobj: + _fix(obj, dims, scale, offs) + if not flip: continue + obj_1_ = obj[1] + obj[1] = dims[0] - obj[3] + obj[3] = dims[0] - obj_1_ + im = imcv2_recolor(im) + + im = self.resize_input(im) + if allobj is None: return im + return im#, np.array(im) # for unit testing + +def postprocess(self, net_out, im, save = True): + """ + Takes net output, draw predictions, save to disk + """ + meta, FLAGS = self.meta, self.FLAGS + threshold = FLAGS.threshold + colors, labels = meta['colors'], meta['labels'] + + boxes = self.findboxes(net_out) + + if type(im) is not np.ndarray: + imgcv = cv2.imread(im) + else: imgcv = im + + h, w, _ = imgcv.shape + resultsForJSON = [] + for b in boxes: + boxResults = self.process_box(b, h, w, threshold) + if boxResults is None: + continue + left, right, top, bot, mess, max_indx, confidence = boxResults + thick = int((h + w) // 300) + if self.FLAGS.json: + resultsForJSON.append({"label": mess, "confidence": float('%.2f' % confidence), "topleft": {"x": left, "y": top}, "bottomright": {"x": right, "y": bot}}) + continue + + cv2.rectangle(imgcv, + (left, top), (right, bot), + self.meta['colors'][max_indx], thick) + cv2.putText( + imgcv, mess, (left, top - 12), + 0, 1e-3 * h, self.meta['colors'][max_indx], + thick // 3) + + + if not save: return imgcv + + outfolder = os.path.join(self.FLAGS.imgdir, 'out') + img_name = os.path.join(outfolder, os.path.basename(im)) + if self.FLAGS.json: + textJSON = json.dumps(resultsForJSON) + textFile = os.path.splitext(img_name)[0] + ".json" + with open(textFile, 'w') as f: + f.write(textJSON) + return + + cv2.imwrite(img_name, imgcv) diff --git a/darkflow/net/yolo/train.py b/darkflow/net/yolo/train.py new file mode 100644 index 0000000..78d8a2f --- /dev/null +++ b/darkflow/net/yolo/train.py @@ -0,0 +1,92 @@ +import tensorflow.contrib.slim as slim +import pickle +import tensorflow as tf +from .misc import show +import numpy as np +import os + +def loss(self, net_out): + """ + Takes net.out and placeholders value + returned in batch() func above, + to build train_op and loss + """ + # meta + m = self.meta + sprob = float(m['class_scale']) + sconf = float(m['object_scale']) + snoob = float(m['noobject_scale']) + scoor = float(m['coord_scale']) + S, B, C = m['side'], m['num'], m['classes'] + SS = S * S # number of grid cells + + print('{} loss hyper-parameters:'.format(m['model'])) + print('\tside = {}'.format(m['side'])) + print('\tbox = {}'.format(m['num'])) + print('\tclasses = {}'.format(m['classes'])) + print('\tscales = {}'.format([sprob, sconf, snoob, scoor])) + + size1 = [None, SS, C] + size2 = [None, SS, B] + + # return the below placeholders + _probs = tf.placeholder(tf.float32, size1) + _confs = tf.placeholder(tf.float32, size2) + _coord = tf.placeholder(tf.float32, size2 + [4]) + # weights term for L2 loss + _proid = tf.placeholder(tf.float32, size1) + # material calculating IOU + _areas = tf.placeholder(tf.float32, size2) + _upleft = tf.placeholder(tf.float32, size2 + [2]) + _botright = tf.placeholder(tf.float32, size2 + [2]) + + self.placeholders = { + 'probs':_probs, 'confs':_confs, 'coord':_coord, 'proid':_proid, + 'areas':_areas, 'upleft':_upleft, 'botright':_botright + } + + # Extract the coordinate prediction from net.out + coords = net_out[:, SS * (C + B):] + coords = tf.reshape(coords, [-1, SS, B, 4]) + wh = tf.pow(coords[:,:,:,2:4], 2) * S # unit: grid cell + area_pred = wh[:,:,:,0] * wh[:,:,:,1] # unit: grid cell^2 + centers = coords[:,:,:,0:2] # [batch, SS, B, 2] + floor = centers - (wh * .5) # [batch, SS, B, 2] + ceil = centers + (wh * .5) # [batch, SS, B, 2] + + # calculate the intersection areas + intersect_upleft = tf.maximum(floor, _upleft) + intersect_botright = tf.minimum(ceil , _botright) + intersect_wh = intersect_botright - intersect_upleft + intersect_wh = tf.maximum(intersect_wh, 0.0) + intersect = tf.multiply(intersect_wh[:,:,:,0], intersect_wh[:,:,:,1]) + + # calculate the best IOU, set 0.0 confidence for worse boxes + iou = tf.truediv(intersect, _areas + area_pred - intersect) + best_box = tf.equal(iou, tf.reduce_max(iou, [2], True)) + best_box = tf.to_float(best_box) + confs = tf.multiply(best_box, _confs) + + # take care of the weight terms + conid = snoob * (1. - confs) + sconf * confs + weight_coo = tf.concat(4 * [tf.expand_dims(confs, -1)], 3) + cooid = scoor * weight_coo + proid = sprob * _proid + + # flatten 'em all + probs = slim.flatten(_probs) + proid = slim.flatten(proid) + confs = slim.flatten(confs) + conid = slim.flatten(conid) + coord = slim.flatten(_coord) + cooid = slim.flatten(cooid) + + self.fetch += [probs, confs, conid, cooid, proid] + true = tf.concat([probs, confs, coord], 1) + wght = tf.concat([proid, conid, cooid], 1) + print('Building {} loss'.format(m['model'])) + loss = tf.pow(net_out - true, 2) + loss = tf.multiply(loss, wght) + loss = tf.reduce_sum(loss, 1) + self.loss = .5 * tf.reduce_mean(loss) + tf.summary.scalar('{} loss'.format(m['model']), self.loss) diff --git a/darkflow/net/yolov2/__init__.py b/darkflow/net/yolov2/__init__.py new file mode 100644 index 0000000..41f89e5 --- /dev/null +++ b/darkflow/net/yolov2/__init__.py @@ -0,0 +1,5 @@ +from . import train +from . import predict +from . import data +from ..yolo import misc +import numpy as np diff --git a/darkflow/net/yolov2/data.py b/darkflow/net/yolov2/data.py new file mode 100644 index 0000000..fbfcdfc --- /dev/null +++ b/darkflow/net/yolov2/data.py @@ -0,0 +1,85 @@ +from ...utils.pascal_voc_clean_xml import pascal_voc_clean_xml +from numpy.random import permutation as perm +from ..yolo.predict import preprocess +from ..yolo.data import shuffle +from copy import deepcopy +import pickle +import numpy as np +import os + +def _batch(self, chunk): + """ + Takes a chunk of parsed annotations + returns value for placeholders of net's + input & loss layer correspond to this chunk + """ + meta = self.meta + labels = meta['labels'] + + H, W, _ = meta['out_size'] + C, B = meta['classes'], meta['num'] + anchors = meta['anchors'] + + # preprocess + jpg = chunk[0]; w, h, allobj_ = chunk[1] + allobj = deepcopy(allobj_) + path = os.path.join(self.FLAGS.dataset, jpg) + img = self.preprocess(path, allobj) + + # Calculate regression target + cellx = 1. * w / W + celly = 1. * h / H + for obj in allobj: + centerx = .5*(obj[1]+obj[3]) #xmin, xmax + centery = .5*(obj[2]+obj[4]) #ymin, ymax + cx = centerx / cellx + cy = centery / celly + if cx >= W or cy >= H: return None, None + obj[3] = float(obj[3]-obj[1]) / w + obj[4] = float(obj[4]-obj[2]) / h + obj[3] = np.sqrt(obj[3]) + obj[4] = np.sqrt(obj[4]) + obj[1] = cx - np.floor(cx) # centerx + obj[2] = cy - np.floor(cy) # centery + obj += [int(np.floor(cy) * W + np.floor(cx))] + + # show(im, allobj, S, w, h, cellx, celly) # unit test + + # Calculate placeholders' values + probs = np.zeros([H*W,B,C]) + confs = np.zeros([H*W,B]) + coord = np.zeros([H*W,B,4]) + proid = np.zeros([H*W,B,C]) + prear = np.zeros([H*W,4]) + for obj in allobj: + probs[obj[5], :, :] = [[0.]*C] * B + probs[obj[5], :, labels.index(obj[0])] = 1. + proid[obj[5], :, :] = [[1.]*C] * B + coord[obj[5], :, :] = [obj[1:5]] * B + prear[obj[5],0] = obj[1] - obj[3]**2 * .5 * W # xleft + prear[obj[5],1] = obj[2] - obj[4]**2 * .5 * H # yup + prear[obj[5],2] = obj[1] + obj[3]**2 * .5 * W # xright + prear[obj[5],3] = obj[2] + obj[4]**2 * .5 * H # ybot + confs[obj[5], :] = [1.] * B + + # Finalise the placeholders' values + upleft = np.expand_dims(prear[:,0:2], 1) + botright = np.expand_dims(prear[:,2:4], 1) + wh = botright - upleft; + area = wh[:,:,0] * wh[:,:,1] + upleft = np.concatenate([upleft] * B, 1) + botright = np.concatenate([botright] * B, 1) + areas = np.concatenate([area] * B, 1) + + # value for placeholder at input layer + inp_feed_val = img + # value for placeholder at loss layer + loss_feed_val = { + 'probs': probs, 'confs': confs, + 'coord': coord, 'proid': proid, + 'areas': areas, 'upleft': upleft, + 'botright': botright + } + + return inp_feed_val, loss_feed_val + diff --git a/darkflow/net/yolov2/predict.py b/darkflow/net/yolov2/predict.py new file mode 100644 index 0000000..b3485c4 --- /dev/null +++ b/darkflow/net/yolov2/predict.py @@ -0,0 +1,71 @@ +import numpy as np +import math +import cv2 +import os +import json +#from scipy.special import expit +#from utils.box import BoundBox, box_iou, prob_compare +#from utils.box import prob_compare2, box_intersection +from ...utils.box import BoundBox +from ...cython_utils.cy_yolo2_findboxes import box_constructor + +def expit(x): + return 1. / (1. + np.exp(-x)) + +def _softmax(x): + e_x = np.exp(x - np.max(x)) + out = e_x / e_x.sum() + return out + +def findboxes(self, net_out): + # meta + meta = self.meta + boxes = list() + boxes=box_constructor(meta,net_out) + return boxes + +def postprocess(self, net_out, im, save = True): + """ + Takes net output, draw net_out, save to disk + """ + boxes = self.findboxes(net_out) + + # meta + meta = self.meta + threshold = meta['thresh'] + colors = meta['colors'] + labels = meta['labels'] + if type(im) is not np.ndarray: + imgcv = cv2.imread(im) + else: imgcv = im + h, w, _ = imgcv.shape + + resultsForJSON = [] + for b in boxes: + boxResults = self.process_box(b, h, w, threshold) + if boxResults is None: + continue + left, right, top, bot, mess, max_indx, confidence = boxResults + thick = int((h + w) // 300) + if self.FLAGS.json: + resultsForJSON.append({"label": mess, "confidence": float('%.2f' % confidence), "topleft": {"x": left, "y": top}, "bottomright": {"x": right, "y": bot}}) + continue + + cv2.rectangle(imgcv, + (left, top), (right, bot), + colors[max_indx], thick) + cv2.putText(imgcv, mess, (left, top - 12), + 0, 1e-3 * h, colors[max_indx],thick//3) + + if not save: return imgcv + + outfolder = os.path.join(self.FLAGS.imgdir, 'out') + img_name = os.path.join(outfolder, os.path.basename(im)) + if self.FLAGS.json: + textJSON = json.dumps(resultsForJSON) + textFile = os.path.splitext(img_name)[0] + ".json" + with open(textFile, 'w') as f: + f.write(textJSON) + return + + cv2.imwrite(img_name, imgcv) diff --git a/darkflow/net/yolov2/train.py b/darkflow/net/yolov2/train.py new file mode 100644 index 0000000..a0bf715 --- /dev/null +++ b/darkflow/net/yolov2/train.py @@ -0,0 +1,107 @@ +import tensorflow.contrib.slim as slim +import pickle +import tensorflow as tf +from ..yolo.misc import show +import numpy as np +import os +import math + +def expit_tensor(x): + return 1. / (1. + tf.exp(-x)) + +def loss(self, net_out): + """ + Takes net.out and placeholders value + returned in batch() func above, + to build train_op and loss + """ + # meta + m = self.meta + sprob = float(m['class_scale']) + sconf = float(m['object_scale']) + snoob = float(m['noobject_scale']) + scoor = float(m['coord_scale']) + H, W, _ = m['out_size'] + B, C = m['num'], m['classes'] + HW = H * W # number of grid cells + anchors = m['anchors'] + + print('{} loss hyper-parameters:'.format(m['model'])) + print('\tH = {}'.format(H)) + print('\tW = {}'.format(W)) + print('\tbox = {}'.format(m['num'])) + print('\tclasses = {}'.format(m['classes'])) + print('\tscales = {}'.format([sprob, sconf, snoob, scoor])) + + size1 = [None, HW, B, C] + size2 = [None, HW, B] + + # return the below placeholders + _probs = tf.placeholder(tf.float32, size1) + _confs = tf.placeholder(tf.float32, size2) + _coord = tf.placeholder(tf.float32, size2 + [4]) + # weights term for L2 loss + _proid = tf.placeholder(tf.float32, size1) + # material calculating IOU + _areas = tf.placeholder(tf.float32, size2) + _upleft = tf.placeholder(tf.float32, size2 + [2]) + _botright = tf.placeholder(tf.float32, size2 + [2]) + + self.placeholders = { + 'probs':_probs, 'confs':_confs, 'coord':_coord, 'proid':_proid, + 'areas':_areas, 'upleft':_upleft, 'botright':_botright + } + + # Extract the coordinate prediction from net.out + net_out_reshape = tf.reshape(net_out, [-1, H, W, B, (4 + 1 + C)]) + coords = net_out_reshape[:, :, :, :, :4] + coords = tf.reshape(coords, [-1, H*W, B, 4]) + adjusted_coords_xy = expit_tensor(coords[:,:,:,0:2]) + adjusted_coords_wh = tf.sqrt(tf.exp(coords[:,:,:,2:4]) * np.reshape(anchors, [1, 1, B, 2]) / np.reshape([W, H], [1, 1, 1, 2])) + coords = tf.concat([adjusted_coords_xy, adjusted_coords_wh], 3) + + adjusted_c = expit_tensor(net_out_reshape[:, :, :, :, 4]) + adjusted_c = tf.reshape(adjusted_c, [-1, H*W, B, 1]) + + adjusted_prob = tf.nn.softmax(net_out_reshape[:, :, :, :, 5:]) + adjusted_prob = tf.reshape(adjusted_prob, [-1, H*W, B, C]) + + adjusted_net_out = tf.concat([adjusted_coords_xy, adjusted_coords_wh, adjusted_c, adjusted_prob], 3) + + wh = tf.pow(coords[:,:,:,2:4], 2) * np.reshape([W, H], [1, 1, 1, 2]) + area_pred = wh[:,:,:,0] * wh[:,:,:,1] + centers = coords[:,:,:,0:2] + floor = centers - (wh * .5) + ceil = centers + (wh * .5) + + # calculate the intersection areas + intersect_upleft = tf.maximum(floor, _upleft) + intersect_botright = tf.minimum(ceil , _botright) + intersect_wh = intersect_botright - intersect_upleft + intersect_wh = tf.maximum(intersect_wh, 0.0) + intersect = tf.multiply(intersect_wh[:,:,:,0], intersect_wh[:,:,:,1]) + + # calculate the best IOU, set 0.0 confidence for worse boxes + iou = tf.truediv(intersect, _areas + area_pred - intersect) + best_box = tf.equal(iou, tf.reduce_max(iou, [2], True)) + best_box = tf.to_float(best_box) + confs = tf.multiply(best_box, _confs) + + # take care of the weight terms + conid = snoob * (1. - confs) + sconf * confs + weight_coo = tf.concat(4 * [tf.expand_dims(confs, -1)], 3) + cooid = scoor * weight_coo + weight_pro = tf.concat(C * [tf.expand_dims(confs, -1)], 3) + proid = sprob * weight_pro + + self.fetch += [_probs, confs, conid, cooid, proid] + true = tf.concat([_coord, tf.expand_dims(confs, 3), _probs ], 3) + wght = tf.concat([cooid, tf.expand_dims(conid, 3), proid ], 3) + + print('Building {} loss'.format(m['model'])) + loss = tf.pow(adjusted_net_out - true, 2) + loss = tf.multiply(loss, wght) + loss = tf.reshape(loss, [-1, H*W*B*(4 + 1 + C)]) + loss = tf.reduce_sum(loss, 1) + self.loss = .5 * tf.reduce_mean(loss) + tf.summary.scalar('{} loss'.format(m['model']), self.loss) \ No newline at end of file diff --git a/darkflow/utils/box.py b/darkflow/utils/box.py new file mode 100644 index 0000000..5136647 --- /dev/null +++ b/darkflow/utils/box.py @@ -0,0 +1,44 @@ +import numpy as np + +class BoundBox: + def __init__(self, classes): + self.x, self.y = float(), float() + self.w, self.h = float(), float() + self.c = float() + self.class_num = classes + self.probs = np.zeros((classes,)) + +def overlap(x1,w1,x2,w2): + l1 = x1 - w1 / 2.; + l2 = x2 - w2 / 2.; + left = max(l1, l2) + r1 = x1 + w1 / 2.; + r2 = x2 + w2 / 2.; + right = min(r1, r2) + return right - left; + +def box_intersection(a, b): + w = overlap(a.x, a.w, b.x, b.w); + h = overlap(a.y, a.h, b.y, b.h); + if w < 0 or h < 0: return 0; + area = w * h; + return area; + +def box_union(a, b): + i = box_intersection(a, b); + u = a.w * a.h + b.w * b.h - i; + return u; + +def box_iou(a, b): + return box_intersection(a, b) / box_union(a, b); + +def prob_compare(box): + return box.probs[box.class_num] + +def prob_compare2(boxa, boxb): + if (boxa.pi < boxb.pi): + return 1 + elif(boxa.pi == boxb.pi): + return 0 + else: + return -1 \ No newline at end of file diff --git a/darkflow/utils/im_transform.py b/darkflow/utils/im_transform.py new file mode 100644 index 0000000..249e27f --- /dev/null +++ b/darkflow/utils/im_transform.py @@ -0,0 +1,31 @@ +import numpy as np +import cv2 + +def imcv2_recolor(im, a = .1): + t = [np.random.uniform()] + t += [np.random.uniform()] + t += [np.random.uniform()] + t = np.array(t) * 2. - 1. + + # random amplify each channel + im = im * (1 + t * a) + mx = 255. * (1 + a) + up = np.random.uniform() * 2 - 1 +# im = np.power(im/mx, 1. + up * .5) + im = cv2.pow(im/mx, 1. + up * .5) + return np.array(im * 255., np.uint8) + +def imcv2_affine_trans(im): + # Scale and translate + h, w, c = im.shape + scale = np.random.uniform() / 10. + 1. + max_offx = (scale-1.) * w + max_offy = (scale-1.) * h + offx = int(np.random.uniform() * max_offx) + offy = int(np.random.uniform() * max_offy) + + im = cv2.resize(im, (0,0), fx = scale, fy = scale) + im = im[offy : (offy + h), offx : (offx + w)] + flip = np.random.binomial(1, .5) + if flip: im = cv2.flip(im, 1) + return im, [w, h, c], [scale, [offx, offy], flip] diff --git a/darkflow/utils/loader.py b/darkflow/utils/loader.py new file mode 100644 index 0000000..723560d --- /dev/null +++ b/darkflow/utils/loader.py @@ -0,0 +1,152 @@ +import tensorflow as tf +import os +from .. import dark +import numpy as np +from os.path import basename + +class loader(object): + """ + interface to work with both .weights and .ckpt files + in loading / recollecting / resolving mode + """ + VAR_LAYER = ['convolutional', 'connected', 'local', + 'select', 'conv-select', + 'extract', 'conv-extract'] + + def __init__(self, *args): + self.src_key = list() + self.vals = list() + self.load(*args) + + def __call__(self, key): + for idx in range(len(key)): + val = self.find(key, idx) + if val is not None: return val + return None + + def find(self, key, idx): + up_to = min(len(self.src_key), 4) + for i in range(up_to): + key_b = self.src_key[i] + if key_b[idx:] == key[idx:]: + return self.yields(i) + return None + + def yields(self, idx): + del self.src_key[idx] + temp = self.vals[idx] + del self.vals[idx] + return temp + +class weights_loader(loader): + """one who understands .weights files""" + + _W_ORDER = dict({ # order of param flattened into .weights file + 'convolutional': [ + 'biases','gamma','moving_mean','moving_variance','kernel' + ], + 'connected': ['biases', 'weights'], + 'local': ['biases', 'kernels'] + }) + + def load(self, path, src_layers): + self.src_layers = src_layers + walker = weights_walker(path) + + for i, layer in enumerate(src_layers): + if layer.type not in self.VAR_LAYER: continue + self.src_key.append([layer]) + + if walker.eof: new = None + else: + args = layer.signature + new = dark.darknet.create_darkop(*args) + self.vals.append(new) + + if new is None: continue + order = self._W_ORDER[new.type] + for par in order: + if par not in new.wshape: continue + val = walker.walk(new.wsize[par]) + new.w[par] = val + new.finalize(walker.transpose) + + if walker.path is not None: + assert walker.offset == walker.size, \ + 'expect {} bytes, found {}'.format( + walker.offset, walker.size) + print('Successfully identified {} bytes'.format( + walker.offset)) + +class checkpoint_loader(loader): + """ + one who understands .ckpt files, very much + """ + def load(self, ckpt, ignore): + meta = ckpt + '.meta' + with tf.Graph().as_default() as graph: + with tf.Session().as_default() as sess: + saver = tf.train.import_meta_graph(meta) + saver.restore(sess, ckpt) + for var in tf.global_variables(): + name = var.name.split(':')[0] + packet = [name, var.get_shape().as_list()] + self.src_key += [packet] + self.vals += [var.eval(sess)] + +def create_loader(path, cfg = None): + if path is None: + load_type = weights_loader + elif '.weights' in path: + load_type = weights_loader + else: + load_type = checkpoint_loader + + return load_type(path, cfg) + +class weights_walker(object): + """incremental reader of float32 binary files""" + def __init__(self, path): + self.eof = False # end of file + self.path = path # current pos + if path is None: + self.eof = True + return + else: + self.size = os.path.getsize(path)# save the path + major, minor, revision, seen = np.memmap(path, + shape = (), mode = 'r', offset = 0, + dtype = '({})i4,'.format(4)) + self.transpose = major > 1000 or minor > 1000 + self.offset = 16 + + def walk(self, size): + if self.eof: return None + end_point = self.offset + 4 * size + assert end_point <= self.size, \ + 'Over-read {}'.format(self.path) + + float32_1D_array = np.memmap( + self.path, shape = (), mode = 'r', + offset = self.offset, + dtype='({})float32,'.format(size) + ) + + self.offset = end_point + if end_point == self.size: + self.eof = True + return float32_1D_array + +def model_name(file_path): + file_name = basename(file_path) + ext = str() + if '.' in file_name: # exclude extension + file_name = file_name.split('.') + ext = file_name[-1] + file_name = '.'.join(file_name[:-1]) + if ext == str() or ext == 'meta': # ckpt file + file_name = file_name.split('-') + num = int(file_name[-1]) + return '-'.join(file_name[:-1]) + if ext == 'weights': + return file_name \ No newline at end of file diff --git a/darkflow/utils/pascal_voc_clean_xml.py b/darkflow/utils/pascal_voc_clean_xml.py new file mode 100644 index 0000000..70eb3e0 --- /dev/null +++ b/darkflow/utils/pascal_voc_clean_xml.py @@ -0,0 +1,79 @@ +""" +parse PASCAL VOC xml annotations +""" + +import os +import sys +import xml.etree.ElementTree as ET +import glob + + +def _pp(l): # pretty printing + for i in l: print('{}: {}'.format(i,l[i])) + +def pascal_voc_clean_xml(ANN, pick, exclusive = False): + print('Parsing for {} {}'.format( + pick, 'exclusively' * int(exclusive))) + + dumps = list() + cur_dir = os.getcwd() + os.chdir(ANN) + annotations = os.listdir('.') + annotations = glob.glob(str(annotations)+'*.xml') + size = len(annotations) + + for i, file in enumerate(annotations): + # progress bar + sys.stdout.write('\r') + percentage = 1. * (i+1) / size + progress = int(percentage * 20) + bar_arg = [progress*'=', ' '*(19-progress), percentage*100] + bar_arg += [file] + sys.stdout.write('[{}>{}]{:.0f}% {}'.format(*bar_arg)) + sys.stdout.flush() + + # actual parsing + in_file = open(file) + tree=ET.parse(in_file) + root = tree.getroot() + jpg = str(root.find('filename').text) + imsize = root.find('size') + w = int(imsize.find('width').text) + h = int(imsize.find('height').text) + all = list() + + for obj in root.iter('object'): + current = list() + name = obj.find('name').text + if name not in pick: + continue + + xmlbox = obj.find('bndbox') + xn = int(float(xmlbox.find('xmin').text)) + xx = int(float(xmlbox.find('xmax').text)) + yn = int(float(xmlbox.find('ymin').text)) + yx = int(float(xmlbox.find('ymax').text)) + current = [name,xn,yn,xx,yx] + all += [current] + + add = [[jpg, [w, h, all]]] + dumps += add + in_file.close() + + # gather all stats + stat = dict() + for dump in dumps: + all = dump[1][2] + for current in all: + if current[0] in pick: + if current[0] in stat: + stat[current[0]]+=1 + else: + stat[current[0]] =1 + + print('\nStatistics:') + _pp(stat) + print('Dataset size: {}'.format(len(dumps))) + + os.chdir(cur_dir) + return dumps \ No newline at end of file diff --git a/darkflow/utils/process.py b/darkflow/utils/process.py new file mode 100644 index 0000000..0836f48 --- /dev/null +++ b/darkflow/utils/process.py @@ -0,0 +1,321 @@ +""" +WARNING: spaghetti code. +""" + +import numpy as np +import pickle +import os + +def parser(model): + """ + Read the .cfg file to extract layers into `layers` + as well as model-specific parameters into `meta` + """ + def _parse(l, i = 1): + return l.split('=')[i].strip() + + with open(model, 'rb') as f: + lines = f.readlines() + + lines = [line.decode() for line in lines] + + meta = dict(); layers = list() # will contains layers' info + h, w, c = [int()] * 3; layer = dict() + for line in lines: + line = line.strip() + line = line.split('#')[0] + if '[' in line: + if layer != dict(): + if layer['type'] == '[net]': + h = layer['height'] + w = layer['width'] + c = layer['channels'] + meta['net'] = layer + else: + if layer['type'] == '[crop]': + h = layer['crop_height'] + w = layer['crop_width'] + layers += [layer] + layer = {'type': line} + else: + try: + i = float(_parse(line)) + if i == int(i): i = int(i) + layer[line.split('=')[0].strip()] = i + except: + try: + key = _parse(line, 0) + val = _parse(line, 1) + layer[key] = val + except: + 'banana ninja yadayada' + + meta.update(layer) # last layer contains meta info + if 'anchors' in meta: + splits = meta['anchors'].split(',') + anchors = [float(x.strip()) for x in splits] + meta['anchors'] = anchors + meta['model'] = model # path to cfg, not model name + meta['inp_size'] = [h, w, c] + return layers, meta + +def cfg_yielder(model, binary): + """ + yielding each layer information to initialize `layer` + """ + layers, meta = parser(model); yield meta; + h, w, c = meta['inp_size']; l = w * h * c + + # Start yielding + flat = False # flag for 1st dense layer + conv = '.conv.' in model + for i, d in enumerate(layers): + #----------------------------------------------------- + if d['type'] == '[crop]': + yield ['crop', i] + #----------------------------------------------------- + elif d['type'] == '[local]': + n = d.get('filters', 1) + size = d.get('size', 1) + stride = d.get('stride', 1) + pad = d.get('pad', 0) + activation = d.get('activation', 'logistic') + w_ = (w - 1 - (1 - pad) * (size - 1)) // stride + 1 + h_ = (h - 1 - (1 - pad) * (size - 1)) // stride + 1 + yield ['local', i, size, c, n, stride, + pad, w_, h_, activation] + if activation != 'linear': yield [activation, i] + w, h, c = w_, h_, n + l = w * h * c + #----------------------------------------------------- + elif d['type'] == '[convolutional]': + n = d.get('filters', 1) + size = d.get('size', 1) + stride = d.get('stride', 1) + pad = d.get('pad', 0) + padding = d.get('padding', 0) + if pad: padding = size // 2 + activation = d.get('activation', 'logistic') + batch_norm = d.get('batch_normalize', 0) or conv + yield ['convolutional', i, size, c, n, + stride, padding, batch_norm, + activation] + if activation != 'linear': yield [activation, i] + w_ = (w + 2 * padding - size) // stride + 1 + h_ = (h + 2 * padding - size) // stride + 1 + w, h, c = w_, h_, n + l = w * h * c + #----------------------------------------------------- + elif d['type'] == '[maxpool]': + stride = d.get('stride', 1) + size = d.get('size', stride) + padding = d.get('padding', (size-1) // 2) + yield ['maxpool', i, size, stride, padding] + w_ = (w + 2*padding) // d['stride'] + h_ = (h + 2*padding) // d['stride'] + w, h = w_, h_ + l = w * h * c + #----------------------------------------------------- + elif d['type'] == '[avgpool]': + flat = True; l = c + yield ['avgpool', i] + #----------------------------------------------------- + elif d['type'] == '[softmax]': + yield ['softmax', i, d['groups']] + #----------------------------------------------------- + elif d['type'] == '[connected]': + if not flat: + yield ['flatten', i] + flat = True + activation = d.get('activation', 'logistic') + yield ['connected', i, l, d['output'], activation] + if activation != 'linear': yield [activation, i] + l = d['output'] + #----------------------------------------------------- + elif d['type'] == '[dropout]': + yield ['dropout', i, d['probability']] + #----------------------------------------------------- + elif d['type'] == '[select]': + if not flat: + yield ['flatten', i] + flat = True + inp = d.get('input', None) + if type(inp) is str: + file = inp.split(',')[0] + layer_num = int(inp.split(',')[1]) + with open(file, 'rb') as f: + profiles = pickle.load(f, encoding = 'latin1')[0] + layer = profiles[layer_num] + else: layer = inp + activation = d.get('activation', 'logistic') + d['keep'] = d['keep'].split('/') + classes = int(d['keep'][-1]) + keep = [int(c) for c in d['keep'][0].split(',')] + keep_n = len(keep) + train_from = classes * d['bins'] + for count in range(d['bins']-1): + for num in keep[-keep_n:]: + keep += [num + classes] + k = 1 + while layers[i-k]['type'] not in ['[connected]', '[extract]']: + k += 1 + if i-k < 0: + break + if i-k < 0: l_ = l + elif layers[i-k]['type'] == 'connected': + l_ = layers[i-k]['output'] + else: + l_ = layers[i-k].get('old',[l])[-1] + yield ['select', i, l_, d['old_output'], + activation, layer, d['output'], + keep, train_from] + if activation != 'linear': yield [activation, i] + l = d['output'] + #----------------------------------------------------- + elif d['type'] == '[conv-select]': + n = d.get('filters', 1) + size = d.get('size', 1) + stride = d.get('stride', 1) + pad = d.get('pad', 0) + padding = d.get('padding', 0) + if pad: padding = size // 2 + activation = d.get('activation', 'logistic') + batch_norm = d.get('batch_normalize', 0) or conv + d['keep'] = d['keep'].split('/') + classes = int(d['keep'][-1]) + keep = [int(x) for x in d['keep'][0].split(',')] + + segment = classes + 5 + assert n % segment == 0, \ + 'conv-select: segment failed' + bins = n // segment + keep_idx = list() + for j in range(bins): + offset = j * segment + for k in range(5): + keep_idx += [offset + k] + for k in keep: + keep_idx += [offset + 5 + k] + w_ = (w + 2 * padding - size) // stride + 1 + h_ = (h + 2 * padding - size) // stride + 1 + c_ = len(keep_idx) + yield ['conv-select', i, size, c, n, + stride, padding, batch_norm, + activation, keep_idx, c_] + w, h, c = w_, h_, c_ + l = w * h * c + #----------------------------------------------------- + elif d['type'] == '[conv-extract]': + file = d['profile'] + with open(file, 'rb') as f: + profiles = pickle.load(f, encoding = 'latin1')[0] + inp_layer = None + inp = d['input'] + out = d['output'] + inp_layer = None + if inp >= 0: + inp_layer = profiles[inp] + if inp_layer is not None: + assert len(inp_layer) == c, \ + 'Conv-extract does not match input dimension' + out_layer = profiles[out] + + n = d.get('filters', 1) + size = d.get('size', 1) + stride = d.get('stride', 1) + pad = d.get('pad', 0) + padding = d.get('padding', 0) + if pad: padding = size // 2 + activation = d.get('activation', 'logistic') + batch_norm = d.get('batch_normalize', 0) or conv + + k = 1 + find = ['[convolutional]','[conv-extract]'] + while layers[i-k]['type'] not in find: + k += 1 + if i-k < 0: break + if i-k >= 0: + previous_layer = layers[i-k] + c_ = previous_layer['filters'] + else: + c_ = c + + yield ['conv-extract', i, size, c_, n, + stride, padding, batch_norm, + activation, inp_layer, out_layer] + if activation != 'linear': yield [activation, i] + w_ = (w + 2 * padding - size) // stride + 1 + h_ = (h + 2 * padding - size) // stride + 1 + w, h, c = w_, h_, len(out_layer) + l = w * h * c + #----------------------------------------------------- + elif d['type'] == '[extract]': + if not flat: + yield['flatten', i] + flat = True + activation = d.get('activation', 'logistic') + file = d['profile'] + with open(file, 'rb') as f: + profiles = pickle.load(f, encoding = 'latin1')[0] + inp_layer = None + inp = d['input'] + out = d['output'] + if inp >= 0: + inp_layer = profiles[inp] + out_layer = profiles[out] + old = d['old'] + old = [int(x) for x in old.split(',')] + if inp_layer is not None: + if len(old) > 2: + h_, w_, c_, n_ = old + new_inp = list() + for p in range(c_): + for q in range(h_): + for r in range(w_): + if p not in inp_layer: + continue + new_inp += [r + w*(q + h*p)] + inp_layer = new_inp + old = [h_ * w_ * c_, n_] + assert len(inp_layer) == l, \ + 'Extract does not match input dimension' + d['old'] = old + yield ['extract', i] + old + [activation] + [inp_layer, out_layer] + if activation != 'linear': yield [activation, i] + l = len(out_layer) + #----------------------------------------------------- + elif d['type'] == '[route]': # add new layer here + routes = d['layers'] + if type(routes) is int: + routes = [routes] + else: + routes = [int(x.strip()) for x in routes.split(',')] + routes = [i + x if x < 0 else x for x in routes] + for j, x in enumerate(routes): + lx = layers[x]; + xtype = lx['type'] + _size = lx['_size'][:3] + if j == 0: + h, w, c = _size + else: + h_, w_, c_ = _size + assert w_ == w and h_ == h, \ + 'Routing incompatible conv sizes' + c += c_ + yield ['route', i, routes] + l = w * h * c + #----------------------------------------------------- + elif d['type'] == '[reorg]': + stride = d.get('stride', 1) + yield ['reorg', i, stride] + w = w // stride; h = h // stride; + c = c * (stride ** 2) + l = w * h * c + #----------------------------------------------------- + else: + exit('Layer {} not implemented'.format(d['type'])) + + d['_size'] = list([h, w, c, l, flat]) + + if not flat: meta['out_size'] = [h, w, c] + else: meta['out_size'] = l \ No newline at end of file diff --git a/darkflow/version.py b/darkflow/version.py new file mode 100644 index 0000000..163f7d8 --- /dev/null +++ b/darkflow/version.py @@ -0,0 +1,2 @@ +__version__ = '1.0.0' +"""Current version of darkflow.""" \ No newline at end of file diff --git a/flow b/flow new file mode 100644 index 0000000..e023989 --- /dev/null +++ b/flow @@ -0,0 +1,7 @@ +#! /usr/bin/env python + +import sys +from darkflow.cli import cliHandler + +cliHandler(sys.argv) + diff --git a/labels.txt b/labels.txt new file mode 100644 index 0000000..c42bfe3 --- /dev/null +++ b/labels.txt @@ -0,0 +1,4 @@ +aeroplane +bicycle +bird +boat \ No newline at end of file diff --git a/preview.png b/preview.png new file mode 100644 index 0000000..2827ee6 Binary files /dev/null and b/preview.png differ diff --git a/sample_img/sample_computer.jpg b/sample_img/sample_computer.jpg new file mode 100644 index 0000000..1a3b717 Binary files /dev/null and b/sample_img/sample_computer.jpg differ diff --git a/sample_img/sample_dog.jpg b/sample_img/sample_dog.jpg new file mode 100644 index 0000000..77b0381 Binary files /dev/null and b/sample_img/sample_dog.jpg differ diff --git a/sample_img/sample_eagle.jpg b/sample_img/sample_eagle.jpg new file mode 100644 index 0000000..8b75095 Binary files /dev/null and b/sample_img/sample_eagle.jpg differ diff --git a/sample_img/sample_giraffe.jpg b/sample_img/sample_giraffe.jpg new file mode 100644 index 0000000..a93e8b8 Binary files /dev/null and b/sample_img/sample_giraffe.jpg differ diff --git a/sample_img/sample_horses.jpg b/sample_img/sample_horses.jpg new file mode 100644 index 0000000..3a761f4 Binary files /dev/null and b/sample_img/sample_horses.jpg differ diff --git a/sample_img/sample_office.jpg b/sample_img/sample_office.jpg new file mode 100644 index 0000000..fef4f53 Binary files /dev/null and b/sample_img/sample_office.jpg differ diff --git a/sample_img/sample_person.jpg b/sample_img/sample_person.jpg new file mode 100644 index 0000000..61d377f Binary files /dev/null and b/sample_img/sample_person.jpg differ diff --git a/sample_img/sample_scream.jpg b/sample_img/sample_scream.jpg new file mode 100644 index 0000000..43f2c36 Binary files /dev/null and b/sample_img/sample_scream.jpg differ diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..d3d255e --- /dev/null +++ b/setup.py @@ -0,0 +1,74 @@ +from setuptools import setup, find_packages +from setuptools.extension import Extension +from Cython.Build import cythonize +import numpy +import os +import imp + +VERSION = imp.load_source('version', os.path.join('.', 'darkflow', 'version.py')) +VERSION = VERSION.__version__ + +if os.name =='nt' : + ext_modules=[ + Extension("darkflow.cython_utils.nms", + sources=["darkflow/cython_utils/nms.pyx"], + #libraries=["m"] # Unix-like specific + include_dirs=[numpy.get_include()] + ), + Extension("darkflow.cython_utils.cy_yolo2_findboxes", + sources=["darkflow/cython_utils/cy_yolo2_findboxes.pyx"], + #libraries=["m"] # Unix-like specific + include_dirs=[numpy.get_include()] + ), + Extension("darkflow.cython_utils.cy_yolo_findboxes", + sources=["darkflow/cython_utils/cy_yolo_findboxes.pyx"], + #libraries=["m"] # Unix-like specific + include_dirs=[numpy.get_include()] + ) + ] + +elif os.name =='posix' : + ext_modules=[ + Extension("darkflow.cython_utils.nms", + sources=["darkflow/cython_utils/nms.pyx"], + libraries=["m"], # Unix-like specific + include_dirs=[numpy.get_include()] + ), + Extension("darkflow.cython_utils.cy_yolo2_findboxes", + sources=["darkflow/cython_utils/cy_yolo2_findboxes.pyx"], + libraries=["m"], # Unix-like specific + include_dirs=[numpy.get_include()] + ), + Extension("darkflow.cython_utils.cy_yolo_findboxes", + sources=["darkflow/cython_utils/cy_yolo_findboxes.pyx"], + libraries=["m"], # Unix-like specific + include_dirs=[numpy.get_include()] + ) + ] + +else : + ext_modules=[ + Extension("darkflow.cython_utils.nms", + sources=["darkflow/cython_utils/nms.pyx"], + libraries=["m"] # Unix-like specific + ), + Extension("darkflow.cython_utils.cy_yolo2_findboxes", + sources=["darkflow/cython_utils/cy_yolo2_findboxes.pyx"], + libraries=["m"] # Unix-like specific + ), + Extension("darkflow.cython_utils.cy_yolo_findboxes", + sources=["darkflow/cython_utils/cy_yolo_findboxes.pyx"], + libraries=["m"] # Unix-like specific + ) + ] + +setup( + version=VERSION, + name='darkflow', + description='Darkflow', + license='GPLv3', + url='https://github.com/thtrieu/darkflow', + packages = find_packages(), + scripts = ['flow'], + ext_modules = cythonize(ext_modules) +) \ No newline at end of file diff --git a/test/requirements-testing.txt b/test/requirements-testing.txt new file mode 100644 index 0000000..5c73070 --- /dev/null +++ b/test/requirements-testing.txt @@ -0,0 +1,8 @@ +tensorflow +pytest +requests +opencv-python +numpy +Cython +codecov +pytest-cov \ No newline at end of file diff --git a/test/test_darkflow.py b/test/test_darkflow.py new file mode 100644 index 0000000..b3ed38c --- /dev/null +++ b/test/test_darkflow.py @@ -0,0 +1,220 @@ +from darkflow.net.build import TFNet +from darkflow.cli import cliHandler +import json +import requests +import cv2 +import os +import sys +import pytest + +#NOTE: This file is designed to be run in the TravisCI environment. If you want to run it locally set the environment variable TRAVIS_BUILD_DIR to the base +# directory of the cloned darkflow repository. WARNING: This file delete images from sample_img/ that won't be used for testing (so don't run it +# locally if you don't want this happening!) + +#Settings +buildPath = os.environ.get("TRAVIS_BUILD_DIR") + +if buildPath is None: + print() + print("TRAVIS_BUILD_DIR environment variable was not found - is this running on TravisCI?") + print("If you want to test this locally, set TRAVIS_BUILD_DIR to the base directory of the cloned darkflow repository.") + exit() + +testImg = {"path": os.path.join(buildPath, "sample_img", "sample_person.jpg"), "width": 640, "height": 424, + "expected-objects": {"yolo-small": [{"label": "dog", "confidence": 0.46, "topleft": {"x": 84, "y": 249}, "bottomright": {"x": 208, "y": 367}}, + {"label": "person", "confidence": 0.60, "topleft": {"x": 159, "y": 102}, "bottomright": {"x": 304, "y": 365}}], + "yolo": [{"label": "person", "confidence": 0.82, "topleft": {"x": 189, "y": 96}, "bottomright": {"x": 271, "y": 380}}, + {"label": "dog", "confidence": 0.79, "topleft": {"x": 69, "y": 258}, "bottomright": {"x": 209, "y": 354}}, + {"label": "horse", "confidence": 0.89, "topleft": {"x": 397, "y": 127}, "bottomright": {"x": 605, "y": 352}}]}} + +trainImgBikePerson = {"path": os.path.join(buildPath, "test", "training", "images", "1.jpg"), "width": 500, "height": 375, + "expected-objects": {"tiny-yolo-voc": [{"label":"bicycle","confidence":0.35,"topleft":{"x":121,"y":126},"bottomright":{"x":233,"y":244}}, + {"label":"person","confidence":0.60,"topleft":{"x":132,"y":35},"bottomright":{"x":232,"y":165}}]}} + +trainImgHorsePerson = {"path": os.path.join(buildPath, "test", "training", "images", "2.jpg"), "width": 500, "height": 332, + "expected-objects": {"tiny-yolo-voc": [{"label":"horse","confidence":0.99,"topleft":{"x":156,"y":108},"bottomright":{"x":410,"y":281}}, + {"label":"person","confidence":0.89,"topleft":{"x":258,"y":52},"bottomright":{"x":300,"y":218}}]}} + + +posCompareThreshold = 0.05 #Comparisons must match be within 5% of width/height when compared to expected value +threshCompareThreshold = 0.1 #Comparisons must match within 0.1 of expected threshold for each prediction + +yolo_small_Download = "https://pjreddie.com/media/files/yolo-small.weights" #YOLOv1 +yolo_Download = "https://pjreddie.com/media/files/yolo.weights" #YOLOv2 +tiny_yolo_voc_Download = "https://pjreddie.com/media/files/tiny-yolo-voc.weights" #YOLOv2 + +def download_file(url, savePath): + fileName = savePath.split("/")[-1] + if not os.path.isfile(savePath): + os.makedirs(os.path.dirname(savePath), exist_ok=True) #Make directories nessecary for file incase they don't exist + print("Downloading " + fileName + " file...") + r = requests.get(url, stream=True) + with open(savePath, 'wb') as f: + for chunk in r.iter_content(chunk_size=1024): + if chunk: # filter out keep-alive new chunks + f.write(chunk) + r.close() + else: + print("Found existing " + fileName + " file.") + +yolo_small_WeightPath = os.path.join(buildPath, "bin", yolo_small_Download.split("/")[-1]) +yolo_small_CfgPath = os.path.join(buildPath, "cfg", "v1", "{0}.cfg".format(os.path.splitext(os.path.basename(yolo_small_WeightPath))[0])) + +yolo_WeightPath = os.path.join(buildPath, "bin", yolo_Download.split("/")[-1]) +yolo_CfgPath = os.path.join(buildPath, "cfg", "{0}.cfg".format(os.path.splitext(os.path.basename(yolo_WeightPath))[0])) + +tiny_yolo_voc_WeightPath = os.path.join(buildPath, "bin", tiny_yolo_voc_Download.split("/")[-1]) +tiny_yolo_voc_CfgPath = os.path.join(buildPath, "cfg", "{0}.cfg".format(os.path.splitext(os.path.basename(tiny_yolo_voc_WeightPath))[0])) + +pbPath = os.path.join(buildPath, "built_graph", os.path.splitext(os.path.basename(yolo_WeightPath))[0] + ".pb") +metaPath = os.path.join(buildPath, "built_graph", os.path.splitext(os.path.basename(yolo_WeightPath))[0] + ".meta") + +generalConfigPath = os.path.join(buildPath, "cfg") + +download_file(yolo_small_Download, yolo_small_WeightPath) #Check if we need to download (and if so download) the yolo-small weights (YOLOv1) +download_file(yolo_Download, yolo_WeightPath) #Check if we need to download (and if so download) the yolo weights (YOLOv2) +download_file(tiny_yolo_voc_Download, tiny_yolo_voc_WeightPath) #Check if we need to download (and if so download) the tiny-yolo-voc weights (YOLOv2) + +def executeCLI(commandString): + print() + print("Executing: {0}".format(commandString)) + print() + splitArgs = [item.strip() for item in commandString.split(" ")] + cliHandler(splitArgs) #Run the command + print() + +def compareSingleObjects(firstObject, secondObject, width, height, threshCompare, posCompare): + if(firstObject["label"] != secondObject["label"]): + return False + if(abs(firstObject["topleft"]["x"] - secondObject["topleft"]["x"]) > width * posCompare): + return False + if(abs(firstObject["topleft"]["y"] - secondObject["topleft"]["y"]) > height * posCompare): + return False + if(abs(firstObject["bottomright"]["x"] - secondObject["bottomright"]["x"]) > width * posCompare): + return False + if(abs(firstObject["bottomright"]["y"] - secondObject["bottomright"]["y"]) > height * posCompare): + return False + if(abs(firstObject["confidence"] - secondObject["confidence"]) > threshCompare): + return False + return True + +def compareObjectData(defaultObjects, newObjects, width, height, threshCompare, posCompare): + currentlyFound = False + for firstObject in defaultObjects: + currentlyFound = False + for secondObject in newObjects: + if compareSingleObjects(firstObject, secondObject, width, height, threshCompare, posCompare): + currentlyFound = True + break + if not currentlyFound: + return False + return True + +#Delete all images that won't be tested on so forwarding the whole folder doesn't take forever +filelist = [f for f in os.listdir(os.path.dirname(testImg["path"])) if os.path.isfile(os.path.join(os.path.dirname(testImg["path"]), f)) and f != os.path.basename(testImg["path"])] +for f in filelist: + os.remove(os.path.join(os.path.dirname(testImg["path"]), f)) + + +#TESTS FOR INFERENCE +def test_CLI_IMG_YOLOv2(): + #Test predictions outputted to an image using the YOLOv2 model through CLI + #NOTE: This test currently does not verify anything about the image created (i.e. proper labeling, proper positioning of prediction boxes, etc.) + # it simply verifies that the code executes properly and that the expected output image is indeed created in ./test/img/out + + testString = "flow --imgdir {0} --model {1} --load {2} --config {3} --threshold 0.4".format(os.path.dirname(testImg["path"]), yolo_CfgPath, yolo_WeightPath, generalConfigPath) + executeCLI(testString) + + outputImgPath = os.path.join(os.path.dirname(testImg["path"]), "out", os.path.basename(testImg["path"])) + assert os.path.exists(outputImgPath), "Expected output image: {0} was not found.".format(outputImgPath) + os.remove(outputImgPath) #Remove the image so that it does not affect subsequent tests + +def test_CLI_JSON_YOLOv2(): + #Test predictions outputted to a JSON file using the YOLOv2 model through CLI + #NOTE: This test verifies that the code executes properly, the JSON file is created properly and the predictions generated are within a certain + # margin of error when compared to the expected predictions. + + testString = "flow --imgdir {0} --model {1} --load {2} --config {3} --threshold 0.4 --json".format(os.path.dirname(testImg["path"]), yolo_CfgPath, yolo_WeightPath, generalConfigPath) + executeCLI(testString) + + outputJSONPath = os.path.join(os.path.dirname(testImg["path"]), "out", os.path.splitext(os.path.basename(testImg["path"]))[0] + ".json") + assert os.path.exists(outputJSONPath), "Expected output JSON file: {0} was not found.".format(outputJSONPath) + + with open(outputJSONPath) as json_file: + loadedPredictions = json.load(json_file) + + assert compareObjectData(testImg["expected-objects"]["yolo"], loadedPredictions, testImg["width"], testImg["height"], threshCompareThreshold, posCompareThreshold), "Generated object predictions from JSON were not within margin of error compared to expected values." + os.remove(outputJSONPath) #Remove the JSON file so that it does not affect subsequent tests + +def test_CLI_JSON_YOLOv1(): + #Test predictions outputted to a JSON file using the YOLOv1 model through CLI + #NOTE: This test verifies that the code executes properly, the JSON file is created properly and the predictions generated are within a certain + # margin of error when compared to the expected predictions. + + testString = "flow --imgdir {0} --model {1} --load {2} --config {3} --threshold 0.4 --json".format(os.path.dirname(testImg["path"]), yolo_small_CfgPath, yolo_small_WeightPath, generalConfigPath) + executeCLI(testString) + + outputJSONPath = os.path.join(os.path.dirname(testImg["path"]), "out", os.path.splitext(os.path.basename(testImg["path"]))[0] + ".json") + assert os.path.exists(outputJSONPath), "Expected output JSON file: {0} was not found.".format(outputJSONPath) + + with open(outputJSONPath) as json_file: + loadedPredictions = json.load(json_file) + + assert compareObjectData(testImg["expected-objects"]["yolo-small"], loadedPredictions, testImg["width"], testImg["height"], threshCompareThreshold, posCompareThreshold), "Generated object predictions from JSON were not within margin of error compared to expected values." + os.remove(outputJSONPath) #Remove the JSON file so that it does not affect subsequent tests + +def test_CLI_SAVEPB_YOLOv2(): + #Save .pb and .meta as generated from the YOLOv2 model through CLI + #NOTE: This test verifies that the code executes properly, and the .pb and .meta files are successfully created. The subsequent test will verify the + # contents of those files. + + testString = "flow --model {0} --load {1} --config {2} --threshold 0.4 --savepb".format(yolo_CfgPath, yolo_WeightPath, generalConfigPath) + + with pytest.raises(SystemExit): + executeCLI(testString) + + assert os.path.exists(pbPath), "Expected output .pb file: {0} was not found.".format(pbPath) + assert os.path.exists(metaPath), "Expected output .meta file: {0} was not found.".format(metaPath) + +def test_RETURNPREDICT_PBLOAD_YOLOv2(): + #Test the .pb and .meta files generated in the previous step + #NOTE: This test verifies that the code executes properly, and the .pb and .meta files that were created are able to be loaded and used for inference. + # The predictions that are generated will be compared against expected predictions. + + options = {"pbLoad": pbPath, "metaLoad": metaPath, "threshold": 0.4} + tfnet = TFNet(options) + imgcv = cv2.imread(testImg["path"]) + loadedPredictions = tfnet.return_predict(imgcv) + + assert compareObjectData(testImg["expected-objects"]["yolo"], loadedPredictions, testImg["width"], testImg["height"], threshCompareThreshold, posCompareThreshold), "Generated object predictions from return_predict() were not within margin of error compared to expected values." + +#TESTS FOR TRAINING +def test_TRAIN_FROM_WEIGHTS_CLI__LOAD_CHECKPOINT_RETURNPREDICT_YOLOv2(): + #Test training using pre-generated weights for tiny-yolo-voc + #NOTE: This test verifies that the code executes properly, and that the expected checkpoint file (tiny-yolo-voc-20.meta in this case) is generated. + # In addition, predictions are generated using the checkpoint file to verify that training completed successfully. + + testString = "flow --model {0} --load {1} --train --dataset {2} --annotation {3} --epoch 20".format(tiny_yolo_voc_CfgPath, tiny_yolo_voc_WeightPath, os.path.join(buildPath, "test", "training", "images"), os.path.join(buildPath, "test", "training", "annotations")) + with pytest.raises(SystemExit): + executeCLI(testString) + + checkpointPath = os.path.join(buildPath, "ckpt", "tiny-yolo-voc-20.meta") + assert os.path.exists(checkpointPath), "Expected output checkpoint file: {0} was not found.".format(checkpointPath) + + #Using trained weights + options = {"model": tiny_yolo_voc_CfgPath, "load": 20, "config": generalConfigPath, "threshold": 0.1} + tfnet = TFNet(options) + + #Make sure predictions very roughly match the expected values for image with bike and person + imgcv = cv2.imread(trainImgBikePerson["path"]) + loadedPredictions = tfnet.return_predict(imgcv) + assert compareObjectData(trainImgBikePerson["expected-objects"]["tiny-yolo-voc"], loadedPredictions, trainImgBikePerson["width"], trainImgBikePerson["height"], 0.7, 0.25), "Generated object predictions from training (for image with person on the bike) were not anywhere close to what they are expected to be.\nTraining may not have completed successfully." + differentThanExpectedBike = compareObjectData(trainImgBikePerson["expected-objects"]["tiny-yolo-voc"], loadedPredictions, trainImgBikePerson["width"], trainImgBikePerson["height"], 0.01, 0.001) + + #Make sure predictions very roughly match the expected values for image with horse and person + imgcv = cv2.imread(trainImgHorsePerson["path"]) + loadedPredictions = tfnet.return_predict(imgcv) + assert compareObjectData(trainImgHorsePerson["expected-objects"]["tiny-yolo-voc"], loadedPredictions, trainImgHorsePerson["width"], trainImgHorsePerson["height"], 0.7, 0.25), "Generated object predictions from training (for image with person on the horse) were not anywhere close to what they are expected to be.\nTraining may not have completed successfully." + differentThanExpectedHorse = compareObjectData(trainImgHorsePerson["expected-objects"]["tiny-yolo-voc"], loadedPredictions, trainImgHorsePerson["width"], trainImgHorsePerson["height"], 0.01, 0.001) + + assert not (differentThanExpectedBike and differentThanExpectedHorse), "The generated object predictions for both images appear to be exactly the same as the ones generated with the original weights.\nTraining may not have completed successfully.\n\nNOTE: It is possible this is a fluke error and training did complete properly (try running this build again to confirm) - but most likely something is wrong." \ No newline at end of file diff --git a/test/training/annotations/1.xml b/test/training/annotations/1.xml new file mode 100644 index 0000000..1700745 --- /dev/null +++ b/test/training/annotations/1.xml @@ -0,0 +1,44 @@ + + VOC2007 + 1.jpg + + The VOC2007 Database + PASCAL VOC2007 + flickr + 336426776 + + + Elder Timothy Chaves + Tim Chaves + + + 500 + 375 + 3 + + 0 + + person + Left + 0 + 0 + + 135 + 25 + 236 + 188 + + + + bicycle + Left + 0 + 0 + + 95 + 85 + 232 + 253 + + + diff --git a/test/training/annotations/2.xml b/test/training/annotations/2.xml new file mode 100644 index 0000000..c38f904 --- /dev/null +++ b/test/training/annotations/2.xml @@ -0,0 +1,44 @@ + + VOC2007 + 2.jpg + + The VOC2007 Database + PASCAL VOC2007 + flickr + 329950741 + + + Lothar Lenz + Lothar Lenz + + + 500 + 332 + 3 + + 0 + + person + Left + 0 + 0 + + 235 + 51 + 309 + 222 + + + + horse + Left + 0 + 0 + + 157 + 106 + 426 + 294 + + + diff --git a/test/training/images/1.jpg b/test/training/images/1.jpg new file mode 100644 index 0000000..3d42943 Binary files /dev/null and b/test/training/images/1.jpg differ diff --git a/test/training/images/2.jpg b/test/training/images/2.jpg new file mode 100644 index 0000000..1ee3cd5 Binary files /dev/null and b/test/training/images/2.jpg differ