【问题标题】:data augmentation error in python: TypeError: pic should be PIL Image or ndarray. Got <class 'torch.Tensor'>python 中的数据扩充错误:TypeError: pic 应该是 PIL Image 或 ndarray。得到 <class 'torch.Tensor'>
【发布时间】:2021-05-28 03:53:28
【问题描述】:

我正在尝试在 python 中执行数据扩充,但出现错误。我的目标是在训练数据集(而不是测试)上应用增强。为此,我有一个默认设置为 False 的标志(名为 augment = False)。对于训练数据集,我已将其设置为“真”。路径“/content/data/img”包含“.jpg”格式的图像。请看代码:

谢谢。

【问题讨论】:

  • 我没有看到你使用了 augument 参数。其次,如果您应用了变换,则不应应用 to_tensor_transform,因为您的变换已经将图像转换为张量。

标签: python tensorflow deep-learning python-imaging-library data-augmentation


【解决方案1】:

我没有看到您使用augument 参数。其次,如果您应用了变换,则不应应用 to_tensor_transform,因为您的变换已经将图像转换为张量。

class LesionDataset(Dataset):
 def __init__(self, img_dir, labels_fname, augment=False):
     
     self.img_dir = img_dir
     self.augument = augument
     self.labels_fname = pd.read_csv(labels_fname)
   
 def __len__(self):
       return len(self.labels_fname)

 def __getitem__(self, idx):
 
       image_id = self.labels_fname.iloc[idx,0]
       image = Image.open(os.path.join(self.img_dir, image_id +'.jpg')).convert("RGB")
       
       labels = self.labels_fname.drop(['image'], axis = 1)
       labels = np.array(labels)
       labels = np.argmax(labels, axis = 1)
       label = labels[idx]
       if self.augument:
           image = train_transforms(image)
       else:
           image = to_tensor_transform(image)
       return image, label

【讨论】:

  • 好的。谢谢,米哈伊尔。它现在可以工作,但它显示了训练循环的另一个错误。 RuntimeError: mat1 dim 1 must match mat2 dim 0
猜你喜欢
  • 2020-11-05
  • 2019-11-06
  • 2021-06-24
  • 2021-01-26
  • 2021-07-06
  • 2021-11-04
  • 2021-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多