Compare commits
179
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cc5803603b | ||
|
|
10a2de644f | ||
|
|
50be33e953 | ||
|
|
429442f4a6 | ||
|
|
8561d5762b | ||
|
|
80f3cf2bb2 | ||
|
|
ee015a1af6 | ||
|
|
2fffd4bddc | ||
|
|
7dba1c07cb | ||
|
|
2d006ce16c | ||
|
|
4aeacaefbf | ||
|
|
dc1432e0dd | ||
|
|
ca5efc316b | ||
|
|
f421f2af2d | ||
|
|
57e03cdd24 | ||
|
|
8aead63f1a | ||
|
|
7edd58d90d | ||
|
|
c15c3b08df | ||
|
|
fd07b103ae | ||
|
|
336bd8703c | ||
|
|
ee10c41e2a | ||
|
|
2e2d45b281 | ||
|
|
fec2221eea | ||
|
|
6ac2ec2b78 | ||
|
|
65b973ac4e | ||
|
|
6d408c06c6 | ||
|
|
f53f703aeb | ||
|
|
963d986396 | ||
|
|
6be32b31d1 | ||
|
|
66ec505975 | ||
|
|
7e6a6e00ad | ||
|
|
5f3317376b | ||
|
|
91d7ee0d09 | ||
|
|
aa75d5cfe8 | ||
|
|
2f6ea2fbca | ||
|
|
d6fcc6b87b | ||
|
|
db71290d26 | ||
|
|
61788c0538 | ||
|
|
12f4f4761b | ||
|
|
419e539fe3 | ||
|
|
e5fbf5c755 | ||
|
|
c080f52cea | ||
|
|
9e5f6b5580 | ||
|
|
d7474a5185 | ||
|
|
6a9ea5b41c | ||
|
|
d4ea5f4d86 | ||
|
|
6d09b8d1df | ||
|
|
ff4ef13dd5 | ||
|
|
d682444ecc | ||
|
|
5ba23cb41f | ||
|
|
4f96ffd0b5 | ||
|
|
861db783c7 | ||
|
|
574c8e554a | ||
|
|
98fd5cde72 | ||
|
|
c0484f1b98 | ||
|
|
f7e86aa420 | ||
|
|
1eaad95533 | ||
|
|
e0ee5bf703 | ||
|
|
66b7d7584f | ||
|
|
d01a2d0156 | ||
|
|
a05c824384 | ||
|
|
5766ce21ab | ||
|
|
b0583be088 | ||
|
|
873efeed49 | ||
|
|
a004d1a855 | ||
|
|
5992564448 | ||
|
|
4e485b7923 | ||
|
|
210fd72bab | ||
|
|
54c519943a | ||
|
|
031dc8cd7f | ||
|
|
861297cefe | ||
|
|
87b77cad5f | ||
|
|
b372f5538b | ||
|
|
eacc03b167 | ||
|
|
1eae307607 | ||
|
|
92d7a13885 | ||
|
|
9b8faefde0 | ||
|
|
45ada1c910 | ||
|
|
dce7fc902a | ||
|
|
530103b586 | ||
|
|
1a0a6a84c3 | ||
|
|
a8490e4019 | ||
|
|
4b460fcb1a | ||
|
|
255be75d30 | ||
|
|
8bacbca0a1 | ||
|
|
b2368a3bce | ||
|
|
5de806184f | ||
|
|
948533950c | ||
|
|
5e2627a1a6 | ||
|
|
8617396c6d | ||
|
|
8b7d3f1bef | ||
|
|
7aa8fcac1e | ||
|
|
1add3cff84 | ||
|
|
bb932dbf9f | ||
|
|
70b50b1dfc | ||
|
|
d66bc86159 | ||
|
|
47f5e216da | ||
|
|
aca1553bde | ||
|
|
76ef3d75f6 | ||
|
|
e0fbe6d27e | ||
|
|
767202a4c3 | ||
|
|
315d5a8ed9 | ||
|
|
b980e7188c | ||
|
|
a1a05ad2d1 | ||
|
|
907a88b2d0 | ||
|
|
2536ecbb17 | ||
|
|
42bf5fa325 | ||
|
|
f98338faa8 | ||
|
|
f0ab972f85 | ||
|
|
bc3e183b73 | ||
|
|
5da1ba0e91 | ||
|
|
1d64976dbc | ||
|
|
727e4d1086 | ||
|
|
b3d3b335cf | ||
|
|
39919c40dd | ||
|
|
af62ad4d25 | ||
|
|
ed769977f0 | ||
|
|
ece27fe989 | ||
|
|
e37d0cdd06 | ||
|
|
5c3254b3ee | ||
|
|
3e7a981194 | ||
|
|
623251ce2b | ||
|
|
b8c38f2bbf | ||
|
|
9d33baba58 | ||
|
|
6c36fe5719 | ||
|
|
d5c14365fd | ||
|
|
460bbae587 | ||
|
|
b340439586 | ||
|
|
f347ddfd80 | ||
|
|
df6d0d9286 | ||
|
|
707a431100 | ||
|
|
ce2d7f7eac | ||
|
|
ce37fdd30e | ||
|
|
7a20f914ed | ||
|
|
6ad3a53e36 | ||
|
|
ea00c1624b | ||
|
|
8f1efdc130 | ||
|
|
04c745ea4f | ||
|
|
7349088d32 | ||
|
|
a357823339 | ||
|
|
2f94331df2 | ||
|
|
4ee7519fc2 | ||
|
|
8ec069e64d | ||
|
|
3110f895b2 | ||
|
|
8acc901ba3 | ||
|
|
1f92336be7 | ||
|
|
6fdad291bd | ||
|
|
cc92dc1f8d | ||
|
|
6435691bb1 | ||
|
|
4117afff11 | ||
|
|
e2c2925eb4 | ||
|
|
d6a599ef9b | ||
|
|
0ac3a07eec | ||
|
|
45fbd1c5fe | ||
|
|
01fd9cf0d2 | ||
|
|
96f1e6be59 | ||
|
|
6684610510 | ||
|
|
d0184b8f76 | ||
|
|
5d12ec82d3 | ||
|
|
969bd8256e | ||
|
|
03694e1f99 | ||
|
|
a65476718f | ||
|
|
fa0c5eb81b | ||
|
|
8d340cfb88 | ||
|
|
84ddd44113 | ||
|
|
1824e9ee3a | ||
|
|
ad3ae44108 | ||
|
|
ec2bd9be75 | ||
|
|
a14f7bf113 | ||
|
|
e59c66c008 | ||
|
|
6c383d2e82 | ||
|
|
45bf9a6264 | ||
|
|
a2d70f25bf | ||
|
|
9ecea0a8d6 | ||
|
|
d3cd46b038 | ||
|
|
875ddfeecf | ||
|
|
cd8673bd9b | ||
|
|
5841990b0d | ||
|
|
12c4d5c6b5 |
No files matched your search
@@ -2,7 +2,7 @@
|
|||||||
name: Feature request
|
name: Feature request
|
||||||
about: Suggest an idea for this project
|
about: Suggest an idea for this project
|
||||||
title: ''
|
title: ''
|
||||||
labels: ''
|
labels: 'suggestion'
|
||||||
assignees: ''
|
assignees: ''
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
# Please read the [contributing wiki page](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Contributing) before submitting a pull request!
|
||||||
|
|
||||||
|
If you have a large change, pay special attention to this paragraph:
|
||||||
|
|
||||||
|
> Before making changes, if you think that your feature will result in more than 100 lines changing, find me and talk to me about the feature you are proposing. It pains me to reject the hard work someone else did, but I won't add everything to the repo, and it's better if the rejection happens before you have to waste time working on the feature.
|
||||||
|
|
||||||
|
Otherwise, after making sure you're following the rules described in wiki page, remove this section and continue on.
|
||||||
|
|
||||||
|
**Describe what this pull request is trying to achieve.**
|
||||||
|
|
||||||
|
A clear and concise description of what you're trying to accomplish with this, so your intent doesn't have to be extracted from your code.
|
||||||
|
|
||||||
|
**Additional notes and description of your changes**
|
||||||
|
|
||||||
|
More technical discussion about your changes go here, plus anything that a maintainer might have to specifically take a look at, or be wary of.
|
||||||
|
|
||||||
|
**Environment this was tested in**
|
||||||
|
|
||||||
|
List the environment you have developed / tested this on. As per the contributing page, changes should be able to work on Windows out of the box.
|
||||||
|
- OS: [e.g. Windows, Linux]
|
||||||
|
- Browser [e.g. chrome, safari]
|
||||||
|
- Graphics card [e.g. NVIDIA RTX 2080 8GB, AMD RX 6600 8GB]
|
||||||
|
|
||||||
|
**Screenshots or videos of your changes**
|
||||||
|
|
||||||
|
If applicable, screenshots or a video showing off your changes. If it edits an existing UI, it should ideally contain a comparison of what used to be there, before your changes were made.
|
||||||
|
|
||||||
|
This is **required** for anything that touches the user interface.
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
* @AUTOMATIC1111
|
||||||
@@ -28,10 +28,12 @@ Check the [custom scripts](https://github.com/AUTOMATIC1111/stable-diffusion-web
|
|||||||
- CodeFormer, face restoration tool as an alternative to GFPGAN
|
- CodeFormer, face restoration tool as an alternative to GFPGAN
|
||||||
- RealESRGAN, neural network upscaler
|
- RealESRGAN, neural network upscaler
|
||||||
- ESRGAN, neural network upscaler with a lot of third party models
|
- ESRGAN, neural network upscaler with a lot of third party models
|
||||||
- SwinIR, neural network upscaler
|
- SwinIR and Swin2SR([see here](https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/2092)), neural network upscalers
|
||||||
- LDSR, Latent diffusion super resolution upscaling
|
- LDSR, Latent diffusion super resolution upscaling
|
||||||
- Resizing aspect ratio options
|
- Resizing aspect ratio options
|
||||||
- Sampling method selection
|
- Sampling method selection
|
||||||
|
- Adjust sampler eta values (noise multiplier)
|
||||||
|
- More advanced noise setting options
|
||||||
- Interrupt processing at any time
|
- Interrupt processing at any time
|
||||||
- 4GB video card support (also reports of 2GB working)
|
- 4GB video card support (also reports of 2GB working)
|
||||||
- Correct seeds for batches
|
- Correct seeds for batches
|
||||||
@@ -67,6 +69,7 @@ Check the [custom scripts](https://github.com/AUTOMATIC1111/stable-diffusion-web
|
|||||||
- also supports weights for prompts: `a cat :1.2 AND a dog AND a penguin :2.2`
|
- also supports weights for prompts: `a cat :1.2 AND a dog AND a penguin :2.2`
|
||||||
- No token limit for prompts (original stable diffusion lets you use up to 75 tokens)
|
- No token limit for prompts (original stable diffusion lets you use up to 75 tokens)
|
||||||
- DeepDanbooru integration, creates danbooru style tags for anime prompts (add --deepdanbooru to commandline args)
|
- DeepDanbooru integration, creates danbooru style tags for anime prompts (add --deepdanbooru to commandline args)
|
||||||
|
- [xformers](https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Xformers), major speed increase for select cards: (add --xformers to commandline args)
|
||||||
|
|
||||||
## 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 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.
|
||||||
@@ -116,13 +119,17 @@ The documentation was moved from this README over to the project's [wiki](https:
|
|||||||
- CodeFormer - https://github.com/sczhou/CodeFormer
|
- CodeFormer - https://github.com/sczhou/CodeFormer
|
||||||
- ESRGAN - https://github.com/xinntao/ESRGAN
|
- ESRGAN - https://github.com/xinntao/ESRGAN
|
||||||
- SwinIR - https://github.com/JingyunLiang/SwinIR
|
- 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
|
||||||
- Ideas for optimizations - https://github.com/basujindal/stable-diffusion
|
- Ideas for optimizations - https://github.com/basujindal/stable-diffusion
|
||||||
- Doggettx - Cross Attention layer optimization - https://github.com/Doggettx/stable-diffusion, original idea for prompt editing.
|
- Doggettx - Cross Attention layer optimization - https://github.com/Doggettx/stable-diffusion, original idea for prompt editing.
|
||||||
|
- InvokeAI, lstein - Cross Attention layer optimization - https://github.com/invoke-ai/InvokeAI (originally http://github.com/lstein/stable-diffusion)
|
||||||
- Rinon Gal - Textual Inversion - https://github.com/rinongal/textual_inversion (we're not using his code, but we are using his ideas).
|
- Rinon Gal - Textual Inversion - https://github.com/rinongal/textual_inversion (we're not using his code, but we are using his ideas).
|
||||||
- Idea for SD upscale - https://github.com/jquesnelle/txt2imghd
|
- Idea for SD upscale - https://github.com/jquesnelle/txt2imghd
|
||||||
- Noise generation for outpainting mk2 - https://github.com/parlance-zz/g-diffuser-bot
|
- Noise generation for outpainting mk2 - https://github.com/parlance-zz/g-diffuser-bot
|
||||||
- CLIP interrogator idea and borrowing some code - https://github.com/pharmapsychotic/clip-interrogator
|
- CLIP interrogator idea and borrowing some code - https://github.com/pharmapsychotic/clip-interrogator
|
||||||
|
- Idea for Composable Diffusion - https://github.com/energy-based-model/Compositional-Visual-Generation-with-Composable-Diffusion-Models-PyTorch
|
||||||
|
- xformers - https://github.com/facebookresearch/xformers
|
||||||
|
- DeepDanbooru - interrogator for anime diffusers https://github.com/KichangKim/DeepDanbooru
|
||||||
- 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.
|
||||||
- DeepDanbooru - interrogator for anime diffusors https://github.com/KichangKim/DeepDanbooru
|
|
||||||
- (You)
|
- (You)
|
||||||
@@ -1045,7 +1045,6 @@ Bakemono Zukushi,0.67051035,anime
|
|||||||
Lucy Madox Brown,0.67032814,fineart
|
Lucy Madox Brown,0.67032814,fineart
|
||||||
Paul Wonner,0.6700563,scribbles
|
Paul Wonner,0.6700563,scribbles
|
||||||
Guido Borelli Da Caluso,0.66966087,digipa-high-impact
|
Guido Borelli Da Caluso,0.66966087,digipa-high-impact
|
||||||
Guido Borelli da Caluso,0.66966087,digipa-high-impact
|
|
||||||
Emil Alzamora,0.5844039,nudity
|
Emil Alzamora,0.5844039,nudity
|
||||||
Heinrich Brocksieper,0.64469147,fineart
|
Heinrich Brocksieper,0.64469147,fineart
|
||||||
Dan Smith,0.669563,digipa-high-impact
|
Dan Smith,0.669563,digipa-high-impact
|
||||||
|
|||||||
|
@@ -3,9 +3,9 @@ channels:
|
|||||||
- pytorch
|
- pytorch
|
||||||
- defaults
|
- defaults
|
||||||
dependencies:
|
dependencies:
|
||||||
- python=3.8.5
|
- python=3.10
|
||||||
- pip=20.3
|
- pip=22.2.2
|
||||||
- cudatoolkit=11.3
|
- cudatoolkit=11.3
|
||||||
- pytorch=1.11.0
|
- pytorch=1.12.1
|
||||||
- torchvision=0.12.0
|
- torchvision=0.13.1
|
||||||
- numpy=1.19.2
|
- numpy=1.23.1
|
||||||
+40
-35
@@ -16,7 +16,7 @@ contextMenuInit = function(){
|
|||||||
oldMenu.remove()
|
oldMenu.remove()
|
||||||
}
|
}
|
||||||
|
|
||||||
let tabButton = gradioApp().querySelector('button')
|
let tabButton = uiCurrentTab
|
||||||
let baseStyle = window.getComputedStyle(tabButton)
|
let baseStyle = window.getComputedStyle(tabButton)
|
||||||
|
|
||||||
const contextMenu = document.createElement('nav')
|
const contextMenu = document.createElement('nav')
|
||||||
@@ -123,48 +123,53 @@ contextMenuInit = function(){
|
|||||||
return [appendContextMenuOption, removeContextMenuOption, addContextMenuEventListener]
|
return [appendContextMenuOption, removeContextMenuOption, addContextMenuEventListener]
|
||||||
}
|
}
|
||||||
|
|
||||||
initResponse = contextMenuInit()
|
initResponse = contextMenuInit();
|
||||||
appendContextMenuOption = initResponse[0]
|
appendContextMenuOption = initResponse[0];
|
||||||
removeContextMenuOption = initResponse[1]
|
removeContextMenuOption = initResponse[1];
|
||||||
addContextMenuEventListener = initResponse[2]
|
addContextMenuEventListener = initResponse[2];
|
||||||
|
|
||||||
|
(function(){
|
||||||
//Start example Context Menu Items
|
//Start example Context Menu Items
|
||||||
generateOnRepeatId = appendContextMenuOption('#txt2img_generate','Generate forever',function(){
|
let generateOnRepeat = function(genbuttonid,interruptbuttonid){
|
||||||
let genbutton = gradioApp().querySelector('#txt2img_generate');
|
let genbutton = gradioApp().querySelector(genbuttonid);
|
||||||
let interruptbutton = gradioApp().querySelector('#txt2img_interrupt');
|
let interruptbutton = gradioApp().querySelector(interruptbuttonid);
|
||||||
if(!interruptbutton.offsetParent){
|
|
||||||
genbutton.click();
|
|
||||||
}
|
|
||||||
clearInterval(window.generateOnRepeatInterval)
|
|
||||||
window.generateOnRepeatInterval = setInterval(function(){
|
|
||||||
if(!interruptbutton.offsetParent){
|
if(!interruptbutton.offsetParent){
|
||||||
genbutton.click();
|
genbutton.click();
|
||||||
}
|
}
|
||||||
},
|
clearInterval(window.generateOnRepeatInterval)
|
||||||
500)}
|
window.generateOnRepeatInterval = setInterval(function(){
|
||||||
)
|
if(!interruptbutton.offsetParent){
|
||||||
|
genbutton.click();
|
||||||
cancelGenerateForever = function(){
|
}
|
||||||
clearInterval(window.generateOnRepeatInterval)
|
},
|
||||||
let interruptbutton = gradioApp().querySelector('#txt2img_interrupt');
|
500)
|
||||||
if(interruptbutton.offsetParent){
|
|
||||||
interruptbutton.click();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
appendContextMenuOption('#txt2img_interrupt','Cancel generate forever',cancelGenerateForever)
|
appendContextMenuOption('#txt2img_generate','Generate forever',function(){
|
||||||
appendContextMenuOption('#txt2img_generate', 'Cancel generate forever',cancelGenerateForever)
|
generateOnRepeat('#txt2img_generate','#txt2img_interrupt');
|
||||||
|
})
|
||||||
|
appendContextMenuOption('#img2img_generate','Generate forever',function(){
|
||||||
|
generateOnRepeat('#img2img_generate','#img2img_interrupt');
|
||||||
|
})
|
||||||
|
|
||||||
|
let cancelGenerateForever = function(){
|
||||||
appendContextMenuOption('#roll','Roll three',
|
clearInterval(window.generateOnRepeatInterval)
|
||||||
function(){
|
|
||||||
let rollbutton = gradioApp().querySelector('#roll');
|
|
||||||
setTimeout(function(){rollbutton.click()},100)
|
|
||||||
setTimeout(function(){rollbutton.click()},200)
|
|
||||||
setTimeout(function(){rollbutton.click()},300)
|
|
||||||
}
|
}
|
||||||
)
|
|
||||||
|
appendContextMenuOption('#txt2img_interrupt','Cancel generate forever',cancelGenerateForever)
|
||||||
|
appendContextMenuOption('#txt2img_generate', 'Cancel generate forever',cancelGenerateForever)
|
||||||
|
appendContextMenuOption('#img2img_interrupt','Cancel generate forever',cancelGenerateForever)
|
||||||
|
appendContextMenuOption('#img2img_generate', 'Cancel generate forever',cancelGenerateForever)
|
||||||
|
|
||||||
|
appendContextMenuOption('#roll','Roll three',
|
||||||
|
function(){
|
||||||
|
let rollbutton = get_uiCurrentTabContent().querySelector('#roll');
|
||||||
|
setTimeout(function(){rollbutton.click()},100)
|
||||||
|
setTimeout(function(){rollbutton.click()},200)
|
||||||
|
setTimeout(function(){rollbutton.click()},300)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})();
|
||||||
//End example Context Menu Items
|
//End example Context Menu Items
|
||||||
|
|
||||||
onUiUpdate(function(){
|
onUiUpdate(function(){
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ addEventListener('keydown', (event) => {
|
|||||||
} else {
|
} else {
|
||||||
end = target.value.slice(selectionEnd + 1).indexOf(")") + 1;
|
end = target.value.slice(selectionEnd + 1).indexOf(")") + 1;
|
||||||
weight = parseFloat(target.value.slice(selectionEnd + 1, selectionEnd + 1 + end));
|
weight = parseFloat(target.value.slice(selectionEnd + 1, selectionEnd + 1 + end));
|
||||||
|
if (isNaN(weight)) return;
|
||||||
if (event.key == minus) weight -= 0.1;
|
if (event.key == minus) weight -= 0.1;
|
||||||
if (event.key == plus) weight += 0.1;
|
if (event.key == plus) weight += 0.1;
|
||||||
|
|
||||||
@@ -38,4 +39,7 @@ addEventListener('keydown', (event) => {
|
|||||||
target.selectionStart = selectionStart;
|
target.selectionStart = selectionStart;
|
||||||
target.selectionEnd = selectionEnd;
|
target.selectionEnd = selectionEnd;
|
||||||
}
|
}
|
||||||
|
// Since we've modified a Gradio Textbox component manually, we need to simulate an `input` DOM event to ensure its
|
||||||
|
// internal Svelte data binding remains in sync.
|
||||||
|
target.dispatchEvent(new Event("input", { bubbles: true }));
|
||||||
});
|
});
|
||||||
@@ -79,6 +79,8 @@ titles = {
|
|||||||
"Highres. fix": "Use a two step process to partially create an image at smaller resolution, upscale, and then improve details in it without changing composition",
|
"Highres. fix": "Use a two step process to partially create an image at smaller resolution, upscale, and then improve details in it without changing composition",
|
||||||
"Scale latent": "Uscale the image in latent space. Alternative is to produce the full image from latent representation, upscale that, and then move it back to latent space.",
|
"Scale latent": "Uscale the image in latent space. Alternative is to produce the full image from latent representation, upscale that, and then move it back to latent space.",
|
||||||
|
|
||||||
|
"Eta noise seed delta": "If this values is non-zero, it will be added to seed and used to initialize RNG for noises when using samplers with Eta. You can use this to produce even more variation of images, or you can use this to match images of other software if you know what you are doing.",
|
||||||
|
"Do not add watermark to images": "If this option is enabled, watermark will not be added to created images. Warning: if you do not add watermark, you may be behaving in an unethical manner.",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+96
-72
@@ -1,72 +1,97 @@
|
|||||||
// A full size 'lightbox' preview modal shown when left clicking on gallery previews
|
// A full size 'lightbox' preview modal shown when left clicking on gallery previews
|
||||||
|
|
||||||
function closeModal() {
|
function closeModal() {
|
||||||
gradioApp().getElementById("lightboxModal").style.display = "none";
|
gradioApp().getElementById("lightboxModal").style.display = "none";
|
||||||
}
|
}
|
||||||
|
|
||||||
function showModal(event) {
|
function showModal(event) {
|
||||||
const source = event.target || event.srcElement;
|
const source = event.target || event.srcElement;
|
||||||
const modalImage = gradioApp().getElementById("modalImage")
|
const modalImage = gradioApp().getElementById("modalImage")
|
||||||
const lb = gradioApp().getElementById("lightboxModal")
|
const lb = gradioApp().getElementById("lightboxModal")
|
||||||
modalImage.src = source.src
|
modalImage.src = source.src
|
||||||
if (modalImage.style.display === 'none') {
|
if (modalImage.style.display === 'none') {
|
||||||
lb.style.setProperty('background-image', 'url(' + source.src + ')');
|
lb.style.setProperty('background-image', 'url(' + source.src + ')');
|
||||||
}
|
}
|
||||||
lb.style.display = "block";
|
lb.style.display = "block";
|
||||||
lb.focus()
|
lb.focus()
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
}
|
}
|
||||||
|
|
||||||
function negmod(n, m) {
|
function negmod(n, m) {
|
||||||
return ((n % m) + m) % m;
|
return ((n % m) + m) % m;
|
||||||
}
|
}
|
||||||
|
|
||||||
function modalImageSwitch(offset){
|
function updateOnBackgroundChange() {
|
||||||
var allgalleryButtons = gradioApp().querySelectorAll(".gallery-item.transition-all")
|
const modalImage = gradioApp().getElementById("modalImage")
|
||||||
var galleryButtons = []
|
if (modalImage && modalImage.offsetParent) {
|
||||||
allgalleryButtons.forEach(function(elem){
|
let allcurrentButtons = gradioApp().querySelectorAll(".gallery-item.transition-all.\\!ring-2")
|
||||||
if(elem.parentElement.offsetParent){
|
let currentButton = null
|
||||||
galleryButtons.push(elem);
|
allcurrentButtons.forEach(function(elem) {
|
||||||
|
if (elem.parentElement.offsetParent) {
|
||||||
|
currentButton = elem;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
if (modalImage.src != currentButton.children[0].src) {
|
||||||
|
modalImage.src = currentButton.children[0].src;
|
||||||
|
if (modalImage.style.display === 'none') {
|
||||||
|
modal.style.setProperty('background-image', `url(${modalImage.src})`)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
if(galleryButtons.length>1){
|
function modalImageSwitch(offset) {
|
||||||
var allcurrentButtons = gradioApp().querySelectorAll(".gallery-item.transition-all.\\!ring-2")
|
var allgalleryButtons = gradioApp().querySelectorAll(".gallery-item.transition-all")
|
||||||
var currentButton = null
|
var galleryButtons = []
|
||||||
allcurrentButtons.forEach(function(elem){
|
allgalleryButtons.forEach(function(elem) {
|
||||||
if(elem.parentElement.offsetParent){
|
if (elem.parentElement.offsetParent) {
|
||||||
currentButton = elem;
|
galleryButtons.push(elem);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
var result = -1
|
if (galleryButtons.length > 1) {
|
||||||
galleryButtons.forEach(function(v, i){ if(v==currentButton) { result = i } })
|
var allcurrentButtons = gradioApp().querySelectorAll(".gallery-item.transition-all.\\!ring-2")
|
||||||
|
var currentButton = null
|
||||||
|
allcurrentButtons.forEach(function(elem) {
|
||||||
|
if (elem.parentElement.offsetParent) {
|
||||||
|
currentButton = elem;
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
if(result != -1){
|
var result = -1
|
||||||
nextButton = galleryButtons[negmod((result+offset),galleryButtons.length)]
|
galleryButtons.forEach(function(v, i) {
|
||||||
nextButton.click()
|
if (v == currentButton) {
|
||||||
const modalImage = gradioApp().getElementById("modalImage");
|
result = i
|
||||||
const modal = gradioApp().getElementById("lightboxModal");
|
}
|
||||||
modalImage.src = nextButton.children[0].src;
|
})
|
||||||
if (modalImage.style.display === 'none') {
|
|
||||||
modal.style.setProperty('background-image', `url(${modalImage.src})`)
|
if (result != -1) {
|
||||||
|
nextButton = galleryButtons[negmod((result + offset), galleryButtons.length)]
|
||||||
|
nextButton.click()
|
||||||
|
const modalImage = gradioApp().getElementById("modalImage");
|
||||||
|
const modal = gradioApp().getElementById("lightboxModal");
|
||||||
|
modalImage.src = nextButton.children[0].src;
|
||||||
|
if (modalImage.style.display === 'none') {
|
||||||
|
modal.style.setProperty('background-image', `url(${modalImage.src})`)
|
||||||
|
}
|
||||||
|
setTimeout(function() {
|
||||||
|
modal.focus()
|
||||||
|
}, 10)
|
||||||
}
|
}
|
||||||
setTimeout( function(){modal.focus()},10)
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function modalNextImage(event){
|
function modalNextImage(event) {
|
||||||
modalImageSwitch(1)
|
modalImageSwitch(1)
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
}
|
}
|
||||||
|
|
||||||
function modalPrevImage(event){
|
function modalPrevImage(event) {
|
||||||
modalImageSwitch(-1)
|
modalImageSwitch(-1)
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
}
|
}
|
||||||
|
|
||||||
function modalKeyHandler(event){
|
function modalKeyHandler(event) {
|
||||||
switch (event.key) {
|
switch (event.key) {
|
||||||
case "ArrowLeft":
|
case "ArrowLeft":
|
||||||
modalPrevImage(event)
|
modalPrevImage(event)
|
||||||
@@ -80,24 +105,22 @@ function modalKeyHandler(event){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function showGalleryImage(){
|
function showGalleryImage() {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
fullImg_preview = gradioApp().querySelectorAll('img.w-full.object-contain')
|
fullImg_preview = gradioApp().querySelectorAll('img.w-full.object-contain')
|
||||||
|
|
||||||
if(fullImg_preview != null){
|
if (fullImg_preview != null) {
|
||||||
fullImg_preview.forEach(function function_name(e) {
|
fullImg_preview.forEach(function function_name(e) {
|
||||||
if (e.dataset.modded)
|
if (e.dataset.modded)
|
||||||
return;
|
return;
|
||||||
e.dataset.modded = true;
|
e.dataset.modded = true;
|
||||||
if(e && e.parentElement.tagName == 'DIV'){
|
if(e && e.parentElement.tagName == 'DIV'){
|
||||||
|
|
||||||
e.style.cursor='pointer'
|
e.style.cursor='pointer'
|
||||||
|
|
||||||
e.addEventListener('click', function (evt) {
|
e.addEventListener('click', function (evt) {
|
||||||
if(!opts.js_modal_lightbox) return;
|
if(!opts.js_modal_lightbox) return;
|
||||||
modalZoomSet(gradioApp().getElementById('modalImage'), opts.js_modal_lightbox_initially_zoomed)
|
modalZoomSet(gradioApp().getElementById('modalImage'), opts.js_modal_lightbox_initially_zoomed)
|
||||||
showModal(evt)
|
showModal(evt)
|
||||||
},true);
|
}, true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -105,21 +128,21 @@ function showGalleryImage(){
|
|||||||
}, 100);
|
}, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
function modalZoomSet(modalImage, enable){
|
function modalZoomSet(modalImage, enable) {
|
||||||
if( enable ){
|
if (enable) {
|
||||||
modalImage.classList.add('modalImageFullscreen');
|
modalImage.classList.add('modalImageFullscreen');
|
||||||
} else{
|
} else {
|
||||||
modalImage.classList.remove('modalImageFullscreen');
|
modalImage.classList.remove('modalImageFullscreen');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function modalZoomToggle(event){
|
function modalZoomToggle(event) {
|
||||||
modalImage = gradioApp().getElementById("modalImage");
|
modalImage = gradioApp().getElementById("modalImage");
|
||||||
modalZoomSet(modalImage, !modalImage.classList.contains('modalImageFullscreen'))
|
modalZoomSet(modalImage, !modalImage.classList.contains('modalImageFullscreen'))
|
||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
}
|
}
|
||||||
|
|
||||||
function modalTileImageToggle(event){
|
function modalTileImageToggle(event) {
|
||||||
const modalImage = gradioApp().getElementById("modalImage");
|
const modalImage = gradioApp().getElementById("modalImage");
|
||||||
const modal = gradioApp().getElementById("lightboxModal");
|
const modal = gradioApp().getElementById("lightboxModal");
|
||||||
const isTiling = modalImage.style.display === 'none';
|
const isTiling = modalImage.style.display === 'none';
|
||||||
@@ -134,17 +157,18 @@ function modalTileImageToggle(event){
|
|||||||
event.stopPropagation()
|
event.stopPropagation()
|
||||||
}
|
}
|
||||||
|
|
||||||
function galleryImageHandler(e){
|
function galleryImageHandler(e) {
|
||||||
if(e && e.parentElement.tagName == 'BUTTON'){
|
if (e && e.parentElement.tagName == 'BUTTON') {
|
||||||
e.onclick = showGalleryImage;
|
e.onclick = showGalleryImage;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onUiUpdate(function(){
|
onUiUpdate(function() {
|
||||||
fullImg_preview = gradioApp().querySelectorAll('img.w-full')
|
fullImg_preview = gradioApp().querySelectorAll('img.w-full')
|
||||||
if(fullImg_preview != null){
|
if (fullImg_preview != null) {
|
||||||
fullImg_preview.forEach(galleryImageHandler);
|
fullImg_preview.forEach(galleryImageHandler);
|
||||||
}
|
}
|
||||||
|
updateOnBackgroundChange();
|
||||||
})
|
})
|
||||||
|
|
||||||
document.addEventListener("DOMContentLoaded", function() {
|
document.addEventListener("DOMContentLoaded", function() {
|
||||||
@@ -152,13 +176,13 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
const modal = document.createElement('div')
|
const modal = document.createElement('div')
|
||||||
modal.onclick = closeModal;
|
modal.onclick = closeModal;
|
||||||
modal.id = "lightboxModal";
|
modal.id = "lightboxModal";
|
||||||
modal.tabIndex=0
|
modal.tabIndex = 0
|
||||||
modal.addEventListener('keydown', modalKeyHandler, true)
|
modal.addEventListener('keydown', modalKeyHandler, true)
|
||||||
|
|
||||||
const modalControls = document.createElement('div')
|
const modalControls = document.createElement('div')
|
||||||
modalControls.className = 'modalControls gradio-container';
|
modalControls.className = 'modalControls gradio-container';
|
||||||
modal.append(modalControls);
|
modal.append(modalControls);
|
||||||
|
|
||||||
const modalZoom = document.createElement('span')
|
const modalZoom = document.createElement('span')
|
||||||
modalZoom.className = 'modalZoom cursor';
|
modalZoom.className = 'modalZoom cursor';
|
||||||
modalZoom.innerHTML = '⤡'
|
modalZoom.innerHTML = '⤡'
|
||||||
@@ -183,30 +207,30 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
const modalImage = document.createElement('img')
|
const modalImage = document.createElement('img')
|
||||||
modalImage.id = 'modalImage';
|
modalImage.id = 'modalImage';
|
||||||
modalImage.onclick = closeModal;
|
modalImage.onclick = closeModal;
|
||||||
modalImage.tabIndex=0
|
modalImage.tabIndex = 0
|
||||||
modalImage.addEventListener('keydown', modalKeyHandler, true)
|
modalImage.addEventListener('keydown', modalKeyHandler, true)
|
||||||
modal.appendChild(modalImage)
|
modal.appendChild(modalImage)
|
||||||
|
|
||||||
const modalPrev = document.createElement('a')
|
const modalPrev = document.createElement('a')
|
||||||
modalPrev.className = 'modalPrev';
|
modalPrev.className = 'modalPrev';
|
||||||
modalPrev.innerHTML = '❮'
|
modalPrev.innerHTML = '❮'
|
||||||
modalPrev.tabIndex=0
|
modalPrev.tabIndex = 0
|
||||||
modalPrev.addEventListener('click',modalPrevImage,true);
|
modalPrev.addEventListener('click', modalPrevImage, true);
|
||||||
modalPrev.addEventListener('keydown', modalKeyHandler, true)
|
modalPrev.addEventListener('keydown', modalKeyHandler, true)
|
||||||
modal.appendChild(modalPrev)
|
modal.appendChild(modalPrev)
|
||||||
|
|
||||||
const modalNext = document.createElement('a')
|
const modalNext = document.createElement('a')
|
||||||
modalNext.className = 'modalNext';
|
modalNext.className = 'modalNext';
|
||||||
modalNext.innerHTML = '❯'
|
modalNext.innerHTML = '❯'
|
||||||
modalNext.tabIndex=0
|
modalNext.tabIndex = 0
|
||||||
modalNext.addEventListener('click',modalNextImage,true);
|
modalNext.addEventListener('click', modalNextImage, true);
|
||||||
modalNext.addEventListener('keydown', modalKeyHandler, true)
|
modalNext.addEventListener('keydown', modalKeyHandler, true)
|
||||||
|
|
||||||
modal.appendChild(modalNext)
|
modal.appendChild(modalNext)
|
||||||
|
|
||||||
|
|
||||||
gradioApp().getRootNode().appendChild(modal)
|
gradioApp().getRootNode().appendChild(modal)
|
||||||
|
|
||||||
document.body.appendChild(modalFragment);
|
document.body.appendChild(modalFragment);
|
||||||
|
|
||||||
});
|
});
|
||||||
+2
-1
@@ -101,7 +101,8 @@ function create_tab_index_args(tabId, args){
|
|||||||
}
|
}
|
||||||
|
|
||||||
function get_extras_tab_index(){
|
function get_extras_tab_index(){
|
||||||
return create_tab_index_args('mode_extras', arguments)
|
const [,,...args] = [...arguments]
|
||||||
|
return [get_tab_index('mode_extras'), get_tab_index('extras_resize_mode'), ...args]
|
||||||
}
|
}
|
||||||
|
|
||||||
function create_submit_args(args){
|
function create_submit_args(args){
|
||||||
|
|||||||
@@ -104,6 +104,7 @@ def prepare_enviroment():
|
|||||||
args, skip_torch_cuda_test = extract_arg(args, '--skip-torch-cuda-test')
|
args, skip_torch_cuda_test = extract_arg(args, '--skip-torch-cuda-test')
|
||||||
xformers = '--xformers' in args
|
xformers = '--xformers' in args
|
||||||
deepdanbooru = '--deepdanbooru' in args
|
deepdanbooru = '--deepdanbooru' in args
|
||||||
|
ngrok = '--ngrok' in args
|
||||||
|
|
||||||
try:
|
try:
|
||||||
commit = run(f"{git} rev-parse HEAD").strip()
|
commit = run(f"{git} rev-parse HEAD").strip()
|
||||||
@@ -127,13 +128,16 @@ def prepare_enviroment():
|
|||||||
|
|
||||||
if not is_installed("xformers") and xformers and platform.python_version().startswith("3.10"):
|
if not is_installed("xformers") and xformers and platform.python_version().startswith("3.10"):
|
||||||
if platform.system() == "Windows":
|
if platform.system() == "Windows":
|
||||||
run_pip("install https://github.com/C43H66N12O12S2/stable-diffusion-webui/releases/download/a/xformers-0.0.14.dev0-cp310-cp310-win_amd64.whl", "xformers")
|
run_pip("install https://github.com/C43H66N12O12S2/stable-diffusion-webui/releases/download/c/xformers-0.0.14.dev0-cp310-cp310-win_amd64.whl", "xformers")
|
||||||
elif platform.system() == "Linux":
|
elif platform.system() == "Linux":
|
||||||
run_pip("install xformers", "xformers")
|
run_pip("install xformers", "xformers")
|
||||||
|
|
||||||
if not is_installed("deepdanbooru") and deepdanbooru:
|
if not is_installed("deepdanbooru") and deepdanbooru:
|
||||||
run_pip("install git+https://github.com/KichangKim/DeepDanbooru.git@edf73df4cdaeea2cf00e9ac08bd8a9026b7a7b26#egg=deepdanbooru[tensorflow] tensorflow==2.10.0 tensorflow-io==0.27.0", "deepdanbooru")
|
run_pip("install git+https://github.com/KichangKim/DeepDanbooru.git@edf73df4cdaeea2cf00e9ac08bd8a9026b7a7b26#egg=deepdanbooru[tensorflow] tensorflow==2.10.0 tensorflow-io==0.27.0", "deepdanbooru")
|
||||||
|
|
||||||
|
if not is_installed("pyngrok") and ngrok:
|
||||||
|
run_pip("install pyngrok", "ngrok")
|
||||||
|
|
||||||
os.makedirs(dir_repos, exist_ok=True)
|
os.makedirs(dir_repos, exist_ok=True)
|
||||||
|
|
||||||
git_clone("https://github.com/CompVis/stable-diffusion.git", repo_dir('stable-diffusion'), "Stable Diffusion", stable_diffusion_commit_hash)
|
git_clone("https://github.com/CompVis/stable-diffusion.git", repo_dir('stable-diffusion'), "Stable Diffusion", stable_diffusion_commit_hash)
|
||||||
|
|||||||
+79
-20
@@ -1,21 +1,75 @@
|
|||||||
import os.path
|
import os.path
|
||||||
from concurrent.futures import ProcessPoolExecutor
|
from concurrent.futures import ProcessPoolExecutor
|
||||||
from multiprocessing import get_context
|
import multiprocessing
|
||||||
|
import time
|
||||||
|
|
||||||
|
def get_deepbooru_tags(pil_image):
|
||||||
|
"""
|
||||||
|
This method is for running only one image at a time for simple use. Used to the img2img interrogate.
|
||||||
|
"""
|
||||||
|
from modules import shared # prevents circular reference
|
||||||
|
create_deepbooru_process(shared.opts.interrogate_deepbooru_score_threshold, shared.opts.deepbooru_sort_alpha)
|
||||||
|
shared.deepbooru_process_return["value"] = -1
|
||||||
|
shared.deepbooru_process_queue.put(pil_image)
|
||||||
|
while shared.deepbooru_process_return["value"] == -1:
|
||||||
|
time.sleep(0.2)
|
||||||
|
tags = shared.deepbooru_process_return["value"]
|
||||||
|
release_process()
|
||||||
|
return tags
|
||||||
|
|
||||||
|
|
||||||
def _load_tf_and_return_tags(pil_image, threshold):
|
def deepbooru_process(queue, deepbooru_process_return, threshold, alpha_sort):
|
||||||
|
model, tags = get_deepbooru_tags_model()
|
||||||
|
while True: # while process is running, keep monitoring queue for new image
|
||||||
|
pil_image = queue.get()
|
||||||
|
if pil_image == "QUIT":
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
deepbooru_process_return["value"] = get_deepbooru_tags_from_model(model, tags, pil_image, threshold, alpha_sort)
|
||||||
|
|
||||||
|
|
||||||
|
def create_deepbooru_process(threshold, alpha_sort):
|
||||||
|
"""
|
||||||
|
Creates deepbooru process. A queue is created to send images into the process. This enables multiple images
|
||||||
|
to be processed in a row without reloading the model or creating a new process. To return the data, a shared
|
||||||
|
dictionary is created to hold the tags created. To wait for tags to be returned, a value of -1 is assigned
|
||||||
|
to the dictionary and the method adding the image to the queue should wait for this value to be updated with
|
||||||
|
the tags.
|
||||||
|
"""
|
||||||
|
from modules import shared # prevents circular reference
|
||||||
|
shared.deepbooru_process_manager = multiprocessing.Manager()
|
||||||
|
shared.deepbooru_process_queue = shared.deepbooru_process_manager.Queue()
|
||||||
|
shared.deepbooru_process_return = shared.deepbooru_process_manager.dict()
|
||||||
|
shared.deepbooru_process_return["value"] = -1
|
||||||
|
shared.deepbooru_process = multiprocessing.Process(target=deepbooru_process, args=(shared.deepbooru_process_queue, shared.deepbooru_process_return, threshold, alpha_sort))
|
||||||
|
shared.deepbooru_process.start()
|
||||||
|
|
||||||
|
|
||||||
|
def release_process():
|
||||||
|
"""
|
||||||
|
Stops the deepbooru process to return used memory
|
||||||
|
"""
|
||||||
|
from modules import shared # prevents circular reference
|
||||||
|
shared.deepbooru_process_queue.put("QUIT")
|
||||||
|
shared.deepbooru_process.join()
|
||||||
|
shared.deepbooru_process_queue = None
|
||||||
|
shared.deepbooru_process = None
|
||||||
|
shared.deepbooru_process_return = None
|
||||||
|
shared.deepbooru_process_manager = None
|
||||||
|
|
||||||
|
def get_deepbooru_tags_model():
|
||||||
import deepdanbooru as dd
|
import deepdanbooru as dd
|
||||||
import tensorflow as tf
|
import tensorflow as tf
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
this_folder = os.path.dirname(__file__)
|
this_folder = os.path.dirname(__file__)
|
||||||
model_path = os.path.abspath(os.path.join(this_folder, '..', 'models', 'deepbooru'))
|
model_path = os.path.abspath(os.path.join(this_folder, '..', 'models', 'deepbooru'))
|
||||||
if not os.path.exists(os.path.join(model_path, 'project.json')):
|
if not os.path.exists(os.path.join(model_path, 'project.json')):
|
||||||
# there is no point importing these every time
|
# there is no point importing these every time
|
||||||
import zipfile
|
import zipfile
|
||||||
from basicsr.utils.download_util import load_file_from_url
|
from basicsr.utils.download_util import load_file_from_url
|
||||||
load_file_from_url(r"https://github.com/KichangKim/DeepDanbooru/releases/download/v3-20211112-sgd-e28/deepdanbooru-v3-20211112-sgd-e28.zip",
|
load_file_from_url(
|
||||||
model_path)
|
r"https://github.com/KichangKim/DeepDanbooru/releases/download/v3-20211112-sgd-e28/deepdanbooru-v3-20211112-sgd-e28.zip",
|
||||||
|
model_path)
|
||||||
with zipfile.ZipFile(os.path.join(model_path, "deepdanbooru-v3-20211112-sgd-e28.zip"), "r") as zip_ref:
|
with zipfile.ZipFile(os.path.join(model_path, "deepdanbooru-v3-20211112-sgd-e28.zip"), "r") as zip_ref:
|
||||||
zip_ref.extractall(model_path)
|
zip_ref.extractall(model_path)
|
||||||
os.remove(os.path.join(model_path, "deepdanbooru-v3-20211112-sgd-e28.zip"))
|
os.remove(os.path.join(model_path, "deepdanbooru-v3-20211112-sgd-e28.zip"))
|
||||||
@@ -24,7 +78,13 @@ def _load_tf_and_return_tags(pil_image, threshold):
|
|||||||
model = dd.project.load_model_from_project(
|
model = dd.project.load_model_from_project(
|
||||||
model_path, compile_model=True
|
model_path, compile_model=True
|
||||||
)
|
)
|
||||||
|
return model, tags
|
||||||
|
|
||||||
|
|
||||||
|
def get_deepbooru_tags_from_model(model, tags, pil_image, threshold, alpha_sort):
|
||||||
|
import deepdanbooru as dd
|
||||||
|
import tensorflow as tf
|
||||||
|
import numpy as np
|
||||||
width = model.input_shape[2]
|
width = model.input_shape[2]
|
||||||
height = model.input_shape[1]
|
height = model.input_shape[1]
|
||||||
image = np.array(pil_image)
|
image = np.array(pil_image)
|
||||||
@@ -46,28 +106,27 @@ def _load_tf_and_return_tags(pil_image, threshold):
|
|||||||
|
|
||||||
for i, tag in enumerate(tags):
|
for i, tag in enumerate(tags):
|
||||||
result_dict[tag] = y[i]
|
result_dict[tag] = y[i]
|
||||||
result_tags_out = []
|
|
||||||
|
unsorted_tags_in_theshold = []
|
||||||
result_tags_print = []
|
result_tags_print = []
|
||||||
for tag in tags:
|
for tag in tags:
|
||||||
if result_dict[tag] >= threshold:
|
if result_dict[tag] >= threshold:
|
||||||
if tag.startswith("rating:"):
|
if tag.startswith("rating:"):
|
||||||
continue
|
continue
|
||||||
result_tags_out.append(tag)
|
unsorted_tags_in_theshold.append((result_dict[tag], tag))
|
||||||
result_tags_print.append(f'{result_dict[tag]} {tag}')
|
result_tags_print.append(f'{result_dict[tag]} {tag}')
|
||||||
|
|
||||||
|
# sort tags
|
||||||
|
result_tags_out = []
|
||||||
|
sort_ndx = 0
|
||||||
|
if alpha_sort:
|
||||||
|
sort_ndx = 1
|
||||||
|
|
||||||
|
# sort by reverse by likelihood and normal for alpha
|
||||||
|
unsorted_tags_in_theshold.sort(key=lambda y: y[sort_ndx], reverse=(not alpha_sort))
|
||||||
|
for weight, tag in unsorted_tags_in_theshold:
|
||||||
|
result_tags_out.append(tag)
|
||||||
|
|
||||||
print('\n'.join(sorted(result_tags_print, reverse=True)))
|
print('\n'.join(sorted(result_tags_print, reverse=True)))
|
||||||
|
|
||||||
return ', '.join(result_tags_out).replace('_', ' ').replace(':', ' ')
|
return ', '.join(result_tags_out).replace('_', ' ').replace(':', ' ')
|
||||||
|
|
||||||
|
|
||||||
def subprocess_init_no_cuda():
|
|
||||||
import os
|
|
||||||
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
|
|
||||||
|
|
||||||
|
|
||||||
def get_deepbooru_tags(pil_image, threshold=0.5):
|
|
||||||
context = get_context('spawn')
|
|
||||||
with ProcessPoolExecutor(initializer=subprocess_init_no_cuda, mp_context=context) as executor:
|
|
||||||
f = executor.submit(_load_tf_and_return_tags, pil_image, threshold, )
|
|
||||||
ret = f.result() # will rethrow any exceptions
|
|
||||||
return ret
|
|
||||||
+5
-1
@@ -36,6 +36,7 @@ errors.run(enable_tf32, "Enabling TF32")
|
|||||||
|
|
||||||
device = device_gfpgan = device_bsrgan = device_esrgan = device_scunet = device_codeformer = get_optimal_device()
|
device = device_gfpgan = device_bsrgan = device_esrgan = device_scunet = device_codeformer = get_optimal_device()
|
||||||
dtype = torch.float16
|
dtype = torch.float16
|
||||||
|
dtype_vae = torch.float16
|
||||||
|
|
||||||
def randn(seed, shape):
|
def randn(seed, shape):
|
||||||
# Pytorch currently doesn't handle setting randomness correctly when the metal backend is used.
|
# Pytorch currently doesn't handle setting randomness correctly when the metal backend is used.
|
||||||
@@ -59,9 +60,12 @@ def randn_without_seed(shape):
|
|||||||
return torch.randn(shape, device=device)
|
return torch.randn(shape, device=device)
|
||||||
|
|
||||||
|
|
||||||
def autocast():
|
def autocast(disable=False):
|
||||||
from modules import shared
|
from modules import shared
|
||||||
|
|
||||||
|
if disable:
|
||||||
|
return contextlib.nullcontext()
|
||||||
|
|
||||||
if dtype == torch.float32 or shared.cmd_opts.precision == "full":
|
if dtype == torch.float32 or shared.cmd_opts.precision == "full":
|
||||||
return contextlib.nullcontext()
|
return contextlib.nullcontext()
|
||||||
|
|
||||||
|
|||||||
+16
-6
@@ -1,3 +1,4 @@
|
|||||||
|
import math
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
@@ -19,7 +20,7 @@ import gradio as gr
|
|||||||
cached_images = {}
|
cached_images = {}
|
||||||
|
|
||||||
|
|
||||||
def run_extras(extras_mode, image, image_folder, gfpgan_visibility, codeformer_visibility, codeformer_weight, upscaling_resize, extras_upscaler_1, extras_upscaler_2, extras_upscaler_2_visibility):
|
def run_extras(extras_mode, resize_mode, image, image_folder, gfpgan_visibility, codeformer_visibility, codeformer_weight, upscaling_resize, upscaling_resize_w, upscaling_resize_h, upscaling_crop, extras_upscaler_1, extras_upscaler_2, extras_upscaler_2_visibility):
|
||||||
devices.torch_gc()
|
devices.torch_gc()
|
||||||
|
|
||||||
imageArr = []
|
imageArr = []
|
||||||
@@ -29,7 +30,7 @@ def run_extras(extras_mode, image, image_folder, gfpgan_visibility, codeformer_v
|
|||||||
if extras_mode == 1:
|
if extras_mode == 1:
|
||||||
#convert file to pillow image
|
#convert file to pillow image
|
||||||
for img in image_folder:
|
for img in image_folder:
|
||||||
image = Image.fromarray(np.array(Image.open(img)))
|
image = Image.open(img)
|
||||||
imageArr.append(image)
|
imageArr.append(image)
|
||||||
imageNameArr.append(os.path.splitext(img.orig_name)[0])
|
imageNameArr.append(os.path.splitext(img.orig_name)[0])
|
||||||
else:
|
else:
|
||||||
@@ -67,8 +68,13 @@ def run_extras(extras_mode, image, image_folder, gfpgan_visibility, codeformer_v
|
|||||||
info += f"CodeFormer w: {round(codeformer_weight, 2)}, CodeFormer visibility:{round(codeformer_visibility, 2)}\n"
|
info += f"CodeFormer w: {round(codeformer_weight, 2)}, CodeFormer visibility:{round(codeformer_visibility, 2)}\n"
|
||||||
image = res
|
image = res
|
||||||
|
|
||||||
|
if resize_mode == 1:
|
||||||
|
upscaling_resize = max(upscaling_resize_w/image.width, upscaling_resize_h/image.height)
|
||||||
|
crop_info = " (crop)" if upscaling_crop else ""
|
||||||
|
info += f"Resize to: {upscaling_resize_w:g}x{upscaling_resize_h:g}{crop_info}\n"
|
||||||
|
|
||||||
if upscaling_resize != 1.0:
|
if upscaling_resize != 1.0:
|
||||||
def upscale(image, scaler_index, resize):
|
def upscale(image, scaler_index, resize, mode, resize_w, resize_h, crop):
|
||||||
small = image.crop((image.width // 2, image.height // 2, image.width // 2 + 10, image.height // 2 + 10))
|
small = image.crop((image.width // 2, image.height // 2, image.width // 2 + 10, image.height // 2 + 10))
|
||||||
pixels = tuple(np.array(small).flatten().tolist())
|
pixels = tuple(np.array(small).flatten().tolist())
|
||||||
key = (resize, scaler_index, image.width, image.height, gfpgan_visibility, codeformer_visibility, codeformer_weight) + pixels
|
key = (resize, scaler_index, image.width, image.height, gfpgan_visibility, codeformer_visibility, codeformer_weight) + pixels
|
||||||
@@ -77,15 +83,19 @@ def run_extras(extras_mode, image, image_folder, gfpgan_visibility, codeformer_v
|
|||||||
if c is None:
|
if c is None:
|
||||||
upscaler = shared.sd_upscalers[scaler_index]
|
upscaler = shared.sd_upscalers[scaler_index]
|
||||||
c = upscaler.scaler.upscale(image, resize, upscaler.data_path)
|
c = upscaler.scaler.upscale(image, resize, upscaler.data_path)
|
||||||
|
if mode == 1 and crop:
|
||||||
|
cropped = Image.new("RGB", (resize_w, resize_h))
|
||||||
|
cropped.paste(c, box=(resize_w // 2 - c.width // 2, resize_h // 2 - c.height // 2))
|
||||||
|
c = cropped
|
||||||
cached_images[key] = c
|
cached_images[key] = c
|
||||||
|
|
||||||
return c
|
return c
|
||||||
|
|
||||||
info += f"Upscale: {round(upscaling_resize, 3)}, model:{shared.sd_upscalers[extras_upscaler_1].name}\n"
|
info += f"Upscale: {round(upscaling_resize, 3)}, model:{shared.sd_upscalers[extras_upscaler_1].name}\n"
|
||||||
res = upscale(image, extras_upscaler_1, upscaling_resize)
|
res = upscale(image, extras_upscaler_1, upscaling_resize, resize_mode, upscaling_resize_w, upscaling_resize_h, upscaling_crop)
|
||||||
|
|
||||||
if extras_upscaler_2 != 0 and extras_upscaler_2_visibility > 0:
|
if extras_upscaler_2 != 0 and extras_upscaler_2_visibility > 0:
|
||||||
res2 = upscale(image, extras_upscaler_2, upscaling_resize)
|
res2 = upscale(image, extras_upscaler_2, upscaling_resize, resize_mode, upscaling_resize_w, upscaling_resize_h, upscaling_crop)
|
||||||
info += f"Upscale: {round(upscaling_resize, 3)}, visibility: {round(extras_upscaler_2_visibility, 3)}, model:{shared.sd_upscalers[extras_upscaler_2].name}\n"
|
info += f"Upscale: {round(upscaling_resize, 3)}, visibility: {round(extras_upscaler_2_visibility, 3)}, model:{shared.sd_upscalers[extras_upscaler_2].name}\n"
|
||||||
res = Image.blend(res, res2, extras_upscaler_2_visibility)
|
res = Image.blend(res, res2, extras_upscaler_2_visibility)
|
||||||
|
|
||||||
@@ -190,7 +200,7 @@ def run_modelmerger(primary_model_name, secondary_model_name, interp_method, int
|
|||||||
theta_0[key] = theta_func(theta_0[key], theta_1[key], (float(1.0) - interp_amount)) # Need to reverse the interp_amount to match the desired mix ration in the merged checkpoint
|
theta_0[key] = theta_func(theta_0[key], theta_1[key], (float(1.0) - interp_amount)) # Need to reverse the interp_amount to match the desired mix ration in the merged checkpoint
|
||||||
if save_as_half:
|
if save_as_half:
|
||||||
theta_0[key] = theta_0[key].half()
|
theta_0[key] = theta_0[key].half()
|
||||||
|
|
||||||
for key in theta_1.keys():
|
for key in theta_1.keys():
|
||||||
if 'model' in key and key not in theta_0:
|
if 'model' in key and key not in theta_0:
|
||||||
theta_0[key] = theta_1[key]
|
theta_0[key] = theta_1[key]
|
||||||
|
|||||||
@@ -1,98 +0,0 @@
|
|||||||
import glob
|
|
||||||
import os
|
|
||||||
import sys
|
|
||||||
import traceback
|
|
||||||
|
|
||||||
import torch
|
|
||||||
|
|
||||||
from ldm.util import default
|
|
||||||
from modules import devices, shared
|
|
||||||
import torch
|
|
||||||
from torch import einsum
|
|
||||||
from einops import rearrange, repeat
|
|
||||||
|
|
||||||
|
|
||||||
class HypernetworkModule(torch.nn.Module):
|
|
||||||
def __init__(self, dim, state_dict):
|
|
||||||
super().__init__()
|
|
||||||
|
|
||||||
self.linear1 = torch.nn.Linear(dim, dim * 2)
|
|
||||||
self.linear2 = torch.nn.Linear(dim * 2, dim)
|
|
||||||
|
|
||||||
self.load_state_dict(state_dict, strict=True)
|
|
||||||
self.to(devices.device)
|
|
||||||
|
|
||||||
def forward(self, x):
|
|
||||||
return x + (self.linear2(self.linear1(x)))
|
|
||||||
|
|
||||||
|
|
||||||
class Hypernetwork:
|
|
||||||
filename = None
|
|
||||||
name = None
|
|
||||||
|
|
||||||
def __init__(self, filename):
|
|
||||||
self.filename = filename
|
|
||||||
self.name = os.path.splitext(os.path.basename(filename))[0]
|
|
||||||
self.layers = {}
|
|
||||||
|
|
||||||
state_dict = torch.load(filename, map_location='cpu')
|
|
||||||
for size, sd in state_dict.items():
|
|
||||||
self.layers[size] = (HypernetworkModule(size, sd[0]), HypernetworkModule(size, sd[1]))
|
|
||||||
|
|
||||||
|
|
||||||
def list_hypernetworks(path):
|
|
||||||
res = {}
|
|
||||||
for filename in glob.iglob(os.path.join(path, '**/*.pt'), recursive=True):
|
|
||||||
name = os.path.splitext(os.path.basename(filename))[0]
|
|
||||||
res[name] = filename
|
|
||||||
return res
|
|
||||||
|
|
||||||
|
|
||||||
def load_hypernetwork(filename):
|
|
||||||
path = shared.hypernetworks.get(filename, None)
|
|
||||||
if path is not None:
|
|
||||||
print(f"Loading hypernetwork {filename}")
|
|
||||||
try:
|
|
||||||
shared.loaded_hypernetwork = Hypernetwork(path)
|
|
||||||
except Exception:
|
|
||||||
print(f"Error loading hypernetwork {path}", file=sys.stderr)
|
|
||||||
print(traceback.format_exc(), file=sys.stderr)
|
|
||||||
else:
|
|
||||||
if shared.loaded_hypernetwork is not None:
|
|
||||||
print(f"Unloading hypernetwork")
|
|
||||||
|
|
||||||
shared.loaded_hypernetwork = None
|
|
||||||
|
|
||||||
|
|
||||||
def attention_CrossAttention_forward(self, x, context=None, mask=None):
|
|
||||||
h = self.heads
|
|
||||||
|
|
||||||
q = self.to_q(x)
|
|
||||||
context = default(context, x)
|
|
||||||
|
|
||||||
hypernetwork = shared.loaded_hypernetwork
|
|
||||||
hypernetwork_layers = (hypernetwork.layers if hypernetwork is not None else {}).get(context.shape[2], None)
|
|
||||||
|
|
||||||
if hypernetwork_layers is not None:
|
|
||||||
k = self.to_k(hypernetwork_layers[0](context))
|
|
||||||
v = self.to_v(hypernetwork_layers[1](context))
|
|
||||||
else:
|
|
||||||
k = self.to_k(context)
|
|
||||||
v = self.to_v(context)
|
|
||||||
|
|
||||||
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v))
|
|
||||||
|
|
||||||
sim = einsum('b i d, b j d -> b i j', q, k) * self.scale
|
|
||||||
|
|
||||||
if mask is not None:
|
|
||||||
mask = rearrange(mask, 'b ... -> b (...)')
|
|
||||||
max_neg_value = -torch.finfo(sim.dtype).max
|
|
||||||
mask = repeat(mask, 'b j -> (b h) () j', h=h)
|
|
||||||
sim.masked_fill_(~mask, max_neg_value)
|
|
||||||
|
|
||||||
# attention, what we cannot get enough of
|
|
||||||
attn = sim.softmax(dim=-1)
|
|
||||||
|
|
||||||
out = einsum('b i j, b j d -> b i d', attn, v)
|
|
||||||
out = rearrange(out, '(b h) n d -> b n (h d)', h=h)
|
|
||||||
return self.to_out(out)
|
|
||||||
@@ -0,0 +1,316 @@
|
|||||||
|
import datetime
|
||||||
|
import glob
|
||||||
|
import html
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
import tqdm
|
||||||
|
|
||||||
|
import torch
|
||||||
|
|
||||||
|
from ldm.util import default
|
||||||
|
from modules import devices, shared, processing, sd_models
|
||||||
|
import torch
|
||||||
|
from torch import einsum
|
||||||
|
from einops import rearrange, repeat
|
||||||
|
import modules.textual_inversion.dataset
|
||||||
|
from modules.textual_inversion.learn_schedule import LearnSchedule
|
||||||
|
|
||||||
|
|
||||||
|
class HypernetworkModule(torch.nn.Module):
|
||||||
|
def __init__(self, dim, state_dict=None):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.linear1 = torch.nn.Linear(dim, dim * 2)
|
||||||
|
self.linear2 = torch.nn.Linear(dim * 2, dim)
|
||||||
|
|
||||||
|
if state_dict is not None:
|
||||||
|
self.load_state_dict(state_dict, strict=True)
|
||||||
|
else:
|
||||||
|
|
||||||
|
self.linear1.weight.data.normal_(mean=0.0, std=0.01)
|
||||||
|
self.linear1.bias.data.zero_()
|
||||||
|
self.linear2.weight.data.normal_(mean=0.0, std=0.01)
|
||||||
|
self.linear2.bias.data.zero_()
|
||||||
|
|
||||||
|
self.to(devices.device)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
return x + (self.linear2(self.linear1(x)))
|
||||||
|
|
||||||
|
|
||||||
|
class Hypernetwork:
|
||||||
|
filename = None
|
||||||
|
name = None
|
||||||
|
|
||||||
|
def __init__(self, name=None, enable_sizes=None):
|
||||||
|
self.filename = None
|
||||||
|
self.name = name
|
||||||
|
self.layers = {}
|
||||||
|
self.step = 0
|
||||||
|
self.sd_checkpoint = None
|
||||||
|
self.sd_checkpoint_name = None
|
||||||
|
|
||||||
|
for size in enable_sizes or []:
|
||||||
|
self.layers[size] = (HypernetworkModule(size), HypernetworkModule(size))
|
||||||
|
|
||||||
|
def weights(self):
|
||||||
|
res = []
|
||||||
|
|
||||||
|
for k, layers in self.layers.items():
|
||||||
|
for layer in layers:
|
||||||
|
layer.train()
|
||||||
|
res += [layer.linear1.weight, layer.linear1.bias, layer.linear2.weight, layer.linear2.bias]
|
||||||
|
|
||||||
|
return res
|
||||||
|
|
||||||
|
def save(self, filename):
|
||||||
|
state_dict = {}
|
||||||
|
|
||||||
|
for k, v in self.layers.items():
|
||||||
|
state_dict[k] = (v[0].state_dict(), v[1].state_dict())
|
||||||
|
|
||||||
|
state_dict['step'] = self.step
|
||||||
|
state_dict['name'] = self.name
|
||||||
|
state_dict['sd_checkpoint'] = self.sd_checkpoint
|
||||||
|
state_dict['sd_checkpoint_name'] = self.sd_checkpoint_name
|
||||||
|
|
||||||
|
torch.save(state_dict, filename)
|
||||||
|
|
||||||
|
def load(self, filename):
|
||||||
|
self.filename = filename
|
||||||
|
if self.name is None:
|
||||||
|
self.name = os.path.splitext(os.path.basename(filename))[0]
|
||||||
|
|
||||||
|
state_dict = torch.load(filename, map_location='cpu')
|
||||||
|
|
||||||
|
for size, sd in state_dict.items():
|
||||||
|
if type(size) == int:
|
||||||
|
self.layers[size] = (HypernetworkModule(size, sd[0]), HypernetworkModule(size, sd[1]))
|
||||||
|
|
||||||
|
self.name = state_dict.get('name', self.name)
|
||||||
|
self.step = state_dict.get('step', 0)
|
||||||
|
self.sd_checkpoint = state_dict.get('sd_checkpoint', None)
|
||||||
|
self.sd_checkpoint_name = state_dict.get('sd_checkpoint_name', None)
|
||||||
|
|
||||||
|
|
||||||
|
def list_hypernetworks(path):
|
||||||
|
res = {}
|
||||||
|
for filename in glob.iglob(os.path.join(path, '**/*.pt'), recursive=True):
|
||||||
|
name = os.path.splitext(os.path.basename(filename))[0]
|
||||||
|
res[name] = filename
|
||||||
|
return res
|
||||||
|
|
||||||
|
|
||||||
|
def load_hypernetwork(filename):
|
||||||
|
path = shared.hypernetworks.get(filename, None)
|
||||||
|
if path is not None:
|
||||||
|
print(f"Loading hypernetwork {filename}")
|
||||||
|
try:
|
||||||
|
shared.loaded_hypernetwork = Hypernetwork()
|
||||||
|
shared.loaded_hypernetwork.load(path)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
print(f"Error loading hypernetwork {path}", file=sys.stderr)
|
||||||
|
print(traceback.format_exc(), file=sys.stderr)
|
||||||
|
else:
|
||||||
|
if shared.loaded_hypernetwork is not None:
|
||||||
|
print(f"Unloading hypernetwork")
|
||||||
|
|
||||||
|
shared.loaded_hypernetwork = None
|
||||||
|
|
||||||
|
|
||||||
|
def find_closest_hypernetwork_name(search: str):
|
||||||
|
if not search:
|
||||||
|
return None
|
||||||
|
search = search.lower()
|
||||||
|
applicable = [name for name in shared.hypernetworks if search in name.lower()]
|
||||||
|
if not applicable:
|
||||||
|
return None
|
||||||
|
applicable = sorted(applicable, key=lambda name: len(name))
|
||||||
|
return applicable[0]
|
||||||
|
|
||||||
|
|
||||||
|
def apply_hypernetwork(hypernetwork, context, layer=None):
|
||||||
|
hypernetwork_layers = (hypernetwork.layers if hypernetwork is not None else {}).get(context.shape[2], None)
|
||||||
|
|
||||||
|
if hypernetwork_layers is None:
|
||||||
|
return context, context
|
||||||
|
|
||||||
|
if layer is not None:
|
||||||
|
layer.hyper_k = hypernetwork_layers[0]
|
||||||
|
layer.hyper_v = hypernetwork_layers[1]
|
||||||
|
|
||||||
|
context_k = hypernetwork_layers[0](context)
|
||||||
|
context_v = hypernetwork_layers[1](context)
|
||||||
|
return context_k, context_v
|
||||||
|
|
||||||
|
|
||||||
|
def attention_CrossAttention_forward(self, x, context=None, mask=None):
|
||||||
|
h = self.heads
|
||||||
|
|
||||||
|
q = self.to_q(x)
|
||||||
|
context = default(context, x)
|
||||||
|
|
||||||
|
context_k, context_v = apply_hypernetwork(shared.loaded_hypernetwork, context, self)
|
||||||
|
k = self.to_k(context_k)
|
||||||
|
v = self.to_v(context_v)
|
||||||
|
|
||||||
|
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v))
|
||||||
|
|
||||||
|
sim = einsum('b i d, b j d -> b i j', q, k) * self.scale
|
||||||
|
|
||||||
|
if mask is not None:
|
||||||
|
mask = rearrange(mask, 'b ... -> b (...)')
|
||||||
|
max_neg_value = -torch.finfo(sim.dtype).max
|
||||||
|
mask = repeat(mask, 'b j -> (b h) () j', h=h)
|
||||||
|
sim.masked_fill_(~mask, max_neg_value)
|
||||||
|
|
||||||
|
# attention, what we cannot get enough of
|
||||||
|
attn = sim.softmax(dim=-1)
|
||||||
|
|
||||||
|
out = einsum('b i j, b j d -> b i d', attn, v)
|
||||||
|
out = rearrange(out, '(b h) n d -> b n (h d)', h=h)
|
||||||
|
return self.to_out(out)
|
||||||
|
|
||||||
|
|
||||||
|
def train_hypernetwork(hypernetwork_name, learn_rate, data_root, log_directory, steps, create_image_every, save_hypernetwork_every, template_file, preview_image_prompt):
|
||||||
|
assert hypernetwork_name, 'hypernetwork not selected'
|
||||||
|
|
||||||
|
path = shared.hypernetworks.get(hypernetwork_name, None)
|
||||||
|
shared.loaded_hypernetwork = Hypernetwork()
|
||||||
|
shared.loaded_hypernetwork.load(path)
|
||||||
|
|
||||||
|
shared.state.textinfo = "Initializing hypernetwork training..."
|
||||||
|
shared.state.job_count = steps
|
||||||
|
|
||||||
|
filename = os.path.join(shared.cmd_opts.hypernetwork_dir, f'{hypernetwork_name}.pt')
|
||||||
|
|
||||||
|
log_directory = os.path.join(log_directory, datetime.datetime.now().strftime("%Y-%m-%d"), hypernetwork_name)
|
||||||
|
unload = shared.opts.unload_models_when_training
|
||||||
|
|
||||||
|
if save_hypernetwork_every > 0:
|
||||||
|
hypernetwork_dir = os.path.join(log_directory, "hypernetworks")
|
||||||
|
os.makedirs(hypernetwork_dir, exist_ok=True)
|
||||||
|
else:
|
||||||
|
hypernetwork_dir = None
|
||||||
|
|
||||||
|
if create_image_every > 0:
|
||||||
|
images_dir = os.path.join(log_directory, "images")
|
||||||
|
os.makedirs(images_dir, exist_ok=True)
|
||||||
|
else:
|
||||||
|
images_dir = None
|
||||||
|
|
||||||
|
shared.state.textinfo = f"Preparing dataset from {html.escape(data_root)}..."
|
||||||
|
with torch.autocast("cuda"):
|
||||||
|
ds = modules.textual_inversion.dataset.PersonalizedBase(data_root=data_root, width=512, height=512, repeats=1, placeholder_token=hypernetwork_name, model=shared.sd_model, device=devices.device, template_file=template_file, include_cond=True)
|
||||||
|
|
||||||
|
if unload:
|
||||||
|
shared.sd_model.cond_stage_model.to(devices.cpu)
|
||||||
|
shared.sd_model.first_stage_model.to(devices.cpu)
|
||||||
|
|
||||||
|
hypernetwork = shared.loaded_hypernetwork
|
||||||
|
weights = hypernetwork.weights()
|
||||||
|
for weight in weights:
|
||||||
|
weight.requires_grad = True
|
||||||
|
|
||||||
|
losses = torch.zeros((32,))
|
||||||
|
|
||||||
|
last_saved_file = "<none>"
|
||||||
|
last_saved_image = "<none>"
|
||||||
|
|
||||||
|
ititial_step = hypernetwork.step or 0
|
||||||
|
if ititial_step > steps:
|
||||||
|
return hypernetwork, filename
|
||||||
|
|
||||||
|
schedules = iter(LearnSchedule(learn_rate, steps, ititial_step))
|
||||||
|
(learn_rate, end_step) = next(schedules)
|
||||||
|
print(f'Training at rate of {learn_rate} until step {end_step}')
|
||||||
|
|
||||||
|
optimizer = torch.optim.AdamW(weights, lr=learn_rate)
|
||||||
|
|
||||||
|
pbar = tqdm.tqdm(enumerate(ds), total=steps - ititial_step)
|
||||||
|
for i, (x, text, cond) in pbar:
|
||||||
|
hypernetwork.step = i + ititial_step
|
||||||
|
|
||||||
|
if hypernetwork.step > end_step:
|
||||||
|
try:
|
||||||
|
(learn_rate, end_step) = next(schedules)
|
||||||
|
except Exception:
|
||||||
|
break
|
||||||
|
tqdm.tqdm.write(f'Training at rate of {learn_rate} until step {end_step}')
|
||||||
|
for pg in optimizer.param_groups:
|
||||||
|
pg['lr'] = learn_rate
|
||||||
|
|
||||||
|
if shared.state.interrupted:
|
||||||
|
break
|
||||||
|
|
||||||
|
with torch.autocast("cuda"):
|
||||||
|
cond = cond.to(devices.device)
|
||||||
|
x = x.to(devices.device)
|
||||||
|
loss = shared.sd_model(x.unsqueeze(0), cond)[0]
|
||||||
|
del x
|
||||||
|
del cond
|
||||||
|
|
||||||
|
losses[hypernetwork.step % losses.shape[0]] = loss.item()
|
||||||
|
|
||||||
|
optimizer.zero_grad()
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
pbar.set_description(f"loss: {losses.mean():.7f}")
|
||||||
|
|
||||||
|
if hypernetwork.step > 0 and hypernetwork_dir is not None and hypernetwork.step % save_hypernetwork_every == 0:
|
||||||
|
last_saved_file = os.path.join(hypernetwork_dir, f'{hypernetwork_name}-{hypernetwork.step}.pt')
|
||||||
|
hypernetwork.save(last_saved_file)
|
||||||
|
|
||||||
|
if hypernetwork.step > 0 and images_dir is not None and hypernetwork.step % create_image_every == 0:
|
||||||
|
last_saved_image = os.path.join(images_dir, f'{hypernetwork_name}-{hypernetwork.step}.png')
|
||||||
|
|
||||||
|
preview_text = text if preview_image_prompt == "" else preview_image_prompt
|
||||||
|
|
||||||
|
optimizer.zero_grad()
|
||||||
|
shared.sd_model.cond_stage_model.to(devices.device)
|
||||||
|
shared.sd_model.first_stage_model.to(devices.device)
|
||||||
|
|
||||||
|
p = processing.StableDiffusionProcessingTxt2Img(
|
||||||
|
sd_model=shared.sd_model,
|
||||||
|
prompt=preview_text,
|
||||||
|
steps=20,
|
||||||
|
do_not_save_grid=True,
|
||||||
|
do_not_save_samples=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
processed = processing.process_images(p)
|
||||||
|
image = processed.images[0]
|
||||||
|
|
||||||
|
if unload:
|
||||||
|
shared.sd_model.cond_stage_model.to(devices.cpu)
|
||||||
|
shared.sd_model.first_stage_model.to(devices.cpu)
|
||||||
|
|
||||||
|
shared.state.current_image = image
|
||||||
|
image.save(last_saved_image)
|
||||||
|
|
||||||
|
last_saved_image += f", prompt: {preview_text}"
|
||||||
|
|
||||||
|
shared.state.job_no = hypernetwork.step
|
||||||
|
|
||||||
|
shared.state.textinfo = f"""
|
||||||
|
<p>
|
||||||
|
Loss: {losses.mean():.7f}<br/>
|
||||||
|
Step: {hypernetwork.step}<br/>
|
||||||
|
Last prompt: {html.escape(text)}<br/>
|
||||||
|
Last saved embedding: {html.escape(last_saved_file)}<br/>
|
||||||
|
Last saved image: {html.escape(last_saved_image)}<br/>
|
||||||
|
</p>
|
||||||
|
"""
|
||||||
|
|
||||||
|
checkpoint = sd_models.select_checkpoint()
|
||||||
|
|
||||||
|
hypernetwork.sd_checkpoint = checkpoint.hash
|
||||||
|
hypernetwork.sd_checkpoint_name = checkpoint.model_name
|
||||||
|
hypernetwork.save(filename)
|
||||||
|
|
||||||
|
return hypernetwork, filename
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,47 @@
|
|||||||
|
import html
|
||||||
|
import os
|
||||||
|
|
||||||
|
import gradio as gr
|
||||||
|
|
||||||
|
import modules.textual_inversion.textual_inversion
|
||||||
|
import modules.textual_inversion.preprocess
|
||||||
|
from modules import sd_hijack, shared, devices
|
||||||
|
from modules.hypernetworks import hypernetwork
|
||||||
|
|
||||||
|
|
||||||
|
def create_hypernetwork(name, enable_sizes):
|
||||||
|
fn = os.path.join(shared.cmd_opts.hypernetwork_dir, f"{name}.pt")
|
||||||
|
assert not os.path.exists(fn), f"file {fn} already exists"
|
||||||
|
|
||||||
|
hypernet = modules.hypernetworks.hypernetwork.Hypernetwork(name=name, enable_sizes=[int(x) for x in enable_sizes])
|
||||||
|
hypernet.save(fn)
|
||||||
|
|
||||||
|
shared.reload_hypernetworks()
|
||||||
|
|
||||||
|
return gr.Dropdown.update(choices=sorted([x for x in shared.hypernetworks.keys()])), f"Created: {fn}", ""
|
||||||
|
|
||||||
|
|
||||||
|
def train_hypernetwork(*args):
|
||||||
|
|
||||||
|
initial_hypernetwork = shared.loaded_hypernetwork
|
||||||
|
|
||||||
|
assert not shared.cmd_opts.lowvram, 'Training models with lowvram is not possible'
|
||||||
|
|
||||||
|
try:
|
||||||
|
sd_hijack.undo_optimizations()
|
||||||
|
|
||||||
|
hypernetwork, filename = modules.hypernetworks.hypernetwork.train_hypernetwork(*args)
|
||||||
|
|
||||||
|
res = f"""
|
||||||
|
Training {'interrupted' if shared.state.interrupted else 'finished'} at {hypernetwork.step} steps.
|
||||||
|
Hypernetwork saved to {html.escape(filename)}
|
||||||
|
"""
|
||||||
|
return res, ""
|
||||||
|
except Exception:
|
||||||
|
raise
|
||||||
|
finally:
|
||||||
|
shared.loaded_hypernetwork = initial_hypernetwork
|
||||||
|
shared.sd_model.cond_stage_model.to(devices.device)
|
||||||
|
shared.sd_model.first_stage_model.to(devices.device)
|
||||||
|
sd_hijack.apply_optimizations()
|
||||||
|
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
from pyngrok import ngrok, conf, exception
|
||||||
|
|
||||||
|
|
||||||
|
def connect(token, port):
|
||||||
|
if token == None:
|
||||||
|
token = 'None'
|
||||||
|
conf.get_default().auth_token = token
|
||||||
|
try:
|
||||||
|
public_url = ngrok.connect(port).public_url
|
||||||
|
except exception.PyngrokNgrokError:
|
||||||
|
print(f'Invalid ngrok authtoken, ngrok connection aborted.\n'
|
||||||
|
f'Your token: {token}, get the right one on https://dashboard.ngrok.com/get-started/your-authtoken')
|
||||||
|
else:
|
||||||
|
print(f'ngrok connected to localhost:{port}! URL: {public_url}\n'
|
||||||
|
'You can use this link after the launch is complete.')
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import modules.safe
|
||||||
|
|
||||||
script_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
script_path = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
|
||||||
models_path = os.path.join(script_path, "models")
|
models_path = os.path.join(script_path, "models")
|
||||||
|
|||||||
+18
-8
@@ -129,7 +129,7 @@ class Processed:
|
|||||||
self.index_of_first_image = index_of_first_image
|
self.index_of_first_image = index_of_first_image
|
||||||
self.styles = p.styles
|
self.styles = p.styles
|
||||||
self.job_timestamp = state.job_timestamp
|
self.job_timestamp = state.job_timestamp
|
||||||
self.clip_skip = opts.CLIP_ignore_last_layers
|
self.clip_skip = opts.CLIP_stop_at_last_layers
|
||||||
|
|
||||||
self.eta = p.eta
|
self.eta = p.eta
|
||||||
self.ddim_discretize = p.ddim_discretize
|
self.ddim_discretize = p.ddim_discretize
|
||||||
@@ -207,7 +207,7 @@ def create_random_tensors(shape, seeds, subseeds=None, subseed_strength=0.0, see
|
|||||||
# enables the generation of additional tensors with noise that the sampler will use during its processing.
|
# enables the generation of additional tensors with noise that the sampler will use during its processing.
|
||||||
# Using those pre-generated tensors instead of simple torch.randn allows a batch with seeds [100, 101] to
|
# Using those pre-generated tensors instead of simple torch.randn allows a batch with seeds [100, 101] to
|
||||||
# produce the same images as with two batches [100], [101].
|
# produce the same images as with two batches [100], [101].
|
||||||
if p is not None and p.sampler is not None and len(seeds) > 1 and opts.enable_batch_seeds:
|
if p is not None and p.sampler is not None and (len(seeds) > 1 and opts.enable_batch_seeds or opts.eta_noise_seed_delta > 0):
|
||||||
sampler_noises = [[] for _ in range(p.sampler.number_of_needed_noises(p))]
|
sampler_noises = [[] for _ in range(p.sampler.number_of_needed_noises(p))]
|
||||||
else:
|
else:
|
||||||
sampler_noises = None
|
sampler_noises = None
|
||||||
@@ -247,6 +247,9 @@ def create_random_tensors(shape, seeds, subseeds=None, subseed_strength=0.0, see
|
|||||||
if sampler_noises is not None:
|
if sampler_noises is not None:
|
||||||
cnt = p.sampler.number_of_needed_noises(p)
|
cnt = p.sampler.number_of_needed_noises(p)
|
||||||
|
|
||||||
|
if opts.eta_noise_seed_delta > 0:
|
||||||
|
torch.manual_seed(seed + opts.eta_noise_seed_delta)
|
||||||
|
|
||||||
for j in range(cnt):
|
for j in range(cnt):
|
||||||
sampler_noises[j].append(devices.randn_without_seed(tuple(noise_shape)))
|
sampler_noises[j].append(devices.randn_without_seed(tuple(noise_shape)))
|
||||||
|
|
||||||
@@ -259,6 +262,13 @@ def create_random_tensors(shape, seeds, subseeds=None, subseed_strength=0.0, see
|
|||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
|
def decode_first_stage(model, x):
|
||||||
|
with devices.autocast(disable=x.dtype == devices.dtype_vae):
|
||||||
|
x = model.decode_first_stage(x)
|
||||||
|
|
||||||
|
return x
|
||||||
|
|
||||||
|
|
||||||
def get_fixed_seed(seed):
|
def get_fixed_seed(seed):
|
||||||
if seed is None or seed == '' or seed == -1:
|
if seed is None or seed == '' or seed == -1:
|
||||||
return int(random.randrange(4294967294))
|
return int(random.randrange(4294967294))
|
||||||
@@ -274,7 +284,7 @@ def fix_seed(p):
|
|||||||
def create_infotext(p, all_prompts, all_seeds, all_subseeds, comments, iteration=0, position_in_batch=0):
|
def create_infotext(p, all_prompts, all_seeds, all_subseeds, comments, iteration=0, position_in_batch=0):
|
||||||
index = position_in_batch + iteration * p.batch_size
|
index = position_in_batch + iteration * p.batch_size
|
||||||
|
|
||||||
clip_skip = getattr(p, 'clip_skip', opts.CLIP_ignore_last_layers)
|
clip_skip = getattr(p, 'clip_skip', opts.CLIP_stop_at_last_layers)
|
||||||
|
|
||||||
generation_params = {
|
generation_params = {
|
||||||
"Steps": p.steps,
|
"Steps": p.steps,
|
||||||
@@ -293,7 +303,8 @@ def create_infotext(p, all_prompts, all_seeds, all_subseeds, comments, iteration
|
|||||||
"Seed resize from": (None if p.seed_resize_from_w == 0 or p.seed_resize_from_h == 0 else f"{p.seed_resize_from_w}x{p.seed_resize_from_h}"),
|
"Seed resize from": (None if p.seed_resize_from_w == 0 or p.seed_resize_from_h == 0 else f"{p.seed_resize_from_w}x{p.seed_resize_from_h}"),
|
||||||
"Denoising strength": getattr(p, 'denoising_strength', None),
|
"Denoising strength": getattr(p, 'denoising_strength', None),
|
||||||
"Eta": (None if p.sampler is None or p.sampler.eta == p.sampler.default_eta else p.sampler.eta),
|
"Eta": (None if p.sampler is None or p.sampler.eta == p.sampler.default_eta else p.sampler.eta),
|
||||||
"Clip skip": None if clip_skip==0 else clip_skip,
|
"Clip skip": None if clip_skip <= 1 else clip_skip,
|
||||||
|
"ENSD": None if opts.eta_noise_seed_delta == 0 else opts.eta_noise_seed_delta,
|
||||||
}
|
}
|
||||||
|
|
||||||
generation_params.update(p.extra_generation_params)
|
generation_params.update(p.extra_generation_params)
|
||||||
@@ -398,9 +409,8 @@ def process_images(p: StableDiffusionProcessing) -> Processed:
|
|||||||
# use the image collected previously in sampler loop
|
# use the image collected previously in sampler loop
|
||||||
samples_ddim = shared.state.current_latent
|
samples_ddim = shared.state.current_latent
|
||||||
|
|
||||||
samples_ddim = samples_ddim.to(devices.dtype)
|
samples_ddim = samples_ddim.to(devices.dtype_vae)
|
||||||
|
x_samples_ddim = decode_first_stage(p.sd_model, samples_ddim)
|
||||||
x_samples_ddim = p.sd_model.decode_first_stage(samples_ddim)
|
|
||||||
x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
|
x_samples_ddim = torch.clamp((x_samples_ddim + 1.0) / 2.0, min=0.0, max=1.0)
|
||||||
|
|
||||||
del samples_ddim
|
del samples_ddim
|
||||||
@@ -533,7 +543,7 @@ class StableDiffusionProcessingTxt2Img(StableDiffusionProcessing):
|
|||||||
if self.scale_latent:
|
if self.scale_latent:
|
||||||
samples = torch.nn.functional.interpolate(samples, size=(self.height // opt_f, self.width // opt_f), mode="bilinear")
|
samples = torch.nn.functional.interpolate(samples, size=(self.height // opt_f, self.width // opt_f), mode="bilinear")
|
||||||
else:
|
else:
|
||||||
decoded_samples = self.sd_model.decode_first_stage(samples)
|
decoded_samples = decode_first_stage(self.sd_model, samples)
|
||||||
|
|
||||||
if opts.upscaler_for_img2img is None or opts.upscaler_for_img2img == "None":
|
if opts.upscaler_for_img2img is None or opts.upscaler_for_img2img == "None":
|
||||||
decoded_samples = torch.nn.functional.interpolate(decoded_samples, size=(self.height, self.width), mode="bilinear")
|
decoded_samples = torch.nn.functional.interpolate(decoded_samples, size=(self.height, self.width), mode="bilinear")
|
||||||
|
|||||||
+110
@@ -0,0 +1,110 @@
|
|||||||
|
# this code is adapted from the script contributed by anon from /h/
|
||||||
|
|
||||||
|
import io
|
||||||
|
import pickle
|
||||||
|
import collections
|
||||||
|
import sys
|
||||||
|
import traceback
|
||||||
|
|
||||||
|
import torch
|
||||||
|
import numpy
|
||||||
|
import _codecs
|
||||||
|
import zipfile
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
# PyTorch 1.13 and later have _TypedStorage renamed to TypedStorage
|
||||||
|
TypedStorage = torch.storage.TypedStorage if hasattr(torch.storage, 'TypedStorage') else torch.storage._TypedStorage
|
||||||
|
|
||||||
|
|
||||||
|
def encode(*args):
|
||||||
|
out = _codecs.encode(*args)
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
class RestrictedUnpickler(pickle.Unpickler):
|
||||||
|
def persistent_load(self, saved_id):
|
||||||
|
assert saved_id[0] == 'storage'
|
||||||
|
return TypedStorage()
|
||||||
|
|
||||||
|
def find_class(self, module, name):
|
||||||
|
if module == 'collections' and name == 'OrderedDict':
|
||||||
|
return getattr(collections, name)
|
||||||
|
if module == 'torch._utils' and name in ['_rebuild_tensor_v2', '_rebuild_parameter']:
|
||||||
|
return getattr(torch._utils, name)
|
||||||
|
if module == 'torch' and name in ['FloatStorage', 'HalfStorage', 'IntStorage', 'LongStorage', 'DoubleStorage']:
|
||||||
|
return getattr(torch, name)
|
||||||
|
if module == 'torch.nn.modules.container' and name in ['ParameterDict']:
|
||||||
|
return getattr(torch.nn.modules.container, name)
|
||||||
|
if module == 'numpy.core.multiarray' and name == 'scalar':
|
||||||
|
return numpy.core.multiarray.scalar
|
||||||
|
if module == 'numpy' and name == 'dtype':
|
||||||
|
return numpy.dtype
|
||||||
|
if module == '_codecs' and name == 'encode':
|
||||||
|
return encode
|
||||||
|
if module == "pytorch_lightning.callbacks" and name == 'model_checkpoint':
|
||||||
|
import pytorch_lightning.callbacks
|
||||||
|
return pytorch_lightning.callbacks.model_checkpoint
|
||||||
|
if module == "pytorch_lightning.callbacks.model_checkpoint" and name == 'ModelCheckpoint':
|
||||||
|
import pytorch_lightning.callbacks.model_checkpoint
|
||||||
|
return pytorch_lightning.callbacks.model_checkpoint.ModelCheckpoint
|
||||||
|
if module == "__builtin__" and name == 'set':
|
||||||
|
return set
|
||||||
|
|
||||||
|
# Forbid everything else.
|
||||||
|
raise pickle.UnpicklingError(f"global '{module}/{name}' is forbidden")
|
||||||
|
|
||||||
|
|
||||||
|
allowed_zip_names = ["archive/data.pkl", "archive/version"]
|
||||||
|
allowed_zip_names_re = re.compile(r"^archive/data/\d+$")
|
||||||
|
|
||||||
|
|
||||||
|
def check_zip_filenames(filename, names):
|
||||||
|
for name in names:
|
||||||
|
if name in allowed_zip_names:
|
||||||
|
continue
|
||||||
|
if allowed_zip_names_re.match(name):
|
||||||
|
continue
|
||||||
|
|
||||||
|
raise Exception(f"bad file inside {filename}: {name}")
|
||||||
|
|
||||||
|
|
||||||
|
def check_pt(filename):
|
||||||
|
try:
|
||||||
|
|
||||||
|
# new pytorch format is a zip file
|
||||||
|
with zipfile.ZipFile(filename) as z:
|
||||||
|
check_zip_filenames(filename, z.namelist())
|
||||||
|
|
||||||
|
with z.open('archive/data.pkl') as file:
|
||||||
|
unpickler = RestrictedUnpickler(file)
|
||||||
|
unpickler.load()
|
||||||
|
|
||||||
|
except zipfile.BadZipfile:
|
||||||
|
|
||||||
|
# if it's not a zip file, it's an olf pytorch format, with five objects written to pickle
|
||||||
|
with open(filename, "rb") as file:
|
||||||
|
unpickler = RestrictedUnpickler(file)
|
||||||
|
for i in range(5):
|
||||||
|
unpickler.load()
|
||||||
|
|
||||||
|
|
||||||
|
def load(filename, *args, **kwargs):
|
||||||
|
from modules import shared
|
||||||
|
|
||||||
|
try:
|
||||||
|
if not shared.cmd_opts.disable_safe_unpickle:
|
||||||
|
check_pt(filename)
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
print(f"Error verifying pickled file from {filename}:", file=sys.stderr)
|
||||||
|
print(traceback.format_exc(), file=sys.stderr)
|
||||||
|
print(f"\nThe file may be malicious, so the program is not going to read it.", file=sys.stderr)
|
||||||
|
print(f"You can skip this check with --disable-safe-unpickle commandline argument.", file=sys.stderr)
|
||||||
|
return None
|
||||||
|
|
||||||
|
return unsafe_torch_load(filename, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
unsafe_torch_load = torch.load
|
||||||
|
torch.load = load
|
||||||
+94
-30
@@ -8,8 +8,9 @@ from torch import einsum
|
|||||||
from torch.nn.functional import silu
|
from torch.nn.functional import silu
|
||||||
|
|
||||||
import modules.textual_inversion.textual_inversion
|
import modules.textual_inversion.textual_inversion
|
||||||
from modules import prompt_parser, devices, sd_hijack_optimizations, shared, hypernetwork
|
from modules import prompt_parser, devices, sd_hijack_optimizations, shared
|
||||||
from modules.shared import opts, device, cmd_opts
|
from modules.shared import opts, device, cmd_opts
|
||||||
|
from modules.sd_hijack_optimizations import invokeAI_mps_available
|
||||||
|
|
||||||
import ldm.modules.attention
|
import ldm.modules.attention
|
||||||
import ldm.modules.diffusionmodules.model
|
import ldm.modules.diffusionmodules.model
|
||||||
@@ -23,30 +24,37 @@ def apply_optimizations():
|
|||||||
|
|
||||||
ldm.modules.diffusionmodules.model.nonlinearity = silu
|
ldm.modules.diffusionmodules.model.nonlinearity = silu
|
||||||
|
|
||||||
if cmd_opts.force_enable_xformers or (cmd_opts.xformers and shared.xformers_available and torch.version.cuda and torch.cuda.get_device_capability(shared.device) == (8, 6)):
|
if cmd_opts.force_enable_xformers or (cmd_opts.xformers and shared.xformers_available and torch.version.cuda and (6, 0) <= torch.cuda.get_device_capability(shared.device) <= (8, 6)):
|
||||||
print("Applying xformers cross attention optimization.")
|
print("Applying xformers cross attention optimization.")
|
||||||
ldm.modules.attention.CrossAttention.forward = sd_hijack_optimizations.xformers_attention_forward
|
ldm.modules.attention.CrossAttention.forward = sd_hijack_optimizations.xformers_attention_forward
|
||||||
ldm.modules.diffusionmodules.model.AttnBlock.forward = sd_hijack_optimizations.xformers_attnblock_forward
|
ldm.modules.diffusionmodules.model.AttnBlock.forward = sd_hijack_optimizations.xformers_attnblock_forward
|
||||||
elif cmd_opts.opt_split_attention_v1:
|
elif cmd_opts.opt_split_attention_v1:
|
||||||
print("Applying v1 cross attention optimization.")
|
print("Applying v1 cross attention optimization.")
|
||||||
ldm.modules.attention.CrossAttention.forward = sd_hijack_optimizations.split_cross_attention_forward_v1
|
ldm.modules.attention.CrossAttention.forward = sd_hijack_optimizations.split_cross_attention_forward_v1
|
||||||
|
elif not cmd_opts.disable_opt_split_attention and (cmd_opts.opt_split_attention_invokeai or not torch.cuda.is_available()):
|
||||||
|
if not invokeAI_mps_available and shared.device.type == 'mps':
|
||||||
|
print("The InvokeAI cross attention optimization for MPS requires the psutil package which is not installed.")
|
||||||
|
print("Applying v1 cross attention optimization.")
|
||||||
|
ldm.modules.attention.CrossAttention.forward = sd_hijack_optimizations.split_cross_attention_forward_v1
|
||||||
|
else:
|
||||||
|
print("Applying cross attention optimization (InvokeAI).")
|
||||||
|
ldm.modules.attention.CrossAttention.forward = sd_hijack_optimizations.split_cross_attention_forward_invokeAI
|
||||||
elif not cmd_opts.disable_opt_split_attention and (cmd_opts.opt_split_attention or torch.cuda.is_available()):
|
elif not cmd_opts.disable_opt_split_attention and (cmd_opts.opt_split_attention or torch.cuda.is_available()):
|
||||||
print("Applying cross attention optimization.")
|
print("Applying cross attention optimization (Doggettx).")
|
||||||
ldm.modules.attention.CrossAttention.forward = sd_hijack_optimizations.split_cross_attention_forward
|
ldm.modules.attention.CrossAttention.forward = sd_hijack_optimizations.split_cross_attention_forward
|
||||||
ldm.modules.diffusionmodules.model.AttnBlock.forward = sd_hijack_optimizations.cross_attention_attnblock_forward
|
ldm.modules.diffusionmodules.model.AttnBlock.forward = sd_hijack_optimizations.cross_attention_attnblock_forward
|
||||||
|
|
||||||
|
|
||||||
def undo_optimizations():
|
def undo_optimizations():
|
||||||
|
from modules.hypernetworks import hypernetwork
|
||||||
|
|
||||||
ldm.modules.attention.CrossAttention.forward = hypernetwork.attention_CrossAttention_forward
|
ldm.modules.attention.CrossAttention.forward = hypernetwork.attention_CrossAttention_forward
|
||||||
ldm.modules.diffusionmodules.model.nonlinearity = diffusionmodules_model_nonlinearity
|
ldm.modules.diffusionmodules.model.nonlinearity = diffusionmodules_model_nonlinearity
|
||||||
ldm.modules.diffusionmodules.model.AttnBlock.forward = diffusionmodules_model_AttnBlock_forward
|
ldm.modules.diffusionmodules.model.AttnBlock.forward = diffusionmodules_model_AttnBlock_forward
|
||||||
|
|
||||||
|
|
||||||
def get_target_prompt_token_count(token_count):
|
def get_target_prompt_token_count(token_count):
|
||||||
if token_count < 75:
|
return math.ceil(max(token_count, 1) / 75) * 75
|
||||||
return 75
|
|
||||||
|
|
||||||
return math.ceil(token_count / 10) * 10
|
|
||||||
|
|
||||||
|
|
||||||
class StableDiffusionModelHijack:
|
class StableDiffusionModelHijack:
|
||||||
@@ -110,6 +118,8 @@ class FrozenCLIPEmbedderWithCustomWords(torch.nn.Module):
|
|||||||
self.tokenizer = wrapped.tokenizer
|
self.tokenizer = wrapped.tokenizer
|
||||||
self.token_mults = {}
|
self.token_mults = {}
|
||||||
|
|
||||||
|
self.comma_token = [v for k, v in self.tokenizer.get_vocab().items() if k == ',</w>'][0]
|
||||||
|
|
||||||
tokens_with_parens = [(k, v) for k, v in self.tokenizer.get_vocab().items() if '(' in k or ')' in k or '[' in k or ']' in k]
|
tokens_with_parens = [(k, v) for k, v in self.tokenizer.get_vocab().items() if '(' in k or ')' in k or '[' in k or ']' in k]
|
||||||
for text, ident in tokens_with_parens:
|
for text, ident in tokens_with_parens:
|
||||||
mult = 1.0
|
mult = 1.0
|
||||||
@@ -127,7 +137,6 @@ class FrozenCLIPEmbedderWithCustomWords(torch.nn.Module):
|
|||||||
self.token_mults[ident] = mult
|
self.token_mults[ident] = mult
|
||||||
|
|
||||||
def tokenize_line(self, line, used_custom_terms, hijack_comments):
|
def tokenize_line(self, line, used_custom_terms, hijack_comments):
|
||||||
id_start = self.wrapped.tokenizer.bos_token_id
|
|
||||||
id_end = self.wrapped.tokenizer.eos_token_id
|
id_end = self.wrapped.tokenizer.eos_token_id
|
||||||
|
|
||||||
if opts.enable_emphasis:
|
if opts.enable_emphasis:
|
||||||
@@ -140,6 +149,7 @@ class FrozenCLIPEmbedderWithCustomWords(torch.nn.Module):
|
|||||||
fixes = []
|
fixes = []
|
||||||
remade_tokens = []
|
remade_tokens = []
|
||||||
multipliers = []
|
multipliers = []
|
||||||
|
last_comma = -1
|
||||||
|
|
||||||
for tokens, (text, weight) in zip(tokenized, parsed):
|
for tokens, (text, weight) in zip(tokenized, parsed):
|
||||||
i = 0
|
i = 0
|
||||||
@@ -148,13 +158,33 @@ class FrozenCLIPEmbedderWithCustomWords(torch.nn.Module):
|
|||||||
|
|
||||||
embedding, embedding_length_in_tokens = self.hijack.embedding_db.find_embedding_at_position(tokens, i)
|
embedding, embedding_length_in_tokens = self.hijack.embedding_db.find_embedding_at_position(tokens, i)
|
||||||
|
|
||||||
|
if token == self.comma_token:
|
||||||
|
last_comma = len(remade_tokens)
|
||||||
|
elif opts.comma_padding_backtrack != 0 and max(len(remade_tokens), 1) % 75 == 0 and last_comma != -1 and len(remade_tokens) - last_comma <= opts.comma_padding_backtrack:
|
||||||
|
last_comma += 1
|
||||||
|
reloc_tokens = remade_tokens[last_comma:]
|
||||||
|
reloc_mults = multipliers[last_comma:]
|
||||||
|
|
||||||
|
remade_tokens = remade_tokens[:last_comma]
|
||||||
|
length = len(remade_tokens)
|
||||||
|
|
||||||
|
rem = int(math.ceil(length / 75)) * 75 - length
|
||||||
|
remade_tokens += [id_end] * rem + reloc_tokens
|
||||||
|
multipliers = multipliers[:last_comma] + [1.0] * rem + reloc_mults
|
||||||
|
|
||||||
if embedding is None:
|
if embedding is None:
|
||||||
remade_tokens.append(token)
|
remade_tokens.append(token)
|
||||||
multipliers.append(weight)
|
multipliers.append(weight)
|
||||||
i += 1
|
i += 1
|
||||||
else:
|
else:
|
||||||
emb_len = int(embedding.vec.shape[0])
|
emb_len = int(embedding.vec.shape[0])
|
||||||
fixes.append((len(remade_tokens), embedding))
|
iteration = len(remade_tokens) // 75
|
||||||
|
if (len(remade_tokens) + emb_len) // 75 != iteration:
|
||||||
|
rem = (75 * (iteration + 1) - len(remade_tokens))
|
||||||
|
remade_tokens += [id_end] * rem
|
||||||
|
multipliers += [1.0] * rem
|
||||||
|
iteration += 1
|
||||||
|
fixes.append((iteration, (len(remade_tokens) % 75, embedding)))
|
||||||
remade_tokens += [0] * emb_len
|
remade_tokens += [0] * emb_len
|
||||||
multipliers += [weight] * emb_len
|
multipliers += [weight] * emb_len
|
||||||
used_custom_terms.append((embedding.name, embedding.checksum()))
|
used_custom_terms.append((embedding.name, embedding.checksum()))
|
||||||
@@ -162,10 +192,10 @@ class FrozenCLIPEmbedderWithCustomWords(torch.nn.Module):
|
|||||||
|
|
||||||
token_count = len(remade_tokens)
|
token_count = len(remade_tokens)
|
||||||
prompt_target_length = get_target_prompt_token_count(token_count)
|
prompt_target_length = get_target_prompt_token_count(token_count)
|
||||||
tokens_to_add = prompt_target_length - len(remade_tokens) + 1
|
tokens_to_add = prompt_target_length - len(remade_tokens)
|
||||||
|
|
||||||
remade_tokens = [id_start] + remade_tokens + [id_end] * tokens_to_add
|
remade_tokens = remade_tokens + [id_end] * tokens_to_add
|
||||||
multipliers = [1.0] + multipliers + [1.0] * tokens_to_add
|
multipliers = multipliers + [1.0] * tokens_to_add
|
||||||
|
|
||||||
return remade_tokens, fixes, multipliers, token_count
|
return remade_tokens, fixes, multipliers, token_count
|
||||||
|
|
||||||
@@ -260,39 +290,73 @@ class FrozenCLIPEmbedderWithCustomWords(torch.nn.Module):
|
|||||||
hijack_fixes.append(fixes)
|
hijack_fixes.append(fixes)
|
||||||
batch_multipliers.append(multipliers)
|
batch_multipliers.append(multipliers)
|
||||||
return batch_multipliers, remade_batch_tokens, used_custom_terms, hijack_comments, hijack_fixes, token_count
|
return batch_multipliers, remade_batch_tokens, used_custom_terms, hijack_comments, hijack_fixes, token_count
|
||||||
|
|
||||||
def forward(self, text):
|
def forward(self, text):
|
||||||
|
use_old = opts.use_old_emphasis_implementation
|
||||||
if opts.use_old_emphasis_implementation:
|
if use_old:
|
||||||
batch_multipliers, remade_batch_tokens, used_custom_terms, hijack_comments, hijack_fixes, token_count = self.process_text_old(text)
|
batch_multipliers, remade_batch_tokens, used_custom_terms, hijack_comments, hijack_fixes, token_count = self.process_text_old(text)
|
||||||
else:
|
else:
|
||||||
batch_multipliers, remade_batch_tokens, used_custom_terms, hijack_comments, hijack_fixes, token_count = self.process_text(text)
|
batch_multipliers, remade_batch_tokens, used_custom_terms, hijack_comments, hijack_fixes, token_count = self.process_text(text)
|
||||||
|
|
||||||
self.hijack.fixes = hijack_fixes
|
|
||||||
self.hijack.comments += hijack_comments
|
self.hijack.comments += hijack_comments
|
||||||
|
|
||||||
if len(used_custom_terms) > 0:
|
if len(used_custom_terms) > 0:
|
||||||
self.hijack.comments.append("Used embeddings: " + ", ".join([f'{word} [{checksum}]' for word, checksum in used_custom_terms]))
|
self.hijack.comments.append("Used embeddings: " + ", ".join([f'{word} [{checksum}]' for word, checksum in used_custom_terms]))
|
||||||
|
|
||||||
|
if use_old:
|
||||||
|
self.hijack.fixes = hijack_fixes
|
||||||
|
return self.process_tokens(remade_batch_tokens, batch_multipliers)
|
||||||
|
|
||||||
|
z = None
|
||||||
|
i = 0
|
||||||
|
while max(map(len, remade_batch_tokens)) != 0:
|
||||||
|
rem_tokens = [x[75:] for x in remade_batch_tokens]
|
||||||
|
rem_multipliers = [x[75:] for x in batch_multipliers]
|
||||||
|
|
||||||
target_token_count = get_target_prompt_token_count(token_count) + 2
|
self.hijack.fixes = []
|
||||||
|
for unfiltered in hijack_fixes:
|
||||||
|
fixes = []
|
||||||
|
for fix in unfiltered:
|
||||||
|
if fix[0] == i:
|
||||||
|
fixes.append(fix[1])
|
||||||
|
self.hijack.fixes.append(fixes)
|
||||||
|
|
||||||
|
tokens = []
|
||||||
|
multipliers = []
|
||||||
|
for j in range(len(remade_batch_tokens)):
|
||||||
|
if len(remade_batch_tokens[j]) > 0:
|
||||||
|
tokens.append(remade_batch_tokens[j][:75])
|
||||||
|
multipliers.append(batch_multipliers[j][:75])
|
||||||
|
else:
|
||||||
|
tokens.append([self.wrapped.tokenizer.eos_token_id] * 75)
|
||||||
|
multipliers.append([1.0] * 75)
|
||||||
|
|
||||||
position_ids_array = [min(x, 75) for x in range(target_token_count-1)] + [76]
|
z1 = self.process_tokens(tokens, multipliers)
|
||||||
position_ids = torch.asarray(position_ids_array, device=devices.device).expand((1, -1))
|
z = z1 if z is None else torch.cat((z, z1), axis=-2)
|
||||||
|
|
||||||
|
remade_batch_tokens = rem_tokens
|
||||||
|
batch_multipliers = rem_multipliers
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
return z
|
||||||
|
|
||||||
|
|
||||||
|
def process_tokens(self, remade_batch_tokens, batch_multipliers):
|
||||||
|
if not opts.use_old_emphasis_implementation:
|
||||||
|
remade_batch_tokens = [[self.wrapped.tokenizer.bos_token_id] + x[:75] + [self.wrapped.tokenizer.eos_token_id] for x in remade_batch_tokens]
|
||||||
|
batch_multipliers = [[1.0] + x[:75] + [1.0] for x in batch_multipliers]
|
||||||
|
|
||||||
|
tokens = torch.asarray(remade_batch_tokens).to(device)
|
||||||
|
outputs = self.wrapped.transformer(input_ids=tokens, output_hidden_states=-opts.CLIP_stop_at_last_layers)
|
||||||
|
|
||||||
remade_batch_tokens_of_same_length = [x + [self.wrapped.tokenizer.eos_token_id] * (target_token_count - len(x)) for x in remade_batch_tokens]
|
if opts.CLIP_stop_at_last_layers > 1:
|
||||||
tokens = torch.asarray(remade_batch_tokens_of_same_length).to(device)
|
z = outputs.hidden_states[-opts.CLIP_stop_at_last_layers]
|
||||||
|
|
||||||
tmp = -opts.CLIP_ignore_last_layers
|
|
||||||
if (opts.CLIP_ignore_last_layers == 0):
|
|
||||||
outputs = self.wrapped.transformer(input_ids=tokens, position_ids=position_ids)
|
|
||||||
z = outputs.last_hidden_state
|
|
||||||
else:
|
|
||||||
outputs = self.wrapped.transformer(input_ids=tokens, position_ids=position_ids, output_hidden_states=tmp)
|
|
||||||
z = outputs.hidden_states[tmp]
|
|
||||||
z = self.wrapped.transformer.text_model.final_layer_norm(z)
|
z = self.wrapped.transformer.text_model.final_layer_norm(z)
|
||||||
|
else:
|
||||||
|
z = outputs.last_hidden_state
|
||||||
|
|
||||||
# restoring original mean is likely not correct, but it seems to work well to prevent artifacts that happen otherwise
|
# restoring original mean is likely not correct, but it seems to work well to prevent artifacts that happen otherwise
|
||||||
batch_multipliers_of_same_length = [x + [1.0] * (target_token_count - len(x)) for x in batch_multipliers]
|
batch_multipliers_of_same_length = [x + [1.0] * (75 - len(x)) for x in batch_multipliers]
|
||||||
batch_multipliers = torch.asarray(batch_multipliers_of_same_length).to(device)
|
batch_multipliers = torch.asarray(batch_multipliers_of_same_length).to(device)
|
||||||
original_mean = z.mean()
|
original_mean = z.mean()
|
||||||
z *= batch_multipliers.reshape(batch_multipliers.shape + (1,)).expand(z.shape)
|
z *= batch_multipliers.reshape(batch_multipliers.shape + (1,)).expand(z.shape)
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import math
|
import math
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
|
import importlib
|
||||||
|
|
||||||
import torch
|
import torch
|
||||||
from torch import einsum
|
from torch import einsum
|
||||||
@@ -9,12 +10,12 @@ from ldm.util import default
|
|||||||
from einops import rearrange
|
from einops import rearrange
|
||||||
|
|
||||||
from modules import shared
|
from modules import shared
|
||||||
|
from modules.hypernetworks import hypernetwork
|
||||||
|
|
||||||
|
|
||||||
if shared.cmd_opts.xformers or shared.cmd_opts.force_enable_xformers:
|
if shared.cmd_opts.xformers or shared.cmd_opts.force_enable_xformers:
|
||||||
try:
|
try:
|
||||||
import xformers.ops
|
import xformers.ops
|
||||||
import functorch
|
|
||||||
xformers._is_functorch_available = True
|
|
||||||
shared.xformers_available = True
|
shared.xformers_available = True
|
||||||
except Exception:
|
except Exception:
|
||||||
print("Cannot import xformers", file=sys.stderr)
|
print("Cannot import xformers", file=sys.stderr)
|
||||||
@@ -28,16 +29,10 @@ def split_cross_attention_forward_v1(self, x, context=None, mask=None):
|
|||||||
q_in = self.to_q(x)
|
q_in = self.to_q(x)
|
||||||
context = default(context, x)
|
context = default(context, x)
|
||||||
|
|
||||||
hypernetwork = shared.loaded_hypernetwork
|
context_k, context_v = hypernetwork.apply_hypernetwork(shared.loaded_hypernetwork, context)
|
||||||
hypernetwork_layers = (hypernetwork.layers if hypernetwork is not None else {}).get(context.shape[2], None)
|
k_in = self.to_k(context_k)
|
||||||
|
v_in = self.to_v(context_v)
|
||||||
if hypernetwork_layers is not None:
|
del context, context_k, context_v, x
|
||||||
k_in = self.to_k(hypernetwork_layers[0](context))
|
|
||||||
v_in = self.to_v(hypernetwork_layers[1](context))
|
|
||||||
else:
|
|
||||||
k_in = self.to_k(context)
|
|
||||||
v_in = self.to_v(context)
|
|
||||||
del context, x
|
|
||||||
|
|
||||||
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q_in, k_in, v_in))
|
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q_in, k_in, v_in))
|
||||||
del q_in, k_in, v_in
|
del q_in, k_in, v_in
|
||||||
@@ -61,22 +56,16 @@ def split_cross_attention_forward_v1(self, x, context=None, mask=None):
|
|||||||
return self.to_out(r2)
|
return self.to_out(r2)
|
||||||
|
|
||||||
|
|
||||||
# taken from https://github.com/Doggettx/stable-diffusion
|
# taken from https://github.com/Doggettx/stable-diffusion and modified
|
||||||
def split_cross_attention_forward(self, x, context=None, mask=None):
|
def split_cross_attention_forward(self, x, context=None, mask=None):
|
||||||
h = self.heads
|
h = self.heads
|
||||||
|
|
||||||
q_in = self.to_q(x)
|
q_in = self.to_q(x)
|
||||||
context = default(context, x)
|
context = default(context, x)
|
||||||
|
|
||||||
hypernetwork = shared.loaded_hypernetwork
|
context_k, context_v = hypernetwork.apply_hypernetwork(shared.loaded_hypernetwork, context)
|
||||||
hypernetwork_layers = (hypernetwork.layers if hypernetwork is not None else {}).get(context.shape[2], None)
|
k_in = self.to_k(context_k)
|
||||||
|
v_in = self.to_v(context_v)
|
||||||
if hypernetwork_layers is not None:
|
|
||||||
k_in = self.to_k(hypernetwork_layers[0](context))
|
|
||||||
v_in = self.to_v(hypernetwork_layers[1](context))
|
|
||||||
else:
|
|
||||||
k_in = self.to_k(context)
|
|
||||||
v_in = self.to_v(context)
|
|
||||||
|
|
||||||
k_in *= self.scale
|
k_in *= self.scale
|
||||||
|
|
||||||
@@ -128,18 +117,111 @@ def split_cross_attention_forward(self, x, context=None, mask=None):
|
|||||||
|
|
||||||
return self.to_out(r2)
|
return self.to_out(r2)
|
||||||
|
|
||||||
|
|
||||||
|
def check_for_psutil():
|
||||||
|
try:
|
||||||
|
spec = importlib.util.find_spec('psutil')
|
||||||
|
return spec is not None
|
||||||
|
except ModuleNotFoundError:
|
||||||
|
return False
|
||||||
|
|
||||||
|
invokeAI_mps_available = check_for_psutil()
|
||||||
|
|
||||||
|
# -- Taken from https://github.com/invoke-ai/InvokeAI --
|
||||||
|
if invokeAI_mps_available:
|
||||||
|
import psutil
|
||||||
|
mem_total_gb = psutil.virtual_memory().total // (1 << 30)
|
||||||
|
|
||||||
|
def einsum_op_compvis(q, k, v):
|
||||||
|
s = einsum('b i d, b j d -> b i j', q, k)
|
||||||
|
s = s.softmax(dim=-1, dtype=s.dtype)
|
||||||
|
return einsum('b i j, b j d -> b i d', s, v)
|
||||||
|
|
||||||
|
def einsum_op_slice_0(q, k, v, slice_size):
|
||||||
|
r = torch.zeros(q.shape[0], q.shape[1], v.shape[2], device=q.device, dtype=q.dtype)
|
||||||
|
for i in range(0, q.shape[0], slice_size):
|
||||||
|
end = i + slice_size
|
||||||
|
r[i:end] = einsum_op_compvis(q[i:end], k[i:end], v[i:end])
|
||||||
|
return r
|
||||||
|
|
||||||
|
def einsum_op_slice_1(q, k, v, slice_size):
|
||||||
|
r = torch.zeros(q.shape[0], q.shape[1], v.shape[2], device=q.device, dtype=q.dtype)
|
||||||
|
for i in range(0, q.shape[1], slice_size):
|
||||||
|
end = i + slice_size
|
||||||
|
r[:, i:end] = einsum_op_compvis(q[:, i:end], k, v)
|
||||||
|
return r
|
||||||
|
|
||||||
|
def einsum_op_mps_v1(q, k, v):
|
||||||
|
if q.shape[1] <= 4096: # (512x512) max q.shape[1]: 4096
|
||||||
|
return einsum_op_compvis(q, k, v)
|
||||||
|
else:
|
||||||
|
slice_size = math.floor(2**30 / (q.shape[0] * q.shape[1]))
|
||||||
|
return einsum_op_slice_1(q, k, v, slice_size)
|
||||||
|
|
||||||
|
def einsum_op_mps_v2(q, k, v):
|
||||||
|
if mem_total_gb > 8 and q.shape[1] <= 4096:
|
||||||
|
return einsum_op_compvis(q, k, v)
|
||||||
|
else:
|
||||||
|
return einsum_op_slice_0(q, k, v, 1)
|
||||||
|
|
||||||
|
def einsum_op_tensor_mem(q, k, v, max_tensor_mb):
|
||||||
|
size_mb = q.shape[0] * q.shape[1] * k.shape[1] * q.element_size() // (1 << 20)
|
||||||
|
if size_mb <= max_tensor_mb:
|
||||||
|
return einsum_op_compvis(q, k, v)
|
||||||
|
div = 1 << int((size_mb - 1) / max_tensor_mb).bit_length()
|
||||||
|
if div <= q.shape[0]:
|
||||||
|
return einsum_op_slice_0(q, k, v, q.shape[0] // div)
|
||||||
|
return einsum_op_slice_1(q, k, v, max(q.shape[1] // div, 1))
|
||||||
|
|
||||||
|
def einsum_op_cuda(q, k, v):
|
||||||
|
stats = torch.cuda.memory_stats(q.device)
|
||||||
|
mem_active = stats['active_bytes.all.current']
|
||||||
|
mem_reserved = stats['reserved_bytes.all.current']
|
||||||
|
mem_free_cuda, _ = torch.cuda.mem_get_info(q.device)
|
||||||
|
mem_free_torch = mem_reserved - mem_active
|
||||||
|
mem_free_total = mem_free_cuda + mem_free_torch
|
||||||
|
# Divide factor of safety as there's copying and fragmentation
|
||||||
|
return self.einsum_op_tensor_mem(q, k, v, mem_free_total / 3.3 / (1 << 20))
|
||||||
|
|
||||||
|
def einsum_op(q, k, v):
|
||||||
|
if q.device.type == 'cuda':
|
||||||
|
return einsum_op_cuda(q, k, v)
|
||||||
|
|
||||||
|
if q.device.type == 'mps':
|
||||||
|
if mem_total_gb >= 32:
|
||||||
|
return einsum_op_mps_v1(q, k, v)
|
||||||
|
return einsum_op_mps_v2(q, k, v)
|
||||||
|
|
||||||
|
# Smaller slices are faster due to L2/L3/SLC caches.
|
||||||
|
# Tested on i7 with 8MB L3 cache.
|
||||||
|
return einsum_op_tensor_mem(q, k, v, 32)
|
||||||
|
|
||||||
|
def split_cross_attention_forward_invokeAI(self, x, context=None, mask=None):
|
||||||
|
h = self.heads
|
||||||
|
|
||||||
|
q = self.to_q(x)
|
||||||
|
context = default(context, x)
|
||||||
|
|
||||||
|
context_k, context_v = hypernetwork.apply_hypernetwork(shared.loaded_hypernetwork, context)
|
||||||
|
k = self.to_k(context_k) * self.scale
|
||||||
|
v = self.to_v(context_v)
|
||||||
|
del context, context_k, context_v, x
|
||||||
|
|
||||||
|
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v))
|
||||||
|
r = einsum_op(q, k, v)
|
||||||
|
return self.to_out(rearrange(r, '(b h) n d -> b n (h d)', h=h))
|
||||||
|
|
||||||
|
# -- End of code from https://github.com/invoke-ai/InvokeAI --
|
||||||
|
|
||||||
def xformers_attention_forward(self, x, context=None, mask=None):
|
def xformers_attention_forward(self, x, context=None, mask=None):
|
||||||
h = self.heads
|
h = self.heads
|
||||||
q_in = self.to_q(x)
|
q_in = self.to_q(x)
|
||||||
context = default(context, x)
|
context = default(context, x)
|
||||||
hypernetwork = shared.loaded_hypernetwork
|
|
||||||
hypernetwork_layers = (hypernetwork.layers if hypernetwork is not None else {}).get(context.shape[2], None)
|
context_k, context_v = hypernetwork.apply_hypernetwork(shared.loaded_hypernetwork, context)
|
||||||
if hypernetwork_layers is not None:
|
k_in = self.to_k(context_k)
|
||||||
k_in = self.to_k(hypernetwork_layers[0](context))
|
v_in = self.to_v(context_v)
|
||||||
v_in = self.to_v(hypernetwork_layers[1](context))
|
|
||||||
else:
|
|
||||||
k_in = self.to_k(context)
|
|
||||||
v_in = self.to_v(context)
|
|
||||||
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b n h d', h=h), (q_in, k_in, v_in))
|
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b n h d', h=h), (q_in, k_in, v_in))
|
||||||
del q_in, k_in, v_in
|
del q_in, k_in, v_in
|
||||||
out = xformers.ops.memory_efficient_attention(q, k, v, attn_bias=None)
|
out = xformers.ops.memory_efficient_attention(q, k, v, attn_bias=None)
|
||||||
|
|||||||
@@ -149,8 +149,13 @@ def load_model_weights(model, checkpoint_info):
|
|||||||
model.half()
|
model.half()
|
||||||
|
|
||||||
devices.dtype = torch.float32 if shared.cmd_opts.no_half else torch.float16
|
devices.dtype = torch.float32 if shared.cmd_opts.no_half else torch.float16
|
||||||
|
devices.dtype_vae = torch.float32 if shared.cmd_opts.no_half or shared.cmd_opts.no_half_vae else torch.float16
|
||||||
|
|
||||||
vae_file = os.path.splitext(checkpoint_file)[0] + ".vae.pt"
|
vae_file = os.path.splitext(checkpoint_file)[0] + ".vae.pt"
|
||||||
|
|
||||||
|
if not os.path.exists(vae_file) and shared.cmd_opts.vae_path is not None:
|
||||||
|
vae_file = shared.cmd_opts.vae_path
|
||||||
|
|
||||||
if os.path.exists(vae_file):
|
if os.path.exists(vae_file):
|
||||||
print(f"Loading VAE weights from: {vae_file}")
|
print(f"Loading VAE weights from: {vae_file}")
|
||||||
vae_ckpt = torch.load(vae_file, map_location="cpu")
|
vae_ckpt = torch.load(vae_file, map_location="cpu")
|
||||||
@@ -158,6 +163,8 @@ def load_model_weights(model, checkpoint_info):
|
|||||||
|
|
||||||
model.first_stage_model.load_state_dict(vae_dict)
|
model.first_stage_model.load_state_dict(vae_dict)
|
||||||
|
|
||||||
|
model.first_stage_model.to(devices.dtype_vae)
|
||||||
|
|
||||||
model.sd_model_hash = sd_model_hash
|
model.sd_model_hash = sd_model_hash
|
||||||
model.sd_model_checkpoint = checkpoint_file
|
model.sd_model_checkpoint = checkpoint_file
|
||||||
model.sd_checkpoint_info = checkpoint_info
|
model.sd_checkpoint_info = checkpoint_info
|
||||||
|
|||||||
+19
-9
@@ -7,7 +7,7 @@ import inspect
|
|||||||
import k_diffusion.sampling
|
import k_diffusion.sampling
|
||||||
import ldm.models.diffusion.ddim
|
import ldm.models.diffusion.ddim
|
||||||
import ldm.models.diffusion.plms
|
import ldm.models.diffusion.plms
|
||||||
from modules import prompt_parser
|
from modules import prompt_parser, devices, processing
|
||||||
|
|
||||||
from modules.shared import opts, cmd_opts, state
|
from modules.shared import opts, cmd_opts, state
|
||||||
import modules.shared as shared
|
import modules.shared as shared
|
||||||
@@ -57,7 +57,7 @@ def set_samplers():
|
|||||||
global samplers, samplers_for_img2img
|
global samplers, samplers_for_img2img
|
||||||
|
|
||||||
hidden = set(opts.hide_samplers)
|
hidden = set(opts.hide_samplers)
|
||||||
hidden_img2img = set(opts.hide_samplers + ['PLMS', 'DPM fast', 'DPM adaptive'])
|
hidden_img2img = set(opts.hide_samplers + ['PLMS'])
|
||||||
|
|
||||||
samplers = [x for x in all_samplers if x.name not in hidden]
|
samplers = [x for x in all_samplers if x.name not in hidden]
|
||||||
samplers_for_img2img = [x for x in all_samplers if x.name not in hidden_img2img]
|
samplers_for_img2img = [x for x in all_samplers if x.name not in hidden_img2img]
|
||||||
@@ -83,7 +83,7 @@ def setup_img2img_steps(p, steps=None):
|
|||||||
|
|
||||||
|
|
||||||
def sample_to_image(samples):
|
def sample_to_image(samples):
|
||||||
x_sample = shared.sd_model.decode_first_stage(samples[0:1].type(shared.sd_model.dtype))[0]
|
x_sample = processing.decode_first_stage(shared.sd_model, samples[0:1])[0]
|
||||||
x_sample = torch.clamp((x_sample + 1.0) / 2.0, min=0.0, max=1.0)
|
x_sample = torch.clamp((x_sample + 1.0) / 2.0, min=0.0, max=1.0)
|
||||||
x_sample = 255. * np.moveaxis(x_sample.cpu().numpy(), 0, 2)
|
x_sample = 255. * np.moveaxis(x_sample.cpu().numpy(), 0, 2)
|
||||||
x_sample = x_sample.astype(np.uint8)
|
x_sample = x_sample.astype(np.uint8)
|
||||||
@@ -365,16 +365,26 @@ class KDiffusionSampler:
|
|||||||
else:
|
else:
|
||||||
sigmas = self.model_wrap.get_sigmas(steps)
|
sigmas = self.model_wrap.get_sigmas(steps)
|
||||||
|
|
||||||
noise = noise * sigmas[steps - t_enc - 1]
|
|
||||||
xi = x + noise
|
|
||||||
|
|
||||||
extra_params_kwargs = self.initialize(p)
|
|
||||||
|
|
||||||
sigma_sched = sigmas[steps - t_enc - 1:]
|
sigma_sched = sigmas[steps - t_enc - 1:]
|
||||||
|
xi = x + noise * sigma_sched[0]
|
||||||
|
|
||||||
|
extra_params_kwargs = self.initialize(p)
|
||||||
|
if 'sigma_min' in inspect.signature(self.func).parameters:
|
||||||
|
## last sigma is zero which isn't allowed by DPM Fast & Adaptive so taking value before last
|
||||||
|
extra_params_kwargs['sigma_min'] = sigma_sched[-2]
|
||||||
|
if 'sigma_max' in inspect.signature(self.func).parameters:
|
||||||
|
extra_params_kwargs['sigma_max'] = sigma_sched[0]
|
||||||
|
if 'n' in inspect.signature(self.func).parameters:
|
||||||
|
extra_params_kwargs['n'] = len(sigma_sched) - 1
|
||||||
|
if 'sigma_sched' in inspect.signature(self.func).parameters:
|
||||||
|
extra_params_kwargs['sigma_sched'] = sigma_sched
|
||||||
|
if 'sigmas' in inspect.signature(self.func).parameters:
|
||||||
|
extra_params_kwargs['sigmas'] = sigma_sched
|
||||||
|
|
||||||
self.model_wrap_cfg.init_latent = x
|
self.model_wrap_cfg.init_latent = x
|
||||||
|
|
||||||
return self.func(self.model_wrap_cfg, xi, sigma_sched, extra_args={'cond': conditioning, 'uncond': unconditional_conditioning, 'cond_scale': p.cfg_scale}, disable=False, callback=self.callback_state, **extra_params_kwargs)
|
return self.func(self.model_wrap_cfg, xi, extra_args={'cond': conditioning, 'uncond': unconditional_conditioning, 'cond_scale': p.cfg_scale}, disable=False, callback=self.callback_state, **extra_params_kwargs)
|
||||||
|
|
||||||
|
|
||||||
def sample(self, p, x, conditioning, unconditional_conditioning, steps=None):
|
def sample(self, p, x, conditioning, unconditional_conditioning, steps=None):
|
||||||
steps = steps or p.steps
|
steps = steps or p.steps
|
||||||
|
|||||||
+32
-8
@@ -13,7 +13,8 @@ import modules.memmon
|
|||||||
import modules.sd_models
|
import modules.sd_models
|
||||||
import modules.styles
|
import modules.styles
|
||||||
import modules.devices as devices
|
import modules.devices as devices
|
||||||
from modules import sd_samplers, hypernetwork
|
from modules import sd_samplers
|
||||||
|
from modules.hypernetworks import hypernetwork
|
||||||
from modules.paths import models_path, script_path, sd_path
|
from modules.paths import models_path, script_path, sd_path
|
||||||
|
|
||||||
sd_model_file = os.path.join(script_path, 'model.ckpt')
|
sd_model_file = os.path.join(script_path, 'model.ckpt')
|
||||||
@@ -25,9 +26,11 @@ parser.add_argument("--ckpt-dir", type=str, default=None, help="Path to director
|
|||||||
parser.add_argument("--gfpgan-dir", type=str, help="GFPGAN directory", default=('./src/gfpgan' if os.path.exists('./src/gfpgan') else './GFPGAN'))
|
parser.add_argument("--gfpgan-dir", type=str, help="GFPGAN directory", default=('./src/gfpgan' if os.path.exists('./src/gfpgan') else './GFPGAN'))
|
||||||
parser.add_argument("--gfpgan-model", type=str, help="GFPGAN model file name", default=None)
|
parser.add_argument("--gfpgan-model", type=str, help="GFPGAN model file name", default=None)
|
||||||
parser.add_argument("--no-half", action='store_true', help="do not switch the model to 16-bit floats")
|
parser.add_argument("--no-half", action='store_true', help="do not switch the model to 16-bit floats")
|
||||||
|
parser.add_argument("--no-half-vae", action='store_true', help="do not switch the VAE model to 16-bit floats")
|
||||||
parser.add_argument("--no-progressbar-hiding", action='store_true', help="do not hide progressbar in gradio UI (we hide it because it slows down ML if you have hardware acceleration in browser)")
|
parser.add_argument("--no-progressbar-hiding", action='store_true', help="do not hide progressbar in gradio UI (we hide it because it slows down ML if you have hardware acceleration in browser)")
|
||||||
parser.add_argument("--max-batch-count", type=int, default=16, help="maximum batch count value for the UI")
|
parser.add_argument("--max-batch-count", type=int, default=16, help="maximum batch count value for the UI")
|
||||||
parser.add_argument("--embeddings-dir", type=str, default=os.path.join(script_path, 'embeddings'), help="embeddings directory for textual inversion (default: embeddings)")
|
parser.add_argument("--embeddings-dir", type=str, default=os.path.join(script_path, 'embeddings'), help="embeddings directory for textual inversion (default: embeddings)")
|
||||||
|
parser.add_argument("--hypernetwork-dir", type=str, default=os.path.join(models_path, 'hypernetworks'), help="hypernetwork 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("--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")
|
||||||
@@ -35,6 +38,7 @@ parser.add_argument("--always-batch-cond-uncond", action='store_true', help="dis
|
|||||||
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("--share", action='store_true', help="use share=True for gradio and make the UI accessible through their site (doesn't work for me but you might have better luck)")
|
parser.add_argument("--share", action='store_true', help="use share=True for gradio and make the UI accessible through their site (doesn't work for me but you might have better luck)")
|
||||||
|
parser.add_argument("--ngrok", type=str, help="ngrok authtoken, alternative to gradio --share", default=None)
|
||||||
parser.add_argument("--codeformer-models-path", type=str, help="Path to directory with codeformer model file(s).", default=os.path.join(models_path, 'Codeformer'))
|
parser.add_argument("--codeformer-models-path", type=str, help="Path to directory with codeformer model file(s).", default=os.path.join(models_path, 'Codeformer'))
|
||||||
parser.add_argument("--gfpgan-models-path", type=str, help="Path to directory with GFPGAN model file(s).", default=os.path.join(models_path, 'GFPGAN'))
|
parser.add_argument("--gfpgan-models-path", type=str, help="Path to directory with GFPGAN model file(s).", default=os.path.join(models_path, 'GFPGAN'))
|
||||||
parser.add_argument("--esrgan-models-path", type=str, help="Path to directory with ESRGAN model file(s).", default=os.path.join(models_path, 'ESRGAN'))
|
parser.add_argument("--esrgan-models-path", type=str, help="Path to directory with ESRGAN model file(s).", default=os.path.join(models_path, 'ESRGAN'))
|
||||||
@@ -46,9 +50,10 @@ parser.add_argument("--ldsr-models-path", type=str, help="Path to directory with
|
|||||||
parser.add_argument("--xformers", action='store_true', help="enable xformers for cross attention layers")
|
parser.add_argument("--xformers", action='store_true', help="enable xformers for cross attention layers")
|
||||||
parser.add_argument("--force-enable-xformers", action='store_true', help="enable xformers for cross attention layers regardless of whether the checking code thinks you can run it; do not make bug reports if this fails to work")
|
parser.add_argument("--force-enable-xformers", action='store_true', help="enable xformers for cross attention layers regardless of whether the checking code thinks you can run it; do not make bug reports if this fails to work")
|
||||||
parser.add_argument("--deepdanbooru", action='store_true', help="enable deepdanbooru interrogator")
|
parser.add_argument("--deepdanbooru", action='store_true', help="enable deepdanbooru interrogator")
|
||||||
parser.add_argument("--opt-split-attention", action='store_true', help="force-enables cross-attention layer optimization. By default, it's on for torch.cuda and off for other torch devices.")
|
parser.add_argument("--opt-split-attention", action='store_true', help="force-enables Doggettx's cross-attention layer optimization. By default, it's on for torch cuda.")
|
||||||
parser.add_argument("--disable-opt-split-attention", action='store_true', help="force-disables cross-attention layer optimization")
|
parser.add_argument("--opt-split-attention-invokeai", action='store_true', help="force-enables InvokeAI's cross-attention layer optimization. By default, it's on when cuda is unavailable.")
|
||||||
parser.add_argument("--opt-split-attention-v1", action='store_true', help="enable older version of split attention optimization that does not consume all the VRAM it can find")
|
parser.add_argument("--opt-split-attention-v1", action='store_true', help="enable older version of split attention optimization that does not consume all the VRAM it can find")
|
||||||
|
parser.add_argument("--disable-opt-split-attention", action='store_true', help="force-disables cross-attention layer optimization")
|
||||||
parser.add_argument("--use-cpu", nargs='+',choices=['SD', 'GFPGAN', 'BSRGAN', 'ESRGAN', 'SCUNet', 'CodeFormer'], help="use CPU as torch device for specified modules", default=[])
|
parser.add_argument("--use-cpu", nargs='+',choices=['SD', 'GFPGAN', 'BSRGAN', 'ESRGAN', 'SCUNet', 'CodeFormer'], help="use CPU as torch device for specified modules", default=[])
|
||||||
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)
|
||||||
@@ -65,6 +70,8 @@ parser.add_argument("--autolaunch", action='store_true', help="open the webui UR
|
|||||||
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="print prompts to console when generating with txt2img and img2img", default=False)
|
||||||
|
parser.add_argument('--vae-path', type=str, help='Path to Variational Autoencoders model', default=None)
|
||||||
|
parser.add_argument("--disable-safe-unpickle", action='store_true', help="disable checking pytorch models for malicious code", default=False)
|
||||||
|
|
||||||
|
|
||||||
cmd_opts = parser.parse_args()
|
cmd_opts = parser.parse_args()
|
||||||
@@ -79,10 +86,18 @@ parallel_processing_allowed = not cmd_opts.lowvram and not cmd_opts.medvram
|
|||||||
xformers_available = False
|
xformers_available = False
|
||||||
config_filename = cmd_opts.ui_settings_file
|
config_filename = cmd_opts.ui_settings_file
|
||||||
|
|
||||||
hypernetworks = hypernetwork.list_hypernetworks(os.path.join(models_path, 'hypernetworks'))
|
os.makedirs(cmd_opts.hypernetwork_dir, exist_ok=True)
|
||||||
|
hypernetworks = hypernetwork.list_hypernetworks(cmd_opts.hypernetwork_dir)
|
||||||
loaded_hypernetwork = None
|
loaded_hypernetwork = None
|
||||||
|
|
||||||
|
|
||||||
|
def reload_hypernetworks():
|
||||||
|
global hypernetworks
|
||||||
|
|
||||||
|
hypernetworks = hypernetwork.list_hypernetworks(cmd_opts.hypernetwork_dir)
|
||||||
|
hypernetwork.load_hypernetwork(opts.sd_hypernetwork)
|
||||||
|
|
||||||
|
|
||||||
class State:
|
class State:
|
||||||
skipped = False
|
skipped = False
|
||||||
interrupted = False
|
interrupted = False
|
||||||
@@ -130,13 +145,14 @@ def realesrgan_models_names():
|
|||||||
|
|
||||||
|
|
||||||
class OptionInfo:
|
class OptionInfo:
|
||||||
def __init__(self, default=None, label="", component=None, component_args=None, onchange=None):
|
def __init__(self, default=None, label="", component=None, component_args=None, onchange=None, show_on_main_page=False):
|
||||||
self.default = default
|
self.default = default
|
||||||
self.label = label
|
self.label = label
|
||||||
self.component = component
|
self.component = component
|
||||||
self.component_args = component_args
|
self.component_args = component_args
|
||||||
self.onchange = onchange
|
self.onchange = onchange
|
||||||
self.section = None
|
self.section = None
|
||||||
|
self.show_on_main_page = show_on_main_page
|
||||||
|
|
||||||
|
|
||||||
def options_section(section_identifier, options_dict):
|
def options_section(section_identifier, options_dict):
|
||||||
@@ -169,6 +185,7 @@ options_templates.update(options_section(('saving-images', "Saving images/grids"
|
|||||||
|
|
||||||
"use_original_name_batch": OptionInfo(False, "Use original name for output filename during batch process in extras tab"),
|
"use_original_name_batch": OptionInfo(False, "Use original name for output filename during batch process in extras tab"),
|
||||||
"save_selected_only": OptionInfo(True, "When using 'Save' button, only save a single selected image"),
|
"save_selected_only": OptionInfo(True, "When using 'Save' button, only save a single selected image"),
|
||||||
|
"do_not_add_watermark": OptionInfo(False, "Do not add watermark to images"),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
options_templates.update(options_section(('saving-paths', "Paths for saving"), {
|
options_templates.update(options_section(('saving-paths', "Paths for saving"), {
|
||||||
@@ -212,8 +229,12 @@ options_templates.update(options_section(('system', "System"), {
|
|||||||
"multiple_tqdm": OptionInfo(True, "Add a second progress bar to the console that shows progress for an entire job."),
|
"multiple_tqdm": OptionInfo(True, "Add a second progress bar to the console that shows progress for an entire job."),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
options_templates.update(options_section(('training', "Training"), {
|
||||||
|
"unload_models_when_training": OptionInfo(False, "Unload VAE and CLIP from VRAM when training"),
|
||||||
|
}))
|
||||||
|
|
||||||
options_templates.update(options_section(('sd', "Stable Diffusion"), {
|
options_templates.update(options_section(('sd', "Stable Diffusion"), {
|
||||||
"sd_model_checkpoint": OptionInfo(None, "Stable Diffusion checkpoint", gr.Dropdown, lambda: {"choices": modules.sd_models.checkpoint_tiles()}),
|
"sd_model_checkpoint": OptionInfo(None, "Stable Diffusion checkpoint", gr.Dropdown, lambda: {"choices": modules.sd_models.checkpoint_tiles()}, show_on_main_page=True),
|
||||||
"sd_hypernetwork": OptionInfo("None", "Stable Diffusion finetune hypernetwork", gr.Dropdown, lambda: {"choices": ["None"] + [x for x in hypernetworks.keys()]}),
|
"sd_hypernetwork": OptionInfo("None", "Stable Diffusion finetune hypernetwork", gr.Dropdown, lambda: {"choices": ["None"] + [x for x in hypernetworks.keys()]}),
|
||||||
"img2img_color_correction": OptionInfo(False, "Apply color correction to img2img results to match original colors."),
|
"img2img_color_correction": OptionInfo(False, "Apply color correction to img2img results to match original colors."),
|
||||||
"save_images_before_color_correction": OptionInfo(False, "Save a copy of image before applying color correction to img2img results"),
|
"save_images_before_color_correction": OptionInfo(False, "Save a copy of image before applying color correction to img2img results"),
|
||||||
@@ -222,8 +243,9 @@ options_templates.update(options_section(('sd', "Stable Diffusion"), {
|
|||||||
"enable_emphasis": OptionInfo(True, "Emphasis: use (text) to make model pay more attention to text and [text] to make it pay less attention"),
|
"enable_emphasis": OptionInfo(True, "Emphasis: use (text) to make model pay more attention to text and [text] to make it pay less attention"),
|
||||||
"use_old_emphasis_implementation": OptionInfo(False, "Use old emphasis implementation. Can be useful to reproduce old seeds."),
|
"use_old_emphasis_implementation": OptionInfo(False, "Use old emphasis implementation. Can be useful to reproduce old seeds."),
|
||||||
"enable_batch_seeds": OptionInfo(True, "Make K-diffusion samplers produce same images in a batch as when making a single image"),
|
"enable_batch_seeds": OptionInfo(True, "Make K-diffusion samplers produce same images in a batch as when making a single image"),
|
||||||
|
"comma_padding_backtrack": OptionInfo(20, "Increase coherency by padding from the last comma within n tokens when using more than 75 tokens", gr.Slider, {"minimum": 0, "maximum": 74, "step": 1 }),
|
||||||
"filter_nsfw": OptionInfo(False, "Filter NSFW content"),
|
"filter_nsfw": OptionInfo(False, "Filter NSFW content"),
|
||||||
'CLIP_ignore_last_layers': OptionInfo(0, "Ignore last layers of CLIP model", gr.Slider, {"minimum": 0, "maximum": 5, "step": 1}),
|
'CLIP_stop_at_last_layers': OptionInfo(1, "Stop At last layers of CLIP model", gr.Slider, {"minimum": 1, "maximum": 12, "step": 1}),
|
||||||
"random_artist_categories": OptionInfo([], "Allowed categories for random artists selection when using the Roll button", gr.CheckboxGroup, {"choices": artist_db.categories()}),
|
"random_artist_categories": OptionInfo([], "Allowed categories for random artists selection when using the Roll button", gr.CheckboxGroup, {"choices": artist_db.categories()}),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
@@ -233,7 +255,8 @@ options_templates.update(options_section(('interrogate', "Interrogate Options"),
|
|||||||
"interrogate_clip_num_beams": OptionInfo(1, "Interrogate: num_beams for BLIP", gr.Slider, {"minimum": 1, "maximum": 16, "step": 1}),
|
"interrogate_clip_num_beams": OptionInfo(1, "Interrogate: num_beams for BLIP", gr.Slider, {"minimum": 1, "maximum": 16, "step": 1}),
|
||||||
"interrogate_clip_min_length": OptionInfo(24, "Interrogate: minimum description length (excluding artists, etc..)", gr.Slider, {"minimum": 1, "maximum": 128, "step": 1}),
|
"interrogate_clip_min_length": OptionInfo(24, "Interrogate: minimum description length (excluding artists, etc..)", gr.Slider, {"minimum": 1, "maximum": 128, "step": 1}),
|
||||||
"interrogate_clip_max_length": OptionInfo(48, "Interrogate: maximum description length", gr.Slider, {"minimum": 1, "maximum": 256, "step": 1}),
|
"interrogate_clip_max_length": OptionInfo(48, "Interrogate: maximum description length", gr.Slider, {"minimum": 1, "maximum": 256, "step": 1}),
|
||||||
"interrogate_clip_dict_limit": OptionInfo(1500, "Interrogate: maximum number of lines in text file (0 = No limit)"),
|
"interrogate_deepbooru_score_threshold": OptionInfo(0.5, "Interrogate: deepbooru score threshold", gr.Slider, {"minimum": 0, "maximum": 1, "step": 0.01}),
|
||||||
|
"deepbooru_sort_alpha": OptionInfo(True, "Interrogate: deepbooru sort alphabetically"),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
options_templates.update(options_section(('ui', "User interface"), {
|
options_templates.update(options_section(('ui', "User interface"), {
|
||||||
@@ -257,6 +280,7 @@ options_templates.update(options_section(('sampler-params', "Sampler parameters"
|
|||||||
's_churn': OptionInfo(0.0, "sigma churn", gr.Slider, {"minimum": 0.0, "maximum": 1.0, "step": 0.01}),
|
's_churn': OptionInfo(0.0, "sigma churn", gr.Slider, {"minimum": 0.0, "maximum": 1.0, "step": 0.01}),
|
||||||
's_tmin': OptionInfo(0.0, "sigma tmin", gr.Slider, {"minimum": 0.0, "maximum": 1.0, "step": 0.01}),
|
's_tmin': OptionInfo(0.0, "sigma tmin", gr.Slider, {"minimum": 0.0, "maximum": 1.0, "step": 0.01}),
|
||||||
's_noise': OptionInfo(1.0, "sigma noise", gr.Slider, {"minimum": 0.0, "maximum": 1.0, "step": 0.01}),
|
's_noise': OptionInfo(1.0, "sigma noise", gr.Slider, {"minimum": 0.0, "maximum": 1.0, "step": 0.01}),
|
||||||
|
'eta_noise_seed_delta': OptionInfo(0, "Eta noise seed delta", gr.Number, {"precision": 0}),
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+28
-7
@@ -10,6 +10,7 @@ from tqdm import tqdm
|
|||||||
from modules import modelloader
|
from modules import modelloader
|
||||||
from modules.shared import cmd_opts, opts, device
|
from modules.shared import cmd_opts, opts, device
|
||||||
from modules.swinir_model_arch import SwinIR as net
|
from modules.swinir_model_arch import SwinIR as net
|
||||||
|
from modules.swinir_model_arch_v2 import Swin2SR as net2
|
||||||
from modules.upscaler import Upscaler, UpscalerData
|
from modules.upscaler import Upscaler, UpscalerData
|
||||||
|
|
||||||
precision_scope = (
|
precision_scope = (
|
||||||
@@ -57,22 +58,42 @@ class UpscalerSwinIR(Upscaler):
|
|||||||
filename = path
|
filename = path
|
||||||
if filename is None or not os.path.exists(filename):
|
if filename is None or not os.path.exists(filename):
|
||||||
return None
|
return None
|
||||||
model = net(
|
if filename.endswith(".v2.pth"):
|
||||||
|
model = net2(
|
||||||
upscale=scale,
|
upscale=scale,
|
||||||
in_chans=3,
|
in_chans=3,
|
||||||
img_size=64,
|
img_size=64,
|
||||||
window_size=8,
|
window_size=8,
|
||||||
img_range=1.0,
|
img_range=1.0,
|
||||||
depths=[6, 6, 6, 6, 6, 6, 6, 6, 6],
|
depths=[6, 6, 6, 6, 6, 6],
|
||||||
embed_dim=240,
|
embed_dim=180,
|
||||||
num_heads=[8, 8, 8, 8, 8, 8, 8, 8, 8],
|
num_heads=[6, 6, 6, 6, 6, 6],
|
||||||
mlp_ratio=2,
|
mlp_ratio=2,
|
||||||
upsampler="nearest+conv",
|
upsampler="nearest+conv",
|
||||||
resi_connection="3conv",
|
resi_connection="1conv",
|
||||||
)
|
)
|
||||||
|
params = None
|
||||||
|
else:
|
||||||
|
model = net(
|
||||||
|
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)
|
pretrained_model = torch.load(filename)
|
||||||
model.load_state_dict(pretrained_model["params_ema"], strict=True)
|
if params is not None:
|
||||||
|
model.load_state_dict(pretrained_model[params], strict=True)
|
||||||
|
else:
|
||||||
|
model.load_state_dict(pretrained_model, strict=True)
|
||||||
if not cmd_opts.no_half:
|
if not cmd_opts.no_half:
|
||||||
model = model.half()
|
model = model.half()
|
||||||
return model
|
return model
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -8,18 +8,17 @@ from torchvision import transforms
|
|||||||
|
|
||||||
import random
|
import random
|
||||||
import tqdm
|
import tqdm
|
||||||
from modules import devices
|
from modules import devices, shared
|
||||||
import re
|
import re
|
||||||
|
|
||||||
re_tag = re.compile(r"[a-zA-Z][_\w\d()]+")
|
re_tag = re.compile(r"[a-zA-Z][_\w\d()]+")
|
||||||
|
|
||||||
|
|
||||||
class PersonalizedBase(Dataset):
|
class PersonalizedBase(Dataset):
|
||||||
def __init__(self, data_root, size=None, repeats=100, flip_p=0.5, placeholder_token="*", width=512, height=512, model=None, device=None, template_file=None):
|
def __init__(self, data_root, width, height, repeats, flip_p=0.5, placeholder_token="*", model=None, device=None, template_file=None, include_cond=False):
|
||||||
|
|
||||||
self.placeholder_token = placeholder_token
|
self.placeholder_token = placeholder_token
|
||||||
|
|
||||||
self.size = size
|
|
||||||
self.width = width
|
self.width = width
|
||||||
self.height = height
|
self.height = height
|
||||||
self.flip = transforms.RandomHorizontalFlip(p=flip_p)
|
self.flip = transforms.RandomHorizontalFlip(p=flip_p)
|
||||||
@@ -33,12 +32,15 @@ class PersonalizedBase(Dataset):
|
|||||||
|
|
||||||
assert data_root, 'dataset directory not specified'
|
assert data_root, 'dataset directory not specified'
|
||||||
|
|
||||||
|
cond_model = shared.sd_model.cond_stage_model
|
||||||
|
|
||||||
self.image_paths = [os.path.join(data_root, file_path) for file_path in os.listdir(data_root)]
|
self.image_paths = [os.path.join(data_root, file_path) for file_path in os.listdir(data_root)]
|
||||||
print("Preparing dataset...")
|
print("Preparing dataset...")
|
||||||
for path in tqdm.tqdm(self.image_paths):
|
for path in tqdm.tqdm(self.image_paths):
|
||||||
image = Image.open(path)
|
try:
|
||||||
image = image.convert('RGB')
|
image = Image.open(path).convert('RGB').resize((self.width, self.height), PIL.Image.BICUBIC)
|
||||||
image = image.resize((self.width, self.height), PIL.Image.BICUBIC)
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
filename = os.path.basename(path)
|
filename = os.path.basename(path)
|
||||||
filename_tokens = os.path.splitext(filename)[0]
|
filename_tokens = os.path.splitext(filename)[0]
|
||||||
@@ -53,7 +55,13 @@ class PersonalizedBase(Dataset):
|
|||||||
init_latent = model.get_first_stage_encoding(model.encode_first_stage(torchdata.unsqueeze(dim=0))).squeeze()
|
init_latent = model.get_first_stage_encoding(model.encode_first_stage(torchdata.unsqueeze(dim=0))).squeeze()
|
||||||
init_latent = init_latent.to(devices.cpu)
|
init_latent = init_latent.to(devices.cpu)
|
||||||
|
|
||||||
self.dataset.append((init_latent, filename_tokens))
|
if include_cond:
|
||||||
|
text = self.create_text(filename_tokens)
|
||||||
|
cond = cond_model([text]).to(devices.cpu)
|
||||||
|
else:
|
||||||
|
cond = None
|
||||||
|
|
||||||
|
self.dataset.append((init_latent, filename_tokens, cond))
|
||||||
|
|
||||||
self.length = len(self.dataset) * repeats
|
self.length = len(self.dataset) * repeats
|
||||||
|
|
||||||
@@ -64,6 +72,12 @@ class PersonalizedBase(Dataset):
|
|||||||
def shuffle(self):
|
def shuffle(self):
|
||||||
self.indexes = self.initial_indexes[torch.randperm(self.initial_indexes.shape[0])]
|
self.indexes = self.initial_indexes[torch.randperm(self.initial_indexes.shape[0])]
|
||||||
|
|
||||||
|
def create_text(self, filename_tokens):
|
||||||
|
text = random.choice(self.lines)
|
||||||
|
text = text.replace("[name]", self.placeholder_token)
|
||||||
|
text = text.replace("[filewords]", ' '.join(filename_tokens))
|
||||||
|
return text
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self.length
|
return self.length
|
||||||
|
|
||||||
@@ -72,10 +86,7 @@ class PersonalizedBase(Dataset):
|
|||||||
self.shuffle()
|
self.shuffle()
|
||||||
|
|
||||||
index = self.indexes[i % len(self.indexes)]
|
index = self.indexes[i % len(self.indexes)]
|
||||||
x, filename_tokens = self.dataset[index]
|
x, filename_tokens, cond = self.dataset[index]
|
||||||
|
|
||||||
text = random.choice(self.lines)
|
text = self.create_text(filename_tokens)
|
||||||
text = text.replace("[name]", self.placeholder_token)
|
return x, text, cond
|
||||||
text = text.replace("[filewords]", ' '.join(filename_tokens))
|
|
||||||
|
|
||||||
return x, text
|
|
||||||
@@ -0,0 +1,219 @@
|
|||||||
|
import base64
|
||||||
|
import json
|
||||||
|
import numpy as np
|
||||||
|
import zlib
|
||||||
|
from PIL import Image, PngImagePlugin, ImageDraw, ImageFont
|
||||||
|
from fonts.ttf import Roboto
|
||||||
|
import torch
|
||||||
|
|
||||||
|
|
||||||
|
class EmbeddingEncoder(json.JSONEncoder):
|
||||||
|
def default(self, obj):
|
||||||
|
if isinstance(obj, torch.Tensor):
|
||||||
|
return {'TORCHTENSOR': obj.cpu().detach().numpy().tolist()}
|
||||||
|
return json.JSONEncoder.default(self, obj)
|
||||||
|
|
||||||
|
|
||||||
|
class EmbeddingDecoder(json.JSONDecoder):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
json.JSONDecoder.__init__(self, object_hook=self.object_hook, *args, **kwargs)
|
||||||
|
|
||||||
|
def object_hook(self, d):
|
||||||
|
if 'TORCHTENSOR' in d:
|
||||||
|
return torch.from_numpy(np.array(d['TORCHTENSOR']))
|
||||||
|
return d
|
||||||
|
|
||||||
|
|
||||||
|
def embedding_to_b64(data):
|
||||||
|
d = json.dumps(data, cls=EmbeddingEncoder)
|
||||||
|
return base64.b64encode(d.encode())
|
||||||
|
|
||||||
|
|
||||||
|
def embedding_from_b64(data):
|
||||||
|
d = base64.b64decode(data)
|
||||||
|
return json.loads(d, cls=EmbeddingDecoder)
|
||||||
|
|
||||||
|
|
||||||
|
def lcg(m=2**32, a=1664525, c=1013904223, seed=0):
|
||||||
|
while True:
|
||||||
|
seed = (a * seed + c) % m
|
||||||
|
yield seed % 255
|
||||||
|
|
||||||
|
|
||||||
|
def xor_block(block):
|
||||||
|
g = lcg()
|
||||||
|
randblock = np.array([next(g) for _ in range(np.product(block.shape))]).astype(np.uint8).reshape(block.shape)
|
||||||
|
return np.bitwise_xor(block.astype(np.uint8), randblock & 0x0F)
|
||||||
|
|
||||||
|
|
||||||
|
def style_block(block, sequence):
|
||||||
|
im = Image.new('RGB', (block.shape[1], block.shape[0]))
|
||||||
|
draw = ImageDraw.Draw(im)
|
||||||
|
i = 0
|
||||||
|
for x in range(-6, im.size[0], 8):
|
||||||
|
for yi, y in enumerate(range(-6, im.size[1], 8)):
|
||||||
|
offset = 0
|
||||||
|
if yi % 2 == 0:
|
||||||
|
offset = 4
|
||||||
|
shade = sequence[i % len(sequence)]
|
||||||
|
i += 1
|
||||||
|
draw.ellipse((x+offset, y, x+6+offset, y+6), fill=(shade, shade, shade))
|
||||||
|
|
||||||
|
fg = np.array(im).astype(np.uint8) & 0xF0
|
||||||
|
|
||||||
|
return block ^ fg
|
||||||
|
|
||||||
|
|
||||||
|
def insert_image_data_embed(image, data):
|
||||||
|
d = 3
|
||||||
|
data_compressed = zlib.compress(json.dumps(data, cls=EmbeddingEncoder).encode(), level=9)
|
||||||
|
data_np_ = np.frombuffer(data_compressed, np.uint8).copy()
|
||||||
|
data_np_high = data_np_ >> 4
|
||||||
|
data_np_low = data_np_ & 0x0F
|
||||||
|
|
||||||
|
h = image.size[1]
|
||||||
|
next_size = data_np_low.shape[0] + (h-(data_np_low.shape[0] % h))
|
||||||
|
next_size = next_size + ((h*d)-(next_size % (h*d)))
|
||||||
|
|
||||||
|
data_np_low.resize(next_size)
|
||||||
|
data_np_low = data_np_low.reshape((h, -1, d))
|
||||||
|
|
||||||
|
data_np_high.resize(next_size)
|
||||||
|
data_np_high = data_np_high.reshape((h, -1, d))
|
||||||
|
|
||||||
|
edge_style = list(data['string_to_param'].values())[0].cpu().detach().numpy().tolist()[0][:1024]
|
||||||
|
edge_style = (np.abs(edge_style)/np.max(np.abs(edge_style))*255).astype(np.uint8)
|
||||||
|
|
||||||
|
data_np_low = style_block(data_np_low, sequence=edge_style)
|
||||||
|
data_np_low = xor_block(data_np_low)
|
||||||
|
data_np_high = style_block(data_np_high, sequence=edge_style[::-1])
|
||||||
|
data_np_high = xor_block(data_np_high)
|
||||||
|
|
||||||
|
im_low = Image.fromarray(data_np_low, mode='RGB')
|
||||||
|
im_high = Image.fromarray(data_np_high, mode='RGB')
|
||||||
|
|
||||||
|
background = Image.new('RGB', (image.size[0]+im_low.size[0]+im_high.size[0]+2, image.size[1]), (0, 0, 0))
|
||||||
|
background.paste(im_low, (0, 0))
|
||||||
|
background.paste(image, (im_low.size[0]+1, 0))
|
||||||
|
background.paste(im_high, (im_low.size[0]+1+image.size[0]+1, 0))
|
||||||
|
|
||||||
|
return background
|
||||||
|
|
||||||
|
|
||||||
|
def crop_black(img, tol=0):
|
||||||
|
mask = (img > tol).all(2)
|
||||||
|
mask0, mask1 = mask.any(0), mask.any(1)
|
||||||
|
col_start, col_end = mask0.argmax(), mask.shape[1]-mask0[::-1].argmax()
|
||||||
|
row_start, row_end = mask1.argmax(), mask.shape[0]-mask1[::-1].argmax()
|
||||||
|
return img[row_start:row_end, col_start:col_end]
|
||||||
|
|
||||||
|
|
||||||
|
def extract_image_data_embed(image):
|
||||||
|
d = 3
|
||||||
|
outarr = crop_black(np.array(image.convert('RGB').getdata()).reshape(image.size[1], image.size[0], d).astype(np.uint8)) & 0x0F
|
||||||
|
black_cols = np.where(np.sum(outarr, axis=(0, 2)) == 0)
|
||||||
|
if black_cols[0].shape[0] < 2:
|
||||||
|
print('No Image data blocks found.')
|
||||||
|
return None
|
||||||
|
|
||||||
|
data_block_lower = outarr[:, :black_cols[0].min(), :].astype(np.uint8)
|
||||||
|
data_block_upper = outarr[:, black_cols[0].max()+1:, :].astype(np.uint8)
|
||||||
|
|
||||||
|
data_block_lower = xor_block(data_block_lower)
|
||||||
|
data_block_upper = xor_block(data_block_upper)
|
||||||
|
|
||||||
|
data_block = (data_block_upper << 4) | (data_block_lower)
|
||||||
|
data_block = data_block.flatten().tobytes()
|
||||||
|
|
||||||
|
data = zlib.decompress(data_block)
|
||||||
|
return json.loads(data, cls=EmbeddingDecoder)
|
||||||
|
|
||||||
|
|
||||||
|
def caption_image_overlay(srcimage, title, footerLeft, footerMid, footerRight, textfont=None):
|
||||||
|
from math import cos
|
||||||
|
|
||||||
|
image = srcimage.copy()
|
||||||
|
|
||||||
|
if textfont is None:
|
||||||
|
try:
|
||||||
|
textfont = ImageFont.truetype(opts.font or Roboto, fontsize)
|
||||||
|
textfont = opts.font or Roboto
|
||||||
|
except Exception:
|
||||||
|
textfont = Roboto
|
||||||
|
|
||||||
|
factor = 1.5
|
||||||
|
gradient = Image.new('RGBA', (1, image.size[1]), color=(0, 0, 0, 0))
|
||||||
|
for y in range(image.size[1]):
|
||||||
|
mag = 1-cos(y/image.size[1]*factor)
|
||||||
|
mag = max(mag, 1-cos((image.size[1]-y)/image.size[1]*factor*1.1))
|
||||||
|
gradient.putpixel((0, y), (0, 0, 0, int(mag*255)))
|
||||||
|
image = Image.alpha_composite(image.convert('RGBA'), gradient.resize(image.size))
|
||||||
|
|
||||||
|
draw = ImageDraw.Draw(image)
|
||||||
|
fontsize = 32
|
||||||
|
font = ImageFont.truetype(textfont, fontsize)
|
||||||
|
padding = 10
|
||||||
|
|
||||||
|
_, _, w, h = draw.textbbox((0, 0), title, font=font)
|
||||||
|
fontsize = min(int(fontsize * (((image.size[0]*0.75)-(padding*4))/w)), 72)
|
||||||
|
font = ImageFont.truetype(textfont, fontsize)
|
||||||
|
_, _, w, h = draw.textbbox((0, 0), title, font=font)
|
||||||
|
draw.text((padding, padding), title, anchor='lt', font=font, fill=(255, 255, 255, 230))
|
||||||
|
|
||||||
|
_, _, w, h = draw.textbbox((0, 0), footerLeft, font=font)
|
||||||
|
fontsize_left = min(int(fontsize * (((image.size[0]/3)-(padding))/w)), 72)
|
||||||
|
_, _, w, h = draw.textbbox((0, 0), footerMid, font=font)
|
||||||
|
fontsize_mid = min(int(fontsize * (((image.size[0]/3)-(padding))/w)), 72)
|
||||||
|
_, _, w, h = draw.textbbox((0, 0), footerRight, font=font)
|
||||||
|
fontsize_right = min(int(fontsize * (((image.size[0]/3)-(padding))/w)), 72)
|
||||||
|
|
||||||
|
font = ImageFont.truetype(textfont, min(fontsize_left, fontsize_mid, fontsize_right))
|
||||||
|
|
||||||
|
draw.text((padding, image.size[1]-padding), footerLeft, anchor='ls', font=font, fill=(255, 255, 255, 230))
|
||||||
|
draw.text((image.size[0]/2, image.size[1]-padding), footerMid, anchor='ms', font=font, fill=(255, 255, 255, 230))
|
||||||
|
draw.text((image.size[0]-padding, image.size[1]-padding), footerRight, anchor='rs', font=font, fill=(255, 255, 255, 230))
|
||||||
|
|
||||||
|
return image
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
|
||||||
|
testEmbed = Image.open('test_embedding.png')
|
||||||
|
data = extract_image_data_embed(testEmbed)
|
||||||
|
assert data is not None
|
||||||
|
|
||||||
|
data = embedding_from_b64(testEmbed.text['sd-ti-embedding'])
|
||||||
|
assert data is not None
|
||||||
|
|
||||||
|
image = Image.new('RGBA', (512, 512), (255, 255, 200, 255))
|
||||||
|
cap_image = caption_image_overlay(image, 'title', 'footerLeft', 'footerMid', 'footerRight')
|
||||||
|
|
||||||
|
test_embed = {'string_to_param': {'*': torch.from_numpy(np.random.random((2, 4096)))}}
|
||||||
|
|
||||||
|
embedded_image = insert_image_data_embed(cap_image, test_embed)
|
||||||
|
|
||||||
|
retrived_embed = extract_image_data_embed(embedded_image)
|
||||||
|
|
||||||
|
assert str(retrived_embed) == str(test_embed)
|
||||||
|
|
||||||
|
embedded_image2 = insert_image_data_embed(cap_image, retrived_embed)
|
||||||
|
|
||||||
|
assert embedded_image == embedded_image2
|
||||||
|
|
||||||
|
g = lcg()
|
||||||
|
shared_random = np.array([next(g) for _ in range(100)]).astype(np.uint8).tolist()
|
||||||
|
|
||||||
|
reference_random = [253, 242, 127, 44, 157, 27, 239, 133, 38, 79, 167, 4, 177,
|
||||||
|
95, 130, 79, 78, 14, 52, 215, 220, 194, 126, 28, 240, 179,
|
||||||
|
160, 153, 149, 50, 105, 14, 21, 218, 199, 18, 54, 198, 193,
|
||||||
|
38, 128, 19, 53, 195, 124, 75, 205, 12, 6, 145, 0, 28,
|
||||||
|
30, 148, 8, 45, 218, 171, 55, 249, 97, 166, 12, 35, 0,
|
||||||
|
41, 221, 122, 215, 170, 31, 113, 186, 97, 119, 31, 23, 185,
|
||||||
|
66, 140, 30, 41, 37, 63, 137, 109, 216, 55, 159, 145, 82,
|
||||||
|
204, 86, 73, 222, 44, 198, 118, 240, 97]
|
||||||
|
|
||||||
|
assert shared_random == reference_random
|
||||||
|
|
||||||
|
hunna_kay_random_sum = sum(np.array([next(g) for _ in range(100000)]).astype(np.uint8).tolist())
|
||||||
|
|
||||||
|
assert 12731374 == hunna_kay_random_sum
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
|
||||||
|
class LearnSchedule:
|
||||||
|
def __init__(self, learn_rate, max_steps, cur_step=0):
|
||||||
|
pairs = learn_rate.split(',')
|
||||||
|
self.rates = []
|
||||||
|
self.it = 0
|
||||||
|
self.maxit = 0
|
||||||
|
for i, pair in enumerate(pairs):
|
||||||
|
tmp = pair.split(':')
|
||||||
|
if len(tmp) == 2:
|
||||||
|
step = int(tmp[1])
|
||||||
|
if step > cur_step:
|
||||||
|
self.rates.append((float(tmp[0]), min(step, max_steps)))
|
||||||
|
self.maxit += 1
|
||||||
|
if step > max_steps:
|
||||||
|
return
|
||||||
|
elif step == -1:
|
||||||
|
self.rates.append((float(tmp[0]), max_steps))
|
||||||
|
self.maxit += 1
|
||||||
|
return
|
||||||
|
else:
|
||||||
|
self.rates.append((float(tmp[0]), max_steps))
|
||||||
|
self.maxit += 1
|
||||||
|
return
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __next__(self):
|
||||||
|
if self.it < self.maxit:
|
||||||
|
self.it += 1
|
||||||
|
return self.rates[self.it - 1]
|
||||||
|
else:
|
||||||
|
raise StopIteration
|
||||||
@@ -3,12 +3,16 @@ from PIL import Image, ImageOps
|
|||||||
import platform
|
import platform
|
||||||
import sys
|
import sys
|
||||||
import tqdm
|
import tqdm
|
||||||
|
import time
|
||||||
|
|
||||||
from modules import shared, images
|
from modules import shared, images
|
||||||
|
from modules.shared import opts, cmd_opts
|
||||||
|
if cmd_opts.deepdanbooru:
|
||||||
|
import modules.deepbooru as deepbooru
|
||||||
|
|
||||||
|
def preprocess(process_src, process_dst, process_width, process_height, process_flip, process_split, process_caption, process_caption_deepbooru=False):
|
||||||
def preprocess(process_src, process_dst, process_flip, process_split, process_caption):
|
width = process_width
|
||||||
size = 512
|
height = process_height
|
||||||
src = os.path.abspath(process_src)
|
src = os.path.abspath(process_src)
|
||||||
dst = os.path.abspath(process_dst)
|
dst = os.path.abspath(process_dst)
|
||||||
|
|
||||||
@@ -24,10 +28,21 @@ def preprocess(process_src, process_dst, process_flip, process_split, process_ca
|
|||||||
if process_caption:
|
if process_caption:
|
||||||
shared.interrogator.load()
|
shared.interrogator.load()
|
||||||
|
|
||||||
|
if process_caption_deepbooru:
|
||||||
|
deepbooru.create_deepbooru_process(opts.interrogate_deepbooru_score_threshold, opts.deepbooru_sort_alpha)
|
||||||
|
|
||||||
def save_pic_with_caption(image, index):
|
def save_pic_with_caption(image, index):
|
||||||
if process_caption:
|
if process_caption:
|
||||||
caption = "-" + shared.interrogator.generate_caption(image)
|
caption = "-" + shared.interrogator.generate_caption(image)
|
||||||
caption = sanitize_caption(os.path.join(dst, f"{index:05}-{subindex[0]}"), caption, ".png")
|
caption = sanitize_caption(os.path.join(dst, f"{index:05}-{subindex[0]}"), caption, ".png")
|
||||||
|
elif process_caption_deepbooru:
|
||||||
|
shared.deepbooru_process_return["value"] = -1
|
||||||
|
shared.deepbooru_process_queue.put(image)
|
||||||
|
while shared.deepbooru_process_return["value"] == -1:
|
||||||
|
time.sleep(0.2)
|
||||||
|
caption = "-" + shared.deepbooru_process_return["value"]
|
||||||
|
caption = sanitize_caption(os.path.join(dst, f"{index:05}-{subindex[0]}"), caption, ".png")
|
||||||
|
shared.deepbooru_process_return["value"] = -1
|
||||||
else:
|
else:
|
||||||
caption = filename
|
caption = filename
|
||||||
caption = os.path.splitext(caption)[0]
|
caption = os.path.splitext(caption)[0]
|
||||||
@@ -45,7 +60,10 @@ def preprocess(process_src, process_dst, process_flip, process_split, process_ca
|
|||||||
for index, imagefile in enumerate(tqdm.tqdm(files)):
|
for index, imagefile in enumerate(tqdm.tqdm(files)):
|
||||||
subindex = [0]
|
subindex = [0]
|
||||||
filename = os.path.join(src, imagefile)
|
filename = os.path.join(src, imagefile)
|
||||||
img = Image.open(filename).convert("RGB")
|
try:
|
||||||
|
img = Image.open(filename).convert("RGB")
|
||||||
|
except Exception:
|
||||||
|
continue
|
||||||
|
|
||||||
if shared.state.interrupted:
|
if shared.state.interrupted:
|
||||||
break
|
break
|
||||||
@@ -55,23 +73,23 @@ def preprocess(process_src, process_dst, process_flip, process_split, process_ca
|
|||||||
is_wide = ratio < 1 / 1.35
|
is_wide = ratio < 1 / 1.35
|
||||||
|
|
||||||
if process_split and is_tall:
|
if process_split and is_tall:
|
||||||
img = img.resize((size, size * img.height // img.width))
|
img = img.resize((width, height * img.height // img.width))
|
||||||
|
|
||||||
top = img.crop((0, 0, size, size))
|
top = img.crop((0, 0, width, height))
|
||||||
save_pic(top, index)
|
save_pic(top, index)
|
||||||
|
|
||||||
bot = img.crop((0, img.height - size, size, img.height))
|
bot = img.crop((0, img.height - height, width, img.height))
|
||||||
save_pic(bot, index)
|
save_pic(bot, index)
|
||||||
elif process_split and is_wide:
|
elif process_split and is_wide:
|
||||||
img = img.resize((size * img.width // img.height, size))
|
img = img.resize((width * img.width // img.height, height))
|
||||||
|
|
||||||
left = img.crop((0, 0, size, size))
|
left = img.crop((0, 0, width, height))
|
||||||
save_pic(left, index)
|
save_pic(left, index)
|
||||||
|
|
||||||
right = img.crop((img.width - size, 0, img.width, size))
|
right = img.crop((img.width - width, 0, img.width, height))
|
||||||
save_pic(right, index)
|
save_pic(right, index)
|
||||||
else:
|
else:
|
||||||
img = images.resize_image(1, img, size, size)
|
img = images.resize_image(1, img, width, height)
|
||||||
save_pic(img, index)
|
save_pic(img, index)
|
||||||
|
|
||||||
shared.state.nextjob()
|
shared.state.nextjob()
|
||||||
@@ -79,6 +97,10 @@ def preprocess(process_src, process_dst, process_flip, process_split, process_ca
|
|||||||
if process_caption:
|
if process_caption:
|
||||||
shared.interrogator.send_blip_to_ram()
|
shared.interrogator.send_blip_to_ram()
|
||||||
|
|
||||||
|
if process_caption_deepbooru:
|
||||||
|
deepbooru.release_process()
|
||||||
|
|
||||||
|
|
||||||
def sanitize_caption(base_path, original_caption, suffix):
|
def sanitize_caption(base_path, original_caption, suffix):
|
||||||
operating_system = platform.system().lower()
|
operating_system = platform.system().lower()
|
||||||
if (operating_system == "windows"):
|
if (operating_system == "windows"):
|
||||||
|
|||||||
Binary file not shown.
|
After Width: | Height: | Size: 478 KiB |
@@ -7,10 +7,15 @@ import tqdm
|
|||||||
import html
|
import html
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
from PIL import Image, PngImagePlugin
|
||||||
|
|
||||||
from modules import shared, devices, sd_hijack, processing, sd_models
|
from modules import shared, devices, sd_hijack, processing, sd_models
|
||||||
import modules.textual_inversion.dataset
|
import modules.textual_inversion.dataset
|
||||||
|
from modules.textual_inversion.learn_schedule import LearnSchedule
|
||||||
|
|
||||||
|
from modules.textual_inversion.image_embedding import (embedding_to_b64, embedding_from_b64,
|
||||||
|
insert_image_data_embed, extract_image_data_embed,
|
||||||
|
caption_image_overlay)
|
||||||
|
|
||||||
class Embedding:
|
class Embedding:
|
||||||
def __init__(self, vec, name, step=None):
|
def __init__(self, vec, name, step=None):
|
||||||
@@ -80,7 +85,18 @@ class EmbeddingDatabase:
|
|||||||
def process_file(path, filename):
|
def process_file(path, filename):
|
||||||
name = os.path.splitext(filename)[0]
|
name = os.path.splitext(filename)[0]
|
||||||
|
|
||||||
data = torch.load(path, map_location="cpu")
|
data = []
|
||||||
|
|
||||||
|
if filename.upper().endswith('.PNG'):
|
||||||
|
embed_image = Image.open(path)
|
||||||
|
if 'sd-ti-embedding' in embed_image.text:
|
||||||
|
data = embedding_from_b64(embed_image.text['sd-ti-embedding'])
|
||||||
|
name = data.get('name', name)
|
||||||
|
else:
|
||||||
|
data = extract_image_data_embed(embed_image)
|
||||||
|
name = data.get('name', name)
|
||||||
|
else:
|
||||||
|
data = torch.load(path, map_location="cpu")
|
||||||
|
|
||||||
# textual inversion embeddings
|
# textual inversion embeddings
|
||||||
if 'string_to_param' in data:
|
if 'string_to_param' in data:
|
||||||
@@ -156,7 +172,8 @@ def create_embedding(name, num_vectors_per_token, init_text='*'):
|
|||||||
return fn
|
return fn
|
||||||
|
|
||||||
|
|
||||||
def train_embedding(embedding_name, learn_rate, data_root, log_directory, steps, create_image_every, save_embedding_every, template_file):
|
|
||||||
|
def train_embedding(embedding_name, learn_rate, data_root, log_directory, training_width, training_height, steps, num_repeats, create_image_every, save_embedding_every, template_file, save_image_with_stored_embedding, preview_image_prompt):
|
||||||
assert embedding_name, 'embedding not selected'
|
assert embedding_name, 'embedding not selected'
|
||||||
|
|
||||||
shared.state.textinfo = "Initializing textual inversion training..."
|
shared.state.textinfo = "Initializing textual inversion training..."
|
||||||
@@ -178,19 +195,23 @@ def train_embedding(embedding_name, learn_rate, data_root, log_directory, steps,
|
|||||||
else:
|
else:
|
||||||
images_dir = None
|
images_dir = None
|
||||||
|
|
||||||
|
if create_image_every > 0 and save_image_with_stored_embedding:
|
||||||
|
images_embeds_dir = os.path.join(log_directory, "image_embeddings")
|
||||||
|
os.makedirs(images_embeds_dir, exist_ok=True)
|
||||||
|
else:
|
||||||
|
images_embeds_dir = None
|
||||||
|
|
||||||
cond_model = shared.sd_model.cond_stage_model
|
cond_model = shared.sd_model.cond_stage_model
|
||||||
|
|
||||||
shared.state.textinfo = f"Preparing dataset from {html.escape(data_root)}..."
|
shared.state.textinfo = f"Preparing dataset from {html.escape(data_root)}..."
|
||||||
with torch.autocast("cuda"):
|
with torch.autocast("cuda"):
|
||||||
ds = modules.textual_inversion.dataset.PersonalizedBase(data_root=data_root, size=512, placeholder_token=embedding_name, model=shared.sd_model, device=devices.device, template_file=template_file)
|
ds = modules.textual_inversion.dataset.PersonalizedBase(data_root=data_root, width=training_width, height=training_height, repeats=num_repeats, placeholder_token=embedding_name, model=shared.sd_model, device=devices.device, template_file=template_file)
|
||||||
|
|
||||||
hijack = sd_hijack.model_hijack
|
hijack = sd_hijack.model_hijack
|
||||||
|
|
||||||
embedding = hijack.embedding_db.word_embeddings[embedding_name]
|
embedding = hijack.embedding_db.word_embeddings[embedding_name]
|
||||||
embedding.vec.requires_grad = True
|
embedding.vec.requires_grad = True
|
||||||
|
|
||||||
optimizer = torch.optim.AdamW([embedding.vec], lr=learn_rate)
|
|
||||||
|
|
||||||
losses = torch.zeros((32,))
|
losses = torch.zeros((32,))
|
||||||
|
|
||||||
last_saved_file = "<none>"
|
last_saved_file = "<none>"
|
||||||
@@ -200,12 +221,24 @@ def train_embedding(embedding_name, learn_rate, data_root, log_directory, steps,
|
|||||||
if ititial_step > steps:
|
if ititial_step > steps:
|
||||||
return embedding, filename
|
return embedding, filename
|
||||||
|
|
||||||
|
schedules = iter(LearnSchedule(learn_rate, steps, ititial_step))
|
||||||
|
(learn_rate, end_step) = next(schedules)
|
||||||
|
print(f'Training at rate of {learn_rate} until step {end_step}')
|
||||||
|
|
||||||
|
optimizer = torch.optim.AdamW([embedding.vec], lr=learn_rate)
|
||||||
|
|
||||||
pbar = tqdm.tqdm(enumerate(ds), total=steps-ititial_step)
|
pbar = tqdm.tqdm(enumerate(ds), total=steps-ititial_step)
|
||||||
for i, (x, text) in pbar:
|
for i, (x, text, _) in pbar:
|
||||||
embedding.step = i + ititial_step
|
embedding.step = i + ititial_step
|
||||||
|
|
||||||
if embedding.step > steps:
|
if embedding.step > end_step:
|
||||||
break
|
try:
|
||||||
|
(learn_rate, end_step) = next(schedules)
|
||||||
|
except:
|
||||||
|
break
|
||||||
|
tqdm.tqdm.write(f'Training at rate of {learn_rate} until step {end_step}')
|
||||||
|
for pg in optimizer.param_groups:
|
||||||
|
pg['lr'] = learn_rate
|
||||||
|
|
||||||
if shared.state.interrupted:
|
if shared.state.interrupted:
|
||||||
break
|
break
|
||||||
@@ -223,7 +256,10 @@ def train_embedding(embedding_name, learn_rate, data_root, log_directory, steps,
|
|||||||
loss.backward()
|
loss.backward()
|
||||||
optimizer.step()
|
optimizer.step()
|
||||||
|
|
||||||
pbar.set_description(f"loss: {losses.mean():.7f}")
|
epoch_num = embedding.step // len(ds)
|
||||||
|
epoch_step = embedding.step - (epoch_num * len(ds)) + 1
|
||||||
|
|
||||||
|
pbar.set_description(f"[Epoch {epoch_num}: {epoch_step}/{len(ds)}]loss: {losses.mean():.7f}")
|
||||||
|
|
||||||
if embedding.step > 0 and embedding_dir is not None and embedding.step % save_embedding_every == 0:
|
if embedding.step > 0 and embedding_dir is not None and embedding.step % save_embedding_every == 0:
|
||||||
last_saved_file = os.path.join(embedding_dir, f'{embedding_name}-{embedding.step}.pt')
|
last_saved_file = os.path.join(embedding_dir, f'{embedding_name}-{embedding.step}.pt')
|
||||||
@@ -232,10 +268,14 @@ def train_embedding(embedding_name, learn_rate, data_root, log_directory, steps,
|
|||||||
if embedding.step > 0 and images_dir is not None and embedding.step % create_image_every == 0:
|
if embedding.step > 0 and images_dir is not None and embedding.step % create_image_every == 0:
|
||||||
last_saved_image = os.path.join(images_dir, f'{embedding_name}-{embedding.step}.png')
|
last_saved_image = os.path.join(images_dir, f'{embedding_name}-{embedding.step}.png')
|
||||||
|
|
||||||
|
preview_text = text if preview_image_prompt == "" else preview_image_prompt
|
||||||
|
|
||||||
p = processing.StableDiffusionProcessingTxt2Img(
|
p = processing.StableDiffusionProcessingTxt2Img(
|
||||||
sd_model=shared.sd_model,
|
sd_model=shared.sd_model,
|
||||||
prompt=text,
|
prompt=preview_text,
|
||||||
steps=20,
|
steps=20,
|
||||||
|
height=training_height,
|
||||||
|
width=training_width,
|
||||||
do_not_save_grid=True,
|
do_not_save_grid=True,
|
||||||
do_not_save_samples=True,
|
do_not_save_samples=True,
|
||||||
)
|
)
|
||||||
@@ -244,9 +284,29 @@ def train_embedding(embedding_name, learn_rate, data_root, log_directory, steps,
|
|||||||
image = processed.images[0]
|
image = processed.images[0]
|
||||||
|
|
||||||
shared.state.current_image = image
|
shared.state.current_image = image
|
||||||
|
|
||||||
|
if save_image_with_stored_embedding and os.path.exists(last_saved_file):
|
||||||
|
|
||||||
|
last_saved_image_chunks = os.path.join(images_embeds_dir, f'{embedding_name}-{embedding.step}.png')
|
||||||
|
|
||||||
|
info = PngImagePlugin.PngInfo()
|
||||||
|
data = torch.load(last_saved_file)
|
||||||
|
info.add_text("sd-ti-embedding", embedding_to_b64(data))
|
||||||
|
|
||||||
|
title = "<{}>".format(data.get('name', '???'))
|
||||||
|
checkpoint = sd_models.select_checkpoint()
|
||||||
|
footer_left = checkpoint.model_name
|
||||||
|
footer_mid = '[{}]'.format(checkpoint.hash)
|
||||||
|
footer_right = '{}'.format(embedding.step)
|
||||||
|
|
||||||
|
captioned_image = caption_image_overlay(image, title, footer_left, footer_mid, footer_right)
|
||||||
|
captioned_image = insert_image_data_embed(captioned_image, data)
|
||||||
|
|
||||||
|
captioned_image.save(last_saved_image_chunks, "PNG", pnginfo=info)
|
||||||
|
|
||||||
image.save(last_saved_image)
|
image.save(last_saved_image)
|
||||||
|
|
||||||
last_saved_image += f", prompt: {text}"
|
last_saved_image += f", prompt: {preview_text}"
|
||||||
|
|
||||||
shared.state.job_no = embedding.step
|
shared.state.job_no = embedding.step
|
||||||
|
|
||||||
@@ -268,4 +328,3 @@ Last saved image: {html.escape(last_saved_image)}<br/>
|
|||||||
embedding.save(filename)
|
embedding.save(filename)
|
||||||
|
|
||||||
return embedding, filename
|
return embedding, filename
|
||||||
|
|
||||||
@@ -23,6 +23,8 @@ def preprocess(*args):
|
|||||||
|
|
||||||
def train_embedding(*args):
|
def train_embedding(*args):
|
||||||
|
|
||||||
|
assert not shared.cmd_opts.lowvram, 'Training models with lowvram not possible'
|
||||||
|
|
||||||
try:
|
try:
|
||||||
sd_hijack.undo_optimizations()
|
sd_hijack.undo_optimizations()
|
||||||
|
|
||||||
|
|||||||
+179
-53
@@ -39,6 +39,7 @@ import modules.generation_parameters_copypaste
|
|||||||
from modules import prompt_parser
|
from modules import prompt_parser
|
||||||
from modules.images import save_image
|
from modules.images import save_image
|
||||||
import modules.textual_inversion.ui
|
import modules.textual_inversion.ui
|
||||||
|
import modules.hypernetworks.ui
|
||||||
|
|
||||||
# this is a fix for Windows users. Without it, javascript files will be served with text/html content-type and the browser will not show any UI
|
# this is a fix for Windows users. Without it, javascript files will be served with text/html content-type and the browser will not show any UI
|
||||||
mimetypes.init()
|
mimetypes.init()
|
||||||
@@ -50,6 +51,11 @@ if not cmd_opts.share and not cmd_opts.listen:
|
|||||||
gradio.utils.version_check = lambda: None
|
gradio.utils.version_check = lambda: None
|
||||||
gradio.utils.get_local_ip_address = lambda: '127.0.0.1'
|
gradio.utils.get_local_ip_address = lambda: '127.0.0.1'
|
||||||
|
|
||||||
|
if cmd_opts.ngrok != None:
|
||||||
|
import modules.ngrok as ngrok
|
||||||
|
print('ngrok authtoken detected, trying to connect...')
|
||||||
|
ngrok.connect(cmd_opts.ngrok, cmd_opts.port if cmd_opts.port != None else 7860)
|
||||||
|
|
||||||
|
|
||||||
def gr_show(visible=True):
|
def gr_show(visible=True):
|
||||||
return {"visible": visible, "__type__": "update"}
|
return {"visible": visible, "__type__": "update"}
|
||||||
@@ -101,7 +107,7 @@ def send_gradio_gallery_to_image(x):
|
|||||||
|
|
||||||
|
|
||||||
def save_files(js_data, images, do_make_zip, index):
|
def save_files(js_data, images, do_make_zip, index):
|
||||||
import csv
|
import csv
|
||||||
filenames = []
|
filenames = []
|
||||||
fullfns = []
|
fullfns = []
|
||||||
|
|
||||||
@@ -125,6 +131,8 @@ def save_files(js_data, images, do_make_zip, index):
|
|||||||
images = [images[index]]
|
images = [images[index]]
|
||||||
start_index = index
|
start_index = index
|
||||||
|
|
||||||
|
os.makedirs(opts.outdir_save, exist_ok=True)
|
||||||
|
|
||||||
with open(os.path.join(opts.outdir_save, "log.csv"), "a", encoding="utf8", newline='') as file:
|
with open(os.path.join(opts.outdir_save, "log.csv"), "a", encoding="utf8", newline='') as file:
|
||||||
at_start = file.tell() == 0
|
at_start = file.tell() == 0
|
||||||
writer = csv.writer(file)
|
writer = csv.writer(file)
|
||||||
@@ -175,8 +183,15 @@ def wrap_gradio_call(func, extra_outputs=None):
|
|||||||
try:
|
try:
|
||||||
res = list(func(*args, **kwargs))
|
res = list(func(*args, **kwargs))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
# When printing out our debug argument list, do not print out more than a MB of text
|
||||||
|
max_debug_str_len = 131072 # (1024*1024)/8
|
||||||
|
|
||||||
print("Error completing request", file=sys.stderr)
|
print("Error completing request", file=sys.stderr)
|
||||||
print("Arguments:", args, kwargs, file=sys.stderr)
|
argStr = f"Arguments: {str(args)} {str(kwargs)}"
|
||||||
|
print(argStr[:max_debug_str_len], file=sys.stderr)
|
||||||
|
if len(argStr) > max_debug_str_len:
|
||||||
|
print(f"(Argument list truncated at {max_debug_str_len}/{len(argStr)} characters)", file=sys.stderr)
|
||||||
|
|
||||||
print(traceback.format_exc(), file=sys.stderr)
|
print(traceback.format_exc(), file=sys.stderr)
|
||||||
|
|
||||||
shared.state.job = ""
|
shared.state.job = ""
|
||||||
@@ -428,7 +443,10 @@ def create_toprow(is_img2img):
|
|||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
with gr.Column(scale=8):
|
with gr.Column(scale=8):
|
||||||
negative_prompt = gr.Textbox(label="Negative prompt", elem_id="negative_prompt", show_label=False, placeholder="Negative prompt", lines=2)
|
with gr.Row():
|
||||||
|
negative_prompt = gr.Textbox(label="Negative prompt", elem_id="negative_prompt", show_label=False, placeholder="Negative prompt", lines=2)
|
||||||
|
with gr.Column(scale=1, elem_id="roll_col"):
|
||||||
|
sh = gr.Button(elem_id="sh", visible=True)
|
||||||
|
|
||||||
with gr.Column(scale=1, elem_id="style_neg_col"):
|
with gr.Column(scale=1, elem_id="style_neg_col"):
|
||||||
prompt_style2 = gr.Dropdown(label="Style 2", elem_id=f"{id_part}_style2_index", choices=[k for k, v in shared.prompt_styles.styles.items()], value=next(iter(shared.prompt_styles.styles.keys())), visible=len(shared.prompt_styles.styles) > 1)
|
prompt_style2 = gr.Dropdown(label="Style 2", elem_id=f"{id_part}_style2_index", choices=[k for k, v in shared.prompt_styles.styles.items()], value=next(iter(shared.prompt_styles.styles.keys())), visible=len(shared.prompt_styles.styles) > 1)
|
||||||
@@ -524,7 +542,7 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
denoising_strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising strength', value=0.7)
|
denoising_strength = gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label='Denoising strength', value=0.7)
|
||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
batch_count = gr.Slider(minimum=1, maximum=cmd_opts.max_batch_count, step=1, label='Batch count', value=1)
|
batch_count = gr.Slider(minimum=1, step=1, label='Batch count', value=1)
|
||||||
batch_size = gr.Slider(minimum=1, maximum=8, step=1, label='Batch size', value=1)
|
batch_size = gr.Slider(minimum=1, maximum=8, step=1, label='Batch size', value=1)
|
||||||
|
|
||||||
cfg_scale = gr.Slider(minimum=1.0, maximum=30.0, step=0.5, label='CFG Scale', value=7.0)
|
cfg_scale = gr.Slider(minimum=1.0, maximum=30.0, step=0.5, label='CFG Scale', value=7.0)
|
||||||
@@ -551,13 +569,13 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
do_make_zip = gr.Checkbox(label="Make Zip when Save?", value=False)
|
do_make_zip = gr.Checkbox(label="Make Zip when Save?", value=False)
|
||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
download_files = gr.File(None, file_count="multiple", interactive=False, show_label=False, visible=False)
|
download_files = gr.File(None, file_count="multiple", interactive=False, show_label=False, visible=False)
|
||||||
|
|
||||||
with gr.Group():
|
with gr.Group():
|
||||||
html_info = gr.HTML()
|
html_info = gr.HTML()
|
||||||
generation_info = gr.Textbox(visible=False)
|
generation_info = gr.Textbox(visible=False)
|
||||||
|
|
||||||
connect_reuse_seed(seed, reuse_seed, generation_info, dummy_component, is_subseed=False)
|
connect_reuse_seed(seed, reuse_seed, generation_info, dummy_component, is_subseed=False)
|
||||||
connect_reuse_seed(subseed, reuse_subseed, generation_info, dummy_component, is_subseed=True)
|
connect_reuse_seed(subseed, reuse_subseed, generation_info, dummy_component, is_subseed=True)
|
||||||
@@ -710,7 +728,7 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
tiling = gr.Checkbox(label='Tiling', value=False)
|
tiling = gr.Checkbox(label='Tiling', value=False)
|
||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
batch_count = gr.Slider(minimum=1, maximum=cmd_opts.max_batch_count, step=1, label='Batch count', value=1)
|
batch_count = gr.Slider(minimum=1, step=1, label='Batch count', value=1)
|
||||||
batch_size = gr.Slider(minimum=1, maximum=8, step=1, label='Batch size', value=1)
|
batch_size = gr.Slider(minimum=1, maximum=8, step=1, label='Batch size', value=1)
|
||||||
|
|
||||||
with gr.Group():
|
with gr.Group():
|
||||||
@@ -739,13 +757,13 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
do_make_zip = gr.Checkbox(label="Make Zip when Save?", value=False)
|
do_make_zip = gr.Checkbox(label="Make Zip when Save?", value=False)
|
||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
download_files = gr.File(None, file_count="multiple", interactive=False, show_label=False, visible=False)
|
download_files = gr.File(None, file_count="multiple", interactive=False, show_label=False, visible=False)
|
||||||
|
|
||||||
with gr.Group():
|
with gr.Group():
|
||||||
html_info = gr.HTML()
|
html_info = gr.HTML()
|
||||||
generation_info = gr.Textbox(visible=False)
|
generation_info = gr.Textbox(visible=False)
|
||||||
|
|
||||||
connect_reuse_seed(seed, reuse_seed, generation_info, dummy_component, is_subseed=False)
|
connect_reuse_seed(seed, reuse_seed, generation_info, dummy_component, is_subseed=False)
|
||||||
connect_reuse_seed(subseed, reuse_subseed, generation_info, dummy_component, is_subseed=True)
|
connect_reuse_seed(subseed, reuse_subseed, generation_info, dummy_component, is_subseed=True)
|
||||||
@@ -903,7 +921,15 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
with gr.TabItem('Batch Process'):
|
with gr.TabItem('Batch Process'):
|
||||||
image_batch = gr.File(label="Batch Process", file_count="multiple", interactive=True, type="file")
|
image_batch = gr.File(label="Batch Process", file_count="multiple", interactive=True, type="file")
|
||||||
|
|
||||||
upscaling_resize = gr.Slider(minimum=1.0, maximum=4.0, step=0.05, label="Resize", value=2)
|
with gr.Tabs(elem_id="extras_resize_mode"):
|
||||||
|
with gr.TabItem('Scale by'):
|
||||||
|
upscaling_resize = gr.Slider(minimum=1.0, maximum=4.0, step=0.05, label="Resize", value=2)
|
||||||
|
with gr.TabItem('Scale to'):
|
||||||
|
with gr.Group():
|
||||||
|
with gr.Row():
|
||||||
|
upscaling_resize_w = gr.Number(label="Width", value=512, precision=0)
|
||||||
|
upscaling_resize_h = gr.Number(label="Height", value=512, precision=0)
|
||||||
|
upscaling_crop = gr.Checkbox(label='Crop to fit', value=True)
|
||||||
|
|
||||||
with gr.Group():
|
with gr.Group():
|
||||||
extras_upscaler_1 = gr.Radio(label='Upscaler 1', choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name, type="index")
|
extras_upscaler_1 = gr.Radio(label='Upscaler 1', choices=[x.name for x in shared.sd_upscalers], value=shared.sd_upscalers[0].name, type="index")
|
||||||
@@ -934,6 +960,7 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
fn=wrap_gradio_gpu_call(modules.extras.run_extras),
|
fn=wrap_gradio_gpu_call(modules.extras.run_extras),
|
||||||
_js="get_extras_tab_index",
|
_js="get_extras_tab_index",
|
||||||
inputs=[
|
inputs=[
|
||||||
|
dummy_component,
|
||||||
dummy_component,
|
dummy_component,
|
||||||
extras_image,
|
extras_image,
|
||||||
image_batch,
|
image_batch,
|
||||||
@@ -941,6 +968,9 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
codeformer_visibility,
|
codeformer_visibility,
|
||||||
codeformer_weight,
|
codeformer_weight,
|
||||||
upscaling_resize,
|
upscaling_resize,
|
||||||
|
upscaling_resize_w,
|
||||||
|
upscaling_resize_h,
|
||||||
|
upscaling_crop,
|
||||||
extras_upscaler_1,
|
extras_upscaler_1,
|
||||||
extras_upscaler_2,
|
extras_upscaler_2,
|
||||||
extras_upscaler_2_visibility,
|
extras_upscaler_2_visibility,
|
||||||
@@ -951,17 +981,17 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
html_info,
|
html_info,
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
extras_send_to_img2img.click(
|
extras_send_to_img2img.click(
|
||||||
fn=lambda x: image_from_url_text(x),
|
fn=lambda x: image_from_url_text(x),
|
||||||
_js="extract_image_from_gallery_img2img",
|
_js="extract_image_from_gallery_img2img",
|
||||||
inputs=[result_images],
|
inputs=[result_images],
|
||||||
outputs=[init_img],
|
outputs=[init_img],
|
||||||
)
|
)
|
||||||
|
|
||||||
extras_send_to_inpaint.click(
|
extras_send_to_inpaint.click(
|
||||||
fn=lambda x: image_from_url_text(x),
|
fn=lambda x: image_from_url_text(x),
|
||||||
_js="extract_image_from_gallery_img2img",
|
_js="extract_image_from_gallery_inpaint",
|
||||||
inputs=[result_images],
|
inputs=[result_images],
|
||||||
outputs=[init_img_with_mask],
|
outputs=[init_img_with_mask],
|
||||||
)
|
)
|
||||||
@@ -1005,14 +1035,14 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
|
|
||||||
sd_hijack.model_hijack.embedding_db.load_textual_inversion_embeddings()
|
sd_hijack.model_hijack.embedding_db.load_textual_inversion_embeddings()
|
||||||
|
|
||||||
with gr.Blocks() as textual_inversion_interface:
|
with gr.Blocks() as train_interface:
|
||||||
with gr.Row().style(equal_height=False):
|
with gr.Row().style(equal_height=False):
|
||||||
with gr.Column():
|
gr.HTML(value="<p style='margin-bottom: 0.7em'>See <b><a href=\"https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Textual-Inversion\">wiki</a></b> for detailed explanation.</p>")
|
||||||
with gr.Group():
|
|
||||||
gr.HTML(value="<p style='margin-bottom: 0.7em'>See <b><a href=\"https://github.com/AUTOMATIC1111/stable-diffusion-webui/wiki/Textual-Inversion\">wiki</a></b> for detailed explanation.</p>")
|
|
||||||
|
|
||||||
gr.HTML(value="<p style='margin-bottom: 0.7em'>Create a new embedding</p>")
|
with gr.Row().style(equal_height=False):
|
||||||
|
with gr.Tabs(elem_id="train_tabs"):
|
||||||
|
|
||||||
|
with gr.Tab(label="Create embedding"):
|
||||||
new_embedding_name = gr.Textbox(label="Name")
|
new_embedding_name = gr.Textbox(label="Name")
|
||||||
initialization_text = gr.Textbox(label="Initialization text", value="*")
|
initialization_text = gr.Textbox(label="Initialization text", value="*")
|
||||||
nvpt = gr.Slider(label="Number of vectors per token", minimum=1, maximum=75, step=1, value=1)
|
nvpt = gr.Slider(label="Number of vectors per token", minimum=1, maximum=75, step=1, value=1)
|
||||||
@@ -1022,18 +1052,33 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
gr.HTML(value="")
|
gr.HTML(value="")
|
||||||
|
|
||||||
with gr.Column():
|
with gr.Column():
|
||||||
create_embedding = gr.Button(value="Create", variant='primary')
|
create_embedding = gr.Button(value="Create embedding", variant='primary')
|
||||||
|
|
||||||
with gr.Group():
|
with gr.Tab(label="Create hypernetwork"):
|
||||||
gr.HTML(value="<p style='margin-bottom: 0.7em'>Preprocess images</p>")
|
new_hypernetwork_name = gr.Textbox(label="Name")
|
||||||
|
new_hypernetwork_sizes = gr.CheckboxGroup(label="Modules", value=["768", "320", "640", "1280"], choices=["768", "320", "640", "1280"])
|
||||||
|
|
||||||
|
with gr.Row():
|
||||||
|
with gr.Column(scale=3):
|
||||||
|
gr.HTML(value="")
|
||||||
|
|
||||||
|
with gr.Column():
|
||||||
|
create_hypernetwork = gr.Button(value="Create hypernetwork", variant='primary')
|
||||||
|
|
||||||
|
with gr.Tab(label="Preprocess images"):
|
||||||
process_src = gr.Textbox(label='Source directory')
|
process_src = gr.Textbox(label='Source directory')
|
||||||
process_dst = gr.Textbox(label='Destination directory')
|
process_dst = gr.Textbox(label='Destination directory')
|
||||||
|
process_width = gr.Slider(minimum=64, maximum=2048, step=64, label="Width", value=512)
|
||||||
|
process_height = gr.Slider(minimum=64, maximum=2048, step=64, label="Height", value=512)
|
||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
process_flip = gr.Checkbox(label='Create flipped copies')
|
process_flip = gr.Checkbox(label='Create flipped copies')
|
||||||
process_split = gr.Checkbox(label='Split oversized images into two')
|
process_split = gr.Checkbox(label='Split oversized images into two')
|
||||||
process_caption = gr.Checkbox(label='Use BLIP caption as filename')
|
process_caption = gr.Checkbox(label='Use BLIP caption as filename')
|
||||||
|
if cmd_opts.deepdanbooru:
|
||||||
|
process_caption_deepbooru = gr.Checkbox(label='Use deepbooru caption as filename')
|
||||||
|
else:
|
||||||
|
process_caption_deepbooru = gr.Checkbox(label='Use deepbooru caption as filename', visible=False)
|
||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
with gr.Column(scale=3):
|
with gr.Column(scale=3):
|
||||||
@@ -1042,25 +1087,27 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
with gr.Column():
|
with gr.Column():
|
||||||
run_preprocess = gr.Button(value="Preprocess", variant='primary')
|
run_preprocess = gr.Button(value="Preprocess", variant='primary')
|
||||||
|
|
||||||
with gr.Group():
|
with gr.Tab(label="Train"):
|
||||||
gr.HTML(value="<p style='margin-bottom: 0.7em'>Train an embedding; must specify a directory with a set of 512x512 images</p>")
|
gr.HTML(value="<p style='margin-bottom: 0.7em'>Train an embedding; must specify a directory with a set of 1:1 ratio images</p>")
|
||||||
train_embedding_name = gr.Dropdown(label='Embedding', choices=sorted(sd_hijack.model_hijack.embedding_db.word_embeddings.keys()))
|
train_embedding_name = gr.Dropdown(label='Embedding', choices=sorted(sd_hijack.model_hijack.embedding_db.word_embeddings.keys()))
|
||||||
learn_rate = gr.Number(label='Learning rate', value=5.0e-03)
|
train_hypernetwork_name = gr.Dropdown(label='Hypernetwork', choices=[x for x in shared.hypernetworks.keys()])
|
||||||
|
learn_rate = gr.Textbox(label='Learning rate', placeholder="Learning rate", value="0.005")
|
||||||
dataset_directory = gr.Textbox(label='Dataset directory', placeholder="Path to directory with input images")
|
dataset_directory = gr.Textbox(label='Dataset directory', placeholder="Path to directory with input images")
|
||||||
log_directory = gr.Textbox(label='Log directory', placeholder="Path to directory where to write outputs", value="textual_inversion")
|
log_directory = gr.Textbox(label='Log directory', placeholder="Path to directory where to write outputs", value="textual_inversion")
|
||||||
template_file = gr.Textbox(label='Prompt template file', value=os.path.join(script_path, "textual_inversion_templates", "style_filewords.txt"))
|
template_file = gr.Textbox(label='Prompt template file', value=os.path.join(script_path, "textual_inversion_templates", "style_filewords.txt"))
|
||||||
|
training_width = gr.Slider(minimum=64, maximum=2048, step=64, label="Width", value=512)
|
||||||
|
training_height = gr.Slider(minimum=64, maximum=2048, step=64, label="Height", value=512)
|
||||||
steps = gr.Number(label='Max steps', value=100000, precision=0)
|
steps = gr.Number(label='Max steps', value=100000, precision=0)
|
||||||
|
num_repeats = gr.Number(label='Number of repeats for a single input image per epoch', value=100, precision=0)
|
||||||
create_image_every = gr.Number(label='Save an image to log directory every N steps, 0 to disable', value=500, precision=0)
|
create_image_every = gr.Number(label='Save an image to log directory every N steps, 0 to disable', value=500, precision=0)
|
||||||
save_embedding_every = gr.Number(label='Save a copy of embedding to log directory every N steps, 0 to disable', value=500, precision=0)
|
save_embedding_every = gr.Number(label='Save a copy of embedding to log directory every N steps, 0 to disable', value=500, precision=0)
|
||||||
|
save_image_with_stored_embedding = gr.Checkbox(label='Save images with embedding in PNG chunks', value=True)
|
||||||
|
preview_image_prompt = gr.Textbox(label='Preview prompt', value="")
|
||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
with gr.Column(scale=2):
|
interrupt_training = gr.Button(value="Interrupt")
|
||||||
gr.HTML(value="")
|
train_hypernetwork = gr.Button(value="Train Hypernetwork", variant='primary')
|
||||||
|
train_embedding = gr.Button(value="Train Embedding", variant='primary')
|
||||||
with gr.Column():
|
|
||||||
with gr.Row():
|
|
||||||
interrupt_training = gr.Button(value="Interrupt")
|
|
||||||
train_embedding = gr.Button(value="Train", variant='primary')
|
|
||||||
|
|
||||||
with gr.Column():
|
with gr.Column():
|
||||||
progressbar = gr.HTML(elem_id="ti_progressbar")
|
progressbar = gr.HTML(elem_id="ti_progressbar")
|
||||||
@@ -1086,15 +1133,31 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
create_hypernetwork.click(
|
||||||
|
fn=modules.hypernetworks.ui.create_hypernetwork,
|
||||||
|
inputs=[
|
||||||
|
new_hypernetwork_name,
|
||||||
|
new_hypernetwork_sizes,
|
||||||
|
],
|
||||||
|
outputs=[
|
||||||
|
train_hypernetwork_name,
|
||||||
|
ti_output,
|
||||||
|
ti_outcome,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
run_preprocess.click(
|
run_preprocess.click(
|
||||||
fn=wrap_gradio_gpu_call(modules.textual_inversion.ui.preprocess, extra_outputs=[gr.update()]),
|
fn=wrap_gradio_gpu_call(modules.textual_inversion.ui.preprocess, extra_outputs=[gr.update()]),
|
||||||
_js="start_training_textual_inversion",
|
_js="start_training_textual_inversion",
|
||||||
inputs=[
|
inputs=[
|
||||||
process_src,
|
process_src,
|
||||||
process_dst,
|
process_dst,
|
||||||
|
process_width,
|
||||||
|
process_height,
|
||||||
process_flip,
|
process_flip,
|
||||||
process_split,
|
process_split,
|
||||||
process_caption,
|
process_caption,
|
||||||
|
process_caption_deepbooru
|
||||||
],
|
],
|
||||||
outputs=[
|
outputs=[
|
||||||
ti_output,
|
ti_output,
|
||||||
@@ -1110,10 +1173,35 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
learn_rate,
|
learn_rate,
|
||||||
dataset_directory,
|
dataset_directory,
|
||||||
log_directory,
|
log_directory,
|
||||||
|
training_width,
|
||||||
|
training_height,
|
||||||
|
steps,
|
||||||
|
num_repeats,
|
||||||
|
create_image_every,
|
||||||
|
save_embedding_every,
|
||||||
|
template_file,
|
||||||
|
save_image_with_stored_embedding,
|
||||||
|
preview_image_prompt,
|
||||||
|
],
|
||||||
|
outputs=[
|
||||||
|
ti_output,
|
||||||
|
ti_outcome,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
train_hypernetwork.click(
|
||||||
|
fn=wrap_gradio_gpu_call(modules.hypernetworks.ui.train_hypernetwork, extra_outputs=[gr.update()]),
|
||||||
|
_js="start_training_textual_inversion",
|
||||||
|
inputs=[
|
||||||
|
train_hypernetwork_name,
|
||||||
|
learn_rate,
|
||||||
|
dataset_directory,
|
||||||
|
log_directory,
|
||||||
steps,
|
steps,
|
||||||
create_image_every,
|
create_image_every,
|
||||||
save_embedding_every,
|
save_embedding_every,
|
||||||
template_file,
|
template_file,
|
||||||
|
preview_image_prompt,
|
||||||
],
|
],
|
||||||
outputs=[
|
outputs=[
|
||||||
ti_output,
|
ti_output,
|
||||||
@@ -1127,6 +1215,7 @@ def create_ui(wrap_gradio_gpu_call):
|
|||||||
outputs=[],
|
outputs=[],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def create_setting_component(key):
|
def create_setting_component(key):
|
||||||
def fun():
|
def fun():
|
||||||
return opts.data[key] if key in opts.data else opts.data_labels[key].default
|
return opts.data[key] if key in opts.data else opts.data_labels[key].default
|
||||||
@@ -1175,10 +1264,13 @@ Requested path was: {f}
|
|||||||
changed = 0
|
changed = 0
|
||||||
|
|
||||||
for key, value, comp in zip(opts.data_labels.keys(), args, components):
|
for key, value, comp in zip(opts.data_labels.keys(), args, components):
|
||||||
if not opts.same_type(value, opts.data_labels[key].default):
|
if comp != dummy_component and not opts.same_type(value, opts.data_labels[key].default):
|
||||||
return f"Bad value for setting {key}: {value}; expecting {type(opts.data_labels[key].default).__name__}"
|
return f"Bad value for setting {key}: {value}; expecting {type(opts.data_labels[key].default).__name__}", opts.dumpjson()
|
||||||
|
|
||||||
for key, value, comp in zip(opts.data_labels.keys(), args, components):
|
for key, value, comp in zip(opts.data_labels.keys(), args, components):
|
||||||
|
if comp == dummy_component:
|
||||||
|
continue
|
||||||
|
|
||||||
comp_args = opts.data_labels[key].component_args
|
comp_args = opts.data_labels[key].component_args
|
||||||
if comp_args and isinstance(comp_args, dict) and comp_args.get('visible') is False:
|
if comp_args and isinstance(comp_args, dict) and comp_args.get('visible') is False:
|
||||||
continue
|
continue
|
||||||
@@ -1196,6 +1288,21 @@ Requested path was: {f}
|
|||||||
|
|
||||||
return f'{changed} settings changed.', opts.dumpjson()
|
return f'{changed} settings changed.', opts.dumpjson()
|
||||||
|
|
||||||
|
def run_settings_single(value, key):
|
||||||
|
if not opts.same_type(value, opts.data_labels[key].default):
|
||||||
|
return gr.update(visible=True), opts.dumpjson()
|
||||||
|
|
||||||
|
oldval = opts.data.get(key, None)
|
||||||
|
opts.data[key] = value
|
||||||
|
|
||||||
|
if oldval != value:
|
||||||
|
if opts.data_labels[key].onchange is not None:
|
||||||
|
opts.data_labels[key].onchange()
|
||||||
|
|
||||||
|
opts.save(shared.config_filename)
|
||||||
|
|
||||||
|
return gr.update(value=value), opts.dumpjson()
|
||||||
|
|
||||||
with gr.Blocks(analytics_enabled=False) as settings_interface:
|
with gr.Blocks(analytics_enabled=False) as settings_interface:
|
||||||
settings_submit = gr.Button(value="Apply settings", variant='primary')
|
settings_submit = gr.Button(value="Apply settings", variant='primary')
|
||||||
result = gr.HTML()
|
result = gr.HTML()
|
||||||
@@ -1203,6 +1310,8 @@ Requested path was: {f}
|
|||||||
settings_cols = 3
|
settings_cols = 3
|
||||||
items_per_col = int(len(opts.data_labels) * 0.9 / settings_cols)
|
items_per_col = int(len(opts.data_labels) * 0.9 / settings_cols)
|
||||||
|
|
||||||
|
quicksettings_list = []
|
||||||
|
|
||||||
cols_displayed = 0
|
cols_displayed = 0
|
||||||
items_displayed = 0
|
items_displayed = 0
|
||||||
previous_section = None
|
previous_section = None
|
||||||
@@ -1225,10 +1334,14 @@ Requested path was: {f}
|
|||||||
|
|
||||||
gr.HTML(elem_id="settings_header_text_{}".format(item.section[0]), value='<h1 class="gr-button-lg">{}</h1>'.format(item.section[1]))
|
gr.HTML(elem_id="settings_header_text_{}".format(item.section[0]), value='<h1 class="gr-button-lg">{}</h1>'.format(item.section[1]))
|
||||||
|
|
||||||
component = create_setting_component(k)
|
if item.show_on_main_page:
|
||||||
component_dict[k] = component
|
quicksettings_list.append((i, k, item))
|
||||||
components.append(component)
|
components.append(dummy_component)
|
||||||
items_displayed += 1
|
else:
|
||||||
|
component = create_setting_component(k)
|
||||||
|
component_dict[k] = component
|
||||||
|
components.append(component)
|
||||||
|
items_displayed += 1
|
||||||
|
|
||||||
request_notifications = gr.Button(value='Request browser notifications', elem_id="request_notifications")
|
request_notifications = gr.Button(value='Request browser notifications', elem_id="request_notifications")
|
||||||
request_notifications.click(
|
request_notifications.click(
|
||||||
@@ -1242,7 +1355,6 @@ Requested path was: {f}
|
|||||||
reload_script_bodies = gr.Button(value='Reload custom script bodies (No ui updates, No restart)', variant='secondary')
|
reload_script_bodies = gr.Button(value='Reload custom script bodies (No ui updates, No restart)', variant='secondary')
|
||||||
restart_gradio = gr.Button(value='Restart Gradio and Refresh components (Custom Scripts, ui.py, js and css only)', variant='primary')
|
restart_gradio = gr.Button(value='Restart Gradio and Refresh components (Custom Scripts, ui.py, js and css only)', variant='primary')
|
||||||
|
|
||||||
|
|
||||||
def reload_scripts():
|
def reload_scripts():
|
||||||
modules.scripts.reload_script_body_only()
|
modules.scripts.reload_script_body_only()
|
||||||
|
|
||||||
@@ -1257,13 +1369,14 @@ Requested path was: {f}
|
|||||||
shared.state.interrupt()
|
shared.state.interrupt()
|
||||||
settings_interface.gradio_ref.do_restart = True
|
settings_interface.gradio_ref.do_restart = True
|
||||||
|
|
||||||
|
|
||||||
restart_gradio.click(
|
restart_gradio.click(
|
||||||
fn=request_restart,
|
fn=request_restart,
|
||||||
inputs=[],
|
inputs=[],
|
||||||
outputs=[],
|
outputs=[],
|
||||||
_js='function(){restart_reload()}'
|
_js='function(){restart_reload()}'
|
||||||
)
|
)
|
||||||
|
|
||||||
if column is not None:
|
if column is not None:
|
||||||
column.__exit__()
|
column.__exit__()
|
||||||
|
|
||||||
@@ -1273,7 +1386,7 @@ Requested path was: {f}
|
|||||||
(extras_interface, "Extras", "extras"),
|
(extras_interface, "Extras", "extras"),
|
||||||
(pnginfo_interface, "PNG Info", "pnginfo"),
|
(pnginfo_interface, "PNG Info", "pnginfo"),
|
||||||
(modelmerger_interface, "Checkpoint Merger", "modelmerger"),
|
(modelmerger_interface, "Checkpoint Merger", "modelmerger"),
|
||||||
(textual_inversion_interface, "Textual inversion", "ti"),
|
(train_interface, "Train", "ti"),
|
||||||
(settings_interface, "Settings", "settings"),
|
(settings_interface, "Settings", "settings"),
|
||||||
]
|
]
|
||||||
|
|
||||||
@@ -1289,14 +1402,18 @@ Requested path was: {f}
|
|||||||
css += css_hide_progressbar
|
css += css_hide_progressbar
|
||||||
|
|
||||||
with gr.Blocks(css=css, analytics_enabled=False, title="Stable Diffusion") as demo:
|
with gr.Blocks(css=css, analytics_enabled=False, title="Stable Diffusion") as demo:
|
||||||
|
with gr.Row(elem_id="quicksettings"):
|
||||||
|
for i, k, item in quicksettings_list:
|
||||||
|
component = create_setting_component(k)
|
||||||
|
component_dict[k] = component
|
||||||
|
|
||||||
settings_interface.gradio_ref = demo
|
settings_interface.gradio_ref = demo
|
||||||
|
|
||||||
with gr.Tabs() as tabs:
|
with gr.Tabs() as tabs:
|
||||||
for interface, label, ifid in interfaces:
|
for interface, label, ifid in interfaces:
|
||||||
with gr.TabItem(label, id=ifid):
|
with gr.TabItem(label, id=ifid, elem_id='tab_' + ifid):
|
||||||
interface.render()
|
interface.render()
|
||||||
|
|
||||||
if os.path.exists(os.path.join(script_path, "notification.mp3")):
|
if os.path.exists(os.path.join(script_path, "notification.mp3")):
|
||||||
audio_notification = gr.Audio(interactive=False, value=os.path.join(script_path, "notification.mp3"), elem_id="audio_notification", visible=False)
|
audio_notification = gr.Audio(interactive=False, value=os.path.join(script_path, "notification.mp3"), elem_id="audio_notification", visible=False)
|
||||||
|
|
||||||
@@ -1306,7 +1423,16 @@ Requested path was: {f}
|
|||||||
inputs=components,
|
inputs=components,
|
||||||
outputs=[result, text_settings],
|
outputs=[result, text_settings],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
for i, k, item in quicksettings_list:
|
||||||
|
component = component_dict[k]
|
||||||
|
|
||||||
|
component.change(
|
||||||
|
fn=lambda value, k=k: run_settings_single(value, key=k),
|
||||||
|
inputs=[component],
|
||||||
|
outputs=[component, text_settings],
|
||||||
|
)
|
||||||
|
|
||||||
def modelmerger(*args):
|
def modelmerger(*args):
|
||||||
try:
|
try:
|
||||||
results = modules.extras.run_modelmerger(*args)
|
results = modules.extras.run_modelmerger(*args)
|
||||||
@@ -1420,10 +1546,10 @@ Requested path was: {f}
|
|||||||
|
|
||||||
if getattr(obj,'custom_script_source',None) is not None:
|
if getattr(obj,'custom_script_source',None) is not None:
|
||||||
key = 'customscript/' + obj.custom_script_source + '/' + key
|
key = 'customscript/' + obj.custom_script_source + '/' + key
|
||||||
|
|
||||||
if getattr(obj, 'do_not_save_to_config', False):
|
if getattr(obj, 'do_not_save_to_config', False):
|
||||||
return
|
return
|
||||||
|
|
||||||
saved_value = ui_settings.get(key, None)
|
saved_value = ui_settings.get(key, None)
|
||||||
if saved_value is None:
|
if saved_value is None:
|
||||||
ui_settings[key] = getattr(obj, field)
|
ui_settings[key] = getattr(obj, field)
|
||||||
@@ -1447,10 +1573,10 @@ Requested path was: {f}
|
|||||||
|
|
||||||
if type(x) == gr.Textbox:
|
if type(x) == gr.Textbox:
|
||||||
apply_field(x, 'value')
|
apply_field(x, 'value')
|
||||||
|
|
||||||
if type(x) == gr.Number:
|
if type(x) == gr.Number:
|
||||||
apply_field(x, 'value')
|
apply_field(x, 'value')
|
||||||
|
|
||||||
visit(txt2img_interface, loadsave, "txt2img")
|
visit(txt2img_interface, loadsave, "txt2img")
|
||||||
visit(img2img_interface, loadsave, "img2img")
|
visit(img2img_interface, loadsave, "img2img")
|
||||||
visit(extras_interface, loadsave, "extras")
|
visit(extras_interface, loadsave, "extras")
|
||||||
|
|||||||
+1
-2
@@ -4,7 +4,7 @@ fairscale==0.4.4
|
|||||||
fonts
|
fonts
|
||||||
font-roboto
|
font-roboto
|
||||||
gfpgan
|
gfpgan
|
||||||
gradio==3.4b3
|
gradio==3.4.1
|
||||||
invisible-watermark
|
invisible-watermark
|
||||||
numpy
|
numpy
|
||||||
omegaconf
|
omegaconf
|
||||||
@@ -23,4 +23,3 @@ resize-right
|
|||||||
torchdiffeq
|
torchdiffeq
|
||||||
kornia
|
kornia
|
||||||
lark
|
lark
|
||||||
functorch
|
|
||||||
@@ -2,7 +2,7 @@ transformers==4.19.2
|
|||||||
diffusers==0.3.0
|
diffusers==0.3.0
|
||||||
basicsr==1.4.2
|
basicsr==1.4.2
|
||||||
gfpgan==1.3.8
|
gfpgan==1.3.8
|
||||||
gradio==3.4b3
|
gradio==3.4.1
|
||||||
numpy==1.23.3
|
numpy==1.23.3
|
||||||
Pillow==9.2.0
|
Pillow==9.2.0
|
||||||
realesrgan==0.3.0
|
realesrgan==0.3.0
|
||||||
@@ -22,4 +22,3 @@ resize-right==0.0.2
|
|||||||
torchdiffeq==0.2.3
|
torchdiffeq==0.2.3
|
||||||
kornia==0.6.7
|
kornia==0.6.7
|
||||||
lark==1.1.2
|
lark==1.1.2
|
||||||
functorch==0.2.1
|
|
||||||
@@ -6,6 +6,10 @@ function get_uiCurrentTab() {
|
|||||||
return gradioApp().querySelector('.tabs button:not(.border-transparent)')
|
return gradioApp().querySelector('.tabs button:not(.border-transparent)')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function get_uiCurrentTabContent() {
|
||||||
|
return gradioApp().querySelector('.tabitem[id^=tab_]:not([style*="display: none"])')
|
||||||
|
}
|
||||||
|
|
||||||
uiUpdateCallbacks = []
|
uiUpdateCallbacks = []
|
||||||
uiTabChangeCallbacks = []
|
uiTabChangeCallbacks = []
|
||||||
let uiCurrentTab = null
|
let uiCurrentTab = null
|
||||||
@@ -40,6 +44,25 @@ document.addEventListener("DOMContentLoaded", function() {
|
|||||||
mutationObserver.observe( gradioApp(), { childList:true, subtree:true })
|
mutationObserver.observe( gradioApp(), { childList:true, subtree:true })
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a ctrl+enter as a shortcut to start a generation
|
||||||
|
*/
|
||||||
|
document.addEventListener('keydown', function(e) {
|
||||||
|
var handled = false;
|
||||||
|
if (e.key !== undefined) {
|
||||||
|
if((e.key == "Enter" && (e.metaKey || e.ctrlKey))) handled = true;
|
||||||
|
} else if (e.keyCode !== undefined) {
|
||||||
|
if((e.keyCode == 13 && (e.metaKey || e.ctrlKey))) handled = true;
|
||||||
|
}
|
||||||
|
if (handled) {
|
||||||
|
button = get_uiCurrentTabContent().querySelector('button[id$=_generate]');
|
||||||
|
if (button) {
|
||||||
|
button.click();
|
||||||
|
}
|
||||||
|
e.preventDefault();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* checks that a UI element is not in another hidden element or tab content
|
* checks that a UI element is not in another hidden element or tab content
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -129,8 +129,6 @@ class Script(scripts.Script):
|
|||||||
return [original_prompt, original_negative_prompt, cfg, st, randomness, sigma_adjustment]
|
return [original_prompt, original_negative_prompt, cfg, st, randomness, sigma_adjustment]
|
||||||
|
|
||||||
def run(self, p, original_prompt, original_negative_prompt, cfg, st, randomness, sigma_adjustment):
|
def run(self, p, original_prompt, original_negative_prompt, cfg, st, randomness, sigma_adjustment):
|
||||||
p.batch_size = 1
|
|
||||||
p.batch_count = 1
|
|
||||||
|
|
||||||
|
|
||||||
def sample_extra(conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength):
|
def sample_extra(conditioning, unconditional_conditioning, seeds, subseeds, subseed_strength):
|
||||||
@@ -154,7 +152,7 @@ class Script(scripts.Script):
|
|||||||
rec_noise = find_noise_for_image(p, cond, uncond, cfg, st)
|
rec_noise = find_noise_for_image(p, cond, uncond, cfg, st)
|
||||||
self.cache = Cached(rec_noise, cfg, st, lat, original_prompt, original_negative_prompt, sigma_adjustment)
|
self.cache = Cached(rec_noise, cfg, st, lat, original_prompt, original_negative_prompt, sigma_adjustment)
|
||||||
|
|
||||||
rand_noise = processing.create_random_tensors(p.init_latent.shape[1:], [p.seed + x + 1 for x in range(p.init_latent.shape[0])])
|
rand_noise = processing.create_random_tensors(p.init_latent.shape[1:], seeds=seeds, subseeds=subseeds, subseed_strength=p.subseed_strength, seed_resize_from_h=p.seed_resize_from_h, seed_resize_from_w=p.seed_resize_from_w, p=p)
|
||||||
|
|
||||||
combined_noise = ((1 - randomness) * rec_noise + randomness * rand_noise) / ((randomness**2 + (1-randomness)**2) ** 0.5)
|
combined_noise = ((1 - randomness) * rec_noise + randomness * rand_noise) / ((randomness**2 + (1-randomness)**2) ** 0.5)
|
||||||
|
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ class Script(scripts.Script):
|
|||||||
|
|
||||||
grids = []
|
grids = []
|
||||||
all_images = []
|
all_images = []
|
||||||
|
original_init_image = p.init_images
|
||||||
state.job_count = loops * batch_count
|
state.job_count = loops * batch_count
|
||||||
|
|
||||||
initial_color_corrections = [processing.setup_color_correction(p.init_images[0])]
|
initial_color_corrections = [processing.setup_color_correction(p.init_images[0])]
|
||||||
@@ -45,6 +46,9 @@ class Script(scripts.Script):
|
|||||||
for n in range(batch_count):
|
for n in range(batch_count):
|
||||||
history = []
|
history = []
|
||||||
|
|
||||||
|
# Reset to original init image at the start of each batch
|
||||||
|
p.init_images = original_init_image
|
||||||
|
|
||||||
for i in range(loops):
|
for i in range(loops):
|
||||||
p.n_iter = 1
|
p.n_iter = 1
|
||||||
p.batch_size = 1
|
p.batch_size = 1
|
||||||
|
|||||||
+70
-34
@@ -10,7 +10,8 @@ import numpy as np
|
|||||||
import modules.scripts as scripts
|
import modules.scripts as scripts
|
||||||
import gradio as gr
|
import gradio as gr
|
||||||
|
|
||||||
from modules import images, hypernetwork
|
from modules import images
|
||||||
|
from modules.hypernetworks import hypernetwork
|
||||||
from modules.processing import process_images, Processed, get_correct_sampler
|
from modules.processing import process_images, Processed, get_correct_sampler
|
||||||
from modules.shared import opts, cmd_opts, state
|
from modules.shared import opts, cmd_opts, state
|
||||||
import modules.shared as shared
|
import modules.shared as shared
|
||||||
@@ -27,6 +28,9 @@ def apply_field(field):
|
|||||||
|
|
||||||
|
|
||||||
def apply_prompt(p, x, xs):
|
def apply_prompt(p, x, xs):
|
||||||
|
if xs[0] not in p.prompt and xs[0] not in p.negative_prompt:
|
||||||
|
raise RuntimeError(f"Prompt S/R did not find {xs[0]} in prompt or negative prompt.")
|
||||||
|
|
||||||
p.prompt = p.prompt.replace(xs[0], x)
|
p.prompt = p.prompt.replace(xs[0], x)
|
||||||
p.negative_prompt = p.negative_prompt.replace(xs[0], x)
|
p.negative_prompt = p.negative_prompt.replace(xs[0], x)
|
||||||
|
|
||||||
@@ -73,14 +77,46 @@ def apply_sampler(p, x, xs):
|
|||||||
p.sampler_index = sampler_index
|
p.sampler_index = sampler_index
|
||||||
|
|
||||||
|
|
||||||
|
def confirm_samplers(p, xs):
|
||||||
|
samplers_dict = build_samplers_dict(p)
|
||||||
|
for x in xs:
|
||||||
|
if x.lower() not in samplers_dict.keys():
|
||||||
|
raise RuntimeError(f"Unknown sampler: {x}")
|
||||||
|
|
||||||
|
|
||||||
def apply_checkpoint(p, x, xs):
|
def apply_checkpoint(p, x, xs):
|
||||||
info = modules.sd_models.get_closet_checkpoint_match(x)
|
info = modules.sd_models.get_closet_checkpoint_match(x)
|
||||||
assert info is not None, f'Checkpoint for {x} not found'
|
if info is None:
|
||||||
|
raise RuntimeError(f"Unknown checkpoint: {x}")
|
||||||
modules.sd_models.reload_model_weights(shared.sd_model, info)
|
modules.sd_models.reload_model_weights(shared.sd_model, info)
|
||||||
|
|
||||||
|
|
||||||
|
def confirm_checkpoints(p, xs):
|
||||||
|
for x in xs:
|
||||||
|
if modules.sd_models.get_closet_checkpoint_match(x) is None:
|
||||||
|
raise RuntimeError(f"Unknown checkpoint: {x}")
|
||||||
|
|
||||||
|
|
||||||
def apply_hypernetwork(p, x, xs):
|
def apply_hypernetwork(p, x, xs):
|
||||||
hypernetwork.load_hypernetwork(x)
|
if x.lower() in ["", "none"]:
|
||||||
|
name = None
|
||||||
|
else:
|
||||||
|
name = hypernetwork.find_closest_hypernetwork_name(x)
|
||||||
|
if not name:
|
||||||
|
raise RuntimeError(f"Unknown hypernetwork: {x}")
|
||||||
|
hypernetwork.load_hypernetwork(name)
|
||||||
|
|
||||||
|
|
||||||
|
def confirm_hypernetworks(p, xs):
|
||||||
|
for x in xs:
|
||||||
|
if x.lower() in ["", "none"]:
|
||||||
|
continue
|
||||||
|
if not hypernetwork.find_closest_hypernetwork_name(x):
|
||||||
|
raise RuntimeError(f"Unknown hypernetwork: {x}")
|
||||||
|
|
||||||
|
|
||||||
|
def apply_clip_skip(p, x, xs):
|
||||||
|
opts.data["CLIP_stop_at_last_layers"] = x
|
||||||
|
|
||||||
|
|
||||||
def format_value_add_label(p, opt, x):
|
def format_value_add_label(p, opt, x):
|
||||||
@@ -113,28 +149,29 @@ def str_permutations(x):
|
|||||||
return x
|
return x
|
||||||
|
|
||||||
|
|
||||||
AxisOption = namedtuple("AxisOption", ["label", "type", "apply", "format_value"])
|
AxisOption = namedtuple("AxisOption", ["label", "type", "apply", "format_value", "confirm"])
|
||||||
AxisOptionImg2Img = namedtuple("AxisOptionImg2Img", ["label", "type", "apply", "format_value"])
|
AxisOptionImg2Img = namedtuple("AxisOptionImg2Img", ["label", "type", "apply", "format_value", "confirm"])
|
||||||
|
|
||||||
|
|
||||||
axis_options = [
|
axis_options = [
|
||||||
AxisOption("Nothing", str, do_nothing, format_nothing),
|
AxisOption("Nothing", str, do_nothing, format_nothing, None),
|
||||||
AxisOption("Seed", int, apply_field("seed"), format_value_add_label),
|
AxisOption("Seed", int, apply_field("seed"), format_value_add_label, None),
|
||||||
AxisOption("Var. seed", int, apply_field("subseed"), format_value_add_label),
|
AxisOption("Var. seed", int, apply_field("subseed"), format_value_add_label, None),
|
||||||
AxisOption("Var. strength", float, apply_field("subseed_strength"), format_value_add_label),
|
AxisOption("Var. strength", float, apply_field("subseed_strength"), format_value_add_label, None),
|
||||||
AxisOption("Steps", int, apply_field("steps"), format_value_add_label),
|
AxisOption("Steps", int, apply_field("steps"), format_value_add_label, None),
|
||||||
AxisOption("CFG Scale", float, apply_field("cfg_scale"), format_value_add_label),
|
AxisOption("CFG Scale", float, apply_field("cfg_scale"), format_value_add_label, None),
|
||||||
AxisOption("Prompt S/R", str, apply_prompt, format_value),
|
AxisOption("Prompt S/R", str, apply_prompt, format_value, None),
|
||||||
AxisOption("Prompt order", str_permutations, apply_order, format_value_join_list),
|
AxisOption("Prompt order", str_permutations, apply_order, format_value_join_list, None),
|
||||||
AxisOption("Sampler", str, apply_sampler, format_value),
|
AxisOption("Sampler", str, apply_sampler, format_value, confirm_samplers),
|
||||||
AxisOption("Checkpoint name", str, apply_checkpoint, format_value),
|
AxisOption("Checkpoint name", str, apply_checkpoint, format_value, confirm_checkpoints),
|
||||||
AxisOption("Hypernetwork", str, apply_hypernetwork, format_value),
|
AxisOption("Hypernetwork", str, apply_hypernetwork, format_value, confirm_hypernetworks),
|
||||||
AxisOption("Sigma Churn", float, apply_field("s_churn"), format_value_add_label),
|
AxisOption("Sigma Churn", float, apply_field("s_churn"), format_value_add_label, None),
|
||||||
AxisOption("Sigma min", float, apply_field("s_tmin"), format_value_add_label),
|
AxisOption("Sigma min", float, apply_field("s_tmin"), format_value_add_label, None),
|
||||||
AxisOption("Sigma max", float, apply_field("s_tmax"), format_value_add_label),
|
AxisOption("Sigma max", float, apply_field("s_tmax"), format_value_add_label, None),
|
||||||
AxisOption("Sigma noise", float, apply_field("s_noise"), format_value_add_label),
|
AxisOption("Sigma noise", float, apply_field("s_noise"), format_value_add_label, None),
|
||||||
AxisOption("Eta", float, apply_field("eta"), format_value_add_label),
|
AxisOption("Eta", float, apply_field("eta"), format_value_add_label, None),
|
||||||
AxisOptionImg2Img("Denoising", float, apply_field("denoising_strength"), format_value_add_label), # as it is now all AxisOptionImg2Img items must go after AxisOption ones
|
AxisOption("Clip skip", int, apply_clip_skip, format_value_add_label, None),
|
||||||
|
AxisOptionImg2Img("Denoising", float, apply_field("denoising_strength"), format_value_add_label, None), # as it is now all AxisOptionImg2Img items must go after AxisOption ones
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
@@ -188,7 +225,7 @@ class Script(scripts.Script):
|
|||||||
x_values = gr.Textbox(label="X values", visible=False, lines=1)
|
x_values = gr.Textbox(label="X values", visible=False, lines=1)
|
||||||
|
|
||||||
with gr.Row():
|
with gr.Row():
|
||||||
y_type = gr.Dropdown(label="Y type", choices=[x.label for x in current_axis_options], value=current_axis_options[4].label, visible=False, type="index", elem_id="y_type")
|
y_type = gr.Dropdown(label="Y type", choices=[x.label for x in current_axis_options], value=current_axis_options[0].label, visible=False, type="index", elem_id="y_type")
|
||||||
y_values = gr.Textbox(label="Y values", visible=False, lines=1)
|
y_values = gr.Textbox(label="Y values", visible=False, lines=1)
|
||||||
|
|
||||||
draw_legend = gr.Checkbox(label='Draw legend', value=True)
|
draw_legend = gr.Checkbox(label='Draw legend', value=True)
|
||||||
@@ -200,7 +237,11 @@ class Script(scripts.Script):
|
|||||||
if not no_fixed_seeds:
|
if not no_fixed_seeds:
|
||||||
modules.processing.fix_seed(p)
|
modules.processing.fix_seed(p)
|
||||||
|
|
||||||
p.batch_size = 1
|
if not opts.return_grid:
|
||||||
|
p.batch_size = 1
|
||||||
|
|
||||||
|
|
||||||
|
CLIP_stop_at_last_layers = opts.CLIP_stop_at_last_layers
|
||||||
|
|
||||||
def process_axis(opt, vals):
|
def process_axis(opt, vals):
|
||||||
if opt.label == 'Nothing':
|
if opt.label == 'Nothing':
|
||||||
@@ -256,17 +297,10 @@ class Script(scripts.Script):
|
|||||||
valslist = list(permutations(valslist))
|
valslist = list(permutations(valslist))
|
||||||
|
|
||||||
valslist = [opt.type(x) for x in valslist]
|
valslist = [opt.type(x) for x in valslist]
|
||||||
|
|
||||||
# Confirm options are valid before starting
|
# Confirm options are valid before starting
|
||||||
if opt.label == "Sampler":
|
if opt.confirm:
|
||||||
samplers_dict = build_samplers_dict(p)
|
opt.confirm(p, valslist)
|
||||||
for sampler_val in valslist:
|
|
||||||
if sampler_val.lower() not in samplers_dict.keys():
|
|
||||||
raise RuntimeError(f"Unknown sampler: {sampler_val}")
|
|
||||||
elif opt.label == "Checkpoint name":
|
|
||||||
for ckpt_val in valslist:
|
|
||||||
if modules.sd_models.get_closet_checkpoint_match(ckpt_val) is None:
|
|
||||||
raise RuntimeError(f"Checkpoint for {ckpt_val} not found")
|
|
||||||
|
|
||||||
return valslist
|
return valslist
|
||||||
|
|
||||||
@@ -321,4 +355,6 @@ class Script(scripts.Script):
|
|||||||
|
|
||||||
hypernetwork.load_hypernetwork(opts.sd_hypernetwork)
|
hypernetwork.load_hypernetwork(opts.sd_hypernetwork)
|
||||||
|
|
||||||
|
opts.data["CLIP_stop_at_last_layers"] = CLIP_stop_at_last_layers
|
||||||
|
|
||||||
return processed
|
return processed
|
||||||
@@ -1,3 +1,28 @@
|
|||||||
|
.container {
|
||||||
|
max-width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
#txt2img_token_counter {
|
||||||
|
height: 0px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#img2img_token_counter {
|
||||||
|
height: 0px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#sh{
|
||||||
|
min-width: 2em;
|
||||||
|
min-height: 2em;
|
||||||
|
max-width: 2em;
|
||||||
|
max-height: 2em;
|
||||||
|
flex-grow: 0;
|
||||||
|
padding-left: 0.25em;
|
||||||
|
padding-right: 0.25em;
|
||||||
|
margin: 0.1em 0;
|
||||||
|
opacity: 0%;
|
||||||
|
cursor: default;
|
||||||
|
}
|
||||||
|
|
||||||
.output-html p {margin: 0 0.5em;}
|
.output-html p {margin: 0 0.5em;}
|
||||||
|
|
||||||
.row > *,
|
.row > *,
|
||||||
@@ -215,6 +240,7 @@ fieldset span.text-gray-500, .gr-block.gr-box span.text-gray-500, label.block s
|
|||||||
#settings fieldset span.text-gray-500, #settings .gr-block.gr-box span.text-gray-500, #settings label.block span{
|
#settings fieldset span.text-gray-500, #settings .gr-block.gr-box span.text-gray-500, #settings label.block span{
|
||||||
position: relative;
|
position: relative;
|
||||||
border: none;
|
border: none;
|
||||||
|
margin-right: 8em;
|
||||||
}
|
}
|
||||||
|
|
||||||
.gr-panel div.flex-col div.justify-between label span{
|
.gr-panel div.flex-col div.justify-between label span{
|
||||||
@@ -453,3 +479,30 @@ input[type="range"]{
|
|||||||
.context-menu-items a:hover{
|
.context-menu-items a:hover{
|
||||||
background: #a55000;
|
background: #a55000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#quicksettings > div{
|
||||||
|
border: none;
|
||||||
|
background: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
#quicksettings > div > div{
|
||||||
|
max-width: 32em;
|
||||||
|
padding: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
canvas[key="mask"] {
|
||||||
|
z-index: 12 !important;
|
||||||
|
filter: invert();
|
||||||
|
mix-blend-mode: multiply;
|
||||||
|
pointer-events: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* gradio 3.4.1 stuff for editable scrollbar values */
|
||||||
|
.gr-box > div > div > input.gr-text-input{
|
||||||
|
position: absolute;
|
||||||
|
right: 0.5em;
|
||||||
|
top: -0.6em;
|
||||||
|
z-index: 200;
|
||||||
|
width: 8em;
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
a photo of a [filewords]
|
||||||
|
a rendering of a [filewords]
|
||||||
|
a cropped photo of the [filewords]
|
||||||
|
the photo of a [filewords]
|
||||||
|
a photo of a clean [filewords]
|
||||||
|
a photo of a dirty [filewords]
|
||||||
|
a dark photo of the [filewords]
|
||||||
|
a photo of my [filewords]
|
||||||
|
a photo of the cool [filewords]
|
||||||
|
a close-up photo of a [filewords]
|
||||||
|
a bright photo of the [filewords]
|
||||||
|
a cropped photo of a [filewords]
|
||||||
|
a photo of the [filewords]
|
||||||
|
a good photo of the [filewords]
|
||||||
|
a photo of one [filewords]
|
||||||
|
a close-up photo of the [filewords]
|
||||||
|
a rendition of the [filewords]
|
||||||
|
a photo of the clean [filewords]
|
||||||
|
a rendition of a [filewords]
|
||||||
|
a photo of a nice [filewords]
|
||||||
|
a good photo of a [filewords]
|
||||||
|
a photo of the nice [filewords]
|
||||||
|
a photo of the small [filewords]
|
||||||
|
a photo of the weird [filewords]
|
||||||
|
a photo of the large [filewords]
|
||||||
|
a photo of a cool [filewords]
|
||||||
|
a photo of a small [filewords]
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
picture
|
||||||
Binary file not shown.
|
Before Width: | Height: | Size: 526 KiB After Width: | Height: | Size: 329 KiB |
@@ -29,13 +29,9 @@ from modules import devices
|
|||||||
from modules import modelloader
|
from modules import modelloader
|
||||||
from modules.paths import script_path
|
from modules.paths import script_path
|
||||||
from modules.shared import cmd_opts
|
from modules.shared import cmd_opts
|
||||||
|
import modules.hypernetworks.hypernetwork
|
||||||
|
|
||||||
|
|
||||||
modelloader.cleanup_models()
|
|
||||||
modules.sd_models.setup_model()
|
|
||||||
codeformer.setup_model(cmd_opts.codeformer_models_path)
|
|
||||||
gfpgan.setup_model(cmd_opts.gfpgan_models_path)
|
|
||||||
shared.face_restorers.append(modules.face_restoration.FaceRestoration())
|
|
||||||
modelloader.load_upscalers()
|
|
||||||
queue_lock = threading.Lock()
|
queue_lock = threading.Lock()
|
||||||
|
|
||||||
|
|
||||||
@@ -77,16 +73,24 @@ def wrap_gradio_gpu_call(func, extra_outputs=None):
|
|||||||
return modules.ui.wrap_gradio_call(f, extra_outputs=extra_outputs)
|
return modules.ui.wrap_gradio_call(f, extra_outputs=extra_outputs)
|
||||||
|
|
||||||
|
|
||||||
modules.scripts.load_scripts(os.path.join(script_path, "scripts"))
|
def initialize():
|
||||||
|
modelloader.cleanup_models()
|
||||||
|
modules.sd_models.setup_model()
|
||||||
|
codeformer.setup_model(cmd_opts.codeformer_models_path)
|
||||||
|
gfpgan.setup_model(cmd_opts.gfpgan_models_path)
|
||||||
|
shared.face_restorers.append(modules.face_restoration.FaceRestoration())
|
||||||
|
modelloader.load_upscalers()
|
||||||
|
|
||||||
shared.sd_model = modules.sd_models.load_model()
|
modules.scripts.load_scripts(os.path.join(script_path, "scripts"))
|
||||||
shared.opts.onchange("sd_model_checkpoint", wrap_queued_call(lambda: modules.sd_models.reload_model_weights(shared.sd_model)))
|
|
||||||
|
|
||||||
loaded_hypernetwork = modules.hypernetwork.load_hypernetwork(shared.opts.sd_hypernetwork)
|
shared.sd_model = modules.sd_models.load_model()
|
||||||
shared.opts.onchange("sd_hypernetwork", wrap_queued_call(lambda: modules.hypernetwork.load_hypernetwork(shared.opts.sd_hypernetwork)))
|
shared.opts.onchange("sd_model_checkpoint", wrap_queued_call(lambda: modules.sd_models.reload_model_weights(shared.sd_model)))
|
||||||
|
shared.opts.onchange("sd_hypernetwork", wrap_queued_call(lambda: modules.hypernetworks.hypernetwork.load_hypernetwork(shared.opts.sd_hypernetwork)))
|
||||||
|
|
||||||
|
|
||||||
def webui():
|
def webui():
|
||||||
|
initialize()
|
||||||
|
|
||||||
# make the program just exit at ctrl+c without waiting for anything
|
# make the program just exit at ctrl+c without waiting for anything
|
||||||
def sigint_handler(sig, frame):
|
def sigint_handler(sig, frame):
|
||||||
print(f'Interrupted with signal {sig} in {frame}')
|
print(f'Interrupted with signal {sig} in {frame}')
|
||||||
@@ -98,7 +102,7 @@ def webui():
|
|||||||
|
|
||||||
demo = modules.ui.create_ui(wrap_gradio_gpu_call=wrap_gradio_gpu_call)
|
demo = modules.ui.create_ui(wrap_gradio_gpu_call=wrap_gradio_gpu_call)
|
||||||
|
|
||||||
app,local_url,share_url = demo.launch(
|
app, local_url, share_url = demo.launch(
|
||||||
share=cmd_opts.share,
|
share=cmd_opts.share,
|
||||||
server_name="0.0.0.0" if cmd_opts.listen else None,
|
server_name="0.0.0.0" if cmd_opts.listen else None,
|
||||||
server_port=cmd_opts.port,
|
server_port=cmd_opts.port,
|
||||||
@@ -108,7 +112,7 @@ def webui():
|
|||||||
prevent_thread_lock=True
|
prevent_thread_lock=True
|
||||||
)
|
)
|
||||||
|
|
||||||
app.add_middleware(GZipMiddleware,minimum_size=1000)
|
app.add_middleware(GZipMiddleware, minimum_size=1000)
|
||||||
|
|
||||||
while 1:
|
while 1:
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
@@ -124,9 +128,10 @@ def webui():
|
|||||||
modules.scripts.reload_scripts(os.path.join(script_path, "scripts"))
|
modules.scripts.reload_scripts(os.path.join(script_path, "scripts"))
|
||||||
print('Reloading modules: modules.ui')
|
print('Reloading modules: modules.ui')
|
||||||
importlib.reload(modules.ui)
|
importlib.reload(modules.ui)
|
||||||
|
print('Refreshing Model List')
|
||||||
|
modules.sd_models.list_models()
|
||||||
print('Restarting Gradio')
|
print('Restarting Gradio')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
webui()
|
webui()
|
||||||
Reference in new issue
Block a user