DEEP LEARNING/Deep Learning Library

DL(딥러닝) : Augmentation로 학습된 AI Transfer Learning & Fine Tunning

신강희 2024. 5. 1. 12:42
728x90

<Transfer Learning & Fine Tunning>

# 전이 학습(Transfer Learning)은 기계 학습에서 사용되는 기술 중 하나로, 한 작업에서 학습한 모델의 지식을 다른 관련 작업에 적용하는 것을 의미

 

# 세밀 조정(Fine-tuning)은 전이 학습의 한 형태로, 미리 학습된 모델을 가져온 후 일부 레이어의 가중치를 고정시키고 일부 레이어의 가중치만 업데이트하여 새로운 작업에 맞게 모델을 미세 조정하는 과정으로, 이를 통해 새로운 작업에 더 잘 맞는 모델을 얻을 수 있다.

 

<개와 고양이 분류를, 이미 잘 만들어진 뉴널네트워크를 활용하여, 성능을 올려보자>

 

Stage 1: Install dependencies and setting up GPU environment

# 구글 Colab 실행시 우측 상단에 화살표를 눌러 런타임 유형을 변경할수 있다.

 

 

# 유료 버전이라면 더 성능좋은 GPU를 선택하면 되고, 무료 버전이라면 T4 GPU로 설정 후 진행.

# GPU 환경이 아니라면 10분이면 끝날 작업을 1시간 동안 돌려야 할수도 있음

 

Downloading the Dogs vs Cats dataset

!wget --no-check-certificate \
    -O ./cats_and_dogs_filtered.zip

 

--2024-04-19 00:42:54-- https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip Resolving storage.googleapis.com (storage.googleapis.com)... 74.125.199.207, 74.125.142.207, 74.125.20.207, ... Connecting to storage.googleapis.com (storage.googleapis.com)|74.125.199.207|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 68606236 (65M) [application/zip] Saving to: ‘./cats_and_dogs_filtered.zip’ ./cats_and_dogs_fil 100%[===================>] 65.43M 151MB/s in 0.4s 2024-04-19 00:42:54 (151 MB/s) - ‘./cats_and_dogs_filtered.zip’ saved [68606236/68606236]

 

Stage 2: Dataset preprocessing

Import project dependencies

import os
import zipfile
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt

# 파이썬의 진행상태를 표시해 주는 라이브러리
 
from tqdm import tqdm_notebook

from keras.preprocessing.image import ImageDataGenerator
 
tf.__version__

 

'2.15.0'

 

Unzipping the Dogs vs Cats dataset

file = zipfile.ZipFile('./cats_and_dogs_filtered.zip')
file.extractall()

 

Seting up dataset paths

train_dir = '/content/cats_and_dogs_filtered/train'
val_dir = '/content/cats_and_dogs_filtered/validation'

 

Building the model : MobileNetV2 를 활용

https://www.tensorflow.org/api_docs/python/tf/keras/applications/MobileNetV2?_gl=1*i1rpm7*_up*MQ..*_ga*NjIyNjkyMDk5LjE3MTM0ODc4NDY.*_ga_W0YLR4190T*MTcxMzQ4Nzg0NS4xLjAuMTcxMzQ4ODE1Ny4wLjAuMA..#expandable-1

 

tf.keras.applications.MobileNetV2  |  TensorFlow v2.16.1

Instantiates the MobileNetV2 architecture.

www.tensorflow.org

 

Loading the pre-trained model (MobileNetV2)

# 모바일이나, 임베디드에서도 실시간을 작동할 수 있게 모델이 경량화 되면서도, 정확도 또한 많이 떨어지지 않게하여, 속도와 정확도 사이의 트레이드 오프 문제를 어느정도 해결한 네트워크 입니다.

 

# 우리가 만들려는 모델의 인풋 이미지는 128행 128열 컬러이미지(R,G,B = 3)로 한다.
IMG_SHAPE = (128, 128, 3)

 

# 트랜스퍼 러닝은,
# 학습이 잘 된 모델을 가져와서, 우리의 문제에 맞게 활용하는 것이므로,
# 학습이 잘 된 모델의 베이스 모델만 가져온다. (헤드모델은 빼고!)
base_model = tf.keras.applications.MobileNetV2(input_shape = IMG_SHAPE,
                                               include_top = False,
                                               weights = 'imagenet')

 

Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/mobilenet_v2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_128_no_top.h5 9406464/9406464 [==============================] - 0s 0us/step

 

base_model.summary()

 

Freezing the base model

# 잘만들어진 베이스 모델 부분은 이미 1400만장으로 학습이 잘 되어있어서
# 이미지의 특징을 뽑아내는 역할을 하는 부분이므로
# 우리 데이터로는 학습 되지 않도록 한다 (프로즌 레이어스)
base_model.trainable = False
base_model.summary()

 

Defining the custom head for our network

from keras.layers import Flatten, Dense
head_model = base_model.output
# ()() 연속해서 쓰는 이유는 텐서플로우에서 이렇게 함수용으로 쓰라고 메뉴얼로 정한것이라 따로 문법적인건 아니다. 그냥 외워야 할듯.
 
