deep_sort_net.py
Go to the documentation of this file.
1 import chainer
2 import chainer.functions as F
3 import chainer.links as L
4 from chainer import initializers
5 
6 
7 class BlockA(chainer.Chain):
8 
9  def __init__(self, in_size, out_size, stride=1, activation=F.elu,
10  is_first=False, pad=1, projection_pad=0, flag=False):
11  super(BlockA, self).__init__()
12  initialW = initializers.HeNormal()
13 
14  self.in_size = in_size
15  self.out_size = out_size
16  self.is_first = is_first
17  self.activation = activation
18  self.flag = flag
19  with self.init_scope():
20  if is_first is False:
21  self.bn0 = L.BatchNormalization(in_size)
22  if flag is False:
23  self.conv1 = L.Convolution2D(
24  in_size, out_size, 3, stride, pad,
25  initialW=initialW, nobias=True)
26  else:
27  self.conv1 = L.Convolution2D(
28  in_size, out_size, 3, stride, 0,
29  initialW=initialW, nobias=True)
30  self.bn1 = L.BatchNormalization(out_size)
31  self.conv2 = L.Convolution2D(
32  out_size, out_size, 3, 1,
33  pad, initialW=initialW, nobias=False)
34  if in_size != out_size:
35  if out_size != 2 * in_size:
36  raise ValueError('out_size should be two 2 * in_size ',
37  '{} != {}'.format(out_size, 2 * in_size))
38  self.projection = L.Convolution2D(
39  in_size, out_size,
40  1, stride, projection_pad, nobias=True)
41 
42  def __call__(self, x):
43  batchsize, channel, height, width = x.shape
44  if self.is_first:
45  h = x
46  else:
47  h = self.activation(self.bn0(x))
48  if self.flag:
49  # for (top, bottom, left, right) padding
50  # (0, 1, 0, 1)
51  h = F.concat([F.concat([
52  h,
53  self.xp.zeros((batchsize, channel, 1, width), 'f')], axis=2),
54  self.xp.zeros((
55  batchsize, channel, height + 1, 1), 'f')],
56  axis=3)
57  h = self.activation(self.bn1(self.conv1(h)))
58  else:
59  h = self.activation(self.bn1(self.conv1(h)))
60  h = F.dropout(h, ratio=0.6)
61  h = self.conv2(h)
62  if self.in_size != self.out_size:
63  return self.projection(x) + h
64  else:
65  return x + h
66 
67 
68 class DeepSortFeatureExtractor(chainer.Chain):
69 
70  def __init__(self):
71  super(DeepSortFeatureExtractor, self).__init__()
72 
73  with self.init_scope():
74  self.conv1_1 = L.Convolution2D(
75  3, 32, 3, 1, 1, nobias=True)
76  self.bn1 = L.BatchNormalization(32)
77  self.conv1_2 = L.Convolution2D(
78  32, 32, 3, 1, 1, nobias=True)
79  self.bn2 = L.BatchNormalization(32)
80  self.conv2_1 = BlockA(32, 32, stride=1, is_first=True)
81  self.conv2_3 = BlockA(32, 32, stride=1)
82  self.conv3_1 = BlockA(
83  32, 64, stride=2, pad=1, projection_pad=0)
84  self.conv3_3 = BlockA(64, 64, stride=1)
85  self.conv4_1 = BlockA(
86  64, 128, stride=2, pad=1, projection_pad=0, flag=True)
87  self.conv4_3 = BlockA(128, 128, stride=1)
88  self.fc1 = L.Linear(16384, 128, nobias=True)
89  self.fc1_bn = L.BatchNormalization(128)
90 
91  self.ball = L.BatchNormalization(128)
92  self.mean_vectors = chainer.Parameter(0, shape=[128, 1501])
93  self.scale = chainer.Parameter(0, shape=[1501])
94 
95  def __call__(self, x):
96  # x.shape == (batchsize, 3, 128, 64)
97  batchsize = x.shape[0]
98  h = F.elu(self.bn1(self.conv1_1(x)))
99  h = F.elu(self.bn2(self.conv1_2(h)))
100  h = F.max_pooling_2d(h, 3, 2, cover_all=False)
101  h = self.conv2_1(h)
102  h = self.conv2_3(h)
103  h = self.conv3_1(h)
104  h = self.conv3_3(h)
105  h = self.conv4_1(h)
106  h = self.conv4_3(h)
107 
108  h = h.reshape(batchsize, -1)
109  h = F.dropout(h, ratio=0.6)
110  h = F.elu(self.fc1_bn(self.fc1(h)))
111 
112  # Features in rows, normalize axis 1.
113  weights = self.mean_vectors
114  features = self.ball(h)
115  features = F.normalize(features, eps=1e-8)
116  scale = F.softplus(self.scale)
117  normalized_weight = F.normalize(weights, axis=0, eps=1e-8)
118  logits = F.tile(scale[None, ], (batchsize, 1)) * \
119  F.matmul(features, normalized_weight)
120  return logits
def __init__(self, in_size, out_size, stride=1, activation=F.elu, is_first=False, pad=1, projection_pad=0, flag=False)


jsk_recognition_utils
Author(s):
autogenerated on Mon May 3 2021 03:03:03