| | import PIL |
| | import requests |
| | import torch |
| | from diffusers import StableDiffusionInstructPix2PixPipeline |
| |
|
| | |
| | |
| | checkpoint_path = "/root/autodl-tmp/my_self/checkpoint-100" |
| |
|
| | |
| | pipe = StableDiffusionInstructPix2PixPipeline.from_pretrained( |
| | checkpoint_path, |
| | torch_dtype=torch.float16, |
| | safety_checker=None |
| | ).to("cuda") |
| |
|
| | |
| | generator = torch.Generator("cuda").manual_seed(0) |
| |
|
| | |
| | def load_local_image(image_path): |
| | """从本地路径加载图像并预处理""" |
| | image = PIL.Image.open(image_path) |
| | image = PIL.ImageOps.exif_transpose(image) |
| | image = image.convert("RGB") |
| | return image |
| |
|
| | |
| | local_image_path = "/root/autodl-tmp/my_model/inference.png" |
| | image = load_local_image(local_image_path) |
| |
|
| | |
| | prompt = "smile" |
| | num_inference_steps = 20 |
| | image_guidance_scale = 1.5 |
| | guidance_scale = 10 |
| |
|
| | |
| | edited_image = pipe( |
| | prompt, |
| | image=image, |
| | num_inference_steps=num_inference_steps, |
| | image_guidance_scale=image_guidance_scale, |
| | guidance_scale=guidance_scale, |
| | generator=generator, |
| | ).images[0] |
| |
|
| | def save_generated_image(image, save_dir, save_filename="generated_smile.png"): |
| | |
| | if not os.path.exists(save_dir): |
| | os.makedirs(save_dir, exist_ok=True) |
| | |
| | |
| | save_path = os.path.join(save_dir, save_filename) |
| | |
| | |
| | image.save(save_path) |
| | |
| | |
| | print(f"\n生成图像已保存 → {save_path}") |
| |
|
| | |
| | save_directory = "/root/autodl-tmp/my_model" |
| | save_filename = "ckpt-100.png" |
| |
|
| | |
| | save_generated_image(edited_image, save_directory, save_filename) |
| |
|
| |
|
| |
|