はじめに
ComfyUI は、Stable Diffusion 用のノードベースの UI です。これは、ノードをドラッグ アンド ドロップすることで複雑な生成パイプラインを構築するのに役立つ視覚的なワークフローです。シンプルな txt2img からマルチ ControlNet + LoRA + 高級パイプラインまで、ComfyUI は最も重要な制作ツールです。
1. ComfyUI のセットアップ
# Clone repository
git clone https://github.com/comfyanonymous/ComfyUI.git
cd ComfyUI
# Install dependencies
pip install -r requirements.txt
# Download models
# Place in ComfyUI/models/checkpoints/
# SDXL: stabilityai/stable-diffusion-xl-base-1.0
# Start server
python main.py --listen 0.0.0.0 --port 8188
# Access at http://localhost:8188
ディレクトリ構造
ComfyUI/
├── models/
│ ├── checkpoints/ # Base models (.safetensors)
│ ├── loras/ # LoRA weights
│ ├── controlnet/ # ControlNet models
│ ├── vae/ # VAE models
│ ├── upscale_models/ # Upscalers
│ └── embeddings/ # Textual inversions
├── custom_nodes/ # Community extensions
├── input/ # Input images
└── output/ # Generated images
2. 基本的なワークフロー — テキストから画像へ
{
"nodes": [
{"type": "CheckpointLoaderSimple", "model": "sdxl_base.safetensors"},
{"type": "CLIPTextEncode", "text": "a cat in space, digital art"},
{"type": "CLIPTextEncode", "text": "blurry, low quality"},
{"type": "EmptyLatentImage", "width": 1024, "height": 1024},
{"type": "KSampler", "steps": 30, "cfg": 7.5, "scheduler": "euler"},
{"type": "VAEDecode"},
{"type": "SaveImage", "filename_prefix": "output"}
]
}
Node flow:
CheckpointLoader → MODEL, CLIP, VAE
↓
CLIPTextEncode (positive) ─┐
CLIPTextEncode (negative) ─┤
EmptyLatentImage ──────────┤
↓
KSampler → LATENT
↓
VAEDecode → IMAGE
↓
SaveImage
3. 高度なワークフロー — ControlNet + LoRA
Workflow:
1. Load base model
2. Load LoRA → merge with model
3. Load ControlNet model
4. Preprocess input (Canny/Depth/Pose)
5. Apply ControlNet conditioning
6. Sample with both text + control
7. Upscale result
8. Save
主要ノード
| ノード | 目的 |
|---|---|
| KSサンプラー | メインのノイズ除去サンプラー |
| コントロールネット適用 | ControlNet コンディショニングを適用する |
| ロラローダー | LoRA 重みをロードする |
| モデル付きイメージアップスケール | AI アップスケーリング (4x) |
| VAEEエンコード/デコード | 潜在 ↔ ピクセル |
| イメージスケール | 画像のサイズを変更する |
| モデル付きの顔復元 | 顔を修正/強化する |
4. ComfyUI API モード
import json
import requests
import io
from PIL import Image
COMFYUI_URL = "http://localhost:8188"
def queue_prompt(workflow):
"""Submit workflow to ComfyUI"""
response = requests.post(
f"{COMFYUI_URL}/prompt",
json={"prompt": workflow}
)
return response.json()["prompt_id"]
def get_image(prompt_id):
"""Wait and retrieve generated image"""
import time
while True:
response = requests.get(f"{COMFYUI_URL}/history/{prompt_id}")
history = response.json()
if prompt_id in history:
outputs = history[prompt_id]["outputs"]
for node_id, output in outputs.items():
if "images" in output:
image_data = output["images"][0]
img_response = requests.get(
f"{COMFYUI_URL}/view",
params=image_data
)
return Image.open(io.BytesIO(img_response.content))
time.sleep(1)
# Load workflow from file
with open("my_workflow_api.json") as f:
workflow = json.load(f)
# Modify prompt dynamically
workflow["6"]["inputs"]["text"] = "a dragon flying over mountains"
# Generate
prompt_id = queue_prompt(workflow)
image = get_image(prompt_id)
image.save("result.png")
5. バッチ処理
import json
def batch_generate(workflow_path, prompts, output_dir):
"""Generate images for multiple prompts"""
with open(workflow_path) as f:
base_workflow = json.load(f)
for i, prompt in enumerate(prompts):
workflow = json.loads(json.dumps(base_workflow))
workflow["6"]["inputs"]["text"] = prompt
workflow["9"]["inputs"]["filename_prefix"] = f"batch_{i:04d}"
prompt_id = queue_prompt(workflow)
image = get_image(prompt_id)
image.save(f"{output_dir}/batch_{i:04d}.png")
print(f"✓ [{i+1}/{len(prompts)}] {prompt[:50]}...")
# Usage
prompts = [
"a sunset over mountains, photography",
"a futuristic city at night, cyberpunk",
"a peaceful garden, watercolor painting",
]
batch_generate("workflow_api.json", prompts, "output/")
6. カスタムノード
Popular custom node packs:
- ComfyUI-Manager: install/manage other custom nodes
- comfyui-reactor: face swap
- ComfyUI-Impact-Pack: detailer, face fixes
- ComfyUI-AnimateDiff: animation workflows
- comfyui-tooling-nodes: utility nodes
- ComfyUI-KJNodes: quality-of-life nodes
Install:
cd ComfyUI/custom_nodes
git clone https://github.com/author/custom-node-pack
pip install -r custom-node-pack/requirements.txt
# Restart ComfyUI
7. パフォーマンスの最適化
Memory optimization:
- Use --lowvram or --medvram flags
- Enable tiling for large images
- Use FP16 models
- Clear VRAM between batches
Speed optimization:
- Use fast schedulers (DPM++ 2M, Euler)
- Reduce steps (20-25 usually sufficient)
- Batch in latent space
- Use SDXL Turbo for real-time (4 steps)
Quality optimization:
- 2-pass workflow: base → refiner
- Hi-res fix: generate small → upscale → img2img
- Face restore: GFPGAN, CodeFormer
- 4x upscale: RealESRGAN, SwinIR
概要
| 特長 | 説明 |
|---|---|
| ノードベースの UI | ビジュアルワークフローデザイン |
| APIモード | 自動化とバッチ処理 |
| カスタムノード | コミュニティ拡張 |
| ワークフローの共有 | JSON のエクスポート/インポート |
| 生産準備完了 | 統合用APIサーバー |
📌 次の記事: Generative AI API サーバー — プラットフォームの構築。