00001 import chainer 00002 from chainer import cuda 00003 import chainer.functions as F 00004 import chainer.links as L 00005 from chainer import Variable 00006 from distutils.version import LooseVersion 00007 00008 00009 class VGG_CNN_M_1024(chainer.Chain): 00010 00011 def __init__(self, n_class=21, bg_label=-1): 00012 super(VGG_CNN_M_1024, self).__init__( 00013 conv1=L.Convolution2D(3, 96, ksize=7, stride=2), 00014 conv2=L.Convolution2D(96, 256, ksize=5, stride=2, pad=1), 00015 conv3=L.Convolution2D(256, 512, ksize=3, stride=1, pad=1), 00016 conv4=L.Convolution2D(512, 512, ksize=3, stride=1, pad=1), 00017 conv5=L.Convolution2D(512, 512, ksize=3, stride=1, pad=1), 00018 fc6=L.Linear(18432, 4096), 00019 fc7=L.Linear(4096, 1024), 00020 cls_score=L.Linear(1024, n_class), 00021 bbox_pred=L.Linear(1024, 4 * n_class) 00022 ) 00023 self.n_class = n_class 00024 self.bg_label = bg_label 00025 00026 def __call__(self, x, rois, t=None): 00027 h = self.conv1(x) 00028 h = F.relu(h) 00029 h = F.local_response_normalization(h, n=5, k=2, alpha=5e-4, beta=.75) 00030 h = F.max_pooling_2d(h, ksize=3, stride=2) 00031 00032 h = self.conv2(h) 00033 h = F.relu(h) 00034 h = F.local_response_normalization(h, n=5, k=2, alpha=5e-4, beta=.75) 00035 h = F.max_pooling_2d(h, ksize=3, stride=2) 00036 00037 h = self.conv3(h) 00038 h = F.relu(h) 00039 00040 h = self.conv4(h) 00041 h = F.relu(h) 00042 00043 h = self.conv5(h) 00044 h = F.relu(h) 00045 00046 h = F.roi_pooling_2d(h, rois, 6, 6, spatial_scale=0.0625) 00047 00048 h = self.fc6(h) 00049 h = F.relu(h) 00050 h = F.dropout(h, ratio=.5) 00051 00052 h = self.fc7(h) 00053 h = F.relu(h) 00054 h = F.dropout(h, ratio=.5) 00055 00056 h_cls_score = self.cls_score(h) 00057 cls_score = F.softmax(h_cls_score) 00058 bbox_pred = self.bbox_pred(h) 00059 00060 if t is None: 00061 assert not chainer.config.train 00062 return cls_score, bbox_pred 00063 00064 t_cls, t_bbox = t 00065 self.cls_loss = F.softmax_cross_entropy(h_cls_score, t_cls) 00066 self.bbox_loss = F.smooth_l1_loss(bbox_pred, t_bbox) 00067 00068 xp = cuda.get_array_module(x.data) 00069 lambda_ = (0.5 * (t_cls.data != self.bg_label)).astype(xp.float32) 00070 L = self.cls_loss + F.sum(lambda_ * self.bbox_loss) 00071 return L