Commit
•
6d8ff37
1
Parent(s):
de55a08
further improve
Browse files- run_kandinsky.py +9 -26
- run_local.py +37 -17
run_kandinsky.py
CHANGED
@@ -8,41 +8,24 @@ import os
|
|
8 |
from huggingface_hub import HfApi
|
9 |
from pathlib import Path
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
|
15 |
-
|
16 |
-
negative_prompt = "low quality, ugly"
|
17 |
|
18 |
-
pipe_prior = DiffusionPipeline.from_pretrained(
|
19 |
-
"kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16
|
20 |
-
)
|
21 |
pipe_prior.to("cuda")
|
|
|
22 |
t2i_pipe = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
|
23 |
t2i_pipe.to("cuda")
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
t2i_pipe.unet = torch.compile(t2i_pipe.unet, mode="reduce-overhead", fullgraph=True)
|
28 |
-
|
29 |
-
next_time = time.time()
|
30 |
-
print("Loading", next_time - prev_time)
|
31 |
-
prev_time = next_time
|
32 |
|
33 |
generator = torch.Generator(device="cuda").manual_seed(12)
|
34 |
-
image_embeds, negative_image_embeds = pipe_prior(prompt, negative_prompt=
|
35 |
-
|
36 |
-
next_time = time.time()
|
37 |
-
print("Prior", next_time - prev_time)
|
38 |
-
prev_time = next_time
|
39 |
-
|
40 |
-
for _ in range(3):
|
41 |
-
images = t2i_pipe(prompt, image_embeds=image_embeds, negative_image_embeds=negative_image_embeds, negative_prompt=negative_prompt, num_inference_steps=50, generator=generator).images
|
42 |
|
43 |
-
|
44 |
-
print("Text-to-image", next_time - prev_time)
|
45 |
-
prev_time = next_time
|
46 |
|
47 |
for i, image in enumerate(images):
|
48 |
path = os.path.join(Path.home(), "images", f"aa_{i}.png")
|
|
|
8 |
from huggingface_hub import HfApi
|
9 |
from pathlib import Path
|
10 |
|
11 |
+
from diffusers import DiffusionPipeline
|
12 |
+
import torch
|
|
|
13 |
|
14 |
+
api = HfApi()
|
|
|
15 |
|
16 |
+
pipe_prior = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16)
|
|
|
|
|
17 |
pipe_prior.to("cuda")
|
18 |
+
|
19 |
t2i_pipe = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16)
|
20 |
t2i_pipe.to("cuda")
|
21 |
|
22 |
+
prompt = "A alien cheeseburger creature eating itself, claymation, cinematic, moody lighting"
|
23 |
+
negative_prompt = "low quality, bad quality"
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
generator = torch.Generator(device="cuda").manual_seed(12)
|
26 |
+
image_embeds, negative_image_embeds = pipe_prior(prompt, negative_prompt, guidance_scale=1.0, generator=generator).to_tuple()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
images = t2i_pipe(prompt, num_images_per_prompt=4, image_embeds=image_embeds, negative_image_embeds=negative_image_embeds, negative_prompt=negative_prompt).images
|
|
|
|
|
29 |
|
30 |
for i, image in enumerate(images):
|
31 |
path = os.path.join(Path.home(), "images", f"aa_{i}.png")
|
run_local.py
CHANGED
@@ -1,8 +1,9 @@
|
|
1 |
#!/usr/bin/env python3
|
2 |
-
from diffusers import StableDiffusionPipeline, DDIMScheduler
|
3 |
import time
|
4 |
import os
|
5 |
from huggingface_hub import HfApi
|
|
|
6 |
import torch
|
7 |
import sys
|
8 |
from pathlib import Path
|
@@ -10,34 +11,53 @@ import requests
|
|
10 |
from PIL import Image
|
11 |
from io import BytesIO
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
|
17 |
api = HfApi()
|
18 |
start_time = time.time()
|
19 |
-
path =
|
20 |
-
pipe =
|
21 |
-
pipe.scheduler =
|
|
|
|
|
|
|
|
|
22 |
pipe = pipe.to("cuda")
|
23 |
-
counter = 1000
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
-
|
|
|
|
|
31 |
|
32 |
for i, image in enumerate(images):
|
33 |
-
|
|
|
34 |
image.save(path)
|
35 |
|
36 |
api.upload_file(
|
37 |
path_or_fileobj=path,
|
38 |
path_in_repo=path.split("/")[-1],
|
39 |
-
repo_id="patrickvonplaten/
|
40 |
repo_type="dataset",
|
41 |
)
|
42 |
-
print(f"https://huggingface.co/datasets/patrickvonplaten/
|
43 |
-
counter += 1
|
|
|
1 |
#!/usr/bin/env python3
|
2 |
+
from diffusers import StableDiffusionPipeline, KDPM2DiscreteScheduler, StableDiffusionImg2ImgPipeline, HeunDiscreteScheduler, KDPM2AncestralDiscreteScheduler, DDIMScheduler
|
3 |
import time
|
4 |
import os
|
5 |
from huggingface_hub import HfApi
|
6 |
+
# from compel import Compel
|
7 |
import torch
|
8 |
import sys
|
9 |
from pathlib import Path
|
|
|
11 |
from PIL import Image
|
12 |
from io import BytesIO
|
13 |
|
14 |
+
# path = sys.argv[1]
|
15 |
+
path = "runwayml/stable-diffusion-v1-5"
|
16 |
+
# path = "stabilityai/stable-diffusion-2-1"
|
17 |
|
18 |
api = HfApi()
|
19 |
start_time = time.time()
|
20 |
+
pipe = StableDiffusionPipeline.from_pretrained(path, torch_dtype=torch.float16)
|
21 |
+
# pipe = StableDiffusionImg2ImgPipeline.from_pretrained(path, torch_dtype=torch.float16, safety_checker=None)
|
22 |
+
# pipe.scheduler = KDPM2AncestralDiscreteScheduler.from_config(pipe.scheduler.config)
|
23 |
+
|
24 |
+
# compel = Compel(tokenizer=pipe.tokenizer, text_encoder=pipe.text_encoder)
|
25 |
+
|
26 |
+
|
27 |
pipe = pipe.to("cuda")
|
|
|
28 |
|
29 |
+
prompt = "A lion in galaxies, spirals, nebulae, stars, smoke, iridescent, intricate detail, octane render, 8k"
|
30 |
+
|
31 |
+
# rompts = ["a cat playing with a ball++ in the forest", "a cat playing with a ball++ in the forest", "a cat playing with a ball-- in the forest"]
|
32 |
+
|
33 |
+
# prompt_embeds = torch.cat([compel.build_conditioning_tensor(prompt) for prompt in prompts])
|
34 |
+
|
35 |
+
# generator = [torch.Generator(device="cuda").manual_seed(0) for _ in range(prompt_embeds.shape[0])]
|
36 |
+
#
|
37 |
+
# url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"
|
38 |
+
#
|
39 |
+
# response = requests.get(url)
|
40 |
+
# image = Image.open(BytesIO(response.content)).convert("RGB")
|
41 |
+
# image.thumbnail((768, 768))
|
42 |
+
#
|
43 |
+
|
44 |
+
for TIMESTEP_TYPE in ["trailing", "leading"]:
|
45 |
+
for RESCALE_BETAS_ZEROS_SNR in [True, False]:
|
46 |
+
for GUIDANCE_RESCALE in [0,0, 0.7]:
|
47 |
|
48 |
+
pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config, timestep_type=TIMESTEP_TYPE, rescale_betas_zero_snr=RESCALE_BETAS_ZEROS_SNR)
|
49 |
+
generator = torch.Generator(device="cpu").manual_seed(0)
|
50 |
+
images = pipe(prompt=prompt, generator=generator, num_images_per_prompt=4, num_inference_steps=40, guidance_rescale=GUIDANCE_RESCALE).images
|
51 |
|
52 |
for i, image in enumerate(images):
|
53 |
+
file_name = f"bb_{i}_{TIMESTEP_TYPE}_{str(int(RESCALE_BETAS_ZEROS_SNR))}_{GUIDANCE_RESCALE}"
|
54 |
+
path = os.path.join(Path.home(), "images", f"{file_name}.png")
|
55 |
image.save(path)
|
56 |
|
57 |
api.upload_file(
|
58 |
path_or_fileobj=path,
|
59 |
path_in_repo=path.split("/")[-1],
|
60 |
+
repo_id="patrickvonplaten/images",
|
61 |
repo_type="dataset",
|
62 |
)
|
63 |
+
print(f"https://huggingface.co/datasets/patrickvonplaten/images/blob/main/{file_name}.png")
|
|