许可证: openrail
基础模型: runwayml/stable-diffusion-v1-5
标签:
- 艺术
- controlnet
- stable-diffusion
- controlnet-v1-1
- 图像修复
- 自定义
- 图像到图像
复制来源: lllyasviel/control_v11p_sd15_inpaint
管道标签: 其他
使用方法
import base64
import requests
HF_TOKEN = 'hf_xxxxxxxxxxxxx'
API_ENDPOINT = 'https://xxxxxxxxxxx.us-east-1.aws.endpoints.huggingface.cloud'
def load_image(path):
try:
with open(path, 'rb') as file:
return file.read()
except FileNotFoundError as error:
print('读取图像时出错:', error)
def get_b64_image(path):
image_buffer = load_image(path)
if image_buffer:
return base64.b64encode(image_buffer).decode('utf-8')
def process_images(original_image_path, mask_image_path, result_path, prompt, width, height):
original_b64 = get_b64_image(original_image_path)
mask_b64 = get_b64_image(mask_image_path)
if not original_b64 or not mask_b64:
return
body = {
'inputs': prompt,
'image': original_b64,
'mask_image': mask_b64,
'width': width,
'height': height
}
headers = {
'Authorization': f'Bearer {HF_TOKEN}',
'Content-Type': 'application/json',
'Accept': 'image/png'
}
response = requests.post(
API_ENDPOINT,
json=body,
headers=headers
)
blob = response.content
save_image(blob, result_path)
def save_image(blob, file_path):
with open(file_path, 'wb') as file:
file.write(blob)
print('文件保存成功!')
if __name__ == '__main__':
original_image_path = 'images/original.png'
mask_image_path = 'images/mask.png'
result_path = 'images/result.png'
process_images(original_image_path, mask_image_path, result_path, '赛博朋克蒙娜丽莎', 512, 768)
Controlnet - v1.1 - 图像修复版本
Controlnet v1.1 由 Lvmin Zhang 在 lllyasviel/ControlNet-v1-1 发布。
此检查点是 原始检查点 转换为 diffusers
格式的版本。
它可以与 Stable Diffusion 结合使用,例如 runwayml/stable-diffusion-v1-5。
更多详情,请参阅 🧨 Diffusers 文档。
ControlNet 是一种通过添加额外条件来控制扩散模型的神经网络结构。

此检查点对应于基于 图像修复 条件的 ControlNet。
模型详情
简介
ControlNet 由 Lvmin Zhang 和 Maneesh Agrawala 在 Adding Conditional Control to Text-to-Image Diffusion Models 中提出。
摘要如下:
我们提出了一种神经网络结构 ControlNet,用于控制预训练的大型扩散模型以支持额外的输入条件。
ControlNet 以端到端的方式学习特定任务的条件,即使训练数据集较小(< 50k),学习过程也稳健。
此外,训练 ControlNet 与微调扩散模型一样快,并且可以在个人设备上进行训练。
如果有强大的计算集群可用,模型可以扩展到大量(数百万到数十亿)数据。
我们报告称,像 Stable Diffusion 这样的大型扩散模型可以通过 ControlNet 增强,以支持边缘图、分割图、关键点等条件输入。
这可能会丰富控制大型扩散模型的方法,并进一步促进相关应用。
示例
建议将此检查点与 Stable Diffusion v1-5 结合使用,因为该检查点已在其上训练。
实验上,该检查点也可以与其他扩散模型(如 dreamboothed stable diffusion)一起使用。
1. 安装 `diffusers` 及相关包:
$ pip install diffusers transformers accelerate
2. 运行代码:
```python
import torch
import os
from diffusers.utils import load_image
from PIL import Image
import numpy as np
from diffusers import (
ControlNetModel,
StableDiffusionControlNetPipeline,
UniPCMultistepScheduler,
)
checkpoint = "lllyasviel/control_v11p_sd15_inpaint"
original_image = load_image(
"https://huggingface.co/lllyasviel/control_v11p_sd15_inpaint/resolve/main/images/original.png"
)
mask_image = load_image(
"https://huggingface.co/lllyasviel/control_v11p_sd15_inpaint/resolve/main/images/mask.png"
)
def make_inpaint_condition(image, image_mask):
image = np.array(image.convert("RGB")).astype(np.float32) / 255.0
image_mask = np.array(image_mask.convert("L"))
assert image.shape[0:1] == image_mask.shape[0:1], "图像和掩码图像必须具有相同的尺寸"
image[image_mask < 128] = -1.0 # 设置为掩码像素
image = np.expand_dims(image, 0).transpose(0, 3, 1, 2)
image = torch.from_numpy(image)
return image
control_image = make_inpaint_condition(original_image, mask_image)
prompt = "最佳质量"
negative_prompt="低分辨率, 解剖结构错误, 手部错误, 裁剪, 最差质量"
controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5", controlnet=controlnet, torch_dtype=torch.float16
)
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
pipe.enable_model_cpu_offload()
generator = torch.manual_seed(2)
image = pipe(prompt, negative_prompt=negative_prompt, num_inference_steps=30,
generator=generator, image=control_image).images[0]
image.save('images/output.png')



其他发布的检查点 v1-1
作者发布了 14 个不同的检查点,每个检查点均基于 Stable Diffusion v1-5 训练,针对不同类型的条件:
更多信息
更多信息,请参阅 Diffusers ControlNet 博客文章 和 官方文档。