我正在编写自定义数据集,但数据加载器未返回正确的目标张量。
标签为010
的地方,我得到10
。
我正在将标签转换为int
,然后再转换为tensor
。
y_label = torch.tensor(int(self.annotations.iloc[index, 1]))
通过数据加载器进行迭代:
Batch idx 0, data shape torch.Size([1, 1, 56, 128, 128]), target shape torch.Size([1])
tensor([1])
Batch idx 1, data shape torch.Size([1, 1, 56, 128, 128]), target shape torch.Size([1])
tensor([100])
Batch idx 2, data shape torch.Size([1, 1, 56, 128, 128]), target shape torch.Size([1])
tensor([10])
Batch idx 3, data shape torch.Size([1, 1, 56, 128, 128]), target shape torch.Size([1])
tensor([1])
csv file如下所示:
1 p0.npy, 100
2 pl.npy, 001
3 p2.npy, 001
4 p3.npy, 001
5 p4.npy, 100
6 p5.npy, 010
7 p6.npy, 100
8 p7.npy, 100
9 p8.npy, 100
10 p9.npy, 010
11 plO.npy, 010
12 pll.npy, 010
13 p12.npy, 010
14 p13.npy, 100
代码:
class patientdataset(Dataset):
def __init__(self, csv_file, root_dir, transform=None):
self.annotations = pd.read_csv(csv_file)
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.annotations)
def __getitem__(self, index):
img_path = os.path.join(self.root_dir, self.annotations.iloc[index,0])
# np_load_old = np.load
# np.load = lambda *a, **k: np_load_old(*a, allow_pickle=True, **k)
image= np.array(np.load(img_path))
# y_label = torch.tensor(np.asarray(self.annotations.iloc[index,1]))
y_label = torch.tensor(int(self.annotations.iloc[index, 1]))
if self.transform:
imagearrays = self.transform(image)
image = imagearrays[None, :, :, :]
imaget = np.transpose(image, (0, 2, 1, 3))
image = imaget
return (image, y_label)
答案 0 :(得分:2)
看来您的标签是二进制格式。 将它们转换为十进制,然后转换为张量将为您解决问题。
y_label = torch.tensor(int(self.annotations.iloc[index, 1], 2))
这样做会将010
转换为2,100
转换为4,依此类推。