Amazon Titan Image Generator G1 - Amazon Bedrock

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Amazon Titan Image Generator G1

Titan Image Generator G1Model Amazon mendukung parameter inferensi dan respons model berikut saat melakukan inferensi model.

Format permintaan dan respons

Saat Anda melakukan InvokeModelpanggilan menggunakan AmazonTitan Image Generator G1, ganti body bidang permintaan dengan format yang cocok dengan kasus penggunaan Anda. Semua tugas berbagi imageGenerationConfig objek, tetapi setiap tugas memiliki objek parameter khusus untuk tugas itu. Kasus penggunaan berikut didukung.

taskType Bidang parameter tugas Jenis tugas Definisi
TEXT_IMAGE textToImageParams Generasi

Hasilkan gambar menggunakan prompt teks.

INPAINTING inPaintingParams Penyuntingan

Ubah gambar dengan mengubah bagian dalam topeng agar sesuai dengan latar belakang sekitarnya.

OUTPAINTING outPaintingParams Penyuntingan Ubah gambar dengan memperluas wilayah yang ditentukan oleh topeng secara mulus.
IMAGE_VARIATION imageVariationParams Penyuntingan Memodifikasi gambar dengan menghasilkan variasi gambar asli.

Tugas pengeditan membutuhkan image bidang di input. Bidang ini terdiri dari string yang mendefinisikan piksel dalam gambar. Setiap piksel ditentukan oleh 3 saluran RGB, yang masing-masing berkisar dari 0 hingga 255 (misalnya, (255 255 0) akan mewakili warna kuning). Saluran ini dikodekan dalam base64.

Gambar yang Anda gunakan harus dalam format JPEG atau PNG.

Jika Anda melakukan inpainting atau outpainting, Anda juga menentukan mask, wilayah atau wilayah yang menentukan bagian gambar yang akan dimodifikasi. Anda dapat mendefinisikan topeng dengan salah satu dari dua cara.

  • maskPrompt— Tulis prompt teks untuk menggambarkan bagian gambar yang akan disamarkan.

  • maskImage— Masukkan string yang disandikan base64 yang mendefinisikan daerah bertopeng dengan menandai setiap piksel dalam gambar input sebagai (0 0 0) atau (255 255 255).

    • Piksel yang didefinisikan sebagai (0 0 0) adalah piksel di dalam topeng.

    • Piksel yang didefinisikan sebagai (255 255 255) adalah piksel di luar topeng.

    Anda dapat menggunakan alat pengeditan foto untuk menggambar topeng. Anda kemudian dapat mengonversi output gambar JPEG atau PNG menjadi pengkodean base64 untuk dimasukkan ke dalam bidang ini. Jika tidak, gunakan maskPrompt bidang sebagai gantinya untuk memungkinkan model menyimpulkan topeng.

Pilih tab untuk melihat badan permintaan API untuk kasus penggunaan pembuatan gambar yang berbeda dan penjelasan bidang.

Text-to-image generation (Request)

