Skip to main content
대규모 데이터셋(ImageNet 등)에서 학습된 모델의 지식을 새로운 태스크에 전이하는 기법입니다. 적은 데이터로도 높은 성능을 빠르게 달성할 수 있습니다.

두 가지 전략

전략학습 대상데이터 요구량학습 시간
Feature Extraction분류 헤드만적음빠름
Fine-Tuning전체 (또는 일부)중간보통

Feature Extraction (특성 추출)

사전학습 모델의 가중치를 고정하고, 마지막 분류층만 새로 학습합니다.
1

사전학습 모델 로드 및 수정

import torch
import torch.nn as nn
import torchvision.models as models

# ResNet-18 사전학습 모델 로드
model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)

# 백본 가중치 동결
for param in model.parameters():
    param.requires_grad = False

# 분류 헤드 교체 (ImageNet 1000 클래스 → 사용자 클래스)
num_classes = 10  # 예: CIFAR-10
model.fc = nn.Linear(model.fc.in_features, num_classes)
# model.fc의 파라미터만 requires_grad=True

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)

# 학습 가능한 파라미터 수 확인
trainable = sum(p.numel() for p in model.parameters() if p.requires_grad)
total = sum(p.numel() for p in model.parameters())
print(f"학습 가능: {trainable:,} / 전체: {total:,} ({trainable/total:.1%})")
2

학습

# 분류 헤드만 학습하므로 높은 학습률 사용 가능
optimizer = torch.optim.Adam(model.fc.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()

for epoch in range(10):
    model.train()
    for images, labels in train_loader:
        images, labels = images.to(device), labels.to(device)
        optimizer.zero_grad()
        outputs = model(images)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

Fine-Tuning (미세 조정)

전체 모델을 낮은 학습률로 학습합니다. 층별로 다른 학습률을 적용하는 것이 효과적입니다.
# 사전학습 모델 로드
model = models.resnet18(weights=models.ResNet18_Weights.DEFAULT)
model.fc = nn.Linear(model.fc.in_features, num_classes)
model = model.to(device)

# 차별적 학습률 (Discriminative Learning Rate)
optimizer = torch.optim.AdamW([
    {'params': model.conv1.parameters(), 'lr': 1e-5},
    {'params': model.layer1.parameters(), 'lr': 1e-5},
    {'params': model.layer2.parameters(), 'lr': 5e-5},
    {'params': model.layer3.parameters(), 'lr': 1e-4},
    {'params': model.layer4.parameters(), 'lr': 5e-4},
    {'params': model.fc.parameters(), 'lr': 1e-3},
], weight_decay=0.01)

전략 선택 가이드

상황추천 전략이유
데이터 매우 적음 (500 미만)Feature Extraction과적합 방지
데이터 적음 (500~5000)Feature Extraction → Fine-Tuning점진적 해동
데이터 보통 (5000+)Fine-Tuning도메인 적응 가능
도메인이 매우 다름Fine-Tuning (초기층부터)전체 특성 재학습 필요
일반적으로 ResNet-50이 균형 잡힌 선택입니다. 경량 모델이 필요하면 EfficientNet-B0, 최고 성능이 필요하면 ConvNeXt-Base를 고려합니다. torchvision의 모델 가중치는 ImageNet-1K 기준이며, 일부 모델은 ImageNet-21K 사전학습 가중치도 제공합니다.
모든 층을 한 번에 학습하는 대신, 출력층부터 점진적으로 동결을 해제하며 학습하는 기법입니다. 사전학습된 저수준 특성(에지, 텍스처)을 보존하면서 고수준 특성만 도메인에 맞게 조정할 수 있습니다.

체크리스트

  • Feature Extraction과 Fine-Tuning의 차이를 설명할 수 있다
  • requires_grad = False로 레이어를 동결할 수 있다
  • 분류 헤드를 교체하고 새로운 클래스 수에 맞게 설정할 수 있다
  • 차별적 학습률을 적용할 수 있다

다음 문서

시퀀스 모델

시계열과 텍스트를 처리하는 RNN 계열 모델

Vision 탭

객체 탐지, 세그멘테이션 등 고급 비전 태스크