λ°μν
Transfer Learning, μ¦ μ μ΄ νμ΅μ ML(λ¨Έμ λ¬λ)κ³Ό DL(λ₯λ¬λ)μμ κΈ°μ‘΄μ Pre-Training λ λͺ¨λΈμ μλ‘μ΄ μμ μ μ¬μ¬μ©νλ κΈ°λ²μ λλ€.
- μ΄ λ°©λ²μ νΉν λκ·λͺ¨ λ°μ΄ν°μ μμ νμ΅λ λͺ¨λΈμ μμ λ°μ΄ν°μ μ μ μ©ν λ μ μ©ν©λλ€.
- μ μ΄ νμ΅μ λͺ¨λΈμ΄ μ¬μ νμ΅ν μ§μμ μλ‘μ΄ λ¬Έμ μ μ μ©νμ¬ νμ΅ μλλ₯Ό λμ΄κ³ μ±λ₯μ ν₯μμν¬ μ μμ΅λλ€.
Transfer Learning (μ μ΄ νμ΅)
- κΈ°μ‘΄μ Neural Network(μ κ²½λ§)μμ μ΅μμ λΆλΆμ μλ‘ μ μν λ€μ, μ΄ λΆλΆμ Training μν€λ κ²μ΄ Transfer Learning (μ μ΄ νμ΅) μ΄λΌκ³ ν©λλ€.
- μ΄λ Neural Network(μ κ²½λ§)μ νμ λΆλΆμ μ΄λ―Έ Trainingλ Neural Network(μ κ²½λ§)μ μ¬μ©νλ―λ‘ μ μ μμ λ°μ΄ν°λ‘ μμ€ν μ Training μν¬ μ μμ΅λλ€.
Transfer Learning (μ μ΄ νμ΅)μ μ£Όμ κ°λ
- μ¬μ νμ΅λ λͺ¨λΈ
- λκ·λͺ¨ λ°μ΄ν°μ , μλ₯Ό λ€μ΄ ImageNetκ³Ό κ°μ Trainingλ λͺ¨λΈμ μ¬μ©ν©λλ€.
- μ΄λ¬ν λͺ¨λΈμ λ€μν κ°μ²΄λ ν¨ν΄μ μΈμνλ λ° λ§€μ° λ₯μν©λλ€.
- λ―ΈμΈ μ‘°μ (Fine-Tuning)
- μ¬μ νμ΅λ λͺ¨λΈμ μΌλΆ Layerλ₯Ό κ³ μ (Freeze)νκ³ , λλ¨Έμ§ Layerλ₯Ό μλ‘μ΄ λ°μ΄ν°μ μΌλ‘ νμ΅ν©λλ€.
- κ³ μ λ λ μ΄μ΄λ μ΄μ μμ μμ Trainingν νΉμ§μ μ μ§ν©λλ€.
- μλ‘μ΄ λ°μ΄ν°μ μμ νμ΅λ Layerλ μλ‘μ΄ μμ μ λ§κ² μ‘°μ λ©λλ€.
- κ³ μ λ νΉμ§ μΆμΆκΈ°(Feature Extractor)
- Pre-Trainλ λͺ¨λΈμ λͺ¨λ Layerλ₯Ό κ³ μ νκ³ , λ§μ§λ§μ μλ‘μ΄ λΆλ₯κΈ°λ₯Ό μΆκ°νμ¬ Trainingν©λλ€.
- Pre-Trainλ λͺ¨λΈμ μλ‘μ΄ Datasetμ Feature(νΉμ§)μ μΆμΆνκ³ , μλ‘μ΄ λΆλ₯κΈ°λ μ΄λ¬ν νΉμ§μ κΈ°λ°μΌλ‘ Predict ν©λλ€.
Transfer Learning (μ μ΄ νμ΅)μ μ₯μ
- νμ΅ μκ° λ¨μΆ
- μ΄κΈ° Weightλ₯Ό μ νμ΅λ λͺ¨λΈλ‘λΆν° κ°μ Έμ€κΈ° λλ¬Έμ Training μκ°μ΄ λ¨μΆλ©λλ€.
- λ μ μ λ°μ΄ν° νμ
- μμ Datasetμμλ λμ μ±λ₯μ λ¬μ±ν μ μμ΅λλ€.
- λκ·λͺ¨ Datasetμμ Trainλ λͺ¨λΈμ μ§μμ νμ©νκΈ° λλ¬Έμ λ μ μ μμ Dataλ‘λ μ’μ μ±λ₯μ λΌ μ μμ΅λλ€.
- μ±λ₯ ν₯μ
- κΈ°μ‘΄ λͺ¨λΈμ Feature(νΉμ§) μΆμΆ λ₯λ ₯μ νμ©νμ¬ μλ‘μ΄ μμ μμλ λμ μ±λ₯μ κΈ°λν μ μμ΅λλ€.
CNN Transfer Learning (CNN μ μ΄νμ΅)
Keras λΌμ΄λΈλ¬λ¦¬μμ μ μλμ΄ μλ CNN ν¨ν€μ§λ€μ μ¬μ©ν μ μμ΅λλ€.
Transfer Learning Example (μ μ΄ νμ΅ μμ by TF, Keras)
νλ² Transfer Learningμ μμ μ½λλ₯Ό νλ² λ΄λ³΄κ² μ΅λλ€.
- μ΄μ½λλ Tensorflowμ Kears λΌμ΄λΈλ¬λ¦¬λ₯Ό μ¬μ©νμ¬ Transfer Learningμ μννλ μμμ λλ€.
Pre-Trainλ λͺ¨λΈ λ‘λ λ° μ€λΉ
import tensorflow as tf
from tensorflow.keras.applications import VGG16
from tensorflow.keras.layers import Dense, Flatten
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
# μ¬μ νμ΅λ VGG16 λͺ¨λΈ λ‘λ (ImageNet λ°μ΄ν°μ
μμ νμ΅λ¨)
base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
# μ¬μ νμ΅λ λͺ¨λΈμ λ μ΄μ΄ κ³ μ (Feature Extractorλ‘ μ¬μ©)
for layer in base_model.layers:
layer.trainable = False
# μλ‘μ΄ λΆλ₯κΈ° λ μ΄μ΄ μΆκ°
x = base_model.output
x = Flatten()(x)
x = Dense(512, activation='relu')(x)
predictions = Dense(10, activation='softmax')(x)
# μ 체 λͺ¨λΈ μ μ
model = Model(inputs=base_model.input, outputs=predictions)
# λͺ¨λΈ μ»΄νμΌ
model.compile(optimizer=Adam(learning_rate=0.0001), loss='categorical_crossentropy', metrics=['accuracy'])
Data μ€λΉ
λ°μ΄ν°μ μ μ€λΉνκ³ , μ΄λ―Έμ§ λ°μ΄ν°λ₯Ό λΆλ¬μ€κ³ , μ μ²λ¦¬νλ κ³Όμ μ ν¬ν¨ν©λλ€.
from tensorflow.keras.preprocessing.image import ImageDataGenerator
# λ°μ΄ν°μ
κ²½λ‘
train_data_dir = '/path/to/train/data'
validation_data_dir = '/path/to/validation/data'
# μ΄λ―Έμ§ λ°μ΄ν° μ λλ μ΄ν° μ μ λ° λ°μ΄ν° μ μ²λ¦¬
train_datagen = ImageDataGenerator(rescale=1.0/255.0, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)
validation_datagen = ImageDataGenerator(rescale=1.0/255.0)
# νμ΅ λ°μ΄ν° μ λλ μ΄ν°
train_generator = train_datagen.flow_from_directory(train_data_dir, target_size=(224, 224), batch_size=32, class_mode='categorical')
# κ²μ¦ λ°μ΄ν° μ λλ μ΄ν°
validation_generator = validation_datagen.flow_from_directory(validation_data_dir, target_size=(224, 224), batch_size=32, class_mode='categorical')
λͺ¨λΈ νμ΅
# λͺ¨λΈ νμ΅
model.fit(train_generator, epochs=10, validation_data=validation_generator)
- μ¬μ νμ΅λ VGG16 λͺ¨λΈμ μ¬μ©νμ¬ μλ‘μ΄ λ°μ΄ν°μ μ λν΄ μ μ΄ νμ΅μ μννλ λ°©λ²μ 보μ¬μ€λλ€.
λ°μν