vgg_cnn_m_1024.py
Go to the documentation of this file.
1 import chainer
2 from chainer import cuda
3 import chainer.functions as F
4 import chainer.links as L
5 from chainer import Variable
6 from distutils.version import LooseVersion
7 
8 
9 class VGG_CNN_M_1024(chainer.Chain):
10 
11  def __init__(self, n_class=21, bg_label=-1):
12  super(VGG_CNN_M_1024, self).__init__(
13  conv1=L.Convolution2D(3, 96, ksize=7, stride=2),
14  conv2=L.Convolution2D(96, 256, ksize=5, stride=2, pad=1),
15  conv3=L.Convolution2D(256, 512, ksize=3, stride=1, pad=1),
16  conv4=L.Convolution2D(512, 512, ksize=3, stride=1, pad=1),
17  conv5=L.Convolution2D(512, 512, ksize=3, stride=1, pad=1),
18  fc6=L.Linear(18432, 4096),
19  fc7=L.Linear(4096, 1024),
20  cls_score=L.Linear(1024, n_class),
21  bbox_pred=L.Linear(1024, 4 * n_class)
22  )
23  self.n_class = n_class
24  self.bg_label = bg_label
25 
26  def __call__(self, x, rois, t=None):
27  h = self.conv1(x)
28  h = F.relu(h)
29  h = F.local_response_normalization(h, n=5, k=2, alpha=5e-4, beta=.75)
30  h = F.max_pooling_2d(h, ksize=3, stride=2)
31 
32  h = self.conv2(h)
33  h = F.relu(h)
34  h = F.local_response_normalization(h, n=5, k=2, alpha=5e-4, beta=.75)
35  h = F.max_pooling_2d(h, ksize=3, stride=2)
36 
37  h = self.conv3(h)
38  h = F.relu(h)
39 
40  h = self.conv4(h)
41  h = F.relu(h)
42 
43  h = self.conv5(h)
44  h = F.relu(h)
45 
46  h = F.roi_pooling_2d(h, rois, 6, 6, spatial_scale=0.0625)
47 
48  h = self.fc6(h)
49  h = F.relu(h)
50  h = F.dropout(h, ratio=.5)
51 
52  h = self.fc7(h)
53  h = F.relu(h)
54  h = F.dropout(h, ratio=.5)
55 
56  h_cls_score = self.cls_score(h)
57  cls_score = F.softmax(h_cls_score)
58  bbox_pred = self.bbox_pred(h)
59 
60  if t is None:
61  assert not chainer.config.train
62  return cls_score, bbox_pred
63 
64  t_cls, t_bbox = t
65  self.cls_loss = F.softmax_cross_entropy(h_cls_score, t_cls)
66  self.bbox_loss = F.smooth_l1_loss(bbox_pred, t_bbox)
67 
68  xp = cuda.get_array_module(x.data)
69  lambda_ = (0.5 * (t_cls.data != self.bg_label)).astype(xp.float32)
70  L = self.cls_loss + F.sum(lambda_ * self.bbox_loss)
71  return L


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