SiamCafe.net Blog
Technology

stable diffusion colab

stable diffusion colab
stable diffusion colab | SiamCafe Blog
2025-06-01· อ. บอม — SiamCafe.net· 8,740 คำ

Stable Diffusion บน Colab

Stable Diffusion Google Colab Text-to-Image AUTOMATIC1111 ComfyUI Diffusers SDXL LoRA ControlNet Prompt GPU T4 A100 Free

PlatformGPUVRAMราคาSession
Colab FreeT415GBฟรีจำกัด ~2-4 ชม.
Colab ProT4/V100/A10015-40GB$10/เดือน24 ชม.
Colab Pro+A10040GB$50/เดือน24 ชม. + Background
RunPodRTX 3090/409024GB$0.2-0.7/ชม.ไม่จำกัด
Vast.aiหลากหลาย8-80GB$0.1-0.5/ชม.ไม่จำกัด
Local PCRTX 3060+12GB+ค่าไฟไม่จำกัด

Colab Setup & Pipeline

# === Stable Diffusion on Google Colab ===

# Cell 1: Install Dependencies
# !pip install diffusers transformers accelerate safetensors
# !pip install xformers  # Memory optimization

# Cell 2: Load Model
# from diffusers import StableDiffusionXLPipeline
# import torch
#
# pipe = StableDiffusionXLPipeline.from_pretrained(
#     "stabilityai/stable-diffusion-xl-base-1.0",
#     torch_dtype=torch.float16,
#     variant="fp16",
#     use_safetensors=True
# )
# pipe = pipe.to("cuda")
# pipe.enable_xformers_memory_efficient_attention()
#
# # Cell 3: Generate Image
# prompt = "masterpiece, best quality, 1girl, long hair, blue eyes"
# negative = "worst quality, low quality, blurry, deformed"
# image = pipe(prompt, negative_prompt=negative,
#              num_inference_steps=30, guidance_scale=7.5).images[0]
# image.save("output.png")

from dataclasses import dataclass

@dataclass
class ColabSetup:
    step: str
    code: str
    time: str
    note: str

steps = [
    ColabSetup("Install Dependencies",
        "pip install diffusers transformers accelerate xformers",
        "2-3 นาที",
        "ติดตั้งครั้งเดียวต่อ Session"),
    ColabSetup("Download Model",
        "StableDiffusionXLPipeline.from_pretrained()",
        "5-10 นาที (SDXL ~6.5GB)",
        "Cache ใน /root/.cache ถ้า Session เดิม"),
    ColabSetup("Load to GPU",
        "pipe.to('cuda') + enable_xformers",
        "10-30 วินาที",
        "ใช้ float16 ลด VRAM 50%"),
    ColabSetup("Generate Image",
        "pipe(prompt, steps=30, cfg=7.5)",
        "10-30 วินาที (T4) 3-10 วินาที (A100)",
        "SDXL 1024x1024 ต้อง T4+ VRAM 15GB+"),
    ColabSetup("Save & Download",
        "image.save() + files.download()",
        "ทันที",
        "หรือ Upload ไป Google Drive"),
]

print("=== Colab Setup Steps ===")
for s in steps:
    print(f"  [{s.step}]")
    print(f"    Code: {s.code}")
    print(f"    Time: {s.time}")
    print(f"    Note: {s.note}")

Prompt Engineering

# === Prompt Templates ===

@dataclass
class PromptTemplate:
    style: str
    positive: str
    negative: str
    cfg: float
    steps: int
    sampler: str

