樣式自訂

Imagen 3 的自訂功能可根據您提供的文字提示和參考圖片,生成新的圖片。參考圖片會引導生成新圖片。

下列機型支援自訂樣式:

用途

Imagen 3 Customization 提供自由式提示詞,因此可能會給人一種印象,認為這項工具的功能比實際訓練內容更多。以下各節將說明 Imagen 3 Customization 的預期用途,以及非預期用途的範例 (僅列舉部分)。

建議您針對預期用途使用 Imagen 3 Customization,因為我們已針對這些用途訓練模型,預期可獲得良好結果。反之,雖然您可以讓模型執行預期用途以外的動作,但我們不認為會得到良好的結果。

預定用途

以下是 Imagen 3 Customization 樣式自訂功能的適用用途:

  • 根據輸入的文字生成圖像,並套用參考圖片提供的特定風格。
  • 變更人物相片。
  • 變更人物相片,並保留臉部表情。

不當用途範例

以下列舉幾個 Imagen 3 自訂功能無法訓練的用途,以及可能產生不良結果的用途:

  • 根據文字和參考圖像生成圖片,並透過參考圖像控制生成圖片的構圖。

  • 從參考圖片生成人物圖像,參考圖片中的人物帶有特定臉部表情。

  • 將兩個人放在不同場景,保留他們的身份,並使用參考圖片指定輸出圖片的樣式 (例如油畫)。

  • 將寵物相片轉換成繪圖,同時保留或指定圖片構圖。

  • 將產品 (例如餅乾或沙發) 放置在不同場景,並從不同角度拍攝,同時遵循特定圖片風格 (例如特定顏色、光線風格或動畫的擬真照片)。

樣式自訂範例

以下是 Imagen 3 自訂樣式的範例:

輸入內容範例 輸出內容範例
  1. 參考圖像1
    動物夥伴自訂功能的輸入範例
  2. 文字提示:
    Generate an image in mosaic style [1] based on
    the following caption: The New York City skyline

動物同伴自訂輸出內容範例

1 使用 Imagen 3 生成的參考輸入圖片 提示詞:簡單的馬賽克

查看「Imagen for Editing and Customization」模型資訊卡

事前準備

  1. 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  8. 為環境設定驗證機制。

    選取這個頁面上的分頁,瞭解如何使用範例:

    控制台

    使用 Google Cloud 控制台存取 Google Cloud 服務和 API 時,無須設定驗證。

    REST

    如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

      安裝 Google Cloud CLI。

      若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI

    詳情請參閱 Google Cloud 驗證說明文件中的「使用 REST 進行驗證」。

自訂樣式

使用 Imagen 3 自訂功能時,您可以提供樣式類型的參考圖片。您選擇的風格會影響生成要求的形式。

使用 Imagen 3 自訂功能時,提示可能會影響生成的圖片品質。以下各節說明建議的提示範本和範例,可做為傳送自訂要求時的參考。

用途 參考圖片 提示範本 範例
物件樣式 主體圖像 (1 到 4 張) 根據以下說明文字,在 STYLE_DESCRIPTION [1] 中生成圖片:IMAGE_DESCRIPTION 根據以下說明文字,在 neon sign style [1] 中生成圖片:a sign saying have a great day
不使用臉部網格輸入內容,為人物圖片套用風格 主體圖像 (1 到 4 張) 建立與說明相符的 SUBJECT_DESCRIPTION [1] 圖片:SUBJECT_DESCRIPTION [1] 的肖像照 ${PROMPT} 請根據以下說明,製作與「a woman with short hair[1]」相關的圖片:以 3D 卡通風格繪製「a woman with short hair[1]」的肖像,背景模糊處理。可愛迷人的角色、笑臉、看著相機、柔和色調、高畫質、4K、傑作、超級細節、皮膚材質、材質對應、柔和陰影、柔和寫實的光線、鮮豔色彩
以臉部網格輸入內容為人物圖片套用風格 主體圖片 (1-3)

臉部網格控制圖片 (1)
Create an image about SUBJECT_DESCRIPTION [1] in the pose of the CONTROL_IMAGE [2] to match the description: a portrait of SUBJECT_DESCRIPTION [1] ${PROMPT} control image [2]的姿勢繪製「a woman with short hair [1]」的圖片,符合以下描述:a woman with short hair [1]的肖像照,採用 3D 卡通風格,背景模糊。可愛的微笑臉龐。請參照 相機、柔和色調、高品質、4K、傑作、超級細節、 皮膚紋理、紋理對應、柔和陰影、柔和逼真光線、 鮮豔色彩

REST

如要進一步瞭解 Imagen API,請參閱下列內容:

