-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Implementation of Stable Diffusion with Aesthetic Gradients #2585
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
AUTOMATIC1111
merged 21 commits into
AUTOMATIC1111:master
from
MalumaDev:test_resolve_conflicts
Oct 21, 2022
Merged
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bb57f30
init
MalumaDev 37d7ffb
fix to tokens lenght, addend embs generator, add new features to edit…
MalumaDev 7b7561f
Merge branch 'master' into test_resolve_conflicts
MalumaDev 4387e4f
Update modules/ui.py
MalumaDev f7df06a
Update README.md
MalumaDev 9b7705e
Update modules/aesthetic_clip.py
MalumaDev 0d4f5db
Update modules/ui.py
MalumaDev ad9bc60
Update modules/ui.py
MalumaDev 3f5c3b9
Update modules/ui.py
MalumaDev 3d21684
Add support to other img format, fixed dropbox update
MalumaDev 97ceaa2
Merge branch 'master' into test_resolve_conflicts
MalumaDev 9325c85
fixed dropbox update
MalumaDev b694bba
Merge remote-tracking branch 'origin/test_resolve_conflicts' into tes…
MalumaDev 523140d
ui fix
MalumaDev e4f8b5f
ui fix
MalumaDev 9324cda
ui fix, re organization of the code
MalumaDev ae0fdad
Merge branch 'master' into test_resolve_conflicts
MalumaDev 589215d
Merge branch 'master' into test_resolve_conflicts
MalumaDev 1997ccf
Merge branch 'master' into test_resolve_conflicts
MalumaDev c2765c9
Merge branch 'master' into test_resolve_conflicts
MalumaDev 2362d5f
Merge branch 'master' into test_resolve_conflicts
MalumaDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import itertools | ||
import os | ||
from pathlib import Path | ||
import html | ||
import gc | ||
|
||
import gradio as gr | ||
import torch | ||
from PIL import Image | ||
from modules import shared | ||
from modules.shared import device, aesthetic_embeddings | ||
from transformers import CLIPModel, CLIPProcessor | ||
|
||
from tqdm.auto import tqdm | ||
|
||
|
||
def get_all_images_in_folder(folder): | ||
return [os.path.join(folder, f) for f in os.listdir(folder) if | ||
os.path.isfile(os.path.join(folder, f)) and check_is_valid_image_file(f)] | ||
|
||
|
||
def check_is_valid_image_file(filename): | ||
return filename.lower().endswith(('.png', '.jpg', '.jpeg')) | ||
MalumaDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def batched(dataset, total, n=1): | ||
for ndx in range(0, total, n): | ||
yield [dataset.__getitem__(i) for i in range(ndx, min(ndx + n, total))] | ||
|
||
|
||
def iter_to_batched(iterable, n=1): | ||
it = iter(iterable) | ||
while True: | ||
chunk = tuple(itertools.islice(it, n)) | ||
if not chunk: | ||
return | ||
yield chunk | ||
|
||
|
||
def generate_imgs_embd(name, folder, batch_size): | ||
# clipModel = CLIPModel.from_pretrained( | ||
# shared.sd_model.cond_stage_model.clipModel.name_or_path | ||
# ) | ||
model = CLIPModel.from_pretrained(shared.sd_model.cond_stage_model.clipModel.name_or_path).to(device) | ||
processor = CLIPProcessor.from_pretrained(shared.sd_model.cond_stage_model.clipModel.name_or_path) | ||
|
||
with torch.no_grad(): | ||
embs = [] | ||
for paths in tqdm(iter_to_batched(get_all_images_in_folder(folder), batch_size), | ||
desc=f"Generating embeddings for {name}"): | ||
if shared.state.interrupted: | ||
break | ||
inputs = processor(images=[Image.open(path) for path in paths], return_tensors="pt").to(device) | ||
outputs = model.get_image_features(**inputs).cpu() | ||
embs.append(torch.clone(outputs)) | ||
inputs.to("cpu") | ||
del inputs, outputs | ||
|
||
embs = torch.cat(embs, dim=0).mean(dim=0, keepdim=True) | ||
|
||
# The generated embedding will be located here | ||
path = str(Path(shared.cmd_opts.aesthetic_embeddings_dir) / f"{name}.pt") | ||
torch.save(embs, path) | ||
|
||
model = model.cpu() | ||
del model | ||
del processor | ||
del embs | ||
gc.collect() | ||
torch.cuda.empty_cache() | ||
res = f""" | ||
Done generating embedding for {name}! | ||
Hypernetwork saved to {html.escape(path)} | ||
MalumaDev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
shared.update_aesthetic_embeddings() | ||
return gr.Dropdown(sorted(aesthetic_embeddings.keys()), label="Imgs embedding", | ||
value=sorted(aesthetic_embeddings.keys())[0] if len( | ||
aesthetic_embeddings) > 0 else None), res, "" |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.