【问题标题】:Is there a way to extract just a needed class from CIFAR-10 training dataset?有没有办法从 CIFAR-10 训练数据集中提取所需的类?
【发布时间】:2020-01-08 12:51:01
【问题描述】:

我想做的事情看起来很简单,但它就是行不通。我想对每一类图像(矩阵)执行某些操作,所以我首先必须从乱码中提取它们中的每一个。

from tensorflow.keras import datasets
import numpy as np

(train_images, train_labels), (test_images, test_labels)= datasets.cifar10.load_data()
print(len(train_images))
print(len(train_images))
train_images[train_labels==6]

这是错误。当然是因为图像矩阵的形状 (50000,32,32,3)。尽管图像和标签的长度相同,均为 50000,python 无法以某种方式将矩阵用作 1 项进行过滤。非常欢迎帮助..

50000
50000


---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-170-029cc3d4f0a9> in <module>
      5 
      6 
----> 7 train_images[train_labels==6]

IndexError: boolean index did not match indexed array along dimension 1; dimension is 32 but corresponding boolean dimension is 1

【问题讨论】:

    标签: python list tensorflow arraylist conv-neural-network


    【解决方案1】:

    这里的问题是 train_labels 具有形状 (50000,1) ,因此当您对其进行索引时,numpy 会尝试将其用作二维。这是一个简单的解决方法。

    from tensorflow.keras import datasets
    import numpy as np
    
    (train_images, train_labels), (test_images, test_labels)= datasets.cifar10.load_data()
    print('Images Shape: {}'.format(train_images.shape))
    print('Labels Shape: {}'.format(train_labels.shape))
    idx = (train_labels == 6).reshape(train_images.shape[0])
    print('Index Shape: {}'.format(idx.shape))
    filtered_images = train_images[idx]
    print('Filtered Images Shape: {}'.format(filtered_images.shape))
    

    【讨论】:

      猜你喜欢
      • 2020-03-10
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-04
      • 1970-01-01
      • 2021-04-07
      相关资源
      最近更新 更多