AI Prompt Engineering · บทเรียน

การปรับปรุงพรอมต์สร้างภาพแบบวนซ้ำ

วิเคราะห์ภาพที่สร้างขึ้นและปรับพรอมต์อย่างเป็นระบบ

บทเรียน 4 จาก 413 ขั้นตอน

การปรับปรุงพรอมต์สร้างภาพแบบวนซ้ำ เป็นบทเรียน AI Prompt Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Prompt Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน

เหตุใดจึงต้องปรับปรุงแบบวนซ้ำ

พรอมต์ภาพจากการ generate ครั้งแรกมักไม่ค่อยให้ผลลัพธ์ตรงตามที่คุณต้องการ การปรับปรุงแบบวนซ้ำคือกระบวนการทำงานอย่างเป็นระบบ: generate วิเคราะห์ ระบุช่องว่าง ปรับพรอมต์ แล้ว generate ใหม่ แต่ละรอบจะลดช่องว่างระหว่างเจตนากับผลลัพธ์

วงจรการปรับปรุง

วงจรการปรับปรุงแบบวนซ้ำมีสี่ขั้นตอน: สร้าง (ใช้พรอมต์ปัจจุบันเพื่อ create ภาพ), วิเคราะห์ (ระบุสิ่งที่ผิดหรือขาดหาย), ปรับ (แก้ไขพรอมต์เพื่อจัดการกับปัญหา) และ สร้างใหม่ ทำซ้ำจนกว่าจะพอใจหรือถูกหยุดด้วยข้อจำกัดด้านงบประมาณหรือเวลา

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.')

การวิเคราะห์ภาพที่ generate: การจัดหมวดหมู่ปัญหา

เมื่อวิเคราะห์ว่าเกิดข้อผิดพลาดอะไรขึ้น ให้จัดหมวดหมู่ปัญหาตามประเภท วิธีนี้จะชี้ให้เห็นว่าควรปรับส่วนใดของพรอมต์ ไม่ว่าจะเป็นการเพิ่มรายละเอียด การลบความขัดแย้ง หรือการปรับน้ำหนัก

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: การแก้ไของค์ประกอบเฉพาะ

การทำซ้ำครั้งที่สองมุ่งแก้ปัญหาที่เหลืออย่างแม่นยำ ไม่ควรเขียนพรอมต์ทั้งหมดใหม่ เพียงจัดการกับปัญหาเฉพาะที่พบในการทำซ้ำครั้งที่ 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)

แนวทางการตัดทอน

บางครั้งพรอมต์อาจมีรายละเอียดมากเกินไปหลังจากผ่านการทำซ้ำหลายรอบ แนวทางการตัดทอน จะเริ่มจากพรอมต์ที่มีรายละเอียด แล้วลบคำศัพท์ออกทีละคำเพื่อดูว่าคำใดมีผลจริง และคำใดเป็นเพียงสัญญาณรบกวน

# 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–3 รอบสำหรับภาพร่าง และ 8–12 รอบสำหรับชิ้นงานหลัก
เริ่มต้นได้ฟรี

เรียนรู้ AI Prompt Engineering ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
53
บทเรียน
199

คำถามที่พบบ่อย

บทเรียน “การปรับปรุงพรอมต์สร้างภาพแบบวนซ้ำ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การปรับปรุงพรอมต์สร้างภาพแบบวนซ้ำ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Prompt Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การปรับปรุงพรอมต์สร้างภาพแบบวนซ้ำ”

วิเคราะห์ภาพที่สร้างขึ้นและปรับพรอมต์อย่างเป็นระบบ คุณปฏิบัติ AI Prompt Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Prompt Engineering หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Prompt Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การปรับปรุงพรอมต์สร้างภาพแบบวนซ้ำ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AI Prompt Engineering นี้ได้ไหม

ได้ บทเรียน AI Prompt Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. องค์ประกอบของพรอมต์สร้างภาพ
  2. การระบุรูปแบบและสื่อศิลป์
  3. พรอมต์เชิงลบและสิ่งที่ต้องยกเว้น
  4. การปรับปรุงพรอมต์สร้างภาพแบบวนซ้ำ
← กลับไปที่ AI Prompt Engineering