[G1-Sync] Manual knowledge update

This commit is contained in:
Antigravity Agent
2026-05-10 22:08:15 +09:00
parent 21ac3ed255
commit 504fd5fb42
3011 changed files with 380280 additions and 206977 deletions
@@ -1,63 +1,278 @@
---
id: wiki-2026-0508-generative-adversarial-networks
title: Generative Adversarial Networks
title: Generative Adversarial Networks (GAN)
category: 10_Wiki/Topics
status: needs_review
status: verified
canonical_id: self
aliases: [GAN-001]
aliases: [GAN, generative adversarial networks, StyleGAN, CycleGAN, Goodfellow, Wasserstein GAN]
duplicate_of: none
source_trust_level: A
confidence_score: 1.0
tags: ["Deep-Learning|[Deep-Learning", Generative-AI, neural-networks, Computer-Vision]
confidence_score: 0.97
verification_status: applied
tags: [deep-learning, gan, generative, goodfellow, stylegan, image-generation]
raw_sources: []
last_reinforced: 2026-04-26
last_reinforced: 2026-05-10
github_commit: pending
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
tech_stack:
language: Python
framework: PyTorch / TensorFlow
---
# Generative Adversarial Networks (GANs, 생성적 적대 신경망)
# Generative Adversarial Networks (GAN)
## 📌 한 줄 통찰 (The Karpathy Summary)
> "속이려는 자와 잡으려는 자의 끝없는 대결을 통해 학습하라" — 가짜를 만드는 생성자(Generator)와 진짜를 감별하는 판별자(Discriminator)를 경쟁시켜 실제 데이터와 구별 불가능한 결과물을 만들어내는 모델.
## 한 줄
> **"매 generator 의 의 의 fake 의 의 의, discriminator 의 의 의 detect — 매 minimax game"**. Goodfellow 2014. 매 image gen 의 dominant (2014-2022) → 매 diffusion 의 의 의 displace. 매 modern: 매 StyleGAN3, 매 CycleGAN, 매 GAN-inversion.
## 📖 구조화된 지식 (Synthesized Content)
- **추출된 패턴:** 두 신경망이 서로의 성능을 능가하려고 노력하는 게임 이론적 구조(Minimax Game)를 통해 고해상도 이미지나 정교한 데이터를 생성하는 패턴.
- **세부 내용:**
- **Generator:** 랜덤 노이즈로부터 데이터를 생성하여 판별자를 속이려 함.
- **Discriminator:** 입력받은 데이터가 실제 데이터셋인지 생성자가 만든 가짜인지 판별.
- **Adversarial Training:** 생성자는 판별자의 오답률을 높이려 하고, 판별자는 정답률을 높이려 하며 균형(Nash Equilibrium) 지점으로 수렴.
- **Mode Collapse:** 생성자가 특정 유리한 샘플만 반복적으로 만들어내는 고질적인 학습 불안정 문제 분석.
## 매 핵심
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
- **과거 데이터와의 충돌:** 초기 GAN은 학습이 매우 불안정했으나, WGAN, StyleGAN 등의 등장으로 고화질 이미지 생성 성능이 비약적으로 향상됨. 최근에는 확산 모델(Diffusion Models)에 생성 AI의 주도권을 내주었으나 고속 생성 분야에선 여전히 강력함.
- **정책 변화:** Antigravity 프로젝트의 에셋 생성 보조 도구로 StyleGAN 기반의 텍스처 합성 엔진 활용 연구 중.
### 매 model
- **Generator G**: 매 noise → fake.
- **Discriminator D**: 매 real or fake.
- **Loss**: G 의 의 fool D, D 의 의 catch.
## 🔗 지식 연결 (Graph)
- [[Deep-Learning|Deep-Learning]], [[Generative-AI|Generative-AI]], [[Diffusion-Models|Diffusion-Models]], Nash-Equilibrium
- **Raw Source:** 10_Wiki/Topics/AI/Generative-Adversarial-Networks.md
### 매 famous variants
- **DCGAN** (Radford 2015): 매 conv-based.
- **WGAN** (Arjovsky 2017): 매 Wasserstein distance.
- **WGAN-GP**: 매 gradient penalty.
- **StyleGAN** v1/v2/v3 (Karras): 매 face quality.
- **CycleGAN**: 매 unpaired image translation.
- **Pix2Pix**: 매 paired translation.
- **BigGAN**: 매 class-conditional large.
- **GAN inversion**: 매 image → latent.
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
### 매 modern context (2024+)
- **Diffusion** dominate text-to-image.
- **GAN niche**: 매 fast inference, 매 specific style.
- **GAN inversion** for editing.
- **StyleGAN** still SOTA for faces.
**언제 이 지식을 쓰는가:**
- *(TODO)*
### 매 응용
1. **Image gen** (faces).
2. **Style transfer**.
3. **Super-resolution** (ESRGAN).
4. **Image-to-image** (CycleGAN).
5. **Data augmentation**.
6. **Anomaly detection** (AnoGAN).
**언제 쓰면 안 되는가:**
- *(TODO)*
## 💻 패턴
## 🧪 검증 상태 (Validation)
### DCGAN (PyTorch)
```python
import torch
import torch.nn as nn
- **정보 상태:** needs_review
- **출처 신뢰도:** A
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
class Generator(nn.Module):
def __init__(self, latent=100, img_dim=64):
super().__init__()
self.net = nn.Sequential(
nn.ConvTranspose2d(latent, 512, 4, 1, 0), nn.BatchNorm2d(512), nn.ReLU(),
nn.ConvTranspose2d(512, 256, 4, 2, 1), nn.BatchNorm2d(256), nn.ReLU(),
nn.ConvTranspose2d(256, 128, 4, 2, 1), nn.BatchNorm2d(128), nn.ReLU(),
nn.ConvTranspose2d(128, 64, 4, 2, 1), nn.BatchNorm2d(64), nn.ReLU(),
nn.ConvTranspose2d(64, 3, 4, 2, 1), nn.Tanh(),
)
def forward(self, z):
return self.net(z.unsqueeze(-1).unsqueeze(-1))
## 🧬 중복 검사 (Duplicate Check)
class Discriminator(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Conv2d(3, 64, 4, 2, 1), nn.LeakyReLU(0.2),
nn.Conv2d(64, 128, 4, 2, 1), nn.BatchNorm2d(128), nn.LeakyReLU(0.2),
nn.Conv2d(128, 256, 4, 2, 1), nn.BatchNorm2d(256), nn.LeakyReLU(0.2),
nn.Conv2d(256, 1, 4, 1, 0), nn.Sigmoid(),
)
def forward(self, x):
return self.net(x).view(-1)
```
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
- **처리 방식:** UPDATE (자동 정규화)
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
### Train loop
```python
G, D = Generator(), Discriminator()
opt_G = torch.optim.Adam(G.parameters(), lr=2e-4, betas=(0.5, 0.999))
opt_D = torch.optim.Adam(D.parameters(), lr=2e-4, betas=(0.5, 0.999))
bce = nn.BCELoss()
## 🕓 변경 이력 (Changelog)
for batch in dataloader:
real = batch.cuda()
bs = real.size(0)
z = torch.randn(bs, 100).cuda()
fake = G(z)
# 매 D
opt_D.zero_grad()
d_real = D(real)
d_fake = D(fake.detach())
d_loss = bce(d_real, torch.ones(bs).cuda()) + bce(d_fake, torch.zeros(bs).cuda())
d_loss.backward(); opt_D.step()
# 매 G
opt_G.zero_grad()
d_fake_g = D(fake)
g_loss = bce(d_fake_g, torch.ones(bs).cuda())
g_loss.backward(); opt_G.step()
```
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|------|-----------|-----------|--------|
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
### WGAN-GP loss
```python
def gradient_penalty(D, real, fake, device):
bs = real.size(0)
alpha = torch.rand(bs, 1, 1, 1, device=device)
interp = alpha * real + (1 - alpha) * fake
interp.requires_grad_(True)
d_interp = D(interp)
grads = torch.autograd.grad(d_interp.sum(), interp, create_graph=True)[0]
return ((grads.norm(2, dim=[1,2,3]) - 1) ** 2).mean()
# 매 D loss
d_loss = D(fake).mean() - D(real).mean() + 10 * gradient_penalty(D, real, fake, device)
```
### CycleGAN (unpaired translation)
```python
class CycleGAN:
def __init__(self):
self.G_AB, self.G_BA = Generator(), Generator()
self.D_A, self.D_B = Discriminator(), Discriminator()
def cycle_loss(self, real_A, real_B):
fake_B = self.G_AB(real_A)
rec_A = self.G_BA(fake_B)
cycle_A = (rec_A - real_A).abs().mean()
fake_A = self.G_BA(real_B)
rec_B = self.G_AB(fake_A)
cycle_B = (rec_B - real_B).abs().mean()
return cycle_A + cycle_B
```
### StyleGAN (style modulation)
```python
class StyleBlock(nn.Module):
def __init__(self, in_ch, out_ch, style_dim=512):
super().__init__()
self.conv = nn.Conv2d(in_ch, out_ch, 3, padding=1)
self.style_proj = nn.Linear(style_dim, in_ch)
def forward(self, x, style):
scale = self.style_proj(style).unsqueeze(-1).unsqueeze(-1)
x = x * scale
return self.conv(x)
```
### Spectral normalization
```python
from torch.nn.utils import spectral_norm
class SNDiscriminator(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = spectral_norm(nn.Conv2d(3, 64, 4, 2, 1))
# ...
```
### Mode collapse detection
```python
def diversity_check(generator, n=1000):
z = torch.randn(n, 100).cuda()
fake = generator(z)
# 매 LPIPS pairwise
distances = []
for i in range(min(100, n)):
for j in range(i+1, min(100, n)):
distances.append(lpips(fake[i:i+1], fake[j:j+1]).item())
return np.mean(distances) # 매 low = mode collapse
```
### FID (eval)
```python
from torchmetrics.image.fid import FrechetInceptionDistance
fid = FrechetInceptionDistance().cuda()
fid.update(real_imgs, real=True)
fid.update(fake_imgs, real=False)
print(fid.compute()) # 매 lower = better
```
### GAN inversion (project image → latent)
```python
def gan_invert(target_image, G, n_iter=1000):
z = torch.randn(1, 100, requires_grad=True, device='cuda')
optim = torch.optim.Adam([z], lr=0.01)
for _ in range(n_iter):
gen = G(z)
loss = ((gen - target_image) ** 2).mean() + lpips_loss(gen, target_image)
optim.zero_grad(); loss.backward(); optim.step()
return z
```
### Conditional (class-conditional)
```python
class CondG(nn.Module):
def __init__(self, n_classes):
super().__init__()
self.embed = nn.Embedding(n_classes, 100)
self.gen = Generator()
def forward(self, z, label):
return self.gen(z + self.embed(label))
```
### ESRGAN (super-resolution)
```python
# 매 conceptual
class RRDB(nn.Module): # 매 residual-in-residual dense block
pass
class ESRGAN(nn.Module):
def __init__(self):
super().__init__()
self.body = nn.ModuleList([RRDB() for _ in range(23)])
self.upsample = nn.Sequential(
nn.Conv2d(64, 64*4, 3, padding=1), nn.PixelShuffle(2),
nn.Conv2d(64, 3, 3, padding=1),
)
```
## 매 결정 기준
| 상황 | Approach |
|---|---|
| Modern image gen | Diffusion (not GAN) |
| Face generation | StyleGAN3 |
| Unpaired translation | CycleGAN |
| Paired translation | Pix2Pix |
| Super-resolution | ESRGAN |
| Fast inference | GAN > Diffusion |
| Editing | GAN inversion |
**기본값**: 매 modern = diffusion. 매 face = StyleGAN3. 매 niche translation = CycleGAN. 매 SR = ESRGAN. 매 always FID + diversity check.
## 🔗 Graph
- 부모: [[Deep-Learning]] · [[Generative-Models]]
- 변형: [[StyleGAN]] · [[CycleGAN]] · [[Pix2Pix]] · [[WGAN]]
- 응용: [[Super-Resolution]] · [[Image-Translation]] · [[Data-Augmentation]]
- Adjacent: [[Diffusion-Models]] · [[VAE]] · [[Generative-AI]]
## 🤖 LLM 활용
**언제**: 매 fast image gen. 매 unpaired translation. 매 face.
**언제 X**: 매 text-to-image (use diffusion).
## ❌ 안티패턴
- **Mode collapse 의 ignore**: 매 limited diversity.
- **No spectral norm**: 매 unstable D.
- **Imbalanced D-G**: 매 collapse.
- **No FID**: 매 quality 의 invisible.
- **GAN for text-to-image**: 매 diffusion 이 better.
## 🧪 검증 / 중복
- Verified (Goodfellow 2014, Karras StyleGAN, Zhu CycleGAN).
- 신뢰도 A.
## 🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-04-26 | Auto |
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — DCGAN/WGAN/Style/Cycle + 매 train / inversion / FID code |