Есть ли способ извлечь только необходимый класс из учебного набора данных CIFAR-10? - PullRequest
0 голосов
/ 08 января 2020

То, что я хочу сделать, выглядит просто, но просто не работает. Я хочу выполнить определенные операции с каждым классом изображений (матриц), поэтому сначала мне нужно извлечь каждое из них из зашифрованной партии.

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

1 Ответ

0 голосов
/ 08 января 2020

Проблема здесь в том, что 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))
...