樣式自訂

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

用途

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

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

目標用途

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

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

不當用途範例

以下列舉幾個 Imagen 3 自訂功能無法處理,且會產生不良結果的用途:

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

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

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

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

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

樣式自訂範例

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

輸入內容範例 輸出內容範例
  1. 參考圖片1
    動物夥伴自訂功能的輸入範例
  2. 文字提示:
    根據以下說明在 mosaic style [1] 中生成圖片:紐約市天際線

動物同伴自訂項目的輸出範例

1 使用 Imagen 3 生成的參考輸入圖片 根據提示「簡單的馬賽克」生成圖片。

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

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Vertex AI API.

    Enable the API

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

    Go to project selector

  6. Make sure that billing is enabled for your Google Cloud project.

  7. Enable the Vertex AI API.

    Enable the API

  8. 為環境設定驗證方法。

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    REST

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

      After installing the Google Cloud CLI, initialize it by running the following command:

      gcloud init

      If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

    詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using 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)
    SUBJECT_DESCRIPTION [1]的姿勢建立CONTROL_IMAGE [2]圖片,符合以下說明:SUBJECT_DESCRIPTION [1]的肖像照 ${PROMPT} 根據說明,以「control image [2]」的姿勢繪製「a woman with short hair [1]」的圖片:3D 卡通風格的「a woman with short hair [1]」肖像,背景模糊。可愛的角色,笑臉。See the camera, pastel color tone, high quality, 4k, masterpiece, super details, skin texture, texture mapping, Soft shadows, soft realistic lighting, vibrant colors

    REST

    如要進一步瞭解 imagen-3.0-capability-001 模型要求,請參閱 imagen-3.0-capability-001 模型 API 參考資料

    使用任何要求資料之前,請先替換以下項目:

    • PROJECT_ID:您的 Google Cloud 專案 ID
    • LOCATION:專案的區域。例如 us-central1europe-west2asia-northeast3。如需可用區域的清單,請參閱「Vertex AI 的生成式 AI 服務地區」。
    • 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,
            seed=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 產品的文章: