resnet_v2_50.py
Go to the documentation of this file.
1 from __future__ import print_function
2 
3 import itertools, pkg_resources, sys
4 from distutils.version import LooseVersion
5 if LooseVersion(pkg_resources.get_distribution("chainer").version) >= LooseVersion('7.0.0') and \
6  sys.version_info.major == 2:
7  print('''Please install chainer < 7.0.0:
8 
9  sudo pip install chainer==6.7.0
10 
11 c.f https://github.com/jsk-ros-pkg/jsk_recognition/pull/2485
12 ''', file=sys.stderr)
13  sys.exit(1)
14 if [p for p in list(itertools.chain(*[pkg_resources.find_distributions(_) for _ in sys.path])) if "cupy-" in p.project_name ] == []:
15  print('''Please install CuPy
16 
17  sudo pip install cupy-cuda[your cuda version]
18 i.e.
19  sudo pip install cupy-cuda91
20 
21 ''', file=sys.stderr)
22  # sys.exit(1)
23 import chainer
24 import chainer.functions as F
25 from chainer import initializers
26 import chainer.links as L
27 
28 
29 class BottleNeckA(chainer.Chain):
30 
31  def __init__(self, in_size, ch, out_size, stride=2):
32  super(BottleNeckA, self).__init__()
33  initialW = initializers.HeNormal()
34  self.stride = stride
35 
36  with self.init_scope():
37  self.preact = L.BatchNormalization(in_size)
38  self.conv1 = L.Convolution2D(
39  in_size, ch, 1, stride, 0, initialW=initialW, nobias=True)
40  self.bn1 = L.BatchNormalization(ch)
41  self.conv2 = L.Convolution2D(
42  ch, ch, 3, stride, 1, initialW=initialW, nobias=True)
43  self.bn2 = L.BatchNormalization(ch)
44  self.conv3 = L.Convolution2D(
45  ch, out_size, 1, 1, 0, initialW=initialW, nobias=False)
46  self.conv4 = L.Convolution2D(
47  in_size, out_size, 1, stride, 0,
48  initialW=initialW, nobias=False)
49 
50  def __call__(self, x):
51  preact = F.relu(self.preact(x))
52  shortcut = self.conv4(preact)
53  h = self.conv1(preact)
54  h = F.relu(self.bn1(h))
55  h = F.relu(self.bn2(self.conv2(h)))
56  h = self.conv3(h)
57  return shortcut + h
58 
59 
60 class BottleNeckB(chainer.Chain):
61 
62  def __init__(self, in_size, ch, stride=2):
63  super(BottleNeckB, self).__init__()
64  initialW = initializers.HeNormal()
65  self.stride = stride
66 
67  with self.init_scope():
68  self.preact = L.BatchNormalization(in_size)
69  self.conv1 = L.Convolution2D(
70  in_size, ch, 1, 1, 0, initialW=initialW, nobias=True)
71  self.bn1 = L.BatchNormalization(ch)
72  self.conv2 = L.Convolution2D(
73  ch, ch, 3, self.stride, 1, initialW=initialW, nobias=True)
74  self.bn2 = L.BatchNormalization(ch)
75  self.conv3 = L.Convolution2D(
76  ch, in_size, 1, 1, 0, initialW=initialW, nobias=False)
77 
78  def __call__(self, x):
79  preact = F.relu(self.preact(x))
80  if self.stride == 1:
81  shortcut = x
82  else:
83  shortcut = F.max_pooling_2d(
84  x, 1, stride=self.stride, cover_all=False)
85  h = self.conv1(preact)
86  h = F.relu(self.bn1(h))
87  h = F.relu(self.bn2(self.conv2(h)))
88  h = self.conv3(h)
89  return shortcut + h
90 
91 
92 class Block(chainer.ChainList):
93 
94  def __init__(self, layer, in_size, ch, out_size, stride=2):
95  super(Block, self).__init__()
96  self.add_link(BottleNeckA(in_size, ch, out_size, 1))
97  for i in range(layer - 2):
98  self.add_link(BottleNeckB(out_size, ch, stride=1))
99  self.add_link(BottleNeckB(out_size, ch, stride=stride))
100 
101  def __call__(self, x):
102  for f in self.children():
103  x = f(x)
104  return x
105 
106 
107 class ResNet_v2_50(chainer.Chain):
108 
109  insize = 224
110 
111  def __init__(self):
112  super(ResNet_v2_50, self).__init__()
113  with self.init_scope():
114  self.conv1 = L.Convolution2D(
115  3, 64, 7, 2, 3, initialW=initializers.HeNormal())
116  self.res2 = Block(3, 64, 64, 256, stride=2)
117  self.res3 = Block(4, 256, 128, 512, stride=2)
118  self.res4 = Block(6, 512, 256, 1024, stride=2)
119  self.res5 = Block(3, 1024, 512, 2048, stride=1)
120  self.postnorm = L.BatchNormalization(2048)
121 
122  def __call__(self, x):
123  h = self.conv1(x)
124  h = F.max_pooling_2d(h, 3, stride=2)
125  h = self.res2(h)
126  h = self.res3(h)
127  h = self.res4(h)
128  h = self.res5(h)
129  h = self.postnorm(h)
130  h = F.relu(h)
131  h = F.average_pooling_2d(h, 7, stride=1)
132  return h
def __init__(self, in_size, ch, out_size, stride=2)
Definition: resnet_v2_50.py:31
def __init__(self, layer, in_size, ch, out_size, stride=2)
Definition: resnet_v2_50.py:94
def __init__(self, in_size, ch, stride=2)
Definition: resnet_v2_50.py:62


jsk_perception
Author(s): Manabu Saito, Ryohei Ueda
autogenerated on Mon May 3 2021 03:03:27