본문 바로가기
Visual Intelligence/Image Deep Learning

[시각 지능] CIFAR-10

by goatlab 2022. 8. 7.
728x90
반응형
SMALL

CIFAR-10

 

 

CIFAR -10 데이터 세트 (Canadian Institute For Advanced Research)는 기계 학습 및 컴퓨터 비전 알고리즘을 훈련하는 데 일반적으로 사용되는 이미지 모음이다. 머신러닝 연구에 가장 널리 사용되는 데이터 세트 중 하나이다. CIFAR-10 데이터 세트에는 10개의 다른 클래스에 60,000개의 32x32 컬러 이미지가 포함되어 있다. 10개의 다른 클래스는 비행기, 자동차, 새, 고양이, 사슴, 개, 개구리, 말, 배 및 트럭을 나타낸다. 각 클래스에는 6,000개의 이미지가 있다.

 

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

from tensorflow.keras.datasets import cifar10
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D
from tensorflow.keras.layers import Flatten, Dense, Dropout
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping

(x_train, y_train), (x_test, y_test) = cifar10.load_data()

x_train=x_train.reshape(-1, 32, 32, 3)
x_test=x_test.reshape(-1, 32, 32, 3)

print(x_train.shape, x_test.shape)
print(y_train.shape, y_test.shape)

x_train = x_train.astype(np.float32) / 255.0
x_test = x_test.astype(np.float32) / 255.0
Downloading data from https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170500096/170498071 [==============================] - 11s 0us/step
170508288/170498071 [==============================] - 11s 0us/step
(50000, 32, 32, 3) (10000, 32, 32, 3)
(50000, 1) (10000, 1)
plt.figure(figsize = (6, 6))

for index in range(25):
  plt.subplot(5, 5, index + 1)
  plt.imshow(x_train[index])
  plt.axis('off')

plt.show()

model = Sequential()

model.add(Conv2D(input_shape=(32,32,3), kernel_size=(3,3), filters=32, activation='relu', padding='same'))
model.add(Conv2D(kernel_size=(3,3), filters=32, activation='relu', padding='same'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(kernel_size=(3,3), filters=64, activation='relu', padding='same'))
model.add(Conv2D(kernel_size=(3,3), filters=64, activation='relu', padding='same'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(kernel_size=(3,3), filters=128, activation='relu', padding='same'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(kernel_size=(3,3), filters=128, activation='relu', padding='same'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Conv2D(kernel_size=(3,3), filters=256, activation='relu', padding='same'))
model.add(MaxPool2D(pool_size=(2,2)))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))

model.compile(loss='sparse_categorical_crossentropy',
            optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy'])

model.summary()
Model: "sequential_8"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_21 (Conv2D)          (None, 32, 32, 32)        896       
                                                                 
 conv2d_22 (Conv2D)          (None, 32, 32, 32)        9248      
                                                                 
 max_pooling2d_15 (MaxPoolin  (None, 16, 16, 32)       0         
 g2D)                                                            
                                                                 
 dropout_17 (Dropout)        (None, 16, 16, 32)        0         
                                                                 
 conv2d_23 (Conv2D)          (None, 16, 16, 64)        18496     
                                                                 
 conv2d_24 (Conv2D)          (None, 16, 16, 64)        36928     
                                                                 
 max_pooling2d_16 (MaxPoolin  (None, 8, 8, 64)         0         
 g2D)                                                            
                                                                 
 dropout_18 (Dropout)        (None, 8, 8, 64)          0         
                                                                 
 conv2d_25 (Conv2D)          (None, 8, 8, 128)         73856     
                                                                 
 max_pooling2d_17 (MaxPoolin  (None, 4, 4, 128)        0         
 g2D)                                                            
                                                                 
 dropout_19 (Dropout)        (None, 4, 4, 128)         0         
                                                                 
 conv2d_26 (Conv2D)          (None, 4, 4, 128)         147584    
                                                                 
 max_pooling2d_18 (MaxPoolin  (None, 2, 2, 128)        0         
 g2D)                                                            
                                                                 
 dropout_20 (Dropout)        (None, 2, 2, 128)         0         
                                                                 
 conv2d_27 (Conv2D)          (None, 2, 2, 256)         295168    
                                                                 
 max_pooling2d_19 (MaxPoolin  (None, 1, 1, 256)        0         
 g2D)                                                            
                                                                 
 dropout_21 (Dropout)        (None, 1, 1, 256)         0         
                                                                 
 flatten_6 (Flatten)         (None, 256)               0         
                                                                 
 dense_8 (Dense)             (None, 256)               65792     
                                                                 
 dropout_22 (Dropout)        (None, 256)               0         
                                                                 
 dense_9 (Dense)             (None, 128)               32896     
                                                                 
 dropout_23 (Dropout)        (None, 128)               0         
                                                                 
 dense_10 (Dense)            (None, 10)                1290      
                                                                 
=================================================================
Total params: 682,154
Trainable params: 682,154
Non-trainable params: 0
_________________________________________________________________
file_path = './modelchpoint_test.h5' # 파일 저장 경로

checkpoint = ModelCheckpoint(file_path,
                             monitor = 'val_loss', # val_loss 값이 개선되었을 때 호출
                             verbose = 1, # log 출력
                             save_best_only = True, # best 값만 저장
                             mode = 'auto') # 자동으로 best를 찾음

stopping = EarlyStopping(monitor = 'val_loss', # val_loss를 관찰
                         patience = 5) # 5 epoch동안 개선되지 않으면 조기종료

from datetime import datetime

start_time = datetime.now()

hist = model.fit(x_train, y_train, batch_size = 128, epochs = 100, validation_data = (x_test, y_test), callbacks = [checkpoint])

end_time = datetime.now()

print('elapsed time => ', end_time-start_time)
model.evaluate(x_test, y_test)
plt.plot(hist.history['accuracy'])
plt.plot(hist.history['val_accuracy'])
plt.title('Accuracy Trend')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train','validation'], loc='best')
plt.grid()
plt.show()

plt.plot(hist.history['loss'])
plt.plot(hist.history['val_loss'])
plt.title('Loss Trend')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train','validation'], loc='best')
plt.grid()
plt.show()

728x90
반응형
LIST