Spaces:
Running
Running
Sean Powell commited on
Commit Β·
ad3f797
1
Parent(s): a9ae6bb
Make experiments consistent & use shared utils.
Browse files- experiments/diamond-crop-img2img.py +41 -27
- experiments/guided_prompt.py +25 -52
- experiments/inpainting.py +31 -77
- generations/{45-degrees/half_drop_tiled β diamond_crop_img2img/half_drop_demos}/.gitkeep +0 -0
- generations/{45-degrees/half_drops β guided_prompt/half_drop_demos}/.gitkeep +0 -0
- generations/{45-degrees/rotations β guided_prompt/half_drops}/.gitkeep +0 -0
- generations/{45-degrees/tiles β guided_prompt/rotations}/.gitkeep +0 -0
- generations/{diamond_crop_img2img/half_drop_demo β guided_prompt/tiles}/.gitkeep +0 -0
- generations/inpainting/half_drops/.gitkeep +0 -0
- generations/inpainting/inner_rotated_tiles/.gitkeep +0 -0
- generations/inpainting/{half_drop_tiled β inpaintings}/.gitkeep +0 -0
- generations/inpainting/originals/.gitkeep +0 -0
- utils/pipes.py +11 -1
experiments/diamond-crop-img2img.py
CHANGED
|
@@ -1,35 +1,49 @@
|
|
| 1 |
from utils import filenames, images, wallpaper, pipes
|
| 2 |
|
| 3 |
prompt = "seamless pattern of parrots. black and white, drawing, white background, seamless, ornament."
|
|
|
|
| 4 |
generation_dir = "./generations/diamond_crop_img2img"
|
| 5 |
|
| 6 |
sdxl_pipe = pipes.create_stable_diffusion_xl_pipeline()
|
| 7 |
-
|
| 8 |
-
original_image = sdxl_pipe(prompt=prompt, num_inference_steps=20).images[0]
|
| 9 |
-
original_image_filename = filenames.generate_filename(f"{generation_dir}/originals", prompt)
|
| 10 |
-
original_image.save(original_image_filename)
|
| 11 |
-
print("generated image generated", original_image_filename)
|
| 12 |
-
|
| 13 |
-
inner_rotated_tile = images.extract_inner_rotated_tile_from_image(original_image)
|
| 14 |
-
inner_rotated_tile_filename = filenames.generate_filename(f"{generation_dir}/inner_rotated_tiles", prompt,
|
| 15 |
-
"-inner-rotated")
|
| 16 |
-
inner_rotated_tile.save(inner_rotated_tile_filename)
|
| 17 |
-
print("generated inner rotated tile", inner_rotated_tile_filename)
|
| 18 |
-
|
| 19 |
sdxl_img2img_pipe = pipes.create_stable_diffusion_xl_img2img_pipe()
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from utils import filenames, images, wallpaper, pipes
|
| 2 |
|
| 3 |
prompt = "seamless pattern of parrots. black and white, drawing, white background, seamless, ornament."
|
| 4 |
+
inference_steps = 20
|
| 5 |
generation_dir = "./generations/diamond_crop_img2img"
|
| 6 |
|
| 7 |
sdxl_pipe = pipes.create_stable_diffusion_xl_pipeline()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
sdxl_img2img_pipe = pipes.create_stable_diffusion_xl_img2img_pipe()
|
| 9 |
|
| 10 |
+
prompts = [
|
| 11 |
+
"seamless pattern of parrots. black and white, drawing, white background, seamless, ornament.",
|
| 12 |
+
"highly detailed black and white geometric pattern, repetition, hexagon, fractal, sacred geometry, sharp angles, "
|
| 13 |
+
"symmetry."
|
| 14 |
+
"spring flowers, happy and beautiful, by jackdaws and william morris, thick black line art, anime.",
|
| 15 |
+
"individual, one, cartoon-style, great horned owl standing on a branch, coloring page, black and white.",
|
| 16 |
+
"geometric pattern, orange background."
|
| 17 |
+
]
|
| 18 |
+
|
| 19 |
+
for prompt in prompts:
|
| 20 |
+
print(f"using prompt: `{prompt}`")
|
| 21 |
+
|
| 22 |
+
original_image = sdxl_pipe(prompt=prompt, num_inference_steps=inference_steps).images[0]
|
| 23 |
+
original_image_filename = filenames.generate_filename(f"{generation_dir}/originals", prompt)
|
| 24 |
+
original_image.save(original_image_filename)
|
| 25 |
+
print("generated original image", original_image_filename)
|
| 26 |
+
|
| 27 |
+
inner_rotated_tile = images.extract_inner_rotated_tile_from_image(original_image)
|
| 28 |
+
inner_rotated_tile_filename = filenames.generate_filename(f"{generation_dir}/inner_rotated_tiles", prompt,
|
| 29 |
+
"-inner-rotated")
|
| 30 |
+
inner_rotated_tile.save(inner_rotated_tile_filename)
|
| 31 |
+
print("generated inner rotated tile", inner_rotated_tile_filename)
|
| 32 |
+
|
| 33 |
+
tileable_image = \
|
| 34 |
+
sdxl_img2img_pipe(prompt, image=inner_rotated_tile.convert("RGB"), num_inference_steps=inference_steps).images[
|
| 35 |
+
0]
|
| 36 |
+
tileable_image_filename = filenames.generate_filename(f"{generation_dir}/tiles", prompt, "-tile")
|
| 37 |
+
tileable_image.save(tileable_image_filename)
|
| 38 |
+
print("generated tileable image", tileable_image_filename)
|
| 39 |
+
|
| 40 |
+
half_drop = wallpaper.convert_tile_to_half_drop(tileable_image)
|
| 41 |
+
half_drop_filename = filenames.generate_filename(f"{generation_dir}/half_drops", prompt, "_half-drop-tile")
|
| 42 |
+
half_drop.save(half_drop_filename)
|
| 43 |
+
print("generated half drop tile", tileable_image_filename)
|
| 44 |
+
|
| 45 |
+
half_drop_demo = wallpaper.generate_tile_test_arrangement(half_drop)
|
| 46 |
+
half_drop_demo_filename = filenames.generate_filename(f"{generation_dir}/half_drop_demos", prompt,
|
| 47 |
+
"_half-drop-demo")
|
| 48 |
+
half_drop_demo.save(half_drop_demo_filename)
|
| 49 |
+
print("generated half drop demo", tileable_image_filename)
|
experiments/guided_prompt.py
CHANGED
|
@@ -1,70 +1,43 @@
|
|
| 1 |
-
import
|
| 2 |
-
import torch
|
| 3 |
-
from diffusers import StableDiffusionXLPipeline
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
pipe = StableDiffusionXLPipeline.from_pretrained(
|
| 8 |
-
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
|
| 9 |
-
)
|
| 10 |
-
pipe = pipe.to("mps")
|
| 11 |
-
pipe.vae.disable_tiling()
|
| 12 |
-
|
| 13 |
-
from typing import Optional
|
| 14 |
-
from torch import Tensor
|
| 15 |
-
from torch.nn import functional as F
|
| 16 |
-
from torch.nn.modules.utils import _pair
|
| 17 |
-
|
| 18 |
-
def asymmetricConv2DConvForward(self, input: Tensor, weight: Tensor, bias: Optional[Tensor]):
|
| 19 |
-
self.paddingX = (self._reversed_padding_repeated_twice[0], self._reversed_padding_repeated_twice[1], 0, 0)
|
| 20 |
-
self.paddingY = (0, 0, self._reversed_padding_repeated_twice[2], self._reversed_padding_repeated_twice[3])
|
| 21 |
-
working = F.pad(input, self.paddingX, mode='circular')
|
| 22 |
-
working = F.pad(working, self.paddingY, mode='circular')
|
| 23 |
-
return F.conv2d(working, weight, bias, self.stride, _pair(0), self.dilation, self.groups)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
targets = [pipe.vae, pipe.text_encoder, pipe.unet]
|
| 27 |
-
conv_layers = []
|
| 28 |
-
for target in targets:
|
| 29 |
-
for module in target.modules():
|
| 30 |
-
if isinstance(module, torch.nn.Conv2d):
|
| 31 |
-
conv_layers.append(module)
|
| 32 |
-
|
| 33 |
-
for cl in conv_layers:
|
| 34 |
-
if isinstance(cl, diffusers.models.lora.LoRACompatibleConv) and cl.lora_layer is None:
|
| 35 |
-
cl.lora_layer = lambda *x: 0
|
| 36 |
-
|
| 37 |
-
cl._conv_forward = asymmetricConv2DConvForward.__get__(cl, torch.nn.Conv2d)
|
| 38 |
|
|
|
|
| 39 |
|
| 40 |
prompt_suffix = "-45 degree angle."
|
| 41 |
|
| 42 |
prompts = [
|
| 43 |
"seamless pattern of parrots. black and white, drawing, white background, seamless, ornament.",
|
| 44 |
-
"highly detailed black and white geometric pattern, repetition, hexagon, fractal, sacred geometry, sharp angles,
|
| 45 |
-
"
|
|
|
|
| 46 |
"individual, one, cartoon-style, great horned owl standing on a branch, coloring page, black and white.",
|
| 47 |
"geometric pattern, orange background."
|
| 48 |
]
|
| 49 |
|
| 50 |
for prompt in prompts:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
rotated = images.rotate_image(
|
| 58 |
-
rotated_filename = filenames.generate_filename("
|
| 59 |
rotated.save(rotated_filename)
|
|
|
|
| 60 |
|
| 61 |
-
half_drop = wallpaper.convert_tile_to_half_drop(
|
| 62 |
-
half_drop_filename = filenames.generate_filename("
|
|
|
|
| 63 |
half_drop.save(half_drop_filename)
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
print(
|
| 70 |
-
|
|
|
|
| 1 |
+
from utils import images, filenames, wallpaper, pipes
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
generation_dir = "./generations/guided_prompt"
|
| 4 |
+
inference_steps = 20
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
sdxl_pipe = pipes.create_stable_diffusion_xl_pipeline()
|
| 7 |
|
| 8 |
prompt_suffix = "-45 degree angle."
|
| 9 |
|
| 10 |
prompts = [
|
| 11 |
"seamless pattern of parrots. black and white, drawing, white background, seamless, ornament.",
|
| 12 |
+
"highly detailed black and white geometric pattern, repetition, hexagon, fractal, sacred geometry, sharp angles, "
|
| 13 |
+
"symmetry."
|
| 14 |
+
"spring flowers, happy and beautiful, by jackdaws and william morris, thick black line art, anime.",
|
| 15 |
"individual, one, cartoon-style, great horned owl standing on a branch, coloring page, black and white.",
|
| 16 |
"geometric pattern, orange background."
|
| 17 |
]
|
| 18 |
|
| 19 |
for prompt in prompts:
|
| 20 |
+
modified_prompt = f"{prompt} {prompt_suffix}"
|
| 21 |
+
print(f"using prompt: `{modified_prompt}`")
|
| 22 |
|
| 23 |
+
original_image = sdxl_pipe(prompt=modified_prompt, num_inference_steps=inference_steps).images[0]
|
| 24 |
+
original_image_filename = filenames.generate_filename(f"{generation_dir}/tiles", modified_prompt)
|
| 25 |
+
original_image.save(original_image_filename)
|
| 26 |
+
print("generated original image", original_image_filename)
|
| 27 |
|
| 28 |
+
rotated = images.rotate_image(original_image)
|
| 29 |
+
rotated_filename = filenames.generate_filename(f"{generation_dir}/rotations", modified_prompt, "_rotated")
|
| 30 |
rotated.save(rotated_filename)
|
| 31 |
+
print("generated rotation", rotated_filename)
|
| 32 |
|
| 33 |
+
half_drop = wallpaper.convert_tile_to_half_drop(original_image)
|
| 34 |
+
half_drop_filename = filenames.generate_filename(f"{generation_dir}/half_drops", modified_prompt,
|
| 35 |
+
"_half-drop-tile")
|
| 36 |
half_drop.save(half_drop_filename)
|
| 37 |
+
print("generated half drop", half_drop_filename)
|
| 38 |
|
| 39 |
+
half_drop_demo = wallpaper.generate_tile_test_arrangement(half_drop)
|
| 40 |
+
half_drop_demo_filename = filenames.generate_filename(f"{generation_dir}/half_drop_demos", modified_prompt,
|
| 41 |
+
"_half-drop-demo")
|
| 42 |
+
half_drop_demo.save(half_drop_demo_filename)
|
| 43 |
+
print("generated half drop demo", half_drop_demo_filename)
|
|
|
experiments/inpainting.py
CHANGED
|
@@ -1,77 +1,31 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
"
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return F.conv2d(working, weight, bias, self.stride, _pair(0), self.dilation, self.groups)
|
| 33 |
-
|
| 34 |
-
# TODO: Are the targets here sufficient?
|
| 35 |
-
targets = [pipe.vae, pipe.text_encoder_2, pipe.unet]
|
| 36 |
-
conv_layers = []
|
| 37 |
-
index = 0
|
| 38 |
-
for target in targets:
|
| 39 |
-
print(index)
|
| 40 |
-
index += 1
|
| 41 |
-
for module in target.modules():
|
| 42 |
-
if isinstance(module, torch.nn.Conv2d):
|
| 43 |
-
conv_layers.append(module)
|
| 44 |
-
|
| 45 |
-
for cl in conv_layers:
|
| 46 |
-
if isinstance(cl, diffusers.models.lora.LoRACompatibleConv) and cl.lora_layer is None:
|
| 47 |
-
cl.lora_layer = lambda *x: 0
|
| 48 |
-
|
| 49 |
-
cl._conv_forward = asymmetricConv2DConvForward.__get__(cl, torch.nn.Conv2d)
|
| 50 |
-
|
| 51 |
-
# prompt = "seamless pattern of parrots. black and white, drawing, white background, seamless, ornament."
|
| 52 |
-
# original_image = pipe(prompt=prompt, num_inference_steps=20).images[0]
|
| 53 |
-
# original_image_filename = filenames.generate_filename("./generations/inpainting/originals", prompt)
|
| 54 |
-
# original_image.save(original_image_filename)
|
| 55 |
-
|
| 56 |
-
prompt = "seamless pattern of parrots. black and white, drawing, white background, seamless, ornament."
|
| 57 |
-
original_image = Image.open("generations/inpainting/originals/seamless-pattern-of-parrots-black-and-white-drawing-white-background-seamless-ornament_20240109-170602.png")
|
| 58 |
-
|
| 59 |
-
inner_rotated_tile = images.extract_inner_rotated_tile_from_image(original_image)
|
| 60 |
-
inner_rotated_tile_filename = filenames.generate_filename("./generations/inpainting/inner_rotated_tiles", prompt,
|
| 61 |
-
"-inner-rotated")
|
| 62 |
-
inner_rotated_tile.save(inner_rotated_tile_filename)
|
| 63 |
-
print("generated inner rotated tile", inner_rotated_tile_filename)
|
| 64 |
-
|
| 65 |
-
tileable_image = pipe(prompt, image=inner_rotated_tile.convert("RGB"), num_inference_steps=20).images[0]
|
| 66 |
-
tileable_image_filename = filenames.generate_filename("./generations/inpainting/tiles", prompt, "-tile")
|
| 67 |
-
tileable_image.save(tileable_image_filename)
|
| 68 |
-
|
| 69 |
-
half_drop = wallpaper.convert_tile_to_half_drop(tileable_image)
|
| 70 |
-
half_drop_filename = filenames.generate_filename("./generations/inpainting/half_drops", prompt, "_half-drop-tile")
|
| 71 |
-
half_drop.save(half_drop_filename)
|
| 72 |
-
|
| 73 |
-
half_drop_tiled = wallpaper.generate_tile_test_arrangement(half_drop)
|
| 74 |
-
half_drop_tiled_filename = filenames.generate_filename("./generations/inpainting/half_drop_tiled", prompt,
|
| 75 |
-
"_half_drop_tiled")
|
| 76 |
-
half_drop_tiled.save(half_drop_tiled_filename)
|
| 77 |
-
|
|
|
|
| 1 |
+
from diffusers.utils import load_image
|
| 2 |
+
|
| 3 |
+
from utils import pipes, filenames
|
| 4 |
+
|
| 5 |
+
sdxl_pipe = pipes.create_stable_diffusion_xl_pipeline()
|
| 6 |
+
sdxl_inpainting_pipe = pipes.create_stable_diffusion_xl_inpainting_pipe()
|
| 7 |
+
|
| 8 |
+
generation_dir = "./generations/inpainting"
|
| 9 |
+
mask = load_image("masks/mask.png").convert("RGB")
|
| 10 |
+
|
| 11 |
+
prompts = [
|
| 12 |
+
"seamless pattern of parrots. black and white, drawing, white background, seamless, ornament.",
|
| 13 |
+
"highly detailed black and white geometric pattern, repetition, hexagon, fractal, sacred geometry, sharp angles, "
|
| 14 |
+
"symmetry."
|
| 15 |
+
"spring flowers, happy and beautiful, by jackdaws and william morris, thick black line art, anime.",
|
| 16 |
+
"individual, one, cartoon-style, great horned owl standing on a branch, coloring page, black and white.",
|
| 17 |
+
"geometric pattern, orange background."
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
for prompt in prompts:
|
| 21 |
+
original_image = sdxl_pipe(prompt=prompt, num_inference_steps=40).images[0]
|
| 22 |
+
original_image_filename = filenames.generate_filename(f"{generation_dir}/tiles", prompt)
|
| 23 |
+
original_image.save(original_image_filename)
|
| 24 |
+
print("generated original image", original_image_filename)
|
| 25 |
+
|
| 26 |
+
inpainted_image = \
|
| 27 |
+
sdxl_inpainting_pipe(prompt=prompt, init_image=original_image, mask_image=mask, num_inference_steps=10,
|
| 28 |
+
strength=0.80).images[0]
|
| 29 |
+
inpainted_filename = filenames.generate_filename(f"{generation_dir}/inpaintings", prompt, "_inpainted")
|
| 30 |
+
inpainted_image.save(inpainted_filename)
|
| 31 |
+
print("generated inpainting", inpainted_filename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
generations/{45-degrees/half_drop_tiled β diamond_crop_img2img/half_drop_demos}/.gitkeep
RENAMED
|
File without changes
|
generations/{45-degrees/half_drops β guided_prompt/half_drop_demos}/.gitkeep
RENAMED
|
File without changes
|
generations/{45-degrees/rotations β guided_prompt/half_drops}/.gitkeep
RENAMED
|
File without changes
|
generations/{45-degrees/tiles β guided_prompt/rotations}/.gitkeep
RENAMED
|
File without changes
|
generations/{diamond_crop_img2img/half_drop_demo β guided_prompt/tiles}/.gitkeep
RENAMED
|
File without changes
|
generations/inpainting/half_drops/.gitkeep
DELETED
|
File without changes
|
generations/inpainting/inner_rotated_tiles/.gitkeep
DELETED
|
File without changes
|
generations/inpainting/{half_drop_tiled β inpaintings}/.gitkeep
RENAMED
|
File without changes
|
generations/inpainting/originals/.gitkeep
DELETED
|
File without changes
|
utils/pipes.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import torch
|
| 2 |
-
from diffusers import StableDiffusionXLImg2ImgPipeline, StableDiffusionXLPipeline
|
| 3 |
|
| 4 |
from utils import tiling
|
| 5 |
|
|
@@ -22,3 +22,13 @@ def create_stable_diffusion_xl_img2img_pipe():
|
|
| 22 |
pipe.vae.disable_tiling()
|
| 23 |
tiling.enable_circular_tiling([pipe.vae, pipe.text_encoder_2, pipe.unet])
|
| 24 |
return pipe
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from diffusers import StableDiffusionXLImg2ImgPipeline, StableDiffusionXLPipeline, StableDiffusionXLInpaintPipeline
|
| 3 |
|
| 4 |
from utils import tiling
|
| 5 |
|
|
|
|
| 22 |
pipe.vae.disable_tiling()
|
| 23 |
tiling.enable_circular_tiling([pipe.vae, pipe.text_encoder_2, pipe.unet])
|
| 24 |
return pipe
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def create_stable_diffusion_xl_inpainting_pipe():
|
| 28 |
+
pipe = StableDiffusionXLInpaintPipeline.from_pretrained(
|
| 29 |
+
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
|
| 30 |
+
)
|
| 31 |
+
pipe = pipe.to("mps")
|
| 32 |
+
pipe.vae.disable_tiling()
|
| 33 |
+
tiling.enable_circular_tiling([pipe.vae, pipe.text_encoder, pipe.unet])
|
| 34 |
+
return pipe
|