【发布时间】:2020-09-17 20:59:47
【问题描述】:
我正在尝试创建一个具有可变图像大小的 PyTorch 数据加载器。这是我的代码的 sn-p
def get_imgs(path_to_imgs):
imgs = []
for path in path_to_imgs:
imgs.append(cv2.imread(path))
imgs = np.asarray(imgs)
return imgs
上面的函数接受一个路径列表并将图像从路径加载到列表'imgs'。顺便说一句,图像大小不相等。该列表看起来像 imgs = [NumPy 数组,NumPy 数组 ....]。但是,当我将列表转换为 np.asarray 时,它会将列表转换为 dtype = object。
这是我的数据加载器类
class Dataset(torch.utils.data.Dataset):
def __init__(self, path_to_imgs, path_to_label):
'Initialization'
self.path_to_imgs = path_to_imgs
self.path_to_label = path_to_label
self.imgs = get_imgs(path_to_imgs)
self.label = get_pts(path_to_label)
self.imgs = torch.Tensor(self.imgs) **Error here
# self.imgs = torch.from_numpy(self.imgs) ** I tried this as well. Same error
self.label = torch.Tensor(self.label)
self.len = len(self.imgs)
def __len__(self):
'Denotes the total number of samples'
return self.len
def __getitem__(self, index):
return self.imgs, self.label
当我尝试将图像列表转换为张量**时,它失败并给出以下错误
无法转换 numpy.object_ 类型的 np.ndarray。唯一支持的类型是:float64、float32、float16、int64、int32、int16、int8、uint8 和 bool。
【问题讨论】:
标签: python-3.x pytorch classification