Reading MNIST image database binary in MATLAB

I have a binary file from the MNIST image database renamed to "myfile.dat". This consists of a set of 4 unsigned 32-bit integers followed by a chain of unsigned 8-bit integers. I want to read this file and save its contents as an array. Here is my code:

file_id = fopen('myfile.dat', 'rb');
data = fread(file_id, 'int');
size(data)
class(data)

And the result:

ans =

    2502           1


ans =

double

Size (2502, 1) meets expectations. But why does this tell me that data doublewhen I indicated what it is int?

I know what the first few numbers should be like, and the output is not as expected. I also tried int32, uintand uint32that give the same problem.

+4
1

, MATLAB 4 , little-endian, MNIST big-endian. , , 0x00, 0x00, 0x08, 0x03, . MATLAB 0x03, 0x08, 0x00, 0x00. , 50855936, , .

, , , uint8. . , , - , . .

swapbytes, , . 1 uint32, , big-endian. , uint32, MATLAB double, , swapbytes.

, numRows x numCols , , . . .

clear all;
close all;

%//Open file
fid = fopen('t10k-images-idx3-ubyte', 'r');

%//Read in magic number
%//A = fread(fid, 4, 'uint8');
%//magicNumber = sum(bitshift(A', [24 16 8 0]));

%//OR
A = fread(fid, 1, 'uint32');
magicNumber = swapbytes(uint32(A));

%//Read in total number of images
%//A = fread(fid, 4, 'uint8');
%//totalImages = sum(bitshift(A', [24 16 8 0]));

%//OR
A = fread(fid, 1, 'uint32');
totalImages = swapbytes(uint32(A));

%//Read in number of rows
%//A = fread(fid, 4, 'uint8');
%//numRows = sum(bitshift(A', [24 16 8 0]));

%//OR
A = fread(fid, 1, 'uint32');
numRows = swapbytes(uint32(A));

%//Read in number of columns
%//A = fread(fid, 4, 'uint8');
%//numCols = sum(bitshift(A', [24 16 8 0]));

%// OR
A = fread(fid, 1, 'uint32');
numCols = swapbytes(uint32(A));

%//For each image, store into an individual cell
imageCellArray = cell(1, totalImages);
for k = 1 : totalImages
    %//Read in numRows*numCols pixels at a time
    A = fread(fid, numRows*numCols, 'uint8');
    %//Reshape so that it becomes a matrix
    %//We are actually reading this in column major format
    %//so we need to transpose this at the end
    imageCellArray{k} = reshape(uint8(A), numCols, numRows)';
end

%//Close the file
fclose(fid);

( numRows, numCols), ( magicNumber) ( totalImages), 2051, 28, 28 10000 . kth imageCellArray kth MNIST. imshow(imageCellArray{k});, k - 1 10000, .

, : double, , uint8, .

!

+3

Source: https://habr.com/ru/post/1543906/


All Articles