以前直接用的是sklearn或者TensorFlow提供的mnist数据集,已经转换为矩阵形式的数据格式。但是sklearn体用的数据集合并不全,一共只有3000+图,每个图是8*8的大小,但是原始数据并不是这样的。 MNIST数据集合的原始网址为:http://yann.lecun.com/exdb/mnist/ 进入官网,发现有4个文件,分别对应训练集、测试集的图像和标签:
官网给的数据集合并不是原始的图像数据格式,而是编码后的二进制格式: 图像的编码为:
典型的head+data模式:前16个字节分为4个整型数据,每个4字节,分别代表:数据信息des、图像数量(img_num),图像行数(row)、图像列数(col),之后的数据全部为像素,每row*col个像素构成一张图,每个色素的值为(0-255)。 标签的编码为:
模式和前面的一样,不同的是head只有8字节,分别为des和标签的数量(label_num).之后每一个字节代表一个标签,值为(0-9)。 弄清楚编码后,就可以直接上代码了:
import numpy as np
import struct
mnist_dir = r'./digit/'
def fetch_mnist(mnist_dir,data_type):
train_data_path = mnist_dir + 'train-images.idx3-ubyte'
train_label_path = mnist_dir + 'train-labels.idx1-ubyte'
test_data_path = mnist_dir + 't10k-images.idx3-ubyte'
test_label_path = mnist_dir + 't10k-labels.idx1-ubyte'
# train_img
with open(train_data_path, 'rb') as f:
data = f.read(16)
des,img_nums,row,col = struct.unpack_from('>IIII', data, 0)
train_x = np.zeros((img_nums, row*col))
for index in range(img_nums):
data = f.read(784)
if len(data) == 784:
train_x[index,:] = np.array(struct.unpack_from('>' + 'B' * (row * col), data, 0)).reshape(1,784)
f.close()
# train label
with open(train_label_path, 'rb') as f:
data = f.read(8)
des,label_nums = struct.unpack_from('>II', data, 0)
train_y = np.zeros((label_nums, 1))
for index in range(label_nums):
data = f.read(1)
train_y[index,:] = np.array(struct.unpack_from('>B', data, 0)).reshape(1,1)
f.close()
# test_img
with open(test_data_path, 'rb') as f:
data = f.read(16)
des, img_nums, row, col = struct.unpack_from('>IIII', data, 0)
test_x = np.zeros((img_nums, row * col))
for index in range(img_nums):
data = f.read(784)
if len(data) == 784:
test_x[index, :] = np.array(struct.unpack_from('>' + 'B' * (row * col), data, 0)).reshape(1, 784)
f.close()
# test label
with open(test_label_path, 'rb') as f:
data = f.read(8)
des, label_nums = struct.unpack_from('>II', data, 0)
test_y = np.zeros((label_nums, 1))
for index in range(label_nums):
data = f.read(1)
test_y[index, :] = np.array(struct.unpack_from('>B', data, 0)).reshape(1, 1)
f.close()
if data_type == 'train':
return train_x, train_y
elif data_type == 'test':
return test_x, test_y
elif data_type == 'all':
return train_x, train_y,test_x, test_y
else:
print('type error')
if __name__ == '__main__':
tr_x, tr_y, te_x, te_y = fetch_mnist(mnist_dir,'all')
import matplotlib.pyplot as plt # plt 用于显示图片
img_0 = tr_x[59999,:].reshape(28,28)
plt.imshow(img_0)
print(tr_y[59999,:])
img_1 = te_x[500,:].reshape(28,28)
plt.imshow(img_1)
print(te_y[500,:])
plt.show()
运行结果: