KT AIVLE SCHOOL/교육 받은 것들

시각지능 딥러닝 (2) - Image Data Augmentation(2) -

y0.0ns 2023. 3. 18. 16:52

Image Data Augmentation을 통해서 cifar100 데이터셋의 train data를 증강하여 학습시켜 보았다.

 

1. 데이터셋을 불러온다.

from tensorflow.datasets.cifar100 import load_data
(x_train, y_train), (x_test, y_test) = load_data()

 

2. 불러온 데이터의 shape를 확인

x_train.shape, x_test.shape, y_train.shape, y_test.shape

 

3. Validation_set 생성

from sklearn.model_selection import train_test_split
x_train, x_val, y_train, y_val = train_test_split(x_train, x_test, test_size=0.2, random_states=2023)

 

4. Min-Max Scaling

x_max = x_train.max()
x_min = x_train.min()
x_train = x_train / x_max
x_val = x_val / x_max
x_test = x_test / x_max
# 정석적인 식은 (x_train - x_min) / (x_max - x_min)이지만 x_min값이 0이기 때문에 x_min 생략

 

5. One-Hot Encoding

import numpy as np
from sklearn.utils import to_categorycal

class_n = len(np.unique(y_train))	# 클래스의 수
y_train = to_categorycal(y_train, class_n)
y_val = to_categorycal(y_val, class_n)
y_test = to_categorycal(y_test, calss_n)

y_train.shape, y_val.shape, y_test.shape	# 결과 확인

 

6. Data Augmentation

from tensorflow.keras.preprocessing.image import ImageDataGenerator

dataGen = ImageDataGenerator(rotation_range=20,
							 zomm_range=0.15,
                             width_shift_range=0.05,
                             height_shift_range=0.05,
                             horizontal_flip=True,
                             vertical_flip=True)
dataGen.fit(x_train)	# 정규화
train_gen = dataGen.flow(x_train, y_train)

 

7. 모델링

import tensorflow as tf
from tensorflow import keras
from keras.layers import *

# 1. 세션 클리어
keras.backend.clear_session()

# 2. layer 조립
il = Input(shape=(32, 32, 3))

cl = Conv2D(filters=32,
			kernel_size=(3, 3),
            padding='same',
            activation='relu')(il)
cl = Conv2D(filters=32,
			kernel_size=(3, 3),
            padding='same',
            activation='relu')(cl)
pl = MaxPool2D(pool_size=(2, 2), strides=(2, 2)(cl)

cl = Conv2D(filters=64,
			kernel_size=(3, 3),
            padding='same',
            activation='relu')(pl)
cl = Conv2D(filters=64,
			kernel_size=(3, 3),
            padding='same',
            activation='relu')(cl)
pl = MaxPool2D(pool_size=(2, 2), strides=(2, 2)(cl)

fl = Flatten()(pl)
dl = Dropout(0.2)(fl)
ol = Dense(100, activation='softmax')(dl)

# 3. 모델 선언
model = keras.models.Model(il, ol)

# 4. 컴파일
model.compile(loss='categorical_crossentropy', metrics=['accuracy'], optimizer='adam')

# 5. 요약
model.summary()

 

8. 학습

from keras.callbacks import EarlyStopping

es = EarlyStopping(monitor='val_loss',
				   min_delta=0,
				   patience=5,
                   verbose=1,
                   restore_best_weights=True)

model.fit(train_gen, epochs=1000, verbose=1, callbacks=[es], validation_data(x_val, y_val))

y_pred = model.predict(x_test)

 

9. 성능 평가 

form sklearn.metrics import accuracy_score

single_y_pred = y_pred.argmax(axis=1)
single_y_test = y_test.argmax(axis=1)
accuracy_score(single_y_test, single_y_pred)

 

10. 시각화 

import random as rd
import matplotlib.pyplot as plt
rand_n = rd.randrange(0, single_y_pred.shape[0])

print(f'id : {rand_n}')
print(f'실제 카테고리 : {single_y_test[rand_n]}')
print(f'모델 예측 : {single_y_pred[rand_n]}')

if single_y_test[rand_n] == single_y_pred[rand_n] :
	print('예측 성공')
else :
	print('예측 실패')

print('======================================')
print(f'모델의 카테고리별 확률 :\n{np.floor(y_pred[rand_n]*100)}')
print('======================================')

plt.figure(figsize=(5, 6))
plt.title(f'Category Index {single_y_test[rand_n]}')
plt.imshow(x_test[rand_n].reshape(32, 32, 3))
plt.show()

 

11. 결과

id : 8274
실제 카테고리 인덱스 : 28
모델의 카테고리 인덱스 예측 : 28
정답
======================================
모델의 카테고리별 확률 : 
[ 0.  0.  0.  0.  0.  0.  0.  0.  0.  8.  0.  3.  0.  0.  0.  1.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. 22.  1.  0.  0.  0.  0.  7.  3.
  0.  0.  0.  0.  3.  0.  0.  0.  0.  0.  2.  0.  0.  0.  0.  1.  0.  0.
  1.  0.  7.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0.  0. 11.  0.  0.
  0.  0.  0.  0.  0.  0.  0.  0.  1.  0.  0.  0.  2.  0.  0.  0.  3.  0.
  0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]
======================================