C.W.K.
Stream
Lesson 03 of 07 · published

Feature extraction

~8 min · transfer

Level 0Keras 도제
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Step 1: base 는 freeze, 새 head 만 학습

feature extraction 은 transfer 의 가장 안전하고 빠른 형태야. base_model.trainable = False 로 pretrained weight 전부 잠그고, 위에 작은 trainable head 만 얹어. head 만 배우니까 network 대부분은 gradient 자체를 안 계산해 — 학습 싸고, GPU 적게 먹고, pretrained feature 를 망칠 방법이 아예 없어. 항상 이게 *첫* 수야. fine-tuning (다음 lesson) 은 이 baseline 이 plateau 칠 때만 그 복잡함이 값을 해.

사람들이 자주 걸리는 두 지점

head 는 일부러 작게: GlobalAveragePooling2D 가 backbone feature map 을 이미지당 vector 하나로 압축하고, Dropout 이 작은 dataset overfitting 을 막고, class 수에 맞춘 Dense 하나가 예측해. 아래 Code section 에서 안 뻔한 두 가지가 값을 해:

  • base 호출할 때 training=False — BatchNorm layer 를 inference mode 로 둬서, weight 가 freeze 된 동안 내 데이터로 running 통계가 흔들리지 않게 해.
  • Sequential 로 감싸는 대신 functional keras.Input 형태 — frozen base / trainable head 경계를 명시적으로 만들고, fine-tuning lesson 이 이 구조 위에 쌓여.

Code

frozen backbone + custom head (feature extraction)·python
# Freeze the pretrained weights
base_model.trainable = False

# Add a custom classification head
inputs = keras.Input(shape=(224, 224, 3))
x = base_model(inputs, training=False)  # Keep BN in inference mode
x = layers.GlobalAveragePooling2D()(x)
x = layers.Dropout(0.2)(x)
outputs = layers.Dense(5, activation="softmax")(x)

model = keras.Model(inputs, outputs)
model.compile(
    optimizer="adam",
    loss="categorical_crossentropy",
    metrics=["accuracy"],
)

# Train only the head (~5-10 epochs)
model.fit(train_ds, epochs=10, validation_data=val_ds)

External links

Exercise

frozen ResNet50 + custom head 로 binary classifier (cat vs dog) 짜. 작은 subset (1000 image) 으로 5 epoch 학습. from-scratch weight 의 같은 architecture 와 비교 — gap 확인.

Progress

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

댓글 0

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

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