画像プロンプトの反復的な改善
生成画像を分析し、体系的にプロンプトを調整します。
「画像プロンプトの反復的な改善」はCoddyKit上の無料AI Prompt Engineeringレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAI Prompt Engineering学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 AI Prompt Engineeringコースには全4レッスンが含まれています。
なぜ反復的な改善を行うのか
最初に作成した画像プロンプトで、望みどおりの結果が得られることはほとんどありません。反復的な改善とは、生成、分析、問題の特定、プロンプトの調整、再生成を体系的に行うワークフローです。各サイクルによって、意図と出力の差を縮めていきます。
改善ループ
反復的な改善ループは4つのステップで構成されます。生成(現在のプロンプトから画像を作成する)、分析(間違っている点や不足している点を特定する)、調整(問題に対処するためプロンプトを変更する)、そして再生成です。満足できる結果になるか、予算や時間の制約で終了するまで繰り返します。
class PromptRefinementSession:
def __init__(self, initial_prompt, negative_prompt=''):
self.history = []
self.current_prompt = initial_prompt
self.current_negative = negative_prompt
self.iteration = 0
def record_iteration(self, issues_found, adjustments_made):
self.history.append({
'iteration': self.iteration,
'prompt': self.current_prompt,
'negative': self.current_negative,
'issues': issues_found,
'adjustments': adjustments_made
})
self.iteration += 1
def update_prompt(self, new_prompt, new_negative=None):
self.current_prompt = new_prompt
if new_negative is not None:
self.current_negative = new_negative
def get_history(self):
return self.history
# Usage
session = PromptRefinementSession(
initial_prompt='a woman walking in a rainy city at night',
negative_prompt='blurry, low quality'
)
print('Refinement session started. Iteration 0.')生成画像の分析:問題の分類
何がうまくいかなかったのかを分析するときは、問題を種類ごとに分類します。これにより、プロンプトのどの部分を調整すべきか、詳細を追加するのか、矛盾を取り除くのか、重みを調整するのかが明確になります。
ISSUE_TAXONOMY = {
'Subject issues': [
'Subject missing or wrong species/gender/age',
'Key detail absent (clothing, expression, props)',
'Pose or action incorrect',
'Background wrong or distracting'
],
'Style issues': [
'Wrong art style (photo when painting expected)',
'Too stylized/not stylized enough',
'Style inconsistency (mixing styles incoherently)'
],
'Lighting issues': [
'Wrong time of day',
'Too dark or too bright',
'Shadows wrong direction',
'Missing dramatic effect'
],
'Composition issues': [
'Wrong framing (too close/far)',
'Subject cropped awkwardly',
'Rule of thirds not applied',
'Cluttered vs. desired clean composition'
],
'Quality issues': [
'Blurry or low detail',
'Anatomical distortion (extra fingers)',
'Watermark or text artifact',
'Overexposed/underexposed areas'
]
}
for category, issues in ISSUE_TAXONOMY.items():
print(f'{category}: {issues[0]}')反復1:改善前と改善後
問題を特定してプロンプトを調整することで、結果がどのように改善されるかを示す、具体的な改善前後の例を見てみましょう。
# ITERATION 0: Initial prompt (too vague)
prompt_v0 = 'a woman walking in a rainy city at night'
# Issues found:
# - Style unspecified -> model defaulted to generic illustration
# - Lighting unspecified -> flat even light, no mood
# - No detail on clothing or setting
# - No composition direction
# ITERATION 1: Add style, lighting, detail
prompt_v1 = (
'a young woman in a yellow raincoat walking down '
'a rain-soaked Tokyo street at night, '
'neon signs reflected in puddles, steam rising from grates, '
'cinematic photography style, street photography, '
'warm neon glow, wet pavement reflections, '
'medium shot, slightly low angle, bokeh background'
)
negative_v1 = 'blurry, low quality, watermark, extra fingers, cartoon'
# Remaining issues after v1:
# - Raincoat not yellow (model defaulted to dark colors)
# - Woman facing wrong way
print('V0 length:', len(prompt_v0.split()))
print('V1 length:', len(prompt_v1.split()))
print('Iteration adds: style, lighting, composition, specific details')反復2:特定要素の修正
2回目の反復では、残っている問題を正確に対象とします。プロンプト全体を書き直すのではなく、1回目の反復で見つかった具体的な問題だけに対処します。
# ITERATION 1 issues:
# - Raincoat not yellow (model defaulted to dark)
# - Woman facing wrong way (walking away from camera)
# ITERATION 2: targeted fixes
prompt_v2 = (
'a young woman in a BRIGHT YELLOW raincoat '
'walking TOWARD the camera '
'down a rain-soaked Tokyo street at night, '
'neon signs reflected in puddles, steam rising from grates, '
'face visible, slight smile, carrying groceries, '
'cinematic photography style, street photography, '
'warm neon glow, wet pavement reflections, '
'medium shot, slightly low angle, bokeh background'
)
# Changes made:
# 1. "BRIGHT YELLOW" capitalization + adjective for emphasis
# 2. Added "walking TOWARD the camera" to fix direction
# 3. Added "face visible" to prevent back-to-camera result
# 4. Added specific detail: "carrying groceries, slight smile"
# Best practice: track what you changed and why
changelog = {
'v0_to_v1': 'Added style, lighting, composition, city details',
'v1_to_v2': 'Fixed raincoat color, fixed walking direction, added face constraint'
}
print('Changelog:', changelog)比較のために固定シードを使用する
プロンプトのバリエーションを比較するときは、変更する変数をプロンプトだけにするため、固定したランダムシードを使います。固定シードがなければ、出力の変化がプロンプトの編集によるものなのか、ランダム性によるものなのかを判断できません。
import requests
SD_API_URL = 'http://localhost:7860/sdapi/v1/txt2img'
def compare_prompt_versions(prompts_dict, negative='blurry, low quality',
seed=12345, steps=30):
results = {}
for version, prompt in prompts_dict.items():
payload = {
'prompt': prompt,
'negative_prompt': negative,
'seed': seed, # FIXED SEED for fair comparison
'steps': steps,
'cfg_scale': 7,
'width': 512,
'height': 512
}
response = requests.post(SD_API_URL, json=payload)
results[version] = response.json().get('images', [None])[0]
print(f'{version}: generated with seed {seed}')
return results
promptvariants = {
'v0': 'a woman walking in a rainy city at night',
'v1': 'cinematic, rainy Tokyo night, yellow raincoat, neon reflections',
'v2': 'BRIGHT YELLOW raincoat, facing camera, cinematic Tokyo rain night'
}
# compare_prompt_versions(prompt_variants, seed=42)引き算のアプローチ
反復を重ねるうちに、プロンプトが冗長になることがあります。引き算のアプローチでは、詳細なプロンプトから語句を1つずつ削除し、実際に効果を発揮している語句と、ノイズを増やしているだけの語句を見極めます。
# Start with a detailed prompt
full_prompt = (
'young woman, yellow raincoat, Tokyo, rain, neon, night, '
'street photography, cinematic, bokeh, wet pavement, '
'medium shot, warm tones, highly detailed, 8K, masterpiece, '
'award winning, beautiful, stunning, gorgeous'
)
# Remove terms and test if output quality degrades
test_removed = [
'masterpiece, award winning, beautiful, stunning, gorgeous', # quality tokens
'8K, highly detailed', # resolution tokens
'wet pavement', # specific detail
'cinematic', # style term
]
# Results typically show:
# - Generic quality tokens (masterpiece, beautiful) have minimal effect
# - Specific scene details (wet pavement, neon) matter most
# - Remove token: if output unchanged, that term is not contributing
print('Subtractive testing: remove terms and observe impact')
print('Terms that do not change output when removed can be discarded')
print('This produces lean, effective prompts')特定の失敗パターンに対する改善
画像生成でよくある失敗パターンには、それぞれ既知の対処法があります。この知識を体系的なチェックリストに組み込むと、改善作業を効率化できます。
FAILURE_FIXES = {
'Extra or deformed fingers': [
'Add to negative: extra fingers, deformed hands, bad anatomy',
'Add to positive: perfect hands, anatomically correct',
'Use inpainting to fix the specific area'
],
'Text artifacts / watermarks': [
'Add to negative: watermark, text, signature, logo',
'Increase CFG scale slightly',
'Use a different model checkpoint'
],
'Wrong style (cartoonish when photo expected)': [
'Add to negative: cartoon, anime, illustration, painted',
'Add to positive: photorealistic, DSLR, film photography',
'Use a photorealism-focused checkpoint'
],
'Background too busy / distracting': [
'Add to positive: simple background, clean background, blurred background',
'Add: shallow depth of field, bokeh background',
'Add to negative: cluttered background, busy background'
],
'Wrong color (model ignores color spec)': [
'Emphasize color: BRIGHT RED (caps), crimson red, deep scarlet',
'Add color to multiple places in prompt',
'Use img2img with a color reference image'
]
}
for failure, fixes in list(FAILURE_FIXES.items())[:3]:
print(f'\nISSUE: {failure}')
for fix in fixes:
print(f' FIX: {fix}')画像生成におけるプロンプトのバージョン管理
プロンプトのバージョンとその出力を体系的に記録します。これにより、今後のプロジェクトで参照できるライブラリが作られ、特定の題材やスタイルで何が有効なのか、そのパターンも明らかになります。
import json
from datetime import datetime
from pathlib import Path
def save_refinement_session(session_name, iterations, output_dir='prompt_sessions'):
Path(output_dir).mkdir(exist_ok=True)
session_data = {
'name': session_name,
'created': datetime.now().isoformat(),
'iterations': iterations
}
filepath = f'{output_dir}/{session_name}.json'
with open(filepath, 'w') as f:
json.dump(session_data, f, indent=2)
print(f'Session saved: {filepath}')
# Example session record
session = [
{
'version': 'v0',
'prompt': 'a woman walking in a rainy city at night',
'issues': ['too vague', 'no style', 'no lighting'],
'seed': 12345
},
{
'version': 'v1',
'prompt': 'young woman, yellow raincoat, Tokyo night rain, neon, cinematic',
'issues': ['raincoat not yellow', 'facing wrong way'],
'seed': 12345
},
{
'version': 'v2',
'prompt': 'BRIGHT YELLOW raincoat, facing camera, Tokyo rain, neon, cinematic',
'issues': [],
'seed': 12345,
'status': 'accepted'
}
]
save_refinement_session('tokyo_rain_woman', session)改善にかける予算:何回反復するか
改善の効果は次第に小さくなります。用途に応じて、何回の反復に投資するかを決めるための実用的な目安を次に示します。
ITERATION_BUDGET_GUIDE = {
'Quick internal mockup': {
'budget': '2-3 iterations',
'goal': 'Good enough to communicate concept',
'stopping_criteria': 'Main subject correct, rough style established'
},
'Marketing asset': {
'budget': '4-6 iterations',
'goal': 'Professional quality, brand-consistent',
'stopping_criteria': 'Color, style, composition match brief exactly'
},
'Hero image / campaign visual': {
'budget': '8-12 iterations + final manual touchup',
'goal': 'Publication quality, no visible artifacts',
'stopping_criteria': 'Zero artifacts, passes creative director review'
},
'Generative art piece': {
'budget': 'Unlimited — creative exploration',
'goal': 'Discover unexpected aesthetic direction',
'stopping_criteria': 'Emotional resonance with creator\'s intent'
}
}
for use_case, guide in ITERATION_BUDGET_GUIDE.items():
print(f'{use_case}: {guide["budget"]}')
print(f' Stop when: {guide["stopping_criteria"]}')
print()LLMを活用したプロンプト改善
テキストLLMを使って画像の問題を分析し、プロンプトの改善案を提案してもらいます。LLMの推論と画像生成を組み合わせることで、メタ改善ループを作り出せます。
import anthropic
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
REFINEMENT_ADVISOR_PROMPT = '''I am generating an image with this prompt:
Current prompt: {current_prompt}
Negative prompt: {current_negative}
The image has these problems:
{issues}
Suggest specific changes to the prompt that would fix these problems.
Provide:
1. Modified positive prompt (full, ready to use)
2. Modified negative prompt (full, ready to use)
3. Explanation of each change
Keep your changes minimal — only fix the stated issues, do not redesign the image.'''
def get_refinement_suggestion(current_prompt, current_negative, issues):
response = client.messages.create(
model='claude-opus-4-5', max_tokens=1000,
messages=[{'role': 'user', 'content':
REFINEMENT_ADVISOR_PROMPT.format(
current_prompt=current_prompt,
current_negative=current_negative,
issues='\n'.join(f'- {i}' for i in issues)
)}]
)
return response.content[0].text
suggestion = get_refinement_suggestion(
current_prompt='a woman in a raincoat at night',
current_negative='blurry',
issues=['raincoat appears dark not yellow', 'background too busy']
)
print(suggestion[:300], '...')確認問題
画像プロンプトの2つのバージョンを比較して、編集によって結果が改善されたか確認したいとします。公平に比較するには、何を一定に保つ必要があるでしょうか。
反復的な改善のまとめ
画像プロンプトの反復的な改善は、体系的に学習できるスキルです。
- ループ:生成 → 分析 → 調整 → 再生成
- 問題の分類:問題を題材、スタイル、照明、構図、品質に分類します
- 固定シード:プロンプトのバージョンは必ず同じシードで比較します
- 対象を絞った編集:特定した問題を修正し、プロンプト全体を書き直さないようにします
- 引き算のアプローチ:語句を削除して、実際に効果をもたらしている語句を特定します
- バージョンの追跡:各反復でプロンプト、問題、変更内容を記録します
- 予算の意識:モックアップでは2~3回、主要アセットでは8~12回の反復が目安です
よくある質問
「画像プロンプトの反復的な改善」レッスンは無料ですか?
はい。「画像プロンプトの反復的な改善」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、AI Prompt Engineeringコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 AI Prompt Engineeringコースには全4レッスンが含まれています。
「画像プロンプトの反復的な改善」で何を学びますか?
生成画像を分析し、体系的にプロンプトを調整します。 ブラウザで直接実行するハンズオンコードでAI Prompt Engineeringを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
AI Prompt Engineeringを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのAI Prompt Engineeringは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「画像プロンプトの反復的な改善」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このAI Prompt Engineeringレッスンでコードを書いて実行できますか?
はい。すべてのAI Prompt Engineeringレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 画像生成プロンプトの構成要素
- スタイルと芸術的媒体の指定
- ネガティブプロンプトと除外指定
- 画像プロンプトの反復的な改善