-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreadMNIST.m
More file actions
executable file
·62 lines (51 loc) · 1.51 KB
/
Copy pathreadMNIST.m
File metadata and controls
executable file
·62 lines (51 loc) · 1.51 KB
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
57
58
59
60
61
62
function [imgs, labels] = readMNIST(imgFile, labelFile, readDigits, offset)
% Read digits
fid = fopen(imgFile, 'r', 'b');
header = fread(fid, 1, 'int32');
if header ~= 2051
error('Invalid image file header');
end
count = fread(fid, 1, 'int32');
if count < readDigits+offset
error('Trying to read too many digits');
end
h = fread(fid, 1, 'int32');
w = fread(fid, 1, 'int32');
if offset > 0
fseek(fid, w*h*offset, 'cof');
end
% imgs = zeros([h w readDigits]);
imgs=zeros(readDigits,h*w);
for i=1:readDigits
for y=1:h
% imgs(y,:,i) = fread(fid, w, 'uint8');
imgs(i,(28*(y-1)+1):28*y)=fread(fid,w,'uint8');
end
end
fclose(fid);
% Read digit labels
fid = fopen(labelFile, 'r', 'b');
header = fread(fid, 1, 'int32');
if header ~= 2049
error('Invalid label file header');
end
count = fread(fid, 1, 'int32');
if count < readDigits+offset
error('Trying to read too many digits');
end
if offset > 0
fseek(fid, offset, 'cof');
end
labels = fread(fid, readDigits, 'uint8');
fclose(fid);
% Calc avg digit and count
% imgs = trimDigits(imgs, 4);
imgs = normalizePixValue(imgs);
%[avg num stddev] = getDigitStats(imgs, labels);
end
function digits = normalizePixValue(digits)
digits = double(digits);
for i=1:size(digits, 3)
digits(:,:,i) = digits(:,:,i)./255.0;
end
end