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);