Spaces:
Runtime error
Runtime error
Update controlnet/callable_functions.py
Browse files- controlnet/callable_functions.py +38 -14
controlnet/callable_functions.py
CHANGED
|
@@ -10,34 +10,58 @@ from transformers import AutoProcessor, SiglipVisionModel
|
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
-
def process_single_image(model,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(model)
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 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 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
def process_single_image_both_ways(model,image_path, prompt, num_inference_steps,image=None):
|
|
|
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
+
def process_single_image(model,image_path, prompt, num_inference_steps, stylecode,image=None):
|
| 14 |
+
# Load and preprocess image
|
| 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 |
|
| 19 |
+
noise_scheduler = DDIMScheduler(
|
| 20 |
+
num_train_timesteps=1000,
|
| 21 |
+
beta_start=0.00085,
|
| 22 |
+
beta_end=0.012,
|
| 23 |
+
beta_schedule="scaled_linear",
|
| 24 |
+
clip_sample=False,
|
| 25 |
+
set_alpha_to_one=False,
|
| 26 |
+
steps_offset=1,
|
| 27 |
+
)
|
| 28 |
|
| 29 |
stylecodes_model.load_model(model)
|
| 30 |
+
|
| 31 |
+
pipe = StableDiffusionPipelineXSv2.from_pretrained(
|
| 32 |
+
"runwayml/stable-diffusion-v1-5",
|
| 33 |
+
unet=unet,
|
| 34 |
+
stylecodes_model=stylecodes_model,
|
| 35 |
+
torch_dtype=torch.float16,
|
| 36 |
+
device="cuda",
|
| 37 |
+
scheduler=noise_scheduler,
|
| 38 |
+
feature_extractor=None,
|
| 39 |
+
safety_checker=None,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
pipe.enable_model_cpu_offload()
|
| 43 |
+
|
| 44 |
if image is None:
|
| 45 |
image = Image.open(image_path).convert("RGB")
|
| 46 |
image = image.resize((512, 512))
|
| 47 |
|
| 48 |
# Set up generator with a fixed seed for reproducibility
|
| 49 |
seed = 238
|
| 50 |
+
generator = torch.Generator(device="cuda").manual_seed(seed)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Run the image through the pipeline with the specified prompt
|
| 53 |
+
output_images = pipe(
|
| 54 |
+
prompt=prompt,
|
| 55 |
+
guidance_scale=3,
|
| 56 |
+
#image=image,
|
| 57 |
+
num_inference_steps=num_inference_steps,
|
| 58 |
+
generator=generator,
|
| 59 |
+
controlnet_conditioning_scale=0.9,
|
| 60 |
+
width=512,
|
| 61 |
+
height=512,
|
| 62 |
+
stylecode=stylecode,
|
| 63 |
+
).images
|
| 64 |
+
return output_images
|
| 65 |
|
| 66 |
|
| 67 |
def process_single_image_both_ways(model,image_path, prompt, num_inference_steps,image=None):
|