Upload 913 files
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributes+2
- Dockerfile+2-2
- LICENSEAdded+661
- README.md+6-8
- __pycache__/jaa.cpython-311.pycAdded
- __pycache__/settings.cpython-311.pycAdded
- chain_img_processor/__init__.pyAdded+4
- chain_img_processor/__pycache__/__init__.cpython-311.pycAdded
- chain_img_processor/__pycache__/batchimage.cpython-311.pycAdded
- chain_img_processor/__pycache__/ffmpeg_writer.cpython-311.pycAdded
- chain_img_processor/__pycache__/image.cpython-311.pycAdded
- chain_img_processor/__pycache__/video.cpython-311.pycAdded
- chain_img_processor/batchimage.pyAdded+86
- chain_img_processor/ffmpeg_writer.pyAdded+253
- chain_img_processor/image.pyAdded+176
- chain_img_processor/video.pyAdded+132
- clip/__init__.pyAdded+1
- clip/__pycache__/__init__.cpython-311.pycAdded
- clip/__pycache__/clip.cpython-311.pycAdded
- clip/__pycache__/model.cpython-311.pycAdded
- clip/__pycache__/simple_tokenizer.cpython-311.pycAdded
- clip/bpe_simple_vocab_16e6.txt.gzAdded+3
- clip/clip.pyAdded+245
- clip/clipseg.pyAdded+538
- clip/model.pyAdded+436
- clip/simple_tokenizer.pyAdded+132
- clip/vitseg.pyAdded+286
- docs/faceselection.pngAdded
- docs/finaloutput.pngAdded
- docs/kickboxing.jpgAdded
- docs/musk.jpgAdded
- docs/screenshot.pngAdded
- gfpgan/weights/detection_Resnet50_Final.pthAdded+3
- gfpgan/weights/parsing_parsenet.pthAdded+3
- installer/installer.pyAdded+83
- installer/windows_run.batAdded+80
- jaa.pyAdded+355
- models/CLIP/rd64-uni-refined.pthAdded+3
- models/CodeFormer/codeformer.pthAdded+3
- models/CodeFormer/facelib/detection_Resnet50_Final.pthAdded+3
- models/CodeFormer/facelib/parsing_parsenet.pthAdded+3
- models/CodeFormer/realesrgan/RealESRGAN_x2plus.pthAdded+3
- models/DMDNet.pthAdded+3
- models/GFPGANv1.4.pthAdded+3
- models/inswapper_128.onnxAdded+3
- mynewshinyroop/.gitignoreAdded+2
- mynewshinyroop/Lib/site-packages/_distutils_hack/__init__.pyAdded+227
- mynewshinyroop/Lib/site-packages/_distutils_hack/override.pyAdded+1
- mynewshinyroop/Lib/site-packages/_virtualenv.pthAdded+3
- mynewshinyroop/Lib/site-packages/_virtualenv.pyAdded+102
35 unmodified lines36373835 unmodified linesapp/.github/examples/snapshot.mp4 filter=lfs diff=lfs merge=lfs -textapp/docs/finaloutput.png filter=lfs diff=lfs merge=lfs -textapp/temp/6efe43a0630bd48ea2e9c9a638f368e81348db7b/image.png filter=lfs diff=lfs merge=lfs -text35 unmodified lines363738394035 unmodified linesapp/.github/examples/snapshot.mp4 filter=lfs diff=lfs merge=lfs -textapp/docs/finaloutput.png filter=lfs diff=lfs merge=lfs -textapp/temp/6efe43a0630bd48ea2e9c9a638f368e81348db7b/image.png filter=lfs diff=lfs merge=lfs -textdocs/finaloutput.png filter=lfs diff=lfs merge=lfs -texttemp/6efe43a0630bd48ea2e9c9a638f368e81348db7b/image.png filter=lfs diff=lfs merge=lfs -text
1234567FROM python:3.11WORKDIR /codeRUN apt-get update && apt-get install -y libgl1-mesa-glxCOPY requirements.txt ./RUN pip install --no-cache-dir -r requirements.txtCOPY . .CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]1234567FROM python:3.11WORKDIR /usr/src/appRUN apt-get update && apt-get install -y libgl1-mesa-glxCOPY requirements.txt ./RUN pip install --no-cache-dir -r requirements.txtCOPY . .CMD ["python", "run.py"]
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661 GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
Preamble
The GNU Affero General Public License is a free, copyleft license forsoftware and other kinds of works, specifically designed to ensurecooperation with the community in the case of network server software.
The licenses for most software and other practical works are designedto take away your freedom to share and change the works. By contrast,our General Public Licenses are intended to guarantee your freedom toshare and change all versions of a program--to make sure it remains freesoftware for all its users.
When we speak of free software, we are referring to freedom, notprice. Our General Public Licenses are designed to make sure that youhave the freedom to distribute copies of free software (and charge forthem if you wish), that you receive source code or can get it if youwant it, that you can change the software or use pieces of it in newfree programs, and that you know you can do these things.
Developers that use our General Public Licenses protect your rightswith two steps: (1) assert copyright on the software, and (2) offeryou this License which gives you legal permission to copy, distributeand/or modify the software.
A secondary benefit of defending all users' freedom is thatimprovements made in alternate versions of the program, if theyreceive widespread use, become available for other developers toincorporate. Many developers of free software are heartened andencouraged by the resulting cooperation. However, in the case ofsoftware used on network servers, this result may fail to come about.The GNU General Public License permits making a modified version andletting the public access it on a server without ever releasing itssource code to the public.
The GNU Affero General Public License is designed specifically toensure that, in such cases, the modified source code becomes availableto the community. It requires the operator of a network server toprovide the source code of the modified version running there to theusers of that server. Therefore, public use of a modified version, ona publicly accessible server, gives the public access to the sourcecode of the modified version.
An older license, called the Affero General Public License andpublished by Affero, was designed to accomplish similar goals. This isa different license, not a version of the Affero GPL, but Affero hasreleased a new version of the Affero GPL which permits relicensing underthis license.
The precise terms and conditions for copying, distribution andmodification follow.
TERMS AND CONDITIONS
0. Definitions.
"This License" refers to version 3 of the GNU Affero General Public License.
"Copyright" also means copyright-like laws that apply to other kinds ofworks, such as semiconductor masks.
"The Program" refers to any copyrightable work licensed under thisLicense. 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 workin a fashion requiring copyright permission, other than the making of anexact copy. The resulting work is called a "modified version" of theearlier work or a work "based on" the earlier work.
A "covered work" means either the unmodified Program or a work basedon the Program.
To "propagate" a work means to do anything with it that, withoutpermission, would make you directly or secondarily liable forinfringement under applicable copyright law, except executing it on acomputer or modifying a private copy. Propagation includes copying,distribution (with or without modification), making available to thepublic, and in some countries other activities as well.
To "convey" a work means any kind of propagation that enables otherparties to make or receive copies. Mere interaction with a user througha 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 visiblefeature that (1) displays an appropriate copyright notice, and (2)tells the user that there is no warranty for the work (except to theextent that warranties are provided), that licensees may convey thework under this License, and how to view a copy of this License. Ifthe interface presents a list of user commands or options, such as amenu, a prominent item in the list meets this criterion.
1. Source Code.
The "source code" for a work means the preferred form of the workfor making modifications to it. "Object code" means any non-sourceform of a work.
A "Standard Interface" means an interface that either is an officialstandard defined by a recognized standards body, or, in the case ofinterfaces specified for a particular programming language, one thatis widely used among developers working in that language.
The "System Libraries" of an executable work include anything, otherthan the work as a whole, that (a) is included in the normal form ofpackaging a Major Component, but which is not part of that MajorComponent, and (b) serves only to enable use of the work with thatMajor Component, or to implement a Standard Interface for which animplementation 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 toproduce the work, or an object code interpreter used to run it.
The "Corresponding Source" for a work in object code form means allthe source code needed to generate, install, and (for an executablework) run the object code and to modify the work, including scripts tocontrol those activities. However, it does not include the work'sSystem Libraries, or general-purpose tools or generally available freeprograms which are used unmodified in performing those activities butwhich are not part of the work. For example, Corresponding Sourceincludes interface definition files associated with source files forthe work, and the source code for shared libraries and dynamicallylinked subprograms that the work is specifically designed to require,such as by intimate data communication or control flow between thosesubprograms and other parts of the work.
The Corresponding Source need not include anything that userscan regenerate automatically from other parts of the CorrespondingSource.
The Corresponding Source for a work in source code form is thatsame work.
2. Basic Permissions.
All rights granted under this License are granted for the term ofcopyright on the Program, and are irrevocable provided the statedconditions are met. This License explicitly affirms your unlimitedpermission to run the unmodified Program. The output from running acovered work is covered by this License only if the output, given itscontent, constitutes a covered work. This License acknowledges yourrights of fair use or other equivalent, as provided by copyright law.
You may make, run and propagate covered works that you do notconvey, without conditions so long as your license otherwise remainsin force. You may convey covered works to others for the sole purposeof having them make modifications exclusively for you, or provide youwith facilities for running those works, provided that you comply withthe terms of this License in conveying all material for which you donot control copyright. Those thus making or running the covered worksfor you must do so exclusively on your behalf, under your directionand control, on terms that prohibit them from making any copies ofyour copyrighted material outside their relationship with you.
Conveying under any other circumstances is permitted solely underthe conditions stated below. Sublicensing is not allowed; section 10makes it unnecessary.
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
No covered work shall be deemed part of an effective technologicalmeasure under any applicable law fulfilling obligations under article11 of the WIPO copyright treaty adopted on 20 December 1996, orsimilar laws prohibiting or restricting circumvention of suchmeasures.
When you convey a covered work, you waive any legal power to forbidcircumvention of technological measures to the extent such circumventionis effected by exercising rights under this License with respect tothe covered work, and you disclaim any intention to limit operation ormodification of the work as a means of enforcing, against the work'susers, your or third parties' legal rights to forbid circumvention oftechnological measures.
4. Conveying Verbatim Copies.
You may convey verbatim copies of the Program's source code as youreceive it, in any medium, provided that you conspicuously andappropriately publish on each copy an appropriate copyright notice;keep intact all notices stating that this License and anynon-permissive terms added in accord with section 7 apply to the code;keep intact all notices of the absence of any warranty; and give allrecipients 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 toproduce it from the Program, in the form of source code under theterms 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 independentworks, 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 notused to limit the access or legal rights of the compilation's usersbeyond what the individual works permit. Inclusion of a covered workin an aggregate does not cause this License to apply to the otherparts of the aggregate.
6. Conveying Non-Source Forms.
You may convey a covered work in object code form under the termsof sections 4 and 5, provided that you also convey themachine-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 excludedfrom the Corresponding Source as a System Library, need not beincluded in conveying the object code work.
A "User Product" is either (1) a "consumer product", which means anytangible personal property which is normally used for personal, family,or household purposes, or (2) anything designed or sold for incorporationinto a dwelling. In determining whether a product is a consumer product,doubtful cases shall be resolved in favor of coverage. For a particularproduct received by a particular user, "normally used" refers to atypical or common use of that class of product, regardless of the statusof the particular user or of the way in which the particular useractually uses, or expects or is expected to use, the product. A productis a consumer product regardless of whether the product has substantialcommercial, industrial or non-consumer uses, unless such uses representthe 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 installand execute modified versions of a covered work in that User Product froma modified version of its Corresponding Source. The information mustsuffice to ensure that the continued functioning of the modified objectcode is in no case prevented or interfered with solely becausemodification has been made.
If you convey an object code work under this section in, or with, orspecifically for use in, a User Product, and the conveying occurs aspart of a transaction in which the right of possession and use of theUser Product is transferred to the recipient in perpetuity or for afixed term (regardless of how the transaction is characterized), theCorresponding Source conveyed under this section must be accompaniedby the Installation Information. But this requirement does not applyif neither you nor any third party retains the ability to installmodified object code on the User Product (for example, the work hasbeen installed in ROM).
The requirement to provide Installation Information does not include arequirement to continue to provide support service, warranty, or updatesfor a work that has been modified or installed by the recipient, or forthe User Product in which it has been modified or installed. Access to anetwork may be denied when the modification itself materially andadversely affects the operation of the network or violates the rules andprotocols for communication across the network.
Corresponding Source conveyed, and Installation Information provided,in accord with this section must be in a format that is publiclydocumented (and with an implementation available to the public insource code form), and must require no special password or key forunpacking, reading or copying.
7. Additional Terms.
"Additional permissions" are terms that supplement the terms of thisLicense by making exceptions from one or more of its conditions.Additional permissions that are applicable to the entire Program shallbe treated as though they were included in this License, to the extentthat they are valid under applicable law. If additional permissionsapply only to part of the Program, that part may be used separatelyunder those permissions, but the entire Program remains governed bythis License without regard to the additional permissions.
When you convey a copy of a covered work, you may at your optionremove any additional permissions from that copy, or from any part ofit. (Additional permissions may be written to require their ownremoval in certain cases when you modify the work.) You may placeadditional 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 youadd to a covered work, you may (if authorized by the copyright holders ofthat 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 "furtherrestrictions" within the meaning of section 10. If the Program as youreceived it, or any part of it, contains a notice stating that it isgoverned by this License along with a term that is a furtherrestriction, you may remove that term. If a license document containsa further restriction but permits relicensing or conveying under thisLicense, you may add to a covered work material governed by the termsof that license document, provided that the further restriction doesnot survive such relicensing or conveying.
If you add terms to a covered work in accord with this section, youmust place, in the relevant source files, a statement of theadditional terms that apply to those files, or a notice indicatingwhere to find the applicable terms.
Additional terms, permissive or non-permissive, may be stated in theform 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 expresslyprovided under this License. Any attempt otherwise to propagate ormodify it is void, and will automatically terminate your rights underthis License (including any patent licenses granted under the thirdparagraph of section 11).
However, if you cease all violation of this License, then yourlicense from a particular copyright holder is reinstated (a)provisionally, unless and until the copyright holder explicitly andfinally terminates your license, and (b) permanently, if the copyrightholder fails to notify you of the violation by some reasonable meansprior to 60 days after the cessation.
Moreover, your license from a particular copyright holder isreinstated permanently if the copyright holder notifies you of theviolation by some reasonable means, this is the first time you havereceived notice of violation of this License (for any work) from thatcopyright holder, and you cure the violation prior to 30 days afteryour receipt of the notice.
Termination of your rights under this section does not terminate thelicenses of parties who have received copies or rights from you underthis License. If your rights have been terminated and not permanentlyreinstated, you do not qualify to receive new licenses for the samematerial under section 10.
9. Acceptance Not Required for Having Copies.
You are not required to accept this License in order to receive orrun a copy of the Program. Ancillary propagation of a covered workoccurring solely as a consequence of using peer-to-peer transmissionto receive a copy likewise does not require acceptance. However,nothing other than this License grants you permission to propagate ormodify any covered work. These actions infringe copyright if you donot accept this License. Therefore, by modifying or propagating acovered 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 automaticallyreceives a license from the original licensors, to run, modify andpropagate that work, subject to this License. You are not responsiblefor enforcing compliance by third parties with this License.
An "entity transaction" is a transaction transferring control of anorganization, or substantially all assets of one, or subdividing anorganization, or merging organizations. If propagation of a coveredwork results from an entity transaction, each party to thattransaction who receives a copy of the work also receives whateverlicenses to the work the party's predecessor in interest had or couldgive under the previous paragraph, plus a right to possession of theCorresponding Source of the work from the predecessor in interest, ifthe predecessor has it or can get it with reasonable efforts.
You may not impose any further restrictions on the exercise of therights granted or affirmed under this License. For example, you maynot impose a license fee, royalty, or other charge for exercise ofrights granted under this License, and you may not initiate litigation(including a cross-claim or counterclaim in a lawsuit) alleging thatany patent claim is infringed by making, using, selling, offering forsale, or importing the Program or any portion of it.
11. Patents.
A "contributor" is a copyright holder who authorizes use under thisLicense of the Program or a work on which the Program is based. Thework thus licensed is called the contributor's "contributor version".
A contributor's "essential patent claims" are all patent claimsowned or controlled by the contributor, whether already acquired orhereafter acquired, that would be infringed by some manner, permittedby this License, of making, using, or selling its contributor version,but do not include claims that would be infringed only as aconsequence of further modification of the contributor version. Forpurposes of this definition, "control" includes the right to grantpatent sublicenses in a manner consistent with the requirements ofthis License.
Each contributor grants you a non-exclusive, worldwide, royalty-freepatent license under the contributor's essential patent claims, tomake, use, sell, offer for sale, import and otherwise run, modify andpropagate the contents of its contributor version.
In the following three paragraphs, a "patent license" is any expressagreement or commitment, however denominated, not to enforce a patent(such as an express permission to practice a patent or covenant not tosue for patent infringement). To "grant" such a patent license to aparty means to make such an agreement or commitment not to enforce apatent 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 anyoneto copy, free of charge and under the terms of this License, through apublicly available network server or other readily accessible means,then you must either (1) cause the Corresponding Source to be soavailable, or (2) arrange to deprive yourself of the benefit of thepatent license for this particular work, or (3) arrange, in a mannerconsistent with the requirements of this License, to extend the patentlicense to downstream recipients. "Knowingly relying" means you haveactual knowledge that, but for the patent license, your conveying thecovered work in a country, or your recipient's use of the covered workin a country, would infringe one or more identifiable patents in thatcountry that you have reason to believe are valid.
If, pursuant to or in connection with a single transaction orarrangement, you convey, or propagate by procuring conveyance of, acovered work, and grant a patent license to some of the partiesreceiving the covered work authorizing them to use, propagate, modifyor convey a specific copy of the covered work, then the patent licenseyou grant is automatically extended to all recipients of the coveredwork and works based on it.
A patent license is "discriminatory" if it does not include withinthe scope of its coverage, prohibits the exercise of, or isconditioned on the non-exercise of one or more of the rights that arespecifically granted under this License. You may not convey a coveredwork if you are a party to an arrangement with a third party that isin the business of distributing software, under which you make paymentto the third party based on the extent of your activity of conveyingthe work, and under which the third party grants, to any of theparties who would receive the covered work from you, a discriminatorypatent license (a) in connection with copies of the covered workconveyed by you (or copies made from those copies), or (b) primarilyfor and in connection with specific products or compilations thatcontain 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 limitingany implied license or other defenses to infringement that mayotherwise 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 orotherwise) that contradict the conditions of this License, they do notexcuse you from the conditions of this License. If you cannot convey acovered work so as to satisfy simultaneously your obligations under thisLicense and any other pertinent obligations, then as a consequence you maynot convey it at all. For example, if you agree to terms that obligate youto collect a royalty for further conveying from those to whom you conveythe Program, the only way you could satisfy both those terms and thisLicense would be to refrain entirely from conveying the Program.
13. Remote Network Interaction; Use with the GNU General Public License.
Notwithstanding any other provision of this License, if you modify theProgram, your modified version must prominently offer all usersinteracting with it remotely through a computer network (if your versionsupports such interaction) an opportunity to receive the CorrespondingSource of your version by providing access to the Corresponding Sourcefrom a network server at no charge, through some standard or customarymeans of facilitating copying of software. This Corresponding Sourceshall include the Corresponding Source for any work covered by version 3of the GNU General Public License that is incorporated pursuant to thefollowing paragraph.
Notwithstanding any other provision of this License, you havepermission to link or combine any covered work with a work licensedunder version 3 of the GNU General Public License into a singlecombined work, and to convey the resulting work. The terms of thisLicense will continue to apply to the part which is the covered work,but the work with which it is combined will remain governed by version3 of the GNU General Public License.
14. Revised Versions of this License.
The Free Software Foundation may publish revised and/or new versions ofthe GNU Affero General Public License from time to time. Such new versionswill be similar in spirit to the present version, but may differ in detail toaddress new problems or concerns.
Each version is given a distinguishing version number. If theProgram specifies that a certain numbered version of the GNU Affero GeneralPublic License "or any later version" applies to it, you have theoption of following the terms and conditions either of that numberedversion or of any later version published by the Free SoftwareFoundation. If the Program does not specify a version number of theGNU Affero General Public License, you may choose any version ever publishedby the Free Software Foundation.
If the Program specifies that a proxy can decide which futureversions of the GNU Affero General Public License can be used, that proxy'spublic statement of acceptance of a version permanently authorizes youto choose that version for the Program.
Later license versions may give you additional or differentpermissions. However, no additional obligations are imposed on anyauthor or copyright holder as a result of your choosing to follow alater version.
15. Disclaimer of Warranty.
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BYAPPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHTHOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTYOF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULARPURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAMIS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OFALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. Limitation of Liability.
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITINGWILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYSTHE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANYGENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THEUSE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OFDATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRDPARTIES 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 OFSUCH DAMAGES.
17. Interpretation of Sections 15 and 16.
If the disclaimer of warranty and limitation of liability providedabove cannot be given local legal effect according to their terms,reviewing courts shall apply local law that most closely approximatesan absolute waiver of all civil liability in connection with theProgram, unless a warranty or assumption of liability accompanies acopy 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 greatestpossible use to the public, the best way to achieve this is to make itfree software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safestto attach them to the start of each source file to most effectivelystate the exclusion of warranty; and each file should have at leastthe "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.> Copyright (C) <year> <name of author>
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
Also add information on how to contact you by electronic and paper mail.
If your software can interact with users remotely through a computernetwork, you should also make sure that it provides a way for users toget its source. For example, if your program is a web application, itsinterface could display a "Source" link that leads users to an archiveof the code. There are many ways you could offer source, and differentsolutions will be better for different programs; see section 13 for thespecific requirements.
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 AGPL, see<https://www.gnu.org/licenses/>.1234567891011---title: Face Swap Dockeremoji: 🚀colorFrom: graycolorTo: yellowsdk: dockerpinned: falselicense: gpl-3.0---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789---title: face-swapapp_file: run.pysdk: gradiosdk_version: 3.40.1---# roop-unleashedWIP Version of roop-unleashed using Gradio UI
1234from .image import ChainImgProcessor, ChainImgPlugin, get_single_image_processor, versionfrom .video import ChainVideoProcessor, get_single_video_processorfrom .batchimage import ChainBatchImageProcessorfrom .ffmpeg_writer import FFMPEG_VideoWriter1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586from typing import Any, List, Callableimport psutilimport osfrom concurrent.futures import ThreadPoolExecutor, as_completedfrom queue import Queuefrom .image import ChainImgProcessorfrom tqdm import tqdmimport cv2
def create_queue(temp_frame_paths: List[str]) -> Queue[str]: queue: Queue[str] = Queue() for frame_path in temp_frame_paths: queue.put(frame_path) return queue
def pick_queue(queue: Queue[str], queue_per_future: int) -> List[str]: queues = [] for _ in range(queue_per_future): if not queue.empty(): queues.append(queue.get()) return queues
class ChainBatchImageProcessor(ChainImgProcessor): chain = None func_params_gen = None num_threads = 1
def __init__(self): ChainImgProcessor.__init__(self)
def init_with_plugins(self): self.init_plugins(["core"]) self.display_init_info()
init_on_start_arr = self.init_on_start.split(",") for proc_id in init_on_start_arr: self.init_processor(proc_id)
def update_progress(self, progress: Any = None) -> None: process = psutil.Process(os.getpid()) memory_usage = process.memory_info().rss / 1024 / 1024 / 1024 progress.set_postfix({ 'memory_usage': '{:.2f}'.format(memory_usage).zfill(5) + 'GB', 'execution_threads': self.num_threads }) progress.refresh() progress.update(1)
def process_frames(self, source_files: List[str], target_files: List[str], current_files, update: Callable[[], None]) -> None: for f in current_files: temp_frame = cv2.imread(f) if temp_frame is not None: if self.func_params_gen: params = self.func_params_gen(None, temp_frame) else: params = {} resimg, _ = self.run_chain(temp_frame, params, self.chain) if resimg is not None: i = source_files.index(f) cv2.imwrite(target_files[i], resimg) if update: update()
def run_batch_chain(self, source_files, target_files, threads:int = 1, chain = None, params_frame_gen_func = None): self.chain = chain self.func_params_gen = params_frame_gen_func progress_bar_format = '{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]' total = len(source_files) self.num_threads = threads with tqdm(total=total, desc='Processing', unit='frame', dynamic_ncols=True, bar_format=progress_bar_format) as progress: with ThreadPoolExecutor(max_workers=threads) as executor: futures = [] queue = create_queue(source_files) queue_per_future = max(len(source_files) // threads, 1) while not queue.empty(): future = executor.submit(self.process_frames, source_files, target_files, pick_queue(queue, queue_per_future), lambda: self.update_progress(progress)) futures.append(future) for future in as_completed(futures): future.result()
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253"""FFMPEG_Writer - write set of frames to video file
original fromhttps://github.com/Zulko/moviepy/blob/master/moviepy/video/io/ffmpeg_writer.py
removed unnecessary dependencies
The MIT License (MIT)
Copyright (c) 2015 ZulkoCopyright (c) 2023 Janvarev Vladislav"""
import osimport subprocess as sp
PIPE = -1STDOUT = -2DEVNULL = -3
FFMPEG_BINARY = "ffmpeg"
class FFMPEG_VideoWriter: """ A class for FFMPEG-based video writing.
A class to write videos using ffmpeg. ffmpeg will write in a large choice of formats.
Parameters -----------
filename Any filename like 'video.mp4' etc. but if you want to avoid complications it is recommended to use the generic extension '.avi' for all your videos.
size Size (width,height) of the output video in pixels.
fps Frames per second in the output video file.
codec FFMPEG codec. It seems that in terms of quality the hierarchy is 'rawvideo' = 'png' > 'mpeg4' > 'libx264' 'png' manages the same lossless quality as 'rawvideo' but yields smaller files. Type ``ffmpeg -codecs`` in a terminal to get a list of accepted codecs.
Note for default 'libx264': by default the pixel format yuv420p is used. If the video dimensions are not both even (e.g. 720x405) another pixel format is used, and this can cause problem in some video readers.
audiofile Optional: The name of an audio file that will be incorporated to the video.
preset Sets the time that FFMPEG will take to compress the video. The slower, the better the compression rate. Possibilities are: ultrafast,superfast, veryfast, faster, fast, medium (default), slow, slower, veryslow, placebo.
bitrate Only relevant for codecs which accept a bitrate. "5000k" offers nice results in general.
"""
def __init__(self, filename, size, fps, codec="libx265", crf=14, audiofile=None, preset="medium", bitrate=None, logfile=None, threads=None, ffmpeg_params=None):
if logfile is None: logfile = sp.PIPE
self.filename = filename self.codec = codec self.ext = self.filename.split(".")[-1] w = size[0] - 1 if size[0] % 2 != 0 else size[0] h = size[1] - 1 if size[1] % 2 != 0 else size[1]
# order is important cmd = [ FFMPEG_BINARY, '-hide_banner', '-hwaccel', 'auto', '-y', '-loglevel', 'error' if logfile == sp.PIPE else 'info', '-f', 'rawvideo', '-vcodec', 'rawvideo', '-s', '%dx%d' % (size[0], size[1]), #'-pix_fmt', 'rgba' if withmask else 'rgb24', '-pix_fmt', 'bgr24', '-r', str(fps), '-an', '-i', '-' ]
if audiofile is not None: cmd.extend([ '-i', audiofile, '-acodec', 'copy' ])
cmd.extend([ '-vcodec', codec, '-crf', str(crf) #'-preset', preset, ]) if ffmpeg_params is not None: cmd.extend(ffmpeg_params) if bitrate is not None: cmd.extend([ '-b', bitrate ])
# scale to a resolution divisible by 2 if not even cmd.extend(['-vf', f'scale={w}:{h}' if w != size[0] or h != size[1] else 'colorspace=bt709:iall=bt601-6-625:fast=1'])
if threads is not None: cmd.extend(["-threads", str(threads)])
cmd.extend([ '-pix_fmt', 'yuv420p',
]) cmd.extend([ filename ])
test = str(cmd) print(test)
popen_params = {"stdout": DEVNULL, "stderr": logfile, "stdin": sp.PIPE}
# This was added so that no extra unwanted window opens on windows # when the child process is created if os.name == "nt": popen_params["creationflags"] = 0x08000000 # CREATE_NO_WINDOW self.proc = sp.Popen(cmd, **popen_params)
def write_frame(self, img_array): """ Writes one frame in the file.""" try: #if PY3: self.proc.stdin.write(img_array.tobytes()) # else: # self.proc.stdin.write(img_array.tostring()) except IOError as err: _, ffmpeg_error = self.proc.communicate() error = (str(err) + ("\n\nMoviePy error: FFMPEG encountered " "the following error while writing file %s:" "\n\n %s" % (self.filename, str(ffmpeg_error))))
if b"Unknown encoder" in ffmpeg_error:
error = error+("\n\nThe video export " "failed because FFMPEG didn't find the specified " "codec for video encoding (%s). Please install " "this codec or change the codec when calling " "write_videofile. For instance:\n" " >>> clip.write_videofile('myvid.webm', codec='libvpx')")%(self.codec)
elif b"incorrect codec parameters ?" in ffmpeg_error:
error = error+("\n\nThe video export " "failed, possibly because the codec specified for " "the video (%s) is not compatible with the given " "extension (%s). Please specify a valid 'codec' " "argument in write_videofile. This would be 'libx264' " "or 'mpeg4' for mp4, 'libtheora' for ogv, 'libvpx for webm. " "Another possible reason is that the audio codec was not " "compatible with the video codec. For instance the video " "extensions 'ogv' and 'webm' only allow 'libvorbis' (default) as a" "video codec." )%(self.codec, self.ext)
elif b"encoder setup failed" in ffmpeg_error:
error = error+("\n\nThe video export " "failed, possibly because the bitrate you specified " "was too high or too low for the video codec.")
elif b"Invalid encoder type" in ffmpeg_error:
error = error + ("\n\nThe video export failed because the codec " "or file extension you provided is not a video")
raise IOError(error)
def close(self): if self.proc: self.proc.stdin.close() if self.proc.stderr is not None: self.proc.stderr.close() self.proc.wait()
self.proc = None
# Support the Context Manager protocol, to ensure that resources are cleaned up.
def __enter__(self): return self
def __exit__(self, exc_type, exc_value, traceback): self.close()
def ffmpeg_write_image(filename, image, logfile=False): """ Writes an image (HxWx3 or HxWx4 numpy array) to a file, using ffmpeg. """
if image.dtype != 'uint8': image = image.astype("uint8")
cmd = [ FFMPEG_BINARY, '-y', '-s', "%dx%d"%(image.shape[:2][::-1]), "-f", 'rawvideo', '-pix_fmt', "rgba" if (image.shape[2] == 4) else "rgb24", '-i','-', filename]
if logfile: log_file = open(filename + ".log", 'w+') else: log_file = sp.PIPE
popen_params = {"stdout": DEVNULL, "stderr": log_file, "stdin": sp.PIPE}
if os.name == "nt": popen_params["creationflags"] = 0x08000000
proc = sp.Popen(cmd, **popen_params) out, err = proc.communicate(image.tostring())
if proc.returncode: err = "\n".join(["[MoviePy] Running : %s\n" % cmd, "WARNING: this command returned an error:", err.decode('utf8')]) raise IOError(err)
del proc 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176from jaa import JaaCorefrom roop.utilities import get_device
from typing import Any
version = "4.0.0"
class ChainImgProcessor(JaaCore):
def __init__(self): JaaCore.__init__(self)
self.processors:dict = { }
self.processors_objects:dict[str,list[ChainImgPlugin]] = {}
self.default_chain = "" self.init_on_start = ""
self.inited_processors = []
self.is_demo_row_render = False
def process_plugin_manifest(self, modname, manifest): # adding processors from plugin manifest if "img_processor" in manifest: # process commands for cmd in manifest["img_processor"].keys(): self.processors[cmd] = manifest["img_processor"][cmd]
return manifest
def init_with_plugins(self): self.init_plugins(["core"]) self.display_init_info()
#self.init_translator_engine(self.default_translator) init_on_start_arr = self.init_on_start.split(",") for proc_id in init_on_start_arr: self.init_processor(proc_id)
def run_chain(self, img, params:dict[str,Any] = None, chain:str = None, thread_index:int = 0): if chain is None: chain = self.default_chain if params is None: params = {} params["_thread_index"] = thread_index chain_ar = chain.split(",") # init all not inited processors first for proc_id in chain_ar: if proc_id != "": if not proc_id in self.inited_processors: self.init_processor(proc_id)
# run processing if self.is_demo_row_render: import cv2 import numpy as np height, width, channels = img.shape img_blank = np.zeros((height+30, width*(1+len(chain_ar)), 3), dtype=np.uint8) img_blank.fill(255)
y = 30 x = 0 img_blank[y:y + height, x:x + width] = img
# Set the font scale and thickness font_scale = 1 thickness = 2
# Set the font face to a monospace font font_face = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img_blank, "original", (x+4, y-7), font_face, font_scale, (0, 0, 0), thickness)
i = 0 for proc_id in chain_ar: i += 1 if proc_id != "": #img = self.processors[proc_id][1](self, img, params) # params can be modified inside y = 30 img = self.processors_objects[proc_id][thread_index].process(img,params) if self.is_demo_row_render: x = width*i img_blank[y:y + height, x:x + width] = img cv2.putText(img_blank, proc_id, (x + 4, y - 7), font_face, font_scale, (0, 0, 0), thickness)
if self.is_demo_row_render: return img_blank, params
return img, params
# ---------------- init translation stuff ---------------- def fill_processors_for_thread_chains(self, threads:int = 1, chain:str = None): if chain is None: chain = self.default_chain
chain_ar = chain.split(",") # init all not initialized processors first for processor_id in chain_ar: if processor_id != "": if self.processors_objects.get(processor_id) is None: self.processors_objects[processor_id] = [] while len(self.processors_objects[processor_id]) < threads: self.add_processor_to_list(processor_id)
def add_processor_to_list(self, processor_id: str): obj = self.processors[processor_id](self) obj.init_plugin() if self.processors_objects.get(processor_id) is None: self.processors_objects[processor_id] = [] self.processors_objects[processor_id].append(obj) def init_processor(self, processor_id: str): if processor_id == "": # blank line case return
if processor_id in self.inited_processors: return
try: if self.verbose: self.print_blue("TRY: init processor plugin '{0}'...".format(processor_id)) self.add_processor_to_list(processor_id) self.inited_processors.append(processor_id) if self.verbose: self.print_blue("SUCCESS: '{0}' initialized!".format(processor_id))
except Exception as e: self.print_error("Error init processor plugin {0}...".format(processor_id), e)
# ------------ formatting stuff ------------------- def display_init_info(self): if self.verbose: print("ChainImgProcessor v{0}:".format(version)) self.format_print_key_list("processors:", self.processors.keys())
def format_print_key_list(self, key:str, value:list): print(key+": ".join(value))
def print_error(self,err_txt,e:Exception = None): print(err_txt,"red") # if e != None: # cprint(e,"red") import traceback traceback.print_exc()
def print_red(self,txt): print(txt)
def print_blue(self, txt): print(txt)
class ChainImgPlugin:
device = 'cpu'
def __init__(self, core: ChainImgProcessor): self.core = core self.device = get_device()
def init_plugin(self): # here you can init something. Called once pass def process(self, img, params:dict): # process img. Called multiple return img
_img_processor:ChainImgProcessor = Nonedef get_single_image_processor() -> ChainImgProcessor: global _img_processor if _img_processor is None: _img_processor = ChainImgProcessor() _img_processor.init_with_plugins() return _img_processor123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132import roop.globals
from threading import Threadfrom chain_img_processor import ChainImgProcessor
class ThreadWithReturnValue(Thread):
def __init__(self, group=None, target=None, name=None, args=(), kwargs={}, Verbose=None): Thread.__init__(self, group, target, name, args, kwargs) self._return = None
def run(self): if self._target is not None: self._return = self._target(*self._args, **self._kwargs)
def join(self, *args): Thread.join(self, *args) return self._return
# in betaclass ChainVideoProcessor(ChainImgProcessor): def __init__(self): ChainImgProcessor.__init__(self)
self.video_save_codec = "libx264" self.video_save_crf = 14
def init_with_plugins(self): self.init_plugins(["core","core_video"]) self.display_init_info()
init_on_start_arr = self.init_on_start.split(",") for proc_id in init_on_start_arr: self.init_processor(proc_id)
def run_video_chain(self, source_video, target_video, fps, threads:int = 1, chain = None, params_frame_gen_func = None, video_audio = None): import cv2 from tqdm import tqdm from chain_img_processor.ffmpeg_writer import FFMPEG_VideoWriter # ffmpeg install needed
cap = cv2.VideoCapture(source_video) # width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
# first frame do manually - because upscale may happen, we need to estimate width/height ret, frame = cap.read() if params_frame_gen_func is not None: params = params_frame_gen_func(self, frame) else: params = {} params["original_frame"] = frame frame_processed, params = self.run_chain(frame,params,chain) height, width, channels = frame_processed.shape
self.fill_processors_for_thread_chains(threads,chain) #print(self.processors_objects) #import threading #locks:list[threading.Lock] = [] locks: list[bool] = [] for i in range(threads): #locks.append(threading.Lock()) locks.append(False)
temp = [] with FFMPEG_VideoWriter(target_video, (width, height), fps, codec=roop.globals.video_encoder, crf=roop.globals.video_quality, audiofile=video_audio) as output_video_ff: with tqdm(total=frame_count, desc='Processing', unit="frame", dynamic_ncols=True, bar_format='{l_bar}{bar}| {n_fmt}/{total_fmt} [{elapsed}<{remaining}, {rate_fmt}{postfix}]') as progress:
# do first frame output_video_ff.write_frame(frame_processed) progress.update(1) # cnt_frames = 0
# do rest frames while True: # getting frame ret, frame = cap.read()
if not ret: break cnt_frames+=1 thread_ind = cnt_frames % threads # we are having an array of length %gpu_threads%, running in parallel # so if array is equal or longer than gpu threads, waiting #while len(temp) >= threads: while locks[thread_ind]: #print('WAIT', thread_ind) # we are order dependent, so we are forced to wait for first element to finish. When finished removing thread from the list frame_processed, params = temp.pop(0).join() locks[params["_thread_index"]] = False #print('OFF',cnt_frames,locks[params["_thread_index"]],locks) # writing into output output_video_ff.write_frame(frame_processed) # updating the status progress.update(1)
# calc params for frame if params_frame_gen_func is not None: params = params_frame_gen_func(self,frame) else: params = {}
# adding new frame to the list and starting it locks[thread_ind] = True #print('ON', cnt_frames, thread_ind, locks) params["original_frame"] = frame temp.append( ThreadWithReturnValue(target=self.run_chain, args=(frame, params, chain, thread_ind))) temp[-1].start()
while len(temp) > 0: # we are order dependent, so we are forced to wait for first element to finish. When finished removing thread from the list frame_processed, params = temp.pop(0).join() locks[params["_thread_index"]] = False # writing into output output_video_ff.write_frame(frame_processed)
progress.update(1)
#print("FINAL", locks)
_video_processor:ChainVideoProcessor = Nonedef get_single_video_processor() -> ChainVideoProcessor: global _video_processor if _video_processor is None: _video_processor = ChainVideoProcessor() _video_processor.init_with_plugins() return _video_processor1from .clip import *123version https://git-lfs.github.com/spec/v1oid sha256:924691ac288e54409236115652ad4aa250f48203de50a9e4722a6ecd48d6804asize 1356917123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245import hashlibimport osimport urllibimport warningsfrom typing import Any, Union, Listfrom pkg_resources import packaging
import torchfrom PIL import Imagefrom torchvision.transforms import Compose, Resize, CenterCrop, ToTensor, Normalizefrom tqdm import tqdm
from .model import build_modelfrom .simple_tokenizer import SimpleTokenizer as _Tokenizer
try: from torchvision.transforms import InterpolationMode BICUBIC = InterpolationMode.BICUBICexcept ImportError: BICUBIC = Image.BICUBIC
if packaging.version.parse(torch.__version__) < packaging.version.parse("1.7.1"): warnings.warn("PyTorch version 1.7.1 or higher is recommended")
__all__ = ["available_models", "load", "tokenize"]_tokenizer = _Tokenizer()
_MODELS = { "RN50": "https://openaipublic.azureedge.net/clip/models/afeb0e10f9e5a86da6080e35cf09123aca3b358a0c3e3b6c78a7b63bc04b6762/RN50.pt", "RN101": "https://openaipublic.azureedge.net/clip/models/8fa8567bab74a42d41c5915025a8e4538c3bdbe8804a470a72f30b0d94fab599/RN101.pt", "RN50x4": "https://openaipublic.azureedge.net/clip/models/7e526bd135e493cef0776de27d5f42653e6b4c8bf9e0f653bb11773263205fdd/RN50x4.pt", "RN50x16": "https://openaipublic.azureedge.net/clip/models/52378b407f34354e150460fe41077663dd5b39c54cd0bfd2b27167a4a06ec9aa/RN50x16.pt", "RN50x64": "https://openaipublic.azureedge.net/clip/models/be1cfb55d75a9666199fb2206c106743da0f6468c9d327f3e0d0a543a9919d9c/RN50x64.pt", "ViT-B/32": "https://openaipublic.azureedge.net/clip/models/40d365715913c9da98579312b702a82c18be219cc2a73407c4526f58eba950af/ViT-B-32.pt", "ViT-B/16": "https://openaipublic.azureedge.net/clip/models/5806e77cd80f8b59890b7e101eabd078d9fb84e6937f9e85e4ecb61988df416f/ViT-B-16.pt", "ViT-L/14": "https://openaipublic.azureedge.net/clip/models/b8cca3fd41ae0c99ba7e8951adf17d267cdb84cd88be6f7c2e0eca1737a03836/ViT-L-14.pt", "ViT-L/14@336px": "https://openaipublic.azureedge.net/clip/models/3035c92b350959924f9f00213499208652fc7ea050643e8b385c2dac08641f02/ViT-L-14-336px.pt",}
def _download(url: str, root: str): os.makedirs(root, exist_ok=True) filename = os.path.basename(url)
expected_sha256 = url.split("/")[-2] download_target = os.path.join(root, filename)
if os.path.exists(download_target) and not os.path.isfile(download_target): raise RuntimeError(f"{download_target} exists and is not a regular file")
if os.path.isfile(download_target): if hashlib.sha256(open(download_target, "rb").read()).hexdigest() == expected_sha256: return download_target else: warnings.warn(f"{download_target} exists, but the SHA256 checksum does not match; re-downloading the file")
with urllib.request.urlopen(url) as source, open(download_target, "wb") as output: with tqdm(total=int(source.info().get("Content-Length")), ncols=80, unit='iB', unit_scale=True, unit_divisor=1024) as loop: while True: buffer = source.read(8192) if not buffer: break
output.write(buffer) loop.update(len(buffer))
if hashlib.sha256(open(download_target, "rb").read()).hexdigest() != expected_sha256: raise RuntimeError("Model has been downloaded but the SHA256 checksum does not not match")
return download_target
def _convert_image_to_rgb(image): return image.convert("RGB")
def _transform(n_px): return Compose([ Resize(n_px, interpolation=BICUBIC), CenterCrop(n_px), _convert_image_to_rgb, ToTensor(), Normalize((0.48145466, 0.4578275, 0.40821073), (0.26862954, 0.26130258, 0.27577711)), ])
def available_models() -> List[str]: """Returns the names of available CLIP models""" return list(_MODELS.keys())
def load(name: str, device: Union[str, torch.device] = "cuda" if torch.cuda.is_available() else "cpu", jit: bool = False, download_root: str = None): """Load a CLIP model
Parameters ---------- name : str A model name listed by `clip.available_models()`, or the path to a model checkpoint containing the state_dict
device : Union[str, torch.device] The device to put the loaded model
jit : bool Whether to load the optimized JIT model or more hackable non-JIT model (default).
download_root: str path to download the model files; by default, it uses "~/.cache/clip"
Returns ------- model : torch.nn.Module The CLIP model
preprocess : Callable[[PIL.Image], torch.Tensor] A torchvision transform that converts a PIL image into a tensor that the returned model can take as its input """ if name in _MODELS: model_path = _download(_MODELS[name], download_root or os.path.expanduser("~/.cache/clip")) elif os.path.isfile(name): model_path = name else: raise RuntimeError(f"Model {name} not found; available models = {available_models()}")
with open(model_path, 'rb') as opened_file: try: # loading JIT archive model = torch.jit.load(opened_file, map_location=device if jit else "cpu").eval() state_dict = None except RuntimeError: # loading saved state dict if jit: warnings.warn(f"File {model_path} is not a JIT archive. Loading as a state dict instead") jit = False state_dict = torch.load(opened_file, map_location="cpu")
if not jit: model = build_model(state_dict or model.state_dict()).to(device) if str(device) == "cpu": model.float() return model, _transform(model.visual.input_resolution)
# patch the device names device_holder = torch.jit.trace(lambda: torch.ones([]).to(torch.device(device)), example_inputs=[]) device_node = [n for n in device_holder.graph.findAllNodes("prim::Constant") if "Device" in repr(n)][-1]
def _node_get(node: torch._C.Node, key: str): """Gets attributes of a node which is polymorphic over return type. From https://github.com/pytorch/pytorch/pull/82628 """ sel = node.kindOf(key) return getattr(node, sel)(key)
def patch_device(module): try: graphs = [module.graph] if hasattr(module, "graph") else [] except RuntimeError: graphs = []
if hasattr(module, "forward1"): graphs.append(module.forward1.graph)
for graph in graphs: for node in graph.findAllNodes("prim::Constant"): if "value" in node.attributeNames() and str(_node_get(node, "value")).startswith("cuda"): node.copyAttributes(device_node)
model.apply(patch_device) patch_device(model.encode_image) patch_device(model.encode_text)
# patch dtype to float32 on CPU if str(device) == "cpu": float_holder = torch.jit.trace(lambda: torch.ones([]).float(), example_inputs=[]) float_input = list(float_holder.graph.findNode("aten::to").inputs())[1] float_node = float_input.node()
def patch_float(module): try: graphs = [module.graph] if hasattr(module, "graph") else [] except RuntimeError: graphs = []
if hasattr(module, "forward1"): graphs.append(module.forward1.graph)
for graph in graphs: for node in graph.findAllNodes("aten::to"): inputs = list(node.inputs()) for i in [1, 2]: # dtype can be the second or third argument to aten::to() if _node_get(inputs[i].node(), "value") == 5: inputs[i].node().copyAttributes(float_node)
model.apply(patch_float) patch_float(model.encode_image) patch_float(model.encode_text)
model.float()
return model, _transform(model.input_resolution.item())
def tokenize(texts: Union[str, List[str]], context_length: int = 77, truncate: bool = False) -> Union[torch.IntTensor, torch.LongTensor]: """ Returns the tokenized representation of given input string(s)
Parameters ---------- texts : Union[str, List[str]] An input string or a list of input strings to tokenize
context_length : int The context length to use; all CLIP models use 77 as the context length
truncate: bool Whether to truncate the text in case its encoding is longer than the context length
Returns ------- A two-dimensional tensor containing the resulting tokens, shape = [number of input strings, context_length]. We return LongTensor when torch version is <1.8.0, since older index_select requires indices to be long. """ if isinstance(texts, str): texts = [texts]
sot_token = _tokenizer.encoder["<|startoftext|>"] eot_token = _tokenizer.encoder["<|endoftext|>"] all_tokens = [[sot_token] + _tokenizer.encode(text) + [eot_token] for text in texts] if packaging.version.parse(torch.__version__) < packaging.version.parse("1.8.0"): result = torch.zeros(len(all_tokens), context_length, dtype=torch.long) else: result = torch.zeros(len(all_tokens), context_length, dtype=torch.int)
for i, tokens in enumerate(all_tokens): if len(tokens) > context_length: if truncate: tokens = tokens[:context_length] tokens[-1] = eot_token else: raise RuntimeError(f"Input {texts[i]} is too long for context length {context_length}") result[i, :len(tokens)] = torch.tensor(tokens)
return result123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538import mathfrom os.path import basename, dirname, join, isfileimport torchfrom torch import nnfrom torch.nn import functional as nnffrom torch.nn.modules.activation import ReLU
def get_prompt_list(prompt): if prompt == 'plain': return ['{}'] elif prompt == 'fixed': return ['a photo of a {}.'] elif prompt == 'shuffle': return ['a photo of a {}.', 'a photograph of a {}.', 'an image of a {}.', '{}.'] elif prompt == 'shuffle+': return ['a photo of a {}.', 'a photograph of a {}.', 'an image of a {}.', '{}.', 'a cropped photo of a {}.', 'a good photo of a {}.', 'a photo of one {}.', 'a bad photo of a {}.', 'a photo of the {}.'] else: raise ValueError('Invalid value for prompt')
def forward_multihead_attention(x, b, with_aff=False, attn_mask=None): """ Simplified version of multihead attention (taken from torch source code but without tons of if clauses). The mlp and layer norm come from CLIP. x: input. b: multihead attention module. """
x_ = b.ln_1(x) q, k, v = nnf.linear(x_, b.attn.in_proj_weight, b.attn.in_proj_bias).chunk(3, dim=-1) tgt_len, bsz, embed_dim = q.size()
head_dim = embed_dim // b.attn.num_heads scaling = float(head_dim) ** -0.5
q = q.contiguous().view(tgt_len, bsz * b.attn.num_heads, b.attn.head_dim).transpose(0, 1) k = k.contiguous().view(-1, bsz * b.attn.num_heads, b.attn.head_dim).transpose(0, 1) v = v.contiguous().view(-1, bsz * b.attn.num_heads, b.attn.head_dim).transpose(0, 1)
q = q * scaling
attn_output_weights = torch.bmm(q, k.transpose(1, 2)) # n_heads * batch_size, tokens^2, tokens^2 if attn_mask is not None:
attn_mask_type, attn_mask = attn_mask n_heads = attn_output_weights.size(0) // attn_mask.size(0) attn_mask = attn_mask.repeat(n_heads, 1) if attn_mask_type == 'cls_token': # the mask only affects similarities compared to the readout-token. attn_output_weights[:, 0, 1:] = attn_output_weights[:, 0, 1:] * attn_mask[None,...] # attn_output_weights[:, 0, 0] = 0*attn_output_weights[:, 0, 0]
if attn_mask_type == 'all': # print(attn_output_weights.shape, attn_mask[:, None].shape) attn_output_weights[:, 1:, 1:] = attn_output_weights[:, 1:, 1:] * attn_mask[:, None] attn_output_weights = torch.softmax(attn_output_weights, dim=-1)
attn_output = torch.bmm(attn_output_weights, v) attn_output = attn_output.transpose(0, 1).contiguous().view(tgt_len, bsz, embed_dim) attn_output = b.attn.out_proj(attn_output)
x = x + attn_output x = x + b.mlp(b.ln_2(x))
if with_aff: return x, attn_output_weights else: return x
class CLIPDenseBase(nn.Module):
def __init__(self, version, reduce_cond, reduce_dim, prompt, n_tokens): super().__init__()
import clip
# prec = torch.FloatTensor self.clip_model, _ = clip.load(version, device='cpu', jit=False) self.model = self.clip_model.visual
# if not None, scale conv weights such that we obtain n_tokens. self.n_tokens = n_tokens
for p in self.clip_model.parameters(): p.requires_grad_(False)
# conditional if reduce_cond is not None: self.reduce_cond = nn.Linear(512, reduce_cond) for p in self.reduce_cond.parameters(): p.requires_grad_(False) else: self.reduce_cond = None
self.film_mul = nn.Linear(512 if reduce_cond is None else reduce_cond, reduce_dim) self.film_add = nn.Linear(512 if reduce_cond is None else reduce_cond, reduce_dim) self.reduce = nn.Linear(768, reduce_dim)
self.prompt_list = get_prompt_list(prompt)
# precomputed prompts import pickle if isfile('precomputed_prompt_vectors.pickle'): precomp = pickle.load(open('precomputed_prompt_vectors.pickle', 'rb')) self.precomputed_prompts = {k: torch.from_numpy(v) for k, v in precomp.items()} else: self.precomputed_prompts = dict() def rescaled_pos_emb(self, new_size): assert len(new_size) == 2
a = self.model.positional_embedding[1:].T.view(1, 768, *self.token_shape) b = nnf.interpolate(a, new_size, mode='bicubic', align_corners=False).squeeze(0).view(768, new_size[0]*new_size[1]).T return torch.cat([self.model.positional_embedding[:1], b])
def visual_forward(self, x_inp, extract_layers=(), skip=False, mask=None):
with torch.no_grad():
inp_size = x_inp.shape[2:]
if self.n_tokens is not None: stride2 = x_inp.shape[2] // self.n_tokens conv_weight2 = nnf.interpolate(self.model.conv1.weight, (stride2, stride2), mode='bilinear', align_corners=True) x = nnf.conv2d(x_inp, conv_weight2, bias=self.model.conv1.bias, stride=stride2, dilation=self.model.conv1.dilation) else: x = self.model.conv1(x_inp) # shape = [*, width, grid, grid]
x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2] x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width]
x = torch.cat([self.model.class_embedding.to(x.dtype) + torch.zeros(x.shape[0], 1, x.shape[-1], dtype=x.dtype, device=x.device), x], dim=1) # shape = [*, grid ** 2 + 1, width]
standard_n_tokens = 50 if self.model.conv1.kernel_size[0] == 32 else 197
if x.shape[1] != standard_n_tokens: new_shape = int(math.sqrt(x.shape[1]-1)) x = x + self.rescaled_pos_emb((new_shape, new_shape)).to(x.dtype)[None,:,:] else: x = x + self.model.positional_embedding.to(x.dtype)
x = self.model.ln_pre(x)
x = x.permute(1, 0, 2) # NLD -> LND
activations, affinities = [], [] for i, res_block in enumerate(self.model.transformer.resblocks): if mask is not None: mask_layer, mask_type, mask_tensor = mask if mask_layer == i or mask_layer == 'all': # import ipdb; ipdb.set_trace() size = int(math.sqrt(x.shape[0] - 1)) attn_mask = (mask_type, nnf.interpolate(mask_tensor.unsqueeze(1).float(), (size, size)).view(mask_tensor.shape[0], size * size)) else: attn_mask = None else: attn_mask = None
x, aff_per_head = forward_multihead_attention(x, res_block, with_aff=True, attn_mask=attn_mask)
if i in extract_layers: affinities += [aff_per_head]
#if self.n_tokens is not None: # activations += [nnf.interpolate(x, inp_size, mode='bilinear', align_corners=True)] #else: activations += [x]
if len(extract_layers) > 0 and i == max(extract_layers) and skip: print('early skip') break x = x.permute(1, 0, 2) # LND -> NLD x = self.model.ln_post(x[:, 0, :])
if self.model.proj is not None: x = x @ self.model.proj
return x, activations, affinities
def sample_prompts(self, words, prompt_list=None):
prompt_list = prompt_list if prompt_list is not None else self.prompt_list
prompt_indices = torch.multinomial(torch.ones(len(prompt_list)), len(words), replacement=True) prompts = [prompt_list[i] for i in prompt_indices] return [promt.format(w) for promt, w in zip(prompts, words)]
def get_cond_vec(self, conditional, batch_size): # compute conditional from a single string if conditional is not None and type(conditional) == str: cond = self.compute_conditional(conditional) cond = cond.repeat(batch_size, 1)
# compute conditional from string list/tuple elif conditional is not None and type(conditional) in {list, tuple} and type(conditional[0]) == str: assert len(conditional) == batch_size cond = self.compute_conditional(conditional)
# use conditional directly elif conditional is not None and type(conditional) == torch.Tensor and conditional.ndim == 2: cond = conditional
# compute conditional from image elif conditional is not None and type(conditional) == torch.Tensor: with torch.no_grad(): cond, _, _ = self.visual_forward(conditional) else: raise ValueError('invalid conditional') return cond
def compute_conditional(self, conditional): import clip
dev = next(self.parameters()).device
if type(conditional) in {list, tuple}: text_tokens = clip.tokenize(conditional).to(dev) cond = self.clip_model.encode_text(text_tokens) else: if conditional in self.precomputed_prompts: cond = self.precomputed_prompts[conditional].float().to(dev) else: text_tokens = clip.tokenize([conditional]).to(dev) cond = self.clip_model.encode_text(text_tokens)[0] if self.shift_vector is not None: return cond + self.shift_vector else: return cond
def clip_load_untrained(version): assert version == 'ViT-B/16' from clip.model import CLIP from clip.clip import _MODELS, _download model = torch.jit.load(_download(_MODELS['ViT-B/16'])).eval() state_dict = model.state_dict()
vision_width = state_dict["visual.conv1.weight"].shape[0] vision_layers = len([k for k in state_dict.keys() if k.startswith("visual.") and k.endswith(".attn.in_proj_weight")]) vision_patch_size = state_dict["visual.conv1.weight"].shape[-1] grid_size = round((state_dict["visual.positional_embedding"].shape[0] - 1) ** 0.5) image_resolution = vision_patch_size * grid_size embed_dim = state_dict["text_projection"].shape[1] context_length = state_dict["positional_embedding"].shape[0] vocab_size = state_dict["token_embedding.weight"].shape[0] transformer_width = state_dict["ln_final.weight"].shape[0] transformer_heads = transformer_width // 64 transformer_layers = len(set(k.split(".")[2] for k in state_dict if k.startswith(f"transformer.resblocks")))
return CLIP(embed_dim, image_resolution, vision_layers, vision_width, vision_patch_size, context_length, vocab_size, transformer_width, transformer_heads, transformer_layers)
class CLIPDensePredT(CLIPDenseBase):
def __init__(self, version='ViT-B/32', extract_layers=(3, 6, 9), cond_layer=0, reduce_dim=128, n_heads=4, prompt='fixed', extra_blocks=0, reduce_cond=None, fix_shift=False, learn_trans_conv_only=False, limit_to_clip_only=False, upsample=False, add_calibration=False, rev_activations=False, trans_conv=None, n_tokens=None, complex_trans_conv=False): super().__init__(version, reduce_cond, reduce_dim, prompt, n_tokens) # device = 'cpu'
self.extract_layers = extract_layers self.cond_layer = cond_layer self.limit_to_clip_only = limit_to_clip_only self.process_cond = None self.rev_activations = rev_activations depth = len(extract_layers)
if add_calibration: self.calibration_conds = 1
self.upsample_proj = nn.Conv2d(reduce_dim, 1, kernel_size=1) if upsample else None
self.add_activation1 = True
self.version = version self.token_shape = {'ViT-B/32': (7, 7), 'ViT-B/16': (14, 14)}[version]
if fix_shift: # self.shift_vector = nn.Parameter(torch.load(join(dirname(basename(__file__)), 'clip_text_shift_vector.pth')), requires_grad=False) self.shift_vector = nn.Parameter(torch.load(join(dirname(basename(__file__)), 'shift_text_to_vis.pth')), requires_grad=False) # self.shift_vector = nn.Parameter(-1*torch.load(join(dirname(basename(__file__)), 'shift2.pth')), requires_grad=False) else: self.shift_vector = None
if trans_conv is None: trans_conv_ks = {'ViT-B/32': (32, 32), 'ViT-B/16': (16, 16)}[version] else: # explicitly define transposed conv kernel size trans_conv_ks = (trans_conv, trans_conv)
if not complex_trans_conv: self.trans_conv = nn.ConvTranspose2d(reduce_dim, 1, trans_conv_ks, stride=trans_conv_ks) else: assert trans_conv_ks[0] == trans_conv_ks[1]
tp_kernels = (trans_conv_ks[0] // 4, trans_conv_ks[0] // 4)
self.trans_conv = nn.Sequential( nn.Conv2d(reduce_dim, reduce_dim, kernel_size=3, padding=1), nn.ReLU(), nn.ConvTranspose2d(reduce_dim, reduce_dim // 2, kernel_size=tp_kernels[0], stride=tp_kernels[0]), nn.ReLU(), nn.ConvTranspose2d(reduce_dim // 2, 1, kernel_size=tp_kernels[1], stride=tp_kernels[1]), )
# self.trans_conv = nn.ConvTranspose2d(reduce_dim, 1, trans_conv_ks, stride=trans_conv_ks) assert len(self.extract_layers) == depth
self.reduces = nn.ModuleList([nn.Linear(768, reduce_dim) for _ in range(depth)]) self.blocks = nn.ModuleList([nn.TransformerEncoderLayer(d_model=reduce_dim, nhead=n_heads) for _ in range(len(self.extract_layers))]) self.extra_blocks = nn.ModuleList([nn.TransformerEncoderLayer(d_model=reduce_dim, nhead=n_heads) for _ in range(extra_blocks)]) # refinement and trans conv
if learn_trans_conv_only: for p in self.parameters(): p.requires_grad_(False) for p in self.trans_conv.parameters(): p.requires_grad_(True)
self.prompt_list = get_prompt_list(prompt)
def forward(self, inp_image, conditional=None, return_features=False, mask=None):
assert type(return_features) == bool
inp_image = inp_image.to(self.model.positional_embedding.device)
if mask is not None: raise ValueError('mask not supported')
# x_inp = normalize(inp_image) x_inp = inp_image
bs, dev = inp_image.shape[0], x_inp.device
cond = self.get_cond_vec(conditional, bs)
visual_q, activations, _ = self.visual_forward(x_inp, extract_layers=[0] + list(self.extract_layers))
activation1 = activations[0] activations = activations[1:]
_activations = activations[::-1] if not self.rev_activations else activations
a = None for i, (activation, block, reduce) in enumerate(zip(_activations, self.blocks, self.reduces)): if a is not None: a = reduce(activation) + a else: a = reduce(activation)
if i == self.cond_layer: if self.reduce_cond is not None: cond = self.reduce_cond(cond) a = self.film_mul(cond) * a + self.film_add(cond)
a = block(a)
for block in self.extra_blocks: a = a + block(a)
a = a[1:].permute(1, 2, 0) # rm cls token and -> BS, Feats, Tokens
size = int(math.sqrt(a.shape[2]))
a = a.view(bs, a.shape[1], size, size)
a = self.trans_conv(a)
if self.n_tokens is not None: a = nnf.interpolate(a, x_inp.shape[2:], mode='bilinear', align_corners=True)
if self.upsample_proj is not None: a = self.upsample_proj(a) a = nnf.interpolate(a, x_inp.shape[2:], mode='bilinear')
if return_features: return a, visual_q, cond, [activation1] + activations else: return a,
class CLIPDensePredTMasked(CLIPDensePredT):
def __init__(self, version='ViT-B/32', extract_layers=(3, 6, 9), cond_layer=0, reduce_dim=128, n_heads=4, prompt='fixed', extra_blocks=0, reduce_cond=None, fix_shift=False, learn_trans_conv_only=False, refine=None, limit_to_clip_only=False, upsample=False, add_calibration=False, n_tokens=None):
super().__init__(version=version, extract_layers=extract_layers, cond_layer=cond_layer, reduce_dim=reduce_dim, n_heads=n_heads, prompt=prompt, extra_blocks=extra_blocks, reduce_cond=reduce_cond, fix_shift=fix_shift, learn_trans_conv_only=learn_trans_conv_only, limit_to_clip_only=limit_to_clip_only, upsample=upsample, add_calibration=add_calibration, n_tokens=n_tokens)
def visual_forward_masked(self, img_s, seg_s): return super().visual_forward(img_s, mask=('all', 'cls_token', seg_s))
def forward(self, img_q, cond_or_img_s, seg_s=None, return_features=False):
if seg_s is None: cond = cond_or_img_s else: img_s = cond_or_img_s
with torch.no_grad(): cond, _, _ = self.visual_forward_masked(img_s, seg_s)
return super().forward(img_q, cond, return_features=return_features)
class CLIPDenseBaseline(CLIPDenseBase):
def __init__(self, version='ViT-B/32', cond_layer=0, extract_layer=9, reduce_dim=128, reduce2_dim=None, prompt='fixed', reduce_cond=None, limit_to_clip_only=False, n_tokens=None): super().__init__(version, reduce_cond, reduce_dim, prompt, n_tokens) device = 'cpu'
# self.cond_layer = cond_layer self.extract_layer = extract_layer self.limit_to_clip_only = limit_to_clip_only self.shift_vector = None
self.token_shape = {'ViT-B/32': (7, 7), 'ViT-B/16': (14, 14)}[version] assert reduce2_dim is not None
self.reduce2 = nn.Sequential( nn.Linear(reduce_dim, reduce2_dim), nn.ReLU(), nn.Linear(reduce2_dim, reduce_dim) ) trans_conv_ks = {'ViT-B/32': (32, 32), 'ViT-B/16': (16, 16)}[version] self.trans_conv = nn.ConvTranspose2d(reduce_dim, 1, trans_conv_ks, stride=trans_conv_ks)
def forward(self, inp_image, conditional=None, return_features=False):
inp_image = inp_image.to(self.model.positional_embedding.device)
# x_inp = normalize(inp_image) x_inp = inp_image
bs, dev = inp_image.shape[0], x_inp.device
cond = self.get_cond_vec(conditional, bs)
visual_q, activations, affinities = self.visual_forward(x_inp, extract_layers=[self.extract_layer])
a = activations[0] a = self.reduce(a) a = self.film_mul(cond) * a + self.film_add(cond)
if self.reduce2 is not None: a = self.reduce2(a)
# the original model would execute a transformer block here
a = a[1:].permute(1, 2, 0) # rm cls token and -> BS, Feats, Tokens
size = int(math.sqrt(a.shape[2]))
a = a.view(bs, a.shape[1], size, size) a = self.trans_conv(a)
if return_features: return a, visual_q, cond, activations else: return a,
class CLIPSegMultiLabel(nn.Module):
def __init__(self, model) -> None: super().__init__()
from third_party.JoEm.data_loader import get_seen_idx, get_unseen_idx, VOC
self.pascal_classes = VOC
from clip.clipseg import CLIPDensePredT from general_utils import load_model # self.clipseg = load_model('rd64-vit16-neg0.2-phrasecut', strict=False) self.clipseg = load_model(model, strict=False) self.clipseg.eval()
def forward(self, x):
bs = x.shape[0] out = torch.ones(21, bs, 352, 352).to(x.device) * -10
for class_id, class_name in enumerate(self.pascal_classes): fac = 3 if class_name == 'background' else 1
with torch.no_grad(): pred = torch.sigmoid(self.clipseg(x, class_name)[0][:,0]) * fac
out[class_id] += pred
out = out.permute(1, 0, 2, 3)
return out
# construct output tensor 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436from collections import OrderedDictfrom typing import Tuple, Union
import numpy as npimport torchimport torch.nn.functional as Ffrom torch import nn
class Bottleneck(nn.Module): expansion = 4
def __init__(self, inplanes, planes, stride=1): super().__init__()
# all conv layers have stride 1. an avgpool is performed after the second convolution when stride > 1 self.conv1 = nn.Conv2d(inplanes, planes, 1, bias=False) self.bn1 = nn.BatchNorm2d(planes) self.relu1 = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(planes, planes, 3, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(planes) self.relu2 = nn.ReLU(inplace=True)
self.avgpool = nn.AvgPool2d(stride) if stride > 1 else nn.Identity()
self.conv3 = nn.Conv2d(planes, planes * self.expansion, 1, bias=False) self.bn3 = nn.BatchNorm2d(planes * self.expansion) self.relu3 = nn.ReLU(inplace=True)
self.downsample = None self.stride = stride
if stride > 1 or inplanes != planes * Bottleneck.expansion: # downsampling layer is prepended with an avgpool, and the subsequent convolution has stride 1 self.downsample = nn.Sequential(OrderedDict([ ("-1", nn.AvgPool2d(stride)), ("0", nn.Conv2d(inplanes, planes * self.expansion, 1, stride=1, bias=False)), ("1", nn.BatchNorm2d(planes * self.expansion)) ]))
def forward(self, x: torch.Tensor): identity = x
out = self.relu1(self.bn1(self.conv1(x))) out = self.relu2(self.bn2(self.conv2(out))) out = self.avgpool(out) out = self.bn3(self.conv3(out))
if self.downsample is not None: identity = self.downsample(x)
out += identity out = self.relu3(out) return out
class AttentionPool2d(nn.Module): def __init__(self, spacial_dim: int, embed_dim: int, num_heads: int, output_dim: int = None): super().__init__() self.positional_embedding = nn.Parameter(torch.randn(spacial_dim ** 2 + 1, embed_dim) / embed_dim ** 0.5) self.k_proj = nn.Linear(embed_dim, embed_dim) self.q_proj = nn.Linear(embed_dim, embed_dim) self.v_proj = nn.Linear(embed_dim, embed_dim) self.c_proj = nn.Linear(embed_dim, output_dim or embed_dim) self.num_heads = num_heads
def forward(self, x): x = x.flatten(start_dim=2).permute(2, 0, 1) # NCHW -> (HW)NC x = torch.cat([x.mean(dim=0, keepdim=True), x], dim=0) # (HW+1)NC x = x + self.positional_embedding[:, None, :].to(x.dtype) # (HW+1)NC x, _ = F.multi_head_attention_forward( query=x[:1], key=x, value=x, embed_dim_to_check=x.shape[-1], num_heads=self.num_heads, q_proj_weight=self.q_proj.weight, k_proj_weight=self.k_proj.weight, v_proj_weight=self.v_proj.weight, in_proj_weight=None, in_proj_bias=torch.cat([self.q_proj.bias, self.k_proj.bias, self.v_proj.bias]), bias_k=None, bias_v=None, add_zero_attn=False, dropout_p=0, out_proj_weight=self.c_proj.weight, out_proj_bias=self.c_proj.bias, use_separate_proj_weight=True, training=self.training, need_weights=False ) return x.squeeze(0)
class ModifiedResNet(nn.Module): """ A ResNet class that is similar to torchvision's but contains the following changes: - There are now 3 "stem" convolutions as opposed to 1, with an average pool instead of a max pool. - Performs anti-aliasing strided convolutions, where an avgpool is prepended to convolutions with stride > 1 - The final pooling layer is a QKV attention instead of an average pool """
def __init__(self, layers, output_dim, heads, input_resolution=224, width=64): super().__init__() self.output_dim = output_dim self.input_resolution = input_resolution
# the 3-layer stem self.conv1 = nn.Conv2d(3, width // 2, kernel_size=3, stride=2, padding=1, bias=False) self.bn1 = nn.BatchNorm2d(width // 2) self.relu1 = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(width // 2, width // 2, kernel_size=3, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(width // 2) self.relu2 = nn.ReLU(inplace=True) self.conv3 = nn.Conv2d(width // 2, width, kernel_size=3, padding=1, bias=False) self.bn3 = nn.BatchNorm2d(width) self.relu3 = nn.ReLU(inplace=True) self.avgpool = nn.AvgPool2d(2)
# residual layers self._inplanes = width # this is a *mutable* variable used during construction self.layer1 = self._make_layer(width, layers[0]) self.layer2 = self._make_layer(width * 2, layers[1], stride=2) self.layer3 = self._make_layer(width * 4, layers[2], stride=2) self.layer4 = self._make_layer(width * 8, layers[3], stride=2)
embed_dim = width * 32 # the ResNet feature dimension self.attnpool = AttentionPool2d(input_resolution // 32, embed_dim, heads, output_dim)
def _make_layer(self, planes, blocks, stride=1): layers = [Bottleneck(self._inplanes, planes, stride)]
self._inplanes = planes * Bottleneck.expansion for _ in range(1, blocks): layers.append(Bottleneck(self._inplanes, planes))
return nn.Sequential(*layers)
def forward(self, x): def stem(x): x = self.relu1(self.bn1(self.conv1(x))) x = self.relu2(self.bn2(self.conv2(x))) x = self.relu3(self.bn3(self.conv3(x))) x = self.avgpool(x) return x
x = x.type(self.conv1.weight.dtype) x = stem(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.attnpool(x)
return x
class LayerNorm(nn.LayerNorm): """Subclass torch's LayerNorm to handle fp16."""
def forward(self, x: torch.Tensor): orig_type = x.dtype ret = super().forward(x.type(torch.float32)) return ret.type(orig_type)
class QuickGELU(nn.Module): def forward(self, x: torch.Tensor): return x * torch.sigmoid(1.702 * x)
class ResidualAttentionBlock(nn.Module): def __init__(self, d_model: int, n_head: int, attn_mask: torch.Tensor = None): super().__init__()
self.attn = nn.MultiheadAttention(d_model, n_head) self.ln_1 = LayerNorm(d_model) self.mlp = nn.Sequential(OrderedDict([ ("c_fc", nn.Linear(d_model, d_model * 4)), ("gelu", QuickGELU()), ("c_proj", nn.Linear(d_model * 4, d_model)) ])) self.ln_2 = LayerNorm(d_model) self.attn_mask = attn_mask
def attention(self, x: torch.Tensor): self.attn_mask = self.attn_mask.to(dtype=x.dtype, device=x.device) if self.attn_mask is not None else None return self.attn(x, x, x, need_weights=False, attn_mask=self.attn_mask)[0]
def forward(self, x: torch.Tensor): x = x + self.attention(self.ln_1(x)) x = x + self.mlp(self.ln_2(x)) return x
class Transformer(nn.Module): def __init__(self, width: int, layers: int, heads: int, attn_mask: torch.Tensor = None): super().__init__() self.width = width self.layers = layers self.resblocks = nn.Sequential(*[ResidualAttentionBlock(width, heads, attn_mask) for _ in range(layers)])
def forward(self, x: torch.Tensor): return self.resblocks(x)
class VisionTransformer(nn.Module): def __init__(self, input_resolution: int, patch_size: int, width: int, layers: int, heads: int, output_dim: int): super().__init__() self.input_resolution = input_resolution self.output_dim = output_dim self.conv1 = nn.Conv2d(in_channels=3, out_channels=width, kernel_size=patch_size, stride=patch_size, bias=False)
scale = width ** -0.5 self.class_embedding = nn.Parameter(scale * torch.randn(width)) self.positional_embedding = nn.Parameter(scale * torch.randn((input_resolution // patch_size) ** 2 + 1, width)) self.ln_pre = LayerNorm(width)
self.transformer = Transformer(width, layers, heads)
self.ln_post = LayerNorm(width) self.proj = nn.Parameter(scale * torch.randn(width, output_dim))
def forward(self, x: torch.Tensor): x = self.conv1(x) # shape = [*, width, grid, grid] x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2] x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width] x = torch.cat([self.class_embedding.to(x.dtype) + torch.zeros(x.shape[0], 1, x.shape[-1], dtype=x.dtype, device=x.device), x], dim=1) # shape = [*, grid ** 2 + 1, width] x = x + self.positional_embedding.to(x.dtype) x = self.ln_pre(x)
x = x.permute(1, 0, 2) # NLD -> LND x = self.transformer(x) x = x.permute(1, 0, 2) # LND -> NLD
x = self.ln_post(x[:, 0, :])
if self.proj is not None: x = x @ self.proj
return x
class CLIP(nn.Module): def __init__(self, embed_dim: int, # vision image_resolution: int, vision_layers: Union[Tuple[int, int, int, int], int], vision_width: int, vision_patch_size: int, # text context_length: int, vocab_size: int, transformer_width: int, transformer_heads: int, transformer_layers: int ): super().__init__()
self.context_length = context_length
if isinstance(vision_layers, (tuple, list)): vision_heads = vision_width * 32 // 64 self.visual = ModifiedResNet( layers=vision_layers, output_dim=embed_dim, heads=vision_heads, input_resolution=image_resolution, width=vision_width ) else: vision_heads = vision_width // 64 self.visual = VisionTransformer( input_resolution=image_resolution, patch_size=vision_patch_size, width=vision_width, layers=vision_layers, heads=vision_heads, output_dim=embed_dim )
self.transformer = Transformer( width=transformer_width, layers=transformer_layers, heads=transformer_heads, attn_mask=self.build_attention_mask() )
self.vocab_size = vocab_size self.token_embedding = nn.Embedding(vocab_size, transformer_width) self.positional_embedding = nn.Parameter(torch.empty(self.context_length, transformer_width)) self.ln_final = LayerNorm(transformer_width)
self.text_projection = nn.Parameter(torch.empty(transformer_width, embed_dim)) self.logit_scale = nn.Parameter(torch.ones([]) * np.log(1 / 0.07))
self.initialize_parameters()
def initialize_parameters(self): nn.init.normal_(self.token_embedding.weight, std=0.02) nn.init.normal_(self.positional_embedding, std=0.01)
if isinstance(self.visual, ModifiedResNet): if self.visual.attnpool is not None: std = self.visual.attnpool.c_proj.in_features ** -0.5 nn.init.normal_(self.visual.attnpool.q_proj.weight, std=std) nn.init.normal_(self.visual.attnpool.k_proj.weight, std=std) nn.init.normal_(self.visual.attnpool.v_proj.weight, std=std) nn.init.normal_(self.visual.attnpool.c_proj.weight, std=std)
for resnet_block in [self.visual.layer1, self.visual.layer2, self.visual.layer3, self.visual.layer4]: for name, param in resnet_block.named_parameters(): if name.endswith("bn3.weight"): nn.init.zeros_(param)
proj_std = (self.transformer.width ** -0.5) * ((2 * self.transformer.layers) ** -0.5) attn_std = self.transformer.width ** -0.5 fc_std = (2 * self.transformer.width) ** -0.5 for block in self.transformer.resblocks: nn.init.normal_(block.attn.in_proj_weight, std=attn_std) nn.init.normal_(block.attn.out_proj.weight, std=proj_std) nn.init.normal_(block.mlp.c_fc.weight, std=fc_std) nn.init.normal_(block.mlp.c_proj.weight, std=proj_std)
if self.text_projection is not None: nn.init.normal_(self.text_projection, std=self.transformer.width ** -0.5)
def build_attention_mask(self): # lazily create causal attention mask, with full attention between the vision tokens # pytorch uses additive attention mask; fill with -inf mask = torch.empty(self.context_length, self.context_length) mask.fill_(float("-inf")) mask.triu_(1) # zero out the lower diagonal return mask
@property def dtype(self): return self.visual.conv1.weight.dtype
def encode_image(self, image): return self.visual(image.type(self.dtype))
def encode_text(self, text): x = self.token_embedding(text).type(self.dtype) # [batch_size, n_ctx, d_model]
x = x + self.positional_embedding.type(self.dtype) x = x.permute(1, 0, 2) # NLD -> LND x = self.transformer(x) x = x.permute(1, 0, 2) # LND -> NLD x = self.ln_final(x).type(self.dtype)
# x.shape = [batch_size, n_ctx, transformer.width] # take features from the eot embedding (eot_token is the highest number in each sequence) x = x[torch.arange(x.shape[0]), text.argmax(dim=-1)] @ self.text_projection
return x
def forward(self, image, text): image_features = self.encode_image(image) text_features = self.encode_text(text)
# normalized features image_features = image_features / image_features.norm(dim=1, keepdim=True) text_features = text_features / text_features.norm(dim=1, keepdim=True)
# cosine similarity as logits logit_scale = self.logit_scale.exp() logits_per_image = logit_scale * image_features @ text_features.t() logits_per_text = logits_per_image.t()
# shape = [global_batch_size, global_batch_size] return logits_per_image, logits_per_text
def convert_weights(model: nn.Module): """Convert applicable model parameters to fp16"""
def _convert_weights_to_fp16(l): if isinstance(l, (nn.Conv1d, nn.Conv2d, nn.Linear)): l.weight.data = l.weight.data.half() if l.bias is not None: l.bias.data = l.bias.data.half()
if isinstance(l, nn.MultiheadAttention): for attr in [*[f"{s}_proj_weight" for s in ["in", "q", "k", "v"]], "in_proj_bias", "bias_k", "bias_v"]: tensor = getattr(l, attr) if tensor is not None: tensor.data = tensor.data.half()
for name in ["text_projection", "proj"]: if hasattr(l, name): attr = getattr(l, name) if attr is not None: attr.data = attr.data.half()
model.apply(_convert_weights_to_fp16)
def build_model(state_dict: dict): vit = "visual.proj" in state_dict
if vit: vision_width = state_dict["visual.conv1.weight"].shape[0] vision_layers = len([k for k in state_dict.keys() if k.startswith("visual.") and k.endswith(".attn.in_proj_weight")]) vision_patch_size = state_dict["visual.conv1.weight"].shape[-1] grid_size = round((state_dict["visual.positional_embedding"].shape[0] - 1) ** 0.5) image_resolution = vision_patch_size * grid_size else: counts: list = [len(set(k.split(".")[2] for k in state_dict if k.startswith(f"visual.layer{b}"))) for b in [1, 2, 3, 4]] vision_layers = tuple(counts) vision_width = state_dict["visual.layer1.0.conv1.weight"].shape[0] output_width = round((state_dict["visual.attnpool.positional_embedding"].shape[0] - 1) ** 0.5) vision_patch_size = None assert output_width ** 2 + 1 == state_dict["visual.attnpool.positional_embedding"].shape[0] image_resolution = output_width * 32
embed_dim = state_dict["text_projection"].shape[1] context_length = state_dict["positional_embedding"].shape[0] vocab_size = state_dict["token_embedding.weight"].shape[0] transformer_width = state_dict["ln_final.weight"].shape[0] transformer_heads = transformer_width // 64 transformer_layers = len(set(k.split(".")[2] for k in state_dict if k.startswith("transformer.resblocks")))
model = CLIP( embed_dim, image_resolution, vision_layers, vision_width, vision_patch_size, context_length, vocab_size, transformer_width, transformer_heads, transformer_layers )
for key in ["input_resolution", "context_length", "vocab_size"]: if key in state_dict: del state_dict[key]
convert_weights(model) model.load_state_dict(state_dict) return model.eval()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132import gzipimport htmlimport osfrom functools import lru_cache
import ftfyimport regex as re
@lru_cache()def default_bpe(): return os.path.join(os.path.dirname(os.path.abspath(__file__)), "bpe_simple_vocab_16e6.txt.gz")
@lru_cache()def bytes_to_unicode(): """ Returns list of utf-8 byte and a corresponding list of unicode strings. The reversible bpe codes work on unicode strings. This means you need a large # of unicode characters in your vocab if you want to avoid UNKs. When you're at something like a 10B token dataset you end up needing around 5K for decent coverage. This is a signficant percentage of your normal, say, 32K bpe vocab. To avoid that, we want lookup tables between utf-8 bytes and unicode strings. And avoids mapping to whitespace/control characters the bpe code barfs on. """ bs = list(range(ord("!"), ord("~")+1))+list(range(ord("¡"), ord("¬")+1))+list(range(ord("®"), ord("ÿ")+1)) cs = bs[:] n = 0 for b in range(2**8): if b not in bs: bs.append(b) cs.append(2**8+n) n += 1 cs = [chr(n) for n in cs] return dict(zip(bs, cs))
def get_pairs(word): """Return set of symbol pairs in a word. Word is represented as tuple of symbols (symbols being variable-length strings). """ pairs = set() prev_char = word[0] for char in word[1:]: pairs.add((prev_char, char)) prev_char = char return pairs
def basic_clean(text): text = ftfy.fix_text(text) text = html.unescape(html.unescape(text)) return text.strip()
def whitespace_clean(text): text = re.sub(r'\s+', ' ', text) text = text.strip() return text
class SimpleTokenizer(object): def __init__(self, bpe_path: str = default_bpe()): self.byte_encoder = bytes_to_unicode() self.byte_decoder = {v: k for k, v in self.byte_encoder.items()} merges = gzip.open(bpe_path).read().decode("utf-8").split('\n') merges = merges[1:49152-256-2+1] merges = [tuple(merge.split()) for merge in merges] vocab = list(bytes_to_unicode().values()) vocab = vocab + [v+'</w>' for v in vocab] for merge in merges: vocab.append(''.join(merge)) vocab.extend(['<|startoftext|>', '<|endoftext|>']) self.encoder = dict(zip(vocab, range(len(vocab)))) self.decoder = {v: k for k, v in self.encoder.items()} self.bpe_ranks = dict(zip(merges, range(len(merges)))) self.cache = {'<|startoftext|>': '<|startoftext|>', '<|endoftext|>': '<|endoftext|>'} self.pat = re.compile(r"""<\|startoftext\|>|<\|endoftext\|>|'s|'t|'re|'ve|'m|'ll|'d|[\p{L}]+|[\p{N}]|[^\s\p{L}\p{N}]+""", re.IGNORECASE)
def bpe(self, token): if token in self.cache: return self.cache[token] word = tuple(token[:-1]) + ( token[-1] + '</w>',) pairs = get_pairs(word)
if not pairs: return token+'</w>'
while True: bigram = min(pairs, key = lambda pair: self.bpe_ranks.get(pair, float('inf'))) if bigram not in self.bpe_ranks: break first, second = bigram new_word = [] i = 0 while i < len(word): try: j = word.index(first, i) new_word.extend(word[i:j]) i = j except: new_word.extend(word[i:]) break
if word[i] == first and i < len(word)-1 and word[i+1] == second: new_word.append(first+second) i += 2 else: new_word.append(word[i]) i += 1 new_word = tuple(new_word) word = new_word if len(word) == 1: break else: pairs = get_pairs(word) word = ' '.join(word) self.cache[token] = word return word
def encode(self, text): bpe_tokens = [] text = whitespace_clean(basic_clean(text)).lower() for token in re.findall(self.pat, text): token = ''.join(self.byte_encoder[b] for b in token.encode('utf-8')) bpe_tokens.extend(self.encoder[bpe_token] for bpe_token in self.bpe(token).split(' ')) return bpe_tokens
def decode(self, tokens): text = ''.join([self.decoder[token] for token in tokens]) text = bytearray([self.byte_decoder[c] for c in text]).decode('utf-8', errors="replace").replace('</w>', ' ') return text123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286import mathfrom posixpath import basename, dirname, join# import clipfrom clip.model import convert_weightsimport torchimport jsonfrom torch import nnfrom torch.nn import functional as nnffrom torch.nn.modules import activationfrom torch.nn.modules.activation import ReLUfrom torchvision import transforms
normalize = transforms.Normalize(mean=(0.48145466, 0.4578275, 0.40821073), std=(0.26862954, 0.26130258, 0.27577711))
from torchvision.models import ResNet
def process_prompts(conditional, prompt_list, conditional_map): # DEPRECATED # randomly sample a synonym words = [conditional_map[int(i)] for i in conditional] words = [syns[torch.multinomial(torch.ones(len(syns)), 1, replacement=True).item()] for syns in words] words = [w.replace('_', ' ') for w in words]
if prompt_list is not None: prompt_indices = torch.multinomial(torch.ones(len(prompt_list)), len(words), replacement=True) prompts = [prompt_list[i] for i in prompt_indices] else: prompts = ['a photo of {}'] * (len(words))
return [promt.format(w) for promt, w in zip(prompts, words)]
class VITDenseBase(nn.Module): def rescaled_pos_emb(self, new_size): assert len(new_size) == 2
a = self.model.positional_embedding[1:].T.view(1, 768, *self.token_shape) b = nnf.interpolate(a, new_size, mode='bicubic', align_corners=False).squeeze(0).view(768, new_size[0]*new_size[1]).T return torch.cat([self.model.positional_embedding[:1], b])
def visual_forward(self, x_inp, extract_layers=(), skip=False, mask=None): with torch.no_grad():
x_inp = nnf.interpolate(x_inp, (384, 384))
x = self.model.patch_embed(x_inp) cls_token = self.model.cls_token.expand(x.shape[0], -1, -1) # stole cls_tokens impl from Phil Wang, thanks if self.model.dist_token is None: x = torch.cat((cls_token, x), dim=1) else: x = torch.cat((cls_token, self.model.dist_token.expand(x.shape[0], -1, -1), x), dim=1) x = self.model.pos_drop(x + self.model.pos_embed)
activations = [] for i, block in enumerate(self.model.blocks): x = block(x)
if i in extract_layers: # permute to be compatible with CLIP activations += [x.permute(1,0,2)]
x = self.model.norm(x) x = self.model.head(self.model.pre_logits(x[:, 0]))
# again for CLIP compatibility # x = x.permute(1, 0, 2)
return x, activations, None
def sample_prompts(self, words, prompt_list=None):
prompt_list = prompt_list if prompt_list is not None else self.prompt_list
prompt_indices = torch.multinomial(torch.ones(len(prompt_list)), len(words), replacement=True) prompts = [prompt_list[i] for i in prompt_indices] return [promt.format(w) for promt, w in zip(prompts, words)]
def get_cond_vec(self, conditional, batch_size): # compute conditional from a single string if conditional is not None and type(conditional) == str: cond = self.compute_conditional(conditional) cond = cond.repeat(batch_size, 1)
# compute conditional from string list/tuple elif conditional is not None and type(conditional) in {list, tuple} and type(conditional[0]) == str: assert len(conditional) == batch_size cond = self.compute_conditional(conditional)
# use conditional directly elif conditional is not None and type(conditional) == torch.Tensor and conditional.ndim == 2: cond = conditional
# compute conditional from image elif conditional is not None and type(conditional) == torch.Tensor: with torch.no_grad(): cond, _, _ = self.visual_forward(conditional) else: raise ValueError('invalid conditional') return cond
def compute_conditional(self, conditional): import clip
dev = next(self.parameters()).device
if type(conditional) in {list, tuple}: text_tokens = clip.tokenize(conditional).to(dev) cond = self.clip_model.encode_text(text_tokens) else: if conditional in self.precomputed_prompts: cond = self.precomputed_prompts[conditional].float().to(dev) else: text_tokens = clip.tokenize([conditional]).to(dev) cond = self.clip_model.encode_text(text_tokens)[0] return cond
class VITDensePredT(VITDenseBase):
def __init__(self, extract_layers=(3, 6, 9), cond_layer=0, reduce_dim=128, n_heads=4, prompt='fixed', depth=3, extra_blocks=0, reduce_cond=None, fix_shift=False, learn_trans_conv_only=False, refine=None, limit_to_clip_only=False, upsample=False, add_calibration=False, process_cond=None, not_pretrained=False): super().__init__() # device = 'cpu'
self.extract_layers = extract_layers self.cond_layer = cond_layer self.limit_to_clip_only = limit_to_clip_only self.process_cond = None if add_calibration: self.calibration_conds = 1
self.upsample_proj = nn.Conv2d(reduce_dim, 1, kernel_size=1) if upsample else None
self.add_activation1 = True
import timm self.model = timm.create_model('vit_base_patch16_384', pretrained=True) self.model.head = nn.Linear(768, 512 if reduce_cond is None else reduce_cond)
for p in self.model.parameters(): p.requires_grad_(False)
import clip self.clip_model, _ = clip.load('ViT-B/16', device='cpu', jit=False) # del self.clip_model.visual self.token_shape = (14, 14)
# conditional if reduce_cond is not None: self.reduce_cond = nn.Linear(512, reduce_cond) for p in self.reduce_cond.parameters(): p.requires_grad_(False) else: self.reduce_cond = None
# self.film = AVAILABLE_BLOCKS['film'](512, 128) self.film_mul = nn.Linear(512 if reduce_cond is None else reduce_cond, reduce_dim) self.film_add = nn.Linear(512 if reduce_cond is None else reduce_cond, reduce_dim) # DEPRECATED # self.conditional_map = {c['id']: c['synonyms'] for c in json.load(open(cond_map))} assert len(self.extract_layers) == depth
self.reduces = nn.ModuleList([nn.Linear(768, reduce_dim) for _ in range(depth)]) self.blocks = nn.ModuleList([nn.TransformerEncoderLayer(d_model=reduce_dim, nhead=n_heads) for _ in range(len(self.extract_layers))]) self.extra_blocks = nn.ModuleList([nn.TransformerEncoderLayer(d_model=reduce_dim, nhead=n_heads) for _ in range(extra_blocks)])
trans_conv_ks = (16, 16) self.trans_conv = nn.ConvTranspose2d(reduce_dim, 1, trans_conv_ks, stride=trans_conv_ks)
# refinement and trans conv
if learn_trans_conv_only: for p in self.parameters(): p.requires_grad_(False) for p in self.trans_conv.parameters(): p.requires_grad_(True)
if prompt == 'fixed': self.prompt_list = ['a photo of a {}.'] elif prompt == 'shuffle': self.prompt_list = ['a photo of a {}.', 'a photograph of a {}.', 'an image of a {}.', '{}.'] elif prompt == 'shuffle+': self.prompt_list = ['a photo of a {}.', 'a photograph of a {}.', 'an image of a {}.', '{}.', 'a cropped photo of a {}.', 'a good photo of a {}.', 'a photo of one {}.', 'a bad photo of a {}.', 'a photo of the {}.'] elif prompt == 'shuffle_clip': from models.clip_prompts import imagenet_templates self.prompt_list = imagenet_templates
if process_cond is not None: if process_cond == 'clamp' or process_cond[0] == 'clamp':
val = process_cond[1] if type(process_cond) in {list, tuple} else 0.2
def clamp_vec(x): return torch.clamp(x, -val, val)
self.process_cond = clamp_vec
elif process_cond.endswith('.pth'): shift = torch.load(process_cond) def add_shift(x): return x + shift.to(x.device)
self.process_cond = add_shift
import pickle precomp = pickle.load(open('precomputed_prompt_vectors.pickle', 'rb')) self.precomputed_prompts = {k: torch.from_numpy(v) for k, v in precomp.items()}
def forward(self, inp_image, conditional=None, return_features=False, mask=None):
assert type(return_features) == bool
# inp_image = inp_image.to(self.model.positional_embedding.device)
if mask is not None: raise ValueError('mask not supported')
# x_inp = normalize(inp_image) x_inp = inp_image
bs, dev = inp_image.shape[0], x_inp.device
inp_image_size = inp_image.shape[2:]
cond = self.get_cond_vec(conditional, bs)
visual_q, activations, _ = self.visual_forward(x_inp, extract_layers=[0] + list(self.extract_layers))
activation1 = activations[0] activations = activations[1:]
a = None for i, (activation, block, reduce) in enumerate(zip(activations[::-1], self.blocks, self.reduces)): if a is not None: a = reduce(activation) + a else: a = reduce(activation)
if i == self.cond_layer: if self.reduce_cond is not None: cond = self.reduce_cond(cond) a = self.film_mul(cond) * a + self.film_add(cond)
a = block(a)
for block in self.extra_blocks: a = a + block(a)
a = a[1:].permute(1, 2, 0) # rm cls token and -> BS, Feats, Tokens
size = int(math.sqrt(a.shape[2]))
a = a.view(bs, a.shape[1], size, size)
if self.trans_conv is not None: a = self.trans_conv(a)
if self.upsample_proj is not None: a = self.upsample_proj(a) a = nnf.interpolate(a, x_inp.shape[2:], mode='bilinear')
a = nnf.interpolate(a, inp_image_size)
if return_features: return a, visual_q, cond, [activation1] + activations else: return a,123version https://git-lfs.github.com/spec/v1oid sha256:6d1de9c2944f2ccddca5f5e010ea5ae64a39845a86311af6fdf30841b0a5a16dsize 109497761123version https://git-lfs.github.com/spec/v1oid sha256:3d558d8d0e42c20224f13cf5a29c79eba2d59913419f945545d8cf7b72920de2size 853311931234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283import argparseimport globimport osimport shutilimport siteimport subprocessimport sys
script_dir = os.getcwd()
def run_cmd(cmd, capture_output=False, env=None): # Run shell commands return subprocess.run(cmd, shell=True, capture_output=capture_output, env=env)
def check_env(): # If we have access to conda, we are probably in an environment conda_not_exist = run_cmd("conda", capture_output=True).returncode if conda_not_exist: print("Conda is not installed. Exiting...") sys.exit() # Ensure this is a new environment and not the base environment if os.environ["CONDA_DEFAULT_ENV"] == "base": print("Create an environment for this project and activate it. Exiting...") sys.exit()
def install_dependencies(): # Install Git and clone repo run_cmd("conda install -y -k git") run_cmd("git clone https://github.com/C0untFloyd/roop-unleashed.git")
# Install the webui dependencies update_dependencies()
def update_dependencies(): global MY_PATH os.chdir(MY_PATH) # do a hard reset for to update even if there are local changes run_cmd("git fetch --all") run_cmd("git reset --hard origin/main") run_cmd("git pull") # Installs/Updates dependencies from all requirements.txt run_cmd("python -m pip install -r requirements.txt")
def start_app(): global MY_PATH os.chdir(MY_PATH) # forward commandline arguments sys.argv.pop(0) args = ' '.join(sys.argv) print("Launching App") run_cmd(f'python run.py {args}')
if __name__ == "__main__": global MY_PATH MY_PATH = "roop-unleashed"
# Verifies we are in a conda environment check_env()
# If webui has already been installed, skip and run if not os.path.exists(MY_PATH): install_dependencies() else: # moved update from batch to here, because of batch limitations updatechoice = input("Check for Updates? [y/n]").lower() if updatechoice == "y": update_dependencies()
# Run the model with webui os.chdir(script_dir) start_app()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980@echo offREM Please set the following commandline arguments to your prefered settingsset COMMANDLINE_ARGS=--execution-provider cuda --frame-processor face_swapper face_enhancer --video-encoder libvpx-vp9
cd /D "%~dp0"
echo "%CD%"| findstr /C:" " >nul && echo This script relies on Miniconda which can not be silently installed under a path with spaces. && goto end
set PATH=%PATH%;%SystemRoot%\system32
@rem configset INSTALL_DIR=%cd%\installer_filesset CONDA_ROOT_PREFIX=%cd%\installer_files\condaset INSTALL_ENV_DIR=%cd%\installer_files\envset MINICONDA_DOWNLOAD_URL=https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exeset FFMPEG_DOWNLOAD_URL=https://github.com/GyanD/codexffmpeg/releases/download/2023-06-21-git-1bcb8a7338/ffmpeg-2023-06-21-git-1bcb8a7338-essentials_build.zipset INSTALL_FFMPEG_DIR=%cd%\installer_files\ffmpegset conda_exists=F
@rem figure out whether git and conda needs to be installedcall "%CONDA_ROOT_PREFIX%\_conda.exe" --version >nul 2>&1if "%ERRORLEVEL%" EQU "0" set conda_exists=T
@rem (if necessary) install git and conda into a contained environment@rem download condaif "%conda_exists%" == "F" ( echo Downloading Miniconda from %MINICONDA_DOWNLOAD_URL% to %INSTALL_DIR%\miniconda_installer.exe
mkdir "%INSTALL_DIR%" call curl -Lk "%MINICONDA_DOWNLOAD_URL%" > "%INSTALL_DIR%\miniconda_installer.exe" || ( echo. && echo Miniconda failed to download. && goto end )
echo Installing Miniconda to %CONDA_ROOT_PREFIX% start /wait "" "%INSTALL_DIR%\miniconda_installer.exe" /InstallationType=JustMe /NoShortcuts=1 /AddToPath=0 /RegisterPython=0 /NoRegistry=1 /S /D=%CONDA_ROOT_PREFIX%
@rem test the conda binary echo Miniconda version: call "%CONDA_ROOT_PREFIX%\_conda.exe" --version || ( echo. && echo Miniconda not found. && goto end ))
@rem create the installer envif not exist "%INSTALL_ENV_DIR%" ( echo Packages to install: %PACKAGES_TO_INSTALL% call "%CONDA_ROOT_PREFIX%\_conda.exe" create --no-shortcuts -y -k --prefix "%INSTALL_ENV_DIR%" python=3.10 || ( echo. && echo Conda environment creation failed. && goto end ))
if not exist "%INSTALL_FFMPEG_DIR%" ( echo Downloading ffmpeg from %FFMPEG_DOWNLOAD_URL% to %INSTALL_DIR% call curl -Lk "%FFMPEG_DOWNLOAD_URL%" > "%INSTALL_DIR%\ffmpeg.zip" || ( echo. && echo ffmpeg failed to download. && goto end ) call powershell -command "Expand-Archive -Force '%INSTALL_DIR%\ffmpeg.zip' '%INSTALL_DIR%\'"
cd "installer_files" setlocal EnableExtensions EnableDelayedExpansion
for /f "tokens=*" %%f in ('dir /s /b /ad "ffmpeg*"') do ( ren "%%f" "ffmpeg" ) endlocal setx PATH "%INSTALL_FFMPEG_DIR%\bin\;%PATH%" echo To use videos, you need to restart roop after this installation. cd ..)
@rem check if conda environment was actually createdif not exist "%INSTALL_ENV_DIR%\python.exe" ( echo. && echo ERROR: Conda environment is empty. && goto end )
@rem activate installer envcall "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" || ( echo. && echo Miniconda hook not found. && goto end )
@rem setup installer envecho Launching roop unleashed - please edit windows_run.bat to customize commandline argumentscall python installer.py %COMMANDLINE_ARGS%
echo.echo Done!
:endpause
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355"""Jaa.py Plugin FrameworkAuthor: Janvarev Vladislav
Jaa.py - minimalistic one-file plugin framework with no dependencies.Main functions:- run all plugins files from "plugins" folder, base on filename- save each plugin options in "options" folder in JSON text files for further editing
- Pluginsmust located in plugins/ foldermust have "start(core)" function, that returns manifest dictmanifest must contain keys "name" and "version"can contain "default_options"- if contain - options will be saved in "options" folder and reload instead next time- if contain - "start_with_options(core,manifest)" function will run with manifest with "options" keymanifest will be processed in "process_plugin_manifest" function if you override it
- Options (for plugins)are saved under "options" folder in JSON formatcreated at first run plugin with "default_options"updated when plugin change "version"
- Example usage:class VoiceAssCore(JaaCore): # class must override JaaCore def __init__(self): JaaCore.__init__(self,__file__) ...
main = VoiceAssCore()main.init_plugins(["core"]) # 1 param - first plugins to be initialized # Good if you need some "core" options/plugin to be loaded before others # not necessary starts with "plugin_" prefix
also can be run like
main.init_plugins()
- RequirementsPython 3.5+ (due to dict mix in final_options calc), can be relaxed"""
import osimport tracebackimport json
# here we trying to use termcolor to highlight plugin info and errors during loadtry: from termcolor import cprintexcept Exception as e: # not found? making a stub! def cprint(p,color=None): if color == None: print(p) else: print(str(color).upper(),p)
version = "2.2.0"
class JaaCore: verbose = False
def __init__(self,root_file = __file__): self.jaaPluginPrefix = "plugin_" self.jaaVersion = version self.jaaRootFolder = os.path.dirname(root_file) self.jaaOptionsPath = self.jaaRootFolder+os.path.sep+"plugin_options" self.jaaShowTracebackOnPluginErrors = False if self.verbose: cprint("JAA.PY v{0} class created!".format(version),"blue")
# ------------- plugins ----------------- def init_plugins(self, list_first_plugins = []): self.plugin_manifests = {}
# 1. run first plugins first! for modname in list_first_plugins: self.init_plugin(modname)
# 2. run all plugins from plugins folder from os import listdir from os.path import isfile, join pluginpath = self.jaaRootFolder+"/plugins" files = [f for f in listdir(pluginpath) if isfile(join(pluginpath, f))]
for fil in files: # print fil[:-3] if fil.startswith(self.jaaPluginPrefix) and fil.endswith(".py"): modfile = fil[:-3] self.init_plugin(modfile)
def init_plugin(self,modname): # import try: mod = self.import_plugin("plugins."+modname) except Exception as e: self.print_error("JAA PLUGIN ERROR: {0} error on load: {1}".format(modname, str(e))) return False
# run start function try: res = mod.start(self) except Exception as e: self.print_error("JAA PLUGIN ERROR: {0} error on start: {1}".format(modname, str(e))) return False
# if plugin has an options if "default_options" in res: try: # saved options try to read saved_options = {} try: with open(self.jaaOptionsPath+'/'+modname+'.json', 'r', encoding="utf-8") as f: s = f.read() saved_options = json.loads(s) #print("Saved options", saved_options) except Exception as e: pass
res["default_options"]["v"] = res["version"]
# only string needs Python 3.5 final_options = {**res["default_options"], **saved_options}
# if no option found or version is differ from mod version if len(saved_options) == 0 or saved_options["v"] != res["version"]: final_options["v"] = res["version"] self.save_plugin_options(modname,final_options)
res["options"] = final_options
try: res2 = mod.start_with_options(self,res) if res2 != None: res = res2 except Exception as e: self.print_error("JAA PLUGIN ERROR: {0} error on start_with_options processing: {1}".format(modname, str(e))) return False
except Exception as e: self.print_error("JAA PLUGIN ERROR: {0} error on options processing: {1}".format(modname, str(e))) return False
# processing plugin manifest try: # set up name and version plugin_name = res["name"] plugin_version = res["version"]
self.process_plugin_manifest(modname,res)
except Exception as e: print("JAA PLUGIN ERROR: {0} error on process startup options: {1}".format(modname, str(e))) return False
self.plugin_manifests[modname] = res
self.on_succ_plugin_start(modname,plugin_name,plugin_version) return True
def on_succ_plugin_start(self, modname, plugin_name, plugin_version): if self.verbose: cprint("JAA PLUGIN: {1} {2} ({0}) started!".format(modname, plugin_name, plugin_version))
def print_error(self,p): cprint(p,"red") if self.jaaShowTracebackOnPluginErrors: traceback.print_exc()
def import_plugin(self, module_name): import sys
__import__(module_name)
if module_name in sys.modules: return sys.modules[module_name] return None
def save_plugin_options(self,modname,options): # check folder exists if not os.path.exists(self.jaaOptionsPath): os.makedirs(self.jaaOptionsPath)
str_options = json.dumps(options, sort_keys=True, indent=4, ensure_ascii=False) with open(self.jaaOptionsPath+'/'+modname+'.json', 'w', encoding="utf-8") as f: f.write(str_options) f.close()
# process manifest must be overrided in inherit class def process_plugin_manifest(self,modname,manifest): print("JAA PLUGIN: {0} manifest dummy procession (override 'process_plugin_manifest' function)".format(modname)) return
def plugin_manifest(self,pluginname): if pluginname in self.plugin_manifests: return self.plugin_manifests[pluginname] return {}
def plugin_options(self,pluginname): manifest = self.plugin_manifest(pluginname) if "options" in manifest: return manifest["options"] return None
# ------------ gradio stuff -------------- def gradio_save(self,pluginname): print("Saving options for {0}!".format(pluginname)) self.save_plugin_options(pluginname,self.plugin_options(pluginname))
def gradio_upd(self, pluginname, option, val): options = self.plugin_options(pluginname)
# special case if isinstance(options[option], (list, dict)) and isinstance(val, str): import json try: options[option] = json.loads(val) except Exception as e: print(e) pass else: options[option] = val print(option,val,options)
def gradio_render_settings_interface(self, title:str="Settings manager", required_fields_to_show_plugin:list=["default_options"]): import gradio as gr
with gr.Blocks() as gr_interface: gr.Markdown("# {0}".format(title)) for pluginname in self.plugin_manifests: manifest = self.plugin_manifests[pluginname]
# calculate if we show plugin is_show_plugin = False if len(required_fields_to_show_plugin) == 0: is_show_plugin = True else: for k in required_fields_to_show_plugin: if manifest.get(k) is not None: is_show_plugin = True
if is_show_plugin: with gr.Tab(pluginname): gr.Markdown("## {0} v{1}".format(manifest["name"],manifest["version"])) if manifest.get("description") is not None: gr.Markdown(manifest.get("description"))
if manifest.get("url") is not None: gr.Markdown("**URL:** [{0}]({0})".format(manifest.get("url")))
if "options" in manifest: options = manifest["options"] if len(options) > 1: # not only v text_button = gr.Button("Save options".format(pluginname)) #options_int_list = [] for option in options:
#gr.Label(label=option) if option != "v": val = options[option] label = option
if manifest.get("options_label") is not None: if manifest.get("options_label").get(option) is not None: label = option+": "+manifest.get("options_label").get(option)
if isinstance(val, (bool, )): gr_elem = gr.Checkbox(value=val,label=label) elif isinstance(val, (dict,list)): import json gr_elem = gr.Textbox(value=json.dumps(val,ensure_ascii=False), label=label) else: gr_elem = gr.Textbox(value=val, label=label)
def handler(x,pluginname=pluginname,option=option): self.gradio_upd(pluginname, option, x)
gr_elem.change(handler, gr_elem, None)
def handler_save(pluginname=pluginname): self.gradio_save(pluginname)
text_button.click(handler_save,inputs=None,outputs=None) else: gr.Markdown("_No options for this plugin_")
return gr_interface
def load_options(options_file=None,py_file=None,default_options={}): # 1. calculating options filename if options_file == None: if py_file == None: raise Exception('JAA: Options or PY file is not defined, cant calc options filename') else: options_file = py_file[:-3]+'.json'
# 2. try to read saved options saved_options = {} try: with open(options_file, 'r', encoding="utf-8") as f: s = f.read() saved_options = json.loads(s) #print("Saved options", saved_options) except Exception as e: pass
# 3. calculating final options
# only string needs Python 3.5 final_options = {**default_options, **saved_options}
# 4. calculating hash from def options to check - is file rewrite needed? import hashlib hash = hashlib.md5((json.dumps(default_options, sort_keys=True)).encode('utf-8')).hexdigest()
# 5. if no option file found or hash was from other default options if len(saved_options) == 0 or not ("hash" in saved_options.keys()) or saved_options["hash"] != hash: final_options["hash"] = hash #self.save_plugin_options(modname,final_options)
# saving in file str_options = json.dumps(final_options, sort_keys=True, indent=4, ensure_ascii=False) with open(options_file, 'w', encoding="utf-8") as f: f.write(str_options) f.close()
return final_options
"""The MIT License (MIT)Copyright (c) 2021 Janvarev Vladislav
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."""123version https://git-lfs.github.com/spec/v1oid sha256:a4956f9a7978a75630b08c9d6ec075b7c51cf43b4751b686e3a011d4012ddc9dsize 4720707123version https://git-lfs.github.com/spec/v1oid sha256:1009e537e0c2a07d4cabce6355f53cb66767cd4b4297ec7a4a64ca4b8a5684b7size 376637898123version https://git-lfs.github.com/spec/v1oid sha256:6d1de9c2944f2ccddca5f5e010ea5ae64a39845a86311af6fdf30841b0a5a16dsize 109497761123version https://git-lfs.github.com/spec/v1oid sha256:3d558d8d0e42c20224f13cf5a29c79eba2d59913419f945545d8cf7b72920de2size 85331193123version https://git-lfs.github.com/spec/v1oid sha256:49fafd45f8fd7aa8d31ab2a22d14d91b536c34494a5cfe31eb5d89c2fa266abbsize 67061725123version https://git-lfs.github.com/spec/v1oid sha256:70daeb4b1fd10f241043b587d892a941f2651d7322db02f06ff64b166537f65csize 603684323123version https://git-lfs.github.com/spec/v1oid sha256:e2cd4703ab14f4d01fd1383a8a8b266f9a5833dacee8e6a79d3bf21a1b6be5adsize 348632874123version https://git-lfs.github.com/spec/v1oid sha256:e4a3f08c753cb72d04e10aa0f7dbe3deebbf39567d4ead6dce08e98aa49e16afsize 55425368112# created by virtualenv automatically*123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227# don't import any costly modulesimport sysimport os
is_pypy = '__pypy__' in sys.builtin_module_names
def warn_distutils_present(): if 'distutils' not in sys.modules: return if is_pypy and sys.version_info < (3, 7): # PyPy for 3.6 unconditionally imports distutils, so bypass the warning # https://foss.heptapod.net/pypy/pypy/-/blob/be829135bc0d758997b3566062999ee8b23872b4/lib-python/3/site.py#L250 return import warnings
warnings.warn( "Distutils was imported before Setuptools, but importing Setuptools " "also replaces the `distutils` module in `sys.modules`. This may lead " "to undesirable behaviors or errors. To avoid these issues, avoid " "using distutils directly, ensure that setuptools is installed in the " "traditional way (e.g. not an editable install), and/or make sure " "that setuptools is always imported before distutils." )
def clear_distutils(): if 'distutils' not in sys.modules: return import warnings
warnings.warn("Setuptools is replacing distutils.") mods = [ name for name in sys.modules if name == "distutils" or name.startswith("distutils.") ] for name in mods: del sys.modules[name]
def enabled(): """ Allow selection of distutils by environment variable. """ which = os.environ.get('SETUPTOOLS_USE_DISTUTILS', 'local') return which == 'local'
def ensure_local_distutils(): import importlib
clear_distutils()
# With the DistutilsMetaFinder in place, # perform an import to cause distutils to be # loaded from setuptools._distutils. Ref #2906. with shim(): importlib.import_module('distutils')
# check that submodules load as expected core = importlib.import_module('distutils.core') assert '_distutils' in core.__file__, core.__file__ assert 'setuptools._distutils.log' not in sys.modules
def do_override(): """ Ensure that the local copy of distutils is preferred over stdlib.
See https://github.com/pypa/setuptools/issues/417#issuecomment-392298401 for more motivation. """ if enabled(): warn_distutils_present() ensure_local_distutils()
class _TrivialRe: def __init__(self, *patterns): self._patterns = patterns
def match(self, string): return all(pat in string for pat in self._patterns)
class DistutilsMetaFinder: def find_spec(self, fullname, path, target=None): # optimization: only consider top level modules and those # found in the CPython test suite. if path is not None and not fullname.startswith('test.'): return
method_name = 'spec_for_{fullname}'.format(**locals()) method = getattr(self, method_name, lambda: None) return method()
def spec_for_distutils(self): if self.is_cpython(): return
import importlib import importlib.abc import importlib.util
try: mod = importlib.import_module('setuptools._distutils') except Exception: # There are a couple of cases where setuptools._distutils # may not be present: # - An older Setuptools without a local distutils is # taking precedence. Ref #2957. # - Path manipulation during sitecustomize removes # setuptools from the path but only after the hook # has been loaded. Ref #2980. # In either case, fall back to stdlib behavior. return
class DistutilsLoader(importlib.abc.Loader): def create_module(self, spec): mod.__name__ = 'distutils' return mod
def exec_module(self, module): pass
return importlib.util.spec_from_loader( 'distutils', DistutilsLoader(), origin=mod.__file__ )
@staticmethod def is_cpython(): """ Suppress supplying distutils for CPython (build and tests). Ref #2965 and #3007. """ return os.path.isfile('pybuilddir.txt')
def spec_for_pip(self): """ Ensure stdlib distutils when running under pip. See pypa/pip#8761 for rationale. """ if sys.version_info >= (3, 12) or self.pip_imported_during_build(): return clear_distutils() self.spec_for_distutils = lambda: None
@classmethod def pip_imported_during_build(cls): """ Detect if pip is being imported in a build script. Ref #2355. """ import traceback
return any( cls.frame_file_is_setup(frame) for frame, line in traceback.walk_stack(None) )
@staticmethod def frame_file_is_setup(frame): """ Return True if the indicated frame suggests a setup.py file. """ # some frames may not have __file__ (#2940) return frame.f_globals.get('__file__', '').endswith('setup.py')
def spec_for_sensitive_tests(self): """ Ensure stdlib distutils when running select tests under CPython.
python/cpython#91169 """ clear_distutils() self.spec_for_distutils = lambda: None
sensitive_tests = ( [ 'test.test_distutils', 'test.test_peg_generator', 'test.test_importlib', ] if sys.version_info < (3, 10) else [ 'test.test_distutils', ] )
for name in DistutilsMetaFinder.sensitive_tests: setattr( DistutilsMetaFinder, f'spec_for_{name}', DistutilsMetaFinder.spec_for_sensitive_tests, )
DISTUTILS_FINDER = DistutilsMetaFinder()
def add_shim(): DISTUTILS_FINDER in sys.meta_path or insert_shim()
class shim: def __enter__(self): insert_shim()
def __exit__(self, exc, value, tb): _remove_shim()
def insert_shim(): sys.meta_path.insert(0, DISTUTILS_FINDER)
def _remove_shim(): try: sys.meta_path.remove(DISTUTILS_FINDER) except ValueError: pass
if sys.version_info < (3, 12): # DistutilsMetaFinder can only be disabled in Python < 3.12 (PEP 632) remove_shim = _remove_shim1__import__('_distutils_hack').do_override()123version https://git-lfs.github.com/spec/v1oid sha256:69ac3d8f27e679c81b94ab30b3b56e9cd138219b1ba94a1fa3606d5a76a1433dsize 18123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102"""Patches that are applied at runtime to the virtual environment."""
from __future__ import annotations
import osimport sysfrom contextlib import suppress
VIRTUALENV_PATCH_FILE = os.path.join(__file__)
def patch_dist(dist): """ Distutils allows user to configure some arguments via a configuration file: https://docs.python.org/3/install/index.html#distutils-configuration-files.
Some of this arguments though don't make sense in context of the virtual environment files, let's fix them up. """ # noqa: D205 # we cannot allow some install config as that would get packages installed outside of the virtual environment old_parse_config_files = dist.Distribution.parse_config_files
def parse_config_files(self, *args, **kwargs): result = old_parse_config_files(self, *args, **kwargs) install = self.get_option_dict("install")
if "prefix" in install: # the prefix governs where to install the libraries install["prefix"] = VIRTUALENV_PATCH_FILE, os.path.abspath(sys.prefix) for base in ("purelib", "platlib", "headers", "scripts", "data"): key = f"install_{base}" if key in install: # do not allow global configs to hijack venv paths install.pop(key, None) return result
dist.Distribution.parse_config_files = parse_config_files
# Import hook that patches some modules to ignore configuration values that break package installation in case# of virtual environments._DISTUTILS_PATCH = "distutils.dist", "setuptools.dist"# https://docs.python.org/3/library/importlib.html#setting-up-an-importer
class _Finder: """A meta path finder that allows patching the imported distutils modules."""
fullname = None
# lock[0] is threading.Lock(), but initialized lazily to avoid importing threading very early at startup, # because there are gevent-based applications that need to be first to import threading by themselves. # See https://github.com/pypa/virtualenv/issues/1895 for details. lock = [] # noqa: RUF012
def find_spec(self, fullname, path, target=None): # noqa: ARG002 if fullname in _DISTUTILS_PATCH and self.fullname is None: # initialize lock[0] lazily if len(self.lock) == 0: import threading
lock = threading.Lock() # there is possibility that two threads T1 and T2 are simultaneously running into find_spec, # observing .lock as empty, and further going into hereby initialization. However due to the GIL, # list.append() operation is atomic and this way only one of the threads will "win" to put the lock # - that every thread will use - into .lock[0]. # https://docs.python.org/3/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe self.lock.append(lock)
from functools import partial from importlib.util import find_spec
with self.lock[0]: self.fullname = fullname try: spec = find_spec(fullname, path) if spec is not None: # https://www.python.org/dev/peps/pep-0451/#how-loading-will-work is_new_api = hasattr(spec.loader, "exec_module") func_name = "exec_module" if is_new_api else "load_module" old = getattr(spec.loader, func_name) func = self.exec_module if is_new_api else self.load_module if old is not func: with suppress(AttributeError): # C-Extension loaders are r/o such as zipimporter with <3.7 setattr(spec.loader, func_name, partial(func, old)) return spec finally: self.fullname = None return None
@staticmethod def exec_module(old, module): old(module) if module.__name__ in _DISTUTILS_PATCH: patch_dist(module)
@staticmethod def load_module(old, name): module = old(name) if module.__name__ in _DISTUTILS_PATCH: patch_dist(module) return module
sys.meta_path.insert(0, _Finder()).gitattributes+2
Dockerfile+2-2
LICENSEAdded+661
README.md+6-8
__pycache__/jaa.cpython-311.pycAdded
__pycache__/settings.cpython-311.pycAdded
chain_img_processor/__init__.pyAdded+4
chain_img_processor/__pycache__/__init__.cpython-311.pycAdded
chain_img_processor/__pycache__/batchimage.cpython-311.pycAdded
chain_img_processor/__pycache__/ffmpeg_writer.cpython-311.pycAdded
chain_img_processor/__pycache__/image.cpython-311.pycAdded
chain_img_processor/__pycache__/video.cpython-311.pycAdded
chain_img_processor/batchimage.pyAdded+86
chain_img_processor/ffmpeg_writer.pyAdded+253
chain_img_processor/image.pyAdded+176
chain_img_processor/video.pyAdded+132
clip/__init__.pyAdded+1
clip/__pycache__/__init__.cpython-311.pycAdded
clip/__pycache__/clip.cpython-311.pycAdded
clip/__pycache__/model.cpython-311.pycAdded
clip/__pycache__/simple_tokenizer.cpython-311.pycAdded
clip/bpe_simple_vocab_16e6.txt.gzAdded+3
clip/clip.pyAdded+245
clip/clipseg.pyAdded+538
clip/model.pyAdded+436
clip/simple_tokenizer.pyAdded+132
clip/vitseg.pyAdded+286
docs/faceselection.pngAdded

docs/finaloutput.pngAdded

Large File Pointer Details
- SHA256: 1b6883bacac4a858a44fa4108e3f11e0ebbbc34bb0393aac87a1916da76aba44
- Pointer size: 132 Bytes
- Size of remote file: 1.42 MB
docs/kickboxing.jpgAdded

docs/musk.jpgAdded

docs/screenshot.pngAdded

gfpgan/weights/detection_Resnet50_Final.pthAdded+3
gfpgan/weights/parsing_parsenet.pthAdded+3
installer/installer.pyAdded+83
installer/windows_run.batAdded+80
jaa.pyAdded+355
models/CLIP/rd64-uni-refined.pthAdded+3
models/CodeFormer/codeformer.pthAdded+3
models/CodeFormer/facelib/detection_Resnet50_Final.pthAdded+3
models/CodeFormer/facelib/parsing_parsenet.pthAdded+3
models/CodeFormer/realesrgan/RealESRGAN_x2plus.pthAdded+3
models/DMDNet.pthAdded+3
models/GFPGANv1.4.pthAdded+3
models/inswapper_128.onnxAdded+3
mynewshinyroop/.gitignoreAdded+2
mynewshinyroop/Lib/site-packages/_distutils_hack/__init__.pyAdded+227
mynewshinyroop/Lib/site-packages/_distutils_hack/override.pyAdded+1
mynewshinyroop/Lib/site-packages/_virtualenv.pthAdded+3
mynewshinyroop/Lib/site-packages/_virtualenv.pyAdded+102