【问题标题】:Cifar10 dataset: read certain number of images from a classCifar10 数据集:从一个类中读取一定数量的图像
【发布时间】:2019-12-16 16:30:27
【问题描述】:

我目前正在使用 Pytorch 学习深度学习,并使用 Cifar 10 数据集进行一些实验。其中有 10 个类,每个类有 5000 个测试图像。我只想使用狗和鹿类数据的 60% 和其他类的 100% 数据。

据我了解,我需要使用自定义数据集。但我实际上无法弄清楚。如果您可以分享任何想法,示例代码或链接,将对我有所帮助。

【问题讨论】:

    标签: python deep-learning dataset pytorch


    【解决方案1】:

    你可以像这样使用Subset

    from torchvision.datasets import CIFAR10
    from torch.utils.data import Subset
    
    ds = CIFAR10('~/.torch/data/', train=True, download=True)
    dog_indices, deer_indices, other_indices = [], [], []
    dog_idx, deer_idx = ds.class_to_idx['dog'], ds.class_to_idx['deer']
    
    for i in range(len(ds)):
      current_class = ds[i][1]
      if current_class == dog_idx:
        dog_indices.append(i)
      elif current_class == deer_idx:
        deer_indices.append(i)
      else:
        other_indices.append(i)
    dog_indices = dog_indices[:int(0.6 * len(dog_indices))]
    deer_indices = deer_indices[:int(0.6 * len(deer_indices))]
    new_dataset = Subset(ds, dog_indices+deer_indices+other_indices)
    

    【讨论】:

      猜你喜欢
      • 2016-09-27
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-07
      • 2016-06-26
      • 1970-01-01
      • 2020-11-10
      相关资源
      最近更新 更多