MLDL_์ •๋ฆฌ/Sample

[DL] CIFAR-10

KimTory 2022. 3. 22. 23:40

๐Ÿ™‹‍โ™‚๏ธ CIFAR-10 ๊ตฌ์„ฑ

CIFAR-10 Data Set์€ 32 x 32 ํฌ๊ธฐ์˜ 60,000๊ฐœ์˜ Image Set์œผ๋กœ ๊ตฌ์„ฑ ๋˜์–ด ์žˆ์œผ๋ฉฐ,

10๊ฐœ์˜ Class๋กœ ๋ถ„๋ฅ˜ ๋œ๋‹ค.๊ฐ Class๋Š” 60,000๊ฐœ์˜ ์ „์ฒด ์ด๋ฏธ์ง€์™€ 50,000๊ฐœ์˜ Train Image, 10,000๊ฐœ์˜ Test Image๋กœ ๊ตฌ์„ฑ

(Labels๋Š” ๋™์ผ)

→ Mnist๋ณด๋‹ค ๊ฐ€๋ณ๊ณ , ์‹œ๊ฐ„์ด ๋œ ์†Œ์š”๋จ

 

โœ Data Set Load / Labels ํ™•์ธ

# Default import
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import os

from tensorflow.keras.datasets import cifar10

# ์ „์ฒด 6๋งŒ๊ฐœ ๋ฐ์ดํ„ฐ ์ค‘, 5๋งŒ๊ฐœ๋Š” ํ•™์Šต ๋ฐ์ดํ„ฐ์šฉ, 1๋งŒ๊ฐœ๋Š” ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ์šฉ์œผ๋กœ ๋ถ„๋ฆฌ
(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()
print("train dataset shape:", train_images.shape, train_labels.shape)
print("test dataset shape:", test_images.shape, test_labels.shape)

# labels ํ™•์ธ
NAMES = np.array(['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck'])
print(train_labels[:10])

# result
[6. 9. 9. 4. 1. 1. 2. 7. 8. 3.]

 

โœ Data Set / Preprocessing

# Image Set ์‹œ๊ฐํ™”

import matplotlib.pyplot as plt
import cv2
%matplotlib inline 

def show_images(images, labels, ncols=8):
    figure, axs = plt.subplots(figsize=(22, 6), nrows=1, ncols=ncols)
    for i in range(ncols):
        axs[i].imshow(images[i])
        label = labels[i].squeeze()
        axs[i].set_title(NAMES[int(label)])
        
show_images(train_images[:8], train_labels[:8], ncols=8)
show_images(train_images[8:16], train_labels[8:16], ncols=8)

# Image Set / Preprocessing
# image array์˜ 0~255 ์‚ฌ์ด์˜ ๊ฐ’์„ → /255ํ•˜์—ฌ 0 ~ 1 ์‚ฌ์ด๋กœ ๋ณ€ํ™˜
(train_images, train_labels), (test_images, test_labels) = cifar10.load_data()

# test์šฉ์œผ๋กœ OHE ์ ์šฉ ์•ˆํ•จ, ์—ฌ๊ธฐ์„œ๋Š” sparse categorical crossentropy ํ…Œ์ŠคํŠธ๋ฅผ ์œ„ํ•ด ์ ์šฉํ•˜์ง€ ์•Š์Œ. 
def get_preprocessed_data(images, labels):
    
    # ํ•™์Šต๊ณผ ํ…Œ์ŠคํŠธ ์ด๋ฏธ์ง€ array๋ฅผ 0~1 ์‚ฌ์ด๊ฐ’์œผ๋กœ scale ๋ฐ float32 ํ˜• ๋ณ€ํ˜•. 
    images = np.array(images/255.0, dtype=np.float32)
    labels = np.array(labels, dtype=np.float32)
    
    return images, labels

# train, test์—๋„ ์ „์ฒ˜๋ฆฌ ์ ์šฉ
train_images, train_labels = get_preprocessed_data(train_images, train_labels)
test_images, test_labels = get_preprocessed_data(test_images, test_labels)

 

โœ CIFAR10 Customizing / Layer ์ƒ์„ฑ

from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Input, Dense , Conv2D , Dropout , Flatten , Activation, MaxPooling2D , GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam , RMSprop 
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.callbacks import ReduceLROnPlateau , EarlyStopping , ModelCheckpoint , LearningRateScheduler

IMAGE_SIZE = 32

input_tensor = Input(shape=(IMAGE_SIZE, IMAGE_SIZE, 3))

#x = Conv2D(filters=32, kernel_size=(5, 5), padding='valid', activation='relu')(input_tensor)
x = Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(input_tensor)
x = Conv2D(filters=32, kernel_size=(3, 3), padding='same', activation='relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)

x = Conv2D(filters=64, kernel_size=(3, 3), padding='same', activation='relu')(x)
x = Conv2D(filters=64, kernel_size=(3, 3), padding='same')(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=2)(x)

x = Conv2D(filters=128, kernel_size=(3,3), padding='same', activation='relu')(x)
x = Conv2D(filters=128, kernel_size=(3,3), padding='same', activation='relu')(x)
x = MaxPooling2D(pool_size=2)(x)

# cifar10์˜ ํด๋ž˜์Šค๊ฐ€ 10๊ฐœ ์ด๋ฏ€๋กœ ๋งˆ์ง€๋ง‰ classification์˜ Dense layer units๊ฐฏ์ˆ˜๋Š” 10
x = Flatten(name='flatten')(x)
x = Dropout(rate=0.5)(x)
x = Dense(300, activation='relu', name='fc1')(x)
x = Dropout(rate=0.3)(x)
output = Dense(10, activation='softmax', name='output')(x)

model = Model(inputs=input_tensor, outputs=output)

model.summary()

 

โœ CIFAR10 Customizing / Model ํ•™์Šต ์ˆ˜ํ–‰ ๋ฐ Test Data ๊ฒ€์ฆ

history = model.fit(x=train_images, y=train_labels, batch_size=64, epochs=30, validation_split=0.15) #7,500๊ฑด์„ validation์—์„œ ์‚ฌ์šฉ

# ์ผ๋ถ€ 
2022-03-22 13:52:32.302979: I tensorflow/stream_executor/cuda/cuda_dnn.cc:369] Loaded cuDNN version 8005
665/665 [==============================] - 11s 7ms/step - loss: 1.6712 - accuracy: 0.3745 - val_loss: 1.4978 - val_accuracy: 0.4800
# 

import matplotlib.pyplot as plt
%matplotlib inline

def show_history(history):
    plt.figure(figsize=(6, 6))
    plt.yticks(np.arange(0, 1, 0.05))
    plt.plot(history.history['accuracy'], label='train')
    plt.plot(history.history['val_accuracy'], label='valid')
    plt.legend()
    
show_history(history)

# ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ๋กœ ์„ฑ๋Šฅ ํ‰๊ฐ€
model.evaluate(test_images, test_labels)

 

โœ CIFAR10 Customizing / model.predit()๋ฅผ ํ†ตํ•œ ์ด๋ฏธ์ง€ ๋ถ„๋ฅ˜ ์˜ˆ์ธก

# ํ…Œ์ŠคํŠธ์šฉ 4์ฐจ์› ์ด๋ฏธ์ง€ ๋ฐฐ์—ด์„ ์ž…๋ ฅํ•ด์„œ predict()์ˆ˜ํ–‰. 
# predict()์˜ ๊ฒฐ๊ณผ๋Š” softmax ์ ์šฉ ๊ฒฐ๊ณผ์ž„. ํ•™์Šต ๋ฐ์ดํ„ฐ์˜ ์›-ํ•ซ ์ธ์ฝ”๋”ฉ ์ ์šฉ ์—ฌ๋ถ€์™€ ๊ด€๊ณ„์—†์ด softmax ์ ์šฉ ๊ฒฐ๊ณผ๋Š” ๋ฌด์กฐ๊ฑด 2์ฐจ์› ์ž„์— ์œ ์˜ 
preds = model.predict(np.expand_dims(test_images[0], axis=0))

print('์˜ˆ์ธก ๊ฒฐ๊ณผ shape:', preds.shape)
print('์˜ˆ์ธก ๊ฒฐ๊ณผ:', preds)


predicted_class = np.argmax(preds, axis=1)
print('์˜ˆ์ธก ํด๋ž˜์Šค ๊ฐ’:', predicted_class)
# result ///
์˜ˆ์ธก ํด๋ž˜์Šค ๊ฐ’: [3 8 8 0 6 6 1 6 3 1 0 9 5 7 9 8 5 7 8 6 7 2 4 9 4 3 4 0 9 6 6 5]
# result ///


# image ์ถœ๋ ฅ
show_images(test_images[:8], predicted_class[:8], ncols=8)
show_images(test_images[:8], test_labels[:8], ncols=8)

'MLDL_์ •๋ฆฌ > Sample' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

Pre Train, Find Tuning ์ •์˜  (0) 2023.02.24
[DL] OpenAOI Git  (0) 2022.05.09
[DL] - Object Detection / MMDetection Package  (0) 2022.03.07
[DL] - MediaPipe / Video pose Detection  (0) 2022.03.06
[DL] - MediaPipe / Video Object Detection  (0) 2022.03.06