在 Google Colab 中,你可以使用 Hugging Face Diffusers 來下載並加載 dreamlike-art/dreamlike-photoreal-2.0
模型。以下是完整的 Colab 設定流程:
🚀 在 Colab 設定並加載 dreamlike-art/dreamlike-photoreal-2.0
1️⃣ 安裝依賴
首先,確保你已經安裝 diffusers
和 transformers
:
!pip install --upgrade diffusers transformers accelerate safetensors
2️⃣ 登入 Hugging Face (如果需要權杖)
某些模型需要 Hugging Face API Token(免費註冊可獲取)。 如果模型有存取限制,你需要先登入:
from huggingface_hub import notebook_login
notebook_login()
然後,根據提示輸入 Hugging Face API Token。
3️⃣ 下載並加載 dreamlike-art/dreamlike-photoreal-2.0
import torch
from diffusers import StableDiffusionPipeline
# Hugging Face 模型名稱
MODEL_NAME = "dreamlike-art/dreamlike-photoreal-2.0"
# 下載並加載模型
pipe = StableDiffusionPipeline.from_pretrained(
MODEL_NAME,
torch_dtype=torch.float16, # 使用 FP16 減少 VRAM 佔用
safety_checker=None # 避免某些版本 diffusers 需要安全檢查器
).to("cuda") # 移動到 GPU
print("✅ 模型加載完成!")
4️⃣ 生成測試圖片
你可以用簡單的 prompt 測試模型:
prompt = "a futuristic cityscape with neon lights, ultra-realistic, 4K"
image = pipe(prompt).images[0]
# 顯示圖片
image.show()
如果你想將圖片存到 Colab 磁碟:
image.save("/content/dreamlike_output.png")
範例:存回 Google Drive
from diffusers import StableDiffusionPipeline
import torch
from google.colab import drive
# 🔹 1. 掛載 Google Drive
drive.mount('/content/drive')
# 🔹 2. 設定 Google Drive 儲存路徑
output_path = "/content/drive/MyDrive/dreamlike_output.png"
# 🔹 3. 加載模型
MODEL_NAME = "dreamlike-art/dreamlike-photoreal-2.0"
pipe = StableDiffusionPipeline.from_pretrained(
MODEL_NAME,
torch_dtype=torch.float16
).to("cuda")
# 🔹 4. 生成圖像
prompt = "A futuristic city with neon lights, cyberpunk style"
image = pipe(prompt).images[0] # 取出生成的圖片
# 🔹 5. 儲存圖片到 Google Drive
image.save(output_path)
print(f"✅ 圖片已儲存到: {output_path}")
⚡ 附加優化(可選)(很容易不相容)
如果你想 加速推理,可以啟用 xformers
和 memory_efficient_attention
:
!pip install xformers
pipe.enable_xformers_memory_efficient_attention()
這可以 減少 VRAM 佔用並加快推理速度 🚀。
✅ 總結
這樣,你已經成功:
- 在 Colab 安裝依賴
- 從 Hugging Face 下載
dreamlike-art/dreamlike-photoreal-2.0
- 使用 Stable Diffusion 生成測試圖片
- 啟用
xformers
進行加速(可選)
這個模型擅長 擬真影像(Photorealism),適合 照片級別細節的生成與 LoRA 訓練 🎨✨。