使用任何要求資料之前,請先修改下列項目的值:

  • PROJECT_ID:您的 Google Cloud 專案 ID
  • LOCATION:專案的區域。例如 us-central1europe-west2asia-northeast3。如需可用區域的清單,請參閱「Vertex AI 生成式 AI 服務地區」。使用地區 API 端點時,系統會根據端點網址中的區域,決定要求處理位置,並忽略資源路徑中發生衝突的 LOCATION
  • TEXT_PROMPT:文字提示會引導模型生成圖片。如要使用 Imagen 3 自訂功能,請在 [$referenceId] 格式中加入您提供的參考圖片 referenceId。例如:
    • 以下文字提示是針對含有單一參考圖片的要求,並提供"referenceId": 1和選用的"styleDescription": "glowing style"說明: 根據以下說明在 glowing style [1] 中生成圖片:山中的教堂。
  • "referenceId":參考圖片的 ID,或對應相同主題或風格的一系列參考圖片 ID。在本範例中,單一參照圖片的 referenceId 為 (1)。
  • BASE64_REFERENCE_IMAGE:引導圖片生成的參考圖片。圖片必須指定為Base64 編碼的位元組字串。
  • STYLE_DESCRIPTION:選用。參考圖片的文字說明,可用於 prompt 欄位。例如:
          "prompt": "Generate an image in glowing style [1] based on the following
            caption: A church in the mountain.",
          [...],
          "styleImageConfig": {
            "styleDescription": "glowing style"
          }
        
  • IMAGE_COUNT:生成的圖像數量。 接受的整數值:1 到 4。預設值:4。

HTTP 方法和網址:

POST https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict

JSON 要求內文:

{
  "instances": [
    {
      "prompt": "TEXT_PROMPT",
      "referenceImages": [
        {
          "referenceType": "REFERENCE_TYPE_STYLE",
          "referenceId": 1,
          "referenceImage": {
            "bytesBase64Encoded": "BASE64_REFERENCE_IMAGE"
          },
          "styleImageConfig": {
            "styleDescription": "STYLE_DESCRIPTION"
          }
        }
      ]
    }
  ],
  "parameters": {
    "sampleCount": IMAGE_COUNT
  }
}

如要傳送要求,請選擇以下其中一個選項:

curl

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" \
-d @request.json \
"https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict"

PowerShell

將要求主體儲存在名為 request.json 的檔案中,然後執行下列指令:

$cred = gcloud auth print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

Invoke-WebRequest `
-Method POST `
-Headers $headers `
-ContentType: "application/json; charset=utf-8" `
-InFile request.json `
-Uri "https://LOCATION-aiplatform.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/publishers/google/models/imagen-3.0-capability-001:predict" | Select-Object -Expand Content
以下是要求的回應範例,其中包含 "sampleCount": 2。回應會傳回兩個預測物件,其中包含以 base64 編碼的生成圖片位元組。
{
  "predictions": [
    {
      "bytesBase64Encoded": "BASE64_IMG_BYTES",
      "mimeType": "image/png"
    },
    {
      "mimeType": "image/png",
      "bytesBase64Encoded": "BASE64_IMG_BYTES"
    }
  ]
}

Python

from google import genai
from google.genai.types import (
    EditImageConfig,
    Image,
    StyleReferenceConfig,
    StyleReferenceImage,
)

client = genai.Client()

# TODO(developer): Update and un-comment below line
# output_gcs_uri = "gs://your-bucket/your-prefix"

# Create a style reference image of a neon sign stored in Google Cloud Storage
# using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/neon.png
style_reference_image = StyleReferenceImage(
    reference_id=1,
    reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/neon.png"),
    config=StyleReferenceConfig(style_description="neon sign"),
)

image = client.models.edit_image(
    model="imagen-3.0-capability-001",
    prompt="generate an image of a neon sign [1] with the words: have a great day",
    reference_images=[style_reference_image],
    config=EditImageConfig(
        edit_mode="EDIT_MODE_DEFAULT",
        number_of_images=1,
        safety_filter_level="BLOCK_MEDIUM_AND_ABOVE",
        person_generation="ALLOW_ADULT",
        output_gcs_uri=output_gcs_uri,
    ),
)

# Example response:
# gs://your-bucket/your-prefix
print(image.generated_images[0].image.gcs_uri)

產品使用量累進區間

如要查看與 Vertex AI 上的 Imagen 相關聯的使用標準和內容限制,請參閱使用指南

模型版本

你可以使用多種圖片生成模型。詳情請參閱「Imagen 模型」。

後續步驟

閱讀有關 Imagen 和其他 Vertex AI 生成式 AI 產品的文章: