# CIFAR-10 dataset consists of 60000 32x32 color images in 10 classes, with 6000 images per class.
# There are 50000 training images and 10000 test images.
# airplane, automobile, bird, cat, deer, dog, frog, horse, ship, truck
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import Dense, Input, Flatten
from keras.models import Sequential, Model
from keras.optimizers import Adam
from keras.utils import to_categorical
from keras.datasets import cifar10
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
print(x_train.shape, y_train.shape, x_test.shape, y_test.shape) # (50000, 32, 32, 3) (50000, 1) (10000, 32, 32, 3) (10000, 1)
print(x_train[0])
print(y_train[0])
plt.figure(figsize=(12, 4))
plt.subplot(131)
plt.imshow(x_train[0])
plt.subplot(132)
plt.imshow(x_train[1])
plt.subplot(133)
plt.imshow(x_train[2])
plt.show()
x_train = x_train.astype('float32') / 255 # 정규화
x_test = x_test.astype('float32')
# print(x_train[0])
NUM_CLASSES = 10
y_train = to_categorical(y_train, NUM_CLASSES) # 레이블에 대해 원핫 처리
y_test = to_categorical(y_test, NUM_CLASSES)
# print(y_train[0])
# model : CNN X
"""
model = Sequential([
Dense(units=512, activation='relu', input_shape=(32, 32, 3)),
Flatten(),
Dense(units=128, activation='relu'),
Dense(units=NUM_CLASSES, activation='softmax')
])
print(model.summary())
"""
# functional api
input_layer = Input((32, 32, 3))
x = Flatten()(input_layer)
x = Dense(512, activation='relu')(x)
x = Dense(128, activation='relu')(x)
output_layer = Dense(NUM_CLASSES, activation='softmax')(x)
model = Model(input_layer, output_layer)
print(model.summary())
opti = Adam(learning_rate=0.01)
model.compile(optimizer=opti, loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=128, epochs=10, shuffle=True, verbose=2)
print('test acc : %.4f'%model.evaluate(x_test, y_test, verbose=0, batch_size=128)[1])
print('train acc : %.4f'%model.evaluate(x_train, y_train, verbose=0, batch_size=128)[1])
print('test loss : %.4f'%model.evaluate(x_test, y_test, verbose=0, batch_size=128)[0])
# CNN을 적용
from keras import optimizers
from keras.layers import Conv2D, Activation, ReLU, LeakyReLU, BatchNormalization, MaxPool2D
# functional api
input_layer = Input((32, 32, 3))
x = Conv2D(filters=64, kernel_size=3, strides=2, padding='same')(input_layer)
# x = ReLU()(x)
x = LeakyReLU()(x)
x = MaxPool2D(pool_size=2)(x)
x = Conv2D(filters=64, kernel_size=3, strides=2, padding='same')(x)
x = LeakyReLU()(x)
x = MaxPool2D(pool_size=2)(x)
x = Flatten()(input_layer)
x = Dense(512, activation='relu')(x)
x = BatchNormalization()(x) # gradient 폭주를 방지. 과적합 방지도 가능(Dropout과 함께 적어주는 경우는 별로 없다.)
x = Dense(128, activation='relu')(x)
output_layer = Dense(NUM_CLASSES, activation='softmax')(x)
model = Model(input_layer, output_layer)
print(model.summary())
opti = Adam(learning_rate=0.01)
model.compile(optimizer=opti, loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=128, epochs=10, shuffle=True, verbose=2)
print('test acc : %.4f'%model.evaluate(x_test, y_test, verbose=0, batch_size=128)[1])
print('train acc : %.4f'%model.evaluate(x_train, y_train, verbose=0, batch_size=128)[1])
print('test loss : %.4f'%model.evaluate(x_test, y_test, verbose=0, batch_size=128)[0])
pred = model.predict(x_test[:10])
print('예측값 :', np.argmax(pred, axis=-1))
print('실제값 :', np.argmax(y_test[:10], axis=-1))
<console>
(50000, 32, 32, 3) (50000, 1) (10000, 32, 32, 3) (10000, 1)
[[[ 59 62 63]
[ 43 46 45]
[ 50 48 43]
...
[158 132 108]
[152 125 102]
[148 124 103]]
[[ 16 20 20]
[ 0 0 0]
[ 18 8 0]
...
[123 88 55]
[119 83 50]
[122 87 57]]
[[ 25 24 21]
[ 16 7 0]
[ 49 27 8]
...
[118 84 50]
[120 84 50]
[109 73 42]]
...
[[208 170 96]
[201 153 34]
[198 161 26]
...
[160 133 70]
[ 56 31 7]
[ 53 34 20]]
[[180 139 96]
[173 123 42]
[186 144 30]
...
[184 148 94]
[ 97 62 34]
[ 83 53 34]]
[[177 144 116]
[168 129 94]
[179 142 87]
...
[216 184 140]
[151 118 84]
[123 92 72]]]
[6]
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 32, 32, 512) 2048
flatten (Flatten) (None, 524288) 0
dense_1 (Dense) (None, 128) 67108992
dense_2 (Dense) (None, 10) 1290
=================================================================
Total params: 67,112,330
Trainable params: 67,112,330
Non-trainable params: 0
_________________________________________________________________
None
Epoch 1/10
391/391 - 2s - loss: 2.9505 - accuracy: 0.1651 - 2s/epoch - 4ms/step
Epoch 2/10
391/391 - 2s - loss: 1.9765 - accuracy: 0.2521 - 2s/epoch - 5ms/step
Epoch 3/10
391/391 - 2s - loss: 1.8418 - accuracy: 0.3211 - 2s/epoch - 5ms/step
Epoch 4/10
391/391 - 2s - loss: 1.7920 - accuracy: 0.3447 - 2s/epoch - 4ms/step
Epoch 5/10
391/391 - 1s - loss: 1.7509 - accuracy: 0.3655 - 1s/epoch - 3ms/step
Epoch 6/10
391/391 - 1s - loss: 1.7393 - accuracy: 0.3677 - 1s/epoch - 3ms/step
Epoch 7/10
391/391 - 1s - loss: 1.7125 - accuracy: 0.3806 - 1s/epoch - 3ms/step
Epoch 8/10
391/391 - 1s - loss: 1.7038 - accuracy: 0.3824 - 1s/epoch - 3ms/step
Epoch 9/10
391/391 - 1s - loss: 1.6964 - accuracy: 0.3885 - 1s/epoch - 3ms/step
Epoch 10/10
391/391 - 1s - loss: 1.6764 - accuracy: 0.3941 - 1s/epoch - 3ms/step
test acc : 0.2572
train acc : 0.4048
test loss : 322.4221
1/1 [==============================] - 0s 80ms/step
예측값 : [3 9 0 0 3 3 0 9 3 1]
실제값 : [3 8 8 0 6 6 1 6 3 1]