본문 바로가기
Visual Intelligence/Image Deep Learning

[시각 지능] false prediction

by goatlab 2022. 8. 6.
728x90
반응형
SMALL

false prediction

 

import tensorflow as tf

from tensorflow.keras.layers import Flatten, Dense, Conv2D, MaxPool2D
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.datasets import mnist

import numpy as np
from datetime import datetime
import matplotlib.pyplot as plt

(x_train, t_train), (x_test, t_test) = mnist.load_data()

x_train = x_train / 255.0
x_test = x_test / 255.0

print('x_train.shape = ', x_train.shape, ' , x_test.shape = ', x_test.shape)
print('t_train.shape = ', t_train.shape, ' , t_test.shape = ', t_test.shape)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
x_train.shape =  (60000, 28, 28)  , x_test.shape =  (10000, 28, 28)
t_train.shape =  (60000,)  , t_test.shape =  (10000,)
# sequential model construction
model = Sequential()

# 5 x 5 x 32 필터
model.add(Conv2D(input_shape=(28,28,1), 
                 kernel_size=5, filters=32, 
                 strides=(1,1), activation='relu', use_bias=True, padding='SAME'))
model.add(MaxPool2D(pool_size=(2,2), padding='SAME'))
model.add(Flatten())
model.add(Dense(10, activation='softmax'))

# model compile
# one hot encoding 방식이 아니기 때문에, loss=sparse_categorical_crossentropy 정의
model.compile(optimizer=Adam(lr=0.001), 
              loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.summary()
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 28, 28, 32)        832       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 32)        0         
_________________________________________________________________
flatten (Flatten)            (None, 6272)              0         
_________________________________________________________________
dense (Dense)                (None, 10)                62730     
=================================================================
Total params: 63,562
Trainable params: 63,562
Non-trainable params: 0
_________________________________________________________________
# model shape 확인
print(model.input_shape, model.output_shape)
(None, 28, 28, 1) (None, 10)
# x_train, x_test의 shape(차원)은 (이미지 수, 28, 28)의 3차원 데이터
# 4차원으로 바꾸기 위해 reshape() 사용
# 가장 처음의 -1은 batch_size에 지정된 데이터가 한번에 embedding
start_time = datetime.now()

hist = model.fit(x_train.reshape(-1,28,28,1), t_train, 
                 batch_size=50, epochs=50, validation_split=0.2)

end_time = datetime.now()

print('\n\nElapsed Time => ', end_time - start_time)
# x_test 데이터를 4차원으로 reshape 변환해주지 않으면 error
model.evaluate(x_test.reshape(-1,28,28,1), t_test)
# index_label_prediction 코드 구현
ret_val = model.predict(x_test.reshape(-1,28,28,1))

print('type(ret_val) = ', type(ret_val), ', ret_val.shape = ', ret_val.shape)

# predict 실행 결과는 softmax 에 의한 확률 결과이므로, argmax 이용해서 10진수로 바꾸어 주어야함
predicted_val = np.argmax(ret_val, axis=1)  # 행 단위로 argmax 실행

prediction_label_comp_val = np.equal(predicted_val, t_test)

# list comprehension 이용하여 index_label_prediction 구현
index_label_prediction_list = [ [index, t_test[index], predicted_val[index] ]  for index, result in enumerate(prediction_label_comp_val)  if result == False ]

print(len(index_label_prediction_list))

print('Accuracy = ', 1 - ( len(index_label_prediction_list) / len(t_test) ))
print(index_label_prediction_list)
# 임의의 false prediction 이미지 출력
false_data_index = np.random.randint(len(index_label_prediction_list))

print('len of index_label_prediction_list => ', len(index_label_prediction_list), ', false_data_index => ', false_data_index)

mnist_index = index_label_prediction_list[false_data_index][0]
label = index_label_prediction_list[false_data_index][1]
prediction = index_label_prediction_list[false_data_index][2]

title_str = 'index = ' + str(mnist_index) + ' , label = ' + str(label) + ' , prediction = ' + str(prediction)

img = x_test[mnist_index].reshape(28,28)

plt.title(title_str)
plt.imshow(img, cmap='gray')
plt.show()

728x90
반응형
LIST