Amazon BedrockにおけるClaudeファミリーの画像認識機能の比較と応用例

835 Views

September 04, 24

スライド概要

Amazon BedrockにおけるClaudeファミリーの画像認識機能の比較と応用例

profile-image

Profile: https://hidekazu-konishi.com/ Blog: https://hidekazu-konishi.com/entry/ Books: https://amzn.to/48zwojS Bookmarks: https://b.hatena.ne.jp/site/hidekazu-konishi.com/?sort=count

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

JAWS-UG朝会 #60(2024-08-22) Amazon BedrockにおけるClaudeファミリーの画像認識機能の比較と応用例 (Hidekazu Konishi) 小西秀和 小西秀和 (Hidekazu Konishi) From NRIネットコム株式会社 ※この発表資料に含まれる意見や見解は、個人的なものであり、 所属する組織や会社の公式な立場や意見を代表するものではありません。

2.

自己紹介 ,テ 立ーブル 人 男 つ が含まれている画 像 自動的に生成された説 明 ◼ 普段の業務 社内向けAWS環境などの構築・運用(情シス所属) 事業部AWSプロジェクトの技術支援 (Hidekazu Konishi) 小西秀和 ◼ AWS関連の業務外活動 小西秀和 (Hidekazu Konishi) ,ホ テキ ワス イトボード ,ホ テキ ワス イトボード 自動的に生成された説 明 自動的に生成された説 明 AWS認定の継続的全取得 AWSに関する書籍・技術ブログ記事の投稿・登壇 ⇒ 2020-2024 Japan AWS All Certifications Engineer 2020-2024 Japan AWS Top Engineer (Services) ,ホ テキ ワス イトボード テキスト テキスト 自動的に生成された説 明 自動的に生成された説 明 自動的に生成された説 明 タイプライターの中のエンジン タイプライターの中のエンジン 中程度の精度で自動的に生成された説 明 中程度の精度で自動的に生成された説 明 ◼ 好きなAWSサービス Amazon Bedrock ⚫ 好きなAIモデル Anthropic Claude(Chat型テキスト生成) Amazon Titan Image Generator G1 v2 (画像生成) 1

3.

本日の内容 ◼ Amazon BedrockとAnthropic Claudeファミリーの概要 ◼ Anthropic ClaudeファミリーのOCR(Optical Character Recognition)比較の試行 ◼ Anthropic Claude 3.5 Sonnetによる画像生成AIのプロンプト最適化・生成画像検証の試行 ◼ まとめ 個人で簡単に試せた一方で、興味深かった内容を発表します! RAGなど無しでAPIの利用だけで試せる内容です。 2

4.

Amazon BedrockとAnthropic Claudeファミリーの概要 3

5.

Amazon Bedrockの概要 ◼ Amazon Bedrockとは ⚫ AWSが提供するフルマネージド型の生成AIサービス ⚫ 複数のAIモデルを同様のAPIやSDKなどから利用可能 プロバイダ 生成物の種類 主なモデル Anthropic テキスト生成 Claude 3(Haiku, Sonnet, Opus), Claude 3.5 Sonnet Meta テキスト生成 Llama 3(8B, 70B), Llama 3.1(8B, 70B, 405B) Amazon テキスト生成 Titan Text G1(Lite, Express, Premier) 画像生成 Titan Image Generator G1, Titan Image Generator G1 v2 Embeddings(テキストや画像などの複雑 なデータを数値ベクトルに変換する技術) Titan Text Embeddings V2, Titan Multimodal Embeddings G1 Mistral AI テキスト生成 Mistral 7B, Mixtral 8x7B, Mistral Large, Mistral Large 2 Stability AI 画像生成 Stable Diffusion XL(SDXL) ※現時点で一番使えるベースモデルが多いのは、Oregon(us-west-2)リージョンで、現時点で35モデル。 ⚫ 生成AIを使用したシステム構築をサポートする様々な機能がある(以下は一部) Bedrockの特徴的機能 概要 Knowledge bases 外部知識ベースからの情報検索を組み合わせるRAGの構築を簡素化する Guardrails 有害コンテンツやハルシネーション(誤情報)をフィルタリングする Agents 手順指示、独自アクション実行、 Knowledge bases使用、Guardrailsの適用などをオーケストレーションして、前処理、 後処理とともにタスクをまとめる Prompt flows プロンプト実行、S3データ入出力、Lambda実行、Knowledge bases・Agentsの使用などをワークフローにできる 4

6.

