最近在跑图像分类的神经网络代码,在CIFAR10上面跑完了之后想在Imagenet上面跑,奈何实验室都在用卡,于是退而求其次,找到了降采样的imagenet
A DOWNSAMPLED VARIANT OF IMAGENET AS AN ALTERNATIVE TO THE CIFAR DATASETS
https://arxiv.org/pdf/1707.08819.pdf
这个数据集也可以在Imagenet的官网上找到。可以看到size=8, 16, 32, 64的都有。
然后我下的是32*32版本的,图像大小也刚好和CIFAR的尺寸相同。然后看pytorch的torchvision里面好像没有写好的class(只要Imagenet的),然后准备自己写一个。
完事儿前面都没问题,后面报了一个错
cunn_ClassNLLCriterion_updateOutput_kernel
查了一下原因https://blog.csdn.net/littlehaes/article/details/102806323
简单说起来就是target(label)越界了,pytorch里面的要求的这个列数是t >= 0 && t < n_classes
然后调试看了一下下载的imagenet32中的target,果然这里面他的范围是[1, 1000],而我们需要的应该是[0, 999],因此在Dataset的__getitem__里面,将每个target减一即可,下面是一个简单实现的代码:
def __getitem__(self, index):
"""
Args:
index (int): Index
Returns:
tuple: (image, target) where target is index of the target class.
"""
# please note that the original targets are in [1, 1000] which dose not conform with nn.CrossEntropyLoss
# and the targets should -1
img, target = self.data[index], self.targets[index] - 1
# doing this so that it is consistent with all other datasets
# to return a PIL Image
img = Image.fromarray(img)
if self.transform is not None:
img = self.transform(img)
if self.target_transform is not None:
target = self.target_transform(target)
return img, target