Jo.Safarzade
Jo.Safarzade

Reputation: 43

How To Use .idx3 File In Matlab?

I work on an OCR project with Matlab and i found out that there is character sample database named MNIST handwritten digit database. I download the file named train-images.idx3-ubyte but i have no idea how to use it does anyone one know how to use this file ?

Upvotes: 1

Views: 10329

Answers (3)

user7630859
user7630859

Reputation: 1

You Can read MNISTImages like this-

trlblid = fopen('train-labels.idx1-ubyte');    
trimgid = fopen('train-images.idx3-ubyte');    
tslblid = fopen('t10k-labels.idx1-ubyte');    
tsimgid = fopen('t10k-images.idx3-ubyte');    

% read train labels    
fread(trlblid, 4);    
numtrlbls = toint(fread(trlblid, 4));    
trainlabels = fread(trlblid, numtrlbls);    

% read train data    
fread(trimgid, 4);    
numtrimg = toint(fread(trimgid, 4));    
trimgh = toint(fread(trimgid, 4));    
trimgw = toint(fread(trimgid, 4));    
trainimages = permute(reshape(fread(trimgid,trimgh*trimgw*numtrimg),trimgh,trimgw,numtrimg), [2 1 3]);    

% read test labels    
fread(tslblid, 4);    
numtslbls = toint(fread(tslblid, 4));    
testlabels = fread(tslblid, numtslbls);    

% read test data    
fread(tsimgid, 4);    
numtsimg = toint(fread(tsimgid, 4));    
tsimgh = toint(fread(tsimgid, 4));    
tsimgw = toint(fread(tsimgid, 4));    
testimages = permute(reshape(fread(tsimgid, tsimgh*tsimgw*numtsimg),tsimgh,tsimgw,numtsimg), [2 1 3]);    

Upvotes: 0

ajaykarpur
ajaykarpur

Reputation: 318

You can use the mnistHelper functions from Stanford.

As an example of how to use these functions, you can check the images and labels using the following code:

% Change the filenames if you have saved the files under different names
% On some platforms, the files might be saved as 
% train-images.idx3-ubyte / train-labels.idx1-ubyte

images = loadMNISTImages('train-images-idx3-ubyte');
labels = loadMNISTLabels('train-labels-idx1-ubyte');

% We are using display_network from the autoencoder code

display_network(images(:,1:100)); % Show the first 100 images
disp(labels(1:10));

Upvotes: 2

yuk
yuk

Reputation: 19880

Those files you download from MNIST database are binary files. You can find their format on MNIST website: http://yann.lecun.com/exdb/mnist/

Use low-level file I/O functions in MATLAB like fopen, fclose, fread, fseek, etc to read the files following their format.

You can also try to use readMNIST function from FileExchange. I don't have any experience with it, and some users seem to have some problems with it, but you can see the code and debug it.

Upvotes: 2

Related Questions