-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun.m
More file actions
155 lines (149 loc) · 6.46 KB
/
run.m
File metadata and controls
155 lines (149 loc) · 6.46 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
function run(task, varargin)
addpath(genpath(pwd));
allowedTasks = {'classification', 'features', 'features-imagenet', ...
'features-hop', 'features-hop-imagenet', 'features-hop-masked'};
assert(ismember(task, allowedTasks), ['task must be one of: ', ...
sprintf('''%s'', ', allowedTasks{1:end - 1}), '''', ...
allowedTasks{end}, '''']);
%% Args
argParser = inputParser();
argParser.KeepUnmatched = true;
argParser.addParameter('dataset', ...
loadData('data/data_occlusion_klab325v2.mat', 'data'), ...
@(d) ~isempty(d) && isa(d, 'dataset'));
argParser.addParameter('images', loadData('KLAB325.mat', 'img_mat'), ...
@(i) ~isempty(i) && iscell(i));
argParser.addParameter('dataSelection', [], @isnumeric);
argParser.addParameter('excludeCategories', [], @isnumeric);
argParser.addParameter('featureExtractors', {});
argParser.addParameter('trainDirectory', [], @(p) exist(p, 'dir'));
argParser.addParameter('testDirectory', [], @(p) exist(p, 'dir'));
argParser.addParameter('bipolarizationValue', 0, @isnumeric);
argParser.addParameter('resultsFilename', ...
datestr(datetime(), 'yyyy-mm-dd_HH-MM-SS'), @ischar);
argParser.parse(varargin{:});
fprintf('Running %s in %s with args:\n', task, pwd);
disp(argParser.Results);
dataset = argParser.Results.dataset;
images = argParser.Results.images;
dataSelection = argParser.Results.dataSelection;
if ismember('dataSelection', argParser.UsingDefaults)
dataSelection = 1:size(dataset, 1);
end
excludedCategories = argParser.Results.excludeCategories;
featureExtractors = argParser.Results.featureExtractors;
trainDir = argParser.Results.trainDirectory;
testDir = argParser.Results.testDirectory;
bipolarizationValue = argParser.Results.bipolarizationValue;
%% Run
switch task
case 'classification'
adjustTestImages = createAdjustTestImages(dataset);
featureProviderFactory = FeatureProviderFactory(...
trainDir, testDir, dataset.pres, dataSelection, ...
images, adjustTestImages);
featureExtractors = cellfun(@(f) featureProviderFactory.get(f), ...
featureExtractors, 'UniformOutput', false);
varargin = replaceOrAddVararg(varargin, ...
'featureExtractors', featureExtractors);
classifier = @LibsvmClassifierCCV;
dataSelection = dataSelection(...
~ismember(dataset.truth(dataSelection), excludedCategories));
varargin = replaceOrAddVararg(varargin, ...
'dataSelection', dataSelection);
runTask(...
'dataPath', [fileparts(mfilename('fullpath')), '/data'], ...
'kfoldValues', unique(dataset.pres(dataSelection)), ...
'getRows', curry(@getRows, dataset, dataSelection), ...
'getLabels', @(rows) dataset.truth(rows), ...
'classifier', classifier, ...
'resultsFilename', [argParser.Results.resultsFilename, '.mat'], ...
varargin{:});
case 'features'
adjustTestImages = createAdjustTestImages(dataset);
computeFeatures('dataSelection', dataSelection, ...
'images', images, 'objectForRow', dataset.pres, ...
'adjustTestImages', adjustTestImages, ...
'featureExtractors', featureExtractors, ...
'trainDirectory', trainDir, 'testDirectory', testDir, ...
varargin{:});
case 'features-imagenet'
objects = 1:size(dataset, 1);
adjustTestImages = curry(@occludeImages, ...
dataset.numBubbles, dataset.bubbleCenters, ...
dataset.bubbleSigmas);
computeFeatures('dataSelection', dataSelection, ...
'images', images, 'objectForRow', objects, ...
'adjustTestImages', adjustTestImages, ...
'featureExtractors', featureExtractors, ...
'trainDirectory', trainDir, 'testDirectory', testDir, ...
varargin{:});
case 'features-hop'
savesteps = [1:100, 110:10:300];
featureProviderFactory = FeatureProviderFactory(trainDir, testDir, ...
dataset.pres, 1:length(dataset));
featureExtractor = HopFeatures(max(savesteps), ...
BipolarFeatures(bipolarizationValue, ...
featureProviderFactory.get(featureExtractors)));
weightsDirectory = [trainDir, '/../weights/'];
if ~exist(weightsDirectory, 'dir')
mkdir(weightsDirectory);
end
computeHopTimeFeatures(...
'objectForRow', dataset.pres, ...
'trainDirectory', trainDir, 'testDirectory', testDir, ...
'weightsDirectory', weightsDirectory, ...
'featureExtractor', featureExtractor, ...
'savesteps', savesteps, ...
varargin{:});
case 'features-hop-imagenet'
savesteps = [1:100, 110:10:300];
objects = (1:50000)';
featureProviderFactory = FeatureProviderFactory(trainDir, testDir, ...
objects, objects);
featureExtractor = HopFeatures(max(savesteps), ...
BipolarFeatures(0, ...
featureProviderFactory.get(featureExtractors)));
weightsDirectory = [trainDir, '/../weights-imagenet/'];
if ~exist(weightsDirectory, 'dir')
mkdir(weightsDirectory);
end
computeHopTimeFeatures(...
'objectForRow', objects, ...
'trainDirectory', trainDir, 'testDirectory', testDir, ...
'weightsDirectory', weightsDirectory, ...
'featureExtractor', featureExtractor, ...
'savesteps', savesteps, ...
varargin{:});
case 'features-hop-masked'
runMaskedHopFeatures(...
'objectForRow', dataset.pres, ...
'trainDirectory', trainDir, 'testDirectory', testDir, ...
varargin{:});
otherwise
error('Unknown task %s', task);
end
end
function rows = getRows(dataset, dataSelection, pres, runType)
if runType == RunType.Train
selectedData = dataset(dataSelection, :);
[~, rows] = unique(selectedData, 'pres');
rows = dataSelection(rows);
else
rows = dataSelection;
end
rows = rows(ismember(dataset.pres(rows), pres));
assert(all(sort(unique(dataset.pres(rows))) == sort(pres)));
if runType == RunType.Train
assert(length(rows) == length(pres));
end
end
function adjustTestImages = createAdjustTestImages(dataset)
if ~ismember('bubbleSigmas', dataset.Properties.VarNames)
bubbleSigmas = repmat(14, size(dataset, 1), max(dataset.nbubbles));
else
bubbleSigmas = dataset.bubbleSigmas;
end
adjustTestImages = curry(@occludeImages, ...
dataset.nbubbles, dataset.bubble_centers, bubbleSigmas);
end