LeNet5的一个MATLAB实现的代码解析(6)

MNIST数据集下载:http://yann.lecun.com/exdb/mnist/进网站后点击红色的连接就可以下载了。
function [I,labels,I_test,labels_test] = readMNIST(num)%读取MNIST数据集,这个RTFSC
%readMNIST MNIST handwriten image database reading.
%
% Syntax
%
% [I,labels,I_test,labels_test] = readMNIST(num)
%
% Description
% Input:
% id - cell array of images 28x28 size
% n - number of images to process
% labels - cell array of labels, corresponding to images
% rand_on - parameter, defining if it is necessary to randomly pick a
% pairs of image/label
% Output:
% I - cell array of training images 28x28 size
% labels - vector of labels (true digits) for training set
% I_test - cell array of testing images 28x28 size
% labels_test - vector of labels (true digits) for testing set
%
%© Sirotenko Mikhail, 2009
%===========Loading training set

%Check if we have MNIST dataset

path = ‘./MNIST/train-images.idx3-ubyte’;
%path = ‘D:/MatLab/MatLab/work/CNN/LeNet5-MNIST-master/MNIST/train-images.idx3-ubyte’;
if(~exist(path,‘file’))
error(‘Training set of MNIST not found. Please download it from http://yann.lecun.com/exdb/mnist/ and put to ./MNIST folder’);
end
fid = fopen(path,‘r’,‘b’); %big-endian
magicNum = fread(fid,1,‘int32’); %Magic number
if(magicNum~=2051)
display(‘Error: cant find magic number’);
return;
end
imgNum = fread(fid,1,‘int32’); %Number of images
rowSz = fread(fid,1,‘int32’); %Image height
colSz = fread(fid,1,‘int32’); %Image width

if(num<imgNum)
imgNum=num;
end

for k=1:imgNum
I{k} = uint8(fread(fid,[rowSz colSz],‘uchar’));
end
fclose(fid);

%============Loading labels
path = ‘./MNIST/train-labels.idx1-ubyte’;
if(~exist(path,‘file’))
error(‘Training labels of MNIST not found. Please download it from http://yann.lecun.com/exdb/mnist/ and put to ./MNIST folder’);
end
fid = fopen(path,‘r’,‘b’); %big-endian
magicNum = fread(fid,1,‘int32’); %Magic number
if(magicNum~=2049)
display(‘Error: cant find magic number’);
return;
end
itmNum = fread(fid,1,‘int32’); %Number of labels

if(num<itmNum)
itmNum=num;
end

labels = uint8(fread(fid,itmNum,‘uint8’)); %Load all labels

fclose(fid);

%============All the same for test set
%path = ‘./MNIST/t10k-images.idx’;
path = ‘./MNIST/t10k-images.idx3-ubyte’;
if(~exist(path,‘file’))
error(‘Test images of MNIST not found. Please download it from http://yann.lecun.com/exdb/mnist/ and put to ./MNIST folder’);
end

fid = fopen(path,‘r’,‘b’);
magicNum = fread(fid,1,‘int32’);
if(magicNum~=2051)
display(‘Error: cant find magic number’);
return;
end
imgNum = fread(fid,1,‘int32’);
rowSz = fread(fid,1,‘int32’);
colSz = fread(fid,1,‘int32’);

if(num<imgNum)
imgNum=num;
end

for k=1:imgNum
I_test{k} = uint8(fread(fid,[rowSz colSz],‘uchar’));
end
fclose(fid);

%============Test labels
path = ‘./MNIST/t10k-labels.idx1-ubyte’;
if(~exist(path,‘file’))
error(‘Test labels of MNIST not found. Please download it from http://yann.lecun.com/exdb/mnist/ and put to ./MNIST folder’);
end

fid = fopen(path,‘r’,‘b’);
magicNum = fread(fid,1,‘int32’);
if(magicNum~=2049)
display(‘Error: cant find magic number’);
return;
end
itmNum = fread(fid,1,‘int32’);
if(num<itmNum)
itmNum=num;
end
labels_test = uint8(fread(fid,itmNum,‘uint8’));

fclose(fid);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值