Skip to content

Commit f1a1364

Browse files
committed
init commit
1 parent de34931 commit f1a1364

File tree

80 files changed

+1127
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1127
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,5 @@ ENV/
9999

100100
# mypy
101101
.mypy_cache/
102+
103+
*.pth

README.md

+17

bbox.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
from __future__ import print_function
2+
import os,sys,cv2,random,datetime,time,math
3+
import argparse
4+
import numpy as np
5+
import torch
6+
7+
try:
8+
from iou import IOU
9+
except:
10+
# IOU cython speedup 10x
11+
def IOU(ax1,ay1,ax2,ay2,bx1,by1,bx2,by2):
12+
sa = abs((ax2-ax1)*(ay2-ay1))
13+
sb = abs((bx2-bx1)*(by2-by1))
14+
x1,y1 = max(ax1,bx1),max(ay1,by1)
15+
x2,y2 = min(ax2,bx2),min(ay2,by2)
16+
w = x2 - x1
17+
h = y2 - y1
18+
if w<0 or h<0: return 0.0
19+
else: return 1.0*w*h/(sa+sb-w*h)
20+
21+
def bboxlog(x1,y1,x2,y2,axc,ayc,aww,ahh):
22+
xc,yc,ww,hh = (x2+x1)/2,(y2+y1)/2,x2-x1,y2-y1
23+
dx,dy = (xc-axc)/aww,(yc-ayc)/ahh
24+
dw,dh = math.log(ww/aww),math.log(hh/ahh)
25+
return dx,dy,dw,dh
26+
27+
def bboxloginv(dx,dy,dw,dh,axc,ayc,aww,ahh):
28+
xc,yc = dx*aww+axc, dy*ahh+ayc
29+
ww,hh = math.exp(dw)*aww,math.exp(dh)*ahh
30+
x1,x2,y1,y2 = xc-ww/2,xc+ww/2,yc-hh/2,yc+hh/2
31+
return x1,y1,x2,y2
32+
33+
def nms(dets, thresh):
34+
if 0==len(dets): return []
35+
x1,y1,x2,y2,scores = dets[:, 0],dets[:, 1],dets[:, 2],dets[:, 3],dets[:, 4]
36+
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
37+
order = scores.argsort()[::-1]
38+
39+
keep = []
40+
while order.size > 0:
41+
i = order[0]
42+
keep.append(i)
43+
xx1,yy1 = np.maximum(x1[i], x1[order[1:]]),np.maximum(y1[i], y1[order[1:]])
44+
xx2,yy2 = np.minimum(x2[i], x2[order[1:]]),np.minimum(y2[i], y2[order[1:]])
45+
46+
w,h = np.maximum(0.0, xx2 - xx1 + 1),np.maximum(0.0, yy2 - yy1 + 1)
47+
ovr = w*h / (areas[i] + areas[order[1:]] - w*h)
48+
49+
inds = np.where(ovr <= thresh)[0]
50+
order = order[inds + 1]
51+
52+
return keep
53+
54+
def encode(matched, priors, variances):
55+
"""Encode the variances from the priorbox layers into the ground truth boxes
56+
we have matched (based on jaccard overlap) with the prior boxes.
57+
Args:
58+
matched: (tensor) Coords of ground truth for each prior in point-form
59+
Shape: [num_priors, 4].
60+
priors: (tensor) Prior boxes in center-offset form
61+
Shape: [num_priors,4].
62+
variances: (list[float]) Variances of priorboxes
63+
Return:
64+
encoded boxes (tensor), Shape: [num_priors, 4]
65+
"""
66+
67+
# dist b/t match center and prior's center
68+
g_cxcy = (matched[:, :2] + matched[:, 2:])/2 - priors[:, :2]
69+
# encode variance
70+
g_cxcy /= (variances[0] * priors[:, 2:])
71+
# match wh / prior wh
72+
g_wh = (matched[:, 2:] - matched[:, :2]) / priors[:, 2:]
73+
g_wh = torch.log(g_wh) / variances[1]
74+
# return target for smooth_l1_loss
75+
return torch.cat([g_cxcy, g_wh], 1) # [num_priors,4]
76+
77+
def decode(loc, priors, variances):
78+
"""Decode locations from predictions using priors to undo
79+
the encoding we did for offset regression at train time.
80+
Args:
81+
loc (tensor): location predictions for loc layers,
82+
Shape: [num_priors,4]
83+
priors (tensor): Prior boxes in center-offset form.
84+
Shape: [num_priors,4].
85+
variances: (list[float]) Variances of priorboxes
86+
Return:
87+
decoded bounding box predictions
88+
"""
89+
90+
boxes = torch.cat((
91+
priors[:, :2] + loc[:, :2] * variances[0] * priors[:, 2:],
92+
priors[:, 2:] * torch.exp(loc[:, 2:] * variances[1])), 1)
93+
boxes[:, :2] -= boxes[:, 2:] / 2
94+
boxes[:, 2:] += boxes[:, :2]
95+
return boxes

