roipool.py
9.15 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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from __future__ import division
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules import module
from utils.utils import *
class ROIPool(nn.Module):
def __init__(self, output_size):
super(ROIPool, self).__init__()
self.maxpool = nn.AdaptiveMaxPool2d(output_size)
self.size = output_size
self.fc1 = nn.Linear(2304, 1024)
self.fc2 = nn.Linear(1024, 512)
self.fc3 = nn.Linear(512, 1)
self.softplus = nn.Softplus()
self.smoothl1 = nn.SmoothL1Loss()
self.mse = nn.MSELoss()
def target_detection_iou(self, box1, box2):
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
# get the corrdinates of the intersection rectangle
b1_x1 = b1_x1.type(torch.float64)
b1_y1 = b1_y1.type(torch.float64)
b1_x2 = b1_x2.type(torch.float64)
b1_y2 = b1_y2.type(torch.float64)
inter_rect_x1 = torch.max(b1_x1, b2_x1)
inter_rect_y1 = torch.max(b1_y1, b2_y1)
inter_rect_x2 = torch.min(b1_x2, b2_x2)
inter_rect_y2 = torch.min(b1_y2, b2_y2)
# Intersection area
inter_area = torch.clamp(inter_rect_x2 - inter_rect_x1 + 1, min=0) * torch.clamp(
inter_rect_y2 - inter_rect_y1 + 1, min=0
)
# Union Area
b1_area = (b1_x2 - b1_x1 + 1) * (b1_y2 - b1_y1 + 1)
b2_area = (b2_x2 - b2_x1 + 1) * (b2_y2 - b2_y1 + 1)
iou = inter_area / (b1_area + b2_area - inter_area + 1e-16)
return iou
def similar_bbox(self, detections, targets):
rescaled_boxes = rescale_boxes(detections, 416, (480, 640))
similar_box = list(range(len(rescaled_boxes)))
for i in range(len(rescaled_boxes)):
for j in range(len(targets)):
target_xyxy = [(targets[j][0]-(targets[j][2]/2))*640, (targets[j][1]-(targets[j][3]/2))*480, (targets[j][0]+(targets[j][2]/2))*640, (targets[j][1]+(targets[j][3]/2))*480]
target_xyxy = torch.tensor(target_xyxy)
iou = self.target_detection_iou(rescaled_boxes[i][:4], target_xyxy)
if iou > 0.01:
similar_box[i] = targets[j][-1]
break
else:
similar_box[i] = -1
return similar_box
def cal_scale(self, x, detections, targets):
targets_distance = targets[:, :4]
square_targets = []
for target_distance in targets_distance:
x1 = (target_distance[0]-(target_distance[2]/2))*416
y1 = ((target_distance[1]-(target_distance[3]/2))*480+80)*13/15
x2 = (target_distance[0]+(target_distance[2]/2))*416
y2 = ((target_distance[1]+(target_distance[3]/2))*480+80)*13/15
square_targets.append([x1, y1, x2, y2])
square_targets = torch.tensor(square_targets)
scale = get_scale(square_targets)
output_distance = []
roi_results = []
for i in scale:
x1_scale = i[0]
y1_scale = i[1]
x2_scale = i[2]
y2_scale = i[3]
output = x[:, :, x1_scale:x2_scale+1, y1_scale:y2_scale+1]
output = self.maxpool(output)
output = output.view(1, -1)
# print(output)
roi_results.append(output)
return roi_results
def cal_scale_evaL(self, x, detections):
detections = detections[:, :4]
scale = get_scale(detections)
output_distance = []
roi_results = []
for i in scale:
x1_scale = i[0]
y1_scale = i[1]
x2_scale = i[2]
y2_scale = i[3]
output = x[:, :, y1_scale:y2_scale+1, x1_scale:x2_scale+1]
output = self.maxpool(output)
output = output.view(1, -1)
roi_results.append(output)
return roi_results
def forward(self, x, detections, targets=None):
if targets is not None:
distances = targets[:, 4]
distances = distances * 10
# distances = distances * 10
# print(f'disatnces = {distances}')
# targets_distance = targets[:, :4]
# square_targets = []
# for target_distance in targets_distance:
# x1 = (target_distance[0]-(target_distance[2]/2))*416
# y1 = ((target_distance[1]-(target_distance[3]/2))*480+80)*13/15
# x2 = (target_distance[0]+(target_distance[2]/2))*416
# y2 = ((target_distance[1]+(target_distance[3]/2))*480+80)*13/15
# square_targets.append([x1, y1, x2, y2])
# square_targets = torch.tensor(square_targets)
# scale = get_scale(square_targets)
# output_distance = []
# roi_results = []
# for i in scale:
# x1_scale = i[0]
# y1_scale = i[1]
# x2_scale = i[2]
# y2_scale = i[3]
# output = x[:, :, x1_scale:x2_scale+1, y1_scale:y2_scale+1]
# output = self.maxpool(output)
# output = output.view(1, -1).cuda()
# # print(output)
# roi_results.append(output)
roi_results = self.cal_scale(x, detections, targets)
output = torch.cat(roi_results, 0)
# print(output.shape)
# print(output.shape)
output = self.fc1(output)
output = self.fc2(output)
output = self.fc3(output)
output = self.softplus(output)
# print(f'output = {output}')
#loss = 0
# output_distance = torch.tensor(output, requires_grad=True)
'''
output = x
# output = x[:, :, y1_scale:y2_scale+1, x1_scale:x2_scale+1]
output = self.maxpool(output)
output = output.view(1, -1).cuda()
# print(output.shape)
output = self.fc1(output)
output = self.fc2(output)
output = self.fc3(output)
output = self.softplus(output)
'''
# output_distance = torch.cuda.FloatTensor(output_distance, requires_grad=True)#.to('cpu')
#print(f'output_distance = {output_distance}')
#print(output_distance.shape)
#print(f'distances = {distances}')
#print(distances.shape)
distances = distances.cuda()
# print(f'output = {output}')
# print(f'output = {output}')
# print(f'distances = {distances}')
loss = self.smoothl1(output, distances.float())
# print(f'loss = {loss}')
# print(f'output_distance = {output_distance}')
# print(f'distances = {distances}')
# print(f'loss = {loss}')
return loss, output
else:
'''
detections = detections[:, :4]
scale = get_scale(detections)
output_distance = []
for i in scale:
x1_scale = i[0]
y1_scale = i[1]
x2_scale = i[2]
y2_scale = i[3]
output = x[:, :, y1_scale:y2_scale+1, x1_scale:x2_scale+1]
output = self.maxpool(output)
output = output.view(1, -1).cuda()
'''
roi_results = self.cal_scale_evaL(x, detections)
output = torch.cat(roi_results, 0)
# print(f'output = {output.shape}')
output = self.fc1(output)
output = self.fc2(output)
output = self.fc3(output)
output = self.softplus(output)
# print(f'output = {output}')
return output
'''
scale = get_scale(detections)
output_distance = []
for i in scale:
x1_scale = i[0]
y1_scale = i[1]
x2_scale = i[2]
y2_scale = i[3]
output = x[:, :, y1_scale:y2_scale+1, x1_scale:x2_scale+1]
# output = x[:, :, x1_scale:x2_scale+1, y1_scale:y2_scale+1]
output = self.maxpool(output)
output = output.view(1, -1).cuda()
output = self.fc1(output)
output = self.fc2(output)
output_distance.append(output)
if targets is None:
return output_distance, 0
else:
loss = 0
box_similar_distance = self.similar_bbox(detections, targets)
for i in range(len(box_similar_distance)):
if box_similar_distance[i] == -1:
output_distance[i] = -1
output_distance = torch.FloatTensor(output_distance).to('cpu')
box_similar_distance = torch.FloatTensor(box_similar_distance).to('cpu')
# print(f'output_distance = {output_distance}')
# print(f'target_distance = {box_similar_distance}')
loss = self.smoothl1(output_distance, box_similar_distance)
'''