-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmilmd_ObjFuncInit.m
162 lines (129 loc) · 5.98 KB
/
milmd_ObjFuncInit.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
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
156
157
158
159
160
161
function [objValues, targetSigInit] = milmd_ObjFuncInit(Ctargets, pDataBags, nDataBags, parameters)
% Function that evaluates the objective function for multiple instance learning for
% multiple diverse (MIL MD) algorithm. Uses Equation 15 on page 4.
% INPUTS:
% 1) Ctargets: the instances closest to the K-Means cluster centers [n_targets, n_dims]
% 2) nDataBags: a cell array containing the negative bags
% 3) parameters: a structure containing the parameter variables
% OUTPUTS:
% 1) objValues: The calculated objective values for each combination of
% KMean cluster representatives.
% 2) targetSigInit: the targets selected that maximize the objective
% function.
% -------------------------------------------------------------------------
% Find all combinations of KCluster target signatures
index = 1:size(Ctargets,1);
combo = combnk(index, parameters.numTargets);
objValues = zeros(size(combo,1),1);
% Loop through all combinations of targets
for c = 1:size(combo,1)
% Set up current set of target signature combinations
targetSignatures = Ctargets(combo(c,:),:);
% Calculate the positive bags term for this combination of targets
cPos = evalPos(targetSignatures, pDataBags, parameters);
% Calculate the negative bags term for this combination of targets
cNeg = evalNeg(targetSignatures, nDataBags);
% Calculate the unique or diversity promoting term for this combination of targets
cUni = evalUnique(targetSignatures, parameters);
% Calculate the constraint term for this combination of targets
cCon = evalConstraint(targetSignatures, parameters);
% Calculate Objective Function for this combination of targets
objValues(c) = cPos - cNeg - cUni - cCon;
end
% Find the combination of targets that maximizes the objective function
[~,targetIndex] = max(objValues);
targetSigInit = Ctargets(combo(targetIndex,:),:);
end
function [cPos] = evalPos(targetSignatures, pDataBags, parameters)
% Average of the instances with maximum detection characteristics using
% the learned target signatures
% First term in Equation 15 on page 4.
% INPUTS:
% 1) targetSignatures: the instances closest to the K-Means cluster centers [n_targets, n_dims]
% 2) pDataBags: a cell array containing the positive bags
% 3) parameters: a structure containing the parameter variables. variables
% used in this function are number of Targets
% OUTPUTS:
% 1) cPos: the positive bag term for the objective function.
% Set up variables
numPBags = size(pDataBags, 2);
pBagSum = zeros(numPBags,1);
% Loop through positive bags
for bag = 1:numPBags
%Get data from specific bag
pData = pDataBags{bag};
pBagMax = zeros(parameters.numTargets,1);
% Loop through Targets
for k = 1:parameters.numTargets
%Confidences (dot product) of a sample across all other samples in pData, data has already been whitened
pConf = sum(pData.*targetSignatures(k,:), 2);
%Get max confidence for this bag
pBagMax(k) = max(pConf)/parameters.numTargets;
end
% Calculate the positive bags confidence sum
pBagSum(bag) = sum(pBagMax);
end
%Calculate actual objectiveValue
cPos = mean(pBagSum(:));
end
function [cNeg] = evalNeg(targetSignatures, nDataBags)
% Function that calculates the average of the max negative instances.
% Get average max confidence of each negative bag.
% Second term in Equation 15 on page 4.
% INPUTS:
% 1) targetSignatures: the instances closest to the K-Means cluster centers [n_targets, n_dims]
% 2) nDataBags: a cell array containing the negative bags
% OUTPUTS:
% 1) cNeg: the negative bag term in the objective function.
% Set up Variables
numNBags = length(nDataBags);
nBagMean = zeros(1, numNBags);
% Loop through negative bags
for bag = 1:numNBags
%Get data from specific bag
nData = nDataBags{bag};
% Find instance/pixel with the max confidence
maxDotProd = max(targetSignatures*nData'); %max{k} s^^(k)T*x^^n
nBagMean(bag) = mean(maxDotProd);
end
% Calculate objective value by calculating the mean from all negative bags
cNeg = mean(nBagMean);
end
function [cU] = evalUnique(targetSignatures, parameters)
% Function that calculates the uniqueness or diversity of selected target signatures
% with alpha is configurable to allow for more different or similar target
% signatures.
% Third term in Equation 15 on page 4.
% INPUTS:
% 1) targetSignatures: the instances closest to the K-Means cluster centers [n_targets, n_dims]
% 2) parameters: a structure containing the parameter variables. variables
% used in this function - alpha, numTargets
% OUTPUTS:
% 1) cU: the diversity promoting term in the objective function.
% Get all combinations of targets in order to determine similarity
numTargets = parameters.numTargets;
allSpectraComb = combnk(1:numTargets, 2);
numCombinations = size(allSpectraComb,1);
cosSimilarity = 0;
% Loop through combinations calculating the target signature similarities
for k = 1:numCombinations
cosSimilarity = cosSimilarity + targetSignatures(allSpectraComb(k,1),:)*targetSignatures(allSpectraComb(k,2),:)';
end
coeff = (2*parameters.alpha)/(numTargets*(numTargets-1));
cU = coeff * cosSimilarity;
end
function [cCon] = evalConstraint(targetSignatures, parameters)
% Function that calculates the constraint term of the objective function.
% The introduction of this constraint into the maximization framework aids
% in the prevention of target signatures being arbitrarily large.
% Fourth term in Equation 15 on page 4.
% INPUTS:
% 1)targetSignature: the instances closest to the K-Means cluster centers [n_targets, n_dims]
% 2) parameters: a structure containing the parameter variables. variables
% used in this function - lambda, numTargets
% OUTPUTS:
% 1) cCon: the computed term for the fourth term in the objective function.
targetSigSquared = diag(targetSignatures*targetSignatures');
lagrangeTerm = abs(targetSigSquared - 1);
cCon = (parameters.lambda / parameters.numTargets) * sum(lagrangeTerm);
end