S.Haviv
S.Haviv

Reputation: 319

CIFAR-10 Dataset using Keras

I'm using Keras to train a model on CIFAR-10 to recognize some of the classes, however, I want some of the classes and not all of them, so I wrote the following code:

selected_classes = [2, 3, 5, 6, 7]
print('train\n', x_train.shape, y_train.shape)
x = [ex for ex, ey in zip(x_train, y_train) if ey in selected_classes]
y = [ey for ex, ey in zip(x_train, y_train) if ey in selected_classes]
x_train = np.stack(x)
y_train = np.stack(y).reshape(-1,1)
print(x_train.shape, y_train.shape)

print('test\n', x_test.shape, y_test.shape)
x = [ex for ex, ey in zip(x_test, y_test) if ey in selected_classes]
y = [ey for ex, ey in zip(x_test, y_test) if ey in selected_classes]
x_test = np.stack(x)
y_test = np.stack(y).reshape(-1,1)
print(x_test.shape, y_test.shape)
num_classes = len(selected_classes)

And I keep getting the following error :

IndexError                                Traceback (most recent call last)
<ipython-input-8-d53a2cf8bdf8> in <module>()

 # Convert class vectors to binary class matrices.
 y_train = keras.utils.to_categorical(y_train, num_classes)
 y_test = keras.utils.to_categorical(y_test, num_classes)

 ~\Anaconda3\lib\site-packages\keras\utils\np_utils.py in to_categorical(y, 
 num_classes)
      n = y.shape[0]
      categorical = np.zeros((n, num_classes))
      categorical[np.arange(n), y] = 1
      output_shape = input_shape + (num_classes,)
      categorical = np.reshape(categorical, output_shape)

 IndexError: index 6 is out of bounds for axis 1 with size 5

I searched the keras source code and found that : y Class vector to be converted into a matrix (integers from 0 to num_classes).4

And when I define the num_classes to 8 or so it does work, however, I've got only 5 classes...

Upvotes: 2

Views: 523

Answers (1)

Ioannis Nasios
Ioannis Nasios

Reputation: 8527

You need to rename your target. I would suggest to turn your targets to strings, then label encode and finally turn to categorical.

from keras.utils import to_categorical
from sklearn.preprocessing import LabelEncoder

y= [2, 3, 5, 6, 7]
y=[str(x) for x in y] #as strings
le = LabelEncoder()
le.fit(y)
y_transformed=le.transform(y)

y_train=to_categorical(y_transformed)

to turn your predictions to classes you can use le.classes_ to find out which class corresponds to which outcome.

Upvotes: 2

Related Questions