Categorical Classification
1. CIFAR 10 Data_Set Load & Review
1-1. Load CIFAR 10 Data_Set
import tensorflow
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])
print(len(X_test))
print(X_test.shape)
print(len(y_test))
print(y_test[0:5])
2. Data Preprocessing
2-1. Normalization
X_train = X_train.astype(float) / 255
X_test = X_test.astype(float) / 255
print(X_train[0])
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 Functional API Modeling
3-1. Model Define
%%time
from tensorflow.keras import models
from tensorflow.keras import layers
input_img = layers.Input((32,32,3))
x = layers.Conv2D(filters=32, kernel_size=(3,3), strides=(1,1), padding='same')(input_img)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(filters=32, kernel_size=(3,3), strides=(2,2), padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(filters=64, kernel_size=(3,3), strides=(1,1), padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Conv2D(filters=64, kernel_size=(3,3), strides=(2,2), padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Flatten()(x)
x = layers.Dense(128)(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
x = layers.Dropout(0.5)(x)
x = layers.Dense(10)(x)
output_y_hat = layers.Activation('softmax')(x)
CIFAR = models.Model(input_img, output_y_hat)
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 = 100,
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))
3. 성능평가
3-1. Label Name 지정
import numpy as np
CLASSES = np.array(['Airplane', 'Automobile', 'Bird', 'Cat', 'Deer',
'Dog', 'Frog', 'Horse', 'Ship', 'Truck'])
preds = CIFAR.predict(X_test)
preds_single = CLASSES[np.argmax(preds, axis = -1)]
actual_single = CLASSES[np.argmax(y_test, axis = -1)]
3-2. 비교 시각화
import matplotlib.pyplot as plt
n_to_show = 10
indices = np.random.choice(range(len(X_test)), n_to_show)
fig = plt.figure(figsize = (15, 3))
fig.subplots_adjust(hspace = 0.4, wspace = 0.4)
for i, idx in enumerate(indices):
img = X_test[idx]
ax = fig.add_subplot(1, n_to_show, i + 1)
ax.axis('off')
ax.text(0.5, -0.35,
'Pred = ' + str(preds_single[idx]),
fontsize = 10,
ha = 'center',
transform = ax.transAxes)
ax.text(0.5, -0.7,
'Act = ' + str(actual_single[idx]),
fontsize = 10,
ha = 'center',
transform = ax.transAxes)
ax.imshow(img)
Top_5_Correctness (0) | 2022.07.04 |
---|---|
CNN(Convolutional Neural Network)-CIFAR 100_ResNet50V2 (0) | 2022.07.04 |
CNN 모델 학습 시각화 (0) | 2022.06.28 |
CNN(Convolutional Neural Network)-VGG-16 (0) | 2022.06.27 |
CNN(Convolutional Neural Network)-이미지 데이터 셋을 이용한 CNN Modeling (0) | 2022.06.24 |