Chuyển đến nội dung chính

第 5 課:人工智慧設計生成 — 文字到圖像、風格遷移和時尚即時工程

整合穩定擴散/SDXL、時尚專業提示工程、ControlNet、風格傳輸、LoRA 微調、修復、修復、大量生成管道、內容審核、GPU 基礎設施。

🏗️ 建築 — 第 5 課 第 5 課:AI 設計生成 — 文字轉圖像、風格轉換和提示 時尚工程

時裝設計與按需印刷系統架構-從領域分析到生產

第 2 部分:人工智慧驅動的設計工作室

亞洲開發網

1. AI設計生成流程

User Input                        AI Generation Pipeline
┌─────────────┐                 ┌────────────────────────────────┐
│ Text Prompt │────────────────▶│ Prompt Engineering Layer       │
│ Style Ref   │                 │  ├── Fashion vocabulary mapper │
│ Color Pref  │                 │  ├── Negative prompt builder   │
│ Product Type│                 │  └── Quality enhancer          │
└─────────────┘                 └────────────┬───────────────────┘
                                             │
                                ┌────────────▼───────────────────┐
                                │ Model Selection & Routing      │
                                │  ├── SDXL (general fashion)    │
                                │  ├── LoRA (brand-specific)     │
                                │  ├── ControlNet (structure)    │
                                │  └── IP-Adapter (style ref)    │
                                └────────────┬───────────────────┘
                                             │
                                ┌────────────▼───────────────────┐
                                │ GPU Inference Farm             │
                                │  ├── NVIDIA A100/H100 cluster  │
                                │  ├── Queue: BullMQ + priority  │
                                │  └── Batch processing          │
                                └────────────┬───────────────────┘
                                             │
                                ┌────────────▼───────────────────┐
                                │ Post-Processing                │
                                │  ├── Upscale (Real-ESRGAN 4x)  │
                                │  ├── Background removal        │
                                │  ├── Color correction          │
                                │  └── Content moderation (NSFW) │
                                └────────────┬───────────────────┘
                                             │
                                ┌────────────▼───────────────────┐
                                │ Design Library                 │
                                │  ├── Save to user gallery      │
                                │  ├── Generate variations       │
                                │  └── Apply to canvas           │
                                └────────────────────────────────┘

2. 時尚領域的快速工程

interface FashionPromptBuilder {
  buildPrompt(input: UserPromptInput): EnhancedPrompt;
}

interface UserPromptInput {
  text: string;                               // "minimalist mountain landscape"
  productType: 'tshirt' | 'hoodie' | 'poster' | 'mug';
  style: FashionStyle;
  colorPalette?: string[];                    // ["#1a1a2e", "#16213e", "#e94560"]
  printMethod: 'dtg' | 'dtf' | 'sublimation' | 'screen_print';
}

type FashionStyle =
  | 'street_wear' | 'vintage_retro' | 'minimalist' | 'japanese'
  | 'grunge' | 'y2k' | 'cyberpunk' | 'botanical'
  | 'typography' | 'abstract' | 'pop_art' | 'watercolor';

interface EnhancedPrompt {
  positive: string;
  negative: string;
  model: string;
  lora?: string;
  controlnet?: ControlNetConfig;
  samplerSettings: SamplerSettings;
}

// Fashion-specific prompt templates
const STYLE_TEMPLATES: Record<FashionStyle, StyleTemplate> = {
  street_wear: {
    prefix: 'urban streetwear graphic design,',
    suffix: 'bold colors, graffiti influence, hip-hop culture, high contrast',
    negativeAdd: 'photorealistic, 3d render',
    recommendedModel: 'sdxl-streetwear-v2',
  },
  vintage_retro: {
    prefix: 'vintage retro illustration,',
    suffix: 'distressed texture, faded colors, 70s aesthetic, halftone dots',
    negativeAdd: 'modern, clean, digital',
    recommendedModel: 'sdxl-base',
    lora: 'vintage-retro-lora-v1',
  },
  minimalist: {
    prefix: 'minimalist design, clean lines,',
    suffix: 'simple geometric, whitespace, modern typography, flat design',
    negativeAdd: 'complex, busy, detailed, ornate',
    recommendedModel: 'sdxl-base',
  },
  japanese: {
    prefix: 'Japanese art style,',
    suffix: 'ukiyo-e influence, cherry blossom, wave, kanji typography, zen aesthetic',
    negativeAdd: 'western, photorealistic',
    recommendedModel: 'sdxl-anime-v3',
  },
  // ... other styles
};

