[1422d3]: / functions / functions_PCANet / PCA_fun / PCANet_train.m

Download this file

109 lines (81 with data), 3.9 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
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
function [V, PCANet] = PCANet_train(InImg, PCANet, fidLogs, param, numCoresFeatExtr)
% =======INPUT=============
% InImg Input images (cell); each cell can be either a matrix (Gray) or a 3D tensor (RGB)
% PCANet PCANet parameters (struct)
% .PCANet.NumStages
% the number of stages in PCANet; e.g., 2
% .PatchSize
% the patch size (filter size) for square patches; e.g., [5 3]
% means patch size equalt to 5 and 3 in the first stage and second stage, respectively
% .NumFilters
% the number of filters in each stage; e.g., [16 8] means 16 and
% 8 filters in the first stage and second stage, respectively
% .HistBlockSize
% the size of each block for local histogram; e.g., [10 10]
% .BlkOverLapRatio
% overlapped block region ratio; e.g., 0 means no overlapped
% between blocks, and 0.3 means 30% of blocksize is overlapped
% .Pyramid
% spatial pyramid matching; e.g., [1 2 4], and [] if no Pyramid
% is applied
% IdtExt a number in {0,1}; 1 do feature extraction, and 0 otherwise
% =======OUTPUT============
% f PCANet features (each column corresponds to feature of each image)
% V learned PCA filter banks (cell)
% BlkIdx index of local block from which the histogram is compuated
% =========================
% addpath('./Utils')
if length(PCANet.NumFilters)~= PCANet.NumStages
fprintf_pers(fidLogs, 'Length(PCANet.NumFilters)~=PCANet.NumStages');
return;
end %if length(PCANet.NumFilters)~= PCANet.NumStages
%init
NumImg = length(InImg);
V = cell(PCANet.NumStages, 1);
retainedVar = cell(PCANet.NumStages, 1);
NumFiltersInit = cell(PCANet.NumStages, 1);
OutImg = InImg;
ImgIdx = (1:NumImg)';
clear InImg;
% for stage = 1 : PCANet.NumStages
%
% %compute filters
% if stage == 1
%
% %display
% fprintf_pers(fidLogs, ['\t\tComputing PCA filter bank at stage ' num2str(stage) '...\n'])
%
% %compute PCANet filter
% [V{stage}, NumFiltersInit{stage}, retainedVar{stage}] = ...
% PCA_FilterBank(OutImg, PCANet.PatchSize(stage), PCANet.NumFilters(stage), stage, param, numCoresFeatExtr); % compute PCA filter banks
%
% %update number of filters
% PCANet.NumFilters(stage) = NumFiltersInit{stage};
%
% %display retained variance
% fprintf_pers(fidLogs, ['\t\t\tNum. of selected components: ' num2str(NumFiltersInit{stage}) '\n']);
% fprintf_pers(fidLogs, ['\t\t\tRetained variance (%%): ' num2str(retainedVar{stage}*100) '\n']);
%
% end %if stage == 1
%
% end %for stage = 1:PCANet.NumStages
%original PCANet
for stage = 1 : PCANet.NumStages
%display
fprintf_pers(fidLogs, ['\t\tComputing PCA filter bank at stage ' num2str(stage) '...\n'])
%compute PCANet filter
[V{stage}, NumFiltersInit{stage}, retainedVar{stage}] = ...
PCA_FilterBank(OutImg, PCANet.PatchSize(stage), PCANet.NumFilters(stage), stage, param, numCoresFeatExtr); % compute PCA filter banks
%update number of filters
PCANet.NumFilters(stage) = NumFiltersInit{stage};
%display retained variance
fprintf_pers(fidLogs, ['\t\t\tNum. of selected components: ' num2str(NumFiltersInit{stage}) '\n']);
fprintf_pers(fidLogs, ['\t\t\tRetained variance (%%): ' num2str(retainedVar{stage}*100) '\n']);
%compute the PCA outputs only when it is NOT the last stage
fprintf_pers(fidLogs, ['\t\tComputing PCA outputs at stage ' num2str(stage) '...\n'])
if stage ~= PCANet.NumStages
[OutImg, ImgIdx] = PCA_output(OutImg, ImgIdx, ...
PCANet.PatchSize(stage), PCANet.NumFilters(stage), V{stage});
end %if stage ~= PCANet.NumStages
end %for stage = 1:PCANet.NumStages
% pause