风格自定义

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

使用场景

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

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

预期使用情形

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

  • 根据文字输入生成图片,该图片采用参考图片提供的特定风格。
  • 更改人物照片。
  • 更改人物照片并保留其面部表情。

非预期用例示例

以下列出了 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 图片生成功能根据以下提示生成的参考输入图片:a simple mosaic

查看 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 自定义结合使用的提示可能会影响生成的图片的质量。以下部分介绍了建议的提示模板和示例,可用于发送自定义请求。

    使用场景 参考图片 提示模板 示例
    对象样式 主题图片 (1-4) Generate an image in STYLE_DESCRIPTION [1] based on the following caption: IMAGE_DESCRIPTION. Generate an image in neon sign style [1] based on the following caption: a sign saying have a great day.
    不使用人脸网格输入进行人物图片风格化 主题图片 (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-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、杰作、超细节、皮肤纹理、纹理映射、柔和的阴影、柔和逼真的光线、鲜艳的色彩

    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" Generate an image in glowing style [1] based on the following caption: A church in the mountain.
    • "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)

    产品使用

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

    模型版本

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

    后续步骤

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