function buildFashionPrompt(input: UserPromptInput): EnhancedPrompt {
  const template = STYLE_TEMPLATES[input.style];
  
  // Color-aware prompting
  const colorHint = input.colorPalette
    ? `color palette: ${input.colorPalette.join(', ')},`
    : '';

  // Print-method-aware
  const printHint = {
    dtg: 'transparent background, isolated design, print-ready',
    dtf: 'transparent background, vibrant colors, high saturation, no gradients',
    sublimation: 'all-over print pattern, edge-to-edge, seamless',
    screen_print: 'limited colors, spot color separation, bold shapes, no gradients',
  }[input.printMethod];

  const positive = [
    template.prefix,
    input.text,
    colorHint,
    printHint,
    template.suffix,
    'high quality, 300 DPI, professional design',
  ].filter(Boolean).join(' ');

  const negative = [
    'blurry, low quality, watermark, signature, text artifact',
    'deformed, ugly, bad anatomy',
    template.negativeAdd,
  ].join(', ');

  return {
    positive,
    negative,
    model: template.recommendedModel,
    lora: template.lora,
    samplerSettings: {
      steps: 30,
      cfgScale: 7.5,
      sampler: 'dpm++_2m_karras',
      width: 1024,
      height: 1024,
      seed: -1,   // random
    },
  };
}

3. 模型架構與微調

LoRA 針對品牌特定風格進行微調

# LoRA training config cho fashion brand style
training_config = {
    "model": "stabilityai/stable-diffusion-xl-base-1.0",
    "lora_rank": 32,
    "lora_alpha": 32,
    "learning_rate": 1e-4,
    "train_batch_size": 4,
    "num_train_epochs": 100,
    "resolution": 1024,
    "dataset": {
        "instance_prompt": "a design in the style of [brand_token]",
        "class_prompt": "a fashion graphic design",
        "instance_data_dir": "./training_data/brand_designs/",
        "num_class_images": 200,
    },
    "optimizer": "adamw",
    "lr_scheduler": "cosine_with_restarts",
    "mixed_precision": "fp16",
    "gradient_accumulation_steps": 4,
}

用於結構引導生成的 ControlNet

interface ControlNetConfig {
  model: ControlNetModel;
  inputImage: string;     // Reference image URL
  strength: number;       // 0.0 - 1.0
  guidanceStart: number;  // When to start applying
  guidanceEnd: number;    // When to stop
}

type ControlNetModel =
  | 'canny'          // Edge detection → maintain outline structure
  | 'depth'          // Depth map → 3D-like structure
  | 'openpose'       // Human pose → design placement trên body
  | 'lineart'        // Line art → sketch to design
  | 'scribble'       // Rough sketch → polished design
  | 'seg'            // Segmentation → region-based generation
  | 'color'          // Color reference → maintain color scheme
  | 'tile';          // Tile/pattern → seamless pattern generation

// Use case: User uploads a sketch, generate polished design
const sketchToDesign: ControlNetConfig = {
  model: 'lineart',
  inputImage: '/user-uploads/sketch-001.png',
  strength: 0.8,
  guidanceStart: 0.0,
  guidanceEnd: 0.8,
};

4. IP 適配器和風格傳輸

// Style transfer: Tham chiếu từ ảnh mẫu → áp dụng style vào design mới
interface StyleTransferRequest {
  // Text prompt mô tả nội dung mong muốn
  prompt: string;                  // "tiger face, fierce expression"
  
  // Style reference image
  styleReference: {
    imageUrl: string;              // Ảnh mẫu style
    strength: number;              // 0.3 - 0.8 (quá cao sẽ copy y nguyên)
    type: 'ip_adapter' | 'ip_adapter_face' | 'ip_adapter_plus';
  };
  
  // Optional: Combine with ControlNet
  structureGuide?: ControlNetConfig;
}

// API endpoint
// POST /api/v1/generate/style-transfer
async function styleTransfer(req: StyleTransferRequest): Promise<GenerationResult> {
  const pipeline = new StableDiffusionXLPipeline({
    model: 'sdxl-base',
    ipAdapter: {
      model: req.styleReference.type,
      image: await loadImage(req.styleReference.imageUrl),
      scale: req.styleReference.strength,
    },
    controlnet: req.structureGuide ? {
      model: req.structureGuide.model,
      image: await loadImage(req.structureGuide.inputImage),
      conditioning_scale: req.structureGuide.strength,
    } : undefined,
  });

  return pipeline.generate({
    prompt: req.prompt,
    negative_prompt: DEFAULT_NEGATIVE,
    num_inference_steps: 30,
    guidance_scale: 7.5,
    width: 1024,
    height: 1024,
    num_images: 4,   // Generate 4 variations
  });
}

5.GPU基礎設施和作業佇列

// GPU Worker Architecture
interface GPUWorkerPool {
  workers: GPUWorker[];
  queue: BullMQ.Queue;
  
  // Auto-scaling
  minWorkers: 2;
  maxWorkers: 20;
  scaleUpThreshold: 0.8;   // 80% utilization → scale up
  scaleDownDelay: 300;      // 5 min idle → scale down
}

