主题自定义

借助 Imagen 3 自定义的正文自定义功能,您可以根据文本提示和您提供的参考图片生成新图片。您提供的参考图片有助于引导新图片的生成。

使用场景

Imagen 3 自定义功能提供自由式提示,这可能会给人一种印象,即它能做的事情比训练时学到的更多。以下部分介绍了 Imagen 3 自定义功能的预期用例,以及一些非详尽的非预期用例示例。

我们建议您将 Imagen 3 自定义功能用于预期用例,因为我们已针对这些用例训练了模型,并期望获得良好的效果。 反之,虽然您可以让模型执行预期使用场景之外的任务,但我们不保证能获得理想的结果。

预期使用情形

以下是适用于 Imagen 3 自定义主题自定义的使用情形:

  • 美化人物照片
  • 对人物照片进行风格化处理,并保留人物的面部表情
  • (成功率低)将沙发或饼干等产品放置在不同的场景中,并采用不同的产品角度。
  • 生成不保留确切详细信息的商品变体
  • 对人像照片进行风格化处理,同时保留面部表情

非预期用例示例

以下列出了 Imagen 3 自定义功能未经过训练,因此无法很好地处理的用例(该列表并未包含所有用例):

  • 将两个人或更多人放置在不同的场景中,同时保留他们的身份
  • 将两名或更多人物放置在不同的场景中,同时保留其身份,并使用示例图片作为风格输入来指定输出图片的风格。
  • 对包含两张或更多人像的照片进行风格化处理,同时保留人像身份信息
  • 将宠物放置到不同的场景中,同时保留其身份
  • 将宠物照片风格化并转换为绘画
  • 对宠物照片进行风格化处理,将其转换为绘画,同时保留或指定图片的风格(例如水彩)
  • 将宠物和人放置在不同的场景中,同时保留两者的身份。
  • 将宠物和一张或多张人物照片进行风格化处理,然后将其转换为绘画作品
  • 将两款产品放置在不同的场景中,并采用不同的产品角度
  • 将商品(例如饼干或沙发)放置在不同的场景中,并采用不同的商品角度,同时遵循特定的图片风格(例如具有特定颜色、光照风格或动画效果的写实风格)
  • 将商品放置到其他场景中,同时保留控制图片指定的特定场景构图
  • 将两款产品放置在不同的场景中,并采用不同的产品角度,同时使用特定图片作为输入内容(例如具有特定颜色、光照样式或动画效果的写实图片)
  • 将两款产品放置到不同的场景中,同时保留控制图片中指定的特定场景构图

主题自定义示例

以下部分介绍了 Imagen 3 自定义主题自定义支持的用例:

人物自定义

示例输入 输出示例
  1. 参考图片1
    产品自定义示例输入
  2. 文本提示:
    Generate an image about the woman with long hair[1] to match this description: a portrait of a woman with long hair[1] in 3d-cartoon style with blurred background. 一个可爱迷人的角色,面带微笑,看着镜头,采用柔和的色调,高品质,4k,杰作,细节丰富,皮肤纹理,纹理映射,柔和的阴影,柔和逼真的光线,鲜艳的色彩。

产品自定义示例输出

1 使用 Imagen 3 图片生成功能根据以下提示生成的参考输入图片:portrait of a woman in paris. she's wearing black pants and a white shirt

产品自定义

示例输入 输出示例
  1. 参考图片2
    产品自定义示例输入
  2. 文本提示:
    Generate an image of the perfume bottle [1] but in cyan

产品自定义示例输出
2 使用 Imagen 3 图片生成功能根据以下提示生成的参考输入图片:perfume bottle product style image in front of a black background

