본문 바로가기

Deep Learning

Deep Learning : Transfer Leaning을 하기위한 코드와 설명 / 에포크 시마다, 기록을 남길 수 있는, CSVLogger 사용방법(작성중)


Transfer Leaning 설명


딥러닝 관점에서, 이미지 분류 문제는 전이 학습(Transfer Learning) 으로 접근할 수 있다. 사실, 최상위 랭크를 기록하는 몇몇 이미지 분류 결과들은 전이학습을 이용해 만들어낸 결과들이기도하다.

기존의 만들어진 모델을 사용하여 새로운 모델을 만들시 학습을 빠르게 하며, 예측을 더 높이는 방법입니다.

 


 

Transfer Leaning에 관한 이미지 참조


학습하기

Topic : Downloading the Dogs vs Cats dataset

[ 파일설치 ]

In : 

!wget --no-check-certificate \
    https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip \
    -O ./cats_and_dogs_filtered.zip

 

Out : 

--2022-06-16 04:10:54--  https://storage.googleapis.com/mledu-datasets/cats_and_dogs_filtered.zip
Resolving storage.googleapis.com (storage.googleapis.com)... 142.251.6.128, 142.251.120.128, 142.251.161.128, ...
Connecting to storage.googleapis.com (storage.googleapis.com)|142.251.6.128|: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   152MB/s    in 0.4s    

2022-06-16 04:10:55 (152 MB/s) - ‘./cats_and_dogs_filtered.zip’ saved [68606236/68606236]

 

 

알집 불러오기 

In : 

import zipfile

zip_ref = zipfile.ZipFile('/content/cats_and_dogs_filtered.zip')

 

# 현재 경로에 압축 푸는 방법

zip_ref.extractall('./')

 


Stage 2: Dataset preprocessing

- Import project dependencies

 

In :

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

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

from tensorflow.keras.preprocessing.image import ImageDataGenerator

%matplotlib inline
tf.__version__

 

Out : 2.8.2


Seting up dataset paths

데이터셋 세팅하기

train_dir = '/content/cats_and_dogs_filtered/train'

validation_dir = '/content/cats_and_dogs_filtered/validation'

train_dir = '/content/cats_and_dogs_filtered/train'

validation_dir = '/content/cats_and_dogs_filtered/validation'

 

 


# fine tuning은 트랜스퍼러닝을 한 상태에서 수행하는 방법이다.
# 1. 베이스 모델의 전체 레이어를 학습 가능토록 먼저 바꿔준다.

# 2. 베이스 모델의 전체 레이어 수를 확인한다.

# 3. 몇번째 레이어까지 학습이 안되게할지 결정해야 한다.
 
# 4. 베이스 모델의 첫번째 레이어부터 우리가 정한 레이어까지는 학습이 안되도록 설정해준다.