モデル概要
モデル特徴
モデル能力
使用事例
🚀 Controlnet - v1.1 - InPaint Version
ControlNetは、追加の条件を付与することで拡散モデルを制御するニューラルネットワーク構造です。このモデルは、Stable Diffusionなどの拡散モデルと組み合わせて使用することができ、画像のインペイントなどの追加条件をサポートします。
🚀 クイックスタート
コードの実行例
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 は、追加の条件を付与することで拡散モデルを制御することができます。
- このチェックポイントは、元のチェックポイントを
diffusers
形式に変換したもので、Stable Diffusionと組み合わせて使用することができます。 - 画像のインペイントなどの追加条件をサポートしています。
📦 インストール
1. `diffusers` と関連パッケージをインストールしましょう。
$ pip install diffusers transformers accelerate
## 💻 使用例
### 基本的な使用法
```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')
📚 ドキュメント
Controlnet v1.1の概要
Controlnet v1.1 は、Lvmin Zhangによって lllyasviel/ControlNet-v1-1 でリリースされました。このチェックポイントは、元のチェックポイント を diffusers
形式に変換したもので、runwayml/stable-diffusion-v1-5 などの Stable Diffusion と組み合わせて使用することができます。
詳細については、🧨 Diffusers docs も参照してください。
モデルの詳細
属性 | 详情 |
---|---|
開発者 | Lvmin Zhang, Maneesh Agrawala |
モデルタイプ | 拡散ベースのテキストから画像への生成モデル |
言語 | 英語 |
ライセンス | The CreativeML OpenRAIL M license は、Open RAIL M license であり、BigScience と the RAIL Initiative が共同で行っている責任あるAIライセンスの分野の作業から適応されています。詳細については、the article about the BLOOM Open RAIL license も参照してください。 |
詳細情報のリソース | GitHub Repository, Paper |
引用形式 | @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 で提案されました。
概要は以下の通りです。 私たちは、事前学習された大規模拡散モデルを制御して、追加の入力条件をサポートするニューラルネットワーク構造であるControlNetを提案します。ControlNetは、タスク固有の条件をエンドツーエンドで学習し、学習データセットが小さい場合(< 50k)でもロバストです。また、ControlNetの学習は拡散モデルのファインチューニングと同じくらい速く、個人用のデバイスで学習することができます。あるいは、強力な計算クラスターが利用可能な場合、モデルは大量(数百万から数十億)のデータに拡張することができます。Stable Diffusionのような大規模拡散モデルは、ControlNetを用いて拡張することで、エッジマップ、セグメンテーションマップ、キーポイントなどの条件入力を可能にすることができます。これにより、大規模拡散モデルを制御する方法が豊富になり、関連するアプリケーションがさらに促進される可能性があります。
他のリリースされたチェックポイント v1-1
著者らは、Stable Diffusion v1-5 を使用して、異なるタイプの条件付きで学習された14種類の異なるチェックポイントをリリースしました。
モデル名 | コントロール画像の概要 | 条件画像 | コントロール画像の例 | 生成画像の例 |
---|---|---|---|---|
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 |
スクリブルベースの画像生成で学習 | スクリブルがある画像、通常はランダムまたはユーザーが描いたストロークです。 | ![]() |
![]() |
📄 ライセンス
このモデルは The CreativeML OpenRAIL M license の下で提供されています。