head_model = Flatten()(head_model)
head_model = Dense(128, 'relu')(head_model)
head_model = Dense(1, 'sigmoid')(head_model)

 

Defining the model

# 위의 두 모델을 하나의 모델로 합친다.
from keras.models import Model
model = Model(inputs = base_model.input, outputs = head_model)
model.summary()

 

Compiling the model

from keras.optimizers import RMSprop
model.compile(RMSprop(0.0001), loss='binary_crossentropy', metrics= ['accuracy'])

 

Creating Data Generators

Resizing images

For example: MobileNet (architecture that we use) supports: (96, 96), (128, 128), (160, 160), (192, 192), (224, 224).

 

train_datagen = ImageDataGenerator(rescale=1/255,
                   zoom_range=0.2,
                   width_shift_range=0.2,
                   height_shift_range=0.2)
train_generator = train_datagen.flow_from_directory(train_dir,
                                  target_size= (128, 128),
                                  class_mode='binary')

Found 2000 images belonging to 2 classes.

 

val_datagen = ImageDataGenerator(rescale=1/255)
val_generator = val_datagen.flow_from_directory(val_dir,
                                target_size=(128,128),
                                class_mode='binary')

Found 1000 images belonging to 2 classes.

 

Training the model

epoch_history = model.fit(train_generator,
                          epochs=5,
                          validation_data= val_generator)

 

Transfer learning model evaluation

import numpy as np
from google.colab import files
from keras.preprocessing import image

uploaded = files.upload()

for fn in uploaded.keys():

  # predicting images
  path = '/content/' + fn
  img = image.load_img(path, target_size=(128,128))
  x = image.img_to_array(img)
  x = np.expand_dims(x, axis=0)

  print(x)

  images = np.vstack([x])

  images = images/255

  # print(images)

  classes = model.predict(images, batch_size=10)
  print(classes[0])
  if classes[0]>0.5:
    print(fn + " is a dog")
  else:
    print(fn + " is a cat")

 

# 이제 임의의 강아지 혹은 고양이 파일을 첨부해 보면 예상한 결과가 출력된다.

# 현재 내옆에서 자고있는 우리집 강아지 사진을 카카오톡으로 다운받아 업로드 해보자.

 

ㄴ 강아지로 예측한걸 볼수있다.

 

< Fine tuning >

There are a few pointers:

  • DO NOT use Fine tuning on the whole network; only a few top layers are enough. In most cases, they are more specialized. The goal of the Fine-tuning is to adopt that specific part of the network for our custom (new) dataset.
  • Start with the fine tunning AFTER you have finished with transfer learning step. If we try to perform Fine tuning immediately, gradients will be much different between our custom head layer and a few unfrozen layers from the base model.

Un-freeze a few top layers from the model

# 파인 튜닝은 트랜스퍼 러닝을 한 다음에 하는 방법이다.
# 위의 트랜스퍼 러닝을 한 후에, 조금더 개선이 가능한지 해보는 방법으로서
# 학습된 모델 그 상태에서 추가로 학습을 시키되
# 좋은 모델의 일부분을, 우리데이터로 학습 가능토록 변경한 후에 학습시키는 방법
model.summary()

 

# 1) 베이스 모델 전체 레이어를 학습 가능토록 일괄 바꾼다.
base_model.trainable = True

 

# 2) 베이스 모델의 전체 레이어수를 확인한다.
len( base_model.layers )
154

 

# 3) 위에서 레이어수 확인했으니까,
#    몇번째 레이어까지 학습이 안되도록 할것인지 결정해준다.
#    그러면, 나머지 레이어는 학습이 가능
# 레이어 101 부터 학습이 가능토록 하자.
for layer in base_model.layers[0 : 100+1] :
  layer.trainable = False
base_model.summary()

 

Compiling the model for fine-tuning

model.compile(RMSprop(0.0001), loss='binary_crossentropy', metrics=['accuracy'])

 

Fine tuning

model.fit(train_generator,
          epochs = 5,
          validation_data= val_generator)
Epoch 1/5
63/63 [==============================] - 22s 212ms/step - loss: 0.2129 - accuracy: 0.9315 - val_loss: 0.0966 - val_accuracy: 0.9660
Epoch 2/5
63/63 [==============================] - 13s 200ms/step - loss: 0.1638 - accuracy: 0.9330 - val_loss: 0.1468 - val_accuracy: 0.9570
Epoch 3/5
63/63 [==============================] - 13s 204ms/step - loss: 0.1087 - accuracy: 0.9585 - val_loss: 0.1173 - val_accuracy: 0.9640
Epoch 4/5
63/63 [==============================] - 13s 202ms/step - loss: 0.1065 - accuracy: 0.9610 - val_loss: 0.1868 - val_accuracy: 0.9510
Epoch 5/5
63/63 [==============================] - 13s 204ms/step - loss: 0.0739 - accuracy: 0.9785 - val_loss: 0.1452 - val_accuracy: 0.9670

ㄴ Fine tunig 까지 완료

 

딥러닝 마무리 #

반응형