-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathex4_1_cnn_mnist_cl.py
95 lines (76 loc) · 2.89 KB
/
ex4_1_cnn_mnist_cl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
###############################
# 분류 CNN 모델링
###############################
import keras
from keras import models, layers
from keras import backend
class CNN(models.Sequential):
def __init__(self, input_shape, num_classes):
super().__init__()
self.add(layers.Conv2D(32, kernel_size=(3, 3),
activation='relu',
input_shape=input_shape))
self.add(layers.Conv2D(64, (3, 3), activation='relu'))
self.add(layers.MaxPooling2D(pool_size=(2, 2)))
self.add(layers.Dropout(0.25))
self.add(layers.Flatten())
self.add(layers.Dense(128, activation='relu'))
self.add(layers.Dropout(0.5))
self.add(layers.Dense(num_classes, activation='softmax'))
self.compile(loss=keras.losses.categorical_crossentropy,
optimizer='rmsprop',
metrics=['accuracy'])
###############################
# 분류 CNN을 위한 데이터 준비
###############################
from keras import datasets
class DATA():
def __init__(self):
num_classes = 10
(x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()
img_rows, img_cols = x_train.shape[1:]
if backend.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols)
else:
x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)
self.input_shape = input_shape
self.num_classes = num_classes
self.x_train, self.y_train = x_train, y_train
self.x_test, self.y_test = x_test, y_test
###########################
# 학습 효과 분석
###########################
from keraspp.skeras import plot_loss, plot_acc
import matplotlib.pyplot as plt
###############################
# 분류 CNN 학습 및 테스트
###############################
def main():
batch_size = 128
epochs = 10
data = DATA()
model = CNN(data.input_shape, data.num_classes)
history = model.fit(data.x_train, data.y_train,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2)
score = model.evaluate(data.x_test, data.y_test)
print()
print('Test loss:', score[0])
print('Test accuracy:', score[1])
plot_loss(history)
plt.show()
plot_acc(history)
plt.show()
if __name__ == '__main__':
main()