% Matlab/Octave code that produces unlabeled training data for deep belief networks and 
% shows a random image and its parameters. 
%
% The code requires the file "StoianovZorzi2012_data.mat", which contains unlabeled binary images and 
% corrisponding labels (in a separate matrix). The latter could be used for further analysis. 
% Variables:
% 1) data_images : 2D matrix, the rows of which contain vectorized images of size 30x30 pixels (900 pixels in total)
% 2) dat_labels  : 2D matrix that contains corrisponding labels useful for analysis of the result. 
%                     The first column contains the numerosity of each image 
%                     (i.e., corresponding row in the data_images matrix)
%                     The second column contains the corresponding cumulative surface
% 3) data_info   : contains the range of numerosity and surface, as well as the image size
%
% Bellow are provided bits of code that could be used to: 
% (A) produce/save training data as a 3D matrix, and (B) visualize a particular image and its parameters.
% 
% Author of this MATLAB/Octave script: 
%  Ivilin P. Stoianov, Computational Cognitive Neuroscience Lab, 
%  Department of General Psychology, University of Padova, Italy
%  Date: 12 April 2013
%  Contact: ivilin.stoianov@gmail.com
%  Websites: http://ccnl.psy.unipd.it   &  http://www.stoianov.it
%
% Granted permissions:
% (a) to use the image data contained in "StoianovZorzi2012_data.mat" and this script if citing the article 
%     Stoianov, I., and Zorzi, M. (2012). Emergence of a “visual number sense” in hierarchical generative models.
%     Nature neuroscience 15, 194–6.
% (b) to use the parallel learning algorithms provided in the site http://ccnl.psy.unipd.it/research/deeplearning 
%     if citing the corresponding article: Testolin, Stoianov, De Filippo De Grazia, and Zorzi (2013).
%     "Deep unsupervised learning on a desktop PC: A primer for cognitive scientists", Frontiers in Cognitive Science.
%
%
%% (A) Code that converts the unlabeled image data into 3D training data 
%
load([pwd filesep 'StoianovZorzi2012_data.mat']);
% Problem dimensions
batchsize=100;                                          % !!! HERE DEFINE the batch size !!!
numdims=size(data_images,2);                            % Size of vectorized images
totnum =size(data_images,1);                            % Total number of images
numbatches=ceil(totnum/batchsize);                      % Calcualte batch size
%
% Convert images into 3D batch data
batchdata  = zeros(batchsize, numdims, numbatches,'single');  % Empty 3D matrixes for batches.
randomorder=randperm(totnum);                           % (Index of all data reordered
for b=1:numbatches                                      % Distribute 2D vectorized image data to 3D batch data
  Iminibatch=randomorder(1+(b-1)*batchsize:b*batchsize);% Index of one batch
  batchdata(:,:,b) = data_images(Iminibatch, :);        % Copy this batch data in the 3D matrix 
end
%
% Save the 3D batchdata in a new file (e.g., StoianovZorzi2012_batchsize100.mat)
save('-v7', sprintf('StoianovZorzi2012_batchsize%d.mat',batchsize), 'batchdata');
%
% Now you are ready to perform deep learning, by directly running the training procedure (i.e., using the variable "batchdata") 
% or first loading this variable from the newly created file (e.g., StoianovZorzi2012_batchsize100.mat)

%
%% (B) Code that shows a randomly selected image and its parameters
%
figure(1);
i=ceil(rand*size(data_images,1));                       % Select a random image to show 
imagesc(reshape(data_images(i,:),30,30));               % Reshape a vectorized image and show it
axis square;
title(sprintf('Image %d containing %d rectangles with total surface area of %d pixles',i,data_labels(i,1),data_labels(i,2)));
