如何解压pkl文件? [英] How to unpack pkl file?
问题描述
我有一个来自 MNIST 数据集的 pkl 文件,其中包含手写数字图像.
我想看看这些数字图像中的每一个,所以我需要解压 pkl 文件,但我不知道如何解压.
有没有办法解压/解压pkl文件?
一般情况
您的 pkl
文件实际上是一个序列化的 pickle
文件,这意味着它已使用 Python 的
但这会更难,因为您需要创建一个程序来读取这些文件中的二进制数据.所以我推荐你用Python,用pickle
加载数据.如您所见,这非常容易.;-)
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?
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()
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. ;-)
这篇关于如何解压pkl文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!