Image Classification fine tunning w ResNetV2

Image Classification fine tuning w ResNetV2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import os 
import keras
import numpy as np
import pandas as pd
from glob import glob
import tensorflow as tf
from tensorflow.keras.utils import load_img, img_to_array
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential, load_model
from keras.layers import GlobalAvgPool2D, Dense, Dropout
from keras.callbacks import EarlyStopping, ModelCheckpoint
from tensorflow.keras.applications import ResNet50V2

# Data Visualization
import plotly.express as px
import matplotlib.pyplot as plt

from zipfile import ZipFile

z = ZipFile('/content/drive/MyDrive/lesson_data/animal_data.zip')
z.extractall()

# Class Names(폴더 이름이 클래스 네임(종류))
root_path = './Animal Classification/Animal Classification/Training Data/'
test_path = './Animal Classification/Animal Classification/Testing Data/'
valid_path = './Animal Classification/Animal Classification/Validation Data/'

class_names = sorted(os.listdir(root_path))
n_classes = len(class_names)


# 이미지 증식(이미지들은 rescale 1/255.를 해줘야함
train_gen = ImageDataGenerator(rescale=1/255., rotation_range=10, horizontal_flip=True)
valid_gen = ImageDataGenerator(rescale=1/255.)
test_gen = ImageDataGenerator(rescale=1/255)

# Load Data
train_ds = train_gen.flow_from_directory(root_path, class_mode='binary', target_size=(256,256), shuffle=True, batch_size=32)
valid_ds = valid_gen.flow_from_directory(valid_path, class_mode='binary', target_size=(256,256), shuffle=True, batch_size=32)
test_ds = test_gen.flow_from_directory(test_path, class_mode='binary', target_size=(256,256), shuffle=True, batch_size=32)

# 0번 GPU를 사용하여 학습
with tf.device("/GPU:0"):
## Pre-Trained Model
base_model = ResNet50V2(input_shape=(256,256,3), include_top=False)
# include_top은 마지막 레이어 클래스를 예측하는 Dense Layer를 사용하지 않는다는 의미입니다.
# 레이어와 이미 학습된 가중치만을 사용한다면 include_top을 False로 해야합니다.

base_model.trainable = False # 기존 레이어의 가중치의 학습 안함

# Model Architecture
name = 'ResNet50V2'#저장할 체크포인트 파일 이름

#ResNet50V2가 이미 깊게 쌓여진 모델이라 다른 레이어를 많이 만들 필요는 없음
model = Sequential([
base_model,
GlobalAvgPool2D(),
Dense(256, activation='relu', kernel_initializer='he_normal'), #가중치 초기화(kenel_initailizer에는 3종류가 있는데 여기서는 HE 초기화를 사용)
Dropout(0.2),
Dense(n_classes, activation='softmax')
], name=name)

# Callbacks
cbs = [EarlyStopping(patience=3, restore_best_weights=True), ModelCheckpoint(name + ".h5", save_best_only=True)]

# Model
opt = tf.keras.optimizers.Adam(learning_rate=2e-3)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

# Model Training
history = model.fit(train_ds, validation_data=valid_ds, callbacks=cbs, epochs=15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
data = pd.DataFrame(history.history)
data

# loss accuracy val_loss val_accuracy
# 0 0.224692 0.922000 0.142165 0.9544
# 1 0.164413 0.943733 0.137304 0.9576
# 2 0.126533 0.957733 0.126196 0.9596
# 3 0.101600 0.964933 0.119228 0.9636
# 4 0.090214 0.970933 0.113578 0.9668
# 5 0.078054 0.974133 0.110474 0.9672
# 6 0.071814 0.977467 0.105573 0.9668
# 7 0.065274 0.981067 0.104021 0.9680
# 8 0.065265 0.979867 0.101604 0.9684
# 9 0.055805 0.982000 0.098761 0.9704
# 10 0.047383 0.986267 0.098213 0.9692
# 11 0.048346 0.983867 0.097358 0.9700
# 12 0.040546 0.987733 0.097479 0.9700
# 13 0.043776 0.987467 0.096769 0.9696
# 14 0.034877 0.990000 0.095712 0.9704
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
model = load_model('./ResNet50V2.h5')
model.summary()
model.evaluate(test_ds)
#0.9559

#이미지 경로 불러오고, 이미지 보여주는 함수입니다.
def load_image(path):
image = tf.cast(tf.image.resize(img_to_array(load_img(path))/255., (256,256)), tf.float32)
return image
def show_image(image, title=None):
plt.imshow(image)
plt.axis('off')
plt.title(title)

#경로 설정
path = './Animal Classification/Animal Classification/Interesting Images/'
interesting_images = [glob(path + name + "/*") for name in class_names]


# 예측 결과(시각화)
for name in class_names:
plt.figure(figsize=(25, 8))
cat_interesting = interesting_images[class_names.index(name)]
for i, i_path in enumerate(cat_interesting):
name = i_path.split("/")[-1].split(".")[0]
image = load_image(i_path)
plt.subplot(1,len(cat_interesting),i+1)

# Model Prediction
org_class = name.title()
preds = model.predict(image[np.newaxis,...])[0]
pred_class = class_names[np.argmax(preds)]
confidence_score = np.round(preds[np.argmax(preds)],2)


# Configure Title
title = f"Pred : {pred_class}\nConfidence : {confidence_score:.2}"

show_image(image, title=title)
plt.show()

결과

Author

InhwanCho

Posted on

2022-12-21

Updated on

2022-12-21

Licensed under

Comments