本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
Amazon Titan Image Generator G1 V1 以及 Titan Image Generator G1 V2 模型在執行模型推論時支援下列推論參數和模型回應。
推論參數
當您使用 Amazon Titan Image Generator 模型進行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 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。
以下是允許的尺寸。
寬度 Height (高度) 長寬比 價格等於 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 1280 3:5 1024 x 1024 384 640 3:5 512 x 512 1280 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 Titan Image Generator 模型SDK。選取標籤以檢視每個使用案例的範例。每個範例都會在最後顯示影像。
# 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()