Data Science/Tensorflow2.0, Pytorch

10. Tensorflow2.0 Evaluating & Predicting

HJChung 2020. 3. 27. 20:00

이전까지 (Beginner방식으로 보면) Training까지 할 수 있었습니다. 

 #Training
 num_epochs = 10
 batch_size = 64
 
 hist = model.fit(X_train, y_train, batch_size = batch_size, epochs = num_epochs, shuffle = True)
 
 his.history
 
 

모델을 학습시켰으니 모델에서 evaluating합니다. 

1. Evaluating

model.evaluate(X_test, y_test, batch_size = batch_size)

2. Predicting 및 결과 확인

1) input으로 들어갈 이미지 데이터 표현(시각화)

import matplotlib.pyplot as plt
import numpy as np

%matplot inline

test_image = X_test[0 , :, :, 0]#X_tesst에서 0번째
test_image.shape #test_image의 크기 확인

plt.title(y_test[0])#y_test에서 0번째 
plt.imshow(test_image, 'gray')
plt.show()

 

pred  = model.pradict(test_image.reshape(1, 28, 28, 1))

np.argmax(pred) #모델 마지막에 softmax를 해주었기 때문에 pred가 예측 한 값 중 가장 높은 (큰) 값이 정답이 된다. 

2) Test Batch

앞에서는 test_image로 X__test 중 하나의 이미지만 넣었는데, 여기서는 batch 로 넣어서 위의 Predicting과정을 진행해보고자 합니다. 

1. Batch로 Test Dataset넣기

test_batch = X_tests[:32] #32개의 이미지를 한 batch로 설정하고 이를 test_batch에 묶어줌
test_batch.shape

2. Batch Test Dataset 모델에 넣기

preds = model.prediction(test_batch)
preds.shape

3. 결과 확인

np.argmax(preds, -1)
#-1을 넣어주는 이유는 preds.shape의 맨 마지막 꺼 즉, (32,10)에서 10을 기준으로 np.argmax해달라는 의미이기 때문입니다. 

#그러면 그 결과 array([7, 2, 3, 8, .... ]) 일렇게 32개의 각각의 이미지에 대해 32개의 예측값이 나옵니다.