Amazon BedrockにおけるAnthropic Claudeファミリーの概要と料金 ◼ Anthropic Claudeファミリーの概要と料金 モデル 入力形式と出力形式 性能 オンデマンド価格(現時点) Anthropic Claude 3 Haiku 入力:テキスト、ファイル (.csv, .doc, .docx, .html, .md, .pdf, .txt, .xls, . xlsx, .gif, .jpeg, .png, .webp)(※1) 出力:テキスト生成 高速でコンパクト Input: $0.00025 Output: $0.00125 (※2) Anthropic Claude 3 Sonnet 入力:テキスト、ファイル (.csv, .doc, .docx, .html, .md, .pdf, .txt, .xls, . xlsx, .gif, .jpeg, .png, .webp)(※1) 出力:テキスト生成 Claude 3ファミリーの中では、インテリジェンス と速度のバランスがとれている Input: $0.003 Output: $0.015 (※2) Anthropic Claude 3 Opus 入力:テキスト、ファイル (.csv, .doc, .docx, .html, .md, .pdf, .txt, .xls, . xlsx, .gif, .jpeg, .png, .webp)(※1) 出力:テキスト生成 Claude 3 Sonnetよりも高いインテリジェンス Input: $0.015 Output: $0.075 (※2) Anthropic Claude 3.5 Sonnet 入力:テキスト、ファイル (.gif, .jpeg, .png, .webp)(※1) 出力:テキスト生成 Claude 3 Opusよりも高いインテリジェンス Claude 3 Opusの約2倍の速度 Input: $0.003 Output: $0.015 (※2) ※1) Claude 3.5 Sonnetがサポートする入力ファイル形式は画像のみ。 ※2)Input, OutputともにOregon(us-west-2)リージョンにおける1,000トークンあたりの価格。 <参考> https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html https://docs.aws.amazon.com/bedrock/latest/userguide/conversation-inference.html https://aws.amazon.com/bedrock/pricing/ https://docs.anthropic.com/ja/docs/models-overview# https://www.anthropic.com/news/claude-3-5-sonnet 5

7.

Anthropic ClaudeファミリーのOCR(Optical Character Recognition)比較の試行 6

8.
[beta]
ClaudeファミリーのOCR比較:[概要]試行パターン
◼ OCR(Optical Character Recognition)とは、画像や印刷されたテキストをデジタルテキストに変換する技術
◼ OCRを実行する機能:Anthropic Claude 3(Haiku, Sonnet, Opus), Claude 3.5 Sonnet, Amazon Textract
⚫ ドキュメントデータ検出・分析に特化したサービスであるAmazon Textractも比較対象とする
⚫ 機能の実行はOregon(us-west-2)リージョンで実施(Claude 3 Opusがここでしか提供されていないため)

◼ OCRの実行方法:Anthropic Claudeファミリーは英語プロンプト、日本語プロンプトをそれぞれ試す
⚫ 英語プロンプト:Please extract all text contained in this image. However, do not include your own commentary
in the output; present the results in the following format:¥nimage_ocr: <result>
⚫ 日本語プロンプト:この画像に含まれるすべてのテキストを抽出してください。ただし、出力にあなたのコメントを含めないでください。
結果を以下の形式で提示してください:¥nimage_ocr: <result>

