from keras.datasets import mnist
import matplotlib.pyplot as plt
from keras.models import load_model
def show_images_labels_predictions(images,labels,predictions,start_id,num=10):
plt.gcf().set_size_inches(12, 14)
if num>25: num=25
for i in range(0, num):
ax=plt.subplot(5,5, i+1)
ax.imshow(images[start_id], cmap='binary') #顯示黑白圖片
if( len(predictions) > 0 ) : #有傳入預測資料
title = 'ai = ' + str(predictions[start_id])
title += (' (o)' if predictions[start_id]==labels[start_id] else ' (x)')
title += '\nlabel = ' + str(labels[start_id])
else : #沒有傳入預測資料
title = 'label = ' + str(labels[start_id])
ax.set_title(title,fontsize=12) #X,Y軸不顯示刻度
ax.set_xticks([]);ax.set_yticks([])
start_id+=1
plt.show()
(train_feature, train_label), (test_feature, test_label) = mnist.load_data()
test_feature_vector = test_feature.reshape(len( test_feature), 784).astype('float32')
test_feature_normalize = test_feature_vector/255
model = load_model('Mnist_mlp_model.h5')
prediction=model.predict_classes(test_feature_normalize) #預測
show_images_labels_predictions(test_feature,test_label,prediction,0)
#***************************
#錯誤訊息如下
# model = load_model('Mnist_mlp_model.h5')
# UnicodeDecodeError: 'utf-8' codec can't decode byte 0xae in position 7: invalid start byte
這個錯誤訊息通常表示在讀取模型檔案時出現了解碼錯誤。可能的原因是該檔案不是一個純文字檔案,因此無法使用 UTF-8 解碼。你可以嘗試以下方法來解決這個問題:
model = load_model('Mnist_mlp_model.h5', compile=False)
import codecs
with codecs.open('Mnist_mlp_model.h5', 'r', encoding='其他編碼格式') as f:
model = f.read()
希望這些方法可以解決你的問題。