forked from arbitularov/SiamFusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
siamfc.py
366 lines (297 loc) · 12.1 KB
/
siamfc.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
from __future__ import absolute_import, division
import torch
import torch.nn as nn
import torch.nn.init as init
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
import cv2
from collections import namedtuple
from torch.optim.lr_scheduler import ExponentialLR
from got10k.trackers import Tracker
class SiamFC(nn.Module):
def __init__(self):
super(SiamFC, self).__init__()
self.feature1 = nn.Sequential(
# conv1
nn.Conv2d(3, 192, 11, 2),
nn.BatchNorm2d(192, eps=1e-6, momentum=0.05),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2))
self.feature2 = nn.Sequential(
# conv1
nn.Conv2d(3, 192, 11, 2),
nn.BatchNorm2d(192, eps=1e-6, momentum=0.05),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2))
self.feature3 = nn.Sequential(
# conv2
nn.Conv2d(192, 256, 3, 1),
nn.Conv2d(256, 256, 3, 1),
nn.BatchNorm2d(256, eps=1e-6, momentum=0.05),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2),
# conv3
nn.Conv2d(256, 512, 3, 1),
nn.BatchNorm2d(512, eps=1e-6, momentum=0.05),
nn.ReLU(inplace=True),
nn.Conv2d(512, 512, 3, 1),
nn.BatchNorm2d(512, eps=1e-6, momentum=0.05),
nn.ReLU(inplace=True),
nn.Conv2d(512, 384, 3, 1),
)
self._initialize_weights()
def forward(self, z, z_noise, x, x_noise):
z = self.feature1(z)
z_noise = self.feature2(z_noise)
z = torch.add(z, z_noise)
z = self.feature3(z)
x = self.feature1(x)
x_noise = self.feature2(x_noise)
x = torch.add(x, x_noise)
x = self.feature3(x)
# fast cross correlation
out = self.fast_cross(x, z, "out", 0.1)
#out = torch.add(cross4, out)
# adjust the scale of responses
out = 0.001 * out + 0.0
return out
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
init.kaiming_normal_(m.weight.data, mode='fan_out',
nonlinearity='relu')
m.bias.data.fill_(0)
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def fast_cross(self, x, z, img_name = None, num = 0.001):
n, c, h, w = x.size()
out = F.conv2d(x.view(1, n * c, h, w), z, groups=n)
out = out.view(n, 1, out.size(-2), out.size(-1))
if img_name != None:
out_img = num * out + 0.0
out_img = np.asarray(out_img[0].permute(1,2,0).detach().cpu())
cv2.imwrite("{}.png".format(img_name), out_img)
return out
class TrackerSiamFC(Tracker):
def __init__(self, net_path=None, **kargs):
super(TrackerSiamFC, self).__init__(
name='SiamFC', is_deterministic=True)
self.cfg = self.parse_args(**kargs)
# setup GPU device if available
self.cuda = torch.cuda.is_available()
self.device = torch.device('cuda:0' if self.cuda else 'cpu')
# setup model
self.net = SiamFC()
if net_path is not None:
self.net.load_state_dict(torch.load(
net_path, map_location=lambda storage, loc: storage))
self.net = self.net.to(self.device)
# setup optimizer
self.optimizer = optim.SGD(
self.net.parameters(),
lr=self.cfg.initial_lr,
weight_decay=self.cfg.weight_decay,
momentum=self.cfg.momentum)
# setup lr scheduler
self.lr_scheduler = ExponentialLR(
self.optimizer, gamma=self.cfg.lr_decay)
def parse_args(self, **kargs):
# default parameters
cfg = {
# inference parameters
'exemplar_sz': 127,
'instance_sz': 255,
'context': 0.5,
'scale_num': 3,
'scale_step': 1.0375,
'scale_lr': 0.59,
'scale_penalty': 0.9745,
'window_influence': 0.176,
'response_sz': 17,
'response_up': 16,
'total_stride': 8,
'adjust_scale': 0.001,
# train parameters
'initial_lr': 0.01,
'lr_decay': 0.8685113737513527,
'weight_decay': 5e-4,
'momentum': 0.9,
'r_pos': 16,
'r_neg': 0}
for key, val in kargs.items():
if key in cfg:
cfg.update({key: val})
return namedtuple('GenericDict', cfg.keys())(**cfg)
def init(self, image, box):
image = np.asarray(image)
# convert box to 0-indexed and center based [y, x, h, w]
box = np.array([
box[1] - 1 + (box[3] - 1) / 2,
box[0] - 1 + (box[2] - 1) / 2,
box[3], box[2]], dtype=np.float32)
self.center, self.target_sz = box[:2], box[2:]
# create hanning window
self.upscale_sz = self.cfg.response_up * self.cfg.response_sz
self.hann_window = np.outer(
np.hanning(self.upscale_sz),
np.hanning(self.upscale_sz))
self.hann_window /= self.hann_window.sum()
# search scale factors
self.scale_factors = self.cfg.scale_step ** np.linspace(
-(self.cfg.scale_num // 2),
self.cfg.scale_num // 2, self.cfg.scale_num)
# exemplar and search sizes
context = self.cfg.context * np.sum(self.target_sz)
self.z_sz = np.sqrt(np.prod(self.target_sz + context))
self.x_sz = self.z_sz * \
self.cfg.instance_sz / self.cfg.exemplar_sz
# exemplar image
self.avg_color = np.mean(image, axis=(0, 1))
exemplar_image = self._crop_and_resize(
image, self.center, self.z_sz,
out_size=self.cfg.exemplar_sz,
pad_color=self.avg_color)
# exemplar features
exemplar_image = torch.from_numpy(exemplar_image).to(
self.device).permute([2, 0, 1]).unsqueeze(0).float()
with torch.set_grad_enabled(False):
self.net.eval()
z = self.net.feature1(exemplar_image)
z_noise = self.net.feature2(exemplar_image)
z = torch.add(z, z_noise)
self.kernel = self.net.feature3(z)
def update(self, image):
image = np.asarray(image)
# search images
instance_images = [self._crop_and_resize(
image, self.center, self.x_sz * f,
out_size=self.cfg.instance_sz,
pad_color=self.avg_color) for f in self.scale_factors]
instance_images = np.stack(instance_images, axis=0)
instance_images = torch.from_numpy(instance_images).to(
self.device).permute([0, 3, 1, 2]).float()
# responses
with torch.set_grad_enabled(False):
self.net.eval()
x = self.net.feature1(instance_images)
x_noise = self.net.feature2(instance_images)
x = torch.add(x, x_noise)
instances = self.net.feature3(x)
responses = F.conv2d(instances, self.kernel) * 0.001
responses = responses.squeeze(1).cpu().numpy()
# upsample responses and penalize scale changes
responses = np.stack([cv2.resize(
t, (self.upscale_sz, self.upscale_sz),
interpolation=cv2.INTER_CUBIC) for t in responses], axis=0)
responses[:self.cfg.scale_num // 2] *= self.cfg.scale_penalty
responses[self.cfg.scale_num // 2 + 1:] *= self.cfg.scale_penalty
# peak scale
scale_id = np.argmax(np.amax(responses, axis=(1, 2)))
# peak location
response = responses[scale_id]
response -= response.min()
response /= response.sum() + 1e-16
response = (1 - self.cfg.window_influence) * response + \
self.cfg.window_influence * self.hann_window
loc = np.unravel_index(response.argmax(), response.shape)
# locate target center
disp_in_response = np.array(loc) - self.upscale_sz // 2
disp_in_instance = disp_in_response * \
self.cfg.total_stride / self.cfg.response_up
disp_in_image = disp_in_instance * self.x_sz * \
self.scale_factors[scale_id] / self.cfg.instance_sz
self.center += disp_in_image
# update target size
scale = (1 - self.cfg.scale_lr) * 1.0 + \
self.cfg.scale_lr * self.scale_factors[scale_id]
self.target_sz *= scale
self.z_sz *= scale
self.x_sz *= scale
# return 1-indexed and left-top based bounding box
box = np.array([
self.center[1] + 1 - (self.target_sz[1] - 1) / 2,
self.center[0] + 1 - (self.target_sz[0] - 1) / 2,
self.target_sz[1], self.target_sz[0]])
return box
def step(self, batch, backward=True, update_lr=False):
if backward:
self.net.train()
if update_lr:
self.lr_scheduler.step()
else:
self.net.eval()
z = batch[0].to(self.device)
z_noise = batch[1].to(self.device)
x = batch[2].to(self.device)
x_noise = batch[3].to(self.device)
with torch.set_grad_enabled(backward):
responses = self.net(z, z_noise, x, x_noise)
labels, weights = self._create_labels(responses.size())
loss = F.binary_cross_entropy_with_logits(
responses, labels, weight=weights, size_average=True)
if backward:
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
print(loss.item())
return loss.item()
def _crop_and_resize(self, image, center, size, out_size, pad_color):
# convert box to corners (0-indexed)
size = round(size)
corners = np.concatenate((
np.round(center - (size - 1) / 2),
np.round(center - (size - 1) / 2) + size))
corners = np.round(corners).astype(int)
# pad image if necessary
pads = np.concatenate((
-corners[:2], corners[2:] - image.shape[:2]))
npad = max(0, int(pads.max()))
if npad > 0:
image = cv2.copyMakeBorder(
image, npad, npad, npad, npad,
cv2.BORDER_CONSTANT, value=pad_color)
# crop image patch
corners = (corners + npad).astype(int)
patch = image[corners[0]:corners[2], corners[1]:corners[3]]
# resize to out_size
patch = cv2.resize(patch, (out_size, out_size))
return patch
def _create_labels(self, size):
# skip if same sized labels already created
if hasattr(self, 'labels') and self.labels.size() == size:
return self.labels, self.weights
def logistic_labels(x, y, r_pos, r_neg):
dist = np.abs(x) + np.abs(y) # block distance
labels = np.where(dist <= r_pos,
np.ones_like(x),
np.where(dist < r_neg,
np.ones_like(x) * 0.5,
np.zeros_like(x)))
return labels
# distances along x- and y-axis
n, c, h, w = size
x = np.arange(w) - w // 2
y = np.arange(h) - h // 2
x, y = np.meshgrid(x, y)
# create logistic labels
r_pos = self.cfg.r_pos / self.cfg.total_stride
r_neg = self.cfg.r_neg / self.cfg.total_stride
labels = logistic_labels(x, y, r_pos, r_neg)
# pos/neg weights
pos_num = np.sum(labels == 1)
neg_num = np.sum(labels == 0)
weights = np.zeros_like(labels)
weights[labels == 1] = 0.5 / pos_num
weights[labels == 0] = 0.5 / neg_num
weights *= pos_num + neg_num
# repeat to size
labels = labels.reshape((1, 1, h, w))
weights = weights.reshape((1, 1, h, w))
labels = np.tile(labels, (n, c, 1, 1))
weights = np.tile(weights, [n, c, 1, 1])
# convert to tensors
self.labels = torch.from_numpy(labels).to(self.device).float()
self.weights = torch.from_numpy(weights).to(self.device).float()
return self.labels, self.weights