-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread_idx.m
56 lines (49 loc) · 1.25 KB
/
read_idx.m
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
function data = read_idx(path)
%READ_IDX Read IDX file format
% Usage: data = read_idx(path)
%
% For details about the IDX file format, see the bottom of
% http://yann.lecun.com/exdb/mnist/
%
% Hang Su 2016
endian = 'b'; % try big-endian first
[pathstr, name, ext] = fileparts(path);
if ~isempty(ext),
assert(strcmp(ext,'.gz'), ['Unknown file type: ' ext]);
gunzip(path);
path = fullfile(pathstr, name);
end
assert(logical(exist(path, 'file')));
fid = fopen(path, 'r');
magicN = fread(fid, 1, '*uint32', endian);
if magicN>=2^16, % wrong endianness
magicN = swapbytes(magicN);
endian = 'l';
end
ndim = mod(magicN, 256);
dtype = floor(magicN/256);
switch dtype,
case hex2dec('08'),
dtypeMat = 'uint8';
case hex2dec('09'),
dtypeMat = 'int8';
case hex2dec('0B'),
dtypeMat = 'int16';
case hex2dec('0C'),
dtypeMat = 'int32';
case hex2dec('0D'),
dtypeMat = 'float32';
case hex2dec('0E'),
dtypeMat = 'float64';
otherwise,
error('Unknown data type: 0x%s', dec2hex(dtype,2));
end
sz = fread(fid, [1, ndim], 'int32', endian);
if ndim==1,
sz = [sz 1];
ndim = 2;
end
data = fread(fid, prod(sz), ['*' dtypeMat], endian);
data = reshape(data, fliplr(sz));
data = permute(data, ndim:-1:1);
fclose(fid);