您可以使用 Imagen on Vertex AI 指示模型如何将图片中的对象自定义和转换为您在文本提示中指定的样式。
查看 Imagen for Editing and Customization 模型卡片
自定义提示撰写
与 Imagen 3 自定义结合使用的提示可能会影响生成的图片的质量。您可以从以下提示模板入手,撰写自定义提示。您可能需要发送多个请求才能获得所需的输出。
使用场景 | 参考图片 | 提示模板 | 示例 |
---|---|---|---|
指示自定义 - 风格转移 | 图片 (1) | Transform the subject in image [1] to have a style of ${STYLE_DESCRIPTION}. The image depicts ${IMAGE_DESCRIPTION}. | Transform the subject in image [1] to have a style of a watercolor painting of the image with loose watercolor techniques, soft tone, pastel colors, brush strokes, delicate, clean background, spontaneity, analog style drawing, intricate highly detailed painting. The image depicts a portrait of a proud woman. |
使用“指示自定义”进行自定义以进行风格转移
使用以下代码示例,根据文本提示中所述的风格指定输出图片的风格。
REST
在使用任何请求数据之前,请先进行以下替换:
- PROJECT_ID:您的 Google Cloud 项目 ID。
- LOCATION:您的项目的区域。 例如
us-central1
、europe-west2
或asia-northeast3
。如需查看可用区域的列表,请参阅 Vertex AI 上的生成式 AI 位置。 - TEXT_PROMPT:文本提示可用于指导模型生成的图片。如需使用 Imagen 3 自定义,请以 [$referenceId] 格式添加您提供的参考图片的
referenceId
。例如:- 将图片 [1] 中的主体转换为数字彩绘玻璃图片风格。
- 为图片 [1] 中的猫咪添加一顶红色牛仔帽。
- Remove the corgi dog in the image [1].
- Change the red ball in the image [1] to a blue box.
"referenceId"
:参考图片的 ID,或与同一主体或风格对应的一系列参考图片的 ID。- BASE64_REFERENCE_IMAGE:用于引导图片生成的参考图片。图片必须指定为 base64 编码的字节字符串。
- 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_RAW", "referenceId": 1, "referenceImage": { "bytesBase64Encoded": "BASE64_REFERENCE_IMAGE" } } ] } ], "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 产品的文章:
- Imagen 3 on Vertex AI 开发者入门指南
- 与创作者一起为创作者打造的全新生成式媒体模型和工具
- Gemini 中的新功能:自定义 Gem 以及通过 Imagen 3 改进的图片生成
- Google DeepMind:Imagen 3 - 我们质量最高的文本转图片模型