Compare commits
1558 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c7fbcb5789 | |||
| 28cc18cbdf | |||
| 9a042c907b | |||
| 36fb876da6 | |||
| 8a6a4ad894 | |||
| e36827af32 | |||
| 3a5196de1b | |||
| 4aa99f77ab | |||
| f190b85182 | |||
| 8459015017 | |||
| d0b65e148b | |||
| f939bce845 | |||
| ed383eb5a0 | |||
| c1713bfeac | |||
| 4a66d2fb22 | |||
| 81126027f5 | |||
| 1dbee391b4 | |||
| 56676ff923 | |||
| a06ae54a18 | |||
| 58a142b56b | |||
| 0f2de4cfbe | |||
| cfa326bbcc | |||
| 7581da5d92 | |||
| 41c2121e51 | |||
| 5df56b3980 | |||
| 9cdd161160 | |||
| 7c30c5eec4 | |||
| 5cbda43d63 | |||
| 30f31d23c6 | |||
| bcdcc8be7d | |||
| 4dde96109b | |||
| d31dc7a739 | |||
| 1ddb886a80 | |||
| 2e7efe47b6 | |||
| a97147bc8a | |||
| 0181c1f76b | |||
| 2cf23099eb | |||
| e1dfd452c0 | |||
| 6916de5c0b | |||
| 45a51c07e2 | |||
| d224fed0ce | |||
| 6acd8e28fc | |||
| 0b83f4c263 | |||
| 315e40a49c | |||
| a75dfe1c0d | |||
| 14b9762bca | |||
| c1e04c63b3 | |||
| e280eb4055 | |||
| 39db1d09d1 | |||
| 92032f2da8 | |||
| 208ccfbe7c | |||
| ee9d487081 | |||
| cfb90a938e | |||
| 92501d4f80 | |||
| b6dc307c99 | |||
| 47b52d9b28 | |||
| 541881e318 | |||
| cb5b335acd | |||
| 0011640ab1 | |||
| 85bf2eb441 | |||
| 4d9f2c3ec8 | |||
| abb630108e | |||
| 9dd2534824 | |||
| 3cc7572f5d | |||
| 751d014cd6 | |||
| 639f22ea7c | |||
| 905b14237f | |||
| ca671e5d7b | |||
| 58d5b042cd | |||
| 1fd69655fe | |||
| e00365962b | |||
| c2c05fcca8 | |||
| 42e6df723c | |||
| 209c26a1cb | |||
| 8d986727b3 | |||
| 46413b20a4 | |||
| 8e292373ec | |||
| 6869d95890 | |||
| 37906e429a | |||
| f56cebf5ba | |||
| 425507bd10 | |||
| 2f98a35fc4 | |||
| 30aa5e0a7c | |||
| cab1d839b5 | |||
| 71e0057137 | |||
| a183de04e3 | |||
| 818d6a11e7 | |||
| 73786c047f | |||
| b00b429477 | |||
| ec9acb3145 | |||
| 16b4d2cf3f | |||
| 8b6848c6db | |||
| a4ee64050a | |||
| 942617f828 | |||
| 233c66b36e | |||
| 88ba095fd0 | |||
| 44744d6005 | |||
| 18ca987c92 | |||
| f8f38c7c28 | |||
| a06dab8d7a | |||
| 6ffbff0857 | |||
| 6fa42e919f | |||
| 9805f35c6f | |||
| 15ec54dd96 | |||
| f903b4dda3 | |||
| 3f7f61e541 | |||
| 1e7a8ce5e4 | |||
| 397251ba0c | |||
| df62ffbd25 | |||
| 149c9d2234 | |||
| 320a217b78 | |||
| 420f56c2e8 | |||
| d9034b48a5 | |||
| 50158a1fc9 | |||
| 62470ee234 | |||
| 3d31d5c27b | |||
| dfdc51246c | |||
| bfc48fbc24 | |||
| 04a005f0e9 | |||
| fccd0b00c2 | |||
| 9c6ea5386b | |||
| 7ad6899bf9 | |||
| e4dcdcc955 | |||
| 62bd7624d2 | |||
| 7c3ab416ad | |||
| 2cacbc124c | |||
| 51f1cca852 | |||
| cf14a6a7aa | |||
| 980970d390 | |||
| 80873b1538 | |||
| 1341b22081 | |||
| 6f9fcfdbb7 | |||
| a5b6a5a3ad | |||
| c2ea571005 | |||
| ac3cc1adc5 | |||
| c32c51a0fc | |||
| 501993ebf2 | |||
| 5d7d1823af | |||
| 1ffdedc11d | |||
| c507d7b252 | |||
| 7ba02e0b7c | |||
| 15156cde18 | |||
| 0aa7c53c0b | |||
| 2a7ad70db5 | |||
| dfd6438221 | |||
| 0ce67cb618 | |||
| cba6fba123 | |||
| ac0ecf3b4b | |||
| 0743ee9b3e | |||
| c352008c95 | |||
| d8126be578 | |||
| 45b7bba3d0 | |||
| 267fd5d76b | |||
| d613cd17c7 | |||
| d859cec696 | |||
| c5496c7646 | |||
| 003b91f083 | |||
| 5692bf1517 | |||
| e55fec9d9a | |||
| 00901bfbe0 | |||
| a70dfb64a8 | |||
| be5f1acc8f | |||
| f3af8c8d04 | |||
| b6f74e936e | |||
| d4945f4422 | |||
| 5768afc776 | |||
| a84e842189 | |||
| 6f86b62a1b | |||
| ce21840a04 | |||
| ae124439c4 | |||
| 777af661a2 | |||
| c0ca6348e8 | |||
| 3be9074031 | |||
| a2f23f9d22 | |||
| bc5ae74c7d | |||
| 8100e901ab | |||
| c2fd7c0344 | |||
| 7c13ffdbb1 | |||
| a86f4411cb | |||
| 393a5b82ba | |||
| af050dcaa7 | |||
| 5fbb13e0da | |||
| 16848f950b | |||
| 48a2a1a437 | |||
| 1465dab715 | |||
| 79c9151802 | |||
| f651405427 | |||
| b58ed1b243 | |||
| c9174253fb | |||
| 91560e98c4 | |||
| f476649c02 | |||
| cd12c0e15c | |||
| 05230c0260 | |||
| 4ad0c0c0a8 | |||
| c756133541 | |||
| b621a63cf6 | |||
| b0f5934234 | |||
| e472383acb | |||
| 12c6f37f8e | |||
| 7aa27b000a | |||
| 31992eff9b | |||
| d05f9e8124 | |||
| c069c2c562 | |||
| adcd65ba34 | |||
| f0e2e8b930 | |||
| 83c0758d90 | |||
| 1d603eb5a8 | |||
| 4b6eb8072b | |||
| 908fb4ea71 | |||
| c9c105c7db | |||
| a79890efd6 | |||
| 32862e4379 | |||
| 8f18263759 | |||
| 11a435b469 | |||
| 0aacd4c72b | |||
| 8b08b78c03 | |||
| ba92135a2b | |||
| 59d060fd5e | |||
| bb07cb6a0d | |||
| dc57ec0296 | |||
| 892e703b59 | |||
| af2951ed53 | |||
| de04573438 | |||
| bfe418a58d | |||
| 00d4a4d4ac | |||
| edfae95d90 | |||
| de1809bd14 | |||
| 9feb034e34 | |||
| 3e068de0dc | |||
| 0d5941edbc | |||
| fe4d084390 | |||
| f586f4973a | |||
| e4b4a9c4ac | |||
| 10945aa41a | |||
| de03882d6c | |||
| 3d9a0d9e4b | |||
| 98c5fa9201 | |||
| 7428ce52ab | |||
| a978320334 | |||
| 4f5281a92e | |||
| 86b3aa94e2 | |||
| 5b7d86d42b | |||
| 93eae69895 | |||
| cd9ce2e31c | |||
| c121f8c315 | |||
| 8edb9144cc | |||
| 60186c7b9d | |||
| 7745db6fc0 | |||
| ea272152e0 | |||
| e9c6325fc6 | |||
| 7504f14503 | |||
| cf2772fab0 | |||
| 0dfffe53ec | |||
| c16fcb7f46 | |||
| 6d7e57ba6a | |||
| da45e73b4f | |||
| d859de37d9 | |||
| 1242ba08e1 | |||
| 0c5427960b | |||
| 3c0c277579 | |||
| 0fb34b57b8 | |||
| 2be85f8fe0 | |||
| eb52c803b8 | |||
| f8871dedcf | |||
| b7e0d4a7e1 | |||
| 5cb1ce470d | |||
| 888b928f0d | |||
| b55f09c4e1 | |||
| c7cd9b441d | |||
| 6ef0ff39f2 | |||
| aeaf1c510f | |||
| 097140ac1a | |||
| 778a30a95e | |||
| 96c393a7a7 | |||
| 09013b357c | |||
| d45f790f58 | |||
| 8c32594d3b | |||
| f3cc5f8382 | |||
| 28bafffdc2 | |||
| 5db09d1865 | |||
| c5631aa90d | |||
| 206de1a6b0 | |||
| b943eebb1d | |||
| 3772a82a70 | |||
| 8fc67f3851 | |||
| 265bc26c21 | |||
| 735c9e8059 | |||
| 89cfbc3bbe | |||
| bda86f0fd9 | |||
| cc41cc4349 | |||
| 6513470f0d | |||
| cee1a40651 | |||
| 1d42babd32 | |||
| 6b8143a84e | |||
| 8b74389e76 | |||
| 23a0e60b9b | |||
| 5381405eaa | |||
| 1a79a5049b | |||
| 9c201550dd | |||
| 39ec4cfea9 | |||
| 049d5642e5 | |||
| 5942979344 | |||
| f1ff932caf | |||
| b2414476ef | |||
| 16bdcce92d | |||
| 659f62e120 | |||
| fc3e246c0f | |||
| f284ae23bc | |||
| 0ef4a4cb23 | |||
| 56604f08a1 | |||
| 8dbacc7d01 | |||
| 2abc417834 | |||
| 39ebd5684b | |||
| ac45789123 | |||
| 4608f6236f | |||
| e90d4334ad | |||
| 9d2cbf8e97 | |||
| 746783f7a4 | |||
| c2bdbb67b6 | |||
| 4d56383025 | |||
| 294ec5ac37 | |||
| 672dc4efa8 | |||
| 895456c4a2 | |||
| 120a84bd2f | |||
| f92d61497a | |||
| 38864816fa | |||
| 49bbf11407 | |||
| 6fc12428e3 | |||
| b32a334e3d | |||
| 60c602232f | |||
| 57f29bd61d | |||
| 1455159cf4 | |||
| 976c1053ef | |||
| 854f8c318c | |||
| 368d66c9cc | |||
| 22e23dbf29 | |||
| 81105ee013 | |||
| 883d6a2b34 | |||
| 24dae9bc4c | |||
| 15322e1b1a | |||
| 259d33c3c8 | |||
| f5f89780cc | |||
| aaacf48232 | |||
| 48fae7ccdc | |||
| 9e1f3feb12 | |||
| 208760f348 | |||
| 06725af40b | |||
| 639ccf254b | |||
| 552f8bc832 | |||
| 28a2b5b4aa | |||
| 334298d473 | |||
| 2d5507fce5 | |||
| d92ce145bb | |||
| d3fdc4af61 | |||
| b4776ea3a2 | |||
| 3bd3a09160 | |||
| bb04d400c9 | |||
| 73ab982d1b | |||
| fed5b1d55c | |||
| 9a15ae2a92 | |||
| 609dea36ea | |||
| 9528d66c94 | |||
| 78acdcf677 | |||
| dc1adeecdd | |||
| 4a43334376 | |||
| 83e8c32276 | |||
| 81c4ddf6eb | |||
| 309a606c2f | |||
| ac02216e54 | |||
| af5f0734c9 | |||
| a5f61aa8c5 | |||
| 11d23e8ca5 | |||
| 50a21cb09f | |||
| 96871e4f74 | |||
| 4a666381bf | |||
| 110485d5bb | |||
| 87cd07b3af | |||
| 0bb6e00ba3 | |||
| 87d973e389 | |||
| ef6b8123dc | |||
| 5ed7daa3d9 | |||
| ef1723ef41 | |||
| 7547d7c791 | |||
| 88736b5557 | |||
| 9eadc4f146 | |||
| 97c8e7e0c7 | |||
| e12a26c253 | |||
| 600036d158 | |||
| 4125552752 | |||
| e294e46d46 | |||
| 7499148ad4 | |||
| b58d061e41 | |||
| 6080045b2a | |||
| 293f44e6c1 | |||
| 01c8f1803a | |||
| c2ed413203 | |||
| 8b40f475a3 | |||
| 668ae34e21 | |||
| de79597ab9 | |||
| ffa7f8201d | |||
| ec6ee5c13b | |||
| 6d0a8dcd89 | |||
| 588a52891d | |||
| b25c126ccd | |||
| c7a1ff8720 | |||
| 284fd8f415 | |||
| c5c7fa06aa | |||
| debf836fcc | |||
| a6e5846453 | |||
| e715e46b6a | |||
| bbba133f05 | |||
| dec791d35d | |||
| 81c00728b8 | |||
| a0096c5897 | |||
| 39eae9f009 | |||
| d608926f81 | |||
| 03ee297aa2 | |||
| 3cd6e1d0a0 | |||
| ec78354efa | |||
| 524d6a4dba | |||
| f207eb7a0d | |||
| 601a7b4ce5 | |||
| 0cd5b0ed54 | |||
| 23c36f59b4 | |||
| 26a0c29587 | |||
| a75314b41f | |||
| 1c64bb7140 | |||
| 9621ca4d64 | |||
| 8a6e4bda21 | |||
| b30cc87b78 | |||
| 1f6844eb7e | |||
| f0f100e67b | |||
| 500de919ed | |||
| a15dd151ff | |||
| 2a40d3c603 | |||
| 3d341ebc7d | |||
| 29f04149b6 | |||
| e44103264d | |||
| 6955c210b7 | |||
| d1750e5eca | |||
| c5a0c59a83 | |||
| f7f015e84b | |||
| f85b74763d | |||
| fd8674a4bc | |||
| d2e0c1ca13 | |||
| 3a9bf4ac10 | |||
| 40ac134c55 | |||
| 5cedc8f9b2 | |||
| 86b99b1e98 | |||
| ac2a981c4f | |||
| 066afda2f6 | |||
| 8fe1e19522 | |||
| f5d719d1f1 | |||
| 370a77f8e7 | |||
| 8aa51f682c | |||
| 58c19545c8 | |||
| 314ae1535e | |||
| 5f36f6ab21 | |||
| 1463cea949 | |||
| 73a0b4bba6 | |||
| 9b471436b2 | |||
| b2e039d07b | |||
| 411da7c281 | |||
| 6d337bf23d | |||
| dea5e43c83 | |||
| 043d2edcf6 | |||
| f383af2729 | |||
| 890181e1d4 | |||
| 598da5cd49 | |||
| b60e1088db | |||
| bde439ef67 | |||
| fc83af4432 | |||
| 337bc4a2fb | |||
| 6fac65f334 | |||
| 5a031d9233 | |||
| e4e875fffe | |||
| b945ba716b | |||
| 2207ef363a | |||
| 3a13b0e762 | |||
| 6429c3db11 | |||
| 5a9dc1c0ca | |||
| 4f2a4a3615 | |||
| 97431f29fe | |||
| ffd0f8ddc3 | |||
| c0725ba2d0 | |||
| c40be2252a | |||
| 7021cdb1de | |||
| cdb60a690d | |||
| 236eb82c3a | |||
| cd12256575 | |||
| 472c22cc8a | |||
| bcfaf3979a | |||
| eb667e715a | |||
| d6d0b22e66 | |||
| af45872fdb | |||
| b29fc6d4de | |||
| a292d2c47f | |||
| c1c816006e | |||
| 94e9669566 | |||
| 8048f36072 | |||
| f6762d2ad9 | |||
| 3bb32befe9 | |||
| 48d6102b31 | |||
| 520e52f846 | |||
| 7af576e745 | |||
| 294f8a514f | |||
| bc1a450124 | |||
| 0d1924c48b | |||
| 0fc7dc1c04 | |||
| 3a4a6c43a4 | |||
| 5432d93013 | |||
| 6a86b3ad9b | |||
| 7ff54005fe | |||
| 66767e3876 | |||
| 6d77a6e1c6 | |||
| 42dbcad3ef | |||
| 98fc525a2c | |||
| ff2952f105 | |||
| 9aa4d098f0 | |||
| a625a7bb81 | |||
| f9c14a8c8c | |||
| 5e80d9ee99 | |||
| 47bccbebae | |||
| 9ba991cad8 | |||
| 9c1c0da026 | |||
| 656437e0a5 | |||
| 6ad666e479 | |||
| 80d639a440 | |||
| 96ee3eff6c | |||
| ff805d8d0e | |||
| c3699d4fd1 | |||
| 4d4a9e7332 | |||
| 44c5097375 | |||
| 44db35fb1a | |||
| ff1609f91e | |||
| d9499f4301 | |||
| 16ab174290 | |||
| 046c7b053a | |||
| 6b8c661c49 | |||
| 2b06cefe66 | |||
| 7edd50f304 | |||
| bbf00a96af | |||
| 329c8bacce | |||
| c3facab495 | |||
| 1dd25be037 | |||
| f6c8201e56 | |||
| fe1967a4c4 | |||
| 452ab8fe72 | |||
| 399baa54c2 | |||
| 21d561885e | |||
| 73c74baa6a | |||
| 1f373a2baa | |||
| 4afaaf8a02 | |||
| bda2ecdbf5 | |||
| 4c423f6d37 | |||
| cc80a09d82 | |||
| 8052a4971e | |||
| 759515316e | |||
| d727ddfccd | |||
| 65ccd6305f | |||
| a2fad6ee05 | |||
| fbc5c531b9 | |||
| be31e7e71a | |||
| 844c23975f | |||
| f2b83517aa | |||
| ddc2a3499b | |||
| d4d3134f6d | |||
| 5121846d34 | |||
| 0beb131c7f | |||
| dda067f64d | |||
| bf5067f50c | |||
| 4830b25136 | |||
| 1df6c8bfec | |||
| 9c1eba2af3 | |||
| eaa9f5162f | |||
| dfc4c27b24 | |||
| 88b2ef3b04 | |||
| 6523edb8a4 | |||
| 3b8515d2c9 | |||
| 4a50c9638c | |||
| de8ee92ed8 | |||
| 76f5abdbdb | |||
| fce86ab7d7 | |||
| 7683547728 | |||
| 2d8c894b27 | |||
| 236dd55dbe | |||
| 443ca983ad | |||
| 464fbcd921 | |||
| 384fab9627 | |||
| 0550659ce6 | |||
| d10c4db57e | |||
| 321680ccd0 | |||
| 5f9ddfa46f | |||
| 7c128bbdac | |||
| eb01d7f0e0 | |||
| 853e21d98e | |||
| 1c6efdbba7 | |||
| ec718f76b5 | |||
| 3d15e58b0a | |||
| 8aa13d5dce | |||
| 861cbd5636 | |||
| d33cb2b812 | |||
| 3e223523ce | |||
| d295e97a0d | |||
| 77bd953da2 | |||
| 2f6ea8b103 | |||
| a3d9b011a3 | |||
| 282903bb67 | |||
| 0d65d0eabd | |||
| f00eaa4d00 | |||
| d4255506ff | |||
| 117ec71994 | |||
| 4be7b620c2 | |||
| a8cbe50c9f | |||
| 19f5795c27 | |||
| 6fe16a9e1a | |||
| eadef35512 | |||
| 771dac9c5f | |||
| 0619df9835 | |||
| 7cc96429f2 | |||
| 26500b8c1b | |||
| a109c7aeb8 | |||
| 27fdc26a74 | |||
| 3a66c3c9e1 | |||
| 499543cf1d | |||
| 902afa6b4c | |||
| fff1a0c74f | |||
| 954499a494 | |||
| 44d14bc32e | |||
| fbc8d21354 | |||
| 906d1179e9 | |||
| dbb10fbd8c | |||
| 891ccb767c | |||
| 81e94de318 | |||
| 2282eb8dd5 | |||
| 3d8b1af6be | |||
| 2aa485b5af | |||
| 9821625a76 | |||
| 3562b0dc74 | |||
| fd51b8501e | |||
| 09a2da835e | |||
| 770ee23f18 | |||
| 76010a51ef | |||
| e34949be52 | |||
| 35fd24e857 | |||
| 7d60076b8b | |||
| 77171923f8 | |||
| c4ffeb857e | |||
| e5381320b9 | |||
| 86a46e8189 | |||
| c2279da522 | |||
| dc2074c46d | |||
| 362675e75b | |||
| 6ab0b65ed1 | |||
| 3f763d41e8 | |||
| f71e919ecb | |||
| e3c849da06 | |||
| c0113872c5 | |||
| deeec0b343 | |||
| c7e810a985 | |||
| 2d947175b9 | |||
| 7026b96476 | |||
| 56ef5e9d48 | |||
| 0eb5fde2fd | |||
| 0935d2c304 | |||
| b2f9709538 | |||
| 5cc7bf3876 | |||
| 416fbde726 | |||
| 1cc7c4bfb3 | |||
| 951842d785 | |||
| 591ad1dbc3 | |||
| fcfe5c179b | |||
| a0e979badb | |||
| 3aa9f01bdc | |||
| 4e5d2526cb | |||
| ab63054f95 | |||
| 0c71967a53 | |||
| b20cd352d9 | |||
| 3a4290f833 | |||
| df48222f3e | |||
| ee8e98711b | |||
| 87b50397a6 | |||
| e309583f29 | |||
| 7ce1f3a142 | |||
| db63cf7d24 | |||
| cdafbcaad2 | |||
| 34055f9d0c | |||
| 9b17416580 | |||
| 833b9b62b5 | |||
| 3b0be0f12f | |||
| 4083639c3c | |||
| 8a758383d2 | |||
| ad3b8a1c41 | |||
| 1b9ca01e4f | |||
| 30f4f25b2e | |||
| a69daae012 | |||
| 99aa702015 | |||
| d00f6dca28 | |||
| fdecf813b6 | |||
| f8f4ff2bb8 | |||
| 702a1e1cc7 | |||
| 8e355fbd75 | |||
| 701feabf49 | |||
| d2878a8b0b | |||
| 663fb87976 | |||
| d9d94141dc | |||
| 813535d38b | |||
| afd0624587 | |||
| ab3d3528a1 | |||
| 0ad38a9b87 | |||
| cf1edc2b54 | |||
| 5b761b49ad | |||
| 102b6617da | |||
| 93015964c7 | |||
| 6fb2194d9c | |||
| 74b80e7211 | |||
| 59544321aa | |||
| e785402b6a | |||
| c485a7d12e | |||
| 413123f08a | |||
| 7d4d871d46 | |||
| 26d0d87f5b | |||
| d6478a60aa | |||
| ab57417175 | |||
| f8042cb323 | |||
| f5959c1c30 | |||
| 25de9a785c | |||
| 924642331b | |||
| c9c457eda8 | |||
| 73c2a03d49 | |||
| 06af73bd1d | |||
| 9cebe308e9 | |||
| 558808c748 | |||
| c68aabc852 | |||
| 46ef185709 | |||
| 46375f0592 | |||
| 558baffa2c | |||
| 4ebed495ed | |||
| e6d41b54cd | |||
| e06c16e884 | |||
| 72bc69e741 | |||
| b33ffc11aa | |||
| 0a2c24003c | |||
| 9e58e11ad4 | |||
| 4c4d7dd01f | |||
| adb3f2bcdd | |||
| 8afabae67d | |||
| fccde0c1f7 | |||
| 3ca4655a18 | |||
| 349f893024 | |||
| 7b44b85730 | |||
| 329c8ab932 | |||
| 259768f27f | |||
| 741e8ecb7d | |||
| 63485b2c55 | |||
| e4726cccf9 | |||
| f11eec81e3 | |||
| c3d51fc696 | |||
| 45881703c5 | |||
| 340fce2113 | |||
| 657404b75b | |||
| 35d1c94549 | |||
| 25189b29af | |||
| 47033afa5c | |||
| de5bb4ca88 | |||
| ff7027ffc0 | |||
| 0c1c9e74cd | |||
| 022639a145 | |||
| 5e16914a4e | |||
| 8f3b02f095 | |||
| f593cbfec4 | |||
| aab385d01b | |||
| a51721cb09 | |||
| 061a4a295d | |||
| ba05e32789 | |||
| 3e67017dfb | |||
| d7e3ea68b3 | |||
| bf0b083216 | |||
| 317d00b2a6 | |||
| 737a013377 | |||
| 78c1a74660 | |||
| bd9b3d15e8 | |||
| 5681bf8016 | |||
| 348c6022f3 | |||
| 76b1ad7daf | |||
| d39440bfb9 | |||
| 5ef669de08 | |||
| 20158d77d9 | |||
| e7965a5eb8 | |||
| 3bff988f1e | |||
| 41196ccbf7 | |||
| 541a3db05b | |||
| ae7291fb49 | |||
| d43333ff71 | |||
| 0cdbd90d6b | |||
| d0026da483 | |||
| 8d54739de5 | |||
| 135b61bc0b | |||
| 6adf2b71c2 | |||
| 87cca029d7 | |||
| ae0b2cc196 | |||
| 1ac11b3dae | |||
| 0ff8b8fb54 | |||
| c985d23c52 | |||
| 87a083d1b2 | |||
| 644b537014 | |||
| 67cd4ec0aa | |||
| 28b084ca25 | |||
| 503bd3fc0f | |||
| f874b1bcad | |||
| 9e7de49fc5 | |||
| 06bc1f4f67 | |||
| 338d0b6103 | |||
| 3989d7e88b | |||
| afea99a72b | |||
| 965c728914 | |||
| 46f3ee9594 | |||
| 323dcadea2 | |||
| 642faa1f65 | |||
| d156d5bffd | |||
| edf3ad5aed | |||
| 4aaae3dc65 | |||
| 9a4a1aac81 | |||
| ee373a737c | |||
| 9e248fb24e | |||
| 08603378e8 | |||
| 834f4c7cd3 | |||
| 549b475be9 | |||
| 7e5fcdaf69 | |||
| e3939f3339 | |||
| cb2a4f2424 | |||
| f564d8ed2c | |||
| ba7d0d225a | |||
| 04b90328c0 | |||
| a0af2852b6 | |||
| b6c1a1bbbf | |||
| 00e393ce10 | |||
| 84d41e49b3 | |||
| 0c9282b84d | |||
| 18ba89863d | |||
| 444f102964 | |||
| 9e8464db1e | |||
| 738e133b24 | |||
| 01a257eb07 | |||
| 6558716018 | |||
| 9c87ae0d9d | |||
| 7ab16e99ee | |||
| 8a7a4275a8 | |||
| 3269572753 | |||
| 5070ab8004 | |||
| 02e7824e6a | |||
| d83a1ba65b | |||
| 1bb21f3510 | |||
| 739686b1c5 | |||
| c0f9821c35 | |||
| cd48308a2a | |||
| 592b0dcfa7 | |||
| 20df81b0cc | |||
| 99acbd5ebe | |||
| d1c93c3822 | |||
| 9e14cac318 | |||
| f898833ea3 | |||
| f3d1631aab | |||
| 8632452627 | |||
| 86708463f1 | |||
| 66146ed72b | |||
| 2b8484a29d | |||
| 18e3e6d6ab | |||
| bfc5c08109 | |||
| ad266d795e | |||
| e422f19ee9 | |||
| d0d5075914 | |||
| 896fde789e | |||
| d63117ace5 | |||
| 66d7630705 | |||
| 63d3150dc4 | |||
| cb81087b59 | |||
| 6139b145f0 | |||
| f331821b27 | |||
| 5359dc0a10 | |||
| 7989765faa | |||
| 783a5754d5 | |||
| 897312de46 | |||
| 23c6b5f124 | |||
| c2463b5323 | |||
| ed2a05fc3f | |||
| e3174a1a42 | |||
| 07878c6ca8 | |||
| 5e30f737b0 | |||
| bd5c16e8da | |||
| f2c55523c0 | |||
| cb5f0823c6 | |||
| 9dd0c4add5 | |||
| 1b46863f24 | |||
| 3d83683a28 | |||
| b7f0e81562 | |||
| 9d8d279d0d | |||
| d888490f85 | |||
| 168eac319d | |||
| 73f69a7453 | |||
| ec54257cb2 | |||
| 72ee347eab | |||
| ac1abf3de6 | |||
| bb90b0ff42 | |||
| db56bdce33 | |||
| f3a1027869 | |||
| dd07b5193e | |||
| 3369fb27df | |||
| 4c6788644a | |||
| a6cedafb27 | |||
| e004384e46 | |||
| e835e61f3a | |||
| 4130e5db3d | |||
| c8c73eae59 | |||
| c39efa6ba6 | |||
| 935d9d899c | |||
| 189229bbf9 | |||
| b6c0217405 | |||
| 995ff5902f | |||
| b0211ff7f8 | |||
| 0027ce1f6e | |||
| 06f18186dc | |||
| 2c570f641c | |||
| fa68d66c98 | |||
| 32e790a47e | |||
| ddf3d1a7ac | |||
| c9c8485bc1 | |||
| 31f2be3dce | |||
| 250c416474 | |||
| 12171ca961 | |||
| bae91855f5 | |||
| f29b4cd7cb | |||
| 0232a987bb | |||
| 6a87e35bef | |||
| 8fd1558179 | |||
| 04cfcf91d9 | |||
| 3ec5ce9416 | |||
| 016554e437 | |||
| bb7dd7b646 | |||
| 9c82b34be7 | |||
| 54fbdcf467 | |||
| 2e9289bcbf | |||
| 7fd0ccdffc | |||
| ed49c7c246 | |||
| 0d90064e9e | |||
| 9158d0fd12 | |||
| c4b11ec54e | |||
| 9e4019c5ff | |||
| 96edfb560b | |||
| f6c52f4f41 | |||
| 7d94e5f33b | |||
| e8a9d213e4 | |||
| 0998256fc5 | |||
| a459075d26 | |||
| 70283a9f4a | |||
| e1b37a066d | |||
| d7c9c61420 | |||
| 79fd17ee63 | |||
| 7a3a6e3855 | |||
| f83996cd9f | |||
| 7da73cbcca | |||
| 299b8096bc | |||
| aed52d1632 | |||
| 9dce2aa735 | |||
| 953c3eab7b | |||
| 18fb522660 | |||
| bd6f070882 | |||
| a3fdef4ed4 | |||
| dfd6ea3fca | |||
| 71a0f6ef85 | |||
| d02c4da483 | |||
| df595ae313 | |||
| b4d21e7113 | |||
| d722d6de36 | |||
| 76ae1019b9 | |||
| a7f18b2297 | |||
| d3632368e6 | |||
| 5a3fe7a8d1 | |||
| be301f224d | |||
| db6c7ff084 | |||
| 268dc9b308 | |||
| 549b0fc526 | |||
| 42b72fe246 | |||
| f65d0dc081 | |||
| af5d2e8e5f | |||
| 5159edbf0e | |||
| 4a2bf65fea | |||
| db5c304e29 | |||
| a0d721e109 | |||
| 2c10fda399 | |||
| 7ca20adc6d | |||
| e0e64bcdf6 | |||
| 499cef3c2b | |||
| 2571767204 | |||
| 36ecff71ae | |||
| a3c8510c05 | |||
| 042e1d5d0b | |||
| ae17c775dc | |||
| 8ce613bb3a | |||
| 9d2299ed0b | |||
| 35db3665b3 | |||
| 5a5913828c | |||
| 448d6bef37 | |||
| 7056fdf2be | |||
| 3d81fd714b | |||
| 58a9082411 | |||
| 99a64edea8 | |||
| d75b521af8 | |||
| 296c8f6a4a | |||
| 99cd8de234 | |||
| 5590be7a8c | |||
| f084e6bbd0 | |||
| cd719b08bd | |||
| 90e560bb75 | |||
| 9182dd7e5d | |||
| f739e3e05d | |||
| e7a044a2d1 | |||
| ca72db23d2 | |||
| e4a2a705ad | |||
| bb91bb5e83 | |||
| 4760c3c0b5 | |||
| 1631e96a98 | |||
| 61c1261e4e | |||
| 956e1d8d90 | |||
| 453a5ac1d0 | |||
| 64d5fa1efd | |||
| 9d1d63afca | |||
| 44d4e7c500 | |||
| f89f01f9d8 | |||
| 640cb1bb8d | |||
| a81dc43fcd | |||
| 8a1f32b6a5 | |||
| f9c2216ffa | |||
| 959f8b32d5 | |||
| 13f1357b7f | |||
| 3ce5fb8e5c | |||
| 46e8898f65 | |||
| 3003b10e0a | |||
| 0dc74545c0 | |||
| 254be4eeb2 | |||
| 541ef9247c | |||
| e1a29266b2 | |||
| fc3a57ff96 | |||
| 0cf85b24df | |||
| eaba3d7349 | |||
| 57e59c14c8 | |||
| 0815c45bcd | |||
| 023a3a98a1 | |||
| 86221269f9 | |||
| d9ddc5d4cd | |||
| a7f7701b64 | |||
| fd563e3274 | |||
| d09d33bc2d | |||
| 7083391931 | |||
| 0f77139253 | |||
| 5b28b7dbc7 | |||
| 85fcb7b8df | |||
| 8b181c812f | |||
| f01682ee01 | |||
| aa57a89a21 | |||
| 7327be97aa | |||
| 63f881a5f0 | |||
| dc0e63a48a | |||
| f117bb64fc | |||
| 54209c1639 | |||
| ec505bac41 | |||
| 2154662826 | |||
| 9ab52caf02 | |||
| bc61ad9ec8 | |||
| b0a6d61d73 | |||
| 371b24b17c | |||
| 79d4e81984 | |||
| 7e77a38cbc | |||
| d6b79b9963 | |||
| 6f86573247 | |||
| 45be87afc6 | |||
| 5daf7983d1 | |||
| f23e5ce2da | |||
| e56b7c8419 | |||
| 2359c07ddf | |||
| bc63339df3 | |||
| a2e213bc7b | |||
| 6bfd4dfecf | |||
| 99ab3d43a7 | |||
| 353c876172 | |||
| d61e31bae6 | |||
| f3b96d4998 | |||
| abbecb3e73 | |||
| b39d9364d8 | |||
| c7c16f805c | |||
| f37cc5f5e1 | |||
| 3a4bee1096 | |||
| c1a31ec9f7 | |||
| f70ded8936 | |||
| aa26f8eb40 | |||
| cda2f0a162 | |||
| aeb76ef174 | |||
| e7c03ccdce | |||
| d9cc27cb29 | |||
| 0ea61a74be | |||
| 007ecfbb29 | |||
| 9cd0475c08 | |||
| 8452708560 | |||
| 16781ba09a | |||
| 09ff5b5416 | |||
| f093c9d39d | |||
| 2035cbbd5d | |||
| 5df535b7c2 | |||
| 232c931f40 | |||
| f4dbb0c820 | |||
| 9058620cec | |||
| 2489252099 | |||
| 87dd685224 | |||
| abfa4ad8bc | |||
| 3163d1269a | |||
| 1c6ca09992 | |||
| d73db17ee3 | |||
| 127ab9114f | |||
| d53f3b5596 | |||
| d41a5bb97d | |||
| 551d2fabcc | |||
| db40d26d08 | |||
| 525b55b1e9 | |||
| ce0829d711 | |||
| ac790fc49b | |||
| f4757032e7 | |||
| d1a70c3f05 | |||
| d8419762c1 | |||
| 60a7405165 | |||
| 1ae9dacb4b | |||
| 69f49c8d39 | |||
| 822597db49 | |||
| 7fa5ee54b1 | |||
| da80d649fd | |||
| 61673451ff | |||
| 599f61a1e0 | |||
| 0e3bac8132 | |||
| fa9370b741 | |||
| 5881dcb887 | |||
| a2b8305096 | |||
| bd4da4474b | |||
| dc5b5ee9c6 | |||
| 299eb54308 | |||
| 8d9ca46e0a | |||
| b2080756fc | |||
| 9d0ec13596 | |||
| 6816ad5ed8 | |||
| 4e8690906c | |||
| f0b72b8121 | |||
| 7a68ac6615 | |||
| f131f84e13 | |||
| 6aa26a26d5 | |||
| fd617fad00 | |||
| d20eb11c9e | |||
| c8d453e915 | |||
| b293ed3061 | |||
| 64311faa68 | |||
| 26c92f056a | |||
| ebc1bafb03 | |||
| 9dae70da79 | |||
| f57bc1a21b | |||
| af27b716e5 | |||
| 7c9c19b2a2 | |||
| 3b2f51602d | |||
| ae6b30907d | |||
| 77c52ea701 | |||
| 3c00e41ec0 | |||
| 340c1cc68d | |||
| 2c79f2af6e | |||
| 4fafc34e49 | |||
| d456fb797a | |||
| 458eda1321 | |||
| 54f926b11d | |||
| a75d756a6f | |||
| 863613293e | |||
| 9af5cce4c7 | |||
| e0906096c5 | |||
| 4549f2a9cc | |||
| f4979422dd | |||
| 5a705c2468 | |||
| 36762f0eaf | |||
| ac8a5d18d3 | |||
| 70a01cd444 | |||
| 959404e0e2 | |||
| 887bcfdf65 | |||
| 40ccd26b19 | |||
| 4412398c4b | |||
| 942d7a118a | |||
| 070b034cd5 | |||
| 9d78d317ae | |||
| 045f740892 | |||
| b13806c150 | |||
| 4f6582cb66 | |||
| 1b3093fe3a | |||
| 237b704172 | |||
| 4d93f48f09 | |||
| ed01d2ee3b | |||
| 386202895f | |||
| 0883810592 | |||
| faca86620d | |||
| 6c23061a7d | |||
| 33446acf47 | |||
| 0a0a9d4fe9 | |||
| 9199b6b7eb | |||
| 2c5106ed06 | |||
| 6ed1541ef5 | |||
| 736aaf348b | |||
| f0edd26998 | |||
| ff1bfd01ba | |||
| 2ceb4f81e2 | |||
| 259805947e | |||
| 66c32e40e8 | |||
| edfae9e78a | |||
| d1ba46b6e1 | |||
| c7b9394daf | |||
| ab42f81c75 | |||
| 8b7b99f8d5 | |||
| 4a64d34001 | |||
| 95821f0132 | |||
| a2a97e57f0 | |||
| f2ebcee7c4 | |||
| eed963e972 | |||
| 7ba8f11688 | |||
| aa10faa591 | |||
| 358f55db6a | |||
| c8c48640e6 | |||
| 0cac6ab615 | |||
| 2617598b7a | |||
| 8eea891718 | |||
| 386245a264 | |||
| 7d81ecbea6 | |||
| 8cf8fc6794 | |||
| da0712ee7d | |||
| a6f840b4dc | |||
| 0d5dc9a6e7 | |||
| d81d3fa8cd | |||
| c102780693 | |||
| 7f9dbc45b1 | |||
| 08e538e2e6 | |||
| bd4b4292ef | |||
| e12a1be1ca | |||
| a74c014425 | |||
| a2360de3f3 | |||
| 0e83c67525 | |||
| 1aefb50259 | |||
| ec194b6374 | |||
| f8ff8c0638 | |||
| 54c3e5c913 | |||
| 70c63c1208 | |||
| bc7906e6d6 | |||
| ae1bde1aa1 | |||
| a8a256f9b5 | |||
| 8285a149d8 | |||
| 2a72d76d6f | |||
| 2d8e4a6544 | |||
| c721884cf5 | |||
| ee2b8f2e1b | |||
| a3e27019e4 | |||
| 7e88f57aaa | |||
| 902f8cf292 | |||
| f17c8c2eff | |||
| c75bda867b | |||
| 8c200c2156 | |||
| b0f7f4a991 | |||
| 01997f45ba | |||
| 251140fc88 | |||
| aea0fa9fd5 | |||
| 912356133a | |||
| 250a95b6fe | |||
| fd67eafc65 | |||
| 4c72377bbf | |||
| 7d8f55ec7c | |||
| 0ea20a0d52 | |||
| 5cf37ca89f | |||
| 3453710d10 | |||
| 6e7828e1d2 | |||
| c96e4750d8 | |||
| 7bcfb4654f | |||
| 976963ab6d | |||
| 5a0db84b6c | |||
| 5a38a9c0ee | |||
| 956e69bf3a | |||
| f1975b0213 | |||
| e866c35462 | |||
| 57e8a11d17 | |||
| f9950da3e3 | |||
| aa42c0ff8e | |||
| 06da34d47a | |||
| 5cae08f2c3 | |||
| 8f31b139b8 | |||
| ce4be668fe | |||
| 2e8b40004e | |||
| 1e8482356c | |||
| e9c591b101 | |||
| ee96a6a588 | |||
| 92b99f3273 | |||
| ee75416e3e | |||
| d86d12e911 | |||
| 2844d9597b | |||
| dd1e2726f3 | |||
| f18a032190 | |||
| 9cbde6c9fd | |||
| f4e4992a4a | |||
| 31506f0771 | |||
| 85c2c138d2 | |||
| c11104fed5 | |||
| dfc01c68cd | |||
| 496cef956b | |||
| b315c20756 | |||
| c6278c15a8 | |||
| 0a0a6b2a4d | |||
| 1f7fc4d7a3 | |||
| 8ece321df3 | |||
| 1d7dcdb6c3 | |||
| 60183eebc3 | |||
| 36ca80d004 | |||
| 3f451f3042 | |||
| c980dca234 | |||
| f879cac1e7 | |||
| ad510b2cd3 | |||
| c74c708ed8 | |||
| e053e21af6 | |||
| 7a64601428 | |||
| b85ec2b9b6 | |||
| d56a9cfe6a | |||
| a32f270a47 | |||
| 8197f24dbc | |||
| ef1698fd6d | |||
| 56888644a6 | |||
| c613416af3 | |||
| 22ecb78b51 | |||
| a6b245e46f | |||
| 0ae2767ae6 | |||
| e64263653a | |||
| d2b842ce07 | |||
| d8371d0b3c | |||
| e7140a36c0 | |||
| aa744cadc8 | |||
| 63cac3c3cc | |||
| bcff763b6e | |||
| 9ac2989edd | |||
| 1d60a609a9 | |||
| 4560176640 | |||
| 31a9966b9d | |||
| c57cb6e89c | |||
| b6596cdb19 | |||
| 9213d5cb3b | |||
| 682ff8936d | |||
| f08a69e629 | |||
| fadbab3781 | |||
| 3ca3c7f1c6 | |||
| daee41e0d6 | |||
| 21000f13a1 | |||
| a0e74c4db4 | |||
| 073342c887 | |||
| 6346d8eeaa | |||
| 094c416a80 | |||
| 99f5f8e76b | |||
| cd4e053e5e | |||
| 2dc2bc4ab5 | |||
| e219211ff6 | |||
| df9fd1d3ae | |||
| 2e613a6ffc | |||
| f5994e84a2 | |||
| c93857922a | |||
| 6391128b41 | |||
| 7c5480eb96 | |||
| 67312653d7 | |||
| e81b431701 | |||
| 695300929a | |||
| 82b415c9c1 | |||
| d89a915b74 | |||
| ac8dfd9386 | |||
| 1f6bfdea80 | |||
| 70e66e81e5 | |||
| f0c1063a70 | |||
| 09165916fa | |||
| c134a48016 | |||
| 75336dfc84 | |||
| 3f9e09a615 | |||
| 01486f6896 | |||
| 56c3f94ba3 | |||
| 073c0ebba3 | |||
| 362789a379 | |||
| 7f1d087cba | |||
| 3bd2c68eb4 | |||
| 71efc5bda8 | |||
| f4d9297127 | |||
| 220e298417 | |||
| f7813fad1c | |||
| 8b37734244 | |||
| bbfff771d7 | |||
| 24f21583cd | |||
| 09c1be9674 | |||
| af528552d6 | |||
| 20549a50cb | |||
| 8e840e1519 | |||
| f56a309432 | |||
| 0904df84e2 | |||
| fca42949a3 | |||
| a1825ee741 | |||
| 84b6fcd02c | |||
| ccb9233934 | |||
| 10ff071e33 | |||
| 390bffa81b | |||
| 0c9b1e7969 | |||
| 6a0d498c8e | |||
| 401ba1b879 | |||
| 07be13caa3 | |||
| 6d3a0c9506 | |||
| 0042954490 | |||
| 8a4149accc | |||
| b98fa1c397 | |||
| c6b826d796 | |||
| 2860c3be3e | |||
| 4b43480fe8 | |||
| 8b036d8a82 | |||
| c46525b70b | |||
| 955542a654 | |||
| 2f1d5b6b04 | |||
| 151b8ed3a6 | |||
| b235022c61 | |||
| c10633f93a | |||
| 0d577aba26 | |||
| c09bc2c608 | |||
| fb87a05fe8 | |||
| 4d9b096663 | |||
| 29d7e31d89 | |||
| dca121e903 | |||
| 0af4127fd1 | |||
| a1eb49627a | |||
| 02038036ff | |||
| f60d9fbe29 | |||
| cc53db6652 | |||
| a64fbe8928 | |||
| eec540b227 | |||
| 77761e7bad | |||
| 40cd59207b | |||
| 3bca90b249 | |||
| c4ee6d9b73 | |||
| 085c903229 | |||
| 8a40e30d08 | |||
| 63a8861c19 | |||
| fb44838176 | |||
| 53ccdefc01 | |||
| 9857537053 | |||
| b95a41ad72 | |||
| 6f0abbb71a | |||
| 4ca9f70b59 | |||
| e18fc29bbf | |||
| 79d6e9cd32 | |||
| aefe1325df | |||
| 11dc92dc0a | |||
| bdeb44aeb2 | |||
| e1323fc1b7 | |||
| 3ac950248d | |||
| bef40851af | |||
| 9a52a30d2f | |||
| fc163218c4 | |||
| 19ac0adf03 | |||
| ac81c1dd1f | |||
| 6cc5a886ae | |||
| 9cbf3461f7 | |||
| 25004d4eee | |||
| 56236dfd3f | |||
| 68f336bd99 | |||
| 50973ec77c | |||
| f82e08cf45 | |||
| 91a131aa6c | |||
| 3039925b27 | |||
| 8220cf37da | |||
| 0cb9711a15 | |||
| 055461ae41 | |||
| 89e6dfff71 | |||
| 5c8f91b229 | |||
| 8284ebd94c | |||
| 187323a606 | |||
| 6b877c35da | |||
| deed8439d5 | |||
| 6305632493 | |||
| eb6d330bb7 | |||
| 246d1f1f70 | |||
| 5360ae2cc5 | |||
| e16eb3d0cb | |||
| ca6f90dc6d | |||
| 835a7dbf0e | |||
| 225eb1b1a0 | |||
| b8a903efbe | |||
| 7c22bbd3ad | |||
| 13e371af73 | |||
| ae36e0899f | |||
| b73c405013 | |||
| 8de6d3ff77 | |||
| fd43558586 | |||
| d0bf509fa1 | |||
| d6ec08ba89 | |||
| 65bf3ba260 | |||
| bed598ce7f | |||
| b1a16a298c | |||
| fee593a07f | |||
| fc8e23dec5 | |||
| a3ddf464a2 | |||
| a68f469030 | |||
| f7c0a963f1 | |||
| 5b06607476 | |||
| 6b68b59032 | |||
| 0a89cd1a58 | |||
| ca45ff1ae6 | |||
| 1cbfafafd2 | |||
| f451994053 | |||
| f2a4073aea | |||
| ec83db8978 | |||
| a8d4213317 | |||
| 9421c11346 | |||
| 0615b3c532 | |||
| 2d635c0192 | |||
| 88a3e1d306 | |||
| 0674fabd0d | |||
| c76a30af41 | |||
| 3c26734d60 | |||
| 2a7e34fe79 | |||
| b2f0040da7 | |||
| 7afe7375e1 | |||
| 90eb731ff1 | |||
| 491d42bb1c | |||
| 45c0f58dc6 | |||
| 1fe2dcaa2a | |||
| 075934a944 | |||
| ed4d7912c7 | |||
| 16eddc622e | |||
| bc91f15ed3 | |||
| 118529a6dc | |||
| 33694baea1 | |||
| f873890298 | |||
| 128d59c9cc | |||
| 2f57a559ac | |||
| 2f98f7c924 | |||
| 6233268964 | |||
| ddbf4a73f5 | |||
| 4bf64976c1 | |||
| 5677296d1b | |||
| cb75734896 | |||
| fc3bdf8c11 | |||
| 0fae47e974 | |||
| c278e60131 | |||
| 3c570421d3 | |||
| 7bb0fbed13 | |||
| 37e048a7e2 | |||
| 15a94d6cf7 | |||
| 40a18d38a8 | |||
| 952effa8b1 | |||
| 0dcf6436a8 | |||
| 95c5c4d64e | |||
| 543ea5730b | |||
| 643836007f | |||
| 24bad5dc7b | |||
| 57d61de25c | |||
| 5ef7590324 |
@@ -74,6 +74,7 @@ module.exports = {
|
|||||||
create_submit_args: "readonly",
|
create_submit_args: "readonly",
|
||||||
restart_reload: "readonly",
|
restart_reload: "readonly",
|
||||||
updateInput: "readonly",
|
updateInput: "readonly",
|
||||||
|
onEdit: "readonly",
|
||||||
//extraNetworks.js
|
//extraNetworks.js
|
||||||
requestGet: "readonly",
|
requestGet: "readonly",
|
||||||
popup: "readonly",
|
popup: "readonly",
|
||||||
@@ -87,5 +88,11 @@ module.exports = {
|
|||||||
modalNextImage: "readonly",
|
modalNextImage: "readonly",
|
||||||
// token-counters.js
|
// token-counters.js
|
||||||
setupTokenCounters: "readonly",
|
setupTokenCounters: "readonly",
|
||||||
|
// localStorage.js
|
||||||
|
localSet: "readonly",
|
||||||
|
localGet: "readonly",
|
||||||
|
localRemove: "readonly",
|
||||||
|
// resizeHandle.js
|
||||||
|
setupResizeHandle: "writable"
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,35 +1,55 @@
|
|||||||
name: Bug Report
|
name: Bug Report
|
||||||
description: You think somethings is broken in the UI
|
description: You think something is broken in the UI
|
||||||
title: "[Bug]: "
|
title: "[Bug]: "
|
||||||
labels: ["bug-report"]
|
labels: ["bug-report"]
|
||||||
|
|
||||||
body:
|
body:
|
||||||
- type: checkboxes
|
|
||||||
attributes:
|
|
||||||
label: Is there an existing issue for this?
|
|
||||||
description: Please search to see if an issue already exists for the bug you encountered, and that it hasn't been fixed in a recent build/commit.
|
|
||||||
options:
|
|
||||||
- label: I have searched the existing issues and checked the recent builds/commits
|
|
||||||
required: true
|
|
||||||
- type: markdown
|
- type: markdown
|
||||||
attributes:
|
attributes:
|
||||||
value: |
|
value: |
|
||||||
*Please fill this form with as much information as possible, don't forget to fill "What OS..." and "What browsers" and *provide screenshots if possible**
|
> The title of the bug report should be short and descriptive.
|
||||||
|
> Use relevant keywords for searchability.
|
||||||
|
> Do not leave it blank, but also do not put an entire error log in it.
|
||||||
|
- type: checkboxes
|
||||||
|
attributes:
|
||||||
|
label: Checklist
|
||||||
|
description: |
|
||||||
|
Please perform basic debugging to see if extensions or configuration is the cause of the issue.
|
||||||
|
Basic debug procedure
|
||||||
|
1. Disable all third-party extensions - check if extension is the cause
|
||||||
|
2. Update extensions and webui - sometimes things just need to be updated
|
||||||
|
3. Backup and remove your config.json and ui-config.json - check if the issue is caused by bad configuration
|
||||||
|
4. Delete venv with third-party extensions disabled - sometimes extensions might cause wrong libraries to be installed
|
||||||
|
5. Try a fresh installation webui in a different directory - see if a clean installation solves the issue
|
||||||
|
Before making a issue report please, check that the issue hasn't been reported recently.
|
||||||
|
options:
|
||||||
|
- label: The issue exists after disabling all extensions
|
||||||
|
- label: The issue exists on a clean installation of webui
|
||||||
|
- label: The issue is caused by an extension, but I believe it is caused by a bug in the webui
|
||||||
|
- label: The issue exists in the current version of the webui
|
||||||
|
- label: The issue has not been reported before recently
|
||||||
|
- label: The issue has been reported before but has not been fixed yet
|
||||||
|
- type: markdown
|
||||||
|
attributes:
|
||||||
|
value: |
|
||||||
|
> Please fill this form with as much information as possible. Don't forget to "Upload Sysinfo" and "What browsers" and provide screenshots if possible
|
||||||
- type: textarea
|
- type: textarea
|
||||||
id: what-did
|
id: what-did
|
||||||
attributes:
|
attributes:
|
||||||
label: What happened?
|
label: What happened?
|
||||||
description: Tell us what happened in a very clear and simple way
|
description: Tell us what happened in a very clear and simple way
|
||||||
|
placeholder: |
|
||||||
|
txt2img is not working as intended.
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
- type: textarea
|
- type: textarea
|
||||||
id: steps
|
id: steps
|
||||||
attributes:
|
attributes:
|
||||||
label: Steps to reproduce the problem
|
label: Steps to reproduce the problem
|
||||||
description: Please provide us with precise step by step information on how to reproduce the bug
|
description: Please provide us with precise step by step instructions on how to reproduce the bug
|
||||||
value: |
|
placeholder: |
|
||||||
1. Go to ....
|
1. Go to ...
|
||||||
2. Press ....
|
2. Press ...
|
||||||
3. ...
|
3. ...
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
@@ -37,64 +57,9 @@ body:
|
|||||||
id: what-should
|
id: what-should
|
||||||
attributes:
|
attributes:
|
||||||
label: What should have happened?
|
label: What should have happened?
|
||||||
description: Tell what you think the normal behavior should be
|
description: Tell us what you think the normal behavior should be
|
||||||
validations:
|
placeholder: |
|
||||||
required: true
|
WebUI should ...
|
||||||
- type: input
|
|
||||||
id: commit
|
|
||||||
attributes:
|
|
||||||
label: Version or Commit where the problem happens
|
|
||||||
description: "Which webui version or commit are you running ? (Do not write *Latest Version/repo/commit*, as this means nothing and will have changed by the time we read your issue. Rather, copy the **Version: v1.2.3** link at the bottom of the UI, or from the cmd/terminal if you can't launch it.)"
|
|
||||||
validations:
|
|
||||||
required: true
|
|
||||||
- type: dropdown
|
|
||||||
id: py-version
|
|
||||||
attributes:
|
|
||||||
label: What Python version are you running on ?
|
|
||||||
multiple: false
|
|
||||||
options:
|
|
||||||
- Python 3.10.x
|
|
||||||
- Python 3.11.x (above, no supported yet)
|
|
||||||
- Python 3.9.x (below, no recommended)
|
|
||||||
- type: dropdown
|
|
||||||
id: platforms
|
|
||||||
attributes:
|
|
||||||
label: What platforms do you use to access the UI ?
|
|
||||||
multiple: true
|
|
||||||
options:
|
|
||||||
- Windows
|
|
||||||
- Linux
|
|
||||||
- MacOS
|
|
||||||
- iOS
|
|
||||||
- Android
|
|
||||||
- Other/Cloud
|
|
||||||
- type: dropdown
|
|
||||||
id: device
|
|
||||||
attributes:
|
|
||||||
label: What device are you running WebUI on?
|
|
||||||
multiple: true
|
|
||||||
options:
|
|
||||||
- Nvidia GPUs (RTX 20 above)
|
|
||||||
- Nvidia GPUs (GTX 16 below)
|
|
||||||
- AMD GPUs (RX 6000 above)
|
|
||||||
- AMD GPUs (RX 5000 below)
|
|
||||||
- CPU
|
|
||||||
- Other GPUs
|
|
||||||
- type: dropdown
|
|
||||||
id: cross_attention_opt
|
|
||||||
attributes:
|
|
||||||
label: Cross attention optimization
|
|
||||||
description: What cross attention optimization are you using, Settings -> Optimizations -> Cross attention optimization
|
|
||||||
multiple: false
|
|
||||||
options:
|
|
||||||
- Automatic
|
|
||||||
- xformers
|
|
||||||
- sdp-no-mem
|
|
||||||
- sdp
|
|
||||||
- Doggettx
|
|
||||||
- V1
|
|
||||||
- InvokeAI
|
|
||||||
- "None "
|
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
- type: dropdown
|
- type: dropdown
|
||||||
@@ -108,26 +73,25 @@ body:
|
|||||||
- Brave
|
- Brave
|
||||||
- Apple Safari
|
- Apple Safari
|
||||||
- Microsoft Edge
|
- Microsoft Edge
|
||||||
|
- Android
|
||||||
|
- iOS
|
||||||
|
- Other
|
||||||
- type: textarea
|
- type: textarea
|
||||||
id: cmdargs
|
id: sysinfo
|
||||||
attributes:
|
attributes:
|
||||||
label: Command Line Arguments
|
label: Sysinfo
|
||||||
description: Are you using any launching parameters/command line arguments (modified webui-user .bat/.sh) ? If yes, please write them below. Write "No" otherwise.
|
description: System info file, generated by WebUI. You can generate it in settings, on the Sysinfo page. Drag the file into the field to upload it. If you submit your report without including the sysinfo file, the report will be closed. If needed, review the report to make sure it includes no personal information you don't want to share. If you can't start WebUI, you can use --dump-sysinfo commandline argument to generate the file.
|
||||||
render: Shell
|
placeholder: |
|
||||||
validations:
|
1. Go to WebUI Settings -> Sysinfo -> Download system info.
|
||||||
required: true
|
If WebUI fails to launch, use --dump-sysinfo commandline argument to generate the file
|
||||||
- type: textarea
|
2. Upload the Sysinfo as a attached file, Do NOT paste it in as plain text.
|
||||||
id: extensions
|
|
||||||
attributes:
|
|
||||||
label: List of extensions
|
|
||||||
description: Are you using any extensions other than built-ins? If yes, provide a list, you can copy it at "Extensions" tab. Write "No" otherwise.
|
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
- type: textarea
|
- type: textarea
|
||||||
id: logs
|
id: logs
|
||||||
attributes:
|
attributes:
|
||||||
label: Console logs
|
label: Console logs
|
||||||
description: Please provide **full** cmd/terminal logs from the moment you started UI to the end of it, after your bug happened. If it's very long, provide a link to pastebin or similar service.
|
description: Please provide **full** cmd/terminal logs from the moment you started UI to the end of it, after the bug occured. If it's very long, provide a link to pastebin or similar service.
|
||||||
render: Shell
|
render: Shell
|
||||||
validations:
|
validations:
|
||||||
required: true
|
required: true
|
||||||
@@ -135,4 +99,7 @@ body:
|
|||||||
id: misc
|
id: misc
|
||||||
attributes:
|
attributes:
|
||||||
label: Additional information
|
label: Additional information
|
||||||
description: Please provide us with any relevant additional info or context.
|
description: |
|
||||||
|
Please provide us with any relevant additional info or context.
|
||||||
|
Examples:
|
||||||
|
I have updated my GPU driver recently.
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ jobs:
|
|||||||
# not to have GHA download an (at the time of writing) 4 GB cache
|
# not to have GHA download an (at the time of writing) 4 GB cache
|
||||||
# of PyTorch and other dependencies.
|
# of PyTorch and other dependencies.
|
||||||
- name: Install Ruff
|
- name: Install Ruff
|
||||||
run: pip install ruff==0.0.272
|
run: pip install ruff==0.1.6
|
||||||
- name: Run Ruff
|
- name: Run Ruff
|
||||||
run: ruff .
|
run: ruff .
|
||||||
lint-js:
|
lint-js:
|
||||||
|
|||||||
@@ -20,6 +20,12 @@ jobs:
|
|||||||
cache-dependency-path: |
|
cache-dependency-path: |
|
||||||
**/requirements*txt
|
**/requirements*txt
|
||||||
launch.py
|
launch.py
|
||||||
|
- name: Cache models
|
||||||
|
id: cache-models
|
||||||
|
uses: actions/cache@v3
|
||||||
|
with:
|
||||||
|
path: models
|
||||||
|
key: "2023-12-30"
|
||||||
- name: Install test dependencies
|
- name: Install test dependencies
|
||||||
run: pip install wait-for-it -r requirements-test.txt
|
run: pip install wait-for-it -r requirements-test.txt
|
||||||
env:
|
env:
|
||||||
@@ -33,6 +39,8 @@ jobs:
|
|||||||
TORCH_INDEX_URL: https://download.pytorch.org/whl/cpu
|
TORCH_INDEX_URL: https://download.pytorch.org/whl/cpu
|
||||||
WEBUI_LAUNCH_LIVE_OUTPUT: "1"
|
WEBUI_LAUNCH_LIVE_OUTPUT: "1"
|
||||||
PYTHONUNBUFFERED: "1"
|
PYTHONUNBUFFERED: "1"
|
||||||
|
- name: Print installed packages
|
||||||
|
run: pip freeze
|
||||||
- name: Start test server
|
- name: Start test server
|
||||||
run: >
|
run: >
|
||||||
python -m coverage run
|
python -m coverage run
|
||||||
@@ -49,7 +57,7 @@ jobs:
|
|||||||
2>&1 | tee output.txt &
|
2>&1 | tee output.txt &
|
||||||
- name: Run tests
|
- name: Run tests
|
||||||
run: |
|
run: |
|
||||||
wait-for-it --service 127.0.0.1:7860 -t 600
|
wait-for-it --service 127.0.0.1:7860 -t 20
|
||||||
python -m pytest -vv --junitxml=test/results.xml --cov . --cov-report=xml --verify-base-url test
|
python -m pytest -vv --junitxml=test/results.xml --cov . --cov-report=xml --verify-base-url test
|
||||||
- name: Kill test server
|
- name: Kill test server
|
||||||
if: always()
|
if: always()
|
||||||
|
|||||||
@@ -37,3 +37,4 @@ notification.mp3
|
|||||||
/node_modules
|
/node_modules
|
||||||
/package-lock.json
|
/package-lock.json
|
||||||
/.coverage*
|
/.coverage*
|
||||||
|
/test/test_outputs
|
||||||
|
|||||||
+328
@@ -1,12 +1,336 @@
|
|||||||
|
## 1.7.0
|
||||||
|
|
||||||
|
### Features:
|
||||||
|
* settings tab rework: add search field, add categories, split UI settings page into many
|
||||||
|
* add altdiffusion-m18 support ([#13364](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13364))
|
||||||
|
* support inference with LyCORIS GLora networks ([#13610](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13610))
|
||||||
|
* add lora-embedding bundle system ([#13568](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13568))
|
||||||
|
* option to move prompt from top row into generation parameters
|
||||||
|
* add support for SSD-1B ([#13865](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13865))
|
||||||
|
* support inference with OFT networks ([#13692](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13692))
|
||||||
|
* script metadata and DAG sorting mechanism ([#13944](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13944))
|
||||||
|
* support HyperTile optimization ([#13948](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13948))
|
||||||
|
* add support for SD 2.1 Turbo ([#14170](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14170))
|
||||||
|
* remove Train->Preprocessing tab and put all its functionality into Extras tab
|
||||||
|
* initial IPEX support for Intel Arc GPU ([#14171](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14171))
|
||||||
|
|
||||||
|
### Minor:
|
||||||
|
* allow reading model hash from images in img2img batch mode ([#12767](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12767))
|
||||||
|
* add option to align with sgm repo's sampling implementation ([#12818](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12818))
|
||||||
|
* extra field for lora metadata viewer: `ss_output_name` ([#12838](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12838))
|
||||||
|
* add action in settings page to calculate all SD checkpoint hashes ([#12909](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12909))
|
||||||
|
* add button to copy prompt to style editor ([#12975](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12975))
|
||||||
|
* add --skip-load-model-at-start option ([#13253](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13253))
|
||||||
|
* write infotext to gif images
|
||||||
|
* read infotext from gif images ([#13068](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13068))
|
||||||
|
* allow configuring the initial state of InputAccordion in ui-config.json ([#13189](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13189))
|
||||||
|
* allow editing whitespace delimiters for ctrl+up/ctrl+down prompt editing ([#13444](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13444))
|
||||||
|
* prevent accidentally closing popup dialogs ([#13480](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13480))
|
||||||
|
* added option to play notification sound or not ([#13631](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13631))
|
||||||
|
* show the preview image in the full screen image viewer if available ([#13459](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13459))
|
||||||
|
* support for webui.settings.bat ([#13638](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13638))
|
||||||
|
* add an option to not print stack traces on ctrl+c
|
||||||
|
* start/restart generation by Ctrl (Alt) + Enter ([#13644](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13644))
|
||||||
|
* update prompts_from_file script to allow concatenating entries with the general prompt ([#13733](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13733))
|
||||||
|
* added a visible checkbox to input accordion
|
||||||
|
* added an option to hide all txt2img/img2img parameters in an accordion ([#13826](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13826))
|
||||||
|
* added 'Path' sorting option for Extra network cards ([#13968](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13968))
|
||||||
|
* enable prompt hotkeys in style editor ([#13931](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13931))
|
||||||
|
* option to show batch img2img results in UI ([#14009](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14009))
|
||||||
|
* infotext updates: add option to disregard certain infotext fields, add option to not include VAE in infotext, add explanation to infotext settings page, move some options to infotext settings page
|
||||||
|
* add FP32 fallback support on sd_vae_approx ([#14046](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14046))
|
||||||
|
* support XYZ scripts / split hires path from unet ([#14126](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14126))
|
||||||
|
* allow use of mutiple styles csv files ([#14125](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14125))
|
||||||
|
|
||||||
|
### Extensions and API:
|
||||||
|
* update gradio to 3.41.2
|
||||||
|
* support installed extensions list api ([#12774](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12774))
|
||||||
|
* update pnginfo API to return dict with parsed values
|
||||||
|
* add noisy latent to `ExtraNoiseParams` for callback ([#12856](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12856))
|
||||||
|
* show extension datetime in UTC ([#12864](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12864), [#12865](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12865), [#13281](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13281))
|
||||||
|
* add an option to choose how to combine hires fix and refiner
|
||||||
|
* include program version in info response. ([#13135](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13135))
|
||||||
|
* sd_unet support for SDXL
|
||||||
|
* patch DDPM.register_betas so that users can put given_betas in model yaml ([#13276](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13276))
|
||||||
|
* xyz_grid: add prepare ([#13266](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13266))
|
||||||
|
* allow multiple localization files with same language in extensions ([#13077](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13077))
|
||||||
|
* add onEdit function for js and rework token-counter.js to use it
|
||||||
|
* fix the key error exception when processing override_settings keys ([#13567](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13567))
|
||||||
|
* ability for extensions to return custom data via api in response.images ([#13463](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13463))
|
||||||
|
* call state.jobnext() before postproces*() ([#13762](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13762))
|
||||||
|
* add option to set notification sound volume ([#13884](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13884))
|
||||||
|
* update Ruff to 0.1.6 ([#14059](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14059))
|
||||||
|
* add Block component creation callback ([#14119](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14119))
|
||||||
|
* catch uncaught exception with ui creation scripts ([#14120](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14120))
|
||||||
|
* use extension name for determining an extension is installed in the index ([#14063](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14063))
|
||||||
|
* update is_installed() from launch_utils.py to fix reinstalling already installed packages ([#14192](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14192))
|
||||||
|
|
||||||
|
### Bug Fixes:
|
||||||
|
* fix pix2pix producing bad results
|
||||||
|
* fix defaults settings page breaking when any of main UI tabs are hidden
|
||||||
|
* fix error that causes some extra networks to be disabled if both <lora:> and <lyco:> are present in the prompt
|
||||||
|
* fix for Reload UI function: if you reload UI on one tab, other opened tabs will no longer stop working
|
||||||
|
* prevent duplicate resize handler ([#12795](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12795))
|
||||||
|
* small typo: vae resolve bug ([#12797](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12797))
|
||||||
|
* hide broken image crop tool ([#12792](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12792))
|
||||||
|
* don't show hidden samplers in dropdown for XYZ script ([#12780](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12780))
|
||||||
|
* fix style editing dialog breaking if it's opened in both img2img and txt2img tabs
|
||||||
|
* hide --gradio-auth and --api-auth values from /internal/sysinfo report
|
||||||
|
* add missing infotext for RNG in options ([#12819](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12819))
|
||||||
|
* fix notification not playing when built-in webui tab is inactive ([#12834](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12834))
|
||||||
|
* honor `--skip-install` for extension installers ([#12832](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12832))
|
||||||
|
* don't print blank stdout in extension installers ([#12833](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12833), [#12855](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12855))
|
||||||
|
* get progressbar to display correctly in extensions tab
|
||||||
|
* keep order in list of checkpoints when loading model that doesn't have a checksum
|
||||||
|
* fix inpainting models in txt2img creating black pictures
|
||||||
|
* fix generation params regex ([#12876](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12876))
|
||||||
|
* fix batch img2img output dir with script ([#12926](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12926))
|
||||||
|
* fix #13080 - Hypernetwork/TI preview generation ([#13084](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13084))
|
||||||
|
* fix bug with sigma min/max overrides. ([#12995](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12995))
|
||||||
|
* more accurate check for enabling cuDNN benchmark on 16XX cards ([#12924](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12924))
|
||||||
|
* don't use multicond parser for negative prompt counter ([#13118](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13118))
|
||||||
|
* fix data-sort-name containing spaces ([#13412](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13412))
|
||||||
|
* update card on correct tab when editing metadata ([#13411](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13411))
|
||||||
|
* fix viewing/editing metadata when filename contains an apostrophe ([#13395](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13395))
|
||||||
|
* fix: --sd_model in "Prompts from file or textbox" script is not working ([#13302](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13302))
|
||||||
|
* better Support for Portable Git ([#13231](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13231))
|
||||||
|
* fix issues when webui_dir is not work_dir ([#13210](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13210))
|
||||||
|
* fix: lora-bias-backup don't reset cache ([#13178](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13178))
|
||||||
|
* account for customizable extra network separators whyen removing extra network text from the prompt ([#12877](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12877))
|
||||||
|
* re fix batch img2img output dir with script ([#13170](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13170))
|
||||||
|
* fix `--ckpt-dir` path separator and option use `short name` for checkpoint dropdown ([#13139](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13139))
|
||||||
|
* consolidated allowed preview formats, Fix extra network `.gif` not woking as preview ([#13121](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13121))
|
||||||
|
* fix venv_dir=- environment variable not working as expected on linux ([#13469](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13469))
|
||||||
|
* repair unload sd checkpoint button
|
||||||
|
* edit-attention fixes ([#13533](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13533))
|
||||||
|
* fix bug when using --gfpgan-models-path ([#13718](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13718))
|
||||||
|
* properly apply sort order for extra network cards when selected from dropdown
|
||||||
|
* fixes generation restart not working for some users when 'Ctrl+Enter' is pressed ([#13962](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13962))
|
||||||
|
* thread safe extra network list_items ([#13014](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13014))
|
||||||
|
* fix not able to exit metadata popup when pop up is too big ([#14156](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14156))
|
||||||
|
* fix auto focal point crop for opencv >= 4.8 ([#14121](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14121))
|
||||||
|
* make 'use-cpu all' actually apply to 'all' ([#14131](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14131))
|
||||||
|
* extras tab batch: actually use original filename
|
||||||
|
* make webui not crash when running with --disable-all-extensions option
|
||||||
|
|
||||||
|
### Other:
|
||||||
|
* non-local condition ([#12814](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12814))
|
||||||
|
* fix minor typos ([#12827](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12827))
|
||||||
|
* remove xformers Python version check ([#12842](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12842))
|
||||||
|
* style: file-metadata word-break ([#12837](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12837))
|
||||||
|
* revert SGM noise multiplier change for img2img because it breaks hires fix
|
||||||
|
* do not change quicksettings dropdown option when value returned is `None` ([#12854](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12854))
|
||||||
|
* [RC 1.6.0 - zoom is partly hidden] Update style.css ([#12839](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12839))
|
||||||
|
* chore: change extension time format ([#12851](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12851))
|
||||||
|
* WEBUI.SH - Use torch 2.1.0 release candidate for Navi 3 ([#12929](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12929))
|
||||||
|
* add Fallback at images.read_info_from_image if exif data was invalid ([#13028](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13028))
|
||||||
|
* update cmd arg description ([#12986](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12986))
|
||||||
|
* fix: update shared.opts.data when add_option ([#12957](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12957), [#13213](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13213))
|
||||||
|
* restore missing tooltips ([#12976](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12976))
|
||||||
|
* use default dropdown padding on mobile ([#12880](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12880))
|
||||||
|
* put enable console prompts option into settings from commandline args ([#13119](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13119))
|
||||||
|
* fix some deprecated types ([#12846](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12846))
|
||||||
|
* bump to torchsde==0.2.6 ([#13418](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13418))
|
||||||
|
* update dragdrop.js ([#13372](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13372))
|
||||||
|
* use orderdict as lru cache:opt/bug ([#13313](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13313))
|
||||||
|
* XYZ if not include sub grids do not save sub grid ([#13282](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13282))
|
||||||
|
* initialize state.time_start befroe state.job_count ([#13229](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13229))
|
||||||
|
* fix fieldname regex ([#13458](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13458))
|
||||||
|
* change denoising_strength default to None. ([#13466](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13466))
|
||||||
|
* fix regression ([#13475](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13475))
|
||||||
|
* fix IndexError ([#13630](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13630))
|
||||||
|
* fix: checkpoints_loaded:{checkpoint:state_dict}, model.load_state_dict issue in dict value empty ([#13535](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13535))
|
||||||
|
* update bug_report.yml ([#12991](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12991))
|
||||||
|
* requirements_versions httpx==0.24.1 ([#13839](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13839))
|
||||||
|
* fix parenthesis auto selection ([#13829](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13829))
|
||||||
|
* fix #13796 ([#13797](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13797))
|
||||||
|
* corrected a typo in `modules/cmd_args.py` ([#13855](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13855))
|
||||||
|
* feat: fix randn found element of type float at pos 2 ([#14004](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14004))
|
||||||
|
* adds tqdm handler to logging_config.py for progress bar integration ([#13996](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13996))
|
||||||
|
* hotfix: call shared.state.end() after postprocessing done ([#13977](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13977))
|
||||||
|
* fix dependency address patch 1 ([#13929](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13929))
|
||||||
|
* save sysinfo as .json ([#14035](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14035))
|
||||||
|
* move exception_records related methods to errors.py ([#14084](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14084))
|
||||||
|
* compatibility ([#13936](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13936))
|
||||||
|
* json.dump(ensure_ascii=False) ([#14108](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14108))
|
||||||
|
* dir buttons start with / so only the correct dir will be shown and no… ([#13957](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13957))
|
||||||
|
* alternate implementation for unet forward replacement that does not depend on hijack being applied
|
||||||
|
* re-add `keyedit_delimiters_whitespace` setting lost as part of commit e294e46 ([#14178](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14178))
|
||||||
|
* fix `save_samples` being checked early when saving masked composite ([#14177](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14177))
|
||||||
|
* slight optimization for mask and mask_composite ([#14181](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14181))
|
||||||
|
* add import_hook hack to work around basicsr/torchvision incompatibility ([#14186](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14186))
|
||||||
|
|
||||||
|
## 1.6.1
|
||||||
|
|
||||||
|
### Bug Fixes:
|
||||||
|
* fix an error causing the webui to fail to start ([#13839](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/13839))
|
||||||
|
|
||||||
|
## 1.6.0
|
||||||
|
|
||||||
|
### Features:
|
||||||
|
* refiner support [#12371](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12371)
|
||||||
|
* add NV option for Random number generator source setting, which allows to generate same pictures on CPU/AMD/Mac as on NVidia videocards
|
||||||
|
* add style editor dialog
|
||||||
|
* hires fix: add an option to use a different checkpoint for second pass ([#12181](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12181))
|
||||||
|
* option to keep multiple loaded models in memory ([#12227](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12227))
|
||||||
|
* new samplers: Restart, DPM++ 2M SDE Exponential, DPM++ 2M SDE Heun, DPM++ 2M SDE Heun Karras, DPM++ 2M SDE Heun Exponential, DPM++ 3M SDE, DPM++ 3M SDE Karras, DPM++ 3M SDE Exponential ([#12300](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12300), [#12519](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12519), [#12542](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12542))
|
||||||
|
* rework DDIM, PLMS, UniPC to use CFG denoiser same as in k-diffusion samplers:
|
||||||
|
* makes all of them work with img2img
|
||||||
|
* makes prompt composition posssible (AND)
|
||||||
|
* makes them available for SDXL
|
||||||
|
* always show extra networks tabs in the UI ([#11808](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/11808))
|
||||||
|
* use less RAM when creating models ([#11958](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/11958), [#12599](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12599))
|
||||||
|
* textual inversion inference support for SDXL
|
||||||
|
* extra networks UI: show metadata for SD checkpoints
|
||||||
|
* checkpoint merger: add metadata support
|
||||||
|
* prompt editing and attention: add support for whitespace after the number ([ red : green : 0.5 ]) (seed breaking change) ([#12177](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12177))
|
||||||
|
* VAE: allow selecting own VAE for each checkpoint (in user metadata editor)
|
||||||
|
* VAE: add selected VAE to infotext
|
||||||
|
* options in main UI: add own separate setting for txt2img and img2img, correctly read values from pasted infotext, add setting for column count ([#12551](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12551))
|
||||||
|
* add resize handle to txt2img and img2img tabs, allowing to change the amount of horizontable space given to generation parameters and resulting image gallery ([#12687](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12687), [#12723](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12723))
|
||||||
|
* change default behavior for batching cond/uncond -- now it's on by default, and is disabled by an UI setting (Optimizatios -> Batch cond/uncond) - if you are on lowvram/medvram and are getting OOM exceptions, you will need to enable it
|
||||||
|
* show current position in queue and make it so that requests are processed in the order of arrival ([#12707](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12707))
|
||||||
|
* add `--medvram-sdxl` flag that only enables `--medvram` for SDXL models
|
||||||
|
* prompt editing timeline has separate range for first pass and hires-fix pass (seed breaking change) ([#12457](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12457))
|
||||||
|
|
||||||
|
### Minor:
|
||||||
|
* img2img batch: RAM savings, VRAM savings, .tif, .tiff in img2img batch ([#12120](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12120), [#12514](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12514), [#12515](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12515))
|
||||||
|
* postprocessing/extras: RAM savings ([#12479](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12479))
|
||||||
|
* XYZ: in the axis labels, remove pathnames from model filenames
|
||||||
|
* XYZ: support hires sampler ([#12298](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12298))
|
||||||
|
* XYZ: new option: use text inputs instead of dropdowns ([#12491](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12491))
|
||||||
|
* add gradio version warning
|
||||||
|
* sort list of VAE checkpoints ([#12297](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12297))
|
||||||
|
* use transparent white for mask in inpainting, along with an option to select the color ([#12326](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12326))
|
||||||
|
* move some settings to their own section: img2img, VAE
|
||||||
|
* add checkbox to show/hide dirs for extra networks
|
||||||
|
* Add TAESD(or more) options for all the VAE encode/decode operation ([#12311](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12311))
|
||||||
|
* gradio theme cache, new gradio themes, along with explanation that the user can input his own values ([#12346](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12346), [#12355](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12355))
|
||||||
|
* sampler fixes/tweaks: s_tmax, s_churn, s_noise, s_tmax ([#12354](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12354), [#12356](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12356), [#12357](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12357), [#12358](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12358), [#12375](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12375), [#12521](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12521))
|
||||||
|
* update README.md with correct instructions for Linux installation ([#12352](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12352))
|
||||||
|
* option to not save incomplete images, on by default ([#12338](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12338))
|
||||||
|
* enable cond cache by default
|
||||||
|
* git autofix for repos that are corrupted ([#12230](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12230))
|
||||||
|
* allow to open images in new browser tab by middle mouse button ([#12379](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12379))
|
||||||
|
* automatically open webui in browser when running "locally" ([#12254](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12254))
|
||||||
|
* put commonly used samplers on top, make DPM++ 2M Karras the default choice
|
||||||
|
* zoom and pan: option to auto-expand a wide image, improved integration ([#12413](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12413), [#12727](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12727))
|
||||||
|
* option to cache Lora networks in memory
|
||||||
|
* rework hires fix UI to use accordion
|
||||||
|
* face restoration and tiling moved to settings - use "Options in main UI" setting if you want them back
|
||||||
|
* change quicksettings items to have variable width
|
||||||
|
* Lora: add Norm module, add support for bias ([#12503](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12503))
|
||||||
|
* Lora: output warnings in UI rather than fail for unfitting loras; switch to logging for error output in console
|
||||||
|
* support search and display of hashes for all extra network items ([#12510](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12510))
|
||||||
|
* add extra noise param for img2img operations ([#12564](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12564))
|
||||||
|
* support for Lora with bias ([#12584](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12584))
|
||||||
|
* make interrupt quicker ([#12634](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12634))
|
||||||
|
* configurable gallery height ([#12648](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12648))
|
||||||
|
* make results column sticky ([#12645](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12645))
|
||||||
|
* more hash filename patterns ([#12639](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12639))
|
||||||
|
* make image viewer actually fit the whole page ([#12635](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12635))
|
||||||
|
* make progress bar work independently from live preview display which results in it being updated a lot more often
|
||||||
|
* forbid Full live preview method for medvram and add a setting to undo the forbidding
|
||||||
|
* make it possible to localize tooltips and placeholders
|
||||||
|
* add option to align with sgm repo's sampling implementation ([#12818](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12818))
|
||||||
|
* Restore faces and Tiling generation parameters have been moved to settings out of main UI
|
||||||
|
* if you want to put them back into main UI, use `Options in main UI` setting on the UI page.
|
||||||
|
|
||||||
|
### Extensions and API:
|
||||||
|
* gradio 3.41.2
|
||||||
|
* also bump versions for packages: transformers, GitPython, accelerate, scikit-image, timm, tomesd
|
||||||
|
* support tooltip kwarg for gradio elements: gr.Textbox(label='hello', tooltip='world')
|
||||||
|
* properly clear the total console progressbar when using txt2img and img2img from API
|
||||||
|
* add cmd_arg --disable-extra-extensions and --disable-all-extensions ([#12294](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12294))
|
||||||
|
* shared.py and webui.py split into many files
|
||||||
|
* add --loglevel commandline argument for logging
|
||||||
|
* add a custom UI element that combines accordion and checkbox
|
||||||
|
* avoid importing gradio in tests because it spams warnings
|
||||||
|
* put infotext label for setting into OptionInfo definition rather than in a separate list
|
||||||
|
* make `StableDiffusionProcessingImg2Img.mask_blur` a property, make more inline with PIL `GaussianBlur` ([#12470](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12470))
|
||||||
|
* option to make scripts UI without gr.Group
|
||||||
|
* add a way for scripts to register a callback for before/after just a single component's creation
|
||||||
|
* use dataclass for StableDiffusionProcessing
|
||||||
|
* store patches for Lora in a specialized module instead of inside torch
|
||||||
|
* support http/https URLs in API ([#12663](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12663), [#12698](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12698))
|
||||||
|
* add extra noise callback ([#12616](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12616))
|
||||||
|
* dump current stack traces when exiting with SIGINT
|
||||||
|
* add type annotations for extra fields of shared.sd_model
|
||||||
|
|
||||||
|
### Bug Fixes:
|
||||||
|
* Don't crash if out of local storage quota for javascriot localStorage
|
||||||
|
* XYZ plot do not fail if an exception occurs
|
||||||
|
* fix missing TI hash in infotext if generation uses both negative and positive TI ([#12269](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12269))
|
||||||
|
* localization fixes ([#12307](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12307))
|
||||||
|
* fix sdxl model invalid configuration after the hijack
|
||||||
|
* correctly toggle extras checkbox for infotext paste ([#12304](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12304))
|
||||||
|
* open raw sysinfo link in new page ([#12318](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12318))
|
||||||
|
* prompt parser: Account for empty field in alternating words syntax ([#12319](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12319))
|
||||||
|
* add tab and carriage return to invalid filename chars ([#12327](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12327))
|
||||||
|
* fix api only Lora not working ([#12387](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12387))
|
||||||
|
* fix options in main UI misbehaving when there's just one element
|
||||||
|
* make it possible to use a sampler from infotext even if it's hidden in the dropdown
|
||||||
|
* fix styles missing from the prompt in infotext when making a grid of batch of multiplie images
|
||||||
|
* prevent bogus progress output in console when calculating hires fix dimensions
|
||||||
|
* fix --use-textbox-seed
|
||||||
|
* fix broken `Lora/Networks: use old method` option ([#12466](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12466))
|
||||||
|
* properly return `None` for VAE hash when using `--no-hashing` ([#12463](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12463))
|
||||||
|
* MPS/macOS fixes and optimizations ([#12526](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12526))
|
||||||
|
* add second_order to samplers that mistakenly didn't have it
|
||||||
|
* when refreshing cards in extra networks UI, do not discard user's custom resolution
|
||||||
|
* fix processing error that happens if batch_size is not a multiple of how many prompts/negative prompts there are ([#12509](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12509))
|
||||||
|
* fix inpaint upload for alpha masks ([#12588](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12588))
|
||||||
|
* fix exception when image sizes are not integers ([#12586](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12586))
|
||||||
|
* fix incorrect TAESD Latent scale ([#12596](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12596))
|
||||||
|
* auto add data-dir to gradio-allowed-path ([#12603](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12603))
|
||||||
|
* fix exception if extensuions dir is missing ([#12607](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12607))
|
||||||
|
* fix issues with api model-refresh and vae-refresh ([#12638](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12638))
|
||||||
|
* fix img2img background color for transparent images option not being used ([#12633](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12633))
|
||||||
|
* attempt to resolve NaN issue with unstable VAEs in fp32 mk2 ([#12630](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12630))
|
||||||
|
* implement missing undo hijack for SDXL
|
||||||
|
* fix xyz swap axes ([#12684](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12684))
|
||||||
|
* fix errors in backup/restore tab if any of config files are broken ([#12689](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12689))
|
||||||
|
* fix SD VAE switch error after model reuse ([#12685](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12685))
|
||||||
|
* fix trying to create images too large for the chosen format ([#12667](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12667))
|
||||||
|
* create Gradio temp directory if necessary ([#12717](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12717))
|
||||||
|
* prevent possible cache loss if exiting as it's being written by using an atomic operation to replace the cache with the new version
|
||||||
|
* set devices.dtype_unet correctly
|
||||||
|
* run RealESRGAN on GPU for non-CUDA devices ([#12737](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12737))
|
||||||
|
* prevent extra network buttons being obscured by description for very small card sizes ([#12745](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12745))
|
||||||
|
* fix error that causes some extra networks to be disabled if both <lora:> and <lyco:> are present in the prompt
|
||||||
|
* fix defaults settings page breaking when any of main UI tabs are hidden
|
||||||
|
* fix incorrect save/display of new values in Defaults page in settings
|
||||||
|
* fix for Reload UI function: if you reload UI on one tab, other opened tabs will no longer stop working
|
||||||
|
* fix an error that prevents VAE being reloaded after an option change if a VAE near the checkpoint exists ([#12797](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12737))
|
||||||
|
* hide broken image crop tool ([#12792](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12737))
|
||||||
|
* don't show hidden samplers in dropdown for XYZ script ([#12780](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12737))
|
||||||
|
* fix style editing dialog breaking if it's opened in both img2img and txt2img tabs
|
||||||
|
* fix a bug allowing users to bypass gradio and API authentication (reported by vysecurity)
|
||||||
|
* fix notification not playing when built-in webui tab is inactive ([#12834](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12834))
|
||||||
|
* honor `--skip-install` for extension installers ([#12832](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12832))
|
||||||
|
* don't print blank stdout in extension installers ([#12833](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12832), [#12855](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12855))
|
||||||
|
* do not change quicksettings dropdown option when value returned is `None` ([#12854](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/12854))
|
||||||
|
* get progressbar to display correctly in extensions tab
|
||||||
|
|
||||||
|
|
||||||
|
## 1.5.2
|
||||||
|
|
||||||
|
### Bug Fixes:
|
||||||
|
* fix memory leak when generation fails
|
||||||
|
* update doggettx cross attention optimization to not use an unreasonable amount of memory in some edge cases -- suggestion by MorkTheOrk
|
||||||
|
|
||||||
|
|
||||||
## 1.5.1
|
## 1.5.1
|
||||||
|
|
||||||
### Minor:
|
### Minor:
|
||||||
* support parsing text encoder blocks in some new LoRAs
|
* support parsing text encoder blocks in some new LoRAs
|
||||||
|
* delete scale checker script due to user demand
|
||||||
|
|
||||||
### Extensions and API:
|
### Extensions and API:
|
||||||
* add postprocess_batch_list script callback
|
* add postprocess_batch_list script callback
|
||||||
|
|
||||||
### Bug Fixes:
|
### Bug Fixes:
|
||||||
|
* fix TI training for SD1
|
||||||
* fix reload altclip model error
|
* fix reload altclip model error
|
||||||
* prepend the pythonpath instead of overriding it
|
* prepend the pythonpath instead of overriding it
|
||||||
* fix typo in SD_WEBUI_RESTARTING
|
* fix typo in SD_WEBUI_RESTARTING
|
||||||
@@ -15,6 +339,10 @@
|
|||||||
* restyle Startup profile for black users
|
* restyle Startup profile for black users
|
||||||
* fix webui not launching with --nowebui
|
* fix webui not launching with --nowebui
|
||||||
* catch exception for non git extensions
|
* catch exception for non git extensions
|
||||||
|
* fix some options missing from /sdapi/v1/options
|
||||||
|
* fix for extension update status always saying "unknown"
|
||||||
|
* fix display of extra network cards that have `<>` in the name
|
||||||
|
* update lora extension to work with python 3.8
|
||||||
|
|
||||||
|
|
||||||
## 1.5.0
|
## 1.5.0
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
cff-version: 1.2.0
|
||||||
|
message: "If you use this software, please cite it as below."
|
||||||
|
authors:
|
||||||
|
- given-names: AUTOMATIC1111
|
||||||
|
title: "Stable Diffusion Web UI"
|
||||||
|
date-released: 2022-08-22
|
||||||
|
url: "https://github.com/AUTOMATIC1111/stable-diffusion-webui"
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# Stable Diffusion web UI
|
# Stable Diffusion web UI
|
||||||
A browser interface based on Gradio library for Stable Diffusion.
|
A web interface for Stable Diffusion, implemented using Gradio library.
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@@ -78,7 +78,7 @@ A browser interface based on Gradio library for Stable Diffusion.
|
|||||||
- Clip skip
|
- Clip skip
|
||||||
- Hypernetworks
|
- Hypernetworks
|
||||||
- Loras (same as Hypernetworks but more pretty)
|
- Loras (same as Hypernetworks but more pretty)
|
||||||
- A sparate UI where you can choose, with preview, which embeddings, hypernetworks or Loras to add to your prompt
|
- A separate UI where you can choose, with preview, which embeddings, hypernetworks or Loras to add to your prompt
|
||||||
- Can select to load a different VAE from settings screen
|
- Can select to load a different VAE from settings screen
|
||||||
- Estimated completion time in progress bar
|
- Estimated completion time in progress bar
|
||||||
- API
|
- API
|
||||||
@@ -88,19 +88,23 @@ A browser interface based on Gradio library for Stable Diffusion.
|
|||||||
- [Alt-Diffusion](https://arxiv.org/abs/2211.06679) support - see [wiki](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#alt-diffusion) for instructions
|
- [Alt-Diffusion](https://arxiv.org/abs/2211.06679) support - see [wiki](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Features#alt-diffusion) for instructions
|
||||||
- Now without any bad letters!
|
- Now without any bad letters!
|
||||||
- Load checkpoints in safetensors format
|
- Load checkpoints in safetensors format
|
||||||
- Eased resolution restriction: generated image's domension must be a multiple of 8 rather than 64
|
- Eased resolution restriction: generated image's dimensions must be a multiple of 8 rather than 64
|
||||||
- Now with a license!
|
- Now with a license!
|
||||||
- Reorder elements in the UI from settings screen
|
- Reorder elements in the UI from settings screen
|
||||||
|
- [Segmind Stable Diffusion](https://huggingface.co/segmind/SSD-1B) support
|
||||||
|
|
||||||
## Installation and Running
|
## Installation and Running
|
||||||
Make sure the required [dependencies](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Dependencies) are met and follow the instructions available for both [NVidia](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Install-and-Run-on-NVidia-GPUs) (recommended) and [AMD](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Install-and-Run-on-AMD-GPUs) GPUs.
|
Make sure the required [dependencies](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Dependencies) are met and follow the instructions available for:
|
||||||
|
- [NVidia](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Install-and-Run-on-NVidia-GPUs) (recommended)
|
||||||
|
- [AMD](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Install-and-Run-on-AMD-GPUs) GPUs.
|
||||||
|
- [Intel CPUs, Intel GPUs (both integrated and discrete)](https://github.com/openvinotoolkit/stable-diffusion-webui/wiki/Installation-on-Intel-Silicon) (external wiki page)
|
||||||
|
|
||||||
Alternatively, use online services (like Google Colab):
|
Alternatively, use online services (like Google Colab):
|
||||||
|
|
||||||
- [List of Online Services](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Online-Services)
|
- [List of Online Services](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Online-Services)
|
||||||
|
|
||||||
### Installation on Windows 10/11 with NVidia-GPUs using release package
|
### Installation on Windows 10/11 with NVidia-GPUs using release package
|
||||||
1. Download `sd.webui.zip` from [v1.0.0-pre](https://github.com/AUTOMATIC1111/stable-diffusion-webui/releases/tag/v1.0.0-pre) and extract it's contents.
|
1. Download `sd.webui.zip` from [v1.0.0-pre](https://github.com/AUTOMATIC1111/stable-diffusion-webui/releases/tag/v1.0.0-pre) and extract its contents.
|
||||||
2. Run `update.bat`.
|
2. Run `update.bat`.
|
||||||
3. Run `run.bat`.
|
3. Run `run.bat`.
|
||||||
> For more details see [Install-and-Run-on-NVidia-GPUs](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Install-and-Run-on-NVidia-GPUs)
|
> For more details see [Install-and-Run-on-NVidia-GPUs](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Install-and-Run-on-NVidia-GPUs)
|
||||||
@@ -115,15 +119,17 @@ Alternatively, use online services (like Google Colab):
|
|||||||
1. Install the dependencies:
|
1. Install the dependencies:
|
||||||
```bash
|
```bash
|
||||||
# Debian-based:
|
# Debian-based:
|
||||||
sudo apt install wget git python3 python3-venv
|
sudo apt install wget git python3 python3-venv libgl1 libglib2.0-0
|
||||||
# Red Hat-based:
|
# Red Hat-based:
|
||||||
sudo dnf install wget git python3
|
sudo dnf install wget git python3 gperftools-libs libglvnd-glx
|
||||||
|
# openSUSE-based:
|
||||||
|
sudo zypper install wget git python3 libtcmalloc4 libglvnd
|
||||||
# Arch-based:
|
# Arch-based:
|
||||||
sudo pacman -S wget git python3
|
sudo pacman -S wget git python3
|
||||||
```
|
```
|
||||||
2. Navigate to the directory you would like the webui to be installed and execute the following command:
|
2. Navigate to the directory you would like the webui to be installed and execute the following command:
|
||||||
```bash
|
```bash
|
||||||
bash <(wget -qO- https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.sh)
|
wget -q https://raw.githubusercontent.com/AUTOMATIC1111/stable-diffusion-webui/master/webui.sh
|
||||||
```
|
```
|
||||||
3. Run `webui.sh`.
|
3. Run `webui.sh`.
|
||||||
4. Check `webui-user.sh` for options.
|
4. Check `webui-user.sh` for options.
|
||||||
@@ -143,13 +149,14 @@ For the purposes of getting Google and other search engines to crawl the wiki, h
|
|||||||
## Credits
|
## Credits
|
||||||
Licenses for borrowed code can be found in `Settings -> Licenses` screen, and also in `html/licenses.html` file.
|
Licenses for borrowed code can be found in `Settings -> Licenses` screen, and also in `html/licenses.html` file.
|
||||||
|
|
||||||
- Stable Diffusion - https://github.com/CompVis/stable-diffusion, https://github.com/CompVis/taming-transformers
|
- Stable Diffusion - https://github.com/Stability-AI/stablediffusion, https://github.com/CompVis/taming-transformers
|
||||||
- k-diffusion - https://github.com/crowsonkb/k-diffusion.git
|
- k-diffusion - https://github.com/crowsonkb/k-diffusion.git
|
||||||
- GFPGAN - https://github.com/TencentARC/GFPGAN.git
|
- Spandrel - https://github.com/chaiNNer-org/spandrel implementing
|
||||||
- CodeFormer - https://github.com/sczhou/CodeFormer
|
- GFPGAN - https://github.com/TencentARC/GFPGAN.git
|
||||||
- ESRGAN - https://github.com/xinntao/ESRGAN
|
- CodeFormer - https://github.com/sczhou/CodeFormer
|
||||||
- SwinIR - https://github.com/JingyunLiang/SwinIR
|
- ESRGAN - https://github.com/xinntao/ESRGAN
|
||||||
- Swin2SR - https://github.com/mv-lab/swin2sr
|
- SwinIR - https://github.com/JingyunLiang/SwinIR
|
||||||
|
- Swin2SR - https://github.com/mv-lab/swin2sr
|
||||||
- LDSR - https://github.com/Hafiidz/latent-diffusion
|
- LDSR - https://github.com/Hafiidz/latent-diffusion
|
||||||
- MiDaS - https://github.com/isl-org/MiDaS
|
- MiDaS - https://github.com/isl-org/MiDaS
|
||||||
- Ideas for optimizations - https://github.com/basujindal/stable-diffusion
|
- Ideas for optimizations - https://github.com/basujindal/stable-diffusion
|
||||||
@@ -169,5 +176,7 @@ Licenses for borrowed code can be found in `Settings -> Licenses` screen, and al
|
|||||||
- UniPC sampler - Wenliang Zhao - https://github.com/wl-zhao/UniPC
|
- UniPC sampler - Wenliang Zhao - https://github.com/wl-zhao/UniPC
|
||||||
- TAESD - Ollin Boer Bohan - https://github.com/madebyollin/taesd
|
- TAESD - Ollin Boer Bohan - https://github.com/madebyollin/taesd
|
||||||
- LyCORIS - KohakuBlueleaf
|
- LyCORIS - KohakuBlueleaf
|
||||||
|
- Restart sampling - lambertae - https://github.com/Newbeeer/diffusion_restart_sampling
|
||||||
|
- Hypertile - tfernd - https://github.com/tfernd/HyperTile
|
||||||
- Initial Gradio script - posted on 4chan by an Anonymous user. Thank you Anonymous user.
|
- Initial Gradio script - posted on 4chan by an Anonymous user. Thank you Anonymous user.
|
||||||
- (You)
|
- (You)
|
||||||
|
|||||||
@@ -0,0 +1,73 @@
|
|||||||
|
model:
|
||||||
|
base_learning_rate: 1.0e-04
|
||||||
|
target: ldm.models.diffusion.ddpm.LatentDiffusion
|
||||||
|
params:
|
||||||
|
linear_start: 0.00085
|
||||||
|
linear_end: 0.0120
|
||||||
|
num_timesteps_cond: 1
|
||||||
|
log_every_t: 200
|
||||||
|
timesteps: 1000
|
||||||
|
first_stage_key: "jpg"
|
||||||
|
cond_stage_key: "txt"
|
||||||
|
image_size: 64
|
||||||
|
channels: 4
|
||||||
|
cond_stage_trainable: false # Note: different from the one we trained before
|
||||||
|
conditioning_key: crossattn
|
||||||
|
monitor: val/loss_simple_ema
|
||||||
|
scale_factor: 0.18215
|
||||||
|
use_ema: False
|
||||||
|
|
||||||
|
scheduler_config: # 10000 warmup steps
|
||||||
|
target: ldm.lr_scheduler.LambdaLinearScheduler
|
||||||
|
params:
|
||||||
|
warm_up_steps: [ 10000 ]
|
||||||
|
cycle_lengths: [ 10000000000000 ] # incredibly large number to prevent corner cases
|
||||||
|
f_start: [ 1.e-6 ]
|
||||||
|
f_max: [ 1. ]
|
||||||
|
f_min: [ 1. ]
|
||||||
|
|
||||||
|
unet_config:
|
||||||
|
target: ldm.modules.diffusionmodules.openaimodel.UNetModel
|
||||||
|
params:
|
||||||
|
image_size: 32 # unused
|
||||||
|
in_channels: 4
|
||||||
|
out_channels: 4
|
||||||
|
model_channels: 320
|
||||||
|
attention_resolutions: [ 4, 2, 1 ]
|
||||||
|
num_res_blocks: 2
|
||||||
|
channel_mult: [ 1, 2, 4, 4 ]
|
||||||
|
num_head_channels: 64
|
||||||
|
use_spatial_transformer: True
|
||||||
|
use_linear_in_transformer: True
|
||||||
|
transformer_depth: 1
|
||||||
|
context_dim: 1024
|
||||||
|
use_checkpoint: True
|
||||||
|
legacy: False
|
||||||
|
|
||||||
|
first_stage_config:
|
||||||
|
target: ldm.models.autoencoder.AutoencoderKL
|
||||||
|
params:
|
||||||
|
embed_dim: 4
|
||||||
|
monitor: val/rec_loss
|
||||||
|
ddconfig:
|
||||||
|
double_z: true
|
||||||
|
z_channels: 4
|
||||||
|
resolution: 256
|
||||||
|
in_channels: 3
|
||||||
|
out_ch: 3
|
||||||
|
ch: 128
|
||||||
|
ch_mult:
|
||||||
|
- 1
|
||||||
|
- 2
|
||||||
|
- 4
|
||||||
|
- 4
|
||||||
|
num_res_blocks: 2
|
||||||
|
attn_resolutions: []
|
||||||
|
dropout: 0.0
|
||||||
|
lossconfig:
|
||||||
|
target: torch.nn.Identity
|
||||||
|
|
||||||
|
cond_stage_config:
|
||||||
|
target: modules.xlmr_m18.BertSeriesModelWithTransformation
|
||||||
|
params:
|
||||||
|
name: "XLMR-Large"
|
||||||
@@ -0,0 +1,98 @@
|
|||||||
|
model:
|
||||||
|
target: sgm.models.diffusion.DiffusionEngine
|
||||||
|
params:
|
||||||
|
scale_factor: 0.13025
|
||||||
|
disable_first_stage_autocast: True
|
||||||
|
|
||||||
|
denoiser_config:
|
||||||
|
target: sgm.modules.diffusionmodules.denoiser.DiscreteDenoiser
|
||||||
|
params:
|
||||||
|
num_idx: 1000
|
||||||
|
|
||||||
|
weighting_config:
|
||||||
|
target: sgm.modules.diffusionmodules.denoiser_weighting.EpsWeighting
|
||||||
|
scaling_config:
|
||||||
|
target: sgm.modules.diffusionmodules.denoiser_scaling.EpsScaling
|
||||||
|
discretization_config:
|
||||||
|
target: sgm.modules.diffusionmodules.discretizer.LegacyDDPMDiscretization
|
||||||
|
|
||||||
|
network_config:
|
||||||
|
target: sgm.modules.diffusionmodules.openaimodel.UNetModel
|
||||||
|
params:
|
||||||
|
adm_in_channels: 2816
|
||||||
|
num_classes: sequential
|
||||||
|
use_checkpoint: True
|
||||||
|
in_channels: 9
|
||||||
|
out_channels: 4
|
||||||
|
model_channels: 320
|
||||||
|
attention_resolutions: [4, 2]
|
||||||
|
num_res_blocks: 2
|
||||||
|
channel_mult: [1, 2, 4]
|
||||||
|
num_head_channels: 64
|
||||||
|
use_spatial_transformer: True
|
||||||
|
use_linear_in_transformer: True
|
||||||
|
transformer_depth: [1, 2, 10] # note: the first is unused (due to attn_res starting at 2) 32, 16, 8 --> 64, 32, 16
|
||||||
|
context_dim: 2048
|
||||||
|
spatial_transformer_attn_type: softmax-xformers
|
||||||
|
legacy: False
|
||||||
|
|
||||||
|
conditioner_config:
|
||||||
|
target: sgm.modules.GeneralConditioner
|
||||||
|
params:
|
||||||
|
emb_models:
|
||||||
|
# crossattn cond
|
||||||
|
- is_trainable: False
|
||||||
|
input_key: txt
|
||||||
|
target: sgm.modules.encoders.modules.FrozenCLIPEmbedder
|
||||||
|
params:
|
||||||
|
layer: hidden
|
||||||
|
layer_idx: 11
|
||||||
|
# crossattn and vector cond
|
||||||
|
- is_trainable: False
|
||||||
|
input_key: txt
|
||||||
|
target: sgm.modules.encoders.modules.FrozenOpenCLIPEmbedder2
|
||||||
|
params:
|
||||||
|
arch: ViT-bigG-14
|
||||||
|
version: laion2b_s39b_b160k
|
||||||
|
freeze: True
|
||||||
|
layer: penultimate
|
||||||
|
always_return_pooled: True
|
||||||
|
legacy: False
|
||||||
|
# vector cond
|
||||||
|
- is_trainable: False
|
||||||
|
input_key: original_size_as_tuple
|
||||||
|
target: sgm.modules.encoders.modules.ConcatTimestepEmbedderND
|
||||||
|
params:
|
||||||
|
outdim: 256 # multiplied by two
|
||||||
|
# vector cond
|
||||||
|
- is_trainable: False
|
||||||
|
input_key: crop_coords_top_left
|
||||||
|
target: sgm.modules.encoders.modules.ConcatTimestepEmbedderND
|
||||||
|
params:
|
||||||
|
outdim: 256 # multiplied by two
|
||||||
|
# vector cond
|
||||||
|
- is_trainable: False
|
||||||
|
input_key: target_size_as_tuple
|
||||||
|
target: sgm.modules.encoders.modules.ConcatTimestepEmbedderND
|
||||||
|
params:
|
||||||
|
outdim: 256 # multiplied by two
|
||||||
|
|
||||||
|
first_stage_config:
|
||||||
|
target: sgm.models.autoencoder.AutoencoderKLInferenceWrapper
|
||||||
|
params:
|
||||||
|
embed_dim: 4
|
||||||
|
monitor: val/rec_loss
|
||||||
|
ddconfig:
|
||||||
|
attn_type: vanilla-xformers
|
||||||
|
double_z: true
|
||||||
|
z_channels: 4
|
||||||
|
resolution: 256
|
||||||
|
in_channels: 3
|
||||||
|
out_ch: 3
|
||||||
|
ch: 128
|
||||||
|
ch_mult: [1, 2, 4, 4]
|
||||||
|
num_res_blocks: 2
|
||||||
|
attn_resolutions: []
|
||||||
|
dropout: 0.0
|
||||||
|
lossconfig:
|
||||||
|
target: torch.nn.Identity
|
||||||
@@ -6,9 +6,14 @@ class ExtraNetworkLora(extra_networks.ExtraNetwork):
|
|||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__('lora')
|
super().__init__('lora')
|
||||||
|
|
||||||
|
self.errors = {}
|
||||||
|
"""mapping of network names to the number of errors the network had during operation"""
|
||||||
|
|
||||||
def activate(self, p, params_list):
|
def activate(self, p, params_list):
|
||||||
additional = shared.opts.sd_lora
|
additional = shared.opts.sd_lora
|
||||||
|
|
||||||
|
self.errors.clear()
|
||||||
|
|
||||||
if additional != "None" and additional in networks.available_networks and not any(x for x in params_list if x.items[0] == additional):
|
if additional != "None" and additional in networks.available_networks and not any(x for x in params_list if x.items[0] == additional):
|
||||||
p.all_prompts = [x + f"<lora:{additional}:{shared.opts.extra_networks_default_multiplier}>" for x in p.all_prompts]
|
p.all_prompts = [x + f"<lora:{additional}:{shared.opts.extra_networks_default_multiplier}>" for x in p.all_prompts]
|
||||||
params_list.append(extra_networks.ExtraNetworkParams(items=[additional, shared.opts.extra_networks_default_multiplier]))
|
params_list.append(extra_networks.ExtraNetworkParams(items=[additional, shared.opts.extra_networks_default_multiplier]))
|
||||||
@@ -56,4 +61,7 @@ class ExtraNetworkLora(extra_networks.ExtraNetwork):
|
|||||||
p.extra_generation_params["Lora hashes"] = ", ".join(network_hashes)
|
p.extra_generation_params["Lora hashes"] = ", ".join(network_hashes)
|
||||||
|
|
||||||
def deactivate(self, p):
|
def deactivate(self, p):
|
||||||
pass
|
if self.errors:
|
||||||
|
p.comment("Networks with errors: " + ", ".join(f"{k} ({v})" for k, v in self.errors.items()))
|
||||||
|
|
||||||
|
self.errors.clear()
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
import sys
|
||||||
|
import copy
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
|
class ColoredFormatter(logging.Formatter):
|
||||||
|
COLORS = {
|
||||||
|
"DEBUG": "\033[0;36m", # CYAN
|
||||||
|
"INFO": "\033[0;32m", # GREEN
|
||||||
|
"WARNING": "\033[0;33m", # YELLOW
|
||||||
|
"ERROR": "\033[0;31m", # RED
|
||||||
|
"CRITICAL": "\033[0;37;41m", # WHITE ON RED
|
||||||
|
"RESET": "\033[0m", # RESET COLOR
|
||||||
|
}
|
||||||
|
|
||||||
|
def format(self, record):
|
||||||
|
colored_record = copy.copy(record)
|
||||||
|
levelname = colored_record.levelname
|
||||||
|
seq = self.COLORS.get(levelname, self.COLORS["RESET"])
|
||||||
|
colored_record.levelname = f"{seq}{levelname}{self.COLORS['RESET']}"
|
||||||
|
return super().format(colored_record)
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger("lora")
|
||||||
|
logger.propagate = False
|
||||||
|
|
||||||
|
|
||||||
|
if not logger.handlers:
|
||||||
|
handler = logging.StreamHandler(sys.stdout)
|
||||||
|
handler.setFormatter(
|
||||||
|
ColoredFormatter("[%(name)s]-%(levelname)s: %(message)s")
|
||||||
|
)
|
||||||
|
logger.addHandler(handler)
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
import torch
|
||||||
|
|
||||||
|
import networks
|
||||||
|
from modules import patches
|
||||||
|
|
||||||
|
|
||||||
|
class LoraPatches:
|
||||||
|
def __init__(self):
|
||||||
|
self.Linear_forward = patches.patch(__name__, torch.nn.Linear, 'forward', networks.network_Linear_forward)
|
||||||
|
self.Linear_load_state_dict = patches.patch(__name__, torch.nn.Linear, '_load_from_state_dict', networks.network_Linear_load_state_dict)
|
||||||
|
self.Conv2d_forward = patches.patch(__name__, torch.nn.Conv2d, 'forward', networks.network_Conv2d_forward)
|
||||||
|
self.Conv2d_load_state_dict = patches.patch(__name__, torch.nn.Conv2d, '_load_from_state_dict', networks.network_Conv2d_load_state_dict)
|
||||||
|
self.GroupNorm_forward = patches.patch(__name__, torch.nn.GroupNorm, 'forward', networks.network_GroupNorm_forward)
|
||||||
|
self.GroupNorm_load_state_dict = patches.patch(__name__, torch.nn.GroupNorm, '_load_from_state_dict', networks.network_GroupNorm_load_state_dict)
|
||||||
|
self.LayerNorm_forward = patches.patch(__name__, torch.nn.LayerNorm, 'forward', networks.network_LayerNorm_forward)
|
||||||
|
self.LayerNorm_load_state_dict = patches.patch(__name__, torch.nn.LayerNorm, '_load_from_state_dict', networks.network_LayerNorm_load_state_dict)
|
||||||
|
self.MultiheadAttention_forward = patches.patch(__name__, torch.nn.MultiheadAttention, 'forward', networks.network_MultiheadAttention_forward)
|
||||||
|
self.MultiheadAttention_load_state_dict = patches.patch(__name__, torch.nn.MultiheadAttention, '_load_from_state_dict', networks.network_MultiheadAttention_load_state_dict)
|
||||||
|
|
||||||
|
def undo(self):
|
||||||
|
self.Linear_forward = patches.undo(__name__, torch.nn.Linear, 'forward')
|
||||||
|
self.Linear_load_state_dict = patches.undo(__name__, torch.nn.Linear, '_load_from_state_dict')
|
||||||
|
self.Conv2d_forward = patches.undo(__name__, torch.nn.Conv2d, 'forward')
|
||||||
|
self.Conv2d_load_state_dict = patches.undo(__name__, torch.nn.Conv2d, '_load_from_state_dict')
|
||||||
|
self.GroupNorm_forward = patches.undo(__name__, torch.nn.GroupNorm, 'forward')
|
||||||
|
self.GroupNorm_load_state_dict = patches.undo(__name__, torch.nn.GroupNorm, '_load_from_state_dict')
|
||||||
|
self.LayerNorm_forward = patches.undo(__name__, torch.nn.LayerNorm, 'forward')
|
||||||
|
self.LayerNorm_load_state_dict = patches.undo(__name__, torch.nn.LayerNorm, '_load_from_state_dict')
|
||||||
|
self.MultiheadAttention_forward = patches.undo(__name__, torch.nn.MultiheadAttention, 'forward')
|
||||||
|
self.MultiheadAttention_load_state_dict = patches.undo(__name__, torch.nn.MultiheadAttention, '_load_from_state_dict')
|
||||||
|
|
||||||
@@ -19,3 +19,50 @@ def rebuild_cp_decomposition(up, down, mid):
|
|||||||
up = up.reshape(up.size(0), -1)
|
up = up.reshape(up.size(0), -1)
|
||||||
down = down.reshape(down.size(0), -1)
|
down = down.reshape(down.size(0), -1)
|
||||||
return torch.einsum('n m k l, i n, m j -> i j k l', mid, up, down)
|
return torch.einsum('n m k l, i n, m j -> i j k l', mid, up, down)
|
||||||
|
|
||||||
|
|
||||||
|
# copied from https://github.com/KohakuBlueleaf/LyCORIS/blob/dev/lycoris/modules/lokr.py
|
||||||
|
def factorization(dimension: int, factor:int=-1) -> tuple[int, int]:
|
||||||
|
'''
|
||||||
|
return a tuple of two value of input dimension decomposed by the number closest to factor
|
||||||
|
second value is higher or equal than first value.
|
||||||
|
|
||||||
|
In LoRA with Kroneckor Product, first value is a value for weight scale.
|
||||||
|
secon value is a value for weight.
|
||||||
|
|
||||||
|
Becuase of non-commutative property, A⊗B ≠ B⊗A. Meaning of two matrices is slightly different.
|
||||||
|
|
||||||
|
examples)
|
||||||
|
factor
|
||||||
|
-1 2 4 8 16 ...
|
||||||
|
127 -> 1, 127 127 -> 1, 127 127 -> 1, 127 127 -> 1, 127 127 -> 1, 127
|
||||||
|
128 -> 8, 16 128 -> 2, 64 128 -> 4, 32 128 -> 8, 16 128 -> 8, 16
|
||||||
|
250 -> 10, 25 250 -> 2, 125 250 -> 2, 125 250 -> 5, 50 250 -> 10, 25
|
||||||
|
360 -> 8, 45 360 -> 2, 180 360 -> 4, 90 360 -> 8, 45 360 -> 12, 30
|
||||||
|
512 -> 16, 32 512 -> 2, 256 512 -> 4, 128 512 -> 8, 64 512 -> 16, 32
|
||||||
|
1024 -> 32, 32 1024 -> 2, 512 1024 -> 4, 256 1024 -> 8, 128 1024 -> 16, 64
|
||||||
|
'''
|
||||||
|
|
||||||
|
if factor > 0 and (dimension % factor) == 0:
|
||||||
|
m = factor
|
||||||
|
n = dimension // factor
|
||||||
|
if m > n:
|
||||||
|
n, m = m, n
|
||||||
|
return m, n
|
||||||
|
if factor < 0:
|
||||||
|
factor = dimension
|
||||||
|
m, n = 1, dimension
|
||||||
|
length = m + n
|
||||||
|
while m<n:
|
||||||
|
new_m = m + 1
|
||||||
|
while dimension%new_m != 0:
|
||||||
|
new_m += 1
|
||||||
|
new_n = dimension // new_m
|
||||||
|
if new_m + new_n > length or new_m>factor:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
m, n = new_m, new_n
|
||||||
|
if m > n:
|
||||||
|
n, m = m, n
|
||||||
|
return m, n
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
|
from __future__ import annotations
|
||||||
import os
|
import os
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
import enum
|
import enum
|
||||||
|
|
||||||
|
import torch.nn as nn
|
||||||
|
import torch.nn.functional as F
|
||||||
|
|
||||||
from modules import sd_models, cache, errors, hashes, shared
|
from modules import sd_models, cache, errors, hashes, shared
|
||||||
|
|
||||||
NetworkWeights = namedtuple('NetworkWeights', ['network_key', 'sd_key', 'w', 'sd_module'])
|
NetworkWeights = namedtuple('NetworkWeights', ['network_key', 'sd_key', 'w', 'sd_module'])
|
||||||
@@ -92,6 +96,7 @@ class Network: # LoraModule
|
|||||||
self.unet_multiplier = 1.0
|
self.unet_multiplier = 1.0
|
||||||
self.dyn_dim = None
|
self.dyn_dim = None
|
||||||
self.modules = {}
|
self.modules = {}
|
||||||
|
self.bundle_embeddings = {}
|
||||||
self.mtime = None
|
self.mtime = None
|
||||||
|
|
||||||
self.mentioned_name = None
|
self.mentioned_name = None
|
||||||
@@ -113,6 +118,29 @@ class NetworkModule:
|
|||||||
if hasattr(self.sd_module, 'weight'):
|
if hasattr(self.sd_module, 'weight'):
|
||||||
self.shape = self.sd_module.weight.shape
|
self.shape = self.sd_module.weight.shape
|
||||||
|
|
||||||
|
self.ops = None
|
||||||
|
self.extra_kwargs = {}
|
||||||
|
if isinstance(self.sd_module, nn.Conv2d):
|
||||||
|
self.ops = F.conv2d
|
||||||
|
self.extra_kwargs = {
|
||||||
|
'stride': self.sd_module.stride,
|
||||||
|
'padding': self.sd_module.padding
|
||||||
|
}
|
||||||
|
elif isinstance(self.sd_module, nn.Linear):
|
||||||
|
self.ops = F.linear
|
||||||
|
elif isinstance(self.sd_module, nn.LayerNorm):
|
||||||
|
self.ops = F.layer_norm
|
||||||
|
self.extra_kwargs = {
|
||||||
|
'normalized_shape': self.sd_module.normalized_shape,
|
||||||
|
'eps': self.sd_module.eps
|
||||||
|
}
|
||||||
|
elif isinstance(self.sd_module, nn.GroupNorm):
|
||||||
|
self.ops = F.group_norm
|
||||||
|
self.extra_kwargs = {
|
||||||
|
'num_groups': self.sd_module.num_groups,
|
||||||
|
'eps': self.sd_module.eps
|
||||||
|
}
|
||||||
|
|
||||||
self.dim = None
|
self.dim = None
|
||||||
self.bias = weights.w.get("bias")
|
self.bias = weights.w.get("bias")
|
||||||
self.alpha = weights.w["alpha"].item() if "alpha" in weights.w else None
|
self.alpha = weights.w["alpha"].item() if "alpha" in weights.w else None
|
||||||
@@ -132,10 +160,10 @@ class NetworkModule:
|
|||||||
|
|
||||||
return 1.0
|
return 1.0
|
||||||
|
|
||||||
def finalize_updown(self, updown, orig_weight, output_shape):
|
def finalize_updown(self, updown, orig_weight, output_shape, ex_bias=None):
|
||||||
if self.bias is not None:
|
if self.bias is not None:
|
||||||
updown = updown.reshape(self.bias.shape)
|
updown = updown.reshape(self.bias.shape)
|
||||||
updown += self.bias.to(orig_weight.device, dtype=orig_weight.dtype)
|
updown += self.bias.to(orig_weight.device, dtype=updown.dtype)
|
||||||
updown = updown.reshape(output_shape)
|
updown = updown.reshape(output_shape)
|
||||||
|
|
||||||
if len(output_shape) == 4:
|
if len(output_shape) == 4:
|
||||||
@@ -144,11 +172,19 @@ class NetworkModule:
|
|||||||
if orig_weight.size().numel() == updown.size().numel():
|
if orig_weight.size().numel() == updown.size().numel():
|
||||||
updown = updown.reshape(orig_weight.shape)
|
updown = updown.reshape(orig_weight.shape)
|
||||||
|
|
||||||
return updown * self.calc_scale() * self.multiplier()
|
if ex_bias is not None:
|
||||||
|
ex_bias = ex_bias * self.multiplier()
|
||||||
|
|
||||||
|
return updown * self.calc_scale() * self.multiplier(), ex_bias
|
||||||
|
|
||||||
def calc_updown(self, target):
|
def calc_updown(self, target):
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def forward(self, x, y):
|
def forward(self, x, y):
|
||||||
raise NotImplementedError()
|
"""A general forward implementation for all modules"""
|
||||||
|
if self.ops is None:
|
||||||
|
raise NotImplementedError()
|
||||||
|
else:
|
||||||
|
updown, ex_bias = self.calc_updown(self.sd_module.weight)
|
||||||
|
return y + self.ops(x, weight=updown, bias=ex_bias, **self.extra_kwargs)
|
||||||
|
|
||||||
|
|||||||
@@ -14,9 +14,14 @@ class NetworkModuleFull(network.NetworkModule):
|
|||||||
super().__init__(net, weights)
|
super().__init__(net, weights)
|
||||||
|
|
||||||
self.weight = weights.w.get("diff")
|
self.weight = weights.w.get("diff")
|
||||||
|
self.ex_bias = weights.w.get("diff_b")
|
||||||
|
|
||||||
def calc_updown(self, orig_weight):
|
def calc_updown(self, orig_weight):
|
||||||
output_shape = self.weight.shape
|
output_shape = self.weight.shape
|
||||||
updown = self.weight.to(orig_weight.device, dtype=orig_weight.dtype)
|
updown = self.weight.to(orig_weight.device)
|
||||||
|
if self.ex_bias is not None:
|
||||||
|
ex_bias = self.ex_bias.to(orig_weight.device)
|
||||||
|
else:
|
||||||
|
ex_bias = None
|
||||||
|
|
||||||
return self.finalize_updown(updown, orig_weight, output_shape)
|
return self.finalize_updown(updown, orig_weight, output_shape, ex_bias)
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
|
||||||
|
import network
|
||||||
|
|
||||||
|
class ModuleTypeGLora(network.ModuleType):
|
||||||
|
def create_module(self, net: network.Network, weights: network.NetworkWeights):
|
||||||
|
if all(x in weights.w for x in ["a1.weight", "a2.weight", "alpha", "b1.weight", "b2.weight"]):
|
||||||
|
return NetworkModuleGLora(net, weights)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
# adapted from https://github.com/KohakuBlueleaf/LyCORIS
|
||||||
|
class NetworkModuleGLora(network.NetworkModule):
|
||||||
|
def __init__(self, net: network.Network, weights: network.NetworkWeights):
|
||||||
|
super().__init__(net, weights)
|
||||||
|
|
||||||
|
if hasattr(self.sd_module, 'weight'):
|
||||||
|
self.shape = self.sd_module.weight.shape
|
||||||
|
|
||||||
|
self.w1a = weights.w["a1.weight"]
|
||||||
|
self.w1b = weights.w["b1.weight"]
|
||||||
|
self.w2a = weights.w["a2.weight"]
|
||||||
|
self.w2b = weights.w["b2.weight"]
|
||||||
|
|
||||||
|
def calc_updown(self, orig_weight):
|
||||||
|
w1a = self.w1a.to(orig_weight.device)
|
||||||
|
w1b = self.w1b.to(orig_weight.device)
|
||||||
|
w2a = self.w2a.to(orig_weight.device)
|
||||||
|
w2b = self.w2b.to(orig_weight.device)
|
||||||
|
|
||||||
|
output_shape = [w1a.size(0), w1b.size(1)]
|
||||||
|
updown = ((w2b @ w1b) + ((orig_weight.to(dtype = w1a.dtype) @ w2a) @ w1a))
|
||||||
|
|
||||||
|
return self.finalize_updown(updown, orig_weight, output_shape)
|
||||||
@@ -27,16 +27,16 @@ class NetworkModuleHada(network.NetworkModule):
|
|||||||
self.t2 = weights.w.get("hada_t2")
|
self.t2 = weights.w.get("hada_t2")
|
||||||
|
|
||||||
def calc_updown(self, orig_weight):
|
def calc_updown(self, orig_weight):
|
||||||
w1a = self.w1a.to(orig_weight.device, dtype=orig_weight.dtype)
|
w1a = self.w1a.to(orig_weight.device)
|
||||||
w1b = self.w1b.to(orig_weight.device, dtype=orig_weight.dtype)
|
w1b = self.w1b.to(orig_weight.device)
|
||||||
w2a = self.w2a.to(orig_weight.device, dtype=orig_weight.dtype)
|
w2a = self.w2a.to(orig_weight.device)
|
||||||
w2b = self.w2b.to(orig_weight.device, dtype=orig_weight.dtype)
|
w2b = self.w2b.to(orig_weight.device)
|
||||||
|
|
||||||
output_shape = [w1a.size(0), w1b.size(1)]
|
output_shape = [w1a.size(0), w1b.size(1)]
|
||||||
|
|
||||||
if self.t1 is not None:
|
if self.t1 is not None:
|
||||||
output_shape = [w1a.size(1), w1b.size(1)]
|
output_shape = [w1a.size(1), w1b.size(1)]
|
||||||
t1 = self.t1.to(orig_weight.device, dtype=orig_weight.dtype)
|
t1 = self.t1.to(orig_weight.device)
|
||||||
updown1 = lyco_helpers.make_weight_cp(t1, w1a, w1b)
|
updown1 = lyco_helpers.make_weight_cp(t1, w1a, w1b)
|
||||||
output_shape += t1.shape[2:]
|
output_shape += t1.shape[2:]
|
||||||
else:
|
else:
|
||||||
@@ -45,7 +45,7 @@ class NetworkModuleHada(network.NetworkModule):
|
|||||||
updown1 = lyco_helpers.rebuild_conventional(w1a, w1b, output_shape)
|
updown1 = lyco_helpers.rebuild_conventional(w1a, w1b, output_shape)
|
||||||
|
|
||||||
if self.t2 is not None:
|
if self.t2 is not None:
|
||||||
t2 = self.t2.to(orig_weight.device, dtype=orig_weight.dtype)
|
t2 = self.t2.to(orig_weight.device)
|
||||||
updown2 = lyco_helpers.make_weight_cp(t2, w2a, w2b)
|
updown2 = lyco_helpers.make_weight_cp(t2, w2a, w2b)
|
||||||
else:
|
else:
|
||||||
updown2 = lyco_helpers.rebuild_conventional(w2a, w2b, output_shape)
|
updown2 = lyco_helpers.rebuild_conventional(w2a, w2b, output_shape)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class NetworkModuleIa3(network.NetworkModule):
|
|||||||
self.on_input = weights.w["on_input"].item()
|
self.on_input = weights.w["on_input"].item()
|
||||||
|
|
||||||
def calc_updown(self, orig_weight):
|
def calc_updown(self, orig_weight):
|
||||||
w = self.w.to(orig_weight.device, dtype=orig_weight.dtype)
|
w = self.w.to(orig_weight.device)
|
||||||
|
|
||||||
output_shape = [w.size(0), orig_weight.size(1)]
|
output_shape = [w.size(0), orig_weight.size(1)]
|
||||||
if self.on_input:
|
if self.on_input:
|
||||||
|
|||||||
@@ -37,22 +37,22 @@ class NetworkModuleLokr(network.NetworkModule):
|
|||||||
|
|
||||||
def calc_updown(self, orig_weight):
|
def calc_updown(self, orig_weight):
|
||||||
if self.w1 is not None:
|
if self.w1 is not None:
|
||||||
w1 = self.w1.to(orig_weight.device, dtype=orig_weight.dtype)
|
w1 = self.w1.to(orig_weight.device)
|
||||||
else:
|
else:
|
||||||
w1a = self.w1a.to(orig_weight.device, dtype=orig_weight.dtype)
|
w1a = self.w1a.to(orig_weight.device)
|
||||||
w1b = self.w1b.to(orig_weight.device, dtype=orig_weight.dtype)
|
w1b = self.w1b.to(orig_weight.device)
|
||||||
w1 = w1a @ w1b
|
w1 = w1a @ w1b
|
||||||
|
|
||||||
if self.w2 is not None:
|
if self.w2 is not None:
|
||||||
w2 = self.w2.to(orig_weight.device, dtype=orig_weight.dtype)
|
w2 = self.w2.to(orig_weight.device)
|
||||||
elif self.t2 is None:
|
elif self.t2 is None:
|
||||||
w2a = self.w2a.to(orig_weight.device, dtype=orig_weight.dtype)
|
w2a = self.w2a.to(orig_weight.device)
|
||||||
w2b = self.w2b.to(orig_weight.device, dtype=orig_weight.dtype)
|
w2b = self.w2b.to(orig_weight.device)
|
||||||
w2 = w2a @ w2b
|
w2 = w2a @ w2b
|
||||||
else:
|
else:
|
||||||
t2 = self.t2.to(orig_weight.device, dtype=orig_weight.dtype)
|
t2 = self.t2.to(orig_weight.device)
|
||||||
w2a = self.w2a.to(orig_weight.device, dtype=orig_weight.dtype)
|
w2a = self.w2a.to(orig_weight.device)
|
||||||
w2b = self.w2b.to(orig_weight.device, dtype=orig_weight.dtype)
|
w2b = self.w2b.to(orig_weight.device)
|
||||||
w2 = lyco_helpers.make_weight_cp(t2, w2a, w2b)
|
w2 = lyco_helpers.make_weight_cp(t2, w2a, w2b)
|
||||||
|
|
||||||
output_shape = [w1.size(0) * w2.size(0), w1.size(1) * w2.size(1)]
|
output_shape = [w1.size(0) * w2.size(0), w1.size(1) * w2.size(1)]
|
||||||
|
|||||||
@@ -61,13 +61,13 @@ class NetworkModuleLora(network.NetworkModule):
|
|||||||
return module
|
return module
|
||||||
|
|
||||||
def calc_updown(self, orig_weight):
|
def calc_updown(self, orig_weight):
|
||||||
up = self.up_model.weight.to(orig_weight.device, dtype=orig_weight.dtype)
|
up = self.up_model.weight.to(orig_weight.device)
|
||||||
down = self.down_model.weight.to(orig_weight.device, dtype=orig_weight.dtype)
|
down = self.down_model.weight.to(orig_weight.device)
|
||||||
|
|
||||||
output_shape = [up.size(0), down.size(1)]
|
output_shape = [up.size(0), down.size(1)]
|
||||||
if self.mid_model is not None:
|
if self.mid_model is not None:
|
||||||
# cp-decomposition
|
# cp-decomposition
|
||||||
mid = self.mid_model.weight.to(orig_weight.device, dtype=orig_weight.dtype)
|
mid = self.mid_model.weight.to(orig_weight.device)
|
||||||
updown = lyco_helpers.rebuild_cp_decomposition(up, down, mid)
|
updown = lyco_helpers.rebuild_cp_decomposition(up, down, mid)
|
||||||
output_shape += mid.shape[2:]
|
output_shape += mid.shape[2:]
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import network
|
||||||
|
|
||||||
|
|
||||||
|
class ModuleTypeNorm(network.ModuleType):
|
||||||
|
def create_module(self, net: network.Network, weights: network.NetworkWeights):
|
||||||
|
if all(x in weights.w for x in ["w_norm", "b_norm"]):
|
||||||
|
return NetworkModuleNorm(net, weights)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
class NetworkModuleNorm(network.NetworkModule):
|
||||||
|
def __init__(self, net: network.Network, weights: network.NetworkWeights):
|
||||||
|
super().__init__(net, weights)
|
||||||
|
|
||||||
|
self.w_norm = weights.w.get("w_norm")
|
||||||
|
self.b_norm = weights.w.get("b_norm")
|
||||||
|
|
||||||
|
def calc_updown(self, orig_weight):
|
||||||
|
output_shape = self.w_norm.shape
|
||||||
|
updown = self.w_norm.to(orig_weight.device)
|
||||||
|
|
||||||
|
if self.b_norm is not None:
|
||||||
|
ex_bias = self.b_norm.to(orig_weight.device)
|
||||||
|
else:
|
||||||
|
ex_bias = None
|
||||||
|
|
||||||
|
return self.finalize_updown(updown, orig_weight, output_shape, ex_bias)
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import torch
|
||||||
|
import network
|
||||||
|
from lyco_helpers import factorization
|
||||||
|
from einops import rearrange
|
||||||
|
|
||||||
|
|
||||||
|
class ModuleTypeOFT(network.ModuleType):
|
||||||
|
def create_module(self, net: network.Network, weights: network.NetworkWeights):
|
||||||
|
if all(x in weights.w for x in ["oft_blocks"]) or all(x in weights.w for x in ["oft_diag"]):
|
||||||
|
return NetworkModuleOFT(net, weights)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
# Supports both kohya-ss' implementation of COFT https://github.com/kohya-ss/sd-scripts/blob/main/networks/oft.py
|
||||||
|
# and KohakuBlueleaf's implementation of OFT/COFT https://github.com/KohakuBlueleaf/LyCORIS/blob/dev/lycoris/modules/diag_oft.py
|
||||||
|
class NetworkModuleOFT(network.NetworkModule):
|
||||||
|
def __init__(self, net: network.Network, weights: network.NetworkWeights):
|
||||||
|
|
||||||
|
super().__init__(net, weights)
|
||||||
|
|
||||||
|
self.lin_module = None
|
||||||
|
self.org_module: list[torch.Module] = [self.sd_module]
|
||||||
|
|
||||||
|
self.scale = 1.0
|
||||||
|
|
||||||
|
# kohya-ss
|
||||||
|
if "oft_blocks" in weights.w.keys():
|
||||||
|
self.is_kohya = True
|
||||||
|
self.oft_blocks = weights.w["oft_blocks"] # (num_blocks, block_size, block_size)
|
||||||
|
self.alpha = weights.w["alpha"] # alpha is constraint
|
||||||
|
self.dim = self.oft_blocks.shape[0] # lora dim
|
||||||
|
# LyCORIS
|
||||||
|
elif "oft_diag" in weights.w.keys():
|
||||||
|
self.is_kohya = False
|
||||||
|
self.oft_blocks = weights.w["oft_diag"]
|
||||||
|
# self.alpha is unused
|
||||||
|
self.dim = self.oft_blocks.shape[1] # (num_blocks, block_size, block_size)
|
||||||
|
|
||||||
|
is_linear = type(self.sd_module) in [torch.nn.Linear, torch.nn.modules.linear.NonDynamicallyQuantizableLinear]
|
||||||
|
is_conv = type(self.sd_module) in [torch.nn.Conv2d]
|
||||||
|
is_other_linear = type(self.sd_module) in [torch.nn.MultiheadAttention] # unsupported
|
||||||
|
|
||||||
|
if is_linear:
|
||||||
|
self.out_dim = self.sd_module.out_features
|
||||||
|
elif is_conv:
|
||||||
|
self.out_dim = self.sd_module.out_channels
|
||||||
|
elif is_other_linear:
|
||||||
|
self.out_dim = self.sd_module.embed_dim
|
||||||
|
|
||||||
|
if self.is_kohya:
|
||||||
|
self.constraint = self.alpha * self.out_dim
|
||||||
|
self.num_blocks = self.dim
|
||||||
|
self.block_size = self.out_dim // self.dim
|
||||||
|
else:
|
||||||
|
self.constraint = None
|
||||||
|
self.block_size, self.num_blocks = factorization(self.out_dim, self.dim)
|
||||||
|
|
||||||
|
def calc_updown(self, orig_weight):
|
||||||
|
oft_blocks = self.oft_blocks.to(orig_weight.device)
|
||||||
|
eye = torch.eye(self.block_size, device=self.oft_blocks.device)
|
||||||
|
|
||||||
|
if self.is_kohya:
|
||||||
|
block_Q = oft_blocks - oft_blocks.transpose(1, 2) # ensure skew-symmetric orthogonal matrix
|
||||||
|
norm_Q = torch.norm(block_Q.flatten())
|
||||||
|
new_norm_Q = torch.clamp(norm_Q, max=self.constraint)
|
||||||
|
block_Q = block_Q * ((new_norm_Q + 1e-8) / (norm_Q + 1e-8))
|
||||||
|
oft_blocks = torch.matmul(eye + block_Q, (eye - block_Q).float().inverse())
|
||||||
|
|
||||||
|
R = oft_blocks.to(orig_weight.device)
|
||||||
|
|
||||||
|
# This errors out for MultiheadAttention, might need to be handled up-stream
|
||||||
|
merged_weight = rearrange(orig_weight, '(k n) ... -> k n ...', k=self.num_blocks, n=self.block_size)
|
||||||
|
merged_weight = torch.einsum(
|
||||||
|
'k n m, k n ... -> k m ...',
|
||||||
|
R,
|
||||||
|
merged_weight
|
||||||
|
)
|
||||||
|
merged_weight = rearrange(merged_weight, 'k m ... -> (k m) ...')
|
||||||
|
|
||||||
|
updown = merged_weight.to(orig_weight.device) - orig_weight.to(merged_weight.dtype)
|
||||||
|
output_shape = orig_weight.shape
|
||||||
|
return self.finalize_updown(updown, orig_weight, output_shape)
|
||||||
@@ -1,17 +1,26 @@
|
|||||||
|
import gradio as gr
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
import lora_patches
|
||||||
import network
|
import network
|
||||||
import network_lora
|
import network_lora
|
||||||
|
import network_glora
|
||||||
import network_hada
|
import network_hada
|
||||||
import network_ia3
|
import network_ia3
|
||||||
import network_lokr
|
import network_lokr
|
||||||
import network_full
|
import network_full
|
||||||
|
import network_norm
|
||||||
|
import network_oft
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
from modules import shared, devices, sd_models, errors, scripts, sd_hijack
|
from modules import shared, devices, sd_models, errors, scripts, sd_hijack
|
||||||
|
import modules.textual_inversion.textual_inversion as textual_inversion
|
||||||
|
|
||||||
|
from lora_logger import logger
|
||||||
|
|
||||||
module_types = [
|
module_types = [
|
||||||
network_lora.ModuleTypeLora(),
|
network_lora.ModuleTypeLora(),
|
||||||
@@ -19,6 +28,9 @@ module_types = [
|
|||||||
network_ia3.ModuleTypeIa3(),
|
network_ia3.ModuleTypeIa3(),
|
||||||
network_lokr.ModuleTypeLokr(),
|
network_lokr.ModuleTypeLokr(),
|
||||||
network_full.ModuleTypeFull(),
|
network_full.ModuleTypeFull(),
|
||||||
|
network_norm.ModuleTypeNorm(),
|
||||||
|
network_glora.ModuleTypeGLora(),
|
||||||
|
network_oft.ModuleTypeOFT(),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@@ -31,6 +43,8 @@ suffix_conversion = {
|
|||||||
"resnets": {
|
"resnets": {
|
||||||
"conv1": "in_layers_2",
|
"conv1": "in_layers_2",
|
||||||
"conv2": "out_layers_3",
|
"conv2": "out_layers_3",
|
||||||
|
"norm1": "in_layers_0",
|
||||||
|
"norm2": "out_layers_0",
|
||||||
"time_emb_proj": "emb_layers_1",
|
"time_emb_proj": "emb_layers_1",
|
||||||
"conv_shortcut": "skip_connection",
|
"conv_shortcut": "skip_connection",
|
||||||
}
|
}
|
||||||
@@ -143,9 +157,20 @@ def load_network(name, network_on_disk):
|
|||||||
is_sd2 = 'model_transformer_resblocks' in shared.sd_model.network_layer_mapping
|
is_sd2 = 'model_transformer_resblocks' in shared.sd_model.network_layer_mapping
|
||||||
|
|
||||||
matched_networks = {}
|
matched_networks = {}
|
||||||
|
bundle_embeddings = {}
|
||||||
|
|
||||||
for key_network, weight in sd.items():
|
for key_network, weight in sd.items():
|
||||||
key_network_without_network_parts, network_part = key_network.split(".", 1)
|
key_network_without_network_parts, _, network_part = key_network.partition(".")
|
||||||
|
|
||||||
|
if key_network_without_network_parts == "bundle_emb":
|
||||||
|
emb_name, vec_name = network_part.split(".", 1)
|
||||||
|
emb_dict = bundle_embeddings.get(emb_name, {})
|
||||||
|
if vec_name.split('.')[0] == 'string_to_param':
|
||||||
|
_, k2 = vec_name.split('.', 1)
|
||||||
|
emb_dict['string_to_param'] = {k2: weight}
|
||||||
|
else:
|
||||||
|
emb_dict[vec_name] = weight
|
||||||
|
bundle_embeddings[emb_name] = emb_dict
|
||||||
|
|
||||||
key = convert_diffusers_name_to_compvis(key_network_without_network_parts, is_sd2)
|
key = convert_diffusers_name_to_compvis(key_network_without_network_parts, is_sd2)
|
||||||
sd_module = shared.sd_model.network_layer_mapping.get(key, None)
|
sd_module = shared.sd_model.network_layer_mapping.get(key, None)
|
||||||
@@ -168,6 +193,17 @@ def load_network(name, network_on_disk):
|
|||||||
key = key_network_without_network_parts.replace("lora_te1_text_model", "transformer_text_model")
|
key = key_network_without_network_parts.replace("lora_te1_text_model", "transformer_text_model")
|
||||||
sd_module = shared.sd_model.network_layer_mapping.get(key, None)
|
sd_module = shared.sd_model.network_layer_mapping.get(key, None)
|
||||||
|
|
||||||
|
# kohya_ss OFT module
|
||||||
|
elif sd_module is None and "oft_unet" in key_network_without_network_parts:
|
||||||
|
key = key_network_without_network_parts.replace("oft_unet", "diffusion_model")
|
||||||
|
sd_module = shared.sd_model.network_layer_mapping.get(key, None)
|
||||||
|
|
||||||
|
# KohakuBlueLeaf OFT module
|
||||||
|
if sd_module is None and "oft_diag" in key:
|
||||||
|
key = key_network_without_network_parts.replace("lora_unet", "diffusion_model")
|
||||||
|
key = key_network_without_network_parts.replace("lora_te1_text_model", "0_transformer_text_model")
|
||||||
|
sd_module = shared.sd_model.network_layer_mapping.get(key, None)
|
||||||
|
|
||||||
if sd_module is None:
|
if sd_module is None:
|
||||||
keys_failed_to_match[key_network] = key
|
keys_failed_to_match[key_network] = key
|
||||||
continue
|
continue
|
||||||
@@ -189,38 +225,62 @@ def load_network(name, network_on_disk):
|
|||||||
|
|
||||||
net.modules[key] = net_module
|
net.modules[key] = net_module
|
||||||
|
|
||||||
|
embeddings = {}
|
||||||
|
for emb_name, data in bundle_embeddings.items():
|
||||||
|
embedding = textual_inversion.create_embedding_from_data(data, emb_name, filename=network_on_disk.filename + "/" + emb_name)
|
||||||
|
embedding.loaded = None
|
||||||
|
embeddings[emb_name] = embedding
|
||||||
|
|
||||||
|
net.bundle_embeddings = embeddings
|
||||||
|
|
||||||
if keys_failed_to_match:
|
if keys_failed_to_match:
|
||||||
print(f"Failed to match keys when loading network {network_on_disk.filename}: {keys_failed_to_match}")
|
logging.debug(f"Network {network_on_disk.filename} didn't match keys: {keys_failed_to_match}")
|
||||||
|
|
||||||
return net
|
return net
|
||||||
|
|
||||||
|
|
||||||
|
def purge_networks_from_memory():
|
||||||
|
while len(networks_in_memory) > shared.opts.lora_in_memory_limit and len(networks_in_memory) > 0:
|
||||||
|
name = next(iter(networks_in_memory))
|
||||||
|
networks_in_memory.pop(name, None)
|
||||||
|
|
||||||
|
devices.torch_gc()
|
||||||
|
|
||||||
|
|
||||||
def load_networks(names, te_multipliers=None, unet_multipliers=None, dyn_dims=None):
|
def load_networks(names, te_multipliers=None, unet_multipliers=None, dyn_dims=None):
|
||||||
|
emb_db = sd_hijack.model_hijack.embedding_db
|
||||||
already_loaded = {}
|
already_loaded = {}
|
||||||
|
|
||||||
for net in loaded_networks:
|
for net in loaded_networks:
|
||||||
if net.name in names:
|
if net.name in names:
|
||||||
already_loaded[net.name] = net
|
already_loaded[net.name] = net
|
||||||
|
for emb_name, embedding in net.bundle_embeddings.items():
|
||||||
|
if embedding.loaded:
|
||||||
|
emb_db.register_embedding_by_name(None, shared.sd_model, emb_name)
|
||||||
|
|
||||||
loaded_networks.clear()
|
loaded_networks.clear()
|
||||||
|
|
||||||
networks_on_disk = [available_network_aliases.get(name, None) for name in names]
|
networks_on_disk = [available_networks.get(name, None) if name.lower() in forbidden_network_aliases else available_network_aliases.get(name, None) for name in names]
|
||||||
if any(x is None for x in networks_on_disk):
|
if any(x is None for x in networks_on_disk):
|
||||||
list_available_networks()
|
list_available_networks()
|
||||||
|
|
||||||
networks_on_disk = [available_network_aliases.get(name, None) for name in names]
|
networks_on_disk = [available_networks.get(name, None) if name.lower() in forbidden_network_aliases else available_network_aliases.get(name, None) for name in names]
|
||||||
|
|
||||||
failed_to_load_networks = []
|
failed_to_load_networks = []
|
||||||
|
|
||||||
for i, name in enumerate(names):
|
for i, (network_on_disk, name) in enumerate(zip(networks_on_disk, names)):
|
||||||
net = already_loaded.get(name, None)
|
net = already_loaded.get(name, None)
|
||||||
|
|
||||||
network_on_disk = networks_on_disk[i]
|
|
||||||
|
|
||||||
if network_on_disk is not None:
|
if network_on_disk is not None:
|
||||||
|
if net is None:
|
||||||
|
net = networks_in_memory.get(name)
|
||||||
|
|
||||||
if net is None or os.path.getmtime(network_on_disk.filename) > net.mtime:
|
if net is None or os.path.getmtime(network_on_disk.filename) > net.mtime:
|
||||||
try:
|
try:
|
||||||
net = load_network(name, network_on_disk)
|
net = load_network(name, network_on_disk)
|
||||||
|
|
||||||
|
networks_in_memory.pop(name, None)
|
||||||
|
networks_in_memory[name] = net
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
errors.display(e, f"loading network {network_on_disk.filename}")
|
errors.display(e, f"loading network {network_on_disk.filename}")
|
||||||
continue
|
continue
|
||||||
@@ -231,7 +291,7 @@ def load_networks(names, te_multipliers=None, unet_multipliers=None, dyn_dims=No
|
|||||||
|
|
||||||
if net is None:
|
if net is None:
|
||||||
failed_to_load_networks.append(name)
|
failed_to_load_networks.append(name)
|
||||||
print(f"Couldn't find network with name {name}")
|
logging.info(f"Couldn't find network with name {name}")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
net.te_multiplier = te_multipliers[i] if te_multipliers else 1.0
|
net.te_multiplier = te_multipliers[i] if te_multipliers else 1.0
|
||||||
@@ -239,24 +299,59 @@ def load_networks(names, te_multipliers=None, unet_multipliers=None, dyn_dims=No
|
|||||||
net.dyn_dim = dyn_dims[i] if dyn_dims else 1.0
|
net.dyn_dim = dyn_dims[i] if dyn_dims else 1.0
|
||||||
loaded_networks.append(net)
|
loaded_networks.append(net)
|
||||||
|
|
||||||
|
for emb_name, embedding in net.bundle_embeddings.items():
|
||||||
|
if embedding.loaded is None and emb_name in emb_db.word_embeddings:
|
||||||
|
logger.warning(
|
||||||
|
f'Skip bundle embedding: "{emb_name}"'
|
||||||
|
' as it was already loaded from embeddings folder'
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
|
embedding.loaded = False
|
||||||
|
if emb_db.expected_shape == -1 or emb_db.expected_shape == embedding.shape:
|
||||||
|
embedding.loaded = True
|
||||||
|
emb_db.register_embedding(embedding, shared.sd_model)
|
||||||
|
else:
|
||||||
|
emb_db.skipped_embeddings[name] = embedding
|
||||||
|
|
||||||
if failed_to_load_networks:
|
if failed_to_load_networks:
|
||||||
sd_hijack.model_hijack.comments.append("Failed to find networks: " + ", ".join(failed_to_load_networks))
|
lora_not_found_message = f'Lora not found: {", ".join(failed_to_load_networks)}'
|
||||||
|
sd_hijack.model_hijack.comments.append(lora_not_found_message)
|
||||||
|
if shared.opts.lora_not_found_warning_console:
|
||||||
|
print(f'\n{lora_not_found_message}\n')
|
||||||
|
if shared.opts.lora_not_found_gradio_warning:
|
||||||
|
gr.Warning(lora_not_found_message)
|
||||||
|
|
||||||
|
purge_networks_from_memory()
|
||||||
|
|
||||||
|
|
||||||
def network_restore_weights_from_backup(self: Union[torch.nn.Conv2d, torch.nn.Linear, torch.nn.MultiheadAttention]):
|
def network_restore_weights_from_backup(self: Union[torch.nn.Conv2d, torch.nn.Linear, torch.nn.GroupNorm, torch.nn.LayerNorm, torch.nn.MultiheadAttention]):
|
||||||
weights_backup = getattr(self, "network_weights_backup", None)
|
weights_backup = getattr(self, "network_weights_backup", None)
|
||||||
|
bias_backup = getattr(self, "network_bias_backup", None)
|
||||||
|
|
||||||
if weights_backup is None:
|
if weights_backup is None and bias_backup is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
if isinstance(self, torch.nn.MultiheadAttention):
|
if weights_backup is not None:
|
||||||
self.in_proj_weight.copy_(weights_backup[0])
|
if isinstance(self, torch.nn.MultiheadAttention):
|
||||||
self.out_proj.weight.copy_(weights_backup[1])
|
self.in_proj_weight.copy_(weights_backup[0])
|
||||||
|
self.out_proj.weight.copy_(weights_backup[1])
|
||||||
|
else:
|
||||||
|
self.weight.copy_(weights_backup)
|
||||||
|
|
||||||
|
if bias_backup is not None:
|
||||||
|
if isinstance(self, torch.nn.MultiheadAttention):
|
||||||
|
self.out_proj.bias.copy_(bias_backup)
|
||||||
|
else:
|
||||||
|
self.bias.copy_(bias_backup)
|
||||||
else:
|
else:
|
||||||
self.weight.copy_(weights_backup)
|
if isinstance(self, torch.nn.MultiheadAttention):
|
||||||
|
self.out_proj.bias = None
|
||||||
|
else:
|
||||||
|
self.bias = None
|
||||||
|
|
||||||
|
|
||||||
def network_apply_weights(self: Union[torch.nn.Conv2d, torch.nn.Linear, torch.nn.MultiheadAttention]):
|
def network_apply_weights(self: Union[torch.nn.Conv2d, torch.nn.Linear, torch.nn.GroupNorm, torch.nn.LayerNorm, torch.nn.MultiheadAttention]):
|
||||||
"""
|
"""
|
||||||
Applies the currently selected set of networks to the weights of torch layer self.
|
Applies the currently selected set of networks to the weights of torch layer self.
|
||||||
If weights already have this particular set of networks applied, does nothing.
|
If weights already have this particular set of networks applied, does nothing.
|
||||||
@@ -271,7 +366,10 @@ def network_apply_weights(self: Union[torch.nn.Conv2d, torch.nn.Linear, torch.nn
|
|||||||
wanted_names = tuple((x.name, x.te_multiplier, x.unet_multiplier, x.dyn_dim) for x in loaded_networks)
|
wanted_names = tuple((x.name, x.te_multiplier, x.unet_multiplier, x.dyn_dim) for x in loaded_networks)
|
||||||
|
|
||||||
weights_backup = getattr(self, "network_weights_backup", None)
|
weights_backup = getattr(self, "network_weights_backup", None)
|
||||||
if weights_backup is None:
|
if weights_backup is None and wanted_names != ():
|
||||||
|
if current_names != ():
|
||||||
|
raise RuntimeError("no backup weights found and current weights are not unchanged")
|
||||||
|
|
||||||
if isinstance(self, torch.nn.MultiheadAttention):
|
if isinstance(self, torch.nn.MultiheadAttention):
|
||||||
weights_backup = (self.in_proj_weight.to(devices.cpu, copy=True), self.out_proj.weight.to(devices.cpu, copy=True))
|
weights_backup = (self.in_proj_weight.to(devices.cpu, copy=True), self.out_proj.weight.to(devices.cpu, copy=True))
|
||||||
else:
|
else:
|
||||||
@@ -279,21 +377,49 @@ def network_apply_weights(self: Union[torch.nn.Conv2d, torch.nn.Linear, torch.nn
|
|||||||
|
|
||||||
self.network_weights_backup = weights_backup
|
self.network_weights_backup = weights_backup
|
||||||
|
|
||||||
|
bias_backup = getattr(self, "network_bias_backup", None)
|
||||||
|
if bias_backup is None:
|
||||||
|
if isinstance(self, torch.nn.MultiheadAttention) and self.out_proj.bias is not None:
|
||||||
|
bias_backup = self.out_proj.bias.to(devices.cpu, copy=True)
|
||||||
|
elif getattr(self, 'bias', None) is not None:
|
||||||
|
bias_backup = self.bias.to(devices.cpu, copy=True)
|
||||||
|
else:
|
||||||
|
bias_backup = None
|
||||||
|
self.network_bias_backup = bias_backup
|
||||||
|
|
||||||
if current_names != wanted_names:
|
if current_names != wanted_names:
|
||||||
network_restore_weights_from_backup(self)
|
network_restore_weights_from_backup(self)
|
||||||
|
|
||||||
for net in loaded_networks:
|
for net in loaded_networks:
|
||||||
module = net.modules.get(network_layer_name, None)
|
module = net.modules.get(network_layer_name, None)
|
||||||
if module is not None and hasattr(self, 'weight'):
|
if module is not None and hasattr(self, 'weight'):
|
||||||
with torch.no_grad():
|
try:
|
||||||
updown = module.calc_updown(self.weight)
|
with torch.no_grad():
|
||||||
|
if getattr(self, 'fp16_weight', None) is None:
|
||||||
|
weight = self.weight
|
||||||
|
bias = self.bias
|
||||||
|
else:
|
||||||
|
weight = self.fp16_weight.clone().to(self.weight.device)
|
||||||
|
bias = getattr(self, 'fp16_bias', None)
|
||||||
|
if bias is not None:
|
||||||
|
bias = bias.clone().to(self.bias.device)
|
||||||
|
updown, ex_bias = module.calc_updown(weight)
|
||||||
|
|
||||||
if len(self.weight.shape) == 4 and self.weight.shape[1] == 9:
|
if len(weight.shape) == 4 and weight.shape[1] == 9:
|
||||||
# inpainting model. zero pad updown to make channel[1] 4 to 9
|
# inpainting model. zero pad updown to make channel[1] 4 to 9
|
||||||
updown = torch.nn.functional.pad(updown, (0, 0, 0, 0, 0, 5))
|
updown = torch.nn.functional.pad(updown, (0, 0, 0, 0, 0, 5))
|
||||||
|
|
||||||
self.weight += updown
|
self.weight.copy_((weight.to(dtype=updown.dtype) + updown).to(dtype=self.weight.dtype))
|
||||||
continue
|
if ex_bias is not None and hasattr(self, 'bias'):
|
||||||
|
if self.bias is None:
|
||||||
|
self.bias = torch.nn.Parameter(ex_bias).to(self.weight.dtype)
|
||||||
|
else:
|
||||||
|
self.bias.copy_((bias + ex_bias).to(dtype=self.bias.dtype))
|
||||||
|
except RuntimeError as e:
|
||||||
|
logging.debug(f"Network {net.name} layer {network_layer_name}: {e}")
|
||||||
|
extra_network_lora.errors[net.name] = extra_network_lora.errors.get(net.name, 0) + 1
|
||||||
|
|
||||||
|
continue
|
||||||
|
|
||||||
module_q = net.modules.get(network_layer_name + "_q_proj", None)
|
module_q = net.modules.get(network_layer_name + "_q_proj", None)
|
||||||
module_k = net.modules.get(network_layer_name + "_k_proj", None)
|
module_k = net.modules.get(network_layer_name + "_k_proj", None)
|
||||||
@@ -301,48 +427,60 @@ def network_apply_weights(self: Union[torch.nn.Conv2d, torch.nn.Linear, torch.nn
|
|||||||
module_out = net.modules.get(network_layer_name + "_out_proj", None)
|
module_out = net.modules.get(network_layer_name + "_out_proj", None)
|
||||||
|
|
||||||
if isinstance(self, torch.nn.MultiheadAttention) and module_q and module_k and module_v and module_out:
|
if isinstance(self, torch.nn.MultiheadAttention) and module_q and module_k and module_v and module_out:
|
||||||
with torch.no_grad():
|
try:
|
||||||
updown_q = module_q.calc_updown(self.in_proj_weight)
|
with torch.no_grad():
|
||||||
updown_k = module_k.calc_updown(self.in_proj_weight)
|
updown_q, _ = module_q.calc_updown(self.in_proj_weight)
|
||||||
updown_v = module_v.calc_updown(self.in_proj_weight)
|
updown_k, _ = module_k.calc_updown(self.in_proj_weight)
|
||||||
updown_qkv = torch.vstack([updown_q, updown_k, updown_v])
|
updown_v, _ = module_v.calc_updown(self.in_proj_weight)
|
||||||
updown_out = module_out.calc_updown(self.out_proj.weight)
|
updown_qkv = torch.vstack([updown_q, updown_k, updown_v])
|
||||||
|
updown_out, ex_bias = module_out.calc_updown(self.out_proj.weight)
|
||||||
|
|
||||||
self.in_proj_weight += updown_qkv
|
self.in_proj_weight += updown_qkv
|
||||||
self.out_proj.weight += updown_out
|
self.out_proj.weight += updown_out
|
||||||
continue
|
if ex_bias is not None:
|
||||||
|
if self.out_proj.bias is None:
|
||||||
|
self.out_proj.bias = torch.nn.Parameter(ex_bias)
|
||||||
|
else:
|
||||||
|
self.out_proj.bias += ex_bias
|
||||||
|
|
||||||
|
except RuntimeError as e:
|
||||||
|
logging.debug(f"Network {net.name} layer {network_layer_name}: {e}")
|
||||||
|
extra_network_lora.errors[net.name] = extra_network_lora.errors.get(net.name, 0) + 1
|
||||||
|
|
||||||
|
continue
|
||||||
|
|
||||||
if module is None:
|
if module is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
print(f'failed to calculate network weights for layer {network_layer_name}')
|
logging.debug(f"Network {net.name} layer {network_layer_name}: couldn't find supported operation")
|
||||||
|
extra_network_lora.errors[net.name] = extra_network_lora.errors.get(net.name, 0) + 1
|
||||||
|
|
||||||
self.network_current_names = wanted_names
|
self.network_current_names = wanted_names
|
||||||
|
|
||||||
|
|
||||||
def network_forward(module, input, original_forward):
|
def network_forward(org_module, input, original_forward):
|
||||||
"""
|
"""
|
||||||
Old way of applying Lora by executing operations during layer's forward.
|
Old way of applying Lora by executing operations during layer's forward.
|
||||||
Stacking many loras this way results in big performance degradation.
|
Stacking many loras this way results in big performance degradation.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if len(loaded_networks) == 0:
|
if len(loaded_networks) == 0:
|
||||||
return original_forward(module, input)
|
return original_forward(org_module, input)
|
||||||
|
|
||||||
input = devices.cond_cast_unet(input)
|
input = devices.cond_cast_unet(input)
|
||||||
|
|
||||||
network_restore_weights_from_backup(module)
|
network_restore_weights_from_backup(org_module)
|
||||||
network_reset_cached_weight(module)
|
network_reset_cached_weight(org_module)
|
||||||
|
|
||||||
y = original_forward(module, input)
|
y = original_forward(org_module, input)
|
||||||
|
|
||||||
network_layer_name = getattr(module, 'network_layer_name', None)
|
network_layer_name = getattr(org_module, 'network_layer_name', None)
|
||||||
for lora in loaded_networks:
|
for lora in loaded_networks:
|
||||||
module = lora.modules.get(network_layer_name, None)
|
module = lora.modules.get(network_layer_name, None)
|
||||||
if module is None:
|
if module is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
y = module.forward(y, input)
|
y = module.forward(input, y)
|
||||||
|
|
||||||
return y
|
return y
|
||||||
|
|
||||||
@@ -350,48 +488,79 @@ def network_forward(module, input, original_forward):
|
|||||||
def network_reset_cached_weight(self: Union[torch.nn.Conv2d, torch.nn.Linear]):
|
def network_reset_cached_weight(self: Union[torch.nn.Conv2d, torch.nn.Linear]):
|
||||||
self.network_current_names = ()
|
self.network_current_names = ()
|
||||||
self.network_weights_backup = None
|
self.network_weights_backup = None
|
||||||
|
self.network_bias_backup = None
|
||||||
|
|
||||||
|
|
||||||
def network_Linear_forward(self, input):
|
def network_Linear_forward(self, input):
|
||||||
if shared.opts.lora_functional:
|
if shared.opts.lora_functional:
|
||||||
return network_forward(self, input, torch.nn.Linear_forward_before_network)
|
return network_forward(self, input, originals.Linear_forward)
|
||||||
|
|
||||||
network_apply_weights(self)
|
network_apply_weights(self)
|
||||||
|
|
||||||
return torch.nn.Linear_forward_before_network(self, input)
|
return originals.Linear_forward(self, input)
|
||||||
|
|
||||||
|
|
||||||
def network_Linear_load_state_dict(self, *args, **kwargs):
|
def network_Linear_load_state_dict(self, *args, **kwargs):
|
||||||
network_reset_cached_weight(self)
|
network_reset_cached_weight(self)
|
||||||
|
|
||||||
return torch.nn.Linear_load_state_dict_before_network(self, *args, **kwargs)
|
return originals.Linear_load_state_dict(self, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def network_Conv2d_forward(self, input):
|
def network_Conv2d_forward(self, input):
|
||||||
if shared.opts.lora_functional:
|
if shared.opts.lora_functional:
|
||||||
return network_forward(self, input, torch.nn.Conv2d_forward_before_network)
|
return network_forward(self, input, originals.Conv2d_forward)
|
||||||
|
|
||||||
network_apply_weights(self)
|
network_apply_weights(self)
|
||||||
|
|
||||||
return torch.nn.Conv2d_forward_before_network(self, input)
|
return originals.Conv2d_forward(self, input)
|
||||||
|
|
||||||
|
|
||||||
def network_Conv2d_load_state_dict(self, *args, **kwargs):
|
def network_Conv2d_load_state_dict(self, *args, **kwargs):
|
||||||
network_reset_cached_weight(self)
|
network_reset_cached_weight(self)
|
||||||
|
|
||||||
return torch.nn.Conv2d_load_state_dict_before_network(self, *args, **kwargs)
|
return originals.Conv2d_load_state_dict(self, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def network_GroupNorm_forward(self, input):
|
||||||
|
if shared.opts.lora_functional:
|
||||||
|
return network_forward(self, input, originals.GroupNorm_forward)
|
||||||
|
|
||||||
|
network_apply_weights(self)
|
||||||
|
|
||||||
|
return originals.GroupNorm_forward(self, input)
|
||||||
|
|
||||||
|
|
||||||
|
def network_GroupNorm_load_state_dict(self, *args, **kwargs):
|
||||||
|
network_reset_cached_weight(self)
|
||||||
|
|
||||||
|
return originals.GroupNorm_load_state_dict(self, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
def network_LayerNorm_forward(self, input):
|
||||||
|
if shared.opts.lora_functional:
|
||||||
|
return network_forward(self, input, originals.LayerNorm_forward)
|
||||||
|
|
||||||
|
network_apply_weights(self)
|
||||||
|
|
||||||
|
return originals.LayerNorm_forward(self, input)
|
||||||
|
|
||||||
|
|
||||||
|
def network_LayerNorm_load_state_dict(self, *args, **kwargs):
|
||||||
|
network_reset_cached_weight(self)
|
||||||
|
|
||||||
|
return originals.LayerNorm_load_state_dict(self, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def network_MultiheadAttention_forward(self, *args, **kwargs):
|
def network_MultiheadAttention_forward(self, *args, **kwargs):
|
||||||
network_apply_weights(self)
|
network_apply_weights(self)
|
||||||
|
|
||||||
return torch.nn.MultiheadAttention_forward_before_network(self, *args, **kwargs)
|
return originals.MultiheadAttention_forward(self, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def network_MultiheadAttention_load_state_dict(self, *args, **kwargs):
|
def network_MultiheadAttention_load_state_dict(self, *args, **kwargs):
|
||||||
network_reset_cached_weight(self)
|
network_reset_cached_weight(self)
|
||||||
|
|
||||||
return torch.nn.MultiheadAttention_load_state_dict_before_network(self, *args, **kwargs)
|
return originals.MultiheadAttention_load_state_dict(self, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
def list_available_networks():
|
def list_available_networks():
|
||||||
@@ -459,9 +628,15 @@ def infotext_pasted(infotext, params):
|
|||||||
params["Prompt"] += "\n" + "".join(added)
|
params["Prompt"] += "\n" + "".join(added)
|
||||||
|
|
||||||
|
|
||||||
|
originals: lora_patches.LoraPatches = None
|
||||||
|
|
||||||
|
extra_network_lora = None
|
||||||
|
|
||||||
available_networks = {}
|
available_networks = {}
|
||||||
available_network_aliases = {}
|
available_network_aliases = {}
|
||||||
loaded_networks = []
|
loaded_networks = []
|
||||||
|
loaded_bundle_embeddings = {}
|
||||||
|
networks_in_memory = {}
|
||||||
available_network_hash_lookup = {}
|
available_network_hash_lookup = {}
|
||||||
forbidden_network_aliases = {}
|
forbidden_network_aliases = {}
|
||||||
|
|
||||||
|
|||||||
@@ -1,57 +1,30 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
import torch
|
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
|
|
||||||
import network
|
import network
|
||||||
import networks
|
import networks
|
||||||
import lora # noqa:F401
|
import lora # noqa:F401
|
||||||
|
import lora_patches
|
||||||
import extra_networks_lora
|
import extra_networks_lora
|
||||||
import ui_extra_networks_lora
|
import ui_extra_networks_lora
|
||||||
from modules import script_callbacks, ui_extra_networks, extra_networks, shared
|
from modules import script_callbacks, ui_extra_networks, extra_networks, shared
|
||||||
|
|
||||||
|
|
||||||
def unload():
|
def unload():
|
||||||
torch.nn.Linear.forward = torch.nn.Linear_forward_before_network
|
networks.originals.undo()
|
||||||
torch.nn.Linear._load_from_state_dict = torch.nn.Linear_load_state_dict_before_network
|
|
||||||
torch.nn.Conv2d.forward = torch.nn.Conv2d_forward_before_network
|
|
||||||
torch.nn.Conv2d._load_from_state_dict = torch.nn.Conv2d_load_state_dict_before_network
|
|
||||||
torch.nn.MultiheadAttention.forward = torch.nn.MultiheadAttention_forward_before_network
|
|
||||||
torch.nn.MultiheadAttention._load_from_state_dict = torch.nn.MultiheadAttention_load_state_dict_before_network
|
|
||||||
|
|
||||||
|
|
||||||
def before_ui():
|
def before_ui():
|
||||||
ui_extra_networks.register_page(ui_extra_networks_lora.ExtraNetworksPageLora())
|
ui_extra_networks.register_page(ui_extra_networks_lora.ExtraNetworksPageLora())
|
||||||
|
|
||||||
extra_network = extra_networks_lora.ExtraNetworkLora()
|
networks.extra_network_lora = extra_networks_lora.ExtraNetworkLora()
|
||||||
extra_networks.register_extra_network(extra_network)
|
extra_networks.register_extra_network(networks.extra_network_lora)
|
||||||
extra_networks.register_extra_network_alias(extra_network, "lyco")
|
extra_networks.register_extra_network_alias(networks.extra_network_lora, "lyco")
|
||||||
|
|
||||||
|
|
||||||
if not hasattr(torch.nn, 'Linear_forward_before_network'):
|
networks.originals = lora_patches.LoraPatches()
|
||||||
torch.nn.Linear_forward_before_network = torch.nn.Linear.forward
|
|
||||||
|
|
||||||
if not hasattr(torch.nn, 'Linear_load_state_dict_before_network'):
|
|
||||||
torch.nn.Linear_load_state_dict_before_network = torch.nn.Linear._load_from_state_dict
|
|
||||||
|
|
||||||
if not hasattr(torch.nn, 'Conv2d_forward_before_network'):
|
|
||||||
torch.nn.Conv2d_forward_before_network = torch.nn.Conv2d.forward
|
|
||||||
|
|
||||||
if not hasattr(torch.nn, 'Conv2d_load_state_dict_before_network'):
|
|
||||||
torch.nn.Conv2d_load_state_dict_before_network = torch.nn.Conv2d._load_from_state_dict
|
|
||||||
|
|
||||||
if not hasattr(torch.nn, 'MultiheadAttention_forward_before_network'):
|
|
||||||
torch.nn.MultiheadAttention_forward_before_network = torch.nn.MultiheadAttention.forward
|
|
||||||
|
|
||||||
if not hasattr(torch.nn, 'MultiheadAttention_load_state_dict_before_network'):
|
|
||||||
torch.nn.MultiheadAttention_load_state_dict_before_network = torch.nn.MultiheadAttention._load_from_state_dict
|
|
||||||
|
|
||||||
torch.nn.Linear.forward = networks.network_Linear_forward
|
|
||||||
torch.nn.Linear._load_from_state_dict = networks.network_Linear_load_state_dict
|
|
||||||
torch.nn.Conv2d.forward = networks.network_Conv2d_forward
|
|
||||||
torch.nn.Conv2d._load_from_state_dict = networks.network_Conv2d_load_state_dict
|
|
||||||
torch.nn.MultiheadAttention.forward = networks.network_MultiheadAttention_forward
|
|
||||||
torch.nn.MultiheadAttention._load_from_state_dict = networks.network_MultiheadAttention_load_state_dict
|
|
||||||
|
|
||||||
script_callbacks.on_model_loaded(networks.assign_network_names_to_compvis_modules)
|
script_callbacks.on_model_loaded(networks.assign_network_names_to_compvis_modules)
|
||||||
script_callbacks.on_script_unloaded(unload)
|
script_callbacks.on_script_unloaded(unload)
|
||||||
@@ -65,6 +38,9 @@ shared.options_templates.update(shared.options_section(('extra_networks', "Extra
|
|||||||
"lora_add_hashes_to_infotext": shared.OptionInfo(True, "Add Lora hashes to infotext"),
|
"lora_add_hashes_to_infotext": shared.OptionInfo(True, "Add Lora hashes to infotext"),
|
||||||
"lora_show_all": shared.OptionInfo(False, "Always show all networks on the Lora page").info("otherwise, those detected as for incompatible version of Stable Diffusion will be hidden"),
|
"lora_show_all": shared.OptionInfo(False, "Always show all networks on the Lora page").info("otherwise, those detected as for incompatible version of Stable Diffusion will be hidden"),
|
||||||
"lora_hide_unknown_for_versions": shared.OptionInfo([], "Hide networks of unknown versions for model versions", gr.CheckboxGroup, {"choices": ["SD1", "SD2", "SDXL"]}),
|
"lora_hide_unknown_for_versions": shared.OptionInfo([], "Hide networks of unknown versions for model versions", gr.CheckboxGroup, {"choices": ["SD1", "SD2", "SDXL"]}),
|
||||||
|
"lora_in_memory_limit": shared.OptionInfo(0, "Number of Lora networks to keep cached in memory", gr.Number, {"precision": 0}),
|
||||||
|
"lora_not_found_warning_console": shared.OptionInfo(False, "Lora not found warning in console"),
|
||||||
|
"lora_not_found_gradio_warning": shared.OptionInfo(False, "Lora not found warning popup in webui"),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
||||||
@@ -121,3 +97,5 @@ def infotext_pasted(infotext, d):
|
|||||||
|
|
||||||
|
|
||||||
script_callbacks.on_infotext_pasted(infotext_pasted)
|
script_callbacks.on_infotext_pasted(infotext_pasted)
|
||||||
|
|
||||||
|
shared.opts.onchange("lora_in_memory_limit", networks.purge_networks_from_memory)
|
||||||
|
|||||||
@@ -54,12 +54,13 @@ class LoraUserMetadataEditor(ui_extra_networks_user_metadata.UserMetadataEditor)
|
|||||||
self.slider_preferred_weight = None
|
self.slider_preferred_weight = None
|
||||||
self.edit_notes = None
|
self.edit_notes = None
|
||||||
|
|
||||||
def save_lora_user_metadata(self, name, desc, sd_version, activation_text, preferred_weight, notes):
|
def save_lora_user_metadata(self, name, desc, sd_version, activation_text, preferred_weight, negative_text, notes):
|
||||||
user_metadata = self.get_user_metadata(name)
|
user_metadata = self.get_user_metadata(name)
|
||||||
user_metadata["description"] = desc
|
user_metadata["description"] = desc
|
||||||
user_metadata["sd version"] = sd_version
|
user_metadata["sd version"] = sd_version
|
||||||
user_metadata["activation text"] = activation_text
|
user_metadata["activation text"] = activation_text
|
||||||
user_metadata["preferred weight"] = preferred_weight
|
user_metadata["preferred weight"] = preferred_weight
|
||||||
|
user_metadata["negative text"] = negative_text
|
||||||
user_metadata["notes"] = notes
|
user_metadata["notes"] = notes
|
||||||
|
|
||||||
self.write_user_metadata(name, user_metadata)
|
self.write_user_metadata(name, user_metadata)
|
||||||
@@ -70,6 +71,7 @@ class LoraUserMetadataEditor(ui_extra_networks_user_metadata.UserMetadataEditor)
|
|||||||
metadata = item.get("metadata") or {}
|
metadata = item.get("metadata") or {}
|
||||||
|
|
||||||
keys = {
|
keys = {
|
||||||
|
'ss_output_name': "Output name:",
|
||||||
'ss_sd_model_name': "Model:",
|
'ss_sd_model_name': "Model:",
|
||||||
'ss_clip_skip': "Clip skip:",
|
'ss_clip_skip': "Clip skip:",
|
||||||
'ss_network_module': "Kohya module:",
|
'ss_network_module': "Kohya module:",
|
||||||
@@ -126,6 +128,7 @@ class LoraUserMetadataEditor(ui_extra_networks_user_metadata.UserMetadataEditor)
|
|||||||
gr.HighlightedText.update(value=gradio_tags, visible=True if tags else False),
|
gr.HighlightedText.update(value=gradio_tags, visible=True if tags else False),
|
||||||
user_metadata.get('activation text', ''),
|
user_metadata.get('activation text', ''),
|
||||||
float(user_metadata.get('preferred weight', 0.0)),
|
float(user_metadata.get('preferred weight', 0.0)),
|
||||||
|
user_metadata.get('negative text', ''),
|
||||||
gr.update(visible=True if tags else False),
|
gr.update(visible=True if tags else False),
|
||||||
gr.update(value=self.generate_random_prompt_from_tags(tags), visible=True if tags else False),
|
gr.update(value=self.generate_random_prompt_from_tags(tags), visible=True if tags else False),
|
||||||
]
|
]
|
||||||
@@ -161,13 +164,13 @@ class LoraUserMetadataEditor(ui_extra_networks_user_metadata.UserMetadataEditor)
|
|||||||
self.taginfo = gr.HighlightedText(label="Training dataset tags")
|
self.taginfo = gr.HighlightedText(label="Training dataset tags")
|
||||||
self.edit_activation_text = gr.Text(label='Activation text', info="Will be added to prompt along with Lora")
|
self.edit_activation_text = gr.Text(label='Activation text', info="Will be added to prompt along with Lora")
|
||||||
self.slider_preferred_weight = gr.Slider(label='Preferred weight', info="Set to 0 to disable", minimum=0.0, maximum=2.0, step=0.01)
|
self.slider_preferred_weight = gr.Slider(label='Preferred weight', info="Set to 0 to disable", minimum=0.0, maximum=2.0, step=0.01)
|
||||||
|
self.edit_negative_text = gr.Text(label='Negative prompt', info="Will be added to negative prompts")
|
||||||
with gr.Row() as row_random_prompt:
|
with gr.Row() as row_random_prompt:
|
||||||
with gr.Column(scale=8):
|
with gr.Column(scale=8):
|
||||||
random_prompt = gr.Textbox(label='Random prompt', lines=4, max_lines=4, interactive=False)
|
random_prompt = gr.Textbox(label='Random prompt', lines=4, max_lines=4, interactive=False)
|
||||||
|
|
||||||
with gr.Column(scale=1, min_width=120):
|
with gr.Column(scale=1, min_width=120):
|
||||||
generate_random_prompt = gr.Button('Generate').style(full_width=True, size="lg")
|
generate_random_prompt = gr.Button('Generate', size="lg", scale=1)
|
||||||
|
|
||||||
self.edit_notes = gr.TextArea(label='Notes', lines=4)
|
self.edit_notes = gr.TextArea(label='Notes', lines=4)
|
||||||
|
|
||||||
@@ -197,6 +200,7 @@ class LoraUserMetadataEditor(ui_extra_networks_user_metadata.UserMetadataEditor)
|
|||||||
self.taginfo,
|
self.taginfo,
|
||||||
self.edit_activation_text,
|
self.edit_activation_text,
|
||||||
self.slider_preferred_weight,
|
self.slider_preferred_weight,
|
||||||
|
self.edit_negative_text,
|
||||||
row_random_prompt,
|
row_random_prompt,
|
||||||
random_prompt,
|
random_prompt,
|
||||||
]
|
]
|
||||||
@@ -210,7 +214,9 @@ class LoraUserMetadataEditor(ui_extra_networks_user_metadata.UserMetadataEditor)
|
|||||||
self.select_sd_version,
|
self.select_sd_version,
|
||||||
self.edit_activation_text,
|
self.edit_activation_text,
|
||||||
self.slider_preferred_weight,
|
self.slider_preferred_weight,
|
||||||
|
self.edit_negative_text,
|
||||||
self.edit_notes,
|
self.edit_notes,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
self.setup_save_handler(self.button_save, self.save_lora_user_metadata, edited_components)
|
self.setup_save_handler(self.button_save, self.save_lora_user_metadata, edited_components)
|
||||||
|
|||||||
@@ -17,6 +17,8 @@ class ExtraNetworksPageLora(ui_extra_networks.ExtraNetworksPage):
|
|||||||
|
|
||||||
def create_item(self, name, index=None, enable_filter=True):
|
def create_item(self, name, index=None, enable_filter=True):
|
||||||
lora_on_disk = networks.available_networks.get(name)
|
lora_on_disk = networks.available_networks.get(name)
|
||||||
|
if lora_on_disk is None:
|
||||||
|
return
|
||||||
|
|
||||||
path, ext = os.path.splitext(lora_on_disk.filename)
|
path, ext = os.path.splitext(lora_on_disk.filename)
|
||||||
|
|
||||||
@@ -25,9 +27,10 @@ class ExtraNetworksPageLora(ui_extra_networks.ExtraNetworksPage):
|
|||||||
item = {
|
item = {
|
||||||
"name": name,
|
"name": name,
|
||||||
"filename": lora_on_disk.filename,
|
"filename": lora_on_disk.filename,
|
||||||
|
"shorthash": lora_on_disk.shorthash,
|
||||||
"preview": self.find_preview(path),
|
"preview": self.find_preview(path),
|
||||||
"description": self.find_description(path),
|
"description": self.find_description(path),
|
||||||
"search_term": self.search_terms_from_path(lora_on_disk.filename),
|
"search_term": self.search_terms_from_path(lora_on_disk.filename) + " " + (lora_on_disk.hash or ""),
|
||||||
"local_preview": f"{path}.{shared.opts.samples_format}",
|
"local_preview": f"{path}.{shared.opts.samples_format}",
|
||||||
"metadata": lora_on_disk.metadata,
|
"metadata": lora_on_disk.metadata,
|
||||||
"sort_keys": {'default': index, **self.get_sort_keys(lora_on_disk.filename)},
|
"sort_keys": {'default': index, **self.get_sort_keys(lora_on_disk.filename)},
|
||||||
@@ -42,6 +45,11 @@ class ExtraNetworksPageLora(ui_extra_networks.ExtraNetworksPage):
|
|||||||
if activation_text:
|
if activation_text:
|
||||||
item["prompt"] += " + " + quote_js(" " + activation_text)
|
item["prompt"] += " + " + quote_js(" " + activation_text)
|
||||||
|
|
||||||
|
negative_prompt = item["user_metadata"].get("negative text")
|
||||||
|
item["negative_prompt"] = quote_js("")
|
||||||
|
if negative_prompt:
|
||||||
|
item["negative_prompt"] = quote_js('(' + negative_prompt + ':1)')
|
||||||
|
|
||||||
sd_version = item["user_metadata"].get("sd version")
|
sd_version = item["user_metadata"].get("sd version")
|
||||||
if sd_version in network.SdVersion.__members__:
|
if sd_version in network.SdVersion.__members__:
|
||||||
item["sd_version"] = sd_version
|
item["sd_version"] = sd_version
|
||||||
@@ -65,9 +73,10 @@ class ExtraNetworksPageLora(ui_extra_networks.ExtraNetworksPage):
|
|||||||
return item
|
return item
|
||||||
|
|
||||||
def list_items(self):
|
def list_items(self):
|
||||||
for index, name in enumerate(networks.available_networks):
|
# instantiate a list to protect against concurrent modification
|
||||||
|
names = list(networks.available_networks)
|
||||||
|
for index, name in enumerate(names):
|
||||||
item = self.create_item(name, index)
|
item = self.create_item(name, index)
|
||||||
|
|
||||||
if item is not None:
|
if item is not None:
|
||||||
yield item
|
yield item
|
||||||
|
|
||||||
|
|||||||
@@ -1,16 +1,9 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
import PIL.Image
|
import PIL.Image
|
||||||
import numpy as np
|
|
||||||
import torch
|
|
||||||
from tqdm import tqdm
|
|
||||||
|
|
||||||
import modules.upscaler
|
import modules.upscaler
|
||||||
from modules import devices, modelloader, script_callbacks, errors
|
from modules import devices, errors, modelloader, script_callbacks, shared, upscaler_utils
|
||||||
from scunet_model_arch import SCUNet
|
|
||||||
|
|
||||||
from modules.modelloader import load_file_from_url
|
|
||||||
from modules.shared import opts
|
|
||||||
|
|
||||||
|
|
||||||
class UpscalerScuNET(modules.upscaler.Upscaler):
|
class UpscalerScuNET(modules.upscaler.Upscaler):
|
||||||
@@ -42,100 +35,37 @@ class UpscalerScuNET(modules.upscaler.Upscaler):
|
|||||||
scalers.append(scaler_data2)
|
scalers.append(scaler_data2)
|
||||||
self.scalers = scalers
|
self.scalers = scalers
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
@torch.no_grad()
|
|
||||||
def tiled_inference(img, model):
|
|
||||||
# test the image tile by tile
|
|
||||||
h, w = img.shape[2:]
|
|
||||||
tile = opts.SCUNET_tile
|
|
||||||
tile_overlap = opts.SCUNET_tile_overlap
|
|
||||||
if tile == 0:
|
|
||||||
return model(img)
|
|
||||||
|
|
||||||
device = devices.get_device_for('scunet')
|
|
||||||
assert tile % 8 == 0, "tile size should be a multiple of window_size"
|
|
||||||
sf = 1
|
|
||||||
|
|
||||||
stride = tile - tile_overlap
|
|
||||||
h_idx_list = list(range(0, h - tile, stride)) + [h - tile]
|
|
||||||
w_idx_list = list(range(0, w - tile, stride)) + [w - tile]
|
|
||||||
E = torch.zeros(1, 3, h * sf, w * sf, dtype=img.dtype, device=device)
|
|
||||||
W = torch.zeros_like(E, dtype=devices.dtype, device=device)
|
|
||||||
|
|
||||||
with tqdm(total=len(h_idx_list) * len(w_idx_list), desc="ScuNET tiles") as pbar:
|
|
||||||
for h_idx in h_idx_list:
|
|
||||||
|
|
||||||
for w_idx in w_idx_list:
|
|
||||||
|
|
||||||
in_patch = img[..., h_idx: h_idx + tile, w_idx: w_idx + tile]
|
|
||||||
|
|
||||||
out_patch = model(in_patch)
|
|
||||||
out_patch_mask = torch.ones_like(out_patch)
|
|
||||||
|
|
||||||
E[
|
|
||||||
..., h_idx * sf: (h_idx + tile) * sf, w_idx * sf: (w_idx + tile) * sf
|
|
||||||
].add_(out_patch)
|
|
||||||
W[
|
|
||||||
..., h_idx * sf: (h_idx + tile) * sf, w_idx * sf: (w_idx + tile) * sf
|
|
||||||
].add_(out_patch_mask)
|
|
||||||
pbar.update(1)
|
|
||||||
output = E.div_(W)
|
|
||||||
|
|
||||||
return output
|
|
||||||
|
|
||||||
def do_upscale(self, img: PIL.Image.Image, selected_file):
|
def do_upscale(self, img: PIL.Image.Image, selected_file):
|
||||||
|
|
||||||
devices.torch_gc()
|
devices.torch_gc()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
model = self.load_model(selected_file)
|
model = self.load_model(selected_file)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"ScuNET: Unable to load model from {selected_file}: {e}", file=sys.stderr)
|
print(f"ScuNET: Unable to load model from {selected_file}: {e}", file=sys.stderr)
|
||||||
return img
|
return img
|
||||||
|
|
||||||
device = devices.get_device_for('scunet')
|
img = upscaler_utils.upscale_2(
|
||||||
tile = opts.SCUNET_tile
|
img,
|
||||||
h, w = img.height, img.width
|
model,
|
||||||
np_img = np.array(img)
|
tile_size=shared.opts.SCUNET_tile,
|
||||||
np_img = np_img[:, :, ::-1] # RGB to BGR
|
tile_overlap=shared.opts.SCUNET_tile_overlap,
|
||||||
np_img = np_img.transpose((2, 0, 1)) / 255 # HWC to CHW
|
scale=1, # ScuNET is a denoising model, not an upscaler
|
||||||
torch_img = torch.from_numpy(np_img).float().unsqueeze(0).to(device) # type: ignore
|
desc='ScuNET',
|
||||||
|
)
|
||||||
if tile > h or tile > w:
|
|
||||||
_img = torch.zeros(1, 3, max(h, tile), max(w, tile), dtype=torch_img.dtype, device=torch_img.device)
|
|
||||||
_img[:, :, :h, :w] = torch_img # pad image
|
|
||||||
torch_img = _img
|
|
||||||
|
|
||||||
torch_output = self.tiled_inference(torch_img, model).squeeze(0)
|
|
||||||
torch_output = torch_output[:, :h * 1, :w * 1] # remove padding, if any
|
|
||||||
np_output: np.ndarray = torch_output.float().cpu().clamp_(0, 1).numpy()
|
|
||||||
del torch_img, torch_output
|
|
||||||
devices.torch_gc()
|
devices.torch_gc()
|
||||||
|
return img
|
||||||
output = np_output.transpose((1, 2, 0)) # CHW to HWC
|
|
||||||
output = output[:, :, ::-1] # BGR to RGB
|
|
||||||
return PIL.Image.fromarray((output * 255).astype(np.uint8))
|
|
||||||
|
|
||||||
def load_model(self, path: str):
|
def load_model(self, path: str):
|
||||||
device = devices.get_device_for('scunet')
|
device = devices.get_device_for('scunet')
|
||||||
if path.startswith("http"):
|
if path.startswith("http"):
|
||||||
# TODO: this doesn't use `path` at all?
|
# TODO: this doesn't use `path` at all?
|
||||||
filename = load_file_from_url(self.model_url, model_dir=self.model_download_path, file_name=f"{self.name}.pth")
|
filename = modelloader.load_file_from_url(self.model_url, model_dir=self.model_download_path, file_name=f"{self.name}.pth")
|
||||||
else:
|
else:
|
||||||
filename = path
|
filename = path
|
||||||
model = SCUNet(in_nc=3, config=[4, 4, 4, 4, 4, 4, 4], dim=64)
|
return modelloader.load_spandrel_model(filename, device=device, expected_architecture='SCUNet')
|
||||||
model.load_state_dict(torch.load(filename), strict=True)
|
|
||||||
model.eval()
|
|
||||||
for _, v in model.named_parameters():
|
|
||||||
v.requires_grad = False
|
|
||||||
model = model.to(device)
|
|
||||||
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
def on_ui_settings():
|
def on_ui_settings():
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
from modules import shared
|
|
||||||
|
|
||||||
shared.opts.add_option("SCUNET_tile", shared.OptionInfo(256, "Tile size for SCUNET upscalers.", gr.Slider, {"minimum": 0, "maximum": 512, "step": 16}, section=('upscaling', "Upscaling")).info("0 = no tiling"))
|
shared.opts.add_option("SCUNET_tile", shared.OptionInfo(256, "Tile size for SCUNET upscalers.", gr.Slider, {"minimum": 0, "maximum": 512, "step": 16}, section=('upscaling', "Upscaling")).info("0 = no tiling"))
|
||||||
shared.opts.add_option("SCUNET_tile_overlap", shared.OptionInfo(8, "Tile overlap for SCUNET upscalers.", gr.Slider, {"minimum": 0, "maximum": 64, "step": 1}, section=('upscaling', "Upscaling")).info("Low values = visible seam"))
|
shared.opts.add_option("SCUNET_tile_overlap", shared.OptionInfo(8, "Tile overlap for SCUNET upscalers.", gr.Slider, {"minimum": 0, "maximum": 64, "step": 1}, section=('upscaling', "Upscaling")).info("Low values = visible seam"))
|
||||||
|
|||||||
@@ -1,268 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
import numpy as np
|
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
from einops import rearrange
|
|
||||||
from einops.layers.torch import Rearrange
|
|
||||||
from timm.models.layers import trunc_normal_, DropPath
|
|
||||||
|
|
||||||
|
|
||||||
class WMSA(nn.Module):
|
|
||||||
""" Self-attention module in Swin Transformer
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, input_dim, output_dim, head_dim, window_size, type):
|
|
||||||
super(WMSA, self).__init__()
|
|
||||||
self.input_dim = input_dim
|
|
||||||
self.output_dim = output_dim
|
|
||||||
self.head_dim = head_dim
|
|
||||||
self.scale = self.head_dim ** -0.5
|
|
||||||
self.n_heads = input_dim // head_dim
|
|
||||||
self.window_size = window_size
|
|
||||||
self.type = type
|
|
||||||
self.embedding_layer = nn.Linear(self.input_dim, 3 * self.input_dim, bias=True)
|
|
||||||
|
|
||||||
self.relative_position_params = nn.Parameter(
|
|
||||||
torch.zeros((2 * window_size - 1) * (2 * window_size - 1), self.n_heads))
|
|
||||||
|
|
||||||
self.linear = nn.Linear(self.input_dim, self.output_dim)
|
|
||||||
|
|
||||||
trunc_normal_(self.relative_position_params, std=.02)
|
|
||||||
self.relative_position_params = torch.nn.Parameter(
|
|
||||||
self.relative_position_params.view(2 * window_size - 1, 2 * window_size - 1, self.n_heads).transpose(1,
|
|
||||||
2).transpose(
|
|
||||||
0, 1))
|
|
||||||
|
|
||||||
def generate_mask(self, h, w, p, shift):
|
|
||||||
""" generating the mask of SW-MSA
|
|
||||||
Args:
|
|
||||||
shift: shift parameters in CyclicShift.
|
|
||||||
Returns:
|
|
||||||
attn_mask: should be (1 1 w p p),
|
|
||||||
"""
|
|
||||||
# supporting square.
|
|
||||||
attn_mask = torch.zeros(h, w, p, p, p, p, dtype=torch.bool, device=self.relative_position_params.device)
|
|
||||||
if self.type == 'W':
|
|
||||||
return attn_mask
|
|
||||||
|
|
||||||
s = p - shift
|
|
||||||
attn_mask[-1, :, :s, :, s:, :] = True
|
|
||||||
attn_mask[-1, :, s:, :, :s, :] = True
|
|
||||||
attn_mask[:, -1, :, :s, :, s:] = True
|
|
||||||
attn_mask[:, -1, :, s:, :, :s] = True
|
|
||||||
attn_mask = rearrange(attn_mask, 'w1 w2 p1 p2 p3 p4 -> 1 1 (w1 w2) (p1 p2) (p3 p4)')
|
|
||||||
return attn_mask
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
""" Forward pass of Window Multi-head Self-attention module.
|
|
||||||
Args:
|
|
||||||
x: input tensor with shape of [b h w c];
|
|
||||||
attn_mask: attention mask, fill -inf where the value is True;
|
|
||||||
Returns:
|
|
||||||
output: tensor shape [b h w c]
|
|
||||||
"""
|
|
||||||
if self.type != 'W':
|
|
||||||
x = torch.roll(x, shifts=(-(self.window_size // 2), -(self.window_size // 2)), dims=(1, 2))
|
|
||||||
|
|
||||||
x = rearrange(x, 'b (w1 p1) (w2 p2) c -> b w1 w2 p1 p2 c', p1=self.window_size, p2=self.window_size)
|
|
||||||
h_windows = x.size(1)
|
|
||||||
w_windows = x.size(2)
|
|
||||||
# square validation
|
|
||||||
# assert h_windows == w_windows
|
|
||||||
|
|
||||||
x = rearrange(x, 'b w1 w2 p1 p2 c -> b (w1 w2) (p1 p2) c', p1=self.window_size, p2=self.window_size)
|
|
||||||
qkv = self.embedding_layer(x)
|
|
||||||
q, k, v = rearrange(qkv, 'b nw np (threeh c) -> threeh b nw np c', c=self.head_dim).chunk(3, dim=0)
|
|
||||||
sim = torch.einsum('hbwpc,hbwqc->hbwpq', q, k) * self.scale
|
|
||||||
# Adding learnable relative embedding
|
|
||||||
sim = sim + rearrange(self.relative_embedding(), 'h p q -> h 1 1 p q')
|
|
||||||
# Using Attn Mask to distinguish different subwindows.
|
|
||||||
if self.type != 'W':
|
|
||||||
attn_mask = self.generate_mask(h_windows, w_windows, self.window_size, shift=self.window_size // 2)
|
|
||||||
sim = sim.masked_fill_(attn_mask, float("-inf"))
|
|
||||||
|
|
||||||
probs = nn.functional.softmax(sim, dim=-1)
|
|
||||||
output = torch.einsum('hbwij,hbwjc->hbwic', probs, v)
|
|
||||||
output = rearrange(output, 'h b w p c -> b w p (h c)')
|
|
||||||
output = self.linear(output)
|
|
||||||
output = rearrange(output, 'b (w1 w2) (p1 p2) c -> b (w1 p1) (w2 p2) c', w1=h_windows, p1=self.window_size)
|
|
||||||
|
|
||||||
if self.type != 'W':
|
|
||||||
output = torch.roll(output, shifts=(self.window_size // 2, self.window_size // 2), dims=(1, 2))
|
|
||||||
|
|
||||||
return output
|
|
||||||
|
|
||||||
def relative_embedding(self):
|
|
||||||
cord = torch.tensor(np.array([[i, j] for i in range(self.window_size) for j in range(self.window_size)]))
|
|
||||||
relation = cord[:, None, :] - cord[None, :, :] + self.window_size - 1
|
|
||||||
# negative is allowed
|
|
||||||
return self.relative_position_params[:, relation[:, :, 0].long(), relation[:, :, 1].long()]
|
|
||||||
|
|
||||||
|
|
||||||
class Block(nn.Module):
|
|
||||||
def __init__(self, input_dim, output_dim, head_dim, window_size, drop_path, type='W', input_resolution=None):
|
|
||||||
""" SwinTransformer Block
|
|
||||||
"""
|
|
||||||
super(Block, self).__init__()
|
|
||||||
self.input_dim = input_dim
|
|
||||||
self.output_dim = output_dim
|
|
||||||
assert type in ['W', 'SW']
|
|
||||||
self.type = type
|
|
||||||
if input_resolution <= window_size:
|
|
||||||
self.type = 'W'
|
|
||||||
|
|
||||||
self.ln1 = nn.LayerNorm(input_dim)
|
|
||||||
self.msa = WMSA(input_dim, input_dim, head_dim, window_size, self.type)
|
|
||||||
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
|
|
||||||
self.ln2 = nn.LayerNorm(input_dim)
|
|
||||||
self.mlp = nn.Sequential(
|
|
||||||
nn.Linear(input_dim, 4 * input_dim),
|
|
||||||
nn.GELU(),
|
|
||||||
nn.Linear(4 * input_dim, output_dim),
|
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = x + self.drop_path(self.msa(self.ln1(x)))
|
|
||||||
x = x + self.drop_path(self.mlp(self.ln2(x)))
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class ConvTransBlock(nn.Module):
|
|
||||||
def __init__(self, conv_dim, trans_dim, head_dim, window_size, drop_path, type='W', input_resolution=None):
|
|
||||||
""" SwinTransformer and Conv Block
|
|
||||||
"""
|
|
||||||
super(ConvTransBlock, self).__init__()
|
|
||||||
self.conv_dim = conv_dim
|
|
||||||
self.trans_dim = trans_dim
|
|
||||||
self.head_dim = head_dim
|
|
||||||
self.window_size = window_size
|
|
||||||
self.drop_path = drop_path
|
|
||||||
self.type = type
|
|
||||||
self.input_resolution = input_resolution
|
|
||||||
|
|
||||||
assert self.type in ['W', 'SW']
|
|
||||||
if self.input_resolution <= self.window_size:
|
|
||||||
self.type = 'W'
|
|
||||||
|
|
||||||
self.trans_block = Block(self.trans_dim, self.trans_dim, self.head_dim, self.window_size, self.drop_path,
|
|
||||||
self.type, self.input_resolution)
|
|
||||||
self.conv1_1 = nn.Conv2d(self.conv_dim + self.trans_dim, self.conv_dim + self.trans_dim, 1, 1, 0, bias=True)
|
|
||||||
self.conv1_2 = nn.Conv2d(self.conv_dim + self.trans_dim, self.conv_dim + self.trans_dim, 1, 1, 0, bias=True)
|
|
||||||
|
|
||||||
self.conv_block = nn.Sequential(
|
|
||||||
nn.Conv2d(self.conv_dim, self.conv_dim, 3, 1, 1, bias=False),
|
|
||||||
nn.ReLU(True),
|
|
||||||
nn.Conv2d(self.conv_dim, self.conv_dim, 3, 1, 1, bias=False)
|
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
conv_x, trans_x = torch.split(self.conv1_1(x), (self.conv_dim, self.trans_dim), dim=1)
|
|
||||||
conv_x = self.conv_block(conv_x) + conv_x
|
|
||||||
trans_x = Rearrange('b c h w -> b h w c')(trans_x)
|
|
||||||
trans_x = self.trans_block(trans_x)
|
|
||||||
trans_x = Rearrange('b h w c -> b c h w')(trans_x)
|
|
||||||
res = self.conv1_2(torch.cat((conv_x, trans_x), dim=1))
|
|
||||||
x = x + res
|
|
||||||
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class SCUNet(nn.Module):
|
|
||||||
# def __init__(self, in_nc=3, config=[2, 2, 2, 2, 2, 2, 2], dim=64, drop_path_rate=0.0, input_resolution=256):
|
|
||||||
def __init__(self, in_nc=3, config=None, dim=64, drop_path_rate=0.0, input_resolution=256):
|
|
||||||
super(SCUNet, self).__init__()
|
|
||||||
if config is None:
|
|
||||||
config = [2, 2, 2, 2, 2, 2, 2]
|
|
||||||
self.config = config
|
|
||||||
self.dim = dim
|
|
||||||
self.head_dim = 32
|
|
||||||
self.window_size = 8
|
|
||||||
|
|
||||||
# drop path rate for each layer
|
|
||||||
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(config))]
|
|
||||||
|
|
||||||
self.m_head = [nn.Conv2d(in_nc, dim, 3, 1, 1, bias=False)]
|
|
||||||
|
|
||||||
begin = 0
|
|
||||||
self.m_down1 = [ConvTransBlock(dim // 2, dim // 2, self.head_dim, self.window_size, dpr[i + begin],
|
|
||||||
'W' if not i % 2 else 'SW', input_resolution)
|
|
||||||
for i in range(config[0])] + \
|
|
||||||
[nn.Conv2d(dim, 2 * dim, 2, 2, 0, bias=False)]
|
|
||||||
|
|
||||||
begin += config[0]
|
|
||||||
self.m_down2 = [ConvTransBlock(dim, dim, self.head_dim, self.window_size, dpr[i + begin],
|
|
||||||
'W' if not i % 2 else 'SW', input_resolution // 2)
|
|
||||||
for i in range(config[1])] + \
|
|
||||||
[nn.Conv2d(2 * dim, 4 * dim, 2, 2, 0, bias=False)]
|
|
||||||
|
|
||||||
begin += config[1]
|
|
||||||
self.m_down3 = [ConvTransBlock(2 * dim, 2 * dim, self.head_dim, self.window_size, dpr[i + begin],
|
|
||||||
'W' if not i % 2 else 'SW', input_resolution // 4)
|
|
||||||
for i in range(config[2])] + \
|
|
||||||
[nn.Conv2d(4 * dim, 8 * dim, 2, 2, 0, bias=False)]
|
|
||||||
|
|
||||||
begin += config[2]
|
|
||||||
self.m_body = [ConvTransBlock(4 * dim, 4 * dim, self.head_dim, self.window_size, dpr[i + begin],
|
|
||||||
'W' if not i % 2 else 'SW', input_resolution // 8)
|
|
||||||
for i in range(config[3])]
|
|
||||||
|
|
||||||
begin += config[3]
|
|
||||||
self.m_up3 = [nn.ConvTranspose2d(8 * dim, 4 * dim, 2, 2, 0, bias=False), ] + \
|
|
||||||
[ConvTransBlock(2 * dim, 2 * dim, self.head_dim, self.window_size, dpr[i + begin],
|
|
||||||
'W' if not i % 2 else 'SW', input_resolution // 4)
|
|
||||||
for i in range(config[4])]
|
|
||||||
|
|
||||||
begin += config[4]
|
|
||||||
self.m_up2 = [nn.ConvTranspose2d(4 * dim, 2 * dim, 2, 2, 0, bias=False), ] + \
|
|
||||||
[ConvTransBlock(dim, dim, self.head_dim, self.window_size, dpr[i + begin],
|
|
||||||
'W' if not i % 2 else 'SW', input_resolution // 2)
|
|
||||||
for i in range(config[5])]
|
|
||||||
|
|
||||||
begin += config[5]
|
|
||||||
self.m_up1 = [nn.ConvTranspose2d(2 * dim, dim, 2, 2, 0, bias=False), ] + \
|
|
||||||
[ConvTransBlock(dim // 2, dim // 2, self.head_dim, self.window_size, dpr[i + begin],
|
|
||||||
'W' if not i % 2 else 'SW', input_resolution)
|
|
||||||
for i in range(config[6])]
|
|
||||||
|
|
||||||
self.m_tail = [nn.Conv2d(dim, in_nc, 3, 1, 1, bias=False)]
|
|
||||||
|
|
||||||
self.m_head = nn.Sequential(*self.m_head)
|
|
||||||
self.m_down1 = nn.Sequential(*self.m_down1)
|
|
||||||
self.m_down2 = nn.Sequential(*self.m_down2)
|
|
||||||
self.m_down3 = nn.Sequential(*self.m_down3)
|
|
||||||
self.m_body = nn.Sequential(*self.m_body)
|
|
||||||
self.m_up3 = nn.Sequential(*self.m_up3)
|
|
||||||
self.m_up2 = nn.Sequential(*self.m_up2)
|
|
||||||
self.m_up1 = nn.Sequential(*self.m_up1)
|
|
||||||
self.m_tail = nn.Sequential(*self.m_tail)
|
|
||||||
# self.apply(self._init_weights)
|
|
||||||
|
|
||||||
def forward(self, x0):
|
|
||||||
|
|
||||||
h, w = x0.size()[-2:]
|
|
||||||
paddingBottom = int(np.ceil(h / 64) * 64 - h)
|
|
||||||
paddingRight = int(np.ceil(w / 64) * 64 - w)
|
|
||||||
x0 = nn.ReplicationPad2d((0, paddingRight, 0, paddingBottom))(x0)
|
|
||||||
|
|
||||||
x1 = self.m_head(x0)
|
|
||||||
x2 = self.m_down1(x1)
|
|
||||||
x3 = self.m_down2(x2)
|
|
||||||
x4 = self.m_down3(x3)
|
|
||||||
x = self.m_body(x4)
|
|
||||||
x = self.m_up3(x + x4)
|
|
||||||
x = self.m_up2(x + x3)
|
|
||||||
x = self.m_up1(x + x2)
|
|
||||||
x = self.m_tail(x + x1)
|
|
||||||
|
|
||||||
x = x[..., :h, :w]
|
|
||||||
|
|
||||||
return x
|
|
||||||
|
|
||||||
def _init_weights(self, m):
|
|
||||||
if isinstance(m, nn.Linear):
|
|
||||||
trunc_normal_(m.weight, std=.02)
|
|
||||||
if m.bias is not None:
|
|
||||||
nn.init.constant_(m.bias, 0)
|
|
||||||
elif isinstance(m, nn.LayerNorm):
|
|
||||||
nn.init.constant_(m.bias, 0)
|
|
||||||
nn.init.constant_(m.weight, 1.0)
|
|
||||||
@@ -1,20 +1,15 @@
|
|||||||
|
import logging
|
||||||
import sys
|
import sys
|
||||||
import platform
|
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
import torch
|
import torch
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from tqdm import tqdm
|
|
||||||
|
|
||||||
from modules import modelloader, devices, script_callbacks, shared
|
from modules import devices, modelloader, script_callbacks, shared, upscaler_utils
|
||||||
from modules.shared import opts, state
|
|
||||||
from swinir_model_arch import SwinIR
|
|
||||||
from swinir_model_arch_v2 import Swin2SR
|
|
||||||
from modules.upscaler import Upscaler, UpscalerData
|
from modules.upscaler import Upscaler, UpscalerData
|
||||||
|
|
||||||
SWINIR_MODEL_URL = "https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/003_realSR_BSRGAN_DFOWMFC_s64w8_SwinIR-L_x4_GAN.pth"
|
SWINIR_MODEL_URL = "https://github.com/JingyunLiang/SwinIR/releases/download/v0.0/003_realSR_BSRGAN_DFOWMFC_s64w8_SwinIR-L_x4_GAN.pth"
|
||||||
|
|
||||||
device_swinir = devices.get_device_for('swinir')
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class UpscalerSwinIR(Upscaler):
|
class UpscalerSwinIR(Upscaler):
|
||||||
@@ -37,26 +32,28 @@ class UpscalerSwinIR(Upscaler):
|
|||||||
scalers.append(model_data)
|
scalers.append(model_data)
|
||||||
self.scalers = scalers
|
self.scalers = scalers
|
||||||
|
|
||||||
def do_upscale(self, img, model_file):
|
def do_upscale(self, img: Image.Image, model_file: str) -> Image.Image:
|
||||||
use_compile = hasattr(opts, 'SWIN_torch_compile') and opts.SWIN_torch_compile \
|
current_config = (model_file, shared.opts.SWIN_tile)
|
||||||
and int(torch.__version__.split('.')[0]) >= 2 and platform.system() != "Windows"
|
|
||||||
current_config = (model_file, opts.SWIN_tile)
|
|
||||||
|
|
||||||
if use_compile and self._cached_model_config == current_config:
|
if self._cached_model_config == current_config:
|
||||||
model = self._cached_model
|
model = self._cached_model
|
||||||
else:
|
else:
|
||||||
self._cached_model = None
|
|
||||||
try:
|
try:
|
||||||
model = self.load_model(model_file)
|
model = self.load_model(model_file)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed loading SwinIR model {model_file}: {e}", file=sys.stderr)
|
print(f"Failed loading SwinIR model {model_file}: {e}", file=sys.stderr)
|
||||||
return img
|
return img
|
||||||
model = model.to(device_swinir, dtype=devices.dtype)
|
self._cached_model = model
|
||||||
if use_compile:
|
self._cached_model_config = current_config
|
||||||
model = torch.compile(model)
|
|
||||||
self._cached_model = model
|
img = upscaler_utils.upscale_2(
|
||||||
self._cached_model_config = current_config
|
img,
|
||||||
img = upscale(img, model)
|
model,
|
||||||
|
tile_size=shared.opts.SWIN_tile,
|
||||||
|
tile_overlap=shared.opts.SWIN_tile_overlap,
|
||||||
|
scale=model.scale,
|
||||||
|
desc="SwinIR",
|
||||||
|
)
|
||||||
devices.torch_gc()
|
devices.torch_gc()
|
||||||
return img
|
return img
|
||||||
|
|
||||||
@@ -69,115 +66,22 @@ class UpscalerSwinIR(Upscaler):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
filename = path
|
filename = path
|
||||||
if filename.endswith(".v2.pth"):
|
|
||||||
model = Swin2SR(
|
|
||||||
upscale=scale,
|
|
||||||
in_chans=3,
|
|
||||||
img_size=64,
|
|
||||||
window_size=8,
|
|
||||||
img_range=1.0,
|
|
||||||
depths=[6, 6, 6, 6, 6, 6],
|
|
||||||
embed_dim=180,
|
|
||||||
num_heads=[6, 6, 6, 6, 6, 6],
|
|
||||||
mlp_ratio=2,
|
|
||||||
upsampler="nearest+conv",
|
|
||||||
resi_connection="1conv",
|
|
||||||
)
|
|
||||||
params = None
|
|
||||||
else:
|
|
||||||
model = SwinIR(
|
|
||||||
upscale=scale,
|
|
||||||
in_chans=3,
|
|
||||||
img_size=64,
|
|
||||||
window_size=8,
|
|
||||||
img_range=1.0,
|
|
||||||
depths=[6, 6, 6, 6, 6, 6, 6, 6, 6],
|
|
||||||
embed_dim=240,
|
|
||||||
num_heads=[8, 8, 8, 8, 8, 8, 8, 8, 8],
|
|
||||||
mlp_ratio=2,
|
|
||||||
upsampler="nearest+conv",
|
|
||||||
resi_connection="3conv",
|
|
||||||
)
|
|
||||||
params = "params_ema"
|
|
||||||
|
|
||||||
pretrained_model = torch.load(filename)
|
model_descriptor = modelloader.load_spandrel_model(
|
||||||
if params is not None:
|
filename,
|
||||||
model.load_state_dict(pretrained_model[params], strict=True)
|
device=self._get_device(),
|
||||||
else:
|
prefer_half=(devices.dtype == torch.float16),
|
||||||
model.load_state_dict(pretrained_model, strict=True)
|
expected_architecture="SwinIR",
|
||||||
return model
|
)
|
||||||
|
if getattr(shared.opts, 'SWIN_torch_compile', False):
|
||||||
|
try:
|
||||||
|
model_descriptor.model.compile()
|
||||||
|
except Exception:
|
||||||
|
logger.warning("Failed to compile SwinIR model, fallback to JIT", exc_info=True)
|
||||||
|
return model_descriptor
|
||||||
|
|
||||||
|
def _get_device(self):
|
||||||
def upscale(
|
return devices.get_device_for('swinir')
|
||||||
img,
|
|
||||||
model,
|
|
||||||
tile=None,
|
|
||||||
tile_overlap=None,
|
|
||||||
window_size=8,
|
|
||||||
scale=4,
|
|
||||||
):
|
|
||||||
tile = tile or opts.SWIN_tile
|
|
||||||
tile_overlap = tile_overlap or opts.SWIN_tile_overlap
|
|
||||||
|
|
||||||
|
|
||||||
img = np.array(img)
|
|
||||||
img = img[:, :, ::-1]
|
|
||||||
img = np.moveaxis(img, 2, 0) / 255
|
|
||||||
img = torch.from_numpy(img).float()
|
|
||||||
img = img.unsqueeze(0).to(device_swinir, dtype=devices.dtype)
|
|
||||||
with torch.no_grad(), devices.autocast():
|
|
||||||
_, _, h_old, w_old = img.size()
|
|
||||||
h_pad = (h_old // window_size + 1) * window_size - h_old
|
|
||||||
w_pad = (w_old // window_size + 1) * window_size - w_old
|
|
||||||
img = torch.cat([img, torch.flip(img, [2])], 2)[:, :, : h_old + h_pad, :]
|
|
||||||
img = torch.cat([img, torch.flip(img, [3])], 3)[:, :, :, : w_old + w_pad]
|
|
||||||
output = inference(img, model, tile, tile_overlap, window_size, scale)
|
|
||||||
output = output[..., : h_old * scale, : w_old * scale]
|
|
||||||
output = output.data.squeeze().float().cpu().clamp_(0, 1).numpy()
|
|
||||||
if output.ndim == 3:
|
|
||||||
output = np.transpose(
|
|
||||||
output[[2, 1, 0], :, :], (1, 2, 0)
|
|
||||||
) # CHW-RGB to HCW-BGR
|
|
||||||
output = (output * 255.0).round().astype(np.uint8) # float32 to uint8
|
|
||||||
return Image.fromarray(output, "RGB")
|
|
||||||
|
|
||||||
|
|
||||||
def inference(img, model, tile, tile_overlap, window_size, scale):
|
|
||||||
# test the image tile by tile
|
|
||||||
b, c, h, w = img.size()
|
|
||||||
tile = min(tile, h, w)
|
|
||||||
assert tile % window_size == 0, "tile size should be a multiple of window_size"
|
|
||||||
sf = scale
|
|
||||||
|
|
||||||
stride = tile - tile_overlap
|
|
||||||
h_idx_list = list(range(0, h - tile, stride)) + [h - tile]
|
|
||||||
w_idx_list = list(range(0, w - tile, stride)) + [w - tile]
|
|
||||||
E = torch.zeros(b, c, h * sf, w * sf, dtype=devices.dtype, device=device_swinir).type_as(img)
|
|
||||||
W = torch.zeros_like(E, dtype=devices.dtype, device=device_swinir)
|
|
||||||
|
|
||||||
with tqdm(total=len(h_idx_list) * len(w_idx_list), desc="SwinIR tiles") as pbar:
|
|
||||||
for h_idx in h_idx_list:
|
|
||||||
if state.interrupted or state.skipped:
|
|
||||||
break
|
|
||||||
|
|
||||||
for w_idx in w_idx_list:
|
|
||||||
if state.interrupted or state.skipped:
|
|
||||||
break
|
|
||||||
|
|
||||||
in_patch = img[..., h_idx: h_idx + tile, w_idx: w_idx + tile]
|
|
||||||
out_patch = model(in_patch)
|
|
||||||
out_patch_mask = torch.ones_like(out_patch)
|
|
||||||
|
|
||||||
E[
|
|
||||||
..., h_idx * sf: (h_idx + tile) * sf, w_idx * sf: (w_idx + tile) * sf
|
|
||||||
].add_(out_patch)
|
|
||||||
W[
|
|
||||||
..., h_idx * sf: (h_idx + tile) * sf, w_idx * sf: (w_idx + tile) * sf
|
|
||||||
].add_(out_patch_mask)
|
|
||||||
pbar.update(1)
|
|
||||||
output = E.div_(W)
|
|
||||||
|
|
||||||
return output
|
|
||||||
|
|
||||||
|
|
||||||
def on_ui_settings():
|
def on_ui_settings():
|
||||||
@@ -185,8 +89,7 @@ def on_ui_settings():
|
|||||||
|
|
||||||
shared.opts.add_option("SWIN_tile", shared.OptionInfo(192, "Tile size for all SwinIR.", gr.Slider, {"minimum": 16, "maximum": 512, "step": 16}, section=('upscaling', "Upscaling")))
|
shared.opts.add_option("SWIN_tile", shared.OptionInfo(192, "Tile size for all SwinIR.", gr.Slider, {"minimum": 16, "maximum": 512, "step": 16}, section=('upscaling', "Upscaling")))
|
||||||
shared.opts.add_option("SWIN_tile_overlap", shared.OptionInfo(8, "Tile overlap, in pixels for SwinIR. Low values = visible seam.", gr.Slider, {"minimum": 0, "maximum": 48, "step": 1}, section=('upscaling', "Upscaling")))
|
shared.opts.add_option("SWIN_tile_overlap", shared.OptionInfo(8, "Tile overlap, in pixels for SwinIR. Low values = visible seam.", gr.Slider, {"minimum": 0, "maximum": 48, "step": 1}, section=('upscaling', "Upscaling")))
|
||||||
if int(torch.__version__.split('.')[0]) >= 2 and platform.system() != "Windows": # torch.compile() require pytorch 2.0 or above, and not on Windows
|
shared.opts.add_option("SWIN_torch_compile", shared.OptionInfo(False, "Use torch.compile to accelerate SwinIR.", gr.Checkbox, {"interactive": True}, section=('upscaling', "Upscaling")).info("Takes longer on first run"))
|
||||||
shared.opts.add_option("SWIN_torch_compile", shared.OptionInfo(False, "Use torch.compile to accelerate SwinIR.", gr.Checkbox, {"interactive": True}, section=('upscaling', "Upscaling")).info("Takes longer on first run"))
|
|
||||||
|
|
||||||
|
|
||||||
script_callbacks.on_ui_settings(on_ui_settings)
|
script_callbacks.on_ui_settings(on_ui_settings)
|
||||||
|
|||||||
@@ -1,867 +0,0 @@
|
|||||||
# -----------------------------------------------------------------------------------
|
|
||||||
# SwinIR: Image Restoration Using Swin Transformer, https://arxiv.org/abs/2108.10257
|
|
||||||
# Originally Written by Ze Liu, Modified by Jingyun Liang.
|
|
||||||
# -----------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
import math
|
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
import torch.nn.functional as F
|
|
||||||
import torch.utils.checkpoint as checkpoint
|
|
||||||
from timm.models.layers import DropPath, to_2tuple, trunc_normal_
|
|
||||||
|
|
||||||
|
|
||||||
class Mlp(nn.Module):
|
|
||||||
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):
|
|
||||||
super().__init__()
|
|
||||||
out_features = out_features or in_features
|
|
||||||
hidden_features = hidden_features or in_features
|
|
||||||
self.fc1 = nn.Linear(in_features, hidden_features)
|
|
||||||
self.act = act_layer()
|
|
||||||
self.fc2 = nn.Linear(hidden_features, out_features)
|
|
||||||
self.drop = nn.Dropout(drop)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = self.fc1(x)
|
|
||||||
x = self.act(x)
|
|
||||||
x = self.drop(x)
|
|
||||||
x = self.fc2(x)
|
|
||||||
x = self.drop(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
def window_partition(x, window_size):
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
x: (B, H, W, C)
|
|
||||||
window_size (int): window size
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
windows: (num_windows*B, window_size, window_size, C)
|
|
||||||
"""
|
|
||||||
B, H, W, C = x.shape
|
|
||||||
x = x.view(B, H // window_size, window_size, W // window_size, window_size, C)
|
|
||||||
windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C)
|
|
||||||
return windows
|
|
||||||
|
|
||||||
|
|
||||||
def window_reverse(windows, window_size, H, W):
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
windows: (num_windows*B, window_size, window_size, C)
|
|
||||||
window_size (int): Window size
|
|
||||||
H (int): Height of image
|
|
||||||
W (int): Width of image
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
x: (B, H, W, C)
|
|
||||||
"""
|
|
||||||
B = int(windows.shape[0] / (H * W / window_size / window_size))
|
|
||||||
x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1)
|
|
||||||
x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class WindowAttention(nn.Module):
|
|
||||||
r""" Window based multi-head self attention (W-MSA) module with relative position bias.
|
|
||||||
It supports both of shifted and non-shifted window.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
dim (int): Number of input channels.
|
|
||||||
window_size (tuple[int]): The height and width of the window.
|
|
||||||
num_heads (int): Number of attention heads.
|
|
||||||
qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True
|
|
||||||
qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set
|
|
||||||
attn_drop (float, optional): Dropout ratio of attention weight. Default: 0.0
|
|
||||||
proj_drop (float, optional): Dropout ratio of output. Default: 0.0
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0., proj_drop=0.):
|
|
||||||
|
|
||||||
super().__init__()
|
|
||||||
self.dim = dim
|
|
||||||
self.window_size = window_size # Wh, Ww
|
|
||||||
self.num_heads = num_heads
|
|
||||||
head_dim = dim // num_heads
|
|
||||||
self.scale = qk_scale or head_dim ** -0.5
|
|
||||||
|
|
||||||
# define a parameter table of relative position bias
|
|
||||||
self.relative_position_bias_table = nn.Parameter(
|
|
||||||
torch.zeros((2 * window_size[0] - 1) * (2 * window_size[1] - 1), num_heads)) # 2*Wh-1 * 2*Ww-1, nH
|
|
||||||
|
|
||||||
# get pair-wise relative position index for each token inside the window
|
|
||||||
coords_h = torch.arange(self.window_size[0])
|
|
||||||
coords_w = torch.arange(self.window_size[1])
|
|
||||||
coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww
|
|
||||||
coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww
|
|
||||||
relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :] # 2, Wh*Ww, Wh*Ww
|
|
||||||
relative_coords = relative_coords.permute(1, 2, 0).contiguous() # Wh*Ww, Wh*Ww, 2
|
|
||||||
relative_coords[:, :, 0] += self.window_size[0] - 1 # shift to start from 0
|
|
||||||
relative_coords[:, :, 1] += self.window_size[1] - 1
|
|
||||||
relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1
|
|
||||||
relative_position_index = relative_coords.sum(-1) # Wh*Ww, Wh*Ww
|
|
||||||
self.register_buffer("relative_position_index", relative_position_index)
|
|
||||||
|
|
||||||
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
|
|
||||||
self.attn_drop = nn.Dropout(attn_drop)
|
|
||||||
self.proj = nn.Linear(dim, dim)
|
|
||||||
|
|
||||||
self.proj_drop = nn.Dropout(proj_drop)
|
|
||||||
|
|
||||||
trunc_normal_(self.relative_position_bias_table, std=.02)
|
|
||||||
self.softmax = nn.Softmax(dim=-1)
|
|
||||||
|
|
||||||
def forward(self, x, mask=None):
|
|
||||||
"""
|
|
||||||
Args:
|
|
||||||
x: input features with shape of (num_windows*B, N, C)
|
|
||||||
mask: (0/-inf) mask with shape of (num_windows, Wh*Ww, Wh*Ww) or None
|
|
||||||
"""
|
|
||||||
B_, N, C = x.shape
|
|
||||||
qkv = self.qkv(x).reshape(B_, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
|
|
||||||
q, k, v = qkv[0], qkv[1], qkv[2] # make torchscript happy (cannot use tensor as tuple)
|
|
||||||
|
|
||||||
q = q * self.scale
|
|
||||||
attn = (q @ k.transpose(-2, -1))
|
|
||||||
|
|
||||||
relative_position_bias = self.relative_position_bias_table[self.relative_position_index.view(-1)].view(
|
|
||||||
self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1) # Wh*Ww,Wh*Ww,nH
|
|
||||||
relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous() # nH, Wh*Ww, Wh*Ww
|
|
||||||
attn = attn + relative_position_bias.unsqueeze(0)
|
|
||||||
|
|
||||||
if mask is not None:
|
|
||||||
nW = mask.shape[0]
|
|
||||||
attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0)
|
|
||||||
attn = attn.view(-1, self.num_heads, N, N)
|
|
||||||
attn = self.softmax(attn)
|
|
||||||
else:
|
|
||||||
attn = self.softmax(attn)
|
|
||||||
|
|
||||||
attn = self.attn_drop(attn)
|
|
||||||
|
|
||||||
x = (attn @ v).transpose(1, 2).reshape(B_, N, C)
|
|
||||||
x = self.proj(x)
|
|
||||||
x = self.proj_drop(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
def extra_repr(self) -> str:
|
|
||||||
return f'dim={self.dim}, window_size={self.window_size}, num_heads={self.num_heads}'
|
|
||||||
|
|
||||||
def flops(self, N):
|
|
||||||
# calculate flops for 1 window with token length of N
|
|
||||||
flops = 0
|
|
||||||
# qkv = self.qkv(x)
|
|
||||||
flops += N * self.dim * 3 * self.dim
|
|
||||||
# attn = (q @ k.transpose(-2, -1))
|
|
||||||
flops += self.num_heads * N * (self.dim // self.num_heads) * N
|
|
||||||
# x = (attn @ v)
|
|
||||||
flops += self.num_heads * N * N * (self.dim // self.num_heads)
|
|
||||||
# x = self.proj(x)
|
|
||||||
flops += N * self.dim * self.dim
|
|
||||||
return flops
|
|
||||||
|
|
||||||
|
|
||||||
class SwinTransformerBlock(nn.Module):
|
|
||||||
r""" Swin Transformer Block.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
dim (int): Number of input channels.
|
|
||||||
input_resolution (tuple[int]): Input resolution.
|
|
||||||
num_heads (int): Number of attention heads.
|
|
||||||
window_size (int): Window size.
|
|
||||||
shift_size (int): Shift size for SW-MSA.
|
|
||||||
mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.
|
|
||||||
qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True
|
|
||||||
qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set.
|
|
||||||
drop (float, optional): Dropout rate. Default: 0.0
|
|
||||||
attn_drop (float, optional): Attention dropout rate. Default: 0.0
|
|
||||||
drop_path (float, optional): Stochastic depth rate. Default: 0.0
|
|
||||||
act_layer (nn.Module, optional): Activation layer. Default: nn.GELU
|
|
||||||
norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, dim, input_resolution, num_heads, window_size=7, shift_size=0,
|
|
||||||
mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0., drop_path=0.,
|
|
||||||
act_layer=nn.GELU, norm_layer=nn.LayerNorm):
|
|
||||||
super().__init__()
|
|
||||||
self.dim = dim
|
|
||||||
self.input_resolution = input_resolution
|
|
||||||
self.num_heads = num_heads
|
|
||||||
self.window_size = window_size
|
|
||||||
self.shift_size = shift_size
|
|
||||||
self.mlp_ratio = mlp_ratio
|
|
||||||
if min(self.input_resolution) <= self.window_size:
|
|
||||||
# if window size is larger than input resolution, we don't partition windows
|
|
||||||
self.shift_size = 0
|
|
||||||
self.window_size = min(self.input_resolution)
|
|
||||||
assert 0 <= self.shift_size < self.window_size, "shift_size must in 0-window_size"
|
|
||||||
|
|
||||||
self.norm1 = norm_layer(dim)
|
|
||||||
self.attn = WindowAttention(
|
|
||||||
dim, window_size=to_2tuple(self.window_size), num_heads=num_heads,
|
|
||||||
qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop)
|
|
||||||
|
|
||||||
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
|
|
||||||
self.norm2 = norm_layer(dim)
|
|
||||||
mlp_hidden_dim = int(dim * mlp_ratio)
|
|
||||||
self.mlp = Mlp(in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop)
|
|
||||||
|
|
||||||
if self.shift_size > 0:
|
|
||||||
attn_mask = self.calculate_mask(self.input_resolution)
|
|
||||||
else:
|
|
||||||
attn_mask = None
|
|
||||||
|
|
||||||
self.register_buffer("attn_mask", attn_mask)
|
|
||||||
|
|
||||||
def calculate_mask(self, x_size):
|
|
||||||
# calculate attention mask for SW-MSA
|
|
||||||
H, W = x_size
|
|
||||||
img_mask = torch.zeros((1, H, W, 1)) # 1 H W 1
|
|
||||||
h_slices = (slice(0, -self.window_size),
|
|
||||||
slice(-self.window_size, -self.shift_size),
|
|
||||||
slice(-self.shift_size, None))
|
|
||||||
w_slices = (slice(0, -self.window_size),
|
|
||||||
slice(-self.window_size, -self.shift_size),
|
|
||||||
slice(-self.shift_size, None))
|
|
||||||
cnt = 0
|
|
||||||
for h in h_slices:
|
|
||||||
for w in w_slices:
|
|
||||||
img_mask[:, h, w, :] = cnt
|
|
||||||
cnt += 1
|
|
||||||
|
|
||||||
mask_windows = window_partition(img_mask, self.window_size) # nW, window_size, window_size, 1
|
|
||||||
mask_windows = mask_windows.view(-1, self.window_size * self.window_size)
|
|
||||||
attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2)
|
|
||||||
attn_mask = attn_mask.masked_fill(attn_mask != 0, float(-100.0)).masked_fill(attn_mask == 0, float(0.0))
|
|
||||||
|
|
||||||
return attn_mask
|
|
||||||
|
|
||||||
def forward(self, x, x_size):
|
|
||||||
H, W = x_size
|
|
||||||
B, L, C = x.shape
|
|
||||||
# assert L == H * W, "input feature has wrong size"
|
|
||||||
|
|
||||||
shortcut = x
|
|
||||||
x = self.norm1(x)
|
|
||||||
x = x.view(B, H, W, C)
|
|
||||||
|
|
||||||
# cyclic shift
|
|
||||||
if self.shift_size > 0:
|
|
||||||
shifted_x = torch.roll(x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2))
|
|
||||||
else:
|
|
||||||
shifted_x = x
|
|
||||||
|
|
||||||
# partition windows
|
|
||||||
x_windows = window_partition(shifted_x, self.window_size) # nW*B, window_size, window_size, C
|
|
||||||
x_windows = x_windows.view(-1, self.window_size * self.window_size, C) # nW*B, window_size*window_size, C
|
|
||||||
|
|
||||||
# W-MSA/SW-MSA (to be compatible for testing on images whose shapes are the multiple of window size
|
|
||||||
if self.input_resolution == x_size:
|
|
||||||
attn_windows = self.attn(x_windows, mask=self.attn_mask) # nW*B, window_size*window_size, C
|
|
||||||
else:
|
|
||||||
attn_windows = self.attn(x_windows, mask=self.calculate_mask(x_size).to(x.device))
|
|
||||||
|
|
||||||
# merge windows
|
|
||||||
attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C)
|
|
||||||
shifted_x = window_reverse(attn_windows, self.window_size, H, W) # B H' W' C
|
|
||||||
|
|
||||||
# reverse cyclic shift
|
|
||||||
if self.shift_size > 0:
|
|
||||||
x = torch.roll(shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2))
|
|
||||||
else:
|
|
||||||
x = shifted_x
|
|
||||||
x = x.view(B, H * W, C)
|
|
||||||
|
|
||||||
# FFN
|
|
||||||
x = shortcut + self.drop_path(x)
|
|
||||||
x = x + self.drop_path(self.mlp(self.norm2(x)))
|
|
||||||
|
|
||||||
return x
|
|
||||||
|
|
||||||
def extra_repr(self) -> str:
|
|
||||||
return f"dim={self.dim}, input_resolution={self.input_resolution}, num_heads={self.num_heads}, " \
|
|
||||||
f"window_size={self.window_size}, shift_size={self.shift_size}, mlp_ratio={self.mlp_ratio}"
|
|
||||||
|
|
||||||
def flops(self):
|
|
||||||
flops = 0
|
|
||||||
H, W = self.input_resolution
|
|
||||||
# norm1
|
|
||||||
flops += self.dim * H * W
|
|
||||||
# W-MSA/SW-MSA
|
|
||||||
nW = H * W / self.window_size / self.window_size
|
|
||||||
flops += nW * self.attn.flops(self.window_size * self.window_size)
|
|
||||||
# mlp
|
|
||||||
flops += 2 * H * W * self.dim * self.dim * self.mlp_ratio
|
|
||||||
# norm2
|
|
||||||
flops += self.dim * H * W
|
|
||||||
return flops
|
|
||||||
|
|
||||||
|
|
||||||
class PatchMerging(nn.Module):
|
|
||||||
r""" Patch Merging Layer.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
input_resolution (tuple[int]): Resolution of input feature.
|
|
||||||
dim (int): Number of input channels.
|
|
||||||
norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, input_resolution, dim, norm_layer=nn.LayerNorm):
|
|
||||||
super().__init__()
|
|
||||||
self.input_resolution = input_resolution
|
|
||||||
self.dim = dim
|
|
||||||
self.reduction = nn.Linear(4 * dim, 2 * dim, bias=False)
|
|
||||||
self.norm = norm_layer(4 * dim)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
"""
|
|
||||||
x: B, H*W, C
|
|
||||||
"""
|
|
||||||
H, W = self.input_resolution
|
|
||||||
B, L, C = x.shape
|
|
||||||
assert L == H * W, "input feature has wrong size"
|
|
||||||
assert H % 2 == 0 and W % 2 == 0, f"x size ({H}*{W}) are not even."
|
|
||||||
|
|
||||||
x = x.view(B, H, W, C)
|
|
||||||
|
|
||||||
x0 = x[:, 0::2, 0::2, :] # B H/2 W/2 C
|
|
||||||
x1 = x[:, 1::2, 0::2, :] # B H/2 W/2 C
|
|
||||||
x2 = x[:, 0::2, 1::2, :] # B H/2 W/2 C
|
|
||||||
x3 = x[:, 1::2, 1::2, :] # B H/2 W/2 C
|
|
||||||
x = torch.cat([x0, x1, x2, x3], -1) # B H/2 W/2 4*C
|
|
||||||
x = x.view(B, -1, 4 * C) # B H/2*W/2 4*C
|
|
||||||
|
|
||||||
x = self.norm(x)
|
|
||||||
x = self.reduction(x)
|
|
||||||
|
|
||||||
return x
|
|
||||||
|
|
||||||
def extra_repr(self) -> str:
|
|
||||||
return f"input_resolution={self.input_resolution}, dim={self.dim}"
|
|
||||||
|
|
||||||
def flops(self):
|
|
||||||
H, W = self.input_resolution
|
|
||||||
flops = H * W * self.dim
|
|
||||||
flops += (H // 2) * (W // 2) * 4 * self.dim * 2 * self.dim
|
|
||||||
return flops
|
|
||||||
|
|
||||||
|
|
||||||
class BasicLayer(nn.Module):
|
|
||||||
""" A basic Swin Transformer layer for one stage.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
dim (int): Number of input channels.
|
|
||||||
input_resolution (tuple[int]): Input resolution.
|
|
||||||
depth (int): Number of blocks.
|
|
||||||
num_heads (int): Number of attention heads.
|
|
||||||
window_size (int): Local window size.
|
|
||||||
mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.
|
|
||||||
qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True
|
|
||||||
qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set.
|
|
||||||
drop (float, optional): Dropout rate. Default: 0.0
|
|
||||||
attn_drop (float, optional): Attention dropout rate. Default: 0.0
|
|
||||||
drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0
|
|
||||||
norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm
|
|
||||||
downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None
|
|
||||||
use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, dim, input_resolution, depth, num_heads, window_size,
|
|
||||||
mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0.,
|
|
||||||
drop_path=0., norm_layer=nn.LayerNorm, downsample=None, use_checkpoint=False):
|
|
||||||
|
|
||||||
super().__init__()
|
|
||||||
self.dim = dim
|
|
||||||
self.input_resolution = input_resolution
|
|
||||||
self.depth = depth
|
|
||||||
self.use_checkpoint = use_checkpoint
|
|
||||||
|
|
||||||
# build blocks
|
|
||||||
self.blocks = nn.ModuleList([
|
|
||||||
SwinTransformerBlock(dim=dim, input_resolution=input_resolution,
|
|
||||||
num_heads=num_heads, window_size=window_size,
|
|
||||||
shift_size=0 if (i % 2 == 0) else window_size // 2,
|
|
||||||
mlp_ratio=mlp_ratio,
|
|
||||||
qkv_bias=qkv_bias, qk_scale=qk_scale,
|
|
||||||
drop=drop, attn_drop=attn_drop,
|
|
||||||
drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path,
|
|
||||||
norm_layer=norm_layer)
|
|
||||||
for i in range(depth)])
|
|
||||||
|
|
||||||
# patch merging layer
|
|
||||||
if downsample is not None:
|
|
||||||
self.downsample = downsample(input_resolution, dim=dim, norm_layer=norm_layer)
|
|
||||||
else:
|
|
||||||
self.downsample = None
|
|
||||||
|
|
||||||
def forward(self, x, x_size):
|
|
||||||
for blk in self.blocks:
|
|
||||||
if self.use_checkpoint:
|
|
||||||
x = checkpoint.checkpoint(blk, x, x_size)
|
|
||||||
else:
|
|
||||||
x = blk(x, x_size)
|
|
||||||
if self.downsample is not None:
|
|
||||||
x = self.downsample(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
def extra_repr(self) -> str:
|
|
||||||
return f"dim={self.dim}, input_resolution={self.input_resolution}, depth={self.depth}"
|
|
||||||
|
|
||||||
def flops(self):
|
|
||||||
flops = 0
|
|
||||||
for blk in self.blocks:
|
|
||||||
flops += blk.flops()
|
|
||||||
if self.downsample is not None:
|
|
||||||
flops += self.downsample.flops()
|
|
||||||
return flops
|
|
||||||
|
|
||||||
|
|
||||||
class RSTB(nn.Module):
|
|
||||||
"""Residual Swin Transformer Block (RSTB).
|
|
||||||
|
|
||||||
Args:
|
|
||||||
dim (int): Number of input channels.
|
|
||||||
input_resolution (tuple[int]): Input resolution.
|
|
||||||
depth (int): Number of blocks.
|
|
||||||
num_heads (int): Number of attention heads.
|
|
||||||
window_size (int): Local window size.
|
|
||||||
mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.
|
|
||||||
qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True
|
|
||||||
qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set.
|
|
||||||
drop (float, optional): Dropout rate. Default: 0.0
|
|
||||||
attn_drop (float, optional): Attention dropout rate. Default: 0.0
|
|
||||||
drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0
|
|
||||||
norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm
|
|
||||||
downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None
|
|
||||||
use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False.
|
|
||||||
img_size: Input image size.
|
|
||||||
patch_size: Patch size.
|
|
||||||
resi_connection: The convolutional block before residual connection.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, dim, input_resolution, depth, num_heads, window_size,
|
|
||||||
mlp_ratio=4., qkv_bias=True, qk_scale=None, drop=0., attn_drop=0.,
|
|
||||||
drop_path=0., norm_layer=nn.LayerNorm, downsample=None, use_checkpoint=False,
|
|
||||||
img_size=224, patch_size=4, resi_connection='1conv'):
|
|
||||||
super(RSTB, self).__init__()
|
|
||||||
|
|
||||||
self.dim = dim
|
|
||||||
self.input_resolution = input_resolution
|
|
||||||
|
|
||||||
self.residual_group = BasicLayer(dim=dim,
|
|
||||||
input_resolution=input_resolution,
|
|
||||||
depth=depth,
|
|
||||||
num_heads=num_heads,
|
|
||||||
window_size=window_size,
|
|
||||||
mlp_ratio=mlp_ratio,
|
|
||||||
qkv_bias=qkv_bias, qk_scale=qk_scale,
|
|
||||||
drop=drop, attn_drop=attn_drop,
|
|
||||||
drop_path=drop_path,
|
|
||||||
norm_layer=norm_layer,
|
|
||||||
downsample=downsample,
|
|
||||||
use_checkpoint=use_checkpoint)
|
|
||||||
|
|
||||||
if resi_connection == '1conv':
|
|
||||||
self.conv = nn.Conv2d(dim, dim, 3, 1, 1)
|
|
||||||
elif resi_connection == '3conv':
|
|
||||||
# to save parameters and memory
|
|
||||||
self.conv = nn.Sequential(nn.Conv2d(dim, dim // 4, 3, 1, 1), nn.LeakyReLU(negative_slope=0.2, inplace=True),
|
|
||||||
nn.Conv2d(dim // 4, dim // 4, 1, 1, 0),
|
|
||||||
nn.LeakyReLU(negative_slope=0.2, inplace=True),
|
|
||||||
nn.Conv2d(dim // 4, dim, 3, 1, 1))
|
|
||||||
|
|
||||||
self.patch_embed = PatchEmbed(
|
|
||||||
img_size=img_size, patch_size=patch_size, in_chans=0, embed_dim=dim,
|
|
||||||
norm_layer=None)
|
|
||||||
|
|
||||||
self.patch_unembed = PatchUnEmbed(
|
|
||||||
img_size=img_size, patch_size=patch_size, in_chans=0, embed_dim=dim,
|
|
||||||
norm_layer=None)
|
|
||||||
|
|
||||||
def forward(self, x, x_size):
|
|
||||||
return self.patch_embed(self.conv(self.patch_unembed(self.residual_group(x, x_size), x_size))) + x
|
|
||||||
|
|
||||||
def flops(self):
|
|
||||||
flops = 0
|
|
||||||
flops += self.residual_group.flops()
|
|
||||||
H, W = self.input_resolution
|
|
||||||
flops += H * W * self.dim * self.dim * 9
|
|
||||||
flops += self.patch_embed.flops()
|
|
||||||
flops += self.patch_unembed.flops()
|
|
||||||
|
|
||||||
return flops
|
|
||||||
|
|
||||||
|
|
||||||
class PatchEmbed(nn.Module):
|
|
||||||
r""" Image to Patch Embedding
|
|
||||||
|
|
||||||
Args:
|
|
||||||
img_size (int): Image size. Default: 224.
|
|
||||||
patch_size (int): Patch token size. Default: 4.
|
|
||||||
in_chans (int): Number of input image channels. Default: 3.
|
|
||||||
embed_dim (int): Number of linear projection output channels. Default: 96.
|
|
||||||
norm_layer (nn.Module, optional): Normalization layer. Default: None
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, img_size=224, patch_size=4, in_chans=3, embed_dim=96, norm_layer=None):
|
|
||||||
super().__init__()
|
|
||||||
img_size = to_2tuple(img_size)
|
|
||||||
patch_size = to_2tuple(patch_size)
|
|
||||||
patches_resolution = [img_size[0] // patch_size[0], img_size[1] // patch_size[1]]
|
|
||||||
self.img_size = img_size
|
|
||||||
self.patch_size = patch_size
|
|
||||||
self.patches_resolution = patches_resolution
|
|
||||||
self.num_patches = patches_resolution[0] * patches_resolution[1]
|
|
||||||
|
|
||||||
self.in_chans = in_chans
|
|
||||||
self.embed_dim = embed_dim
|
|
||||||
|
|
||||||
if norm_layer is not None:
|
|
||||||
self.norm = norm_layer(embed_dim)
|
|
||||||
else:
|
|
||||||
self.norm = None
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = x.flatten(2).transpose(1, 2) # B Ph*Pw C
|
|
||||||
if self.norm is not None:
|
|
||||||
x = self.norm(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
def flops(self):
|
|
||||||
flops = 0
|
|
||||||
H, W = self.img_size
|
|
||||||
if self.norm is not None:
|
|
||||||
flops += H * W * self.embed_dim
|
|
||||||
return flops
|
|
||||||
|
|
||||||
|
|
||||||
class PatchUnEmbed(nn.Module):
|
|
||||||
r""" Image to Patch Unembedding
|
|
||||||
|
|
||||||
Args:
|
|
||||||
img_size (int): Image size. Default: 224.
|
|
||||||
patch_size (int): Patch token size. Default: 4.
|
|
||||||
in_chans (int): Number of input image channels. Default: 3.
|
|
||||||
embed_dim (int): Number of linear projection output channels. Default: 96.
|
|
||||||
norm_layer (nn.Module, optional): Normalization layer. Default: None
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, img_size=224, patch_size=4, in_chans=3, embed_dim=96, norm_layer=None):
|
|
||||||
super().__init__()
|
|
||||||
img_size = to_2tuple(img_size)
|
|
||||||
patch_size = to_2tuple(patch_size)
|
|
||||||
patches_resolution = [img_size[0] // patch_size[0], img_size[1] // patch_size[1]]
|
|
||||||
self.img_size = img_size
|
|
||||||
self.patch_size = patch_size
|
|
||||||
self.patches_resolution = patches_resolution
|
|
||||||
self.num_patches = patches_resolution[0] * patches_resolution[1]
|
|
||||||
|
|
||||||
self.in_chans = in_chans
|
|
||||||
self.embed_dim = embed_dim
|
|
||||||
|
|
||||||
def forward(self, x, x_size):
|
|
||||||
B, HW, C = x.shape
|
|
||||||
x = x.transpose(1, 2).view(B, self.embed_dim, x_size[0], x_size[1]) # B Ph*Pw C
|
|
||||||
return x
|
|
||||||
|
|
||||||
def flops(self):
|
|
||||||
flops = 0
|
|
||||||
return flops
|
|
||||||
|
|
||||||
|
|
||||||
class Upsample(nn.Sequential):
|
|
||||||
"""Upsample module.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
scale (int): Scale factor. Supported scales: 2^n and 3.
|
|
||||||
num_feat (int): Channel number of intermediate features.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, scale, num_feat):
|
|
||||||
m = []
|
|
||||||
if (scale & (scale - 1)) == 0: # scale = 2^n
|
|
||||||
for _ in range(int(math.log(scale, 2))):
|
|
||||||
m.append(nn.Conv2d(num_feat, 4 * num_feat, 3, 1, 1))
|
|
||||||
m.append(nn.PixelShuffle(2))
|
|
||||||
elif scale == 3:
|
|
||||||
m.append(nn.Conv2d(num_feat, 9 * num_feat, 3, 1, 1))
|
|
||||||
m.append(nn.PixelShuffle(3))
|
|
||||||
else:
|
|
||||||
raise ValueError(f'scale {scale} is not supported. ' 'Supported scales: 2^n and 3.')
|
|
||||||
super(Upsample, self).__init__(*m)
|
|
||||||
|
|
||||||
|
|
||||||
class UpsampleOneStep(nn.Sequential):
|
|
||||||
"""UpsampleOneStep module (the difference with Upsample is that it always only has 1conv + 1pixelshuffle)
|
|
||||||
Used in lightweight SR to save parameters.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
scale (int): Scale factor. Supported scales: 2^n and 3.
|
|
||||||
num_feat (int): Channel number of intermediate features.
|
|
||||||
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, scale, num_feat, num_out_ch, input_resolution=None):
|
|
||||||
self.num_feat = num_feat
|
|
||||||
self.input_resolution = input_resolution
|
|
||||||
m = []
|
|
||||||
m.append(nn.Conv2d(num_feat, (scale ** 2) * num_out_ch, 3, 1, 1))
|
|
||||||
m.append(nn.PixelShuffle(scale))
|
|
||||||
super(UpsampleOneStep, self).__init__(*m)
|
|
||||||
|
|
||||||
def flops(self):
|
|
||||||
H, W = self.input_resolution
|
|
||||||
flops = H * W * self.num_feat * 3 * 9
|
|
||||||
return flops
|
|
||||||
|
|
||||||
|
|
||||||
class SwinIR(nn.Module):
|
|
||||||
r""" SwinIR
|
|
||||||
A PyTorch impl of : `SwinIR: Image Restoration Using Swin Transformer`, based on Swin Transformer.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
img_size (int | tuple(int)): Input image size. Default 64
|
|
||||||
patch_size (int | tuple(int)): Patch size. Default: 1
|
|
||||||
in_chans (int): Number of input image channels. Default: 3
|
|
||||||
embed_dim (int): Patch embedding dimension. Default: 96
|
|
||||||
depths (tuple(int)): Depth of each Swin Transformer layer.
|
|
||||||
num_heads (tuple(int)): Number of attention heads in different layers.
|
|
||||||
window_size (int): Window size. Default: 7
|
|
||||||
mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4
|
|
||||||
qkv_bias (bool): If True, add a learnable bias to query, key, value. Default: True
|
|
||||||
qk_scale (float): Override default qk scale of head_dim ** -0.5 if set. Default: None
|
|
||||||
drop_rate (float): Dropout rate. Default: 0
|
|
||||||
attn_drop_rate (float): Attention dropout rate. Default: 0
|
|
||||||
drop_path_rate (float): Stochastic depth rate. Default: 0.1
|
|
||||||
norm_layer (nn.Module): Normalization layer. Default: nn.LayerNorm.
|
|
||||||
ape (bool): If True, add absolute position embedding to the patch embedding. Default: False
|
|
||||||
patch_norm (bool): If True, add normalization after patch embedding. Default: True
|
|
||||||
use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False
|
|
||||||
upscale: Upscale factor. 2/3/4/8 for image SR, 1 for denoising and compress artifact reduction
|
|
||||||
img_range: Image range. 1. or 255.
|
|
||||||
upsampler: The reconstruction reconstruction module. 'pixelshuffle'/'pixelshuffledirect'/'nearest+conv'/None
|
|
||||||
resi_connection: The convolutional block before residual connection. '1conv'/'3conv'
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, img_size=64, patch_size=1, in_chans=3,
|
|
||||||
embed_dim=96, depths=(6, 6, 6, 6), num_heads=(6, 6, 6, 6),
|
|
||||||
window_size=7, mlp_ratio=4., qkv_bias=True, qk_scale=None,
|
|
||||||
drop_rate=0., attn_drop_rate=0., drop_path_rate=0.1,
|
|
||||||
norm_layer=nn.LayerNorm, ape=False, patch_norm=True,
|
|
||||||
use_checkpoint=False, upscale=2, img_range=1., upsampler='', resi_connection='1conv',
|
|
||||||
**kwargs):
|
|
||||||
super(SwinIR, self).__init__()
|
|
||||||
num_in_ch = in_chans
|
|
||||||
num_out_ch = in_chans
|
|
||||||
num_feat = 64
|
|
||||||
self.img_range = img_range
|
|
||||||
if in_chans == 3:
|
|
||||||
rgb_mean = (0.4488, 0.4371, 0.4040)
|
|
||||||
self.mean = torch.Tensor(rgb_mean).view(1, 3, 1, 1)
|
|
||||||
else:
|
|
||||||
self.mean = torch.zeros(1, 1, 1, 1)
|
|
||||||
self.upscale = upscale
|
|
||||||
self.upsampler = upsampler
|
|
||||||
self.window_size = window_size
|
|
||||||
|
|
||||||
#####################################################################################################
|
|
||||||
################################### 1, shallow feature extraction ###################################
|
|
||||||
self.conv_first = nn.Conv2d(num_in_ch, embed_dim, 3, 1, 1)
|
|
||||||
|
|
||||||
#####################################################################################################
|
|
||||||
################################### 2, deep feature extraction ######################################
|
|
||||||
self.num_layers = len(depths)
|
|
||||||
self.embed_dim = embed_dim
|
|
||||||
self.ape = ape
|
|
||||||
self.patch_norm = patch_norm
|
|
||||||
self.num_features = embed_dim
|
|
||||||
self.mlp_ratio = mlp_ratio
|
|
||||||
|
|
||||||
# split image into non-overlapping patches
|
|
||||||
self.patch_embed = PatchEmbed(
|
|
||||||
img_size=img_size, patch_size=patch_size, in_chans=embed_dim, embed_dim=embed_dim,
|
|
||||||
norm_layer=norm_layer if self.patch_norm else None)
|
|
||||||
num_patches = self.patch_embed.num_patches
|
|
||||||
patches_resolution = self.patch_embed.patches_resolution
|
|
||||||
self.patches_resolution = patches_resolution
|
|
||||||
|
|
||||||
# merge non-overlapping patches into image
|
|
||||||
self.patch_unembed = PatchUnEmbed(
|
|
||||||
img_size=img_size, patch_size=patch_size, in_chans=embed_dim, embed_dim=embed_dim,
|
|
||||||
norm_layer=norm_layer if self.patch_norm else None)
|
|
||||||
|
|
||||||
# absolute position embedding
|
|
||||||
if self.ape:
|
|
||||||
self.absolute_pos_embed = nn.Parameter(torch.zeros(1, num_patches, embed_dim))
|
|
||||||
trunc_normal_(self.absolute_pos_embed, std=.02)
|
|
||||||
|
|
||||||
self.pos_drop = nn.Dropout(p=drop_rate)
|
|
||||||
|
|
||||||
# stochastic depth
|
|
||||||
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))] # stochastic depth decay rule
|
|
||||||
|
|
||||||
# build Residual Swin Transformer blocks (RSTB)
|
|
||||||
self.layers = nn.ModuleList()
|
|
||||||
for i_layer in range(self.num_layers):
|
|
||||||
layer = RSTB(dim=embed_dim,
|
|
||||||
input_resolution=(patches_resolution[0],
|
|
||||||
patches_resolution[1]),
|
|
||||||
depth=depths[i_layer],
|
|
||||||
num_heads=num_heads[i_layer],
|
|
||||||
window_size=window_size,
|
|
||||||
mlp_ratio=self.mlp_ratio,
|
|
||||||
qkv_bias=qkv_bias, qk_scale=qk_scale,
|
|
||||||
drop=drop_rate, attn_drop=attn_drop_rate,
|
|
||||||
drop_path=dpr[sum(depths[:i_layer]):sum(depths[:i_layer + 1])], # no impact on SR results
|
|
||||||
norm_layer=norm_layer,
|
|
||||||
downsample=None,
|
|
||||||
use_checkpoint=use_checkpoint,
|
|
||||||
img_size=img_size,
|
|
||||||
patch_size=patch_size,
|
|
||||||
resi_connection=resi_connection
|
|
||||||
|
|
||||||
)
|
|
||||||
self.layers.append(layer)
|
|
||||||
self.norm = norm_layer(self.num_features)
|
|
||||||
|
|
||||||
# build the last conv layer in deep feature extraction
|
|
||||||
if resi_connection == '1conv':
|
|
||||||
self.conv_after_body = nn.Conv2d(embed_dim, embed_dim, 3, 1, 1)
|
|
||||||
elif resi_connection == '3conv':
|
|
||||||
# to save parameters and memory
|
|
||||||
self.conv_after_body = nn.Sequential(nn.Conv2d(embed_dim, embed_dim // 4, 3, 1, 1),
|
|
||||||
nn.LeakyReLU(negative_slope=0.2, inplace=True),
|
|
||||||
nn.Conv2d(embed_dim // 4, embed_dim // 4, 1, 1, 0),
|
|
||||||
nn.LeakyReLU(negative_slope=0.2, inplace=True),
|
|
||||||
nn.Conv2d(embed_dim // 4, embed_dim, 3, 1, 1))
|
|
||||||
|
|
||||||
#####################################################################################################
|
|
||||||
################################ 3, high quality image reconstruction ################################
|
|
||||||
if self.upsampler == 'pixelshuffle':
|
|
||||||
# for classical SR
|
|
||||||
self.conv_before_upsample = nn.Sequential(nn.Conv2d(embed_dim, num_feat, 3, 1, 1),
|
|
||||||
nn.LeakyReLU(inplace=True))
|
|
||||||
self.upsample = Upsample(upscale, num_feat)
|
|
||||||
self.conv_last = nn.Conv2d(num_feat, num_out_ch, 3, 1, 1)
|
|
||||||
elif self.upsampler == 'pixelshuffledirect':
|
|
||||||
# for lightweight SR (to save parameters)
|
|
||||||
self.upsample = UpsampleOneStep(upscale, embed_dim, num_out_ch,
|
|
||||||
(patches_resolution[0], patches_resolution[1]))
|
|
||||||
elif self.upsampler == 'nearest+conv':
|
|
||||||
# for real-world SR (less artifacts)
|
|
||||||
self.conv_before_upsample = nn.Sequential(nn.Conv2d(embed_dim, num_feat, 3, 1, 1),
|
|
||||||
nn.LeakyReLU(inplace=True))
|
|
||||||
self.conv_up1 = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
|
|
||||||
if self.upscale == 4:
|
|
||||||
self.conv_up2 = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
|
|
||||||
self.conv_hr = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
|
|
||||||
self.conv_last = nn.Conv2d(num_feat, num_out_ch, 3, 1, 1)
|
|
||||||
self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)
|
|
||||||
else:
|
|
||||||
# for image denoising and JPEG compression artifact reduction
|
|
||||||
self.conv_last = nn.Conv2d(embed_dim, num_out_ch, 3, 1, 1)
|
|
||||||
|
|
||||||
self.apply(self._init_weights)
|
|
||||||
|
|
||||||
def _init_weights(self, m):
|
|
||||||
if isinstance(m, nn.Linear):
|
|
||||||
trunc_normal_(m.weight, std=.02)
|
|
||||||
if isinstance(m, nn.Linear) and m.bias is not None:
|
|
||||||
nn.init.constant_(m.bias, 0)
|
|
||||||
elif isinstance(m, nn.LayerNorm):
|
|
||||||
nn.init.constant_(m.bias, 0)
|
|
||||||
nn.init.constant_(m.weight, 1.0)
|
|
||||||
|
|
||||||
@torch.jit.ignore
|
|
||||||
def no_weight_decay(self):
|
|
||||||
return {'absolute_pos_embed'}
|
|
||||||
|
|
||||||
@torch.jit.ignore
|
|
||||||
def no_weight_decay_keywords(self):
|
|
||||||
return {'relative_position_bias_table'}
|
|
||||||
|
|
||||||
def check_image_size(self, x):
|
|
||||||
_, _, h, w = x.size()
|
|
||||||
mod_pad_h = (self.window_size - h % self.window_size) % self.window_size
|
|
||||||
mod_pad_w = (self.window_size - w % self.window_size) % self.window_size
|
|
||||||
x = F.pad(x, (0, mod_pad_w, 0, mod_pad_h), 'reflect')
|
|
||||||
return x
|
|
||||||
|
|
||||||
def forward_features(self, x):
|
|
||||||
x_size = (x.shape[2], x.shape[3])
|
|
||||||
x = self.patch_embed(x)
|
|
||||||
if self.ape:
|
|
||||||
x = x + self.absolute_pos_embed
|
|
||||||
x = self.pos_drop(x)
|
|
||||||
|
|
||||||
for layer in self.layers:
|
|
||||||
x = layer(x, x_size)
|
|
||||||
|
|
||||||
x = self.norm(x) # B L C
|
|
||||||
x = self.patch_unembed(x, x_size)
|
|
||||||
|
|
||||||
return x
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
H, W = x.shape[2:]
|
|
||||||
x = self.check_image_size(x)
|
|
||||||
|
|
||||||
self.mean = self.mean.type_as(x)
|
|
||||||
x = (x - self.mean) * self.img_range
|
|
||||||
|
|
||||||
if self.upsampler == 'pixelshuffle':
|
|
||||||
# for classical SR
|
|
||||||
x = self.conv_first(x)
|
|
||||||
x = self.conv_after_body(self.forward_features(x)) + x
|
|
||||||
x = self.conv_before_upsample(x)
|
|
||||||
x = self.conv_last(self.upsample(x))
|
|
||||||
elif self.upsampler == 'pixelshuffledirect':
|
|
||||||
# for lightweight SR
|
|
||||||
x = self.conv_first(x)
|
|
||||||
x = self.conv_after_body(self.forward_features(x)) + x
|
|
||||||
x = self.upsample(x)
|
|
||||||
elif self.upsampler == 'nearest+conv':
|
|
||||||
# for real-world SR
|
|
||||||
x = self.conv_first(x)
|
|
||||||
x = self.conv_after_body(self.forward_features(x)) + x
|
|
||||||
x = self.conv_before_upsample(x)
|
|
||||||
x = self.lrelu(self.conv_up1(torch.nn.functional.interpolate(x, scale_factor=2, mode='nearest')))
|
|
||||||
if self.upscale == 4:
|
|
||||||
x = self.lrelu(self.conv_up2(torch.nn.functional.interpolate(x, scale_factor=2, mode='nearest')))
|
|
||||||
x = self.conv_last(self.lrelu(self.conv_hr(x)))
|
|
||||||
else:
|
|
||||||
# for image denoising and JPEG compression artifact reduction
|
|
||||||
x_first = self.conv_first(x)
|
|
||||||
res = self.conv_after_body(self.forward_features(x_first)) + x_first
|
|
||||||
x = x + self.conv_last(res)
|
|
||||||
|
|
||||||
x = x / self.img_range + self.mean
|
|
||||||
|
|
||||||
return x[:, :, :H*self.upscale, :W*self.upscale]
|
|
||||||
|
|
||||||
def flops(self):
|
|
||||||
flops = 0
|
|
||||||
H, W = self.patches_resolution
|
|
||||||
flops += H * W * 3 * self.embed_dim * 9
|
|
||||||
flops += self.patch_embed.flops()
|
|
||||||
for layer in self.layers:
|
|
||||||
flops += layer.flops()
|
|
||||||
flops += H * W * 3 * self.embed_dim * self.embed_dim
|
|
||||||
flops += self.upsample.flops()
|
|
||||||
return flops
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
upscale = 4
|
|
||||||
window_size = 8
|
|
||||||
height = (1024 // upscale // window_size + 1) * window_size
|
|
||||||
width = (720 // upscale // window_size + 1) * window_size
|
|
||||||
model = SwinIR(upscale=2, img_size=(height, width),
|
|
||||||
window_size=window_size, img_range=1., depths=[6, 6, 6, 6],
|
|
||||||
embed_dim=60, num_heads=[6, 6, 6, 6], mlp_ratio=2, upsampler='pixelshuffledirect')
|
|
||||||
print(model)
|
|
||||||
print(height, width, model.flops() / 1e9)
|
|
||||||
|
|
||||||
x = torch.randn((1, 3, height, width))
|
|
||||||
x = model(x)
|
|
||||||
print(x.shape)
|
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -12,8 +12,22 @@ onUiLoaded(async() => {
|
|||||||
"Sketch": elementIDs.sketch
|
"Sketch": elementIDs.sketch
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Helper functions
|
// Helper functions
|
||||||
// Get active tab
|
// Get active tab
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Waits for an element to be present in the DOM.
|
||||||
|
*/
|
||||||
|
const waitForElement = (id) => new Promise(resolve => {
|
||||||
|
const checkForElement = () => {
|
||||||
|
const element = document.querySelector(id);
|
||||||
|
if (element) return resolve(element);
|
||||||
|
setTimeout(checkForElement, 100);
|
||||||
|
};
|
||||||
|
checkForElement();
|
||||||
|
});
|
||||||
|
|
||||||
function getActiveTab(elements, all = false) {
|
function getActiveTab(elements, all = false) {
|
||||||
const tabs = elements.img2imgTabs.querySelectorAll("button");
|
const tabs = elements.img2imgTabs.querySelectorAll("button");
|
||||||
|
|
||||||
@@ -34,7 +48,7 @@ onUiLoaded(async() => {
|
|||||||
|
|
||||||
// Wait until opts loaded
|
// Wait until opts loaded
|
||||||
async function waitForOpts() {
|
async function waitForOpts() {
|
||||||
for (;;) {
|
for (; ;) {
|
||||||
if (window.opts && Object.keys(window.opts).length) {
|
if (window.opts && Object.keys(window.opts).length) {
|
||||||
return window.opts;
|
return window.opts;
|
||||||
}
|
}
|
||||||
@@ -42,6 +56,11 @@ onUiLoaded(async() => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Detect whether the element has a horizontal scroll bar
|
||||||
|
function hasHorizontalScrollbar(element) {
|
||||||
|
return element.scrollWidth > element.clientWidth;
|
||||||
|
}
|
||||||
|
|
||||||
// Function for defining the "Ctrl", "Shift" and "Alt" keys
|
// Function for defining the "Ctrl", "Shift" and "Alt" keys
|
||||||
function isModifierKey(event, key) {
|
function isModifierKey(event, key) {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
@@ -199,14 +218,19 @@ onUiLoaded(async() => {
|
|||||||
canvas_hotkey_fullscreen: "KeyS",
|
canvas_hotkey_fullscreen: "KeyS",
|
||||||
canvas_hotkey_move: "KeyF",
|
canvas_hotkey_move: "KeyF",
|
||||||
canvas_hotkey_overlap: "KeyO",
|
canvas_hotkey_overlap: "KeyO",
|
||||||
|
canvas_hotkey_shrink_brush: "KeyQ",
|
||||||
|
canvas_hotkey_grow_brush: "KeyW",
|
||||||
canvas_disabled_functions: [],
|
canvas_disabled_functions: [],
|
||||||
canvas_show_tooltip: true,
|
canvas_show_tooltip: true,
|
||||||
canvas_blur_prompt: false
|
canvas_auto_expand: true,
|
||||||
|
canvas_blur_prompt: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const functionMap = {
|
const functionMap = {
|
||||||
"Zoom": "canvas_hotkey_zoom",
|
"Zoom": "canvas_hotkey_zoom",
|
||||||
"Adjust brush size": "canvas_hotkey_adjust",
|
"Adjust brush size": "canvas_hotkey_adjust",
|
||||||
|
"Hotkey shrink brush": "canvas_hotkey_shrink_brush",
|
||||||
|
"Hotkey enlarge brush": "canvas_hotkey_grow_brush",
|
||||||
"Moving canvas": "canvas_hotkey_move",
|
"Moving canvas": "canvas_hotkey_move",
|
||||||
"Fullscreen": "canvas_hotkey_fullscreen",
|
"Fullscreen": "canvas_hotkey_fullscreen",
|
||||||
"Reset Zoom": "canvas_hotkey_reset",
|
"Reset Zoom": "canvas_hotkey_reset",
|
||||||
@@ -249,7 +273,7 @@ onUiLoaded(async() => {
|
|||||||
input?.addEventListener("input", () => restoreImgRedMask(elements));
|
input?.addEventListener("input", () => restoreImgRedMask(elements));
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyZoomAndPan(elemId) {
|
function applyZoomAndPan(elemId, isExtension = true) {
|
||||||
const targetElement = gradioApp().querySelector(elemId);
|
const targetElement = gradioApp().querySelector(elemId);
|
||||||
|
|
||||||
if (!targetElement) {
|
if (!targetElement) {
|
||||||
@@ -361,6 +385,12 @@ onUiLoaded(async() => {
|
|||||||
panY: 0
|
panY: 0
|
||||||
};
|
};
|
||||||
|
|
||||||
|
if (isExtension) {
|
||||||
|
targetElement.style.overflow = "hidden";
|
||||||
|
}
|
||||||
|
|
||||||
|
targetElement.isZoomed = false;
|
||||||
|
|
||||||
fixCanvas();
|
fixCanvas();
|
||||||
targetElement.style.transform = `scale(${elemData[elemId].zoomLevel}) translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px)`;
|
targetElement.style.transform = `scale(${elemData[elemId].zoomLevel}) translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px)`;
|
||||||
|
|
||||||
@@ -371,8 +401,27 @@ onUiLoaded(async() => {
|
|||||||
toggleOverlap("off");
|
toggleOverlap("off");
|
||||||
fullScreenMode = false;
|
fullScreenMode = false;
|
||||||
|
|
||||||
|
const closeBtn = targetElement.querySelector("button[aria-label='Remove Image']");
|
||||||
|
if (closeBtn) {
|
||||||
|
closeBtn.addEventListener("click", resetZoom);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (canvas && isExtension) {
|
||||||
|
const parentElement = targetElement.closest('[id^="component-"]');
|
||||||
|
if (
|
||||||
|
canvas &&
|
||||||
|
parseFloat(canvas.style.width) > parentElement.offsetWidth &&
|
||||||
|
parseFloat(targetElement.style.width) > parentElement.offsetWidth
|
||||||
|
) {
|
||||||
|
fitToElement();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
canvas &&
|
canvas &&
|
||||||
|
!isExtension &&
|
||||||
parseFloat(canvas.style.width) > 865 &&
|
parseFloat(canvas.style.width) > 865 &&
|
||||||
parseFloat(targetElement.style.width) > 865
|
parseFloat(targetElement.style.width) > 865
|
||||||
) {
|
) {
|
||||||
@@ -381,9 +430,6 @@ onUiLoaded(async() => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
targetElement.style.width = "";
|
targetElement.style.width = "";
|
||||||
if (canvas) {
|
|
||||||
targetElement.style.height = canvas.style.height;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle the zIndex of the target element between two values, allowing it to overlap or be overlapped by other elements
|
// Toggle the zIndex of the target element between two values, allowing it to overlap or be overlapped by other elements
|
||||||
@@ -439,7 +485,7 @@ onUiLoaded(async() => {
|
|||||||
|
|
||||||
// Update the zoom level and pan position of the target element based on the values of the zoomLevel, panX and panY variables
|
// Update the zoom level and pan position of the target element based on the values of the zoomLevel, panX and panY variables
|
||||||
function updateZoom(newZoomLevel, mouseX, mouseY) {
|
function updateZoom(newZoomLevel, mouseX, mouseY) {
|
||||||
newZoomLevel = Math.max(0.5, Math.min(newZoomLevel, 15));
|
newZoomLevel = Math.max(0.1, Math.min(newZoomLevel, 15));
|
||||||
|
|
||||||
elemData[elemId].panX +=
|
elemData[elemId].panX +=
|
||||||
mouseX - (mouseX * newZoomLevel) / elemData[elemId].zoomLevel;
|
mouseX - (mouseX * newZoomLevel) / elemData[elemId].zoomLevel;
|
||||||
@@ -450,6 +496,10 @@ onUiLoaded(async() => {
|
|||||||
targetElement.style.transform = `translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px) scale(${newZoomLevel})`;
|
targetElement.style.transform = `translate(${elemData[elemId].panX}px, ${elemData[elemId].panY}px) scale(${newZoomLevel})`;
|
||||||
|
|
||||||
toggleOverlap("on");
|
toggleOverlap("on");
|
||||||
|
if (isExtension) {
|
||||||
|
targetElement.style.overflow = "visible";
|
||||||
|
}
|
||||||
|
|
||||||
return newZoomLevel;
|
return newZoomLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -472,10 +522,12 @@ onUiLoaded(async() => {
|
|||||||
fullScreenMode = false;
|
fullScreenMode = false;
|
||||||
elemData[elemId].zoomLevel = updateZoom(
|
elemData[elemId].zoomLevel = updateZoom(
|
||||||
elemData[elemId].zoomLevel +
|
elemData[elemId].zoomLevel +
|
||||||
(operation === "+" ? delta : -delta),
|
(operation === "+" ? delta : -delta),
|
||||||
zoomPosX - targetElement.getBoundingClientRect().left,
|
zoomPosX - targetElement.getBoundingClientRect().left,
|
||||||
zoomPosY - targetElement.getBoundingClientRect().top
|
zoomPosY - targetElement.getBoundingClientRect().top
|
||||||
);
|
);
|
||||||
|
|
||||||
|
targetElement.isZoomed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -489,10 +541,19 @@ onUiLoaded(async() => {
|
|||||||
//Reset Zoom
|
//Reset Zoom
|
||||||
targetElement.style.transform = `translate(${0}px, ${0}px) scale(${1})`;
|
targetElement.style.transform = `translate(${0}px, ${0}px) scale(${1})`;
|
||||||
|
|
||||||
|
let parentElement;
|
||||||
|
|
||||||
|
if (isExtension) {
|
||||||
|
parentElement = targetElement.closest('[id^="component-"]');
|
||||||
|
} else {
|
||||||
|
parentElement = targetElement.parentElement;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Get element and screen dimensions
|
// Get element and screen dimensions
|
||||||
const elementWidth = targetElement.offsetWidth;
|
const elementWidth = targetElement.offsetWidth;
|
||||||
const elementHeight = targetElement.offsetHeight;
|
const elementHeight = targetElement.offsetHeight;
|
||||||
const parentElement = targetElement.parentElement;
|
|
||||||
const screenWidth = parentElement.clientWidth;
|
const screenWidth = parentElement.clientWidth;
|
||||||
const screenHeight = parentElement.clientHeight;
|
const screenHeight = parentElement.clientHeight;
|
||||||
|
|
||||||
@@ -545,8 +606,12 @@ onUiLoaded(async() => {
|
|||||||
|
|
||||||
if (!canvas) return;
|
if (!canvas) return;
|
||||||
|
|
||||||
if (canvas.offsetWidth > 862) {
|
if (canvas.offsetWidth > 862 || isExtension) {
|
||||||
targetElement.style.width = canvas.offsetWidth + "px";
|
targetElement.style.width = (canvas.offsetWidth + 2) + "px";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isExtension) {
|
||||||
|
targetElement.style.overflow = "visible";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fullScreenMode) {
|
if (fullScreenMode) {
|
||||||
@@ -625,7 +690,9 @@ onUiLoaded(async() => {
|
|||||||
const hotkeyActions = {
|
const hotkeyActions = {
|
||||||
[hotkeysConfig.canvas_hotkey_reset]: resetZoom,
|
[hotkeysConfig.canvas_hotkey_reset]: resetZoom,
|
||||||
[hotkeysConfig.canvas_hotkey_overlap]: toggleOverlap,
|
[hotkeysConfig.canvas_hotkey_overlap]: toggleOverlap,
|
||||||
[hotkeysConfig.canvas_hotkey_fullscreen]: fitToScreen
|
[hotkeysConfig.canvas_hotkey_fullscreen]: fitToScreen,
|
||||||
|
[hotkeysConfig.canvas_hotkey_shrink_brush]: () => adjustBrushSize(elemId, 10),
|
||||||
|
[hotkeysConfig.canvas_hotkey_grow_brush]: () => adjustBrushSize(elemId, -10)
|
||||||
};
|
};
|
||||||
|
|
||||||
const action = hotkeyActions[event.code];
|
const action = hotkeyActions[event.code];
|
||||||
@@ -648,8 +715,48 @@ onUiLoaded(async() => {
|
|||||||
mouseY = e.offsetY;
|
mouseY = e.offsetY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Simulation of the function to put a long image into the screen.
|
||||||
|
// We detect if an image has a scroll bar or not, make a fullscreen to reveal the image, then reduce it to fit into the element.
|
||||||
|
// We hide the image and show it to the user when it is ready.
|
||||||
|
|
||||||
|
targetElement.isExpanded = false;
|
||||||
|
function autoExpand() {
|
||||||
|
const canvas = document.querySelector(`${elemId} canvas[key="interface"]`);
|
||||||
|
if (canvas) {
|
||||||
|
if (hasHorizontalScrollbar(targetElement) && targetElement.isExpanded === false) {
|
||||||
|
targetElement.style.visibility = "hidden";
|
||||||
|
setTimeout(() => {
|
||||||
|
fitToScreen();
|
||||||
|
resetZoom();
|
||||||
|
targetElement.style.visibility = "visible";
|
||||||
|
targetElement.isExpanded = true;
|
||||||
|
}, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
targetElement.addEventListener("mousemove", getMousePosition);
|
targetElement.addEventListener("mousemove", getMousePosition);
|
||||||
|
|
||||||
|
//observers
|
||||||
|
// Creating an observer with a callback function to handle DOM changes
|
||||||
|
const observer = new MutationObserver((mutationsList, observer) => {
|
||||||
|
for (let mutation of mutationsList) {
|
||||||
|
// If the style attribute of the canvas has changed, by observation it happens only when the picture changes
|
||||||
|
if (mutation.type === 'attributes' && mutation.attributeName === 'style' &&
|
||||||
|
mutation.target.tagName.toLowerCase() === 'canvas') {
|
||||||
|
targetElement.isExpanded = false;
|
||||||
|
setTimeout(resetZoom, 10);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Apply auto expand if enabled
|
||||||
|
if (hotkeysConfig.canvas_auto_expand) {
|
||||||
|
targetElement.addEventListener("mousemove", autoExpand);
|
||||||
|
// Set up an observer to track attribute changes
|
||||||
|
observer.observe(targetElement, {attributes: true, childList: true, subtree: true});
|
||||||
|
}
|
||||||
|
|
||||||
// Handle events only inside the targetElement
|
// Handle events only inside the targetElement
|
||||||
let isKeyDownHandlerAttached = false;
|
let isKeyDownHandlerAttached = false;
|
||||||
|
|
||||||
@@ -754,6 +861,11 @@ onUiLoaded(async() => {
|
|||||||
if (isMoving && elemId === activeElement) {
|
if (isMoving && elemId === activeElement) {
|
||||||
updatePanPosition(e.movementX, e.movementY);
|
updatePanPosition(e.movementX, e.movementY);
|
||||||
targetElement.style.pointerEvents = "none";
|
targetElement.style.pointerEvents = "none";
|
||||||
|
|
||||||
|
if (isExtension) {
|
||||||
|
targetElement.style.overflow = "visible";
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
targetElement.style.pointerEvents = "auto";
|
targetElement.style.pointerEvents = "auto";
|
||||||
}
|
}
|
||||||
@@ -764,13 +876,93 @@ onUiLoaded(async() => {
|
|||||||
isMoving = false;
|
isMoving = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Checks for extension
|
||||||
|
function checkForOutBox() {
|
||||||
|
const parentElement = targetElement.closest('[id^="component-"]');
|
||||||
|
if (parentElement.offsetWidth < targetElement.offsetWidth && !targetElement.isExpanded) {
|
||||||
|
resetZoom();
|
||||||
|
targetElement.isExpanded = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parentElement.offsetWidth < targetElement.offsetWidth && elemData[elemId].zoomLevel == 1) {
|
||||||
|
resetZoom();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parentElement.offsetWidth < targetElement.offsetWidth && targetElement.offsetWidth * elemData[elemId].zoomLevel > parentElement.offsetWidth && elemData[elemId].zoomLevel < 1 && !targetElement.isZoomed) {
|
||||||
|
resetZoom();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isExtension) {
|
||||||
|
targetElement.addEventListener("mousemove", checkForOutBox);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
window.addEventListener('resize', (e) => {
|
||||||
|
resetZoom();
|
||||||
|
|
||||||
|
if (isExtension) {
|
||||||
|
targetElement.isExpanded = false;
|
||||||
|
targetElement.isZoomed = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
gradioApp().addEventListener("mousemove", handleMoveByKey);
|
gradioApp().addEventListener("mousemove", handleMoveByKey);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
applyZoomAndPan(elementIDs.sketch);
|
applyZoomAndPan(elementIDs.sketch, false);
|
||||||
applyZoomAndPan(elementIDs.inpaint);
|
applyZoomAndPan(elementIDs.inpaint, false);
|
||||||
applyZoomAndPan(elementIDs.inpaintSketch);
|
applyZoomAndPan(elementIDs.inpaintSketch, false);
|
||||||
|
|
||||||
// Make the function global so that other extensions can take advantage of this solution
|
// Make the function global so that other extensions can take advantage of this solution
|
||||||
window.applyZoomAndPan = applyZoomAndPan;
|
const applyZoomAndPanIntegration = async(id, elementIDs) => {
|
||||||
|
const mainEl = document.querySelector(id);
|
||||||
|
if (id.toLocaleLowerCase() === "none") {
|
||||||
|
for (const elementID of elementIDs) {
|
||||||
|
const el = await waitForElement(elementID);
|
||||||
|
if (!el) break;
|
||||||
|
applyZoomAndPan(elementID);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!mainEl) return;
|
||||||
|
mainEl.addEventListener("click", async() => {
|
||||||
|
for (const elementID of elementIDs) {
|
||||||
|
const el = await waitForElement(elementID);
|
||||||
|
if (!el) break;
|
||||||
|
applyZoomAndPan(elementID);
|
||||||
|
}
|
||||||
|
}, {once: true});
|
||||||
|
};
|
||||||
|
|
||||||
|
window.applyZoomAndPan = applyZoomAndPan; // Only 1 elements, argument elementID, for example applyZoomAndPan("#txt2img_controlnet_ControlNet_input_image")
|
||||||
|
|
||||||
|
window.applyZoomAndPanIntegration = applyZoomAndPanIntegration; // for any extension
|
||||||
|
|
||||||
|
/*
|
||||||
|
The function `applyZoomAndPanIntegration` takes two arguments:
|
||||||
|
|
||||||
|
1. `id`: A string identifier for the element to which zoom and pan functionality will be applied on click.
|
||||||
|
If the `id` value is "none", the functionality will be applied to all elements specified in the second argument without a click event.
|
||||||
|
|
||||||
|
2. `elementIDs`: An array of string identifiers for elements. Zoom and pan functionality will be applied to each of these elements on click of the element specified by the first argument.
|
||||||
|
If "none" is specified in the first argument, the functionality will be applied to each of these elements without a click event.
|
||||||
|
|
||||||
|
Example usage:
|
||||||
|
applyZoomAndPanIntegration("#txt2img_controlnet", ["#txt2img_controlnet_ControlNet_input_image"]);
|
||||||
|
In this example, zoom and pan functionality will be applied to the element with the identifier "txt2img_controlnet_ControlNet_input_image" upon clicking the element with the identifier "txt2img_controlnet".
|
||||||
|
*/
|
||||||
|
|
||||||
|
// More examples
|
||||||
|
// Add integration with ControlNet txt2img One TAB
|
||||||
|
// applyZoomAndPanIntegration("#txt2img_controlnet", ["#txt2img_controlnet_ControlNet_input_image"]);
|
||||||
|
|
||||||
|
// Add integration with ControlNet txt2img Tabs
|
||||||
|
// applyZoomAndPanIntegration("#txt2img_controlnet",Array.from({ length: 10 }, (_, i) => `#txt2img_controlnet_ControlNet-${i}_input_image`));
|
||||||
|
|
||||||
|
// Add integration with Inpaint Anything
|
||||||
|
// applyZoomAndPanIntegration("None", ["#ia_sam_image", "#ia_sel_mask"]);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -4,11 +4,14 @@ from modules import shared
|
|||||||
shared.options_templates.update(shared.options_section(('canvas_hotkey', "Canvas Hotkeys"), {
|
shared.options_templates.update(shared.options_section(('canvas_hotkey', "Canvas Hotkeys"), {
|
||||||
"canvas_hotkey_zoom": shared.OptionInfo("Alt", "Zoom canvas", gr.Radio, {"choices": ["Shift","Ctrl", "Alt"]}).info("If you choose 'Shift' you cannot scroll horizontally, 'Alt' can cause a little trouble in firefox"),
|
"canvas_hotkey_zoom": shared.OptionInfo("Alt", "Zoom canvas", gr.Radio, {"choices": ["Shift","Ctrl", "Alt"]}).info("If you choose 'Shift' you cannot scroll horizontally, 'Alt' can cause a little trouble in firefox"),
|
||||||
"canvas_hotkey_adjust": shared.OptionInfo("Ctrl", "Adjust brush size", gr.Radio, {"choices": ["Shift","Ctrl", "Alt"]}).info("If you choose 'Shift' you cannot scroll horizontally, 'Alt' can cause a little trouble in firefox"),
|
"canvas_hotkey_adjust": shared.OptionInfo("Ctrl", "Adjust brush size", gr.Radio, {"choices": ["Shift","Ctrl", "Alt"]}).info("If you choose 'Shift' you cannot scroll horizontally, 'Alt' can cause a little trouble in firefox"),
|
||||||
|
"canvas_hotkey_shrink_brush": shared.OptionInfo("Q", "Shrink the brush size"),
|
||||||
|
"canvas_hotkey_grow_brush": shared.OptionInfo("W", "Enlarge the brush size"),
|
||||||
"canvas_hotkey_move": shared.OptionInfo("F", "Moving the canvas").info("To work correctly in firefox, turn off 'Automatically search the page text when typing' in the browser settings"),
|
"canvas_hotkey_move": shared.OptionInfo("F", "Moving the canvas").info("To work correctly in firefox, turn off 'Automatically search the page text when typing' in the browser settings"),
|
||||||
"canvas_hotkey_fullscreen": shared.OptionInfo("S", "Fullscreen Mode, maximizes the picture so that it fits into the screen and stretches it to its full width "),
|
"canvas_hotkey_fullscreen": shared.OptionInfo("S", "Fullscreen Mode, maximizes the picture so that it fits into the screen and stretches it to its full width "),
|
||||||
"canvas_hotkey_reset": shared.OptionInfo("R", "Reset zoom and canvas positon"),
|
"canvas_hotkey_reset": shared.OptionInfo("R", "Reset zoom and canvas positon"),
|
||||||
"canvas_hotkey_overlap": shared.OptionInfo("O", "Toggle overlap").info("Technical button, neededs for testing"),
|
"canvas_hotkey_overlap": shared.OptionInfo("O", "Toggle overlap").info("Technical button, neededs for testing"),
|
||||||
"canvas_show_tooltip": shared.OptionInfo(True, "Enable tooltip on the canvas"),
|
"canvas_show_tooltip": shared.OptionInfo(True, "Enable tooltip on the canvas"),
|
||||||
|
"canvas_auto_expand": shared.OptionInfo(True, "Automatically expands an image that does not fit completely in the canvas area, similar to manually pressing the S and R buttons"),
|
||||||
"canvas_blur_prompt": shared.OptionInfo(False, "Take the focus off the prompt when working with a canvas"),
|
"canvas_blur_prompt": shared.OptionInfo(False, "Take the focus off the prompt when working with a canvas"),
|
||||||
"canvas_disabled_functions": shared.OptionInfo(["Overlap"], "Disable function that you don't use", gr.CheckboxGroup, {"choices": ["Zoom","Adjust brush size", "Moving canvas","Fullscreen","Reset Zoom","Overlap"]}),
|
"canvas_disabled_functions": shared.OptionInfo(["Overlap"], "Disable function that you don't use", gr.CheckboxGroup, {"choices": ["Zoom","Adjust brush size","Hotkey enlarge brush","Hotkey shrink brush","Moving canvas","Fullscreen","Reset Zoom","Overlap"]}),
|
||||||
}))
|
}))
|
||||||
|
|||||||
@@ -61,3 +61,6 @@
|
|||||||
to {opacity: 1;}
|
to {opacity: 1;}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.styler {
|
||||||
|
overflow:inherit !important;
|
||||||
|
}
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
|
import math
|
||||||
|
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
from modules import scripts, shared, ui_components, ui_settings
|
from modules import scripts, shared, ui_components, ui_settings, infotext_utils
|
||||||
from modules.ui_components import FormColumn
|
from modules.ui_components import FormColumn
|
||||||
|
|
||||||
|
|
||||||
@@ -19,18 +21,39 @@ class ExtraOptionsSection(scripts.Script):
|
|||||||
def ui(self, is_img2img):
|
def ui(self, is_img2img):
|
||||||
self.comps = []
|
self.comps = []
|
||||||
self.setting_names = []
|
self.setting_names = []
|
||||||
|
self.infotext_fields = []
|
||||||
|
extra_options = shared.opts.extra_options_img2img if is_img2img else shared.opts.extra_options_txt2img
|
||||||
|
elem_id_tabname = "extra_options_" + ("img2img" if is_img2img else "txt2img")
|
||||||
|
|
||||||
|
mapping = {k: v for v, k in infotext_utils.infotext_to_setting_name_mapping}
|
||||||
|
|
||||||
with gr.Blocks() as interface:
|
with gr.Blocks() as interface:
|
||||||
with gr.Accordion("Options", open=False) if shared.opts.extra_options_accordion and shared.opts.extra_options else gr.Group(), gr.Row():
|
with gr.Accordion("Options", open=False, elem_id=elem_id_tabname) if shared.opts.extra_options_accordion and extra_options else gr.Group(elem_id=elem_id_tabname):
|
||||||
for setting_name in shared.opts.extra_options:
|
|
||||||
with FormColumn():
|
|
||||||
comp = ui_settings.create_setting_component(setting_name)
|
|
||||||
|
|
||||||
self.comps.append(comp)
|
row_count = math.ceil(len(extra_options) / shared.opts.extra_options_cols)
|
||||||
self.setting_names.append(setting_name)
|
|
||||||
|
for row in range(row_count):
|
||||||
|
with gr.Row():
|
||||||
|
for col in range(shared.opts.extra_options_cols):
|
||||||
|
index = row * shared.opts.extra_options_cols + col
|
||||||
|
if index >= len(extra_options):
|
||||||
|
break
|
||||||
|
|
||||||
|
setting_name = extra_options[index]
|
||||||
|
|
||||||
|
with FormColumn():
|
||||||
|
comp = ui_settings.create_setting_component(setting_name)
|
||||||
|
|
||||||
|
self.comps.append(comp)
|
||||||
|
self.setting_names.append(setting_name)
|
||||||
|
|
||||||
|
setting_infotext_name = mapping.get(setting_name)
|
||||||
|
if setting_infotext_name is not None:
|
||||||
|
self.infotext_fields.append((comp, setting_infotext_name))
|
||||||
|
|
||||||
def get_settings_values():
|
def get_settings_values():
|
||||||
return [ui_settings.get_value_for_setting(key) for key in self.setting_names]
|
res = [ui_settings.get_value_for_setting(key) for key in self.setting_names]
|
||||||
|
return res[0] if len(res) == 1 else res
|
||||||
|
|
||||||
interface.load(fn=get_settings_values, inputs=[], outputs=self.comps, queue=False, show_progress=False)
|
interface.load(fn=get_settings_values, inputs=[], outputs=self.comps, queue=False, show_progress=False)
|
||||||
|
|
||||||
@@ -42,7 +65,14 @@ class ExtraOptionsSection(scripts.Script):
|
|||||||
p.override_settings[name] = value
|
p.override_settings[name] = value
|
||||||
|
|
||||||
|
|
||||||
shared.options_templates.update(shared.options_section(('ui', "User interface"), {
|
shared.options_templates.update(shared.options_section(('settings_in_ui', "Settings in UI", "ui"), {
|
||||||
"extra_options": shared.OptionInfo([], "Options in main UI", ui_components.DropdownMulti, lambda: {"choices": list(shared.opts.data_labels.keys())}).js("info", "settingsHintsShowQuicksettings").info("setting entries that also appear in txt2img/img2img interfaces").needs_restart(),
|
"settings_in_ui": shared.OptionHTML("""
|
||||||
"extra_options_accordion": shared.OptionInfo(False, "Place options in main UI into an accordion")
|
This page allows you to add some settings to the main interface of txt2img and img2img tabs.
|
||||||
|
"""),
|
||||||
|
"extra_options_txt2img": shared.OptionInfo([], "Settings for txt2img", ui_components.DropdownMulti, lambda: {"choices": list(shared.opts.data_labels.keys())}).js("info", "settingsHintsShowQuicksettings").info("setting entries that also appear in txt2img interfaces").needs_reload_ui(),
|
||||||
|
"extra_options_img2img": shared.OptionInfo([], "Settings for img2img", ui_components.DropdownMulti, lambda: {"choices": list(shared.opts.data_labels.keys())}).js("info", "settingsHintsShowQuicksettings").info("setting entries that also appear in img2img interfaces").needs_reload_ui(),
|
||||||
|
"extra_options_cols": shared.OptionInfo(1, "Number of columns for added settings", gr.Slider, {"step": 1, "minimum": 1, "maximum": 20}).info("displayed amount will depend on the actual browser window width").needs_reload_ui(),
|
||||||
|
"extra_options_accordion": shared.OptionInfo(False, "Place added settings into an accordion").needs_reload_ui()
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,351 @@
|
|||||||
|
"""
|
||||||
|
Hypertile module for splitting attention layers in SD-1.5 U-Net and SD-1.5 VAE
|
||||||
|
Warn: The patch works well only if the input image has a width and height that are multiples of 128
|
||||||
|
Original author: @tfernd Github: https://github.com/tfernd/HyperTile
|
||||||
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
from functools import wraps, cache
|
||||||
|
|
||||||
|
import math
|
||||||
|
import torch.nn as nn
|
||||||
|
import random
|
||||||
|
|
||||||
|
from einops import rearrange
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class HypertileParams:
|
||||||
|
depth = 0
|
||||||
|
layer_name = ""
|
||||||
|
tile_size: int = 0
|
||||||
|
swap_size: int = 0
|
||||||
|
aspect_ratio: float = 1.0
|
||||||
|
forward = None
|
||||||
|
enabled = False
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# TODO add SD-XL layers
|
||||||
|
DEPTH_LAYERS = {
|
||||||
|
0: [
|
||||||
|
# SD 1.5 U-Net (diffusers)
|
||||||
|
"down_blocks.0.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
"down_blocks.0.attentions.1.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.3.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.3.attentions.1.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.3.attentions.2.transformer_blocks.0.attn1",
|
||||||
|
# SD 1.5 U-Net (ldm)
|
||||||
|
"input_blocks.1.1.transformer_blocks.0.attn1",
|
||||||
|
"input_blocks.2.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.9.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.10.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.11.1.transformer_blocks.0.attn1",
|
||||||
|
# SD 1.5 VAE
|
||||||
|
"decoder.mid_block.attentions.0",
|
||||||
|
"decoder.mid.attn_1",
|
||||||
|
],
|
||||||
|
1: [
|
||||||
|
# SD 1.5 U-Net (diffusers)
|
||||||
|
"down_blocks.1.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
"down_blocks.1.attentions.1.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.2.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.2.attentions.1.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.2.attentions.2.transformer_blocks.0.attn1",
|
||||||
|
# SD 1.5 U-Net (ldm)
|
||||||
|
"input_blocks.4.1.transformer_blocks.0.attn1",
|
||||||
|
"input_blocks.5.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.6.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.7.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.8.1.transformer_blocks.0.attn1",
|
||||||
|
],
|
||||||
|
2: [
|
||||||
|
# SD 1.5 U-Net (diffusers)
|
||||||
|
"down_blocks.2.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
"down_blocks.2.attentions.1.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.1.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.1.attentions.1.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.1.attentions.2.transformer_blocks.0.attn1",
|
||||||
|
# SD 1.5 U-Net (ldm)
|
||||||
|
"input_blocks.7.1.transformer_blocks.0.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.3.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.4.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.5.1.transformer_blocks.0.attn1",
|
||||||
|
],
|
||||||
|
3: [
|
||||||
|
# SD 1.5 U-Net (diffusers)
|
||||||
|
"mid_block.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
# SD 1.5 U-Net (ldm)
|
||||||
|
"middle_block.1.transformer_blocks.0.attn1",
|
||||||
|
],
|
||||||
|
}
|
||||||
|
# XL layers, thanks for GitHub@gel-crabs for the help
|
||||||
|
DEPTH_LAYERS_XL = {
|
||||||
|
0: [
|
||||||
|
# SD 1.5 U-Net (diffusers)
|
||||||
|
"down_blocks.0.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
"down_blocks.0.attentions.1.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.3.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.3.attentions.1.transformer_blocks.0.attn1",
|
||||||
|
"up_blocks.3.attentions.2.transformer_blocks.0.attn1",
|
||||||
|
# SD 1.5 U-Net (ldm)
|
||||||
|
"input_blocks.4.1.transformer_blocks.0.attn1",
|
||||||
|
"input_blocks.5.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.3.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.4.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.5.1.transformer_blocks.0.attn1",
|
||||||
|
# SD 1.5 VAE
|
||||||
|
"decoder.mid_block.attentions.0",
|
||||||
|
"decoder.mid.attn_1",
|
||||||
|
],
|
||||||
|
1: [
|
||||||
|
# SD 1.5 U-Net (diffusers)
|
||||||
|
#"down_blocks.1.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
#"down_blocks.1.attentions.1.transformer_blocks.0.attn1",
|
||||||
|
#"up_blocks.2.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
#"up_blocks.2.attentions.1.transformer_blocks.0.attn1",
|
||||||
|
#"up_blocks.2.attentions.2.transformer_blocks.0.attn1",
|
||||||
|
# SD 1.5 U-Net (ldm)
|
||||||
|
"input_blocks.4.1.transformer_blocks.1.attn1",
|
||||||
|
"input_blocks.5.1.transformer_blocks.1.attn1",
|
||||||
|
"output_blocks.3.1.transformer_blocks.1.attn1",
|
||||||
|
"output_blocks.4.1.transformer_blocks.1.attn1",
|
||||||
|
"output_blocks.5.1.transformer_blocks.1.attn1",
|
||||||
|
"input_blocks.7.1.transformer_blocks.0.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.0.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.1.1.transformer_blocks.0.attn1",
|
||||||
|
"output_blocks.2.1.transformer_blocks.0.attn1",
|
||||||
|
"input_blocks.7.1.transformer_blocks.1.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.1.attn1",
|
||||||
|
"output_blocks.0.1.transformer_blocks.1.attn1",
|
||||||
|
"output_blocks.1.1.transformer_blocks.1.attn1",
|
||||||
|
"output_blocks.2.1.transformer_blocks.1.attn1",
|
||||||
|
"input_blocks.7.1.transformer_blocks.2.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.2.attn1",
|
||||||
|
"output_blocks.0.1.transformer_blocks.2.attn1",
|
||||||
|
"output_blocks.1.1.transformer_blocks.2.attn1",
|
||||||
|
"output_blocks.2.1.transformer_blocks.2.attn1",
|
||||||
|
"input_blocks.7.1.transformer_blocks.3.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.3.attn1",
|
||||||
|
"output_blocks.0.1.transformer_blocks.3.attn1",
|
||||||
|
"output_blocks.1.1.transformer_blocks.3.attn1",
|
||||||
|
"output_blocks.2.1.transformer_blocks.3.attn1",
|
||||||
|
"input_blocks.7.1.transformer_blocks.4.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.4.attn1",
|
||||||
|
"output_blocks.0.1.transformer_blocks.4.attn1",
|
||||||
|
"output_blocks.1.1.transformer_blocks.4.attn1",
|
||||||
|
"output_blocks.2.1.transformer_blocks.4.attn1",
|
||||||
|
"input_blocks.7.1.transformer_blocks.5.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.5.attn1",
|
||||||
|
"output_blocks.0.1.transformer_blocks.5.attn1",
|
||||||
|
"output_blocks.1.1.transformer_blocks.5.attn1",
|
||||||
|
"output_blocks.2.1.transformer_blocks.5.attn1",
|
||||||
|
"input_blocks.7.1.transformer_blocks.6.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.6.attn1",
|
||||||
|
"output_blocks.0.1.transformer_blocks.6.attn1",
|
||||||
|
"output_blocks.1.1.transformer_blocks.6.attn1",
|
||||||
|
"output_blocks.2.1.transformer_blocks.6.attn1",
|
||||||
|
"input_blocks.7.1.transformer_blocks.7.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.7.attn1",
|
||||||
|
"output_blocks.0.1.transformer_blocks.7.attn1",
|
||||||
|
"output_blocks.1.1.transformer_blocks.7.attn1",
|
||||||
|
"output_blocks.2.1.transformer_blocks.7.attn1",
|
||||||
|
"input_blocks.7.1.transformer_blocks.8.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.8.attn1",
|
||||||
|
"output_blocks.0.1.transformer_blocks.8.attn1",
|
||||||
|
"output_blocks.1.1.transformer_blocks.8.attn1",
|
||||||
|
"output_blocks.2.1.transformer_blocks.8.attn1",
|
||||||
|
"input_blocks.7.1.transformer_blocks.9.attn1",
|
||||||
|
"input_blocks.8.1.transformer_blocks.9.attn1",
|
||||||
|
"output_blocks.0.1.transformer_blocks.9.attn1",
|
||||||
|
"output_blocks.1.1.transformer_blocks.9.attn1",
|
||||||
|
"output_blocks.2.1.transformer_blocks.9.attn1",
|
||||||
|
],
|
||||||
|
2: [
|
||||||
|
# SD 1.5 U-Net (diffusers)
|
||||||
|
"mid_block.attentions.0.transformer_blocks.0.attn1",
|
||||||
|
# SD 1.5 U-Net (ldm)
|
||||||
|
"middle_block.1.transformer_blocks.0.attn1",
|
||||||
|
"middle_block.1.transformer_blocks.1.attn1",
|
||||||
|
"middle_block.1.transformer_blocks.2.attn1",
|
||||||
|
"middle_block.1.transformer_blocks.3.attn1",
|
||||||
|
"middle_block.1.transformer_blocks.4.attn1",
|
||||||
|
"middle_block.1.transformer_blocks.5.attn1",
|
||||||
|
"middle_block.1.transformer_blocks.6.attn1",
|
||||||
|
"middle_block.1.transformer_blocks.7.attn1",
|
||||||
|
"middle_block.1.transformer_blocks.8.attn1",
|
||||||
|
"middle_block.1.transformer_blocks.9.attn1",
|
||||||
|
],
|
||||||
|
3 : [] # TODO - separate layers for SD-XL
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
RNG_INSTANCE = random.Random()
|
||||||
|
|
||||||
|
@cache
|
||||||
|
def get_divisors(value: int, min_value: int, /, max_options: int = 1) -> list[int]:
|
||||||
|
"""
|
||||||
|
Returns divisors of value that
|
||||||
|
x * min_value <= value
|
||||||
|
in big -> small order, amount of divisors is limited by max_options
|
||||||
|
"""
|
||||||
|
max_options = max(1, max_options) # at least 1 option should be returned
|
||||||
|
min_value = min(min_value, value)
|
||||||
|
divisors = [i for i in range(min_value, value + 1) if value % i == 0] # divisors in small -> big order
|
||||||
|
ns = [value // i for i in divisors[:max_options]] # has at least 1 element # big -> small order
|
||||||
|
return ns
|
||||||
|
|
||||||
|
|
||||||
|
def random_divisor(value: int, min_value: int, /, max_options: int = 1) -> int:
|
||||||
|
"""
|
||||||
|
Returns a random divisor of value that
|
||||||
|
x * min_value <= value
|
||||||
|
if max_options is 1, the behavior is deterministic
|
||||||
|
"""
|
||||||
|
ns = get_divisors(value, min_value, max_options=max_options) # get cached divisors
|
||||||
|
idx = RNG_INSTANCE.randint(0, len(ns) - 1)
|
||||||
|
|
||||||
|
return ns[idx]
|
||||||
|
|
||||||
|
|
||||||
|
def set_hypertile_seed(seed: int) -> None:
|
||||||
|
RNG_INSTANCE.seed(seed)
|
||||||
|
|
||||||
|
|
||||||
|
@cache
|
||||||
|
def largest_tile_size_available(width: int, height: int) -> int:
|
||||||
|
"""
|
||||||
|
Calculates the largest tile size available for a given width and height
|
||||||
|
Tile size is always a power of 2
|
||||||
|
"""
|
||||||
|
gcd = math.gcd(width, height)
|
||||||
|
largest_tile_size_available = 1
|
||||||
|
while gcd % (largest_tile_size_available * 2) == 0:
|
||||||
|
largest_tile_size_available *= 2
|
||||||
|
return largest_tile_size_available
|
||||||
|
|
||||||
|
|
||||||
|
def iterative_closest_divisors(hw:int, aspect_ratio:float) -> tuple[int, int]:
|
||||||
|
"""
|
||||||
|
Finds h and w such that h*w = hw and h/w = aspect_ratio
|
||||||
|
We check all possible divisors of hw and return the closest to the aspect ratio
|
||||||
|
"""
|
||||||
|
divisors = [i for i in range(2, hw + 1) if hw % i == 0] # all divisors of hw
|
||||||
|
pairs = [(i, hw // i) for i in divisors] # all pairs of divisors of hw
|
||||||
|
ratios = [w/h for h, w in pairs] # all ratios of pairs of divisors of hw
|
||||||
|
closest_ratio = min(ratios, key=lambda x: abs(x - aspect_ratio)) # closest ratio to aspect_ratio
|
||||||
|
closest_pair = pairs[ratios.index(closest_ratio)] # closest pair of divisors to aspect_ratio
|
||||||
|
return closest_pair
|
||||||
|
|
||||||
|
|
||||||
|
@cache
|
||||||
|
def find_hw_candidates(hw:int, aspect_ratio:float) -> tuple[int, int]:
|
||||||
|
"""
|
||||||
|
Finds h and w such that h*w = hw and h/w = aspect_ratio
|
||||||
|
"""
|
||||||
|
h, w = round(math.sqrt(hw * aspect_ratio)), round(math.sqrt(hw / aspect_ratio))
|
||||||
|
# find h and w such that h*w = hw and h/w = aspect_ratio
|
||||||
|
if h * w != hw:
|
||||||
|
w_candidate = hw / h
|
||||||
|
# check if w is an integer
|
||||||
|
if not w_candidate.is_integer():
|
||||||
|
h_candidate = hw / w
|
||||||
|
# check if h is an integer
|
||||||
|
if not h_candidate.is_integer():
|
||||||
|
return iterative_closest_divisors(hw, aspect_ratio)
|
||||||
|
else:
|
||||||
|
h = int(h_candidate)
|
||||||
|
else:
|
||||||
|
w = int(w_candidate)
|
||||||
|
return h, w
|
||||||
|
|
||||||
|
|
||||||
|
def self_attn_forward(params: HypertileParams, scale_depth=True) -> Callable:
|
||||||
|
|
||||||
|
@wraps(params.forward)
|
||||||
|
def wrapper(*args, **kwargs):
|
||||||
|
if not params.enabled:
|
||||||
|
return params.forward(*args, **kwargs)
|
||||||
|
|
||||||
|
latent_tile_size = max(128, params.tile_size) // 8
|
||||||
|
x = args[0]
|
||||||
|
|
||||||
|
# VAE
|
||||||
|
if x.ndim == 4:
|
||||||
|
b, c, h, w = x.shape
|
||||||
|
|
||||||
|
nh = random_divisor(h, latent_tile_size, params.swap_size)
|
||||||
|
nw = random_divisor(w, latent_tile_size, params.swap_size)
|
||||||
|
|
||||||
|
if nh * nw > 1:
|
||||||
|
x = rearrange(x, "b c (nh h) (nw w) -> (b nh nw) c h w", nh=nh, nw=nw) # split into nh * nw tiles
|
||||||
|
|
||||||
|
out = params.forward(x, *args[1:], **kwargs)
|
||||||
|
|
||||||
|
if nh * nw > 1:
|
||||||
|
out = rearrange(out, "(b nh nw) c h w -> b c (nh h) (nw w)", nh=nh, nw=nw)
|
||||||
|
|
||||||
|
# U-Net
|
||||||
|
else:
|
||||||
|
hw: int = x.size(1)
|
||||||
|
h, w = find_hw_candidates(hw, params.aspect_ratio)
|
||||||
|
assert h * w == hw, f"Invalid aspect ratio {params.aspect_ratio} for input of shape {x.shape}, hw={hw}, h={h}, w={w}"
|
||||||
|
|
||||||
|
factor = 2 ** params.depth if scale_depth else 1
|
||||||
|
nh = random_divisor(h, latent_tile_size * factor, params.swap_size)
|
||||||
|
nw = random_divisor(w, latent_tile_size * factor, params.swap_size)
|
||||||
|
|
||||||
|
if nh * nw > 1:
|
||||||
|
x = rearrange(x, "b (nh h nw w) c -> (b nh nw) (h w) c", h=h // nh, w=w // nw, nh=nh, nw=nw)
|
||||||
|
|
||||||
|
out = params.forward(x, *args[1:], **kwargs)
|
||||||
|
|
||||||
|
if nh * nw > 1:
|
||||||
|
out = rearrange(out, "(b nh nw) hw c -> b nh nw hw c", nh=nh, nw=nw)
|
||||||
|
out = rearrange(out, "b nh nw (h w) c -> b (nh h nw w) c", h=h // nh, w=w // nw)
|
||||||
|
|
||||||
|
return out
|
||||||
|
|
||||||
|
return wrapper
|
||||||
|
|
||||||
|
|
||||||
|
def hypertile_hook_model(model: nn.Module, width, height, *, enable=False, tile_size_max=128, swap_size=1, max_depth=3, is_sdxl=False):
|
||||||
|
hypertile_layers = getattr(model, "__webui_hypertile_layers", None)
|
||||||
|
if hypertile_layers is None:
|
||||||
|
if not enable:
|
||||||
|
return
|
||||||
|
|
||||||
|
hypertile_layers = {}
|
||||||
|
layers = DEPTH_LAYERS_XL if is_sdxl else DEPTH_LAYERS
|
||||||
|
|
||||||
|
for depth in range(4):
|
||||||
|
for layer_name, module in model.named_modules():
|
||||||
|
if any(layer_name.endswith(try_name) for try_name in layers[depth]):
|
||||||
|
params = HypertileParams()
|
||||||
|
module.__webui_hypertile_params = params
|
||||||
|
params.forward = module.forward
|
||||||
|
params.depth = depth
|
||||||
|
params.layer_name = layer_name
|
||||||
|
module.forward = self_attn_forward(params)
|
||||||
|
|
||||||
|
hypertile_layers[layer_name] = 1
|
||||||
|
|
||||||
|
model.__webui_hypertile_layers = hypertile_layers
|
||||||
|
|
||||||
|
aspect_ratio = width / height
|
||||||
|
tile_size = min(largest_tile_size_available(width, height), tile_size_max)
|
||||||
|
|
||||||
|
for layer_name, module in model.named_modules():
|
||||||
|
if layer_name in hypertile_layers:
|
||||||
|
params = module.__webui_hypertile_params
|
||||||
|
|
||||||
|
params.tile_size = tile_size
|
||||||
|
params.swap_size = swap_size
|
||||||
|
params.aspect_ratio = aspect_ratio
|
||||||
|
params.enabled = enable and params.depth <= max_depth
|
||||||
@@ -0,0 +1,109 @@
|
|||||||
|
import hypertile
|
||||||
|
from modules import scripts, script_callbacks, shared
|
||||||
|
from scripts.hypertile_xyz import add_axis_options
|
||||||
|
|
||||||
|
|
||||||
|
class ScriptHypertile(scripts.Script):
|
||||||
|
name = "Hypertile"
|
||||||
|
|
||||||
|
def title(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
def show(self, is_img2img):
|
||||||
|
return scripts.AlwaysVisible
|
||||||
|
|
||||||
|
def process(self, p, *args):
|
||||||
|
hypertile.set_hypertile_seed(p.all_seeds[0])
|
||||||
|
|
||||||
|
configure_hypertile(p.width, p.height, enable_unet=shared.opts.hypertile_enable_unet)
|
||||||
|
|
||||||
|
self.add_infotext(p)
|
||||||
|
|
||||||
|
def before_hr(self, p, *args):
|
||||||
|
|
||||||
|
enable = shared.opts.hypertile_enable_unet_secondpass or shared.opts.hypertile_enable_unet
|
||||||
|
|
||||||
|
# exclusive hypertile seed for the second pass
|
||||||
|
if enable:
|
||||||
|
hypertile.set_hypertile_seed(p.all_seeds[0])
|
||||||
|
|
||||||
|
configure_hypertile(p.hr_upscale_to_x, p.hr_upscale_to_y, enable_unet=enable)
|
||||||
|
|
||||||
|
if enable and not shared.opts.hypertile_enable_unet:
|
||||||
|
p.extra_generation_params["Hypertile U-Net second pass"] = True
|
||||||
|
|
||||||
|
self.add_infotext(p, add_unet_params=True)
|
||||||
|
|
||||||
|
def add_infotext(self, p, add_unet_params=False):
|
||||||
|
def option(name):
|
||||||
|
value = getattr(shared.opts, name)
|
||||||
|
default_value = shared.opts.get_default(name)
|
||||||
|
return None if value == default_value else value
|
||||||
|
|
||||||
|
if shared.opts.hypertile_enable_unet:
|
||||||
|
p.extra_generation_params["Hypertile U-Net"] = True
|
||||||
|
|
||||||
|
if shared.opts.hypertile_enable_unet or add_unet_params:
|
||||||
|
p.extra_generation_params["Hypertile U-Net max depth"] = option('hypertile_max_depth_unet')
|
||||||
|
p.extra_generation_params["Hypertile U-Net max tile size"] = option('hypertile_max_tile_unet')
|
||||||
|
p.extra_generation_params["Hypertile U-Net swap size"] = option('hypertile_swap_size_unet')
|
||||||
|
|
||||||
|
if shared.opts.hypertile_enable_vae:
|
||||||
|
p.extra_generation_params["Hypertile VAE"] = True
|
||||||
|
p.extra_generation_params["Hypertile VAE max depth"] = option('hypertile_max_depth_vae')
|
||||||
|
p.extra_generation_params["Hypertile VAE max tile size"] = option('hypertile_max_tile_vae')
|
||||||
|
p.extra_generation_params["Hypertile VAE swap size"] = option('hypertile_swap_size_vae')
|
||||||
|
|
||||||
|
|
||||||
|
def configure_hypertile(width, height, enable_unet=True):
|
||||||
|
hypertile.hypertile_hook_model(
|
||||||
|
shared.sd_model.first_stage_model,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
swap_size=shared.opts.hypertile_swap_size_vae,
|
||||||
|
max_depth=shared.opts.hypertile_max_depth_vae,
|
||||||
|
tile_size_max=shared.opts.hypertile_max_tile_vae,
|
||||||
|
enable=shared.opts.hypertile_enable_vae,
|
||||||
|
)
|
||||||
|
|
||||||
|
hypertile.hypertile_hook_model(
|
||||||
|
shared.sd_model.model,
|
||||||
|
width,
|
||||||
|
height,
|
||||||
|
swap_size=shared.opts.hypertile_swap_size_unet,
|
||||||
|
max_depth=shared.opts.hypertile_max_depth_unet,
|
||||||
|
tile_size_max=shared.opts.hypertile_max_tile_unet,
|
||||||
|
enable=enable_unet,
|
||||||
|
is_sdxl=shared.sd_model.is_sdxl
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def on_ui_settings():
|
||||||
|
import gradio as gr
|
||||||
|
|
||||||
|
options = {
|
||||||
|
"hypertile_explanation": shared.OptionHTML("""
|
||||||
|
<a href='https://github.com/tfernd/HyperTile'>Hypertile</a> optimizes the self-attention layer within U-Net and VAE models,
|
||||||
|
resulting in a reduction in computation time ranging from 1 to 4 times. The larger the generated image is, the greater the
|
||||||
|
benefit.
|
||||||
|
"""),
|
||||||
|
|
||||||
|
"hypertile_enable_unet": shared.OptionInfo(False, "Enable Hypertile U-Net", infotext="Hypertile U-Net").info("enables hypertile for all modes, including hires fix second pass; noticeable change in details of the generated picture"),
|
||||||
|
"hypertile_enable_unet_secondpass": shared.OptionInfo(False, "Enable Hypertile U-Net for hires fix second pass", infotext="Hypertile U-Net second pass").info("enables hypertile just for hires fix second pass - regardless of whether the above setting is enabled"),
|
||||||
|
"hypertile_max_depth_unet": shared.OptionInfo(3, "Hypertile U-Net max depth", gr.Slider, {"minimum": 0, "maximum": 3, "step": 1}, infotext="Hypertile U-Net max depth").info("larger = more neural network layers affected; minor effect on performance"),
|
||||||
|
"hypertile_max_tile_unet": shared.OptionInfo(256, "Hypertile U-Net max tile size", gr.Slider, {"minimum": 0, "maximum": 512, "step": 16}, infotext="Hypertile U-Net max tile size").info("larger = worse performance"),
|
||||||
|
"hypertile_swap_size_unet": shared.OptionInfo(3, "Hypertile U-Net swap size", gr.Slider, {"minimum": 0, "maximum": 64, "step": 1}, infotext="Hypertile U-Net swap size"),
|
||||||
|
|
||||||
|
"hypertile_enable_vae": shared.OptionInfo(False, "Enable Hypertile VAE", infotext="Hypertile VAE").info("minimal change in the generated picture"),
|
||||||
|
"hypertile_max_depth_vae": shared.OptionInfo(3, "Hypertile VAE max depth", gr.Slider, {"minimum": 0, "maximum": 3, "step": 1}, infotext="Hypertile VAE max depth"),
|
||||||
|
"hypertile_max_tile_vae": shared.OptionInfo(128, "Hypertile VAE max tile size", gr.Slider, {"minimum": 0, "maximum": 512, "step": 16}, infotext="Hypertile VAE max tile size"),
|
||||||
|
"hypertile_swap_size_vae": shared.OptionInfo(3, "Hypertile VAE swap size ", gr.Slider, {"minimum": 0, "maximum": 64, "step": 1}, infotext="Hypertile VAE swap size"),
|
||||||
|
}
|
||||||
|
|
||||||
|
for name, opt in options.items():
|
||||||
|
opt.section = ('hypertile', "Hypertile")
|
||||||
|
shared.opts.add_option(name, opt)
|
||||||
|
|
||||||
|
|
||||||
|
script_callbacks.on_ui_settings(on_ui_settings)
|
||||||
|
script_callbacks.on_before_ui(add_axis_options)
|
||||||
@@ -0,0 +1,51 @@
|
|||||||
|
from modules import scripts
|
||||||
|
from modules.shared import opts
|
||||||
|
|
||||||
|
xyz_grid = [x for x in scripts.scripts_data if x.script_class.__module__ == "xyz_grid.py"][0].module
|
||||||
|
|
||||||
|
def int_applier(value_name:str, min_range:int = -1, max_range:int = -1):
|
||||||
|
"""
|
||||||
|
Returns a function that applies the given value to the given value_name in opts.data.
|
||||||
|
"""
|
||||||
|
def validate(value_name:str, value:str):
|
||||||
|
value = int(value)
|
||||||
|
# validate value
|
||||||
|
if not min_range == -1:
|
||||||
|
assert value >= min_range, f"Value {value} for {value_name} must be greater than or equal to {min_range}"
|
||||||
|
if not max_range == -1:
|
||||||
|
assert value <= max_range, f"Value {value} for {value_name} must be less than or equal to {max_range}"
|
||||||
|
def apply_int(p, x, xs):
|
||||||
|
validate(value_name, x)
|
||||||
|
opts.data[value_name] = int(x)
|
||||||
|
return apply_int
|
||||||
|
|
||||||
|
def bool_applier(value_name:str):
|
||||||
|
"""
|
||||||
|
Returns a function that applies the given value to the given value_name in opts.data.
|
||||||
|
"""
|
||||||
|
def validate(value_name:str, value:str):
|
||||||
|
assert value.lower() in ["true", "false"], f"Value {value} for {value_name} must be either true or false"
|
||||||
|
def apply_bool(p, x, xs):
|
||||||
|
validate(value_name, x)
|
||||||
|
value_boolean = x.lower() == "true"
|
||||||
|
opts.data[value_name] = value_boolean
|
||||||
|
return apply_bool
|
||||||
|
|
||||||
|
def add_axis_options():
|
||||||
|
extra_axis_options = [
|
||||||
|
xyz_grid.AxisOption("[Hypertile] Unet First pass Enabled", str, bool_applier("hypertile_enable_unet"), choices=xyz_grid.boolean_choice(reverse=True)),
|
||||||
|
xyz_grid.AxisOption("[Hypertile] Unet Second pass Enabled", str, bool_applier("hypertile_enable_unet_secondpass"), choices=xyz_grid.boolean_choice(reverse=True)),
|
||||||
|
xyz_grid.AxisOption("[Hypertile] Unet Max Depth", int, int_applier("hypertile_max_depth_unet", 0, 3), choices=lambda: [str(x) for x in range(4)]),
|
||||||
|
xyz_grid.AxisOption("[Hypertile] Unet Max Tile Size", int, int_applier("hypertile_max_tile_unet", 0, 512)),
|
||||||
|
xyz_grid.AxisOption("[Hypertile] Unet Swap Size", int, int_applier("hypertile_swap_size_unet", 0, 64)),
|
||||||
|
xyz_grid.AxisOption("[Hypertile] VAE Enabled", str, bool_applier("hypertile_enable_vae"), choices=xyz_grid.boolean_choice(reverse=True)),
|
||||||
|
xyz_grid.AxisOption("[Hypertile] VAE Max Depth", int, int_applier("hypertile_max_depth_vae", 0, 3), choices=lambda: [str(x) for x in range(4)]),
|
||||||
|
xyz_grid.AxisOption("[Hypertile] VAE Max Tile Size", int, int_applier("hypertile_max_tile_vae", 0, 512)),
|
||||||
|
xyz_grid.AxisOption("[Hypertile] VAE Swap Size", int, int_applier("hypertile_swap_size_vae", 0, 64)),
|
||||||
|
]
|
||||||
|
set_a = {opt.label for opt in xyz_grid.axis_options}
|
||||||
|
set_b = {opt.label for opt in extra_axis_options}
|
||||||
|
if set_a.intersection(set_b):
|
||||||
|
return
|
||||||
|
|
||||||
|
xyz_grid.axis_options.extend(extra_axis_options)
|
||||||
@@ -12,6 +12,8 @@ function isMobile() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function reportWindowSize() {
|
function reportWindowSize() {
|
||||||
|
if (gradioApp().querySelector('.toprow-compact-tools')) return; // not applicable for compact prompt layout
|
||||||
|
|
||||||
var currentlyMobile = isMobile();
|
var currentlyMobile = isMobile();
|
||||||
if (currentlyMobile == isSetupForMobile) return;
|
if (currentlyMobile == isSetupForMobile) return;
|
||||||
isSetupForMobile = currentlyMobile;
|
isSetupForMobile = currentlyMobile;
|
||||||
@@ -20,7 +22,13 @@ function reportWindowSize() {
|
|||||||
var button = gradioApp().getElementById(tab + '_generate_box');
|
var button = gradioApp().getElementById(tab + '_generate_box');
|
||||||
var target = gradioApp().getElementById(currentlyMobile ? tab + '_results' : tab + '_actions_column');
|
var target = gradioApp().getElementById(currentlyMobile ? tab + '_results' : tab + '_actions_column');
|
||||||
target.insertBefore(button, target.firstElementChild);
|
target.insertBefore(button, target.firstElementChild);
|
||||||
|
|
||||||
|
gradioApp().getElementById(tab + '_results').classList.toggle('mobile', currentlyMobile);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("resize", reportWindowSize);
|
window.addEventListener("resize", reportWindowSize);
|
||||||
|
|
||||||
|
onUiLoaded(function() {
|
||||||
|
reportWindowSize();
|
||||||
|
});
|
||||||
|
|||||||
@@ -0,0 +1,759 @@
|
|||||||
|
import numpy as np
|
||||||
|
import gradio as gr
|
||||||
|
import math
|
||||||
|
from modules.ui_components import InputAccordion
|
||||||
|
import modules.scripts as scripts
|
||||||
|
from modules import infotext_utils
|
||||||
|
|
||||||
|
infotext_utils.register_info_json('Soft Inpainting')
|
||||||
|
|
||||||
|
|
||||||
|
class SoftInpaintingSettings:
|
||||||
|
def __init__(self,
|
||||||
|
mask_blend_power,
|
||||||
|
mask_blend_scale,
|
||||||
|
inpaint_detail_preservation,
|
||||||
|
composite_mask_influence,
|
||||||
|
composite_difference_threshold,
|
||||||
|
composite_difference_contrast):
|
||||||
|
self.mask_blend_power = mask_blend_power
|
||||||
|
self.mask_blend_scale = mask_blend_scale
|
||||||
|
self.inpaint_detail_preservation = inpaint_detail_preservation
|
||||||
|
self.composite_mask_influence = composite_mask_influence
|
||||||
|
self.composite_difference_threshold = composite_difference_threshold
|
||||||
|
self.composite_difference_contrast = composite_difference_contrast
|
||||||
|
|
||||||
|
def add_generation_params(self, dest):
|
||||||
|
dest['Soft Inpainting'] = {
|
||||||
|
'sb': self.mask_blend_power,
|
||||||
|
'ps': self.mask_blend_scale,
|
||||||
|
'tcb': self.inpaint_detail_preservation,
|
||||||
|
'mi': self.composite_mask_influence,
|
||||||
|
'dt': self.composite_difference_threshold,
|
||||||
|
'dc': self.composite_difference_contrast,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------- Methods -------------------
|
||||||
|
|
||||||
|
def processing_uses_inpainting(p):
|
||||||
|
# TODO: Figure out a better way to determine if inpainting is being used by p
|
||||||
|
if getattr(p, "image_mask", None) is not None:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if getattr(p, "mask", None) is not None:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if getattr(p, "nmask", None) is not None:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def latent_blend(settings, a, b, t):
|
||||||
|
"""
|
||||||
|
Interpolates two latent image representations according to the parameter t,
|
||||||
|
where the interpolated vectors' magnitudes are also interpolated separately.
|
||||||
|
The "detail_preservation" factor biases the magnitude interpolation towards
|
||||||
|
the larger of the two magnitudes.
|
||||||
|
"""
|
||||||
|
import torch
|
||||||
|
|
||||||
|
# NOTE: We use inplace operations wherever possible.
|
||||||
|
|
||||||
|
# [4][w][h] to [1][4][w][h]
|
||||||
|
t2 = t.unsqueeze(0)
|
||||||
|
# [4][w][h] to [1][1][w][h] - the [4] seem redundant.
|
||||||
|
t3 = t[0].unsqueeze(0).unsqueeze(0)
|
||||||
|
|
||||||
|
one_minus_t2 = 1 - t2
|
||||||
|
one_minus_t3 = 1 - t3
|
||||||
|
|
||||||
|
# Linearly interpolate the image vectors.
|
||||||
|
a_scaled = a * one_minus_t2
|
||||||
|
b_scaled = b * t2
|
||||||
|
image_interp = a_scaled
|
||||||
|
image_interp.add_(b_scaled)
|
||||||
|
result_type = image_interp.dtype
|
||||||
|
del a_scaled, b_scaled, t2, one_minus_t2
|
||||||
|
|
||||||
|
# Calculate the magnitude of the interpolated vectors. (We will remove this magnitude.)
|
||||||
|
# 64-bit operations are used here to allow large exponents.
|
||||||
|
current_magnitude = torch.norm(image_interp, p=2, dim=1, keepdim=True).to(torch.float64).add_(0.00001)
|
||||||
|
|
||||||
|
# Interpolate the powered magnitudes, then un-power them (bring them back to a power of 1).
|
||||||
|
a_magnitude = torch.norm(a, p=2, dim=1, keepdim=True).to(torch.float64).pow_(
|
||||||
|
settings.inpaint_detail_preservation) * one_minus_t3
|
||||||
|
b_magnitude = torch.norm(b, p=2, dim=1, keepdim=True).to(torch.float64).pow_(
|
||||||
|
settings.inpaint_detail_preservation) * t3
|
||||||
|
desired_magnitude = a_magnitude
|
||||||
|
desired_magnitude.add_(b_magnitude).pow_(1 / settings.inpaint_detail_preservation)
|
||||||
|
del a_magnitude, b_magnitude, t3, one_minus_t3
|
||||||
|
|
||||||
|
# Change the linearly interpolated image vectors' magnitudes to the value we want.
|
||||||
|
# This is the last 64-bit operation.
|
||||||
|
image_interp_scaling_factor = desired_magnitude
|
||||||
|
image_interp_scaling_factor.div_(current_magnitude)
|
||||||
|
image_interp_scaling_factor = image_interp_scaling_factor.to(result_type)
|
||||||
|
image_interp_scaled = image_interp
|
||||||
|
image_interp_scaled.mul_(image_interp_scaling_factor)
|
||||||
|
del current_magnitude
|
||||||
|
del desired_magnitude
|
||||||
|
del image_interp
|
||||||
|
del image_interp_scaling_factor
|
||||||
|
del result_type
|
||||||
|
|
||||||
|
return image_interp_scaled
|
||||||
|
|
||||||
|
|
||||||
|
def get_modified_nmask(settings, nmask, sigma):
|
||||||
|
"""
|
||||||
|
Converts a negative mask representing the transparency of the original latent vectors being overlayed
|
||||||
|
to a mask that is scaled according to the denoising strength for this step.
|
||||||
|
|
||||||
|
Where:
|
||||||
|
0 = fully opaque, infinite density, fully masked
|
||||||
|
1 = fully transparent, zero density, fully unmasked
|
||||||
|
|
||||||
|
We bring this transparency to a power, as this allows one to simulate N number of blending operations
|
||||||
|
where N can be any positive real value. Using this one can control the balance of influence between
|
||||||
|
the denoiser and the original latents according to the sigma value.
|
||||||
|
|
||||||
|
NOTE: "mask" is not used
|
||||||
|
"""
|
||||||
|
import torch
|
||||||
|
return torch.pow(nmask, (sigma ** settings.mask_blend_power) * settings.mask_blend_scale)
|
||||||
|
|
||||||
|
|
||||||
|
def apply_adaptive_masks(
|
||||||
|
settings: SoftInpaintingSettings,
|
||||||
|
nmask,
|
||||||
|
latent_orig,
|
||||||
|
latent_processed,
|
||||||
|
overlay_images,
|
||||||
|
width, height,
|
||||||
|
paste_to):
|
||||||
|
import torch
|
||||||
|
import modules.processing as proc
|
||||||
|
import modules.images as images
|
||||||
|
from PIL import Image, ImageOps, ImageFilter
|
||||||
|
|
||||||
|
# TODO: Bias the blending according to the latent mask, add adjustable parameter for bias control.
|
||||||
|
latent_mask = nmask[0].float()
|
||||||
|
# convert the original mask into a form we use to scale distances for thresholding
|
||||||
|
mask_scalar = 1 - (torch.clamp(latent_mask, min=0, max=1) ** (settings.mask_blend_scale / 2))
|
||||||
|
mask_scalar = (0.5 * (1 - settings.composite_mask_influence)
|
||||||
|
+ mask_scalar * settings.composite_mask_influence)
|
||||||
|
mask_scalar = mask_scalar / (1.00001 - mask_scalar)
|
||||||
|
mask_scalar = mask_scalar.cpu().numpy()
|
||||||
|
|
||||||
|
latent_distance = torch.norm(latent_processed - latent_orig, p=2, dim=1)
|
||||||
|
|
||||||
|
kernel, kernel_center = get_gaussian_kernel(stddev_radius=1.5, max_radius=2)
|
||||||
|
|
||||||
|
masks_for_overlay = []
|
||||||
|
|
||||||
|
for i, (distance_map, overlay_image) in enumerate(zip(latent_distance, overlay_images)):
|
||||||
|
converted_mask = distance_map.float().cpu().numpy()
|
||||||
|
converted_mask = weighted_histogram_filter(converted_mask, kernel, kernel_center,
|
||||||
|
percentile_min=0.9, percentile_max=1, min_width=1)
|
||||||
|
converted_mask = weighted_histogram_filter(converted_mask, kernel, kernel_center,
|
||||||
|
percentile_min=0.25, percentile_max=0.75, min_width=1)
|
||||||
|
|
||||||
|
# The distance at which opacity of original decreases to 50%
|
||||||
|
half_weighted_distance = settings.composite_difference_threshold * mask_scalar
|
||||||
|
converted_mask = converted_mask / half_weighted_distance
|
||||||
|
|
||||||
|
converted_mask = 1 / (1 + converted_mask ** settings.composite_difference_contrast)
|
||||||
|
converted_mask = smootherstep(converted_mask)
|
||||||
|
converted_mask = 1 - converted_mask
|
||||||
|
converted_mask = 255. * converted_mask
|
||||||
|
converted_mask = converted_mask.astype(np.uint8)
|
||||||
|
converted_mask = Image.fromarray(converted_mask)
|
||||||
|
converted_mask = images.resize_image(2, converted_mask, width, height)
|
||||||
|
converted_mask = proc.create_binary_mask(converted_mask, round=False)
|
||||||
|
|
||||||
|
# Remove aliasing artifacts using a gaussian blur.
|
||||||
|
converted_mask = converted_mask.filter(ImageFilter.GaussianBlur(radius=4))
|
||||||
|
|
||||||
|
# Expand the mask to fit the whole image if needed.
|
||||||
|
if paste_to is not None:
|
||||||
|
converted_mask = proc.uncrop(converted_mask,
|
||||||
|
(overlay_image.width, overlay_image.height),
|
||||||
|
paste_to)
|
||||||
|
|
||||||
|
masks_for_overlay.append(converted_mask)
|
||||||
|
|
||||||
|
image_masked = Image.new('RGBa', (overlay_image.width, overlay_image.height))
|
||||||
|
image_masked.paste(overlay_image.convert("RGBA").convert("RGBa"),
|
||||||
|
mask=ImageOps.invert(converted_mask.convert('L')))
|
||||||
|
|
||||||
|
overlay_images[i] = image_masked.convert('RGBA')
|
||||||
|
|
||||||
|
return masks_for_overlay
|
||||||
|
|
||||||
|
|
||||||
|
def apply_masks(
|
||||||
|
settings,
|
||||||
|
nmask,
|
||||||
|
overlay_images,
|
||||||
|
width, height,
|
||||||
|
paste_to):
|
||||||
|
import torch
|
||||||
|
import modules.processing as proc
|
||||||
|
import modules.images as images
|
||||||
|
from PIL import Image, ImageOps, ImageFilter
|
||||||
|
|
||||||
|
converted_mask = nmask[0].float()
|
||||||
|
converted_mask = torch.clamp(converted_mask, min=0, max=1).pow_(settings.mask_blend_scale / 2)
|
||||||
|
converted_mask = 255. * converted_mask
|
||||||
|
converted_mask = converted_mask.cpu().numpy().astype(np.uint8)
|
||||||
|
converted_mask = Image.fromarray(converted_mask)
|
||||||
|
converted_mask = images.resize_image(2, converted_mask, width, height)
|
||||||
|
converted_mask = proc.create_binary_mask(converted_mask, round=False)
|
||||||
|
|
||||||
|
# Remove aliasing artifacts using a gaussian blur.
|
||||||
|
converted_mask = converted_mask.filter(ImageFilter.GaussianBlur(radius=4))
|
||||||
|
|
||||||
|
# Expand the mask to fit the whole image if needed.
|
||||||
|
if paste_to is not None:
|
||||||
|
converted_mask = proc.uncrop(converted_mask,
|
||||||
|
(width, height),
|
||||||
|
paste_to)
|
||||||
|
|
||||||
|
masks_for_overlay = []
|
||||||
|
|
||||||
|
for i, overlay_image in enumerate(overlay_images):
|
||||||
|
masks_for_overlay[i] = converted_mask
|
||||||
|
|
||||||
|
image_masked = Image.new('RGBa', (overlay_image.width, overlay_image.height))
|
||||||
|
image_masked.paste(overlay_image.convert("RGBA").convert("RGBa"),
|
||||||
|
mask=ImageOps.invert(converted_mask.convert('L')))
|
||||||
|
|
||||||
|
overlay_images[i] = image_masked.convert('RGBA')
|
||||||
|
|
||||||
|
return masks_for_overlay
|
||||||
|
|
||||||
|
|
||||||
|
def weighted_histogram_filter(img, kernel, kernel_center, percentile_min=0.0, percentile_max=1.0, min_width=1.0):
|
||||||
|
"""
|
||||||
|
Generalization convolution filter capable of applying
|
||||||
|
weighted mean, median, maximum, and minimum filters
|
||||||
|
parametrically using an arbitrary kernel.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
img (nparray):
|
||||||
|
The image, a 2-D array of floats, to which the filter is being applied.
|
||||||
|
kernel (nparray):
|
||||||
|
The kernel, a 2-D array of floats.
|
||||||
|
kernel_center (nparray):
|
||||||
|
The kernel center coordinate, a 1-D array with two elements.
|
||||||
|
percentile_min (float):
|
||||||
|
The lower bound of the histogram window used by the filter,
|
||||||
|
from 0 to 1.
|
||||||
|
percentile_max (float):
|
||||||
|
The upper bound of the histogram window used by the filter,
|
||||||
|
from 0 to 1.
|
||||||
|
min_width (float):
|
||||||
|
The minimum size of the histogram window bounds, in weight units.
|
||||||
|
Must be greater than 0.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
(nparray): A filtered copy of the input image "img", a 2-D array of floats.
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Converts an index tuple into a vector.
|
||||||
|
def vec(x):
|
||||||
|
return np.array(x)
|
||||||
|
|
||||||
|
kernel_min = -kernel_center
|
||||||
|
kernel_max = vec(kernel.shape) - kernel_center
|
||||||
|
|
||||||
|
def weighted_histogram_filter_single(idx):
|
||||||
|
idx = vec(idx)
|
||||||
|
min_index = np.maximum(0, idx + kernel_min)
|
||||||
|
max_index = np.minimum(vec(img.shape), idx + kernel_max)
|
||||||
|
window_shape = max_index - min_index
|
||||||
|
|
||||||
|
class WeightedElement:
|
||||||
|
"""
|
||||||
|
An element of the histogram, its weight
|
||||||
|
and bounds.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self, value, weight):
|
||||||
|
self.value: float = value
|
||||||
|
self.weight: float = weight
|
||||||
|
self.window_min: float = 0.0
|
||||||
|
self.window_max: float = 1.0
|
||||||
|
|
||||||
|
# Collect the values in the image as WeightedElements,
|
||||||
|
# weighted by their corresponding kernel values.
|
||||||
|
values = []
|
||||||
|
for window_tup in np.ndindex(tuple(window_shape)):
|
||||||
|
window_index = vec(window_tup)
|
||||||
|
image_index = window_index + min_index
|
||||||
|
centered_kernel_index = image_index - idx
|
||||||
|
kernel_index = centered_kernel_index + kernel_center
|
||||||
|
element = WeightedElement(img[tuple(image_index)], kernel[tuple(kernel_index)])
|
||||||
|
values.append(element)
|
||||||
|
|
||||||
|
def sort_key(x: WeightedElement):
|
||||||
|
return x.value
|
||||||
|
|
||||||
|
values.sort(key=sort_key)
|
||||||
|
|
||||||
|
# Calculate the height of the stack (sum)
|
||||||
|
# and each sample's range they occupy in the stack
|
||||||
|
sum = 0
|
||||||
|
for i in range(len(values)):
|
||||||
|
values[i].window_min = sum
|
||||||
|
sum += values[i].weight
|
||||||
|
values[i].window_max = sum
|
||||||
|
|
||||||
|
# Calculate what range of this stack ("window")
|
||||||
|
# we want to get the weighted average across.
|
||||||
|
window_min = sum * percentile_min
|
||||||
|
window_max = sum * percentile_max
|
||||||
|
window_width = window_max - window_min
|
||||||
|
|
||||||
|
# Ensure the window is within the stack and at least a certain size.
|
||||||
|
if window_width < min_width:
|
||||||
|
window_center = (window_min + window_max) / 2
|
||||||
|
window_min = window_center - min_width / 2
|
||||||
|
window_max = window_center + min_width / 2
|
||||||
|
|
||||||
|
if window_max > sum:
|
||||||
|
window_max = sum
|
||||||
|
window_min = sum - min_width
|
||||||
|
|
||||||
|
if window_min < 0:
|
||||||
|
window_min = 0
|
||||||
|
window_max = min_width
|
||||||
|
|
||||||
|
value = 0
|
||||||
|
value_weight = 0
|
||||||
|
|
||||||
|
# Get the weighted average of all the samples
|
||||||
|
# that overlap with the window, weighted
|
||||||
|
# by the size of their overlap.
|
||||||
|
for i in range(len(values)):
|
||||||
|
if window_min >= values[i].window_max:
|
||||||
|
continue
|
||||||
|
if window_max <= values[i].window_min:
|
||||||
|
break
|
||||||
|
|
||||||
|
s = max(window_min, values[i].window_min)
|
||||||
|
e = min(window_max, values[i].window_max)
|
||||||
|
w = e - s
|
||||||
|
|
||||||
|
value += values[i].value * w
|
||||||
|
value_weight += w
|
||||||
|
|
||||||
|
return value / value_weight if value_weight != 0 else 0
|
||||||
|
|
||||||
|
img_out = img.copy()
|
||||||
|
|
||||||
|
# Apply the kernel operation over each pixel.
|
||||||
|
for index in np.ndindex(img.shape):
|
||||||
|
img_out[index] = weighted_histogram_filter_single(index)
|
||||||
|
|
||||||
|
return img_out
|
||||||
|
|
||||||
|
|
||||||
|
def smoothstep(x):
|
||||||
|
"""
|
||||||
|
The smoothstep function, input should be clamped to 0-1 range.
|
||||||
|
Turns a diagonal line (f(x) = x) into a sigmoid-like curve.
|
||||||
|
"""
|
||||||
|
return x * x * (3 - 2 * x)
|
||||||
|
|
||||||
|
|
||||||
|
def smootherstep(x):
|
||||||
|
"""
|
||||||
|
The smootherstep function, input should be clamped to 0-1 range.
|
||||||
|
Turns a diagonal line (f(x) = x) into a sigmoid-like curve.
|
||||||
|
"""
|
||||||
|
return x * x * x * (x * (6 * x - 15) + 10)
|
||||||
|
|
||||||
|
|
||||||
|
def get_gaussian_kernel(stddev_radius=1.0, max_radius=2):
|
||||||
|
"""
|
||||||
|
Creates a Gaussian kernel with thresholded edges.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
stddev_radius (float):
|
||||||
|
Standard deviation of the gaussian kernel, in pixels.
|
||||||
|
max_radius (int):
|
||||||
|
The size of the filter kernel. The number of pixels is (max_radius*2+1) ** 2.
|
||||||
|
The kernel is thresholded so that any values one pixel beyond this radius
|
||||||
|
is weighted at 0.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
(nparray, nparray): A kernel array (shape: (N, N)), its center coordinate (shape: (2))
|
||||||
|
"""
|
||||||
|
|
||||||
|
# Evaluates a 0-1 normalized gaussian function for a given square distance from the mean.
|
||||||
|
def gaussian(sqr_mag):
|
||||||
|
return math.exp(-sqr_mag / (stddev_radius * stddev_radius))
|
||||||
|
|
||||||
|
# Helper function for converting a tuple to an array.
|
||||||
|
def vec(x):
|
||||||
|
return np.array(x)
|
||||||
|
|
||||||
|
"""
|
||||||
|
Since a gaussian is unbounded, we need to limit ourselves
|
||||||
|
to a finite range.
|
||||||
|
We taper the ends off at the end of that range so they equal zero
|
||||||
|
while preserving the maximum value of 1 at the mean.
|
||||||
|
"""
|
||||||
|
zero_radius = max_radius + 1.0
|
||||||
|
gauss_zero = gaussian(zero_radius * zero_radius)
|
||||||
|
gauss_kernel_scale = 1 / (1 - gauss_zero)
|
||||||
|
|
||||||
|
def gaussian_kernel_func(coordinate):
|
||||||
|
x = coordinate[0] ** 2.0 + coordinate[1] ** 2.0
|
||||||
|
x = gaussian(x)
|
||||||
|
x -= gauss_zero
|
||||||
|
x *= gauss_kernel_scale
|
||||||
|
x = max(0.0, x)
|
||||||
|
return x
|
||||||
|
|
||||||
|
size = max_radius * 2 + 1
|
||||||
|
kernel_center = max_radius
|
||||||
|
kernel = np.zeros((size, size))
|
||||||
|
|
||||||
|
for index in np.ndindex(kernel.shape):
|
||||||
|
kernel[index] = gaussian_kernel_func(vec(index) - kernel_center)
|
||||||
|
|
||||||
|
return kernel, kernel_center
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------- Constants -------------------
|
||||||
|
|
||||||
|
|
||||||
|
default = SoftInpaintingSettings(1, 0.5, 4, 0, 0.5, 2)
|
||||||
|
|
||||||
|
enabled_ui_label = "Soft inpainting"
|
||||||
|
enabled_gen_param_label = "Soft inpainting enabled"
|
||||||
|
enabled_el_id = "soft_inpainting_enabled"
|
||||||
|
|
||||||
|
ui_labels = SoftInpaintingSettings(
|
||||||
|
"Schedule bias",
|
||||||
|
"Preservation strength",
|
||||||
|
"Transition contrast boost",
|
||||||
|
"Mask influence",
|
||||||
|
"Difference threshold",
|
||||||
|
"Difference contrast")
|
||||||
|
|
||||||
|
ui_info = SoftInpaintingSettings(
|
||||||
|
"Shifts when preservation of original content occurs during denoising.",
|
||||||
|
"How strongly partially masked content should be preserved.",
|
||||||
|
"Amplifies the contrast that may be lost in partially masked regions.",
|
||||||
|
"How strongly the original mask should bias the difference threshold.",
|
||||||
|
"How much an image region can change before the original pixels are not blended in anymore.",
|
||||||
|
"How sharp the transition should be between blended and not blended.")
|
||||||
|
|
||||||
|
gen_param_labels = SoftInpaintingSettings(
|
||||||
|
"Soft inpainting schedule bias",
|
||||||
|
"Soft inpainting preservation strength",
|
||||||
|
"Soft inpainting transition contrast boost",
|
||||||
|
"Soft inpainting mask influence",
|
||||||
|
"Soft inpainting difference threshold",
|
||||||
|
"Soft inpainting difference contrast")
|
||||||
|
|
||||||
|
el_ids = SoftInpaintingSettings(
|
||||||
|
"mask_blend_power",
|
||||||
|
"mask_blend_scale",
|
||||||
|
"inpaint_detail_preservation",
|
||||||
|
"composite_mask_influence",
|
||||||
|
"composite_difference_threshold",
|
||||||
|
"composite_difference_contrast")
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------- Script -------------------
|
||||||
|
|
||||||
|
|
||||||
|
class Script(scripts.Script):
|
||||||
|
def __init__(self):
|
||||||
|
self.section = "inpaint"
|
||||||
|
self.masks_for_overlay = None
|
||||||
|
self.overlay_images = None
|
||||||
|
|
||||||
|
def title(self):
|
||||||
|
return "Soft Inpainting"
|
||||||
|
|
||||||
|
def show(self, is_img2img):
|
||||||
|
return scripts.AlwaysVisible if is_img2img else False
|
||||||
|
|
||||||
|
def ui(self, is_img2img):
|
||||||
|
if not is_img2img:
|
||||||
|
return
|
||||||
|
|
||||||
|
with InputAccordion(False, label=enabled_ui_label, elem_id=enabled_el_id) as soft_inpainting_enabled:
|
||||||
|
with gr.Group():
|
||||||
|
gr.Markdown(
|
||||||
|
"""
|
||||||
|
Soft inpainting allows you to **seamlessly blend original content with inpainted content** according to the mask opacity.
|
||||||
|
**High _Mask blur_** values are recommended!
|
||||||
|
""")
|
||||||
|
|
||||||
|
power = \
|
||||||
|
gr.Slider(label=ui_labels.mask_blend_power,
|
||||||
|
info=ui_info.mask_blend_power,
|
||||||
|
minimum=0,
|
||||||
|
maximum=8,
|
||||||
|
step=0.1,
|
||||||
|
value=default.mask_blend_power,
|
||||||
|
elem_id=el_ids.mask_blend_power)
|
||||||
|
scale = \
|
||||||
|
gr.Slider(label=ui_labels.mask_blend_scale,
|
||||||
|
info=ui_info.mask_blend_scale,
|
||||||
|
minimum=0,
|
||||||
|
maximum=8,
|
||||||
|
step=0.05,
|
||||||
|
value=default.mask_blend_scale,
|
||||||
|
elem_id=el_ids.mask_blend_scale)
|
||||||
|
detail = \
|
||||||
|
gr.Slider(label=ui_labels.inpaint_detail_preservation,
|
||||||
|
info=ui_info.inpaint_detail_preservation,
|
||||||
|
minimum=1,
|
||||||
|
maximum=32,
|
||||||
|
step=0.5,
|
||||||
|
value=default.inpaint_detail_preservation,
|
||||||
|
elem_id=el_ids.inpaint_detail_preservation)
|
||||||
|
|
||||||
|
gr.Markdown(
|
||||||
|
"""
|
||||||
|
### Pixel Composite Settings
|
||||||
|
""")
|
||||||
|
|
||||||
|
mask_inf = \
|
||||||
|
gr.Slider(label=ui_labels.composite_mask_influence,
|
||||||
|
info=ui_info.composite_mask_influence,
|
||||||
|
minimum=0,
|
||||||
|
maximum=1,
|
||||||
|
step=0.05,
|
||||||
|
value=default.composite_mask_influence,
|
||||||
|
elem_id=el_ids.composite_mask_influence)
|
||||||
|
|
||||||
|
dif_thresh = \
|
||||||
|
gr.Slider(label=ui_labels.composite_difference_threshold,
|
||||||
|
info=ui_info.composite_difference_threshold,
|
||||||
|
minimum=0,
|
||||||
|
maximum=8,
|
||||||
|
step=0.25,
|
||||||
|
value=default.composite_difference_threshold,
|
||||||
|
elem_id=el_ids.composite_difference_threshold)
|
||||||
|
|
||||||
|
dif_contr = \
|
||||||
|
gr.Slider(label=ui_labels.composite_difference_contrast,
|
||||||
|
info=ui_info.composite_difference_contrast,
|
||||||
|
minimum=0,
|
||||||
|
maximum=8,
|
||||||
|
step=0.25,
|
||||||
|
value=default.composite_difference_contrast,
|
||||||
|
elem_id=el_ids.composite_difference_contrast)
|
||||||
|
|
||||||
|
with gr.Accordion("Help", open=False):
|
||||||
|
gr.Markdown(
|
||||||
|
f"""
|
||||||
|
### {ui_labels.mask_blend_power}
|
||||||
|
|
||||||
|
The blending strength of original content is scaled proportionally with the decreasing noise level values at each step (sigmas).
|
||||||
|
This ensures that the influence of the denoiser and original content preservation is roughly balanced at each step.
|
||||||
|
This balance can be shifted using this parameter, controlling whether earlier or later steps have stronger preservation.
|
||||||
|
|
||||||
|
- **Below 1**: Stronger preservation near the end (with low sigma)
|
||||||
|
- **1**: Balanced (proportional to sigma)
|
||||||
|
- **Above 1**: Stronger preservation in the beginning (with high sigma)
|
||||||
|
""")
|
||||||
|
gr.Markdown(
|
||||||
|
f"""
|
||||||
|
### {ui_labels.mask_blend_scale}
|
||||||
|
|
||||||
|
Skews whether partially masked image regions should be more likely to preserve the original content or favor inpainted content.
|
||||||
|
This may need to be adjusted depending on the {ui_labels.mask_blend_power}, CFG Scale, prompt and Denoising strength.
|
||||||
|
|
||||||
|
- **Low values**: Favors generated content.
|
||||||
|
- **High values**: Favors original content.
|
||||||
|
""")
|
||||||
|
gr.Markdown(
|
||||||
|
f"""
|
||||||
|
### {ui_labels.inpaint_detail_preservation}
|
||||||
|
|
||||||
|
This parameter controls how the original latent vectors and denoised latent vectors are interpolated.
|
||||||
|
With higher values, the magnitude of the resulting blended vector will be closer to the maximum of the two interpolated vectors.
|
||||||
|
This can prevent the loss of contrast that occurs with linear interpolation.
|
||||||
|
|
||||||
|
- **Low values**: Softer blending, details may fade.
|
||||||
|
- **High values**: Stronger contrast, may over-saturate colors.
|
||||||
|
""")
|
||||||
|
|
||||||
|
gr.Markdown(
|
||||||
|
"""
|
||||||
|
## Pixel Composite Settings
|
||||||
|
|
||||||
|
Masks are generated based on how much a part of the image changed after denoising.
|
||||||
|
These masks are used to blend the original and final images together.
|
||||||
|
If the difference is low, the original pixels are used instead of the pixels returned by the inpainting process.
|
||||||
|
""")
|
||||||
|
|
||||||
|
gr.Markdown(
|
||||||
|
f"""
|
||||||
|
### {ui_labels.composite_mask_influence}
|
||||||
|
|
||||||
|
This parameter controls how much the mask should bias this sensitivity to difference.
|
||||||
|
|
||||||
|
- **0**: Ignore the mask, only consider differences in image content.
|
||||||
|
- **1**: Follow the mask closely despite image content changes.
|
||||||
|
""")
|
||||||
|
|
||||||
|
gr.Markdown(
|
||||||
|
f"""
|
||||||
|
### {ui_labels.composite_difference_threshold}
|
||||||
|
|
||||||
|
This value represents the difference at which the original pixels will have less than 50% opacity.
|
||||||
|
|
||||||
|
- **Low values**: Two images patches must be almost the same in order to retain original pixels.
|
||||||
|
- **High values**: Two images patches can be very different and still retain original pixels.
|
||||||
|
""")
|
||||||
|
|
||||||
|
gr.Markdown(
|
||||||
|
f"""
|
||||||
|
### {ui_labels.composite_difference_contrast}
|
||||||
|
|
||||||
|
This value represents the contrast between the opacity of the original and inpainted content.
|
||||||
|
|
||||||
|
- **Low values**: The blend will be more gradual and have longer transitions, but may cause ghosting.
|
||||||
|
- **High values**: Ghosting will be less common, but transitions may be very sudden.
|
||||||
|
""")
|
||||||
|
|
||||||
|
def get_element_value(generation_params: dict, old_key, new_key):
|
||||||
|
if 'Soft Inpainting' in generation_params:
|
||||||
|
return generation_params['Soft Inpainting'].get(new_key, True)
|
||||||
|
else:
|
||||||
|
return generation_params.get(old_key)
|
||||||
|
|
||||||
|
self.infotext_fields = [
|
||||||
|
(soft_inpainting_enabled, lambda d: get_element_value(d, enabled_gen_param_label, None)),
|
||||||
|
(power, lambda d: get_element_value(d, gen_param_labels.mask_blend_power, 'sb')),
|
||||||
|
(scale, lambda d: get_element_value(d, gen_param_labels.mask_blend_scale, 'ps')),
|
||||||
|
(detail, lambda d: get_element_value(d, gen_param_labels.inpaint_detail_preservation, 'tcb')),
|
||||||
|
(mask_inf, lambda d: get_element_value(d, gen_param_labels.composite_mask_influence, 'mi')),
|
||||||
|
(dif_thresh, lambda d: get_element_value(d, gen_param_labels.composite_difference_threshold, 'dt')),
|
||||||
|
(dif_contr, lambda d: get_element_value(d, gen_param_labels.composite_difference_contrast, 'dc'))
|
||||||
|
]
|
||||||
|
|
||||||
|
self.paste_field_names = []
|
||||||
|
for _, field_name in self.infotext_fields:
|
||||||
|
self.paste_field_names.append(field_name)
|
||||||
|
|
||||||
|
return [soft_inpainting_enabled,
|
||||||
|
power,
|
||||||
|
scale,
|
||||||
|
detail,
|
||||||
|
mask_inf,
|
||||||
|
dif_thresh,
|
||||||
|
dif_contr]
|
||||||
|
|
||||||
|
def process(self, p, enabled, power, scale, detail_preservation, mask_inf, dif_thresh, dif_contr):
|
||||||
|
if not enabled:
|
||||||
|
return
|
||||||
|
|
||||||
|
if not processing_uses_inpainting(p):
|
||||||
|
return
|
||||||
|
|
||||||
|
# Shut off the rounding it normally does.
|
||||||
|
p.mask_round = False
|
||||||
|
|
||||||
|
settings = SoftInpaintingSettings(power, scale, detail_preservation, mask_inf, dif_thresh, dif_contr)
|
||||||
|
|
||||||
|
# p.extra_generation_params["Mask rounding"] = False
|
||||||
|
settings.add_generation_params(p.extra_generation_params)
|
||||||
|
|
||||||
|
def on_mask_blend(self, p, mba: scripts.MaskBlendArgs, enabled, power, scale, detail_preservation, mask_inf,
|
||||||
|
dif_thresh, dif_contr):
|
||||||
|
if not enabled:
|
||||||
|
return
|
||||||
|
|
||||||
|
if not processing_uses_inpainting(p):
|
||||||
|
return
|
||||||
|
|
||||||
|
if mba.is_final_blend:
|
||||||
|
mba.blended_latent = mba.current_latent
|
||||||
|
return
|
||||||
|
|
||||||
|
settings = SoftInpaintingSettings(power, scale, detail_preservation, mask_inf, dif_thresh, dif_contr)
|
||||||
|
|
||||||
|
# todo: Why is sigma 2D? Both values are the same.
|
||||||
|
mba.blended_latent = latent_blend(settings,
|
||||||
|
mba.init_latent,
|
||||||
|
mba.current_latent,
|
||||||
|
get_modified_nmask(settings, mba.nmask, mba.sigma[0]))
|
||||||
|
|
||||||
|
def post_sample(self, p, ps: scripts.PostSampleArgs, enabled, power, scale, detail_preservation, mask_inf,
|
||||||
|
dif_thresh, dif_contr):
|
||||||
|
if not enabled:
|
||||||
|
return
|
||||||
|
|
||||||
|
if not processing_uses_inpainting(p):
|
||||||
|
return
|
||||||
|
|
||||||
|
nmask = getattr(p, "nmask", None)
|
||||||
|
if nmask is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
from modules import images
|
||||||
|
from modules.shared import opts
|
||||||
|
|
||||||
|
settings = SoftInpaintingSettings(power, scale, detail_preservation, mask_inf, dif_thresh, dif_contr)
|
||||||
|
|
||||||
|
# since the original code puts holes in the existing overlay images,
|
||||||
|
# we have to rebuild them.
|
||||||
|
self.overlay_images = []
|
||||||
|
for img in p.init_images:
|
||||||
|
|
||||||
|
image = images.flatten(img, opts.img2img_background_color)
|
||||||
|
|
||||||
|
if p.paste_to is None and p.resize_mode != 3:
|
||||||
|
image = images.resize_image(p.resize_mode, image, p.width, p.height)
|
||||||
|
|
||||||
|
self.overlay_images.append(image.convert('RGBA'))
|
||||||
|
|
||||||
|
if len(p.init_images) == 1:
|
||||||
|
self.overlay_images = self.overlay_images * p.batch_size
|
||||||
|
|
||||||
|
if getattr(ps.samples, 'already_decoded', False):
|
||||||
|
self.masks_for_overlay = apply_masks(settings=settings,
|
||||||
|
nmask=nmask,
|
||||||
|
overlay_images=self.overlay_images,
|
||||||
|
width=p.width,
|
||||||
|
height=p.height,
|
||||||
|
paste_to=p.paste_to)
|
||||||
|
else:
|
||||||
|
self.masks_for_overlay = apply_adaptive_masks(settings=settings,
|
||||||
|
nmask=nmask,
|
||||||
|
latent_orig=p.init_latent,
|
||||||
|
latent_processed=ps.samples,
|
||||||
|
overlay_images=self.overlay_images,
|
||||||
|
width=p.width,
|
||||||
|
height=p.height,
|
||||||
|
paste_to=p.paste_to)
|
||||||
|
|
||||||
|
def postprocess_maskoverlay(self, p, ppmo: scripts.PostProcessMaskOverlayArgs, enabled, power, scale,
|
||||||
|
detail_preservation, mask_inf, dif_thresh, dif_contr):
|
||||||
|
if not enabled:
|
||||||
|
return
|
||||||
|
|
||||||
|
if not processing_uses_inpainting(p):
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.masks_for_overlay is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
if self.overlay_images is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
ppmo.mask_for_overlay = self.masks_for_overlay[ppmo.index]
|
||||||
|
ppmo.overlay_image = self.overlay_images[ppmo.index]
|
||||||
+1
-309
@@ -4,107 +4,6 @@
|
|||||||
#licenses pre { margin: 1em 0 2em 0;}
|
#licenses pre { margin: 1em 0 2em 0;}
|
||||||
</style>
|
</style>
|
||||||
|
|
||||||
<h2><a href="https://github.com/sczhou/CodeFormer/blob/master/LICENSE">CodeFormer</a></h2>
|
|
||||||
<small>Parts of CodeFormer code had to be copied to be compatible with GFPGAN.</small>
|
|
||||||
<pre>
|
|
||||||
S-Lab License 1.0
|
|
||||||
|
|
||||||
Copyright 2022 S-Lab
|
|
||||||
|
|
||||||
Redistribution and use for non-commercial purpose in source and
|
|
||||||
binary forms, with or without modification, are permitted provided
|
|
||||||
that the following conditions are met:
|
|
||||||
|
|
||||||
1. Redistributions of source code must retain the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright
|
|
||||||
notice, this list of conditions and the following disclaimer in
|
|
||||||
the documentation and/or other materials provided with the
|
|
||||||
distribution.
|
|
||||||
|
|
||||||
3. Neither the name of the copyright holder nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived
|
|
||||||
from this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
||||||
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
||||||
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
||||||
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
||||||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
||||||
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
||||||
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
||||||
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
||||||
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
|
|
||||||
In the event that redistribution and/or use for commercial purpose in
|
|
||||||
source or binary forms, with or without modification is required,
|
|
||||||
please contact the contributor(s) of the work.
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
|
|
||||||
<h2><a href="https://github.com/victorca25/iNNfer/blob/main/LICENSE">ESRGAN</a></h2>
|
|
||||||
<small>Code for architecture and reading models copied.</small>
|
|
||||||
<pre>
|
|
||||||
MIT License
|
|
||||||
|
|
||||||
Copyright (c) 2021 victorca25
|
|
||||||
|
|
||||||
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.
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
<h2><a href="https://github.com/xinntao/Real-ESRGAN/blob/master/LICENSE">Real-ESRGAN</a></h2>
|
|
||||||
<small>Some code is copied to support ESRGAN models.</small>
|
|
||||||
<pre>
|
|
||||||
BSD 3-Clause License
|
|
||||||
|
|
||||||
Copyright (c) 2021, Xintao Wang
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
Redistribution and use in source and binary forms, with or without
|
|
||||||
modification, are permitted provided that the following conditions are met:
|
|
||||||
|
|
||||||
1. Redistributions of source code must retain the above copyright notice, this
|
|
||||||
list of conditions and the following disclaimer.
|
|
||||||
|
|
||||||
2. Redistributions in binary form must reproduce the above copyright notice,
|
|
||||||
this list of conditions and the following disclaimer in the documentation
|
|
||||||
and/or other materials provided with the distribution.
|
|
||||||
|
|
||||||
3. Neither the name of the copyright holder nor the names of its
|
|
||||||
contributors may be used to endorse or promote products derived from
|
|
||||||
this software without specific prior written permission.
|
|
||||||
|
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
||||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
||||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
||||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
||||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
||||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
||||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
||||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
||||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
||||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
<h2><a href="https://github.com/invoke-ai/InvokeAI/blob/main/LICENSE">InvokeAI</a></h2>
|
<h2><a href="https://github.com/invoke-ai/InvokeAI/blob/main/LICENSE">InvokeAI</a></h2>
|
||||||
<small>Some code for compatibility with OSX is taken from lstein's repository.</small>
|
<small>Some code for compatibility with OSX is taken from lstein's repository.</small>
|
||||||
<pre>
|
<pre>
|
||||||
@@ -183,213 +82,6 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
</pre>
|
</pre>
|
||||||
|
|
||||||
<h2><a href="https://github.com/JingyunLiang/SwinIR/blob/main/LICENSE">SwinIR</a></h2>
|
|
||||||
<small>Code added by contributors, most likely copied from this repository.</small>
|
|
||||||
|
|
||||||
<pre>
|
|
||||||
Apache License
|
|
||||||
Version 2.0, January 2004
|
|
||||||
http://www.apache.org/licenses/
|
|
||||||
|
|
||||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
||||||
|
|
||||||
1. Definitions.
|
|
||||||
|
|
||||||
"License" shall mean the terms and conditions for use, reproduction,
|
|
||||||
and distribution as defined by Sections 1 through 9 of this document.
|
|
||||||
|
|
||||||
"Licensor" shall mean the copyright owner or entity authorized by
|
|
||||||
the copyright owner that is granting the License.
|
|
||||||
|
|
||||||
"Legal Entity" shall mean the union of the acting entity and all
|
|
||||||
other entities that control, are controlled by, or are under common
|
|
||||||
control with that entity. For the purposes of this definition,
|
|
||||||
"control" means (i) the power, direct or indirect, to cause the
|
|
||||||
direction or management of such entity, whether by contract or
|
|
||||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
||||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
||||||
|
|
||||||
"You" (or "Your") shall mean an individual or Legal Entity
|
|
||||||
exercising permissions granted by this License.
|
|
||||||
|
|
||||||
"Source" form shall mean the preferred form for making modifications,
|
|
||||||
including but not limited to software source code, documentation
|
|
||||||
source, and configuration files.
|
|
||||||
|
|
||||||
"Object" form shall mean any form resulting from mechanical
|
|
||||||
transformation or translation of a Source form, including but
|
|
||||||
not limited to compiled object code, generated documentation,
|
|
||||||
and conversions to other media types.
|
|
||||||
|
|
||||||
"Work" shall mean the work of authorship, whether in Source or
|
|
||||||
Object form, made available under the License, as indicated by a
|
|
||||||
copyright notice that is included in or attached to the work
|
|
||||||
(an example is provided in the Appendix below).
|
|
||||||
|
|
||||||
"Derivative Works" shall mean any work, whether in Source or Object
|
|
||||||
form, that is based on (or derived from) the Work and for which the
|
|
||||||
editorial revisions, annotations, elaborations, or other modifications
|
|
||||||
represent, as a whole, an original work of authorship. For the purposes
|
|
||||||
of this License, Derivative Works shall not include works that remain
|
|
||||||
separable from, or merely link (or bind by name) to the interfaces of,
|
|
||||||
the Work and Derivative Works thereof.
|
|
||||||
|
|
||||||
"Contribution" shall mean any work of authorship, including
|
|
||||||
the original version of the Work and any modifications or additions
|
|
||||||
to that Work or Derivative Works thereof, that is intentionally
|
|
||||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
||||||
or by an individual or Legal Entity authorized to submit on behalf of
|
|
||||||
the copyright owner. For the purposes of this definition, "submitted"
|
|
||||||
means any form of electronic, verbal, or written communication sent
|
|
||||||
to the Licensor or its representatives, including but not limited to
|
|
||||||
communication on electronic mailing lists, source code control systems,
|
|
||||||
and issue tracking systems that are managed by, or on behalf of, the
|
|
||||||
Licensor for the purpose of discussing and improving the Work, but
|
|
||||||
excluding communication that is conspicuously marked or otherwise
|
|
||||||
designated in writing by the copyright owner as "Not a Contribution."
|
|
||||||
|
|
||||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
||||||
on behalf of whom a Contribution has been received by Licensor and
|
|
||||||
subsequently incorporated within the Work.
|
|
||||||
|
|
||||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
copyright license to reproduce, prepare Derivative Works of,
|
|
||||||
publicly display, publicly perform, sublicense, and distribute the
|
|
||||||
Work and such Derivative Works in Source or Object form.
|
|
||||||
|
|
||||||
3. Grant of Patent License. Subject to the terms and conditions of
|
|
||||||
this License, each Contributor hereby grants to You a perpetual,
|
|
||||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
||||||
(except as stated in this section) patent license to make, have made,
|
|
||||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
||||||
where such license applies only to those patent claims licensable
|
|
||||||
by such Contributor that are necessarily infringed by their
|
|
||||||
Contribution(s) alone or by combination of their Contribution(s)
|
|
||||||
with the Work to which such Contribution(s) was submitted. If You
|
|
||||||
institute patent litigation against any entity (including a
|
|
||||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
||||||
or a Contribution incorporated within the Work constitutes direct
|
|
||||||
or contributory patent infringement, then any patent licenses
|
|
||||||
granted to You under this License for that Work shall terminate
|
|
||||||
as of the date such litigation is filed.
|
|
||||||
|
|
||||||
4. Redistribution. You may reproduce and distribute copies of the
|
|
||||||
Work or Derivative Works thereof in any medium, with or without
|
|
||||||
modifications, and in Source or Object form, provided that You
|
|
||||||
meet the following conditions:
|
|
||||||
|
|
||||||
(a) You must give any other recipients of the Work or
|
|
||||||
Derivative Works a copy of this License; and
|
|
||||||
|
|
||||||
(b) You must cause any modified files to carry prominent notices
|
|
||||||
stating that You changed the files; and
|
|
||||||
|
|
||||||
(c) You must retain, in the Source form of any Derivative Works
|
|
||||||
that You distribute, all copyright, patent, trademark, and
|
|
||||||
attribution notices from the Source form of the Work,
|
|
||||||
excluding those notices that do not pertain to any part of
|
|
||||||
the Derivative Works; and
|
|
||||||
|
|
||||||
(d) If the Work includes a "NOTICE" text file as part of its
|
|
||||||
distribution, then any Derivative Works that You distribute must
|
|
||||||
include a readable copy of the attribution notices contained
|
|
||||||
within such NOTICE file, excluding those notices that do not
|
|
||||||
pertain to any part of the Derivative Works, in at least one
|
|
||||||
of the following places: within a NOTICE text file distributed
|
|
||||||
as part of the Derivative Works; within the Source form or
|
|
||||||
documentation, if provided along with the Derivative Works; or,
|
|
||||||
within a display generated by the Derivative Works, if and
|
|
||||||
wherever such third-party notices normally appear. The contents
|
|
||||||
of the NOTICE file are for informational purposes only and
|
|
||||||
do not modify the License. You may add Your own attribution
|
|
||||||
notices within Derivative Works that You distribute, alongside
|
|
||||||
or as an addendum to the NOTICE text from the Work, provided
|
|
||||||
that such additional attribution notices cannot be construed
|
|
||||||
as modifying the License.
|
|
||||||
|
|
||||||
You may add Your own copyright statement to Your modifications and
|
|
||||||
may provide additional or different license terms and conditions
|
|
||||||
for use, reproduction, or distribution of Your modifications, or
|
|
||||||
for any such Derivative Works as a whole, provided Your use,
|
|
||||||
reproduction, and distribution of the Work otherwise complies with
|
|
||||||
the conditions stated in this License.
|
|
||||||
|
|
||||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
||||||
any Contribution intentionally submitted for inclusion in the Work
|
|
||||||
by You to the Licensor shall be under the terms and conditions of
|
|
||||||
this License, without any additional terms or conditions.
|
|
||||||
Notwithstanding the above, nothing herein shall supersede or modify
|
|
||||||
the terms of any separate license agreement you may have executed
|
|
||||||
with Licensor regarding such Contributions.
|
|
||||||
|
|
||||||
6. Trademarks. This License does not grant permission to use the trade
|
|
||||||
names, trademarks, service marks, or product names of the Licensor,
|
|
||||||
except as required for reasonable and customary use in describing the
|
|
||||||
origin of the Work and reproducing the content of the NOTICE file.
|
|
||||||
|
|
||||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
||||||
agreed to in writing, Licensor provides the Work (and each
|
|
||||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
||||||
implied, including, without limitation, any warranties or conditions
|
|
||||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
||||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
||||||
appropriateness of using or redistributing the Work and assume any
|
|
||||||
risks associated with Your exercise of permissions under this License.
|
|
||||||
|
|
||||||
8. Limitation of Liability. In no event and under no legal theory,
|
|
||||||
whether in tort (including negligence), contract, or otherwise,
|
|
||||||
unless required by applicable law (such as deliberate and grossly
|
|
||||||
negligent acts) or agreed to in writing, shall any Contributor be
|
|
||||||
liable to You for damages, including any direct, indirect, special,
|
|
||||||
incidental, or consequential damages of any character arising as a
|
|
||||||
result of this License or out of the use or inability to use the
|
|
||||||
Work (including but not limited to damages for loss of goodwill,
|
|
||||||
work stoppage, computer failure or malfunction, or any and all
|
|
||||||
other commercial damages or losses), even if such Contributor
|
|
||||||
has been advised of the possibility of such damages.
|
|
||||||
|
|
||||||
9. Accepting Warranty or Additional Liability. While redistributing
|
|
||||||
the Work or Derivative Works thereof, You may choose to offer,
|
|
||||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
||||||
or other liability obligations and/or rights consistent with this
|
|
||||||
License. However, in accepting such obligations, You may act only
|
|
||||||
on Your own behalf and on Your sole responsibility, not on behalf
|
|
||||||
of any other Contributor, and only if You agree to indemnify,
|
|
||||||
defend, and hold each Contributor harmless for any liability
|
|
||||||
incurred by, or claims asserted against, such Contributor by reason
|
|
||||||
of your accepting any such warranty or additional liability.
|
|
||||||
|
|
||||||
END OF TERMS AND CONDITIONS
|
|
||||||
|
|
||||||
APPENDIX: How to apply the Apache License to your work.
|
|
||||||
|
|
||||||
To apply the Apache License to your work, attach the following
|
|
||||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
||||||
replaced with your own identifying information. (Don't include
|
|
||||||
the brackets!) The text should be enclosed in the appropriate
|
|
||||||
comment syntax for the file format. We also recommend that a
|
|
||||||
file or class name and description of purpose be included on the
|
|
||||||
same "printed page" as the copyright notice for easier
|
|
||||||
identification within third-party archives.
|
|
||||||
|
|
||||||
Copyright [2021] [SwinIR Authors]
|
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
you may not use this file except in compliance with the License.
|
|
||||||
You may obtain a copy of the License at
|
|
||||||
|
|
||||||
http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
|
|
||||||
Unless required by applicable law or agreed to in writing, software
|
|
||||||
distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
See the License for the specific language governing permissions and
|
|
||||||
limitations under the License.
|
|
||||||
</pre>
|
|
||||||
|
|
||||||
<h2><a href="https://github.com/AminRezaei0x443/memory-efficient-attention/blob/main/LICENSE">Memory Efficient Attention</a></h2>
|
<h2><a href="https://github.com/AminRezaei0x443/memory-efficient-attention/blob/main/LICENSE">Memory Efficient Attention</a></h2>
|
||||||
<small>The sub-quadratic cross attention optimization uses modified code from the Memory Efficient Attention package that Alex Birch optimized for 3D tensors. This license is updated to reflect that.</small>
|
<small>The sub-quadratic cross attention optimization uses modified code from the Memory Efficient Attention package that Alex Birch optimized for 3D tensors. This license is updated to reflect that.</small>
|
||||||
<pre>
|
<pre>
|
||||||
@@ -687,4 +379,4 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
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
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
SOFTWARE.
|
SOFTWARE.
|
||||||
</pre>
|
</pre>
|
||||||
|
|||||||
@@ -1,108 +0,0 @@
|
|||||||
(function() {
|
|
||||||
var ignore = localStorage.getItem("bad-scale-ignore-it") == "ignore-it";
|
|
||||||
|
|
||||||
function getScale() {
|
|
||||||
var ratio = 0,
|
|
||||||
screen = window.screen,
|
|
||||||
ua = navigator.userAgent.toLowerCase();
|
|
||||||
|
|
||||||
if (window.devicePixelRatio !== undefined) {
|
|
||||||
ratio = window.devicePixelRatio;
|
|
||||||
} else if (~ua.indexOf('msie')) {
|
|
||||||
if (screen.deviceXDPI && screen.logicalXDPI) {
|
|
||||||
ratio = screen.deviceXDPI / screen.logicalXDPI;
|
|
||||||
}
|
|
||||||
} else if (window.outerWidth !== undefined && window.innerWidth !== undefined) {
|
|
||||||
ratio = window.outerWidth / window.innerWidth;
|
|
||||||
}
|
|
||||||
|
|
||||||
return ratio == 0 ? 0 : Math.round(ratio * 100);
|
|
||||||
}
|
|
||||||
|
|
||||||
var showing = false;
|
|
||||||
|
|
||||||
var div = document.createElement("div");
|
|
||||||
div.style.position = "fixed";
|
|
||||||
div.style.top = "0px";
|
|
||||||
div.style.left = "0px";
|
|
||||||
div.style.width = "100vw";
|
|
||||||
div.style.backgroundColor = "firebrick";
|
|
||||||
div.style.textAlign = "center";
|
|
||||||
div.style.zIndex = 99;
|
|
||||||
|
|
||||||
var b = document.createElement("b");
|
|
||||||
b.innerHTML = 'Bad Scale: ??% ';
|
|
||||||
|
|
||||||
div.appendChild(b);
|
|
||||||
|
|
||||||
var note1 = document.createElement("p");
|
|
||||||
note1.innerHTML = "Change your browser or your computer settings!";
|
|
||||||
note1.title = 'Just make sure "computer-scale" * "browser-scale" = 100% ,\n' +
|
|
||||||
"you can keep your computer-scale and only change this page's scale,\n" +
|
|
||||||
"for example: your computer-scale is 125%, just use [\"CTRL\"+\"-\"] to make your browser-scale of this page to 80%.";
|
|
||||||
div.appendChild(note1);
|
|
||||||
|
|
||||||
var note2 = document.createElement("p");
|
|
||||||
note2.innerHTML = " Otherwise, it will cause this page to not function properly!";
|
|
||||||
note2.title = "When you click \"Copy image to: [inpaint sketch]\" in some img2img's tab,\n" +
|
|
||||||
"if scale<100% the canvas will be invisible,\n" +
|
|
||||||
"else if scale>100% this page will take large amount of memory and CPU performance.";
|
|
||||||
div.appendChild(note2);
|
|
||||||
|
|
||||||
var btn = document.createElement("button");
|
|
||||||
btn.innerHTML = "Click here to ignore";
|
|
||||||
|
|
||||||
div.appendChild(btn);
|
|
||||||
|
|
||||||
function tryShowTopBar(scale) {
|
|
||||||
if (showing) return;
|
|
||||||
|
|
||||||
b.innerHTML = 'Bad Scale: ' + scale + '% ';
|
|
||||||
|
|
||||||
var updateScaleTimer = setInterval(function() {
|
|
||||||
var newScale = getScale();
|
|
||||||
b.innerHTML = 'Bad Scale: ' + newScale + '% ';
|
|
||||||
if (newScale == 100) {
|
|
||||||
var p = div.parentNode;
|
|
||||||
if (p != null) p.removeChild(div);
|
|
||||||
showing = false;
|
|
||||||
clearInterval(updateScaleTimer);
|
|
||||||
check();
|
|
||||||
}
|
|
||||||
}, 999);
|
|
||||||
|
|
||||||
btn.onclick = function() {
|
|
||||||
clearInterval(updateScaleTimer);
|
|
||||||
var p = div.parentNode;
|
|
||||||
if (p != null) p.removeChild(div);
|
|
||||||
ignore = true;
|
|
||||||
showing = false;
|
|
||||||
localStorage.setItem("bad-scale-ignore-it", "ignore-it");
|
|
||||||
};
|
|
||||||
|
|
||||||
document.body.appendChild(div);
|
|
||||||
}
|
|
||||||
|
|
||||||
function check() {
|
|
||||||
if (!ignore) {
|
|
||||||
var timer = setInterval(function() {
|
|
||||||
var scale = getScale();
|
|
||||||
if (scale != 100 && !ignore) {
|
|
||||||
tryShowTopBar(scale);
|
|
||||||
clearInterval(timer);
|
|
||||||
}
|
|
||||||
if (ignore) {
|
|
||||||
clearInterval(timer);
|
|
||||||
}
|
|
||||||
}, 999);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (document.readyState != "complete") {
|
|
||||||
document.onreadystatechange = function() {
|
|
||||||
if (document.readyState != "complete") check();
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
check();
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
Vendored
+1
-1
@@ -119,7 +119,7 @@ window.addEventListener('paste', e => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const firstFreeImageField = visibleImageFields
|
const firstFreeImageField = visibleImageFields
|
||||||
.filter(el => el.querySelector('input[type=file]'))?.[0];
|
.filter(el => !el.querySelector('img'))?.[0];
|
||||||
|
|
||||||
dropReplaceImage(
|
dropReplaceImage(
|
||||||
firstFreeImageField ?
|
firstFreeImageField ?
|
||||||
|
|||||||
@@ -18,37 +18,43 @@ function keyupEditAttention(event) {
|
|||||||
const before = text.substring(0, selectionStart);
|
const before = text.substring(0, selectionStart);
|
||||||
let beforeParen = before.lastIndexOf(OPEN);
|
let beforeParen = before.lastIndexOf(OPEN);
|
||||||
if (beforeParen == -1) return false;
|
if (beforeParen == -1) return false;
|
||||||
let beforeParenClose = before.lastIndexOf(CLOSE);
|
|
||||||
while (beforeParenClose !== -1 && beforeParenClose > beforeParen) {
|
let beforeClosingParen = before.lastIndexOf(CLOSE);
|
||||||
beforeParen = before.lastIndexOf(OPEN, beforeParen - 1);
|
if (beforeClosingParen != -1 && beforeClosingParen > beforeParen) return false;
|
||||||
beforeParenClose = before.lastIndexOf(CLOSE, beforeParenClose - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Find closing parenthesis around current cursor
|
// Find closing parenthesis around current cursor
|
||||||
const after = text.substring(selectionStart);
|
const after = text.substring(selectionStart);
|
||||||
let afterParen = after.indexOf(CLOSE);
|
let afterParen = after.indexOf(CLOSE);
|
||||||
if (afterParen == -1) return false;
|
if (afterParen == -1) return false;
|
||||||
let afterParenOpen = after.indexOf(OPEN);
|
|
||||||
while (afterParenOpen !== -1 && afterParen > afterParenOpen) {
|
let afterOpeningParen = after.indexOf(OPEN);
|
||||||
afterParen = after.indexOf(CLOSE, afterParen + 1);
|
if (afterOpeningParen != -1 && afterOpeningParen < afterParen) return false;
|
||||||
afterParenOpen = after.indexOf(OPEN, afterParenOpen + 1);
|
|
||||||
}
|
|
||||||
if (beforeParen === -1 || afterParen === -1) return false;
|
|
||||||
|
|
||||||
// Set the selection to the text between the parenthesis
|
// Set the selection to the text between the parenthesis
|
||||||
const parenContent = text.substring(beforeParen + 1, selectionStart + afterParen);
|
const parenContent = text.substring(beforeParen + 1, selectionStart + afterParen);
|
||||||
const lastColon = parenContent.lastIndexOf(":");
|
if (/.*:-?[\d.]+/s.test(parenContent)) {
|
||||||
selectionStart = beforeParen + 1;
|
const lastColon = parenContent.lastIndexOf(":");
|
||||||
selectionEnd = selectionStart + lastColon;
|
selectionStart = beforeParen + 1;
|
||||||
|
selectionEnd = selectionStart + lastColon;
|
||||||
|
} else {
|
||||||
|
selectionStart = beforeParen + 1;
|
||||||
|
selectionEnd = selectionStart + parenContent.length;
|
||||||
|
}
|
||||||
|
|
||||||
target.setSelectionRange(selectionStart, selectionEnd);
|
target.setSelectionRange(selectionStart, selectionEnd);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function selectCurrentWord() {
|
function selectCurrentWord() {
|
||||||
if (selectionStart !== selectionEnd) return false;
|
if (selectionStart !== selectionEnd) return false;
|
||||||
const delimiters = opts.keyedit_delimiters + " \r\n\t";
|
const whitespace_delimiters = {"Tab": "\t", "Carriage Return": "\r", "Line Feed": "\n"};
|
||||||
|
let delimiters = opts.keyedit_delimiters;
|
||||||
|
|
||||||
// seek backward until to find beggining
|
for (let i of opts.keyedit_delimiters_whitespace) {
|
||||||
|
delimiters += whitespace_delimiters[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// seek backward to find beginning
|
||||||
while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) {
|
while (!delimiters.includes(text[selectionStart - 1]) && selectionStart > 0) {
|
||||||
selectionStart--;
|
selectionStart--;
|
||||||
}
|
}
|
||||||
@@ -63,7 +69,7 @@ function keyupEditAttention(event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If the user hasn't selected anything, let's select their current parenthesis block or word
|
// If the user hasn't selected anything, let's select their current parenthesis block or word
|
||||||
if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')')) {
|
if (!selectCurrentParenthesisBlock('<', '>') && !selectCurrentParenthesisBlock('(', ')') && !selectCurrentParenthesisBlock('[', ']')) {
|
||||||
selectCurrentWord();
|
selectCurrentWord();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,33 +77,54 @@ function keyupEditAttention(event) {
|
|||||||
|
|
||||||
var closeCharacter = ')';
|
var closeCharacter = ')';
|
||||||
var delta = opts.keyedit_precision_attention;
|
var delta = opts.keyedit_precision_attention;
|
||||||
|
var start = selectionStart > 0 ? text[selectionStart - 1] : "";
|
||||||
|
var end = text[selectionEnd];
|
||||||
|
|
||||||
if (selectionStart > 0 && text[selectionStart - 1] == '<') {
|
if (start == '<') {
|
||||||
closeCharacter = '>';
|
closeCharacter = '>';
|
||||||
delta = opts.keyedit_precision_extra;
|
delta = opts.keyedit_precision_extra;
|
||||||
} else if (selectionStart == 0 || text[selectionStart - 1] != "(") {
|
} else if (start == '(' && end == ')' || start == '[' && end == ']') { // convert old-style (((emphasis)))
|
||||||
|
let numParen = 0;
|
||||||
|
|
||||||
|
while (text[selectionStart - numParen - 1] == start && text[selectionEnd + numParen] == end) {
|
||||||
|
numParen++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (start == "[") {
|
||||||
|
weight = (1 / 1.1) ** numParen;
|
||||||
|
} else {
|
||||||
|
weight = 1.1 ** numParen;
|
||||||
|
}
|
||||||
|
|
||||||
|
weight = Math.round(weight / opts.keyedit_precision_attention) * opts.keyedit_precision_attention;
|
||||||
|
|
||||||
|
text = text.slice(0, selectionStart - numParen) + "(" + text.slice(selectionStart, selectionEnd) + ":" + weight + ")" + text.slice(selectionEnd + numParen);
|
||||||
|
selectionStart -= numParen - 1;
|
||||||
|
selectionEnd -= numParen - 1;
|
||||||
|
} else if (start != '(') {
|
||||||
// do not include spaces at the end
|
// do not include spaces at the end
|
||||||
while (selectionEnd > selectionStart && text[selectionEnd - 1] == ' ') {
|
while (selectionEnd > selectionStart && text[selectionEnd - 1] == ' ') {
|
||||||
selectionEnd -= 1;
|
selectionEnd--;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectionStart == selectionEnd) {
|
if (selectionStart == selectionEnd) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
text = text.slice(0, selectionStart) + "(" + text.slice(selectionStart, selectionEnd) + ":1.0)" + text.slice(selectionEnd);
|
text = text.slice(0, selectionStart) + "(" + text.slice(selectionStart, selectionEnd) + ":1.0)" + text.slice(selectionEnd);
|
||||||
|
|
||||||
selectionStart += 1;
|
selectionStart++;
|
||||||
selectionEnd += 1;
|
selectionEnd++;
|
||||||
}
|
}
|
||||||
|
|
||||||
var end = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1;
|
if (text[selectionEnd] != ':') return;
|
||||||
var weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + 1 + end));
|
var weightLength = text.slice(selectionEnd + 1).indexOf(closeCharacter) + 1;
|
||||||
|
var weight = parseFloat(text.slice(selectionEnd + 1, selectionEnd + weightLength));
|
||||||
if (isNaN(weight)) return;
|
if (isNaN(weight)) return;
|
||||||
|
|
||||||
weight += isPlus ? delta : -delta;
|
weight += isPlus ? delta : -delta;
|
||||||
weight = parseFloat(weight.toPrecision(12));
|
weight = parseFloat(weight.toPrecision(12));
|
||||||
if (String(weight).length == 1) weight += ".0";
|
if (Number.isInteger(weight)) weight += ".0";
|
||||||
|
|
||||||
if (closeCharacter == ')' && weight == 1) {
|
if (closeCharacter == ')' && weight == 1) {
|
||||||
var endParenPos = text.substring(selectionEnd).indexOf(')');
|
var endParenPos = text.substring(selectionEnd).indexOf(')');
|
||||||
@@ -105,7 +132,7 @@ function keyupEditAttention(event) {
|
|||||||
selectionStart--;
|
selectionStart--;
|
||||||
selectionEnd--;
|
selectionEnd--;
|
||||||
} else {
|
} else {
|
||||||
text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + end);
|
text = text.slice(0, selectionEnd + 1) + weight + text.slice(selectionEnd + weightLength);
|
||||||
}
|
}
|
||||||
|
|
||||||
target.focus();
|
target.focus();
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ function extensions_check() {
|
|||||||
|
|
||||||
|
|
||||||
var id = randomId();
|
var id = randomId();
|
||||||
requestProgress(id, gradioApp().getElementById('extensions_installed_top'), null, function() {
|
requestProgress(id, gradioApp().getElementById('extensions_installed_html'), null, function() {
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
+136
-38
@@ -1,20 +1,39 @@
|
|||||||
|
function toggleCss(key, css, enable) {
|
||||||
|
var style = document.getElementById(key);
|
||||||
|
if (enable && !style) {
|
||||||
|
style = document.createElement('style');
|
||||||
|
style.id = key;
|
||||||
|
style.type = 'text/css';
|
||||||
|
document.head.appendChild(style);
|
||||||
|
}
|
||||||
|
if (style && !enable) {
|
||||||
|
document.head.removeChild(style);
|
||||||
|
}
|
||||||
|
if (style) {
|
||||||
|
style.innerHTML == '';
|
||||||
|
style.appendChild(document.createTextNode(css));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function setupExtraNetworksForTab(tabname) {
|
function setupExtraNetworksForTab(tabname) {
|
||||||
gradioApp().querySelector('#' + tabname + '_extra_tabs').classList.add('extra-networks');
|
gradioApp().querySelector('#' + tabname + '_extra_tabs').classList.add('extra-networks');
|
||||||
|
|
||||||
var tabs = gradioApp().querySelector('#' + tabname + '_extra_tabs > div');
|
var tabs = gradioApp().querySelector('#' + tabname + '_extra_tabs > div');
|
||||||
var search = gradioApp().querySelector('#' + tabname + '_extra_search textarea');
|
var searchDiv = gradioApp().getElementById(tabname + '_extra_search');
|
||||||
|
var search = searchDiv.querySelector('textarea');
|
||||||
var sort = gradioApp().getElementById(tabname + '_extra_sort');
|
var sort = gradioApp().getElementById(tabname + '_extra_sort');
|
||||||
var sortOrder = gradioApp().getElementById(tabname + '_extra_sortorder');
|
var sortOrder = gradioApp().getElementById(tabname + '_extra_sortorder');
|
||||||
var refresh = gradioApp().getElementById(tabname + '_extra_refresh');
|
var refresh = gradioApp().getElementById(tabname + '_extra_refresh');
|
||||||
|
var showDirsDiv = gradioApp().getElementById(tabname + '_extra_show_dirs');
|
||||||
|
var showDirs = gradioApp().querySelector('#' + tabname + '_extra_show_dirs input');
|
||||||
|
var promptContainer = gradioApp().querySelector('.prompt-container-compact#' + tabname + '_prompt_container');
|
||||||
|
var negativePrompt = gradioApp().querySelector('#' + tabname + '_neg_prompt');
|
||||||
|
|
||||||
search.classList.add('search');
|
tabs.appendChild(searchDiv);
|
||||||
sort.classList.add('sort');
|
|
||||||
sortOrder.classList.add('sortorder');
|
|
||||||
sort.dataset.sortkey = 'sortDefault';
|
|
||||||
tabs.appendChild(search);
|
|
||||||
tabs.appendChild(sort);
|
tabs.appendChild(sort);
|
||||||
tabs.appendChild(sortOrder);
|
tabs.appendChild(sortOrder);
|
||||||
tabs.appendChild(refresh);
|
tabs.appendChild(refresh);
|
||||||
|
tabs.appendChild(showDirsDiv);
|
||||||
|
|
||||||
var applyFilter = function() {
|
var applyFilter = function() {
|
||||||
var searchTerm = search.value.toLowerCase();
|
var searchTerm = search.value.toLowerCase();
|
||||||
@@ -31,20 +50,23 @@ function setupExtraNetworksForTab(tabname) {
|
|||||||
|
|
||||||
elem.style.display = visible ? "" : "none";
|
elem.style.display = visible ? "" : "none";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
applySort();
|
||||||
};
|
};
|
||||||
|
|
||||||
var applySort = function() {
|
var applySort = function() {
|
||||||
|
var cards = gradioApp().querySelectorAll('#' + tabname + '_extra_tabs div.card');
|
||||||
|
|
||||||
var reverse = sortOrder.classList.contains("sortReverse");
|
var reverse = sortOrder.classList.contains("sortReverse");
|
||||||
var sortKey = sort.querySelector("input").value.toLowerCase().replace("sort", "").replaceAll(" ", "_").replace(/_+$/, "").trim();
|
var sortKey = sort.querySelector("input").value.toLowerCase().replace("sort", "").replaceAll(" ", "_").replace(/_+$/, "").trim() || "name";
|
||||||
sortKey = sortKey ? "sort" + sortKey.charAt(0).toUpperCase() + sortKey.slice(1) : "";
|
sortKey = "sort" + sortKey.charAt(0).toUpperCase() + sortKey.slice(1);
|
||||||
var sortKeyStore = sortKey ? sortKey + (reverse ? "Reverse" : "") : "";
|
var sortKeyStore = sortKey + "-" + (reverse ? "Descending" : "Ascending") + "-" + cards.length;
|
||||||
if (!sortKey || sortKeyStore == sort.dataset.sortkey) {
|
|
||||||
|
if (sortKeyStore == sort.dataset.sortkey) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.dataset.sortkey = sortKeyStore;
|
sort.dataset.sortkey = sortKeyStore;
|
||||||
|
|
||||||
var cards = gradioApp().querySelectorAll('#' + tabname + '_extra_tabs div.card');
|
|
||||||
cards.forEach(function(card) {
|
cards.forEach(function(card) {
|
||||||
card.originalParentElement = card.parentElement;
|
card.originalParentElement = card.parentElement;
|
||||||
});
|
});
|
||||||
@@ -70,23 +92,70 @@ function setupExtraNetworksForTab(tabname) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
search.addEventListener("input", applyFilter);
|
search.addEventListener("input", applyFilter);
|
||||||
applyFilter();
|
|
||||||
["change", "blur", "click"].forEach(function(evt) {
|
|
||||||
sort.querySelector("input").addEventListener(evt, applySort);
|
|
||||||
});
|
|
||||||
sortOrder.addEventListener("click", function() {
|
sortOrder.addEventListener("click", function() {
|
||||||
sortOrder.classList.toggle("sortReverse");
|
sortOrder.classList.toggle("sortReverse");
|
||||||
applySort();
|
applySort();
|
||||||
});
|
});
|
||||||
|
applyFilter();
|
||||||
|
|
||||||
|
extraNetworksApplySort[tabname] = applySort;
|
||||||
extraNetworksApplyFilter[tabname] = applyFilter;
|
extraNetworksApplyFilter[tabname] = applyFilter;
|
||||||
|
|
||||||
|
var showDirsUpdate = function() {
|
||||||
|
var css = '#' + tabname + '_extra_tabs .extra-network-subdirs { display: none; }';
|
||||||
|
toggleCss(tabname + '_extra_show_dirs_style', css, !showDirs.checked);
|
||||||
|
localSet('extra-networks-show-dirs', showDirs.checked ? 1 : 0);
|
||||||
|
};
|
||||||
|
showDirs.checked = localGet('extra-networks-show-dirs', 1) == 1;
|
||||||
|
showDirs.addEventListener("change", showDirsUpdate);
|
||||||
|
showDirsUpdate();
|
||||||
|
}
|
||||||
|
|
||||||
|
function extraNetworksMovePromptToTab(tabname, id, showPrompt, showNegativePrompt) {
|
||||||
|
if (!gradioApp().querySelector('.toprow-compact-tools')) return; // only applicable for compact prompt layout
|
||||||
|
|
||||||
|
var promptContainer = gradioApp().getElementById(tabname + '_prompt_container');
|
||||||
|
var prompt = gradioApp().getElementById(tabname + '_prompt_row');
|
||||||
|
var negPrompt = gradioApp().getElementById(tabname + '_neg_prompt_row');
|
||||||
|
var elem = id ? gradioApp().getElementById(id) : null;
|
||||||
|
|
||||||
|
if (showNegativePrompt && elem) {
|
||||||
|
elem.insertBefore(negPrompt, elem.firstChild);
|
||||||
|
} else {
|
||||||
|
promptContainer.insertBefore(negPrompt, promptContainer.firstChild);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (showPrompt && elem) {
|
||||||
|
elem.insertBefore(prompt, elem.firstChild);
|
||||||
|
} else {
|
||||||
|
promptContainer.insertBefore(prompt, promptContainer.firstChild);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elem) {
|
||||||
|
elem.classList.toggle('extra-page-prompts-active', showNegativePrompt || showPrompt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function extraNetworksUrelatedTabSelected(tabname) { // called from python when user selects an unrelated tab (generate)
|
||||||
|
extraNetworksMovePromptToTab(tabname, '', false, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
function extraNetworksTabSelected(tabname, id, showPrompt, showNegativePrompt) { // called from python when user selects an extra networks tab
|
||||||
|
extraNetworksMovePromptToTab(tabname, id, showPrompt, showNegativePrompt);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyExtraNetworkFilter(tabname) {
|
function applyExtraNetworkFilter(tabname) {
|
||||||
setTimeout(extraNetworksApplyFilter[tabname], 1);
|
setTimeout(extraNetworksApplyFilter[tabname], 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function applyExtraNetworkSort(tabname) {
|
||||||
|
setTimeout(extraNetworksApplySort[tabname], 1);
|
||||||
|
}
|
||||||
|
|
||||||
var extraNetworksApplyFilter = {};
|
var extraNetworksApplyFilter = {};
|
||||||
|
var extraNetworksApplySort = {};
|
||||||
var activePromptTextarea = {};
|
var activePromptTextarea = {};
|
||||||
|
|
||||||
function setupExtraNetworks() {
|
function setupExtraNetworks() {
|
||||||
@@ -113,19 +182,22 @@ function setupExtraNetworks() {
|
|||||||
|
|
||||||
onUiLoaded(setupExtraNetworks);
|
onUiLoaded(setupExtraNetworks);
|
||||||
|
|
||||||
var re_extranet = /<([^:]+:[^:]+):[\d.]+>(.*)/;
|
var re_extranet = /<([^:^>]+:[^:]+):[\d.]+>(.*)/;
|
||||||
var re_extranet_g = /\s+<([^:]+:[^:]+):[\d.]+>/g;
|
var re_extranet_g = /<([^:^>]+:[^:]+):[\d.]+>/g;
|
||||||
|
|
||||||
function tryToRemoveExtraNetworkFromPrompt(textarea, text) {
|
var re_extranet_neg = /\(([^:^>]+:[\d.]+)\)/;
|
||||||
var m = text.match(re_extranet);
|
var re_extranet_g_neg = /\(([^:^>]+:[\d.]+)\)/g;
|
||||||
|
function tryToRemoveExtraNetworkFromPrompt(textarea, text, isNeg) {
|
||||||
|
var m = text.match(isNeg ? re_extranet_neg : re_extranet);
|
||||||
var replaced = false;
|
var replaced = false;
|
||||||
var newTextareaText;
|
var newTextareaText;
|
||||||
if (m) {
|
if (m) {
|
||||||
|
var extraTextBeforeNet = opts.extra_networks_add_text_separator;
|
||||||
var extraTextAfterNet = m[2];
|
var extraTextAfterNet = m[2];
|
||||||
var partToSearch = m[1];
|
var partToSearch = m[1];
|
||||||
var foundAtPosition = -1;
|
var foundAtPosition = -1;
|
||||||
newTextareaText = textarea.value.replaceAll(re_extranet_g, function(found, net, pos) {
|
newTextareaText = textarea.value.replaceAll(isNeg ? re_extranet_g_neg : re_extranet_g, function(found, net, pos) {
|
||||||
m = found.match(re_extranet);
|
m = found.match(isNeg ? re_extranet_neg : re_extranet);
|
||||||
if (m[1] == partToSearch) {
|
if (m[1] == partToSearch) {
|
||||||
replaced = true;
|
replaced = true;
|
||||||
foundAtPosition = pos;
|
foundAtPosition = pos;
|
||||||
@@ -134,8 +206,13 @@ function tryToRemoveExtraNetworkFromPrompt(textarea, text) {
|
|||||||
return found;
|
return found;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (foundAtPosition >= 0 && newTextareaText.substr(foundAtPosition, extraTextAfterNet.length) == extraTextAfterNet) {
|
if (foundAtPosition >= 0) {
|
||||||
newTextareaText = newTextareaText.substr(0, foundAtPosition) + newTextareaText.substr(foundAtPosition + extraTextAfterNet.length);
|
if (extraTextAfterNet && newTextareaText.substr(foundAtPosition, extraTextAfterNet.length) == extraTextAfterNet) {
|
||||||
|
newTextareaText = newTextareaText.substr(0, foundAtPosition) + newTextareaText.substr(foundAtPosition + extraTextAfterNet.length);
|
||||||
|
}
|
||||||
|
if (newTextareaText.substr(foundAtPosition - extraTextBeforeNet.length, extraTextBeforeNet.length) == extraTextBeforeNet) {
|
||||||
|
newTextareaText = newTextareaText.substr(0, foundAtPosition - extraTextBeforeNet.length) + newTextareaText.substr(foundAtPosition);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
newTextareaText = textarea.value.replaceAll(new RegExp(text, "g"), function(found) {
|
newTextareaText = textarea.value.replaceAll(new RegExp(text, "g"), function(found) {
|
||||||
@@ -155,14 +232,23 @@ function tryToRemoveExtraNetworkFromPrompt(textarea, text) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function cardClicked(tabname, textToAdd, allowNegativePrompt) {
|
function updatePromptArea(text, textArea, isNeg) {
|
||||||
var textarea = allowNegativePrompt ? activePromptTextarea[tabname] : gradioApp().querySelector("#" + tabname + "_prompt > label > textarea");
|
|
||||||
|
|
||||||
if (!tryToRemoveExtraNetworkFromPrompt(textarea, textToAdd)) {
|
if (!tryToRemoveExtraNetworkFromPrompt(textArea, text, isNeg)) {
|
||||||
textarea.value = textarea.value + opts.extra_networks_add_text_separator + textToAdd;
|
textArea.value = textArea.value + opts.extra_networks_add_text_separator + text;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateInput(textarea);
|
updateInput(textArea);
|
||||||
|
}
|
||||||
|
|
||||||
|
function cardClicked(tabname, textToAdd, textToAddNegative, allowNegativePrompt) {
|
||||||
|
if (textToAddNegative.length > 0) {
|
||||||
|
updatePromptArea(textToAdd, gradioApp().querySelector("#" + tabname + "_prompt > label > textarea"));
|
||||||
|
updatePromptArea(textToAddNegative, gradioApp().querySelector("#" + tabname + "_neg_prompt > label > textarea"), true);
|
||||||
|
} else {
|
||||||
|
var textarea = allowNegativePrompt ? activePromptTextarea[tabname] : gradioApp().querySelector("#" + tabname + "_prompt > label > textarea");
|
||||||
|
updatePromptArea(textToAdd, textarea);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveCardPreview(event, tabname, filename) {
|
function saveCardPreview(event, tabname, filename) {
|
||||||
@@ -179,7 +265,7 @@ function saveCardPreview(event, tabname, filename) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function extraNetworksSearchButton(tabs_id, event) {
|
function extraNetworksSearchButton(tabs_id, event) {
|
||||||
var searchTextarea = gradioApp().querySelector("#" + tabs_id + ' > div > textarea');
|
var searchTextarea = gradioApp().querySelector("#" + tabs_id + ' > label > textarea');
|
||||||
var button = event.target;
|
var button = event.target;
|
||||||
var text = button.classList.contains("search-all") ? "" : button.textContent.trim();
|
var text = button.classList.contains("search-all") ? "" : button.textContent.trim();
|
||||||
|
|
||||||
@@ -189,27 +275,24 @@ function extraNetworksSearchButton(tabs_id, event) {
|
|||||||
|
|
||||||
var globalPopup = null;
|
var globalPopup = null;
|
||||||
var globalPopupInner = null;
|
var globalPopupInner = null;
|
||||||
|
|
||||||
function closePopup() {
|
function closePopup() {
|
||||||
if (!globalPopup) return;
|
if (!globalPopup) return;
|
||||||
|
|
||||||
globalPopup.style.display = "none";
|
globalPopup.style.display = "none";
|
||||||
}
|
}
|
||||||
|
|
||||||
function popup(contents) {
|
function popup(contents) {
|
||||||
if (!globalPopup) {
|
if (!globalPopup) {
|
||||||
globalPopup = document.createElement('div');
|
globalPopup = document.createElement('div');
|
||||||
globalPopup.onclick = closePopup;
|
|
||||||
globalPopup.classList.add('global-popup');
|
globalPopup.classList.add('global-popup');
|
||||||
|
|
||||||
var close = document.createElement('div');
|
var close = document.createElement('div');
|
||||||
close.classList.add('global-popup-close');
|
close.classList.add('global-popup-close');
|
||||||
close.onclick = closePopup;
|
close.addEventListener("click", closePopup);
|
||||||
close.title = "Close";
|
close.title = "Close";
|
||||||
globalPopup.appendChild(close);
|
globalPopup.appendChild(close);
|
||||||
|
|
||||||
globalPopupInner = document.createElement('div');
|
globalPopupInner = document.createElement('div');
|
||||||
globalPopupInner.onclick = function(event) {
|
|
||||||
event.stopPropagation(); return false;
|
|
||||||
};
|
|
||||||
globalPopupInner.classList.add('global-popup-inner');
|
globalPopupInner.classList.add('global-popup-inner');
|
||||||
globalPopup.appendChild(globalPopupInner);
|
globalPopup.appendChild(globalPopupInner);
|
||||||
|
|
||||||
@@ -222,6 +305,15 @@ function popup(contents) {
|
|||||||
globalPopup.style.display = "flex";
|
globalPopup.style.display = "flex";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var storedPopupIds = {};
|
||||||
|
function popupId(id) {
|
||||||
|
if (!storedPopupIds[id]) {
|
||||||
|
storedPopupIds[id] = gradioApp().getElementById(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
popup(storedPopupIds[id]);
|
||||||
|
}
|
||||||
|
|
||||||
function extraNetworksShowMetadata(text) {
|
function extraNetworksShowMetadata(text) {
|
||||||
var elem = document.createElement('pre');
|
var elem = document.createElement('pre');
|
||||||
elem.classList.add('popup-metadata');
|
elem.classList.add('popup-metadata');
|
||||||
@@ -299,15 +391,21 @@ function extraNetworksEditUserMetadata(event, tabname, extraPage, cardName) {
|
|||||||
function extraNetworksRefreshSingleCard(page, tabname, name) {
|
function extraNetworksRefreshSingleCard(page, tabname, name) {
|
||||||
requestGet("./sd_extra_networks/get-single-card", {page: page, tabname: tabname, name: name}, function(data) {
|
requestGet("./sd_extra_networks/get-single-card", {page: page, tabname: tabname, name: name}, function(data) {
|
||||||
if (data && data.html) {
|
if (data && data.html) {
|
||||||
var card = gradioApp().querySelector('.card[data-name=' + JSON.stringify(name) + ']'); // likely using the wrong stringify function
|
var card = gradioApp().querySelector(`#${tabname}_${page.replace(" ", "_")}_cards > .card[data-name="${name}"]`);
|
||||||
|
|
||||||
var newDiv = document.createElement('DIV');
|
var newDiv = document.createElement('DIV');
|
||||||
newDiv.innerHTML = data.html;
|
newDiv.innerHTML = data.html;
|
||||||
var newCard = newDiv.firstElementChild;
|
var newCard = newDiv.firstElementChild;
|
||||||
|
|
||||||
newCard.style = '';
|
newCard.style.display = '';
|
||||||
card.parentElement.insertBefore(newCard, card);
|
card.parentElement.insertBefore(newCard, card);
|
||||||
card.parentElement.removeChild(card);
|
card.parentElement.removeChild(card);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
window.addEventListener("keydown", function(event) {
|
||||||
|
if (event.key == "Escape") {
|
||||||
|
closePopup();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -190,3 +190,14 @@ onUiUpdate(function(mutationRecords) {
|
|||||||
tooltipCheckTimer = setTimeout(processTooltipCheckNodes, 1000);
|
tooltipCheckTimer = setTimeout(processTooltipCheckNodes, 1000);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
onUiLoaded(function() {
|
||||||
|
for (var comp of window.gradio_config.components) {
|
||||||
|
if (comp.props.webui_tooltip && comp.props.elem_id) {
|
||||||
|
var elem = gradioApp().getElementById(comp.props.elem_id);
|
||||||
|
if (elem) {
|
||||||
|
elem.title = comp.props.webui_tooltip;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|||||||
@@ -33,8 +33,11 @@ function updateOnBackgroundChange() {
|
|||||||
const modalImage = gradioApp().getElementById("modalImage");
|
const modalImage = gradioApp().getElementById("modalImage");
|
||||||
if (modalImage && modalImage.offsetParent) {
|
if (modalImage && modalImage.offsetParent) {
|
||||||
let currentButton = selected_gallery_button();
|
let currentButton = selected_gallery_button();
|
||||||
|
let preview = gradioApp().querySelectorAll('.livePreview > img');
|
||||||
if (currentButton?.children?.length > 0 && modalImage.src != currentButton.children[0].src) {
|
if (opts.js_live_preview_in_modal_lightbox && preview.length > 0) {
|
||||||
|
// show preview image if available
|
||||||
|
modalImage.src = preview[preview.length - 1].src;
|
||||||
|
} else if (currentButton?.children?.length > 0 && modalImage.src != currentButton.children[0].src) {
|
||||||
modalImage.src = currentButton.children[0].src;
|
modalImage.src = currentButton.children[0].src;
|
||||||
if (modalImage.style.display === 'none') {
|
if (modalImage.style.display === 'none') {
|
||||||
const modal = gradioApp().getElementById("lightboxModal");
|
const modal = gradioApp().getElementById("lightboxModal");
|
||||||
@@ -136,6 +139,11 @@ function setupImageForLightbox(e) {
|
|||||||
var event = isFirefox ? 'mousedown' : 'click';
|
var event = isFirefox ? 'mousedown' : 'click';
|
||||||
|
|
||||||
e.addEventListener(event, function(evt) {
|
e.addEventListener(event, function(evt) {
|
||||||
|
if (evt.button == 1) {
|
||||||
|
open(evt.target.src);
|
||||||
|
evt.preventDefault();
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (!opts.js_modal_lightbox || evt.button != 0) return;
|
if (!opts.js_modal_lightbox || evt.button != 0) return;
|
||||||
|
|
||||||
modalZoomSet(gradioApp().getElementById('modalImage'), opts.js_modal_lightbox_initially_zoomed);
|
modalZoomSet(gradioApp().getElementById('modalImage'), opts.js_modal_lightbox_initially_zoomed);
|
||||||
|
|||||||
@@ -0,0 +1,68 @@
|
|||||||
|
function inputAccordionChecked(id, checked) {
|
||||||
|
var accordion = gradioApp().getElementById(id);
|
||||||
|
accordion.visibleCheckbox.checked = checked;
|
||||||
|
accordion.onVisibleCheckboxChange();
|
||||||
|
}
|
||||||
|
|
||||||
|
function setupAccordion(accordion) {
|
||||||
|
var labelWrap = accordion.querySelector('.label-wrap');
|
||||||
|
var gradioCheckbox = gradioApp().querySelector('#' + accordion.id + "-checkbox input");
|
||||||
|
var extra = gradioApp().querySelector('#' + accordion.id + "-extra");
|
||||||
|
var span = labelWrap.querySelector('span');
|
||||||
|
var linked = true;
|
||||||
|
|
||||||
|
var isOpen = function() {
|
||||||
|
return labelWrap.classList.contains('open');
|
||||||
|
};
|
||||||
|
|
||||||
|
var observerAccordionOpen = new MutationObserver(function(mutations) {
|
||||||
|
mutations.forEach(function(mutationRecord) {
|
||||||
|
accordion.classList.toggle('input-accordion-open', isOpen());
|
||||||
|
|
||||||
|
if (linked) {
|
||||||
|
accordion.visibleCheckbox.checked = isOpen();
|
||||||
|
accordion.onVisibleCheckboxChange();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
observerAccordionOpen.observe(labelWrap, {attributes: true, attributeFilter: ['class']});
|
||||||
|
|
||||||
|
if (extra) {
|
||||||
|
labelWrap.insertBefore(extra, labelWrap.lastElementChild);
|
||||||
|
}
|
||||||
|
|
||||||
|
accordion.onChecked = function(checked) {
|
||||||
|
if (isOpen() != checked) {
|
||||||
|
labelWrap.click();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var visibleCheckbox = document.createElement('INPUT');
|
||||||
|
visibleCheckbox.type = 'checkbox';
|
||||||
|
visibleCheckbox.checked = isOpen();
|
||||||
|
visibleCheckbox.id = accordion.id + "-visible-checkbox";
|
||||||
|
visibleCheckbox.className = gradioCheckbox.className + " input-accordion-checkbox";
|
||||||
|
span.insertBefore(visibleCheckbox, span.firstChild);
|
||||||
|
|
||||||
|
accordion.visibleCheckbox = visibleCheckbox;
|
||||||
|
accordion.onVisibleCheckboxChange = function() {
|
||||||
|
if (linked && isOpen() != visibleCheckbox.checked) {
|
||||||
|
labelWrap.click();
|
||||||
|
}
|
||||||
|
|
||||||
|
gradioCheckbox.checked = visibleCheckbox.checked;
|
||||||
|
updateInput(gradioCheckbox);
|
||||||
|
};
|
||||||
|
|
||||||
|
visibleCheckbox.addEventListener('click', function(event) {
|
||||||
|
linked = false;
|
||||||
|
event.stopPropagation();
|
||||||
|
});
|
||||||
|
visibleCheckbox.addEventListener('input', accordion.onVisibleCheckboxChange);
|
||||||
|
}
|
||||||
|
|
||||||
|
onUiLoaded(function() {
|
||||||
|
for (var accordion of gradioApp().querySelectorAll('.input-accordion')) {
|
||||||
|
setupAccordion(accordion);
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
function localSet(k, v) {
|
||||||
|
try {
|
||||||
|
localStorage.setItem(k, v);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(`Failed to save ${k} to localStorage: ${e}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function localGet(k, def) {
|
||||||
|
try {
|
||||||
|
return localStorage.getItem(k);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(`Failed to load ${k} from localStorage: ${e}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return def;
|
||||||
|
}
|
||||||
|
|
||||||
|
function localRemove(k) {
|
||||||
|
try {
|
||||||
|
return localStorage.removeItem(k);
|
||||||
|
} catch (e) {
|
||||||
|
console.warn(`Failed to remove ${k} from localStorage: ${e}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,11 +11,11 @@ var ignore_ids_for_localization = {
|
|||||||
train_hypernetwork: 'OPTION',
|
train_hypernetwork: 'OPTION',
|
||||||
txt2img_styles: 'OPTION',
|
txt2img_styles: 'OPTION',
|
||||||
img2img_styles: 'OPTION',
|
img2img_styles: 'OPTION',
|
||||||
setting_random_artist_categories: 'SPAN',
|
setting_random_artist_categories: 'OPTION',
|
||||||
setting_face_restoration_model: 'SPAN',
|
setting_face_restoration_model: 'OPTION',
|
||||||
setting_realesrgan_enabled_models: 'SPAN',
|
setting_realesrgan_enabled_models: 'OPTION',
|
||||||
extras_upscaler_1: 'SPAN',
|
extras_upscaler_1: 'OPTION',
|
||||||
extras_upscaler_2: 'SPAN',
|
extras_upscaler_2: 'OPTION',
|
||||||
};
|
};
|
||||||
|
|
||||||
var re_num = /^[.\d]+$/;
|
var re_num = /^[.\d]+$/;
|
||||||
@@ -107,12 +107,41 @@ function processNode(node) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function localizeWholePage() {
|
||||||
|
processNode(gradioApp());
|
||||||
|
|
||||||
|
function elem(comp) {
|
||||||
|
var elem_id = comp.props.elem_id ? comp.props.elem_id : "component-" + comp.id;
|
||||||
|
return gradioApp().getElementById(elem_id);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var comp of window.gradio_config.components) {
|
||||||
|
if (comp.props.webui_tooltip) {
|
||||||
|
let e = elem(comp);
|
||||||
|
|
||||||
|
let tl = e ? getTranslation(e.title) : undefined;
|
||||||
|
if (tl !== undefined) {
|
||||||
|
e.title = tl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (comp.props.placeholder) {
|
||||||
|
let e = elem(comp);
|
||||||
|
let textbox = e ? e.querySelector('[placeholder]') : null;
|
||||||
|
|
||||||
|
let tl = textbox ? getTranslation(textbox.placeholder) : undefined;
|
||||||
|
if (tl !== undefined) {
|
||||||
|
textbox.placeholder = tl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function dumpTranslations() {
|
function dumpTranslations() {
|
||||||
if (!hasLocalization()) {
|
if (!hasLocalization()) {
|
||||||
// If we don't have any localization,
|
// If we don't have any localization,
|
||||||
// we will not have traversed the app to find
|
// we will not have traversed the app to find
|
||||||
// original_lines, so do that now.
|
// original_lines, so do that now.
|
||||||
processNode(gradioApp());
|
localizeWholePage();
|
||||||
}
|
}
|
||||||
var dumped = {};
|
var dumped = {};
|
||||||
if (localization.rtl) {
|
if (localization.rtl) {
|
||||||
@@ -154,7 +183,7 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
processNode(gradioApp());
|
localizeWholePage();
|
||||||
|
|
||||||
if (localization.rtl) { // if the language is from right to left,
|
if (localization.rtl) { // if the language is from right to left,
|
||||||
(new MutationObserver((mutations, observer) => { // wait for the style to load
|
(new MutationObserver((mutations, observer) => { // wait for the style to load
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ onAfterUiUpdate(function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const galleryPreviews = gradioApp().querySelectorAll('div[id^="tab_"][style*="display: block"] div[id$="_results"] .thumbnail-item > img');
|
const galleryPreviews = gradioApp().querySelectorAll('div[id^="tab_"] div[id$="_results"] .thumbnail-item > img');
|
||||||
|
|
||||||
if (galleryPreviews == null) return;
|
if (galleryPreviews == null) return;
|
||||||
|
|
||||||
@@ -26,7 +26,11 @@ onAfterUiUpdate(function() {
|
|||||||
lastHeadImg = headImg;
|
lastHeadImg = headImg;
|
||||||
|
|
||||||
// play notification sound if available
|
// play notification sound if available
|
||||||
gradioApp().querySelector('#audio_notification audio')?.play();
|
const notificationAudio = gradioApp().querySelector('#audio_notification audio');
|
||||||
|
if (notificationAudio) {
|
||||||
|
notificationAudio.volume = opts.notification_volume / 100.0 || 1.0;
|
||||||
|
notificationAudio.play();
|
||||||
|
}
|
||||||
|
|
||||||
if (document.hasFocus()) return;
|
if (document.hasFocus()) return;
|
||||||
|
|
||||||
|
|||||||
+38
-29
@@ -69,7 +69,6 @@ function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgre
|
|||||||
var dateStart = new Date();
|
var dateStart = new Date();
|
||||||
var wasEverActive = false;
|
var wasEverActive = false;
|
||||||
var parentProgressbar = progressbarContainer.parentNode;
|
var parentProgressbar = progressbarContainer.parentNode;
|
||||||
var parentGallery = gallery ? gallery.parentNode : null;
|
|
||||||
|
|
||||||
var divProgress = document.createElement('div');
|
var divProgress = document.createElement('div');
|
||||||
divProgress.className = 'progressDiv';
|
divProgress.className = 'progressDiv';
|
||||||
@@ -80,32 +79,26 @@ function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgre
|
|||||||
divProgress.appendChild(divInner);
|
divProgress.appendChild(divInner);
|
||||||
parentProgressbar.insertBefore(divProgress, progressbarContainer);
|
parentProgressbar.insertBefore(divProgress, progressbarContainer);
|
||||||
|
|
||||||
if (parentGallery) {
|
var livePreview = null;
|
||||||
var livePreview = document.createElement('div');
|
|
||||||
livePreview.className = 'livePreview';
|
|
||||||
parentGallery.insertBefore(livePreview, gallery);
|
|
||||||
}
|
|
||||||
|
|
||||||
var removeProgressBar = function() {
|
var removeProgressBar = function() {
|
||||||
|
if (!divProgress) return;
|
||||||
|
|
||||||
setTitle("");
|
setTitle("");
|
||||||
parentProgressbar.removeChild(divProgress);
|
parentProgressbar.removeChild(divProgress);
|
||||||
if (parentGallery) parentGallery.removeChild(livePreview);
|
if (gallery && livePreview) gallery.removeChild(livePreview);
|
||||||
atEnd();
|
atEnd();
|
||||||
|
|
||||||
|
divProgress = null;
|
||||||
};
|
};
|
||||||
|
|
||||||
var fun = function(id_task, id_live_preview) {
|
var funProgress = function(id_task) {
|
||||||
request("./internal/progress", {id_task: id_task, id_live_preview: id_live_preview}, function(res) {
|
request("./internal/progress", {id_task: id_task, live_preview: false}, function(res) {
|
||||||
if (res.completed) {
|
if (res.completed) {
|
||||||
removeProgressBar();
|
removeProgressBar();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var rect = progressbarContainer.getBoundingClientRect();
|
|
||||||
|
|
||||||
if (rect.width) {
|
|
||||||
divProgress.style.width = rect.width + "px";
|
|
||||||
}
|
|
||||||
|
|
||||||
let progressText = "";
|
let progressText = "";
|
||||||
|
|
||||||
divInner.style.width = ((res.progress || 0) * 100.0) + '%';
|
divInner.style.width = ((res.progress || 0) * 100.0) + '%';
|
||||||
@@ -119,7 +112,6 @@ function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgre
|
|||||||
progressText += " ETA: " + formatTime(res.eta);
|
progressText += " ETA: " + formatTime(res.eta);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
setTitle(progressText);
|
setTitle(progressText);
|
||||||
|
|
||||||
if (res.textinfo && res.textinfo.indexOf("\n") == -1) {
|
if (res.textinfo && res.textinfo.indexOf("\n") == -1) {
|
||||||
@@ -142,16 +134,33 @@ function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgre
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (onProgress) {
|
||||||
|
onProgress(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
funProgress(id_task, res.id_live_preview);
|
||||||
|
}, opts.live_preview_refresh_period || 500);
|
||||||
|
}, function() {
|
||||||
|
removeProgressBar();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
var funLivePreview = function(id_task, id_live_preview) {
|
||||||
|
request("./internal/progress", {id_task: id_task, id_live_preview: id_live_preview}, function(res) {
|
||||||
|
if (!divProgress) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (res.live_preview && gallery) {
|
if (res.live_preview && gallery) {
|
||||||
rect = gallery.getBoundingClientRect();
|
|
||||||
if (rect.width) {
|
|
||||||
livePreview.style.width = rect.width + "px";
|
|
||||||
livePreview.style.height = rect.height + "px";
|
|
||||||
}
|
|
||||||
|
|
||||||
var img = new Image();
|
var img = new Image();
|
||||||
img.onload = function() {
|
img.onload = function() {
|
||||||
|
if (!livePreview) {
|
||||||
|
livePreview = document.createElement('div');
|
||||||
|
livePreview.className = 'livePreview';
|
||||||
|
gallery.insertBefore(livePreview, gallery.firstElementChild);
|
||||||
|
}
|
||||||
|
|
||||||
livePreview.appendChild(img);
|
livePreview.appendChild(img);
|
||||||
if (livePreview.childElementCount > 2) {
|
if (livePreview.childElementCount > 2) {
|
||||||
livePreview.removeChild(livePreview.firstElementChild);
|
livePreview.removeChild(livePreview.firstElementChild);
|
||||||
@@ -160,18 +169,18 @@ function requestProgress(id_task, progressbarContainer, gallery, atEnd, onProgre
|
|||||||
img.src = res.live_preview;
|
img.src = res.live_preview;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (onProgress) {
|
|
||||||
onProgress(res);
|
|
||||||
}
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
fun(id_task, res.id_live_preview);
|
funLivePreview(id_task, res.id_live_preview);
|
||||||
}, opts.live_preview_refresh_period || 500);
|
}, opts.live_preview_refresh_period || 500);
|
||||||
}, function() {
|
}, function() {
|
||||||
removeProgressBar();
|
removeProgressBar();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
fun(id_task, 0);
|
funProgress(id_task, 0);
|
||||||
|
|
||||||
|
if (gallery) {
|
||||||
|
funLivePreview(id_task, 0);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,141 @@
|
|||||||
|
(function() {
|
||||||
|
const GRADIO_MIN_WIDTH = 320;
|
||||||
|
const GRID_TEMPLATE_COLUMNS = '1fr 16px 1fr';
|
||||||
|
const PAD = 16;
|
||||||
|
const DEBOUNCE_TIME = 100;
|
||||||
|
|
||||||
|
const R = {
|
||||||
|
tracking: false,
|
||||||
|
parent: null,
|
||||||
|
parentWidth: null,
|
||||||
|
leftCol: null,
|
||||||
|
leftColStartWidth: null,
|
||||||
|
screenX: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
let resizeTimer;
|
||||||
|
let parents = [];
|
||||||
|
|
||||||
|
function setLeftColGridTemplate(el, width) {
|
||||||
|
el.style.gridTemplateColumns = `${width}px 16px 1fr`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function displayResizeHandle(parent) {
|
||||||
|
if (window.innerWidth < GRADIO_MIN_WIDTH * 2 + PAD * 4) {
|
||||||
|
parent.style.display = 'flex';
|
||||||
|
if (R.handle != null) {
|
||||||
|
R.handle.style.opacity = '0';
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
parent.style.display = 'grid';
|
||||||
|
if (R.handle != null) {
|
||||||
|
R.handle.style.opacity = '100';
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function afterResize(parent) {
|
||||||
|
if (displayResizeHandle(parent) && parent.style.gridTemplateColumns != GRID_TEMPLATE_COLUMNS) {
|
||||||
|
const oldParentWidth = R.parentWidth;
|
||||||
|
const newParentWidth = parent.offsetWidth;
|
||||||
|
const widthL = parseInt(parent.style.gridTemplateColumns.split(' ')[0]);
|
||||||
|
|
||||||
|
const ratio = newParentWidth / oldParentWidth;
|
||||||
|
|
||||||
|
const newWidthL = Math.max(Math.floor(ratio * widthL), GRADIO_MIN_WIDTH);
|
||||||
|
setLeftColGridTemplate(parent, newWidthL);
|
||||||
|
|
||||||
|
R.parentWidth = newParentWidth;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setup(parent) {
|
||||||
|
const leftCol = parent.firstElementChild;
|
||||||
|
const rightCol = parent.lastElementChild;
|
||||||
|
|
||||||
|
parents.push(parent);
|
||||||
|
|
||||||
|
parent.style.display = 'grid';
|
||||||
|
parent.style.gap = '0';
|
||||||
|
parent.style.gridTemplateColumns = GRID_TEMPLATE_COLUMNS;
|
||||||
|
|
||||||
|
const resizeHandle = document.createElement('div');
|
||||||
|
resizeHandle.classList.add('resize-handle');
|
||||||
|
parent.insertBefore(resizeHandle, rightCol);
|
||||||
|
|
||||||
|
resizeHandle.addEventListener('mousedown', (evt) => {
|
||||||
|
if (evt.button !== 0) return;
|
||||||
|
|
||||||
|
evt.preventDefault();
|
||||||
|
evt.stopPropagation();
|
||||||
|
|
||||||
|
document.body.classList.add('resizing');
|
||||||
|
|
||||||
|
R.tracking = true;
|
||||||
|
R.parent = parent;
|
||||||
|
R.parentWidth = parent.offsetWidth;
|
||||||
|
R.handle = resizeHandle;
|
||||||
|
R.leftCol = leftCol;
|
||||||
|
R.leftColStartWidth = leftCol.offsetWidth;
|
||||||
|
R.screenX = evt.screenX;
|
||||||
|
});
|
||||||
|
|
||||||
|
resizeHandle.addEventListener('dblclick', (evt) => {
|
||||||
|
evt.preventDefault();
|
||||||
|
evt.stopPropagation();
|
||||||
|
|
||||||
|
parent.style.gridTemplateColumns = GRID_TEMPLATE_COLUMNS;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterResize(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
window.addEventListener('mousemove', (evt) => {
|
||||||
|
if (evt.button !== 0) return;
|
||||||
|
|
||||||
|
if (R.tracking) {
|
||||||
|
evt.preventDefault();
|
||||||
|
evt.stopPropagation();
|
||||||
|
|
||||||
|
const delta = R.screenX - evt.screenX;
|
||||||
|
const leftColWidth = Math.max(Math.min(R.leftColStartWidth - delta, R.parent.offsetWidth - GRADIO_MIN_WIDTH - PAD), GRADIO_MIN_WIDTH);
|
||||||
|
setLeftColGridTemplate(R.parent, leftColWidth);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
window.addEventListener('mouseup', (evt) => {
|
||||||
|
if (evt.button !== 0) return;
|
||||||
|
|
||||||
|
if (R.tracking) {
|
||||||
|
evt.preventDefault();
|
||||||
|
evt.stopPropagation();
|
||||||
|
|
||||||
|
R.tracking = false;
|
||||||
|
|
||||||
|
document.body.classList.remove('resizing');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
window.addEventListener('resize', () => {
|
||||||
|
clearTimeout(resizeTimer);
|
||||||
|
|
||||||
|
resizeTimer = setTimeout(function() {
|
||||||
|
for (const parent of parents) {
|
||||||
|
afterResize(parent);
|
||||||
|
}
|
||||||
|
}, DEBOUNCE_TIME);
|
||||||
|
});
|
||||||
|
|
||||||
|
setupResizeHandle = setup;
|
||||||
|
})();
|
||||||
|
|
||||||
|
onUiLoaded(function() {
|
||||||
|
for (var elem of gradioApp().querySelectorAll('.resize-handle-row')) {
|
||||||
|
if (!elem.querySelector('.resize-handle')) {
|
||||||
|
setupResizeHandle(elem);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
let settingsExcludeTabsFromShowAll = {
|
||||||
|
settings_tab_defaults: 1,
|
||||||
|
settings_tab_sysinfo: 1,
|
||||||
|
settings_tab_actions: 1,
|
||||||
|
settings_tab_licenses: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
function settingsShowAllTabs() {
|
||||||
|
gradioApp().querySelectorAll('#settings > div').forEach(function(elem) {
|
||||||
|
if (settingsExcludeTabsFromShowAll[elem.id]) return;
|
||||||
|
|
||||||
|
elem.style.display = "block";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function settingsShowOneTab() {
|
||||||
|
gradioApp().querySelector('#settings_show_one_page').click();
|
||||||
|
}
|
||||||
|
|
||||||
|
onUiLoaded(function() {
|
||||||
|
var edit = gradioApp().querySelector('#settings_search');
|
||||||
|
var editTextarea = gradioApp().querySelector('#settings_search > label > input');
|
||||||
|
var buttonShowAllPages = gradioApp().getElementById('settings_show_all_pages');
|
||||||
|
var settings_tabs = gradioApp().querySelector('#settings div');
|
||||||
|
|
||||||
|
onEdit('settingsSearch', editTextarea, 250, function() {
|
||||||
|
var searchText = (editTextarea.value || "").trim().toLowerCase();
|
||||||
|
|
||||||
|
gradioApp().querySelectorAll('#settings > div[id^=settings_] div[id^=column_settings_] > *').forEach(function(elem) {
|
||||||
|
var visible = elem.textContent.trim().toLowerCase().indexOf(searchText) != -1;
|
||||||
|
elem.style.display = visible ? "" : "none";
|
||||||
|
});
|
||||||
|
|
||||||
|
if (searchText != "") {
|
||||||
|
settingsShowAllTabs();
|
||||||
|
} else {
|
||||||
|
settingsShowOneTab();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
settings_tabs.insertBefore(edit, settings_tabs.firstChild);
|
||||||
|
settings_tabs.appendChild(buttonShowAllPages);
|
||||||
|
|
||||||
|
|
||||||
|
buttonShowAllPages.addEventListener("click", settingsShowAllTabs);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
onOptionsChanged(function() {
|
||||||
|
if (gradioApp().querySelector('#settings .settings-category')) return;
|
||||||
|
|
||||||
|
var sectionMap = {};
|
||||||
|
gradioApp().querySelectorAll('#settings > div > button').forEach(function(x) {
|
||||||
|
sectionMap[x.textContent.trim()] = x;
|
||||||
|
});
|
||||||
|
|
||||||
|
opts._categories.forEach(function(x) {
|
||||||
|
var section = x[0];
|
||||||
|
var category = x[1];
|
||||||
|
|
||||||
|
var span = document.createElement('SPAN');
|
||||||
|
span.textContent = category;
|
||||||
|
span.className = 'settings-category';
|
||||||
|
|
||||||
|
var sectionElem = sectionMap[section];
|
||||||
|
if (!sectionElem) return;
|
||||||
|
|
||||||
|
sectionElem.parentElement.insertBefore(span, sectionElem);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
let promptTokenCountDebounceTime = 800;
|
let promptTokenCountUpdateFunctions = {};
|
||||||
let promptTokenCountTimeouts = {};
|
|
||||||
var promptTokenCountUpdateFunctions = {};
|
|
||||||
|
|
||||||
function update_txt2img_tokens(...args) {
|
function update_txt2img_tokens(...args) {
|
||||||
// Called from Gradio
|
// Called from Gradio
|
||||||
update_token_counter("txt2img_token_button");
|
update_token_counter("txt2img_token_button");
|
||||||
|
update_token_counter("txt2img_negative_token_button");
|
||||||
if (args.length == 2) {
|
if (args.length == 2) {
|
||||||
return args[0];
|
return args[0];
|
||||||
}
|
}
|
||||||
@@ -14,6 +13,7 @@ function update_txt2img_tokens(...args) {
|
|||||||
function update_img2img_tokens(...args) {
|
function update_img2img_tokens(...args) {
|
||||||
// Called from Gradio
|
// Called from Gradio
|
||||||
update_token_counter("img2img_token_button");
|
update_token_counter("img2img_token_button");
|
||||||
|
update_token_counter("img2img_negative_token_button");
|
||||||
if (args.length == 2) {
|
if (args.length == 2) {
|
||||||
return args[0];
|
return args[0];
|
||||||
}
|
}
|
||||||
@@ -21,16 +21,7 @@ function update_img2img_tokens(...args) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function update_token_counter(button_id) {
|
function update_token_counter(button_id) {
|
||||||
if (opts.disable_token_counters) {
|
promptTokenCountUpdateFunctions[button_id]?.();
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (promptTokenCountTimeouts[button_id]) {
|
|
||||||
clearTimeout(promptTokenCountTimeouts[button_id]);
|
|
||||||
}
|
|
||||||
promptTokenCountTimeouts[button_id] = setTimeout(
|
|
||||||
() => gradioApp().getElementById(button_id)?.click(),
|
|
||||||
promptTokenCountDebounceTime,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -69,10 +60,11 @@ function setupTokenCounting(id, id_counter, id_button) {
|
|||||||
prompt.parentElement.insertBefore(counter, prompt);
|
prompt.parentElement.insertBefore(counter, prompt);
|
||||||
prompt.parentElement.style.position = "relative";
|
prompt.parentElement.style.position = "relative";
|
||||||
|
|
||||||
promptTokenCountUpdateFunctions[id] = function() {
|
var func = onEdit(id, textarea, 800, function() {
|
||||||
update_token_counter(id_button);
|
gradioApp().getElementById(id_button)?.click();
|
||||||
};
|
});
|
||||||
textarea.addEventListener("input", promptTokenCountUpdateFunctions[id]);
|
promptTokenCountUpdateFunctions[id] = func;
|
||||||
|
promptTokenCountUpdateFunctions[id_button] = func;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupTokenCounters() {
|
function setupTokenCounters() {
|
||||||
|
|||||||
+76
-44
@@ -19,28 +19,11 @@ function all_gallery_buttons() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function selected_gallery_button() {
|
function selected_gallery_button() {
|
||||||
var allCurrentButtons = gradioApp().querySelectorAll('[style="display: block;"].tabitem div[id$=_gallery].gradio-gallery .thumbnail-item.thumbnail-small.selected');
|
return all_gallery_buttons().find(elem => elem.classList.contains('selected')) ?? null;
|
||||||
var visibleCurrentButton = null;
|
|
||||||
allCurrentButtons.forEach(function(elem) {
|
|
||||||
if (elem.parentElement.offsetParent) {
|
|
||||||
visibleCurrentButton = elem;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return visibleCurrentButton;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function selected_gallery_index() {
|
function selected_gallery_index() {
|
||||||
var buttons = all_gallery_buttons();
|
return all_gallery_buttons().findIndex(elem => elem.classList.contains('selected'));
|
||||||
var button = selected_gallery_button();
|
|
||||||
|
|
||||||
var result = -1;
|
|
||||||
buttons.forEach(function(v, i) {
|
|
||||||
if (v == button) {
|
|
||||||
result = i;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function extract_image_from_gallery(gallery) {
|
function extract_image_from_gallery(gallery) {
|
||||||
@@ -152,11 +135,11 @@ function submit() {
|
|||||||
showSubmitButtons('txt2img', false);
|
showSubmitButtons('txt2img', false);
|
||||||
|
|
||||||
var id = randomId();
|
var id = randomId();
|
||||||
localStorage.setItem("txt2img_task_id", id);
|
localSet("txt2img_task_id", id);
|
||||||
|
|
||||||
requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function() {
|
requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function() {
|
||||||
showSubmitButtons('txt2img', true);
|
showSubmitButtons('txt2img', true);
|
||||||
localStorage.removeItem("txt2img_task_id");
|
localRemove("txt2img_task_id");
|
||||||
showRestoreProgressButton('txt2img', false);
|
showRestoreProgressButton('txt2img', false);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -167,15 +150,23 @@ function submit() {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function submit_txt2img_upscale() {
|
||||||
|
var res = submit(...arguments);
|
||||||
|
|
||||||
|
res[2] = selected_gallery_index();
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
function submit_img2img() {
|
function submit_img2img() {
|
||||||
showSubmitButtons('img2img', false);
|
showSubmitButtons('img2img', false);
|
||||||
|
|
||||||
var id = randomId();
|
var id = randomId();
|
||||||
localStorage.setItem("img2img_task_id", id);
|
localSet("img2img_task_id", id);
|
||||||
|
|
||||||
requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function() {
|
requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function() {
|
||||||
showSubmitButtons('img2img', true);
|
showSubmitButtons('img2img', true);
|
||||||
localStorage.removeItem("img2img_task_id");
|
localRemove("img2img_task_id");
|
||||||
showRestoreProgressButton('img2img', false);
|
showRestoreProgressButton('img2img', false);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -187,11 +178,26 @@ function submit_img2img() {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function submit_extras() {
|
||||||
|
showSubmitButtons('extras', false);
|
||||||
|
|
||||||
|
var id = randomId();
|
||||||
|
|
||||||
|
requestProgress(id, gradioApp().getElementById('extras_gallery_container'), gradioApp().getElementById('extras_gallery'), function() {
|
||||||
|
showSubmitButtons('extras', true);
|
||||||
|
});
|
||||||
|
|
||||||
|
var res = create_submit_args(arguments);
|
||||||
|
|
||||||
|
res[0] = id;
|
||||||
|
|
||||||
|
console.log(res);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
function restoreProgressTxt2img() {
|
function restoreProgressTxt2img() {
|
||||||
showRestoreProgressButton("txt2img", false);
|
showRestoreProgressButton("txt2img", false);
|
||||||
var id = localStorage.getItem("txt2img_task_id");
|
var id = localGet("txt2img_task_id");
|
||||||
|
|
||||||
id = localStorage.getItem("txt2img_task_id");
|
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function() {
|
requestProgress(id, gradioApp().getElementById('txt2img_gallery_container'), gradioApp().getElementById('txt2img_gallery'), function() {
|
||||||
@@ -205,7 +211,7 @@ function restoreProgressTxt2img() {
|
|||||||
function restoreProgressImg2img() {
|
function restoreProgressImg2img() {
|
||||||
showRestoreProgressButton("img2img", false);
|
showRestoreProgressButton("img2img", false);
|
||||||
|
|
||||||
var id = localStorage.getItem("img2img_task_id");
|
var id = localGet("img2img_task_id");
|
||||||
|
|
||||||
if (id) {
|
if (id) {
|
||||||
requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function() {
|
requestProgress(id, gradioApp().getElementById('img2img_gallery_container'), gradioApp().getElementById('img2img_gallery'), function() {
|
||||||
@@ -217,9 +223,33 @@ function restoreProgressImg2img() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configure the width and height elements on `tabname` to accept
|
||||||
|
* pasting of resolutions in the form of "width x height".
|
||||||
|
*/
|
||||||
|
function setupResolutionPasting(tabname) {
|
||||||
|
var width = gradioApp().querySelector(`#${tabname}_width input[type=number]`);
|
||||||
|
var height = gradioApp().querySelector(`#${tabname}_height input[type=number]`);
|
||||||
|
for (const el of [width, height]) {
|
||||||
|
el.addEventListener('paste', function(event) {
|
||||||
|
var pasteData = event.clipboardData.getData('text/plain');
|
||||||
|
var parsed = pasteData.match(/^\s*(\d+)\D+(\d+)\s*$/);
|
||||||
|
if (parsed) {
|
||||||
|
width.value = parsed[1];
|
||||||
|
height.value = parsed[2];
|
||||||
|
updateInput(width);
|
||||||
|
updateInput(height);
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onUiLoaded(function() {
|
onUiLoaded(function() {
|
||||||
showRestoreProgressButton('txt2img', localStorage.getItem("txt2img_task_id"));
|
showRestoreProgressButton('txt2img', localGet("txt2img_task_id"));
|
||||||
showRestoreProgressButton('img2img', localStorage.getItem("img2img_task_id"));
|
showRestoreProgressButton('img2img', localGet("img2img_task_id"));
|
||||||
|
setupResolutionPasting('txt2img');
|
||||||
|
setupResolutionPasting('img2img');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
@@ -282,21 +312,6 @@ onAfterUiUpdate(function() {
|
|||||||
json_elem.parentElement.style.display = "none";
|
json_elem.parentElement.style.display = "none";
|
||||||
|
|
||||||
setupTokenCounters();
|
setupTokenCounters();
|
||||||
|
|
||||||
var show_all_pages = gradioApp().getElementById('settings_show_all_pages');
|
|
||||||
var settings_tabs = gradioApp().querySelector('#settings div');
|
|
||||||
if (show_all_pages && settings_tabs) {
|
|
||||||
settings_tabs.appendChild(show_all_pages);
|
|
||||||
show_all_pages.onclick = function() {
|
|
||||||
gradioApp().querySelectorAll('#settings > div').forEach(function(elem) {
|
|
||||||
if (elem.id == "settings_tab_licenses") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
elem.style.display = "block";
|
|
||||||
});
|
|
||||||
};
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
onOptionsChanged(function() {
|
onOptionsChanged(function() {
|
||||||
@@ -385,3 +400,20 @@ function switchWidthHeight(tabname) {
|
|||||||
updateInput(height);
|
updateInput(height);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var onEditTimers = {};
|
||||||
|
|
||||||
|
// calls func after afterMs milliseconds has passed since the input elem has beed enited by user
|
||||||
|
function onEdit(editId, elem, afterMs, func) {
|
||||||
|
var edited = function() {
|
||||||
|
var existingTimer = onEditTimers[editId];
|
||||||
|
if (existingTimer) clearTimeout(existingTimer);
|
||||||
|
|
||||||
|
onEditTimers[editId] = setTimeout(func, afterMs);
|
||||||
|
};
|
||||||
|
|
||||||
|
elem.addEventListener("input", edited);
|
||||||
|
|
||||||
|
return edited;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
from modules import launch_utils
|
from modules import launch_utils
|
||||||
|
|
||||||
|
|
||||||
args = launch_utils.args
|
args = launch_utils.args
|
||||||
python = launch_utils.python
|
python = launch_utils.python
|
||||||
git = launch_utils.git
|
git = launch_utils.git
|
||||||
@@ -26,8 +25,18 @@ start = launch_utils.start
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if not args.skip_prepare_environment:
|
if args.dump_sysinfo:
|
||||||
prepare_environment()
|
filename = launch_utils.dump_sysinfo()
|
||||||
|
|
||||||
|
print(f"Sysinfo saved as {filename}. Exiting...")
|
||||||
|
|
||||||
|
exit(0)
|
||||||
|
|
||||||
|
launch_utils.startup_timer.record("initial startup")
|
||||||
|
|
||||||
|
with launch_utils.startup_timer.subcategory("prepare environment"):
|
||||||
|
if not args.skip_prepare_environment:
|
||||||
|
prepare_environment()
|
||||||
|
|
||||||
if args.test_server:
|
if args.test_server:
|
||||||
configure_for_tests()
|
configure_for_tests()
|
||||||
|
|||||||
+220
-59
@@ -4,6 +4,8 @@ import os
|
|||||||
import time
|
import time
|
||||||
import datetime
|
import datetime
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
import ipaddress
|
||||||
|
import requests
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
from threading import Lock
|
from threading import Lock
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
@@ -15,24 +17,21 @@ from fastapi.encoders import jsonable_encoder
|
|||||||
from secrets import compare_digest
|
from secrets import compare_digest
|
||||||
|
|
||||||
import modules.shared as shared
|
import modules.shared as shared
|
||||||
from modules import sd_samplers, deepbooru, sd_hijack, images, scripts, ui, postprocessing, errors, restart
|
from modules import sd_samplers, deepbooru, sd_hijack, images, scripts, ui, postprocessing, errors, restart, shared_items, script_callbacks, infotext_utils, sd_models
|
||||||
from modules.api import models
|
from modules.api import models
|
||||||
from modules.shared import opts
|
from modules.shared import opts
|
||||||
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img, process_images
|
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img, process_images
|
||||||
from modules.textual_inversion.textual_inversion import create_embedding, train_embedding
|
from modules.textual_inversion.textual_inversion import create_embedding, train_embedding
|
||||||
from modules.textual_inversion.preprocess import preprocess
|
|
||||||
from modules.hypernetworks.hypernetwork import create_hypernetwork, train_hypernetwork
|
from modules.hypernetworks.hypernetwork import create_hypernetwork, train_hypernetwork
|
||||||
from PIL import PngImagePlugin,Image
|
from PIL import PngImagePlugin, Image
|
||||||
from modules.sd_models import checkpoints_list, unload_model_weights, reload_model_weights, checkpoint_aliases
|
|
||||||
from modules.sd_vae import vae_dict
|
|
||||||
from modules.sd_models_config import find_checkpoint_config_near_filename
|
from modules.sd_models_config import find_checkpoint_config_near_filename
|
||||||
from modules.realesrgan_model import get_realesrgan_models
|
from modules.realesrgan_model import get_realesrgan_models
|
||||||
from modules import devices
|
from modules import devices
|
||||||
from typing import Dict, List, Any
|
from typing import Any
|
||||||
import piexif
|
import piexif
|
||||||
import piexif.helper
|
import piexif.helper
|
||||||
from contextlib import closing
|
from contextlib import closing
|
||||||
|
from modules.progress import create_task_id, add_task_to_queue, start_task, finish_task, current_task
|
||||||
|
|
||||||
def script_name_to_index(name, scripts):
|
def script_name_to_index(name, scripts):
|
||||||
try:
|
try:
|
||||||
@@ -56,7 +55,41 @@ def setUpscalers(req: dict):
|
|||||||
return reqDict
|
return reqDict
|
||||||
|
|
||||||
|
|
||||||
|
def verify_url(url):
|
||||||
|
"""Returns True if the url refers to a global resource."""
|
||||||
|
|
||||||
|
import socket
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
try:
|
||||||
|
parsed_url = urlparse(url)
|
||||||
|
domain_name = parsed_url.netloc
|
||||||
|
host = socket.gethostbyname_ex(domain_name)
|
||||||
|
for ip in host[2]:
|
||||||
|
ip_addr = ipaddress.ip_address(ip)
|
||||||
|
if not ip_addr.is_global:
|
||||||
|
return False
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def decode_base64_to_image(encoding):
|
def decode_base64_to_image(encoding):
|
||||||
|
if encoding.startswith("http://") or encoding.startswith("https://"):
|
||||||
|
if not opts.api_enable_requests:
|
||||||
|
raise HTTPException(status_code=500, detail="Requests not allowed")
|
||||||
|
|
||||||
|
if opts.api_forbid_local_requests and not verify_url(encoding):
|
||||||
|
raise HTTPException(status_code=500, detail="Request to local resource not allowed")
|
||||||
|
|
||||||
|
headers = {'user-agent': opts.api_useragent} if opts.api_useragent else {}
|
||||||
|
response = requests.get(encoding, timeout=30, headers=headers)
|
||||||
|
try:
|
||||||
|
image = Image.open(BytesIO(response.content))
|
||||||
|
return image
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail="Invalid image url") from e
|
||||||
|
|
||||||
if encoding.startswith("data:image/"):
|
if encoding.startswith("data:image/"):
|
||||||
encoding = encoding.split(";")[1].split(",")[1]
|
encoding = encoding.split(";")[1].split(",")[1]
|
||||||
try:
|
try:
|
||||||
@@ -68,7 +101,8 @@ def decode_base64_to_image(encoding):
|
|||||||
|
|
||||||
def encode_pil_to_base64(image):
|
def encode_pil_to_base64(image):
|
||||||
with io.BytesIO() as output_bytes:
|
with io.BytesIO() as output_bytes:
|
||||||
|
if isinstance(image, str):
|
||||||
|
return image
|
||||||
if opts.samples_format.lower() == 'png':
|
if opts.samples_format.lower() == 'png':
|
||||||
use_metadata = False
|
use_metadata = False
|
||||||
metadata = PngImagePlugin.PngInfo()
|
metadata = PngImagePlugin.PngInfo()
|
||||||
@@ -186,27 +220,28 @@ class Api:
|
|||||||
self.add_api_route("/sdapi/v1/options", self.get_config, methods=["GET"], response_model=models.OptionsModel)
|
self.add_api_route("/sdapi/v1/options", self.get_config, methods=["GET"], response_model=models.OptionsModel)
|
||||||
self.add_api_route("/sdapi/v1/options", self.set_config, methods=["POST"])
|
self.add_api_route("/sdapi/v1/options", self.set_config, methods=["POST"])
|
||||||
self.add_api_route("/sdapi/v1/cmd-flags", self.get_cmd_flags, methods=["GET"], response_model=models.FlagsModel)
|
self.add_api_route("/sdapi/v1/cmd-flags", self.get_cmd_flags, methods=["GET"], response_model=models.FlagsModel)
|
||||||
self.add_api_route("/sdapi/v1/samplers", self.get_samplers, methods=["GET"], response_model=List[models.SamplerItem])
|
self.add_api_route("/sdapi/v1/samplers", self.get_samplers, methods=["GET"], response_model=list[models.SamplerItem])
|
||||||
self.add_api_route("/sdapi/v1/upscalers", self.get_upscalers, methods=["GET"], response_model=List[models.UpscalerItem])
|
self.add_api_route("/sdapi/v1/upscalers", self.get_upscalers, methods=["GET"], response_model=list[models.UpscalerItem])
|
||||||
self.add_api_route("/sdapi/v1/latent-upscale-modes", self.get_latent_upscale_modes, methods=["GET"], response_model=List[models.LatentUpscalerModeItem])
|
self.add_api_route("/sdapi/v1/latent-upscale-modes", self.get_latent_upscale_modes, methods=["GET"], response_model=list[models.LatentUpscalerModeItem])
|
||||||
self.add_api_route("/sdapi/v1/sd-models", self.get_sd_models, methods=["GET"], response_model=List[models.SDModelItem])
|
self.add_api_route("/sdapi/v1/sd-models", self.get_sd_models, methods=["GET"], response_model=list[models.SDModelItem])
|
||||||
self.add_api_route("/sdapi/v1/sd-vae", self.get_sd_vaes, methods=["GET"], response_model=List[models.SDVaeItem])
|
self.add_api_route("/sdapi/v1/sd-vae", self.get_sd_vaes, methods=["GET"], response_model=list[models.SDVaeItem])
|
||||||
self.add_api_route("/sdapi/v1/hypernetworks", self.get_hypernetworks, methods=["GET"], response_model=List[models.HypernetworkItem])
|
self.add_api_route("/sdapi/v1/hypernetworks", self.get_hypernetworks, methods=["GET"], response_model=list[models.HypernetworkItem])
|
||||||
self.add_api_route("/sdapi/v1/face-restorers", self.get_face_restorers, methods=["GET"], response_model=List[models.FaceRestorerItem])
|
self.add_api_route("/sdapi/v1/face-restorers", self.get_face_restorers, methods=["GET"], response_model=list[models.FaceRestorerItem])
|
||||||
self.add_api_route("/sdapi/v1/realesrgan-models", self.get_realesrgan_models, methods=["GET"], response_model=List[models.RealesrganItem])
|
self.add_api_route("/sdapi/v1/realesrgan-models", self.get_realesrgan_models, methods=["GET"], response_model=list[models.RealesrganItem])
|
||||||
self.add_api_route("/sdapi/v1/prompt-styles", self.get_prompt_styles, methods=["GET"], response_model=List[models.PromptStyleItem])
|
self.add_api_route("/sdapi/v1/prompt-styles", self.get_prompt_styles, methods=["GET"], response_model=list[models.PromptStyleItem])
|
||||||
self.add_api_route("/sdapi/v1/embeddings", self.get_embeddings, methods=["GET"], response_model=models.EmbeddingsResponse)
|
self.add_api_route("/sdapi/v1/embeddings", self.get_embeddings, methods=["GET"], response_model=models.EmbeddingsResponse)
|
||||||
self.add_api_route("/sdapi/v1/refresh-checkpoints", self.refresh_checkpoints, methods=["POST"])
|
self.add_api_route("/sdapi/v1/refresh-checkpoints", self.refresh_checkpoints, methods=["POST"])
|
||||||
|
self.add_api_route("/sdapi/v1/refresh-vae", self.refresh_vae, methods=["POST"])
|
||||||
self.add_api_route("/sdapi/v1/create/embedding", self.create_embedding, methods=["POST"], response_model=models.CreateResponse)
|
self.add_api_route("/sdapi/v1/create/embedding", self.create_embedding, methods=["POST"], response_model=models.CreateResponse)
|
||||||
self.add_api_route("/sdapi/v1/create/hypernetwork", self.create_hypernetwork, methods=["POST"], response_model=models.CreateResponse)
|
self.add_api_route("/sdapi/v1/create/hypernetwork", self.create_hypernetwork, methods=["POST"], response_model=models.CreateResponse)
|
||||||
self.add_api_route("/sdapi/v1/preprocess", self.preprocess, methods=["POST"], response_model=models.PreprocessResponse)
|
|
||||||
self.add_api_route("/sdapi/v1/train/embedding", self.train_embedding, methods=["POST"], response_model=models.TrainResponse)
|
self.add_api_route("/sdapi/v1/train/embedding", self.train_embedding, methods=["POST"], response_model=models.TrainResponse)
|
||||||
self.add_api_route("/sdapi/v1/train/hypernetwork", self.train_hypernetwork, methods=["POST"], response_model=models.TrainResponse)
|
self.add_api_route("/sdapi/v1/train/hypernetwork", self.train_hypernetwork, methods=["POST"], response_model=models.TrainResponse)
|
||||||
self.add_api_route("/sdapi/v1/memory", self.get_memory, methods=["GET"], response_model=models.MemoryResponse)
|
self.add_api_route("/sdapi/v1/memory", self.get_memory, methods=["GET"], response_model=models.MemoryResponse)
|
||||||
self.add_api_route("/sdapi/v1/unload-checkpoint", self.unloadapi, methods=["POST"])
|
self.add_api_route("/sdapi/v1/unload-checkpoint", self.unloadapi, methods=["POST"])
|
||||||
self.add_api_route("/sdapi/v1/reload-checkpoint", self.reloadapi, methods=["POST"])
|
self.add_api_route("/sdapi/v1/reload-checkpoint", self.reloadapi, methods=["POST"])
|
||||||
self.add_api_route("/sdapi/v1/scripts", self.get_scripts_list, methods=["GET"], response_model=models.ScriptsList)
|
self.add_api_route("/sdapi/v1/scripts", self.get_scripts_list, methods=["GET"], response_model=models.ScriptsList)
|
||||||
self.add_api_route("/sdapi/v1/script-info", self.get_script_info, methods=["GET"], response_model=List[models.ScriptInfo])
|
self.add_api_route("/sdapi/v1/script-info", self.get_script_info, methods=["GET"], response_model=list[models.ScriptInfo])
|
||||||
|
self.add_api_route("/sdapi/v1/extensions", self.get_extensions_list, methods=["GET"], response_model=list[models.ExtensionItem])
|
||||||
|
|
||||||
if shared.cmd_opts.api_server_stop:
|
if shared.cmd_opts.api_server_stop:
|
||||||
self.add_api_route("/sdapi/v1/server-kill", self.kill_webui, methods=["POST"])
|
self.add_api_route("/sdapi/v1/server-kill", self.kill_webui, methods=["POST"])
|
||||||
@@ -216,6 +251,24 @@ class Api:
|
|||||||
self.default_script_arg_txt2img = []
|
self.default_script_arg_txt2img = []
|
||||||
self.default_script_arg_img2img = []
|
self.default_script_arg_img2img = []
|
||||||
|
|
||||||
|
txt2img_script_runner = scripts.scripts_txt2img
|
||||||
|
img2img_script_runner = scripts.scripts_img2img
|
||||||
|
|
||||||
|
if not txt2img_script_runner.scripts or not img2img_script_runner.scripts:
|
||||||
|
ui.create_ui()
|
||||||
|
|
||||||
|
if not txt2img_script_runner.scripts:
|
||||||
|
txt2img_script_runner.initialize_scripts(False)
|
||||||
|
if not self.default_script_arg_txt2img:
|
||||||
|
self.default_script_arg_txt2img = self.init_default_script_args(txt2img_script_runner)
|
||||||
|
|
||||||
|
if not img2img_script_runner.scripts:
|
||||||
|
img2img_script_runner.initialize_scripts(True)
|
||||||
|
if not self.default_script_arg_img2img:
|
||||||
|
self.default_script_arg_img2img = self.init_default_script_args(img2img_script_runner)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def add_api_route(self, path: str, endpoint, **kwargs):
|
def add_api_route(self, path: str, endpoint, **kwargs):
|
||||||
if shared.cmd_opts.api_auth:
|
if shared.cmd_opts.api_auth:
|
||||||
return self.app.add_api_route(path, endpoint, dependencies=[Depends(self.auth)], **kwargs)
|
return self.app.add_api_route(path, endpoint, dependencies=[Depends(self.auth)], **kwargs)
|
||||||
@@ -277,8 +330,13 @@ class Api:
|
|||||||
script_args[script.args_from:script.args_to] = ui_default_values
|
script_args[script.args_from:script.args_to] = ui_default_values
|
||||||
return script_args
|
return script_args
|
||||||
|
|
||||||
def init_script_args(self, request, default_script_args, selectable_scripts, selectable_idx, script_runner):
|
def init_script_args(self, request, default_script_args, selectable_scripts, selectable_idx, script_runner, *, input_script_args=None):
|
||||||
script_args = default_script_args.copy()
|
script_args = default_script_args.copy()
|
||||||
|
|
||||||
|
if input_script_args is not None:
|
||||||
|
for index, value in input_script_args.items():
|
||||||
|
script_args[index] = value
|
||||||
|
|
||||||
# position 0 in script_arg is the idx+1 of the selectable script that is going to be run when using scripts.scripts_*2img.run()
|
# position 0 in script_arg is the idx+1 of the selectable script that is going to be run when using scripts.scripts_*2img.run()
|
||||||
if selectable_scripts:
|
if selectable_scripts:
|
||||||
script_args[selectable_scripts.args_from:selectable_scripts.args_to] = request.script_args
|
script_args[selectable_scripts.args_from:selectable_scripts.args_to] = request.script_args
|
||||||
@@ -300,13 +358,83 @@ class Api:
|
|||||||
script_args[alwayson_script.args_from + idx] = request.alwayson_scripts[alwayson_script_name]["args"][idx]
|
script_args[alwayson_script.args_from + idx] = request.alwayson_scripts[alwayson_script_name]["args"][idx]
|
||||||
return script_args
|
return script_args
|
||||||
|
|
||||||
|
def apply_infotext(self, request, tabname, *, script_runner=None, mentioned_script_args=None):
|
||||||
|
"""Processes `infotext` field from the `request`, and sets other fields of the `request` accoring to what's in infotext.
|
||||||
|
|
||||||
|
If request already has a field set, and that field is encountered in infotext too, the value from infotext is ignored.
|
||||||
|
|
||||||
|
Additionally, fills `mentioned_script_args` dict with index: value pairs for script arguments read from infotext.
|
||||||
|
"""
|
||||||
|
|
||||||
|
if not request.infotext:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
possible_fields = infotext_utils.paste_fields[tabname]["fields"]
|
||||||
|
set_fields = request.model_dump(exclude_unset=True) if hasattr(request, "request") else request.dict(exclude_unset=True) # pydantic v1/v2 have differenrt names for this
|
||||||
|
params = infotext_utils.parse_generation_parameters(request.infotext)
|
||||||
|
|
||||||
|
def get_field_value(field, params):
|
||||||
|
value = field.function(params) if field.function else params.get(field.label)
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if field.api in request.__fields__:
|
||||||
|
target_type = request.__fields__[field.api].type_
|
||||||
|
else:
|
||||||
|
target_type = type(field.component.value)
|
||||||
|
|
||||||
|
if target_type == type(None):
|
||||||
|
return None
|
||||||
|
|
||||||
|
if isinstance(value, dict) and value.get('__type__') == 'generic_update': # this is a gradio.update rather than a value
|
||||||
|
value = value.get('value')
|
||||||
|
|
||||||
|
if value is not None and not isinstance(value, target_type):
|
||||||
|
value = target_type(value)
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
for field in possible_fields:
|
||||||
|
if not field.api:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if field.api in set_fields:
|
||||||
|
continue
|
||||||
|
|
||||||
|
value = get_field_value(field, params)
|
||||||
|
if value is not None:
|
||||||
|
setattr(request, field.api, value)
|
||||||
|
|
||||||
|
if request.override_settings is None:
|
||||||
|
request.override_settings = {}
|
||||||
|
|
||||||
|
overriden_settings = infotext_utils.get_override_settings(params)
|
||||||
|
for _, setting_name, value in overriden_settings:
|
||||||
|
if setting_name not in request.override_settings:
|
||||||
|
request.override_settings[setting_name] = value
|
||||||
|
|
||||||
|
if script_runner is not None and mentioned_script_args is not None:
|
||||||
|
indexes = {v: i for i, v in enumerate(script_runner.inputs)}
|
||||||
|
script_fields = ((field, indexes[field.component]) for field in possible_fields if field.component in indexes)
|
||||||
|
|
||||||
|
for field, index in script_fields:
|
||||||
|
value = get_field_value(field, params)
|
||||||
|
|
||||||
|
if value is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
mentioned_script_args[index] = value
|
||||||
|
|
||||||
|
return params
|
||||||
|
|
||||||
def text2imgapi(self, txt2imgreq: models.StableDiffusionTxt2ImgProcessingAPI):
|
def text2imgapi(self, txt2imgreq: models.StableDiffusionTxt2ImgProcessingAPI):
|
||||||
|
task_id = txt2imgreq.force_task_id or create_task_id("txt2img")
|
||||||
|
|
||||||
script_runner = scripts.scripts_txt2img
|
script_runner = scripts.scripts_txt2img
|
||||||
if not script_runner.scripts:
|
|
||||||
script_runner.initialize_scripts(False)
|
infotext_script_args = {}
|
||||||
ui.create_ui()
|
self.apply_infotext(txt2imgreq, "txt2img", script_runner=script_runner, mentioned_script_args=infotext_script_args)
|
||||||
if not self.default_script_arg_txt2img:
|
|
||||||
self.default_script_arg_txt2img = self.init_default_script_args(script_runner)
|
|
||||||
selectable_scripts, selectable_script_idx = self.get_selectable_script(txt2imgreq.script_name, script_runner)
|
selectable_scripts, selectable_script_idx = self.get_selectable_script(txt2imgreq.script_name, script_runner)
|
||||||
|
|
||||||
populate = txt2imgreq.copy(update={ # Override __init__ params
|
populate = txt2imgreq.copy(update={ # Override __init__ params
|
||||||
@@ -321,34 +449,43 @@ class Api:
|
|||||||
args.pop('script_name', None)
|
args.pop('script_name', None)
|
||||||
args.pop('script_args', None) # will refeed them to the pipeline directly after initializing them
|
args.pop('script_args', None) # will refeed them to the pipeline directly after initializing them
|
||||||
args.pop('alwayson_scripts', None)
|
args.pop('alwayson_scripts', None)
|
||||||
|
args.pop('infotext', None)
|
||||||
|
|
||||||
script_args = self.init_script_args(txt2imgreq, self.default_script_arg_txt2img, selectable_scripts, selectable_script_idx, script_runner)
|
script_args = self.init_script_args(txt2imgreq, self.default_script_arg_txt2img, selectable_scripts, selectable_script_idx, script_runner, input_script_args=infotext_script_args)
|
||||||
|
|
||||||
send_images = args.pop('send_images', True)
|
send_images = args.pop('send_images', True)
|
||||||
args.pop('save_images', None)
|
args.pop('save_images', None)
|
||||||
|
|
||||||
|
add_task_to_queue(task_id)
|
||||||
|
|
||||||
with self.queue_lock:
|
with self.queue_lock:
|
||||||
with closing(StableDiffusionProcessingTxt2Img(sd_model=shared.sd_model, **args)) as p:
|
with closing(StableDiffusionProcessingTxt2Img(sd_model=shared.sd_model, **args)) as p:
|
||||||
|
p.is_api = True
|
||||||
p.scripts = script_runner
|
p.scripts = script_runner
|
||||||
p.outpath_grids = opts.outdir_txt2img_grids
|
p.outpath_grids = opts.outdir_txt2img_grids
|
||||||
p.outpath_samples = opts.outdir_txt2img_samples
|
p.outpath_samples = opts.outdir_txt2img_samples
|
||||||
|
|
||||||
try:
|
try:
|
||||||
shared.state.begin(job="scripts_txt2img")
|
shared.state.begin(job="scripts_txt2img")
|
||||||
|
start_task(task_id)
|
||||||
if selectable_scripts is not None:
|
if selectable_scripts is not None:
|
||||||
p.script_args = script_args
|
p.script_args = script_args
|
||||||
processed = scripts.scripts_txt2img.run(p, *p.script_args) # Need to pass args as list here
|
processed = scripts.scripts_txt2img.run(p, *p.script_args) # Need to pass args as list here
|
||||||
else:
|
else:
|
||||||
p.script_args = tuple(script_args) # Need to pass args as tuple here
|
p.script_args = tuple(script_args) # Need to pass args as tuple here
|
||||||
processed = process_images(p)
|
processed = process_images(p)
|
||||||
|
finish_task(task_id)
|
||||||
finally:
|
finally:
|
||||||
shared.state.end()
|
shared.state.end()
|
||||||
|
shared.total_tqdm.clear()
|
||||||
|
|
||||||
b64images = list(map(encode_pil_to_base64, processed.images)) if send_images else []
|
b64images = list(map(encode_pil_to_base64, processed.images)) if send_images else []
|
||||||
|
|
||||||
return models.TextToImageResponse(images=b64images, parameters=vars(txt2imgreq), info=processed.js())
|
return models.TextToImageResponse(images=b64images, parameters=vars(txt2imgreq), info=processed.js())
|
||||||
|
|
||||||
def img2imgapi(self, img2imgreq: models.StableDiffusionImg2ImgProcessingAPI):
|
def img2imgapi(self, img2imgreq: models.StableDiffusionImg2ImgProcessingAPI):
|
||||||
|
task_id = img2imgreq.force_task_id or create_task_id("img2img")
|
||||||
|
|
||||||
init_images = img2imgreq.init_images
|
init_images = img2imgreq.init_images
|
||||||
if init_images is None:
|
if init_images is None:
|
||||||
raise HTTPException(status_code=404, detail="Init image not found")
|
raise HTTPException(status_code=404, detail="Init image not found")
|
||||||
@@ -358,11 +495,10 @@ class Api:
|
|||||||
mask = decode_base64_to_image(mask)
|
mask = decode_base64_to_image(mask)
|
||||||
|
|
||||||
script_runner = scripts.scripts_img2img
|
script_runner = scripts.scripts_img2img
|
||||||
if not script_runner.scripts:
|
|
||||||
script_runner.initialize_scripts(True)
|
infotext_script_args = {}
|
||||||
ui.create_ui()
|
self.apply_infotext(img2imgreq, "img2img", script_runner=script_runner, mentioned_script_args=infotext_script_args)
|
||||||
if not self.default_script_arg_img2img:
|
|
||||||
self.default_script_arg_img2img = self.init_default_script_args(script_runner)
|
|
||||||
selectable_scripts, selectable_script_idx = self.get_selectable_script(img2imgreq.script_name, script_runner)
|
selectable_scripts, selectable_script_idx = self.get_selectable_script(img2imgreq.script_name, script_runner)
|
||||||
|
|
||||||
populate = img2imgreq.copy(update={ # Override __init__ params
|
populate = img2imgreq.copy(update={ # Override __init__ params
|
||||||
@@ -379,29 +515,36 @@ class Api:
|
|||||||
args.pop('script_name', None)
|
args.pop('script_name', None)
|
||||||
args.pop('script_args', None) # will refeed them to the pipeline directly after initializing them
|
args.pop('script_args', None) # will refeed them to the pipeline directly after initializing them
|
||||||
args.pop('alwayson_scripts', None)
|
args.pop('alwayson_scripts', None)
|
||||||
|
args.pop('infotext', None)
|
||||||
|
|
||||||
script_args = self.init_script_args(img2imgreq, self.default_script_arg_img2img, selectable_scripts, selectable_script_idx, script_runner)
|
script_args = self.init_script_args(img2imgreq, self.default_script_arg_img2img, selectable_scripts, selectable_script_idx, script_runner, input_script_args=infotext_script_args)
|
||||||
|
|
||||||
send_images = args.pop('send_images', True)
|
send_images = args.pop('send_images', True)
|
||||||
args.pop('save_images', None)
|
args.pop('save_images', None)
|
||||||
|
|
||||||
|
add_task_to_queue(task_id)
|
||||||
|
|
||||||
with self.queue_lock:
|
with self.queue_lock:
|
||||||
with closing(StableDiffusionProcessingImg2Img(sd_model=shared.sd_model, **args)) as p:
|
with closing(StableDiffusionProcessingImg2Img(sd_model=shared.sd_model, **args)) as p:
|
||||||
p.init_images = [decode_base64_to_image(x) for x in init_images]
|
p.init_images = [decode_base64_to_image(x) for x in init_images]
|
||||||
|
p.is_api = True
|
||||||
p.scripts = script_runner
|
p.scripts = script_runner
|
||||||
p.outpath_grids = opts.outdir_img2img_grids
|
p.outpath_grids = opts.outdir_img2img_grids
|
||||||
p.outpath_samples = opts.outdir_img2img_samples
|
p.outpath_samples = opts.outdir_img2img_samples
|
||||||
|
|
||||||
try:
|
try:
|
||||||
shared.state.begin(job="scripts_img2img")
|
shared.state.begin(job="scripts_img2img")
|
||||||
|
start_task(task_id)
|
||||||
if selectable_scripts is not None:
|
if selectable_scripts is not None:
|
||||||
p.script_args = script_args
|
p.script_args = script_args
|
||||||
processed = scripts.scripts_img2img.run(p, *p.script_args) # Need to pass args as list here
|
processed = scripts.scripts_img2img.run(p, *p.script_args) # Need to pass args as list here
|
||||||
else:
|
else:
|
||||||
p.script_args = tuple(script_args) # Need to pass args as tuple here
|
p.script_args = tuple(script_args) # Need to pass args as tuple here
|
||||||
processed = process_images(p)
|
processed = process_images(p)
|
||||||
|
finish_task(task_id)
|
||||||
finally:
|
finally:
|
||||||
shared.state.end()
|
shared.state.end()
|
||||||
|
shared.total_tqdm.clear()
|
||||||
|
|
||||||
b64images = list(map(encode_pil_to_base64, processed.images)) if send_images else []
|
b64images = list(map(encode_pil_to_base64, processed.images)) if send_images else []
|
||||||
|
|
||||||
@@ -433,9 +576,6 @@ class Api:
|
|||||||
return models.ExtrasBatchImagesResponse(images=list(map(encode_pil_to_base64, result[0])), html_info=result[1])
|
return models.ExtrasBatchImagesResponse(images=list(map(encode_pil_to_base64, result[0])), html_info=result[1])
|
||||||
|
|
||||||
def pnginfoapi(self, req: models.PNGInfoRequest):
|
def pnginfoapi(self, req: models.PNGInfoRequest):
|
||||||
if(not req.image.strip()):
|
|
||||||
return models.PNGInfoResponse(info="")
|
|
||||||
|
|
||||||
image = decode_base64_to_image(req.image.strip())
|
image = decode_base64_to_image(req.image.strip())
|
||||||
if image is None:
|
if image is None:
|
||||||
return models.PNGInfoResponse(info="")
|
return models.PNGInfoResponse(info="")
|
||||||
@@ -444,9 +584,10 @@ class Api:
|
|||||||
if geninfo is None:
|
if geninfo is None:
|
||||||
geninfo = ""
|
geninfo = ""
|
||||||
|
|
||||||
items = {**{'parameters': geninfo}, **items}
|
params = infotext_utils.parse_generation_parameters(geninfo)
|
||||||
|
script_callbacks.infotext_pasted_callback(geninfo, params)
|
||||||
|
|
||||||
return models.PNGInfoResponse(info=geninfo, items=items)
|
return models.PNGInfoResponse(info=geninfo, items=items, parameters=params)
|
||||||
|
|
||||||
def progressapi(self, req: models.ProgressRequest = Depends()):
|
def progressapi(self, req: models.ProgressRequest = Depends()):
|
||||||
# copy from check_progress_call of ui.py
|
# copy from check_progress_call of ui.py
|
||||||
@@ -474,7 +615,7 @@ class Api:
|
|||||||
if shared.state.current_image and not req.skip_current_image:
|
if shared.state.current_image and not req.skip_current_image:
|
||||||
current_image = encode_pil_to_base64(shared.state.current_image)
|
current_image = encode_pil_to_base64(shared.state.current_image)
|
||||||
|
|
||||||
return models.ProgressResponse(progress=progress, eta_relative=eta_relative, state=shared.state.dict(), current_image=current_image, textinfo=shared.state.textinfo)
|
return models.ProgressResponse(progress=progress, eta_relative=eta_relative, state=shared.state.dict(), current_image=current_image, textinfo=shared.state.textinfo, current_task=current_task)
|
||||||
|
|
||||||
def interrogateapi(self, interrogatereq: models.InterrogateRequest):
|
def interrogateapi(self, interrogatereq: models.InterrogateRequest):
|
||||||
image_b64 = interrogatereq.image
|
image_b64 = interrogatereq.image
|
||||||
@@ -501,12 +642,12 @@ class Api:
|
|||||||
return {}
|
return {}
|
||||||
|
|
||||||
def unloadapi(self):
|
def unloadapi(self):
|
||||||
unload_model_weights()
|
sd_models.unload_model_weights()
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
def reloadapi(self):
|
def reloadapi(self):
|
||||||
reload_model_weights()
|
sd_models.send_model_to_device(shared.sd_model)
|
||||||
|
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
@@ -524,13 +665,13 @@ class Api:
|
|||||||
|
|
||||||
return options
|
return options
|
||||||
|
|
||||||
def set_config(self, req: Dict[str, Any]):
|
def set_config(self, req: dict[str, Any]):
|
||||||
checkpoint_name = req.get("sd_model_checkpoint", None)
|
checkpoint_name = req.get("sd_model_checkpoint", None)
|
||||||
if checkpoint_name is not None and checkpoint_name not in checkpoint_aliases:
|
if checkpoint_name is not None and checkpoint_name not in sd_models.checkpoint_aliases:
|
||||||
raise RuntimeError(f"model {checkpoint_name!r} not found")
|
raise RuntimeError(f"model {checkpoint_name!r} not found")
|
||||||
|
|
||||||
for k, v in req.items():
|
for k, v in req.items():
|
||||||
shared.opts.set(k, v)
|
shared.opts.set(k, v, is_api=True)
|
||||||
|
|
||||||
shared.opts.save(shared.config_filename)
|
shared.opts.save(shared.config_filename)
|
||||||
return
|
return
|
||||||
@@ -562,10 +703,12 @@ class Api:
|
|||||||
]
|
]
|
||||||
|
|
||||||
def get_sd_models(self):
|
def get_sd_models(self):
|
||||||
return [{"title": x.title, "model_name": x.model_name, "hash": x.shorthash, "sha256": x.sha256, "filename": x.filename, "config": find_checkpoint_config_near_filename(x)} for x in checkpoints_list.values()]
|
import modules.sd_models as sd_models
|
||||||
|
return [{"title": x.title, "model_name": x.model_name, "hash": x.shorthash, "sha256": x.sha256, "filename": x.filename, "config": find_checkpoint_config_near_filename(x)} for x in sd_models.checkpoints_list.values()]
|
||||||
|
|
||||||
def get_sd_vaes(self):
|
def get_sd_vaes(self):
|
||||||
return [{"model_name": x, "filename": vae_dict[x]} for x in vae_dict.keys()]
|
import modules.sd_vae as sd_vae
|
||||||
|
return [{"model_name": x, "filename": sd_vae.vae_dict[x]} for x in sd_vae.vae_dict.keys()]
|
||||||
|
|
||||||
def get_hypernetworks(self):
|
def get_hypernetworks(self):
|
||||||
return [{"name": name, "path": shared.hypernetworks[name]} for name in shared.hypernetworks]
|
return [{"name": name, "path": shared.hypernetworks[name]} for name in shared.hypernetworks]
|
||||||
@@ -608,6 +751,10 @@ class Api:
|
|||||||
with self.queue_lock:
|
with self.queue_lock:
|
||||||
shared.refresh_checkpoints()
|
shared.refresh_checkpoints()
|
||||||
|
|
||||||
|
def refresh_vae(self):
|
||||||
|
with self.queue_lock:
|
||||||
|
shared_items.refresh_vae_list()
|
||||||
|
|
||||||
def create_embedding(self, args: dict):
|
def create_embedding(self, args: dict):
|
||||||
try:
|
try:
|
||||||
shared.state.begin(job="create_embedding")
|
shared.state.begin(job="create_embedding")
|
||||||
@@ -630,19 +777,6 @@ class Api:
|
|||||||
finally:
|
finally:
|
||||||
shared.state.end()
|
shared.state.end()
|
||||||
|
|
||||||
def preprocess(self, args: dict):
|
|
||||||
try:
|
|
||||||
shared.state.begin(job="preprocess")
|
|
||||||
preprocess(**args) # quick operation unless blip/booru interrogation is enabled
|
|
||||||
shared.state.end()
|
|
||||||
return models.PreprocessResponse(info='preprocess complete')
|
|
||||||
except KeyError as e:
|
|
||||||
return models.PreprocessResponse(info=f"preprocess error: invalid token: {e}")
|
|
||||||
except Exception as e:
|
|
||||||
return models.PreprocessResponse(info=f"preprocess error: {e}")
|
|
||||||
finally:
|
|
||||||
shared.state.end()
|
|
||||||
|
|
||||||
def train_embedding(self, args: dict):
|
def train_embedding(self, args: dict):
|
||||||
try:
|
try:
|
||||||
shared.state.begin(job="train_embedding")
|
shared.state.begin(job="train_embedding")
|
||||||
@@ -724,9 +858,36 @@ class Api:
|
|||||||
cuda = {'error': f'{err}'}
|
cuda = {'error': f'{err}'}
|
||||||
return models.MemoryResponse(ram=ram, cuda=cuda)
|
return models.MemoryResponse(ram=ram, cuda=cuda)
|
||||||
|
|
||||||
|
def get_extensions_list(self):
|
||||||
|
from modules import extensions
|
||||||
|
extensions.list_extensions()
|
||||||
|
ext_list = []
|
||||||
|
for ext in extensions.extensions:
|
||||||
|
ext: extensions.Extension
|
||||||
|
ext.read_info_from_repo()
|
||||||
|
if ext.remote is not None:
|
||||||
|
ext_list.append({
|
||||||
|
"name": ext.name,
|
||||||
|
"remote": ext.remote,
|
||||||
|
"branch": ext.branch,
|
||||||
|
"commit_hash":ext.commit_hash,
|
||||||
|
"commit_date":ext.commit_date,
|
||||||
|
"version":ext.version,
|
||||||
|
"enabled":ext.enabled
|
||||||
|
})
|
||||||
|
return ext_list
|
||||||
|
|
||||||
def launch(self, server_name, port, root_path):
|
def launch(self, server_name, port, root_path):
|
||||||
self.app.include_router(self.router)
|
self.app.include_router(self.router)
|
||||||
uvicorn.run(self.app, host=server_name, port=port, timeout_keep_alive=shared.cmd_opts.timeout_keep_alive, root_path=root_path)
|
uvicorn.run(
|
||||||
|
self.app,
|
||||||
|
host=server_name,
|
||||||
|
port=port,
|
||||||
|
timeout_keep_alive=shared.cmd_opts.timeout_keep_alive,
|
||||||
|
root_path=root_path,
|
||||||
|
ssl_keyfile=shared.cmd_opts.tls_keyfile,
|
||||||
|
ssl_certfile=shared.cmd_opts.tls_certfile
|
||||||
|
)
|
||||||
|
|
||||||
def kill_webui(self):
|
def kill_webui(self):
|
||||||
restart.stop_program()
|
restart.stop_program()
|
||||||
|
|||||||
+33
-25
@@ -1,12 +1,10 @@
|
|||||||
import inspect
|
import inspect
|
||||||
|
|
||||||
from pydantic import BaseModel, Field, create_model
|
from pydantic import BaseModel, Field, create_model
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional, Literal
|
||||||
from typing_extensions import Literal
|
|
||||||
from inflection import underscore
|
from inflection import underscore
|
||||||
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img
|
from modules.processing import StableDiffusionProcessingTxt2Img, StableDiffusionProcessingImg2Img
|
||||||
from modules.shared import sd_upscalers, opts, parser
|
from modules.shared import sd_upscalers, opts, parser
|
||||||
from typing import Dict, List
|
|
||||||
|
|
||||||
API_NOT_ALLOWED = [
|
API_NOT_ALLOWED = [
|
||||||
"self",
|
"self",
|
||||||
@@ -50,10 +48,12 @@ class PydanticModelGenerator:
|
|||||||
additional_fields = None,
|
additional_fields = None,
|
||||||
):
|
):
|
||||||
def field_type_generator(k, v):
|
def field_type_generator(k, v):
|
||||||
# field_type = str if not overrides.get(k) else overrides[k]["type"]
|
|
||||||
# print(k, v.annotation, v.default)
|
|
||||||
field_type = v.annotation
|
field_type = v.annotation
|
||||||
|
|
||||||
|
if field_type == 'Image':
|
||||||
|
# images are sent as base64 strings via API
|
||||||
|
field_type = 'str'
|
||||||
|
|
||||||
return Optional[field_type]
|
return Optional[field_type]
|
||||||
|
|
||||||
def merge_class_params(class_):
|
def merge_class_params(class_):
|
||||||
@@ -63,7 +63,6 @@ class PydanticModelGenerator:
|
|||||||
parameters = {**parameters, **inspect.signature(classes.__init__).parameters}
|
parameters = {**parameters, **inspect.signature(classes.__init__).parameters}
|
||||||
return parameters
|
return parameters
|
||||||
|
|
||||||
|
|
||||||
self._model_name = model_name
|
self._model_name = model_name
|
||||||
self._class_data = merge_class_params(class_instance)
|
self._class_data = merge_class_params(class_instance)
|
||||||
|
|
||||||
@@ -72,7 +71,7 @@ class PydanticModelGenerator:
|
|||||||
field=underscore(k),
|
field=underscore(k),
|
||||||
field_alias=k,
|
field_alias=k,
|
||||||
field_type=field_type_generator(k, v),
|
field_type=field_type_generator(k, v),
|
||||||
field_value=v.default
|
field_value=None if isinstance(v.default, property) else v.default
|
||||||
)
|
)
|
||||||
for (k,v) in self._class_data.items() if k not in API_NOT_ALLOWED
|
for (k,v) in self._class_data.items() if k not in API_NOT_ALLOWED
|
||||||
]
|
]
|
||||||
@@ -108,6 +107,8 @@ StableDiffusionTxt2ImgProcessingAPI = PydanticModelGenerator(
|
|||||||
{"key": "send_images", "type": bool, "default": True},
|
{"key": "send_images", "type": bool, "default": True},
|
||||||
{"key": "save_images", "type": bool, "default": False},
|
{"key": "save_images", "type": bool, "default": False},
|
||||||
{"key": "alwayson_scripts", "type": dict, "default": {}},
|
{"key": "alwayson_scripts", "type": dict, "default": {}},
|
||||||
|
{"key": "force_task_id", "type": str, "default": None},
|
||||||
|
{"key": "infotext", "type": str, "default": None},
|
||||||
]
|
]
|
||||||
).generate_model()
|
).generate_model()
|
||||||
|
|
||||||
@@ -125,16 +126,18 @@ StableDiffusionImg2ImgProcessingAPI = PydanticModelGenerator(
|
|||||||
{"key": "send_images", "type": bool, "default": True},
|
{"key": "send_images", "type": bool, "default": True},
|
||||||
{"key": "save_images", "type": bool, "default": False},
|
{"key": "save_images", "type": bool, "default": False},
|
||||||
{"key": "alwayson_scripts", "type": dict, "default": {}},
|
{"key": "alwayson_scripts", "type": dict, "default": {}},
|
||||||
|
{"key": "force_task_id", "type": str, "default": None},
|
||||||
|
{"key": "infotext", "type": str, "default": None},
|
||||||
]
|
]
|
||||||
).generate_model()
|
).generate_model()
|
||||||
|
|
||||||
class TextToImageResponse(BaseModel):
|
class TextToImageResponse(BaseModel):
|
||||||
images: List[str] = Field(default=None, title="Image", description="The generated image in base64 format.")
|
images: list[str] = Field(default=None, title="Image", description="The generated image in base64 format.")
|
||||||
parameters: dict
|
parameters: dict
|
||||||
info: str
|
info: str
|
||||||
|
|
||||||
class ImageToImageResponse(BaseModel):
|
class ImageToImageResponse(BaseModel):
|
||||||
images: List[str] = Field(default=None, title="Image", description="The generated image in base64 format.")
|
images: list[str] = Field(default=None, title="Image", description="The generated image in base64 format.")
|
||||||
parameters: dict
|
parameters: dict
|
||||||
info: str
|
info: str
|
||||||
|
|
||||||
@@ -167,17 +170,18 @@ class FileData(BaseModel):
|
|||||||
name: str = Field(title="File name")
|
name: str = Field(title="File name")
|
||||||
|
|
||||||
class ExtrasBatchImagesRequest(ExtrasBaseRequest):
|
class ExtrasBatchImagesRequest(ExtrasBaseRequest):
|
||||||
imageList: List[FileData] = Field(title="Images", description="List of images to work on. Must be Base64 strings")
|
imageList: list[FileData] = Field(title="Images", description="List of images to work on. Must be Base64 strings")
|
||||||
|
|
||||||
class ExtrasBatchImagesResponse(ExtraBaseResponse):
|
class ExtrasBatchImagesResponse(ExtraBaseResponse):
|
||||||
images: List[str] = Field(title="Images", description="The generated images in base64 format.")
|
images: list[str] = Field(title="Images", description="The generated images in base64 format.")
|
||||||
|
|
||||||
class PNGInfoRequest(BaseModel):
|
class PNGInfoRequest(BaseModel):
|
||||||
image: str = Field(title="Image", description="The base64 encoded PNG image")
|
image: str = Field(title="Image", description="The base64 encoded PNG image")
|
||||||
|
|
||||||
class PNGInfoResponse(BaseModel):
|
class PNGInfoResponse(BaseModel):
|
||||||
info: str = Field(title="Image info", description="A string with the parameters used to generate the image")
|
info: str = Field(title="Image info", description="A string with the parameters used to generate the image")
|
||||||
items: dict = Field(title="Items", description="An object containing all the info the image had")
|
items: dict = Field(title="Items", description="A dictionary containing all the other fields the image had")
|
||||||
|
parameters: dict = Field(title="Parameters", description="A dictionary with parsed generation info fields")
|
||||||
|
|
||||||
class ProgressRequest(BaseModel):
|
class ProgressRequest(BaseModel):
|
||||||
skip_current_image: bool = Field(default=False, title="Skip current image", description="Skip current image serialization")
|
skip_current_image: bool = Field(default=False, title="Skip current image", description="Skip current image serialization")
|
||||||
@@ -202,17 +206,12 @@ class TrainResponse(BaseModel):
|
|||||||
class CreateResponse(BaseModel):
|
class CreateResponse(BaseModel):
|
||||||
info: str = Field(title="Create info", description="Response string from create embedding or hypernetwork task.")
|
info: str = Field(title="Create info", description="Response string from create embedding or hypernetwork task.")
|
||||||
|
|
||||||
class PreprocessResponse(BaseModel):
|
|
||||||
info: str = Field(title="Preprocess info", description="Response string from preprocessing task.")
|
|
||||||
|
|
||||||
fields = {}
|
fields = {}
|
||||||
for key, metadata in opts.data_labels.items():
|
for key, metadata in opts.data_labels.items():
|
||||||
value = opts.data.get(key)
|
value = opts.data.get(key)
|
||||||
optType = opts.typemap.get(type(metadata.default), type(metadata.default))
|
optType = opts.typemap.get(type(metadata.default), type(metadata.default)) if metadata.default else Any
|
||||||
|
|
||||||
if metadata.default is None:
|
if metadata is not None:
|
||||||
pass
|
|
||||||
elif metadata is not None:
|
|
||||||
fields.update({key: (Optional[optType], Field(default=metadata.default, description=metadata.label))})
|
fields.update({key: (Optional[optType], Field(default=metadata.default, description=metadata.label))})
|
||||||
else:
|
else:
|
||||||
fields.update({key: (Optional[optType], Field())})
|
fields.update({key: (Optional[optType], Field())})
|
||||||
@@ -233,8 +232,8 @@ FlagsModel = create_model("Flags", **flags)
|
|||||||
|
|
||||||
class SamplerItem(BaseModel):
|
class SamplerItem(BaseModel):
|
||||||
name: str = Field(title="Name")
|
name: str = Field(title="Name")
|
||||||
aliases: List[str] = Field(title="Aliases")
|
aliases: list[str] = Field(title="Aliases")
|
||||||
options: Dict[str, str] = Field(title="Options")
|
options: dict[str, str] = Field(title="Options")
|
||||||
|
|
||||||
class UpscalerItem(BaseModel):
|
class UpscalerItem(BaseModel):
|
||||||
name: str = Field(title="Name")
|
name: str = Field(title="Name")
|
||||||
@@ -285,8 +284,8 @@ class EmbeddingItem(BaseModel):
|
|||||||
vectors: int = Field(title="Vectors", description="The number of vectors in the embedding")
|
vectors: int = Field(title="Vectors", description="The number of vectors in the embedding")
|
||||||
|
|
||||||
class EmbeddingsResponse(BaseModel):
|
class EmbeddingsResponse(BaseModel):
|
||||||
loaded: Dict[str, EmbeddingItem] = Field(title="Loaded", description="Embeddings loaded for the current model")
|
loaded: dict[str, EmbeddingItem] = Field(title="Loaded", description="Embeddings loaded for the current model")
|
||||||
skipped: Dict[str, EmbeddingItem] = Field(title="Skipped", description="Embeddings skipped for the current model (likely due to architecture incompatibility)")
|
skipped: dict[str, EmbeddingItem] = Field(title="Skipped", description="Embeddings skipped for the current model (likely due to architecture incompatibility)")
|
||||||
|
|
||||||
class MemoryResponse(BaseModel):
|
class MemoryResponse(BaseModel):
|
||||||
ram: dict = Field(title="RAM", description="System memory stats")
|
ram: dict = Field(title="RAM", description="System memory stats")
|
||||||
@@ -304,11 +303,20 @@ class ScriptArg(BaseModel):
|
|||||||
minimum: Optional[Any] = Field(default=None, title="Minimum", description="Minimum allowed value for the argumentin UI")
|
minimum: Optional[Any] = Field(default=None, title="Minimum", description="Minimum allowed value for the argumentin UI")
|
||||||
maximum: Optional[Any] = Field(default=None, title="Minimum", description="Maximum allowed value for the argumentin UI")
|
maximum: Optional[Any] = Field(default=None, title="Minimum", description="Maximum allowed value for the argumentin UI")
|
||||||
step: Optional[Any] = Field(default=None, title="Minimum", description="Step for changing value of the argumentin UI")
|
step: Optional[Any] = Field(default=None, title="Minimum", description="Step for changing value of the argumentin UI")
|
||||||
choices: Optional[List[str]] = Field(default=None, title="Choices", description="Possible values for the argument")
|
choices: Optional[list[str]] = Field(default=None, title="Choices", description="Possible values for the argument")
|
||||||
|
|
||||||
|
|
||||||
class ScriptInfo(BaseModel):
|
class ScriptInfo(BaseModel):
|
||||||
name: str = Field(default=None, title="Name", description="Script name")
|
name: str = Field(default=None, title="Name", description="Script name")
|
||||||
is_alwayson: bool = Field(default=None, title="IsAlwayson", description="Flag specifying whether this script is an alwayson script")
|
is_alwayson: bool = Field(default=None, title="IsAlwayson", description="Flag specifying whether this script is an alwayson script")
|
||||||
is_img2img: bool = Field(default=None, title="IsImg2img", description="Flag specifying whether this script is an img2img script")
|
is_img2img: bool = Field(default=None, title="IsImg2img", description="Flag specifying whether this script is an img2img script")
|
||||||
args: List[ScriptArg] = Field(title="Arguments", description="List of script's arguments")
|
args: list[ScriptArg] = Field(title="Arguments", description="List of script's arguments")
|
||||||
|
|
||||||
|
class ExtensionItem(BaseModel):
|
||||||
|
name: str = Field(title="Name", description="Extension name")
|
||||||
|
remote: str = Field(title="Remote", description="Extension Repository URL")
|
||||||
|
branch: str = Field(title="Branch", description="Extension Repository Branch")
|
||||||
|
commit_hash: str = Field(title="Commit Hash", description="Extension Repository Commit Hash")
|
||||||
|
version: str = Field(title="Version", description="Extension Version")
|
||||||
|
commit_date: str = Field(title="Commit Date", description="Extension Repository Commit Date")
|
||||||
|
enabled: bool = Field(title="Enabled", description="Flag specifying whether this extension is enabled")
|
||||||
|
|||||||
+15
-12
@@ -1,11 +1,12 @@
|
|||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import os.path
|
import os.path
|
||||||
import threading
|
import threading
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from modules.paths import data_path, script_path
|
from modules.paths import data_path, script_path
|
||||||
|
|
||||||
cache_filename = os.path.join(data_path, "cache.json")
|
cache_filename = os.environ.get('SD_WEBUI_CACHE_FILE', os.path.join(data_path, "cache.json"))
|
||||||
cache_data = None
|
cache_data = None
|
||||||
cache_lock = threading.Lock()
|
cache_lock = threading.Lock()
|
||||||
|
|
||||||
@@ -29,8 +30,11 @@ def dump_cache():
|
|||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
with cache_lock:
|
with cache_lock:
|
||||||
with open(cache_filename, "w", encoding="utf8") as file:
|
cache_filename_tmp = cache_filename + "-"
|
||||||
json.dump(cache_data, file, indent=4)
|
with open(cache_filename_tmp, "w", encoding="utf8") as file:
|
||||||
|
json.dump(cache_data, file, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
|
os.replace(cache_filename_tmp, cache_filename)
|
||||||
|
|
||||||
dump_cache_after = None
|
dump_cache_after = None
|
||||||
dump_cache_thread = None
|
dump_cache_thread = None
|
||||||
@@ -58,16 +62,15 @@ def cache(subsection):
|
|||||||
if cache_data is None:
|
if cache_data is None:
|
||||||
with cache_lock:
|
with cache_lock:
|
||||||
if cache_data is None:
|
if cache_data is None:
|
||||||
if not os.path.isfile(cache_filename):
|
try:
|
||||||
|
with open(cache_filename, "r", encoding="utf8") as file:
|
||||||
|
cache_data = json.load(file)
|
||||||
|
except FileNotFoundError:
|
||||||
|
cache_data = {}
|
||||||
|
except Exception:
|
||||||
|
os.replace(cache_filename, os.path.join(script_path, "tmp", "cache.json"))
|
||||||
|
print('[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache')
|
||||||
cache_data = {}
|
cache_data = {}
|
||||||
else:
|
|
||||||
try:
|
|
||||||
with open(cache_filename, "r", encoding="utf8") as file:
|
|
||||||
cache_data = json.load(file)
|
|
||||||
except Exception:
|
|
||||||
os.replace(cache_filename, os.path.join(script_path, "tmp", "cache.json"))
|
|
||||||
print('[ERROR] issue occurred while trying to read cache.json, move current cache to tmp/cache.json and create new cache')
|
|
||||||
cache_data = {}
|
|
||||||
|
|
||||||
s = cache_data.get(subsection, {})
|
s = cache_data.get(subsection, {})
|
||||||
cache_data[subsection] = s
|
cache_data[subsection] = s
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
from functools import wraps
|
from functools import wraps
|
||||||
import html
|
import html
|
||||||
import threading
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from modules import shared, progress, errors
|
from modules import shared, progress, errors, devices, fifo_lock
|
||||||
|
|
||||||
queue_lock = threading.Lock()
|
queue_lock = fifo_lock.FIFOLock()
|
||||||
|
|
||||||
|
|
||||||
def wrap_queued_call(func):
|
def wrap_queued_call(func):
|
||||||
@@ -75,8 +74,11 @@ def wrap_gradio_call(func, extra_outputs=None, add_stats=False):
|
|||||||
error_message = f'{type(e).__name__}: {e}'
|
error_message = f'{type(e).__name__}: {e}'
|
||||||
res = extra_outputs_array + [f"<div class='error'>{html.escape(error_message)}</div>"]
|
res = extra_outputs_array + [f"<div class='error'>{html.escape(error_message)}</div>"]
|
||||||
|
|
||||||
|
devices.torch_gc()
|
||||||
|
|
||||||
shared.state.skipped = False
|
shared.state.skipped = False
|
||||||
shared.state.interrupted = False
|
shared.state.interrupted = False
|
||||||
|
shared.state.stopping_generation = False
|
||||||
shared.state.job_count = 0
|
shared.state.job_count = 0
|
||||||
|
|
||||||
if not add_stats:
|
if not add_stats:
|
||||||
|
|||||||
+17
-6
@@ -13,8 +13,11 @@ parser.add_argument("--reinstall-xformers", action='store_true', help="launch.py
|
|||||||
parser.add_argument("--reinstall-torch", action='store_true', help="launch.py argument: install the appropriate version of torch even if you have some version already installed")
|
parser.add_argument("--reinstall-torch", action='store_true', help="launch.py argument: install the appropriate version of torch even if you have some version already installed")
|
||||||
parser.add_argument("--update-check", action='store_true', help="launch.py argument: check for updates at startup")
|
parser.add_argument("--update-check", action='store_true', help="launch.py argument: check for updates at startup")
|
||||||
parser.add_argument("--test-server", action='store_true', help="launch.py argument: configure server for testing")
|
parser.add_argument("--test-server", action='store_true', help="launch.py argument: configure server for testing")
|
||||||
|
parser.add_argument("--log-startup", action='store_true', help="launch.py argument: print a detailed log of what's happening at startup")
|
||||||
parser.add_argument("--skip-prepare-environment", action='store_true', help="launch.py argument: skip all environment preparation")
|
parser.add_argument("--skip-prepare-environment", action='store_true', help="launch.py argument: skip all environment preparation")
|
||||||
parser.add_argument("--skip-install", action='store_true', help="launch.py argument: skip installation of packages")
|
parser.add_argument("--skip-install", action='store_true', help="launch.py argument: skip installation of packages")
|
||||||
|
parser.add_argument("--dump-sysinfo", action='store_true', help="launch.py argument: dump limited sysinfo file (without information about extensions, options) to disk and quit")
|
||||||
|
parser.add_argument("--loglevel", type=str, help="log level; one of: CRITICAL, ERROR, WARNING, INFO, DEBUG", default=None)
|
||||||
parser.add_argument("--do-not-download-clip", action='store_true', help="do not download CLIP model even if it's not included in the checkpoint")
|
parser.add_argument("--do-not-download-clip", action='store_true', help="do not download CLIP model even if it's not included in the checkpoint")
|
||||||
parser.add_argument("--data-dir", type=str, default=os.path.dirname(os.path.dirname(os.path.realpath(__file__))), help="base path where all user data is stored")
|
parser.add_argument("--data-dir", type=str, default=os.path.dirname(os.path.dirname(os.path.realpath(__file__))), help="base path where all user data is stored")
|
||||||
parser.add_argument("--config", type=str, default=sd_default_config, help="path to config which constructs model",)
|
parser.add_argument("--config", type=str, default=sd_default_config, help="path to config which constructs model",)
|
||||||
@@ -33,9 +36,10 @@ parser.add_argument("--hypernetwork-dir", type=str, default=os.path.join(models_
|
|||||||
parser.add_argument("--localizations-dir", type=str, default=os.path.join(script_path, 'localizations'), help="localizations directory")
|
parser.add_argument("--localizations-dir", type=str, default=os.path.join(script_path, 'localizations'), help="localizations directory")
|
||||||
parser.add_argument("--allow-code", action='store_true', help="allow custom script execution from webui")
|
parser.add_argument("--allow-code", action='store_true', help="allow custom script execution from webui")
|
||||||
parser.add_argument("--medvram", action='store_true', help="enable stable diffusion model optimizations for sacrificing a little speed for low VRM usage")
|
parser.add_argument("--medvram", action='store_true', help="enable stable diffusion model optimizations for sacrificing a little speed for low VRM usage")
|
||||||
|
parser.add_argument("--medvram-sdxl", action='store_true', help="enable --medvram optimization just for SDXL models")
|
||||||
parser.add_argument("--lowvram", action='store_true', help="enable stable diffusion model optimizations for sacrificing a lot of speed for very low VRM usage")
|
parser.add_argument("--lowvram", action='store_true', help="enable stable diffusion model optimizations for sacrificing a lot of speed for very low VRM usage")
|
||||||
parser.add_argument("--lowram", action='store_true', help="load stable diffusion checkpoint weights to VRAM instead of RAM")
|
parser.add_argument("--lowram", action='store_true', help="load stable diffusion checkpoint weights to VRAM instead of RAM")
|
||||||
parser.add_argument("--always-batch-cond-uncond", action='store_true', help="disables cond/uncond batching that is enabled to save memory with --medvram or --lowvram")
|
parser.add_argument("--always-batch-cond-uncond", action='store_true', help="does not do anything")
|
||||||
parser.add_argument("--unload-gfpgan", action='store_true', help="does not do anything.")
|
parser.add_argument("--unload-gfpgan", action='store_true', help="does not do anything.")
|
||||||
parser.add_argument("--precision", type=str, help="evaluate at this precision", choices=["full", "autocast"], default="autocast")
|
parser.add_argument("--precision", type=str, help="evaluate at this precision", choices=["full", "autocast"], default="autocast")
|
||||||
parser.add_argument("--upcast-sampling", action='store_true', help="upcast sampling. No effect with --no-half. Usually produces similar results to --no-half with better performance while using less memory.")
|
parser.add_argument("--upcast-sampling", action='store_true', help="upcast sampling. No effect with --no-half. Usually produces similar results to --no-half with better performance while using less memory.")
|
||||||
@@ -66,26 +70,30 @@ parser.add_argument("--opt-sdp-no-mem-attention", action='store_true', help="pre
|
|||||||
parser.add_argument("--disable-opt-split-attention", action='store_true', help="prefer no cross-attention layer optimization for automatic choice of optimization")
|
parser.add_argument("--disable-opt-split-attention", action='store_true', help="prefer no cross-attention layer optimization for automatic choice of optimization")
|
||||||
parser.add_argument("--disable-nan-check", action='store_true', help="do not check if produced images/latent spaces have nans; useful for running without a checkpoint in CI")
|
parser.add_argument("--disable-nan-check", action='store_true', help="do not check if produced images/latent spaces have nans; useful for running without a checkpoint in CI")
|
||||||
parser.add_argument("--use-cpu", nargs='+', help="use CPU as torch device for specified modules", default=[], type=str.lower)
|
parser.add_argument("--use-cpu", nargs='+', help="use CPU as torch device for specified modules", default=[], type=str.lower)
|
||||||
|
parser.add_argument("--use-ipex", action="store_true", help="use Intel XPU as torch device")
|
||||||
|
parser.add_argument("--disable-model-loading-ram-optimization", action='store_true', help="disable an optimization that reduces RAM use when loading a model")
|
||||||
parser.add_argument("--listen", action='store_true', help="launch gradio with 0.0.0.0 as server name, allowing to respond to network requests")
|
parser.add_argument("--listen", action='store_true', help="launch gradio with 0.0.0.0 as server name, allowing to respond to network requests")
|
||||||
parser.add_argument("--port", type=int, help="launch gradio with given server port, you need root/admin rights for ports < 1024, defaults to 7860 if available", default=None)
|
parser.add_argument("--port", type=int, help="launch gradio with given server port, you need root/admin rights for ports < 1024, defaults to 7860 if available", default=None)
|
||||||
parser.add_argument("--show-negative-prompt", action='store_true', help="does not do anything", default=False)
|
parser.add_argument("--show-negative-prompt", action='store_true', help="does not do anything", default=False)
|
||||||
parser.add_argument("--ui-config-file", type=str, help="filename to use for ui configuration", default=os.path.join(data_path, 'ui-config.json'))
|
parser.add_argument("--ui-config-file", type=str, help="filename to use for ui configuration", default=os.path.join(data_path, 'ui-config.json'))
|
||||||
parser.add_argument("--hide-ui-dir-config", action='store_true', help="hide directory configuration from webui", default=False)
|
parser.add_argument("--hide-ui-dir-config", action='store_true', help="hide directory configuration from webui", default=False)
|
||||||
parser.add_argument("--freeze-settings", action='store_true', help="disable editing settings", default=False)
|
parser.add_argument("--freeze-settings", action='store_true', help="disable editing of all settings globally", default=False)
|
||||||
|
parser.add_argument("--freeze-settings-in-sections", type=str, help='disable editing settings in specific sections of the settings page by specifying a comma-delimited list such like "saving-images,upscaling". The list of setting names can be found in the modules/shared_options.py file', default=None)
|
||||||
|
parser.add_argument("--freeze-specific-settings", type=str, help='disable editing of individual settings by specifying a comma-delimited list like "samples_save,samples_format". The list of setting names can be found in the config.json file', default=None)
|
||||||
parser.add_argument("--ui-settings-file", type=str, help="filename to use for ui settings", default=os.path.join(data_path, 'config.json'))
|
parser.add_argument("--ui-settings-file", type=str, help="filename to use for ui settings", default=os.path.join(data_path, 'config.json'))
|
||||||
parser.add_argument("--gradio-debug", action='store_true', help="launch gradio with --debug option")
|
parser.add_argument("--gradio-debug", action='store_true', help="launch gradio with --debug option")
|
||||||
parser.add_argument("--gradio-auth", type=str, help='set gradio authentication like "username:password"; or comma-delimit multiple like "u1:p1,u2:p2,u3:p3"', default=None)
|
parser.add_argument("--gradio-auth", type=str, help='set gradio authentication like "username:password"; or comma-delimit multiple like "u1:p1,u2:p2,u3:p3"', default=None)
|
||||||
parser.add_argument("--gradio-auth-path", type=str, help='set gradio authentication file path ex. "/path/to/auth/file" same auth format as --gradio-auth', default=None)
|
parser.add_argument("--gradio-auth-path", type=str, help='set gradio authentication file path ex. "/path/to/auth/file" same auth format as --gradio-auth', default=None)
|
||||||
parser.add_argument("--gradio-img2img-tool", type=str, help='does not do anything')
|
parser.add_argument("--gradio-img2img-tool", type=str, help='does not do anything')
|
||||||
parser.add_argument("--gradio-inpaint-tool", type=str, help="does not do anything")
|
parser.add_argument("--gradio-inpaint-tool", type=str, help="does not do anything")
|
||||||
parser.add_argument("--gradio-allowed-path", action='append', help="add path to gradio's allowed_paths, make it possible to serve files from it")
|
parser.add_argument("--gradio-allowed-path", action='append', help="add path to gradio's allowed_paths, make it possible to serve files from it", default=[data_path])
|
||||||
parser.add_argument("--opt-channelslast", action='store_true', help="change memory type for stable diffusion to channels last")
|
parser.add_argument("--opt-channelslast", action='store_true', help="change memory type for stable diffusion to channels last")
|
||||||
parser.add_argument("--styles-file", type=str, help="filename to use for styles", default=os.path.join(data_path, 'styles.csv'))
|
parser.add_argument("--styles-file", type=str, help="filename to use for styles", default=os.path.join(data_path, 'styles.csv'))
|
||||||
parser.add_argument("--autolaunch", action='store_true', help="open the webui URL in the system's default browser upon launch", default=False)
|
parser.add_argument("--autolaunch", action='store_true', help="open the webui URL in the system's default browser upon launch", default=False)
|
||||||
parser.add_argument("--theme", type=str, help="launches the UI with light or dark theme", default=None)
|
parser.add_argument("--theme", type=str, help="launches the UI with light or dark theme", default=None)
|
||||||
parser.add_argument("--use-textbox-seed", action='store_true', help="use textbox for seeds in UI (no up/down, but possible to input long seeds)", default=False)
|
parser.add_argument("--use-textbox-seed", action='store_true', help="use textbox for seeds in UI (no up/down, but possible to input long seeds)", default=False)
|
||||||
parser.add_argument("--disable-console-progressbars", action='store_true', help="do not output progressbars to console", default=False)
|
parser.add_argument("--disable-console-progressbars", action='store_true', help="do not output progressbars to console", default=False)
|
||||||
parser.add_argument("--enable-console-prompts", action='store_true', help="print prompts to console when generating with txt2img and img2img", default=False)
|
parser.add_argument("--enable-console-prompts", action='store_true', help="does not do anything", default=False) # Legacy compatibility, use as default value shared.opts.enable_console_prompts
|
||||||
parser.add_argument('--vae-path', type=str, help='Checkpoint to use as VAE; setting this argument disables all settings related to VAE', default=None)
|
parser.add_argument('--vae-path', type=str, help='Checkpoint to use as VAE; setting this argument disables all settings related to VAE', default=None)
|
||||||
parser.add_argument("--disable-safe-unpickle", action='store_true', help="disable checking pytorch models for malicious code", default=False)
|
parser.add_argument("--disable-safe-unpickle", action='store_true', help="disable checking pytorch models for malicious code", default=False)
|
||||||
parser.add_argument("--api", action='store_true', help="use api=True to launch the API together with the webui (use --nowebui instead for only the API)")
|
parser.add_argument("--api", action='store_true', help="use api=True to launch the API together with the webui (use --nowebui instead for only the API)")
|
||||||
@@ -102,11 +110,14 @@ parser.add_argument("--tls-certfile", type=str, help="Partially enables TLS, req
|
|||||||
parser.add_argument("--disable-tls-verify", action="store_false", help="When passed, enables the use of self-signed certificates.", default=None)
|
parser.add_argument("--disable-tls-verify", action="store_false", help="When passed, enables the use of self-signed certificates.", default=None)
|
||||||
parser.add_argument("--server-name", type=str, help="Sets hostname of server", default=None)
|
parser.add_argument("--server-name", type=str, help="Sets hostname of server", default=None)
|
||||||
parser.add_argument("--gradio-queue", action='store_true', help="does not do anything", default=True)
|
parser.add_argument("--gradio-queue", action='store_true', help="does not do anything", default=True)
|
||||||
parser.add_argument("--no-gradio-queue", action='store_true', help="Disables gradio queue; causes the webpage to use http requests instead of websockets; was the defaul in earlier versions")
|
parser.add_argument("--no-gradio-queue", action='store_true', help="Disables gradio queue; causes the webpage to use http requests instead of websockets; was the default in earlier versions")
|
||||||
parser.add_argument("--skip-version-check", action='store_true', help="Do not check versions of torch and xformers")
|
parser.add_argument("--skip-version-check", action='store_true', help="Do not check versions of torch and xformers")
|
||||||
parser.add_argument("--no-hashing", action='store_true', help="disable sha256 hashing of checkpoints to help loading performance", default=False)
|
parser.add_argument("--no-hashing", action='store_true', help="disable sha256 hashing of checkpoints to help loading performance", default=False)
|
||||||
parser.add_argument("--no-download-sd-model", action='store_true', help="don't download SD1.5 model even if no model is found in --ckpt-dir", default=False)
|
parser.add_argument("--no-download-sd-model", action='store_true', help="don't download SD1.5 model even if no model is found in --ckpt-dir", default=False)
|
||||||
parser.add_argument('--subpath', type=str, help='customize the subpath for gradio, use with reverse proxy')
|
parser.add_argument('--subpath', type=str, help='customize the subpath for gradio, use with reverse proxy')
|
||||||
parser.add_argument('--add-stop-route', action='store_true', help='add /_stop route to stop server')
|
parser.add_argument('--add-stop-route', action='store_true', help='does not do anything')
|
||||||
parser.add_argument('--api-server-stop', action='store_true', help='enable server stop/restart/kill via api')
|
parser.add_argument('--api-server-stop', action='store_true', help='enable server stop/restart/kill via api')
|
||||||
parser.add_argument('--timeout-keep-alive', type=int, default=30, help='set timeout_keep_alive for uvicorn')
|
parser.add_argument('--timeout-keep-alive', type=int, default=30, help='set timeout_keep_alive for uvicorn')
|
||||||
|
parser.add_argument("--disable-all-extensions", action='store_true', help="prevent all extensions from running regardless of any other settings", default=False)
|
||||||
|
parser.add_argument("--disable-extra-extensions", action='store_true', help="prevent all extensions except built-in from running regardless of any other settings", default=False)
|
||||||
|
parser.add_argument("--skip-load-model-at-start", action='store_true', help="if load a model at web start, only take effect when --nowebui", )
|
||||||
|
|||||||
@@ -1,276 +0,0 @@
|
|||||||
# this file is copied from CodeFormer repository. Please see comment in modules/codeformer_model.py
|
|
||||||
|
|
||||||
import math
|
|
||||||
import torch
|
|
||||||
from torch import nn, Tensor
|
|
||||||
import torch.nn.functional as F
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from modules.codeformer.vqgan_arch import VQAutoEncoder, ResBlock
|
|
||||||
from basicsr.utils.registry import ARCH_REGISTRY
|
|
||||||
|
|
||||||
def calc_mean_std(feat, eps=1e-5):
|
|
||||||
"""Calculate mean and std for adaptive_instance_normalization.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
feat (Tensor): 4D tensor.
|
|
||||||
eps (float): A small value added to the variance to avoid
|
|
||||||
divide-by-zero. Default: 1e-5.
|
|
||||||
"""
|
|
||||||
size = feat.size()
|
|
||||||
assert len(size) == 4, 'The input feature should be 4D tensor.'
|
|
||||||
b, c = size[:2]
|
|
||||||
feat_var = feat.view(b, c, -1).var(dim=2) + eps
|
|
||||||
feat_std = feat_var.sqrt().view(b, c, 1, 1)
|
|
||||||
feat_mean = feat.view(b, c, -1).mean(dim=2).view(b, c, 1, 1)
|
|
||||||
return feat_mean, feat_std
|
|
||||||
|
|
||||||
|
|
||||||
def adaptive_instance_normalization(content_feat, style_feat):
|
|
||||||
"""Adaptive instance normalization.
|
|
||||||
|
|
||||||
Adjust the reference features to have the similar color and illuminations
|
|
||||||
as those in the degradate features.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
content_feat (Tensor): The reference feature.
|
|
||||||
style_feat (Tensor): The degradate features.
|
|
||||||
"""
|
|
||||||
size = content_feat.size()
|
|
||||||
style_mean, style_std = calc_mean_std(style_feat)
|
|
||||||
content_mean, content_std = calc_mean_std(content_feat)
|
|
||||||
normalized_feat = (content_feat - content_mean.expand(size)) / content_std.expand(size)
|
|
||||||
return normalized_feat * style_std.expand(size) + style_mean.expand(size)
|
|
||||||
|
|
||||||
|
|
||||||
class PositionEmbeddingSine(nn.Module):
|
|
||||||
"""
|
|
||||||
This is a more standard version of the position embedding, very similar to the one
|
|
||||||
used by the Attention is all you need paper, generalized to work on images.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, num_pos_feats=64, temperature=10000, normalize=False, scale=None):
|
|
||||||
super().__init__()
|
|
||||||
self.num_pos_feats = num_pos_feats
|
|
||||||
self.temperature = temperature
|
|
||||||
self.normalize = normalize
|
|
||||||
if scale is not None and normalize is False:
|
|
||||||
raise ValueError("normalize should be True if scale is passed")
|
|
||||||
if scale is None:
|
|
||||||
scale = 2 * math.pi
|
|
||||||
self.scale = scale
|
|
||||||
|
|
||||||
def forward(self, x, mask=None):
|
|
||||||
if mask is None:
|
|
||||||
mask = torch.zeros((x.size(0), x.size(2), x.size(3)), device=x.device, dtype=torch.bool)
|
|
||||||
not_mask = ~mask
|
|
||||||
y_embed = not_mask.cumsum(1, dtype=torch.float32)
|
|
||||||
x_embed = not_mask.cumsum(2, dtype=torch.float32)
|
|
||||||
if self.normalize:
|
|
||||||
eps = 1e-6
|
|
||||||
y_embed = y_embed / (y_embed[:, -1:, :] + eps) * self.scale
|
|
||||||
x_embed = x_embed / (x_embed[:, :, -1:] + eps) * self.scale
|
|
||||||
|
|
||||||
dim_t = torch.arange(self.num_pos_feats, dtype=torch.float32, device=x.device)
|
|
||||||
dim_t = self.temperature ** (2 * (dim_t // 2) / self.num_pos_feats)
|
|
||||||
|
|
||||||
pos_x = x_embed[:, :, :, None] / dim_t
|
|
||||||
pos_y = y_embed[:, :, :, None] / dim_t
|
|
||||||
pos_x = torch.stack(
|
|
||||||
(pos_x[:, :, :, 0::2].sin(), pos_x[:, :, :, 1::2].cos()), dim=4
|
|
||||||
).flatten(3)
|
|
||||||
pos_y = torch.stack(
|
|
||||||
(pos_y[:, :, :, 0::2].sin(), pos_y[:, :, :, 1::2].cos()), dim=4
|
|
||||||
).flatten(3)
|
|
||||||
pos = torch.cat((pos_y, pos_x), dim=3).permute(0, 3, 1, 2)
|
|
||||||
return pos
|
|
||||||
|
|
||||||
def _get_activation_fn(activation):
|
|
||||||
"""Return an activation function given a string"""
|
|
||||||
if activation == "relu":
|
|
||||||
return F.relu
|
|
||||||
if activation == "gelu":
|
|
||||||
return F.gelu
|
|
||||||
if activation == "glu":
|
|
||||||
return F.glu
|
|
||||||
raise RuntimeError(F"activation should be relu/gelu, not {activation}.")
|
|
||||||
|
|
||||||
|
|
||||||
class TransformerSALayer(nn.Module):
|
|
||||||
def __init__(self, embed_dim, nhead=8, dim_mlp=2048, dropout=0.0, activation="gelu"):
|
|
||||||
super().__init__()
|
|
||||||
self.self_attn = nn.MultiheadAttention(embed_dim, nhead, dropout=dropout)
|
|
||||||
# Implementation of Feedforward model - MLP
|
|
||||||
self.linear1 = nn.Linear(embed_dim, dim_mlp)
|
|
||||||
self.dropout = nn.Dropout(dropout)
|
|
||||||
self.linear2 = nn.Linear(dim_mlp, embed_dim)
|
|
||||||
|
|
||||||
self.norm1 = nn.LayerNorm(embed_dim)
|
|
||||||
self.norm2 = nn.LayerNorm(embed_dim)
|
|
||||||
self.dropout1 = nn.Dropout(dropout)
|
|
||||||
self.dropout2 = nn.Dropout(dropout)
|
|
||||||
|
|
||||||
self.activation = _get_activation_fn(activation)
|
|
||||||
|
|
||||||
def with_pos_embed(self, tensor, pos: Optional[Tensor]):
|
|
||||||
return tensor if pos is None else tensor + pos
|
|
||||||
|
|
||||||
def forward(self, tgt,
|
|
||||||
tgt_mask: Optional[Tensor] = None,
|
|
||||||
tgt_key_padding_mask: Optional[Tensor] = None,
|
|
||||||
query_pos: Optional[Tensor] = None):
|
|
||||||
|
|
||||||
# self attention
|
|
||||||
tgt2 = self.norm1(tgt)
|
|
||||||
q = k = self.with_pos_embed(tgt2, query_pos)
|
|
||||||
tgt2 = self.self_attn(q, k, value=tgt2, attn_mask=tgt_mask,
|
|
||||||
key_padding_mask=tgt_key_padding_mask)[0]
|
|
||||||
tgt = tgt + self.dropout1(tgt2)
|
|
||||||
|
|
||||||
# ffn
|
|
||||||
tgt2 = self.norm2(tgt)
|
|
||||||
tgt2 = self.linear2(self.dropout(self.activation(self.linear1(tgt2))))
|
|
||||||
tgt = tgt + self.dropout2(tgt2)
|
|
||||||
return tgt
|
|
||||||
|
|
||||||
class Fuse_sft_block(nn.Module):
|
|
||||||
def __init__(self, in_ch, out_ch):
|
|
||||||
super().__init__()
|
|
||||||
self.encode_enc = ResBlock(2*in_ch, out_ch)
|
|
||||||
|
|
||||||
self.scale = nn.Sequential(
|
|
||||||
nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1),
|
|
||||||
nn.LeakyReLU(0.2, True),
|
|
||||||
nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1))
|
|
||||||
|
|
||||||
self.shift = nn.Sequential(
|
|
||||||
nn.Conv2d(in_ch, out_ch, kernel_size=3, padding=1),
|
|
||||||
nn.LeakyReLU(0.2, True),
|
|
||||||
nn.Conv2d(out_ch, out_ch, kernel_size=3, padding=1))
|
|
||||||
|
|
||||||
def forward(self, enc_feat, dec_feat, w=1):
|
|
||||||
enc_feat = self.encode_enc(torch.cat([enc_feat, dec_feat], dim=1))
|
|
||||||
scale = self.scale(enc_feat)
|
|
||||||
shift = self.shift(enc_feat)
|
|
||||||
residual = w * (dec_feat * scale + shift)
|
|
||||||
out = dec_feat + residual
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
@ARCH_REGISTRY.register()
|
|
||||||
class CodeFormer(VQAutoEncoder):
|
|
||||||
def __init__(self, dim_embd=512, n_head=8, n_layers=9,
|
|
||||||
codebook_size=1024, latent_size=256,
|
|
||||||
connect_list=('32', '64', '128', '256'),
|
|
||||||
fix_modules=('quantize', 'generator')):
|
|
||||||
super(CodeFormer, self).__init__(512, 64, [1, 2, 2, 4, 4, 8], 'nearest',2, [16], codebook_size)
|
|
||||||
|
|
||||||
if fix_modules is not None:
|
|
||||||
for module in fix_modules:
|
|
||||||
for param in getattr(self, module).parameters():
|
|
||||||
param.requires_grad = False
|
|
||||||
|
|
||||||
self.connect_list = connect_list
|
|
||||||
self.n_layers = n_layers
|
|
||||||
self.dim_embd = dim_embd
|
|
||||||
self.dim_mlp = dim_embd*2
|
|
||||||
|
|
||||||
self.position_emb = nn.Parameter(torch.zeros(latent_size, self.dim_embd))
|
|
||||||
self.feat_emb = nn.Linear(256, self.dim_embd)
|
|
||||||
|
|
||||||
# transformer
|
|
||||||
self.ft_layers = nn.Sequential(*[TransformerSALayer(embed_dim=dim_embd, nhead=n_head, dim_mlp=self.dim_mlp, dropout=0.0)
|
|
||||||
for _ in range(self.n_layers)])
|
|
||||||
|
|
||||||
# logits_predict head
|
|
||||||
self.idx_pred_layer = nn.Sequential(
|
|
||||||
nn.LayerNorm(dim_embd),
|
|
||||||
nn.Linear(dim_embd, codebook_size, bias=False))
|
|
||||||
|
|
||||||
self.channels = {
|
|
||||||
'16': 512,
|
|
||||||
'32': 256,
|
|
||||||
'64': 256,
|
|
||||||
'128': 128,
|
|
||||||
'256': 128,
|
|
||||||
'512': 64,
|
|
||||||
}
|
|
||||||
|
|
||||||
# after second residual block for > 16, before attn layer for ==16
|
|
||||||
self.fuse_encoder_block = {'512':2, '256':5, '128':8, '64':11, '32':14, '16':18}
|
|
||||||
# after first residual block for > 16, before attn layer for ==16
|
|
||||||
self.fuse_generator_block = {'16':6, '32': 9, '64':12, '128':15, '256':18, '512':21}
|
|
||||||
|
|
||||||
# fuse_convs_dict
|
|
||||||
self.fuse_convs_dict = nn.ModuleDict()
|
|
||||||
for f_size in self.connect_list:
|
|
||||||
in_ch = self.channels[f_size]
|
|
||||||
self.fuse_convs_dict[f_size] = Fuse_sft_block(in_ch, in_ch)
|
|
||||||
|
|
||||||
def _init_weights(self, module):
|
|
||||||
if isinstance(module, (nn.Linear, nn.Embedding)):
|
|
||||||
module.weight.data.normal_(mean=0.0, std=0.02)
|
|
||||||
if isinstance(module, nn.Linear) and module.bias is not None:
|
|
||||||
module.bias.data.zero_()
|
|
||||||
elif isinstance(module, nn.LayerNorm):
|
|
||||||
module.bias.data.zero_()
|
|
||||||
module.weight.data.fill_(1.0)
|
|
||||||
|
|
||||||
def forward(self, x, w=0, detach_16=True, code_only=False, adain=False):
|
|
||||||
# ################### Encoder #####################
|
|
||||||
enc_feat_dict = {}
|
|
||||||
out_list = [self.fuse_encoder_block[f_size] for f_size in self.connect_list]
|
|
||||||
for i, block in enumerate(self.encoder.blocks):
|
|
||||||
x = block(x)
|
|
||||||
if i in out_list:
|
|
||||||
enc_feat_dict[str(x.shape[-1])] = x.clone()
|
|
||||||
|
|
||||||
lq_feat = x
|
|
||||||
# ################# Transformer ###################
|
|
||||||
# quant_feat, codebook_loss, quant_stats = self.quantize(lq_feat)
|
|
||||||
pos_emb = self.position_emb.unsqueeze(1).repeat(1,x.shape[0],1)
|
|
||||||
# BCHW -> BC(HW) -> (HW)BC
|
|
||||||
feat_emb = self.feat_emb(lq_feat.flatten(2).permute(2,0,1))
|
|
||||||
query_emb = feat_emb
|
|
||||||
# Transformer encoder
|
|
||||||
for layer in self.ft_layers:
|
|
||||||
query_emb = layer(query_emb, query_pos=pos_emb)
|
|
||||||
|
|
||||||
# output logits
|
|
||||||
logits = self.idx_pred_layer(query_emb) # (hw)bn
|
|
||||||
logits = logits.permute(1,0,2) # (hw)bn -> b(hw)n
|
|
||||||
|
|
||||||
if code_only: # for training stage II
|
|
||||||
# logits doesn't need softmax before cross_entropy loss
|
|
||||||
return logits, lq_feat
|
|
||||||
|
|
||||||
# ################# Quantization ###################
|
|
||||||
# if self.training:
|
|
||||||
# quant_feat = torch.einsum('btn,nc->btc', [soft_one_hot, self.quantize.embedding.weight])
|
|
||||||
# # b(hw)c -> bc(hw) -> bchw
|
|
||||||
# quant_feat = quant_feat.permute(0,2,1).view(lq_feat.shape)
|
|
||||||
# ------------
|
|
||||||
soft_one_hot = F.softmax(logits, dim=2)
|
|
||||||
_, top_idx = torch.topk(soft_one_hot, 1, dim=2)
|
|
||||||
quant_feat = self.quantize.get_codebook_feat(top_idx, shape=[x.shape[0],16,16,256])
|
|
||||||
# preserve gradients
|
|
||||||
# quant_feat = lq_feat + (quant_feat - lq_feat).detach()
|
|
||||||
|
|
||||||
if detach_16:
|
|
||||||
quant_feat = quant_feat.detach() # for training stage III
|
|
||||||
if adain:
|
|
||||||
quant_feat = adaptive_instance_normalization(quant_feat, lq_feat)
|
|
||||||
|
|
||||||
# ################## Generator ####################
|
|
||||||
x = quant_feat
|
|
||||||
fuse_list = [self.fuse_generator_block[f_size] for f_size in self.connect_list]
|
|
||||||
|
|
||||||
for i, block in enumerate(self.generator.blocks):
|
|
||||||
x = block(x)
|
|
||||||
if i in fuse_list: # fuse after i-th block
|
|
||||||
f_size = str(x.shape[-1])
|
|
||||||
if w>0:
|
|
||||||
x = self.fuse_convs_dict[f_size](enc_feat_dict[f_size].detach(), x, w)
|
|
||||||
out = x
|
|
||||||
# logits doesn't need softmax before cross_entropy loss
|
|
||||||
return out, logits, lq_feat
|
|
||||||
@@ -1,435 +0,0 @@
|
|||||||
# this file is copied from CodeFormer repository. Please see comment in modules/codeformer_model.py
|
|
||||||
|
|
||||||
'''
|
|
||||||
VQGAN code, adapted from the original created by the Unleashing Transformers authors:
|
|
||||||
https://github.com/samb-t/unleashing-transformers/blob/master/models/vqgan.py
|
|
||||||
|
|
||||||
'''
|
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
import torch.nn.functional as F
|
|
||||||
from basicsr.utils import get_root_logger
|
|
||||||
from basicsr.utils.registry import ARCH_REGISTRY
|
|
||||||
|
|
||||||
def normalize(in_channels):
|
|
||||||
return torch.nn.GroupNorm(num_groups=32, num_channels=in_channels, eps=1e-6, affine=True)
|
|
||||||
|
|
||||||
|
|
||||||
@torch.jit.script
|
|
||||||
def swish(x):
|
|
||||||
return x*torch.sigmoid(x)
|
|
||||||
|
|
||||||
|
|
||||||
# Define VQVAE classes
|
|
||||||
class VectorQuantizer(nn.Module):
|
|
||||||
def __init__(self, codebook_size, emb_dim, beta):
|
|
||||||
super(VectorQuantizer, self).__init__()
|
|
||||||
self.codebook_size = codebook_size # number of embeddings
|
|
||||||
self.emb_dim = emb_dim # dimension of embedding
|
|
||||||
self.beta = beta # commitment cost used in loss term, beta * ||z_e(x)-sg[e]||^2
|
|
||||||
self.embedding = nn.Embedding(self.codebook_size, self.emb_dim)
|
|
||||||
self.embedding.weight.data.uniform_(-1.0 / self.codebook_size, 1.0 / self.codebook_size)
|
|
||||||
|
|
||||||
def forward(self, z):
|
|
||||||
# reshape z -> (batch, height, width, channel) and flatten
|
|
||||||
z = z.permute(0, 2, 3, 1).contiguous()
|
|
||||||
z_flattened = z.view(-1, self.emb_dim)
|
|
||||||
|
|
||||||
# distances from z to embeddings e_j (z - e)^2 = z^2 + e^2 - 2 e * z
|
|
||||||
d = (z_flattened ** 2).sum(dim=1, keepdim=True) + (self.embedding.weight**2).sum(1) - \
|
|
||||||
2 * torch.matmul(z_flattened, self.embedding.weight.t())
|
|
||||||
|
|
||||||
mean_distance = torch.mean(d)
|
|
||||||
# find closest encodings
|
|
||||||
# min_encoding_indices = torch.argmin(d, dim=1).unsqueeze(1)
|
|
||||||
min_encoding_scores, min_encoding_indices = torch.topk(d, 1, dim=1, largest=False)
|
|
||||||
# [0-1], higher score, higher confidence
|
|
||||||
min_encoding_scores = torch.exp(-min_encoding_scores/10)
|
|
||||||
|
|
||||||
min_encodings = torch.zeros(min_encoding_indices.shape[0], self.codebook_size).to(z)
|
|
||||||
min_encodings.scatter_(1, min_encoding_indices, 1)
|
|
||||||
|
|
||||||
# get quantized latent vectors
|
|
||||||
z_q = torch.matmul(min_encodings, self.embedding.weight).view(z.shape)
|
|
||||||
# compute loss for embedding
|
|
||||||
loss = torch.mean((z_q.detach()-z)**2) + self.beta * torch.mean((z_q - z.detach()) ** 2)
|
|
||||||
# preserve gradients
|
|
||||||
z_q = z + (z_q - z).detach()
|
|
||||||
|
|
||||||
# perplexity
|
|
||||||
e_mean = torch.mean(min_encodings, dim=0)
|
|
||||||
perplexity = torch.exp(-torch.sum(e_mean * torch.log(e_mean + 1e-10)))
|
|
||||||
# reshape back to match original input shape
|
|
||||||
z_q = z_q.permute(0, 3, 1, 2).contiguous()
|
|
||||||
|
|
||||||
return z_q, loss, {
|
|
||||||
"perplexity": perplexity,
|
|
||||||
"min_encodings": min_encodings,
|
|
||||||
"min_encoding_indices": min_encoding_indices,
|
|
||||||
"min_encoding_scores": min_encoding_scores,
|
|
||||||
"mean_distance": mean_distance
|
|
||||||
}
|
|
||||||
|
|
||||||
def get_codebook_feat(self, indices, shape):
|
|
||||||
# input indices: batch*token_num -> (batch*token_num)*1
|
|
||||||
# shape: batch, height, width, channel
|
|
||||||
indices = indices.view(-1,1)
|
|
||||||
min_encodings = torch.zeros(indices.shape[0], self.codebook_size).to(indices)
|
|
||||||
min_encodings.scatter_(1, indices, 1)
|
|
||||||
# get quantized latent vectors
|
|
||||||
z_q = torch.matmul(min_encodings.float(), self.embedding.weight)
|
|
||||||
|
|
||||||
if shape is not None: # reshape back to match original input shape
|
|
||||||
z_q = z_q.view(shape).permute(0, 3, 1, 2).contiguous()
|
|
||||||
|
|
||||||
return z_q
|
|
||||||
|
|
||||||
|
|
||||||
class GumbelQuantizer(nn.Module):
|
|
||||||
def __init__(self, codebook_size, emb_dim, num_hiddens, straight_through=False, kl_weight=5e-4, temp_init=1.0):
|
|
||||||
super().__init__()
|
|
||||||
self.codebook_size = codebook_size # number of embeddings
|
|
||||||
self.emb_dim = emb_dim # dimension of embedding
|
|
||||||
self.straight_through = straight_through
|
|
||||||
self.temperature = temp_init
|
|
||||||
self.kl_weight = kl_weight
|
|
||||||
self.proj = nn.Conv2d(num_hiddens, codebook_size, 1) # projects last encoder layer to quantized logits
|
|
||||||
self.embed = nn.Embedding(codebook_size, emb_dim)
|
|
||||||
|
|
||||||
def forward(self, z):
|
|
||||||
hard = self.straight_through if self.training else True
|
|
||||||
|
|
||||||
logits = self.proj(z)
|
|
||||||
|
|
||||||
soft_one_hot = F.gumbel_softmax(logits, tau=self.temperature, dim=1, hard=hard)
|
|
||||||
|
|
||||||
z_q = torch.einsum("b n h w, n d -> b d h w", soft_one_hot, self.embed.weight)
|
|
||||||
|
|
||||||
# + kl divergence to the prior loss
|
|
||||||
qy = F.softmax(logits, dim=1)
|
|
||||||
diff = self.kl_weight * torch.sum(qy * torch.log(qy * self.codebook_size + 1e-10), dim=1).mean()
|
|
||||||
min_encoding_indices = soft_one_hot.argmax(dim=1)
|
|
||||||
|
|
||||||
return z_q, diff, {
|
|
||||||
"min_encoding_indices": min_encoding_indices
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class Downsample(nn.Module):
|
|
||||||
def __init__(self, in_channels):
|
|
||||||
super().__init__()
|
|
||||||
self.conv = torch.nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=2, padding=0)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
pad = (0, 1, 0, 1)
|
|
||||||
x = torch.nn.functional.pad(x, pad, mode="constant", value=0)
|
|
||||||
x = self.conv(x)
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Upsample(nn.Module):
|
|
||||||
def __init__(self, in_channels):
|
|
||||||
super().__init__()
|
|
||||||
self.conv = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = F.interpolate(x, scale_factor=2.0, mode="nearest")
|
|
||||||
x = self.conv(x)
|
|
||||||
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class ResBlock(nn.Module):
|
|
||||||
def __init__(self, in_channels, out_channels=None):
|
|
||||||
super(ResBlock, self).__init__()
|
|
||||||
self.in_channels = in_channels
|
|
||||||
self.out_channels = in_channels if out_channels is None else out_channels
|
|
||||||
self.norm1 = normalize(in_channels)
|
|
||||||
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=1, padding=1)
|
|
||||||
self.norm2 = normalize(out_channels)
|
|
||||||
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1)
|
|
||||||
if self.in_channels != self.out_channels:
|
|
||||||
self.conv_out = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding=0)
|
|
||||||
|
|
||||||
def forward(self, x_in):
|
|
||||||
x = x_in
|
|
||||||
x = self.norm1(x)
|
|
||||||
x = swish(x)
|
|
||||||
x = self.conv1(x)
|
|
||||||
x = self.norm2(x)
|
|
||||||
x = swish(x)
|
|
||||||
x = self.conv2(x)
|
|
||||||
if self.in_channels != self.out_channels:
|
|
||||||
x_in = self.conv_out(x_in)
|
|
||||||
|
|
||||||
return x + x_in
|
|
||||||
|
|
||||||
|
|
||||||
class AttnBlock(nn.Module):
|
|
||||||
def __init__(self, in_channels):
|
|
||||||
super().__init__()
|
|
||||||
self.in_channels = in_channels
|
|
||||||
|
|
||||||
self.norm = normalize(in_channels)
|
|
||||||
self.q = torch.nn.Conv2d(
|
|
||||||
in_channels,
|
|
||||||
in_channels,
|
|
||||||
kernel_size=1,
|
|
||||||
stride=1,
|
|
||||||
padding=0
|
|
||||||
)
|
|
||||||
self.k = torch.nn.Conv2d(
|
|
||||||
in_channels,
|
|
||||||
in_channels,
|
|
||||||
kernel_size=1,
|
|
||||||
stride=1,
|
|
||||||
padding=0
|
|
||||||
)
|
|
||||||
self.v = torch.nn.Conv2d(
|
|
||||||
in_channels,
|
|
||||||
in_channels,
|
|
||||||
kernel_size=1,
|
|
||||||
stride=1,
|
|
||||||
padding=0
|
|
||||||
)
|
|
||||||
self.proj_out = torch.nn.Conv2d(
|
|
||||||
in_channels,
|
|
||||||
in_channels,
|
|
||||||
kernel_size=1,
|
|
||||||
stride=1,
|
|
||||||
padding=0
|
|
||||||
)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
h_ = x
|
|
||||||
h_ = self.norm(h_)
|
|
||||||
q = self.q(h_)
|
|
||||||
k = self.k(h_)
|
|
||||||
v = self.v(h_)
|
|
||||||
|
|
||||||
# compute attention
|
|
||||||
b, c, h, w = q.shape
|
|
||||||
q = q.reshape(b, c, h*w)
|
|
||||||
q = q.permute(0, 2, 1)
|
|
||||||
k = k.reshape(b, c, h*w)
|
|
||||||
w_ = torch.bmm(q, k)
|
|
||||||
w_ = w_ * (int(c)**(-0.5))
|
|
||||||
w_ = F.softmax(w_, dim=2)
|
|
||||||
|
|
||||||
# attend to values
|
|
||||||
v = v.reshape(b, c, h*w)
|
|
||||||
w_ = w_.permute(0, 2, 1)
|
|
||||||
h_ = torch.bmm(v, w_)
|
|
||||||
h_ = h_.reshape(b, c, h, w)
|
|
||||||
|
|
||||||
h_ = self.proj_out(h_)
|
|
||||||
|
|
||||||
return x+h_
|
|
||||||
|
|
||||||
|
|
||||||
class Encoder(nn.Module):
|
|
||||||
def __init__(self, in_channels, nf, emb_dim, ch_mult, num_res_blocks, resolution, attn_resolutions):
|
|
||||||
super().__init__()
|
|
||||||
self.nf = nf
|
|
||||||
self.num_resolutions = len(ch_mult)
|
|
||||||
self.num_res_blocks = num_res_blocks
|
|
||||||
self.resolution = resolution
|
|
||||||
self.attn_resolutions = attn_resolutions
|
|
||||||
|
|
||||||
curr_res = self.resolution
|
|
||||||
in_ch_mult = (1,)+tuple(ch_mult)
|
|
||||||
|
|
||||||
blocks = []
|
|
||||||
# initial convultion
|
|
||||||
blocks.append(nn.Conv2d(in_channels, nf, kernel_size=3, stride=1, padding=1))
|
|
||||||
|
|
||||||
# residual and downsampling blocks, with attention on smaller res (16x16)
|
|
||||||
for i in range(self.num_resolutions):
|
|
||||||
block_in_ch = nf * in_ch_mult[i]
|
|
||||||
block_out_ch = nf * ch_mult[i]
|
|
||||||
for _ in range(self.num_res_blocks):
|
|
||||||
blocks.append(ResBlock(block_in_ch, block_out_ch))
|
|
||||||
block_in_ch = block_out_ch
|
|
||||||
if curr_res in attn_resolutions:
|
|
||||||
blocks.append(AttnBlock(block_in_ch))
|
|
||||||
|
|
||||||
if i != self.num_resolutions - 1:
|
|
||||||
blocks.append(Downsample(block_in_ch))
|
|
||||||
curr_res = curr_res // 2
|
|
||||||
|
|
||||||
# non-local attention block
|
|
||||||
blocks.append(ResBlock(block_in_ch, block_in_ch))
|
|
||||||
blocks.append(AttnBlock(block_in_ch))
|
|
||||||
blocks.append(ResBlock(block_in_ch, block_in_ch))
|
|
||||||
|
|
||||||
# normalise and convert to latent size
|
|
||||||
blocks.append(normalize(block_in_ch))
|
|
||||||
blocks.append(nn.Conv2d(block_in_ch, emb_dim, kernel_size=3, stride=1, padding=1))
|
|
||||||
self.blocks = nn.ModuleList(blocks)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
for block in self.blocks:
|
|
||||||
x = block(x)
|
|
||||||
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
class Generator(nn.Module):
|
|
||||||
def __init__(self, nf, emb_dim, ch_mult, res_blocks, img_size, attn_resolutions):
|
|
||||||
super().__init__()
|
|
||||||
self.nf = nf
|
|
||||||
self.ch_mult = ch_mult
|
|
||||||
self.num_resolutions = len(self.ch_mult)
|
|
||||||
self.num_res_blocks = res_blocks
|
|
||||||
self.resolution = img_size
|
|
||||||
self.attn_resolutions = attn_resolutions
|
|
||||||
self.in_channels = emb_dim
|
|
||||||
self.out_channels = 3
|
|
||||||
block_in_ch = self.nf * self.ch_mult[-1]
|
|
||||||
curr_res = self.resolution // 2 ** (self.num_resolutions-1)
|
|
||||||
|
|
||||||
blocks = []
|
|
||||||
# initial conv
|
|
||||||
blocks.append(nn.Conv2d(self.in_channels, block_in_ch, kernel_size=3, stride=1, padding=1))
|
|
||||||
|
|
||||||
# non-local attention block
|
|
||||||
blocks.append(ResBlock(block_in_ch, block_in_ch))
|
|
||||||
blocks.append(AttnBlock(block_in_ch))
|
|
||||||
blocks.append(ResBlock(block_in_ch, block_in_ch))
|
|
||||||
|
|
||||||
for i in reversed(range(self.num_resolutions)):
|
|
||||||
block_out_ch = self.nf * self.ch_mult[i]
|
|
||||||
|
|
||||||
for _ in range(self.num_res_blocks):
|
|
||||||
blocks.append(ResBlock(block_in_ch, block_out_ch))
|
|
||||||
block_in_ch = block_out_ch
|
|
||||||
|
|
||||||
if curr_res in self.attn_resolutions:
|
|
||||||
blocks.append(AttnBlock(block_in_ch))
|
|
||||||
|
|
||||||
if i != 0:
|
|
||||||
blocks.append(Upsample(block_in_ch))
|
|
||||||
curr_res = curr_res * 2
|
|
||||||
|
|
||||||
blocks.append(normalize(block_in_ch))
|
|
||||||
blocks.append(nn.Conv2d(block_in_ch, self.out_channels, kernel_size=3, stride=1, padding=1))
|
|
||||||
|
|
||||||
self.blocks = nn.ModuleList(blocks)
|
|
||||||
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
for block in self.blocks:
|
|
||||||
x = block(x)
|
|
||||||
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
@ARCH_REGISTRY.register()
|
|
||||||
class VQAutoEncoder(nn.Module):
|
|
||||||
def __init__(self, img_size, nf, ch_mult, quantizer="nearest", res_blocks=2, attn_resolutions=None, codebook_size=1024, emb_dim=256,
|
|
||||||
beta=0.25, gumbel_straight_through=False, gumbel_kl_weight=1e-8, model_path=None):
|
|
||||||
super().__init__()
|
|
||||||
logger = get_root_logger()
|
|
||||||
self.in_channels = 3
|
|
||||||
self.nf = nf
|
|
||||||
self.n_blocks = res_blocks
|
|
||||||
self.codebook_size = codebook_size
|
|
||||||
self.embed_dim = emb_dim
|
|
||||||
self.ch_mult = ch_mult
|
|
||||||
self.resolution = img_size
|
|
||||||
self.attn_resolutions = attn_resolutions or [16]
|
|
||||||
self.quantizer_type = quantizer
|
|
||||||
self.encoder = Encoder(
|
|
||||||
self.in_channels,
|
|
||||||
self.nf,
|
|
||||||
self.embed_dim,
|
|
||||||
self.ch_mult,
|
|
||||||
self.n_blocks,
|
|
||||||
self.resolution,
|
|
||||||
self.attn_resolutions
|
|
||||||
)
|
|
||||||
if self.quantizer_type == "nearest":
|
|
||||||
self.beta = beta #0.25
|
|
||||||
self.quantize = VectorQuantizer(self.codebook_size, self.embed_dim, self.beta)
|
|
||||||
elif self.quantizer_type == "gumbel":
|
|
||||||
self.gumbel_num_hiddens = emb_dim
|
|
||||||
self.straight_through = gumbel_straight_through
|
|
||||||
self.kl_weight = gumbel_kl_weight
|
|
||||||
self.quantize = GumbelQuantizer(
|
|
||||||
self.codebook_size,
|
|
||||||
self.embed_dim,
|
|
||||||
self.gumbel_num_hiddens,
|
|
||||||
self.straight_through,
|
|
||||||
self.kl_weight
|
|
||||||
)
|
|
||||||
self.generator = Generator(
|
|
||||||
self.nf,
|
|
||||||
self.embed_dim,
|
|
||||||
self.ch_mult,
|
|
||||||
self.n_blocks,
|
|
||||||
self.resolution,
|
|
||||||
self.attn_resolutions
|
|
||||||
)
|
|
||||||
|
|
||||||
if model_path is not None:
|
|
||||||
chkpt = torch.load(model_path, map_location='cpu')
|
|
||||||
if 'params_ema' in chkpt:
|
|
||||||
self.load_state_dict(torch.load(model_path, map_location='cpu')['params_ema'])
|
|
||||||
logger.info(f'vqgan is loaded from: {model_path} [params_ema]')
|
|
||||||
elif 'params' in chkpt:
|
|
||||||
self.load_state_dict(torch.load(model_path, map_location='cpu')['params'])
|
|
||||||
logger.info(f'vqgan is loaded from: {model_path} [params]')
|
|
||||||
else:
|
|
||||||
raise ValueError('Wrong params!')
|
|
||||||
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x = self.encoder(x)
|
|
||||||
quant, codebook_loss, quant_stats = self.quantize(x)
|
|
||||||
x = self.generator(quant)
|
|
||||||
return x, codebook_loss, quant_stats
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# patch based discriminator
|
|
||||||
@ARCH_REGISTRY.register()
|
|
||||||
class VQGANDiscriminator(nn.Module):
|
|
||||||
def __init__(self, nc=3, ndf=64, n_layers=4, model_path=None):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
layers = [nn.Conv2d(nc, ndf, kernel_size=4, stride=2, padding=1), nn.LeakyReLU(0.2, True)]
|
|
||||||
ndf_mult = 1
|
|
||||||
ndf_mult_prev = 1
|
|
||||||
for n in range(1, n_layers): # gradually increase the number of filters
|
|
||||||
ndf_mult_prev = ndf_mult
|
|
||||||
ndf_mult = min(2 ** n, 8)
|
|
||||||
layers += [
|
|
||||||
nn.Conv2d(ndf * ndf_mult_prev, ndf * ndf_mult, kernel_size=4, stride=2, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(ndf * ndf_mult),
|
|
||||||
nn.LeakyReLU(0.2, True)
|
|
||||||
]
|
|
||||||
|
|
||||||
ndf_mult_prev = ndf_mult
|
|
||||||
ndf_mult = min(2 ** n_layers, 8)
|
|
||||||
|
|
||||||
layers += [
|
|
||||||
nn.Conv2d(ndf * ndf_mult_prev, ndf * ndf_mult, kernel_size=4, stride=1, padding=1, bias=False),
|
|
||||||
nn.BatchNorm2d(ndf * ndf_mult),
|
|
||||||
nn.LeakyReLU(0.2, True)
|
|
||||||
]
|
|
||||||
|
|
||||||
layers += [
|
|
||||||
nn.Conv2d(ndf * ndf_mult, 1, kernel_size=4, stride=1, padding=1)] # output 1 channel prediction map
|
|
||||||
self.main = nn.Sequential(*layers)
|
|
||||||
|
|
||||||
if model_path is not None:
|
|
||||||
chkpt = torch.load(model_path, map_location='cpu')
|
|
||||||
if 'params_d' in chkpt:
|
|
||||||
self.load_state_dict(torch.load(model_path, map_location='cpu')['params_d'])
|
|
||||||
elif 'params' in chkpt:
|
|
||||||
self.load_state_dict(torch.load(model_path, map_location='cpu')['params'])
|
|
||||||
else:
|
|
||||||
raise ValueError('Wrong params!')
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
return self.main(x)
|
|
||||||
+49
-117
@@ -1,132 +1,64 @@
|
|||||||
import os
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
|
||||||
import cv2
|
|
||||||
import torch
|
import torch
|
||||||
|
|
||||||
import modules.face_restoration
|
from modules import (
|
||||||
import modules.shared
|
devices,
|
||||||
from modules import shared, devices, modelloader, errors
|
errors,
|
||||||
from modules.paths import models_path
|
face_restoration,
|
||||||
|
face_restoration_utils,
|
||||||
|
modelloader,
|
||||||
|
shared,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
# codeformer people made a choice to include modified basicsr library to their project which makes
|
|
||||||
# it utterly impossible to use it alongside with other libraries that also use basicsr, like GFPGAN.
|
|
||||||
# I am making a choice to include some files from codeformer to work around this issue.
|
|
||||||
model_dir = "Codeformer"
|
|
||||||
model_path = os.path.join(models_path, model_dir)
|
|
||||||
model_url = 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth'
|
model_url = 'https://github.com/sczhou/CodeFormer/releases/download/v0.1.0/codeformer.pth'
|
||||||
|
model_download_name = 'codeformer-v0.1.0.pth'
|
||||||
|
|
||||||
codeformer = None
|
# used by e.g. postprocessing_codeformer.py
|
||||||
|
codeformer: face_restoration.FaceRestoration | None = None
|
||||||
|
|
||||||
|
|
||||||
def setup_model(dirname):
|
class FaceRestorerCodeFormer(face_restoration_utils.CommonFaceRestoration):
|
||||||
os.makedirs(model_path, exist_ok=True)
|
def name(self):
|
||||||
|
return "CodeFormer"
|
||||||
|
|
||||||
path = modules.paths.paths.get("CodeFormer", None)
|
def load_net(self) -> torch.Module:
|
||||||
if path is None:
|
for model_path in modelloader.load_models(
|
||||||
return
|
model_path=self.model_path,
|
||||||
|
model_url=model_url,
|
||||||
|
command_path=self.model_path,
|
||||||
|
download_name=model_download_name,
|
||||||
|
ext_filter=['.pth'],
|
||||||
|
):
|
||||||
|
return modelloader.load_spandrel_model(
|
||||||
|
model_path,
|
||||||
|
device=devices.device_codeformer,
|
||||||
|
expected_architecture='CodeFormer',
|
||||||
|
).model
|
||||||
|
raise ValueError("No codeformer model found")
|
||||||
|
|
||||||
|
def get_device(self):
|
||||||
|
return devices.device_codeformer
|
||||||
|
|
||||||
|
def restore(self, np_image, w: float | None = None):
|
||||||
|
if w is None:
|
||||||
|
w = getattr(shared.opts, "code_former_weight", 0.5)
|
||||||
|
|
||||||
|
def restore_face(cropped_face_t):
|
||||||
|
assert self.net is not None
|
||||||
|
return self.net(cropped_face_t, w=w, adain=True)[0]
|
||||||
|
|
||||||
|
return self.restore_with_helper(np_image, restore_face)
|
||||||
|
|
||||||
|
|
||||||
|
def setup_model(dirname: str) -> None:
|
||||||
|
global codeformer
|
||||||
try:
|
try:
|
||||||
from torchvision.transforms.functional import normalize
|
|
||||||
from modules.codeformer.codeformer_arch import CodeFormer
|
|
||||||
from basicsr.utils import img2tensor, tensor2img
|
|
||||||
from facelib.utils.face_restoration_helper import FaceRestoreHelper
|
|
||||||
from facelib.detection.retinaface import retinaface
|
|
||||||
|
|
||||||
net_class = CodeFormer
|
|
||||||
|
|
||||||
class FaceRestorerCodeFormer(modules.face_restoration.FaceRestoration):
|
|
||||||
def name(self):
|
|
||||||
return "CodeFormer"
|
|
||||||
|
|
||||||
def __init__(self, dirname):
|
|
||||||
self.net = None
|
|
||||||
self.face_helper = None
|
|
||||||
self.cmd_dir = dirname
|
|
||||||
|
|
||||||
def create_models(self):
|
|
||||||
|
|
||||||
if self.net is not None and self.face_helper is not None:
|
|
||||||
self.net.to(devices.device_codeformer)
|
|
||||||
return self.net, self.face_helper
|
|
||||||
model_paths = modelloader.load_models(model_path, model_url, self.cmd_dir, download_name='codeformer-v0.1.0.pth', ext_filter=['.pth'])
|
|
||||||
if len(model_paths) != 0:
|
|
||||||
ckpt_path = model_paths[0]
|
|
||||||
else:
|
|
||||||
print("Unable to load codeformer model.")
|
|
||||||
return None, None
|
|
||||||
net = net_class(dim_embd=512, codebook_size=1024, n_head=8, n_layers=9, connect_list=['32', '64', '128', '256']).to(devices.device_codeformer)
|
|
||||||
checkpoint = torch.load(ckpt_path)['params_ema']
|
|
||||||
net.load_state_dict(checkpoint)
|
|
||||||
net.eval()
|
|
||||||
|
|
||||||
if hasattr(retinaface, 'device'):
|
|
||||||
retinaface.device = devices.device_codeformer
|
|
||||||
face_helper = FaceRestoreHelper(1, face_size=512, crop_ratio=(1, 1), det_model='retinaface_resnet50', save_ext='png', use_parse=True, device=devices.device_codeformer)
|
|
||||||
|
|
||||||
self.net = net
|
|
||||||
self.face_helper = face_helper
|
|
||||||
|
|
||||||
return net, face_helper
|
|
||||||
|
|
||||||
def send_model_to(self, device):
|
|
||||||
self.net.to(device)
|
|
||||||
self.face_helper.face_det.to(device)
|
|
||||||
self.face_helper.face_parse.to(device)
|
|
||||||
|
|
||||||
def restore(self, np_image, w=None):
|
|
||||||
np_image = np_image[:, :, ::-1]
|
|
||||||
|
|
||||||
original_resolution = np_image.shape[0:2]
|
|
||||||
|
|
||||||
self.create_models()
|
|
||||||
if self.net is None or self.face_helper is None:
|
|
||||||
return np_image
|
|
||||||
|
|
||||||
self.send_model_to(devices.device_codeformer)
|
|
||||||
|
|
||||||
self.face_helper.clean_all()
|
|
||||||
self.face_helper.read_image(np_image)
|
|
||||||
self.face_helper.get_face_landmarks_5(only_center_face=False, resize=640, eye_dist_threshold=5)
|
|
||||||
self.face_helper.align_warp_face()
|
|
||||||
|
|
||||||
for cropped_face in self.face_helper.cropped_faces:
|
|
||||||
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
|
|
||||||
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
|
|
||||||
cropped_face_t = cropped_face_t.unsqueeze(0).to(devices.device_codeformer)
|
|
||||||
|
|
||||||
try:
|
|
||||||
with torch.no_grad():
|
|
||||||
output = self.net(cropped_face_t, w=w if w is not None else shared.opts.code_former_weight, adain=True)[0]
|
|
||||||
restored_face = tensor2img(output, rgb2bgr=True, min_max=(-1, 1))
|
|
||||||
del output
|
|
||||||
devices.torch_gc()
|
|
||||||
except Exception:
|
|
||||||
errors.report('Failed inference for CodeFormer', exc_info=True)
|
|
||||||
restored_face = tensor2img(cropped_face_t, rgb2bgr=True, min_max=(-1, 1))
|
|
||||||
|
|
||||||
restored_face = restored_face.astype('uint8')
|
|
||||||
self.face_helper.add_restored_face(restored_face)
|
|
||||||
|
|
||||||
self.face_helper.get_inverse_affine(None)
|
|
||||||
|
|
||||||
restored_img = self.face_helper.paste_faces_to_input_image()
|
|
||||||
restored_img = restored_img[:, :, ::-1]
|
|
||||||
|
|
||||||
if original_resolution != restored_img.shape[0:2]:
|
|
||||||
restored_img = cv2.resize(restored_img, (0, 0), fx=original_resolution[1]/restored_img.shape[1], fy=original_resolution[0]/restored_img.shape[0], interpolation=cv2.INTER_LINEAR)
|
|
||||||
|
|
||||||
self.face_helper.clean_all()
|
|
||||||
|
|
||||||
if shared.opts.face_restoration_unload:
|
|
||||||
self.send_model_to(devices.cpu)
|
|
||||||
|
|
||||||
return restored_img
|
|
||||||
|
|
||||||
global codeformer
|
|
||||||
codeformer = FaceRestorerCodeFormer(dirname)
|
codeformer = FaceRestorerCodeFormer(dirname)
|
||||||
shared.face_restorers.append(codeformer)
|
shared.face_restorers.append(codeformer)
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
errors.report("Error setting up CodeFormer", exc_info=True)
|
errors.report("Error setting up CodeFormer", exc_info=True)
|
||||||
|
|
||||||
# sys.path = stored_sys_path
|
|
||||||
|
|||||||
@@ -4,18 +4,15 @@ Supports saving and restoring webui and extensions from a known working set of c
|
|||||||
|
|
||||||
import os
|
import os
|
||||||
import json
|
import json
|
||||||
import time
|
|
||||||
import tqdm
|
import tqdm
|
||||||
|
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from collections import OrderedDict
|
|
||||||
import git
|
import git
|
||||||
|
|
||||||
from modules import shared, extensions, errors
|
from modules import shared, extensions, errors
|
||||||
from modules.paths_internal import script_path, config_states_dir
|
from modules.paths_internal import script_path, config_states_dir
|
||||||
|
|
||||||
|
all_config_states = {}
|
||||||
all_config_states = OrderedDict()
|
|
||||||
|
|
||||||
|
|
||||||
def list_config_states():
|
def list_config_states():
|
||||||
@@ -28,15 +25,19 @@ def list_config_states():
|
|||||||
for filename in os.listdir(config_states_dir):
|
for filename in os.listdir(config_states_dir):
|
||||||
if filename.endswith(".json"):
|
if filename.endswith(".json"):
|
||||||
path = os.path.join(config_states_dir, filename)
|
path = os.path.join(config_states_dir, filename)
|
||||||
with open(path, "r", encoding="utf-8") as f:
|
try:
|
||||||
j = json.load(f)
|
with open(path, "r", encoding="utf-8") as f:
|
||||||
j["filepath"] = path
|
j = json.load(f)
|
||||||
config_states.append(j)
|
assert "created_at" in j, '"created_at" does not exist'
|
||||||
|
j["filepath"] = path
|
||||||
|
config_states.append(j)
|
||||||
|
except Exception as e:
|
||||||
|
print(f'[ERROR]: Config states {path}, {e}')
|
||||||
|
|
||||||
config_states = sorted(config_states, key=lambda cs: cs["created_at"], reverse=True)
|
config_states = sorted(config_states, key=lambda cs: cs["created_at"], reverse=True)
|
||||||
|
|
||||||
for cs in config_states:
|
for cs in config_states:
|
||||||
timestamp = time.asctime(time.gmtime(cs["created_at"]))
|
timestamp = datetime.fromtimestamp(cs["created_at"]).strftime('%Y-%m-%d %H:%M:%S')
|
||||||
name = cs.get("name", "Config")
|
name = cs.get("name", "Config")
|
||||||
full_name = f"{name}: {timestamp}"
|
full_name = f"{name}: {timestamp}"
|
||||||
all_config_states[full_name] = cs
|
all_config_states[full_name] = cs
|
||||||
|
|||||||
@@ -0,0 +1,79 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
from modules import modelloader, errors
|
||||||
|
from modules.shared import cmd_opts, opts
|
||||||
|
from modules.upscaler import Upscaler, UpscalerData
|
||||||
|
from modules.upscaler_utils import upscale_with_model
|
||||||
|
|
||||||
|
|
||||||
|
class UpscalerDAT(Upscaler):
|
||||||
|
def __init__(self, user_path):
|
||||||
|
self.name = "DAT"
|
||||||
|
self.user_path = user_path
|
||||||
|
self.scalers = []
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
for file in self.find_models(ext_filter=[".pt", ".pth"]):
|
||||||
|
name = modelloader.friendly_name(file)
|
||||||
|
scaler_data = UpscalerData(name, file, upscaler=self, scale=None)
|
||||||
|
self.scalers.append(scaler_data)
|
||||||
|
|
||||||
|
for model in get_dat_models(self):
|
||||||
|
if model.name in opts.dat_enabled_models:
|
||||||
|
self.scalers.append(model)
|
||||||
|
|
||||||
|
def do_upscale(self, img, path):
|
||||||
|
try:
|
||||||
|
info = self.load_model(path)
|
||||||
|
except Exception:
|
||||||
|
errors.report(f"Unable to load DAT model {path}", exc_info=True)
|
||||||
|
return img
|
||||||
|
|
||||||
|
model_descriptor = modelloader.load_spandrel_model(
|
||||||
|
info.local_data_path,
|
||||||
|
device=self.device,
|
||||||
|
prefer_half=(not cmd_opts.no_half and not cmd_opts.upcast_sampling),
|
||||||
|
expected_architecture="DAT",
|
||||||
|
)
|
||||||
|
return upscale_with_model(
|
||||||
|
model_descriptor,
|
||||||
|
img,
|
||||||
|
tile_size=opts.DAT_tile,
|
||||||
|
tile_overlap=opts.DAT_tile_overlap,
|
||||||
|
)
|
||||||
|
|
||||||
|
def load_model(self, path):
|
||||||
|
for scaler in self.scalers:
|
||||||
|
if scaler.data_path == path:
|
||||||
|
if scaler.local_data_path.startswith("http"):
|
||||||
|
scaler.local_data_path = modelloader.load_file_from_url(
|
||||||
|
scaler.data_path,
|
||||||
|
model_dir=self.model_download_path,
|
||||||
|
)
|
||||||
|
if not os.path.exists(scaler.local_data_path):
|
||||||
|
raise FileNotFoundError(f"DAT data missing: {scaler.local_data_path}")
|
||||||
|
return scaler
|
||||||
|
raise ValueError(f"Unable to find model info: {path}")
|
||||||
|
|
||||||
|
|
||||||
|
def get_dat_models(scaler):
|
||||||
|
return [
|
||||||
|
UpscalerData(
|
||||||
|
name="DAT x2",
|
||||||
|
path="https://github.com/n0kovo/dat_upscaler_models/raw/main/DAT/DAT_x2.pth",
|
||||||
|
scale=2,
|
||||||
|
upscaler=scaler,
|
||||||
|
),
|
||||||
|
UpscalerData(
|
||||||
|
name="DAT x3",
|
||||||
|
path="https://github.com/n0kovo/dat_upscaler_models/raw/main/DAT/DAT_x3.pth",
|
||||||
|
scale=3,
|
||||||
|
upscaler=scaler,
|
||||||
|
),
|
||||||
|
UpscalerData(
|
||||||
|
name="DAT x4",
|
||||||
|
path="https://github.com/n0kovo/dat_upscaler_models/raw/main/DAT/DAT_x4.pth",
|
||||||
|
scale=4,
|
||||||
|
upscaler=scaler,
|
||||||
|
),
|
||||||
|
]
|
||||||
+114
-30
@@ -3,11 +3,19 @@ import contextlib
|
|||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from modules import errors
|
from modules import errors, shared
|
||||||
|
from modules import torch_utils
|
||||||
|
|
||||||
if sys.platform == "darwin":
|
if sys.platform == "darwin":
|
||||||
from modules import mac_specific
|
from modules import mac_specific
|
||||||
|
|
||||||
|
if shared.cmd_opts.use_ipex:
|
||||||
|
from modules import xpu_specific
|
||||||
|
|
||||||
|
|
||||||
|
def has_xpu() -> bool:
|
||||||
|
return shared.cmd_opts.use_ipex and xpu_specific.has_xpu
|
||||||
|
|
||||||
|
|
||||||
def has_mps() -> bool:
|
def has_mps() -> bool:
|
||||||
if sys.platform != "darwin":
|
if sys.platform != "darwin":
|
||||||
@@ -16,9 +24,24 @@ def has_mps() -> bool:
|
|||||||
return mac_specific.has_mps
|
return mac_specific.has_mps
|
||||||
|
|
||||||
|
|
||||||
def get_cuda_device_string():
|
def cuda_no_autocast(device_id=None) -> bool:
|
||||||
from modules import shared
|
if device_id is None:
|
||||||
|
device_id = get_cuda_device_id()
|
||||||
|
return (
|
||||||
|
torch.cuda.get_device_capability(device_id) == (7, 5)
|
||||||
|
and torch.cuda.get_device_name(device_id).startswith("NVIDIA GeForce GTX 16")
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_cuda_device_id():
|
||||||
|
return (
|
||||||
|
int(shared.cmd_opts.device_id)
|
||||||
|
if shared.cmd_opts.device_id is not None and shared.cmd_opts.device_id.isdigit()
|
||||||
|
else 0
|
||||||
|
) or torch.cuda.current_device()
|
||||||
|
|
||||||
|
|
||||||
|
def get_cuda_device_string():
|
||||||
if shared.cmd_opts.device_id is not None:
|
if shared.cmd_opts.device_id is not None:
|
||||||
return f"cuda:{shared.cmd_opts.device_id}"
|
return f"cuda:{shared.cmd_opts.device_id}"
|
||||||
|
|
||||||
@@ -32,6 +55,9 @@ def get_optimal_device_name():
|
|||||||
if has_mps():
|
if has_mps():
|
||||||
return "mps"
|
return "mps"
|
||||||
|
|
||||||
|
if has_xpu():
|
||||||
|
return xpu_specific.get_xpu_device_string()
|
||||||
|
|
||||||
return "cpu"
|
return "cpu"
|
||||||
|
|
||||||
|
|
||||||
@@ -40,9 +66,7 @@ def get_optimal_device():
|
|||||||
|
|
||||||
|
|
||||||
def get_device_for(task):
|
def get_device_for(task):
|
||||||
from modules import shared
|
if task in shared.cmd_opts.use_cpu or "all" in shared.cmd_opts.use_cpu:
|
||||||
|
|
||||||
if task in shared.cmd_opts.use_cpu:
|
|
||||||
return cpu
|
return cpu
|
||||||
|
|
||||||
return get_optimal_device()
|
return get_optimal_device()
|
||||||
@@ -58,27 +82,35 @@ def torch_gc():
|
|||||||
if has_mps():
|
if has_mps():
|
||||||
mac_specific.torch_mps_gc()
|
mac_specific.torch_mps_gc()
|
||||||
|
|
||||||
|
if has_xpu():
|
||||||
|
xpu_specific.torch_xpu_gc()
|
||||||
|
|
||||||
|
|
||||||
def enable_tf32():
|
def enable_tf32():
|
||||||
if torch.cuda.is_available():
|
if torch.cuda.is_available():
|
||||||
|
|
||||||
# enabling benchmark option seems to enable a range of cards to do fp16 when they otherwise can't
|
# enabling benchmark option seems to enable a range of cards to do fp16 when they otherwise can't
|
||||||
# see https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/4407
|
# see https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/4407
|
||||||
if any(torch.cuda.get_device_capability(devid) == (7, 5) for devid in range(0, torch.cuda.device_count())):
|
if cuda_no_autocast():
|
||||||
torch.backends.cudnn.benchmark = True
|
torch.backends.cudnn.benchmark = True
|
||||||
|
|
||||||
torch.backends.cuda.matmul.allow_tf32 = True
|
torch.backends.cuda.matmul.allow_tf32 = True
|
||||||
torch.backends.cudnn.allow_tf32 = True
|
torch.backends.cudnn.allow_tf32 = True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
errors.run(enable_tf32, "Enabling TF32")
|
errors.run(enable_tf32, "Enabling TF32")
|
||||||
|
|
||||||
cpu = torch.device("cpu")
|
cpu: torch.device = torch.device("cpu")
|
||||||
device = device_interrogate = device_gfpgan = device_esrgan = device_codeformer = None
|
fp8: bool = False
|
||||||
dtype = torch.float16
|
device: torch.device = None
|
||||||
dtype_vae = torch.float16
|
device_interrogate: torch.device = None
|
||||||
dtype_unet = torch.float16
|
device_gfpgan: torch.device = None
|
||||||
|
device_esrgan: torch.device = None
|
||||||
|
device_codeformer: torch.device = None
|
||||||
|
dtype: torch.dtype = torch.float16
|
||||||
|
dtype_vae: torch.dtype = torch.float16
|
||||||
|
dtype_unet: torch.dtype = torch.float16
|
||||||
|
dtype_inference: torch.dtype = torch.float16
|
||||||
unet_needs_upcast = False
|
unet_needs_upcast = False
|
||||||
|
|
||||||
|
|
||||||
@@ -90,32 +122,85 @@ def cond_cast_float(input):
|
|||||||
return input.float() if unet_needs_upcast else input
|
return input.float() if unet_needs_upcast else input
|
||||||
|
|
||||||
|
|
||||||
def randn(seed, shape):
|
nv_rng = None
|
||||||
from modules.shared import opts
|
patch_module_list = [
|
||||||
|
torch.nn.Linear,
|
||||||
torch.manual_seed(seed)
|
torch.nn.Conv2d,
|
||||||
if opts.randn_source == "CPU" or device.type == 'mps':
|
torch.nn.MultiheadAttention,
|
||||||
return torch.randn(shape, device=cpu).to(device)
|
torch.nn.GroupNorm,
|
||||||
return torch.randn(shape, device=device)
|
torch.nn.LayerNorm,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def randn_without_seed(shape):
|
def manual_cast_forward(target_dtype):
|
||||||
from modules.shared import opts
|
def forward_wrapper(self, *args, **kwargs):
|
||||||
|
if any(
|
||||||
|
isinstance(arg, torch.Tensor) and arg.dtype != target_dtype
|
||||||
|
for arg in args
|
||||||
|
):
|
||||||
|
args = [arg.to(target_dtype) if isinstance(arg, torch.Tensor) else arg for arg in args]
|
||||||
|
kwargs = {k: v.to(target_dtype) if isinstance(v, torch.Tensor) else v for k, v in kwargs.items()}
|
||||||
|
|
||||||
if opts.randn_source == "CPU" or device.type == 'mps':
|
org_dtype = torch_utils.get_param(self).dtype
|
||||||
return torch.randn(shape, device=cpu).to(device)
|
if org_dtype != target_dtype:
|
||||||
return torch.randn(shape, device=device)
|
self.to(target_dtype)
|
||||||
|
result = self.org_forward(*args, **kwargs)
|
||||||
|
if org_dtype != target_dtype:
|
||||||
|
self.to(org_dtype)
|
||||||
|
|
||||||
|
if target_dtype != dtype_inference:
|
||||||
|
if isinstance(result, tuple):
|
||||||
|
result = tuple(
|
||||||
|
i.to(dtype_inference)
|
||||||
|
if isinstance(i, torch.Tensor)
|
||||||
|
else i
|
||||||
|
for i in result
|
||||||
|
)
|
||||||
|
elif isinstance(result, torch.Tensor):
|
||||||
|
result = result.to(dtype_inference)
|
||||||
|
return result
|
||||||
|
return forward_wrapper
|
||||||
|
|
||||||
|
|
||||||
|
@contextlib.contextmanager
|
||||||
|
def manual_cast(target_dtype):
|
||||||
|
applied = False
|
||||||
|
for module_type in patch_module_list:
|
||||||
|
if hasattr(module_type, "org_forward"):
|
||||||
|
continue
|
||||||
|
applied = True
|
||||||
|
org_forward = module_type.forward
|
||||||
|
if module_type == torch.nn.MultiheadAttention and has_xpu():
|
||||||
|
module_type.forward = manual_cast_forward(torch.float32)
|
||||||
|
else:
|
||||||
|
module_type.forward = manual_cast_forward(target_dtype)
|
||||||
|
module_type.org_forward = org_forward
|
||||||
|
try:
|
||||||
|
yield None
|
||||||
|
finally:
|
||||||
|
if applied:
|
||||||
|
for module_type in patch_module_list:
|
||||||
|
if hasattr(module_type, "org_forward"):
|
||||||
|
module_type.forward = module_type.org_forward
|
||||||
|
delattr(module_type, "org_forward")
|
||||||
|
|
||||||
|
|
||||||
def autocast(disable=False):
|
def autocast(disable=False):
|
||||||
from modules import shared
|
|
||||||
|
|
||||||
if disable:
|
if disable:
|
||||||
return contextlib.nullcontext()
|
return contextlib.nullcontext()
|
||||||
|
|
||||||
if dtype == torch.float32 or shared.cmd_opts.precision == "full":
|
if fp8 and device==cpu:
|
||||||
|
return torch.autocast("cpu", dtype=torch.bfloat16, enabled=True)
|
||||||
|
|
||||||
|
if fp8 and dtype_inference == torch.float32:
|
||||||
|
return manual_cast(dtype)
|
||||||
|
|
||||||
|
if dtype == torch.float32 or dtype_inference == torch.float32:
|
||||||
return contextlib.nullcontext()
|
return contextlib.nullcontext()
|
||||||
|
|
||||||
|
if has_xpu() or has_mps() or cuda_no_autocast():
|
||||||
|
return manual_cast(dtype)
|
||||||
|
|
||||||
return torch.autocast("cuda")
|
return torch.autocast("cuda")
|
||||||
|
|
||||||
|
|
||||||
@@ -128,8 +213,6 @@ class NansException(Exception):
|
|||||||
|
|
||||||
|
|
||||||
def test_for_nans(x, where):
|
def test_for_nans(x, where):
|
||||||
from modules import shared
|
|
||||||
|
|
||||||
if shared.cmd_opts.disable_nan_check:
|
if shared.cmd_opts.disable_nan_check:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -169,3 +252,4 @@ def first_time_calculation():
|
|||||||
x = torch.zeros((1, 1, 3, 3)).to(device, dtype)
|
x = torch.zeros((1, 1, 3, 3)).to(device, dtype)
|
||||||
conv2d = torch.nn.Conv2d(1, 1, (3, 3)).to(device, dtype)
|
conv2d = torch.nn.Conv2d(1, 1, (3, 3)).to(device, dtype)
|
||||||
conv2d(x)
|
conv2d(x)
|
||||||
|
|
||||||
|
|||||||
+66
-1
@@ -6,6 +6,21 @@ import traceback
|
|||||||
exception_records = []
|
exception_records = []
|
||||||
|
|
||||||
|
|
||||||
|
def format_traceback(tb):
|
||||||
|
return [[f"{x.filename}, line {x.lineno}, {x.name}", x.line] for x in traceback.extract_tb(tb)]
|
||||||
|
|
||||||
|
|
||||||
|
def format_exception(e, tb):
|
||||||
|
return {"exception": str(e), "traceback": format_traceback(tb)}
|
||||||
|
|
||||||
|
|
||||||
|
def get_exceptions():
|
||||||
|
try:
|
||||||
|
return list(reversed(exception_records))
|
||||||
|
except Exception as e:
|
||||||
|
return str(e)
|
||||||
|
|
||||||
|
|
||||||
def record_exception():
|
def record_exception():
|
||||||
_, e, tb = sys.exc_info()
|
_, e, tb = sys.exc_info()
|
||||||
if e is None:
|
if e is None:
|
||||||
@@ -14,7 +29,7 @@ def record_exception():
|
|||||||
if exception_records and exception_records[-1] == e:
|
if exception_records and exception_records[-1] == e:
|
||||||
return
|
return
|
||||||
|
|
||||||
exception_records.append((e, tb))
|
exception_records.append(format_exception(e, tb))
|
||||||
|
|
||||||
if len(exception_records) > 5:
|
if len(exception_records) > 5:
|
||||||
exception_records.pop(0)
|
exception_records.pop(0)
|
||||||
@@ -83,3 +98,53 @@ def run(code, task):
|
|||||||
code()
|
code()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
display(task, e)
|
display(task, e)
|
||||||
|
|
||||||
|
|
||||||
|
def check_versions():
|
||||||
|
from packaging import version
|
||||||
|
from modules import shared
|
||||||
|
|
||||||
|
import torch
|
||||||
|
import gradio
|
||||||
|
|
||||||
|
expected_torch_version = "2.1.2"
|
||||||
|
expected_xformers_version = "0.0.23.post1"
|
||||||
|
expected_gradio_version = "3.41.2"
|
||||||
|
|
||||||
|
if version.parse(torch.__version__) < version.parse(expected_torch_version):
|
||||||
|
print_error_explanation(f"""
|
||||||
|
You are running torch {torch.__version__}.
|
||||||
|
The program is tested to work with torch {expected_torch_version}.
|
||||||
|
To reinstall the desired version, run with commandline flag --reinstall-torch.
|
||||||
|
Beware that this will cause a lot of large files to be downloaded, as well as
|
||||||
|
there are reports of issues with training tab on the latest version.
|
||||||
|
|
||||||
|
Use --skip-version-check commandline argument to disable this check.
|
||||||
|
""".strip())
|
||||||
|
|
||||||
|
if shared.xformers_available:
|
||||||
|
import xformers
|
||||||
|
|
||||||
|
if version.parse(xformers.__version__) < version.parse(expected_xformers_version):
|
||||||
|
print_error_explanation(f"""
|
||||||
|
You are running xformers {xformers.__version__}.
|
||||||
|
The program is tested to work with xformers {expected_xformers_version}.
|
||||||
|
To reinstall the desired version, run with commandline flag --reinstall-xformers.
|
||||||
|
|
||||||
|
Use --skip-version-check commandline argument to disable this check.
|
||||||
|
""".strip())
|
||||||
|
|
||||||
|
if gradio.__version__ != expected_gradio_version:
|
||||||
|
print_error_explanation(f"""
|
||||||
|
You are running gradio {gradio.__version__}.
|
||||||
|
The program is designed to work with gradio {expected_gradio_version}.
|
||||||
|
Using a different version of gradio is extremely likely to break the program.
|
||||||
|
|
||||||
|
Reasons why you have the mismatched gradio version can be:
|
||||||
|
- you use --skip-install flag.
|
||||||
|
- you use webui.py to start the program instead of launch.py.
|
||||||
|
- an extension installs the incompatible gradio version.
|
||||||
|
|
||||||
|
Use --skip-version-check commandline argument to disable this check.
|
||||||
|
""".strip())
|
||||||
|
|
||||||
|
|||||||
+16
-183
@@ -1,121 +1,7 @@
|
|||||||
import sys
|
from modules import modelloader, devices, errors
|
||||||
|
|
||||||
import numpy as np
|
|
||||||
import torch
|
|
||||||
from PIL import Image
|
|
||||||
|
|
||||||
import modules.esrgan_model_arch as arch
|
|
||||||
from modules import modelloader, images, devices
|
|
||||||
from modules.shared import opts
|
from modules.shared import opts
|
||||||
from modules.upscaler import Upscaler, UpscalerData
|
from modules.upscaler import Upscaler, UpscalerData
|
||||||
|
from modules.upscaler_utils import upscale_with_model
|
||||||
|
|
||||||
def mod2normal(state_dict):
|
|
||||||
# this code is copied from https://github.com/victorca25/iNNfer
|
|
||||||
if 'conv_first.weight' in state_dict:
|
|
||||||
crt_net = {}
|
|
||||||
items = list(state_dict)
|
|
||||||
|
|
||||||
crt_net['model.0.weight'] = state_dict['conv_first.weight']
|
|
||||||
crt_net['model.0.bias'] = state_dict['conv_first.bias']
|
|
||||||
|
|
||||||
for k in items.copy():
|
|
||||||
if 'RDB' in k:
|
|
||||||
ori_k = k.replace('RRDB_trunk.', 'model.1.sub.')
|
|
||||||
if '.weight' in k:
|
|
||||||
ori_k = ori_k.replace('.weight', '.0.weight')
|
|
||||||
elif '.bias' in k:
|
|
||||||
ori_k = ori_k.replace('.bias', '.0.bias')
|
|
||||||
crt_net[ori_k] = state_dict[k]
|
|
||||||
items.remove(k)
|
|
||||||
|
|
||||||
crt_net['model.1.sub.23.weight'] = state_dict['trunk_conv.weight']
|
|
||||||
crt_net['model.1.sub.23.bias'] = state_dict['trunk_conv.bias']
|
|
||||||
crt_net['model.3.weight'] = state_dict['upconv1.weight']
|
|
||||||
crt_net['model.3.bias'] = state_dict['upconv1.bias']
|
|
||||||
crt_net['model.6.weight'] = state_dict['upconv2.weight']
|
|
||||||
crt_net['model.6.bias'] = state_dict['upconv2.bias']
|
|
||||||
crt_net['model.8.weight'] = state_dict['HRconv.weight']
|
|
||||||
crt_net['model.8.bias'] = state_dict['HRconv.bias']
|
|
||||||
crt_net['model.10.weight'] = state_dict['conv_last.weight']
|
|
||||||
crt_net['model.10.bias'] = state_dict['conv_last.bias']
|
|
||||||
state_dict = crt_net
|
|
||||||
return state_dict
|
|
||||||
|
|
||||||
|
|
||||||
def resrgan2normal(state_dict, nb=23):
|
|
||||||
# this code is copied from https://github.com/victorca25/iNNfer
|
|
||||||
if "conv_first.weight" in state_dict and "body.0.rdb1.conv1.weight" in state_dict:
|
|
||||||
re8x = 0
|
|
||||||
crt_net = {}
|
|
||||||
items = list(state_dict)
|
|
||||||
|
|
||||||
crt_net['model.0.weight'] = state_dict['conv_first.weight']
|
|
||||||
crt_net['model.0.bias'] = state_dict['conv_first.bias']
|
|
||||||
|
|
||||||
for k in items.copy():
|
|
||||||
if "rdb" in k:
|
|
||||||
ori_k = k.replace('body.', 'model.1.sub.')
|
|
||||||
ori_k = ori_k.replace('.rdb', '.RDB')
|
|
||||||
if '.weight' in k:
|
|
||||||
ori_k = ori_k.replace('.weight', '.0.weight')
|
|
||||||
elif '.bias' in k:
|
|
||||||
ori_k = ori_k.replace('.bias', '.0.bias')
|
|
||||||
crt_net[ori_k] = state_dict[k]
|
|
||||||
items.remove(k)
|
|
||||||
|
|
||||||
crt_net[f'model.1.sub.{nb}.weight'] = state_dict['conv_body.weight']
|
|
||||||
crt_net[f'model.1.sub.{nb}.bias'] = state_dict['conv_body.bias']
|
|
||||||
crt_net['model.3.weight'] = state_dict['conv_up1.weight']
|
|
||||||
crt_net['model.3.bias'] = state_dict['conv_up1.bias']
|
|
||||||
crt_net['model.6.weight'] = state_dict['conv_up2.weight']
|
|
||||||
crt_net['model.6.bias'] = state_dict['conv_up2.bias']
|
|
||||||
|
|
||||||
if 'conv_up3.weight' in state_dict:
|
|
||||||
# modification supporting: https://github.com/ai-forever/Real-ESRGAN/blob/main/RealESRGAN/rrdbnet_arch.py
|
|
||||||
re8x = 3
|
|
||||||
crt_net['model.9.weight'] = state_dict['conv_up3.weight']
|
|
||||||
crt_net['model.9.bias'] = state_dict['conv_up3.bias']
|
|
||||||
|
|
||||||
crt_net[f'model.{8+re8x}.weight'] = state_dict['conv_hr.weight']
|
|
||||||
crt_net[f'model.{8+re8x}.bias'] = state_dict['conv_hr.bias']
|
|
||||||
crt_net[f'model.{10+re8x}.weight'] = state_dict['conv_last.weight']
|
|
||||||
crt_net[f'model.{10+re8x}.bias'] = state_dict['conv_last.bias']
|
|
||||||
|
|
||||||
state_dict = crt_net
|
|
||||||
return state_dict
|
|
||||||
|
|
||||||
|
|
||||||
def infer_params(state_dict):
|
|
||||||
# this code is copied from https://github.com/victorca25/iNNfer
|
|
||||||
scale2x = 0
|
|
||||||
scalemin = 6
|
|
||||||
n_uplayer = 0
|
|
||||||
plus = False
|
|
||||||
|
|
||||||
for block in list(state_dict):
|
|
||||||
parts = block.split(".")
|
|
||||||
n_parts = len(parts)
|
|
||||||
if n_parts == 5 and parts[2] == "sub":
|
|
||||||
nb = int(parts[3])
|
|
||||||
elif n_parts == 3:
|
|
||||||
part_num = int(parts[1])
|
|
||||||
if (part_num > scalemin
|
|
||||||
and parts[0] == "model"
|
|
||||||
and parts[2] == "weight"):
|
|
||||||
scale2x += 1
|
|
||||||
if part_num > n_uplayer:
|
|
||||||
n_uplayer = part_num
|
|
||||||
out_nc = state_dict[block].shape[0]
|
|
||||||
if not plus and "conv1x1" in block:
|
|
||||||
plus = True
|
|
||||||
|
|
||||||
nf = state_dict["model.0.weight"].shape[0]
|
|
||||||
in_nc = state_dict["model.0.weight"].shape[1]
|
|
||||||
out_nc = out_nc
|
|
||||||
scale = 2 ** scale2x
|
|
||||||
|
|
||||||
return in_nc, out_nc, nf, nb, plus, scale
|
|
||||||
|
|
||||||
|
|
||||||
class UpscalerESRGAN(Upscaler):
|
class UpscalerESRGAN(Upscaler):
|
||||||
@@ -143,12 +29,11 @@ class UpscalerESRGAN(Upscaler):
|
|||||||
def do_upscale(self, img, selected_model):
|
def do_upscale(self, img, selected_model):
|
||||||
try:
|
try:
|
||||||
model = self.load_model(selected_model)
|
model = self.load_model(selected_model)
|
||||||
except Exception as e:
|
except Exception:
|
||||||
print(f"Unable to load ESRGAN model {selected_model}: {e}", file=sys.stderr)
|
errors.report(f"Unable to load ESRGAN model {selected_model}", exc_info=True)
|
||||||
return img
|
return img
|
||||||
model.to(devices.device_esrgan)
|
model.to(devices.device_esrgan)
|
||||||
img = esrgan_upscale(model, img)
|
return esrgan_upscale(model, img)
|
||||||
return img
|
|
||||||
|
|
||||||
def load_model(self, path: str):
|
def load_model(self, path: str):
|
||||||
if path.startswith("http"):
|
if path.startswith("http"):
|
||||||
@@ -161,69 +46,17 @@ class UpscalerESRGAN(Upscaler):
|
|||||||
else:
|
else:
|
||||||
filename = path
|
filename = path
|
||||||
|
|
||||||
state_dict = torch.load(filename, map_location='cpu' if devices.device_esrgan.type == 'mps' else None)
|
return modelloader.load_spandrel_model(
|
||||||
|
filename,
|
||||||
if "params_ema" in state_dict:
|
device=('cpu' if devices.device_esrgan.type == 'mps' else None),
|
||||||
state_dict = state_dict["params_ema"]
|
expected_architecture='ESRGAN',
|
||||||
elif "params" in state_dict:
|
)
|
||||||
state_dict = state_dict["params"]
|
|
||||||
num_conv = 16 if "realesr-animevideov3" in filename else 32
|
|
||||||
model = arch.SRVGGNetCompact(num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=num_conv, upscale=4, act_type='prelu')
|
|
||||||
model.load_state_dict(state_dict)
|
|
||||||
model.eval()
|
|
||||||
return model
|
|
||||||
|
|
||||||
if "body.0.rdb1.conv1.weight" in state_dict and "conv_first.weight" in state_dict:
|
|
||||||
nb = 6 if "RealESRGAN_x4plus_anime_6B" in filename else 23
|
|
||||||
state_dict = resrgan2normal(state_dict, nb)
|
|
||||||
elif "conv_first.weight" in state_dict:
|
|
||||||
state_dict = mod2normal(state_dict)
|
|
||||||
elif "model.0.weight" not in state_dict:
|
|
||||||
raise Exception("The file is not a recognized ESRGAN model.")
|
|
||||||
|
|
||||||
in_nc, out_nc, nf, nb, plus, mscale = infer_params(state_dict)
|
|
||||||
|
|
||||||
model = arch.RRDBNet(in_nc=in_nc, out_nc=out_nc, nf=nf, nb=nb, upscale=mscale, plus=plus)
|
|
||||||
model.load_state_dict(state_dict)
|
|
||||||
model.eval()
|
|
||||||
|
|
||||||
return model
|
|
||||||
|
|
||||||
|
|
||||||
def upscale_without_tiling(model, img):
|
|
||||||
img = np.array(img)
|
|
||||||
img = img[:, :, ::-1]
|
|
||||||
img = np.ascontiguousarray(np.transpose(img, (2, 0, 1))) / 255
|
|
||||||
img = torch.from_numpy(img).float()
|
|
||||||
img = img.unsqueeze(0).to(devices.device_esrgan)
|
|
||||||
with torch.no_grad():
|
|
||||||
output = model(img)
|
|
||||||
output = output.squeeze().float().cpu().clamp_(0, 1).numpy()
|
|
||||||
output = 255. * np.moveaxis(output, 0, 2)
|
|
||||||
output = output.astype(np.uint8)
|
|
||||||
output = output[:, :, ::-1]
|
|
||||||
return Image.fromarray(output, 'RGB')
|
|
||||||
|
|
||||||
|
|
||||||
def esrgan_upscale(model, img):
|
def esrgan_upscale(model, img):
|
||||||
if opts.ESRGAN_tile == 0:
|
return upscale_with_model(
|
||||||
return upscale_without_tiling(model, img)
|
model,
|
||||||
|
img,
|
||||||
grid = images.split_grid(img, opts.ESRGAN_tile, opts.ESRGAN_tile, opts.ESRGAN_tile_overlap)
|
tile_size=opts.ESRGAN_tile,
|
||||||
newtiles = []
|
tile_overlap=opts.ESRGAN_tile_overlap,
|
||||||
scale_factor = 1
|
)
|
||||||
|
|
||||||
for y, h, row in grid.tiles:
|
|
||||||
newrow = []
|
|
||||||
for tiledata in row:
|
|
||||||
x, w, tile = tiledata
|
|
||||||
|
|
||||||
output = upscale_without_tiling(model, tile)
|
|
||||||
scale_factor = output.width // tile.width
|
|
||||||
|
|
||||||
newrow.append([x * scale_factor, w * scale_factor, output])
|
|
||||||
newtiles.append([y * scale_factor, h * scale_factor, newrow])
|
|
||||||
|
|
||||||
newgrid = images.Grid(newtiles, grid.tile_w * scale_factor, grid.tile_h * scale_factor, grid.image_w * scale_factor, grid.image_h * scale_factor, grid.overlap * scale_factor)
|
|
||||||
output = images.combine_grid(newgrid)
|
|
||||||
return output
|
|
||||||
|
|||||||
@@ -1,465 +0,0 @@
|
|||||||
# this file is adapted from https://github.com/victorca25/iNNfer
|
|
||||||
|
|
||||||
from collections import OrderedDict
|
|
||||||
import math
|
|
||||||
import torch
|
|
||||||
import torch.nn as nn
|
|
||||||
import torch.nn.functional as F
|
|
||||||
|
|
||||||
|
|
||||||
####################
|
|
||||||
# RRDBNet Generator
|
|
||||||
####################
|
|
||||||
|
|
||||||
class RRDBNet(nn.Module):
|
|
||||||
def __init__(self, in_nc, out_nc, nf, nb, nr=3, gc=32, upscale=4, norm_type=None,
|
|
||||||
act_type='leakyrelu', mode='CNA', upsample_mode='upconv', convtype='Conv2D',
|
|
||||||
finalact=None, gaussian_noise=False, plus=False):
|
|
||||||
super(RRDBNet, self).__init__()
|
|
||||||
n_upscale = int(math.log(upscale, 2))
|
|
||||||
if upscale == 3:
|
|
||||||
n_upscale = 1
|
|
||||||
|
|
||||||
self.resrgan_scale = 0
|
|
||||||
if in_nc % 16 == 0:
|
|
||||||
self.resrgan_scale = 1
|
|
||||||
elif in_nc != 4 and in_nc % 4 == 0:
|
|
||||||
self.resrgan_scale = 2
|
|
||||||
|
|
||||||
fea_conv = conv_block(in_nc, nf, kernel_size=3, norm_type=None, act_type=None, convtype=convtype)
|
|
||||||
rb_blocks = [RRDB(nf, nr, kernel_size=3, gc=32, stride=1, bias=1, pad_type='zero',
|
|
||||||
norm_type=norm_type, act_type=act_type, mode='CNA', convtype=convtype,
|
|
||||||
gaussian_noise=gaussian_noise, plus=plus) for _ in range(nb)]
|
|
||||||
LR_conv = conv_block(nf, nf, kernel_size=3, norm_type=norm_type, act_type=None, mode=mode, convtype=convtype)
|
|
||||||
|
|
||||||
if upsample_mode == 'upconv':
|
|
||||||
upsample_block = upconv_block
|
|
||||||
elif upsample_mode == 'pixelshuffle':
|
|
||||||
upsample_block = pixelshuffle_block
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(f'upsample mode [{upsample_mode}] is not found')
|
|
||||||
if upscale == 3:
|
|
||||||
upsampler = upsample_block(nf, nf, 3, act_type=act_type, convtype=convtype)
|
|
||||||
else:
|
|
||||||
upsampler = [upsample_block(nf, nf, act_type=act_type, convtype=convtype) for _ in range(n_upscale)]
|
|
||||||
HR_conv0 = conv_block(nf, nf, kernel_size=3, norm_type=None, act_type=act_type, convtype=convtype)
|
|
||||||
HR_conv1 = conv_block(nf, out_nc, kernel_size=3, norm_type=None, act_type=None, convtype=convtype)
|
|
||||||
|
|
||||||
outact = act(finalact) if finalact else None
|
|
||||||
|
|
||||||
self.model = sequential(fea_conv, ShortcutBlock(sequential(*rb_blocks, LR_conv)),
|
|
||||||
*upsampler, HR_conv0, HR_conv1, outact)
|
|
||||||
|
|
||||||
def forward(self, x, outm=None):
|
|
||||||
if self.resrgan_scale == 1:
|
|
||||||
feat = pixel_unshuffle(x, scale=4)
|
|
||||||
elif self.resrgan_scale == 2:
|
|
||||||
feat = pixel_unshuffle(x, scale=2)
|
|
||||||
else:
|
|
||||||
feat = x
|
|
||||||
|
|
||||||
return self.model(feat)
|
|
||||||
|
|
||||||
|
|
||||||
class RRDB(nn.Module):
|
|
||||||
"""
|
|
||||||
Residual in Residual Dense Block
|
|
||||||
(ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks)
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, nf, nr=3, kernel_size=3, gc=32, stride=1, bias=1, pad_type='zero',
|
|
||||||
norm_type=None, act_type='leakyrelu', mode='CNA', convtype='Conv2D',
|
|
||||||
spectral_norm=False, gaussian_noise=False, plus=False):
|
|
||||||
super(RRDB, self).__init__()
|
|
||||||
# This is for backwards compatibility with existing models
|
|
||||||
if nr == 3:
|
|
||||||
self.RDB1 = ResidualDenseBlock_5C(nf, kernel_size, gc, stride, bias, pad_type,
|
|
||||||
norm_type, act_type, mode, convtype, spectral_norm=spectral_norm,
|
|
||||||
gaussian_noise=gaussian_noise, plus=plus)
|
|
||||||
self.RDB2 = ResidualDenseBlock_5C(nf, kernel_size, gc, stride, bias, pad_type,
|
|
||||||
norm_type, act_type, mode, convtype, spectral_norm=spectral_norm,
|
|
||||||
gaussian_noise=gaussian_noise, plus=plus)
|
|
||||||
self.RDB3 = ResidualDenseBlock_5C(nf, kernel_size, gc, stride, bias, pad_type,
|
|
||||||
norm_type, act_type, mode, convtype, spectral_norm=spectral_norm,
|
|
||||||
gaussian_noise=gaussian_noise, plus=plus)
|
|
||||||
else:
|
|
||||||
RDB_list = [ResidualDenseBlock_5C(nf, kernel_size, gc, stride, bias, pad_type,
|
|
||||||
norm_type, act_type, mode, convtype, spectral_norm=spectral_norm,
|
|
||||||
gaussian_noise=gaussian_noise, plus=plus) for _ in range(nr)]
|
|
||||||
self.RDBs = nn.Sequential(*RDB_list)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
if hasattr(self, 'RDB1'):
|
|
||||||
out = self.RDB1(x)
|
|
||||||
out = self.RDB2(out)
|
|
||||||
out = self.RDB3(out)
|
|
||||||
else:
|
|
||||||
out = self.RDBs(x)
|
|
||||||
return out * 0.2 + x
|
|
||||||
|
|
||||||
|
|
||||||
class ResidualDenseBlock_5C(nn.Module):
|
|
||||||
"""
|
|
||||||
Residual Dense Block
|
|
||||||
The core module of paper: (Residual Dense Network for Image Super-Resolution, CVPR 18)
|
|
||||||
Modified options that can be used:
|
|
||||||
- "Partial Convolution based Padding" arXiv:1811.11718
|
|
||||||
- "Spectral normalization" arXiv:1802.05957
|
|
||||||
- "ICASSP 2020 - ESRGAN+ : Further Improving ESRGAN" N. C.
|
|
||||||
{Rakotonirina} and A. {Rasoanaivo}
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, nf=64, kernel_size=3, gc=32, stride=1, bias=1, pad_type='zero',
|
|
||||||
norm_type=None, act_type='leakyrelu', mode='CNA', convtype='Conv2D',
|
|
||||||
spectral_norm=False, gaussian_noise=False, plus=False):
|
|
||||||
super(ResidualDenseBlock_5C, self).__init__()
|
|
||||||
|
|
||||||
self.noise = GaussianNoise() if gaussian_noise else None
|
|
||||||
self.conv1x1 = conv1x1(nf, gc) if plus else None
|
|
||||||
|
|
||||||
self.conv1 = conv_block(nf, gc, kernel_size, stride, bias=bias, pad_type=pad_type,
|
|
||||||
norm_type=norm_type, act_type=act_type, mode=mode, convtype=convtype,
|
|
||||||
spectral_norm=spectral_norm)
|
|
||||||
self.conv2 = conv_block(nf+gc, gc, kernel_size, stride, bias=bias, pad_type=pad_type,
|
|
||||||
norm_type=norm_type, act_type=act_type, mode=mode, convtype=convtype,
|
|
||||||
spectral_norm=spectral_norm)
|
|
||||||
self.conv3 = conv_block(nf+2*gc, gc, kernel_size, stride, bias=bias, pad_type=pad_type,
|
|
||||||
norm_type=norm_type, act_type=act_type, mode=mode, convtype=convtype,
|
|
||||||
spectral_norm=spectral_norm)
|
|
||||||
self.conv4 = conv_block(nf+3*gc, gc, kernel_size, stride, bias=bias, pad_type=pad_type,
|
|
||||||
norm_type=norm_type, act_type=act_type, mode=mode, convtype=convtype,
|
|
||||||
spectral_norm=spectral_norm)
|
|
||||||
if mode == 'CNA':
|
|
||||||
last_act = None
|
|
||||||
else:
|
|
||||||
last_act = act_type
|
|
||||||
self.conv5 = conv_block(nf+4*gc, nf, 3, stride, bias=bias, pad_type=pad_type,
|
|
||||||
norm_type=norm_type, act_type=last_act, mode=mode, convtype=convtype,
|
|
||||||
spectral_norm=spectral_norm)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
x1 = self.conv1(x)
|
|
||||||
x2 = self.conv2(torch.cat((x, x1), 1))
|
|
||||||
if self.conv1x1:
|
|
||||||
x2 = x2 + self.conv1x1(x)
|
|
||||||
x3 = self.conv3(torch.cat((x, x1, x2), 1))
|
|
||||||
x4 = self.conv4(torch.cat((x, x1, x2, x3), 1))
|
|
||||||
if self.conv1x1:
|
|
||||||
x4 = x4 + x2
|
|
||||||
x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1))
|
|
||||||
if self.noise:
|
|
||||||
return self.noise(x5.mul(0.2) + x)
|
|
||||||
else:
|
|
||||||
return x5 * 0.2 + x
|
|
||||||
|
|
||||||
|
|
||||||
####################
|
|
||||||
# ESRGANplus
|
|
||||||
####################
|
|
||||||
|
|
||||||
class GaussianNoise(nn.Module):
|
|
||||||
def __init__(self, sigma=0.1, is_relative_detach=False):
|
|
||||||
super().__init__()
|
|
||||||
self.sigma = sigma
|
|
||||||
self.is_relative_detach = is_relative_detach
|
|
||||||
self.noise = torch.tensor(0, dtype=torch.float)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
if self.training and self.sigma != 0:
|
|
||||||
self.noise = self.noise.to(x.device)
|
|
||||||
scale = self.sigma * x.detach() if self.is_relative_detach else self.sigma * x
|
|
||||||
sampled_noise = self.noise.repeat(*x.size()).normal_() * scale
|
|
||||||
x = x + sampled_noise
|
|
||||||
return x
|
|
||||||
|
|
||||||
def conv1x1(in_planes, out_planes, stride=1):
|
|
||||||
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
|
|
||||||
|
|
||||||
|
|
||||||
####################
|
|
||||||
# SRVGGNetCompact
|
|
||||||
####################
|
|
||||||
|
|
||||||
class SRVGGNetCompact(nn.Module):
|
|
||||||
"""A compact VGG-style network structure for super-resolution.
|
|
||||||
This class is copied from https://github.com/xinntao/Real-ESRGAN
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, num_in_ch=3, num_out_ch=3, num_feat=64, num_conv=16, upscale=4, act_type='prelu'):
|
|
||||||
super(SRVGGNetCompact, self).__init__()
|
|
||||||
self.num_in_ch = num_in_ch
|
|
||||||
self.num_out_ch = num_out_ch
|
|
||||||
self.num_feat = num_feat
|
|
||||||
self.num_conv = num_conv
|
|
||||||
self.upscale = upscale
|
|
||||||
self.act_type = act_type
|
|
||||||
|
|
||||||
self.body = nn.ModuleList()
|
|
||||||
# the first conv
|
|
||||||
self.body.append(nn.Conv2d(num_in_ch, num_feat, 3, 1, 1))
|
|
||||||
# the first activation
|
|
||||||
if act_type == 'relu':
|
|
||||||
activation = nn.ReLU(inplace=True)
|
|
||||||
elif act_type == 'prelu':
|
|
||||||
activation = nn.PReLU(num_parameters=num_feat)
|
|
||||||
elif act_type == 'leakyrelu':
|
|
||||||
activation = nn.LeakyReLU(negative_slope=0.1, inplace=True)
|
|
||||||
self.body.append(activation)
|
|
||||||
|
|
||||||
# the body structure
|
|
||||||
for _ in range(num_conv):
|
|
||||||
self.body.append(nn.Conv2d(num_feat, num_feat, 3, 1, 1))
|
|
||||||
# activation
|
|
||||||
if act_type == 'relu':
|
|
||||||
activation = nn.ReLU(inplace=True)
|
|
||||||
elif act_type == 'prelu':
|
|
||||||
activation = nn.PReLU(num_parameters=num_feat)
|
|
||||||
elif act_type == 'leakyrelu':
|
|
||||||
activation = nn.LeakyReLU(negative_slope=0.1, inplace=True)
|
|
||||||
self.body.append(activation)
|
|
||||||
|
|
||||||
# the last conv
|
|
||||||
self.body.append(nn.Conv2d(num_feat, num_out_ch * upscale * upscale, 3, 1, 1))
|
|
||||||
# upsample
|
|
||||||
self.upsampler = nn.PixelShuffle(upscale)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
out = x
|
|
||||||
for i in range(0, len(self.body)):
|
|
||||||
out = self.body[i](out)
|
|
||||||
|
|
||||||
out = self.upsampler(out)
|
|
||||||
# add the nearest upsampled image, so that the network learns the residual
|
|
||||||
base = F.interpolate(x, scale_factor=self.upscale, mode='nearest')
|
|
||||||
out += base
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
####################
|
|
||||||
# Upsampler
|
|
||||||
####################
|
|
||||||
|
|
||||||
class Upsample(nn.Module):
|
|
||||||
r"""Upsamples a given multi-channel 1D (temporal), 2D (spatial) or 3D (volumetric) data.
|
|
||||||
The input data is assumed to be of the form
|
|
||||||
`minibatch x channels x [optional depth] x [optional height] x width`.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(self, size=None, scale_factor=None, mode="nearest", align_corners=None):
|
|
||||||
super(Upsample, self).__init__()
|
|
||||||
if isinstance(scale_factor, tuple):
|
|
||||||
self.scale_factor = tuple(float(factor) for factor in scale_factor)
|
|
||||||
else:
|
|
||||||
self.scale_factor = float(scale_factor) if scale_factor else None
|
|
||||||
self.mode = mode
|
|
||||||
self.size = size
|
|
||||||
self.align_corners = align_corners
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
return nn.functional.interpolate(x, size=self.size, scale_factor=self.scale_factor, mode=self.mode, align_corners=self.align_corners)
|
|
||||||
|
|
||||||
def extra_repr(self):
|
|
||||||
if self.scale_factor is not None:
|
|
||||||
info = f'scale_factor={self.scale_factor}'
|
|
||||||
else:
|
|
||||||
info = f'size={self.size}'
|
|
||||||
info += f', mode={self.mode}'
|
|
||||||
return info
|
|
||||||
|
|
||||||
|
|
||||||
def pixel_unshuffle(x, scale):
|
|
||||||
""" Pixel unshuffle.
|
|
||||||
Args:
|
|
||||||
x (Tensor): Input feature with shape (b, c, hh, hw).
|
|
||||||
scale (int): Downsample ratio.
|
|
||||||
Returns:
|
|
||||||
Tensor: the pixel unshuffled feature.
|
|
||||||
"""
|
|
||||||
b, c, hh, hw = x.size()
|
|
||||||
out_channel = c * (scale**2)
|
|
||||||
assert hh % scale == 0 and hw % scale == 0
|
|
||||||
h = hh // scale
|
|
||||||
w = hw // scale
|
|
||||||
x_view = x.view(b, c, h, scale, w, scale)
|
|
||||||
return x_view.permute(0, 1, 3, 5, 2, 4).reshape(b, out_channel, h, w)
|
|
||||||
|
|
||||||
|
|
||||||
def pixelshuffle_block(in_nc, out_nc, upscale_factor=2, kernel_size=3, stride=1, bias=True,
|
|
||||||
pad_type='zero', norm_type=None, act_type='relu', convtype='Conv2D'):
|
|
||||||
"""
|
|
||||||
Pixel shuffle layer
|
|
||||||
(Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional
|
|
||||||
Neural Network, CVPR17)
|
|
||||||
"""
|
|
||||||
conv = conv_block(in_nc, out_nc * (upscale_factor ** 2), kernel_size, stride, bias=bias,
|
|
||||||
pad_type=pad_type, norm_type=None, act_type=None, convtype=convtype)
|
|
||||||
pixel_shuffle = nn.PixelShuffle(upscale_factor)
|
|
||||||
|
|
||||||
n = norm(norm_type, out_nc) if norm_type else None
|
|
||||||
a = act(act_type) if act_type else None
|
|
||||||
return sequential(conv, pixel_shuffle, n, a)
|
|
||||||
|
|
||||||
|
|
||||||
def upconv_block(in_nc, out_nc, upscale_factor=2, kernel_size=3, stride=1, bias=True,
|
|
||||||
pad_type='zero', norm_type=None, act_type='relu', mode='nearest', convtype='Conv2D'):
|
|
||||||
""" Upconv layer """
|
|
||||||
upscale_factor = (1, upscale_factor, upscale_factor) if convtype == 'Conv3D' else upscale_factor
|
|
||||||
upsample = Upsample(scale_factor=upscale_factor, mode=mode)
|
|
||||||
conv = conv_block(in_nc, out_nc, kernel_size, stride, bias=bias,
|
|
||||||
pad_type=pad_type, norm_type=norm_type, act_type=act_type, convtype=convtype)
|
|
||||||
return sequential(upsample, conv)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
####################
|
|
||||||
# Basic blocks
|
|
||||||
####################
|
|
||||||
|
|
||||||
|
|
||||||
def make_layer(basic_block, num_basic_block, **kwarg):
|
|
||||||
"""Make layers by stacking the same blocks.
|
|
||||||
Args:
|
|
||||||
basic_block (nn.module): nn.module class for basic block. (block)
|
|
||||||
num_basic_block (int): number of blocks. (n_layers)
|
|
||||||
Returns:
|
|
||||||
nn.Sequential: Stacked blocks in nn.Sequential.
|
|
||||||
"""
|
|
||||||
layers = []
|
|
||||||
for _ in range(num_basic_block):
|
|
||||||
layers.append(basic_block(**kwarg))
|
|
||||||
return nn.Sequential(*layers)
|
|
||||||
|
|
||||||
|
|
||||||
def act(act_type, inplace=True, neg_slope=0.2, n_prelu=1, beta=1.0):
|
|
||||||
""" activation helper """
|
|
||||||
act_type = act_type.lower()
|
|
||||||
if act_type == 'relu':
|
|
||||||
layer = nn.ReLU(inplace)
|
|
||||||
elif act_type in ('leakyrelu', 'lrelu'):
|
|
||||||
layer = nn.LeakyReLU(neg_slope, inplace)
|
|
||||||
elif act_type == 'prelu':
|
|
||||||
layer = nn.PReLU(num_parameters=n_prelu, init=neg_slope)
|
|
||||||
elif act_type == 'tanh': # [-1, 1] range output
|
|
||||||
layer = nn.Tanh()
|
|
||||||
elif act_type == 'sigmoid': # [0, 1] range output
|
|
||||||
layer = nn.Sigmoid()
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(f'activation layer [{act_type}] is not found')
|
|
||||||
return layer
|
|
||||||
|
|
||||||
|
|
||||||
class Identity(nn.Module):
|
|
||||||
def __init__(self, *kwargs):
|
|
||||||
super(Identity, self).__init__()
|
|
||||||
|
|
||||||
def forward(self, x, *kwargs):
|
|
||||||
return x
|
|
||||||
|
|
||||||
|
|
||||||
def norm(norm_type, nc):
|
|
||||||
""" Return a normalization layer """
|
|
||||||
norm_type = norm_type.lower()
|
|
||||||
if norm_type == 'batch':
|
|
||||||
layer = nn.BatchNorm2d(nc, affine=True)
|
|
||||||
elif norm_type == 'instance':
|
|
||||||
layer = nn.InstanceNorm2d(nc, affine=False)
|
|
||||||
elif norm_type == 'none':
|
|
||||||
def norm_layer(x): return Identity()
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(f'normalization layer [{norm_type}] is not found')
|
|
||||||
return layer
|
|
||||||
|
|
||||||
|
|
||||||
def pad(pad_type, padding):
|
|
||||||
""" padding layer helper """
|
|
||||||
pad_type = pad_type.lower()
|
|
||||||
if padding == 0:
|
|
||||||
return None
|
|
||||||
if pad_type == 'reflect':
|
|
||||||
layer = nn.ReflectionPad2d(padding)
|
|
||||||
elif pad_type == 'replicate':
|
|
||||||
layer = nn.ReplicationPad2d(padding)
|
|
||||||
elif pad_type == 'zero':
|
|
||||||
layer = nn.ZeroPad2d(padding)
|
|
||||||
else:
|
|
||||||
raise NotImplementedError(f'padding layer [{pad_type}] is not implemented')
|
|
||||||
return layer
|
|
||||||
|
|
||||||
|
|
||||||
def get_valid_padding(kernel_size, dilation):
|
|
||||||
kernel_size = kernel_size + (kernel_size - 1) * (dilation - 1)
|
|
||||||
padding = (kernel_size - 1) // 2
|
|
||||||
return padding
|
|
||||||
|
|
||||||
|
|
||||||
class ShortcutBlock(nn.Module):
|
|
||||||
""" Elementwise sum the output of a submodule to its input """
|
|
||||||
def __init__(self, submodule):
|
|
||||||
super(ShortcutBlock, self).__init__()
|
|
||||||
self.sub = submodule
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
output = x + self.sub(x)
|
|
||||||
return output
|
|
||||||
|
|
||||||
def __repr__(self):
|
|
||||||
return 'Identity + \n|' + self.sub.__repr__().replace('\n', '\n|')
|
|
||||||
|
|
||||||
|
|
||||||
def sequential(*args):
|
|
||||||
""" Flatten Sequential. It unwraps nn.Sequential. """
|
|
||||||
if len(args) == 1:
|
|
||||||
if isinstance(args[0], OrderedDict):
|
|
||||||
raise NotImplementedError('sequential does not support OrderedDict input.')
|
|
||||||
return args[0] # No sequential is needed.
|
|
||||||
modules = []
|
|
||||||
for module in args:
|
|
||||||
if isinstance(module, nn.Sequential):
|
|
||||||
for submodule in module.children():
|
|
||||||
modules.append(submodule)
|
|
||||||
elif isinstance(module, nn.Module):
|
|
||||||
modules.append(module)
|
|
||||||
return nn.Sequential(*modules)
|
|
||||||
|
|
||||||
|
|
||||||
def conv_block(in_nc, out_nc, kernel_size, stride=1, dilation=1, groups=1, bias=True,
|
|
||||||
pad_type='zero', norm_type=None, act_type='relu', mode='CNA', convtype='Conv2D',
|
|
||||||
spectral_norm=False):
|
|
||||||
""" Conv layer with padding, normalization, activation """
|
|
||||||
assert mode in ['CNA', 'NAC', 'CNAC'], f'Wrong conv mode [{mode}]'
|
|
||||||
padding = get_valid_padding(kernel_size, dilation)
|
|
||||||
p = pad(pad_type, padding) if pad_type and pad_type != 'zero' else None
|
|
||||||
padding = padding if pad_type == 'zero' else 0
|
|
||||||
|
|
||||||
if convtype=='PartialConv2D':
|
|
||||||
from torchvision.ops import PartialConv2d # this is definitely not going to work, but PartialConv2d doesn't work anyway and this shuts up static analyzer
|
|
||||||
c = PartialConv2d(in_nc, out_nc, kernel_size=kernel_size, stride=stride, padding=padding,
|
|
||||||
dilation=dilation, bias=bias, groups=groups)
|
|
||||||
elif convtype=='DeformConv2D':
|
|
||||||
from torchvision.ops import DeformConv2d # not tested
|
|
||||||
c = DeformConv2d(in_nc, out_nc, kernel_size=kernel_size, stride=stride, padding=padding,
|
|
||||||
dilation=dilation, bias=bias, groups=groups)
|
|
||||||
elif convtype=='Conv3D':
|
|
||||||
c = nn.Conv3d(in_nc, out_nc, kernel_size=kernel_size, stride=stride, padding=padding,
|
|
||||||
dilation=dilation, bias=bias, groups=groups)
|
|
||||||
else:
|
|
||||||
c = nn.Conv2d(in_nc, out_nc, kernel_size=kernel_size, stride=stride, padding=padding,
|
|
||||||
dilation=dilation, bias=bias, groups=groups)
|
|
||||||
|
|
||||||
if spectral_norm:
|
|
||||||
c = nn.utils.spectral_norm(c)
|
|
||||||
|
|
||||||
a = act(act_type) if act_type else None
|
|
||||||
if 'CNA' in mode:
|
|
||||||
n = norm(norm_type, out_nc) if norm_type else None
|
|
||||||
return sequential(p, c, n, a)
|
|
||||||
elif mode == 'NAC':
|
|
||||||
if norm_type is None and act_type is not None:
|
|
||||||
a = act(act_type, inplace=False)
|
|
||||||
n = norm(norm_type, in_nc) if norm_type else None
|
|
||||||
return sequential(n, a, p, c)
|
|
||||||
+97
-19
@@ -1,29 +1,77 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import configparser
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
|
import re
|
||||||
|
|
||||||
from modules import shared, errors, cache
|
from modules import shared, errors, cache, scripts
|
||||||
from modules.gitpython_hack import Repo
|
from modules.gitpython_hack import Repo
|
||||||
from modules.paths_internal import extensions_dir, extensions_builtin_dir, script_path # noqa: F401
|
from modules.paths_internal import extensions_dir, extensions_builtin_dir, script_path # noqa: F401
|
||||||
|
|
||||||
extensions = []
|
|
||||||
|
|
||||||
os.makedirs(extensions_dir, exist_ok=True)
|
os.makedirs(extensions_dir, exist_ok=True)
|
||||||
|
|
||||||
|
|
||||||
def active():
|
def active():
|
||||||
if shared.opts.disable_all_extensions == "all":
|
if shared.cmd_opts.disable_all_extensions or shared.opts.disable_all_extensions == "all":
|
||||||
return []
|
return []
|
||||||
elif shared.opts.disable_all_extensions == "extra":
|
elif shared.cmd_opts.disable_extra_extensions or shared.opts.disable_all_extensions == "extra":
|
||||||
return [x for x in extensions if x.enabled and x.is_builtin]
|
return [x for x in extensions if x.enabled and x.is_builtin]
|
||||||
else:
|
else:
|
||||||
return [x for x in extensions if x.enabled]
|
return [x for x in extensions if x.enabled]
|
||||||
|
|
||||||
|
|
||||||
|
class ExtensionMetadata:
|
||||||
|
filename = "metadata.ini"
|
||||||
|
config: configparser.ConfigParser
|
||||||
|
canonical_name: str
|
||||||
|
requires: list
|
||||||
|
|
||||||
|
def __init__(self, path, canonical_name):
|
||||||
|
self.config = configparser.ConfigParser()
|
||||||
|
|
||||||
|
filepath = os.path.join(path, self.filename)
|
||||||
|
# `self.config.read()` will quietly swallow OSErrors (which FileNotFoundError is),
|
||||||
|
# so no need to check whether the file exists beforehand.
|
||||||
|
try:
|
||||||
|
self.config.read(filepath)
|
||||||
|
except Exception:
|
||||||
|
errors.report(f"Error reading {self.filename} for extension {canonical_name}.", exc_info=True)
|
||||||
|
|
||||||
|
self.canonical_name = self.config.get("Extension", "Name", fallback=canonical_name)
|
||||||
|
self.canonical_name = canonical_name.lower().strip()
|
||||||
|
|
||||||
|
self.requires = self.get_script_requirements("Requires", "Extension")
|
||||||
|
|
||||||
|
def get_script_requirements(self, field, section, extra_section=None):
|
||||||
|
"""reads a list of requirements from the config; field is the name of the field in the ini file,
|
||||||
|
like Requires or Before, and section is the name of the [section] in the ini file; additionally,
|
||||||
|
reads more requirements from [extra_section] if specified."""
|
||||||
|
|
||||||
|
x = self.config.get(section, field, fallback='')
|
||||||
|
|
||||||
|
if extra_section:
|
||||||
|
x = x + ', ' + self.config.get(extra_section, field, fallback='')
|
||||||
|
|
||||||
|
return self.parse_list(x.lower())
|
||||||
|
|
||||||
|
def parse_list(self, text):
|
||||||
|
"""converts a line from config ("ext1 ext2, ext3 ") into a python list (["ext1", "ext2", "ext3"])"""
|
||||||
|
|
||||||
|
if not text:
|
||||||
|
return []
|
||||||
|
|
||||||
|
# both "," and " " are accepted as separator
|
||||||
|
return [x for x in re.split(r"[,\s]+", text.strip()) if x]
|
||||||
|
|
||||||
|
|
||||||
class Extension:
|
class Extension:
|
||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
cached_fields = ['remote', 'commit_date', 'branch', 'commit_hash', 'version']
|
cached_fields = ['remote', 'commit_date', 'branch', 'commit_hash', 'version']
|
||||||
|
metadata: ExtensionMetadata
|
||||||
|
|
||||||
def __init__(self, name, path, enabled=True, is_builtin=False):
|
def __init__(self, name, path, enabled=True, is_builtin=False, metadata=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.path = path
|
self.path = path
|
||||||
self.enabled = enabled
|
self.enabled = enabled
|
||||||
@@ -36,6 +84,8 @@ class Extension:
|
|||||||
self.branch = None
|
self.branch = None
|
||||||
self.remote = None
|
self.remote = None
|
||||||
self.have_info_from_repo = False
|
self.have_info_from_repo = False
|
||||||
|
self.metadata = metadata if metadata else ExtensionMetadata(self.path, name.lower())
|
||||||
|
self.canonical_name = metadata.canonical_name
|
||||||
|
|
||||||
def to_dict(self):
|
def to_dict(self):
|
||||||
return {x: getattr(self, x) for x in self.cached_fields}
|
return {x: getattr(self, x) for x in self.cached_fields}
|
||||||
@@ -56,12 +106,13 @@ class Extension:
|
|||||||
self.do_read_info_from_repo()
|
self.do_read_info_from_repo()
|
||||||
|
|
||||||
return self.to_dict()
|
return self.to_dict()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
d = cache.cached_data_for_file('extensions-git', self.name, os.path.join(self.path, ".git"), read_from_repo)
|
d = cache.cached_data_for_file('extensions-git', self.name, os.path.join(self.path, ".git"), read_from_repo)
|
||||||
self.from_dict(d)
|
self.from_dict(d)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
pass
|
pass
|
||||||
self.status = 'unknown'
|
self.status = 'unknown' if self.status == '' else self.status
|
||||||
|
|
||||||
def do_read_info_from_repo(self):
|
def do_read_info_from_repo(self):
|
||||||
repo = None
|
repo = None
|
||||||
@@ -90,8 +141,6 @@ class Extension:
|
|||||||
self.have_info_from_repo = True
|
self.have_info_from_repo = True
|
||||||
|
|
||||||
def list_files(self, subdir, extension):
|
def list_files(self, subdir, extension):
|
||||||
from modules import scripts
|
|
||||||
|
|
||||||
dirpath = os.path.join(self.path, subdir)
|
dirpath = os.path.join(self.path, subdir)
|
||||||
if not os.path.isdir(dirpath):
|
if not os.path.isdir(dirpath):
|
||||||
return []
|
return []
|
||||||
@@ -138,26 +187,55 @@ class Extension:
|
|||||||
def list_extensions():
|
def list_extensions():
|
||||||
extensions.clear()
|
extensions.clear()
|
||||||
|
|
||||||
if not os.path.isdir(extensions_dir):
|
if shared.cmd_opts.disable_all_extensions:
|
||||||
return
|
print("*** \"--disable-all-extensions\" arg was used, will not load any extensions ***")
|
||||||
|
elif shared.opts.disable_all_extensions == "all":
|
||||||
if shared.opts.disable_all_extensions == "all":
|
|
||||||
print("*** \"Disable all extensions\" option was set, will not load any extensions ***")
|
print("*** \"Disable all extensions\" option was set, will not load any extensions ***")
|
||||||
|
elif shared.cmd_opts.disable_extra_extensions:
|
||||||
|
print("*** \"--disable-extra-extensions\" arg was used, will only load built-in extensions ***")
|
||||||
elif shared.opts.disable_all_extensions == "extra":
|
elif shared.opts.disable_all_extensions == "extra":
|
||||||
print("*** \"Disable all extensions\" option was set, will only load built-in extensions ***")
|
print("*** \"Disable all extensions\" option was set, will only load built-in extensions ***")
|
||||||
|
|
||||||
extension_paths = []
|
loaded_extensions = {}
|
||||||
for dirname in [extensions_dir, extensions_builtin_dir]:
|
|
||||||
|
# scan through extensions directory and load metadata
|
||||||
|
for dirname in [extensions_builtin_dir, extensions_dir]:
|
||||||
if not os.path.isdir(dirname):
|
if not os.path.isdir(dirname):
|
||||||
return
|
continue
|
||||||
|
|
||||||
for extension_dirname in sorted(os.listdir(dirname)):
|
for extension_dirname in sorted(os.listdir(dirname)):
|
||||||
path = os.path.join(dirname, extension_dirname)
|
path = os.path.join(dirname, extension_dirname)
|
||||||
if not os.path.isdir(path):
|
if not os.path.isdir(path):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
extension_paths.append((extension_dirname, path, dirname == extensions_builtin_dir))
|
canonical_name = extension_dirname
|
||||||
|
metadata = ExtensionMetadata(path, canonical_name)
|
||||||
|
|
||||||
for dirname, path, is_builtin in extension_paths:
|
# check for duplicated canonical names
|
||||||
extension = Extension(name=dirname, path=path, enabled=dirname not in shared.opts.disabled_extensions, is_builtin=is_builtin)
|
already_loaded_extension = loaded_extensions.get(metadata.canonical_name)
|
||||||
extensions.append(extension)
|
if already_loaded_extension is not None:
|
||||||
|
errors.report(f'Duplicate canonical name "{canonical_name}" found in extensions "{extension_dirname}" and "{already_loaded_extension.name}". Former will be discarded.', exc_info=False)
|
||||||
|
continue
|
||||||
|
|
||||||
|
is_builtin = dirname == extensions_builtin_dir
|
||||||
|
extension = Extension(name=extension_dirname, path=path, enabled=extension_dirname not in shared.opts.disabled_extensions, is_builtin=is_builtin, metadata=metadata)
|
||||||
|
extensions.append(extension)
|
||||||
|
loaded_extensions[canonical_name] = extension
|
||||||
|
|
||||||
|
# check for requirements
|
||||||
|
for extension in extensions:
|
||||||
|
if not extension.enabled:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for req in extension.metadata.requires:
|
||||||
|
required_extension = loaded_extensions.get(req)
|
||||||
|
if required_extension is None:
|
||||||
|
errors.report(f'Extension "{extension.name}" requires "{req}" which is not installed.', exc_info=False)
|
||||||
|
continue
|
||||||
|
|
||||||
|
if not required_extension.enabled:
|
||||||
|
errors.report(f'Extension "{extension.name}" requires "{required_extension.name}" which is disabled.', exc_info=False)
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
|
extensions: list[Extension] = []
|
||||||
|
|||||||
+63
-17
@@ -1,4 +1,7 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
|
import logging
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
||||||
from modules import errors
|
from modules import errors
|
||||||
@@ -84,27 +87,55 @@ class ExtraNetwork:
|
|||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
|
||||||
|
def lookup_extra_networks(extra_network_data):
|
||||||
|
"""returns a dict mapping ExtraNetwork objects to lists of arguments for those extra networks.
|
||||||
|
|
||||||
|
Example input:
|
||||||
|
{
|
||||||
|
'lora': [<modules.extra_networks.ExtraNetworkParams object at 0x0000020690D58310>],
|
||||||
|
'lyco': [<modules.extra_networks.ExtraNetworkParams object at 0x0000020690D58F70>],
|
||||||
|
'hypernet': [<modules.extra_networks.ExtraNetworkParams object at 0x0000020690D5A800>]
|
||||||
|
}
|
||||||
|
|
||||||
|
Example output:
|
||||||
|
|
||||||
|
{
|
||||||
|
<extra_networks_lora.ExtraNetworkLora object at 0x0000020581BEECE0>: [<modules.extra_networks.ExtraNetworkParams object at 0x0000020690D58310>, <modules.extra_networks.ExtraNetworkParams object at 0x0000020690D58F70>],
|
||||||
|
<modules.extra_networks_hypernet.ExtraNetworkHypernet object at 0x0000020581BEEE60>: [<modules.extra_networks.ExtraNetworkParams object at 0x0000020690D5A800>]
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
res = {}
|
||||||
|
|
||||||
|
for extra_network_name, extra_network_args in list(extra_network_data.items()):
|
||||||
|
extra_network = extra_network_registry.get(extra_network_name, None)
|
||||||
|
alias = extra_network_aliases.get(extra_network_name, None)
|
||||||
|
|
||||||
|
if alias is not None and extra_network is None:
|
||||||
|
extra_network = alias
|
||||||
|
|
||||||
|
if extra_network is None:
|
||||||
|
logging.info(f"Skipping unknown extra network: {extra_network_name}")
|
||||||
|
continue
|
||||||
|
|
||||||
|
res.setdefault(extra_network, []).extend(extra_network_args)
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
def activate(p, extra_network_data):
|
def activate(p, extra_network_data):
|
||||||
"""call activate for extra networks in extra_network_data in specified order, then call
|
"""call activate for extra networks in extra_network_data in specified order, then call
|
||||||
activate for all remaining registered networks with an empty argument list"""
|
activate for all remaining registered networks with an empty argument list"""
|
||||||
|
|
||||||
activated = []
|
activated = []
|
||||||
|
|
||||||
for extra_network_name, extra_network_args in extra_network_data.items():
|
for extra_network, extra_network_args in lookup_extra_networks(extra_network_data).items():
|
||||||
extra_network = extra_network_registry.get(extra_network_name, None)
|
|
||||||
|
|
||||||
if extra_network is None:
|
|
||||||
extra_network = extra_network_aliases.get(extra_network_name, None)
|
|
||||||
|
|
||||||
if extra_network is None:
|
|
||||||
print(f"Skipping unknown extra network: {extra_network_name}")
|
|
||||||
continue
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
extra_network.activate(p, extra_network_args)
|
extra_network.activate(p, extra_network_args)
|
||||||
activated.append(extra_network)
|
activated.append(extra_network)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
errors.display(e, f"activating extra network {extra_network_name} with arguments {extra_network_args}")
|
errors.display(e, f"activating extra network {extra_network.name} with arguments {extra_network_args}")
|
||||||
|
|
||||||
for extra_network_name, extra_network in extra_network_registry.items():
|
for extra_network_name, extra_network in extra_network_registry.items():
|
||||||
if extra_network in activated:
|
if extra_network in activated:
|
||||||
@@ -123,19 +154,16 @@ def deactivate(p, extra_network_data):
|
|||||||
"""call deactivate for extra networks in extra_network_data in specified order, then call
|
"""call deactivate for extra networks in extra_network_data in specified order, then call
|
||||||
deactivate for all remaining registered networks"""
|
deactivate for all remaining registered networks"""
|
||||||
|
|
||||||
for extra_network_name in extra_network_data:
|
data = lookup_extra_networks(extra_network_data)
|
||||||
extra_network = extra_network_registry.get(extra_network_name, None)
|
|
||||||
if extra_network is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
|
for extra_network in data:
|
||||||
try:
|
try:
|
||||||
extra_network.deactivate(p)
|
extra_network.deactivate(p)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
errors.display(e, f"deactivating extra network {extra_network_name}")
|
errors.display(e, f"deactivating extra network {extra_network.name}")
|
||||||
|
|
||||||
for extra_network_name, extra_network in extra_network_registry.items():
|
for extra_network_name, extra_network in extra_network_registry.items():
|
||||||
args = extra_network_data.get(extra_network_name, None)
|
if extra_network in data:
|
||||||
if args is not None:
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -177,3 +205,21 @@ def parse_prompts(prompts):
|
|||||||
|
|
||||||
return res, extra_data
|
return res, extra_data
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_metadata(filename, lister=None):
|
||||||
|
if filename is None:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
basename, ext = os.path.splitext(filename)
|
||||||
|
metadata_filename = basename + '.json'
|
||||||
|
|
||||||
|
metadata = {}
|
||||||
|
try:
|
||||||
|
exists = lister.exists(metadata_filename) if lister else os.path.exists(metadata_filename)
|
||||||
|
if exists:
|
||||||
|
with open(metadata_filename, "r", encoding="utf8") as file:
|
||||||
|
metadata = json.load(file)
|
||||||
|
except Exception as e:
|
||||||
|
errors.display(e, f"reading extra network user metadata from {metadata_filename}")
|
||||||
|
|
||||||
|
return metadata
|
||||||
|
|||||||
+33
-6
@@ -7,7 +7,7 @@ import json
|
|||||||
import torch
|
import torch
|
||||||
import tqdm
|
import tqdm
|
||||||
|
|
||||||
from modules import shared, images, sd_models, sd_vae, sd_models_config
|
from modules import shared, images, sd_models, sd_vae, sd_models_config, errors
|
||||||
from modules.ui_common import plaintext_to_html
|
from modules.ui_common import plaintext_to_html
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
import safetensors.torch
|
import safetensors.torch
|
||||||
@@ -72,7 +72,20 @@ def to_half(tensor, enable):
|
|||||||
return tensor
|
return tensor
|
||||||
|
|
||||||
|
|
||||||
def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_model_name, interp_method, multiplier, save_as_half, custom_name, checkpoint_format, config_source, bake_in_vae, discard_weights, save_metadata):
|
def read_metadata(primary_model_name, secondary_model_name, tertiary_model_name):
|
||||||
|
metadata = {}
|
||||||
|
|
||||||
|
for checkpoint_name in [primary_model_name, secondary_model_name, tertiary_model_name]:
|
||||||
|
checkpoint_info = sd_models.checkpoints_list.get(checkpoint_name, None)
|
||||||
|
if checkpoint_info is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
metadata.update(checkpoint_info.metadata)
|
||||||
|
|
||||||
|
return json.dumps(metadata, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
|
|
||||||
|
def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_model_name, interp_method, multiplier, save_as_half, custom_name, checkpoint_format, config_source, bake_in_vae, discard_weights, save_metadata, add_merge_recipe, copy_metadata_fields, metadata_json):
|
||||||
shared.state.begin(job="model-merge")
|
shared.state.begin(job="model-merge")
|
||||||
|
|
||||||
def fail(message):
|
def fail(message):
|
||||||
@@ -241,11 +254,25 @@ def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_
|
|||||||
shared.state.textinfo = "Saving"
|
shared.state.textinfo = "Saving"
|
||||||
print(f"Saving to {output_modelname}...")
|
print(f"Saving to {output_modelname}...")
|
||||||
|
|
||||||
metadata = None
|
metadata = {}
|
||||||
|
|
||||||
|
if save_metadata and copy_metadata_fields:
|
||||||
|
if primary_model_info:
|
||||||
|
metadata.update(primary_model_info.metadata)
|
||||||
|
if secondary_model_info:
|
||||||
|
metadata.update(secondary_model_info.metadata)
|
||||||
|
if tertiary_model_info:
|
||||||
|
metadata.update(tertiary_model_info.metadata)
|
||||||
|
|
||||||
if save_metadata:
|
if save_metadata:
|
||||||
metadata = {"format": "pt"}
|
try:
|
||||||
|
metadata.update(json.loads(metadata_json))
|
||||||
|
except Exception as e:
|
||||||
|
errors.display(e, "readin metadata from json")
|
||||||
|
|
||||||
|
metadata["format"] = "pt"
|
||||||
|
|
||||||
|
if save_metadata and add_merge_recipe:
|
||||||
merge_recipe = {
|
merge_recipe = {
|
||||||
"type": "webui", # indicate this model was merged with webui's built-in merger
|
"type": "webui", # indicate this model was merged with webui's built-in merger
|
||||||
"primary_model_hash": primary_model_info.sha256,
|
"primary_model_hash": primary_model_info.sha256,
|
||||||
@@ -261,7 +288,6 @@ def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_
|
|||||||
"is_inpainting": result_is_inpainting_model,
|
"is_inpainting": result_is_inpainting_model,
|
||||||
"is_instruct_pix2pix": result_is_instruct_pix2pix_model
|
"is_instruct_pix2pix": result_is_instruct_pix2pix_model
|
||||||
}
|
}
|
||||||
metadata["sd_merge_recipe"] = json.dumps(merge_recipe)
|
|
||||||
|
|
||||||
sd_merge_models = {}
|
sd_merge_models = {}
|
||||||
|
|
||||||
@@ -281,11 +307,12 @@ def run_modelmerger(id_task, primary_model_name, secondary_model_name, tertiary_
|
|||||||
if tertiary_model_info:
|
if tertiary_model_info:
|
||||||
add_model_metadata(tertiary_model_info)
|
add_model_metadata(tertiary_model_info)
|
||||||
|
|
||||||
|
metadata["sd_merge_recipe"] = json.dumps(merge_recipe)
|
||||||
metadata["sd_merge_models"] = json.dumps(sd_merge_models)
|
metadata["sd_merge_models"] = json.dumps(sd_merge_models)
|
||||||
|
|
||||||
_, extension = os.path.splitext(output_modelname)
|
_, extension = os.path.splitext(output_modelname)
|
||||||
if extension.lower() == ".safetensors":
|
if extension.lower() == ".safetensors":
|
||||||
safetensors.torch.save_file(theta_0, output_modelname, metadata=metadata)
|
safetensors.torch.save_file(theta_0, output_modelname, metadata=metadata if len(metadata)>0 else None)
|
||||||
else:
|
else:
|
||||||
torch.save(theta_0, output_modelname)
|
torch.save(theta_0, output_modelname)
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,180 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from functools import cached_property
|
||||||
|
from typing import TYPE_CHECKING, Callable
|
||||||
|
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
import torch
|
||||||
|
|
||||||
|
from modules import devices, errors, face_restoration, shared
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
|
def bgr_image_to_rgb_tensor(img: np.ndarray) -> torch.Tensor:
|
||||||
|
"""Convert a BGR NumPy image in [0..1] range to a PyTorch RGB float32 tensor."""
|
||||||
|
assert img.shape[2] == 3, "image must be RGB"
|
||||||
|
if img.dtype == "float64":
|
||||||
|
img = img.astype("float32")
|
||||||
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
||||||
|
return torch.from_numpy(img.transpose(2, 0, 1)).float()
|
||||||
|
|
||||||
|
|
||||||
|
def rgb_tensor_to_bgr_image(tensor: torch.Tensor, *, min_max=(0.0, 1.0)) -> np.ndarray:
|
||||||
|
"""
|
||||||
|
Convert a PyTorch RGB tensor in range `min_max` to a BGR NumPy image in [0..1] range.
|
||||||
|
"""
|
||||||
|
tensor = tensor.squeeze(0).float().detach().cpu().clamp_(*min_max)
|
||||||
|
tensor = (tensor - min_max[0]) / (min_max[1] - min_max[0])
|
||||||
|
assert tensor.dim() == 3, "tensor must be RGB"
|
||||||
|
img_np = tensor.numpy().transpose(1, 2, 0)
|
||||||
|
if img_np.shape[2] == 1: # gray image, no RGB/BGR required
|
||||||
|
return np.squeeze(img_np, axis=2)
|
||||||
|
return cv2.cvtColor(img_np, cv2.COLOR_BGR2RGB)
|
||||||
|
|
||||||
|
|
||||||
|
def create_face_helper(device) -> FaceRestoreHelper:
|
||||||
|
from facexlib.detection import retinaface
|
||||||
|
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
|
||||||
|
if hasattr(retinaface, 'device'):
|
||||||
|
retinaface.device = device
|
||||||
|
return FaceRestoreHelper(
|
||||||
|
upscale_factor=1,
|
||||||
|
face_size=512,
|
||||||
|
crop_ratio=(1, 1),
|
||||||
|
det_model='retinaface_resnet50',
|
||||||
|
save_ext='png',
|
||||||
|
use_parse=True,
|
||||||
|
device=device,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def restore_with_face_helper(
|
||||||
|
np_image: np.ndarray,
|
||||||
|
face_helper: FaceRestoreHelper,
|
||||||
|
restore_face: Callable[[torch.Tensor], torch.Tensor],
|
||||||
|
) -> np.ndarray:
|
||||||
|
"""
|
||||||
|
Find faces in the image using face_helper, restore them using restore_face, and paste them back into the image.
|
||||||
|
|
||||||
|
`restore_face` should take a cropped face image and return a restored face image.
|
||||||
|
"""
|
||||||
|
from torchvision.transforms.functional import normalize
|
||||||
|
np_image = np_image[:, :, ::-1]
|
||||||
|
original_resolution = np_image.shape[0:2]
|
||||||
|
|
||||||
|
try:
|
||||||
|
logger.debug("Detecting faces...")
|
||||||
|
face_helper.clean_all()
|
||||||
|
face_helper.read_image(np_image)
|
||||||
|
face_helper.get_face_landmarks_5(only_center_face=False, resize=640, eye_dist_threshold=5)
|
||||||
|
face_helper.align_warp_face()
|
||||||
|
logger.debug("Found %d faces, restoring", len(face_helper.cropped_faces))
|
||||||
|
for cropped_face in face_helper.cropped_faces:
|
||||||
|
cropped_face_t = bgr_image_to_rgb_tensor(cropped_face / 255.0)
|
||||||
|
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
|
||||||
|
cropped_face_t = cropped_face_t.unsqueeze(0).to(devices.device_codeformer)
|
||||||
|
|
||||||
|
try:
|
||||||
|
with torch.no_grad():
|
||||||
|
cropped_face_t = restore_face(cropped_face_t)
|
||||||
|
devices.torch_gc()
|
||||||
|
except Exception:
|
||||||
|
errors.report('Failed face-restoration inference', exc_info=True)
|
||||||
|
|
||||||
|
restored_face = rgb_tensor_to_bgr_image(cropped_face_t, min_max=(-1, 1))
|
||||||
|
restored_face = (restored_face * 255.0).astype('uint8')
|
||||||
|
face_helper.add_restored_face(restored_face)
|
||||||
|
|
||||||
|
logger.debug("Merging restored faces into image")
|
||||||
|
face_helper.get_inverse_affine(None)
|
||||||
|
img = face_helper.paste_faces_to_input_image()
|
||||||
|
img = img[:, :, ::-1]
|
||||||
|
if original_resolution != img.shape[0:2]:
|
||||||
|
img = cv2.resize(
|
||||||
|
img,
|
||||||
|
(0, 0),
|
||||||
|
fx=original_resolution[1] / img.shape[1],
|
||||||
|
fy=original_resolution[0] / img.shape[0],
|
||||||
|
interpolation=cv2.INTER_LINEAR,
|
||||||
|
)
|
||||||
|
logger.debug("Face restoration complete")
|
||||||
|
finally:
|
||||||
|
face_helper.clean_all()
|
||||||
|
return img
|
||||||
|
|
||||||
|
|
||||||
|
class CommonFaceRestoration(face_restoration.FaceRestoration):
|
||||||
|
net: torch.Module | None
|
||||||
|
model_url: str
|
||||||
|
model_download_name: str
|
||||||
|
|
||||||
|
def __init__(self, model_path: str):
|
||||||
|
super().__init__()
|
||||||
|
self.net = None
|
||||||
|
self.model_path = model_path
|
||||||
|
os.makedirs(model_path, exist_ok=True)
|
||||||
|
|
||||||
|
@cached_property
|
||||||
|
def face_helper(self) -> FaceRestoreHelper:
|
||||||
|
return create_face_helper(self.get_device())
|
||||||
|
|
||||||
|
def send_model_to(self, device):
|
||||||
|
if self.net:
|
||||||
|
logger.debug("Sending %s to %s", self.net, device)
|
||||||
|
self.net.to(device)
|
||||||
|
if self.face_helper:
|
||||||
|
logger.debug("Sending face helper to %s", device)
|
||||||
|
self.face_helper.face_det.to(device)
|
||||||
|
self.face_helper.face_parse.to(device)
|
||||||
|
|
||||||
|
def get_device(self):
|
||||||
|
raise NotImplementedError("get_device must be implemented by subclasses")
|
||||||
|
|
||||||
|
def load_net(self) -> torch.Module:
|
||||||
|
raise NotImplementedError("load_net must be implemented by subclasses")
|
||||||
|
|
||||||
|
def restore_with_helper(
|
||||||
|
self,
|
||||||
|
np_image: np.ndarray,
|
||||||
|
restore_face: Callable[[torch.Tensor], torch.Tensor],
|
||||||
|
) -> np.ndarray:
|
||||||
|
try:
|
||||||
|
if self.net is None:
|
||||||
|
self.net = self.load_net()
|
||||||
|
except Exception:
|
||||||
|
logger.warning("Unable to load face-restoration model", exc_info=True)
|
||||||
|
return np_image
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.send_model_to(self.get_device())
|
||||||
|
return restore_with_face_helper(np_image, self.face_helper, restore_face)
|
||||||
|
finally:
|
||||||
|
if shared.opts.face_restoration_unload:
|
||||||
|
self.send_model_to(devices.cpu)
|
||||||
|
|
||||||
|
|
||||||
|
def patch_facexlib(dirname: str) -> None:
|
||||||
|
import facexlib.detection
|
||||||
|
import facexlib.parsing
|
||||||
|
|
||||||
|
det_facex_load_file_from_url = facexlib.detection.load_file_from_url
|
||||||
|
par_facex_load_file_from_url = facexlib.parsing.load_file_from_url
|
||||||
|
|
||||||
|
def update_kwargs(kwargs):
|
||||||
|
return dict(kwargs, save_dir=dirname, model_dir=None)
|
||||||
|
|
||||||
|
def facex_load_file_from_url(**kwargs):
|
||||||
|
return det_facex_load_file_from_url(**update_kwargs(kwargs))
|
||||||
|
|
||||||
|
def facex_load_file_from_url2(**kwargs):
|
||||||
|
return par_facex_load_file_from_url(**update_kwargs(kwargs))
|
||||||
|
|
||||||
|
facexlib.detection.load_file_from_url = facex_load_file_from_url
|
||||||
|
facexlib.parsing.load_file_from_url = facex_load_file_from_url2
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
import threading
|
||||||
|
import collections
|
||||||
|
|
||||||
|
|
||||||
|
# reference: https://gist.github.com/vitaliyp/6d54dd76ca2c3cdfc1149d33007dc34a
|
||||||
|
class FIFOLock(object):
|
||||||
|
def __init__(self):
|
||||||
|
self._lock = threading.Lock()
|
||||||
|
self._inner_lock = threading.Lock()
|
||||||
|
self._pending_threads = collections.deque()
|
||||||
|
|
||||||
|
def acquire(self, blocking=True):
|
||||||
|
with self._inner_lock:
|
||||||
|
lock_acquired = self._lock.acquire(False)
|
||||||
|
if lock_acquired:
|
||||||
|
return True
|
||||||
|
elif not blocking:
|
||||||
|
return False
|
||||||
|
|
||||||
|
release_event = threading.Event()
|
||||||
|
self._pending_threads.append(release_event)
|
||||||
|
|
||||||
|
release_event.wait()
|
||||||
|
return self._lock.acquire()
|
||||||
|
|
||||||
|
def release(self):
|
||||||
|
with self._inner_lock:
|
||||||
|
if self._pending_threads:
|
||||||
|
release_event = self._pending_threads.popleft()
|
||||||
|
release_event.set()
|
||||||
|
|
||||||
|
self._lock.release()
|
||||||
|
|
||||||
|
__enter__ = acquire
|
||||||
|
|
||||||
|
def __exit__(self, t, v, tb):
|
||||||
|
self.release()
|
||||||
+50
-89
@@ -1,110 +1,71 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import facexlib
|
import torch
|
||||||
import gfpgan
|
|
||||||
|
|
||||||
import modules.face_restoration
|
from modules import (
|
||||||
from modules import paths, shared, devices, modelloader, errors
|
devices,
|
||||||
|
errors,
|
||||||
|
face_restoration,
|
||||||
|
face_restoration_utils,
|
||||||
|
modelloader,
|
||||||
|
shared,
|
||||||
|
)
|
||||||
|
|
||||||
model_dir = "GFPGAN"
|
logger = logging.getLogger(__name__)
|
||||||
user_path = None
|
|
||||||
model_path = os.path.join(paths.models_path, model_dir)
|
|
||||||
model_url = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
|
model_url = "https://github.com/TencentARC/GFPGAN/releases/download/v1.3.0/GFPGANv1.4.pth"
|
||||||
have_gfpgan = False
|
model_download_name = "GFPGANv1.4.pth"
|
||||||
loaded_gfpgan_model = None
|
gfpgan_face_restorer: face_restoration.FaceRestoration | None = None
|
||||||
|
|
||||||
|
|
||||||
def gfpgann():
|
class FaceRestorerGFPGAN(face_restoration_utils.CommonFaceRestoration):
|
||||||
global loaded_gfpgan_model
|
def name(self):
|
||||||
global model_path
|
return "GFPGAN"
|
||||||
if loaded_gfpgan_model is not None:
|
|
||||||
loaded_gfpgan_model.gfpgan.to(devices.device_gfpgan)
|
|
||||||
return loaded_gfpgan_model
|
|
||||||
|
|
||||||
if gfpgan_constructor is None:
|
def get_device(self):
|
||||||
return None
|
return devices.device_gfpgan
|
||||||
|
|
||||||
models = modelloader.load_models(model_path, model_url, user_path, ext_filter="GFPGAN")
|
def load_net(self) -> torch.Module:
|
||||||
if len(models) == 1 and models[0].startswith("http"):
|
for model_path in modelloader.load_models(
|
||||||
model_file = models[0]
|
model_path=self.model_path,
|
||||||
elif len(models) != 0:
|
model_url=model_url,
|
||||||
latest_file = max(models, key=os.path.getctime)
|
command_path=self.model_path,
|
||||||
model_file = latest_file
|
download_name=model_download_name,
|
||||||
else:
|
ext_filter=['.pth'],
|
||||||
print("Unable to load gfpgan model!")
|
):
|
||||||
return None
|
if 'GFPGAN' in os.path.basename(model_path):
|
||||||
if hasattr(facexlib.detection.retinaface, 'device'):
|
model = modelloader.load_spandrel_model(
|
||||||
facexlib.detection.retinaface.device = devices.device_gfpgan
|
model_path,
|
||||||
model = gfpgan_constructor(model_path=model_file, upscale=1, arch='clean', channel_multiplier=2, bg_upsampler=None, device=devices.device_gfpgan)
|
device=self.get_device(),
|
||||||
loaded_gfpgan_model = model
|
expected_architecture='GFPGAN',
|
||||||
|
).model
|
||||||
|
model.different_w = True # see https://github.com/chaiNNer-org/spandrel/pull/81
|
||||||
|
return model
|
||||||
|
raise ValueError("No GFPGAN model found")
|
||||||
|
|
||||||
return model
|
def restore(self, np_image):
|
||||||
|
def restore_face(cropped_face_t):
|
||||||
|
assert self.net is not None
|
||||||
|
return self.net(cropped_face_t, return_rgb=False)[0]
|
||||||
|
|
||||||
|
return self.restore_with_helper(np_image, restore_face)
|
||||||
def send_model_to(model, device):
|
|
||||||
model.gfpgan.to(device)
|
|
||||||
model.face_helper.face_det.to(device)
|
|
||||||
model.face_helper.face_parse.to(device)
|
|
||||||
|
|
||||||
|
|
||||||
def gfpgan_fix_faces(np_image):
|
def gfpgan_fix_faces(np_image):
|
||||||
model = gfpgann()
|
if gfpgan_face_restorer:
|
||||||
if model is None:
|
return gfpgan_face_restorer.restore(np_image)
|
||||||
return np_image
|
logger.warning("GFPGAN face restorer not set up")
|
||||||
|
|
||||||
send_model_to(model, devices.device_gfpgan)
|
|
||||||
|
|
||||||
np_image_bgr = np_image[:, :, ::-1]
|
|
||||||
cropped_faces, restored_faces, gfpgan_output_bgr = model.enhance(np_image_bgr, has_aligned=False, only_center_face=False, paste_back=True)
|
|
||||||
np_image = gfpgan_output_bgr[:, :, ::-1]
|
|
||||||
|
|
||||||
model.face_helper.clean_all()
|
|
||||||
|
|
||||||
if shared.opts.face_restoration_unload:
|
|
||||||
send_model_to(model, devices.cpu)
|
|
||||||
|
|
||||||
return np_image
|
return np_image
|
||||||
|
|
||||||
|
|
||||||
gfpgan_constructor = None
|
def setup_model(dirname: str) -> None:
|
||||||
|
global gfpgan_face_restorer
|
||||||
|
|
||||||
|
|
||||||
def setup_model(dirname):
|
|
||||||
try:
|
try:
|
||||||
os.makedirs(model_path, exist_ok=True)
|
face_restoration_utils.patch_facexlib(dirname)
|
||||||
from gfpgan import GFPGANer
|
gfpgan_face_restorer = FaceRestorerGFPGAN(model_path=dirname)
|
||||||
from facexlib import detection, parsing # noqa: F401
|
shared.face_restorers.append(gfpgan_face_restorer)
|
||||||
global user_path
|
|
||||||
global have_gfpgan
|
|
||||||
global gfpgan_constructor
|
|
||||||
|
|
||||||
load_file_from_url_orig = gfpgan.utils.load_file_from_url
|
|
||||||
facex_load_file_from_url_orig = facexlib.detection.load_file_from_url
|
|
||||||
facex_load_file_from_url_orig2 = facexlib.parsing.load_file_from_url
|
|
||||||
|
|
||||||
def my_load_file_from_url(**kwargs):
|
|
||||||
return load_file_from_url_orig(**dict(kwargs, model_dir=model_path))
|
|
||||||
|
|
||||||
def facex_load_file_from_url(**kwargs):
|
|
||||||
return facex_load_file_from_url_orig(**dict(kwargs, save_dir=model_path, model_dir=None))
|
|
||||||
|
|
||||||
def facex_load_file_from_url2(**kwargs):
|
|
||||||
return facex_load_file_from_url_orig2(**dict(kwargs, save_dir=model_path, model_dir=None))
|
|
||||||
|
|
||||||
gfpgan.utils.load_file_from_url = my_load_file_from_url
|
|
||||||
facexlib.detection.load_file_from_url = facex_load_file_from_url
|
|
||||||
facexlib.parsing.load_file_from_url = facex_load_file_from_url2
|
|
||||||
user_path = dirname
|
|
||||||
have_gfpgan = True
|
|
||||||
gfpgan_constructor = GFPGANer
|
|
||||||
|
|
||||||
class FaceRestorerGFPGAN(modules.face_restoration.FaceRestoration):
|
|
||||||
def name(self):
|
|
||||||
return "GFPGAN"
|
|
||||||
|
|
||||||
def restore(self, np_image):
|
|
||||||
return gfpgan_fix_faces(np_image)
|
|
||||||
|
|
||||||
shared.face_restorers.append(FaceRestorerGFPGAN())
|
|
||||||
except Exception:
|
except Exception:
|
||||||
errors.report("Error setting up GFPGAN", exc_info=True)
|
errors.report("Error setting up GFPGAN", exc_info=True)
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ class Git(git.Git):
|
|||||||
)
|
)
|
||||||
return self._parse_object_header(ret)
|
return self._parse_object_header(ret)
|
||||||
|
|
||||||
def stream_object_data(self, ref: str) -> tuple[str, str, int, "Git.CatFileContentStream"]:
|
def stream_object_data(self, ref: str) -> tuple[str, str, int, Git.CatFileContentStream]:
|
||||||
# Not really streaming, per se; this buffers the entire object in memory.
|
# Not really streaming, per se; this buffers the entire object in memory.
|
||||||
# Shouldn't be a problem for our use case, since we're only using this for
|
# Shouldn't be a problem for our use case, since we're only using this for
|
||||||
# object headers (commit objects).
|
# object headers (commit objects).
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
import gradio as gr
|
||||||
|
|
||||||
|
from modules import scripts, ui_tempdir, patches
|
||||||
|
|
||||||
|
|
||||||
|
def add_classes_to_gradio_component(comp):
|
||||||
|
"""
|
||||||
|
this adds gradio-* to the component for css styling (ie gradio-button to gr.Button), as well as some others
|
||||||
|
"""
|
||||||
|
|
||||||
|
comp.elem_classes = [f"gradio-{comp.get_block_name()}", *(comp.elem_classes or [])]
|
||||||
|
|
||||||
|
if getattr(comp, 'multiselect', False):
|
||||||
|
comp.elem_classes.append('multiselect')
|
||||||
|
|
||||||
|
|
||||||
|
def IOComponent_init(self, *args, **kwargs):
|
||||||
|
self.webui_tooltip = kwargs.pop('tooltip', None)
|
||||||
|
|
||||||
|
if scripts.scripts_current is not None:
|
||||||
|
scripts.scripts_current.before_component(self, **kwargs)
|
||||||
|
|
||||||
|
scripts.script_callbacks.before_component_callback(self, **kwargs)
|
||||||
|
|
||||||
|
res = original_IOComponent_init(self, *args, **kwargs)
|
||||||
|
|
||||||
|
add_classes_to_gradio_component(self)
|
||||||
|
|
||||||
|
scripts.script_callbacks.after_component_callback(self, **kwargs)
|
||||||
|
|
||||||
|
if scripts.scripts_current is not None:
|
||||||
|
scripts.scripts_current.after_component(self, **kwargs)
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def Block_get_config(self):
|
||||||
|
config = original_Block_get_config(self)
|
||||||
|
|
||||||
|
webui_tooltip = getattr(self, 'webui_tooltip', None)
|
||||||
|
if webui_tooltip:
|
||||||
|
config["webui_tooltip"] = webui_tooltip
|
||||||
|
|
||||||
|
config.pop('example_inputs', None)
|
||||||
|
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
def BlockContext_init(self, *args, **kwargs):
|
||||||
|
if scripts.scripts_current is not None:
|
||||||
|
scripts.scripts_current.before_component(self, **kwargs)
|
||||||
|
|
||||||
|
scripts.script_callbacks.before_component_callback(self, **kwargs)
|
||||||
|
|
||||||
|
res = original_BlockContext_init(self, *args, **kwargs)
|
||||||
|
|
||||||
|
add_classes_to_gradio_component(self)
|
||||||
|
|
||||||
|
scripts.script_callbacks.after_component_callback(self, **kwargs)
|
||||||
|
|
||||||
|
if scripts.scripts_current is not None:
|
||||||
|
scripts.scripts_current.after_component(self, **kwargs)
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def Blocks_get_config_file(self, *args, **kwargs):
|
||||||
|
config = original_Blocks_get_config_file(self, *args, **kwargs)
|
||||||
|
|
||||||
|
for comp_config in config["components"]:
|
||||||
|
if "example_inputs" in comp_config:
|
||||||
|
comp_config["example_inputs"] = {"serialized": []}
|
||||||
|
|
||||||
|
return config
|
||||||
|
|
||||||
|
|
||||||
|
original_IOComponent_init = patches.patch(__name__, obj=gr.components.IOComponent, field="__init__", replacement=IOComponent_init)
|
||||||
|
original_Block_get_config = patches.patch(__name__, obj=gr.blocks.Block, field="get_config", replacement=Block_get_config)
|
||||||
|
original_BlockContext_init = patches.patch(__name__, obj=gr.blocks.BlockContext, field="__init__", replacement=BlockContext_init)
|
||||||
|
original_Blocks_get_config_file = patches.patch(__name__, obj=gr.blocks.Blocks, field="get_config_file", replacement=Blocks_get_config_file)
|
||||||
|
|
||||||
|
|
||||||
|
ui_tempdir.install_ui_tempdir_override()
|
||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from modules import modelloader, devices
|
||||||
|
from modules.shared import opts
|
||||||
|
from modules.upscaler import Upscaler, UpscalerData
|
||||||
|
from modules.upscaler_utils import upscale_with_model
|
||||||
|
|
||||||
|
|
||||||
|
class UpscalerHAT(Upscaler):
|
||||||
|
def __init__(self, dirname):
|
||||||
|
self.name = "HAT"
|
||||||
|
self.scalers = []
|
||||||
|
self.user_path = dirname
|
||||||
|
super().__init__()
|
||||||
|
for file in self.find_models(ext_filter=[".pt", ".pth"]):
|
||||||
|
name = modelloader.friendly_name(file)
|
||||||
|
scale = 4 # TODO: scale might not be 4, but we can't know without loading the model
|
||||||
|
scaler_data = UpscalerData(name, file, upscaler=self, scale=scale)
|
||||||
|
self.scalers.append(scaler_data)
|
||||||
|
|
||||||
|
def do_upscale(self, img, selected_model):
|
||||||
|
try:
|
||||||
|
model = self.load_model(selected_model)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Unable to load HAT model {selected_model}: {e}", file=sys.stderr)
|
||||||
|
return img
|
||||||
|
model.to(devices.device_esrgan) # TODO: should probably be device_hat
|
||||||
|
return upscale_with_model(
|
||||||
|
model,
|
||||||
|
img,
|
||||||
|
tile_size=opts.ESRGAN_tile, # TODO: should probably be HAT_tile
|
||||||
|
tile_overlap=opts.ESRGAN_tile_overlap, # TODO: should probably be HAT_tile_overlap
|
||||||
|
)
|
||||||
|
|
||||||
|
def load_model(self, path: str):
|
||||||
|
if not os.path.isfile(path):
|
||||||
|
raise FileNotFoundError(f"Model file {path} not found")
|
||||||
|
return modelloader.load_spandrel_model(
|
||||||
|
path,
|
||||||
|
device=devices.device_esrgan, # TODO: should probably be device_hat
|
||||||
|
expected_architecture='HAT',
|
||||||
|
)
|
||||||
@@ -10,7 +10,7 @@ import torch
|
|||||||
import tqdm
|
import tqdm
|
||||||
from einops import rearrange, repeat
|
from einops import rearrange, repeat
|
||||||
from ldm.util import default
|
from ldm.util import default
|
||||||
from modules import devices, processing, sd_models, shared, sd_samplers, hashes, sd_hijack_checkpoint, errors
|
from modules import devices, sd_models, shared, sd_samplers, hashes, sd_hijack_checkpoint, errors
|
||||||
from modules.textual_inversion import textual_inversion, logging
|
from modules.textual_inversion import textual_inversion, logging
|
||||||
from modules.textual_inversion.learn_schedule import LearnRateScheduler
|
from modules.textual_inversion.learn_schedule import LearnRateScheduler
|
||||||
from torch import einsum
|
from torch import einsum
|
||||||
@@ -468,9 +468,8 @@ def create_hypernetwork(name, enable_sizes, overwrite_old, layer_structure=None,
|
|||||||
shared.reload_hypernetworks()
|
shared.reload_hypernetworks()
|
||||||
|
|
||||||
|
|
||||||
def train_hypernetwork(id_task, hypernetwork_name, learn_rate, batch_size, gradient_step, data_root, log_directory, training_width, training_height, varsize, steps, clip_grad_mode, clip_grad_value, shuffle_tags, tag_drop_out, latent_sampling_method, use_weight, create_image_every, save_hypernetwork_every, template_filename, preview_from_txt2img, preview_prompt, preview_negative_prompt, preview_steps, preview_sampler_index, preview_cfg_scale, preview_seed, preview_width, preview_height):
|
def train_hypernetwork(id_task, hypernetwork_name: str, learn_rate: float, batch_size: int, gradient_step: int, data_root: str, log_directory: str, training_width: int, training_height: int, varsize: bool, steps: int, clip_grad_mode: str, clip_grad_value: float, shuffle_tags: bool, tag_drop_out: bool, latent_sampling_method: str, use_weight: bool, create_image_every: int, save_hypernetwork_every: int, template_filename: str, preview_from_txt2img: bool, preview_prompt: str, preview_negative_prompt: str, preview_steps: int, preview_sampler_name: str, preview_cfg_scale: float, preview_seed: int, preview_width: int, preview_height: int):
|
||||||
# images allows training previews to have infotext. Importing it at the top causes a circular import problem.
|
from modules import images, processing
|
||||||
from modules import images
|
|
||||||
|
|
||||||
save_hypernetwork_every = save_hypernetwork_every or 0
|
save_hypernetwork_every = save_hypernetwork_every or 0
|
||||||
create_image_every = create_image_every or 0
|
create_image_every = create_image_every or 0
|
||||||
@@ -699,7 +698,7 @@ def train_hypernetwork(id_task, hypernetwork_name, learn_rate, batch_size, gradi
|
|||||||
p.prompt = preview_prompt
|
p.prompt = preview_prompt
|
||||||
p.negative_prompt = preview_negative_prompt
|
p.negative_prompt = preview_negative_prompt
|
||||||
p.steps = preview_steps
|
p.steps = preview_steps
|
||||||
p.sampler_name = sd_samplers.samplers[preview_sampler_index].name
|
p.sampler_name = sd_samplers.samplers_map[preview_sampler_name.lower()]
|
||||||
p.cfg_scale = preview_cfg_scale
|
p.cfg_scale = preview_cfg_scale
|
||||||
p.seed = preview_seed
|
p.seed = preview_seed
|
||||||
p.width = preview_width
|
p.width = preview_width
|
||||||
|
|||||||
+62
-21
@@ -21,8 +21,6 @@ from modules import sd_samplers, shared, script_callbacks, errors
|
|||||||
from modules.paths_internal import roboto_ttf_file
|
from modules.paths_internal import roboto_ttf_file
|
||||||
from modules.shared import opts
|
from modules.shared import opts
|
||||||
|
|
||||||
import modules.sd_vae as sd_vae
|
|
||||||
|
|
||||||
LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)
|
LANCZOS = (Image.Resampling.LANCZOS if hasattr(Image, 'Resampling') else Image.LANCZOS)
|
||||||
|
|
||||||
|
|
||||||
@@ -63,12 +61,17 @@ def image_grid(imgs, batch_size=1, rows=None):
|
|||||||
return grid
|
return grid
|
||||||
|
|
||||||
|
|
||||||
Grid = namedtuple("Grid", ["tiles", "tile_w", "tile_h", "image_w", "image_h", "overlap"])
|
class Grid(namedtuple("_Grid", ["tiles", "tile_w", "tile_h", "image_w", "image_h", "overlap"])):
|
||||||
|
@property
|
||||||
|
def tile_count(self) -> int:
|
||||||
|
"""
|
||||||
|
The total number of tiles in the grid.
|
||||||
|
"""
|
||||||
|
return sum(len(row[2]) for row in self.tiles)
|
||||||
|
|
||||||
|
|
||||||
def split_grid(image, tile_w=512, tile_h=512, overlap=64):
|
def split_grid(image: Image.Image, tile_w: int = 512, tile_h: int = 512, overlap: int = 64) -> Grid:
|
||||||
w = image.width
|
w, h = image.size
|
||||||
h = image.height
|
|
||||||
|
|
||||||
non_overlap_width = tile_w - overlap
|
non_overlap_width = tile_w - overlap
|
||||||
non_overlap_height = tile_h - overlap
|
non_overlap_height = tile_h - overlap
|
||||||
@@ -318,7 +321,7 @@ def resize_image(resize_mode, im, width, height, upscaler_name=None):
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
invalid_filename_chars = '<>:"/\\|?*\n'
|
invalid_filename_chars = '#<>:"/\\|?*\n\r\t'
|
||||||
invalid_filename_prefix = ' '
|
invalid_filename_prefix = ' '
|
||||||
invalid_filename_postfix = ' .'
|
invalid_filename_postfix = ' .'
|
||||||
re_nonletters = re.compile(r'[\s' + string.punctuation + ']+')
|
re_nonletters = re.compile(r'[\s' + string.punctuation + ']+')
|
||||||
@@ -342,16 +345,6 @@ def sanitize_filename_part(text, replace_spaces=True):
|
|||||||
|
|
||||||
|
|
||||||
class FilenameGenerator:
|
class FilenameGenerator:
|
||||||
def get_vae_filename(self): #get the name of the VAE file.
|
|
||||||
if sd_vae.loaded_vae_file is None:
|
|
||||||
return "NoneType"
|
|
||||||
file_name = os.path.basename(sd_vae.loaded_vae_file)
|
|
||||||
split_file_name = file_name.split('.')
|
|
||||||
if len(split_file_name) > 1 and split_file_name[0] == '':
|
|
||||||
return split_file_name[1] # if the first character of the filename is "." then [1] is obtained.
|
|
||||||
else:
|
|
||||||
return split_file_name[0]
|
|
||||||
|
|
||||||
replacements = {
|
replacements = {
|
||||||
'seed': lambda self: self.seed if self.seed is not None else '',
|
'seed': lambda self: self.seed if self.seed is not None else '',
|
||||||
'seed_first': lambda self: self.seed if self.p.batch_size == 1 else self.p.all_seeds[0],
|
'seed_first': lambda self: self.seed if self.p.batch_size == 1 else self.p.all_seeds[0],
|
||||||
@@ -367,7 +360,9 @@ class FilenameGenerator:
|
|||||||
'date': lambda self: datetime.datetime.now().strftime('%Y-%m-%d'),
|
'date': lambda self: datetime.datetime.now().strftime('%Y-%m-%d'),
|
||||||
'datetime': lambda self, *args: self.datetime(*args), # accepts formats: [datetime], [datetime<Format>], [datetime<Format><Time Zone>]
|
'datetime': lambda self, *args: self.datetime(*args), # accepts formats: [datetime], [datetime<Format>], [datetime<Format><Time Zone>]
|
||||||
'job_timestamp': lambda self: getattr(self.p, "job_timestamp", shared.state.job_timestamp),
|
'job_timestamp': lambda self: getattr(self.p, "job_timestamp", shared.state.job_timestamp),
|
||||||
'prompt_hash': lambda self: hashlib.sha256(self.prompt.encode()).hexdigest()[0:8],
|
'prompt_hash': lambda self, *args: self.string_hash(self.prompt, *args),
|
||||||
|
'negative_prompt_hash': lambda self, *args: self.string_hash(self.p.negative_prompt, *args),
|
||||||
|
'full_prompt_hash': lambda self, *args: self.string_hash(f"{self.p.prompt} {self.p.negative_prompt}", *args), # a space in between to create a unique string
|
||||||
'prompt': lambda self: sanitize_filename_part(self.prompt),
|
'prompt': lambda self: sanitize_filename_part(self.prompt),
|
||||||
'prompt_no_styles': lambda self: self.prompt_no_style(),
|
'prompt_no_styles': lambda self: self.prompt_no_style(),
|
||||||
'prompt_spaces': lambda self: sanitize_filename_part(self.prompt, replace_spaces=False),
|
'prompt_spaces': lambda self: sanitize_filename_part(self.prompt, replace_spaces=False),
|
||||||
@@ -380,7 +375,8 @@ class FilenameGenerator:
|
|||||||
'denoising': lambda self: self.p.denoising_strength if self.p and self.p.denoising_strength else NOTHING_AND_SKIP_PREVIOUS_TEXT,
|
'denoising': lambda self: self.p.denoising_strength if self.p and self.p.denoising_strength else NOTHING_AND_SKIP_PREVIOUS_TEXT,
|
||||||
'user': lambda self: self.p.user,
|
'user': lambda self: self.p.user,
|
||||||
'vae_filename': lambda self: self.get_vae_filename(),
|
'vae_filename': lambda self: self.get_vae_filename(),
|
||||||
'none': lambda self: '', # Overrides the default so you can get just the sequence number
|
'none': lambda self: '', # Overrides the default, so you can get just the sequence number
|
||||||
|
'image_hash': lambda self, *args: self.image_hash(*args) # accepts formats: [image_hash<length>] default full hash
|
||||||
}
|
}
|
||||||
default_time_format = '%Y%m%d%H%M%S'
|
default_time_format = '%Y%m%d%H%M%S'
|
||||||
|
|
||||||
@@ -391,6 +387,22 @@ class FilenameGenerator:
|
|||||||
self.image = image
|
self.image = image
|
||||||
self.zip = zip
|
self.zip = zip
|
||||||
|
|
||||||
|
def get_vae_filename(self):
|
||||||
|
"""Get the name of the VAE file."""
|
||||||
|
|
||||||
|
import modules.sd_vae as sd_vae
|
||||||
|
|
||||||
|
if sd_vae.loaded_vae_file is None:
|
||||||
|
return "NoneType"
|
||||||
|
|
||||||
|
file_name = os.path.basename(sd_vae.loaded_vae_file)
|
||||||
|
split_file_name = file_name.split('.')
|
||||||
|
if len(split_file_name) > 1 and split_file_name[0] == '':
|
||||||
|
return split_file_name[1] # if the first character of the filename is "." then [1] is obtained.
|
||||||
|
else:
|
||||||
|
return split_file_name[0]
|
||||||
|
|
||||||
|
|
||||||
def hasprompt(self, *args):
|
def hasprompt(self, *args):
|
||||||
lower = self.prompt.lower()
|
lower = self.prompt.lower()
|
||||||
if self.p is None or self.prompt is None:
|
if self.p is None or self.prompt is None:
|
||||||
@@ -444,6 +456,14 @@ class FilenameGenerator:
|
|||||||
|
|
||||||
return sanitize_filename_part(formatted_time, replace_spaces=False)
|
return sanitize_filename_part(formatted_time, replace_spaces=False)
|
||||||
|
|
||||||
|
def image_hash(self, *args):
|
||||||
|
length = int(args[0]) if (args and args[0] != "") else None
|
||||||
|
return hashlib.sha256(self.image.tobytes()).hexdigest()[0:length]
|
||||||
|
|
||||||
|
def string_hash(self, text, *args):
|
||||||
|
length = int(args[0]) if (args and args[0] != "") else 8
|
||||||
|
return hashlib.sha256(text.encode()).hexdigest()[0:length]
|
||||||
|
|
||||||
def apply(self, x):
|
def apply(self, x):
|
||||||
res = ''
|
res = ''
|
||||||
|
|
||||||
@@ -546,6 +566,8 @@ def save_image_with_geninfo(image, geninfo, filename, extension=None, existing_p
|
|||||||
})
|
})
|
||||||
|
|
||||||
piexif.insert(exif_bytes, filename)
|
piexif.insert(exif_bytes, filename)
|
||||||
|
elif extension.lower() == ".gif":
|
||||||
|
image.save(filename, format=image_format, comment=geninfo)
|
||||||
else:
|
else:
|
||||||
image.save(filename, format=image_format, quality=opts.jpeg_quality)
|
image.save(filename, format=image_format, quality=opts.jpeg_quality)
|
||||||
|
|
||||||
@@ -585,6 +607,11 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
|
|||||||
"""
|
"""
|
||||||
namegen = FilenameGenerator(p, seed, prompt, image)
|
namegen = FilenameGenerator(p, seed, prompt, image)
|
||||||
|
|
||||||
|
# WebP and JPG formats have maximum dimension limits of 16383 and 65535 respectively. switch to PNG which has a much higher limit
|
||||||
|
if (image.height > 65535 or image.width > 65535) and extension.lower() in ("jpg", "jpeg") or (image.height > 16383 or image.width > 16383) and extension.lower() == "webp":
|
||||||
|
print('Image dimensions too large; saving as PNG')
|
||||||
|
extension = ".png"
|
||||||
|
|
||||||
if save_to_dirs is None:
|
if save_to_dirs is None:
|
||||||
save_to_dirs = (grid and opts.grid_save_to_dirs) or (not grid and opts.save_to_dirs and not no_prompt)
|
save_to_dirs = (grid and opts.grid_save_to_dirs) or (not grid and opts.save_to_dirs and not no_prompt)
|
||||||
|
|
||||||
@@ -641,7 +668,13 @@ def save_image(image, path, basename, seed=None, prompt=None, extension='png', i
|
|||||||
|
|
||||||
save_image_with_geninfo(image_to_save, info, temp_file_path, extension, existing_pnginfo=params.pnginfo, pnginfo_section_name=pnginfo_section_name)
|
save_image_with_geninfo(image_to_save, info, temp_file_path, extension, existing_pnginfo=params.pnginfo, pnginfo_section_name=pnginfo_section_name)
|
||||||
|
|
||||||
os.replace(temp_file_path, filename_without_extension + extension)
|
filename = filename_without_extension + extension
|
||||||
|
if shared.opts.save_images_replace_action != "Replace":
|
||||||
|
n = 0
|
||||||
|
while os.path.exists(filename):
|
||||||
|
n += 1
|
||||||
|
filename = f"{filename_without_extension}-{n}{extension}"
|
||||||
|
os.replace(temp_file_path, filename)
|
||||||
|
|
||||||
fullfn_without_extension, extension = os.path.splitext(params.filename)
|
fullfn_without_extension, extension = os.path.splitext(params.filename)
|
||||||
if hasattr(os, 'statvfs'):
|
if hasattr(os, 'statvfs'):
|
||||||
@@ -698,7 +731,12 @@ def read_info_from_image(image: Image.Image) -> tuple[str | None, dict]:
|
|||||||
geninfo = items.pop('parameters', None)
|
geninfo = items.pop('parameters', None)
|
||||||
|
|
||||||
if "exif" in items:
|
if "exif" in items:
|
||||||
exif = piexif.load(items["exif"])
|
exif_data = items["exif"]
|
||||||
|
try:
|
||||||
|
exif = piexif.load(exif_data)
|
||||||
|
except OSError:
|
||||||
|
# memory / exif was not valid so piexif tried to read from a file
|
||||||
|
exif = None
|
||||||
exif_comment = (exif or {}).get("Exif", {}).get(piexif.ExifIFD.UserComment, b'')
|
exif_comment = (exif or {}).get("Exif", {}).get(piexif.ExifIFD.UserComment, b'')
|
||||||
try:
|
try:
|
||||||
exif_comment = piexif.helper.UserComment.load(exif_comment)
|
exif_comment = piexif.helper.UserComment.load(exif_comment)
|
||||||
@@ -708,6 +746,8 @@ def read_info_from_image(image: Image.Image) -> tuple[str | None, dict]:
|
|||||||
if exif_comment:
|
if exif_comment:
|
||||||
items['exif comment'] = exif_comment
|
items['exif comment'] = exif_comment
|
||||||
geninfo = exif_comment
|
geninfo = exif_comment
|
||||||
|
elif "comment" in items: # for gif
|
||||||
|
geninfo = items["comment"].decode('utf8', errors="ignore")
|
||||||
|
|
||||||
for field in IGNORED_INFO_KEYS:
|
for field in IGNORED_INFO_KEYS:
|
||||||
items.pop(field, None)
|
items.pop(field, None)
|
||||||
@@ -756,3 +796,4 @@ def flatten(img, bgcolor):
|
|||||||
img = background
|
img = background
|
||||||
|
|
||||||
return img.convert('RGB')
|
return img.convert('RGB')
|
||||||
|
|
||||||
|
|||||||
+50
-47
@@ -3,14 +3,14 @@ from contextlib import closing
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from PIL import Image, ImageOps, ImageFilter, ImageEnhance, ImageChops, UnidentifiedImageError
|
from PIL import Image, ImageOps, ImageFilter, ImageEnhance, UnidentifiedImageError
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
|
|
||||||
from modules import sd_samplers, images as imgutil
|
from modules import images as imgutil
|
||||||
from modules.generation_parameters_copypaste import create_override_settings_dict, parse_generation_parameters
|
from modules.infotext_utils import create_override_settings_dict, parse_generation_parameters
|
||||||
from modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images
|
from modules.processing import Processed, StableDiffusionProcessingImg2Img, process_images
|
||||||
from modules.shared import opts, state
|
from modules.shared import opts, state
|
||||||
from modules.images import save_image
|
from modules.sd_models import get_closet_checkpoint_match
|
||||||
import modules.shared as shared
|
import modules.shared as shared
|
||||||
import modules.processing as processing
|
import modules.processing as processing
|
||||||
from modules.ui import plaintext_to_html
|
from modules.ui import plaintext_to_html
|
||||||
@@ -18,9 +18,10 @@ import modules.scripts
|
|||||||
|
|
||||||
|
|
||||||
def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args, to_scale=False, scale_by=1.0, use_png_info=False, png_info_props=None, png_info_dir=None):
|
def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args, to_scale=False, scale_by=1.0, use_png_info=False, png_info_props=None, png_info_dir=None):
|
||||||
|
output_dir = output_dir.strip()
|
||||||
processing.fix_seed(p)
|
processing.fix_seed(p)
|
||||||
|
|
||||||
images = list(shared.walk_files(input_dir, allowed_extensions=(".png", ".jpg", ".jpeg", ".webp")))
|
images = list(shared.walk_files(input_dir, allowed_extensions=(".png", ".jpg", ".jpeg", ".webp", ".tif", ".tiff")))
|
||||||
|
|
||||||
is_inpaint_batch = False
|
is_inpaint_batch = False
|
||||||
if inpaint_mask_dir:
|
if inpaint_mask_dir:
|
||||||
@@ -32,11 +33,6 @@ def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args, to_scale=Fal
|
|||||||
|
|
||||||
print(f"Will process {len(images)} images, creating {p.n_iter * p.batch_size} new images for each.")
|
print(f"Will process {len(images)} images, creating {p.n_iter * p.batch_size} new images for each.")
|
||||||
|
|
||||||
save_normally = output_dir == ''
|
|
||||||
|
|
||||||
p.do_not_save_grid = True
|
|
||||||
p.do_not_save_samples = not save_normally
|
|
||||||
|
|
||||||
state.job_count = len(images) * p.n_iter
|
state.job_count = len(images) * p.n_iter
|
||||||
|
|
||||||
# extract "default" params to use in case getting png info fails
|
# extract "default" params to use in case getting png info fails
|
||||||
@@ -46,13 +42,16 @@ def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args, to_scale=Fal
|
|||||||
cfg_scale = p.cfg_scale
|
cfg_scale = p.cfg_scale
|
||||||
sampler_name = p.sampler_name
|
sampler_name = p.sampler_name
|
||||||
steps = p.steps
|
steps = p.steps
|
||||||
|
override_settings = p.override_settings
|
||||||
|
sd_model_checkpoint_override = get_closet_checkpoint_match(override_settings.get("sd_model_checkpoint", None))
|
||||||
|
batch_results = None
|
||||||
|
discard_further_results = False
|
||||||
for i, image in enumerate(images):
|
for i, image in enumerate(images):
|
||||||
state.job = f"{i+1} out of {len(images)}"
|
state.job = f"{i+1} out of {len(images)}"
|
||||||
if state.skipped:
|
if state.skipped:
|
||||||
state.skipped = False
|
state.skipped = False
|
||||||
|
|
||||||
if state.interrupted:
|
if state.interrupted or state.stopping_generation:
|
||||||
break
|
break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -109,42 +108,58 @@ def process_batch(p, input_dir, output_dir, inpaint_mask_dir, args, to_scale=Fal
|
|||||||
p.sampler_name = parsed_parameters.get("Sampler", sampler_name)
|
p.sampler_name = parsed_parameters.get("Sampler", sampler_name)
|
||||||
p.steps = int(parsed_parameters.get("Steps", steps))
|
p.steps = int(parsed_parameters.get("Steps", steps))
|
||||||
|
|
||||||
|
model_info = get_closet_checkpoint_match(parsed_parameters.get("Model hash", None))
|
||||||
|
if model_info is not None:
|
||||||
|
p.override_settings['sd_model_checkpoint'] = model_info.name
|
||||||
|
elif sd_model_checkpoint_override:
|
||||||
|
p.override_settings['sd_model_checkpoint'] = sd_model_checkpoint_override
|
||||||
|
else:
|
||||||
|
p.override_settings.pop("sd_model_checkpoint", None)
|
||||||
|
|
||||||
|
if output_dir:
|
||||||
|
p.outpath_samples = output_dir
|
||||||
|
p.override_settings['save_to_dirs'] = False
|
||||||
|
p.override_settings['save_images_replace_action'] = "Add number suffix"
|
||||||
|
if p.n_iter > 1 or p.batch_size > 1:
|
||||||
|
p.override_settings['samples_filename_pattern'] = f'{image_path.stem}-[generation_number]'
|
||||||
|
else:
|
||||||
|
p.override_settings['samples_filename_pattern'] = f'{image_path.stem}'
|
||||||
|
|
||||||
proc = modules.scripts.scripts_img2img.run(p, *args)
|
proc = modules.scripts.scripts_img2img.run(p, *args)
|
||||||
|
|
||||||
if proc is None:
|
if proc is None:
|
||||||
|
p.override_settings.pop('save_images_replace_action', None)
|
||||||
proc = process_images(p)
|
proc = process_images(p)
|
||||||
|
|
||||||
for n, processed_image in enumerate(proc.images):
|
if not discard_further_results and proc:
|
||||||
filename = image_path.stem
|
if batch_results:
|
||||||
infotext = proc.infotext(p, n)
|
batch_results.images.extend(proc.images)
|
||||||
relpath = os.path.dirname(os.path.relpath(image, input_dir))
|
batch_results.infotexts.extend(proc.infotexts)
|
||||||
|
else:
|
||||||
|
batch_results = proc
|
||||||
|
|
||||||
if n > 0:
|
if 0 <= shared.opts.img2img_batch_show_results_limit < len(batch_results.images):
|
||||||
filename += f"-{n}"
|
discard_further_results = True
|
||||||
|
batch_results.images = batch_results.images[:int(shared.opts.img2img_batch_show_results_limit)]
|
||||||
|
batch_results.infotexts = batch_results.infotexts[:int(shared.opts.img2img_batch_show_results_limit)]
|
||||||
|
|
||||||
if not save_normally:
|
return batch_results
|
||||||
os.makedirs(os.path.join(output_dir, relpath), exist_ok=True)
|
|
||||||
if processed_image.mode == 'RGBA':
|
|
||||||
processed_image = processed_image.convert("RGB")
|
|
||||||
save_image(processed_image, os.path.join(output_dir, relpath), None, extension=opts.samples_format, info=infotext, forced_filename=filename, save_to_dirs=False)
|
|
||||||
|
|
||||||
|
|
||||||
def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_styles, init_img, sketch, init_img_with_mask, inpaint_color_sketch, inpaint_color_sketch_orig, init_img_inpaint, init_mask_inpaint, steps: int, sampler_index: int, mask_blur: int, mask_alpha: float, inpainting_fill: int, restore_faces: bool, tiling: bool, n_iter: int, batch_size: int, cfg_scale: float, image_cfg_scale: float, denoising_strength: float, seed: int, subseed: int, subseed_strength: float, seed_resize_from_h: int, seed_resize_from_w: int, seed_enable_extras: bool, selected_scale_tab: int, height: int, width: int, scale_by: float, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, img2img_batch_inpaint_mask_dir: str, override_settings_texts, img2img_batch_use_png_info: bool, img2img_batch_png_info_props: list, img2img_batch_png_info_dir: str, request: gr.Request, *args):
|
def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_styles, init_img, sketch, init_img_with_mask, inpaint_color_sketch, inpaint_color_sketch_orig, init_img_inpaint, init_mask_inpaint, steps: int, sampler_name: str, mask_blur: int, mask_alpha: float, inpainting_fill: int, n_iter: int, batch_size: int, cfg_scale: float, image_cfg_scale: float, denoising_strength: float, selected_scale_tab: int, height: int, width: int, scale_by: float, resize_mode: int, inpaint_full_res: bool, inpaint_full_res_padding: int, inpainting_mask_invert: int, img2img_batch_input_dir: str, img2img_batch_output_dir: str, img2img_batch_inpaint_mask_dir: str, override_settings_texts, img2img_batch_use_png_info: bool, img2img_batch_png_info_props: list, img2img_batch_png_info_dir: str, request: gr.Request, *args):
|
||||||
override_settings = create_override_settings_dict(override_settings_texts)
|
override_settings = create_override_settings_dict(override_settings_texts)
|
||||||
|
|
||||||
is_batch = mode == 5
|
is_batch = mode == 5
|
||||||
|
|
||||||
if mode == 0: # img2img
|
if mode == 0: # img2img
|
||||||
image = init_img.convert("RGB")
|
image = init_img
|
||||||
mask = None
|
mask = None
|
||||||
elif mode == 1: # img2img sketch
|
elif mode == 1: # img2img sketch
|
||||||
image = sketch.convert("RGB")
|
image = sketch
|
||||||
mask = None
|
mask = None
|
||||||
elif mode == 2: # inpaint
|
elif mode == 2: # inpaint
|
||||||
image, mask = init_img_with_mask["image"], init_img_with_mask["mask"]
|
image, mask = init_img_with_mask["image"], init_img_with_mask["mask"]
|
||||||
alpha_mask = ImageOps.invert(image.split()[-1]).convert('L').point(lambda x: 255 if x > 0 else 0, mode='1')
|
mask = processing.create_binary_mask(mask)
|
||||||
mask = mask.convert('L').point(lambda x: 255 if x > 128 else 0, mode='1')
|
|
||||||
mask = ImageChops.lighter(alpha_mask, mask).convert('L')
|
|
||||||
image = image.convert("RGB")
|
|
||||||
elif mode == 3: # inpaint sketch
|
elif mode == 3: # inpaint sketch
|
||||||
image = inpaint_color_sketch
|
image = inpaint_color_sketch
|
||||||
orig = inpaint_color_sketch_orig or inpaint_color_sketch
|
orig = inpaint_color_sketch_orig or inpaint_color_sketch
|
||||||
@@ -153,7 +168,6 @@ def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_s
|
|||||||
mask = ImageEnhance.Brightness(mask).enhance(1 - mask_alpha / 100)
|
mask = ImageEnhance.Brightness(mask).enhance(1 - mask_alpha / 100)
|
||||||
blur = ImageFilter.GaussianBlur(mask_blur)
|
blur = ImageFilter.GaussianBlur(mask_blur)
|
||||||
image = Image.composite(image.filter(blur), orig, mask.filter(blur))
|
image = Image.composite(image.filter(blur), orig, mask.filter(blur))
|
||||||
image = image.convert("RGB")
|
|
||||||
elif mode == 4: # inpaint upload mask
|
elif mode == 4: # inpaint upload mask
|
||||||
image = init_img_inpaint
|
image = init_img_inpaint
|
||||||
mask = init_mask_inpaint
|
mask = init_mask_inpaint
|
||||||
@@ -180,21 +194,13 @@ def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_s
|
|||||||
prompt=prompt,
|
prompt=prompt,
|
||||||
negative_prompt=negative_prompt,
|
negative_prompt=negative_prompt,
|
||||||
styles=prompt_styles,
|
styles=prompt_styles,
|
||||||
seed=seed,
|
sampler_name=sampler_name,
|
||||||
subseed=subseed,
|
|
||||||
subseed_strength=subseed_strength,
|
|
||||||
seed_resize_from_h=seed_resize_from_h,
|
|
||||||
seed_resize_from_w=seed_resize_from_w,
|
|
||||||
seed_enable_extras=seed_enable_extras,
|
|
||||||
sampler_name=sd_samplers.samplers_for_img2img[sampler_index].name,
|
|
||||||
batch_size=batch_size,
|
batch_size=batch_size,
|
||||||
n_iter=n_iter,
|
n_iter=n_iter,
|
||||||
steps=steps,
|
steps=steps,
|
||||||
cfg_scale=cfg_scale,
|
cfg_scale=cfg_scale,
|
||||||
width=width,
|
width=width,
|
||||||
height=height,
|
height=height,
|
||||||
restore_faces=restore_faces,
|
|
||||||
tiling=tiling,
|
|
||||||
init_images=[image],
|
init_images=[image],
|
||||||
mask=mask,
|
mask=mask,
|
||||||
mask_blur=mask_blur,
|
mask_blur=mask_blur,
|
||||||
@@ -213,19 +219,16 @@ def img2img(id_task: str, mode: int, prompt: str, negative_prompt: str, prompt_s
|
|||||||
|
|
||||||
p.user = request.username
|
p.user = request.username
|
||||||
|
|
||||||
if shared.cmd_opts.enable_console_prompts:
|
if shared.opts.enable_console_prompts:
|
||||||
print(f"\nimg2img: {prompt}", file=shared.progress_print_out)
|
print(f"\nimg2img: {prompt}", file=shared.progress_print_out)
|
||||||
|
|
||||||
if mask:
|
|
||||||
p.extra_generation_params["Mask blur"] = mask_blur
|
|
||||||
|
|
||||||
with closing(p):
|
with closing(p):
|
||||||
if is_batch:
|
if is_batch:
|
||||||
assert not shared.cmd_opts.hide_ui_dir_config, "Launched with --hide-ui-dir-config, batch img2img disabled"
|
assert not shared.cmd_opts.hide_ui_dir_config, "Launched with --hide-ui-dir-config, batch img2img disabled"
|
||||||
|
processed = process_batch(p, img2img_batch_input_dir, img2img_batch_output_dir, img2img_batch_inpaint_mask_dir, args, to_scale=selected_scale_tab == 1, scale_by=scale_by, use_png_info=img2img_batch_use_png_info, png_info_props=img2img_batch_png_info_props, png_info_dir=img2img_batch_png_info_dir)
|
||||||
|
|
||||||
process_batch(p, img2img_batch_input_dir, img2img_batch_output_dir, img2img_batch_inpaint_mask_dir, args, to_scale=selected_scale_tab == 1, scale_by=scale_by, use_png_info=img2img_batch_use_png_info, png_info_props=img2img_batch_png_info_props, png_info_dir=img2img_batch_png_info_dir)
|
if processed is None:
|
||||||
|
processed = Processed(p, [], p.seed, "")
|
||||||
processed = Processed(p, [], p.seed, "")
|
|
||||||
else:
|
else:
|
||||||
processed = modules.scripts.scripts_img2img.run(p, *args)
|
processed = modules.scripts.scripts_img2img.run(p, *args)
|
||||||
if processed is None:
|
if processed is None:
|
||||||
|
|||||||
@@ -3,3 +3,14 @@ import sys
|
|||||||
# this will break any attempt to import xformers which will prevent stability diffusion repo from trying to use it
|
# this will break any attempt to import xformers which will prevent stability diffusion repo from trying to use it
|
||||||
if "--xformers" not in "".join(sys.argv):
|
if "--xformers" not in "".join(sys.argv):
|
||||||
sys.modules["xformers"] = None
|
sys.modules["xformers"] = None
|
||||||
|
|
||||||
|
# Hack to fix a changed import in torchvision 0.17+, which otherwise breaks
|
||||||
|
# basicsr; see https://github.com/AUTOMATIC1111/stable-diffusion-webui/issues/13985
|
||||||
|
try:
|
||||||
|
import torchvision.transforms.functional_tensor # noqa: F401
|
||||||
|
except ImportError:
|
||||||
|
try:
|
||||||
|
import torchvision.transforms.functional as functional
|
||||||
|
sys.modules["torchvision.transforms.functional_tensor"] = functional
|
||||||
|
except ImportError:
|
||||||
|
pass # shrug...
|
||||||
|
|||||||
@@ -1,22 +1,51 @@
|
|||||||
|
from __future__ import annotations
|
||||||
import base64
|
import base64
|
||||||
import io
|
import io
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
from modules.paths import data_path
|
from modules.paths import data_path
|
||||||
from modules import shared, ui_tempdir, script_callbacks
|
from modules import shared, ui_tempdir, script_callbacks, processing, infotext_versions
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
|
||||||
re_param_code = r'\s*([\w ]+):\s*("(?:\\"[^,]|\\"|\\|[^\"])+"|[^,]*)(?:,|$)'
|
sys.modules['modules.generation_parameters_copypaste'] = sys.modules[__name__] # alias for old name
|
||||||
|
|
||||||
|
re_param_code = r'\s*(\w[\w \-/]+):\s*("(?:\\.|[^\\"])+"|[^,]*)(?:,|$)'
|
||||||
re_param = re.compile(re_param_code)
|
re_param = re.compile(re_param_code)
|
||||||
re_imagesize = re.compile(r"^(\d+)x(\d+)$")
|
re_imagesize = re.compile(r"^(\d+)x(\d+)$")
|
||||||
re_hypernet_hash = re.compile("\(([0-9a-f]+)\)$")
|
re_hypernet_hash = re.compile("\(([0-9a-f]+)\)$")
|
||||||
type_of_gr_update = type(gr.update())
|
type_of_gr_update = type(gr.update())
|
||||||
|
quote_swap = str.maketrans('\'"', '"\'')
|
||||||
|
info_json_keys = set()
|
||||||
|
|
||||||
paste_fields = {}
|
|
||||||
registered_param_bindings = []
|
def info_json_dumps(data):
|
||||||
|
"""encode data into json string, but swap single and double quotes to reduce escaping issues"""
|
||||||
|
return json.dumps(data, ensure_ascii=False, separators=(',', ':')).translate(quote_swap)
|
||||||
|
|
||||||
|
|
||||||
|
def info_json_loads(info_json):
|
||||||
|
"""decode json string into info data, but swap single and double quotes to reduce escaping issues"""
|
||||||
|
return json.loads(info_json.translate(quote_swap))
|
||||||
|
|
||||||
|
|
||||||
|
def build_infotext(info: dict):
|
||||||
|
for info_json_key in info_json_keys:
|
||||||
|
if info_json_key in info:
|
||||||
|
info[info_json_key] = info_json_dumps(info[info_json_key])
|
||||||
|
return ", ".join([k if k == v else f'{k}: {quote(v)}' for k, v in info.items() if v is not None])
|
||||||
|
|
||||||
|
|
||||||
|
def register_info_json(key):
|
||||||
|
"""register an infotext key as infojson
|
||||||
|
after a key is registered, a json compatible data structure like dict or list can be used as a value in
|
||||||
|
generation_parameters and extra_generation_parameters
|
||||||
|
"""
|
||||||
|
global info_json_keys
|
||||||
|
info_json_keys.add(key)
|
||||||
|
|
||||||
|
|
||||||
class ParamBinding:
|
class ParamBinding:
|
||||||
@@ -30,8 +59,26 @@ class ParamBinding:
|
|||||||
self.paste_field_names = paste_field_names or []
|
self.paste_field_names = paste_field_names or []
|
||||||
|
|
||||||
|
|
||||||
|
class PasteField(tuple):
|
||||||
|
def __new__(cls, component, target, *, api=None):
|
||||||
|
return super().__new__(cls, (component, target))
|
||||||
|
|
||||||
|
def __init__(self, component, target, *, api=None):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.api = api
|
||||||
|
self.component = component
|
||||||
|
self.label = target if isinstance(target, str) else None
|
||||||
|
self.function = target if callable(target) else None
|
||||||
|
|
||||||
|
|
||||||
|
paste_fields: dict[str, dict] = {}
|
||||||
|
registered_param_bindings: list[ParamBinding] = []
|
||||||
|
|
||||||
|
|
||||||
def reset():
|
def reset():
|
||||||
paste_fields.clear()
|
paste_fields.clear()
|
||||||
|
registered_param_bindings.clear()
|
||||||
|
|
||||||
|
|
||||||
def quote(text):
|
def quote(text):
|
||||||
@@ -81,6 +128,12 @@ def image_from_url_text(filedata):
|
|||||||
|
|
||||||
|
|
||||||
def add_paste_fields(tabname, init_img, fields, override_settings_component=None):
|
def add_paste_fields(tabname, init_img, fields, override_settings_component=None):
|
||||||
|
|
||||||
|
if fields:
|
||||||
|
for i in range(len(fields)):
|
||||||
|
if not isinstance(fields[i], PasteField):
|
||||||
|
fields[i] = PasteField(*fields[i])
|
||||||
|
|
||||||
paste_fields[tabname] = {"init_img": init_img, "fields": fields, "override_settings_component": override_settings_component}
|
paste_fields[tabname] = {"init_img": init_img, "fields": fields, "override_settings_component": override_settings_component}
|
||||||
|
|
||||||
# backwards compatibility for existing extensions
|
# backwards compatibility for existing extensions
|
||||||
@@ -112,7 +165,6 @@ def register_paste_params_button(binding: ParamBinding):
|
|||||||
|
|
||||||
|
|
||||||
def connect_paste_params_buttons():
|
def connect_paste_params_buttons():
|
||||||
binding: ParamBinding
|
|
||||||
for binding in registered_param_bindings:
|
for binding in registered_param_bindings:
|
||||||
destination_image_component = paste_fields[binding.tabname]["init_img"]
|
destination_image_component = paste_fields[binding.tabname]["init_img"]
|
||||||
fields = paste_fields[binding.tabname]["fields"]
|
fields = paste_fields[binding.tabname]["fields"]
|
||||||
@@ -198,7 +250,6 @@ def restore_old_hires_fix_params(res):
|
|||||||
height = int(res.get("Size-2", 512))
|
height = int(res.get("Size-2", 512))
|
||||||
|
|
||||||
if firstpass_width == 0 or firstpass_height == 0:
|
if firstpass_width == 0 or firstpass_height == 0:
|
||||||
from modules import processing
|
|
||||||
firstpass_width, firstpass_height = processing.old_hires_fix_first_pass_dimensions(width, height)
|
firstpass_width, firstpass_height = processing.old_hires_fix_first_pass_dimensions(width, height)
|
||||||
|
|
||||||
res['Size-1'] = firstpass_width
|
res['Size-1'] = firstpass_width
|
||||||
@@ -207,7 +258,7 @@ def restore_old_hires_fix_params(res):
|
|||||||
res['Hires resize-2'] = height
|
res['Hires resize-2'] = height
|
||||||
|
|
||||||
|
|
||||||
def parse_generation_parameters(x: str):
|
def parse_generation_parameters(x: str, skip_fields: list[str] | None = None):
|
||||||
"""parses generation parameters string, the one you see in text field under the picture in UI:
|
"""parses generation parameters string, the one you see in text field under the picture in UI:
|
||||||
```
|
```
|
||||||
girl with an artist's beret, determined, blue eyes, desert scene, computer monitors, heavy makeup, by Alphonse Mucha and Charlie Bowater, ((eyeshadow)), (coquettish), detailed, intricate
|
girl with an artist's beret, determined, blue eyes, desert scene, computer monitors, heavy makeup, by Alphonse Mucha and Charlie Bowater, ((eyeshadow)), (coquettish), detailed, intricate
|
||||||
@@ -217,6 +268,8 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model
|
|||||||
|
|
||||||
returns a dict with field values
|
returns a dict with field values
|
||||||
"""
|
"""
|
||||||
|
if skip_fields is None:
|
||||||
|
skip_fields = shared.opts.infotext_skip_pasting
|
||||||
|
|
||||||
res = {}
|
res = {}
|
||||||
|
|
||||||
@@ -280,12 +333,27 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model
|
|||||||
if "Hires sampler" not in res:
|
if "Hires sampler" not in res:
|
||||||
res["Hires sampler"] = "Use same sampler"
|
res["Hires sampler"] = "Use same sampler"
|
||||||
|
|
||||||
|
if "Hires checkpoint" not in res:
|
||||||
|
res["Hires checkpoint"] = "Use same checkpoint"
|
||||||
|
|
||||||
if "Hires prompt" not in res:
|
if "Hires prompt" not in res:
|
||||||
res["Hires prompt"] = ""
|
res["Hires prompt"] = ""
|
||||||
|
|
||||||
if "Hires negative prompt" not in res:
|
if "Hires negative prompt" not in res:
|
||||||
res["Hires negative prompt"] = ""
|
res["Hires negative prompt"] = ""
|
||||||
|
|
||||||
|
if "Mask mode" not in res:
|
||||||
|
res["Mask mode"] = "Inpaint masked"
|
||||||
|
|
||||||
|
if "Masked content" not in res:
|
||||||
|
res["Masked content"] = 'original'
|
||||||
|
|
||||||
|
if "Inpaint area" not in res:
|
||||||
|
res["Inpaint area"] = "Whole picture"
|
||||||
|
|
||||||
|
if "Masked area padding" not in res:
|
||||||
|
res["Masked area padding"] = 32
|
||||||
|
|
||||||
restore_old_hires_fix_params(res)
|
restore_old_hires_fix_params(res)
|
||||||
|
|
||||||
# Missing RNG means the default was set, which is GPU RNG
|
# Missing RNG means the default was set, which is GPU RNG
|
||||||
@@ -304,32 +372,46 @@ Steps: 20, Sampler: Euler a, CFG scale: 7, Seed: 965400086, Size: 512x512, Model
|
|||||||
if "Schedule rho" not in res:
|
if "Schedule rho" not in res:
|
||||||
res["Schedule rho"] = 0
|
res["Schedule rho"] = 0
|
||||||
|
|
||||||
|
if "VAE Encoder" not in res:
|
||||||
|
res["VAE Encoder"] = "Full"
|
||||||
|
|
||||||
|
if "VAE Decoder" not in res:
|
||||||
|
res["VAE Decoder"] = "Full"
|
||||||
|
|
||||||
|
if "FP8 weight" not in res:
|
||||||
|
res["FP8 weight"] = "Disable"
|
||||||
|
|
||||||
|
if "Cache FP16 weight for LoRA" not in res and res["FP8 weight"] != "Disable":
|
||||||
|
res["Cache FP16 weight for LoRA"] = False
|
||||||
|
|
||||||
|
for key in info_json_keys:
|
||||||
|
if key in res:
|
||||||
|
try:
|
||||||
|
res[key] = info_json_loads(res[key])
|
||||||
|
except Exception:
|
||||||
|
print(f'Error parsing "{key}: {res[key]}"')
|
||||||
|
|
||||||
|
infotext_versions.backcompat(res)
|
||||||
|
|
||||||
|
for key in skip_fields:
|
||||||
|
res.pop(key, None)
|
||||||
|
|
||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
infotext_to_setting_name_mapping = [
|
infotext_to_setting_name_mapping = [
|
||||||
('Clip skip', 'CLIP_stop_at_last_layers', ),
|
|
||||||
|
]
|
||||||
|
"""Mapping of infotext labels to setting names. Only left for backwards compatibility - use OptionInfo(..., infotext='...') instead.
|
||||||
|
Example content:
|
||||||
|
|
||||||
|
infotext_to_setting_name_mapping = [
|
||||||
('Conditional mask weight', 'inpainting_mask_weight'),
|
('Conditional mask weight', 'inpainting_mask_weight'),
|
||||||
('Model hash', 'sd_model_checkpoint'),
|
('Model hash', 'sd_model_checkpoint'),
|
||||||
('ENSD', 'eta_noise_seed_delta'),
|
('ENSD', 'eta_noise_seed_delta'),
|
||||||
('Schedule type', 'k_sched_type'),
|
('Schedule type', 'k_sched_type'),
|
||||||
('Schedule max sigma', 'sigma_max'),
|
|
||||||
('Schedule min sigma', 'sigma_min'),
|
|
||||||
('Schedule rho', 'rho'),
|
|
||||||
('Noise multiplier', 'initial_noise_multiplier'),
|
|
||||||
('Eta', 'eta_ancestral'),
|
|
||||||
('Eta DDIM', 'eta_ddim'),
|
|
||||||
('Discard penultimate sigma', 'always_discard_next_to_last_sigma'),
|
|
||||||
('UniPC variant', 'uni_pc_variant'),
|
|
||||||
('UniPC skip type', 'uni_pc_skip_type'),
|
|
||||||
('UniPC order', 'uni_pc_order'),
|
|
||||||
('UniPC lower order final', 'uni_pc_lower_order_final'),
|
|
||||||
('Token merging ratio', 'token_merging_ratio'),
|
|
||||||
('Token merging ratio hr', 'token_merging_ratio_hr'),
|
|
||||||
('RNG', 'randn_source'),
|
|
||||||
('NGMS', 's_min_uncond'),
|
|
||||||
('Pad conds', 'pad_cond_uncond'),
|
|
||||||
]
|
]
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
def create_override_settings_dict(text_pairs):
|
def create_override_settings_dict(text_pairs):
|
||||||
@@ -350,7 +432,8 @@ def create_override_settings_dict(text_pairs):
|
|||||||
|
|
||||||
params[k] = v.strip()
|
params[k] = v.strip()
|
||||||
|
|
||||||
for param_name, setting_name in infotext_to_setting_name_mapping:
|
mapping = [(info.infotext, k) for k, info in shared.opts.data_labels.items() if info.infotext]
|
||||||
|
for param_name, setting_name in mapping + infotext_to_setting_name_mapping:
|
||||||
value = params.get(param_name, None)
|
value = params.get(param_name, None)
|
||||||
|
|
||||||
if value is None:
|
if value is None:
|
||||||
@@ -361,13 +444,57 @@ def create_override_settings_dict(text_pairs):
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def get_override_settings(params, *, skip_fields=None):
|
||||||
|
"""Returns a list of settings overrides from the infotext parameters dictionary.
|
||||||
|
|
||||||
|
This function checks the `params` dictionary for any keys that correspond to settings in `shared.opts` and returns
|
||||||
|
a list of tuples containing the parameter name, setting name, and new value cast to correct type.
|
||||||
|
|
||||||
|
It checks for conditions before adding an override:
|
||||||
|
- ignores settings that match the current value
|
||||||
|
- ignores parameter keys present in skip_fields argument.
|
||||||
|
|
||||||
|
Example input:
|
||||||
|
{"Clip skip": "2"}
|
||||||
|
|
||||||
|
Example output:
|
||||||
|
[("Clip skip", "CLIP_stop_at_last_layers", 2)]
|
||||||
|
"""
|
||||||
|
|
||||||
|
res = []
|
||||||
|
|
||||||
|
mapping = [(info.infotext, k) for k, info in shared.opts.data_labels.items() if info.infotext]
|
||||||
|
for param_name, setting_name in mapping + infotext_to_setting_name_mapping:
|
||||||
|
if param_name in (skip_fields or {}):
|
||||||
|
continue
|
||||||
|
|
||||||
|
v = params.get(param_name, None)
|
||||||
|
if v is None:
|
||||||
|
continue
|
||||||
|
|
||||||
|
if setting_name == "sd_model_checkpoint" and shared.opts.disable_weights_auto_swap:
|
||||||
|
continue
|
||||||
|
|
||||||
|
v = shared.opts.cast_value(setting_name, v)
|
||||||
|
current_value = getattr(shared.opts, setting_name, None)
|
||||||
|
|
||||||
|
if v == current_value:
|
||||||
|
continue
|
||||||
|
|
||||||
|
res.append((param_name, setting_name, v))
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
def connect_paste(button, paste_fields, input_comp, override_settings_component, tabname):
|
def connect_paste(button, paste_fields, input_comp, override_settings_component, tabname):
|
||||||
def paste_func(prompt):
|
def paste_func(prompt):
|
||||||
if not prompt and not shared.cmd_opts.hide_ui_dir_config:
|
if not prompt and not shared.cmd_opts.hide_ui_dir_config:
|
||||||
filename = os.path.join(data_path, "params.txt")
|
filename = os.path.join(data_path, "params.txt")
|
||||||
if os.path.exists(filename):
|
try:
|
||||||
with open(filename, "r", encoding="utf8") as file:
|
with open(filename, "r", encoding="utf8") as file:
|
||||||
prompt = file.read()
|
prompt = file.read()
|
||||||
|
except OSError:
|
||||||
|
pass
|
||||||
|
|
||||||
params = parse_generation_parameters(prompt)
|
params = parse_generation_parameters(prompt)
|
||||||
script_callbacks.infotext_pasted_callback(prompt, params)
|
script_callbacks.infotext_pasted_callback(prompt, params)
|
||||||
@@ -389,6 +516,8 @@ def connect_paste(button, paste_fields, input_comp, override_settings_component,
|
|||||||
|
|
||||||
if valtype == bool and v == "False":
|
if valtype == bool and v == "False":
|
||||||
val = False
|
val = False
|
||||||
|
elif valtype == int:
|
||||||
|
val = float(v)
|
||||||
else:
|
else:
|
||||||
val = valtype(v)
|
val = valtype(v)
|
||||||
|
|
||||||
@@ -399,26 +528,12 @@ def connect_paste(button, paste_fields, input_comp, override_settings_component,
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
if override_settings_component is not None:
|
if override_settings_component is not None:
|
||||||
|
already_handled_fields = {key: 1 for _, key in paste_fields}
|
||||||
|
|
||||||
def paste_settings(params):
|
def paste_settings(params):
|
||||||
vals = {}
|
vals = get_override_settings(params, skip_fields=already_handled_fields)
|
||||||
|
|
||||||
for param_name, setting_name in infotext_to_setting_name_mapping:
|
vals_pairs = [f"{infotext_text}: {value}" for infotext_text, setting_name, value in vals]
|
||||||
v = params.get(param_name, None)
|
|
||||||
if v is None:
|
|
||||||
continue
|
|
||||||
|
|
||||||
if setting_name == "sd_model_checkpoint" and shared.opts.disable_weights_auto_swap:
|
|
||||||
continue
|
|
||||||
|
|
||||||
v = shared.opts.cast_value(setting_name, v)
|
|
||||||
current_value = getattr(shared.opts, setting_name, None)
|
|
||||||
|
|
||||||
if v == current_value:
|
|
||||||
continue
|
|
||||||
|
|
||||||
vals[param_name] = v
|
|
||||||
|
|
||||||
vals_pairs = [f"{k}: {v}" for k, v in vals.items()]
|
|
||||||
|
|
||||||
return gr.Dropdown.update(value=vals_pairs, choices=vals_pairs, visible=bool(vals_pairs))
|
return gr.Dropdown.update(value=vals_pairs, choices=vals_pairs, visible=bool(vals_pairs))
|
||||||
|
|
||||||
@@ -437,3 +552,4 @@ def connect_paste(button, paste_fields, input_comp, override_settings_component,
|
|||||||
outputs=[],
|
outputs=[],
|
||||||
show_progress=False,
|
show_progress=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
from modules import shared
|
||||||
|
from packaging import version
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
v160 = version.parse("1.6.0")
|
||||||
|
v170_tsnr = version.parse("v1.7.0-225")
|
||||||
|
|
||||||
|
|
||||||
|
def parse_version(text):
|
||||||
|
if text is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
m = re.match(r'([^-]+-[^-]+)-.*', text)
|
||||||
|
if m:
|
||||||
|
text = m.group(1)
|
||||||
|
|
||||||
|
try:
|
||||||
|
return version.parse(text)
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
def backcompat(d):
|
||||||
|
"""Checks infotext Version field, and enables backwards compatibility options according to it."""
|
||||||
|
|
||||||
|
if not shared.opts.auto_backcompat:
|
||||||
|
return
|
||||||
|
|
||||||
|
ver = parse_version(d.get("Version"))
|
||||||
|
if ver is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
if ver < v160:
|
||||||
|
d["Old prompt editing timelines"] = True
|
||||||
|
|
||||||
|
if ver < v170_tsnr:
|
||||||
|
d["Downcast alphas_cumprod"] = True
|
||||||
|
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
import importlib
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import warnings
|
||||||
|
from threading import Thread
|
||||||
|
|
||||||
|
from modules.timer import startup_timer
|
||||||
|
|
||||||
|
|
||||||
|
def imports():
|
||||||
|
logging.getLogger("torch.distributed.nn").setLevel(logging.ERROR) # sshh...
|
||||||
|
logging.getLogger("xformers").addFilter(lambda record: 'A matching Triton is not available' not in record.getMessage())
|
||||||
|
|
||||||
|
import torch # noqa: F401
|
||||||
|
startup_timer.record("import torch")
|
||||||
|
import pytorch_lightning # noqa: F401
|
||||||
|
startup_timer.record("import torch")
|
||||||
|
warnings.filterwarnings(action="ignore", category=DeprecationWarning, module="pytorch_lightning")
|
||||||
|
warnings.filterwarnings(action="ignore", category=UserWarning, module="torchvision")
|
||||||
|
|
||||||
|
os.environ.setdefault('GRADIO_ANALYTICS_ENABLED', 'False')
|
||||||
|
import gradio # noqa: F401
|
||||||
|
startup_timer.record("import gradio")
|
||||||
|
|
||||||
|
from modules import paths, timer, import_hook, errors # noqa: F401
|
||||||
|
startup_timer.record("setup paths")
|
||||||
|
|
||||||
|
import ldm.modules.encoders.modules # noqa: F401
|
||||||
|
startup_timer.record("import ldm")
|
||||||
|
|
||||||
|
import sgm.modules.encoders.modules # noqa: F401
|
||||||
|
startup_timer.record("import sgm")
|
||||||
|
|
||||||
|
from modules import shared_init
|
||||||
|
shared_init.initialize()
|
||||||
|
startup_timer.record("initialize shared")
|
||||||
|
|
||||||
|
from modules import processing, gradio_extensons, ui # noqa: F401
|
||||||
|
startup_timer.record("other imports")
|
||||||
|
|
||||||
|
|
||||||
|
def check_versions():
|
||||||
|
from modules.shared_cmd_options import cmd_opts
|
||||||
|
|
||||||
|
if not cmd_opts.skip_version_check:
|
||||||
|
from modules import errors
|
||||||
|
errors.check_versions()
|
||||||
|
|
||||||
|
|
||||||
|
def initialize():
|
||||||
|
from modules import initialize_util
|
||||||
|
initialize_util.fix_torch_version()
|
||||||
|
initialize_util.fix_asyncio_event_loop_policy()
|
||||||
|
initialize_util.validate_tls_options()
|
||||||
|
initialize_util.configure_sigint_handler()
|
||||||
|
initialize_util.configure_opts_onchange()
|
||||||
|
|
||||||
|
from modules import sd_models
|
||||||
|
sd_models.setup_model()
|
||||||
|
startup_timer.record("setup SD model")
|
||||||
|
|
||||||
|
from modules.shared_cmd_options import cmd_opts
|
||||||
|
|
||||||
|
from modules import codeformer_model
|
||||||
|
warnings.filterwarnings(action="ignore", category=UserWarning, module="torchvision.transforms.functional_tensor")
|
||||||
|
codeformer_model.setup_model(cmd_opts.codeformer_models_path)
|
||||||
|
startup_timer.record("setup codeformer")
|
||||||
|
|
||||||
|
from modules import gfpgan_model
|
||||||
|
gfpgan_model.setup_model(cmd_opts.gfpgan_models_path)
|
||||||
|
startup_timer.record("setup gfpgan")
|
||||||
|
|
||||||
|
initialize_rest(reload_script_modules=False)
|
||||||
|
|
||||||
|
|
||||||
|
def initialize_rest(*, reload_script_modules=False):
|
||||||
|
"""
|
||||||
|
Called both from initialize() and when reloading the webui.
|
||||||
|
"""
|
||||||
|
from modules.shared_cmd_options import cmd_opts
|
||||||
|
|
||||||
|
from modules import sd_samplers
|
||||||
|
sd_samplers.set_samplers()
|
||||||
|
startup_timer.record("set samplers")
|
||||||
|
|
||||||
|
from modules import extensions
|
||||||
|
extensions.list_extensions()
|
||||||
|
startup_timer.record("list extensions")
|
||||||
|
|
||||||
|
from modules import initialize_util
|
||||||
|
initialize_util.restore_config_state_file()
|
||||||
|
startup_timer.record("restore config state file")
|
||||||
|
|
||||||
|
from modules import shared, upscaler, scripts
|
||||||
|
if cmd_opts.ui_debug_mode:
|
||||||
|
shared.sd_upscalers = upscaler.UpscalerLanczos().scalers
|
||||||
|
scripts.load_scripts()
|
||||||
|
return
|
||||||
|
|
||||||
|
from modules import sd_models
|
||||||
|
sd_models.list_models()
|
||||||
|
startup_timer.record("list SD models")
|
||||||
|
|
||||||
|
from modules import localization
|
||||||
|
localization.list_localizations(cmd_opts.localizations_dir)
|
||||||
|
startup_timer.record("list localizations")
|
||||||
|
|
||||||
|
with startup_timer.subcategory("load scripts"):
|
||||||
|
scripts.load_scripts()
|
||||||
|
|
||||||
|
if reload_script_modules:
|
||||||
|
for module in [module for name, module in sys.modules.items() if name.startswith("modules.ui")]:
|
||||||
|
importlib.reload(module)
|
||||||
|
startup_timer.record("reload script modules")
|
||||||
|
|
||||||
|
from modules import modelloader
|
||||||
|
modelloader.load_upscalers()
|
||||||
|
startup_timer.record("load upscalers")
|
||||||
|
|
||||||
|
from modules import sd_vae
|
||||||
|
sd_vae.refresh_vae_list()
|
||||||
|
startup_timer.record("refresh VAE")
|
||||||
|
|
||||||
|
from modules import textual_inversion
|
||||||
|
textual_inversion.textual_inversion.list_textual_inversion_templates()
|
||||||
|
startup_timer.record("refresh textual inversion templates")
|
||||||
|
|
||||||
|
from modules import script_callbacks, sd_hijack_optimizations, sd_hijack
|
||||||
|
script_callbacks.on_list_optimizers(sd_hijack_optimizations.list_optimizers)
|
||||||
|
sd_hijack.list_optimizers()
|
||||||
|
startup_timer.record("scripts list_optimizers")
|
||||||
|
|
||||||
|
from modules import sd_unet
|
||||||
|
sd_unet.list_unets()
|
||||||
|
startup_timer.record("scripts list_unets")
|
||||||
|
|
||||||
|
def load_model():
|
||||||
|
"""
|
||||||
|
Accesses shared.sd_model property to load model.
|
||||||
|
After it's available, if it has been loaded before this access by some extension,
|
||||||
|
its optimization may be None because the list of optimizaers has neet been filled
|
||||||
|
by that time, so we apply optimization again.
|
||||||
|
"""
|
||||||
|
|
||||||
|
shared.sd_model # noqa: B018
|
||||||
|
|
||||||
|
if sd_hijack.current_optimizer is None:
|
||||||
|
sd_hijack.apply_optimizations()
|
||||||
|
|
||||||
|
from modules import devices
|
||||||
|
devices.first_time_calculation()
|
||||||
|
if not shared.cmd_opts.skip_load_model_at_start:
|
||||||
|
Thread(target=load_model).start()
|
||||||
|
|
||||||
|
from modules import shared_items
|
||||||
|
shared_items.reload_hypernetworks()
|
||||||
|
startup_timer.record("reload hypernetworks")
|
||||||
|
|
||||||
|
from modules import ui_extra_networks
|
||||||
|
ui_extra_networks.initialize()
|
||||||
|
ui_extra_networks.register_default_pages()
|
||||||
|
|
||||||
|
from modules import extra_networks
|
||||||
|
extra_networks.initialize()
|
||||||
|
extra_networks.register_default_extra_networks()
|
||||||
|
startup_timer.record("initialize extra networks")
|
||||||
@@ -0,0 +1,208 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
import signal
|
||||||
|
import sys
|
||||||
|
import re
|
||||||
|
|
||||||
|
from modules.timer import startup_timer
|
||||||
|
|
||||||
|
|
||||||
|
def gradio_server_name():
|
||||||
|
from modules.shared_cmd_options import cmd_opts
|
||||||
|
|
||||||
|
if cmd_opts.server_name:
|
||||||
|
return cmd_opts.server_name
|
||||||
|
else:
|
||||||
|
return "0.0.0.0" if cmd_opts.listen else None
|
||||||
|
|
||||||
|
|
||||||
|
def fix_torch_version():
|
||||||
|
import torch
|
||||||
|
|
||||||
|
# Truncate version number of nightly/local build of PyTorch to not cause exceptions with CodeFormer or Safetensors
|
||||||
|
if ".dev" in torch.__version__ or "+git" in torch.__version__:
|
||||||
|
torch.__long_version__ = torch.__version__
|
||||||
|
torch.__version__ = re.search(r'[\d.]+[\d]', torch.__version__).group(0)
|
||||||
|
|
||||||
|
|
||||||
|
def fix_asyncio_event_loop_policy():
|
||||||
|
"""
|
||||||
|
The default `asyncio` event loop policy only automatically creates
|
||||||
|
event loops in the main threads. Other threads must create event
|
||||||
|
loops explicitly or `asyncio.get_event_loop` (and therefore
|
||||||
|
`.IOLoop.current`) will fail. Installing this policy allows event
|
||||||
|
loops to be created automatically on any thread, matching the
|
||||||
|
behavior of Tornado versions prior to 5.0 (or 5.0 on Python 2).
|
||||||
|
"""
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
if sys.platform == "win32" and hasattr(asyncio, "WindowsSelectorEventLoopPolicy"):
|
||||||
|
# "Any thread" and "selector" should be orthogonal, but there's not a clean
|
||||||
|
# interface for composing policies so pick the right base.
|
||||||
|
_BasePolicy = asyncio.WindowsSelectorEventLoopPolicy # type: ignore
|
||||||
|
else:
|
||||||
|
_BasePolicy = asyncio.DefaultEventLoopPolicy
|
||||||
|
|
||||||
|
class AnyThreadEventLoopPolicy(_BasePolicy): # type: ignore
|
||||||
|
"""Event loop policy that allows loop creation on any thread.
|
||||||
|
Usage::
|
||||||
|
|
||||||
|
asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
|
||||||
|
"""
|
||||||
|
|
||||||
|
def get_event_loop(self) -> asyncio.AbstractEventLoop:
|
||||||
|
try:
|
||||||
|
return super().get_event_loop()
|
||||||
|
except (RuntimeError, AssertionError):
|
||||||
|
# This was an AssertionError in python 3.4.2 (which ships with debian jessie)
|
||||||
|
# and changed to a RuntimeError in 3.4.3.
|
||||||
|
# "There is no current event loop in thread %r"
|
||||||
|
loop = self.new_event_loop()
|
||||||
|
self.set_event_loop(loop)
|
||||||
|
return loop
|
||||||
|
|
||||||
|
asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
|
||||||
|
|
||||||
|
|
||||||
|
def restore_config_state_file():
|
||||||
|
from modules import shared, config_states
|
||||||
|
|
||||||
|
config_state_file = shared.opts.restore_config_state_file
|
||||||
|
if config_state_file == "":
|
||||||
|
return
|
||||||
|
|
||||||
|
shared.opts.restore_config_state_file = ""
|
||||||
|
shared.opts.save(shared.config_filename)
|
||||||
|
|
||||||
|
if os.path.isfile(config_state_file):
|
||||||
|
print(f"*** About to restore extension state from file: {config_state_file}")
|
||||||
|
with open(config_state_file, "r", encoding="utf-8") as f:
|
||||||
|
config_state = json.load(f)
|
||||||
|
config_states.restore_extension_config(config_state)
|
||||||
|
startup_timer.record("restore extension config")
|
||||||
|
elif config_state_file:
|
||||||
|
print(f"!!! Config state backup not found: {config_state_file}")
|
||||||
|
|
||||||
|
|
||||||
|
def validate_tls_options():
|
||||||
|
from modules.shared_cmd_options import cmd_opts
|
||||||
|
|
||||||
|
if not (cmd_opts.tls_keyfile and cmd_opts.tls_certfile):
|
||||||
|
return
|
||||||
|
|
||||||
|
try:
|
||||||
|
if not os.path.exists(cmd_opts.tls_keyfile):
|
||||||
|
print("Invalid path to TLS keyfile given")
|
||||||
|
if not os.path.exists(cmd_opts.tls_certfile):
|
||||||
|
print(f"Invalid path to TLS certfile: '{cmd_opts.tls_certfile}'")
|
||||||
|
except TypeError:
|
||||||
|
cmd_opts.tls_keyfile = cmd_opts.tls_certfile = None
|
||||||
|
print("TLS setup invalid, running webui without TLS")
|
||||||
|
else:
|
||||||
|
print("Running with TLS")
|
||||||
|
startup_timer.record("TLS")
|
||||||
|
|
||||||
|
|
||||||
|
def get_gradio_auth_creds():
|
||||||
|
"""
|
||||||
|
Convert the gradio_auth and gradio_auth_path commandline arguments into
|
||||||
|
an iterable of (username, password) tuples.
|
||||||
|
"""
|
||||||
|
from modules.shared_cmd_options import cmd_opts
|
||||||
|
|
||||||
|
def process_credential_line(s):
|
||||||
|
s = s.strip()
|
||||||
|
if not s:
|
||||||
|
return None
|
||||||
|
return tuple(s.split(':', 1))
|
||||||
|
|
||||||
|
if cmd_opts.gradio_auth:
|
||||||
|
for cred in cmd_opts.gradio_auth.split(','):
|
||||||
|
cred = process_credential_line(cred)
|
||||||
|
if cred:
|
||||||
|
yield cred
|
||||||
|
|
||||||
|
if cmd_opts.gradio_auth_path:
|
||||||
|
with open(cmd_opts.gradio_auth_path, 'r', encoding="utf8") as file:
|
||||||
|
for line in file.readlines():
|
||||||
|
for cred in line.strip().split(','):
|
||||||
|
cred = process_credential_line(cred)
|
||||||
|
if cred:
|
||||||
|
yield cred
|
||||||
|
|
||||||
|
|
||||||
|
def dumpstacks():
|
||||||
|
import threading
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
id2name = {th.ident: th.name for th in threading.enumerate()}
|
||||||
|
code = []
|
||||||
|
for threadId, stack in sys._current_frames().items():
|
||||||
|
code.append(f"\n# Thread: {id2name.get(threadId, '')}({threadId})")
|
||||||
|
for filename, lineno, name, line in traceback.extract_stack(stack):
|
||||||
|
code.append(f"""File: "{filename}", line {lineno}, in {name}""")
|
||||||
|
if line:
|
||||||
|
code.append(" " + line.strip())
|
||||||
|
|
||||||
|
print("\n".join(code))
|
||||||
|
|
||||||
|
|
||||||
|
def configure_sigint_handler():
|
||||||
|
# make the program just exit at ctrl+c without waiting for anything
|
||||||
|
|
||||||
|
from modules import shared
|
||||||
|
|
||||||
|
def sigint_handler(sig, frame):
|
||||||
|
print(f'Interrupted with signal {sig} in {frame}')
|
||||||
|
|
||||||
|
if shared.opts.dump_stacks_on_signal:
|
||||||
|
dumpstacks()
|
||||||
|
|
||||||
|
os._exit(0)
|
||||||
|
|
||||||
|
if not os.environ.get("COVERAGE_RUN"):
|
||||||
|
# Don't install the immediate-quit handler when running under coverage,
|
||||||
|
# as then the coverage report won't be generated.
|
||||||
|
signal.signal(signal.SIGINT, sigint_handler)
|
||||||
|
|
||||||
|
|
||||||
|
def configure_opts_onchange():
|
||||||
|
from modules import shared, sd_models, sd_vae, ui_tempdir, sd_hijack
|
||||||
|
from modules.call_queue import wrap_queued_call
|
||||||
|
|
||||||
|
shared.opts.onchange("sd_model_checkpoint", wrap_queued_call(lambda: sd_models.reload_model_weights()), call=False)
|
||||||
|
shared.opts.onchange("sd_vae", wrap_queued_call(lambda: sd_vae.reload_vae_weights()), call=False)
|
||||||
|
shared.opts.onchange("sd_vae_overrides_per_model_preferences", wrap_queued_call(lambda: sd_vae.reload_vae_weights()), call=False)
|
||||||
|
shared.opts.onchange("temp_dir", ui_tempdir.on_tmpdir_changed)
|
||||||
|
shared.opts.onchange("gradio_theme", shared.reload_gradio_theme)
|
||||||
|
shared.opts.onchange("cross_attention_optimization", wrap_queued_call(lambda: sd_hijack.model_hijack.redo_hijack(shared.sd_model)), call=False)
|
||||||
|
shared.opts.onchange("fp8_storage", wrap_queued_call(lambda: sd_models.reload_model_weights()), call=False)
|
||||||
|
shared.opts.onchange("cache_fp16_weight", wrap_queued_call(lambda: sd_models.reload_model_weights(forced_reload=True)), call=False)
|
||||||
|
startup_timer.record("opts onchange")
|
||||||
|
|
||||||
|
|
||||||
|
def setup_middleware(app):
|
||||||
|
from starlette.middleware.gzip import GZipMiddleware
|
||||||
|
|
||||||
|
app.middleware_stack = None # reset current middleware to allow modifying user provided list
|
||||||
|
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
||||||
|
configure_cors_middleware(app)
|
||||||
|
app.build_middleware_stack() # rebuild middleware stack on-the-fly
|
||||||
|
|
||||||
|
|
||||||
|
def configure_cors_middleware(app):
|
||||||
|
from starlette.middleware.cors import CORSMiddleware
|
||||||
|
from modules.shared_cmd_options import cmd_opts
|
||||||
|
|
||||||
|
cors_options = {
|
||||||
|
"allow_methods": ["*"],
|
||||||
|
"allow_headers": ["*"],
|
||||||
|
"allow_credentials": True,
|
||||||
|
}
|
||||||
|
if cmd_opts.cors_allow_origins:
|
||||||
|
cors_options["allow_origins"] = cmd_opts.cors_allow_origins.split(',')
|
||||||
|
if cmd_opts.cors_allow_origins_regex:
|
||||||
|
cors_options["allow_origin_regex"] = cmd_opts.cors_allow_origins_regex
|
||||||
|
app.add_middleware(CORSMiddleware, **cors_options)
|
||||||
|
|
||||||
@@ -10,7 +10,7 @@ import torch.hub
|
|||||||
from torchvision import transforms
|
from torchvision import transforms
|
||||||
from torchvision.transforms.functional import InterpolationMode
|
from torchvision.transforms.functional import InterpolationMode
|
||||||
|
|
||||||
from modules import devices, paths, shared, lowvram, modelloader, errors
|
from modules import devices, paths, shared, lowvram, modelloader, errors, torch_utils
|
||||||
|
|
||||||
blip_image_eval_size = 384
|
blip_image_eval_size = 384
|
||||||
clip_model_name = 'ViT-L/14'
|
clip_model_name = 'ViT-L/14'
|
||||||
@@ -131,7 +131,7 @@ class InterrogateModels:
|
|||||||
|
|
||||||
self.clip_model = self.clip_model.to(devices.device_interrogate)
|
self.clip_model = self.clip_model.to(devices.device_interrogate)
|
||||||
|
|
||||||
self.dtype = next(self.clip_model.parameters()).dtype
|
self.dtype = torch_utils.get_param(self.clip_model).dtype
|
||||||
|
|
||||||
def send_clip_to_ram(self):
|
def send_clip_to_ram(self):
|
||||||
if not shared.opts.interrogate_keep_models_in_memory:
|
if not shared.opts.interrogate_keep_models_in_memory:
|
||||||
@@ -186,9 +186,8 @@ class InterrogateModels:
|
|||||||
res = ""
|
res = ""
|
||||||
shared.state.begin(job="interrogate")
|
shared.state.begin(job="interrogate")
|
||||||
try:
|
try:
|
||||||
if shared.cmd_opts.lowvram or shared.cmd_opts.medvram:
|
lowvram.send_everything_to_cpu()
|
||||||
lowvram.send_everything_to_cpu()
|
devices.torch_gc()
|
||||||
devices.torch_gc()
|
|
||||||
|
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
|
|||||||
+130
-49
@@ -1,20 +1,23 @@
|
|||||||
# this scripts installs necessary requirements and launches main program in webui.py
|
# this scripts installs necessary requirements and launches main program in webui.py
|
||||||
|
import logging
|
||||||
import re
|
import re
|
||||||
import subprocess
|
import subprocess
|
||||||
import os
|
import os
|
||||||
|
import shutil
|
||||||
import sys
|
import sys
|
||||||
import importlib.util
|
import importlib.util
|
||||||
|
import importlib.metadata
|
||||||
import platform
|
import platform
|
||||||
import json
|
import json
|
||||||
from functools import lru_cache
|
from functools import lru_cache
|
||||||
|
|
||||||
from modules import cmd_args, errors
|
from modules import cmd_args, errors
|
||||||
from modules.paths_internal import script_path, extensions_dir
|
from modules.paths_internal import script_path, extensions_dir
|
||||||
from modules import timer
|
from modules.timer import startup_timer
|
||||||
|
from modules import logging_config
|
||||||
timer.startup_timer.record("start")
|
|
||||||
|
|
||||||
args, _ = cmd_args.parser.parse_known_args()
|
args, _ = cmd_args.parser.parse_known_args()
|
||||||
|
logging_config.setup_logging(args.loglevel)
|
||||||
|
|
||||||
python = sys.executable
|
python = sys.executable
|
||||||
git = os.environ.get('GIT', "git")
|
git = os.environ.get('GIT', "git")
|
||||||
@@ -24,8 +27,7 @@ dir_repos = "repositories"
|
|||||||
# Whether to default to printing command output
|
# Whether to default to printing command output
|
||||||
default_command_live = (os.environ.get('WEBUI_LAUNCH_LIVE_OUTPUT') == "1")
|
default_command_live = (os.environ.get('WEBUI_LAUNCH_LIVE_OUTPUT') == "1")
|
||||||
|
|
||||||
if 'GRADIO_ANALYTICS_ENABLED' not in os.environ:
|
os.environ.setdefault('GRADIO_ANALYTICS_ENABLED', 'False')
|
||||||
os.environ['GRADIO_ANALYTICS_ENABLED'] = 'False'
|
|
||||||
|
|
||||||
|
|
||||||
def check_python_version():
|
def check_python_version():
|
||||||
@@ -62,7 +64,7 @@ Use --skip-python-version-check to suppress this warning.
|
|||||||
@lru_cache()
|
@lru_cache()
|
||||||
def commit_hash():
|
def commit_hash():
|
||||||
try:
|
try:
|
||||||
return subprocess.check_output([git, "rev-parse", "HEAD"], shell=False, encoding='utf8').strip()
|
return subprocess.check_output([git, "-C", script_path, "rev-parse", "HEAD"], shell=False, encoding='utf8').strip()
|
||||||
except Exception:
|
except Exception:
|
||||||
return "<none>"
|
return "<none>"
|
||||||
|
|
||||||
@@ -70,7 +72,7 @@ def commit_hash():
|
|||||||
@lru_cache()
|
@lru_cache()
|
||||||
def git_tag():
|
def git_tag():
|
||||||
try:
|
try:
|
||||||
return subprocess.check_output([git, "describe", "--tags"], shell=False, encoding='utf8').strip()
|
return subprocess.check_output([git, "-C", script_path, "describe", "--tags"], shell=False, encoding='utf8').strip()
|
||||||
except Exception:
|
except Exception:
|
||||||
try:
|
try:
|
||||||
|
|
||||||
@@ -117,11 +119,16 @@ def run(command, desc=None, errdesc=None, custom_env=None, live: bool = default_
|
|||||||
|
|
||||||
def is_installed(package):
|
def is_installed(package):
|
||||||
try:
|
try:
|
||||||
spec = importlib.util.find_spec(package)
|
dist = importlib.metadata.distribution(package)
|
||||||
except ModuleNotFoundError:
|
except importlib.metadata.PackageNotFoundError:
|
||||||
return False
|
try:
|
||||||
|
spec = importlib.util.find_spec(package)
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
return False
|
||||||
|
|
||||||
return spec is not None
|
return spec is not None
|
||||||
|
|
||||||
|
return dist is not None
|
||||||
|
|
||||||
|
|
||||||
def repo_dir(name):
|
def repo_dir(name):
|
||||||
@@ -141,6 +148,25 @@ def check_run_python(code: str) -> bool:
|
|||||||
return result.returncode == 0
|
return result.returncode == 0
|
||||||
|
|
||||||
|
|
||||||
|
def git_fix_workspace(dir, name):
|
||||||
|
run(f'"{git}" -C "{dir}" fetch --refetch --no-auto-gc', f"Fetching all contents for {name}", f"Couldn't fetch {name}", live=True)
|
||||||
|
run(f'"{git}" -C "{dir}" gc --aggressive --prune=now', f"Pruning {name}", f"Couldn't prune {name}", live=True)
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
|
def run_git(dir, name, command, desc=None, errdesc=None, custom_env=None, live: bool = default_command_live, autofix=True):
|
||||||
|
try:
|
||||||
|
return run(f'"{git}" -C "{dir}" {command}', desc=desc, errdesc=errdesc, custom_env=custom_env, live=live)
|
||||||
|
except RuntimeError:
|
||||||
|
if not autofix:
|
||||||
|
raise
|
||||||
|
|
||||||
|
print(f"{errdesc}, attempting autofix...")
|
||||||
|
git_fix_workspace(dir, name)
|
||||||
|
|
||||||
|
return run(f'"{git}" -C "{dir}" {command}', desc=desc, errdesc=errdesc, custom_env=custom_env, live=live)
|
||||||
|
|
||||||
|
|
||||||
def git_clone(url, dir, name, commithash=None):
|
def git_clone(url, dir, name, commithash=None):
|
||||||
# TODO clone into temporary dir and move if successful
|
# TODO clone into temporary dir and move if successful
|
||||||
|
|
||||||
@@ -148,15 +174,24 @@ def git_clone(url, dir, name, commithash=None):
|
|||||||
if commithash is None:
|
if commithash is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
current_hash = run(f'"{git}" -C "{dir}" rev-parse HEAD', None, f"Couldn't determine {name}'s hash: {commithash}", live=False).strip()
|
current_hash = run_git(dir, name, 'rev-parse HEAD', None, f"Couldn't determine {name}'s hash: {commithash}", live=False).strip()
|
||||||
if current_hash == commithash:
|
if current_hash == commithash:
|
||||||
return
|
return
|
||||||
|
|
||||||
run(f'"{git}" -C "{dir}" fetch', f"Fetching updates for {name}...", f"Couldn't fetch {name}")
|
if run_git(dir, name, 'config --get remote.origin.url', None, f"Couldn't determine {name}'s origin URL", live=False).strip() != url:
|
||||||
run(f'"{git}" -C "{dir}" checkout {commithash}', f"Checking out commit for {name} with hash: {commithash}...", f"Couldn't checkout commit {commithash} for {name}", live=True)
|
run_git(dir, name, f'remote set-url origin "{url}"', None, f"Failed to set {name}'s origin URL", live=False)
|
||||||
|
|
||||||
|
run_git(dir, name, 'fetch', f"Fetching updates for {name}...", f"Couldn't fetch {name}", autofix=False)
|
||||||
|
|
||||||
|
run_git(dir, name, f'checkout {commithash}', f"Checking out commit for {name} with hash: {commithash}...", f"Couldn't checkout commit {commithash} for {name}", live=True)
|
||||||
|
|
||||||
return
|
return
|
||||||
|
|
||||||
run(f'"{git}" clone "{url}" "{dir}"', f"Cloning {name} into {dir}...", f"Couldn't clone {name}", live=True)
|
try:
|
||||||
|
run(f'"{git}" clone "{url}" "{dir}"', f"Cloning {name} into {dir}...", f"Couldn't clone {name}", live=True)
|
||||||
|
except RuntimeError:
|
||||||
|
shutil.rmtree(dir, ignore_errors=True)
|
||||||
|
raise
|
||||||
|
|
||||||
if commithash is not None:
|
if commithash is not None:
|
||||||
run(f'"{git}" -C "{dir}" checkout {commithash}', None, "Couldn't checkout {name}'s hash: {commithash}")
|
run(f'"{git}" -C "{dir}" checkout {commithash}', None, "Couldn't checkout {name}'s hash: {commithash}")
|
||||||
@@ -198,7 +233,9 @@ def run_extension_installer(extension_dir):
|
|||||||
env = os.environ.copy()
|
env = os.environ.copy()
|
||||||
env['PYTHONPATH'] = f"{os.path.abspath('.')}{os.pathsep}{env.get('PYTHONPATH', '')}"
|
env['PYTHONPATH'] = f"{os.path.abspath('.')}{os.pathsep}{env.get('PYTHONPATH', '')}"
|
||||||
|
|
||||||
print(run(f'"{python}" "{path_installer}"', errdesc=f"Error running install.py for extension {extension_dir}", custom_env=env))
|
stdout = run(f'"{python}" "{path_installer}"', errdesc=f"Error running install.py for extension {extension_dir}", custom_env=env).strip()
|
||||||
|
if stdout:
|
||||||
|
print(stdout)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
errors.report(str(e))
|
errors.report(str(e))
|
||||||
|
|
||||||
@@ -207,16 +244,19 @@ def list_extensions(settings_file):
|
|||||||
settings = {}
|
settings = {}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if os.path.isfile(settings_file):
|
with open(settings_file, "r", encoding="utf8") as file:
|
||||||
with open(settings_file, "r", encoding="utf8") as file:
|
settings = json.load(file)
|
||||||
settings = json.load(file)
|
except FileNotFoundError:
|
||||||
|
pass
|
||||||
except Exception:
|
except Exception:
|
||||||
errors.report("Could not load settings", exc_info=True)
|
errors.report(f'\nCould not load settings\nThe config file "{settings_file}" is likely corrupted\nIt has been moved to the "tmp/config.json"\nReverting config to default\n\n''', exc_info=True)
|
||||||
|
os.replace(settings_file, os.path.join(script_path, "tmp", "config.json"))
|
||||||
|
settings = {}
|
||||||
|
|
||||||
disabled_extensions = set(settings.get('disabled_extensions', []))
|
disabled_extensions = set(settings.get('disabled_extensions', []))
|
||||||
disable_all_extensions = settings.get('disable_all_extensions', 'none')
|
disable_all_extensions = settings.get('disable_all_extensions', 'none')
|
||||||
|
|
||||||
if disable_all_extensions != 'none':
|
if disable_all_extensions != 'none' or args.disable_extra_extensions or args.disable_all_extensions or not os.path.isdir(extensions_dir):
|
||||||
return []
|
return []
|
||||||
|
|
||||||
return [x for x in os.listdir(extensions_dir) if x not in disabled_extensions]
|
return [x for x in os.listdir(extensions_dir) if x not in disabled_extensions]
|
||||||
@@ -226,8 +266,15 @@ def run_extensions_installers(settings_file):
|
|||||||
if not os.path.isdir(extensions_dir):
|
if not os.path.isdir(extensions_dir):
|
||||||
return
|
return
|
||||||
|
|
||||||
for dirname_extension in list_extensions(settings_file):
|
with startup_timer.subcategory("run extensions installers"):
|
||||||
run_extension_installer(os.path.join(extensions_dir, dirname_extension))
|
for dirname_extension in list_extensions(settings_file):
|
||||||
|
logging.debug(f"Installing {dirname_extension}")
|
||||||
|
|
||||||
|
path = os.path.join(extensions_dir, dirname_extension)
|
||||||
|
|
||||||
|
if os.path.isdir(path):
|
||||||
|
run_extension_installer(path)
|
||||||
|
startup_timer.record(dirname_extension)
|
||||||
|
|
||||||
|
|
||||||
re_requirement = re.compile(r"\s*([-_a-zA-Z0-9]+)\s*(?:==\s*([-+_.a-zA-Z0-9]+))?\s*")
|
re_requirement = re.compile(r"\s*([-_a-zA-Z0-9]+)\s*(?:==\s*([-+_.a-zA-Z0-9]+))?\s*")
|
||||||
@@ -269,29 +316,48 @@ def requirements_met(requirements_file):
|
|||||||
|
|
||||||
|
|
||||||
def prepare_environment():
|
def prepare_environment():
|
||||||
torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://download.pytorch.org/whl/cu118")
|
torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://download.pytorch.org/whl/cu121")
|
||||||
torch_command = os.environ.get('TORCH_COMMAND', f"pip install torch==2.0.1 torchvision==0.15.2 --extra-index-url {torch_index_url}")
|
torch_command = os.environ.get('TORCH_COMMAND', f"pip install torch==2.1.2 torchvision==0.16.2 --extra-index-url {torch_index_url}")
|
||||||
|
if args.use_ipex:
|
||||||
|
if platform.system() == "Windows":
|
||||||
|
# The "Nuullll/intel-extension-for-pytorch" wheels were built from IPEX source for Intel Arc GPU: https://github.com/intel/intel-extension-for-pytorch/tree/xpu-main
|
||||||
|
# This is NOT an Intel official release so please use it at your own risk!!
|
||||||
|
# See https://github.com/Nuullll/intel-extension-for-pytorch/releases/tag/v2.0.110%2Bxpu-master%2Bdll-bundle for details.
|
||||||
|
#
|
||||||
|
# Strengths (over official IPEX 2.0.110 windows release):
|
||||||
|
# - AOT build (for Arc GPU only) to eliminate JIT compilation overhead: https://github.com/intel/intel-extension-for-pytorch/issues/399
|
||||||
|
# - Bundles minimal oneAPI 2023.2 dependencies into the python wheels, so users don't need to install oneAPI for the whole system.
|
||||||
|
# - Provides a compatible torchvision wheel: https://github.com/intel/intel-extension-for-pytorch/issues/465
|
||||||
|
# Limitation:
|
||||||
|
# - Only works for python 3.10
|
||||||
|
url_prefix = "https://github.com/Nuullll/intel-extension-for-pytorch/releases/download/v2.0.110%2Bxpu-master%2Bdll-bundle"
|
||||||
|
torch_command = os.environ.get('TORCH_COMMAND', f"pip install {url_prefix}/torch-2.0.0a0+gite9ebda2-cp310-cp310-win_amd64.whl {url_prefix}/torchvision-0.15.2a0+fa99a53-cp310-cp310-win_amd64.whl {url_prefix}/intel_extension_for_pytorch-2.0.110+gitc6ea20b-cp310-cp310-win_amd64.whl")
|
||||||
|
else:
|
||||||
|
# Using official IPEX release for linux since it's already an AOT build.
|
||||||
|
# However, users still have to install oneAPI toolkit and activate oneAPI environment manually.
|
||||||
|
# See https://intel.github.io/intel-extension-for-pytorch/index.html#installation for details.
|
||||||
|
torch_index_url = os.environ.get('TORCH_INDEX_URL', "https://pytorch-extension.intel.com/release-whl/stable/xpu/us/")
|
||||||
|
torch_command = os.environ.get('TORCH_COMMAND', f"pip install torch==2.0.0a0 intel-extension-for-pytorch==2.0.110+gitba7f6c1 --extra-index-url {torch_index_url}")
|
||||||
requirements_file = os.environ.get('REQS_FILE', "requirements_versions.txt")
|
requirements_file = os.environ.get('REQS_FILE', "requirements_versions.txt")
|
||||||
|
|
||||||
xformers_package = os.environ.get('XFORMERS_PACKAGE', 'xformers==0.0.20')
|
xformers_package = os.environ.get('XFORMERS_PACKAGE', 'xformers==0.0.23.post1')
|
||||||
gfpgan_package = os.environ.get('GFPGAN_PACKAGE', "https://github.com/TencentARC/GFPGAN/archive/8d2447a2d918f8eba5a4a01463fd48e45126a379.zip")
|
|
||||||
clip_package = os.environ.get('CLIP_PACKAGE', "https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip")
|
clip_package = os.environ.get('CLIP_PACKAGE', "https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip")
|
||||||
openclip_package = os.environ.get('OPENCLIP_PACKAGE', "https://github.com/mlfoundations/open_clip/archive/bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b.zip")
|
openclip_package = os.environ.get('OPENCLIP_PACKAGE', "https://github.com/mlfoundations/open_clip/archive/bb6e834e9c70d9c27d0dc3ecedeebeaeb1ffad6b.zip")
|
||||||
|
|
||||||
|
assets_repo = os.environ.get('ASSETS_REPO', "https://github.com/AUTOMATIC1111/stable-diffusion-webui-assets.git")
|
||||||
stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "https://github.com/Stability-AI/stablediffusion.git")
|
stable_diffusion_repo = os.environ.get('STABLE_DIFFUSION_REPO', "https://github.com/Stability-AI/stablediffusion.git")
|
||||||
stable_diffusion_xl_repo = os.environ.get('STABLE_DIFFUSION_XL_REPO', "https://github.com/Stability-AI/generative-models.git")
|
stable_diffusion_xl_repo = os.environ.get('STABLE_DIFFUSION_XL_REPO', "https://github.com/Stability-AI/generative-models.git")
|
||||||
k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://github.com/crowsonkb/k-diffusion.git')
|
k_diffusion_repo = os.environ.get('K_DIFFUSION_REPO', 'https://github.com/crowsonkb/k-diffusion.git')
|
||||||
codeformer_repo = os.environ.get('CODEFORMER_REPO', 'https://github.com/sczhou/CodeFormer.git')
|
|
||||||
blip_repo = os.environ.get('BLIP_REPO', 'https://github.com/salesforce/BLIP.git')
|
blip_repo = os.environ.get('BLIP_REPO', 'https://github.com/salesforce/BLIP.git')
|
||||||
|
|
||||||
|
assets_commit_hash = os.environ.get('ASSETS_COMMIT_HASH', "6f7db241d2f8ba7457bac5ca9753331f0c266917")
|
||||||
stable_diffusion_commit_hash = os.environ.get('STABLE_DIFFUSION_COMMIT_HASH', "cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf")
|
stable_diffusion_commit_hash = os.environ.get('STABLE_DIFFUSION_COMMIT_HASH', "cf1d67a6fd5ea1aa600c4df58e5b47da45f6bdbf")
|
||||||
stable_diffusion_xl_commit_hash = os.environ.get('STABLE_DIFFUSION_XL_COMMIT_HASH', "5c10deee76adad0032b412294130090932317a87")
|
stable_diffusion_xl_commit_hash = os.environ.get('STABLE_DIFFUSION_XL_COMMIT_HASH', "45c443b316737a4ab6e40413d7794a7f5657c19f")
|
||||||
k_diffusion_commit_hash = os.environ.get('K_DIFFUSION_COMMIT_HASH', "c9fe758757e022f05ca5a53fa8fac28889e4f1cf")
|
k_diffusion_commit_hash = os.environ.get('K_DIFFUSION_COMMIT_HASH', "ab527a9a6d347f364e3d185ba6d714e22d80cb3c")
|
||||||
codeformer_commit_hash = os.environ.get('CODEFORMER_COMMIT_HASH', "c5b4593074ba6214284d6acd5f1719b6c5d739af")
|
|
||||||
blip_commit_hash = os.environ.get('BLIP_COMMIT_HASH', "48211a1594f1321b00f14c9f7a5b4813144b2fb9")
|
blip_commit_hash = os.environ.get('BLIP_COMMIT_HASH', "48211a1594f1321b00f14c9f7a5b4813144b2fb9")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# the existance of this file is a signal to webui.sh/bat that webui needs to be restarted when it stops execution
|
# the existence of this file is a signal to webui.sh/bat that webui needs to be restarted when it stops execution
|
||||||
os.remove(os.path.join(script_path, "tmp", "restart"))
|
os.remove(os.path.join(script_path, "tmp", "restart"))
|
||||||
os.environ.setdefault('SD_WEBUI_RESTARTING', '1')
|
os.environ.setdefault('SD_WEBUI_RESTARTING', '1')
|
||||||
except OSError:
|
except OSError:
|
||||||
@@ -300,8 +366,11 @@ def prepare_environment():
|
|||||||
if not args.skip_python_version_check:
|
if not args.skip_python_version_check:
|
||||||
check_python_version()
|
check_python_version()
|
||||||
|
|
||||||
|
startup_timer.record("checks")
|
||||||
|
|
||||||
commit = commit_hash()
|
commit = commit_hash()
|
||||||
tag = git_tag()
|
tag = git_tag()
|
||||||
|
startup_timer.record("git version info")
|
||||||
|
|
||||||
print(f"Python {sys.version}")
|
print(f"Python {sys.version}")
|
||||||
print(f"Version: {tag}")
|
print(f"Version: {tag}")
|
||||||
@@ -309,61 +378,60 @@ def prepare_environment():
|
|||||||
|
|
||||||
if args.reinstall_torch or not is_installed("torch") or not is_installed("torchvision"):
|
if args.reinstall_torch or not is_installed("torch") or not is_installed("torchvision"):
|
||||||
run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch", live=True)
|
run(f'"{python}" -m {torch_command}', "Installing torch and torchvision", "Couldn't install torch", live=True)
|
||||||
|
startup_timer.record("install torch")
|
||||||
|
|
||||||
|
if args.use_ipex:
|
||||||
|
args.skip_torch_cuda_test = True
|
||||||
if not args.skip_torch_cuda_test and not check_run_python("import torch; assert torch.cuda.is_available()"):
|
if not args.skip_torch_cuda_test and not check_run_python("import torch; assert torch.cuda.is_available()"):
|
||||||
raise RuntimeError(
|
raise RuntimeError(
|
||||||
'Torch is not able to use GPU; '
|
'Torch is not able to use GPU; '
|
||||||
'add --skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check'
|
'add --skip-torch-cuda-test to COMMANDLINE_ARGS variable to disable this check'
|
||||||
)
|
)
|
||||||
|
startup_timer.record("torch GPU test")
|
||||||
if not is_installed("gfpgan"):
|
|
||||||
run_pip(f"install {gfpgan_package}", "gfpgan")
|
|
||||||
|
|
||||||
if not is_installed("clip"):
|
if not is_installed("clip"):
|
||||||
run_pip(f"install {clip_package}", "clip")
|
run_pip(f"install {clip_package}", "clip")
|
||||||
|
startup_timer.record("install clip")
|
||||||
|
|
||||||
if not is_installed("open_clip"):
|
if not is_installed("open_clip"):
|
||||||
run_pip(f"install {openclip_package}", "open_clip")
|
run_pip(f"install {openclip_package}", "open_clip")
|
||||||
|
startup_timer.record("install open_clip")
|
||||||
|
|
||||||
if (not is_installed("xformers") or args.reinstall_xformers) and args.xformers:
|
if (not is_installed("xformers") or args.reinstall_xformers) and args.xformers:
|
||||||
if platform.system() == "Windows":
|
run_pip(f"install -U -I --no-deps {xformers_package}", "xformers")
|
||||||
if platform.python_version().startswith("3.10"):
|
startup_timer.record("install xformers")
|
||||||
run_pip(f"install -U -I --no-deps {xformers_package}", "xformers", live=True)
|
|
||||||
else:
|
|
||||||
print("Installation of xformers is not supported in this version of Python.")
|
|
||||||
print("You can also check this and build manually: https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Xformers#building-xformers-on-windows-by-duckness")
|
|
||||||
if not is_installed("xformers"):
|
|
||||||
exit(0)
|
|
||||||
elif platform.system() == "Linux":
|
|
||||||
run_pip(f"install -U -I --no-deps {xformers_package}", "xformers")
|
|
||||||
|
|
||||||
if not is_installed("ngrok") and args.ngrok:
|
if not is_installed("ngrok") and args.ngrok:
|
||||||
run_pip("install ngrok", "ngrok")
|
run_pip("install ngrok", "ngrok")
|
||||||
|
startup_timer.record("install ngrok")
|
||||||
|
|
||||||
os.makedirs(os.path.join(script_path, dir_repos), exist_ok=True)
|
os.makedirs(os.path.join(script_path, dir_repos), exist_ok=True)
|
||||||
|
|
||||||
|
git_clone(assets_repo, repo_dir('stable-diffusion-webui-assets'), "assets", assets_commit_hash)
|
||||||
git_clone(stable_diffusion_repo, repo_dir('stable-diffusion-stability-ai'), "Stable Diffusion", stable_diffusion_commit_hash)
|
git_clone(stable_diffusion_repo, repo_dir('stable-diffusion-stability-ai'), "Stable Diffusion", stable_diffusion_commit_hash)
|
||||||
git_clone(stable_diffusion_xl_repo, repo_dir('generative-models'), "Stable Diffusion XL", stable_diffusion_xl_commit_hash)
|
git_clone(stable_diffusion_xl_repo, repo_dir('generative-models'), "Stable Diffusion XL", stable_diffusion_xl_commit_hash)
|
||||||
git_clone(k_diffusion_repo, repo_dir('k-diffusion'), "K-diffusion", k_diffusion_commit_hash)
|
git_clone(k_diffusion_repo, repo_dir('k-diffusion'), "K-diffusion", k_diffusion_commit_hash)
|
||||||
git_clone(codeformer_repo, repo_dir('CodeFormer'), "CodeFormer", codeformer_commit_hash)
|
|
||||||
git_clone(blip_repo, repo_dir('BLIP'), "BLIP", blip_commit_hash)
|
git_clone(blip_repo, repo_dir('BLIP'), "BLIP", blip_commit_hash)
|
||||||
|
|
||||||
if not is_installed("lpips"):
|
startup_timer.record("clone repositores")
|
||||||
run_pip(f"install -r \"{os.path.join(repo_dir('CodeFormer'), 'requirements.txt')}\"", "requirements for CodeFormer")
|
|
||||||
|
|
||||||
if not os.path.isfile(requirements_file):
|
if not os.path.isfile(requirements_file):
|
||||||
requirements_file = os.path.join(script_path, requirements_file)
|
requirements_file = os.path.join(script_path, requirements_file)
|
||||||
|
|
||||||
if not requirements_met(requirements_file):
|
if not requirements_met(requirements_file):
|
||||||
run_pip(f"install -r \"{requirements_file}\"", "requirements")
|
run_pip(f"install -r \"{requirements_file}\"", "requirements")
|
||||||
|
startup_timer.record("install requirements")
|
||||||
|
|
||||||
run_extensions_installers(settings_file=args.ui_settings_file)
|
if not args.skip_install:
|
||||||
|
run_extensions_installers(settings_file=args.ui_settings_file)
|
||||||
|
|
||||||
if args.update_check:
|
if args.update_check:
|
||||||
version_check(commit)
|
version_check(commit)
|
||||||
|
startup_timer.record("check version")
|
||||||
|
|
||||||
if args.update_all_extensions:
|
if args.update_all_extensions:
|
||||||
git_pull_recursive(extensions_dir)
|
git_pull_recursive(extensions_dir)
|
||||||
|
startup_timer.record("update extensions")
|
||||||
|
|
||||||
if "--exit" in sys.argv:
|
if "--exit" in sys.argv:
|
||||||
print("Exiting because of --exit argument")
|
print("Exiting because of --exit argument")
|
||||||
@@ -392,3 +460,16 @@ def start():
|
|||||||
webui.api_only()
|
webui.api_only()
|
||||||
else:
|
else:
|
||||||
webui.webui()
|
webui.webui()
|
||||||
|
|
||||||
|
|
||||||
|
def dump_sysinfo():
|
||||||
|
from modules import sysinfo
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
text = sysinfo.get()
|
||||||
|
filename = f"sysinfo-{datetime.datetime.utcnow().strftime('%Y-%m-%d-%H-%M')}.json"
|
||||||
|
|
||||||
|
with open(filename, "w", encoding="utf8") as file:
|
||||||
|
file.write(text)
|
||||||
|
|
||||||
|
return filename
|
||||||
|
|||||||
+13
-11
@@ -1,7 +1,7 @@
|
|||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from modules import errors
|
from modules import errors, scripts
|
||||||
|
|
||||||
localizations = {}
|
localizations = {}
|
||||||
|
|
||||||
@@ -14,22 +14,24 @@ def list_localizations(dirname):
|
|||||||
if ext.lower() != ".json":
|
if ext.lower() != ".json":
|
||||||
continue
|
continue
|
||||||
|
|
||||||
localizations[fn] = os.path.join(dirname, file)
|
localizations[fn] = [os.path.join(dirname, file)]
|
||||||
|
|
||||||
from modules import scripts
|
|
||||||
for file in scripts.list_scripts("localizations", ".json"):
|
for file in scripts.list_scripts("localizations", ".json"):
|
||||||
fn, ext = os.path.splitext(file.filename)
|
fn, ext = os.path.splitext(file.filename)
|
||||||
localizations[fn] = file.path
|
if fn not in localizations:
|
||||||
|
localizations[fn] = []
|
||||||
|
localizations[fn].append(file.path)
|
||||||
|
|
||||||
|
|
||||||
def localization_js(current_localization_name: str) -> str:
|
def localization_js(current_localization_name: str) -> str:
|
||||||
fn = localizations.get(current_localization_name, None)
|
fns = localizations.get(current_localization_name, None)
|
||||||
data = {}
|
data = {}
|
||||||
if fn is not None:
|
if fns is not None:
|
||||||
try:
|
for fn in fns:
|
||||||
with open(fn, "r", encoding="utf8") as file:
|
try:
|
||||||
data = json.load(file)
|
with open(fn, "r", encoding="utf8") as file:
|
||||||
except Exception:
|
data.update(json.load(file))
|
||||||
errors.report(f"Error loading localization from {fn}", exc_info=True)
|
except Exception:
|
||||||
|
errors.report(f"Error loading localization from {fn}", exc_info=True)
|
||||||
|
|
||||||
return f"window.localization = {json.dumps(data)}"
|
return f"window.localization = {json.dumps(data)}"
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
import logging
|
||||||
|
import os
|
||||||
|
|
||||||
|
try:
|
||||||
|
from tqdm import tqdm
|
||||||
|
|
||||||
|
|
||||||
|
class TqdmLoggingHandler(logging.Handler):
|
||||||
|
def __init__(self, fallback_handler: logging.Handler):
|
||||||
|
super().__init__()
|
||||||
|
self.fallback_handler = fallback_handler
|
||||||
|
|
||||||
|
def emit(self, record):
|
||||||
|
try:
|
||||||
|
# If there are active tqdm progress bars,
|
||||||
|
# attempt to not interfere with them.
|
||||||
|
if tqdm._instances:
|
||||||
|
tqdm.write(self.format(record))
|
||||||
|
else:
|
||||||
|
self.fallback_handler.emit(record)
|
||||||
|
except Exception:
|
||||||
|
self.fallback_handler.emit(record)
|
||||||
|
|
||||||
|
except ImportError:
|
||||||
|
TqdmLoggingHandler = None
|
||||||
|
|
||||||
|
|
||||||
|
def setup_logging(loglevel):
|
||||||
|
if loglevel is None:
|
||||||
|
loglevel = os.environ.get("SD_WEBUI_LOG_LEVEL")
|
||||||
|
|
||||||
|
if not loglevel:
|
||||||
|
return
|
||||||
|
|
||||||
|
if logging.root.handlers:
|
||||||
|
# Already configured, do not interfere
|
||||||
|
return
|
||||||
|
|
||||||
|
formatter = logging.Formatter(
|
||||||
|
'%(asctime)s %(levelname)s [%(name)s] %(message)s',
|
||||||
|
'%Y-%m-%d %H:%M:%S',
|
||||||
|
)
|
||||||
|
|
||||||
|
if os.environ.get("SD_WEBUI_RICH_LOG"):
|
||||||
|
from rich.logging import RichHandler
|
||||||
|
handler = RichHandler()
|
||||||
|
else:
|
||||||
|
handler = logging.StreamHandler()
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
if TqdmLoggingHandler:
|
||||||
|
handler = TqdmLoggingHandler(handler)
|
||||||
|
|
||||||
|
handler.setFormatter(formatter)
|
||||||
|
|
||||||
|
log_level = getattr(logging, loglevel.upper(), None) or logging.INFO
|
||||||
|
logging.root.setLevel(log_level)
|
||||||
|
logging.root.addHandler(handler)
|
||||||
+19
-2
@@ -1,5 +1,5 @@
|
|||||||
import torch
|
import torch
|
||||||
from modules import devices
|
from modules import devices, shared
|
||||||
|
|
||||||
module_in_gpu = None
|
module_in_gpu = None
|
||||||
cpu = torch.device("cpu")
|
cpu = torch.device("cpu")
|
||||||
@@ -14,7 +14,24 @@ def send_everything_to_cpu():
|
|||||||
module_in_gpu = None
|
module_in_gpu = None
|
||||||
|
|
||||||
|
|
||||||
|
def is_needed(sd_model):
|
||||||
|
return shared.cmd_opts.lowvram or shared.cmd_opts.medvram or shared.cmd_opts.medvram_sdxl and hasattr(sd_model, 'conditioner')
|
||||||
|
|
||||||
|
|
||||||
|
def apply(sd_model):
|
||||||
|
enable = is_needed(sd_model)
|
||||||
|
shared.parallel_processing_allowed = not enable
|
||||||
|
|
||||||
|
if enable:
|
||||||
|
setup_for_low_vram(sd_model, not shared.cmd_opts.lowvram)
|
||||||
|
else:
|
||||||
|
sd_model.lowvram = False
|
||||||
|
|
||||||
|
|
||||||
def setup_for_low_vram(sd_model, use_medvram):
|
def setup_for_low_vram(sd_model, use_medvram):
|
||||||
|
if getattr(sd_model, 'lowvram', False):
|
||||||
|
return
|
||||||
|
|
||||||
sd_model.lowvram = True
|
sd_model.lowvram = True
|
||||||
|
|
||||||
parents = {}
|
parents = {}
|
||||||
@@ -127,4 +144,4 @@ def setup_for_low_vram(sd_model, use_medvram):
|
|||||||
|
|
||||||
|
|
||||||
def is_enabled(sd_model):
|
def is_enabled(sd_model):
|
||||||
return getattr(sd_model, 'lowvram', False)
|
return sd_model.lowvram
|
||||||
|
|||||||
+17
-5
@@ -1,9 +1,11 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
|
from torch import Tensor
|
||||||
import platform
|
import platform
|
||||||
from modules.sd_hijack_utils import CondFunc
|
from modules.sd_hijack_utils import CondFunc
|
||||||
from packaging import version
|
from packaging import version
|
||||||
|
from modules import shared
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -30,8 +32,7 @@ has_mps = check_for_mps()
|
|||||||
|
|
||||||
def torch_mps_gc() -> None:
|
def torch_mps_gc() -> None:
|
||||||
try:
|
try:
|
||||||
from modules.shared import state
|
if shared.state.current_latent is not None:
|
||||||
if state.current_latent is not None:
|
|
||||||
log.debug("`current_latent` is set, skipping MPS garbage collection")
|
log.debug("`current_latent` is set, skipping MPS garbage collection")
|
||||||
return
|
return
|
||||||
from torch.mps import empty_cache
|
from torch.mps import empty_cache
|
||||||
@@ -51,10 +52,18 @@ def cumsum_fix(input, cumsum_func, *args, **kwargs):
|
|||||||
return cumsum_func(input, *args, **kwargs)
|
return cumsum_func(input, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
if has_mps:
|
# MPS workaround for https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14046
|
||||||
# MPS fix for randn in torchsde
|
def interpolate_with_fp32_fallback(orig_func, *args, **kwargs) -> Tensor:
|
||||||
CondFunc('torchsde._brownian.brownian_interval._randn', lambda _, size, dtype, device, seed: torch.randn(size, dtype=dtype, device=torch.device("cpu"), generator=torch.Generator(torch.device("cpu")).manual_seed(int(seed))).to(device), lambda _, size, dtype, device, seed: device.type == 'mps')
|
try:
|
||||||
|
return orig_func(*args, **kwargs)
|
||||||
|
except RuntimeError as e:
|
||||||
|
if "not implemented for" in str(e) and "Half" in str(e):
|
||||||
|
input_tensor = args[0]
|
||||||
|
return orig_func(input_tensor.to(torch.float32), *args[1:], **kwargs).to(input_tensor.dtype)
|
||||||
|
else:
|
||||||
|
print(f"An unexpected RuntimeError occurred: {str(e)}")
|
||||||
|
|
||||||
|
if has_mps:
|
||||||
if platform.mac_ver()[0].startswith("13.2."):
|
if platform.mac_ver()[0].startswith("13.2."):
|
||||||
# MPS workaround for https://github.com/pytorch/pytorch/issues/95188, thanks to danieldk (https://github.com/explosion/curated-transformers/pull/124)
|
# MPS workaround for https://github.com/pytorch/pytorch/issues/95188, thanks to danieldk (https://github.com/explosion/curated-transformers/pull/124)
|
||||||
CondFunc('torch.nn.functional.linear', lambda _, input, weight, bias: (torch.matmul(input, weight.t()) + bias) if bias is not None else torch.matmul(input, weight.t()), lambda _, input, weight, bias: input.numel() > 10485760)
|
CondFunc('torch.nn.functional.linear', lambda _, input, weight, bias: (torch.matmul(input, weight.t()) + bias) if bias is not None else torch.matmul(input, weight.t()), lambda _, input, weight, bias: input.numel() > 10485760)
|
||||||
@@ -80,6 +89,9 @@ if has_mps:
|
|||||||
# MPS workaround for https://github.com/pytorch/pytorch/issues/96113
|
# MPS workaround for https://github.com/pytorch/pytorch/issues/96113
|
||||||
CondFunc('torch.nn.functional.layer_norm', lambda orig_func, x, normalized_shape, weight, bias, eps, **kwargs: orig_func(x.float(), normalized_shape, weight.float() if weight is not None else None, bias.float() if bias is not None else bias, eps).to(x.dtype), lambda _, input, *args, **kwargs: len(args) == 4 and input.device.type == 'mps')
|
CondFunc('torch.nn.functional.layer_norm', lambda orig_func, x, normalized_shape, weight, bias, eps, **kwargs: orig_func(x.float(), normalized_shape, weight.float() if weight is not None else None, bias.float() if bias is not None else bias, eps).to(x.dtype), lambda _, input, *args, **kwargs: len(args) == 4 and input.device.type == 'mps')
|
||||||
|
|
||||||
|
# MPS workaround for https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14046
|
||||||
|
CondFunc('torch.nn.functional.interpolate', interpolate_with_fp32_fallback, None)
|
||||||
|
|
||||||
# MPS workaround for https://github.com/pytorch/pytorch/issues/92311
|
# MPS workaround for https://github.com/pytorch/pytorch/issues/92311
|
||||||
if platform.processor() == 'i386':
|
if platform.processor() == 'i386':
|
||||||
for funcName in ['torch.argmax', 'torch.Tensor.argmax']:
|
for funcName in ['torch.argmax', 'torch.Tensor.argmax']:
|
||||||
|
|||||||
+9
-34
@@ -3,40 +3,15 @@ from PIL import Image, ImageFilter, ImageOps
|
|||||||
|
|
||||||
def get_crop_region(mask, pad=0):
|
def get_crop_region(mask, pad=0):
|
||||||
"""finds a rectangular region that contains all masked ares in an image. Returns (x1, y1, x2, y2) coordinates of the rectangle.
|
"""finds a rectangular region that contains all masked ares in an image. Returns (x1, y1, x2, y2) coordinates of the rectangle.
|
||||||
For example, if a user has painted the top-right part of a 512x512 image", the result may be (256, 0, 512, 256)"""
|
For example, if a user has painted the top-right part of a 512x512 image, the result may be (256, 0, 512, 256)"""
|
||||||
|
mask_img = mask if isinstance(mask, Image.Image) else Image.fromarray(mask)
|
||||||
h, w = mask.shape
|
box = mask_img.getbbox()
|
||||||
|
if box:
|
||||||
crop_left = 0
|
x1, y1, x2, y2 = box
|
||||||
for i in range(w):
|
else: # when no box is found
|
||||||
if not (mask[:, i] == 0).all():
|
x1, y1 = mask_img.size
|
||||||
break
|
x2 = y2 = 0
|
||||||
crop_left += 1
|
return max(x1 - pad, 0), max(y1 - pad, 0), min(x2 + pad, mask_img.size[0]), min(y2 + pad, mask_img.size[1])
|
||||||
|
|
||||||
crop_right = 0
|
|
||||||
for i in reversed(range(w)):
|
|
||||||
if not (mask[:, i] == 0).all():
|
|
||||||
break
|
|
||||||
crop_right += 1
|
|
||||||
|
|
||||||
crop_top = 0
|
|
||||||
for i in range(h):
|
|
||||||
if not (mask[i] == 0).all():
|
|
||||||
break
|
|
||||||
crop_top += 1
|
|
||||||
|
|
||||||
crop_bottom = 0
|
|
||||||
for i in reversed(range(h)):
|
|
||||||
if not (mask[i] == 0).all():
|
|
||||||
break
|
|
||||||
crop_bottom += 1
|
|
||||||
|
|
||||||
return (
|
|
||||||
int(max(crop_left-pad, 0)),
|
|
||||||
int(max(crop_top-pad, 0)),
|
|
||||||
int(min(w - crop_right + pad, w)),
|
|
||||||
int(min(h - crop_bottom + pad, h))
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def expand_crop_region(crop_region, processing_width, processing_height, image_width, image_height):
|
def expand_crop_region(crop_region, processing_width, processing_height, image_width, image_height):
|
||||||
|
|||||||
+41
-51
@@ -1,13 +1,20 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
|
||||||
import shutil
|
|
||||||
import importlib
|
import importlib
|
||||||
|
import logging
|
||||||
|
import os
|
||||||
|
from typing import TYPE_CHECKING
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
import torch
|
||||||
|
|
||||||
from modules import shared
|
from modules import shared
|
||||||
from modules.upscaler import Upscaler, UpscalerLanczos, UpscalerNearest, UpscalerNone
|
from modules.upscaler import Upscaler, UpscalerLanczos, UpscalerNearest, UpscalerNone
|
||||||
from modules.paths import script_path, models_path
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
import spandrel
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def load_file_from_url(
|
def load_file_from_url(
|
||||||
@@ -90,54 +97,6 @@ def friendly_name(file: str):
|
|||||||
return model_name
|
return model_name
|
||||||
|
|
||||||
|
|
||||||
def cleanup_models():
|
|
||||||
# This code could probably be more efficient if we used a tuple list or something to store the src/destinations
|
|
||||||
# and then enumerate that, but this works for now. In the future, it'd be nice to just have every "model" scaler
|
|
||||||
# somehow auto-register and just do these things...
|
|
||||||
root_path = script_path
|
|
||||||
src_path = models_path
|
|
||||||
dest_path = os.path.join(models_path, "Stable-diffusion")
|
|
||||||
move_files(src_path, dest_path, ".ckpt")
|
|
||||||
move_files(src_path, dest_path, ".safetensors")
|
|
||||||
src_path = os.path.join(root_path, "ESRGAN")
|
|
||||||
dest_path = os.path.join(models_path, "ESRGAN")
|
|
||||||
move_files(src_path, dest_path)
|
|
||||||
src_path = os.path.join(models_path, "BSRGAN")
|
|
||||||
dest_path = os.path.join(models_path, "ESRGAN")
|
|
||||||
move_files(src_path, dest_path, ".pth")
|
|
||||||
src_path = os.path.join(root_path, "gfpgan")
|
|
||||||
dest_path = os.path.join(models_path, "GFPGAN")
|
|
||||||
move_files(src_path, dest_path)
|
|
||||||
src_path = os.path.join(root_path, "SwinIR")
|
|
||||||
dest_path = os.path.join(models_path, "SwinIR")
|
|
||||||
move_files(src_path, dest_path)
|
|
||||||
src_path = os.path.join(root_path, "repositories/latent-diffusion/experiments/pretrained_models/")
|
|
||||||
dest_path = os.path.join(models_path, "LDSR")
|
|
||||||
move_files(src_path, dest_path)
|
|
||||||
|
|
||||||
|
|
||||||
def move_files(src_path: str, dest_path: str, ext_filter: str = None):
|
|
||||||
try:
|
|
||||||
os.makedirs(dest_path, exist_ok=True)
|
|
||||||
if os.path.exists(src_path):
|
|
||||||
for file in os.listdir(src_path):
|
|
||||||
fullpath = os.path.join(src_path, file)
|
|
||||||
if os.path.isfile(fullpath):
|
|
||||||
if ext_filter is not None:
|
|
||||||
if ext_filter not in file:
|
|
||||||
continue
|
|
||||||
print(f"Moving {file} from {src_path} to {dest_path}.")
|
|
||||||
try:
|
|
||||||
shutil.move(fullpath, dest_path)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if len(os.listdir(src_path)) == 0:
|
|
||||||
print(f"Removing empty folder: {src_path}")
|
|
||||||
shutil.rmtree(src_path, True)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def load_upscalers():
|
def load_upscalers():
|
||||||
# We can only do this 'magic' method to dynamically load upscalers if they are referenced,
|
# We can only do this 'magic' method to dynamically load upscalers if they are referenced,
|
||||||
# so we'll try to import any _model.py files before looking in __subclasses__
|
# so we'll try to import any _model.py files before looking in __subclasses__
|
||||||
@@ -177,3 +136,34 @@ def load_upscalers():
|
|||||||
# Special case for UpscalerNone keeps it at the beginning of the list.
|
# Special case for UpscalerNone keeps it at the beginning of the list.
|
||||||
key=lambda x: x.name.lower() if not isinstance(x.scaler, (UpscalerNone, UpscalerLanczos, UpscalerNearest)) else ""
|
key=lambda x: x.name.lower() if not isinstance(x.scaler, (UpscalerNone, UpscalerLanczos, UpscalerNearest)) else ""
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def load_spandrel_model(
|
||||||
|
path: str | os.PathLike,
|
||||||
|
*,
|
||||||
|
device: str | torch.device | None,
|
||||||
|
prefer_half: bool = False,
|
||||||
|
dtype: str | torch.dtype | None = None,
|
||||||
|
expected_architecture: str | None = None,
|
||||||
|
) -> spandrel.ModelDescriptor:
|
||||||
|
import spandrel
|
||||||
|
model_descriptor = spandrel.ModelLoader(device=device).load_from_file(str(path))
|
||||||
|
if expected_architecture and model_descriptor.architecture != expected_architecture:
|
||||||
|
logger.warning(
|
||||||
|
f"Model {path!r} is not a {expected_architecture!r} model (got {model_descriptor.architecture!r})",
|
||||||
|
)
|
||||||
|
half = False
|
||||||
|
if prefer_half:
|
||||||
|
if model_descriptor.supports_half:
|
||||||
|
model_descriptor.model.half()
|
||||||
|
half = True
|
||||||
|
else:
|
||||||
|
logger.info("Model %s does not support half precision, ignoring --half", path)
|
||||||
|
if dtype:
|
||||||
|
model_descriptor.model.to(dtype=dtype)
|
||||||
|
model_descriptor.model.eval()
|
||||||
|
logger.debug(
|
||||||
|
"Loaded %s from %s (device=%s, half=%s, dtype=%s)",
|
||||||
|
model_descriptor, path, device, half, dtype,
|
||||||
|
)
|
||||||
|
return model_descriptor
|
||||||
|
|||||||
@@ -24,10 +24,15 @@ from pytorch_lightning.utilities.distributed import rank_zero_only
|
|||||||
from ldm.util import log_txt_as_img, exists, default, ismap, isimage, mean_flat, count_params, instantiate_from_config
|
from ldm.util import log_txt_as_img, exists, default, ismap, isimage, mean_flat, count_params, instantiate_from_config
|
||||||
from ldm.modules.ema import LitEma
|
from ldm.modules.ema import LitEma
|
||||||
from ldm.modules.distributions.distributions import normal_kl, DiagonalGaussianDistribution
|
from ldm.modules.distributions.distributions import normal_kl, DiagonalGaussianDistribution
|
||||||
from ldm.models.autoencoder import VQModelInterface, IdentityFirstStage, AutoencoderKL
|
from ldm.models.autoencoder import IdentityFirstStage, AutoencoderKL
|
||||||
from ldm.modules.diffusionmodules.util import make_beta_schedule, extract_into_tensor, noise_like
|
from ldm.modules.diffusionmodules.util import make_beta_schedule, extract_into_tensor, noise_like
|
||||||
from ldm.models.diffusion.ddim import DDIMSampler
|
from ldm.models.diffusion.ddim import DDIMSampler
|
||||||
|
|
||||||
|
try:
|
||||||
|
from ldm.models.autoencoder import VQModelInterface
|
||||||
|
except Exception:
|
||||||
|
class VQModelInterface:
|
||||||
|
pass
|
||||||
|
|
||||||
__conditioning_keys__ = {'concat': 'c_concat',
|
__conditioning_keys__ = {'concat': 'c_concat',
|
||||||
'crossattn': 'c_crossattn',
|
'crossattn': 'c_crossattn',
|
||||||
|
|||||||
@@ -0,0 +1,331 @@
|
|||||||
|
import os
|
||||||
|
import json
|
||||||
|
import sys
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
import gradio as gr
|
||||||
|
|
||||||
|
from modules import errors
|
||||||
|
from modules.shared_cmd_options import cmd_opts
|
||||||
|
from modules.paths_internal import script_path
|
||||||
|
|
||||||
|
|
||||||
|
class OptionInfo:
|
||||||
|
def __init__(self, default=None, label="", component=None, component_args=None, onchange=None, section=None, refresh=None, comment_before='', comment_after='', infotext=None, restrict_api=False, category_id=None):
|
||||||
|
self.default = default
|
||||||
|
self.label = label
|
||||||
|
self.component = component
|
||||||
|
self.component_args = component_args
|
||||||
|
self.onchange = onchange
|
||||||
|
self.section = section
|
||||||
|
self.category_id = category_id
|
||||||
|
self.refresh = refresh
|
||||||
|
self.do_not_save = False
|
||||||
|
|
||||||
|
self.comment_before = comment_before
|
||||||
|
"""HTML text that will be added after label in UI"""
|
||||||
|
|
||||||
|
self.comment_after = comment_after
|
||||||
|
"""HTML text that will be added before label in UI"""
|
||||||
|
|
||||||
|
self.infotext = infotext
|
||||||
|
|
||||||
|
self.restrict_api = restrict_api
|
||||||
|
"""If True, the setting will not be accessible via API"""
|
||||||
|
|
||||||
|
def link(self, label, url):
|
||||||
|
self.comment_before += f"[<a href='{url}' target='_blank'>{label}</a>]"
|
||||||
|
return self
|
||||||
|
|
||||||
|
def js(self, label, js_func):
|
||||||
|
self.comment_before += f"[<a onclick='{js_func}(); return false'>{label}</a>]"
|
||||||
|
return self
|
||||||
|
|
||||||
|
def info(self, info):
|
||||||
|
self.comment_after += f"<span class='info'>({info})</span>"
|
||||||
|
return self
|
||||||
|
|
||||||
|
def html(self, html):
|
||||||
|
self.comment_after += html
|
||||||
|
return self
|
||||||
|
|
||||||
|
def needs_restart(self):
|
||||||
|
self.comment_after += " <span class='info'>(requires restart)</span>"
|
||||||
|
return self
|
||||||
|
|
||||||
|
def needs_reload_ui(self):
|
||||||
|
self.comment_after += " <span class='info'>(requires Reload UI)</span>"
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
class OptionHTML(OptionInfo):
|
||||||
|
def __init__(self, text):
|
||||||
|
super().__init__(str(text).strip(), label='', component=lambda **kwargs: gr.HTML(elem_classes="settings-info", **kwargs))
|
||||||
|
|
||||||
|
self.do_not_save = True
|
||||||
|
|
||||||
|
|
||||||
|
def options_section(section_identifier, options_dict):
|
||||||
|
for v in options_dict.values():
|
||||||
|
if len(section_identifier) == 2:
|
||||||
|
v.section = section_identifier
|
||||||
|
elif len(section_identifier) == 3:
|
||||||
|
v.section = section_identifier[0:2]
|
||||||
|
v.category_id = section_identifier[2]
|
||||||
|
|
||||||
|
return options_dict
|
||||||
|
|
||||||
|
|
||||||
|
options_builtin_fields = {"data_labels", "data", "restricted_opts", "typemap"}
|
||||||
|
|
||||||
|
|
||||||
|
class Options:
|
||||||
|
typemap = {int: float}
|
||||||
|
|
||||||
|
def __init__(self, data_labels: dict[str, OptionInfo], restricted_opts):
|
||||||
|
self.data_labels = data_labels
|
||||||
|
self.data = {k: v.default for k, v in self.data_labels.items() if not v.do_not_save}
|
||||||
|
self.restricted_opts = restricted_opts
|
||||||
|
|
||||||
|
def __setattr__(self, key, value):
|
||||||
|
if key in options_builtin_fields:
|
||||||
|
return super(Options, self).__setattr__(key, value)
|
||||||
|
|
||||||
|
if self.data is not None:
|
||||||
|
if key in self.data or key in self.data_labels:
|
||||||
|
|
||||||
|
# Check that settings aren't globally frozen
|
||||||
|
assert not cmd_opts.freeze_settings, "changing settings is disabled"
|
||||||
|
|
||||||
|
# Get the info related to the setting being changed
|
||||||
|
info = self.data_labels.get(key, None)
|
||||||
|
if info.do_not_save:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Restrict component arguments
|
||||||
|
comp_args = info.component_args if info else None
|
||||||
|
if isinstance(comp_args, dict) and comp_args.get('visible', True) is False:
|
||||||
|
raise RuntimeError(f"not possible to set '{key}' because it is restricted")
|
||||||
|
|
||||||
|
# Check that this section isn't frozen
|
||||||
|
if cmd_opts.freeze_settings_in_sections is not None:
|
||||||
|
frozen_sections = list(map(str.strip, cmd_opts.freeze_settings_in_sections.split(','))) # Trim whitespace from section names
|
||||||
|
section_key = info.section[0]
|
||||||
|
section_name = info.section[1]
|
||||||
|
assert section_key not in frozen_sections, f"not possible to set '{key}' because settings in section '{section_name}' ({section_key}) are frozen with --freeze-settings-in-sections"
|
||||||
|
|
||||||
|
# Check that this section of the settings isn't frozen
|
||||||
|
if cmd_opts.freeze_specific_settings is not None:
|
||||||
|
frozen_keys = list(map(str.strip, cmd_opts.freeze_specific_settings.split(','))) # Trim whitespace from setting keys
|
||||||
|
assert key not in frozen_keys, f"not possible to set '{key}' because this setting is frozen with --freeze-specific-settings"
|
||||||
|
|
||||||
|
# Check shorthand option which disables editing options in "saving-paths"
|
||||||
|
if cmd_opts.hide_ui_dir_config and key in self.restricted_opts:
|
||||||
|
raise RuntimeError(f"not possible to set '{key}' because it is restricted with --hide_ui_dir_config")
|
||||||
|
|
||||||
|
self.data[key] = value
|
||||||
|
return
|
||||||
|
|
||||||
|
return super(Options, self).__setattr__(key, value)
|
||||||
|
|
||||||
|
def __getattr__(self, item):
|
||||||
|
if item in options_builtin_fields:
|
||||||
|
return super(Options, self).__getattribute__(item)
|
||||||
|
|
||||||
|
if self.data is not None:
|
||||||
|
if item in self.data:
|
||||||
|
return self.data[item]
|
||||||
|
|
||||||
|
if item in self.data_labels:
|
||||||
|
return self.data_labels[item].default
|
||||||
|
|
||||||
|
return super(Options, self).__getattribute__(item)
|
||||||
|
|
||||||
|
def set(self, key, value, is_api=False, run_callbacks=True):
|
||||||
|
"""sets an option and calls its onchange callback, returning True if the option changed and False otherwise"""
|
||||||
|
|
||||||
|
oldval = self.data.get(key, None)
|
||||||
|
if oldval == value:
|
||||||
|
return False
|
||||||
|
|
||||||
|
option = self.data_labels[key]
|
||||||
|
if option.do_not_save:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if is_api and option.restrict_api:
|
||||||
|
return False
|
||||||
|
|
||||||
|
try:
|
||||||
|
setattr(self, key, value)
|
||||||
|
except RuntimeError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
if run_callbacks and option.onchange is not None:
|
||||||
|
try:
|
||||||
|
option.onchange()
|
||||||
|
except Exception as e:
|
||||||
|
errors.display(e, f"changing setting {key} to {value}")
|
||||||
|
setattr(self, key, oldval)
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def get_default(self, key):
|
||||||
|
"""returns the default value for the key"""
|
||||||
|
|
||||||
|
data_label = self.data_labels.get(key)
|
||||||
|
if data_label is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return data_label.default
|
||||||
|
|
||||||
|
def save(self, filename):
|
||||||
|
assert not cmd_opts.freeze_settings, "saving settings is disabled"
|
||||||
|
|
||||||
|
with open(filename, "w", encoding="utf8") as file:
|
||||||
|
json.dump(self.data, file, indent=4, ensure_ascii=False)
|
||||||
|
|
||||||
|
def same_type(self, x, y):
|
||||||
|
if x is None or y is None:
|
||||||
|
return True
|
||||||
|
|
||||||
|
type_x = self.typemap.get(type(x), type(x))
|
||||||
|
type_y = self.typemap.get(type(y), type(y))
|
||||||
|
|
||||||
|
return type_x == type_y
|
||||||
|
|
||||||
|
def load(self, filename):
|
||||||
|
try:
|
||||||
|
with open(filename, "r", encoding="utf8") as file:
|
||||||
|
self.data = json.load(file)
|
||||||
|
except Exception:
|
||||||
|
errors.report(f'\nCould not load settings\nThe config file "{filename}" is likely corrupted\nIt has been moved to the "tmp/config.json"\nReverting config to default\n\n''', exc_info=True)
|
||||||
|
os.replace(filename, os.path.join(script_path, "tmp", "config.json"))
|
||||||
|
self.data = {}
|
||||||
|
# 1.6.0 VAE defaults
|
||||||
|
if self.data.get('sd_vae_as_default') is not None and self.data.get('sd_vae_overrides_per_model_preferences') is None:
|
||||||
|
self.data['sd_vae_overrides_per_model_preferences'] = not self.data.get('sd_vae_as_default')
|
||||||
|
|
||||||
|
# 1.1.1 quicksettings list migration
|
||||||
|
if self.data.get('quicksettings') is not None and self.data.get('quicksettings_list') is None:
|
||||||
|
self.data['quicksettings_list'] = [i.strip() for i in self.data.get('quicksettings').split(',')]
|
||||||
|
|
||||||
|
# 1.4.0 ui_reorder
|
||||||
|
if isinstance(self.data.get('ui_reorder'), str) and self.data.get('ui_reorder') and "ui_reorder_list" not in self.data:
|
||||||
|
self.data['ui_reorder_list'] = [i.strip() for i in self.data.get('ui_reorder').split(',')]
|
||||||
|
|
||||||
|
bad_settings = 0
|
||||||
|
for k, v in self.data.items():
|
||||||
|
info = self.data_labels.get(k, None)
|
||||||
|
if info is not None and not self.same_type(info.default, v):
|
||||||
|
print(f"Warning: bad setting value: {k}: {v} ({type(v).__name__}; expected {type(info.default).__name__})", file=sys.stderr)
|
||||||
|
bad_settings += 1
|
||||||
|
|
||||||
|
if bad_settings > 0:
|
||||||
|
print(f"The program is likely to not work with bad settings.\nSettings file: {filename}\nEither fix the file, or delete it and restart.", file=sys.stderr)
|
||||||
|
|
||||||
|
def onchange(self, key, func, call=True):
|
||||||
|
item = self.data_labels.get(key)
|
||||||
|
item.onchange = func
|
||||||
|
|
||||||
|
if call:
|
||||||
|
func()
|
||||||
|
|
||||||
|
def dumpjson(self):
|
||||||
|
d = {k: self.data.get(k, v.default) for k, v in self.data_labels.items()}
|
||||||
|
d["_comments_before"] = {k: v.comment_before for k, v in self.data_labels.items() if v.comment_before is not None}
|
||||||
|
d["_comments_after"] = {k: v.comment_after for k, v in self.data_labels.items() if v.comment_after is not None}
|
||||||
|
|
||||||
|
item_categories = {}
|
||||||
|
for item in self.data_labels.values():
|
||||||
|
category = categories.mapping.get(item.category_id)
|
||||||
|
category = "Uncategorized" if category is None else category.label
|
||||||
|
if category not in item_categories:
|
||||||
|
item_categories[category] = item.section[1]
|
||||||
|
|
||||||
|
# _categories is a list of pairs: [section, category]. Each section (a setting page) will get a special heading above it with the category as text.
|
||||||
|
d["_categories"] = [[v, k] for k, v in item_categories.items()] + [["Defaults", "Other"]]
|
||||||
|
|
||||||
|
return json.dumps(d)
|
||||||
|
|
||||||
|
def add_option(self, key, info):
|
||||||
|
self.data_labels[key] = info
|
||||||
|
if key not in self.data and not info.do_not_save:
|
||||||
|
self.data[key] = info.default
|
||||||
|
|
||||||
|
def reorder(self):
|
||||||
|
"""Reorder settings so that:
|
||||||
|
- all items related to section always go together
|
||||||
|
- all sections belonging to a category go together
|
||||||
|
- sections inside a category are ordered alphabetically
|
||||||
|
- categories are ordered by creation order
|
||||||
|
|
||||||
|
Category is a superset of sections: for category "postprocessing" there could be multiple sections: "face restoration", "upscaling".
|
||||||
|
|
||||||
|
This function also changes items' category_id so that all items belonging to a section have the same category_id.
|
||||||
|
"""
|
||||||
|
|
||||||
|
category_ids = {}
|
||||||
|
section_categories = {}
|
||||||
|
|
||||||
|
settings_items = self.data_labels.items()
|
||||||
|
for _, item in settings_items:
|
||||||
|
if item.section not in section_categories:
|
||||||
|
section_categories[item.section] = item.category_id
|
||||||
|
|
||||||
|
for _, item in settings_items:
|
||||||
|
item.category_id = section_categories.get(item.section)
|
||||||
|
|
||||||
|
for category_id in categories.mapping:
|
||||||
|
if category_id not in category_ids:
|
||||||
|
category_ids[category_id] = len(category_ids)
|
||||||
|
|
||||||
|
def sort_key(x):
|
||||||
|
item: OptionInfo = x[1]
|
||||||
|
category_order = category_ids.get(item.category_id, len(category_ids))
|
||||||
|
section_order = item.section[1]
|
||||||
|
|
||||||
|
return category_order, section_order
|
||||||
|
|
||||||
|
self.data_labels = dict(sorted(settings_items, key=sort_key))
|
||||||
|
|
||||||
|
def cast_value(self, key, value):
|
||||||
|
"""casts an arbitrary to the same type as this setting's value with key
|
||||||
|
Example: cast_value("eta_noise_seed_delta", "12") -> returns 12 (an int rather than str)
|
||||||
|
"""
|
||||||
|
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
default_value = self.data_labels[key].default
|
||||||
|
if default_value is None:
|
||||||
|
default_value = getattr(self, key, None)
|
||||||
|
if default_value is None:
|
||||||
|
return None
|
||||||
|
|
||||||
|
expected_type = type(default_value)
|
||||||
|
if expected_type == bool and value == "False":
|
||||||
|
value = False
|
||||||
|
else:
|
||||||
|
value = expected_type(value)
|
||||||
|
|
||||||
|
return value
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class OptionsCategory:
|
||||||
|
id: str
|
||||||
|
label: str
|
||||||
|
|
||||||
|
class OptionsCategories:
|
||||||
|
def __init__(self):
|
||||||
|
self.mapping = {}
|
||||||
|
|
||||||
|
def register_category(self, category_id, label):
|
||||||
|
if category_id in self.mapping:
|
||||||
|
return category_id
|
||||||
|
|
||||||
|
self.mapping[category_id] = OptionsCategory(category_id, label)
|
||||||
|
|
||||||
|
|
||||||
|
categories = OptionsCategories()
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user