Reputation: 179
I have finished writing a .pkl file but error appears when I try to load it.
f = open('train.pkl')
X, Y, X_test, Y_test = cPickle.load(f)
or
X, Y ,X_test,Y_test=pickle.load(open("train.pkl","rb"))
Both attempts failed. The code for writing the .pkl file is as below. Inside the loop to process the images and labels:
img_ndarray = numpy.asarray(img, dtype='float64')
img_raw[i] = numpy.ndarray.flatten(img_ndarray)
img_label[i]=numpy.asarray(name,dtype='float64')
i=i+1
Outside the loop to write the pickle file:
write_file = open('train.pkl', 'wb')
cPickle.dump([[img_raw[0:4],img_label[0:4]], [img_raw[5:7], img_label[5:7]]],write_file, -1)
write_file.close()
Thank you for any input.
Upvotes: 1
Views: 665
Reputation: 96360
The error message should make this obvious, you've pickled a list with 2 elements, so you can't unpack that into 4 names. Consider (making up some data):
In [35]: img_raw, img_label = range(10), 'abcdefghhijlmnop'
In [36]: [[img_raw[0:4],img_label[0:4]], [img_raw[5:7], img_label[5:7]]]
Out[36]: [[range(0, 4), 'abcd'], [range(5, 7), 'fg']]
In [37]: len([[img_raw[0:4],img_label[0:4]], [img_raw[5:7], img_label[5:7]]])
Out[37]: 2
However, this is easily remedied, since each of the sublists has 2 items in them too:
In [38]: (X, Y) , (X_test, Y_test) = [[img_raw[0:4],img_label[0:4]], [img_raw[5:7], img_label[5:7]]]
And it works!
In [41]: X
Out[41]: range(0, 4)
In [42]: Y
Out[42]: 'abcd'
In [43]: X_test
Out[43]: range(5, 7)
In [44]: Y_test
Out[44]: 'fg'
In general, what you unpack into has to have the same shape:
In [45]: data = [[[1, 2], [3, 4, 5]],[6, [7, 8]]]
In [46]: (((a, b), (c, d, e)), (f, (g, h))) = data
In [47]: a,b,c,d,e,f,g,h
Out[47]: (1, 2, 3, 4, 5, 6, 7, 8)
Upvotes: 1