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


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