-
Notifications
You must be signed in to change notification settings - Fork 1
/
computeHopTimeFeatures.m
72 lines (67 loc) · 2.71 KB
/
computeHopTimeFeatures.m
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
function computeHopTimeFeatures(varargin)
%% Setup
argParser = inputParser();
argParser.KeepUnmatched = true;
argParser.addParameter('objectForRow', [], @(x) ~isempty(x) && isnumeric(x));
argParser.addParameter('savesteps', [1:100, 110:10:300], @isnumeric);
argParser.addParameter('trainDirectory', [], @(p) exist(p, 'dir'));
argParser.addParameter('testDirectory', [], @(p) exist(p, 'dir'));
argParser.addParameter('weightsDirectory', [], @(p) exist(p, 'dir'));
argParser.addParameter('featureExtractor', [], ...
@(f) isa(f, 'HopFeatures'));
argParser.addParameter('omitTrain', false, @(b) b == true || b == false);
argParser.parse(varargin{:});
fprintf('Computing features in %s with args:\n', pwd);
disp(argParser.Results);
objectForRow = argParser.Results.objectForRow;
savesteps = argParser.Results.savesteps;
trainDir = argParser.Results.trainDirectory;
testDir = argParser.Results.testDirectory;
weightsDir = argParser.Results.weightsDirectory;
featureExtractor = argParser.Results.featureExtractor;
omitTrain = argParser.Results.omitTrain;
%% whole
if ~omitTrain
[~, wholePresRows] = unique(objectForRow);
fprintf('%s whole objects %d\n', featureExtractor.getName(), ...
numel(wholePresRows));
features = featureExtractor.extractFeatures(wholePresRows, ...
RunType.Train, []);
net = featureExtractor.net;
save([weightsDir, featureExtractor.getName(), '-net.mat'], 'net', '-v7.3');
for t = savesteps
saveFeatures(features, trainDir, featureExtractor, t);
end
end
%% occluded
sliceSize = 1000;
numSlices = ceil(size(objectForRow, 1) / sliceSize);
features = cell(numSlices, 1);
parfor sliceIter = 1:numSlices
%for sliceIter = 1:numSlices
dataStart = (sliceIter - 1) * sliceSize + 1;
dataEnd = dataStart + sliceSize - 1;
fprintf('%s occluded %d/%d (%d:%d)\n', featureExtractor.getName(), ...
sliceIter, numSlices, dataStart, dataEnd);
rows = dataStart:dataEnd;
[~, ys] = featureExtractor.extractFeatures(rows, RunType.Test, []);
features{sliceIter} = ys(:, :, savesteps);
end
features = vertcat(features{:});
% save
parfor timeIter = 1:numel(savesteps)
%for timeIter = 1:numel(savesteps)
timestep = savesteps(timeIter);
fprintf('%s save occluded t=%d\n', featureExtractor.getName(), ...
timestep);
saveFeatures(features(:, :, timeIter), ...
testDir, featureExtractor, timestep);
end
end
function saveFeatures(features, dir, featureExtractor, timestep)
% Replace last timestep with current timestep.
% Since matlab has no last option, replace the first of the reverse.
name = fliplr(regexprep(fliplr(featureExtractor.getName()), ...
'[0-9]+t_', fliplr(['_t', num2str(timestep)]), 'once'));
save([dir, '/', name, '.mat'], '-v7.3', 'features');
end