Pytorch加载数据集 ImageNet32

最近在跑图像分类的神经网络代码,在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

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值