◼ OCRの対象画像の種類:自身の個人ブログの記事一覧ページをWindowsでスクリーンショットしたものを使用
⚫ 英語標準、英語手書き風、日本語標準、日本語手書き風のフォント表示を試す
• 英語標準:Arial(Windows標準)
• 英語手書き風:Caveat(Googleフォント, https://fonts.google.com/specimen/Caveat)
• 日本語標準:Yu Gothic(Windows標準)
• 日本語手書き風:Yomogi(Googleフォント, https://fonts.google.com/specimen/Yomogi)

7

9.

ClaudeファミリーのOCR比較:[概要]アーキテクチャ ◼ 実装したコードは画像タイトル付け、画像コメント付けも対応しているが、今回は画像OCRのみを実行。 ◼ Amazon Textractの試行では、画像OCR処理をTextractで行うAWS Lambda関数を別に用意する。 <実装の詳細> ・ Using Amazon Bedrock for titling, commenting, and OCR (Optical Character Recognition) with Claude 3.5 Sonnet ・ Using Amazon Textract for OCR(Optical Character Recognition) 8

10.

ClaudeファミリーのOCR比較: [概要]各試行パターンで使用する画像(英語標準) 英語 標準 9

11.

ClaudeファミリーのOCR比較: [概要]各試行パターンで使用する画像(英語手書き風) 英語 手書き風 10

12.

ClaudeファミリーのOCR比較: [概要]各試行パターンで使用する画像(日本語標準) 日本語 標準 11

13.

ClaudeファミリーのOCR比較: [概要]各試行パターンで使用する画像(日本語手書き風) 日本語 手書き風 12

14.

ClaudeファミリーのOCR比較:[概要] 検証方法 ◼ [(Claudeモデル+Textact)✕(プロンプト言語)✕(画像の種類)]の組み合わせに対して認識精度を出す ⚫ Amazon Textractについてはプロンプト言語は無いので、1パターンとして数える ◼ Pythonの SequenceMatcher(None, a, b).ratio() で文字単位の類似度を算出する ⚫ Ratcliff/Obershelpアルゴリズム:順序・スペース・句読点・大小文字を区別し、文脈や意味を考慮しない ⇒人間の見た目よりも類似度が低く感じられることもある。しかし、今回は機械的にこのメソッドで算出。 ⚫ 改行は比較対象外とする ⚫ Unicodeで検出した場合はデコードし、元の文字と内容が合っていれば正解とする <実装の詳細> ・ Evaluating OCR Accuracy of Claude on Amazon Bedrock and Amazon Textract Using Similarity Metrics 13

15.

ClaudeファミリーのOCR比較:[結果]まとめ ※小数点第3位を四捨五入 ※類似度単位:パーセント(%) モデル名 言語とフォント 文字単位類似度(英語プロンプトによる実行) 文字単位類似度(日本語プロンプトによる実行) Claude 3 Haiku 英語:標準 97.20 96.89 Claude 3 Haiku 英語:手書き風 22.52 8.39 Claude 3 Haiku 日本語:標準 28.08 15.43 Claude 3 Haiku 日本語:手書き風 24.84 24.85 Claude 3 Sonnet 英語:標準 22.51 34.15 Claude 3 Sonnet 英語:手書き風 1.24 10.77 Claude 3 Sonnet 日本語:標準 25.64 27.32 Claude 3 Sonnet 日本語:手書き風 5.08 13.48 Claude 3 Opus 英語:標準 99.86 Claude 3 Opus 英語:手書き風 27.85 Claude 3 Opus 日本語:標準 16.71 Claude 3 Opus 日本語:手書き風 Claude 3.5 Sonnet 英語:標準 99.93 99.67 Claude 3.5 Sonnet 英語:手書き風 52.83 44.92 Claude 3.5 Sonnet 日本語:標準 32.13 32.30 Claude 3.5 Sonnet 日本語:手書き風 31.09 31.16 Amazon Textract 英語:標準 94.51 - Amazon Textract 英語:手書き風 99.04 Amazon Textract 日本語:標準 51.51 これら結果はあくまで個人で試行した - 結果です。 - Amazon Textract 日本語:手書き風 45.56 - Claude 3 Opusはキャパシティエラーが 多発するため、今回はデータを全て採 取するのは断念しました。 14

16.

ClaudeファミリーのVision(画像認識機能)を使用した応用例: 私が最近試してみたもの ◼ Claude 3.5 SonnetのVision機能(画像認識機能:画像の理解・分析)はOCRだけではない ⇒画像に何が描かれているかや何を表現しているかも認識する ◼ (今回は割愛)画像ベースの確認や承認フローの支援・自動化 ⚫ 確認者、承認者が少ない場面でレビュー品質を高めてくれる。一方で、最終的な確認と承認は人間が責任をもってやるべき。 ⚫ 承認処理については様々な観点や利用方法があり、賛否両論が予想されるので、AI介入部分はアウトプットしていない。 <承認フロー部分の実装> ・ How to Add an Approval Flow to AWS Step Functions Workflow (AWS Systems Manager Automation Edition) ・ How to Add an Approval Flow to AWS Step Functions Workflow (AWS Systems Manager Automation and Amazon EventBridge Edition) ◼ (今回はこちらを紹介)画像生成AIのプロンプト最適化・生成画像検証 ⚫ Claudeは画像生成はできないが、テキスト生成機能と画像認識機能で画像生成AIを制御できそう ⚫ 画像生成AIで思い通りの画像が生成されない、意図に合う画像かの目視確認が大変という問題を解決する ⚫ 画像生成AIモデルのベストプラクティスに合わせてClaude 3.5 Sonnetでプロンプトを最適化(テキスト生成機能) ⚫ 生成された画像をClaude 3.5 Sonnetで元のプロンプトの条件を満たしているかを検証(画像認識機能) ⚫ 画像生成モデルはStability AI Stable Diffusion XL(SDXL)とAmazon Titan Image Generator G1で検証 <内容の詳細> ・ Using Amazon Bedrock to repeatedly generate images with Stable Diffusion XL via Claude 3.5 Sonnet until requirements are met ・ Using Claude 3.5 Sonnet Vision Capabilities on Amazon Bedrock to Verify, Regenerate, and Automate Image Generation with Amazon Titan Image Generator G1 15

17.

Anthropic Claude 3.5 Sonnetによる画像生成AIのプロンプト最適化・生成画像検証の試行 16

18.

Claude 3.5 Sonnetによる画像生成AIのプロンプト最適化・生成画像検証 アーキテクチャ ◼ 初期プロンプト、画像生成回数、プロンプト修正回数が含まれるイベントパラ メータでLambda関数を実行。 ◼ 手順2-1、2-2、2-3を実行し、Claudeによって生成画像が初期プロンプトの要 件を満たしていると判断されれば処理完了。 要件を満たしていないと判断された場合は手順2-1から2-3を再度実行。 要件を満たさない場合、画像生成回数に達するまで、繰り返す。 ◼ 画像生成回数を実行回数が超えた場合、Claudeでプロンプトを改善する。 生成画像検証がパスされるか、プロンプト修正回数まで繰り返す。 ※「3. プロンプト改善」をするプロンプトはあらかじめ、Claude 3.5 Sonnetで画像生成AIのド キュメントから指示内容を生成しておく。 17

19.
[beta]
画像生成AIのプロンプトを最適化するClaude 3.5 Sonnetのプロンプト作成例
◼ 例としてAmazon Titan Image Generator G1の場合を挙げる
◼ 作成手順
1. Amazon Titan Image Generator G1のプロンプト仕様書からClaude 3.5 Sonnetでプロンプトを作成する
参考:Amazon Titan Image Generator Prompt Engineering Best Practices
2. 作成した「画像生成AIのプロンプトを最適化するプロンプト」をプロンプト修正するプログラム内で使用する

◼ 実際に作成した「画像生成AIのプロンプトを最適化するClaude 3.5 Sonnetのプロンプト」
Revise the following image generation prompt to optimize it for Titan Image Generator G1, incorporating best practices:
{original_prompt}
Please consider the following guidelines in your revision:
1. Start the prompt with "An image of..." and be specific and descriptive, using vivid adjectives and clear nouns.
2. Include detailed descriptions about composition, lighting, style, mood, color, and medium if relevant.
3. Mention specific artists or art styles if relevant, though this is not emphasized in Titan's guidelines.
4. Use descriptive keywords like "highly detailed" if appropriate. While "4k", "8k", or "photorealistic" can be used, they are not specifically emphasized
for Titan.
5. Separate different concepts with commas, using them to structure the prompt logically.
6. Place more important elements, especially the main subject, at the beginning of the prompt.
7. Consider using negative prompts to specify what should NOT be included in the image.
8. If the original prompt is not in English, translate it to English.
9. Use double quotes instead of single quotes for any quoted text within the prompt.
10. Provide context or background details to help improve the realism and coherence of the generated image.
11. Ensure the final prompt is no longer than 500 characters. Prioritize the most important elements if you need to shorten the prompt.
Your goal is to create a clear, detailed prompt that will result in a high-quality image generation with Titan Image Generator G1, while staying within
the 500-character limit.
Please provide your response in the following JSON format:
{{"revised_prompt":""}}
Ensure your response can be parsed as valid JSON. Do not include any explanations, comments, or additional text outside of the JSON structure.

18

20.

Claude 3.5 Sonnetによる画像生成AIのプロンプト最適化・生成画像検証 Stable Diffusion XLの場合:プロンプトと出力画像の変化 ①初期プロンプト 自然の中から見た夜景で、空にはオーロラと月と流星群があり、地上に は海が広がって流氷が流れ、地平線から太陽が出ている無人の写真。 ②修正プロンプト(1回目) Breathtaking night landscape, vibrant aurora borealis (aurora:1.2) dancing across the starry sky, crescent moon illuminating the scene, meteor shower streaking through the atmosphere, vast arctic ocean with floating ice floes, midnight sun peeking over the horizon, ethereal glow, long exposure photography, ultra high resolution 8k, photorealistic, highly detailed, dramatic lighting, panoramic composition, inspired by Aurora Borealis paintings of Frederic Edwin Church ③修正プロンプト(2回目) Breathtaking Arctic night landscape, vibrant green aurora borealis dancing across the starry sky, full moon illuminating the scene, meteor shower streaking through the atmosphere, vast icy ocean with floating ice floes, midnight sun peeking over the horizon, ultra-high resolution 8K, photorealistic, highly detailed, dramatic lighting, panoramic composition, inspired by Albert Bierstadt and Frederic Edwin Church, (aurora borealis:1.3), (meteor shower:1.2), serene and majestic atmosphere ④修正プロンプト(3回目) Breathtaking nightscape from nature's perspective, vibrant aurora borealis (aurora:1.2) dancing across the starry sky, crescent moon illuminating the scene, meteor shower streaking through the atmosphere, vast ocean with floating ice floes, sun peeking over the horizon creating a golden glow, no human presence, photorealistic, highly detailed, 8k resolution, dramatic lighting, wide-angle composition, inspired by Thomas Kinkade and Aurora HDR style, serene and awe-inspiring mood ① ② ③ ④ ※画像生成回数は5を設定(横の画像数。) ※プロンプト修正回数は5を設定(縦の行数-1。3回で完了。) ※赤枠が最終的に検証をパスした画像 19

21.

Claude 3.5 Sonnetによる画像生成AIのプロンプト最適化・生成画像検証 Stable Diffusion XLの場合:最終的にClaude 3.5 Sonnetの検証をパスした画像 ◼ プロンプトの要件をほぼ満たした画像が検証をパス 「自然の中から見た夜景で、空にはオーロラと月と流 星群があり、地上には海が広がって流氷が流れ、地 平線から太陽が出ている無人の写真。」 という要件を最終的な画像はほぼ満たしている。 ※流星群と流氷の具現化は弱いが、月と地平線の 太陽という矛盾した情景はしっかりと表現されている。 検証パスする前の画像よりも、指示された要件をより 多く満たしている。 ⇒ Claude 3.5 Sonnetが適切に修正したプロンプト で、要件を満たす画像が生成された ⇒Claude 3.5 Sonnetの画像認識機能で、画像の 構図が検証できた 20

22.

Claude 3.5 Sonnetによる画像生成AIのプロンプト最適化・生成画像検証 Titan Image Generator G1の場合:プロンプトと出力画像の変化 ①初期プロンプト 自然の中から見た夜景で、空にはオーロラと月と流星群があり、地上に は海が広がって流氷が流れ、地平線から太陽が出ている無人の写真。 ②修正プロンプト(1回目) An image of a breathtaking nighttime landscape viewed from nature, featuring a vibrant aurora borealis dancing across the sky, accompanied by a luminous full moon and a dazzling meteor shower. In the foreground, a vast, dark sea stretches to the horizon, dotted with floating ice floes. The sun is just beginning to rise at the horizon, casting a warm glow on the icy waters. The scene is devoid of human presence, highly detailed, with a serene and mystical atmosphere. ① ② ※画像生成回数は5を設定(横の画像数。) ※プロンプト修正回数は5を設定(縦の行数-1。1回で完了。) ※赤枠が最終的に検証をパスした画像 21

23.

Claude 3.5 Sonnetによる画像生成AIのプロンプト最適化・生成画像検証 Titan Image Generator G1の場合:最終的にClaude 3.5 Sonnetの検証をパスした画像 ◼ プロンプトの要件を満たした画像が検証をパス 「自然の中から見た夜景で、空にはオーロラと月と流 星群があり、地上には海が広がって流氷が流れ、地 平線から太陽が出ている無人の写真。」 という要件を最終的な画像はすべて満たしている。 ※地平線の太陽という構図の具現化は弱いですが、 月と地平線の太陽という矛盾した情景や流星群と流 氷はしっかりと表現されている。 検証パスする前の画像よりも、指示された要件をより 多く満たしている。 ⇒ Claude 3.5 Sonnetが適切に修正したプロンプト で、要件を満たす画像が生成された ⇒Claude 3.5 Sonnetの画像認識機能で、画像の 構図が検証できた 22

24.

まとめ ◼ Amazon BedrockにおけるAnthropic ClaudeのOCR精度がどの程度か確認できた ◼ Anthropic Claudeと他のAIモデルを組み合わせる応用で、自分のアイデアを簡単に実現できた ◼ 何よりも実験したプログラムコードの殆どはClaude 3.5 Sonnetで作成しました 今まで実現できなかった実験やアイデアがAmazon Bedrockで実現できるかも!? まだ、使ったことがない方はぜひお試しください! 23