gpt4 book ai didi

python - 如何从 infimnist/mnist8m 数据集中读取图像和标签?

转载 作者:行者123 更新时间:2023-12-01 07:46:48 26 4
gpt4 key购买 nike

使用此链接中的程序,https://leon.bottou.org/projects/infimnist ,我生成了一些数据。

据我所知,它是某种二进制格式:

b"\x00\x00\x08\x01\x00\x00'\x10\x07\x02\x01\x00\x04\x01\x04\t\x05 ...

我需要从这样的两个数据集中提取标签和图片,生成方式:

https://leon.bottou.org/projects/infimnist

with open("test10k-labels", "rb") as binary_file:
data = binary_file.read()
print(data)

>>> b"\x00\x00\x08\x01\x00\x00'\x10\x07\x02\x01\x00\x04\x01\x04\t\x05 ...

b"\x00\x00\x08\x01 ...".decode('ascii')

>>> "\x00\x00\x08\x01 ..."

我也尝试了 binascii 包,但没有成功。

感谢您的帮助!

创建数据

要创建我所说的数据集,请从以下链接下载包:https://leon.bottou.org/projects/infimnist .

$ cd dir_of_folder
$ make

然后我获取了弹出的 infimnist 可执行文件的路径:

$ app_path lab 10000 69999 > mnist60k-labels-idx1-ubyte

这应该将我使用的文件放在文件夹中。

app_path后面的命令可以替换为他在侧面列出的任何其他命令。

最终更新

它有效!使用一些 numpy 函数可以将图像返回到正常方向。

# for the labels
with open(path, "rb") as binary_file:
y_train = np.array(array("B", binary_file.read()))

# for the images
with open("images path", "rb") as binary_file:
images = []
emnistRotate = True
magic, size, rows, cols = struct.unpack(">IIII", binary_file.read(16))
if magic != 2051:
raise ValueError('Magic number mismatch, expected 2051,''got {}'.format(magic))
for i in range(size):
images.append([0] * rows * cols)
image_data = array("B", binary_file.read())
for i in range(size):
images[i][:] = image_data[i * rows * cols:(i + 1) * rows * cols]

# for some reason EMNIST is mirrored and rotated
if emnistRotate:
x = image_data[i * rows * cols:(i + 1) * rows * cols]

subs = []
for r in range(rows):
subs.append(x[(rows - r) * cols - cols:(rows - r)*cols])

l = list(zip(*reversed(subs)))
fixed = [item for sublist in l for item in sublist]
images[i][:] = fixed
x = []
for image in images:
x.append(np.rot90(np.flip(np.array(image).reshape((28,28)), 1), 1))
x_train = np.array(x)

如此简单的事情的疯狂解决方案:)

最佳答案

好吧,看一下python-mnist源码,似乎解压二进制格式的正确方法如下:

from array import array
with open("test10k-labels", "rb") as binary_file:
magic, size = struct.unpack(">II", file.read(8))
if magic != 2049:
raise ValueError("Magic number mismatch, expected 2049,got{}".format(magic))
labels = array("B", binary_file.read())
print(labels)

更新

所以我还没有对此进行广泛的测试,但以下代码应该可以工作。它是从上述 python-mnist 中获取并修改的,请参阅 source

from array import array
import struct
with open("mnist8m-patterns-idx3-ubyte", "rb") as binary_file:
images = []
emnistRotate = True
magic, size, rows, cols = struct.unpack(">IIII", binary_file.read(16))
if magic != 2051:
raise ValueError('Magic number mismatch, expected 2051,''got {}'.format(magic))
for i in range(size):
images.append([0] * rows * cols)
image_data = array("B", binary_file.read())
for i in range(size):
images[i][:] = image_data[i * rows * cols:(i + 1) * rows * cols]

# for some reason EMNIST is mirrored and rotated
if emnistRotate:
x = image_data[i * rows * cols:(i + 1) * rows * cols]

subs = []
for r in range(rows):
subs.append(x[(rows - r) * cols - cols:(rows - r)*cols])

l = list(zip(*reversed(subs)))
fixed = [item for sublist in l for item in sublist]
images[i][:] = fixed
print(images)

上一个答案:

您可以使用python-mnist库:

from mnist import MNIST
mndata = MNIST('./data')
images, labels = mndata.load_training()

关于python - 如何从 infimnist/mnist8m 数据集中读取图像和标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56424829/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com