查看 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 身份验证文档中的使用 REST 时进行身份验证

    主题自定义

    使用 Imagen 3 自定义时,您可以提供主题类型的参考图片。具体而言,使用 Imagen 3 自定义的少样本提示支持以下主题:产品、人物和宠物。您选择的主题会影响您生成请求的方式。

    与 Imagen 3 自定义结合使用的提示可能会影响生成的图片的质量。以下部分介绍了建议的提示模板和示例,可用于发送自定义请求。

    人物自定义

    下表介绍了我们建议您在撰写人物自定义提示时作为起点的提示模板:

    使用场景 参考图片 提示模板 示例
    使用人脸网格输入进行人物图片风格化 主题图片 (1)

    Facemesh 控制图片 (1)
    Generate an image of SUBJECT_DESCRIPTION [1] with the facemesh from the control image [2]. ${PROMPT} Generate an image of the person [1] with the facemesh from the control image [2]. The person should be looking straight ahead with a neutral expression. The background should be a ...
    不使用人脸网格输入进行人物图片风格化 主题图片 (1-4) Create an image about SUBJECT_DESCRIPTION [1] to match the description: a portrait of SUBJECT_DESCRIPTION [1] ${PROMPT} Create an image about a woman with short hair[1] to match the description: a portrait of a woman with short hair[1] in 3d-cartoon style with blurred background. 一个可爱的人物角色,笑脸,看着镜头,柔和的色调,高品质,4k,杰作,超级细节,皮肤纹理,纹理映射,柔和的阴影,柔和逼真的光线,鲜艳的色彩
    不使用人脸网格输入进行人物图片风格化 主题图片 (1-4) Create a STYLE_DESCRIPTION [2] image about SUBJECT_DESCRIPTION [1] to match the description: a portrait of SUBJECT_DESCRIPTION [1] STYLE_PROMPT Create a 3d-cartoon style [2] image about a woman with short hair [1] to match the description: a portrait of a woman with short hair [1] in 3d-cartoon style with blur background. 一个可爱的人物,笑脸,看向镜头,采用柔和的色调,高品质,4k,杰作,细节丰富,皮肤纹理,纹理映射,柔和的阴影,柔和逼真的光线,鲜艳的色彩
    使用人脸网格输入进行人物图片风格化 主题图片 (1-3)

    Facemesh 控制图片 (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} Create an image about a woman with short hair [1] in the pose of the control image [2] to match the description: a portrait of a woman with short hair [1] in 3d-cartoon style with blur background. 一个可爱萌萌的角色,笑脸。查看相机、柔和的色调、高品质、4k、杰作、超细节、皮肤纹理、纹理映射、柔和的阴影、柔和逼真的光线、鲜艳的色彩
    使用人脸网格输入进行人物图片风格化 主题图片 (1-3)

    Facemesh 控制图片 (1)
    Create a STYLE_DESCRIPTION [3] image about SUBJECT_DESCRIPTION [1] in the pose of the CONTROL_IMAGE [2] to match the description: a portrait of SUBJECT_DESCRIPTION [1] ${PROMPT} Create a 3d-cartoon style [3] image about a woman with short hair [1] in the pose of the control image [2] to match the description: a portrait of a woman with short hair [1] in 3d-cartoon style with blur background. 一个可爱的人物,笑脸。查看相机、柔和的色调、高品质、4k、杰作、超细节、皮肤纹理、纹理映射、柔和的阴影、柔和逼真的光线、鲜艳的色彩

    我们建议参考图片中的人脸具有以下特征:

    • 居中显示,且至少占据整个图片的一半
    • 在正面视图中沿所有方向(横滚、俯仰和偏航)旋转
    • 未被太阳镜或口罩等物体遮挡

    使用以下示例发送自定义请求,其中包含用于引导图片生成的人物参考图片。发送此类请求时可包含或不包含人脸网格控制图片,以进一步引导图片生成。

    REST

    如需详细了解 imagen-3.0-capability-001 模型请求,请参阅 imagen-3.0-capability-001 模型 API 参考文档

    此示例展示了如何指定人脸网格控制区域以引导生成,但您也可以省略控制参考对象 ("referenceType": "REFERENCE_TYPE_CONTROL"),Imagen 会自动检测人脸网格控制区域。

    在使用任何请求数据之前,请先进行以下替换:

    • PROJECT_ID:您的 Google Cloud 项目 ID
    • LOCATION:您的项目的区域。 例如 us-central1europe-west2asia-northeast3。如需查看可用区域的列表,请参阅 Vertex AI 上的生成式 AI 位置
    • TEXT_PROMPT:文本提示可用于指导模型生成的图片。如需使用 Imagen 3 自定义,请以 [$referenceId] 格式添加您提供的参考图片的 referenceId。例如:
      • 以下文本提示适用于包含两个具有 "referenceId": 1 的参考图片的请求。这两个图片的可选说明均为 "subjectDescription": "man with short hair"。此外,还有一个使用 "referenceId": 2 指定的控制人脸网格图片:Create an image about a man with short hair [1] in the pose of control image [2] to match the description: A pencil style sketch of a full-body portrait of a man with short hair [1] with hatch-cross drawing, hatch drawing of portrait with 6B and graphite pencils, white background, pencil drawing, high quality, pencil stroke, looking at camera, natural human eyes
    • "referenceId":参考图片的 ID,或与同一主体或风格对应的一系列参考图片的 ID。 在此示例中,这两个参考图片均为同一人,因此它们共用同一个 referenceId (1),而控制人脸网格图片具有不同的 referenceId (2)。生成的图片遵循从参考图片中提取的人脸网格的面部结构,并会改进面部外观跟踪。仅支持一个人脸网格控制。
    • BASE64_REFERENCE_IMAGE:用于引导图片生成的参考图片。图片必须指定为 base64 编码的字节字符串。
    • SUBJECT_DESCRIPTION:可选。您随后可以用于 prompt 字段的参考图片的文本说明。例如:
            "prompt": "a full-body portrait of a man with short hair [1] with hatch-cross
            drawing",
            [...],
            "subjectDescription": "man with short hair"
          
    • 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_SUBJECT",
              "referenceId": 1,
              "referenceImage": {
                "bytesBase64Encoded": "BASE64_REFERENCE_IMAGE"
              },
              "subjectImageConfig": {
                "subjectDescription": "SUBJECT_DESCRIPTION",
                "subjectType": "SUBJECT_TYPE_PERSON"
              }
            },
            {
              "referenceType": "REFERENCE_TYPE_SUBJECT",
              "referenceId": 1,
              "referenceImage": {
                "bytesBase64Encoded": "BASE64_REFERENCE_IMAGE"
              },
              "subjectImageConfig": {
                "subjectDescription": "SUBJECT_DESCRIPTION",
                "subjectType": "SUBJECT_TYPE_PERSON"
              }
            },
            {
              "referenceType": "REFERENCE_TYPE_CONTROL",
              "referenceId": 2,
              "referenceImage": {
                "bytesBase64Encoded": "BASE64_REFERENCE_IMAGE"
              },
              "controlImageConfig": {
                "controlType": "CONTROL_TYPE_FACE_MESH",
                "enableControlImageComputation": true
              }
            }
          ]
        }
      ],
      "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 (
        ControlReferenceConfig,
        ControlReferenceImage,
        EditImageConfig,
        Image,
        SubjectReferenceConfig,
        SubjectReferenceImage
    )
    
    client = genai.Client()
    
    # TODO(developer): Update and un-comment below line
    # output_gcs_uri = "gs://your-bucket/your-prefix"
    
    # Create subject and control reference images of a photograph stored in Google Cloud Storage
    # using https://storage.googleapis.com/cloud-samples-data/generative-ai/image/person.png
    subject_reference_image = SubjectReferenceImage(
        reference_id=1,
        reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/person.png"),
        config=SubjectReferenceConfig(
            subject_description="a headshot of a woman", subject_type="SUBJECT_TYPE_PERSON"
        ),
    )
    control_reference_image = ControlReferenceImage(
        reference_id=2,
        reference_image=Image(gcs_uri="gs://cloud-samples-data/generative-ai/image/person.png"),
        config=ControlReferenceConfig(control_type="CONTROL_TYPE_FACE_MESH"),
    )
    
    image = client.models.edit_image(
        model="imagen-3.0-capability-001",
        prompt="""
        a portrait of a woman[1] in the pose of the control image[2]in a watercolor style by a professional artist,
        light and low-contrast stokes, bright pastel colors, a warm atmosphere, clean background, grainy paper,
        bold visible brushstrokes, patchy details
        """,
        reference_images=[subject_reference_image, control_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)

    产品自定义

    下表介绍了我们建议您在撰写产品自定义提示时作为起点的提示模板:

    参考图片 提示模板 示例
    主题图片 (1-4) Create an image about SUBJECT_DESCRIPTION [1] to match the description: ${PROMPT}

    Create an image about Luxe Elixir hair oil, golden liquid in glass bottle [1] to match the description: A close-up, high-key image of a woman's hand holding Luxe Elixir hair oil, golden liquid in glass bottle [1] against a pure white background. The woman's hand is well-lit and the focus is sharp on the bottle, with a shallow depth of field blurring the background and emphasizing the product. The lighting is soft and diffused, creating a subtle glow around the bottle and hand. The overall composition is simple and elegant, highlighting the product's luxurious appeal.

    主题图片 (1-4) Generate an image of a SUBJECT_DESCRIPTION but ${PROMPT}

    生成一张 Seiko watch [1] 的图片,但采用蓝色

    使用以下示例发送自定义请求,其中包含用于引导图片生成的产品参考图片。

    控制台

    1. 在 Google Cloud 控制台中,前往 Vertex AI > Media Studio 页面。
      前往媒体工作室
    2. 参数窗格的模型部分中,选择 Imagen 3(如果尚未选择)。
    3. 可选。选择 1:1(默认值)以外的宽高比
    4. 可选。更改结果数量
    5. 可选。提供否定提示,以引导模型避免生成的内容。
    6. 可选。更改任何高级选项
    7. 文本提示字段中(撰写提示...),点击添加参考
      1. 添加参考窗格中,选择参考类型Subject - product
      2. 参考图片部分中,点击上传
      3. 选择本地存储的图片,然后点击打开
      4. 可选。提供参考图片的说明
      5. 点击完成
      6. 可选。如需添加更多参考图片,请点击添加图片,然后上传其他图片。
      7. 添加完所有参考图片后,点击添加参考

        您在该窗格中添加的所有参考图片都具有相同的参考编号。添加文本提示时,请使用此参考编号。

    8. 文本提示字段中(撰写提示...),添加文本提示,其中包含参考图片的参考编号。例如:
      1. bright white room, the product [1] on a glass table
      2. the animal [1] standing on a wide open field with a forest in the distance
      3. a black and white portrait of the person [1] on a city street in film noir style [2]

    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。例如:
      • Create an image about Luxe Elixir hair oil, golden liquid in glass bottle [1] to match the description: A close-up, high-key image of a woman's hand holding Luxe Elixir hair oil, golden liquid in glass bottle [1] against a pure white background. The woman's hand is well-lit and the focus is sharp on the bottle, with a shallow depth of field blurring the background and emphasizing the product.
    • "referenceId":参考图片的 ID,或与同一主体或风格对应的一系列参考图片的 ID。 在此示例中,这两个参考图片属于同一产品,因此共用同一个 referenceId (1)。
    • BASE64_REFERENCE_IMAGE:用于引导图片生成的参考图片。图片必须指定为 base64 编码的字节字符串。
    • SUBJECT_DESCRIPTION:可选。您随后可以用于 prompt 字段的参考图片的文本说明。例如:
            "prompt": "Luxe Elixir hair oil, golden liquid in glass bottle [1]
             against a pure white background.",
            [...],
            "subjectDescription": "Luxe Elixir hair oil, golden liquid in glass bottle"
          
    • 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_SUBJECT",
              "referenceId": 1,
              "referenceImage": {
                "bytesBase64Encoded": "BASE64_REFERENCE_IMAGE"
              },
              "subjectImageConfig": {
                "subjectType": "SUBJECT_TYPE_PRODUCT",
                "subjectDescription": "SUBJECT_DESCRIPTION"
              }
            },
            {
              "referenceType": "REFERENCE_TYPE_SUBJECT",
              "referenceId": 1,
              "referenceImage": {
                "bytesBase64Encoded": "BASE64_REFERENCE_IMAGE"
              },
              "subjectImageConfig": {
                "subjectType": "SUBJECT_TYPE_PRODUCT",
                "subjectDescription": "SUBJECT_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"
        }
      ]
    }
    

    产品使用

    如需查看与 Imagen on Vertex AI 关联的使用标准和内容限制,请参阅使用指南

    模型版本

    您可以使用多种图片生成模型。如需了解详情,请参阅 Imagen 模型

    后续步骤

    阅读有关 Imagen 和其他 Vertex AI 上的生成式 AI 产品的文章: