拥有prediction和label之后对TP TN FP FN进行标色( 直接可用)

def visualize_prediction(output, prediction,path=None):
    # 创建一个空的 RGB 图像,大小为 [3, 256, 256]
    result_image = torch.zeros(3, 256, 256, dtype=torch.uint8).cuda()

    # 计算 TP、FN、TN 和 FP 区域
    TP = (output == 1) & (prediction == 1)
    FN = (output == 1) & (prediction == 0)
    TN = (output == 0) & (prediction == 0)
    FP = (output == 0) & (prediction == 1)

    # 将对应的颜色填充到 RGB 图像中
    result_image[0] = torch.where(FP, torch.tensor(255, dtype=torch.uint8).cuda(), result_image[0])  # 红色: [255, 0, 0]
    result_image[1] = torch.where(FN, torch.tensor(255, dtype=torch.uint8).cuda(), result_image[1])  # 绿色: [0, 255, 0]
    result_image[0] = torch.where(TP, torch.tensor(255, dtype=torch.uint8).cuda(),result_image[0])  # 白色: [255, 255, 255]
    result_image[1] = torch.where(TP, torch.tensor(255, dtype=torch.uint8).cuda(),result_image[1])  # 白色: [255, 255, 255]
    result_image[2] = torch.where(TP, torch.tensor(255, dtype=torch.uint8).cuda(),result_image[2])  # 白色: [255, 255, 255]
    #黑色就是最后没标的那些,因为本身像素点是黑色的
    # 从 GPU 转移到 CPU 并转换为 NumPy 数组
    result_image_np = result_image.cpu().numpy()

    # 将 NumPy 数组转换为 PIL 图像
    result_image_pil = Image.fromarray(result_image_np.transpose(1, 2, 0))  # 转置为 [H, W, C]

    # 保存图像
    result_image_pil.save(path)#path_name 中输入自己需要的路径

在该函数示例中,output和prediction都是尺寸为[1,256,256] 、数据类型为tensor且都在cuda上。

需要特别注意的是颜色的注入顺序很有讲究,为了让红绿不被覆盖,我们需要先涂红绿再涂白色。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值