今天正式嘗試了 Google Cloud Skills Boost 的生成式 AI 實驗課程。報名後,系統會先發給你 9 點積分,而要再獲得額外的 200 點免費點數,必須先完成至少一個實驗室(Lab)。建議可以從這個入門實驗開始:
👉 Build an AI Image Generator app using Imagen on Vertex AI
這是一個非常實用的基礎實驗,能快速體驗 Google Cloud 的 Vertex AI 服務,並了解如何呼叫 Imagen 3.0 模型來生成圖片。
時間:約 15 分鐘
費用:免費(需登入帳號)
難度:入門
在這個 Lab 裡,你會學會:
ImageGenerationModel.from_pretrained()
載入 imagen-3.0-generate-002
。image.jpeg
)。在 Cloud Editor 中建立一個新檔案 GenerateImage.py
,貼上以下程式:
import argparse
import vertexai
from vertexai.preview.vision_models import ImageGenerationModel
def generate_image(project_id: str, location: str, output_file: str, prompt: str):
vertexai.init(project=project_id, location=location)
model = ImageGenerationModel.from_pretrained("imagen-3.0-generate-002")
images = model.generate_images(
prompt=prompt,
number_of_images=1,
seed=1,
add_watermark=False,
)
images[0].save(location=output_file)
return images
# 範例執行
generate_image(
project_id="your-project-id",
location="us-central1",
output_file="image.jpeg",
prompt="Create an image of a cricket ground in the heart of Los Angeles",
)
接著執行:
/usr/bin/python3 /GenerateImage.py
執行完成後,可在 EXPLORER 面板中看到生成的 image.jpeg
。
vertexai.init()
:初始化專案與地區設定。ImageGenerationModel.from_pretrained()
:載入 Google 的 Imagen 模型。generate_images()
:依據文字 prompt 生成圖像,可設定圖片數量、隨機種子、浮水印等參數。add_watermark=False
:關閉預設的 SynthID 浮水印(無法與 seed 同時使用)。完成後,別忘了點擊 Check my progress 驗證成果,最後記得按end lab,這樣才能正式獲得 200 點額度,繼續挑戰更進階的生成式 AI Lab!