티스토리 뷰

5. 파이썬

[텐서플로] 패션 MNIST 예제 fashion_mnist.py

패스트코드블로그 2020. 5. 14. 23:02
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
 
class FashionModel:
    def __init__(self):
        self.fashion_mnist = keras.datasets.fashion_mnist
        (self.train_images, self.train_labels), (self.test_images, self.test_labels) \
            = self.fashion_mnist.load_data()
        self.class_names = ['T-shirt/top''Trouser''Pullover''Dress''Coat',
                       'Sandal''Shirt''Sneaker''Bag''Ankle boot']
        self.model = None
 
    def execute(self):
        self.show_dataset() # 데이터셋 정보
        self.create_model() # 모델 구성 & 훈련 
        predictions = self.predict_image() # 예측하기
        self.subplot_test(predictions) # 전체 테스트
        self.one_test() # 1개 이미지 테스트
 
    def show_dataset(self):
        print('--------- TRAIN SET SPEC -------------')
        print('훈련이미지 : {}'.format(self.train_images.shape))
        print('훈련이미지 수 : %s' % len(self.train_labels))
        print('훈련이미지 라벨 : %s' % self.train_labels)
        print('--------- TEST SET SPEC -------------')
        print('테스트이미지 :  {}'.format(self.test_images.shape))
        print('테스트이미지 수 :  %s' % len(self.test_labels))
        print('테스트이미지 라벨:  %s' % self.test_labels)
        plt.figure(figsize=(1010))
        for i in range(25):
            plt.subplot(55, i + 1)
            plt.xticks([])
            plt.yticks([])
            plt.grid(False)
            plt.imshow(self.train_images[i], cmap=plt.cm.binary)
            plt.xlabel(self.class_names[self.train_labels[i]])
        plt.show()
 
    def create_model(self):
        self.model = keras.Sequential([
            keras.layers.Flatten(input_shape=(2828)),
            keras.layers.Dense(128, activation='relu'),
            keras.layers.Dense(10, activation='softmax')
        ])
        self.model.compile(optimizer='adam',
                      loss='sparse_categorical_crossentropy',
                      metrics=['accuracy'])
        self.model.fit(self.train_images, self.train_labels, epochs=5)
        test_loss, test_acc = self.model.evaluate(self.test_images, self.test_labels, verbose=2)
        print('\n테스트 정확도:', test_acc)
 
    def predict_image(self):
        predictions = self.model.predict(self.test_images)
        print('예측값 : %s' % predictions[0])
        print('가장 신뢰도가 높은 레이블:  %s' % np.argmax(predictions[0]))
        return predictions
 
    def subplot_test(self, predictions):
        print('테스트:  %s' % self.test_labels[0])
        print('====================================')
        num_rows = 5
        num_cols = 3
        num_images = num_rows * num_cols
        plt.figure(figsize=(2 * 2 * num_cols, 2 * num_rows))
        for i in range(num_images):
            plt.subplot(num_rows, 2 * num_cols, 2 * i + 1)
            self.plot_image(i, predictions, self.test_labels, self.test_images)
            plt.subplot(num_rows, 2 * num_cols, 2 * i + 2)
            self.plot_value_array(i, predictions, self.test_labels)
        plt.show()
 
    def one_test(self):
        # 테스트 세트에서 이미지 하나를 선택합니다
        img = self.test_images[0]
        print(img.shape)
        # 이미지 하나만 사용할 때도 배치에 추가합니다
        img = (np.expand_dims(img, 0))
        print(img.shape)
        predictions_single = self.model.predict(img)
        print(predictions_single)
        self.plot_value_array(0, predictions_single, self.test_labels)
        _ = plt.xticks(range(10), self.class_names, rotation=45)
        np.argmax(predictions_single[0])
 
    def plot_image(self, i, predictions_array, true_label, img):
        predictions_array, true_label, img = predictions_array[i], true_label[i], img[i]
        plt.grid(False)
        plt.xticks([])
        plt.yticks([])
 
        plt.imshow(img, cmap=plt.cm.binary)
 
        predicted_label = np.argmax(predictions_array)
        if predicted_label == true_label:
            color = 'blue'
        else:
            color = 'red'
 
        plt.xlabel("{} {:2.0f}% ({})".format(self.class_names[predicted_label],
                                             100 * np.max(predictions_array),
                                             self.class_names[true_label]),
                   color=color)
 
 
    def plot_value_array(self, i, predictions_array, true_label):
        predictions_array, true_label = predictions_array[i], true_label[i]
        plt.grid(False)
        plt.xticks([])
        plt.yticks([])
        thisplot = plt.bar(range(10), predictions_array, color="#777777")
        plt.ylim([01])
        predicted_label = np.argmax(predictions_array)
 
        thisplot[predicted_label].set_color('red')
        thisplot[true_label].set_color('blue')
 
 
 
if __name__ == '__main__':
    f = FashionModel()
    f.execute()
cs
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/01   »
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
글 보관함