我是 雪天鱼,一名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 。