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='', 附上修改后的代码:
-
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