% A script to make a smaller dataset in an ASCII text
% format. Unrealistically small data was required to make our course
% exercises doable in a faster pace.

fname = fullfile('mnist','train-images-idx3-ubyte');
lname = fullfile('mnist','train-labels-idx1-ubyte');
fnameTes = fullfile('mnist','t10k-images-idx3-ubyte');
lnameTes = fullfile('mnist','t10k-labels-idx1-ubyte');


[tr.data, tr.sizes] = idx_ubyte_read(fname,3);
[tr.labels, tr.n] = idx_ubyte_read(lname,1);

[tes.data, tes.sizes] = idx_ubyte_read(fnameTes,3);
[tes.labels, tes.n] = idx_ubyte_read(lnameTes,1);

% Get 1000 samples from the beginning of train (75%) and test (25%) sets.
reduced.data =[ tr.data(1:(750*tr.sizes(2)*tr.sizes(3))) ; ...
               tes.data(1:(250*tes.sizes(2)*tes.sizes(3))) ];
reduced.labels = [tr.labels(1:750)  ; tes.labels(1:250)];

% Re-label '0' as 10 to get labels start from 1
reduced.labels(reduced.labels==0)=10;
output = [reduced.labels reshape(reduced.data, tr.sizes(2)*tr.sizes(3), ...
				 750+250)'];


dlmwrite('mnist_reduced.dat',output,',');

% Make a "Very reduced version" too:
% Get 250 samples from the beginning of train (50%) and test (50%) sets.
reduced.data =[ tr.data(1:(250*tr.sizes(2)*tr.sizes(3))) ; ...
               tes.data(1:(250*tes.sizes(2)*tes.sizes(3))) ];
reduced.labels = [tr.labels(1:750)  ; tes.labels(1:250)];

% Re-label '0' as 10 to get labels start from 1
reduced.labels(reduced.labels==0)=10;
output = [reduced.labels reshape(reduced.data, tr.sizes(2)*tr.sizes(3), ...
				 250+250)'];

dlmwrite('mnist_very_reduced.dat',output,',');



inds=[1:25]

rows = tr.sizes(2);
cols = tr.sizes(3);

% Additionally, make a large image with 4x20 tiles.. I used this for
% my lecture slides.
outrows = 4;
outcols = 20;

ind = 0;
bigpic = [];
for ir = 1:outrows
  bigpicrow = [];
  for ic = 1:outcols
    ind = ind+1;
    ibeg = (ind-1)*rows*cols+1;
    iend = (ind-1)*rows*cols + rows*cols;
    pic = reshape(tr.data(ibeg:iend),rows,cols)';  % Data is row-wise.
    bigpicrow = [bigpicrow pic];
  end
  bigpic = [bigpic ; bigpicrow];
end
saveimage(sprintf('test.ppm'),bigpic,'ppm',1-gray(256));
