问题遇到的现象和发生背景
在服务器用python3.7跑代码时报错
问题相关代码,请勿粘贴截图
def own_loader(id, root): # 这里只导入了原始数据的,用的就是这个
img = cv2.imread(os.path.join(root, '{}_sat.png').format(id))
mask = cv2.imread(os.path.join(root + '{}_mask.png').format(id), cv2.IMREAD_GRAYSCALE)
mask = np.expand_dims(mask, axis=2)
# img = np.array(img, np.float32).transpose(2,0,1)/255.0 * 3.2 - 1.6
# mask = np.array(mask, np.float32).transpose(2,0,1)/255.0
mask[mask >= 0.5] = 1
mask[mask <= 0.5] = 0
img = np.array(img, np.float32).transpose(2, 0, 1)
mask = np.array(mask, np.float32).transpose(2, 0, 1)
return img, mask
class ImageFolder(data.Dataset):
def __init__(self, trainlist, root):
self.ids = trainlist
# self.loader = default_loader #原始的
self.loader = own_loader # 用了自己的数据导入
self.root = root
# self.trans = transforms.Compose([transforms.ToTensor()])
# self.trans = transforms.Compose([transforms.ToTensor(),transforms.Normalize(mean=(0.5,0.5,0.5),std=(0.5,0.5,0.5))])
def __getitem__(self, index):
id = list(self.ids)[index]
img, mask = self.loader(id, self.root)
# img = np.transpose(img, (1,2,0))
# img = self.trans(img)
img = torch.Tensor(img)
mask = torch.Tensor(mask)
return img, mask
def __len__(self):
return len(list(self.ids))
运行结果及报错内容
line 132, in own_loader
mask[mask >= 0.5] = 1
TypeError: '>=' not supported between instances of 'NoneType' and 'float'