-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hjj_check_iou.lua
109 lines (85 loc) · 2.77 KB
/
Hjj_check_iou.lua
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
function func_gt_loc_reg(gt, mask)
local gt_mask = {gt[2],gt[3],gt[4],gt[5]}
local h = mask[3] - mask[1]
local w = mask[4] - mask[2]
return torch.Tensor{(gt_mask[1] - mask[1])/h,
(gt_mask[2] - mask[2])/w,
(gt_mask[3] - mask[3])/h,
(gt_mask[4] - mask[4])/w}:cuda()
end
--[[
function func_calculate_iou( mask, gt )
--print('calculate iou:')
--print(mask)
local gt_mask = {math.ceil(gt[2]), math.ceil(gt[3])
,math.floor(gt[4]),math.floor(gt[5])}
mask[1] = math.ceil(mask[1])
mask[2] = math.ceil(mask[2])
if mask[1] <= 0 then mask[1] = 1 end
if mask[2] <= 0 then mask[2] = 1 end
mask[3] = math.floor(mask[3])
if mask[3] < mask[1] then mask[3] = mask[1] end
mask[4] = math.floor(mask[4])
if mask[4] < mask[2] then mask[4] = mask[2] end
local map_size = {}
if gt_mask[3] >= mask[3] then
table.insert(map_size,gt_mask[3])
else
table.insert(map_size, mask[3])
end
if gt_mask[4] >= mask[4] then
table.insert(map_size,gt_mask[4])
else
table.insert(map_size,mask[4])
end
local gt_map = torch.LongTensor(map_size[1], map_size[2]):fill(0)
gt_map[{ {gt_mask[1], gt_mask[3]}, {gt_mask[2], gt_mask[4]}}]:fill(1)
local mask_map = torch.LongTensor(map_size[1], map_size[2]):fill(0)
--print(mask_map:size())
--print(mask)
mask_map[{{mask[1], mask[3]}, {mask[2], mask[4]}}]:fill(1)
return torch.cbitand(gt_map, mask_map):sum() / torch.cbitor(gt_map, mask_map):sum()
end
--]]
function func_calculate_iou( mask, gt )
local gt_mask = {gt[3],gt[2],gt[5],gt[4]}
local m_area = (mask[3]-mask[1])*(mask[4]-mask[2])
local g_area = (gt_mask[3]-gt_mask[1])*(gt_mask[4]-gt_mask[2])
local x1 = math.max(mask[1],gt_mask[1])
local y1 = math.max(mask[2], gt_mask[2])
local x2 = math.min(mask[3],gt_mask[3])
local y2 = math.min(mask[4],gt_mask[4])
local w = math.max(0,x2-x1)
local h = math.max(0,y2-y1)
return (w*h)/(m_area+g_area-(w*h))
end
function func_follow_iou(mask, gt, detected_obj_table, iou_table, thd)
local result_table = torch.Tensor(iou_table:size()):fill(0)
local iou = 0
local new_iou = 0
local index = 0
local new_detected_obj_table = detected_obj_table:clone()
for i = 1, #gt do
iou = func_calculate_iou(mask, gt[i])
result_table[i] = iou
if iou >= thd then new_detected_obj_table[i] = 1 end
end
new_iou, index = torch.max(result_table, 1)
-- from tensor to numeric type
new_iou = new_iou[1]
index = index[1]
iou = iou_table[index]
return iou, new_iou, result_table, index, new_detected_obj_table
end
function func_get_reward(old_iou, new_iou, new_detected_obj_table, old_detected_obj_table)
local reward
if new_iou - old_iou > 0.007 then
reward = 5
else
reward = -1
end
if (new_detected_obj_table - old_detected_obj_table):sum() > 0 then
reward = 10 * (new_detected_obj_table - old_detected_obj_table):sum()
end
return reward
end