【机器学习】MATLAB读取mnist数据库

原文出处:http://blog.csdn.net/tracer9/article/details/51253604

最近要做《优化理论基础》的课程大作业,需要用到mnist这个手写识别数据库,在网上查了一下如何使用,分享在这里,以飨读者。

mnist是纽约大学(NYU)Yann Lecun在上个世纪90年代做的一个关于手写数字识别的数据库。该数据库提出的Motivation是为了解决美国邮政zip code机器识别的问题。经过十几年的发展,目前手写数字识别的识别率已经高达99%+,真正地做到了商业化。

在Yann Lecun教授主页上有对mnist数据集的简单说明。首先明确一下,数据集中有哪几个文件。

  1. train-images-idx3-ubyte.gz: 训练数据集数据
  2. train-labels-idx1-ubyte.gz: 训练数据集标签
  3. t10k-images-idx3-ubyte.gz: 测试数据集数据
  4. t10k-labels-idx1-ubyte.gz: 测试数据集标签

注意到上述4个文件都是使用Linux系统下gunzip打包好的.gz文件,同时,可能是由于网络上的原因吧,这几个文件虽然不大,但是笔者还是下载了一会儿的。为了方便后来者,笔者将解压好的文件放在了我的百度云盘上(百度网盘-链接不存在),提供给大家下载。

下面说明如何使用mnist数据库:

本文参考了斯坦福大学Andrew Ng教授的课件,通过loadMNISTImages.m和loadMNISTLabels.m两个文件读取mnist数据集。

loadMNISTImages.m

function images = loadMNISTImages(filename)
%loadMNISTImages returns a 28x28x[number of MNIST images] matrix containing
%the raw MNIST images

fp = fopen(filename, 'rb');
assert(fp ~= -1, ['Could not open ', filename, '']);

magic = fread(fp, 1, 'int32', 0, 'ieee-be');
assert(magic == 2051, ['Bad magic number in ', filename, '']);

numImages = fread(fp, 1, 'int32', 0, 'ieee-be');
numRows = fread(fp, 1, 'int32', 0, 'ieee-be');
numCols = fread(fp, 1, 'int32', 0, 'ieee-be');

images = fread(fp, inf, 'unsigned char');
images = reshape(images, numCols, numRows, numImages);
images = permute(images,[2 1 3]);

fclose(fp);

% Reshape to #pixels x #examples
images = reshape(images, size(images, 1) * size(images, 2), size(images, 3));
% Convert to double and rescale to [0,1]
images = double(images) / 255;

end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

loadMNISTLabels.m

function labels = loadMNISTLabels(filename)
%loadMNISTLabels returns a [number of MNIST images]x1 matrix containing
%the labels for the MNIST images

fp = fopen(filename, 'rb');
assert(fp ~= -1, ['Could not open ', filename, '']);

magic = fread(fp, 1, 'int32', 0, 'ieee-be');
assert(magic == 2049, ['Bad magic number in ', filename, '']);

numLabels = fread(fp, 1, 'int32', 0, 'ieee-be');

labels = fread(fp, inf, 'unsigned char');

assert(size(labels,1) == numLabels, 'Mismatch in label count');

fclose(fp);

end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

可以看到,上述两个脚本的核心都是,先通过fopen得到文件句柄fid,在偏移得到魔数(magic)以及一些其他的信息,最终得到所有的数据,并将数据reshape到相应的维度(图片都是28x28大小的)。

在mnist数据库中,Lecun教授已经对该数据库进行了一定的说明,如下所示:

这里写图片描述

这里写图片描述

可见,我们的文件是一个binary的文件,在第一个偏移是魔数,我猜测作用应该是为了作校验。其中label是2049,而image是2051。之后的偏移量是图片数、行数、列数。之后就是pixel的值了。pixel的值是0~255的0。0(白色)作为背景、255(黑色)作为前景。

本文介绍了如何在MATLAB中使用,你学会了么?

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

rs勿忘初心

您的鼓励将是我的最大创动原动力

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

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

打赏作者

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

抵扣说明:

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

余额充值