复现U-net所遇问题总结分析

1.参考代码:GitHub - milesial/Pytorch-UNet: PyTorch implementation of the U-Net for image semantic segmentation with high quality images

2.参考文章:(30条消息) U-net复现pytorch版本 以及制作自己的数据集并训练_奶盖芒果的博客-CSDN博客_u-net数据集 

3.复现代码所遇问题:

  • 1    
    ither no mask or multiple masks found for the ID{idx}:{mask_file}'
    AssertionError:Either no mask or multiple masks found for the ID.

    解决办法:data_loading中的CarvanaDataset类下的改为mask_suffix='', 附上修改后的代码:

  1. class CarvanaDataset(BasicDataset):
        def __init__(self, images_dir, masks_dir, scale=1):
            super().__init__(images_dir, masks_dir, scale, mask_suffix=''

     2.RuntimeError: CUDA error: device-side assert triggered..cu:95: block: [0,0,0], thread: [481,0,0] Assertion `t >= 0 && t < n_classes` failed.

      解决方法:网上解决办法说是将分类类别设置为:256 (因为有的二分类图像标签只是0和255所以分类数目设置为256,有的二分类图像为0和1,分类数目设置为2),如果你的数据集标签是以上情况,那么这种做法是正确的。但是!!!,如果你的mask标签是含有0和255,例如打印下你的mask像素值(numpy.uniq()函数),是[0,45,78,122,200]这种情况,解决办法如下:在data_loading.py 的 def preprocess(pil_img, scale, is_mask)函数众进行一下修改:

def preprocess(pil_img, scale, is_mask):
        w, h = pil_img.size
        newW, newH = int(scale * w), int(scale * h)
        assert newW > 0 and newH > 0, 'Scale is too small, resized images would have no pixel'
        pil_img = pil_img.resize((newW, newH), resample=Image.NEAREST if is_mask else Image.BICUBIC)
        img_ndarray = np.asarray(pil_img)

        if not is_mask:
            if img_ndarray.ndim == 2:
                img_ndarray = img_ndarray[np.newaxis, ...]
            else:
                img_ndarray = img_ndarray.transpose((2, 0, 1))

            img_ndarray = img_ndarray / 255
        else:
            img_ndarray = img_ndarray / 255 #添加的

        return img_ndarray

  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值