Transfer learning & fine-tuning

psy4072·2022년 12월 7일
0

Transfer learning

  • Introduction
    : 한 문제에 대해 학습된 피쳐를 구성하고, 새롭고 유사한 문제에 활용하는 것으로 구성된다.
    예를 들면, 호랑이를 식별하는 방법을 배운 모델의 기능은 고양이를 식별하기 위한 모델을 시작하는 데 유용하다.

✅ transfer learning은 일반적으로 데이터셋에 데이터가 너무 적어 처음부터 전체 모델을 학습 할 수 없는 작업에 대하여 수행한다.

Transfer learning 방법

1️⃣ Take layers from a previously trained model.
2️⃣ Freeze them, so as to avoid destroying any of the information they contain during future training rounds.
3️⃣ Add some new, trainable layers on top of the frozen layers. They will learn to turn the old features into predictions on a new dataset.
4️⃣ Train the new layers on your dataset.

사전에 학습된 모델을 가져온다.
향후 학습 라운드의 정보 파괴를 피하기 위하여 freeze 시킨다.
새로운 layers들을 frozen layers위에 쌓아 올린다.
새로운 layers들은 기존의 feature을 기반으로 새로운 데이터셋에서 학습한다.

✅ 즉, 기존에 학습된 것들은 freeze, 그 위에 새로운 데이터를 추가하여 layer만 학습 시키는 것.

fine-tuning

: 모델의 파라미터를 미세하게 조정하는 행위이다.
이미 존재하는 모델에 추가 데이터를 투입하여 파라미터를 업데이트하는 것을 말한다.

예시

  • 'Cat vs. Dog'
    : 고양이와 강아지 분류기를 만드는 데 다른 데이터로 학습된 모델 (VGG16)을 가져다 쓰는 경우

VGG16 모델의 경우 1000개의 카테고리를 학습시켰기 때문에 고양이와 강아지, 2개의 카테고리만 필요한 우리의 문제를 해결하는데 모든 레이어를 그대로 사용할 수 없다.
➡️ 가장 쉽게 이용하기 위해 나의 데이터를 해당 모델로 predict 하여 bottleneck features만 추출하여 이를 이용한 Fully-connected layer 만 학습시켜서 사용하는 방법을 취한다.

bottleneck features
    : 모델에서 가장 추상화된 feature
    특히 CNN 모델에서 Fully-connected layer 바로 직전 CNN 블록의 output 값

하지만 이 경우는 fine-tuning이 아니다. feature을 추출해내는 레이어의 파라미터를 업데이트 하지 않았기 때문이다.
fine-tuning을 하기 위해서는 기존의 학습이 된 레이어에 나의 데이터를 추가로 학습시켜 파라미터를 업데이트 해야한다.
중요한 점은, 정교하고 미세하게 해야한다.

It's also critical to use a very low learning rate at this stage, because you are training a much larger model than in the first round of training, on a dataset that is typically very small. As a result, you are at risk of overfitting very quickly if you apply large weight updates. Here, you only want to readapt the pretrained weights in an incremental way.

랜덤한 초기 파라미터를 쓴다거나 가장 아래쪽의 레이어 (덜추상화된 레이어)의 파라미터를 학습한다면 오버피팅이 일어나거나 전체 파라미터가 망가지는 문제가 생기기 때문이다.

실습

VGG16

: AlexNet의 8-layers 모델보다 깊이가 2배 이상 깊은 네트워크의 학습에 성공 ( 오차율 16.4 ➡️ 7.3 감소 )

  • 모든 합성곱 레이어에서 3*3 필터 사용하여 쌓아 올림
    ➡️ regularization effect
    ➡️ small filter, deeper networks.
    ➡️ feature의 가로, 세로가 절반으로 줄어들 때, channels의 수를 두배로 늘린다. (Reduction cell)

from tensorflow.keras.applications.vgg16 import VGG16

vgg = VGG16(
	include_top=False, # not include the ImageNet classifier at the top
    weight='imagenet',
    input_shape(width, height, 3)
    )

model = Sequential()
model.add(vgg)
model.add(Flatten())
model.add(Dense(1, activation='sigmoid'))

reference
https://keras.io/guides/transfer_learning/#finetuning
https://deepnote.com/@jhon-smith-flores/Transfer-Learning-864f7d51-84f9-4d43-baa0-6194de7943de
https://haandol.github.io/2016/12/25/define-bottleneck-feature-and-fine-tuning.html
https://medium.com/swlh/essentials-of-convolutional-neural-networks-with-lenet-alexnet-vgg-googlenet-and-resnet-3f9dd477f666

profile
Ⓓ🅰️🅣🄰 ♡♥︎

0개의 댓글