Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unpack pkl file?

I have a pkl file from MNIST dataset, which consists of handwritten digit images.

I'd like to take a look at each of those digit images, so I need to unpack the pkl file, except I can't find out how.

Is there a way to unpack/unzip pkl file?

like image 799
ytrewq Avatar asked Jul 23 '14 08:07

ytrewq


People also ask

Can we open PKL file?

You need a suitable software like Python to open a PKL file. Without proper software you will receive a Windows message "How do you want to open this file?" or "Windows cannot open this file" or a similar Mac/iPhone/Android alert.

How do I extract a pickle file?

To retrieve pickled data, the steps are quite simple. You have to use pickle. load() function to do that. The primary argument of pickle load function is the file object that you get by opening the file in read-binary (rb) mode.


2 Answers

Generally

Your pkl file is, in fact, a serialized pickle file, which means it has been dumped using Python's pickle module.

To un-pickle the data you can:

import pickle   with open('serialized.pkl', 'rb') as f:     data = pickle.load(f) 

For the MNIST data set

Note gzip is only needed if the file is compressed:

import gzip import pickle   with gzip.open('mnist.pkl.gz', 'rb') as f:     train_set, valid_set, test_set = pickle.load(f) 

Where each set can be further divided (i.e. for the training set):

train_x, train_y = train_set 

Those would be the inputs (digits) and outputs (labels) of your sets.

If you want to display the digits:

import matplotlib.cm as cm import matplotlib.pyplot as plt   plt.imshow(train_x[0].reshape((28, 28)), cmap=cm.Greys_r) plt.show() 

mnist_digit

The other alternative would be to look at the original data:

http://yann.lecun.com/exdb/mnist/

But that will be harder, as you'll need to create a program to read the binary data in those files. So I recommend you to use Python, and load the data with pickle. As you've seen, it's very easy. ;-)

like image 162
Peque Avatar answered Sep 22 '22 22:09

Peque


Handy one-liner

pkl() (   python -c 'import pickle,sys;d=pickle.load(open(sys.argv[1],"rb"));print(d)' "$1" ) pkl my.pkl 

Will print __str__ for the pickled object.

The generic problem of visualizing an object is of course undefined, so if __str__ is not enough, you will need a custom script.