将标签转化为字符形式显示

将标签转化为字符形式显示

比如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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值