-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHard_negative_mining.m
More file actions
154 lines (115 loc) · 3.65 KB
/
Hard_negative_mining.m
File metadata and controls
154 lines (115 loc) · 3.65 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
[trD, trLb, valD, valLb, trRegs, valRegs] = HW2_Utils.getPosAndRandomNeg();
%HW2_Utils.genRsltFile(w,b,'./val','outfile');
pos_x = trD(:,1:size(trLb(trLb==1)));
pos_y = trLb(1:size(trLb(trLb==1)))';
neg_x = trD(:,size(trLb(trLb==1))+1:size(trD,2));
neg_y = trLb(size(trLb(trLb==1))+1:size(trD,2))';
x_train = [pos_x neg_x];
y_train = [pos_y neg_y]';
%[weights,bias] = train_quadratic(trainingX,trainingY);
x=x_train;
y=y_train;
[d,n]=size(x);
kernel = x'*x;
f = -1.*ones(n,1);
beq = 0;
aeq = y';
y_diag = diag(y);
H = y_diag*kernel*y_diag;
lb = zeros(n,1);
C = 0.1;
ub = C.*ones(n,1);
alpha = quadprog(H,f,[],[],aeq,beq,lb,ub);
alpha_new=diag(alpha);
temp=alpha_new*y;
w=x*temp;
temp1=y(1);
b=min(y-(w'*x)')/2;
obj_list = zeros(1,10);
ap_list = zeros(1,10);
for epoch = 1:8
original_val = size(neg_x,2);
A_temp = (w'*neg_x)+b;
A_temp = A_temp';
A = and(A_temp>-1,A_temp<1);
neg_x=neg_x(:,A);
imFiles = ml_getFilesInDir(sprintf('%s/%sIms/', HW2_Utils.dataDir, 'train'), 'jpg');
nIm = length(imFiles);
rects = cell(1, nIm);
startT = tic;
for i=1:50
ml_progressBar(i, nIm, 'Ub detection', startT);
im = imread(imFiles{i});
rects{i} = HW2_Utils.detect(im, w, b);
rects_neg = rects{i}(1:4,rects{i}(5,:)<0);
rects_pos = rects{i}(1:4,rects{i}(5,:)>0);
[imH, imW,~] = size(im);
badIdxs = or(rects_neg(3,:) > imW, rects_neg(4,:) > imH);
rects_neg = rects_neg(:,~badIdxs);
% Remove random rects that overlap more than 30% with an annotated upper body
nsize = size(rects_pos,2);
for j=1:nsize
overlap = HW2_Utils.rectOverlap(rects_neg, rects_pos(:,j));
rects_neg = rects_neg(:, overlap < 0.3);
if isempty(rects_neg)
break;
end;
end;
[D_i, R_i] = deal(cell(1, size(rects_neg,2)));
for j=1:size(rects_neg,2)
imReg = im(rects_neg(2,j):rects_neg(4,j), rects_neg(1,j):rects_neg(3,j),:);
imReg = imresize(imReg, HW2_Utils.normImSz);
R_i{j} = imReg;
D_i{j} = HW2_Utils.cmpFeat(rgb2gray(imReg));
end
dim = size(pos_x,1);
num = size(D_i,2);
B = zeros(dim,num);
for k=1:num
B(:,k) = D_i{k};
end
%if(size(B,2))>1000
% B = B(:,1:1000);
%end
neg_x = [neg_x B];
end
new_val = size(neg_x,2);
temp = new_val-original_val;
if (temp)>=0
%if (temp) >1000
% neg_y = [neg_y [-1.*ones(1,1000)]];
%else
neg_y = [neg_y [-1.*ones(1,(temp))]];
% end
else
neg_y = neg_y(1,1:(size(neg_y,2))+temp);
end
x_train = [pos_x neg_x];
y_train = [pos_y neg_y]';
x=x_train;
y=y_train;
[d,n]=size(x);
kernel = x'*x;
f = -1.*ones(n,1);
beq = 0;
aeq = y';
y_diag = diag(y);
H = double(y_diag*kernel*y_diag);
lb = zeros(n,1);
C = 0.1;
ub = C.*ones(n,1);
[alpha,obj] = quadprog(H,f,[],[],aeq,beq,lb,ub);
alpha_new=diag(alpha);
temp=alpha_new*y;
w=x*temp;
temp1=y(1);
b=min(y-(w'*x)')/2;
-1*obj
obj_list(epoch) = -1*obj;
[ap,precision,recall] = HW2_Utils.cmpAP('outfile','./val');
ap_list(epoch) = ap;
end
plot(obj_list);
plot(ap_list);
HW2_Utils.genRsltFile(w,b,'./test','4_2_file');
%[ap,precision,recall] = HW2_Utils.cmpAP('outfile','./val');