Does loadMNISTI​mages(file​name) accept filename.m ?

Hello,
LoadMNISTImages(filename) is dedicated for files with idx3-ubyte format. l'm wonderning whether it's possible to use this function with .mat format. If not how can we convert the image data to idx3-ubyte format. Thank you

2 commentaires

Guillaume
Guillaume le 19 Juil 2016
I don't think LoadMNISTImages is a function that comes in any of the matlab toolboxes. If so, you'll be better off asking whoever wrote that function for support.
Similarly, idx3-ubyte is not something that standard in matlab.
Mnist dataset is a handwritten digit used for pattern recognition. For further details check this website http://yann.lecun.com/exdb/mnist/ then we have to function for images and their labels
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;
end
et la deuxiéme
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

Connectez-vous pour commenter.

 Réponse acceptée

Guillaume
Guillaume le 19 Juil 2016
I don't really understand what you mean by "using [LoadMNISTImages] with .mat format". It's obviously designed to load a particular file format, so it's certainly not going to use with .mat files. You load .mat files with the built-in matlab function load.
I have no idea what the idx3-ubyte format format is. If you want to transform an image into the same sort of array as returned by LoadMNISTImages:
out = double(yourgrayimage(:)) / 255; %assuming the original image is of type uint8

4 commentaires

MiMad
MiMad le 19 Juil 2016
My purpose is to be able to load my image data using these two functions in order to have the same sort of array as returned by LoadMNISTImages. Actually my images are under the png format
To load a png image, you use imread.
The LoadMNISTImages returns several images as an MxN double array, where M is the number of pixels in the image, and N is the number of image. That is each row of the array is a pixel, each column an image.
To load a bunch of png and have the same output:
pnglist = {'png1.png', 'png2.png', 'png3.png}; %array of filenames
pngimages = cellfun(@imread, pnglist, 'UniformOutput', false);
%assuming the png images are grayscale uint8 images
assert(ndims(pngimages{1}) == 3 %%strcmp(class(pngimages{1}, 'uin8'), 'assumption broken');
pngimages = cat(3, pngimages{:});
pngimages = reshape(pngimages, [], size(pngimages, 3));
MiMad
MiMad le 19 Juil 2016
following these steps l get at the end data with the same format of mnist ?
Guillaume
Guillaume le 19 Juil 2016
Unless I misunderstood something in the code you've attached, yes you get the same.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Convert Image Type dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by