상세 컨텐츠

본문 제목

CIFAR 10 - Categorical Classification

인공지능/실습

by 2^7 2022. 6. 24. 14:19

본문

CIFAR 10 - Categorical Classification

 

1. CIFAR 10 Data_Set Load & Review

1-1. Load CIFAR 10 Data_Set

from tensorflow.keras.datasets import cifar10

(X_train, y_train), (X_test, y_test) = cifar10.load_data()
print(len(X_train))
print(X_train.shape)

print(len(y_train))
print(y_train[0:5])

Train_Data Information

print(len(X_test))
print(X_test.shape)

print(len(y_test))
print(y_test[0:5])

Test_Data Information


1-2.Visualization 

import matplotlib.pyplot as plt

digit = X_train[0]
plt.imshow(digit)
plt.show()

import numpy as np
np.set_printoptions(linewidth = 150)

print(X_train[0][0])


2. Data Preprocessing

2-1. Reshape and Normalization

#reshape
#(50000, 32, 32, 3) to (50000, 3072)

X_train = X_train.reshape((50000, 32 * 32 * 3))
X_test = X_test.reshape((10000, 32 * 32 * 3))

X_train.shape, X_test.shape

((50000, 3072), (10000, 3072))

#Normalization

X_train = X_train.astype(float) / 255
X_test = X_test.astype(float) / 255
print(X_train[0])

[0.23137255 0.24313725 0.24705882 ... 0.48235294 0.36078431 0.28235294]


2-2. One Hot Encoding

from tensorflow.keras.utils import to_categorical

y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
print(y_train[:5])


3. Keras Modeling

3-1. Model Define

  • 모델 신경망 구조 정의
    • 3개의 Hidden Layers & 2816개의 Nodes
from tensorflow.keras import models
from tensorflow.keras import layers

CIFAR = models.Sequential()
CIFAR.add(layers.Dense(2048, activation = 'relu', input_shape = (32 * 32 * 3,)))
CIFAR.add(layers.Dropout(0.4))
CIFAR.add(layers.Dense(512, activation = 'relu'))
CIFAR.add(layers.Dropout(0.4))
CIFAR.add(layers.Dense(128, activation = 'relu'))
CIFAR.add(layers.Dense(10, activation = 'softmax'))
CIFAR.summary()


2-2.Model Compile

CIFAR.compile(loss = 'categorical_crossentropy',
              optimizer = 'adam',
              metrics = ['accuracy'])

2-3.Model Fit

%%time

Hist_CIFAR = CIFAR.fit(X_train, y_train,
                       epochs = 200,
                       batch_size = 128,
                       validation_split = 0.2)


2-4. 학습 결과 시각화

import matplotlib.pyplot as plt

epochs = range(1, len(Hist_CIFAR.history['loss']) + 1)

plt.figure(figsize = (9, 6))
plt.plot(epochs, Hist_CIFAR.history['loss'])
plt.plot(epochs, Hist_CIFAR.history['val_loss'])
# plt.ylim(0, 0.25)
plt.title('Training & Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend(['Training Loss', 'Validation Loss'])
plt.grid()
plt.show()


2-5. Model Evaluate

loss, accuracy = CIFAR.evaluate(X_test, y_test)

print('Loss = {:.5f}'.format(loss))
print('Accuracy = {:.5f}'.format(accuracy))


2-6.Model Predict

#Probability

np.set_printoptions(suppress = True, precision = 9)

print(CIFAR.predict(X_test[:1,:]))

[[0.012462812 0.023513502 0.029073663 0.32488498 0.085242055 0.41452456 0.012651604 0.044881113 0.043340538 0.009425166]]

print(np.argmax(CIFAR.predict(X_test[:1,:])))

5

728x90

'인공지능 > 실습' 카테고리의 다른 글

Fashion MNIST - Categorical Classification  (0) 2022.06.20
Kaggle 신용카드 사기 검출  (0) 2022.06.13

관련글 더보기