-
Notifications
You must be signed in to change notification settings - Fork 2
/
SiamFC.py
60 lines (51 loc) · 1.81 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
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 numpy as np
class SiamFC(nn.Module):
def __init__(self):
super(SiamFC, self).__init__()
self.feature = nn.Sequential(
# conv1
nn.Conv2d(3, 96, 11, 2),
nn.BatchNorm2d(96, eps=1e-6, momentum=0.05),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2),
# conv2
nn.Conv2d(96, 256, 5, 1, groups=2),
nn.BatchNorm2d(256, eps=1e-6, momentum=0.05),
nn.ReLU(inplace=True),
nn.MaxPool2d(3, 2),
# conv3
nn.Conv2d(256, 384, 3, 1),
nn.BatchNorm2d(384, eps=1e-6, momentum=0.05),
nn.ReLU(inplace=True),
# conv4
nn.Conv2d(384, 384, 3, 1, groups=2),
nn.BatchNorm2d(384, eps=1e-6, momentum=0.05),
nn.ReLU(inplace=True),
# conv5
nn.Conv2d(384, 256, 3, 1, groups=2))
self._initialize_weights()
def forward(self, z, x):
z = self.feature(z)
x = self.feature(x)
# fast cross correlation
n, c, h, w = x.size()
x = x.view(1, n * c, h, w)
out = F.conv2d(x, z, groups=n)
out = out.view(n, 1, out.size(-2), out.size(-1))
# 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_()