-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetupMNIST.m
More file actions
155 lines (116 loc) · 4.55 KB
/
setupMNIST.m
File metadata and controls
155 lines (116 loc) · 4.55 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function [Ytrain,Ctrain,Ytest,Ctest] = setupMNIST(nTrain,nTest)
% [Ytrain,Ctrain,Ytest,Ctest] = setupMNIST(nTrain,nTest)
%
%
% Output:
% Ytrain - nTrain 28x28 training images in tensor (28,28,nTrain)
% Ctrain - corresponding training classes (10, nTrain)
% Ytest - nTest 28x28 test images in tensor (28,28,nTest)
% Ctest - corresponding test classes (10, nTest)
%
if nargin==0
runMinimalExample;
return;
end
if not(exist('nTrain','var')) || isempty(nTrain)
nTrain = 50000;
end
if not(exist('nTest','var')) || isempty(nTest)
nTest = round(nTrain/5);
end
if not(exist('train-images.idx3-ubyte','file')) ||...
not(exist('train-labels.idx1-ubyte','file')) || ...
not(exist('t10k-images-idx3-ubyte','file')) || ...
not(exist('t10k-labels-idx1-ubyte','file'))
warning('MNIST data cannot be found in MATLAB path')
dataDir = [fileparts(which('driverWeightDecay.m')) filesep 'data' filesep 'MNIST'];
if not(exist(dataDir,'dir'))
mkdir(dataDir);
end
doDownload = input(sprintf('Do you want to download http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz (around 10 MB) to %s? Y/N [Y]: ',dataDir),'s');
if isempty(doDownload) || strcmp(doDownload,'Y')
if not(exist(dataDir,'dir'))
mkdir(dataDir);
end
imgz = websave(fullfile(dataDir,'train-images.idx3-ubyte.gz'),'http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz');
gunzip(imgz);
delete(imgz)
imgz = websave(fullfile(dataDir,'train-labels.idx1-ubyte.gz'),'http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz');
gunzip(imgz);
delete(imgz)
imgz = websave(fullfile(dataDir,'t10k-images-idx3-ubyte.gz'),'http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz');
gunzip(imgz);
delete(imgz)
imgz = websave(fullfile(dataDir,'t10k-labels-idx1-ubyte.gz'),'http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz');
gunzip(imgz);
delete(imgz)
addpath(dataDir);
else
error('MNNIST data not available. Please make sure it is in the current path');
end
end
images = reshape ( loadMNISTImages('train-images.idx3-ubyte') , 28,28,1,[] );
labels = loadMNISTLabels('train-labels.idx1-ubyte');
% get class probability matrix
C = zeros(10,numel(labels));
ind = sub2ind(size(C),labels+1,(1:numel(labels))');
C(ind) = 1;
idx = randperm(size(C,2));
idTrain = idx(1:nTrain);
Ytrain = images(:,:,:,idTrain);
Ctrain = C(:,idTrain);
if nargout>2
images = reshape ( loadMNISTImages('t10k-images-idx3-ubyte') , 28,28,1,[] );
labels = loadMNISTLabels('t10k-labels-idx1-ubyte');
Ctest = zeros(10,numel(labels));
ind = sub2ind(size(Ctest),labels+1,(1:numel(labels))');
Ctest(ind) = 1;
idx = randperm(size(Ctest,2));
idTest = idx(1:nTest);
Ytest = images(:,:,:,idTest);
Ctest = Ctest(:,idTest);
end
function runMinimalExample
[Yrain,~,Ytest,~] = feval(mfilename,50,10);
figure(1);clf;
subplot(2,1,1);
montageArray(Yrain,10);
axis equal tight
colormap(flipud(colormap('gray')))
colorbar
title('training images');
subplot(2,1,2);
montageArray(Ytest,10);
axis equal tight
colormap(flipud(colormap('gray')))
colorbar
title('test images');
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);
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;