interface GPUWorker {
  id: string;
  gpu: 'a100_40gb' | 'a100_80gb' | 'h100';
  status: 'idle' | 'processing' | 'loading_model';
  currentModel: string;       // Model đang load trên VRAM
  vramUsageMB: number;
  
  // Model caching — giữ model trên VRAM
  loadedModels: Map<string, LoadedModel>;
}

// Job Queue với priority
const generationQueue = new Queue('ai-generation', {
  defaultJobOptions: {
    attempts: 3,
    backoff: { type: 'exponential', delay: 2000 },
    removeOnComplete: { age: 3600 },        // Clean after 1h
    removeOnFail: { age: 86400 },           // Keep failed 24h
  },
});

// Priority levels
enum JobPriority {
  REALTIME = 1,      // Premium user, interactive
  HIGH = 5,          // Logged-in user
  NORMAL = 10,       // Free user
  BATCH = 20,        // Background batch generation
}

6. 後處理流程

async function postProcessDesign(rawImage: Buffer): Promise<ProcessedDesign> {
  // 1. Upscale (Real-ESRGAN)
  const upscaled = await upscaleImage(rawImage, {
    model: 'realesrgan-x4plus',
    scale: 4,         // 1024 → 4096
    denoise: 0.5,
  });

  // 2. Background removal (always transparent for POD)
  const noBackground = await removeBackground(upscaled, {
    model: 'rembg-u2net',
    alphaMatting: true,
    alphaMattingForegroundThreshold: 240,
    alphaMattingBackgroundThreshold: 10,
  });

  // 3. Content moderation
  const moderationResult = await moderateContent(noBackground, {
    checkNSFW: true,
    checkViolence: true,
    checkCopyright: true,    // CLIP-based similarity check vs known brands/IPs
    threshold: 0.85,
  });

  if (!moderationResult.safe) {
    throw new ContentPolicyViolation(moderationResult.reason);
  }

  // 4. Color correction cho print
  const corrected = await colorCorrect(noBackground, {
    targetColorSpace: 'srgb',
    contrastEnhance: 1.1,
    saturationBoost: 1.05,    // Slightly boost cho print (colors look duller on fabric)
  });

  return {
    preview: await exportWebP(corrected, { quality: 85, maxWidth: 1200 }),
    printReady: await exportPNG(corrected, { dpi: 300, colorSpace: 'srgb' }),
    metadata: {
      width: corrected.width,
      height: corrected.height,
      hasTransparency: true,
      colorCount: await analyzeColors(corrected),
      moderationScore: moderationResult.score,
    },
  };
}

7. 批次產生和變化

// Batch generation: Tạo nhiều design cùng lúc cho product line
interface BatchGenerationRequest {
  basePrompt: string;
  variations: VariationType[];
  count: number;                // Số lượng per variation
  productTypes: string[];       // Apply lên nhiều product
}

type VariationType =
  | { type: 'color_swap'; palettes: string[][] }       // Same design, different colors
  | { type: 'style_mix'; styles: FashionStyle[] }      // Same content, different styles
  | { type: 'prompt_expand'; keywords: string[] }      // Expand prompt with keywords
  | { type: 'seed_walk'; seedStart: number; step: number }; // Smooth seed variation

async function batchGenerate(req: BatchGenerationRequest): Promise<BatchResult> {
  const jobs: GenerationJob[] = [];

  for (const variation of req.variations) {
    for (let i = 0; i < req.count; i++) {
      const prompt = applyVariation(req.basePrompt, variation, i);
      
      for (const productType of req.productTypes) {
        jobs.push({
          prompt,
          productType,
          priority: JobPriority.BATCH,
          callbackUrl: '/webhooks/batch-complete',
        });
      }
    }
  }

  // Enqueue all jobs
  const bulkResult = await generationQueue.addBulk(
    jobs.map(job => ({
      name: 'generate',
      data: job,
      opts: { priority: job.priority },
    }))
  );

  return {
    batchId: generateBatchId(),
    totalJobs: jobs.length,
    estimatedTime: estimateCompletionTime(jobs.length),
    statusUrl: `/api/v1/batch/${batchId}/status`,
  };
}

八、總結

組件科技目的
基本型號SDXL 1.0高品質 1024x1024 生成
微調洛拉(排名 32)品牌特定風格適配
結構控制控制網從草圖到設計,姿勢引導
風格轉移IP適配器基於參考的風格匹配
升級真實 ESRGAN 4x1024 → 4096 用於列印 (300 DPI)
背景去除劑U2-Net/REMBGPOD 透明背景
內容審核CLIP + NSFW 分類器安全性和 IP 合規性
作業佇列BullMQ + RedisPriority queue, retry, batch
GPU基礎設施A100/H100 + 自動縮放經濟高效的 GPU 分配
及時工程時尚專用模板領域優化生成