templates = [
    PromptTemplate("Anime Character",
        "masterpiece, best quality, 1girl, long silver hair, "
        "blue eyes, school uniform, cherry blossom, spring, "
        "detailed background, soft lighting, anime style",
        "worst quality, low quality, blurry, deformed, "
        "extra fingers, bad anatomy, watermark, text",
        7.5, 30, "DPM++ 2M Karras"),
    PromptTemplate("Photorealistic Portrait",
        "RAW photo, masterpiece, best quality, ultra detailed, "
        "1woman, 25yo, beautiful face, natural skin, "
        "studio lighting, shallow depth of field, 85mm lens, "
        "bokeh, photorealistic",
        "painting, drawing, anime, cartoon, deformed, "
        "airbrushed, overexposed, blurry",
        9.0, 40, "DPM++ SDE Karras"),
    PromptTemplate("Landscape Fantasy",
        "masterpiece, epic fantasy landscape, floating islands, "
        "waterfalls, magical forest, glowing crystals, "
        "dramatic sky, golden hour, volumetric lighting, "
        "matte painting style, ultra detailed",
        "low quality, blurry, text, watermark, "
        "modern buildings, cars, people",
        8.0, 35, "Euler a"),
    PromptTemplate("Product Photo",
        "professional product photography, white sneaker, "
        "clean white background, studio lighting, "
        "high resolution, commercial, minimalist, "
        "sharp focus, 8k uhd",
        "shadow, dirty, damaged, text, watermark, "
        "blurry, low quality",
        10.0, 40, "DDIM"),
]

print("=== Prompt Templates ===")
for t in templates:
    print(f"\n  [{t.style}]")
    print(f"    Positive: {t.positive[:80]}...")
    print(f"    Negative: {t.negative[:60]}...")
    print(f"    CFG: {t.cfg} | Steps: {t.steps} | Sampler: {t.sampler}")

Model & ControlNet

# === Models & Extensions ===

@dataclass
class SDModel:
    name: str
    type_: str
    size: str
    style: str
    source: str

models = [
    SDModel("SDXL Base 1.0",
        "Checkpoint (Official)",
        "6.5GB",
        "General Purpose High Quality 1024x1024",
        "Hugging Face stabilityai/stable-diffusion-xl-base-1.0"),
    SDModel("Realistic Vision V6",
        "Checkpoint (Community)",
        "2GB (SD 1.5)",
        "Photorealistic เหมือนจริงมาก",
        "CivitAI"),
    SDModel("Anything V5",
        "Checkpoint (Community)",
        "2GB (SD 1.5)",
        "Anime สวยงาม สีสดใส",
        "CivitAI"),
    SDModel("Detail Tweaker LoRA",
        "LoRA",
        "144MB",
        "เพิ่มรายละเอียด ใช้กับ Checkpoint ไหนัก็ได้",
        "CivitAI"),
    SDModel("ControlNet Canny",
        "ControlNet",
        "1.4GB",
        "ควบคุม Output ตามขอบภาพ Edge Detection",
        "Hugging Face lllyasviel/ControlNet"),
    SDModel("ControlNet OpenPose",
        "ControlNet",
        "1.4GB",
        "ควบคุม Pose ท่าทางคน",
        "Hugging Face lllyasviel/ControlNet"),
]

print("=== SD Models ===")
for m in models:
    print(f"  [{m.name}] Type: {m.type_} | Size: {m.size}")
    print(f"    Style: {m.style}")
    print(f"    Source: {m.source}")

เคล็ดลับ

Stable Diffusion คืออะไร

Open Source Text-to-Image AI Latent Diffusion SD 1.5 SDXL SD 3.0 FLUX img2img Inpainting ControlNet LoRA GPU Upscaling

ติดตั้งบน Colab อย่างไร

Runtime GPU T4 pip install diffusers AUTOMATIC1111 WebUI ComfyUI Diffusers Pipeline share=True ngrok Colab Free Pro Session

Prompt เขียนอย่างไร

Subject Detail Style Quality Background Lighting Negative Prompt Weight CFG 7-12 Steps 20-50 Sampler DPM++ Euler DDIM Template

Model และ LoRA ใช้อย่างไร

Checkpoint SDXL Realistic Anime LoRA Fine-tune CivitAI Hugging Face ControlNet Canny OpenPose Depth Scribble Upscaling

สรุป

Stable Diffusion Colab Text-to-Image SDXL AUTOMATIC1111 ComfyUI Diffusers Prompt LoRA ControlNet GPU T4 Free Production

📖 บทความที่เกี่ยวข้อง

Stable Diffusion ComfyUI Observability Stackอ่านบทความ → stable diffusion installอ่านบทความ → Stable Diffusion ComfyUI Compliance Automationอ่านบทความ → Stable Diffusion ComfyUI Multi-cloud Strategyอ่านบทความ → Stable Diffusion ComfyUI Certification Pathอ่านบทความ →

📚 ดูบทความทั้งหมด →