๐โ๏ธ 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 |