CoolFace
Modelpublic

diffusers/tools

sourceHugging Facecreativeml-openrail-mupdated 3y agoView on Hugging Face
11likes27downloads
control_net_inpaint.py56 linesDownload Raw Back to root
1#!/usr/bin/env python32import torch3import os4from huggingface_hub import HfApi5from pathlib import Path6from diffusers.utils import load_image7from PIL import Image8import numpy as np9 10from diffusers import (11    ControlNetModel,12    StableDiffusionControlNetPipeline,13    DDIMScheduler,14)15import sys16 17checkpoint = sys.argv[1]18 19 20# pre-process image and mask21image = load_image("https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png").convert('RGB')22mask_image = load_image("https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png").convert("L")23 24# convert to float3225image = np.asarray(image, dtype=np.float32)26mask_image = np.asarray(mask_image, dtype=np.float32)27 28image[mask_image > 127] = -255.029image = torch.from_numpy(image)[None].permute(0, 3, 1, 2) / 255.030 31prompt = "A blue cat sitting on a park bench"32 33controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16)34pipe = StableDiffusionControlNetPipeline.from_pretrained(35    "runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float1636)37 38pipe.scheduler = DDIMScheduler.from_config(pipe.scheduler.config)39pipe.enable_model_cpu_offload()40 41generator = torch.manual_seed(0)42out_image = pipe(prompt, num_inference_steps=20, generator=generator, image=image, guidance_scale=9.0).images[0]43 44path = os.path.join(Path.home(), "images", "aa.png")45out_image.save(path)46 47api = HfApi()48 49api.upload_file(50    path_or_fileobj=path,51    path_in_repo=path.split("/")[-1],52    repo_id="patrickvonplaten/images",53    repo_type="dataset",54)55print("https://huggingface.co/datasets/patrickvonplaten/images/blob/main/aa.png")56