● CIFAR 10
- 50,000개의 학습 데이터, 10,000개의 테스트 데이터로 구성
- 데이터 복잡도가 MNIST보다 훨씬 높은 특징이 있음
- 신경망이 특징을 검출하기 어려움
1. modules import
import tensorflow as tf
from tensorflow.keras.datasets import cifar10
from tensorflow.keras.layers import Conv2D, MaxPool2D, Dense, Flatten, Input, Dropout, BatchNormalization
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
import numpy as np
2. 데이터 로드 및 전처리
(x_train_full, y_train_full), (x_test, y_test) = cifar10.load_data()
print(x_train_full.shape, y_train_full.shape)
print(x_test.shape, y_test.shape)
# 출력 결과
(50000, 32, 32, 3) (50000, 1)
(10000, 32, 32, 3) (10000, 1)
# 정답 데이터의 값은 레이블로 되어있음
print(y_test[0])
# 출력 결과
[3]
# 예시 데이터
np.random.seed(777)
class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'sheep', 'truck']
sample_size = 9
random_idx = np.random.randint(60000, size = sample_size)
plt.figure(figsize = (5, 5))
for i, idx in enumerate(random_idx):
plt.subplot(3, 3, i + 1)
plt.xticks([])
plt.yticks([])
plt.imshow(x_train_full[i])
plt.xlabel(class_names[int(y_train_full[i])])
plt.show()
- 32 * 32 이미지라 화질이 낮음
# x 데이터 정규화
x_mean = np.mean(x_train_full, axis = (0, 1, 2))
x_std = np.std(x_train_full, axis = (0, 1, 2))
x_train_full = (x_train_full - x_mean) / x_std
x_test = (x_test - x_mean) / x_std
# 학습데이터와 검증데이터 분리
x_train, x_val, y_train, y_val = train_test_split(x_train_full, y_train_full, test_size = 0.3)
# 전처리한 데이터 형태 출력
print(x_train.shape)
print(y_train.shape)
print(x_val.shape)
print(y_val.shape)
print(x_test.shape)
print(y_test.shape)
# 출력 결과
(35000, 32, 32, 3)
(35000, 1)
(15000, 32, 32, 3)
(15000, 1)
(10000, 32, 32, 3)
(10000, 1)
3. 모델 구성 및 컴파일
def model_build():
model = Sequential()
input = Input(shape = (32, 32, 3))
output = Conv2D(filters = 32, kernel_size = 3, padding = 'same', activation = 'relu')(input)
output = MaxPool2D(pool_size = (2, 2), strides = 2, padding = 'same')(output)
output = Conv2D(filters = 64, kernel_size = 3, padding = 'same', activation = 'relu')(output)
output = MaxPool2D(pool_size = (2, 2), strides = 2, padding = 'same')(output)
output = Conv2D(filters = 128, kernel_size = 3, padding = 'same', activation = 'relu')(output)
output = MaxPool2D(pool_size = (2, 2), strides = 2, padding = 'same')(output)
output = Flatten()(output)
output = Dense(256, activation = 'relu')(output)
output = Dense(128, activation = 'relu')(output)
output = Dense(10, activation = 'softmax')(output)
model = Model(inputs = [input], outputs = [output])
model.compile(optimizer = Adam(learning_rate = 1e-4),
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
return model
model = model_build()
model.summary()
# 출력 결과
Model: "model_1"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) [(None, 32, 32, 3)] 0
conv2d_3 (Conv2D) (None, 32, 32, 32) 896
max_pooling2d_3 (MaxPooling (None, 16, 16, 32) 0
2D)
conv2d_4 (Conv2D) (None, 16, 16, 64) 18496
max_pooling2d_4 (MaxPooling (None, 8, 8, 64) 0
2D)
conv2d_5 (Conv2D) (None, 8, 8, 128) 73856
max_pooling2d_5 (MaxPooling (None, 4, 4, 128) 0
2D)
flatten_1 (Flatten) (None, 2048) 0
dense_3 (Dense) (None, 256) 524544
dense_4 (Dense) (None, 128) 32896
dense_5 (Dense) (None, 10) 1290
=================================================================
Total params: 651,978
Trainable params: 651,978
Non-trainable params: 0
_________________________________________________________________
4. 모델 학습 및 평가
history = model.fit(x_train, y_train,
epochs = 30,
batch_size = 256,
validation_data = (x_val, y_val))
5. 학습 과정 시각화
plt.figure(figsize = (12, 4))
plt.subplot(1, 2, 1)
plt.plot(history.history['loss'], 'b--', label = 'loss')
plt.plot(history.history['val_loss'], 'r:', label = 'val_loss')
plt.xlabel('Epochs')
plt.grid()
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history.history['accuracy'], 'b--', label = 'accuracy')
plt.plot(history.history['val_accuracy'], 'r:', label = 'val_accuracy')
plt.xlabel('Epochs')
plt.grid()
plt.legend()
- 해당 모델은 성능이 좋지 않음
- 규제화, 드롭아웃 등 과대적합을 방지하는 기술 필요
def model_build2():
model = Sequential()
input = Input(shape = (32, 32, 3))
output = Conv2D(filters = 32, kernel_size = 3, padding = 'same', activation = 'relu')(input)
output = BatchNormalization()(output)
output = MaxPool2D(pool_size = (2, 2), strides = 2, padding = 'same')(output)
output = Conv2D(filters = 64, kernel_size = 3, padding = 'same', activation = 'relu')(output)
output = BatchNormalization()(output)
output = MaxPool2D(pool_size = (2, 2), strides = 2, padding = 'same')(output)
output = Conv2D(filters = 128, kernel_size = 3, padding = 'same', activation = 'relu')(output)
output = BatchNormalization()(output)
output = MaxPool2D(pool_size = (2, 2), strides = 2, padding = 'same')(output)
output = Dropout(0.5)(output)
output = Flatten()(output)
output = Dense(256, activation = 'relu')(output)
output = Dropout(0.5)(output)
output = Dense(128, activation = 'relu')(output)
output = Dense(10, activation = 'softmax')(output)
model = Model(inputs = [input], outputs = [output])
model.compile(optimizer = Adam(learning_rate = 1e-4),
loss = 'sparse_categorical_crossentropy',
metrics = ['accuracy'])
return model
model2 = model_build2()
model2.summary()
Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_4 (InputLayer) [(None, 32, 32, 3)] 0
conv2d_6 (Conv2D) (None, 32, 32, 32) 896
batch_normalization (BatchN (None, 32, 32, 32) 128
ormalization)
max_pooling2d_6 (MaxPooling (None, 16, 16, 32) 0
2D)
conv2d_7 (Conv2D) (None, 16, 16, 64) 18496
batch_normalization_1 (Batc (None, 16, 16, 64) 256
hNormalization)
max_pooling2d_7 (MaxPooling (None, 8, 8, 64) 0
2D)
conv2d_8 (Conv2D) (None, 8, 8, 128) 73856
batch_normalization_2 (Batc (None, 8, 8, 128) 512
hNormalization)
max_pooling2d_8 (MaxPooling (None, 4, 4, 128) 0
2D)
dropout (Dropout) (None, 4, 4, 128) 0
flatten_2 (Flatten) (None, 2048) 0
dense_6 (Dense) (None, 256) 524544
dropout_1 (Dropout) (None, 256) 0
dense_7 (Dense) (None, 128) 32896
dense_8 (Dense) (None, 10) 1290
=================================================================
Total params: 652,874
Trainable params: 652,426
Non-trainable params: 448
_________________________________________________________________
6. 모델 학습 및 평가
history2 = model2.fit(x_train, y_train,
epochs = 30,
batch_size = 256,
validation_data = (x_val, y_val))
7. 학습 과정 시각화
plt.figure(figsize = (12, 4))
plt.subplot(1, 2, 1)
plt.plot(history2.history['loss'], 'b--', label = 'loss')
plt.plot(history2.history['val_loss'], 'r:', label = 'val_loss')
plt.xlabel('Epochs')
plt.grid()
plt.legend()
plt.subplot(1, 2, 2)
plt.plot(history2.history['accuracy'], 'b--', label = 'accuracy')
plt.plot(history2.history['val_accuracy'], 'r:', label = 'val_accuracy')
plt.xlabel('Epochs')
plt.grid()
plt.legend()
- 검증데이터의 결과가 많이 개선됨
'Python > Deep Learning' 카테고리의 다른 글
[딥러닝-케라스] 케라스 텍스트 처리 및 임베딩(1) (0) | 2023.05.17 |
---|---|
[딥러닝-케라스] 케라스 전이학습 (1) | 2023.05.17 |
[딥러닝-케라스] 케라스 Fashion MNIST CNN 모델 (0) | 2023.05.14 |
[딥러닝-케라스] 케라스 컨볼루션 신경망 (0) | 2023.05.13 |
[딥러닝-텐서플로우] 텐서플로우 Data API (1) | 2023.05.12 |