Download this file

48 lines (38 with data), 1.4 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
function patches = sampleIMAGES(IMAGES,patchsize,norm_ena)
% sampleIMAGES
% inputs
% numpatches: for example 1E4
% patchsize% for example 8*8
% IMAGES: images in a 3D matrix
% output
% patches % a vector of randomly chosen patches
if nargin==2
norm_ena=1;
end
visibleSize = patchsize*patchsize; % number of input units
% get size and number of images
[xn, yn, zn]=size(IMAGES);
scale=patchsize/xn;
imgs = imresize(IMAGES, scale);
patches=(reshape(imgs,visibleSize,zn));
%% ---------------------------------------------------------------
% For the autoencoder to work well we need to normalize the data
% Specifically, since the output of the network is bounded between [0,1]
% (due to the sigmoid activation function), we have to make sure
% the range of pixel values is also bounded between [0,1]
if norm_ena==1
patches = normalizeData(patches);
end
end
%% ---------------------------------------------------------------
function patches = normalizeData(patches)
% Squash data to [0.1, 0.9] since we use sigmoid as the activation
% function in the output layer
% Remove DC (mean of images).
patches = bsxfun(@minus, patches, mean(patches));
% Truncate to +/-3 standard deviations and scale to -1 to 1
pstd = 3 * std(patches(:));
patches = max(min(patches, pstd), -pstd) / pstd;
% Rescale from [-1,1] to [0.1,0.9]
patches = (patches + 1) * 0.4 + 0.1;
end