本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
Amazon Titan Image Generator G1 V1 以及 Titan Image Generator G1 V2 模型在执行模型推理时支持以下推理参数和模型响应。
推理参数
当您使用 Amazon Titan 图像生成器模型InvokeModel拨打电话时,请将请求body
字段替换为与您的用例匹配的格式。所有任务共享一个 imageGenerationConfig
对象,但每个任务都有一个特定于该任务的参数对象。支持以下用例:
taskType | 任务参数字段 | 任务类型 | 定义 |
---|---|---|---|
TEXT_IMAGE |
textToImageParams |
生成 |
使用文本提示生成图像。 |
TEXT_IMAGE |
textToImageParams |
生成 |
(仅限 Image conditioning-V2)提供额外的输入调节图像以及文本提示,以生成与调节图像的布局和构图一致的图像。 |
INPAINTING |
inPaintingParams |
编辑 |
通过更改掩膜内部以匹配周围背景来修改图像。 |
OUTPAINTING |
outPaintingParams |
编辑 | 通过无缝扩展掩膜定义的区域来修改图像。 |
IMAGE_VARIATION |
imageVariationParams |
编辑 | 通过生成原始图像的变体来修改图像。 |
COLOR_GUIDED_GENERATION (V2 only) |
colorGuidedGenerationParams |
生成 | 提供十六进制颜色代码列表和文本提示,以生成符合调色板的图像。 |
BACKGROUND_REMOVAL (V2 only) |
backgroundRemovalParams |
编辑 | 通过识别多个对象并移除背景来修改图像,输出具有透明背景的图像。 |
编辑任务需要在输入中使用一个 image
字段。该字段由一个字符串组成,用于定义图像中的像素。每个像素由 3 个RGB通道定义,每个通道的范围从 0 到 255(例如,(255 255 0) 表示黄色)。这些通道以 base64 编码。
您使用的图像必须采用JPEG或PNG格式。
如果要进行补绘或扩绘,则还需定义一个掩膜,即用于定义要修改的图像部分的一个或多个区域。可以通过以下两种方式之一定义掩膜:
-
maskPrompt
– 编写一个文本提示来描述图像中要遮盖的部分。 -
maskImage
– 输入一个 base64 编码的字符串,通过将输入图像中的每个像素标记为 (0 0 0) 或 (255 255 255) 来定义掩膜覆盖区域。-
定义为 (0 0 0) 的像素是掩膜内的像素。
-
定义为 (255 255 255) 的像素是掩膜外的像素。
可以使用照片编辑工具来绘制掩膜。然后,您可以将输出JPEG或PNG图像转换为 base64 编码,然后将其输入到该字段中。否则,请改用
maskPrompt
字段允许模型推断掩膜。 -
选择一个选项卡可查看不同图像生成用例的API请求正文和字段说明。
用于生成图像的文本提示必须 <= 512 个字符。从长远来看,分辨率 <= 1,408。 negativeText (可选)-用于定义图像中不包含的内容的文本提示,长度小于 512 个字符。请参阅下表,查看有关分辨率的完整列表。
{
"taskType": "TEXT_IMAGE",
"textToImageParams": {
"text": "string",
"negativeText": "string"
},
"imageGenerationConfig": {
"numberOfImages": int,
"height": int,
"width": int,
"cfgScale": float,
"seed": int
}
}
textToImageParams
字段如下所述。
-
text(必要)– 用于生成图像的文本提示。
-
negativeText(可选)-用于定义图像中不包含的内容的文本提示。
注意
请勿在
negativeText
提示中使用否定词。假如您不想在图像中包含镜像,请在negativeText
提示中输入mirrors
,而不要输入no mirrors
。
共享且可选的 imageGenerationConfig
包含以下字段。如果不包含此对象,将使用默认配置。
-
numberOfImages(可选)-要生成的图像数量。
最小值 最大值 默认 1 5 1 -
cfgScale(可选)-指定生成的图像应在多大程度上遵守提示。使用较低的值可在生成中引入更多的随机性。
最小值 最大值 默认 1.1 10.0 8.0 -
以下参数定义了您想要输出图像的大小。有关按图像大小定价的更多详细信息,请参阅 Amazon Bedrock 定价
。 -
height(可选)– 图像的高度(以像素为单位)。默认值是 1408。
-
width(可选)– 图像的宽度(以像素为单位)。默认值是 1408。
允许使用以下尺寸。
宽度 高度 纵横比 价格相当于 1024 1024 1:1 1024 x 1024 768 768 1:1 512 x 512 512 512 1:1 512 x 512 768 1152 2:3 1024 x 1024 384 576 2:3 512 x 512 1152 768 3:2 1024 x 1024 576 384 3:2 512 x 512 768 1 280 3:5 1024 x 1024 384 640 3:5 512 x 512 1 280 768 5:3 1024 x 1024 640 384 5:3 512 x 512 896 1152 7:9 1024 x 1024 448 576 7:9 512 x 512 1152 896 9:7 1024 x 1024 576 448 9:7 512 x 512 768 1408 6:11 1024 x 1024 384 704 6:11 512 x 512 1408 768 11:6 1024 x 1024 704 384 11:6 512 x 512 640 1408 5:11 1024 x 1024 320 704 5:11 512 x 512 1408 640 11:5 1024 x 1024 704 320 11:5 512 x 512 1152 640 9:5 1024 x 1024 1173 640 16:9 1024 x 1024 -
-
seed(可选)– 用于控制和重现结果。决定初始噪声设置。使用与上一次运行时相同的种子和相同的设置,以使推理可以创建相似的图像。
最小值 最大值 默认 0 2,147,483,646 42
示例
以下示例展示了如何在 Python 中使用按需吞吐量调用 Amazon SDK Titan 图像生成器模型。选择一个选项卡查看每个用例的示例。每个示例最后都显示了图像。
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Shows how to generate an image from a text prompt with the Amazon Titan Image Generator G1 model (on demand).
"""
import base64
import io
import json
import logging
import boto3
from PIL import Image
from botocore.exceptions import ClientError
class ImageError(Exception):
"Custom exception for errors returned by Amazon Titan Image Generator G1"
def __init__(self, message):
self.message = message
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def generate_image(model_id, body):
"""
Generate an image using Amazon Titan Image Generator G1 model on demand.
Args:
model_id (str): The model ID to use.
body (str) : The request body to use.
Returns:
image_bytes (bytes): The image generated by the model.
"""
logger.info(
"Generating image with Amazon Titan Image Generator G1 model %s", model_id)
bedrock = boto3.client(service_name='bedrock-runtime')
accept = "application/json"
content_type = "application/json"
response = bedrock.invoke_model(
body=body, modelId=model_id, accept=accept, contentType=content_type
)
response_body = json.loads(response.get("body").read())
base64_image = response_body.get("images")[0]
base64_bytes = base64_image.encode('ascii')
image_bytes = base64.b64decode(base64_bytes)
finish_reason = response_body.get("error")
if finish_reason is not None:
raise ImageError(f"Image generation error. Error is {finish_reason}")
logger.info(
"Successfully generated image with Amazon Titan Image Generator G1 model %s", model_id)
return image_bytes
def main():
"""
Entrypoint for Amazon Titan Image Generator G1 example.
"""
logging.basicConfig(level=logging.INFO,
format="%(levelname)s: %(message)s")
model_id = 'amazon.titan-image-generator-v1'
prompt = """A photograph of a cup of coffee from the side."""
body = json.dumps({
"taskType": "TEXT_IMAGE",
"textToImageParams": {
"text": prompt
},
"imageGenerationConfig": {
"numberOfImages": 1,
"height": 1024,
"width": 1024,
"cfgScale": 8.0,
"seed": 0
}
})
try:
image_bytes = generate_image(model_id=model_id,
body=body)
image = Image.open(io.BytesIO(image_bytes))
image.show()
except ClientError as err:
message = err.response["Error"]["Message"]
logger.error("A client error occurred: %s", message)
print("A client error occured: " +
format(message))
except ImageError as err:
logger.error(err.message)
print(err.message)
else:
print(
f"Finished generating image with Amazon Titan Image Generator G1 model {model_id}.")
if __name__ == "__main__":
main()