data/test01.jpg

88 KB

data/test01_output.png

786 KB
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function o = boxoverlap(a, b)
2+
% Compute the symmetric intersection over union overlap between a set of
3+
% bounding boxes in a and a single bounding box in b.
4+
%
5+
% a a matrix where each row specifies a bounding box
6+
% b a single bounding box
7+
8+
% AUTORIGHTS
9+
% -------------------------------------------------------
10+
% Copyright (C) 2011-2012 Ross Girshick
11+
% Copyright (C) 2008, 2009, 2010 Pedro Felzenszwalb, Ross Girshick
12+
%
13+
% This file is part of the voc-releaseX code
14+
% (http://people.cs.uchicago.edu/~rbg/latent/)
15+
% and is available under the terms of an MIT-like license
16+
% provided in COPYING. Please retain this notice and
17+
% COPYING if you use this file (or a portion of it) in
18+
% your project.
19+
% -------------------------------------------------------
20+
21+
x1 = max(a(:,1), b(1));
22+
y1 = max(a(:,2), b(2));
23+
x2 = min(a(:,3), b(3));
24+
y2 = min(a(:,4), b(4));
25+
26+
w = x2-x1+1;
27+
h = y2-y1+1;
28+
inter = w.*h;
29+
aarea = (a(:,3)-a(:,1)+1) .* (a(:,4)-a(:,2)+1);
30+
barea = (b(3)-b(1)+1) * (b(4)-b(2)+1);
31+
% intersection over union overlap
32+
o = inter ./ (aarea+barea-inter);
33+
% set invalid entries to 0 overlap
34+
o(w <= 0) = 0;
35+
o(h <= 0) = 0;
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
function evaluation(norm_pred_list,gt_dir,setting_name,setting_class,legend_name)
2+
load(gt_dir);
3+
if ~exist(sprintf('./plot/baselines/Val/%s/%s',setting_class,legend_name),'dir')
4+
mkdir(sprintf('./plot/baselines/Val/%s/%s',setting_class,legend_name));
5+
end
6+
IoU_thresh = 0.5;
7+
event_num = 61;
8+
thresh_num = 1000;
9+
org_pr_cruve = zeros(thresh_num,2);
10+
count_face = 0;
11+
12+
for i = 1:event_num
13+
img_list = file_list{i};
14+
gt_bbx_list = face_bbx_list{i};
15+
pred_list = norm_pred_list{i};
16+
sub_gt_list = gt_list{i};
17+
img_pr_info_list = cell(length(img_list),1);
18+
19+
fprintf('%s, current event %d\n',setting_name,i);
20+
for j = 1:length(img_list)
21+
gt_bbx = gt_bbx_list{j};
22+
pred_info = pred_list{j};
23+
keep_index = sub_gt_list{j};
24+
count_face = count_face + length(keep_index);
25+
26+
if isempty(gt_bbx) || isempty(pred_info)
27+
continue;
28+
end
29+
ignore = zeros(size(gt_bbx,1),1);
30+
if ~isempty(keep_index)
31+
ignore(keep_index) = 1;
32+
end
33+
34+
[pred_recall, proposal_list] = image_evaluation(pred_info, gt_bbx, ignore, IoU_thresh);
35+
36+
img_pr_info = image_pr_info(thresh_num, pred_info, proposal_list, pred_recall);
37+
img_pr_info_list{j} = img_pr_info;
38+
end
39+
for j = 1:length(img_list)
40+
img_pr_info = img_pr_info_list{j};
41+
if ~isempty(img_pr_info)
42+
org_pr_cruve(:,1) = org_pr_cruve(:,1) + img_pr_info(:,1);
43+
org_pr_cruve(:,2) = org_pr_cruve(:,2) + img_pr_info(:,2);
44+
end
45+
end
46+
end
47+
pr_cruve = dataset_pr_info(thresh_num, org_pr_cruve, count_face);
48+
% save(sprintf('./plot/baselines/Val/%s/%s/wider_pr_info_%s_%s.mat',setting_class,legend_name,legend_name,setting_name),'pr_cruve','legend_name','-v7.3');
49+
save(sprintf('./plot/baselines/Val/%s/%s/wider_pr_info_%s_%s.mat',setting_class,legend_name,legend_name,setting_name),'pr_cruve','legend_name','-v7');
50+
end
51+
52+
function [pred_recall,proposal_list] = image_evaluation(pred_info, gt_bbx, ignore, IoU_thresh)
53+
pred_recall = zeros(size(pred_info,1),1);
54+
recall_list = zeros(size(gt_bbx,1),1);
55+
proposal_list = zeros(size(pred_info,1),1);
56+
proposal_list = proposal_list + 1;
57+
pred_info(:,3) = pred_info(:,1) + pred_info(:,3);
58+
pred_info(:,4) = pred_info(:,2) + pred_info(:,4);
59+
gt_bbx(:,3) = gt_bbx(:,1) + gt_bbx(:,3);
60+
gt_bbx(:,4) = gt_bbx(:,2) + gt_bbx(:,4);
61+
for h = 1:size(pred_info,1)
62+
overlap_list = boxoverlap(gt_bbx, pred_info(h,1:4));
63+
[max_overlap, idx] = max(overlap_list);
64+
if max_overlap >= IoU_thresh
65+
if (ignore(idx) == 0)
66+
recall_list(idx) = -1;
67+
proposal_list(h) = -1;
68+
elseif (recall_list(idx)==0)
69+
recall_list(idx) = 1;
70+
end
71+
end
72+
r_keep_index = find(recall_list == 1);
73+
pred_recall(h) = length(r_keep_index);
74+
end
75+
end
76+
77+
function img_pr_info = image_pr_info(thresh_num, pred_info, proposal_list, pred_recall)
78+
img_pr_info = zeros(thresh_num,2);
79+
for t = 1:thresh_num
80+
thresh = 1-t/thresh_num;
81+
r_index = find(pred_info(:,5)>=thresh,1,'last');
82+
if (isempty(r_index))
83+
img_pr_info(t,2) = 0;
84+
img_pr_info(t,1) = 0;
85+
else
86+
p_index = find(proposal_list(1:r_index) == 1);
87+
img_pr_info(t,1) = length(p_index);
88+
img_pr_info(t,2) = pred_recall(r_index);
89+
end
90+
end
91+
end
92+
93+
function pr_cruve = dataset_pr_info(thresh_num, org_pr_cruve, count_face)
94+
pr_cruve = zeros(thresh_num,2);
95+
for i = 1:thresh_num
96+
pr_cruve(i,1) = org_pr_cruve(i,2)/org_pr_cruve(i,1);
97+
pr_cruve(i,2) = org_pr_cruve(i,2)/count_face;
98+
end
99+
end
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

eval/eval_tools_old-version/nms.m

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
function pick = nms(boxes, overlap)
2+
% top = nms(boxes, overlap)
3+
% Non-maximum suppression. (FAST VERSION)
4+
% Greedily select high-scoring detections and skip detections
5+
% that are significantly covered by a previously selected
6+
% detection.
7+
%
8+
% NOTE: This is adapted from Pedro Felzenszwalb's version (nms.m),
9+
% but an inner loop has been eliminated to significantly speed it
10+
% up in the case of a large number of boxes
11+
12+
% Copyright (C) 2011-12 by Tomasz Malisiewicz
13+
% All rights reserved.
14+
%
15+
% This file is part of the Exemplar-SVM library and is made
16+
% available under the terms of the MIT license (see COPYING file).
17+
% Project homepage: https://github.com/quantombone/exemplarsvm
18+
19+
20+
if isempty(boxes)
21+
pick = [];
22+
return;
23+
end
24+
25+
x1 = boxes(:,1);
26+
y1 = boxes(:,2);
27+
x2 = boxes(:,3);
28+
y2 = boxes(:,4);
29+
s = boxes(:,end);
30+
31+
area = (x2-x1+1) .* (y2-y1+1);
32+
[vals, I] = sort(s);
33+
34+
pick = s*0;
35+
counter = 1;
36+
while ~isempty(I)
37+
last = length(I);
38+
i = I(last);
39+
pick(counter) = i;
40+
counter = counter + 1;
41+
42+
xx1 = max(x1(i), x1(I(1:last-1)));
43+
yy1 = max(y1(i), y1(I(1:last-1)));
44+
xx2 = min(x2(i), x2(I(1:last-1)));
45+
yy2 = min(y2(i), y2(I(1:last-1)));
46+
47+
w = max(0.0, xx2-xx1+1);
48+
h = max(0.0, yy2-yy1+1);
49+
50+
inter = w.*h;
51+
o = inter ./ (area(i) + area(I(1:last-1)) - inter);
52+
53+
I = I(find(o<=overlap));
54+
end
55+
56+
pick = pick(1:(counter-1));
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function norm_pred_list = norm_score(org_pred_list)
2+
3+
event_num = 61;
4+
norm_pred_list = cell(event_num,1);
5+
max_score = realmin('single');
6+
min_score = realmax('single');
7+
parfor i = 1:event_num
8+
pred_list = org_pred_list{i};
9+
for j = 1:size(pred_list,1)
10+
if(isempty(pred_list{j}))
11+
continue;
12+
end
13+
score_list = pred_list{j}(:,5);
14+
max_score = max(max_score,max(score_list));
15+
min_score = min(min_score,min(score_list));
16+
end
17+
end
18+
19+
parfor i = 1:event_num
20+
fprintf('Norm prediction: current event %d\n',i);
21+
pred_list = org_pred_list{i};
22+
for j = 1:size(pred_list,1)
23+
if(isempty(pred_list{j}))
24+
continue;
25+
end
26+
score_list = pred_list{j}(:,5);
27+
norm_score_list = (score_list - min_score)/(max_score - min_score);
28+
pred_list{j}(:,5) = norm_score_list;
29+
end
30+
norm_pred_list{i} = pred_list;
31+
end
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function ap = VOCap(rec,prec)
2+
3+
mrec=[0 ; rec ; 1];
4+
mpre=[0 ; prec ; 0];
5+
for i=numel(mpre)-1:-1:1
6+
mpre(i)=max(mpre(i),mpre(i+1));
7+
end
8+
i=find(mrec(2:end)~=mrec(1:end-1))+1;
9+
ap=sum((mrec(i)-mrec(i-1)).*mpre(i));
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
function plot_pr(propose,recall,lendge_name,seting_class,setting_name,dateset_class)
2+
model_num = size(propose,1);
3+
figure1 = figure('PaperSize',[20.98 29.68],'Color',[1 1 1]);
4+
axes1 = axes('Parent',figure1,...
5+
'LineWidth',2,...
6+
'FontSize',15,...
7+
'FontName','Times New Roman',...
8+
'FontWeight','bold');
9+
box(axes1,'on');
10+
hold on;
11+
12+
LineColor = colormap(hsv(model_num));
13+
for i=1:model_num
14+
plot(propose{i},recall{i},...
15+
'MarkerEdgeColor',LineColor(i,:),...
16+
'MarkerFaceColor',LineColor(i,:),...
17+
'LineWidth',4,...
18+
'Color',LineColor(i,:))
19+
hleg = legend(lendge_name{:},'Location','SouthEast');
20+
grid on;
21+
hold on;
22+
end
23+
xlim([0,1]);
24+
ylim([0,1]);
25+
xlabel('Recall');
26+
ylabel('Precision');
27+
28+
% savename = sprintf('./plot/figure/%s/wider_pr_cruve_%s_%s.pdf',dateset_class,seting_class,setting_name);
29+
savename = sprintf('./plot/figure_%s_wider_pr_cruve_%s_%s.jpg',dateset_class,seting_class,setting_name);
30+
print("-djpg",savename)
31+
% saveTightFigure(gcf,savename);
32+
clear gcf;
33+
hold off;
34+
35+
36+
37+

0 commit comments

Comments
 (0)