HuggingFace镜像/controlnet-inpaint-endpoint
模型介绍
文件和版本
分析

使用方法

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 reading image:', 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('File saved successfully!')


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, 'cyberpunk mona lisa', 512, 768)

Controlnet - v1.1 - InPaint 版本

Controlnet v1.1 由 Lvmin Zhang 在 lllyasviel/ControlNet-v1-1 发布。

此检查点是将 原始检查点 转换为 diffusers 格式的版本。 它可与 Stable Diffusion 结合使用,例如 runwayml/stable-diffusion-v1-5。

有关更多详细信息,另请参阅 🧨 Diffusers 文档。

ControlNet 是一种神经网络结构,通过添加额外条件来控制扩散模型。

img

此检查点对应于以修复图像为条件的 ControlNet。

模型详情

  • 开发人员: Lvmin Zhang、Maneesh Agrawala

  • 模型类型: 基于扩散的文本到图像生成模型

  • 语言: 英语

  • 许可证: CreativeML OpenRAIL M 许可证 是一种 Open RAIL M 许可证,改编自 BigScience 和 RAIL Initiative 在负责任 AI 许可领域联合开展的工作。另请参阅 关于 BLOOM Open RAIL 许可证的文章,我们的许可证即基于此。

  • 更多信息资源: GitHub 仓库、论文。

  • 引用格式:

    @misc{zhang2023adding, title={Adding Conditional Control to Text-to-Image Diffusion Models}, author={Lvmin Zhang and Maneesh Agrawala}, year={2023}, eprint={2302.05543}, archivePrefix={arXiv}, primaryClass={cs.CV} }

简介

Controlnet 由 Lvmin Zhang 与 Maneesh Agrawala 在论文《Adding Conditional Control to Text-to-Image Diffusion Models》(https://arxiv.org/abs/2302.05543)中提出。

其摘要如下:

我们提出了一种名为 ControlNet 的神经网络结构,用于控制预训练的大型扩散模型,使其支持额外的输入条件。ControlNet 以端到端的方式学习特定任务的条件,即使在训练数据集规模较小(<50k)时,学习过程依然稳健。此外,训练 ControlNet 的速度与微调扩散模型相当,甚至可以在个人设备上完成训练。或者,若有强大的计算集群支持,该模型可扩展至处理海量数据(数百万到数十亿规模)。我们的研究表明,像 Stable Diffusion 这样的大型扩散模型可通过 ControlNet 进行增强,从而支持边缘图、分割图、关键点等条件输入。这可能会丰富大型扩散模型的控制方法,并进一步推动相关应用的发展。

示例

建议将该 checkpoint 与 Stable Diffusion v1-5 配合使用,因为该 checkpoint 是基于此模型训练的。

经过实验验证,该 checkpoint 也可与其他扩散模型配合使用,例如 dreamboothed stable diffusion。

1. Let's install `diffusers` and related packages:

$ pip install diffusers transformers accelerate

2. Run code:
```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 and image_mask must have the same image size"
    image[image_mask < 128] = -1.0 # set as masked pixel 
    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 = "best quality"
negative_prompt="lowres, bad anatomy, bad hands, cropped, worst quality"
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')

original mask inpaint_output

其他已发布的 v1-1 检查点

作者发布了 14 个不同的检查点,每个检查点均基于 Stable Diffusion v1-5 在不同类型的条件控制下训练而成:

模型名称控制图像概述条件图像控制图像示例生成图像示例
lllyasviel/control_v11p_sd15_canny
基于边缘检测训练黑色背景上带有白色边缘的单色图像。
lllyasviel/control_v11e_sd15_ip2p
基于像素到像素指令训练无条件。
lllyasviel/control_v11p_sd15_inpaint
基于图像修复训练无条件。
lllyasviel/control_v11p_sd15_mlsd
基于多级别线段检测训练带有标注线段的图像。
lllyasviel/control_v11f1p_sd15_depth
基于深度估计训练包含深度信息的图像,通常表现为灰度图像。
lllyasviel/control_v11p_sd15_normalbae
基于表面法线估计训练包含表面法线信息的图像,通常表现为彩色编码图像。
lllyasviel/control_v11p_sd15_seg
基于图像分割训练包含分割区域的图像,通常表现为彩色编码图像。
lllyasviel/control_v11p_sd15_lineart
基于线稿生成训练线稿图像,通常为白色背景上的黑色线条。
lllyasviel/control_v11p_sd15s2_lineart_anime
基于动漫线稿生成训练动漫风格线稿图像。
lllyasviel/control_v11p_sd15_openpose
基于人体姿态估计训练包含人体姿态的图像,通常表现为一组关键点或骨架。
lllyasviel/control_v11p_sd15_scribble
基于涂鸦图像生成训练包含涂鸦的图像,通常为随机或用户绘制的笔触。
lllyasviel/control_v11p_sd15_softedge
基于软边缘图像生成训练包含软边缘的图像,通常用于营造更具绘画感或艺术效果。
lllyasviel/control_v11e_sd15_shuffle
基于图像打乱训练包含打乱的图像块或区域的图像。
lllyasviel/control_v11f1e_sd15_tile
基于图像平铺训练模糊图像或图像的一部分。

更多信息

如需了解更多信息,也请查看 Diffusers ControlNet 博客文章 和 官方文档。