C.W.K.
Stream
Lesson 04 of 06 · published

Transfer learning — feature extraction vs fine-tuning

~14 min · transfer-learning, fine-tuning, pretrained

Level 0Level 0
0 XP0/78 lessons0/17 achievements
0/100 XP to next level100 XP to go0% complete

컴퓨터 비전에서 가장 비용 효율적인 트릭

강한 vision model을 처음부터 training하려면 라벨 달린 이미지 수백만 개 필요. 실전 프로젝트에 그런 거 거의 없어. Transfer learning은 ImageNet (1.28M 이미지, 1000 클래스)에 pretrained 된 model에서 시작해서 적은 데이터로 task에 적응시켜 해결.

두 단계, 이 순서대로:

  1. Feature extraction — backbone 동결, 새 classifier head만 데이터로 training. Pretrained feature가 최종 정확도의 80–90%를 적은 compute로 줘.
  2. Fine-tuning — backbone (또는 마지막 몇 블록) 해동, 훨씬 작은 learning rate (예: 1/10) 사용, training 계속. Pretrained feature를 특정 task로 살짝 밀어.
해동 전에 항상 동결. 처음부터 모든 weight trainable로 시작하면 랜덤 classifier head가 거대한 gradient 만들어 pretrained backbone weight를 몇 step 만에 망쳐. 항상 feature extraction 먼저, 그 다음 fine-tuning.

Code

Feature extraction phase·python
import tensorflow as tf
from tensorflow import keras

base_model = keras.applications.EfficientNetV2S(
    input_shape=(224, 224, 3),
    include_top=False,
    weights='imagenet',
)
base_model.trainable = False    # FREEZE backbone

inputs = keras.Input(shape=(224, 224, 3))
x = base_model(inputs, training=False)
x = keras.layers.GlobalAveragePooling2D()(x)
x = keras.layers.Dropout(0.3)(x)
outputs = keras.layers.Dense(NUM_CLASSES, activation='softmax')(x)

model = keras.Model(inputs, outputs)
model.compile(
    optimizer=keras.optimizers.Adam(1e-3),
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'],
)
model.fit(train_ds, validation_data=val_ds, epochs=10)
Fine-tuning phase — smaller LR·python
# After feature extraction is converged, unfreeze (some of) the backbone
base_model.trainable = True

# Optional: freeze BatchNormalization layers — they tend to harm fine-tuning
for layer in base_model.layers[:100]:    # freeze first 100 layers
    layer.trainable = False

# Recompile with a much smaller LR — typically 10x smaller
model.compile(
    optimizer=keras.optimizers.Adam(1e-5),
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy'],
)
model.fit(train_ds, validation_data=val_ds, epochs=10)

External links

Exercise

EfficientNetV2S를 CIFAR-10 (또는 가진 10-class 이미지 세트)에 fine-tune. Feature extraction 5 epoch, 그 다음 LR=1e-5로 5 epoch fine-tuning. 각 단계 test accuracy 적어 — fine-tuning 후 명확한 개선 보일 거야.

Progress

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

댓글 0

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

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