Prompt teks untuk menghasilkan gambar harus <= 512 karakter. Resolusi <= 1.408 di sisi yang lebih panjang. I NegativeText (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar -- <= 512 karakter. Lihat tabel di bawah ini untuk daftar lengkap resolusi.

{ "taskType": "TEXT_IMAGE", "textToImageParams": { "text": "string", "negativeText": "string" }, "imageGenerationConfig": { "numberOfImages": int, "height": int, "width": int, "cfgScale": float, "seed": int } }

textToImageParamsBidang dijelaskan di bawah ini.

  • text (Wajib) — Sebuah prompt teks untuk menghasilkan gambar.

  • NegativeText (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar.

    catatan

    Jangan gunakan kata-kata negatif dalam negativeText prompt. Misalnya, jika Anda tidak ingin menyertakan cermin dalam gambar, masukkan mirrors negativeText prompt. Jangan masukno mirrors.

Inpainting (Request)

text (Opsional) — Sebuah prompt teks untuk menentukan apa yang harus diubah di dalam topeng. Jika Anda tidak menyertakan bidang ini, model mencoba mengganti seluruh area topeng dengan latar belakang. Harus <= 512 karakter. negativeText (Opsional) - Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar. Harus <= 512 karakter. Batas ukuran untuk gambar input dan masukan mask adalah <= 1.408 pada sisi gambar yang lebih panjang. Ukuran output sama dengan ukuran input.

{ "taskType": "INPAINTING", "inPaintingParams": { "image": "base64-encoded string", "text": "string", "negativeText": "string", "maskPrompt": "string", "maskImage": "base64-encoded string", }, "imageGenerationConfig": { "numberOfImages": int, "height": int, "width": int, "cfgScale": float } }

inPaintingParamsBidang dijelaskan di bawah ini. Topeng mendefinisikan bagian dari gambar yang ingin Anda modifikasi.

Outpainting (Request)

text (Required) — Sebuah prompt teks untuk menentukan apa yang harus diubah di luar topeng. Harus <= 512 karakter. negativeText (Opsional) - Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar. Harus <= 512 karakter. Batas ukuran untuk gambar input dan masukan mask adalah <= 1.408 pada sisi gambar yang lebih panjang. Ukuran output sama dengan ukuran input.

{ "taskType": "OUTPAINTING", "outPaintingParams": { "text": "string", "negativeText": "string", "image": "base64-encoded string", "maskPrompt": "string", "maskImage": "base64-encoded string", "outPaintingMode": "DEFAULT | PRECISE" }, "imageGenerationConfig": { "numberOfImages": int, "height": int, "width": int, "cfgScale": float } }

outPaintingParamsBidang didefinisikan di bawah ini. Topeng mendefinisikan wilayah dalam gambar yang tidak ingin Anda modifikasi. Generasi dengan mulus memperluas wilayah yang Anda tentukan.

  • image (Required) - Gambar JPEG atau PNG untuk dimodifikasi, diformat sebagai string yang menentukan urutan piksel, masing-masing didefinisikan dalam nilai RGB dan dikodekan dalam base64. Untuk contoh cara menyandikan gambar menjadi base64 dan memecahkan kode string yang dikodekan base64 dan mengubahnya menjadi gambar, lihat contoh kode.

  • Anda harus menentukan salah satu bidang berikut (tetapi tidak keduanya) untuk menentukan.

  • text (Required) — Sebuah prompt teks untuk menentukan apa yang harus diubah di luar topeng.

  • NegativeText (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar.

    catatan

    Jangan gunakan kata-kata negatif dalam negativeText prompt. Misalnya, jika Anda tidak ingin menyertakan cermin dalam gambar, masukkan mirrors negativeText prompt. Jangan masukno mirrors.

  • out PaintingMode - Menentukan apakah untuk memungkinkan modifikasi piksel di dalam topeng atau tidak. Nilai-nilai berikut dimungkinkan.

    • DEFAULT - Gunakan opsi ini untuk memungkinkan modifikasi gambar di dalam topeng agar tetap konsisten dengan latar belakang yang direkonstruksi.

    • PRECISE — Gunakan opsi ini untuk mencegah modifikasi gambar di dalam topeng.

Image variation (Request)

Variasi gambar memungkinkan Anda untuk membuat variasi gambar asli Anda berdasarkan nilai parameter. Batas ukuran untuk gambar input adalah <= 1.408 pada sisi gambar yang lebih panjang. Lihat tabel di bawah ini untuk daftar lengkap resolusi.

  • teks (Opsional) — Sebuah prompt teks yang dapat menentukan apa yang harus dipertahankan dan apa yang harus diubah dalam gambar. Harus <= 512 karakter.

  • NegativeText (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar. Harus <= 512 karakter.

  • teks (Opsional) — Sebuah prompt teks yang dapat menentukan apa yang harus dipertahankan dan apa yang harus diubah dalam gambar. Harus <= 512 karakter.

  • SimilarityStrength (Opsional) - Menentukan seberapa mirip gambar yang dihasilkan seharusnya dengan gambar input Gunakan nilai yang lebih rendah untuk memperkenalkan lebih banyak keacakan dalam pembuatan. Rentang yang diterima adalah antara 0,2 dan 1,0 (keduanya inklusif), sedangkan default 0,7 digunakan jika parameter ini tidak ada dalam permintaan.

{ "taskType": "IMAGE_VARIATION", "imageVariationParams": { "text": "string", "negativeText": "string", "images": ["base64-encoded string"], "similarityStrength": 0.7, # Range: 0.2 to 1.0 }, "imageGenerationConfig": { "numberOfImages": int, "height": int, "width": int, "cfgScale": float } }

imageVariationParamsBidang didefinisikan di bawah ini.

  • gambar (Wajib) - Daftar gambar untuk menghasilkan variasi. Anda dapat menyertakan 1 hingga 5 gambar. Gambar didefinisikan sebagai string gambar yang dikodekan base64. Untuk contoh cara menyandikan gambar menjadi base64 dan memecahkan kode string yang dikodekan base64 dan mengubahnya menjadi gambar, lihat contoh kode.

  • teks (Opsional) — Sebuah prompt teks yang dapat menentukan apa yang harus dipertahankan dan apa yang harus diubah dalam gambar.

  • SimilarityStrength (Opsional) - Menentukan seberapa mirip gambar yang dihasilkan harus dengan gambar masukan (s). Rentang 0,2 hingga 1,0 dengan nilai yang lebih rendah digunakan untuk memperkenalkan lebih banyak keacakan.

  • NegativeText (Opsional) — Sebuah prompt teks untuk menentukan apa yang tidak termasuk dalam gambar.

    catatan

    Jangan gunakan kata-kata negatif dalam negativeText prompt. Misalnya, jika Anda tidak ingin menyertakan cermin dalam gambar, masukkan mirrors negativeText prompt. Jangan masukno mirrors.

Response body
{ "images": [ "base64-encoded string", ... ], "error": "string" }

Badan respons adalah objek streaming yang berisi salah satu bidang berikut.

Yang dibagikan dan opsional imageGenerationConfig berisi bidang berikut. Jika Anda tidak menyertakan objek ini, konfigurasi default akan digunakan.

  • nomor OfImages (Opsional) — Jumlah gambar yang akan dihasilkan.

    Minimum Maksimum Default
    1 5 1
  • CFGScale (Opsional) - Menentukan seberapa kuat gambar yang dihasilkan harus mematuhi prompt. Gunakan nilai yang lebih rendah untuk memperkenalkan lebih banyak keacakan dalam generasi.

    Minimum Maksimum Default
    1.1 10.0 8.0
  • Parameter berikut menentukan ukuran gambar keluaran yang Anda inginkan. Untuk detail selengkapnya tentang harga berdasarkan ukuran gambar, lihat harga Amazon Bedrock.

    • tinggi (Opsional) - Ketinggian gambar dalam piksel. Nilai defaultnya adalah 1408.

    • lebar (Opsional) - Lebar gambar dalam piksel. Nilai defaultnya adalah 1408.

    Ukuran berikut diperbolehkan.

    Lebar Tinggi Rasio aspek Harga setara dengan
    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 (Opsional) — Gunakan untuk mengontrol dan mereproduksi hasil. Menentukan pengaturan kebisingan awal. Gunakan seed yang sama dan pengaturan yang sama seperti proses sebelumnya untuk memungkinkan inferensi membuat gambar serupa.

    Minimum Maksimum Default
    0 2.147,483,646 0

Contoh kode

Contoh berikut menunjukkan cara memanggil Titan Image Generator G1 model Amazon dengan throughput sesuai permintaan di Python SDK. Pilih tab untuk melihat contoh untuk setiap kasus penggunaan. Setiap contoh menampilkan gambar di akhir.

Text-to-image generation
# 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()
Inpainting
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to use inpainting to generate an image from a source image with the Amazon Titan Image Generator G1 model (on demand). The example uses a mask prompt to specify the area to inpaint. """ 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. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'amazon.titan-image-generator-v1' # Read image from file and encode it as base64 string. with open("/path/to/image", "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') body = json.dumps({ "taskType": "INPAINTING", "inPaintingParams": { "text": "Modernize the windows of the house", "negativeText": "bad quality, low res", "image": input_image, "maskPrompt": "windows" }, "imageGenerationConfig": { "numberOfImages": 1, "height": 512, "width": 512, "cfgScale": 8.0 } }) 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()
Outpainting
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to use outpainting to generate an image from a source image with the Amazon Titan Image Generator G1 model (on demand). The example uses a mask image to outpaint the original image. """ 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. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'amazon.titan-image-generator-v1' # Read image and mask image from file and encode as base64 strings. with open("/path/to/image", "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') with open("/path/to/mask_image", "rb") as mask_image_file: input_mask_image = base64.b64encode( mask_image_file.read()).decode('utf8') body = json.dumps({ "taskType": "OUTPAINTING", "outPaintingParams": { "text": "Draw a chocolate chip cookie", "negativeText": "bad quality, low res", "image": input_image, "maskImage": input_mask_image, "outPaintingMode": "DEFAULT" }, "imageGenerationConfig": { "numberOfImages": 1, "height": 512, "width": 512, "cfgScale": 8.0 } } ) 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()
Image variation
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Shows how to generate an image variation from a source image 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. """ try: logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") model_id = 'amazon.titan-image-generator-v1' # Read image from file and encode it as base64 string. with open("/path/to/image", "rb") as image_file: input_image = base64.b64encode(image_file.read()).decode('utf8') body = json.dumps({ "taskType": "IMAGE_VARIATION", "imageVariationParams": { "text": "Modernize the house, photo-realistic, 8k, hdr", "negativeText": "bad quality, low resolution, cartoon", "images": [input_image], "similarityStrength": 0.7, # Range: 0.2 to 1.0 }, "imageGenerationConfig": { "numberOfImages": 1, "height": 512, "width": 512, "cfgScale": 8.0 } }) 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()