将标签转化为字符形式显示
比如EMNIST数据集的by_class类有62类(含数字和大小写字母),想显示图片时在标题显示图片标签,把原标签例如10显示为A,36显示为a,有以下两种方法可以参考:
第一种:
y_test1 = [y_test[i].argmax() for i in range(116323)]
model = load_model('byclass_LeNet5.h5')
result = model.predict(x_test)
result1 = [result[j].argmax() for j in range(116323)]
y_test1 = np.array(y_test1, dtype=np.int)
result1 = np.array(result1, dtype=np.int)
result2 = np.ones((116323))
result2 = result2.astype(np.str)
for i in range(len(result1)):
if result1[i] < 10:
result2[i] = result1[i]
elif (result1[i] >= 10 and result1[i] <= 35):
result2[i] = chr(result1[i] + 55)
else:
result2[i] = chr(result1[i] + 61)
y_test2 = np.ones((116323))
y_test2 = y_test2.astype(np.str)
for j in range(len(y_test1)):
if y_test1[j] < 10:
y_test2[j] = y_test1[j]
elif (y_test1[j] >= 10 and y_test1[j] <= 35):
y_test2[j] = chr(y_test1[j] + 55)
else:
y_test2[j] = chr(y_test1[j] + 61)
#查看识别错误的样本图片,在上面显示标签
miscl_img = x_test[y_test1 != result1][:25]
correct_lab = y_test2[y_test1 != result1][:25]
miscl_lab = result2[y_test1 != result1][:25]
fig, ax = plt.subplots(nrows=5, ncols=5, sharex=True, sharey=True,)
ax = ax.flatten()
for i in range(25):
img = miscl_img[i].reshape(28, 28)
ax[i].imshow(img, cmap='Greys', interpolation='nearest')
ax[i].set_title('%d) t: %c p: %c' % (i+1, correct_lab[i], miscl_lab[i]))
ax[0].set_xticks([])
ax[0].set_yticks([])
plt.tight_layout()
plt.show()
第二种:
fig = plt.figure()
for i in
range(20):
ax = fig.add_subplot(4, 5, i + 1)
ax.imshow(train_image[i, :, :])
ax.set_title("{0:c}".format(maps[str(int(train_label[i]))]))
plt.tight_layout()
plt.show()
hhh第一种可能有一点傻傻的
查看图片部分的代码参考:https://www.cnblogs.com/jxxclj/p/10266889.html