Using the MNIST Dataset

From Ufldl

Jump to: navigation, search
(Introduction)
Line 5: Line 5:
=== Usage ===
=== Usage ===
-
The image and label data is stored in a binary format described on the website. For your convenience, two MATLAB functions for extracting the data are provided below.
+
The image and label data is stored in a binary format described on the website. For your convenience, we have provided two MATLAB helper functions for extracting the data. These functions are available at [[http://ufldl.stanford.edu/wiki/resources/mnistHelper.zip]].
-
 
+
-
For loading the images:
+
-
<syntaxhighlight>
+
-
function images = loadMNISTImages(filename)
+
-
%loadMNISTImages returns a 28x28x[number of MNIST images] matrix
+
-
%containing the 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);
+
-
 
+
-
end
+
-
</syntaxhighlight>
+
-
 
+
-
For loading the labels:
+
-
<syntaxhighlight>
+
-
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
+
-
</syntaxhighlight>
+
As an example of how to use these functions, you can check the images and labels using the following code:
As an example of how to use these functions, you can check the images and labels using the following code:

Revision as of 04:48, 13 April 2011

Personal tools