在使用albumentations库进行数据增强时,调用torchvision.datasets.ImageNet()读取ImageNet数据集,在将albumentations方法作为transform参数传入时,运行报错。
File "/Users/yi/miniforge3/envs/PyTorch_py39/lib/python3.9/site-packages/albumentations/core/composition.py", line 175, in __call__
raise KeyError("You have to pass data to augmentations as named arguments, for example: aug(image=image)")
KeyError: 'You have to pass data to augmentations as named arguments, for example: aug(image=image)'
经过查询,在其官网发现解决方法,不能直接使用 torchvision.datasets来直接读取数据集
class ImageNetSearchDataset(torchvision.datasets.ImageNet):
def __getitem__(self, index):
path, label = self.samples[index]
image = cv2.imread(path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
if self.transform is not None:
transformed = self.transform(image=image)
image = transformed["image"]
return image, label