본문 바로가기
C.W.K.
Stream
Lesson 02 of 06 · published

비전 모델: ResNet, EfficientNet, ConvNeXt, ViT

~12 min · resnet, efficientnet, convnext, vit

Level 0텐서 탐구자
0 XP0/62 lessons0/13 achievements
0/120 XP to next level120 XP to go0% complete

사전 학습된 비전 모델 선택지

torchvision은 수십 가지 구조의 사전 학습 가중치를 제공해. 가장 자주 쓰는 모델군은 네 가지야:

  • ResNet(resnet18/34/50/101/152): 튼튼한 기본 선택이야. 신뢰할 만하고 구조가 잘 알려져 있으며 CUDA, MPS, CPU에서 모두 빠르게 돌아. 새 작업을 시작할 때 좋은 기준점이야.
  • EfficientNet(efficientnet_b0..b7, _v2_*): 정확도와 매개변수 수 사이의 균형이 좋아. 모바일 배포나 대규모 추론처럼 모델 크기가 중요할 때 적합해.
  • ConvNeXt(convnext_tiny/small/base/large): ViT와의 성능 격차를 좁힌 현대적인 ConvNet이야. 성능이 강하면서 같은 정확도에서는 ViT보다 빠른 경우가 많아.
  • 비전 Transformer(vit_b_16/32, vit_l_16, vit_h_14): Transformer를 이미지에 적용한 구조야. 많은 작업에서 최고 수준의 성능을 내지만 CNN보다 더 많은 데이터를 요구하고 매개변수당 계산도 조금 느려.

구조마다 다른 헤드 교체 방법

분류기 헤드가 있는 위치는 구조마다 달라:

  • ResNet: model.fc = nn.Linear(model.fc.in_features, num_classes)
  • EfficientNet: model.classifier[1] = nn.Linear(model.classifier[1].in_features, num_classes)
  • ConvNeXt: model.classifier[2] = nn.Linear(model.classifier[2].in_features, num_classes)
  • ViT: model.heads.head = nn.Linear(model.heads.head.in_features, num_classes)

먼저 print(model)로 구조를 보고 마지막 선형 계층을 찾아 교체해. 관례가 어느 정도 일정하므로 작은 도우미 함수를 한 번 작성해 재사용할 수 있어.

Code

네 가지 모델군 로딩·python
from torchvision import models
from torchvision.models import (
    ResNet50_Weights, EfficientNet_B0_Weights,
    ConvNeXt_Base_Weights, ViT_B_16_Weights,
)

resnet = models.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2)
effnet = models.efficientnet_b0(weights=EfficientNet_B0_Weights.IMAGENET1K_V1)
convnext = models.convnext_base(weights=ConvNeXt_Base_Weights.IMAGENET1K_V1)
vit = models.vit_b_16(weights=ViT_B_16_Weights.IMAGENET1K_V1)

for m in [resnet, effnet, convnext, vit]:
    n = sum(p.numel() for p in m.parameters())
    print(f"{type(m).__name__:14s}  {n/1e6:6.1f}M params")
구조를 고려한 헤드 교체·python
import torch.nn as nn
from torchvision import models
from torchvision.models import (
    ResNet50_Weights, EfficientNet_B0_Weights,
    ConvNeXt_Base_Weights, ViT_B_16_Weights,
)

def make_classifier(arch, num_classes):
    if arch == 'resnet50':
        m = models.resnet50(weights=ResNet50_Weights.IMAGENET1K_V2)
        m.fc = nn.Linear(m.fc.in_features, num_classes)
    elif arch == 'efficientnet_b0':
        m = models.efficientnet_b0(weights=EfficientNet_B0_Weights.IMAGENET1K_V1)
        m.classifier[1] = nn.Linear(m.classifier[1].in_features, num_classes)
    elif arch == 'convnext_base':
        m = models.convnext_base(weights=ConvNeXt_Base_Weights.IMAGENET1K_V1)
        m.classifier[2] = nn.Linear(m.classifier[2].in_features, num_classes)
    elif arch == 'vit_b_16':
        m = models.vit_b_16(weights=ViT_B_16_Weights.IMAGENET1K_V1)
        m.heads.head = nn.Linear(m.heads.head.in_features, num_classes)
    return m

model = make_classifier('convnext_base', num_classes=5)
timm: torchvision에 없는 구조 필요할 때·python
# pip install timm
import timm

# timm has 1000+ pretrained vision models — ConvNeXt v2, MaxViT, EVA, BEiT, etc.
model = timm.create_model('convnext_base.fb_in22k_ft_in1k', pretrained=True, num_classes=5)

# timm also gives you the matching transforms
data_cfg = timm.data.resolve_data_config({}, model=model)
transform = timm.data.create_transform(**data_cfg)

# For research, timm is often more up-to-date than torchvision
# For production stability, torchvision is the safer pick.

External links

Exercise

make_classifier 함수로 num_classes=5인 네 구조를 모두 만들어 봐. 사용할 수 있는 가장 빠른 장치에서 (8, 3, 224, 224) 배치 하나의 순전파 시간을 재. 구조별 지연 시간을 기록해 두면 배포 모델을 결정할 때 유용해.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.