forked from xiaobai1217/MBMD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
siamese_utils.py
309 lines (253 loc) · 11.1 KB
/
siamese_utils.py
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import sys
import numpy as np
import os
import time
import matplotlib.pyplot as plt
import cv2
from numpy.random import *
from pylab import *
from PIL import Image
import tempfile
import random
from region_to_bbox import region_to_bbox
import tensorflow as tf
os.environ['CUDA_VISIBLE_DEVICES'] = '0'
def getSubWinTracking(img, pos, modelSz, originalSz, avgChans):
if originalSz is None:
originalSz = modelSz
sz = originalSz
im_sz = img.shape
# make sure the size is not too small
assert min(im_sz[:2]) > 2, "the size is too small"
c = (np.array(sz) + 1) / 2
# check out-of-bounds coordinates, and set them to black
context_xmin = round(pos[1] - c[1])
context_xmax = context_xmin + sz[1] - 1
context_ymin = round(pos[0] - c[0])
context_ymax = context_ymin + sz[0] - 1
left_pad = max(0, int(-context_xmin))
top_pad = max(0, int(-context_ymin))
right_pad = max(0, int(context_xmax - im_sz[1] + 1))
bottom_pad = max(0, int(context_ymax - im_sz[0] + 1))
context_xmin = int(context_xmin + left_pad)
context_xmax = int(context_xmax + left_pad)
context_ymin = int(context_ymin + top_pad)
context_ymax = int(context_ymax + top_pad)
if top_pad or left_pad or bottom_pad or right_pad:
r = np.pad(img[:, :, 0], ((top_pad, bottom_pad), (left_pad, right_pad)), mode='constant',
constant_values=avgChans[0])
g = np.pad(img[:, :, 1], ((top_pad, bottom_pad), (left_pad, right_pad)), mode='constant',
constant_values=avgChans[1])
b = np.pad(img[:, :, 2], ((top_pad, bottom_pad), (left_pad, right_pad)), mode='constant',
constant_values=avgChans[2])
r = np.expand_dims(r, 2)
g = np.expand_dims(g, 2)
b = np.expand_dims(b, 2)
# h, w = r.shape
# r1 = np.zeros([h, w, 1], dtype=np.float32)
# r1[:, :, 0] = r
# g1 = np.zeros([h, w, 1], dtype=np.float32)
# g1[:, :, 0] = g
# b1 = np.zeros([h, w, 1], dtype=np.float32)
# b1[:, :, 0] = b
img = np.concatenate((r, g, b ), axis=2)
im_patch_original = img[context_ymin:context_ymax + 1, context_xmin:context_xmax + 1, :]
if not np.array_equal(modelSz, originalSz):
im_patch = cv2.resize(im_patch_original, modelSz)
# im_patch_original = im_patch_original/255.0
# im_patch = transform.resize(im_patch_original, modelSz)*255.0
# im = Image.fromarray(im_patch_original.astype(np.float))
# im = im.resize(modelSz)
# im_patch = np.array(im).astype(np.float32)
else:
im_patch = im_patch_original
# im_patch = im_patch[:, :, ::-1]
# im_patch[:, :, 0] = im_patch[:, :, 0] - 103.939
# im_patch[:, :, 1] = im_patch[:, :, 1] - 116.779
# im_patch[:, :, 2] = im_patch[:, :, 2] - 123.68
return im_patch, im_patch_original
def _update_target_position(pos_x, pos_y, score, final_score_sz, tot_stride, search_sz, response_up, x_sz):
# find location of score maximizer
p = np.asarray(np.unravel_index(np.argmax(score), np.shape(score)))
# displacement from the center in search area final representation ...
center = float(final_score_sz - 1) / 2
disp_in_area = p - center
# displacement from the center in instance crop
disp_in_xcrop = disp_in_area * float(tot_stride) / response_up
# displacement from the center in instance crop (in frame coordinates)
disp_in_frame = disp_in_xcrop * x_sz / search_sz
# *position* within frame in frame coordinates
pos_y, pos_x = pos_y + disp_in_frame[0], pos_x + disp_in_frame[1]
return pos_x, pos_y
def trackerEval(score, sx, targetPosition, window, hp,design):
# responseMaps = np.transpose(score[:, :, :, 0], [1, 2, 0])
responseMaps = score[:,:,:,0]
upsz = design['score_sz']*hp['response_up']
# responseMapsUp = np.zeros([opts['scoreSize']*opts['responseUp'], opts['scoreSize']*opts['responseUp'], opts['numScale']])
responseMapsUP = []
if hp['scale_num'] > 1:
currentScaleID = int(hp['scale_num']/2)
bestScale = currentScaleID
bestPeak = -float('Inf')
for s in range(hp['scale_num']):
if hp['response_up'] > 1:
responseMapsUP.append(cv2.resize(responseMaps[s, :, :], (upsz, upsz), interpolation=cv2.INTER_CUBIC))
else:
responseMapsUP.append(responseMaps[s, :, :])
thisResponse = responseMapsUP[-1]
if s != currentScaleID:
thisResponse = thisResponse*hp['scale_penalty']
thisPeak = np.max(thisResponse)
if thisPeak > bestPeak:
bestPeak = thisPeak
bestScale = s
responseMap = responseMapsUP[bestScale]
else:
responseMap = cv2.resize(responseMaps[0, :, :], (upsz, upsz), interpolation=cv2.INTER_CUBIC)
bestScale = 0
responseMap = responseMap - np.min(responseMap)
responseMap = responseMap/np.sum(responseMap)
responseMap = (1-hp['window_influence'])*responseMap+hp['window_influence']*window
rMax, cMax = np.unravel_index(responseMap.argmax(), responseMap.shape)
pCorr = np.array((rMax, cMax))
dispInstanceFinal = pCorr-int(upsz/2)
dispInstanceInput = dispInstanceFinal*design['tot_stride']/hp['response_up']
dispInstanceFrame = dispInstanceInput*sx/design['search_sz']
newTargetPosition = targetPosition+dispInstanceFrame
# print(bestScale)
return newTargetPosition, bestScale
def makeScalePyramid(im, targetPosition, in_side_scaled, out_side, avgChans, numScale):
"""
computes a pyramid of re-scaled copies of the target (centered on TARGETPOSITION)
and resizes them to OUT_SIDE. If crops exceed image boundaries they are padded with AVGCHANS.
"""
in_side_scaled = np.round(in_side_scaled)
max_target_side = int(round(in_side_scaled[-1]))
min_target_side = int(round(in_side_scaled[0]))
beta = out_side / float(min_target_side)
# size_in_search_area = beta * size_in_image
# e.g. out_side = beta * min_target_side
search_side = int(round(beta * max_target_side))
search_region, _ = getSubWinTracking(im, targetPosition, (search_side, search_side),
(max_target_side, max_target_side), avgChans)
assert round(beta * min_target_side) == int(out_side)
tmp_list = []
tmp_pos = ((search_side - 1) / 2., (search_side - 1) / 2.)
for s in range(numScale):
target_side = round(beta * in_side_scaled[s])
tmp_region, _ = getSubWinTracking(search_region, tmp_pos, (out_side, out_side), (target_side, target_side),
avgChans)
tmp_list.append(tmp_region)
pyramid = np.stack(tmp_list)
return pyramid
def _init_video(video):
root_dataset = '/home/xiaobai/dataset/OTB/'
video += '/'
video_folder = os.path.join(root_dataset, video,'img')
frame_name_list = [f for f in os.listdir(video_folder) if f.endswith(".jpg")]
frame_name_list = [os.path.join(root_dataset, video,'img', '') + s for s in frame_name_list]
frame_name_list.sort()
#with Image.open(frame_name_list[0]) as img:
img = Image.open(frame_name_list[0])
frame_sz = np.asarray(img.size)
frame_sz[1], frame_sz[0] = frame_sz[0], frame_sz[1]
# read the initialization from ground truth
gt_file = os.path.join(root_dataset,video, 'groundtruth_rect.txt')
gt = np.genfromtxt(gt_file, delimiter=',')
if len(gt.shape) < 2:
gt = np.genfromtxt(gt_file)
n_frames = len(frame_name_list)
if n_frames > len(gt):
frame_name_list = frame_name_list[:len(gt)]
return gt, frame_name_list, frame_sz, n_frames
def _compile_results(gt, bboxes, dist_threshold):
l = np.size(bboxes, 0)
gt4 = np.zeros((l, 4))
new_distances = np.zeros(l)
new_ious = np.zeros(l)
n_thresholds = 50
precisions_ths = np.zeros(n_thresholds)
for i in range(l):
gt4[i, :] = region_to_bbox(gt[i, :], center=False)
new_distances[i] = _compute_distance(bboxes[i, :], gt4[i, :])
new_ious[i] = _compute_iou(bboxes[i, :], gt4[i, :])
# what's the percentage of frame in which center displacement is inferior to given threshold? (OTB metric)
precision = sum(new_distances < dist_threshold)/float(np.size(new_distances)) * 100.0
# find above result for many thresholds, then report the AUC
thresholds = np.linspace(0, 25, n_thresholds+1)
thresholds = thresholds[-n_thresholds:]
# reverse it so that higher values of precision goes at the beginning
thresholds = thresholds[::-1]
for i in range(n_thresholds):
precisions_ths[i] = sum(new_distances < thresholds[i])/float(np.size(new_distances))
# integrate over the thresholds
precision_auc = np.trapz(precisions_ths)
# per frame averaged intersection over union (OTB metric)
iou = np.mean(new_ious) * 100
return l, precision, precision_auc, iou
def _compute_distance(boxA, boxB):
a = np.array((boxA[0]+boxA[2]/2, boxA[1]+boxA[3]/2))
b = np.array((boxB[0]+boxB[2]/2, boxB[1]+boxB[3]/2))
dist = np.linalg.norm(a - b)
assert dist >= 0
assert dist != float('Inf')
return dist
def _compute_iou(boxA, boxB):
# determine the (x, y)-coordinates of the intersection rectangle
xA = max(boxA[0], boxB[0])
yA = max(boxA[1], boxB[1])
xB = min(boxA[0] + boxA[2], boxB[0] + boxB[2])
yB = min(boxA[1] + boxA[3], boxB[1] + boxB[3])
if xA < xB and yA < yB:
# compute the area of intersection rectangle
interArea = (xB - xA) * (yB - yA)
# compute the area of both the prediction and ground-truth
# rectangles
boxAArea = boxA[2] * boxA[3]
boxBArea = boxB[2] * boxB[3]
# compute the intersection over union by taking the intersection
# area and dividing it by the sum of prediction + ground-truth
# areas - the intersection area
iou = interArea / float(boxAArea + boxBArea - interArea)
else:
iou = 0
assert iou >= 0
assert iou <= 1.01
return iou
def getOpts(opts):
print("config opts...")
opts['numScale'] = 3
opts['scaleStep'] = 1.0375
opts['scalePenalty'] = 0.9745
# opts['scalePenalty'] = 1/0.9745
opts['scaleLr'] = 0.59
opts['responseUp'] = 16
opts['windowing'] = 'cosine'
opts['wInfluence'] = 0.176
opts['exemplarSize'] = 127
opts['instanceSize'] = 239
opts['scoreSize'] = 17
opts['totalStride'] = 8
opts['contextAmount'] = 0.5
opts['trainWeightDecay'] = 5e-04
opts['stddev'] = 0.03
opts['subMean'] = False
opts['video'] = 'vot15_bag'
opts['modelPath'] = './models/'
opts['modelName'] = opts['modelPath']+"model_tf.ckpt"
opts['summaryFile'] = './data_track/'+opts['video']+'_20170518'
return opts
def makeGaussian(size, fwhm=3, center=None):
""" Make a square gaussian kernel.
size is the length of a side of the square
fwhm is full-width-half-maximum, which
can be thought of as an effective radius.
"""
x = np.arange(0, size, 1, float)
y = x[:, np.newaxis]
if center is None:
x0 = y0 = size // 2
else:
x0 = center[0]
y0 = center[1]
return np.exp(-4 * np.log(2) * ((x - x0) ** 2 + (y - y0) ** 2) / fwhm ** 2)