how can I read labels in words from cifar10 datasets?

767 views
Skip to first unread message

Jake Moon

unread,
May 6, 2016, 11:50:22 PM5/6/16
to Keras-users
From this page: https://www.cs.toronto.edu/~kriz/cifar.html

It says that I can read labels not as integers, but as meaningful names by reading the batches.meta file in the dataset.

But I just can't seem to find a way to read the file.

I tried by modifying the keras/datasets/cifar10.py at https://github.com/fchollet/keras/blob/master/keras/datasets/cifar10.py, like below:

from __future__ import absolute_import
from keras.datasets.cifar import load_batch
from keras.utils.data_utils import get_file
import numpy as np
import os


def load_data():
    dirname = "cifar-10-batches-py"
    path = get_file(dirname, origin=origin, untar=True)

    nb_train_samples = 50000

    X_train = np.zeros((nb_train_samples, 3, 32, 32), dtype="uint8")
    y_train = np.zeros((nb_train_samples,), dtype="uint8")

    for i in range(1, 6):
        fpath = os.path.join(path, 'data_batch_' + str(i))
        bpath = os.path.join(path, 'batches.meta')
        data, labels = load_batch(fpath)
        label_names = load_batch(bpath)
        
    return data, labels, label_names

data, labels, label_names = load_data()

print(labels[10], label_names[7])


But I get an error message like below:



---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-15-cf7a7f68853d> in <module>()
     24     return data, labels, label_names
     25 
---> 26 data, labels, label_names = load_data()
     27 
     28 print(labels[10], label_names[7])

<ipython-input-15-cf7a7f68853d> in load_data()
     20         bpath = os.path.join(path, 'batches.meta')
     21         data, labels = load_batch(fpath)
---> 22         label_names = load_batch(bpath)
     23 
     24     return data, labels, label_names

/home/robotics/anaconda3/lib/python3.5/site-packages/keras/datasets/cifar.py in load_batch(fpath, label_key)
     17             d[k.decode("utf8")] = v
     18     f.close()
---> 19     data = d["data"]
     20     labels = d[label_key]
     21 

KeyError: 'data'


It doesn't look like I'm on the right track, so could anyone help me with this please?? Thank you in advance. 



 

Jake Moon

unread,
May 7, 2016, 12:10:09 AM5/7/16
to Keras-users
I'm trying to display how my model does via displaying the target image and my model prediction with meaningful names.

How can I do those also?? 
Reply all
Reply to author
Forward
0 new messages