「MNIST」手写数字数据集下载并转换为图片格式(.png)

我是 雪天鱼,一名FPGA爱好者,研究方向是FPGA架构探索和数字IC设计。

关注公众号【集成电路设计教程】,获取更多学习资料,并拉你进“IC设计交流群”。
QQIC设计&FPGA&DL交流群 群号:866169462

一、下载 MNIST 数据集

官方链接http://yann.lecun.com/exdb/mnist/


依次下载上图红框中四个压缩包。

二、格式转换

将下载好的数据集放在同一文件夹下,并全部解压。

训练集转换代码:

import numpy as np
import struct
 
from PIL import Image
import os

dataset_path = 'C:/Users/Administrator/Desktop/MNIST/'  # 需要修改的路径:解压的数据集所在文件夹
data_file = dataset_path + 'train-images.idx3-ubyte' 

# It's 47040016B, but we should set to 47040000B
data_file_size = 47040016
data_file_size = str(data_file_size - 16) + 'B'
 
data_buf = open(data_file, 'rb').read()
 
magic, numImages, numRows, numColumns = struct.unpack_from('>IIII', data_buf, 0)
datas = struct.unpack_from('>' + data_file_size, data_buf, struct.calcsize('>IIII'))
datas = np.array(datas).astype(np.uint8).reshape(numImages, 1, numRows, numColumns)
 
label_file = dataset_path + 'train-labels.idx1-ubyte' 

# It's 60008B, but we should set to 60000B
label_file_size = 60008
label_file_size = str(label_file_size - 8) + 'B'
 
label_buf = open(label_file, 'rb').read()
 
magic, numLabels = struct.unpack_from('>II', label_buf, 0)
labels = struct.unpack_from('>' + label_file_size, label_buf, struct.calcsize('>II'))
labels = np.array(labels).astype(np.int64)
 
train_path = dataset_path + 'mnist_train' # 转换后的训练集所在路径
if not os.path.exists(train_path):
    os.mkdir(train_path)
	
# 新建 0~9 十个文件夹,存放转换后的图片
for i in range(10): 
    file_name = train_path + os.sep + str(i)
    if not os.path.exists(file_name):
        os.mkdir(file_name)
 
for ii in range(numLabels):
    img = Image.fromarray(datas[ii, 0, 0:28, 0:28])
    label = labels[ii]
    file_name = train_path + os.sep + str(label) + os.sep + str(ii) + '.png'
    img.save(file_name)

测试集转换代码(其实思路是一样的):

import numpy as np
import struct
 
from PIL import Image
import os

dataset_path = 'C:/Users/Administrator/Desktop/MNIST/'  # 需要修改的路径:解压的数据集所在文件夹
data_file = dataset_path + 't10k-images.idx3-ubyte' 
 
# It's 7840016B, but we should set to 7840000B
data_file_size = 7840016
data_file_size = str(data_file_size - 16) + 'B'
 
data_buf = open(data_file, 'rb').read()
 
magic, numImages, numRows, numColumns = struct.unpack_from('>IIII', data_buf, 0)
datas = struct.unpack_from('>' + data_file_size, data_buf, struct.calcsize('>IIII'))
datas = np.array(datas).astype(np.uint8).reshape(numImages, 1, numRows, numColumns)
 
label_file = dataset_path + 't10k-labels.idx1-ubyte'
 
# It's 10008B, but we should set to 10000B
label_file_size = 10008
label_file_size = str(label_file_size - 8) + 'B'
 
label_buf = open(label_file, 'rb').read()
 
magic, numLabels = struct.unpack_from('>II', label_buf, 0)
labels = struct.unpack_from('>' + label_file_size, label_buf, struct.calcsize('>II'))
labels = np.array(labels).astype(np.int64)
 
test_path = dataset_path + 'mnist_test' 
if not os.path.exists(test_path):
    os.mkdir(test_path)

# 新建 0~9 十个文件夹,存放转换后的图片
for i in range(10):
    file_name = test_path + os.sep + str(i)
    if not os.path.exists(file_name):
        os.mkdir(file_name)
 
for ii in range(numLabels):
    img = Image.fromarray(datas[ii, 0, 0:28, 0:28])
    label = labels[ii]
    file_name = test_path + os.sep + str(label) + os.sep  + str(ii) + '.png'
    img.save(file_name)

转换结果:

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
像素为 28*28 。

评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雪天鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值