resnet101.py
Go to the documentation of this file.
1 # Modified work: Copyright (c) 2017 Kentaro Wada.
2 # Original work: https://github.com/yasunorikudo/chainer-ResNet
3 
4 import chainer
5 import chainer.functions as F
6 import chainer.links as L
7 
8 
9 class BottleNeckA(chainer.Chain):
10 
11  def __init__(self, in_size, ch, out_size, stride=2):
12  initialW = chainer.initializers.HeNormal()
13  super(BottleNeckA, self).__init__(
14  conv1=L.Convolution2D(
15  in_size, ch, 1, stride, 0, nobias=True, initialW=initialW),
16  bn1=L.BatchNormalization(ch),
17  conv2=L.Convolution2D(
18  ch, ch, 3, 1, 1, nobias=True, initialW=initialW),
19  bn2=L.BatchNormalization(ch),
20  conv3=L.Convolution2D(
21  ch, out_size, 1, 1, 0, nobias=True, initialW=initialW),
22  bn3=L.BatchNormalization(out_size),
23 
24  conv4=L.Convolution2D(
25  in_size, out_size, 1, stride, 0,
26  nobias=True, initialW=initialW),
27  bn4=L.BatchNormalization(out_size),
28  )
29 
30  def __call__(self, x):
31  h1 = F.relu(self.bn1(self.conv1(x)))
32  h1 = F.relu(self.bn2(self.conv2(h1)))
33  h1 = self.bn3(self.conv3(h1))
34  h2 = self.bn4(self.conv4(x))
35 
36  return F.relu(h1 + h2)
37 
38 
39 class BottleNeckB(chainer.Chain):
40 
41  def __init__(self, in_size, ch):
42  initialW = chainer.initializers.HeNormal()
43  super(BottleNeckB, self).__init__(
44  conv1=L.Convolution2D(
45  in_size, ch, 1, 1, 0, nobias=True, initialW=initialW),
46  bn1=L.BatchNormalization(ch),
47  conv2=L.Convolution2D(
48  ch, ch, 3, 1, 1, nobias=True, initialW=initialW),
49  bn2=L.BatchNormalization(ch),
50  conv3=L.Convolution2D(
51  ch, in_size, 1, 1, 0, nobias=True, initialW=initialW),
52  bn3=L.BatchNormalization(in_size),
53  )
54 
55  def __call__(self, x):
56  h = F.relu(self.bn1(self.conv1(x)))
57  h = F.relu(self.bn2(self.conv2(h)))
58  h = self.bn3(self.conv3(h))
59 
60  return F.relu(h + x)
61 
62 
63 class Block(chainer.Chain):
64 
65  def __init__(self, layer, in_size, ch, out_size, stride=2):
66  super(Block, self).__init__()
67  links = [('a', BottleNeckA(in_size, ch, out_size, stride))]
68  for i in range(layer - 1):
69  links += [('b{}'.format(i + 1), BottleNeckB(out_size, ch))]
70 
71  for l in links:
72  self.add_link(*l)
73  self.forward = links
74 
75  def __call__(self, x):
76  for name, _ in self.forward:
77  f = getattr(self, name)
78  x = f(x)
79 
80  return x
81 
82 
83 class ResNet101(chainer.Chain):
84 
85  insize = 224
86 
87  def __init__(self):
88  initialW = chainer.initializers.HeNormal()
89  super(ResNet101, self).__init__(
90  conv1=L.Convolution2D(
91  3, 64, 7, 2, 3, nobias=True, initialW=initialW),
92  bn1=L.BatchNormalization(64),
93  res2=Block(3, 64, 64, 256, 1),
94  res3=Block(4, 256, 128, 512),
95  res4=Block(23, 512, 256, 1024),
96  res5=Block(3, 1024, 512, 2048),
97  fc=L.Linear(2048, 1000),
98  )
99 
100  def clear(self):
101  self.loss = None
102  self.accuracy = None
103 
104  def __call__(self, x, t=None):
105  self.clear()
106  h = self.bn1(self.conv1(x))
107  h = F.max_pooling_2d(F.relu(h), 3, stride=2)
108  h = self.res2(h)
109  h = self.res3(h)
110  h = self.res4(h)
111  h = self.res5(h)
112  h = F.average_pooling_2d(h, 7, stride=1)
113  h = self.fc(h)
114 
115  if t is None:
116  return h
117 
118  self.loss = F.softmax_cross_entropy(h, t)
119  self.accuracy = F.accuracy(h, t)
120  return self.loss
121 
122 
124 
125  def __call__(self, x):
126  h = self.bn1(self.conv1(x))
127  h = F.max_pooling_2d(F.relu(h), 3, stride=2)
128  h = self.res2(h)
129  h = self.res3(h)
130  h = self.res4(h)
131  h = self.res5(h)
132  h = F.average_pooling_2d(h, 7, stride=1)
133  return h
def __init__(self, layer, in_size, ch, out_size, stride=2)
Definition: resnet101.py:65
def __init__(self, in_size, ch, out_size, stride=2)
Definition: resnet101.py:11


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