Spaces:
Runtime error
Runtime error
Update controlnet/callable_functions.py
Browse files- controlnet/callable_functions.py +124 -124
controlnet/callable_functions.py
CHANGED
|
@@ -1,125 +1,125 @@
|
|
| 1 |
-
import argparse
|
| 2 |
-
import os
|
| 3 |
-
import torch
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from diffusers import DDIMScheduler
|
| 6 |
-
from controlnet.pipline_controlnet_xs_v2 import StableDiffusionPipelineXSv2
|
| 7 |
-
from controlnet.controlnetxs_appearance import StyleCodesModel
|
| 8 |
-
from diffusers.models import UNet2DConditionModel
|
| 9 |
-
from transformers import AutoProcessor, SiglipVisionModel
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def process_single_image(image_path, image=None):
|
| 14 |
-
|
| 15 |
-
# Set up model components
|
| 16 |
-
unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet", torch_dtype=torch.float16, device="cuda")
|
| 17 |
-
stylecodes_model = StyleCodesModel.from_unet(unet, size_ratio=1.0).to(dtype=torch.float16, device="cuda")
|
| 18 |
-
stylecodes_model.requires_grad_(False)
|
| 19 |
-
stylecodes_model= stylecodes_model.to("cuda")
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
stylecodes_model.load_model("models/
|
| 23 |
-
# Load and preprocess image
|
| 24 |
-
if image is None:
|
| 25 |
-
image = Image.open(image_path).convert("RGB")
|
| 26 |
-
image = image.resize((512, 512))
|
| 27 |
-
|
| 28 |
-
# Set up generator with a fixed seed for reproducibility
|
| 29 |
-
seed = 238
|
| 30 |
-
clip_image_processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-224")
|
| 31 |
-
image_encoder = SiglipVisionModel.from_pretrained("google/siglip-base-patch16-224").to(dtype=torch.float16,device=stylecodes_model.device)
|
| 32 |
-
clip_image = clip_image_processor(images=image, return_tensors="pt").pixel_values
|
| 33 |
-
clip_image = clip_image.to(stylecodes_model.device, dtype=torch.float16)
|
| 34 |
-
clip_image = {"pixel_values": clip_image}
|
| 35 |
-
clip_image_embeds = image_encoder(**clip_image, output_hidden_states=True).hidden_states[-2]
|
| 36 |
-
|
| 37 |
-
# Run the image through the pipeline with the specified prompt
|
| 38 |
-
code = stylecodes_model.sref_autoencoder.make_stylecode(clip_image_embeds)
|
| 39 |
-
print("stylecode = ",code)
|
| 40 |
-
return code
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
def process_single_image_both_ways(image_path, prompt, num_inference_steps,image=None):
|
| 44 |
-
# Load and preprocess image
|
| 45 |
-
# Set up model components
|
| 46 |
-
unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet", torch_dtype=torch.float16, device="cuda")
|
| 47 |
-
stylecodes_model = StyleCodesModel.from_unet(unet, size_ratio=1.0).to(dtype=torch.float16, device="cuda")
|
| 48 |
-
|
| 49 |
-
noise_scheduler = DDIMScheduler(
|
| 50 |
-
num_train_timesteps=1000,
|
| 51 |
-
beta_start=0.00085,
|
| 52 |
-
beta_end=0.012,
|
| 53 |
-
beta_schedule="scaled_linear",
|
| 54 |
-
clip_sample=False,
|
| 55 |
-
set_alpha_to_one=False,
|
| 56 |
-
steps_offset=1,
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
stylecodes_model.load_model("models/
|
| 60 |
-
|
| 61 |
-
pipe = StableDiffusionPipelineXSv2.from_pretrained(
|
| 62 |
-
"runwayml/stable-diffusion-v1-5",
|
| 63 |
-
unet=unet,
|
| 64 |
-
stylecodes_model=stylecodes_model,
|
| 65 |
-
torch_dtype=torch.float16,
|
| 66 |
-
device="cuda",
|
| 67 |
-
scheduler=noise_scheduler,
|
| 68 |
-
feature_extractor=None,
|
| 69 |
-
safety_checker=None,
|
| 70 |
-
)
|
| 71 |
-
|
| 72 |
-
pipe.enable_model_cpu_offload()
|
| 73 |
-
|
| 74 |
-
if image is None:
|
| 75 |
-
image = Image.open(image_path).convert("RGB")
|
| 76 |
-
image = image.resize((512, 512))
|
| 77 |
-
|
| 78 |
-
# Set up generator with a fixed seed for reproducibility
|
| 79 |
-
seed = 238
|
| 80 |
-
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 81 |
-
|
| 82 |
-
# Run the image through the pipeline with the specified prompt
|
| 83 |
-
output_images = pipe(
|
| 84 |
-
prompt=prompt,
|
| 85 |
-
guidance_scale=3,
|
| 86 |
-
image=image,
|
| 87 |
-
num_inference_steps=num_inference_steps,
|
| 88 |
-
generator=generator,
|
| 89 |
-
controlnet_conditioning_scale=0.9,
|
| 90 |
-
width=512,
|
| 91 |
-
height=512,
|
| 92 |
-
stylecode=None,
|
| 93 |
-
).images
|
| 94 |
-
return output_images
|
| 95 |
-
# Save the output image
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
def make_stylecode(image_path, image=None):
|
| 99 |
-
|
| 100 |
-
# Set up model components
|
| 101 |
-
unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet", torch_dtype=torch.float16, device="cuda")
|
| 102 |
-
stylecodes_model = StyleCodesModel.from_unet(unet, size_ratio=1.0).to(dtype=torch.float16, device="cuda")
|
| 103 |
-
stylecodes_model.requires_grad_(False)
|
| 104 |
-
stylecodes_model= stylecodes_model.to("cuda")
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
stylecodes_model.load_model("models/
|
| 108 |
-
# Load and preprocess image
|
| 109 |
-
if image is None:
|
| 110 |
-
image = Image.open(image_path).convert("RGB")
|
| 111 |
-
image = image.resize((512, 512))
|
| 112 |
-
|
| 113 |
-
# Set up generator with a fixed seed for reproducibility
|
| 114 |
-
seed = 238
|
| 115 |
-
clip_image_processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-224")
|
| 116 |
-
image_encoder = SiglipVisionModel.from_pretrained("google/siglip-base-patch16-224").to(dtype=torch.float16,device=stylecodes_model.device)
|
| 117 |
-
clip_image = clip_image_processor(images=image, return_tensors="pt").pixel_values
|
| 118 |
-
clip_image = clip_image.to(stylecodes_model.device, dtype=torch.float16)
|
| 119 |
-
clip_image = {"pixel_values": clip_image}
|
| 120 |
-
clip_image_embeds = image_encoder(**clip_image, output_hidden_states=True).hidden_states[-2]
|
| 121 |
-
|
| 122 |
-
# Run the image through the pipeline with the specified prompt
|
| 123 |
-
code = stylecodes_model.sref_autoencoder.make_stylecode(clip_image_embeds)
|
| 124 |
-
print("stylecode = ",code)
|
| 125 |
return code
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import os
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from diffusers import DDIMScheduler
|
| 6 |
+
from controlnet.pipline_controlnet_xs_v2 import StableDiffusionPipelineXSv2
|
| 7 |
+
from controlnet.controlnetxs_appearance import StyleCodesModel
|
| 8 |
+
from diffusers.models import UNet2DConditionModel
|
| 9 |
+
from transformers import AutoProcessor, SiglipVisionModel
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def process_single_image(image_path, image=None):
|
| 14 |
+
|
| 15 |
+
# Set up model components
|
| 16 |
+
unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet", torch_dtype=torch.float16, device="cuda")
|
| 17 |
+
stylecodes_model = StyleCodesModel.from_unet(unet, size_ratio=1.0).to(dtype=torch.float16, device="cuda")
|
| 18 |
+
stylecodes_model.requires_grad_(False)
|
| 19 |
+
stylecodes_model= stylecodes_model.to("cuda")
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
stylecodes_model.load_model("models/stylecodes_sd15_v1.bin")
|
| 23 |
+
# Load and preprocess image
|
| 24 |
+
if image is None:
|
| 25 |
+
image = Image.open(image_path).convert("RGB")
|
| 26 |
+
image = image.resize((512, 512))
|
| 27 |
+
|
| 28 |
+
# Set up generator with a fixed seed for reproducibility
|
| 29 |
+
seed = 238
|
| 30 |
+
clip_image_processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-224")
|
| 31 |
+
image_encoder = SiglipVisionModel.from_pretrained("google/siglip-base-patch16-224").to(dtype=torch.float16,device=stylecodes_model.device)
|
| 32 |
+
clip_image = clip_image_processor(images=image, return_tensors="pt").pixel_values
|
| 33 |
+
clip_image = clip_image.to(stylecodes_model.device, dtype=torch.float16)
|
| 34 |
+
clip_image = {"pixel_values": clip_image}
|
| 35 |
+
clip_image_embeds = image_encoder(**clip_image, output_hidden_states=True).hidden_states[-2]
|
| 36 |
+
|
| 37 |
+
# Run the image through the pipeline with the specified prompt
|
| 38 |
+
code = stylecodes_model.sref_autoencoder.make_stylecode(clip_image_embeds)
|
| 39 |
+
print("stylecode = ",code)
|
| 40 |
+
return code
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def process_single_image_both_ways(image_path, prompt, num_inference_steps,image=None):
|
| 44 |
+
# Load and preprocess image
|
| 45 |
+
# Set up model components
|
| 46 |
+
unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet", torch_dtype=torch.float16, device="cuda")
|
| 47 |
+
stylecodes_model = StyleCodesModel.from_unet(unet, size_ratio=1.0).to(dtype=torch.float16, device="cuda")
|
| 48 |
+
|
| 49 |
+
noise_scheduler = DDIMScheduler(
|
| 50 |
+
num_train_timesteps=1000,
|
| 51 |
+
beta_start=0.00085,
|
| 52 |
+
beta_end=0.012,
|
| 53 |
+
beta_schedule="scaled_linear",
|
| 54 |
+
clip_sample=False,
|
| 55 |
+
set_alpha_to_one=False,
|
| 56 |
+
steps_offset=1,
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
stylecodes_model.load_model("models/stylecodes_sd15_v1.bin")
|
| 60 |
+
|
| 61 |
+
pipe = StableDiffusionPipelineXSv2.from_pretrained(
|
| 62 |
+
"runwayml/stable-diffusion-v1-5",
|
| 63 |
+
unet=unet,
|
| 64 |
+
stylecodes_model=stylecodes_model,
|
| 65 |
+
torch_dtype=torch.float16,
|
| 66 |
+
device="cuda",
|
| 67 |
+
scheduler=noise_scheduler,
|
| 68 |
+
feature_extractor=None,
|
| 69 |
+
safety_checker=None,
|
| 70 |
+
)
|
| 71 |
+
|
| 72 |
+
pipe.enable_model_cpu_offload()
|
| 73 |
+
|
| 74 |
+
if image is None:
|
| 75 |
+
image = Image.open(image_path).convert("RGB")
|
| 76 |
+
image = image.resize((512, 512))
|
| 77 |
+
|
| 78 |
+
# Set up generator with a fixed seed for reproducibility
|
| 79 |
+
seed = 238
|
| 80 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
| 81 |
+
|
| 82 |
+
# Run the image through the pipeline with the specified prompt
|
| 83 |
+
output_images = pipe(
|
| 84 |
+
prompt=prompt,
|
| 85 |
+
guidance_scale=3,
|
| 86 |
+
image=image,
|
| 87 |
+
num_inference_steps=num_inference_steps,
|
| 88 |
+
generator=generator,
|
| 89 |
+
controlnet_conditioning_scale=0.9,
|
| 90 |
+
width=512,
|
| 91 |
+
height=512,
|
| 92 |
+
stylecode=None,
|
| 93 |
+
).images
|
| 94 |
+
return output_images
|
| 95 |
+
# Save the output image
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
def make_stylecode(image_path, image=None):
|
| 99 |
+
|
| 100 |
+
# Set up model components
|
| 101 |
+
unet = UNet2DConditionModel.from_pretrained("runwayml/stable-diffusion-v1-5", subfolder="unet", torch_dtype=torch.float16, device="cuda")
|
| 102 |
+
stylecodes_model = StyleCodesModel.from_unet(unet, size_ratio=1.0).to(dtype=torch.float16, device="cuda")
|
| 103 |
+
stylecodes_model.requires_grad_(False)
|
| 104 |
+
stylecodes_model= stylecodes_model.to("cuda")
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
stylecodes_model.load_model("models/stylecodes_sd15_v1.bin")
|
| 108 |
+
# Load and preprocess image
|
| 109 |
+
if image is None:
|
| 110 |
+
image = Image.open(image_path).convert("RGB")
|
| 111 |
+
image = image.resize((512, 512))
|
| 112 |
+
|
| 113 |
+
# Set up generator with a fixed seed for reproducibility
|
| 114 |
+
seed = 238
|
| 115 |
+
clip_image_processor = AutoProcessor.from_pretrained("google/siglip-base-patch16-224")
|
| 116 |
+
image_encoder = SiglipVisionModel.from_pretrained("google/siglip-base-patch16-224").to(dtype=torch.float16,device=stylecodes_model.device)
|
| 117 |
+
clip_image = clip_image_processor(images=image, return_tensors="pt").pixel_values
|
| 118 |
+
clip_image = clip_image.to(stylecodes_model.device, dtype=torch.float16)
|
| 119 |
+
clip_image = {"pixel_values": clip_image}
|
| 120 |
+
clip_image_embeds = image_encoder(**clip_image, output_hidden_states=True).hidden_states[-2]
|
| 121 |
+
|
| 122 |
+
# Run the image through the pipeline with the specified prompt
|
| 123 |
+
code = stylecodes_model.sref_autoencoder.make_stylecode(clip_image_embeds)
|
| 124 |
+
print("stylecode = ",code)
|
| 125 |
return code
|