简体   繁体   中英

Python - Retrieving data and labels from a pickle file

I have a pickle file that looks as follows:

[array([[[148, 124, 115],
        [150, 127, 116],
        [154, 129, 121],
        ..., 
        [159, 142, 133],
        [159, 142, 133],
        [161, 145, 142]],

       [[165, 136, 145],
        [176, 137, 141],
        [178, 138, 144],
        ..., 
        [199, 163, 171],
        [202, 163, 167],
        [200, 158, 163]]]), array([1, 1])]

In a previous question , we were able to retrieve the data and label by doing that separately. But, when I have many images that approach would not be suitable.

My script now looks as follows:

data, labels = [], []
    for i in range(0, 1):

        filename = 'data.pickle'
        batch_data = unpickle(filename)
        if len(data) > 0:
            data = np.vstack((data, batch_data[0][i]))
            labels = np.hstack((labels, batch_data[1][i]))
        else:
            data = batch_data[0][0]
            labels = batch_data[1][0]

        data = data.astype(np.float32)
        return data, labels

When I run the code and print the labels for instance, I always get 1 , while I was expecting to get two labels, [1 1] (I'm not sure if they should be displayed as an array?)

What am I doing wrong here?

Thanks.

I was able to get labels the way you expect it to be. I used

# Create batch data that represents what you are asking, I created three labels and data
batch_data = np.array([[np.random.random((5,5)), np.random.random((5,5)), np.random.random((5,5))], np.array([1,1,1])])

#pickle the data
import pickle
pickle.dump( batch_data, open( "test.pickle", "wb" ) )

# create data and labels seperately

def test_func(batch_data):
    data, labels = [], []
    for i in range(0, batch_data.shape[1]):
        if len(data) > 0:
            data = np.vstack((data, batch_data[0][i]))
            labels = np.hstack((labels, batch_data[1][i]))
        else:
            data = batch_data[0][0]
            labels = batch_data[1][0]
        data = data.astype(np.float32)
    return data, labels

# unpickle
unpickled_batch_data = pickle.load(open( "test.pickle", "rb" ))

# get stacked data and labels
data, labels =  test_func(unpickled_batch_data)
print labels

returns

[1 1 1]

You may be able to get away with just using zip twice:

In [24]: pickle_data = [array([[[148, 124, 115],
    ...:         [150, 127, 116],
    ...:         [154, 129, 121],
    ...:         [159, 142, 133],
    ...:         [159, 142, 133],
    ...:         [161, 145, 142]],
    ...:
    ...:        [[165, 136, 145],
    ...:         [176, 137, 141],
    ...:         [178, 138, 144],
    ...:         [199, 163, 171],
    ...:         [202, 163, 167],
    ...:         [200, 158, 163]]]), array([1, 1])]

You also need argument unpacking with the * operator:

In [25]: data, labels = zip(*zip(*pickle_data))

In [26]: data
Out[26]:
(array([[148, 124, 115],
        [150, 127, 116],
        [154, 129, 121],
        [159, 142, 133],
        [159, 142, 133],
        [161, 145, 142]]), array([[165, 136, 145],
        [176, 137, 141],
        [178, 138, 144],
        [199, 163, 171],
        [202, 163, 167],
        [200, 158, 163]]))

In [27]: labels
Out[27]: (1, 1)

Now the labels and data correspond by index:

In [28]: data[0]
Out[28]:
array([[148, 124, 115],
       [150, 127, 116],
       [154, 129, 121],
       [159, 142, 133],
       [159, 142, 133],
       [161, 145, 142]])

In [29]: data[1]
Out[29]:
array([[165, 136, 145],
       [176, 137, 141],
       [178, 138, 144],
       [199, 163, 171],
       [202, 163, 167],
       [200, 158, 163]])

In [30]: labels[0]
Out[30]: 1

In [31]: labels[1]
Out[31]: 1

Or better yet, I think, since your images are being stored along the first axis, you can just decompose the array into a list of arrays using a list comprehension:

In [37]: images = pickle_data[0]

In [38]: labels = pickle_data[1]

Decompose the array:

In [39]: images = [x for x in images]

In [40]: images[0]
Out[40]:
array([[148, 124, 115],
       [150, 127, 116],
       [154, 129, 121],
       [159, 142, 133],
       [159, 142, 133],
       [161, 145, 142]])

In [41]: images[1]
Out[41]:
array([[165, 136, 145],
       [176, 137, 141],
       [178, 138, 144],
       [199, 163, 171],
       [202, 163, 167],
       [200, 158, 163]])

In [42]: labels[0]
Out[42]: 1

In [43]: labels[1]
Out[43]: 1

In [44]: labels
Out[44]: array([1, 1])

In [45]: images
Out[45]:
[array([[148, 124, 115],
        [150, 127, 116],
        [154, 129, 121],
        [159, 142, 133],
        [159, 142, 133],
        [161, 145, 142]]), array([[165, 136, 145],
        [176, 137, 141],
        [178, 138, 144],
        [199, 163, 171],
        [202, 163, 167],
        [200, 158, 163]])]

In [46]:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM