diff --git a/ai/LICENSE.md b/ai/LICENSE.md
new file mode 100644
index 0000000..f288702
--- /dev/null
+++ b/ai/LICENSE.md
@@ -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.
+
+
+ Copyright (C)
+
+ 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:
+
+ Copyright (C)
+ 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/ai/README.md b/ai/README.md
new file mode 100644
index 0000000..6ed745d
--- /dev/null
+++ b/ai/README.md
@@ -0,0 +1,29 @@
+# Training the PyTorch Localizer and Detector
+
+## Content
+
+The `model.py` files contains a Python class called `Dataset`, which has methods for manipulating datasets of SkyScan images and training models on them. There are also some example functions that use the class, including `revised_sequence()`, which runs an entire data analysis pipeline. The steps in that pipeline including ingesting the imagery, training a localizer (which finds bounding boxes), using the localizer to find bounding boxes on additional images, training a detector (which finds bounding boxes and aircraft class), and testing the performance of localizer and detector with testing data.
+
+The pipeline will need to be adapted to meet your specific needs. Each time a model is trained or used, YOLOv7 outputs the results to a folder. To retrain a model or rerun a test, the corresponding folder name should be changed in the code to avoid a collision. On the other hand, if you've already trained a model or run a test and are rerunning the code, you can instead comment out the corresponding `.train()` and `.test()` calls to save time. In general, lines ending with `##` in `revised_sequence()` are time-intensive steps that only need to be run once, and can be commented out to save time if rerunning the code.
+
+## Setup
+
+Install the [YOLOv7 repo](https://github.com/WongKinYiu/yolov7), following the instructions there. Then, copy the following files into the root folder of your YOLOv7 installation:
+* `model.py`
+* `test2.py`
+* `localizer_rev2.yaml`
+* `detector_rev2.yaml`
+* `taxon.json`
+
+Copy the following files into the `utils` folder:
+* `metrics2.py`
+* `datasets2.py`
+
+Then, edit the `model.py` file's `revised_sequence()` method and update the file paths to point to the appropriate folders for your system. File paths in `localizer_rev2.yaml` and `detector_rev2.yaml` will also need to be updated.
+
+Finally, from the YOLOv7 root folder, run:
+`./model.py`
+
+## License
+
+The contents of this folder are derived from https://github.com/WongKinYiu/yolov7 and are being released under the GNU General Public License v3.0. (The remainder of this repo is released under the Apache License 2.0.)
diff --git a/ai/datasets2.py b/ai/datasets2.py
new file mode 100644
index 0000000..44e14e2
--- /dev/null
+++ b/ai/datasets2.py
@@ -0,0 +1,1354 @@
+# Dataset utils and dataloaders
+
+import glob
+import logging
+import math
+import os
+import random
+import shutil
+import time
+from itertools import repeat
+from multiprocessing.pool import ThreadPool
+from pathlib import Path
+from threading import Thread
+
+import cv2
+import numpy as np
+import torch
+import torch.nn.functional as F
+from PIL import Image, ExifTags
+from torch.utils.data import Dataset
+from tqdm import tqdm
+
+import pickle
+from copy import deepcopy
+#from pycocotools import mask as maskUtils
+from torchvision.utils import save_image
+from torchvision.ops import roi_pool, roi_align, ps_roi_pool, ps_roi_align
+
+from utils.general import check_requirements, xyxy2xywh, xywh2xyxy, xywhn2xyxy, xyn2xy, segment2box, segments2boxes, \
+ resample_segments, clean_str
+from utils.torch_utils import torch_distributed_zero_first
+
+# Parameters
+help_url = 'https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data'
+img_formats = ['bmp', 'jpg', 'jpeg', 'png', 'tif', 'tiff', 'dng', 'webp', 'mpo'] # acceptable image suffixes
+vid_formats = ['mov', 'avi', 'mp4', 'mpg', 'mpeg', 'm4v', 'wmv', 'mkv'] # acceptable video suffixes
+logger = logging.getLogger(__name__)
+
+# Get orientation exif tag
+for orientation in ExifTags.TAGS.keys():
+ if ExifTags.TAGS[orientation] == 'Orientation':
+ break
+
+
+def get_hash(files):
+ # Returns a single hash value of a list of files
+ return sum(os.path.getsize(f) for f in files if os.path.isfile(f))
+
+
+def exif_size(img):
+ # Returns exif-corrected PIL size
+ s = img.size # (width, height)
+ try:
+ rotation = dict(img._getexif().items())[orientation]
+ if rotation == 6: # rotation 270
+ s = (s[1], s[0])
+ elif rotation == 8: # rotation 90
+ s = (s[1], s[0])
+ except:
+ pass
+
+ return s
+
+
+def create_dataloader(path, imgsz, batch_size, stride, opt, hyp=None, augment=False, cache=False, pad=0.0, rect=False,
+ rank=-1, world_size=1, workers=8, image_weights=False, quad=False, prefix=''):
+ # Make sure only the first process in DDP process the dataset first, and the following others can use the cache
+ with torch_distributed_zero_first(rank):
+ dataset = LoadImagesAndLabels(path, imgsz, batch_size,
+ augment=augment, # augment images
+ hyp=hyp, # augmentation hyperparameters
+ rect=rect, # rectangular training
+ cache_images=cache,
+ single_cls=opt.single_cls,
+ stride=int(stride),
+ pad=pad,
+ image_weights=image_weights,
+ prefix=prefix)
+
+ batch_size = min(batch_size, len(dataset))
+ nw = min([os.cpu_count() // world_size, batch_size if batch_size > 1 else 0, workers]) # number of workers
+ sampler = torch.utils.data.distributed.DistributedSampler(dataset) if rank != -1 else None
+ loader = torch.utils.data.DataLoader if image_weights else InfiniteDataLoader
+ # Use torch.utils.data.DataLoader() if dataset.properties will update during training else InfiniteDataLoader()
+ dataloader = loader(dataset,
+ batch_size=batch_size,
+ num_workers=nw,
+ sampler=sampler,
+ pin_memory=True,
+ collate_fn=LoadImagesAndLabels.collate_fn4 if quad else LoadImagesAndLabels.collate_fn)
+ return dataloader, dataset
+
+
+class InfiniteDataLoader(torch.utils.data.dataloader.DataLoader):
+ """ Dataloader that reuses workers
+
+ Uses same syntax as vanilla DataLoader
+ """
+
+ def __init__(self, *args, **kwargs):
+ super().__init__(*args, **kwargs)
+ object.__setattr__(self, 'batch_sampler', _RepeatSampler(self.batch_sampler))
+ self.iterator = super().__iter__()
+
+ def __len__(self):
+ return len(self.batch_sampler.sampler)
+
+ def __iter__(self):
+ for i in range(len(self)):
+ yield next(self.iterator)
+
+
+class _RepeatSampler(object):
+ """ Sampler that repeats forever
+
+ Args:
+ sampler (Sampler)
+ """
+
+ def __init__(self, sampler):
+ self.sampler = sampler
+
+ def __iter__(self):
+ while True:
+ yield from iter(self.sampler)
+
+
+class LoadImages: # for inference
+ def __init__(self, path, img_size=640, stride=32):
+ p = str(Path(path).absolute()) # os-agnostic absolute path
+ if '*' in p:
+ files = sorted(glob.glob(p, recursive=True)) # glob
+ elif os.path.isdir(p):
+ files = sorted(glob.glob(os.path.join(p, '*.*'))) # dir
+ elif os.path.isfile(p):
+ files = [p] # files
+ else:
+ raise Exception(f'ERROR: {p} does not exist')
+
+ images = [x for x in files if x.split('.')[-1].lower() in img_formats]
+ videos = [x for x in files if x.split('.')[-1].lower() in vid_formats]
+ ni, nv = len(images), len(videos)
+
+ self.img_size = img_size
+ self.stride = stride
+ self.files = images + videos
+ self.nf = ni + nv # number of files
+ self.video_flag = [False] * ni + [True] * nv
+ self.mode = 'image'
+ if any(videos):
+ self.new_video(videos[0]) # new video
+ else:
+ self.cap = None
+ assert self.nf > 0, f'No images or videos found in {p}. ' \
+ f'Supported formats are:\nimages: {img_formats}\nvideos: {vid_formats}'
+
+ def __iter__(self):
+ self.count = 0
+ return self
+
+ def __next__(self):
+ if self.count == self.nf:
+ raise StopIteration
+ path = self.files[self.count]
+
+ if self.video_flag[self.count]:
+ # Read video
+ self.mode = 'video'
+ ret_val, img0 = self.cap.read()
+ if not ret_val:
+ self.count += 1
+ self.cap.release()
+ if self.count == self.nf: # last video
+ raise StopIteration
+ else:
+ path = self.files[self.count]
+ self.new_video(path)
+ ret_val, img0 = self.cap.read()
+
+ self.frame += 1
+ print(f'video {self.count + 1}/{self.nf} ({self.frame}/{self.nframes}) {path}: ', end='')
+
+ else:
+ # Read image
+ self.count += 1
+ img0 = cv2.imread(path) # BGR
+ assert img0 is not None, 'Image Not Found ' + path
+ #print(f'image {self.count}/{self.nf} {path}: ', end='')
+
+ # Padded resize
+ img = letterbox(img0, self.img_size, stride=self.stride)[0]
+
+ # Convert
+ img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
+ img = np.ascontiguousarray(img)
+
+ return path, img, img0, self.cap
+
+ def new_video(self, path):
+ self.frame = 0
+ self.cap = cv2.VideoCapture(path)
+ self.nframes = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
+
+ def __len__(self):
+ return self.nf # number of files
+
+
+class LoadWebcam: # for inference
+ def __init__(self, pipe='0', img_size=640, stride=32):
+ self.img_size = img_size
+ self.stride = stride
+
+ if pipe.isnumeric():
+ pipe = eval(pipe) # local camera
+ # pipe = 'rtsp://192.168.1.64/1' # IP camera
+ # pipe = 'rtsp://username:password@192.168.1.64/1' # IP camera with login
+ # pipe = 'http://wmccpinetop.axiscam.net/mjpg/video.mjpg' # IP golf camera
+
+ self.pipe = pipe
+ self.cap = cv2.VideoCapture(pipe) # video capture object
+ self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 3) # set buffer size
+
+ def __iter__(self):
+ self.count = -1
+ return self
+
+ def __next__(self):
+ self.count += 1
+ if cv2.waitKey(1) == ord('q'): # q to quit
+ self.cap.release()
+ cv2.destroyAllWindows()
+ raise StopIteration
+
+ # Read frame
+ if self.pipe == 0: # local camera
+ ret_val, img0 = self.cap.read()
+ img0 = cv2.flip(img0, 1) # flip left-right
+ else: # IP camera
+ n = 0
+ while True:
+ n += 1
+ self.cap.grab()
+ if n % 30 == 0: # skip frames
+ ret_val, img0 = self.cap.retrieve()
+ if ret_val:
+ break
+
+ # Print
+ assert ret_val, f'Camera Error {self.pipe}'
+ img_path = 'webcam.jpg'
+ print(f'webcam {self.count}: ', end='')
+
+ # Padded resize
+ img = letterbox(img0, self.img_size, stride=self.stride)[0]
+
+ # Convert
+ img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
+ img = np.ascontiguousarray(img)
+
+ return img_path, img, img0, None
+
+ def __len__(self):
+ return 0
+
+
+class LoadFromDir: # for inference
+ def __init__(self, path, img_size=640, stride=32):
+ self.path = path
+ self.img_size = img_size
+ self.stride = stride
+ self.mode = 'image'
+
+ def __iter__(self):
+ self.count = 0
+ return self
+
+ def __next__(self):
+ # Check for files in folder, waiting if necessary
+ while True:
+ if os.path.isdir(self.path):
+ img_paths = glob.glob(os.path.join(self.path, '*'))
+ if len(img_paths) > 0:
+ break
+ time.sleep(0.5)
+ img_path = min(img_paths, key=os.path.getctime)
+
+ # Process image
+ self.count += 1
+ img0 = cv2.imread(img_path)
+ assert img0 is not None, 'Image Not Found ' + img_path
+ img = letterbox(img0, self.img_size, stride=self.stride)[0] # Size
+ img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, channel first
+ img = np.ascontiguousarray(img)
+ return img_path, img, img0, None
+
+ def __len__(self):
+ return 0
+
+
+class LoadStreams: # multiple IP or RTSP cameras
+ def __init__(self, sources='streams.txt', img_size=640, stride=32):
+ self.mode = 'stream'
+ self.img_size = img_size
+ self.stride = stride
+
+ if os.path.isfile(sources):
+ with open(sources, 'r') as f:
+ sources = [x.strip() for x in f.read().strip().splitlines() if len(x.strip())]
+ else:
+ sources = [sources]
+
+ n = len(sources)
+ self.imgs = [None] * n
+ self.sources = [clean_str(x) for x in sources] # clean source names for later
+ for i, s in enumerate(sources):
+ # Start the thread to read frames from the video stream
+ print(f'{i + 1}/{n}: {s}... ', end='')
+ url = eval(s) if s.isnumeric() else s
+ if 'youtube.com/' in str(url) or 'youtu.be/' in str(url): # if source is YouTube video
+ check_requirements(('pafy', 'youtube_dl'))
+ import pafy
+ url = pafy.new(url).getbest(preftype="mp4").url
+ cap = cv2.VideoCapture(url)
+ assert cap.isOpened(), f'Failed to open {s}'
+ w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
+ h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
+ self.fps = cap.get(cv2.CAP_PROP_FPS) % 100
+
+ _, self.imgs[i] = cap.read() # guarantee first frame
+ thread = Thread(target=self.update, args=([i, cap]), daemon=True)
+ print(f' success ({w}x{h} at {self.fps:.2f} FPS).')
+ thread.start()
+ print('') # newline
+
+ # check for common shapes
+ s = np.stack([letterbox(x, self.img_size, stride=self.stride)[0].shape for x in self.imgs], 0) # shapes
+ self.rect = np.unique(s, axis=0).shape[0] == 1 # rect inference if all shapes equal
+ if not self.rect:
+ print('WARNING: Different stream shapes detected. For optimal performance supply similarly-shaped streams.')
+
+ def update(self, index, cap):
+ # Read next stream frame in a daemon thread
+ n = 0
+ while cap.isOpened():
+ n += 1
+ # _, self.imgs[index] = cap.read()
+ cap.grab()
+ if n == 4: # read every 4th frame
+ success, im = cap.retrieve()
+ self.imgs[index] = im if success else self.imgs[index] * 0
+ n = 0
+ time.sleep(1 / self.fps) # wait time
+
+ def __iter__(self):
+ self.count = -1
+ return self
+
+ def __next__(self):
+ self.count += 1
+ img0 = self.imgs.copy()
+ if cv2.waitKey(1) == ord('q'): # q to quit
+ cv2.destroyAllWindows()
+ raise StopIteration
+
+ # Letterbox
+ img = [letterbox(x, self.img_size, auto=self.rect, stride=self.stride)[0] for x in img0]
+
+ # Stack
+ img = np.stack(img, 0)
+
+ # Convert
+ img = img[:, :, :, ::-1].transpose(0, 3, 1, 2) # BGR to RGB, to bsx3x416x416
+ img = np.ascontiguousarray(img)
+
+ return self.sources, img, img0, None
+
+ def __len__(self):
+ return 0 # 1E12 frames = 32 streams at 30 FPS for 30 years
+
+
+def img2label_paths(img_paths):
+ # Define label paths as a function of image paths
+ sa, sb = os.sep + 'images' + os.sep, os.sep + 'labels' + os.sep # /images/, /labels/ substrings
+ return ['txt'.join(x.replace(sa, sb, 1).rsplit(x.split('.')[-1], 1)) for x in img_paths]
+
+
+class LoadImagesAndLabels(Dataset): # for training/testing
+ def __init__(self, path, img_size=640, batch_size=16, augment=False, hyp=None, rect=False, image_weights=False,
+ cache_images=False, single_cls=False, stride=32, pad=0.0, prefix=''):
+ self.img_size = img_size
+ self.augment = augment
+ self.hyp = hyp
+ self.image_weights = image_weights
+ self.rect = False if image_weights else rect
+ self.mosaic = self.augment and not self.rect # load 4 images at a time into a mosaic (only during training)
+ self.mosaic_border = [-img_size // 2, -img_size // 2]
+ self.stride = stride
+ self.path = path
+ #self.albumentations = Albumentations() if augment else None
+
+ try:
+ f = [] # image files
+ for p in path if isinstance(path, list) else [path]:
+ p = Path(p) # os-agnostic
+ if p.is_dir(): # dir
+ f += glob.glob(str(p / '**' / '*.*'), recursive=True)
+ # f = list(p.rglob('**/*.*')) # pathlib
+ elif p.is_file(): # file
+ with open(p, 'r') as t:
+ t = t.read().strip().splitlines()
+ parent = str(p.parent) + os.sep
+ f += [x.replace('./', parent) if x.startswith('./') else x for x in t] # local to global path
+ # f += [p.parent / x.lstrip(os.sep) for x in t] # local to global path (pathlib)
+ else:
+ raise Exception(f'{prefix}{p} does not exist')
+ self.img_files = sorted([x.replace('/', os.sep) for x in f if x.split('.')[-1].lower() in img_formats])
+ # self.img_files = sorted([x for x in f if x.suffix[1:].lower() in img_formats]) # pathlib
+ assert self.img_files, f'{prefix}No images found'
+ except Exception as e:
+ raise Exception(f'{prefix}Error loading data from {path}: {e}\nSee {help_url}')
+
+ # Check cache
+ self.label_files = img2label_paths(self.img_files) # labels
+ cache_path = (p if p.is_file() else Path(self.label_files[0]).parent).with_suffix('.cache') # cached labels
+ if cache_path.is_file():
+ cache, exists = torch.load(cache_path), True # load
+ #if cache['hash'] != get_hash(self.label_files + self.img_files) or 'version' not in cache: # changed
+ # cache, exists = self.cache_labels(cache_path, prefix), False # re-cache
+ else:
+ cache, exists = self.cache_labels(cache_path, prefix), False # cache
+
+ # Display cache
+ nf, nm, ne, nc, n = cache.pop('results') # found, missing, empty, corrupted, total
+ if exists:
+ d = f"Scanning '{cache_path}' images and labels... {nf} found, {nm} missing, {ne} empty, {nc} corrupted"
+ tqdm(None, desc=prefix + d, total=n, initial=n) # display cache results
+ assert nf > 0 or not augment, f'{prefix}No labels in {cache_path}. Can not train without labels. See {help_url}'
+
+ # Read cache
+ cache.pop('hash') # remove hash
+ cache.pop('version') # remove version
+ labels, shapes, self.segments = zip(*cache.values())
+ self.labels = list(labels)
+ self.shapes = np.array(shapes, dtype=np.float64)
+ self.img_files = list(cache.keys()) # update
+ self.label_files = img2label_paths(cache.keys()) # update
+ if single_cls:
+ for x in self.labels:
+ x[:, 0] = 0
+
+ n = len(shapes) # number of images
+ bi = np.floor(np.arange(n) / batch_size).astype(np.int) # batch index
+ nb = bi[-1] + 1 # number of batches
+ self.batch = bi # batch index of image
+ self.n = n
+ self.indices = range(n)
+
+ # Rectangular Training
+ if self.rect:
+ # Sort by aspect ratio
+ s = self.shapes # wh
+ ar = s[:, 1] / s[:, 0] # aspect ratio
+ irect = ar.argsort()
+ self.img_files = [self.img_files[i] for i in irect]
+ self.label_files = [self.label_files[i] for i in irect]
+ self.labels = [self.labels[i] for i in irect]
+ self.shapes = s[irect] # wh
+ ar = ar[irect]
+
+ # Set training image shapes
+ shapes = [[1, 1]] * nb
+ for i in range(nb):
+ ari = ar[bi == i]
+ mini, maxi = ari.min(), ari.max()
+ if maxi < 1:
+ shapes[i] = [maxi, 1]
+ elif mini > 1:
+ shapes[i] = [1, 1 / mini]
+
+ self.batch_shapes = np.ceil(np.array(shapes) * img_size / stride + pad).astype(np.int) * stride
+
+ # Cache images into memory for faster training (WARNING: large datasets may exceed system RAM)
+ self.imgs = [None] * n
+ if cache_images:
+ if cache_images == 'disk':
+ self.im_cache_dir = Path(Path(self.img_files[0]).parent.as_posix() + '_npy')
+ self.img_npy = [self.im_cache_dir / Path(f).with_suffix('.npy').name for f in self.img_files]
+ self.im_cache_dir.mkdir(parents=True, exist_ok=True)
+ gb = 0 # Gigabytes of cached images
+ self.img_hw0, self.img_hw = [None] * n, [None] * n
+ results = ThreadPool(8).imap(lambda x: load_image(*x), zip(repeat(self), range(n)))
+ pbar = tqdm(enumerate(results), total=n)
+ for i, x in pbar:
+ if cache_images == 'disk':
+ if not self.img_npy[i].exists():
+ np.save(self.img_npy[i].as_posix(), x[0])
+ gb += self.img_npy[i].stat().st_size
+ else:
+ self.imgs[i], self.img_hw0[i], self.img_hw[i] = x
+ gb += self.imgs[i].nbytes
+ pbar.desc = f'{prefix}Caching images ({gb / 1E9:.1f}GB)'
+ pbar.close()
+
+ def cache_labels(self, path=Path('./labels.cache'), prefix=''):
+ # Cache dataset labels, check images and read shapes
+ x = {} # dict
+ nm, nf, ne, nc = 0, 0, 0, 0 # number missing, found, empty, duplicate
+ pbar = tqdm(zip(self.img_files, self.label_files), desc='Scanning images', total=len(self.img_files))
+ for i, (im_file, lb_file) in enumerate(pbar):
+ try:
+ # verify images
+ im = Image.open(im_file)
+ im.verify() # PIL verify
+ shape = exif_size(im) # image size
+ segments = [] # instance segments
+ assert (shape[0] > 9) & (shape[1] > 9), f'image size {shape} <10 pixels'
+ assert im.format.lower() in img_formats, f'invalid image format {im.format}'
+
+ # verify labels
+ if os.path.isfile(lb_file):
+ nf += 1 # label found
+ with open(lb_file, 'r') as f:
+ l = [x.split() for x in f.read().strip().splitlines()]
+ if any([len(x) > 8 for x in l]): # is segment
+ classes = np.array([x[0] for x in l], dtype=np.float32)
+ segments = [np.array(x[1:], dtype=np.float32).reshape(-1, 2) for x in l] # (cls, xy1...)
+ l = np.concatenate((classes.reshape(-1, 1), segments2boxes(segments)), 1) # (cls, xywh)
+ l = np.array(l, dtype=np.float32)
+ if len(l):
+ assert l.shape[1] == 5, 'labels require 5 columns each'
+ assert (l >= 0).all(), 'negative labels'
+ assert (l[:, 1:] <= 1).all(), 'non-normalized or out of bounds coordinate labels'
+ assert np.unique(l, axis=0).shape[0] == l.shape[0], 'duplicate labels'
+ else:
+ ne += 1 # label empty
+ l = np.zeros((0, 5), dtype=np.float32)
+ else:
+ nm += 1 # label missing
+ l = np.zeros((0, 5), dtype=np.float32)
+ x[im_file] = [l, shape, segments]
+ except Exception as e:
+ nc += 1
+ print(f'{prefix}WARNING: Ignoring corrupted image and/or label {im_file}: {e}')
+
+ pbar.desc = f"{prefix}Scanning '{path.parent / path.stem}' images and labels... " \
+ f"{nf} found, {nm} missing, {ne} empty, {nc} corrupted"
+ pbar.close()
+
+ if nf == 0:
+ print(f'{prefix}WARNING: No labels found in {path}. See {help_url}')
+
+ x['hash'] = get_hash(self.label_files + self.img_files)
+ x['results'] = nf, nm, ne, nc, i + 1
+ x['version'] = 0.1 # cache version
+ torch.save(x, path) # save for next time
+ logging.info(f'{prefix}New cache created: {path}')
+ return x
+
+ def __len__(self):
+ return len(self.img_files)
+
+ # def __iter__(self):
+ # self.count = -1
+ # print('ran dataset iter')
+ # #self.shuffled_vector = np.random.permutation(self.nF) if self.augment else np.arange(self.nF)
+ # return self
+
+ def __getitem__(self, index):
+ index = self.indices[index] # linear, shuffled, or image_weights
+
+ hyp = self.hyp
+ mosaic = self.mosaic and random.random() < hyp['mosaic']
+ if mosaic:
+ # Load mosaic
+ if random.random() < 0.8:
+ img, labels = load_mosaic(self, index)
+ else:
+ img, labels = load_mosaic9(self, index)
+ shapes = None
+
+ # MixUp https://arxiv.org/pdf/1710.09412.pdf
+ if random.random() < hyp['mixup']:
+ if random.random() < 0.8:
+ img2, labels2 = load_mosaic(self, random.randint(0, len(self.labels) - 1))
+ else:
+ img2, labels2 = load_mosaic9(self, random.randint(0, len(self.labels) - 1))
+ r = np.random.beta(8.0, 8.0) # mixup ratio, alpha=beta=8.0
+ img = (img * r + img2 * (1 - r)).astype(np.uint8)
+ labels = np.concatenate((labels, labels2), 0)
+
+ else:
+ # Load image
+ img, (h0, w0), (h, w) = load_image(self, index)
+
+ # Letterbox
+ shape = self.batch_shapes[self.batch[index]] if self.rect else self.img_size # final letterboxed shape
+ img, ratio, pad = letterbox(img, shape, auto=False, scaleup=self.augment)
+ shapes = (h0, w0), ((h / h0, w / w0), pad) # for COCO mAP rescaling
+
+ labels = self.labels[index].copy()
+ if labels.size: # normalized xywh to pixel xyxy format
+ labels[:, 1:] = xywhn2xyxy(labels[:, 1:], ratio[0] * w, ratio[1] * h, padw=pad[0], padh=pad[1])
+
+ if self.augment:
+ # Augment imagespace
+ if not mosaic:
+ img, labels = random_perspective(img, labels,
+ degrees=hyp['degrees'],
+ translate=hyp['translate'],
+ scale=hyp['scale'],
+ shear=hyp['shear'],
+ perspective=hyp['perspective'])
+
+
+ #img, labels = self.albumentations(img, labels)
+
+ # Augment colorspace
+ augment_hsv(img, hgain=hyp['hsv_h'], sgain=hyp['hsv_s'], vgain=hyp['hsv_v'])
+
+ # Apply cutouts
+ # if random.random() < 0.9:
+ # labels = cutout(img, labels)
+
+ if random.random() < hyp['paste_in']:
+ sample_labels, sample_images, sample_masks = [], [], []
+ while len(sample_labels) < 30:
+ sample_labels_, sample_images_, sample_masks_ = load_samples(self, random.randint(0, len(self.labels) - 1))
+ sample_labels += sample_labels_
+ sample_images += sample_images_
+ sample_masks += sample_masks_
+ #print(len(sample_labels))
+ if len(sample_labels) == 0:
+ break
+ labels = pastein(img, labels, sample_labels, sample_images, sample_masks)
+
+ nL = len(labels) # number of labels
+ if nL:
+ labels[:, 1:5] = xyxy2xywh(labels[:, 1:5]) # convert xyxy to xywh
+ labels[:, [2, 4]] /= img.shape[0] # normalized height 0-1
+ labels[:, [1, 3]] /= img.shape[1] # normalized width 0-1
+
+ if self.augment:
+ # flip up-down
+ if random.random() < hyp['flipud']:
+ img = np.flipud(img)
+ if nL:
+ labels[:, 2] = 1 - labels[:, 2]
+
+ # flip left-right
+ if random.random() < hyp['fliplr']:
+ img = np.fliplr(img)
+ if nL:
+ labels[:, 1] = 1 - labels[:, 1]
+
+ labels_out = torch.zeros((nL, 6))
+ if nL:
+ labels_out[:, 1:] = torch.from_numpy(labels)
+
+ # Convert
+ img = img[:, :, ::-1].transpose(2, 0, 1) # BGR to RGB, to 3x416x416
+ img = np.ascontiguousarray(img)
+
+ return torch.from_numpy(img), labels_out, self.img_files[index], shapes
+
+ @staticmethod
+ def collate_fn(batch):
+ img, label, path, shapes = zip(*batch) # transposed
+ for i, l in enumerate(label):
+ l[:, 0] = i # add target image index for build_targets()
+ return torch.stack(img, 0), torch.cat(label, 0), path, shapes
+
+ @staticmethod
+ def collate_fn4(batch):
+ img, label, path, shapes = zip(*batch) # transposed
+ n = len(shapes) // 4
+ img4, label4, path4, shapes4 = [], [], path[:n], shapes[:n]
+
+ ho = torch.tensor([[0., 0, 0, 1, 0, 0]])
+ wo = torch.tensor([[0., 0, 1, 0, 0, 0]])
+ s = torch.tensor([[1, 1, .5, .5, .5, .5]]) # scale
+ for i in range(n): # zidane torch.zeros(16,3,720,1280) # BCHW
+ i *= 4
+ if random.random() < 0.5:
+ im = F.interpolate(img[i].unsqueeze(0).float(), scale_factor=2., mode='bilinear', align_corners=False)[
+ 0].type(img[i].type())
+ l = label[i]
+ else:
+ im = torch.cat((torch.cat((img[i], img[i + 1]), 1), torch.cat((img[i + 2], img[i + 3]), 1)), 2)
+ l = torch.cat((label[i], label[i + 1] + ho, label[i + 2] + wo, label[i + 3] + ho + wo), 0) * s
+ img4.append(im)
+ label4.append(l)
+
+ for i, l in enumerate(label4):
+ l[:, 0] = i # add target image index for build_targets()
+
+ return torch.stack(img4, 0), torch.cat(label4, 0), path4, shapes4
+
+
+# Ancillary functions --------------------------------------------------------------------------------------------------
+def load_image(self, index):
+ # loads 1 image from dataset, returns img, original hw, resized hw
+ img = self.imgs[index]
+ if img is None: # not cached
+ path = self.img_files[index]
+ img = cv2.imread(path) # BGR
+ assert img is not None, 'Image Not Found ' + path
+ h0, w0 = img.shape[:2] # orig hw
+ r = self.img_size / max(h0, w0) # resize image to img_size
+ if r != 1: # always resize down, only resize up if training with augmentation
+ interp = cv2.INTER_AREA if r < 1 and not self.augment else cv2.INTER_LINEAR
+ img = cv2.resize(img, (int(w0 * r), int(h0 * r)), interpolation=interp)
+ return img, (h0, w0), img.shape[:2] # img, hw_original, hw_resized
+ else:
+ return self.imgs[index], self.img_hw0[index], self.img_hw[index] # img, hw_original, hw_resized
+
+
+def augment_hsv(img, hgain=0.5, sgain=0.5, vgain=0.5):
+ r = np.random.uniform(-1, 1, 3) * [hgain, sgain, vgain] + 1 # random gains
+ hue, sat, val = cv2.split(cv2.cvtColor(img, cv2.COLOR_BGR2HSV))
+ dtype = img.dtype # uint8
+
+ x = np.arange(0, 256, dtype=np.int16)
+ lut_hue = ((x * r[0]) % 180).astype(dtype)
+ lut_sat = np.clip(x * r[1], 0, 255).astype(dtype)
+ lut_val = np.clip(x * r[2], 0, 255).astype(dtype)
+
+ img_hsv = cv2.merge((cv2.LUT(hue, lut_hue), cv2.LUT(sat, lut_sat), cv2.LUT(val, lut_val))).astype(dtype)
+ cv2.cvtColor(img_hsv, cv2.COLOR_HSV2BGR, dst=img) # no return needed
+
+
+def hist_equalize(img, clahe=True, bgr=False):
+ # Equalize histogram on BGR image 'img' with img.shape(n,m,3) and range 0-255
+ yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV if bgr else cv2.COLOR_RGB2YUV)
+ if clahe:
+ c = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
+ yuv[:, :, 0] = c.apply(yuv[:, :, 0])
+ else:
+ yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0]) # equalize Y channel histogram
+ return cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR if bgr else cv2.COLOR_YUV2RGB) # convert YUV image to RGB
+
+
+def load_mosaic(self, index):
+ # loads images in a 4-mosaic
+
+ labels4, segments4 = [], []
+ s = self.img_size
+ yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border] # mosaic center x, y
+ indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices
+ for i, index in enumerate(indices):
+ # Load image
+ img, _, (h, w) = load_image(self, index)
+
+ # place img in img4
+ if i == 0: # top left
+ img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
+ x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image)
+ x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image)
+ elif i == 1: # top right
+ x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
+ x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
+ elif i == 2: # bottom left
+ x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
+ x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h)
+ elif i == 3: # bottom right
+ x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
+ x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
+
+ img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
+ padw = x1a - x1b
+ padh = y1a - y1b
+
+ # Labels
+ labels, segments = self.labels[index].copy(), self.segments[index].copy()
+ if labels.size:
+ labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format
+ segments = [xyn2xy(x, w, h, padw, padh) for x in segments]
+ labels4.append(labels)
+ segments4.extend(segments)
+
+ # Concat/clip labels
+ labels4 = np.concatenate(labels4, 0)
+ for x in (labels4[:, 1:], *segments4):
+ np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective()
+ # img4, labels4 = replicate(img4, labels4) # replicate
+
+ # Augment
+ #img4, labels4, segments4 = remove_background(img4, labels4, segments4)
+ #sample_segments(img4, labels4, segments4, probability=self.hyp['copy_paste'])
+ img4, labels4, segments4 = copy_paste(img4, labels4, segments4, probability=self.hyp['copy_paste'])
+ img4, labels4 = random_perspective(img4, labels4, segments4,
+ degrees=self.hyp['degrees'],
+ translate=self.hyp['translate'],
+ scale=self.hyp['scale'],
+ shear=self.hyp['shear'],
+ perspective=self.hyp['perspective'],
+ border=self.mosaic_border) # border to remove
+
+ return img4, labels4
+
+
+def load_mosaic9(self, index):
+ # loads images in a 9-mosaic
+
+ labels9, segments9 = [], []
+ s = self.img_size
+ indices = [index] + random.choices(self.indices, k=8) # 8 additional image indices
+ for i, index in enumerate(indices):
+ # Load image
+ img, _, (h, w) = load_image(self, index)
+
+ # place img in img9
+ if i == 0: # center
+ img9 = np.full((s * 3, s * 3, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
+ h0, w0 = h, w
+ c = s, s, s + w, s + h # xmin, ymin, xmax, ymax (base) coordinates
+ elif i == 1: # top
+ c = s, s - h, s + w, s
+ elif i == 2: # top right
+ c = s + wp, s - h, s + wp + w, s
+ elif i == 3: # right
+ c = s + w0, s, s + w0 + w, s + h
+ elif i == 4: # bottom right
+ c = s + w0, s + hp, s + w0 + w, s + hp + h
+ elif i == 5: # bottom
+ c = s + w0 - w, s + h0, s + w0, s + h0 + h
+ elif i == 6: # bottom left
+ c = s + w0 - wp - w, s + h0, s + w0 - wp, s + h0 + h
+ elif i == 7: # left
+ c = s - w, s + h0 - h, s, s + h0
+ elif i == 8: # top left
+ c = s - w, s + h0 - hp - h, s, s + h0 - hp
+
+ padx, pady = c[:2]
+ x1, y1, x2, y2 = [max(x, 0) for x in c] # allocate coords
+
+ # Labels
+ labels, segments = self.labels[index].copy(), self.segments[index].copy()
+ if labels.size:
+ labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padx, pady) # normalized xywh to pixel xyxy format
+ segments = [xyn2xy(x, w, h, padx, pady) for x in segments]
+ labels9.append(labels)
+ segments9.extend(segments)
+
+ # Image
+ img9[y1:y2, x1:x2] = img[y1 - pady:, x1 - padx:] # img9[ymin:ymax, xmin:xmax]
+ hp, wp = h, w # height, width previous
+
+ # Offset
+ yc, xc = [int(random.uniform(0, s)) for _ in self.mosaic_border] # mosaic center x, y
+ img9 = img9[yc:yc + 2 * s, xc:xc + 2 * s]
+
+ # Concat/clip labels
+ labels9 = np.concatenate(labels9, 0)
+ labels9[:, [1, 3]] -= xc
+ labels9[:, [2, 4]] -= yc
+ c = np.array([xc, yc]) # centers
+ segments9 = [x - c for x in segments9]
+
+ for x in (labels9[:, 1:], *segments9):
+ np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective()
+ # img9, labels9 = replicate(img9, labels9) # replicate
+
+ # Augment
+ #img9, labels9, segments9 = remove_background(img9, labels9, segments9)
+ img9, labels9, segments9 = copy_paste(img9, labels9, segments9, probability=self.hyp['copy_paste'])
+ img9, labels9 = random_perspective(img9, labels9, segments9,
+ degrees=self.hyp['degrees'],
+ translate=self.hyp['translate'],
+ scale=self.hyp['scale'],
+ shear=self.hyp['shear'],
+ perspective=self.hyp['perspective'],
+ border=self.mosaic_border) # border to remove
+
+ return img9, labels9
+
+
+def load_samples(self, index):
+ # loads images in a 4-mosaic
+
+ labels4, segments4 = [], []
+ s = self.img_size
+ yc, xc = [int(random.uniform(-x, 2 * s + x)) for x in self.mosaic_border] # mosaic center x, y
+ indices = [index] + random.choices(self.indices, k=3) # 3 additional image indices
+ for i, index in enumerate(indices):
+ # Load image
+ img, _, (h, w) = load_image(self, index)
+
+ # place img in img4
+ if i == 0: # top left
+ img4 = np.full((s * 2, s * 2, img.shape[2]), 114, dtype=np.uint8) # base image with 4 tiles
+ x1a, y1a, x2a, y2a = max(xc - w, 0), max(yc - h, 0), xc, yc # xmin, ymin, xmax, ymax (large image)
+ x1b, y1b, x2b, y2b = w - (x2a - x1a), h - (y2a - y1a), w, h # xmin, ymin, xmax, ymax (small image)
+ elif i == 1: # top right
+ x1a, y1a, x2a, y2a = xc, max(yc - h, 0), min(xc + w, s * 2), yc
+ x1b, y1b, x2b, y2b = 0, h - (y2a - y1a), min(w, x2a - x1a), h
+ elif i == 2: # bottom left
+ x1a, y1a, x2a, y2a = max(xc - w, 0), yc, xc, min(s * 2, yc + h)
+ x1b, y1b, x2b, y2b = w - (x2a - x1a), 0, w, min(y2a - y1a, h)
+ elif i == 3: # bottom right
+ x1a, y1a, x2a, y2a = xc, yc, min(xc + w, s * 2), min(s * 2, yc + h)
+ x1b, y1b, x2b, y2b = 0, 0, min(w, x2a - x1a), min(y2a - y1a, h)
+
+ img4[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
+ padw = x1a - x1b
+ padh = y1a - y1b
+
+ # Labels
+ labels, segments = self.labels[index].copy(), self.segments[index].copy()
+ if labels.size:
+ labels[:, 1:] = xywhn2xyxy(labels[:, 1:], w, h, padw, padh) # normalized xywh to pixel xyxy format
+ segments = [xyn2xy(x, w, h, padw, padh) for x in segments]
+ labels4.append(labels)
+ segments4.extend(segments)
+
+ # Concat/clip labels
+ labels4 = np.concatenate(labels4, 0)
+ for x in (labels4[:, 1:], *segments4):
+ np.clip(x, 0, 2 * s, out=x) # clip when using random_perspective()
+ # img4, labels4 = replicate(img4, labels4) # replicate
+
+ # Augment
+ #img4, labels4, segments4 = remove_background(img4, labels4, segments4)
+ sample_labels, sample_images, sample_masks = sample_segments(img4, labels4, segments4, probability=0.5)
+
+ return sample_labels, sample_images, sample_masks
+
+
+def copy_paste(img, labels, segments, probability=0.5):
+ # Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
+ n = len(segments)
+ if probability and n:
+ h, w, c = img.shape # height, width, channels
+ im_new = np.zeros(img.shape, np.uint8)
+ for j in random.sample(range(n), k=round(probability * n)):
+ l, s = labels[j], segments[j]
+ box = w - l[3], l[2], w - l[1], l[4]
+ ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
+ if (ioa < 0.30).all(): # allow 30% obscuration of existing labels
+ labels = np.concatenate((labels, [[l[0], *box]]), 0)
+ segments.append(np.concatenate((w - s[:, 0:1], s[:, 1:2]), 1))
+ cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED)
+
+ result = cv2.bitwise_and(src1=img, src2=im_new)
+ result = cv2.flip(result, 1) # augment segments (flip left-right)
+ i = result > 0 # pixels to replace
+ # i[:, :] = result.max(2).reshape(h, w, 1) # act over ch
+ img[i] = result[i] # cv2.imwrite('debug.jpg', img) # debug
+
+ return img, labels, segments
+
+
+def remove_background(img, labels, segments):
+ # Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
+ n = len(segments)
+ h, w, c = img.shape # height, width, channels
+ im_new = np.zeros(img.shape, np.uint8)
+ img_new = np.ones(img.shape, np.uint8) * 114
+ for j in range(n):
+ cv2.drawContours(im_new, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED)
+
+ result = cv2.bitwise_and(src1=img, src2=im_new)
+
+ i = result > 0 # pixels to replace
+ img_new[i] = result[i] # cv2.imwrite('debug.jpg', img) # debug
+
+ return img_new, labels, segments
+
+
+def sample_segments(img, labels, segments, probability=0.5):
+ # Implement Copy-Paste augmentation https://arxiv.org/abs/2012.07177, labels as nx5 np.array(cls, xyxy)
+ n = len(segments)
+ sample_labels = []
+ sample_images = []
+ sample_masks = []
+ if probability and n:
+ h, w, c = img.shape # height, width, channels
+ for j in random.sample(range(n), k=round(probability * n)):
+ l, s = labels[j], segments[j]
+ box = l[1].astype(int).clip(0,w-1), l[2].astype(int).clip(0,h-1), l[3].astype(int).clip(0,w-1), l[4].astype(int).clip(0,h-1)
+
+ #print(box)
+ if (box[2] <= box[0]) or (box[3] <= box[1]):
+ continue
+
+ sample_labels.append(l[0])
+
+ mask = np.zeros(img.shape, np.uint8)
+
+ cv2.drawContours(mask, [segments[j].astype(np.int32)], -1, (255, 255, 255), cv2.FILLED)
+ sample_masks.append(mask[box[1]:box[3],box[0]:box[2],:])
+
+ result = cv2.bitwise_and(src1=img, src2=mask)
+ i = result > 0 # pixels to replace
+ mask[i] = result[i] # cv2.imwrite('debug.jpg', img) # debug
+ #print(box)
+ sample_images.append(mask[box[1]:box[3],box[0]:box[2],:])
+
+ return sample_labels, sample_images, sample_masks
+
+
+def replicate(img, labels):
+ # Replicate labels
+ h, w = img.shape[:2]
+ boxes = labels[:, 1:].astype(int)
+ x1, y1, x2, y2 = boxes.T
+ s = ((x2 - x1) + (y2 - y1)) / 2 # side length (pixels)
+ for i in s.argsort()[:round(s.size * 0.5)]: # smallest indices
+ x1b, y1b, x2b, y2b = boxes[i]
+ bh, bw = y2b - y1b, x2b - x1b
+ yc, xc = int(random.uniform(0, h - bh)), int(random.uniform(0, w - bw)) # offset x, y
+ x1a, y1a, x2a, y2a = [xc, yc, xc + bw, yc + bh]
+ img[y1a:y2a, x1a:x2a] = img[y1b:y2b, x1b:x2b] # img4[ymin:ymax, xmin:xmax]
+ labels = np.append(labels, [[labels[i, 0], x1a, y1a, x2a, y2a]], axis=0)
+
+ return img, labels
+
+
+def letterbox(img, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
+ # Resize and pad image while meeting stride-multiple constraints
+ shape = img.shape[:2] # current shape [height, width]
+ if isinstance(new_shape, int):
+ new_shape = (new_shape, new_shape)
+
+ # Scale ratio (new / old)
+ r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
+ if not scaleup: # only scale down, do not scale up (for better test mAP)
+ r = min(r, 1.0)
+
+ # Compute padding
+ ratio = r, r # width, height ratios
+ new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
+ dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding
+ if auto: # minimum rectangle
+ dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding
+ elif scaleFill: # stretch
+ dw, dh = 0.0, 0.0
+ new_unpad = (new_shape[1], new_shape[0])
+ ratio = new_shape[1] / shape[1], new_shape[0] / shape[0] # width, height ratios
+
+ dw /= 2 # divide padding into 2 sides
+ dh /= 2
+
+ if shape[::-1] != new_unpad: # resize
+ img = cv2.resize(img, new_unpad, interpolation=cv2.INTER_LINEAR)
+ top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
+ left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
+ img = cv2.copyMakeBorder(img, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border
+ return img, ratio, (dw, dh)
+
+
+def random_perspective(img, targets=(), segments=(), degrees=10, translate=.1, scale=.1, shear=10, perspective=0.0,
+ border=(0, 0)):
+ # torchvision.transforms.RandomAffine(degrees=(-10, 10), translate=(.1, .1), scale=(.9, 1.1), shear=(-10, 10))
+ # targets = [cls, xyxy]
+
+ height = img.shape[0] + border[0] * 2 # shape(h,w,c)
+ width = img.shape[1] + border[1] * 2
+
+ # Center
+ C = np.eye(3)
+ C[0, 2] = -img.shape[1] / 2 # x translation (pixels)
+ C[1, 2] = -img.shape[0] / 2 # y translation (pixels)
+
+ # Perspective
+ P = np.eye(3)
+ P[2, 0] = random.uniform(-perspective, perspective) # x perspective (about y)
+ P[2, 1] = random.uniform(-perspective, perspective) # y perspective (about x)
+
+ # Rotation and Scale
+ R = np.eye(3)
+ a = random.uniform(-degrees, degrees)
+ # a += random.choice([-180, -90, 0, 90]) # add 90deg rotations to small rotations
+ s = random.uniform(1 - scale, 1.1 + scale)
+ # s = 2 ** random.uniform(-scale, scale)
+ R[:2] = cv2.getRotationMatrix2D(angle=a, center=(0, 0), scale=s)
+
+ # Shear
+ S = np.eye(3)
+ S[0, 1] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # x shear (deg)
+ S[1, 0] = math.tan(random.uniform(-shear, shear) * math.pi / 180) # y shear (deg)
+
+ # Translation
+ T = np.eye(3)
+ T[0, 2] = random.uniform(0.5 - translate, 0.5 + translate) * width # x translation (pixels)
+ T[1, 2] = random.uniform(0.5 - translate, 0.5 + translate) * height # y translation (pixels)
+
+ # Combined rotation matrix
+ M = T @ S @ R @ P @ C # order of operations (right to left) is IMPORTANT
+ if (border[0] != 0) or (border[1] != 0) or (M != np.eye(3)).any(): # image changed
+ if perspective:
+ img = cv2.warpPerspective(img, M, dsize=(width, height), borderValue=(114, 114, 114))
+ else: # affine
+ img = cv2.warpAffine(img, M[:2], dsize=(width, height), borderValue=(114, 114, 114))
+
+ # Visualize
+ # import matplotlib.pyplot as plt
+ # ax = plt.subplots(1, 2, figsize=(12, 6))[1].ravel()
+ # ax[0].imshow(img[:, :, ::-1]) # base
+ # ax[1].imshow(img2[:, :, ::-1]) # warped
+
+ # Transform label coordinates
+ n = len(targets)
+ if n:
+ use_segments = any(x.any() for x in segments)
+ new = np.zeros((n, 4))
+ if use_segments: # warp segments
+ segments = resample_segments(segments) # upsample
+ for i, segment in enumerate(segments):
+ xy = np.ones((len(segment), 3))
+ xy[:, :2] = segment
+ xy = xy @ M.T # transform
+ xy = xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2] # perspective rescale or affine
+
+ # clip
+ new[i] = segment2box(xy, width, height)
+
+ else: # warp boxes
+ xy = np.ones((n * 4, 3))
+ xy[:, :2] = targets[:, [1, 2, 3, 4, 1, 4, 3, 2]].reshape(n * 4, 2) # x1y1, x2y2, x1y2, x2y1
+ xy = xy @ M.T # transform
+ xy = (xy[:, :2] / xy[:, 2:3] if perspective else xy[:, :2]).reshape(n, 8) # perspective rescale or affine
+
+ # create new boxes
+ x = xy[:, [0, 2, 4, 6]]
+ y = xy[:, [1, 3, 5, 7]]
+ new = np.concatenate((x.min(1), y.min(1), x.max(1), y.max(1))).reshape(4, n).T
+
+ # clip
+ new[:, [0, 2]] = new[:, [0, 2]].clip(0, width)
+ new[:, [1, 3]] = new[:, [1, 3]].clip(0, height)
+
+ # filter candidates
+ i = box_candidates(box1=targets[:, 1:5].T * s, box2=new.T, area_thr=0.01 if use_segments else 0.10)
+ targets = targets[i]
+ targets[:, 1:5] = new[i]
+
+ return img, targets
+
+
+def box_candidates(box1, box2, wh_thr=2, ar_thr=20, area_thr=0.1, eps=1e-16): # box1(4,n), box2(4,n)
+ # Compute candidate boxes: box1 before augment, box2 after augment, wh_thr (pixels), aspect_ratio_thr, area_ratio
+ w1, h1 = box1[2] - box1[0], box1[3] - box1[1]
+ w2, h2 = box2[2] - box2[0], box2[3] - box2[1]
+ ar = np.maximum(w2 / (h2 + eps), h2 / (w2 + eps)) # aspect ratio
+ return (w2 > wh_thr) & (h2 > wh_thr) & (w2 * h2 / (w1 * h1 + eps) > area_thr) & (ar < ar_thr) # candidates
+
+
+def bbox_ioa(box1, box2):
+ # Returns the intersection over box2 area given box1, box2. box1 is 4, box2 is nx4. boxes are x1y1x2y2
+ box2 = box2.transpose()
+
+ # Get the coordinates of bounding boxes
+ b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
+ b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
+
+ # Intersection area
+ inter_area = (np.minimum(b1_x2, b2_x2) - np.maximum(b1_x1, b2_x1)).clip(0) * \
+ (np.minimum(b1_y2, b2_y2) - np.maximum(b1_y1, b2_y1)).clip(0)
+
+ # box2 area
+ box2_area = (b2_x2 - b2_x1) * (b2_y2 - b2_y1) + 1e-16
+
+ # Intersection over box2 area
+ return inter_area / box2_area
+
+
+def cutout(image, labels):
+ # Applies image cutout augmentation https://arxiv.org/abs/1708.04552
+ h, w = image.shape[:2]
+
+ # create random masks
+ scales = [0.5] * 1 + [0.25] * 2 + [0.125] * 4 + [0.0625] * 8 + [0.03125] * 16 # image size fraction
+ for s in scales:
+ mask_h = random.randint(1, int(h * s))
+ mask_w = random.randint(1, int(w * s))
+
+ # box
+ xmin = max(0, random.randint(0, w) - mask_w // 2)
+ ymin = max(0, random.randint(0, h) - mask_h // 2)
+ xmax = min(w, xmin + mask_w)
+ ymax = min(h, ymin + mask_h)
+
+ # apply random color mask
+ image[ymin:ymax, xmin:xmax] = [random.randint(64, 191) for _ in range(3)]
+
+ # return unobscured labels
+ if len(labels) and s > 0.03:
+ box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32)
+ ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
+ labels = labels[ioa < 0.60] # remove >60% obscured labels
+
+ return labels
+
+
+def pastein(image, labels, sample_labels, sample_images, sample_masks):
+ # Applies image cutout augmentation https://arxiv.org/abs/1708.04552
+ h, w = image.shape[:2]
+
+ # create random masks
+ scales = [0.75] * 2 + [0.5] * 4 + [0.25] * 4 + [0.125] * 4 + [0.0625] * 6 # image size fraction
+ for s in scales:
+ if random.random() < 0.2:
+ continue
+ mask_h = random.randint(1, int(h * s))
+ mask_w = random.randint(1, int(w * s))
+
+ # box
+ xmin = max(0, random.randint(0, w) - mask_w // 2)
+ ymin = max(0, random.randint(0, h) - mask_h // 2)
+ xmax = min(w, xmin + mask_w)
+ ymax = min(h, ymin + mask_h)
+
+ box = np.array([xmin, ymin, xmax, ymax], dtype=np.float32)
+ if len(labels):
+ ioa = bbox_ioa(box, labels[:, 1:5]) # intersection over area
+ else:
+ ioa = np.zeros(1)
+
+ if (ioa < 0.30).all() and len(sample_labels) and (xmax > xmin+20) and (ymax > ymin+20): # allow 30% obscuration of existing labels
+ sel_ind = random.randint(0, len(sample_labels)-1)
+ #print(len(sample_labels))
+ #print(sel_ind)
+ #print((xmax-xmin, ymax-ymin))
+ #print(image[ymin:ymax, xmin:xmax].shape)
+ #print([[sample_labels[sel_ind], *box]])
+ #print(labels.shape)
+ hs, ws, cs = sample_images[sel_ind].shape
+ r_scale = min((ymax-ymin)/hs, (xmax-xmin)/ws)
+ r_w = int(ws*r_scale)
+ r_h = int(hs*r_scale)
+
+ if (r_w > 10) and (r_h > 10):
+ r_mask = cv2.resize(sample_masks[sel_ind], (r_w, r_h))
+ r_image = cv2.resize(sample_images[sel_ind], (r_w, r_h))
+ temp_crop = image[ymin:ymin+r_h, xmin:xmin+r_w]
+ m_ind = r_mask > 0
+ if m_ind.astype(np.int).sum() > 60:
+ temp_crop[m_ind] = r_image[m_ind]
+ #print(sample_labels[sel_ind])
+ #print(sample_images[sel_ind].shape)
+ #print(temp_crop.shape)
+ box = np.array([xmin, ymin, xmin+r_w, ymin+r_h], dtype=np.float32)
+ if len(labels):
+ labels = np.concatenate((labels, [[sample_labels[sel_ind], *box]]), 0)
+ else:
+ labels = np.array([[sample_labels[sel_ind], *box]])
+
+ image[ymin:ymin+r_h, xmin:xmin+r_w] = temp_crop
+
+ return labels
+
+class Albumentations:
+ # YOLOv5 Albumentations class (optional, only used if package is installed)
+ def __init__(self):
+ self.transform = None
+ import albumentations as A
+
+ self.transform = A.Compose([
+ A.CLAHE(p=0.01),
+ A.RandomBrightnessContrast(brightness_limit=0.2, contrast_limit=0.2, p=0.01),
+ A.RandomGamma(gamma_limit=[80, 120], p=0.01),
+ A.Blur(p=0.01),
+ A.MedianBlur(p=0.01),
+ A.ToGray(p=0.01),
+ A.ImageCompression(quality_lower=75, p=0.01),],
+ bbox_params=A.BboxParams(format='pascal_voc', label_fields=['class_labels']))
+
+ #logging.info(colorstr('albumentations: ') + ', '.join(f'{x}' for x in self.transform.transforms if x.p))
+
+ def __call__(self, im, labels, p=1.0):
+ if self.transform and random.random() < p:
+ new = self.transform(image=im, bboxes=labels[:, 1:], class_labels=labels[:, 0]) # transformed
+ im, labels = new['image'], np.array([[c, *b] for c, b in zip(new['class_labels'], new['bboxes'])])
+ return im, labels
+
+
+def create_folder(path='./new'):
+ # Create folder
+ if os.path.exists(path):
+ shutil.rmtree(path) # delete output folder
+ os.makedirs(path) # make new output folder
+
+
+def flatten_recursive(path='../coco'):
+ # Flatten a recursive directory by bringing all files to top level
+ new_path = Path(path + '_flat')
+ create_folder(new_path)
+ for file in tqdm(glob.glob(str(Path(path)) + '/**/*.*', recursive=True)):
+ shutil.copyfile(file, new_path / Path(file).name)
+
+
+def extract_boxes(path='../coco/'): # from utils.datasets import *; extract_boxes('../coco128')
+ # Convert detection dataset into classification dataset, with one directory per class
+
+ path = Path(path) # images dir
+ shutil.rmtree(path / 'classifier') if (path / 'classifier').is_dir() else None # remove existing
+ files = list(path.rglob('*.*'))
+ n = len(files) # number of files
+ for im_file in tqdm(files, total=n):
+ if im_file.suffix[1:] in img_formats:
+ # image
+ im = cv2.imread(str(im_file))[..., ::-1] # BGR to RGB
+ h, w = im.shape[:2]
+
+ # labels
+ lb_file = Path(img2label_paths([str(im_file)])[0])
+ if Path(lb_file).exists():
+ with open(lb_file, 'r') as f:
+ lb = np.array([x.split() for x in f.read().strip().splitlines()], dtype=np.float32) # labels
+
+ for j, x in enumerate(lb):
+ c = int(x[0]) # class
+ f = (path / 'classifier') / f'{c}' / f'{path.stem}_{im_file.stem}_{j}.jpg' # new filename
+ if not f.parent.is_dir():
+ f.parent.mkdir(parents=True)
+
+ b = x[1:] * [w, h, w, h] # box
+ # b[2:] = b[2:].max() # rectangle to square
+ b[2:] = b[2:] * 1.2 + 3 # pad
+ b = xywh2xyxy(b.reshape(-1, 4)).ravel().astype(np.int)
+
+ b[[0, 2]] = np.clip(b[[0, 2]], 0, w) # clip boxes outside of image
+ b[[1, 3]] = np.clip(b[[1, 3]], 0, h)
+ assert cv2.imwrite(str(f), im[b[1]:b[3], b[0]:b[2]]), f'box failure in {f}'
+
+
+def autosplit(path='../coco', weights=(0.9, 0.1, 0.0), annotated_only=False):
+ """ Autosplit a dataset into train/val/test splits and save path/autosplit_*.txt files
+ Usage: from utils.datasets import *; autosplit('../coco')
+ Arguments
+ path: Path to images directory
+ weights: Train, val, test weights (list)
+ annotated_only: Only use images with an annotated txt file
+ """
+ path = Path(path) # images dir
+ files = sum([list(path.rglob(f"*.{img_ext}")) for img_ext in img_formats], []) # image files only
+ n = len(files) # number of files
+ indices = random.choices([0, 1, 2], weights=weights, k=n) # assign each image to a split
+
+ txt = ['autosplit_train.txt', 'autosplit_val.txt', 'autosplit_test.txt'] # 3 txt files
+ [(path / x).unlink() for x in txt if (path / x).exists()] # remove existing
+
+ print(f'Autosplitting images from {path}' + ', using *.txt labeled images only' * annotated_only)
+ for i, img in tqdm(zip(indices, files), total=n):
+ if not annotated_only or Path(img2label_paths([str(img)])[0]).exists(): # check label
+ with open(path / txt[i], 'a') as f:
+ f.write(str(img) + '\n') # add image to txt file
+
+
+def load_segmentations(self, index):
+ key = '/work/handsomejw66/coco17/' + self.img_files[index]
+ #print(key)
+ # /work/handsomejw66/coco17/
+ return self.segs[key]
diff --git a/ai/detector_rev2.yaml b/ai/detector_rev2.yaml
new file mode 100644
index 0000000..6537433
--- /dev/null
+++ b/ai/detector_rev2.yaml
@@ -0,0 +1,10 @@
+train: /data/dataset/detector_train.txt
+val: /data/dataset/detector_val.txt
+test: /data/dataset/detector_test.txt
+
+nc: 12
+
+names: ['Airbus A319', 'Airbus A320', 'Airbus A321',
+ 'Beechcraft Bonanza', 'Boeing 737', 'Bombardier CL-600',
+ 'Cessna Citation', 'Cessna Skyhawk', 'Cessna Skylane',
+ 'Embraer ERJ-170', 'Piper PA-28 Cherokee', 'Piper PA-32 Cherokee Six']
diff --git a/ai/localizer_rev2.yaml b/ai/localizer_rev2.yaml
new file mode 100644
index 0000000..31396a2
--- /dev/null
+++ b/ai/localizer_rev2.yaml
@@ -0,0 +1,9 @@
+train: /data/dataset/localizer_train
+val: /data/dataset/localizer_val
+test: [/data/dataset/detector_train, /data/dataset/detector_val]
+test2: /data/dataset/localizer_test
+test3: /data/dataset/detector_test
+
+nc: 1
+
+names: ['aircraft']
diff --git a/ai/metrics2.py b/ai/metrics2.py
new file mode 100644
index 0000000..61cd550
--- /dev/null
+++ b/ai/metrics2.py
@@ -0,0 +1,280 @@
+# Model validation metrics
+
+from pathlib import Path
+
+import matplotlib.pyplot as plt
+import numpy as np
+import torch
+import sys
+import math
+
+from . import general
+
+
+def fitness(x):
+ # Model fitness as a weighted combination of metrics
+ w = [0.0, 0.0, 0.1, 0.9] # weights for [P, R, mAP@0.5, mAP@0.5:0.95]
+ return (x[:, :4] * w).sum(1)
+
+
+def ap_per_class(tp, conf, pred_cls, target_cls, v5_metric=False, plot=False, save_dir='.', names=()):
+ """ Compute the average precision, given the recall and precision curves.
+ Source: https://github.com/rafaelpadilla/Object-Detection-Metrics.
+ # Arguments
+ tp: True positives (nparray, nx1 or nx10).
+ conf: Objectness value from 0-1 (nparray).
+ pred_cls: Predicted object classes (nparray).
+ target_cls: True object classes (nparray).
+ plot: Plot precision-recall curve at mAP@0.5
+ save_dir: Plot save directory
+ # Returns
+ The average precision as computed in py-faster-rcnn.
+ """
+
+ # Sort by objectness
+ i = np.argsort(-conf)
+ tp, conf, pred_cls = tp[i], conf[i], pred_cls[i]
+
+ # Find unique classes
+ unique_classes = np.unique(target_cls)
+ nc = unique_classes.shape[0] # number of classes, number of detections
+
+ # Create Precision-Recall curve and compute AP for each class
+ px, py = np.linspace(0, 1, 1000), [] # for plotting
+ ap, p, r = np.zeros((nc, tp.shape[1])), np.zeros((nc, 1000)), np.zeros((nc, 1000))
+ for ci, c in enumerate(unique_classes):
+ i = pred_cls == c
+ n_l = (target_cls == c).sum() # number of labels
+ n_p = i.sum() # number of predictions
+
+ if n_p == 0 or n_l == 0:
+ continue
+ else:
+ # Accumulate FPs and TPs
+ fpc = (1 - tp[i]).cumsum(0)
+ tpc = tp[i].cumsum(0)
+
+ # Recall
+ recall = tpc / (n_l + 1e-16) # recall curve
+ r[ci] = np.interp(-px, -conf[i], recall[:, 0], left=0) # negative x, xp because xp decreases
+
+ # Precision
+ precision = tpc / (tpc + fpc) # precision curve
+ p[ci] = np.interp(-px, -conf[i], precision[:, 0], left=1) # p at pr_score
+
+ # AP from recall-precision curve
+ for j in range(tp.shape[1]):
+ ap[ci, j], mpre, mrec = compute_ap(recall[:, j], precision[:, j], v5_metric=v5_metric)
+ if plot and j == 0:
+ py.append(np.interp(px, mrec, mpre)) # precision at mAP@0.5
+
+ # Compute F1 (harmonic mean of precision and recall)
+ f1 = 2 * p * r / (p + r + 1e-16)
+ if plot:
+ plot_pr_curve(px, py, ap, Path(save_dir) / 'PR_curve.png', names)
+ plot_mc_curve(px, f1, Path(save_dir) / 'F1_curve.png', names, ylabel='F1')
+ plot_mc_curve(px, p, Path(save_dir) / 'P_curve.png', names, ylabel='Precision')
+ plot_mc_curve(px, r, Path(save_dir) / 'R_curve.png', names, ylabel='Recall')
+
+ i = f1.mean(0).argmax() # max F1 index
+ return p[:, i], r[:, i], ap, f1[:, i], unique_classes.astype('int32')
+
+
+def compute_ap(recall, precision, v5_metric=False):
+ """ Compute the average precision, given the recall and precision curves
+ # Arguments
+ recall: The recall curve (list)
+ precision: The precision curve (list)
+ v5_metric: Assume maximum recall to be 1.0, as in YOLOv5, MMDetetion etc.
+ # Returns
+ Average precision, precision curve, recall curve
+ """
+
+ # Append sentinel values to beginning and end
+ if v5_metric: # New YOLOv5 metric, same as MMDetection and Detectron2 repositories
+ mrec = np.concatenate(([0.], recall, [1.0]))
+ else: # Old YOLOv5 metric, i.e. default YOLOv7 metric
+ mrec = np.concatenate(([0.], recall, [recall[-1] + 0.01]))
+ mpre = np.concatenate(([1.], precision, [0.]))
+
+ # Compute the precision envelope
+ mpre = np.flip(np.maximum.accumulate(np.flip(mpre)))
+
+ # Integrate area under curve
+ method = 'interp' # methods: 'continuous', 'interp'
+ if method == 'interp':
+ x = np.linspace(0, 1, 101) # 101-point interp (COCO)
+ ap = np.trapz(np.interp(x, mrec, mpre), x) # integrate
+ else: # 'continuous'
+ i = np.where(mrec[1:] != mrec[:-1])[0] # points where x axis (recall) changes
+ ap = np.sum((mrec[i + 1] - mrec[i]) * mpre[i + 1]) # area under curve
+
+ return ap, mpre, mrec
+
+
+class ConfusionMatrix:
+ # Updated version of https://github.com/kaanakan/object_detection_confusion_matrix
+ # Further updates made here
+ def __init__(self, nc, conf=0.25, iou_thres=0.45):
+ self.matrix = np.zeros((nc + 1, nc + 1))
+ self.nc = nc # number of classes
+ self.conf = conf
+ self.iou_thres = iou_thres
+
+ def process_batch(self, detections, labels):
+ """
+ Return intersection-over-union (Jaccard index) of boxes.
+ Both sets of boxes are expected to be in (x1, y1, x2, y2) format.
+ Arguments:
+ detections (Array[N, 6]), x1, y1, x2, y2, conf, class
+ labels (Array[M, 5]), class, x1, y1, x2, y2
+ Returns:
+ None, updates confusion matrix accordingly
+ """
+ detections = detections[detections[:, 4] > self.conf]
+ gt_classes = labels[:, 0].int()
+ detection_classes = detections[:, 5].int()
+ iou = general.box_iou(labels[:, 1:], detections[:, :4])
+
+ x = torch.where(iou > self.iou_thres)
+ if x[0].shape[0]:
+ matches = torch.cat((torch.stack(x, 1), iou[x[0], x[1]][:, None]), 1).cpu().numpy()
+ if x[0].shape[0] > 1:
+ matches = matches[matches[:, 2].argsort()[::-1]]
+ matches = matches[np.unique(matches[:, 1], return_index=True)[1]]
+ matches = matches[matches[:, 2].argsort()[::-1]]
+ matches = matches[np.unique(matches[:, 0], return_index=True)[1]]
+ else:
+ matches = np.zeros((0, 3))
+
+ n = matches.shape[0] > 0
+ m0, m1, _ = matches.transpose().astype(np.int16)
+ for i, gc in enumerate(gt_classes):
+ j = m0 == i
+ if n and sum(j) == 1:
+ self.matrix[gc, detection_classes[m1[j]]] += 1 # correct
+ else:
+ self.matrix[gc, self.nc] += 1 # background FP
+
+ if n:
+ for i, dc in enumerate(detection_classes):
+ if not any(m1 == i):
+ self.matrix[self.nc, dc] += 1 # background FN
+
+ def matrix(self):
+ return self.matrix
+
+ def plot(self, save_dir='', names=(), normalize=True, transpose=False,
+ filename=None, round_to_int=False):
+ try:
+ import seaborn as sn
+
+ if normalize:
+ array = self.matrix / (self.matrix.sum(0).reshape(1, self.nc + 1) + 1E-6) # normalize
+ else:
+ array = self.matrix.copy()
+ if transpose:
+ array = np.transpose(array)
+ array[array < 0.005] = np.nan # don't annotate (would appear as 0.00)
+
+ fig = plt.figure(figsize=(12, 9), tight_layout=True)
+ sn.set(font_scale=1.0 if self.nc < 50 else 0.8) # for label size
+ labels = (0 < len(names) < 99) and len(names) == self.nc # apply names to ticklabels
+ if not transpose:
+ xticklabels=names + ['background FN'] if labels else "auto"
+ yticklabels=names + ['background FP'] if labels else "auto"
+ else:
+ xticklabels=names + ['background FP'] if labels else "auto"
+ yticklabels=names + ['background FN'] if labels else "auto"
+ if round_to_int:
+ fmt = '.0f'
+ else:
+ fmt = '.2f'
+ sn.heatmap(array, annot=self.nc < 30, annot_kws={"size": 8},
+ cmap='Blues', fmt=fmt, square=True,
+ xticklabels=xticklabels,
+ yticklabels=yticklabels).set_facecolor((1, 1, 1))
+ if not transpose:
+ fig.axes[0].set_xlabel('Predicted')
+ fig.axes[0].set_ylabel('True')
+ else:
+ fig.axes[0].set_xlabel('True')
+ fig.axes[0].set_ylabel('Predicted')
+ if filename is None:
+ fig.savefig(Path(save_dir) / 'confusion_matrix.png', dpi=250)
+ else:
+ fig.savefig(Path(save_dir) / (filename + '.png'), dpi=250)
+ except Exception as e:
+ pass
+
+ def print(self):
+ for i in range(self.nc + 1):
+ print(' '.join(map(str, self.matrix[i])))
+
+ def write(self, path):
+ with open(path, 'w') as wfile:
+ stdout = sys.stdout
+ sys.stdout = wfile
+ self.print()
+ sys.stdout = stdout
+
+ def read(self, path):
+ data = np.fromfile(path, sep=' ')
+ self.nc = round(math.sqrt(len(data))) - 1
+ self.matrix = np.reshape(data, (self.nc + 1, self.nc + 1))
+
+ def norm(self, axis=None):
+ # Returns a new confusion matrix that's normalized
+ if axis is None:
+ m = np.copy(self.matrix)
+ elif axis == 0:
+ m = self.matrix / (self.matrix.sum(0).reshape(1, self.nc + 1) + 1E-6)
+ elif axis == 1:
+ m = self.matrix / (self.matrix.sum(1).reshape(self.nc + 1, 1) + 1E-6)
+ else:
+ raise Exception('! Invalid axis')
+ cm = ConfusionMatrix(self.nc, self.conf, self.iou_thres)
+ cm.matrix = m
+ return cm
+
+
+# Plots ----------------------------------------------------------------------------------------------------------------
+
+def plot_pr_curve(px, py, ap, save_dir='pr_curve.png', names=()):
+ # Precision-recall curve
+ fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
+ py = np.stack(py, axis=1)
+
+ if 0 < len(names) < 21: # display per-class legend if < 21 classes
+ for i, y in enumerate(py.T):
+ ax.plot(px, y, linewidth=1, label=f'{names[i]} {ap[i, 0]:.3f}') # plot(recall, precision)
+ else:
+ ax.plot(px, py, linewidth=1, color='grey') # plot(recall, precision)
+
+ ax.plot(px, py.mean(1), linewidth=3, color='blue', label='all classes %.3f mAP@0.5' % ap[:, 0].mean())
+ ax.set_xlabel('Recall')
+ ax.set_ylabel('Precision')
+ ax.set_xlim(0, 1)
+ ax.set_ylim(0, 1)
+ plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
+ fig.savefig(Path(save_dir), dpi=250)
+
+
+def plot_mc_curve(px, py, save_dir='mc_curve.png', names=(), xlabel='Confidence', ylabel='Metric'):
+ # Metric-confidence curve
+ fig, ax = plt.subplots(1, 1, figsize=(9, 6), tight_layout=True)
+
+ if 0 < len(names) < 21: # display per-class legend if < 21 classes
+ for i, y in enumerate(py):
+ ax.plot(px, y, linewidth=1, label=f'{names[i]}') # plot(confidence, metric)
+ else:
+ ax.plot(px, py.T, linewidth=1, color='grey') # plot(confidence, metric)
+
+ y = py.mean(0)
+ ax.plot(px, y, linewidth=3, color='blue', label=f'all classes {y.max():.2f} at {px[y.argmax()]:.3f}')
+ ax.set_xlabel(xlabel)
+ ax.set_ylabel(ylabel)
+ ax.set_xlim(0, 1)
+ ax.set_ylim(0, 1)
+ plt.legend(bbox_to_anchor=(1.04, 1), loc="upper left")
+ fig.savefig(Path(save_dir), dpi=250)
diff --git a/ai/model.py b/ai/model.py
new file mode 100755
index 0000000..c37530c
--- /dev/null
+++ b/ai/model.py
@@ -0,0 +1,1116 @@
+#!/usr/bin/env python
+# coding: utf-8
+
+"""
+SkyScan Edge AI
+"""
+
+import os
+import sys
+import json
+import glob
+import shutil
+import subprocess
+import numpy as np
+import pandas as pd
+import IPython.display
+
+from utils.metrics2 import ConfusionMatrix
+
+display_width = 120
+default_seed = 1903
+
+class Dataset:
+
+ def create_from_images(self, path, suffix='jpg'):
+ """
+ Initialize dataframe using a directory of images
+ """
+ image_paths = sorted(glob.glob(path + '/**/*.'+suffix, recursive=True))
+ self.df = pd.DataFrame(image_paths, columns=['path'])
+ self.df['filename'] = self.df['path'].apply(
+ lambda x: os.path.basename(x))
+ self.df['icao24'] = self.df['path'].apply(
+ lambda x: os.path.basename(x).split('_')[0].lower())
+
+ def create_from_dirs(self, paths, suffix='jpg'):
+ """
+ Initialize dataframe using a list of directories of images
+ """
+ image_paths = []
+ for path in paths:
+ more_image_paths = sorted(glob.glob(path + '/**/*.'+suffix,
+ recursive=True))
+ image_paths.extend(more_image_paths)
+ self.df = pd.DataFrame(image_paths, columns=['path'])
+ self.df['filename'] = self.df['path'].apply(
+ lambda x: os.path.basename(x))
+ self.df['icao24'] = self.df['path'].apply(
+ lambda x: os.path.basename(x).split('_')[0].lower())
+
+ def create_from_paths_file(self, path, suffix='jpg'):
+ """
+ Initialize dataframe using a file of paths
+ Note: Assumes paths are relative to the folder from which code is run
+ """
+ #path_file = open(path, 'r')
+ with open(path, 'r') as path_file:
+ image_paths = path_file.readlines()
+ self.df = pd.DataFrame(image_paths, columns=['path'])
+ self.df['filename'] = self.df['path'].apply(
+ lambda x: os.path.basename(x))
+ self.df['icao24'] = self.df['path'].apply(
+ lambda x: os.path.basename(x).split('_')[0].lower())
+
+ def concat(self, second_dataset):
+ """
+ Return a new dataset which is the concatenation of this one
+ and the provided `second_dataset`
+ """
+ new_dataset = Dataset()
+ new_dataset.df = pd.concat((self.df, second_dataset.df),
+ ignore_index=True)
+ return new_dataset
+
+ def load_makemodel_string(self, database_path=None, method=1):
+ """
+ Add raw make+model string from 3rd-party database
+ """
+ if method == 0:
+ # Assumes images are already sorted into folders named after
+ # make+model string
+ df['string'] = df['path'].apply(lambda x: x.split('/')[-2])
+ else:
+ # Convert icao24 (ICAO 24-bit address) to make+model string with
+ # OpenSky Network database. Source:
+ # https://opensky-network.org/datasets/metadata/aircraftDatabase.csv
+
+ # Step 1: Load and format lookup table
+ database_dataframe = pd.read_csv(database_path, usecols=[
+ 'icao24', 'manufacturername', 'model'])
+ database_dataframe['string'] = database_dataframe[
+ 'manufacturername'] + ' ' + database_dataframe['model']
+ database_dataframe = database_dataframe[['icao24', 'string']]
+
+ # Step 2: Join tables
+ self.df = pd.merge(self.df, database_dataframe,
+ how='left', on='icao24')
+ database_dataframe = None
+
+ def load_makemodel_mm(self, hierarchy_path,
+ print_lookup_table=False, warn_missing=True):
+ """
+ Convert raw make+model string to sanitized version with a lookup table
+ """
+
+ # Convert class hierarchy to table of make/model/string combinations
+ # Note: 'string' is unsanitized make+model from database;
+ # 'mm' is make+model after data cleaning
+ class_hierarchy = json.load(open(hierarchy_path))
+ class_array = np.concatenate([np.concatenate([np.stack([
+ np.array([make + ' ' + model, make, model, string])
+ for string in class_hierarchy['make_model_strings'][make][model]], axis=0)
+ for model in class_hierarchy['make_model_strings'][make]], axis=0)
+ for make in class_hierarchy['make_model_strings']], axis=0)
+ class_dataframe = pd.DataFrame(class_array, columns=['mm', 'make', 'model', 'string'])
+
+ if print_lookup_table:
+ with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.width', display_width):
+ print(class_dataframe[['mm', 'string']])
+
+ # Join class information to dataset item information
+ self.df = pd.merge(self.df, class_dataframe, how='left', on='string')
+
+ # Check for items not found in class hierarchy
+ if warn_missing:
+ missing = sorted(self.df[self.df['mm'].isna() & ~self.df['string'].isna()]['string'].unique())
+ if len(missing) > 0:
+ print('! Make+model strings missing from class hierarchy!')
+ print(missing)
+
+ # Return class hierarchy
+ return class_hierarchy
+
+ def print_classes(self, group_field='mm', title=None, condition=None):
+ """
+ Print count of each class
+ """
+ if title is not None:
+ print('-' * display_width)
+ print(title)
+ if condition is not None:
+ df = self.df[condition]
+ else:
+ df = self.df
+ image_count = df.groupby(group_field)[group_field].count()
+ plane_count = df.groupby('icao24').head(1).groupby(group_field)[group_field].count()
+ count_dataframe = pd.concat([image_count, plane_count], axis=1)
+ count_dataframe.columns=['image_count', 'aircraft_count']
+ with pd.option_context('display.max_rows', None,
+ 'display.width', display_width):
+ print(count_dataframe)
+ print()
+ print('Identified images:', image_count.sum())
+ print('Identified aircraft:', plane_count.sum())
+ print('Total images:', df['path'].count())
+ print('Total aircraft:', df.groupby('icao24')['icao24'].head(1).count())
+
+ def add_usable(self, detector_classes, block_makes=None):
+ """
+ Mark usable items for localizer and detector
+ """
+ # Identified (and unidentified, if not overwritten below)
+ if block_makes is not None:
+ self.df['localizer_usable'] = ~self.df['make'].isin(block_makes)
+ else:
+ self.df['localizer_usable'] = True
+ self.df['detector_usable'] = self.df['mm'].isin(detector_classes)
+
+ # Unidentified (if different from above)
+ # self.df.loc[self.df['mm'].isna(), 'localizer_usable'] = True
+ # self.df.loc[self.df['mm'].isna(), 'detector_usable'] = False
+
+ def print(self, title=None, condition=None):
+ """
+ Print dataframe
+ """
+ if title is not None:
+ print('-' * display_width)
+ print(title)
+ with pd.option_context('display.max_columns', None,
+ 'display.width', display_width):
+ if condition is None:
+ print(self.df)
+ else:
+ print(self.df[condition])
+
+ def write_suggested_labels(self, output_path,
+ max_planes_per_class=3,
+ max_images_per_plane=5,
+ seed1=default_seed,
+ seed2=default_seed):
+ """
+ Write a file with a list of suggested images to hand-label
+ with bounding boxes. Include N random images per plane from
+ up to M planes of each class, to emphasize less common classes.
+ """
+ # Step 1: Select planes
+ planes_to_label = self.df.groupby('icao24').head(1)[['icao24', 'mm']].sample(frac=1, random_state=seed1).groupby('mm').head(max_planes_per_class).sort_values(by='mm')
+
+ # Step 2: Select images
+ images_to_label = self.df[self.df['icao24'].isin(planes_to_label['icao24'])]
+ images_to_label = images_to_label[images_to_label['localizer_usable']]
+ images_to_label = images_to_label.sample(frac=1, random_state=seed2).groupby('icao24').head(max_images_per_plane).sort_values(by=['mm', 'icao24'])
+
+ # Step 3: Save
+ images_to_label['path'].to_csv(output_path, header=False, index=False)
+
+ def write_suggested_labels_prop(self, output_path,
+ num_planes=10,
+ max_images_per_plane=5,
+ seed1=default_seed,
+ seed2=default_seed):
+ """
+ Write a file with a list of suggested images to hand-label
+ with bounding boxes. Include N random images per plane from
+ each of M planes, to follow the observed distribution of classes.
+ """
+ # Step 1: Select planes
+ planes_to_label = self.df.groupby('icao24').head(1)[['icao24', 'mm']].sample(frac=1, random_state=seed1).head(num_planes).sort_values(by=['mm', 'icao24'])
+
+ # Step 2: Select images
+ images_to_label = self.df[self.df['icao24'].isin(planes_to_label['icao24'])]
+ images_to_label = images_to_label[images_to_label['localizer_usable']]
+ images_to_label = images_to_label.sample(frac=1, random_state=seed2).groupby('icao24').head(max_images_per_plane).sort_values(by=['mm', 'icao24'])
+
+ # Step 3: Save
+ images_to_label['path'].to_csv(output_path, header=False, index=False)
+
+ def load_labelbox_labels(self, input_path,
+ img_height=1080, img_width=1920, min_area=100):
+ """
+ Load manual bounding boxes from LabelBox export file into dataframe
+ """
+ label_struct = json.load(open(input_path))
+
+ self.df['manual_bbox_flag'] = False
+ self.df['manual_bbox_count'] = 0
+ self.df['manual_bbox_desc'] = ''
+ filename_index = pd.Index(self.df['filename'])
+ for item in label_struct:
+ # Find this item in dataframe
+ filename = item['External ID']
+ try:
+ loc = filename_index.get_loc(filename)
+ except KeyError:
+ print('! Did not find entry for', filename)
+ continue
+
+ # Parse hand-labeled bounding box
+ desc = ''
+ valid_boxes = 0
+ for obj in item['Label']['objects']:
+ bbox = obj['bbox']
+ top = bbox['top']
+ left = bbox['left']
+ height = bbox['height']
+ width = bbox['width']
+ if height * width >= min_area:
+ if valid_boxes > 0:
+ desc += '\n'
+ desc += '0 %f %f %f %f' % (
+ (left + 0.5 * width) / img_width,
+ (top + 0.5 * height) / img_height,
+ width / img_width,
+ height / img_height
+ )
+ valid_boxes += 1
+ self.df.at[loc, 'manual_bbox_flag'] = True
+ self.df.at[loc, 'manual_bbox_count'] = valid_boxes
+ self.df.at[loc, 'manual_bbox_desc'] = desc
+
+ def load_yolov7_labels(self, name,
+ img_height=1080, img_width=1920,
+ min_area=100,
+ always_flag=['detector_train', 'detector_test']):
+ """
+ Load YOLOv7-generated bounding boxes
+ """
+ path = os.path.join('runs/test', name, 'best_predictions.json')
+ label_struct = json.load(open(path))
+
+ self.df['auto_bbox_flag'] = False
+ self.df['auto_bbox_count'] = 0
+ self.df['auto_bbox_desc'] = ''
+ filename_index = pd.Index(self.df['filename'].str[:-4])
+ for entry in label_struct:
+ # Find this item in dataframe
+ filename = entry['image_id']
+ loc = filename_index.get_loc(filename)
+
+ # Parse auto-labeled bounding box
+ left, top, width, height = tuple(entry['bbox'])
+ if height * width >= min_area:
+ if self.df.loc[loc, 'auto_bbox_count'] > 0:
+ self.df.loc[loc, 'auto_bbox_desc'] += '\n'
+ self.df.loc[loc, 'auto_bbox_desc'] += '0 %f %f %f %f' % (
+ (left + 0.5 * width) / img_width,
+ (top + 0.5 * height) / img_height,
+ width / img_width,
+ height / img_height
+ )
+ self.df.loc[loc, 'auto_bbox_count'] += 1
+
+ # Flag all entries for which bboxes were sought
+ # (e.g., detector train/test), even those with no bboxes found
+ for colname in always_flag:
+ self.df.loc[self.df[colname], 'auto_bbox_flag'] = True
+
+ def split_train_test(self, detector_classes,
+ localizer_train_count = 2,
+ detector_test_count = 3,
+ seed3 = default_seed,
+ seed4 = default_seed,
+ verbose = True):
+ """
+ Divides images into four categories: localizer training, localizer
+ testing, detector training, and detector testing. The division is
+ such that no one plane's images appear in more than one category.
+ Not all images are assigned, because localizer categories can only
+ use a plane's hand-labeled bounding boxes.
+ """
+
+ # Assign planes to use: train/test for localizer/detector
+ planes = self.df[['icao24', 'mm', 'manual_bbox_flag']].groupby('icao24', as_index=False).max()
+ planes_manual = planes[planes['manual_bbox_flag']]
+ planes_notman = planes[~planes['manual_bbox_flag']]
+ planes_localizer = planes_manual
+ planes_localizer_train = planes_localizer.sample(frac=1, random_state=seed3).groupby('mm', as_index=False).head(localizer_train_count)
+ planes_localizer_test = planes_localizer[~planes_localizer['icao24'].isin(planes_localizer_train['icao24'])]
+ planes_detector = planes_notman[planes_notman['mm'].isin(detector_classes)]
+ planes_detector_test = planes_detector.sample(frac=1, random_state=seed4).groupby('mm', as_index=False).head(detector_test_count)
+ planes_detector_train = planes_detector[~planes_detector['icao24'].isin(planes_detector_test['icao24'])]
+
+ # Print plane counts
+ if verbose:
+ print('planes', len(planes))
+ print('planes_manual', len(planes_manual))
+ print('planes_notman', len(planes_notman))
+ print('planes_localizer_train', len(planes_localizer_train))
+ print('planes_localizer_test', len(planes_localizer_test))
+ print('planes_detector_train', len(planes_detector_train))
+ print('planes_detector_test', len(planes_detector_test))
+
+ # Assign dataset items to use: train/test for localizer/detector
+ items_localizer_train = self.df[self.df['icao24'].isin(planes_localizer_train['icao24'])]
+ items_localizer_train = items_localizer_train[items_localizer_train['manual_bbox_flag']]
+ items_localizer_test = self.df[self.df['icao24'].isin(planes_localizer_test['icao24'])]
+ items_localizer_test = items_localizer_test[items_localizer_test['manual_bbox_flag']]
+ items_detector_train = self.df[self.df['icao24'].isin(planes_detector_train['icao24'])]
+ items_detector_test = self.df[self.df['icao24'].isin(planes_detector_test['icao24'])]
+
+ # Save to main dataframe
+ self.df['localizer_train'] = self.df['path'].isin(items_localizer_train['path'])
+ self.df['localizer_test'] = self.df['path'].isin(items_localizer_test['path'])
+ self.df['detector_train'] = self.df['path'].isin(items_detector_train['path'])
+ self.df['detector_test'] = self.df['path'].isin(items_detector_test['path'])
+
+ # Print item counts
+ if verbose:
+ print('items', self.df.shape[0])
+ print('items_localizer_train', self.df['localizer_train'].sum())
+ print('items_localizer_test', self.df['localizer_test'].sum())
+ print('items_detector_train', self.df['detector_train'].sum())
+ print('items_detector_test', self.df['detector_test'].sum())
+
+ def split_train_val_shared(self, detector_classes,
+ val_fraction = 0.3,
+ seed3 = default_seed,
+ verbose = True):
+ """
+ Assigns images to four categories: localizer training, localizer
+ validation, detector training, and detector validation. The localizer
+ images are a proper subset of the detector images, but no image is used
+ for both training and validation.
+ """
+
+ # Assign planes to use: either train or validation
+ planes = self.df[['icao24', 'mm', 'manual_bbox_flag']].groupby('icao24', as_index=False).max()
+ val_count = int(val_fraction * len(planes))
+ planes_randomized = planes.sample(frac=1, random_state=seed3)
+ planes_train = planes_randomized.iloc[val_count:]
+ planes_val = planes_randomized.iloc[:val_count]
+
+ # Assign dataset items to use: train/validation for localizer/detector
+ items_train = self.df[self.df['icao24'].isin(planes_train['icao24'])]
+ items_val = self.df[self.df['icao24'].isin(planes_val['icao24'])]
+ items_localizer_train = items_train[items_train['manual_bbox_flag']]
+ items_localizer_val = items_val[items_val['manual_bbox_flag']]
+ items_detector_train = items_train[items_train['mm'].isin(detector_classes)]
+ items_detector_val = items_val[items_val['mm'].isin(detector_classes)]
+
+ # Save to main dataframe
+ self.df['localizer_train'] = self.df['path'].isin(
+ items_localizer_train['path'])
+ self.df['localizer_val'] = self.df['path'].isin(
+ items_localizer_val['path'])
+ self.df['detector_train'] = self.df['path'].isin(
+ items_detector_train['path'])
+ self.df['detector_val'] = self.df['path'].isin(
+ items_detector_val['path'])
+
+ # Print plane counts
+ if verbose:
+ print('planes', len(planes))
+ print('planes_train', len(planes_train))
+ print('planes_val', len(planes_val))
+
+ # Print item counts
+ if verbose:
+ print('items', self.df.shape[0])
+ print('items_train', len(items_train))
+ print('items_val', len(items_val))
+ print('items_localizer_train', self.df['localizer_train'].sum())
+ print('items_localizer_val', self.df['localizer_val'].sum())
+ print('items_detector_train', self.df['detector_train'].sum())
+ print('items_detector_val', self.df['detector_val'].sum())
+
+ def check_categories_nonintersecting(self, categories):
+ """
+ Confirm that no plane's images appear in multiple usage categories
+ """
+ for cat1idx in range(len(categories)):
+ cat1 = set(self.df.loc[self.df[categories[cat1idx]], 'icao24'])
+ for cat2idx in range(cat1idx + 1, len(categories)):
+ cat2 = set(self.df.loc[self.df[categories[cat2idx]], 'icao24'])
+ inter = cat1.intersection(cat2)
+ if len(inter) > 0:
+ print('! Warning: Category Overlap!',
+ categories[cat1idx], categories[cat2idx])
+
+ def write_folders(self, base_path, categories):
+ """
+ Create folders to hold dataset files
+ """
+ for category in categories:
+ os.makedirs(os.path.join(base_path, category, 'images'),
+ exist_ok=True)
+ os.makedirs(os.path.join(base_path, category, 'labels'),
+ exist_ok=True)
+
+ def write_images(self, base_path, categories):
+ """
+ Write dataset images
+ """
+ for category in categories:
+ dest_dir = os.path.join(base_path, category, 'images')
+ for src_path in self.df.loc[self.df[category], 'path']:
+ shutil.copy2(src_path, dest_dir)
+
+ def write_labels(self, base_path, categories, col, condition=None):
+ """
+ Write dataset labels
+ """
+ for category in categories:
+ for idx, row in self.df[self.df[category]].iterrows():
+ if condition is None or condition[idx]:
+ dest_name = os.path.splitext(row['filename'])[0] + '.txt'
+ dest_path = os.path.join(base_path, category,
+ 'labels', dest_name)
+ dest_file = open(dest_path, 'w')
+ print(row[col], file=dest_file)
+ dest_file.close()
+
+ def write_image_lists(self, base_path, categories, condition):
+ """
+ Write image lists, using only images that meet condition
+ """
+ for category in categories:
+ list_content = './' + category + '/images/' + self.df.loc[
+ self.df[category] & condition, 'filename']
+ list_content.to_csv(os.path.join(base_path, category + '.txt'),
+ header=False, index=False)
+
+ def add_class_labels(self, detector_classes, condition=None,
+ input_col='auto_bbox_desc',
+ output_col='auto_bboxclass_desc'):
+ """
+ Given localizer bounding boxes (which are all of class zero),
+ generate detector bounding boxes (which indicate class of plane)
+ """
+ detector_classes_dict = {x:y for y, x in enumerate(detector_classes)}
+ self.df[output_col] = ''
+ for idx, row in self.df.iterrows():
+ if condition is None or condition[idx]:
+ dest_cont = str(detector_classes_dict[row['mm']]) + row[input_col][1:].replace('\n0', '\n' + str(detector_classes_dict[row['mm']])) # Assumes input class is zero
+ self.df.loc[idx, output_col] = dest_cont
+
+ def train(self,
+ weights='yolov7-tiny.pt',
+ cfg='cfg/training/yolov7-tiny.yaml',
+ data=None, #e.g., /code/localizer.yaml
+ hyp='data/hyp.scratch.tiny.yaml',
+ epochs=None, #e.g., 500
+ batchsize=4,
+ workers=4,
+ name=None, #e.g., yolov7_localizer_01_1
+ ):
+ """
+ Train YOLOv7 model
+ """
+ cmd = 'python train.py'
+ cmd += ' --weights ' + weights
+ cmd += ' --cfg ' + cfg
+ cmd += ' --data ' + data
+ cmd += ' --hyp ' + hyp
+ cmd += ' --epochs ' + str(epochs)
+ cmd += ' --batch-size ' + str(batchsize)
+ cmd += ' --workers ' + str(workers)
+ cmd += ' --name ' + name
+ print(cmd)
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, bufsize=1)
+ for line in p.stdout:
+ print(line)
+
+ def test(self,
+ train_name, #e.g., yolov7_localizer_01_1
+ test_name, #e.g., yolov7_localizer_01_1
+ data, #e.g., /code/localizer.yaml
+ batchsize=32,
+ confthres=0.25,
+ iouthres=0.5,
+ task='test',
+ ):
+ """
+ Run YOLOv7 model to generate labels on categories listed in yaml file
+ """
+ weights = os.path.join('runs/train/', train_name, 'weights/best.pt')
+ cmd = 'python test2.py'
+ cmd += ' --weights ' + weights
+ cmd += ' --data ' + data
+ cmd += ' --batch-size ' + str(batchsize)
+ cmd += ' --conf-thres ' + str(confthres)
+ cmd += ' --iou-thres ' + str(iouthres)
+ cmd += ' --task ' + task
+ cmd += ' --name ' + test_name
+ cmd += ' --save-json'
+ print(cmd)
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, bufsize=1)
+ for line in p.stdout:
+ print(line)
+
+ def detect(self,
+ train_name, # e.g., yolov7_localizer_01_1
+ detect_name, # e.g., yolov7_localizer_01_1
+ source, # e.g., path/filename.jpg
+ imgsize=640,
+ confthres=0.25
+ ):
+ """
+ Run YOLOv7 model on specified file(s)
+ """
+ weights = os.path.join('runs/train/', train_name, 'weights/best.pt')
+ cmd = 'python detect.py'
+ cmd += ' --weights ' + weights
+ cmd += ' --source "' + source + '"'
+ cmd += ' --img-size ' + str(imgsize)
+ cmd += ' --conf-thres ' + str(confthres)
+ cmd += ' --save-txt'
+ cmd += ' --save-conf'
+ cmd += ' --agnostic-nms'
+ cmd += ' --name ' + detect_name
+ print(cmd)
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True, bufsize=1)
+ for line in p.stdout:
+ print(line)
+
+ def sort(self,
+ sourcedir,
+ planedir,
+ noplanedir,
+ logdir,
+ weights='../code/localizer.pt',
+ imgsize=640,
+ confthres=0.25,
+ device='cpu',
+ savejson=False,
+ ):
+ """
+ Runs daemon to sort folder contents into plane and noplane folders
+ Enter Ctrl+c to end (otherwise, continues indefinitely)
+ """
+ cmd = 'python sort.py'
+ cmd += ' --weights ' + weights
+ cmd += ' --source-dir ' + sourcedir
+ cmd += ' --plane-dir ' + planedir
+ cmd += ' --noplane-dir ' + noplanedir
+ cmd += ' --log-dir ' + logdir
+ cmd += ' --img-size ' + str(imgsize)
+ cmd += ' --conf-thres ' + str(confthres)
+ cmd += ' --device ' + device
+ cmd += ' --nosave'
+ cmd += ' --agnostic-nms'
+ if savejson:
+ cmd += ' --save-json'
+ print(cmd)
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
+ for line in p.stdout:
+ print(line)
+
+ def sort_tflite(self,
+ sourcedir,
+ planedir,
+ noplanedir,
+ logdir,
+ weights='../data/tflite/localizer.tflite',
+ savejson=False,
+ ):
+ """
+ Runs daemon to sort folder contents into plane and noplane folders
+ using the Tensorflow Lite version of the model.
+ Enter Ctrl+c to end (otherwise, continues indefinitely).
+ """
+ cmd = 'python sort_tflite.py'
+ cmd += ' --weights ' + weights
+ cmd += ' --source-dir ' + sourcedir
+ cmd += ' --plane-dir ' + planedir
+ cmd += ' --noplane-dir ' + noplanedir
+ cmd += ' --log-dir ' + logdir
+ if savejson:
+ cmd += ' --save-json'
+ print(cmd)
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
+ for line in p.stdout:
+ print(line)
+
+ def visualize_train(self, name, batch):
+ """
+ Show some graphics generated during model training
+ Currently, just prints paths to graphics
+ """
+ results_dir = os.path.join('runs/train/', name)
+ file_names = ['results.png',
+ 'test_batch' + str(batch) + '_labels.jpg',
+ 'test_batch' + str(batch) + '_pred.jpg']
+ for file_name in file_names:
+ file_path = os.path.join(results_dir, file_name)
+ print(file_path)
+
+ def visualize_detect(self, name, image_path):
+ """
+ Visualize and print detector inference example
+ Currently, just prints path to graphic
+ """
+ results_dir = os.path.join('runs/detect', name)
+ idstring = os.path.splitext(os.path.split(image_path)[1])[0]
+ image_path = os.path.join(results_dir, idstring + '.jpg')
+ text_path = os.path.join(results_dir, 'labels', idstring + '.txt')
+
+ file = open(text_path)
+ print(file.readlines())
+ file.close()
+ print(image_path)
+
+ def check_planes_per_image(self, flag_col='auto_bbox_flag',
+ count_col='auto_bbox_count'):
+ """
+ Count images with various numbers of planes found
+ """
+ print('0 Planes:',
+ (self.df[flag_col] & (self.df[count_col] == 0)).sum())
+ print('1 Plane :',
+ (self.df[flag_col] & (self.df[count_col] == 1)).sum())
+ print('2+ Planes:',
+ (self.df[flag_col] & (self.df[count_col] >= 2)).sum())
+
+ def check_top_global_mm_strings(self, database_path, count=25):
+ """
+ Print the most common unsanitized make+model strings in the
+ aircraft database file
+ """
+ database_dataframe = pd.read_csv(database_path, usecols=['icao24', 'manufacturername', 'model'])
+ database_dataframe['string'] = database_dataframe['manufacturername'] + ' ' + database_dataframe['model']
+ database_dataframe = database_dataframe[['icao24', 'string']]
+ database_dataframe = database_dataframe.groupby('string', as_index=False).count().sort_values(by='icao24', ascending=False)
+ database_dataframe = database_dataframe.reset_index(drop=True)
+ with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.width', display_width):
+ print(database_dataframe.iloc[:count])
+
+ def check_confusion_matrix(self, cmpath, save=False, names=()):
+ """
+ Show a confusion matrix previously saved to disk
+ """
+ cm = ConfusionMatrix(0)
+ cm.read(cmpath)
+ print(cm.matrix)
+ print()
+ with np.printoptions(precision=3):
+ print(cm.norm(0).matrix)
+ print()
+ print(cm.norm(1).matrix)
+ if save:
+ savedir = os.path.split(cmpath)[0]
+ cm.plot(savedir, names, normalize=False, transpose=False,
+ round_to_int=True,
+ filename='confusion_matrix_counts')
+ cm.norm(0).plot(savedir, names, normalize=False, transpose=False,
+ filename='confusion_matrix_bytrue')
+ cm.norm(1).plot(savedir, names, normalize=False, transpose=False,
+ filename='confusion_matrix_bypred')
+
+ def torch2onnx(self,
+ weights, # e.g., 'runs/train/xyz/yolov7_localizer_01_01/weights/best.pt'
+ imgsize=640,
+ confthres=0.25,
+ iouthres=0.5,
+ nms=True
+ ):
+ """
+ Convert PyTorch YOLOv7 model to ONNX
+ """
+ cmd = 'python export.py'
+ cmd += ' --weights ' + weights
+ cmd += ' --grid'
+ if nms:
+ cmd += ' --end2end'
+ cmd += ' --simplify'
+ cmd += ' --topk-all 100'
+ cmd += ' --iou-thres ' + str(iouthres)
+ cmd += ' --conf-thres ' + str(confthres)
+ cmd += ' --img-size ' + str(imgsize) + ' ' + str(imgsize)
+ cmd += ' --max-wh ' + str(imgsize)
+ print(cmd)
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
+ for line in p.stdout:
+ print(line)
+
+ def onnx2tf(self,
+ input_model, # e.g., 'runs/train/xyz/yolov7_localizer_01_01/weights/best.onnx'
+ output_model # e.g., '../data/tf/localizer'
+ ):
+ """
+ Convert ONNX YOLOv7 model to TensorFlow
+ """
+ cmd = 'onnx-tf convert'
+ cmd += ' -i ' + input_model
+ cmd += ' -o ' + output_model
+ print(cmd)
+ p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
+ for line in p.stdout:
+ print(line)
+
+ def tf2tflite(self,
+ input_model, # e.g., '../data/tf/localizer'
+ output_model # e.g., '../data/tflite/localizer.tflite'
+ ):
+ """
+ Convert TensorFlow YOLOv7 model to TensorFlow Lite
+ """
+ import tensorflow as tf
+ tflite_model = tf.lite.TFLiteConverter\
+ .from_saved_model(input_model)\
+ .convert()
+ with open(output_model, 'wb') as output_file:
+ output_file.write(tflite_model)
+
+
+def main_sequence():
+ """
+ Original model-building sequence, using six classes
+ and previously-acquired close-view data
+ Note: Methods train, test, detect, sort, and write_* write output to disk
+ """
+ # Images
+ ds = Dataset()
+ ds.create_from_images('../data/2021/skyscan-datasets/dataset/close-view')
+ ds.load_makemodel_string('../data/databases/aircraftDatabase.csv')
+ ch = ds.load_makemodel_mm('../code/taxon.json', True)
+ ds.print_classes()
+ detector_classes = ['Airbus A319', 'Airbus A320', 'Airbus A321',
+ 'Boeing 737', 'Bombardier CL-600', 'Embraer ERJ-170']
+ ds.add_usable(detector_classes, ch['helicopters'])
+ ds.print(title='Initial Dataset')
+
+ # Bounding boxes and data split
+ ds.write_suggested_labels('../data/label_suggestions/label_suggestions.csv')
+ ds.load_labelbox_labels('../data/labelbox_export/export-2022-10-09T04_14_18.580Z.json')
+ ds.print(title='Manual Labels', condition=ds.df['manual_bbox_flag'])
+ ds.split_train_test(detector_classes)
+ categories = ['localizer_train', 'localizer_test', 'detector_train', 'detector_test']
+ ds.check_categories_nonintersecting(categories)
+ ds.print(title='Split')
+
+ # Write dataset to disk
+ # ds.write_folders('../data/dataset', categories)
+ # ds.write_images('../data/dataset', categories)
+ # ds.write_labels('../data/dataset', categories[:2], 'manual_bbox_desc')
+
+ # Localizer
+ loc_name_trn = 'yolov7_localizer_01_2' # Change before retraining
+ loc_name_tst = 'yolov7_localizer_01_23' # Change before retesting
+ loc_data = '/code/localizer.yaml'
+ # ds.train(data=loc_data, epochs=500, name=loc_name_trn)
+ ds.visualize_train(loc_name_trn, 0)
+ # ds.test(train_name=loc_name_trn, test_name=loc_name_tst, data=loc_data)
+ ds.load_yolov7_labels(name=loc_name_tst)
+ ds.check_planes_per_image()
+ ds.write_image_lists('../data/dataset', categories[2:],
+ ds.df['auto_bbox_count']==1)
+ ds.add_class_labels(detector_classes, ds.df['auto_bbox_count']==1,
+ 'auto_bbox_desc', 'auto_bboxclass_desc')
+ # ds.write_labels('../data/dataset', categories[2:], 'auto_bboxclass_desc',
+ # ds.df['auto_bbox_count']==1)
+ ds.print(title='Auto Labels', condition=ds.df['auto_bbox_flag'])
+
+ # Detector
+ det_name_trn = 'yolov7_detector_01_4' # Change before retraining
+ det_name_det = 'yolov7_detector_01_2' # Change before re-detecting
+ det_data = '/code/detector.yaml'
+ # ds.train(data=det_data, epochs=50, name=det_name_trn)
+ ds.visualize_train(det_name_trn, 0)
+ example_file = '../data/skyscan-datasets/dataset/close-view/4-23-DCA/Airbus A319 112/AA0AAB_2021-04-23-14-43-05.jpg'
+ # ds.detect(train_name=det_name_trn, detect_name=det_name_det,
+ # source=example_file)
+ ds.visualize_detect(det_name_det, example_file)
+
+ # Other
+ # Sorting daemon
+ # ds.sort('../data/edge_test/tosort', '../data/edge_test/plane',
+ # '../data/edge_test/noplane', '../data/edge_test/log')
+ # Common make+models
+ # ds.check_top_global_mm_strings('../data/databases/aircraftDatabase.csv', 100)
+ # Confusion matrix
+ det_name_tst = 'yolov7_detector_01_3' # Change before retesting
+ # ds.test(train_name=det_name_trn, test_name=det_name_tst, data=det_data,
+ # task='val')
+ cmpath = os.path.join('runs/test', det_name_tst, 'confusion_matrix_counts.txt')
+ ds.check_confusion_matrix(cmpath, save=True, names=detector_classes)
+
+
+def daemon():
+ """
+ Runs sorting daemon
+ """
+ ds = Dataset()
+ ds.sort('../data/edge_test/tosort',
+ '../data/edge_test/plane',
+ '../data/edge_test/noplane',
+ '../data/edge_test/log',
+ savejson=True)
+
+
+def compare_collections():
+ """
+ Look at contents of collections of images
+ Note: The first set of paths includes many images multiple times
+ """
+ paths = ['../data/2021/skyscan-datasets/dataset/cruising-view',
+ '../data/2021/skyscan-datasets/dataset/close-view',
+ '../data/2022/airshow',
+ '../data/2022/multi/2022-11-16_field-test',
+ '../data/2022/multi/2022-11-18--21_weekend-home',
+ '../data/2021/tf/dataset-export/multi_class_train_by_aircraft',
+ '../data/2021/tf/dataset-export/multi_class_eval_by_aircraft',
+ '../data/2021/322images-training',
+ '../data/2021/500image-train']
+ paths = ['../data/2022/airshow',
+ '../data/2021/skyscan-datasets/dataset/close-view',
+ '../data/2022/multi/2022-11-16_field-test']
+ # paths = ['../data/classification/test/%02d' % (classnum,)
+ # for classnum in range(23)]
+ for path in paths:
+ ds = Dataset()
+ ds.create_from_images(path)
+ ds.load_makemodel_string('../data/databases/aircraftDatabase.csv')
+ ds.load_makemodel_mm('../code/taxon.json')
+ ds.print_classes(title=path)
+ if True:
+ ds = Dataset()
+ ds.create_from_dirs(paths)
+ ds.load_makemodel_string('../data/databases/aircraftDatabase.csv')
+ ds.load_makemodel_mm('../code/taxon.json')
+ ds.print_classes(title='all')
+ print('Identified make+models:', len(ds.df['mm'].unique()))
+
+
+def entry_search():
+ """
+ Print dataset items matching command line argument
+ """
+ paths = ['../data/2022/airshow',
+ '../data/2021/skyscan-datasets/dataset/close-view',
+ '../data/2022/multi/2022-11-16_field-test']
+ col = 'make'
+ ds = Dataset()
+ ds.create_from_dirs(paths)
+ ds.load_makemodel_string('../data/databases/aircraftDatabase.csv')
+ ds.load_makemodel_mm('../code/taxon.json')
+ print(ds.df[ds.df[col]==sys.argv[1]])
+ print(ds.df[ds.df[col]==sys.argv[1]]['icao24'].unique())
+
+
+def revised_sequence():
+ """
+ Revised sequence of steps to build two models: a "localizer" to identify
+ bounding boxes, and a "detector" to identify bounding boxes as well as
+ class of each aircraft
+ """
+
+ # FILE NAMES
+ # Commercial air transport images for training/validation
+ paths_cat = ['../data/2021/skyscan-datasets/dataset/close-view',
+ '../data/2022/multi/2022-11-16_field-test']
+ # General aviation images for training/validation
+ paths_ga = ['../data/2022/airshow']
+ # Images for testing
+ paths_test = ['../data/2023/dulles/raw']
+ # Path to database of aircraft from OpenSky Network
+ database_path = '../data/databases/aircraftDatabase.csv'
+ # Lookup table with hierarchy of aircraft makes, models, and descriptions
+ hierarchy_path = '../code/taxon.json'
+ # Make/models to train detector on
+ detector_classes = ['Airbus A319', 'Airbus A320', 'Airbus A321',
+ 'Beechcraft Bonanza',
+ 'Boeing 737',
+ 'Bombardier CL-600',
+ 'Cessna Citation', 'Cessna Skyhawk', 'Cessna Skylane',
+ 'Embraer ERJ-170',
+ 'Piper PA-28 Cherokee', 'Piper PA-32 Cherokee Six']
+ # Files in which to save lists of suggested images to manually label
+ ls_cat = '../data/label_suggestions/label_suggestions_revised_cat.csv'
+ ls_ga = '../data/label_suggestions/label_suggestions_revised_ga.csv'
+ ls = '../data/label_suggestions/label_suggestions_revised.csv'
+ ls_test = '../data/label_suggestions/label_suggestions_revised_test.csv'
+ # Folder in which to store dataset
+ dataset_dir = '../data/dataset'
+ # Files of hand-drawn bounding boxes, exported from LabelBox
+ labelbox_export_trainval = '../data/labelbox_export/export-2022-12-07T07_41_59.984Z.json'
+ labelbox_export_test = '../data/labelbox_export/export-2023-03-02T07_38_51.807Z.json'
+
+ # YOLOv7 OUTPUT NAMES
+ # Folder name for training the localizer
+ loc_name_trn = 'yolov7_localizer_02_01' # Change before retraining
+ # Folder name for testing the localizer
+ loc_name_tst = 'yolov7_localizer_02_04' # Change before retesting
+ # Folder name for training the detector
+ det_name_trn = 'yolov7_detector_02_01' # Change before retraining
+ # Folder name for testing the detector
+ det_name_tst = 'yolov7_detector_02_01' # Change before re-detecting
+ # Folder name for testing on new data
+ test_name = 'yolov7_test_02_14' # Change before retesting
+
+ # Open CAT and GA collects separately, to label different fractions thereof
+ ds_cat = Dataset()
+ ds_ga = Dataset()
+ ds_cat.create_from_dirs(paths_cat)
+ ds_ga.create_from_dirs(paths_ga)
+ ds_cat.load_makemodel_string(database_path)
+ ds_ga.load_makemodel_string(database_path)
+ ds_cat.load_makemodel_mm(hierarchy_path)
+ ch = ds_ga.load_makemodel_mm(hierarchy_path)
+ ds_cat.print_classes(title='Field tests')
+ ds_ga.print_classes(title='Airshow')
+ ds_cat.add_usable(detector_classes, ch['helicopters']) # + ch['homebuilt'])
+ ds_ga.add_usable(detector_classes, ch['helicopters']) # + ch['homebuilt'])
+ ds_cat.write_suggested_labels(
+ ls_cat, max_planes_per_class=12, max_images_per_plane=2)
+ ds_ga.write_suggested_labels(
+ ls_ga, max_planes_per_class=12, max_images_per_plane=1)
+ subprocess.check_output("cat %s %s > %s" % (ls_cat, ls_ga, ls), shell=True)
+ ds = ds_cat.concat(ds_ga)
+ ds.print_classes(title='Combined dataset')
+
+ # Check that suggested files to hand-label are as expected
+ ds_suggest = Dataset()
+ ds_suggest.create_from_paths_file(ls)
+ ds_suggest.load_makemodel_string(database_path)
+ ds_suggest.load_makemodel_mm(hierarchy_path)
+ ds_suggest.print_classes(title='Hand-labeled')
+
+ # Data split
+ if not os.path.exists(labelbox_export_trainval):
+ raise Exception('Use LabelBox to label bounding boxes for the files in ' + ls + ' and save the exported LabelBox file to ' + labelbox_export_trainval + ' (or specify the correct path in the code), then re-run this function.')
+ ds.load_labelbox_labels(labelbox_export_trainval, min_area=400)
+ ds.print(title='Manual Labels', condition=ds.df['manual_bbox_flag'])
+ ds.split_train_val_shared(detector_classes, val_fraction=0.25)
+ ds.print_classes(title='Detector Train', condition=ds.df['detector_train'])
+ ds.print_classes(title='Detector Val', condition=ds.df['detector_val'])
+
+ # Write dataset to disk
+ categories = ['localizer_train', 'localizer_val',
+ 'detector_train', 'detector_val']
+ ds.write_folders(dataset_dir, categories)
+ ds.write_images(dataset_dir, categories) ##
+ ds.write_labels(dataset_dir, categories[:2], 'manual_bbox_desc')
+
+ # Localizer
+ loc_data = 'localizer_rev2.yaml'
+ ds.train(data=loc_data, epochs=500, name=loc_name_trn) ##
+ ds.visualize_train(loc_name_trn, 0)
+
+ # Test on localizer_val data, to generate localizer confusion matrix
+ ds.test(train_name=loc_name_trn, test_name=loc_name_tst,
+ data=loc_data, task='val') ##
+ ds.load_yolov7_labels(name=loc_name_tst, min_area=400,
+ always_flag=['localizer_val']) # Used?
+ ds.check_planes_per_image()
+ cmpath = os.path.join('runs/test', loc_name_tst,
+ 'confusion_matrix_counts.txt')
+ ds.check_confusion_matrix(cmpath, save=True, names=['aircraft'])
+
+ # Test on detector data, to generate detector training data labels
+ ds.df.drop(['auto_bbox_flag', 'auto_bbox_count', 'auto_bbox_desc'],
+ axis='columns')
+ ds.test(train_name=loc_name_trn, test_name=loc_name_tst+'b',
+ data=loc_data) ##
+ ds.load_yolov7_labels(name=loc_name_tst+'b', min_area=400,
+ always_flag=['detector_train', 'detector_val'])
+ ds.check_planes_per_image()
+ ds.write_image_lists(dataset_dir, categories[2:],
+ ds.df['auto_bbox_count']==1)
+ ds.add_class_labels(detector_classes, ds.df['auto_bbox_count']==1,
+ 'auto_bbox_desc', 'auto_bboxclass_desc')
+ ds.write_labels(dataset_dir, categories[2:], 'auto_bboxclass_desc',
+ ds.df['auto_bbox_count']==1) ##
+
+ # Detector
+ det_data = 'detector_rev2.yaml'
+ ds.train(data=det_data, epochs=50, name=det_name_trn) ##
+ ds.visualize_train(det_name_trn, 0)
+
+ # Test on detector_val data, to generate detector confusion matrix
+ ds.test(train_name=det_name_trn, test_name=det_name_tst,
+ data=det_data, task='val') ##
+ ds.load_yolov7_labels(name=det_name_tst, min_area=400,
+ always_flag=['detector_val'])
+ ds.check_planes_per_image()
+ cmpath = os.path.join('runs/test', det_name_tst,
+ 'confusion_matrix_counts.txt')
+ ds.check_confusion_matrix(cmpath, save=True, names=detector_classes)
+
+ # Testing the model on a new dataset
+ ds = Dataset()
+ ds.create_from_dirs(paths_test)
+ ds.load_makemodel_string(database_path)
+ ch = ds.load_makemodel_mm(hierarchy_path)
+ ds.add_usable(detector_classes, ch['helicopters'])
+ ds.print_classes(title='Testing Data')
+ # Manual bounding boxes
+ ds.write_suggested_labels_prop(ls_test, num_planes=100,
+ max_images_per_plane=1)
+ if not os.path.exists(labelbox_export_trainval):
+ raise Exception('Use LabelBox to label bounding boxes for the files in ' + ls_test + ' and save the exported LabelBox file to ' + labelbox_export_test + ' (or specify the correct path in the code), then re-run this function.')
+ ds.load_labelbox_labels(labelbox_export_test, min_area=400)
+ # Write imagery and manual bounding boxes (for test of localizer)
+ categories = ['localizer_test', 'detector_test']
+ ds.df['localizer_test'] = ds.df['manual_bbox_flag']
+ ds.df['detector_test'] = ds.df['detector_usable']
+ ds.write_folders(dataset_dir, categories)
+ ds.write_images(dataset_dir, categories) ##
+ ds.write_labels(dataset_dir, categories[:1], 'manual_bbox_desc')
+ # Generate and write auto bounding boxes (for test of detector)
+ ds.test(train_name=loc_name_trn, test_name=test_name+'a',
+ data=loc_data, task='test3') ##
+ ds.load_yolov7_labels(name=test_name+'a', min_area=400,
+ always_flag=['detector_test'])
+ ds.check_planes_per_image()
+ ds.write_image_lists(dataset_dir, categories[1:],
+ ds.df['auto_bbox_count']==1)
+ ds.add_class_labels(detector_classes, ds.df['auto_bbox_count']==1,
+ 'auto_bbox_desc', 'auto_bboxclass_desc')
+ ds.write_labels(dataset_dir, categories[1:], 'auto_bboxclass_desc',
+ ds.df['auto_bbox_count']==1) ##
+ ds.print(title='Test data', condition=ds.df['manual_bbox_count']>=1)
+ # Test of localizer with testing data
+ ds.test(train_name=loc_name_trn, test_name=test_name+'b',
+ data=loc_data, task='test2') ##
+ cmpath = os.path.join('runs/test', test_name+'b',
+ 'confusion_matrix_counts.txt')
+ ds.check_confusion_matrix(cmpath, save=True, names=['aircraft'])
+ # Test of detector with testing data
+ ds.test(train_name=det_name_trn, test_name=test_name+'c',
+ data=det_data, task='test') ##
+ cmpath = os.path.join('runs/test', test_name+'c',
+ 'confusion_matrix_counts.txt')
+ ds.check_confusion_matrix(cmpath, save=True, names=detector_classes)
+
+
+def tflite_conversion():
+ """
+ Convert the model:
+ PyTorch --> ONNX --> TensorFlow --> TensorFlow Lite
+ """
+ train_name = 'yolov7_localizer_02_01'
+ model_name = 'localizer'
+ ds = Dataset()
+
+ torch_model = os.path.join('runs/train/', train_name, 'weights/best.pt')
+ onnx_model = os.path.join('runs/train/', train_name, 'weights/best.onnx')
+ tf_model = os.path.join('../data/model/tf', model_name)
+ tflite_model = os.path.join('../data/model/tflite', model_name + '.tflite')
+
+ ds.torch2onnx(torch_model)
+ ds.onnx2tf(onnx_model, tf_model)
+ ds.tf2tflite(tf_model, tflite_model)
+
+
+def daemon_tflite():
+ """
+ Runs TFLite version of the sorting daemon
+ """
+ ds = Dataset()
+ ds.sort_tflite('../data/edge_test/tosort',
+ '../data/edge_test/plane',
+ '../data/edge_test/noplane',
+ '../data/edge_test/log',
+ savejson=True)
+
+
+if __name__ == '__main__':
+ #main_sequence()
+ #daemon()
+ #compare_collections()
+ #entry_search()
+ revised_sequence()
+ #tflite_conversion()
+ #daemon_tflite()
diff --git a/ai/taxon.json b/ai/taxon.json
new file mode 100644
index 0000000..46127e8
--- /dev/null
+++ b/ai/taxon.json
@@ -0,0 +1,902 @@
+{
+ "version": 0.4,
+ "helicopters": ["Bell", "Eurocopter", "MBB", "Robinson", "Sikorsky"],
+ "homebuilt": ["Van's", "(Kit)"],
+ "make_model_strings": {
+ "Aeronca": {
+ "Champion": [
+ "Aeronca 7AC",
+ "Bellanca 7ACA"
+ ]
+ },
+ "Aero Commander": {
+ "500 Family": [
+ "Aero Commander 560-A"
+ ]
+ },
+ "Aero Vodochody": {
+ "L-39 Albatros": [
+ "Aero L-39 ALBATROS",
+ "Aero Vodochody L-39",
+ "Aero Vodochody L-39 C",
+ "Aero Vodochody L39C"
+ ]
+ },
+ "Airbus": {
+ "A300": [
+ "Airbus A300B4-662R",
+ "Airbus A300 B4-622R"
+ ],
+ "A319": [
+ "Airbus A319 112",
+ "Airbus A319-112",
+ "Airbus A319-114",
+ "Airbus A319-115",
+ "Airbus A319 115SL",
+ "Airbus A319-132",
+ "Airbus Industrie A319-112",
+ "Airbus Industrie A319-114",
+ "Airbus Industrie A319-131",
+ "Airbus Industrie A319-132"
+ ],
+ "A320": [
+ "Airbus A320 211",
+ "Airbus A320-211",
+ "Airbus A320 214",
+ "Airbus A320-214",
+ "Airbus A320 214SL",
+ "Airbus A320 232",
+ "Airbus A320-232",
+ "Airbus A320 232SL",
+ "Airbus A320-232SL",
+ "Airbus A320-251N",
+ "Airbus Industrie A320-211",
+ "Airbus Industrie A320-212",
+ "Airbus Industrie A320-214",
+ "Airbus Industrie A320-232",
+ "Airbus Sas A320-251N"
+ ],
+ "A321": [
+ "Airbus A321-211",
+ "Airbus A321 211SL",
+ "Airbus A321-231",
+ "Airbus A321 231SL",
+ "Airbus A321-253N",
+ "Airbus A321-253NX",
+ "Airbus A321-271NX",
+ "Airbus Industrie A321-211",
+ "Airbus Industrie A321-211 (Airbus)",
+ "Airbus Industrie A321-231",
+ "Airbus Sas A321-211",
+ "Airbus Sas A321-213"
+ ],
+ "A330": [
+ "Airbus A330 203"
+ ],
+ "A340": [
+ "Airbus A340-313"
+ ],
+ "A350": [
+ "Airbus Industrie A350-1041",
+ "Airbus Airbus A350 941"
+ ]
+ },
+ "Beechcraft": {
+ "Model 18": [
+ "Beech Beech 18 C / S"
+ ],
+ "Musketeer": [
+ "Beech B24R"
+ ],
+ "Bonanza": [
+ "Beech F33A",
+ "Beech 35-C33",
+ "Beech D35",
+ "Beech F35",
+ "Beech J35",
+ "Beech K35",
+ "Beech P35",
+ "Beech S35",
+ "Beech V35",
+ "Beech V35B",
+ "Beech A36",
+ "Raytheon Aircraft Company G36"
+ ],
+ "Model 45": [
+ "Beech A45"
+ ],
+ "Baron": [
+ "Beech E-55",
+ "Beech 58",
+ "Beech 95-55",
+ "Beech 95-B55 (T42A)",
+ "Hawker Beechcraft Corp G58",
+ "Raytheon Aircraft Company 58",
+ "Textron Aviation Inc G58"
+ ],
+ "Model 99": [
+ "Beech 99"
+ ],
+ "Super King Air": [
+ "Beech 200",
+ "Beech B200",
+ "Beech B300",
+ "Hawker Beechcraft Corp B300C",
+ "Beech C-12C Huron",
+ "Raytheon Aircraft Company MC-12S Liberty",
+ "Textron Aviation Inc B300"
+ ],
+ "Premier I": [
+ "Raytheon Aircraft Company 390"
+ ]
+ },
+ "Bell": {
+ "206": [
+ "Bell 206B",
+ "Bell 206L-3"
+ ],
+ "407": [
+ "Bell 407"
+ ],
+ "429": [
+ "Bell Helicopter Textron Canada 429"
+ ]
+ },
+ "Bellanca": {
+ "Decathlon": [
+ "Bellanca 8KCAB",
+ "American Champion Aircraft Corp. 8KCAB (Bellanca)"
+ ]
+ },
+ "Boeing": {
+ "717": [
+ "Boeing 717 2BD",
+ "Boeing 717-200",
+ "Boeing Boeing 717-2BD"
+ ],
+ "737": [
+ "Boeing 737-4B7",
+ "Boeing 737-71Q",
+ "Boeing 737-724",
+ "Boeing 737-73V",
+ "Boeing 737-752",
+ "Boeing 737-76Q",
+ "Boeing 737-7AD",
+ "Boeing 737-7BD",
+ "Boeing 737-7CT",
+ "Boeing 737-7H4",
+ "Boeing 737-7L9",
+ "Boeing 737-7Q8",
+ "Boeing 737-8",
+ "Boeing 737-800",
+ "Boeing 737-823",
+ "Boeing 737-824",
+ "Boeing 737-832",
+ "Boeing 737-852",
+ "Boeing 737-8H4",
+ "Boeing 737-8KN",
+ "Boeing 737-8Q8",
+ "Boeing 737-890",
+ "Boeing 737-9",
+ "Boeing 737-900ER",
+ "Boeing 737-924ER",
+ "Boeing 737-932ER",
+ "Boeing 737NG 7H4",
+ "Boeing 737NG 7H4/W",
+ "Boeing 737NG 800/W",
+ "Boeing 737NG 823",
+ "Boeing 737NG 823/W",
+ "Boeing 737NG 824/W",
+ "Boeing 737NG 852",
+ "Boeing 737NG 86N",
+ "Boeing 737NG 86N/W",
+ "Boeing 737NG 8Q8/W",
+ "Boeing 737NG 900ER/W",
+ "Boeing 737NG 924ER",
+ "Boeing 737NG 924ER/W",
+ "Boeing Boeing 737-823",
+ "The Boeing Company 737-8Q8 (Boeing)",
+ "Boeing Boeing C-40A",
+ "Boeing C-40C"
+ ],
+ "747": [
+ "Boeing 747 4R7F",
+ "Boeing 747 830"
+ ],
+ "757": [
+ "Boeing 757-224",
+ "Boeing 757-232",
+ "Boeing 757-251",
+ "Boeing 757 2Q8/W"
+ ],
+ "767": [
+ "Boeing 767-223",
+ "Boeing 767-281",
+ "Boeing 767-322",
+ "Boeing 767-424ER",
+ "The Boeing Company Commercial Airplane Division 767-33A (Boeing)"
+ ],
+ "777": [
+ "Boeing 777-222",
+ "Boeing 777-223",
+ "Boeing 777-300ER",
+ "Boeing 777 368ER",
+ "Boeing 777 3F2ER",
+ "Boeing 777 FDZ",
+ "Boeing Boeing 777-328(ER)"
+ ],
+ "787": [
+ "Boeing 787 8",
+ "Boeing 787-8",
+ "Boeing 787 9",
+ "Boeing Company BOEING 787-9 Dreamliner"
+ ],
+ "B-17 Flying Fortress": [
+ "Boeing B-17G"
+ ],
+ "C-17 Globemaster III": [
+ "Boeing C-17A Globemaster III"
+ ]
+ },
+ "Bombardier": {
+ "BD-100": [
+ "Bombardier Challenger 300",
+ "Bombardier Inc BD-100-1A10"
+ ],
+ "BD-500": [
+ "Airbus Canada Lp BD-500-1A11",
+ "Airbus Canada Ltd Ptnrsp BD-500-1A10",
+ "C Series Aircraft Ltd Ptnrsp BD-500-1A10"
+ ],
+ "BD-700": [
+ "Bombardier Inc BD-700-1A10",
+ "Bombardier Inc BD-700-1A11",
+ "Bombardier Inc BD-700-2A12"
+ ],
+ "CL-600": [
+ "Bombardier Inc CL-600-2B16",
+ "Bombardier Inc CL-600-2B19",
+ "Bombardier Inc CL-600-2C10",
+ "Bombardier Inc CL-600-2C11",
+ "Bombardier Inc CL-600-2D24",
+ "Bombardier Inc. CL-600-2B19 (Series 100) (Bombardier)",
+ "Canadair Ltd CL-600-2B16",
+ "Bombardier CRJ 900 LR NG",
+ "Bombardier Global 6000"
+ ]
+ },
+ "British Aerospace": {
+ "Jetstream 3101": [
+ "British Aerospace BAE JETSTREAM 3101"
+ ]
+ },
+ "Cessna": {
+ "120/140": [
+ "Cessna 120",
+ "Cessna 140"
+ ],
+ "150/152": [
+ "Cessna 150F",
+ "Cessna 150G",
+ "Cessna 150H",
+ "Cessna 150J",
+ "Cessna 150L",
+ "Cessna 150M",
+ "Cessna 152"
+ ],
+ "170": [
+ "Cessna 170B"
+ ],
+ "Skyhawk": [
+ "Cessna 172",
+ "Cessna 172D",
+ "Cessna 172E",
+ "Cessna 172F",
+ "Cessna 172G",
+ "Cessna 172H",
+ "Cessna 172K",
+ "Cessna 172L",
+ "Cessna 172M",
+ "Cessna 172N",
+ "Cessna 172P",
+ "Cessna 172R",
+ "Cessna 172S",
+ "Cessna R172K",
+ "Cessna Aircraft Company 172M (Cessna)"
+ ],
+ "Skylark": [
+ "Cessna 175"
+ ],
+ "Cardinal": [
+ "Cessna 177A",
+ "Cessna 177B",
+ "Cessna 177RG"
+ ],
+ "Skywagon": [
+ "Cessna 180",
+ "Cessna 180J",
+ "Cessna 180K",
+ "Cessna A185F"
+ ],
+ "Skylane": [
+ "Cessna 182A",
+ "Cessna 182C",
+ "Cessna 182F",
+ "Cessna 182J",
+ "Cessna 182N",
+ "Cessna 182P",
+ "Cessna 182Q",
+ "Cessna 182R",
+ "Cessna 182S",
+ "Cessna 182T",
+ "Cessna R182",
+ "Cessna T182T",
+ "Cessna TR182",
+ "Cessna Aircraft Company 182Q (Cessna)"
+ ],
+ "190/195": [
+ "Cessna 195",
+ "Cessna 195A"
+ ],
+ "206": [
+ "Cessna U206G",
+ "Cessna T206H"
+ ],
+ "Caravan": [
+ "Cessna 208",
+ "Cessna 208B"
+ ],
+ "Centurion": [
+ "Cessna 210J",
+ "Cessna T210L",
+ "Cessna 210M",
+ "Cessna T210N"
+ ],
+ "310/320": [
+ "Cessna 310E",
+ "Cessna 310K",
+ "Cessna 310R",
+ "Cessna 320F"
+ ],
+ "401/402/411/421": [
+ "Cessna 402B",
+ "Cessna 421C"
+ ],
+ "SkyCourier": [
+ "Textron Aviation Inc 408"
+ ],
+ "414": [
+ "Cessna 414"
+ ],
+ "Citation": [
+ "Cessna 501",
+ "Cessna 510",
+ "Cessna 525B",
+ "Cessna 550",
+ "Cessna 560",
+ "Cessna 560XL",
+ "Cessna 650",
+ "Cessna 680",
+ "Cessna 750",
+ "Cessna Aircraft Co 560XLS",
+ "Cessna Citation Excel",
+ "Cessna Citation S/II",
+ "Cessna Citation V",
+ "Cessna Citation VII",
+ "Cessna Citation XLS",
+ "Cessna UC-35C Citation",
+ "Textron Aviation Inc 525B",
+ "Textron Aviation Inc 525C",
+ "Textron Aviation Inc 680A"
+ ]
+ },
+ "Cirrus": {
+ "SR20/22": [
+ "Cirrus Design Corp SR20",
+ "Cirrus Design Corp SR22",
+ "Cirrus Design Corp SR22T"
+ ],
+ "SF50": [
+ "Cirrus Design Corp SF50"
+ ]
+ },
+ "Consolidated Vultee": {
+ "BT-13 Valiant": [
+ "Consolidated Vultee BT-13A"
+ ],
+ "PBY Catalina": [
+ "Consolidated Vultee 28-5ACF"
+ ]
+ },
+ "Curtiss": {
+ "P-40 Warhawk": [
+ "Curtiss Wright P-40N"
+ ]
+ },
+ "Dassault": {
+ "Falcon": [
+ "Dassault FALCON 900 EX",
+ "Dassault Aviation FALCON 7X",
+ "Dassault Aviation FALCON 900EX"
+ ]
+ },
+ "De Havilland": {
+ "Dash 8": [
+ "Bombardier Inc. DHC-8-402 (Dehavilland)"
+ ]
+ },
+ "Diamond": {
+ "DA40": [
+ "Diamond Aircraft Ind Inc DA 40"
+ ]
+ },
+ "Douglas": {
+ "DC-3": [
+ "Douglas DC3A",
+ "Douglas DC3C-R",
+ "Douglas DC3C-S1C3G",
+ "Douglas DC3C-S4C4G"
+ ]
+ },
+ "Embraer": {
+ "ERJ-135/140/145": [
+ "Embraer EMB-135LR",
+ "Embraer ERJ-145 LR",
+ "Embraer EMB-145LR",
+ "Embraer EMB-145XR"
+ ],
+ "ERJ-170": [
+ "Embraer ERJ 170-100 LR",
+ "Embraer ERJ 170-100 SE",
+ "Embraer ERJ 170-100SU",
+ "Embraer ERJ 170-200 LR",
+ "Embraer S A ERJ 170-200 LR",
+ "Embraer-empresa Brasileira De ERJ 170-200 LR",
+ "Yabora Industria Aeronautica S ERJ 170-200 LR"
+ ],
+ "ERJ-175": [
+ "Embraer EMB-175 200LR"
+ ],
+ "ERJ-190": [
+ "Embraer ERJ 190-100 IGW",
+ "Embraer EMB-190 AR"
+ ],
+ "EMB-500": [
+ "Embraer Sa EMB-500",
+ "Embraer-empresa Brasileira De EMB-500"
+ ],
+ "EMB-505": [
+ "Embraer Phenom 300",
+ "Embraer S A EMB-505",
+ "Embraer Executive Aircraft Inc EMB-505",
+ "Embraer-empresa Brasileira De EMB-505"
+ ],
+ "EMB-545/550": [
+ "Embraer S A EMB-545",
+ "Embraer Sa EMB-550"
+ ]
+ },
+ "Eurocopter": {
+ "BK-117": [
+ "Eurocopter Deutschland Gmbh MBB-BK 117 C-2"
+ ],
+ "EC-120": [
+ "Eurocopter EC120B"
+ ],
+ "EC-135": [
+ "Eurocopter Deutschland Gmbh EC 135 P2+"
+ ],
+ "AS-350": [
+ "Eurocopter AS 350 B3"
+ ]
+ },
+ "Evolution": {
+ "Revo": [
+ "Evolution Aircraft Inc REVO"
+ ]
+ },
+ "Extra Flugzeugbau": {
+ "EA-300": [
+ "Extra Flugzeugproduktions EA 300/SC"
+ ]
+ },
+ "Flight Design": {
+ "CT": [
+ "Flight Design Gmbh CTLS"
+ ]
+ },
+ "Game Composites": {
+ "GB1 GameBird": [
+ "Game Composites Llc GB1 GAMEBIRD"
+ ]
+ },
+ "Grumman": {
+ "F6F Hellcat": [
+ "Grumman A/c Eng Corp-nichols F6F-5"
+ ]
+ },
+ "Grumman American": {
+ "AA-5": [
+ "Grumman American Avn. Corp. AA-5B"
+ ]
+ },
+ "Gulfstream": {
+ "GIV": [
+ "Gulfstream Aerospace G-IV",
+ "Gulfstream Aerospace GIV-X (G450)"
+ ],
+ "GV": [
+ "Gulfstream Aerospace GV",
+ "Gulfstream Aerospace G-V",
+ "Gulfstream Aerospace GV-SP (G550)"
+ ],
+ "GVI": [
+ "Gulfstream Aerospace Corp GVI(G650ER)",
+ "Gulfstream Aerospace Corp GVI (G650ER)"
+ ]
+ },
+ "Hawker": {
+ "400": [
+ "Raytheon Aircraft Company 400A",
+ "Hawker Beechcraft Corp 400A"
+ ],
+ "800/900": [
+ "British Aerospace 125 700A",
+ "Raytheon Aircraft Company HAWKER 800XP",
+ "Raytheon Aircraft Company 125 800XP",
+ "Hawker Beechcraft Corp Hawker 900XP",
+ "Hawker Beechcraft Corp HAWKER 900XP"
+ ]
+ },
+ "Honda": {
+ "HondaJet": [
+ "Honda Aircraft Co Llc HA-420"
+ ]
+ },
+ "Howard": {
+ "DGA-15": [
+ "Howard Aircraft DGA-15P"
+ ]
+ },
+ "IAI": {
+ "G150": [
+ "Iai Ltd GULFSTREAM G150"
+ ],
+ "G200": [
+ "Israel Aerospace Industriesltd G200",
+ "Israel Aircraft Industries GULFSTREAM 200"
+ ],
+ "G280": [
+ "Iai Ltd GULFSTREAM G280"
+ ]
+ },
+ "Learjet": {
+ "31": [
+ "Learjet Inc 31",
+ "Learjet Inc 31A",
+ "Bombardier Learjet 31 A"
+ ],
+ "35/36": [
+ "Gates Lear Jet 36A",
+ "Gates Lear Jet 36 A"
+ ],
+ "45": [
+ "Learjet Inc 45",
+ "Bombardier Learjet 45"
+ ],
+ "60": [
+ "Learjet Inc 60"
+ ]
+ },
+ "Lockheed": {
+ "Hercules": [
+ "Lockheed L-100-30 Hercules"
+ ],
+ "Silver Star": [
+ "Canadair T33-AN MK3"
+ ]
+ },
+ "Luscombe": {
+ "8": [
+ "Luscombe 8A"
+ ]
+ },
+ "Maule": {
+ "M-7": [
+ "Maule M-7-235C"
+ ]
+ },
+ "MBB": {
+ "Bo 105": [
+ "Mbb BO105 CBS4"
+ ]
+ },
+ "Mooney": {
+ "M20": [
+ "Mooney M20C",
+ "Mooney M20E",
+ "Mooney M20F",
+ "Mooney M20J",
+ "Mooney M20M",
+ "Mooney M20R",
+ "Mooney Aircraft Corp. M20K",
+ "Mooney Airplane Co Inc M20TN"
+ ]
+ },
+ "North American": {
+ "B-25 Mitchell": [
+ "North American B-25H",
+ "North American B-25J"
+ ],
+ "P-51 Mustang": [
+ "North American F-51",
+ "North American F-51D",
+ "North American/aero Classics P-51D"
+ ],
+ "T-6 Texan": [
+ "North American AT-6D Texan",
+ "North American T-6G",
+ "North American T-6G Texan",
+ "North American HARVARD 2",
+ "North American SNJ-2",
+ "North American SNJ-6"
+ ],
+ "T-28 Trojan": [
+ "North American T-28B",
+ "North American NA-200"
+ ]
+ },
+ "Northrop": {
+ "F-5": [
+ "Northrop RF-5A Freedom Fighter"
+ ]
+ },
+ "Pilatus": {
+ "PC-12": [
+ "Pilatus PC-12",
+ "Pilatus PC-12/45",
+ "Pilatus PC-XII 45",
+ "Pilatus Aircraft Ltd PC-12",
+ "Pilatus Aircraft Ltd PC-12/47E"
+ ]
+ },
+ "Piper": {
+ "J-3 Cub": [
+ "Piper J3C-65"
+ ],
+ "PA-12 Super Cruiser": [
+ "Piper PA-12"
+ ],
+ "PA-18 Super Cub": [
+ "Piper PA-18-150"
+ ],
+ "PA-20/22": [
+ "Piper PA-22-108",
+ "Piper PA-22-150"
+ ],
+ "PA-23 Aztec": [
+ "Piper PA-23-250"
+ ],
+ "PA-24 Comanche": [
+ "Piper PA-24",
+ "Piper PA-24-250",
+ "Piper PA-24-260",
+ "Piper PA-24-400"
+ ],
+ "PA-28 Cherokee": [
+ "Piper PA-28-140",
+ "Piper PA-28-151",
+ "Piper PA-28-161",
+ "Piper PA-28-180",
+ "Piper PA-28R-180",
+ "Piper PA-28-181",
+ "Piper PA-28R-200",
+ "Piper PA-28RT-201T",
+ "Piper PA-28-235",
+ "Piper PA-28-236",
+ "Piper Aircraft Corporation PA-28-140 (Piper)"
+ ],
+ "PA-30 Twin Comanche": [
+ "Piper PA-30"
+ ],
+ "PA-31 Navajo": [
+ "Piper PA-31T1",
+ "Piper PA-31T1 Cheyenne I",
+ "Piper PA-31-350"
+ ],
+ "PA-32 Cherokee Six": [
+ "Piper PA-32-260",
+ "Piper PA-32-300",
+ "Piper PA-32R-300",
+ "Piper PA-32R-301",
+ "Piper PA-32R-301T",
+ "Piper PA-32RT-300"
+ ],
+ "PA-34 Seneca": [
+ "Piper PA-34-200T"
+ ],
+ "PA-46": [
+ "Piper PA46-500TP",
+ "Piper Aircraft Inc PA46-500TP",
+ "Piper Aircraft Inc PA-46-600TP"
+ ]
+ },
+ "Quest": {
+ "Kodiak": [
+ "Quest Aircraft Co Inc KODIAK 100"
+ ]
+ },
+ "Robinson": {
+ "R22": [
+ "Robinson R22 Beta II",
+ "Robinson Helicopter R22 BETA"
+ ],
+ "R44": [
+ "Robinson R44 Raven II",
+ "Robinson Helicopter Company R44 II"
+ ],
+ "R66": [
+ "Robinson Helicopter Co R66"
+ ]
+ },
+ "Rockwell": {
+ "OV-10 Bronco": [
+ "Rockwell OV-10D"
+ ]
+ },
+ "Scottish Aviation": {
+ "Bulldog": [
+ "Scottish Aviation SERIES 100 MDL 101"
+ ]
+ },
+ "Siai-Marchetti": {
+ "S.211": [
+ "Siai Marchetti S-211",
+ "Siai-marchetti Srl S 211"
+ ],
+ "SF.260": [
+ "Siai-marchetti SF260"
+ ]
+ },
+ "Sikorsky": {
+ "S-76": [
+ "Sikorsky S-76B"
+ ]
+ },
+ "Socata": {
+ "TB": [
+ "Socata TB 30 EPSILON",
+ "Compagnie Daher TB 30 EPSILON"
+ ],
+ "TBM": [
+ "Socata TBM 700",
+ "Compagnie Daher TBM 700",
+ "Eads Socata TBM 850"
+ ]
+ },
+ "Stearman": {
+ "Model 75": [
+ "Boeing A75N1(PT17)"
+ ]
+ },
+ "Taylorcraft": {
+ "B": [
+ "Taylorcraft BC12-D"
+ ]
+ },
+ "Van's": {
+ "RV-4": [
+ "Bendure Ryan RV-4",
+ "Giffin Walter C RV-4",
+ "Peterson Keith D HARMON ROCKET"
+ ],
+ "RV-6": [
+ "Barbee Michael C EXPERMINTAL RV-6",
+ "Cole Stanley T RV-6",
+ "Dane Julie RV6A",
+ "Kormylo Gene J VANS RV6A",
+ "Myers R.w. RV-6",
+ "Reisinger James A RV-6",
+ "Robb David M VANS RV6"
+ ],
+ "RV-7": [
+ "Gary A Barnes RV-7A",
+ "James K Hamilton RV-7"
+ ],
+ "RV-8": [
+ "Clark Michael J VANS RV-8",
+ "David M Schmitz/derek C James RV-8",
+ "Hammes Charles J VANS RV8",
+ "Keeling Larry RV8",
+ "Paine Lauran Jr RV-8",
+ "Quest One Llc RV-8",
+ "Richcreek Karl J VANS RV8",
+ "Rv83137 Llc RV-8"
+ ],
+ "RV-9": [
+ "Nyberg Randall L RV-9"
+ ],
+ "RV-10": [
+ "Hecker Robert J VANS RV-10",
+ "Paul Jeffrey Schwalen VANS RV10",
+ "Reeves Christopher D VANS RV10",
+ "Richard Dupee VANS RV-10",
+ "Sherman Michael VANS RV-10",
+ "Van Thomme Brendon S RV-10"
+ ],
+ "RV-12": [
+ "Steven G Isaacs RV-12",
+ "Vans Aircraft Inc RV-12"
+ ],
+ "RV-14": [
+ "Bernard L Hartnell RV 14A",
+ "Eisele Donald L VANS RV-14",
+ "Howard Dean RV-14",
+ "Meyers Robert A RV-14A",
+ "Michael J Konz RV-14A"
+ ]
+ },
+ "Vought": {
+ "F4U Corsair": [
+ "Chance Vought F4U-5",
+ "Goodyear FG1D"
+ ]
+ },
+ "(Kit)": {
+ "Aeroprakt A-22": [
+ "Aeroprakt Manufacturing Sp Zoo A22LS"
+ ],
+ "AMD Zodiac": [
+ "Jaime Rabins ZODIAC 650 B"
+ ],
+ "Barrows Bearhawk": [
+ "Yates Jared N AVIPRO BEARHAWK"
+ ],
+ "Bede BD-4": [
+ "Mahoney Steven BD-4"
+ ],
+ "Cub Crafters CC-11": [
+ "Cubcrafters Inc CC11-160"
+ ],
+ "Cub Crafters CC-19": [
+ "Cub Crafters Inc CC19-180"
+ ],
+ "Giles G-202": [
+ "Bergevin Steven GILES G-202"
+ ],
+ "Glasair III": [
+ "Broen Peter C GLASAIR III",
+ "Moman Guy E Jr GLASAIR III"
+ ],
+ "Glasair Sportsman 2+2": [
+ "Moring Ronald L SPORTSMAN 2+2"
+ ],
+ "Sling TSi": [
+ "Christopher P Bloch SLING 4 TSI"
+ ],
+ "Just Highlander": [
+ "Kyle Bessent JUST ACFT HIGHLANDER",
+ "Marlowe Thooft JUST ACFT HIGHLANDER"
+ ],
+ "Lancair Model 42": [
+ "Lancair Company LC42-550FG"
+ ],
+ "Lockwood Aircam": [
+ "Red Eagle Aviation Llc AIRCAM"
+ ],
+ "Quickie Q2": [
+ "Scheevel Jay QUICKIE TRI-Q2"
+ ],
+ "Rans S-9": [
+ "Sailer Denis R D&S-9A"
+ ],
+ "Robinson Special": [
+ "Lush Michael ROBINSON SPECIAL"
+ ],
+ "Velocity SE": [
+ "Hancock William J VELOCITY-STANDARD",
+ "Martino Thomas G VELCITY 173 ELITE RG"
+ ]
+ }
+ }
+}
diff --git a/ai/test2.py b/ai/test2.py
new file mode 100644
index 0000000..965cbe8
--- /dev/null
+++ b/ai/test2.py
@@ -0,0 +1,354 @@
+import argparse
+import json
+import os
+from pathlib import Path
+from threading import Thread
+
+import numpy as np
+import torch
+import yaml
+from tqdm import tqdm
+
+from models.experimental import attempt_load
+from utils.datasets import create_dataloader
+from utils.general import coco80_to_coco91_class, check_dataset, check_file, check_img_size, check_requirements, \
+ box_iou, non_max_suppression, scale_coords, xyxy2xywh, xywh2xyxy, set_logging, increment_path, colorstr
+from utils.metrics2 import ap_per_class, ConfusionMatrix
+from utils.plots import plot_images, output_to_target, plot_study_txt
+from utils.torch_utils import select_device, time_synchronized, TracedModel
+
+
+def test(data,
+ weights=None,
+ batch_size=32,
+ imgsz=640,
+ conf_thres=0.001,
+ iou_thres=0.6, # for NMS
+ save_json=False,
+ single_cls=False,
+ augment=False,
+ verbose=False,
+ model=None,
+ dataloader=None,
+ save_dir=Path(''), # for saving images
+ save_txt=False, # for auto-labelling
+ save_hybrid=False, # for hybrid auto-labelling
+ save_conf=False, # save auto-label confidences
+ plots=True,
+ wandb_logger=None,
+ compute_loss=None,
+ half_precision=True,
+ trace=False,
+ is_coco=False,
+ v5_metric=False):
+ # Initialize/load model and set device
+ training = model is not None
+ if training: # called by train.py
+ device = next(model.parameters()).device # get model device
+
+ else: # called directly
+ set_logging()
+ device = select_device(opt.device, batch_size=batch_size)
+
+ # Directories
+ save_dir = Path(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok)) # increment run
+ (save_dir / 'labels' if save_txt else save_dir).mkdir(parents=True, exist_ok=True) # make dir
+
+ # Load model
+ model = attempt_load(weights, map_location=device) # load FP32 model
+ gs = max(int(model.stride.max()), 32) # grid size (max stride)
+ imgsz = check_img_size(imgsz, s=gs) # check img_size
+
+ if trace:
+ model = TracedModel(model, device, imgsz)
+
+ # Half
+ half = device.type != 'cpu' and half_precision # half precision only supported on CUDA
+ if half:
+ model.half()
+
+ # Configure
+ model.eval()
+ if isinstance(data, str):
+ is_coco = data.endswith('coco.yaml')
+ with open(data) as f:
+ data = yaml.load(f, Loader=yaml.SafeLoader)
+ check_dataset(data) # check
+ nc = 1 if single_cls else int(data['nc']) # number of classes
+ iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for mAP@0.5:0.95
+ niou = iouv.numel()
+
+ # Logging
+ log_imgs = 0
+ if wandb_logger and wandb_logger.wandb:
+ log_imgs = min(wandb_logger.log_imgs, 100)
+ # Dataloader
+ if not training:
+ if device.type != 'cpu':
+ model(torch.zeros(1, 3, imgsz, imgsz).to(device).type_as(next(model.parameters()))) # run once
+ task = opt.task if opt.task in ('train', 'val', 'test', 'test2', 'test3') else 'val' # path to train/val/test images
+ dataloader = create_dataloader(data[task], imgsz, batch_size, gs, opt, pad=0.5, rect=True,
+ prefix=colorstr(f'{task}: '))[0]
+
+ if v5_metric:
+ print("Testing with YOLOv5 AP metric...")
+
+ seen = 0
+ confusion_matrix = ConfusionMatrix(nc=nc)
+ names = {k: v for k, v in enumerate(model.names if hasattr(model, 'names') else model.module.names)}
+ coco91class = coco80_to_coco91_class()
+ s = ('%20s' + '%12s' * 6) % ('Class', 'Images', 'Labels', 'P', 'R', 'mAP@.5', 'mAP@.5:.95')
+ p, r, f1, mp, mr, map50, map, t0, t1 = 0., 0., 0., 0., 0., 0., 0., 0., 0.
+ loss = torch.zeros(3, device=device)
+ jdict, stats, ap, ap_class, wandb_images = [], [], [], [], []
+ for batch_i, (img, targets, paths, shapes) in enumerate(tqdm(dataloader, desc=s)):
+ img = img.to(device, non_blocking=True)
+ img = img.half() if half else img.float() # uint8 to fp16/32
+ img /= 255.0 # 0 - 255 to 0.0 - 1.0
+ targets = targets.to(device)
+ nb, _, height, width = img.shape # batch size, channels, height, width
+
+ with torch.no_grad():
+ # Run model
+ t = time_synchronized()
+ out, train_out = model(img, augment=augment) # inference and training outputs
+ t0 += time_synchronized() - t
+
+ # Compute loss
+ if compute_loss:
+ loss += compute_loss([x.float() for x in train_out], targets)[1][:3] # box, obj, cls
+
+ # Run NMS
+ targets[:, 2:] *= torch.Tensor([width, height, width, height]).to(device) # to pixels
+ lb = [targets[targets[:, 0] == i, 1:] for i in range(nb)] if save_hybrid else [] # for autolabelling
+ t = time_synchronized()
+ out = non_max_suppression(out, conf_thres=conf_thres, iou_thres=iou_thres, labels=lb, multi_label=True)
+ t1 += time_synchronized() - t
+
+ # Statistics per image
+ for si, pred in enumerate(out):
+ labels = targets[targets[:, 0] == si, 1:]
+ nl = len(labels)
+ tcls = labels[:, 0].tolist() if nl else [] # target class
+ path = Path(paths[si])
+ seen += 1
+
+ if len(pred) == 0:
+ if nl:
+ stats.append((torch.zeros(0, niou, dtype=torch.bool), torch.Tensor(), torch.Tensor(), tcls))
+ continue
+
+ # Predictions
+ predn = pred.clone()
+ scale_coords(img[si].shape[1:], predn[:, :4], shapes[si][0], shapes[si][1]) # native-space pred
+
+ # Append to text file
+ if save_txt:
+ gn = torch.tensor(shapes[si][0])[[1, 0, 1, 0]] # normalization gain whwh
+ for *xyxy, conf, cls in predn.tolist():
+ xywh = (xyxy2xywh(torch.tensor(xyxy).view(1, 4)) / gn).view(-1).tolist() # normalized xywh
+ line = (cls, *xywh, conf) if save_conf else (cls, *xywh) # label format
+ with open(save_dir / 'labels' / (path.stem + '.txt'), 'a') as f:
+ f.write(('%g ' * len(line)).rstrip() % line + '\n')
+
+ # W&B logging - Media Panel Plots
+ if len(wandb_images) < log_imgs and wandb_logger.current_epoch > 0: # Check for test operation
+ if wandb_logger.current_epoch % wandb_logger.bbox_interval == 0:
+ box_data = [{"position": {"minX": xyxy[0], "minY": xyxy[1], "maxX": xyxy[2], "maxY": xyxy[3]},
+ "class_id": int(cls),
+ "box_caption": "%s %.3f" % (names[cls], conf),
+ "scores": {"class_score": conf},
+ "domain": "pixel"} for *xyxy, conf, cls in pred.tolist()]
+ boxes = {"predictions": {"box_data": box_data, "class_labels": names}} # inference-space
+ wandb_images.append(wandb_logger.wandb.Image(img[si], boxes=boxes, caption=path.name))
+ wandb_logger.log_training_progress(predn, path, names) if wandb_logger and wandb_logger.wandb_run else None
+
+ # Append to pycocotools JSON dictionary
+ if save_json:
+ # [{"image_id": 42, "category_id": 18, "bbox": [258.15, 41.29, 348.26, 243.78], "score": 0.236}, ...
+ image_id = int(path.stem) if path.stem.isnumeric() else path.stem
+ box = xyxy2xywh(predn[:, :4]) # xywh
+ box[:, :2] -= box[:, 2:] / 2 # xy center to top-left corner
+ for p, b in zip(pred.tolist(), box.tolist()):
+ jdict.append({'image_id': image_id,
+ 'category_id': coco91class[int(p[5])] if is_coco else int(p[5]),
+ 'bbox': [round(x, 3) for x in b],
+ 'score': round(p[4], 5)})
+
+ # Assign all predictions as incorrect
+ correct = torch.zeros(pred.shape[0], niou, dtype=torch.bool, device=device)
+ if nl:
+ detected = [] # target indices
+ tcls_tensor = labels[:, 0]
+
+ # target boxes
+ tbox = xywh2xyxy(labels[:, 1:5])
+ scale_coords(img[si].shape[1:], tbox, shapes[si][0], shapes[si][1]) # native-space labels
+ if plots:
+ confusion_matrix.process_batch(predn, torch.cat((labels[:, 0:1], tbox), 1))
+
+ # Per target class
+ for cls in torch.unique(tcls_tensor):
+ ti = (cls == tcls_tensor).nonzero(as_tuple=False).view(-1) # prediction indices
+ pi = (cls == pred[:, 5]).nonzero(as_tuple=False).view(-1) # target indices
+
+ # Search for detections
+ if pi.shape[0]:
+ # Prediction to target ious
+ ious, i = box_iou(predn[pi, :4], tbox[ti]).max(1) # best ious, indices
+
+ # Append detections
+ detected_set = set()
+ for j in (ious > iouv[0]).nonzero(as_tuple=False):
+ d = ti[i[j]] # detected target
+ if d.item() not in detected_set:
+ detected_set.add(d.item())
+ detected.append(d)
+ correct[pi[j]] = ious[j] > iouv # iou_thres is 1xn
+ if len(detected) == nl: # all targets already located in image
+ break
+
+ # Append statistics (correct, conf, pcls, tcls)
+ stats.append((correct.cpu(), pred[:, 4].cpu(), pred[:, 5].cpu(), tcls))
+
+ # Plot images
+ if plots and batch_i < 3:
+ f = save_dir / f'test_batch{batch_i}_labels.jpg' # labels
+ Thread(target=plot_images, args=(img, targets, paths, f, names), daemon=True).start()
+ f = save_dir / f'test_batch{batch_i}_pred.jpg' # predictions
+ Thread(target=plot_images, args=(img, output_to_target(out), paths, f, names), daemon=True).start()
+
+ # Compute statistics
+ stats = [np.concatenate(x, 0) for x in zip(*stats)] # to numpy
+ if len(stats) and stats[0].any():
+ p, r, ap, f1, ap_class = ap_per_class(*stats, plot=plots, v5_metric=v5_metric, save_dir=save_dir, names=names)
+ ap50, ap = ap[:, 0], ap.mean(1) # AP@0.5, AP@0.5:0.95
+ mp, mr, map50, map = p.mean(), r.mean(), ap50.mean(), ap.mean()
+ nt = np.bincount(stats[3].astype(np.int64), minlength=nc) # number of targets per class
+ else:
+ nt = torch.zeros(1)
+
+ # Print results
+ pf = '%20s' + '%12i' * 2 + '%12.3g' * 4 # print format
+ print(pf % ('all', seen, nt.sum(), mp, mr, map50, map))
+
+ # Print results per class
+ if (verbose or (nc < 50 and not training)) and nc > 1 and len(stats):
+ for i, c in enumerate(ap_class):
+ print(pf % (names[c], seen, nt[c], p[i], r[i], ap50[i], ap[i]))
+
+ # Print speeds
+ t = tuple(x / seen * 1E3 for x in (t0, t1, t0 + t1)) + (imgsz, imgsz, batch_size) # tuple
+ if not training:
+ print('Speed: %.1f/%.1f/%.1f ms inference/NMS/total per %gx%g image at batch-size %g' % t)
+
+ # Plots
+ if plots:
+ confusion_matrix.plot(save_dir=save_dir, names=list(names.values()))
+ confusion_matrix.write(os.path.join(save_dir, 'confusion_matrix_counts.txt'))
+ if wandb_logger and wandb_logger.wandb:
+ val_batches = [wandb_logger.wandb.Image(str(f), caption=f.name) for f in sorted(save_dir.glob('test*.jpg'))]
+ wandb_logger.log({"Validation": val_batches})
+ if wandb_images:
+ wandb_logger.log({"Bounding Box Debugger/Images": wandb_images})
+
+ # Save JSON
+ if save_json and len(jdict):
+ w = Path(weights[0] if isinstance(weights, list) else weights).stem if weights is not None else '' # weights
+ anno_json = './coco/annotations/instances_val2017.json' # annotations json
+ pred_json = str(save_dir / f"{w}_predictions.json") # predictions json
+ print('\nEvaluating pycocotools mAP... saving %s...' % pred_json)
+ with open(pred_json, 'w') as f:
+ json.dump(jdict, f)
+
+ try: # https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocoEvalDemo.ipynb
+ from pycocotools.coco import COCO
+ from pycocotools.cocoeval import COCOeval
+
+ anno = COCO(anno_json) # init annotations api
+ pred = anno.loadRes(pred_json) # init predictions api
+ eval = COCOeval(anno, pred, 'bbox')
+ if is_coco:
+ eval.params.imgIds = [int(Path(x).stem) for x in dataloader.dataset.img_files] # image IDs to evaluate
+ eval.evaluate()
+ eval.accumulate()
+ eval.summarize()
+ map, map50 = eval.stats[:2] # update results (mAP@0.5:0.95, mAP@0.5)
+ except Exception as e:
+ print(f'pycocotools unable to run: {e}')
+
+ # Return results
+ model.float() # for training
+ if not training:
+ s = f"\n{len(list(save_dir.glob('labels/*.txt')))} labels saved to {save_dir / 'labels'}" if save_txt else ''
+ print(f"Results saved to {save_dir}{s}")
+ maps = np.zeros(nc) + map
+ for i, c in enumerate(ap_class):
+ maps[c] = ap[i]
+ return (mp, mr, map50, map, *(loss.cpu() / len(dataloader)).tolist()), maps, t
+
+
+if __name__ == '__main__':
+ parser = argparse.ArgumentParser(prog='test.py')
+ parser.add_argument('--weights', nargs='+', type=str, default='yolov7.pt', help='model.pt path(s)')
+ parser.add_argument('--data', type=str, default='data/coco.yaml', help='*.data path')
+ parser.add_argument('--batch-size', type=int, default=32, help='size of each image batch')
+ parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
+ parser.add_argument('--conf-thres', type=float, default=0.001, help='object confidence threshold')
+ parser.add_argument('--iou-thres', type=float, default=0.65, help='IOU threshold for NMS')
+ parser.add_argument('--task', default='val', help='train, val, test, speed or study')
+ parser.add_argument('--device', default='', help='cuda device, i.e. 0 or 0,1,2,3 or cpu')
+ parser.add_argument('--single-cls', action='store_true', help='treat as single-class dataset')
+ parser.add_argument('--augment', action='store_true', help='augmented inference')
+ parser.add_argument('--verbose', action='store_true', help='report mAP by class')
+ parser.add_argument('--save-txt', action='store_true', help='save results to *.txt')
+ parser.add_argument('--save-hybrid', action='store_true', help='save label+prediction hybrid results to *.txt')
+ parser.add_argument('--save-conf', action='store_true', help='save confidences in --save-txt labels')
+ parser.add_argument('--save-json', action='store_true', help='save a cocoapi-compatible JSON results file')
+ parser.add_argument('--project', default='runs/test', help='save to project/name')
+ parser.add_argument('--name', default='exp', help='save to project/name')
+ parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
+ parser.add_argument('--no-trace', action='store_true', help='don`t trace model')
+ parser.add_argument('--v5-metric', action='store_true', help='assume maximum recall as 1.0 in AP calculation')
+ opt = parser.parse_args()
+ opt.save_json |= opt.data.endswith('coco.yaml')
+ opt.data = check_file(opt.data) # check file
+ print(opt)
+ #check_requirements()
+
+ if opt.task in ('train', 'val', 'test', 'test2', 'test3'): # run normally
+ test(opt.data,
+ opt.weights,
+ opt.batch_size,
+ opt.img_size,
+ opt.conf_thres,
+ opt.iou_thres,
+ opt.save_json,
+ opt.single_cls,
+ opt.augment,
+ opt.verbose,
+ save_txt=opt.save_txt | opt.save_hybrid,
+ save_hybrid=opt.save_hybrid,
+ save_conf=opt.save_conf,
+ trace=not opt.no_trace,
+ v5_metric=opt.v5_metric
+ )
+
+ elif opt.task == 'speed': # speed benchmarks
+ for w in opt.weights:
+ test(opt.data, w, opt.batch_size, opt.img_size, 0.25, 0.45, save_json=False, plots=False, v5_metric=opt.v5_metric)
+
+ elif opt.task == 'study': # run over a range of settings and save/plot
+ # python test.py --task study --data coco.yaml --iou 0.65 --weights yolov7.pt
+ x = list(range(256, 1536 + 128, 128)) # x axis (image sizes)
+ for w in opt.weights:
+ f = f'study_{Path(opt.data).stem}_{Path(w).stem}.txt' # filename to save to
+ y = [] # y axis
+ for i in x: # img-size
+ print(f'\nRunning {f} point {i}...')
+ r, _, t = test(opt.data, w, opt.batch_size, i, opt.conf_thres, opt.iou_thres, opt.save_json,
+ plots=False, v5_metric=opt.v5_metric)
+ y.append(r + t) # results and times
+ np.savetxt(f, y, fmt='%10.4g') # save
+ os.system('zip -r study.zip study_*.txt')
+ plot_study_txt(x=x) # plot