コード例 #1
0
    def __init__(self, pretrained_model, n_layers):
        super(ResNetLayers, self).__init__()

        if pretrained_model:
            # As a sampling process is time-consuming,
            # we employ a zero initializer for faster computation.
            kwargs = {'initialW': constant.Zero()}
        else:
            # employ default initializers used in the original paper
            kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if n_layers == 50:
            block = [3, 4, 6, 3]
        elif n_layers == 101:
            block = [3, 4, 23, 3]
        elif n_layers == 152:
            block = [3, 8, 36, 3]
        else:
            raise ValueError('The n_layers argument should be either 50, 101,'
                             ' or 152, but {} was given.'.format(n_layers))

        with self.init_scope():
            self.conv1 = Convolution2D(3, 64, 7, 2, 3, **kwargs)
            self.bn1 = BatchNormalization(64)
            self.res2 = BuildingBlock(block[0], 64, 64, 256, 1, **kwargs)
            self.res3 = BuildingBlock(block[1], 256, 128, 512, 2, **kwargs)
            self.res4 = BuildingBlock(block[2], 512, 256, 1024, 2, **kwargs)
            self.res5 = BuildingBlock(block[3], 1024, 512, 2048, 2, **kwargs)
            self.fc6 = Linear(2048, 1000)

        if pretrained_model and pretrained_model.endswith('.caffemodel'):
            _retrieve(n_layers, 'ResNet-{}-model.npz'.format(n_layers),
                      pretrained_model, self)
        elif pretrained_model:
            npz.load_npz(pretrained_model, self)
コード例 #2
0
 def __init__(self, pretrained_model='auto'):
     if pretrained_model:
         # As a sampling process is time-consuming,
         # we employ a zero initializer for faster computation.
         kwargs = {'initialW': constant.Zero()}
     else:
         # employ default initializers used in the original paper
         kwargs = {'initialW': normal.HeNormal(scale=1.0)}
     super(ResNet50Layers, self).__init__(
         conv1=Convolution2D(3, 64, 7, 2, 3, **kwargs),
         bn1=BatchNormalization(64),
         res2=BuildingBlock(3, 64, 64, 256, 1, **kwargs),
         res3=BuildingBlock(4, 256, 128, 512, 2, **kwargs),
         res4=BuildingBlock(6, 512, 256, 1024, 2, **kwargs),
         res5=BuildingBlock(3, 1024, 512, 2048, 2, **kwargs),
         fc6=Linear(2048, 1000),
     )
     if pretrained_model == 'auto':
         _retrieve(
             'ResNet-50-model.npz', 'ResNet-50-model.caffemodel', self)
     elif pretrained_model:
         npz.load_npz(pretrained_model, self)
     self.functions = OrderedDict([
         ('conv1', [self.conv1, self.bn1, relu]),
         ('pool1', [lambda x: max_pooling_2d(x, ksize=3, stride=2)]),
         ('res2', [self.res2]),
         ('res3', [self.res3]),
         ('res4', [self.res4]),
         ('res5', [self.res5]),
         ('pool5', [_global_average_pooling_2d]),
         ('fc6', [self.fc6]),
         ('prob', [softmax]),
     ])
コード例 #3
0
 def __init__(self, out_ch: int) -> None:
     super().__init__()
     kw = {'initialW': normal.HeNormal(scale=1.0)}
     with self.init_scope():
         self.conv1 = L.Convolution2D(None,
                                      64,
                                      ksize=7,
                                      stride=2,
                                      pad=3,
                                      **kw)
         self.bn1 = L.BatchNormalization(64)
         self.block2 = Block(64, stride=2)
         self.block3 = Block(64)
         self.block4 = Block(64)
         self.block5 = Block(128, stride=2)
         self.block6 = Block(128)
         self.block7 = Block(128)
         self.block8 = Block(128)
         self.block9 = Block(256, stride=2)
         self.block10 = Block(256)
         self.block11 = Block(256)
         self.block12 = Block(256)
         self.block13 = Block(256)
         self.block14 = Block(256)
         self.block15 = Block(512, stride=2)
         self.block16 = Block(512)
         self.block17 = Block(512)
         self.fc18 = L.Linear(512, out_ch)
コード例 #4
0
    def __init__(self, out_ch: int, stride: int = 1) -> None:
        super().__init__()
        kw = {'initialW': normal.HeNormal(scale=1.0)}
        with self.init_scope():
            self.conv1 = L.Convolution2D(None,
                                         out_ch,
                                         ksize=3,
                                         stride=stride,
                                         pad=1,
                                         nobias=True,
                                         **kw)
            self.bn1 = L.BatchNormalization(out_ch)
            self.conv2 = L.Convolution2D(out_ch,
                                         out_ch,
                                         ksize=3,
                                         pad=1,
                                         nobias=True,
                                         **kw)
            self.bn2 = L.BatchNormalization(out_ch)
            if stride != 1:
                self.conv_skip = L.Convolution2D(None,
                                                 out_ch,
                                                 ksize=1,
                                                 stride=stride,
                                                 nobias=True,
                                                 **kw)
                self.bn_skip = L.BatchNormalization(out_ch)

        self.stride = stride
コード例 #5
0
    def __init__(self, n_out=10, n_layer=26, base_width=64):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if (n_layer - 2) % 6 == 0:
            n_block = (n_layer - 2) // 6
            # n_layer = n_stage(=3) * n_block * n_conv(=2) + 2(conv1 + fc5)
        else:
            raise ValueError(
                'The n_layer argument should be mod({} - 2, 6) == 0, \
                 but {} was given.'.format(n_layer, n_layer))

        with self.init_scope():
            self.conv1 = L.Convolution2D(3,
                                         16,
                                         ksize=3,
                                         stride=1,
                                         pad=1,
                                         nobias=True,
                                         **kwargs)
            self.bn1 = L.BatchNormalization(16)

            k = base_width
            self.stage2 = BuildingShakeBlocks(n_block, 16, k, 1, **kwargs)
            self.stage3 = BuildingShakeBlocks(n_block, k, 2 * k, 2, **kwargs)
            self.stage4 = BuildingShakeBlocks(n_block, 2 * k, 4 * k, 2,
                                              **kwargs)

            self.fc5 = L.Linear(4 * k, n_out)
コード例 #6
0
    def __init__(self, n_layers, n_out, n_kernel=4, grid=2, seed_value=0,
                 random=False, lattice=False, width=tuple(), layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if n_layers == 47:
            block = [5, 5, 5]
        elif n_layers == 74:
            block = [8, 8, 8]
        elif n_layers == 110:
            block = [12, 12, 12]
        elif n_layers == 164:
            block = [18, 18, 18]
        else:
            raise ValueError(
                'The n_layers argument should be mod({} - 2, 3) == 0,  \
                 but {} was given.'.format(n_layers, n_layers))

        with self.init_scope():
            self.conv1 = L.Convolution2D(3, 16, 3, 1, 1, **kwargs)
            self.res2 = PreBuildingBlock(block[0], 16, 16, 64, 1, **kwargs)
            self.expand3 = Expand(n_kernel, 32, grid=grid,
                                  seed_value=seed_value, random=random,
                                  lattice=lattice, width=width)
            self.res3 = PreBuildingBlock_shareBN(block[1], 64, 32, 128, 1, 2,
                                                 n_kernel, **kwargs)
            self.expand4 = Expand(n_kernel, 16, grid=grid,
                                  seed_value=seed_value, random=random,
                                  lattice=lattice, width=width)
            self.res4 = PreBuildingBlock_shareBN(block[2], 128, 64, 256, 1, 4,
                                                 n_kernel, **kwargs)
            self.bn4 = L.BatchNormalization(256)
            self.fc5 = L.Linear(256, n_out)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv1]),
            ('res2', [self.res2]),
            ('expand3', [self.expand3]),
            ('res3', [self.res3]),
            ('expand4', [self.expand4]),
            ('res4', [self.res4]),
            ('squeeze4', [lambda x: batch_squeeze(x, 4, n_kernel)]),
            ('pool4', [self.bn4, F.relu,
                       lambda x: F.split_axis(x, (n_kernel // 4) ** 2, axis=0),
                       lambda x: F.concat(x, axis=2),
                       R._global_average_pooling_2d]),
            ('fc5', [self.fc5]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str) and
                all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #7
0
ファイル: resnetfcn.py プロジェクト: jonaswgit/fcn
    def __init__(self, pretrained_model, n_layers, n_class, class_weight=None):
        super(ResNetLayersFCN32, self).__init__()
        self.n_class = n_class
        if class_weight is not None:
            assert class_weight.shape == (self.n_class,)
            self.class_weight = class_weight
        else:
            self.class_weight = None

        if pretrained_model:
            # As a sampling process is time-consuming,
            # we employ a zero initializer for faster computation.
            kwargs = {'initialW': constant.Zero()}

        else:
            # employ default initializers used in the original paper
            kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        kwargs2 = {
            'initialW': chainer.initializers.Zero(),
            'initial_bias': chainer.initializers.Zero(),
            }

        if n_layers == 50:
            block = [3, 4, 6, 3]
        elif n_layers == 101:
            block = [3, 4, 23, 3]
        elif n_layers == 152:
            block = [3, 8, 36, 3]
        else:
            raise ValueError('The n_layers argument should be either 50, 101,'
                             ' or 152, but {} was given.'.format(n_layers))

        with self.init_scope(): #in the comments are the sizes (of default images of 224x224) AFTER the cooresponding layer
            self.conv1 = Convolution2D(3, 64, 7, 2, 3, **kwargs)                #112x112
            self.bn1 = BatchNormalization(64)
            self.res2 = BuildingBlock(block[0], 64, 64, 256, 1, **kwargs)       #56x56
            self.res3 = BuildingBlock(block[1], 256, 128, 512, 2, **kwargs)     #28x28
            self.res4 = BuildingBlock(block[2], 512, 256, 1024, 2, **kwargs)    #14x14
            self.res5 = BuildingBlock(block[3], 1024, 512, 2048, 2, **kwargs)   #7x7
            #self.fc6 = Linear(2048, 1000)
            self.score_fr = L.Convolution2D(2048, n_class, 1, 1, 0, **kwargs2)
            self.upscore = L.Deconvolution2D(n_class, n_class, 64, 32, 0, nobias=True, initialW=initializers.UpsamplingDeconvWeight()) #224x224

        if pretrained_model and pretrained_model.endswith('.caffemodel'):  #default resnet model
            originalresnet = ResNetLayers(pretrained_model, n_layers)
            if n_layers == 50:
                _transfer_resnet50(originalresnet, self)
            elif n_layers == 101:
                _transfer_resnet101(originalresnet, self)
            elif n_layers == 152:
                _transfer_resnet152(originalresnet, self)
            else:
                raise ValueError('The n_layers argument should be either 50, 101,'
                                 ' or 152, but {} was given.'.format(n_layers))

        elif pretrained_model:
            npz.load_npz(pretrained_model, self)
コード例 #8
0
    def __init__(self,
                 n_layers,
                 n_out,
                 alpha=48,
                 stride=(1, 2, 2),
                 layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}
        in_ch = 16

        if (n_layers - 2) % 9 == 0:
            n = (n_layers - 2) // 9
        else:
            raise ValueError(
                'The n_layers argument should be mod({} - 2, 9) == 0,  \
                 but {} was given.'.format(n_layers, n_layers))

        add_ch = alpha / (3 * n)
        temp_ch = in_ch
        with self.init_scope():
            self.conv1 = L.Convolution2D(3,
                                         in_ch,
                                         3,
                                         1,
                                         1,
                                         nobias=True,
                                         **kwargs)
            self.bn1 = L.BatchNormalization(in_ch)

            for i in range(3):
                name = 'pyramid{}'.format(i + 1)
                pyramid = BuildingPyramidBlock(n, in_ch, temp_ch, add_ch,
                                               stride[i], **kwargs)
                setattr(self, name, pyramid)
                temp_ch += n * add_ch  # temp channel sum
                in_ch = 4 * round(temp_ch)  # next in_ch
            self.bn4 = L.BatchNormalization(in_ch)
            self.fc5 = L.Linear(in_ch, n_out)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv1]),
            ('pyramid2', [self.pyramid1]),
            ('pyramid3', [self.pyramid2]),
            ('pyramid4', [self.pyramid3]),
            ('pool4', [self.bn4, F.relu, lambda x: F.average(x, axis=(2, 3))]),
            ('fc5', [self.fc5]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str)
                and all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #9
0
    def __init__(self, n_out, grid1=(1,), grid2=(1,),
                 grid_shuffle=False, weights1=None, weights2=None,
                 layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if grid_shuffle:
            n_kernel1 = 4
            n_kernel2 = 4
            n_grid1 = 1
            n_grid2 = 1
        else:
            n_kernel1 = len(grid1) * 4
            n_kernel2 = len(grid2) * 4
            n_grid1 = len(grid1)
            n_grid2 = len(grid2)

        with self.init_scope():
            self.conv1_1 = Conv2DBNActiv(None, 96, 3, 1, 1, nobias=True, **kwargs)
            self.conv1_2 = Conv2DBNActiv(None, 96, 3, 1, 1, nobias=True, **kwargs)
            self.expand1 = Expand(n_kernel1, 32, grid=grid1,
                                  grid_shuffle=grid_shuffle, weights=weights1)
            self.conv1_3 = Conv2DBNActiv(None, 96, 3, 1, 1, nobias=True, **kwargs)
            self.conv2_1 = Conv2DBNActiv(None, 192, 3, 1, 1, nobias=True, **kwargs)
            self.conv2_2 = Conv2DBNActiv(None, 192, 3, 1, 1, nobias=True, **kwargs)
            self.expand2 = Expand(n_kernel2, 16, grid=grid2,
                                  grid_shuffle=grid_shuffle, weights=weights2)
            self.conv2_3 = Conv2DBNActiv(None, 192, 3, 1, 1, nobias=True, **kwargs)
            self.conv3_1 = Conv2DBNActiv(None, 192, 3, 1, 1, nobias=True, **kwargs)
            self.conv3_2 = Conv2DBNActiv(None, 192, 1, 1, 0, nobias=True, **kwargs)
            self.conv3_3 = Conv2DBNActiv(None, 192, 1, 1, 0, nobias=True, **kwargs)
            self.fc = L.Linear(None, n_out)

        self.functions = collections.OrderedDict([
            ('conv1', [lambda x: F.dropout(x, 0.2),
                       self.conv1_1, self.conv1_2, self.expand1, self.conv1_3, F.dropout]),
            ('conv2', [self.conv2_1, self.conv2_2, self.expand2, self.conv2_3, F.dropout]),
            ('conv3', [self.conv3_1, self.conv3_2, self.conv3_3]),
            ('squeeze3', [lambda x: batch_squeeze(x, 4, n_kernel2)]),
            ('pool3', [lambda x: F.split_axis(x, n_grid1 * n_grid2, axis=0),
                       lambda x: F.concat(x, axis=2),
                       R._global_average_pooling_2d]),
            ('fc', [self.fc]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str) and
                all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #10
0
ファイル: net.py プロジェクト: tanaka-daiki/EizoMediaReport
 def __init__(self):
     super().__init__()
     kwargs = {'initialW': normal.HeNormal(scale=1.0)}
     with self.init_scope():
         self.deconv1 = L.Deconvolution2D(512, 256, **kwargs, ksize=5)
         self.bn1 = L.BatchNormalization(256, eps=1e-5)
         self.deconv2 = L.Deconvolution2D(256, 128, **kwargs, ksize=5)
         self.bn2 = L.BatchNormalization(128, eps=1e-5)
         self.deconv3 = L.Deconvolution2D(128, 64, **kwargs, ksize=5)
         self.bn3 = L.BatchNormalization(64, eps=1e-5)
         self.deconv4 = L.Deconvolution2D(64, 1, **kwargs, ksize=5)
コード例 #11
0
    def __init__(self,
                 n_layers,
                 n_out,
                 cardinality=8,
                 base_width=64,
                 widen_factor=4,
                 layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if (n_layers - 2) % 9 == 0:
            block = [(n_layers - 2) // 9] * 3
        else:
            raise ValueError(
                'The n_layers argument should be mod({} - 2, 9) == 0,  \
                 but {} was given.'.format(n_layers, n_layers))

        with self.init_scope():
            self.conv1 = L.Convolution2D(None, 64, 3, 1, 1, **kwargs)
            self.bn1 = L.BatchNormalization(64)
            self.res2 = BuildingBlock(block[0], 64, 64 * widen_factor,
                                      cardinality, base_width, widen_factor, 1,
                                      **kwargs)
            self.res3 = BuildingBlock(block[1], 64 * widen_factor,
                                      128 * widen_factor, cardinality,
                                      base_width, widen_factor, 1, **kwargs)
            self.res4 = BuildingBlock(block[2], 128 * widen_factor,
                                      256 * widen_factor, cardinality,
                                      base_width, widen_factor, 1, **kwargs)
            self.fc5 = L.Linear(256 * widen_factor, n_out)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv1, self.bn1, F.relu]),
            ('res2', [self.res2]),
            ('expand3', [lambda x: pgp(x, 2)]),
            ('res3', [self.res3]),
            ('expand4', [lambda x: pgp(x, 2)]),
            ('res4', [self.res4]),
            ('squeeze4', [lambda x: pgp_inv(x, 4)]),
            ('pool4', [lambda x: F.average(x, axis=(2, 3))]),
            ('fc5', [self.fc5]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str)
                and all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #12
0
ファイル: net.py プロジェクト: tanaka-daiki/EizoMediaReport
 def __init__(self):
     super().__init__()
     kwargs = {'initialW': normal.HeNormal(scale=1.0)}
     with self.init_scope():
         self.conv1 = L.Convolution2D(1, 64, **kwargs, ksize=5)
         self.conv2 = L.Convolution2D(64, 128, **kwargs, ksize=5)
         self.bn2 = L.BatchNormalization(128, eps=1e-5)
         self.conv3 = L.Convolution2D(128, 256, **kwargs, ksize=5)
         self.bn3 = L.BatchNormalization(256, eps=1e-5)
         self.conv4 = L.Convolution2D(256, 512, **kwargs, ksize=5)
         self.bn4 = L.BatchNormalization(512, eps=1e-5)
         self.fc = L.Linear(512, 1)
コード例 #13
0
    def __init__(self, pretrained_model):
        initialW = normal.HeNormal(scale=1.0)
        super(Xception, self).__init__()
        with self.init_scope():
            self.conv1 = L.Convolution2D(3, 32, 3, 2, 0, nobias=True, initialW=initialW)
            self.bn1 = L.BatchNormalization(32)
            self.conv2 = L.Convolution2D(32, 64, 3, nobias=True, initialW=initialW)
            self.bn2 = L.BatchNormalization(64)

            self.block1 = BuildingBlock(
                64, 128, 2, 2, initialW, start_with_relu=False, grow_first=True)
            self.block2 = BuildingBlock(
                128, 256, 2, 2, initialW, start_with_relu=True, grow_first=True)
            self.block3 = BuildingBlock(
                256, 728, 2, 2, initialW, start_with_relu=True, grow_first=True)

            self.block4 = BuildingBlock(
                728, 728, 3, 1, initialW, start_with_relu=True, grow_first=True)
            self.block5 = BuildingBlock(
                728, 728, 3, 1, initialW, start_with_relu=True, grow_first=True)
            self.block6 = BuildingBlock(
                728, 728, 3, 1, initialW, start_with_relu=True, grow_first=True)
            self.block7 = BuildingBlock(
                728, 728, 3, 1, initialW, start_with_relu=True, grow_first=True)

            self.block8 = BuildingBlock(
                728, 728, 3, 1, initialW, start_with_relu=True, grow_first=True)
            self.block9 = BuildingBlock(
                728, 728, 3, 1, initialW, start_with_relu=True, grow_first=True)
            self.block10 = BuildingBlock(
                728, 728, 3, 1, initialW, start_with_relu=True, grow_first=True)
            self.block11 = BuildingBlock(
                728, 728, 3, 1, initialW, start_with_relu=True, grow_first=True)

            self.block12 = BuildingBlock(
                728, 1024, 2, 2, initialW, start_with_relu=True, grow_first=False)

            self.conv3 = SeparableConv2D(1024, 1536, 3, 1, 1, initialW=initialW)
            self.bn3 = L.BatchNormalization(1536)

            self.conv4 = SeparableConv2D(1536, 2048, 3, 1, 1, initialW=initialW)
            self.bn4 = L.BatchNormalization(2048)

            self.fc = L.Linear(2048, 1000)

        if pretrained_model and pretrained_model.endswith('.caffemodel'):
            _retrieve('xception.npz', pretrained_model, self)
        elif pretrained_model:
            chainer.serializers.load_npz(pretrained_model, self)
コード例 #14
0
    def __init__(self, n_class=36, pretrained_model=None, output_scale=1.0):
        if pretrained_model:
            # As a sampling process is time-consuming,
            # we employ a zero initializer for faster computation.
            kwargs = {'initialW': constant.Zero()}
        else:
            print "train resblock : True"
            # employ default initializers used in the original paper
            kwargs = {'initialW': normal.HeNormal(scale=1.0)}
        self.n_class = n_class
        self.output_scale = output_scale
        super(DualCenterProposalNetworkRes50FCN, self).__init__(
            # resnet50
            conv1=L.Convolution2D(3, 64, 7, 2, 3, **kwargs),
            bn1=L.BatchNormalization(64),
            res2=R.BuildingBlock(3, 64, 64, 256, 1, **kwargs),
            res3=R.BuildingBlock(4, 256, 128, 512, 2, **kwargs),
            res4=R.BuildingBlock(6, 512, 256, 1024, 2, **kwargs),
            res5=R.BuildingBlock(3, 1024, 512, 2048, 2, **kwargs),
            upscore32=L.Deconvolution2D(2048, 512, 8, stride=4, pad=2),
            bn_up32=L.BatchNormalization(512),
            upscore16=L.Deconvolution2D(1024, 512, 4, stride=2, pad=1),
            bn_up16=L.BatchNormalization(512),
            concat_conv=L.Convolution2D(512 * 3, 512 * 3, 3, stride=1, pad=1),
            bn_concat=L.BatchNormalization(512 * 3),
            score_pool=L.Convolution2D(512 * 3, n_class, 1, stride=1, pad=0),
            upscore_final=L.Deconvolution2D(self.n_class,
                                            self.n_class,
                                            16,
                                            stride=8,
                                            pad=4),
            conv_cp1=L.Convolution2D(512 * 3, 1024, 3, stride=1, pad=1),
            bn_cp1=L.BatchNormalization(1024),

            # center pose network
            conv_cp2=L.Convolution2D(1024, 512, 3, stride=1, pad=1),
            bn_cp2=L.BatchNormalization(512),
            upscore_cp1=L.Deconvolution2D(512, 16, 8, stride=4, pad=2),
            bn_cp3=L.BatchNormalization(16),
            upscore_cp2=L.Deconvolution2D(16, 3, 4, stride=2, pad=1),

            # origin center pose network
            conv_ocp2=L.Convolution2D(1024, 512, 3, stride=1, pad=1),
            bn_ocp2=L.BatchNormalization(512),
            upscore_ocp1=L.Deconvolution2D(512, 16, 8, stride=4, pad=2),
            bn_ocp3=L.BatchNormalization(16),
            upscore_ocp2=L.Deconvolution2D(16, 3, 4, stride=2, pad=1),
        )
コード例 #15
0
    def __init__(self, n_layers, n_out, layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if n_layers == 50:
            block = [3, 4, 6, 3]
        elif n_layers == 101:
            block = [3, 4, 23, 3]
        elif n_layers == 152:
            block = [3, 8, 36, 3]
        else:
            raise ValueError('The n_layers argument should be either 50, 101,'
                             ' or 152, but {} was given.'.format(n_layers))

        with self.init_scope():
            self.conv1 = L.Convolution2D(3, 64, 7, 2, 3, nobias=True, **kwargs)
            self.bn1 = L.BatchNormalization(64)
            self.res2 = BuildingBlock(block[0], 64, 64, 256, 1, **kwargs)
            self.res3 = BuildingBlock(block[1], 256, 128, 512, 2, **kwargs)
            self.res4 = BuildingBlock_alter(block[2], 512, 256, 1024, 2,
                                            **kwargs)
            self.res5 = BuildingBlock_alter(block[3], 1024, 512, 2048, 2,
                                            **kwargs)
            self.fc6 = L.Linear(2048, n_out)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv1, self.bn1, F.relu]),
            ('pool1', [lambda x: F.max_pooling_2d(x, ksize=3, stride=2)]),
            ('res2', [self.res2]),
            ('res3', [self.res3]),
            # ('expand4', [lambda x: _expand(x, 2)]),
            ('res4', [self.res4]),
            # ('expand5', [lambda x: _expand(x, 2)]),
            ('res5', [self.res5]),
            ('pool5', [R._global_average_pooling_2d]),
            ('fc6', [self.fc6]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str)
                and all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #16
0
ファイル: resnet-fcn_abondoned.py プロジェクト: jonaswgit/fcn
    def __init__(self, pretrained_model, n_layers, n_class=3):
        super(ResNetLayersFCN, self).__init__()

        self.n_class = n_class

        if pretrained_model:
            # As a sampling process is time-consuming,
            # we employ a zero initializer for faster computation.
            kwargs = {'initialW': constant.Zero()}
        else:
            # employ default initializers used in the original paper
            kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if n_layers == 50:
            block = [3, 4, 6, 3]
        elif n_layers == 101:
            block = [3, 4, 23, 3]
        elif n_layers == 152:
            block = [3, 8, 36, 3]
        else:
            raise ValueError('The n_layers argument should be either 50, 101,'
                             ' or 152, but {} was given.'.format(n_layers))

        with self.init_scope(): #in the comments are the sizes (of default images of 224x224) AFTER the cooresponding layer
            self.conv1 = Convolution2D(3, 64, 7, 2, 3, **kwargs)                #112x112
            self.bn1 = BatchNormalization(64)
            self.res2 = BuildingBlock(block[0], 64, 64, 256, 1, **kwargs)       #56x56
            self.res3 = BuildingBlock(block[1], 256, 128, 512, 2, **kwargs)     #28x28
            self.res4 = BuildingBlock(block[2], 512, 256, 1024, 2, **kwargs)    #14x14
            self.res5 = BuildingBlock(block[3], 1024, 512, 2048, 2, **kwargs)   #7x7
            #self.fc6 = Linear(2048, 1000)
            self.score_fr = L.Convolution2D(2048, n_class, 1, 1, 0, **kwargs)
            self.upscore = L.Deconvolution2D(
                n_class, n_class, 64, 32, 0, nobias=True,
                initialW=initializers.UpsamplingDeconvWeight())                 #224x224

        #if pretrained_model and pretrained_model.endswith('.caffemodel'):
        #    _retrieve(n_layers, 'ResNet-{}-model.npz'.format(n_layers),
        #              pretrained_model, self)
        if pretrained_model and pretrained_model is in ['ResNet-101-model.caffemodel']: #later maybe and 50 and 152 here
        #  open default resnet and extract weigths form it
        #              pretrained_model, self)
           resnet101 = ResNetLayers(pretrained_model, 101)
           init_from_resnet101(resnet101)

        elif pretrained_model:
            npz.load_npz(pretrained_model, self)
コード例 #17
0
ファイル: resnet.py プロジェクト: KosukeMizufune/Grad-CAM
    def __init__(self, pretrained_model, n_layers):
        super(ResNet, self).__init__()
        if pretrained_model:
            # As a sampling process is time-consuming,
            # we employ a zero initializer for faster computation.
            kwargs = {'initialW': constant.Zero()}
        else:
            # employ default initializers used in the original paper
            kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if n_layers == 50:
            block = [3, 4, 6, 3]
        elif n_layers == 101:
            block = [3, 4, 23, 3]
        elif n_layers == 152:
            block = [3, 8, 36, 3]
        else:
            raise ValueError('The n_layers argument should be either 50, 101,'
                             ' or 152, but {} was given.'.format(n_layers))

        with self.init_scope():
            self.conv1 = L.Convolution2D(3, 64, 7, 2, 3, **kwargs)
            self.bn1 = L.BatchNormalization(64)
            self.res2 = BuildingBlock(block[0], 64, 64, 256, 1, **kwargs)
            self.res3 = BuildingBlock(block[1], 256, 128, 512, 2, **kwargs)
            self.res4 = BuildingBlock(block[2], 512, 256, 1024, 2, **kwargs)
            self.res5 = BuildingBlock(block[3], 1024, 512, 2048, 2, **kwargs)
            self.fc6 = L.Linear(2048, 1000)

        if pretrained_model and pretrained_model.endswith('.caffemodel'):
            _retrieve(n_layers, 'ResNet-{}-model.npz'.format(n_layers),
                      pretrained_model, self)
        elif pretrained_model:
            npz.load_npz(pretrained_model, self)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv1, self.bn1, F.relu]),
            ('pool1', [lambda x: F.max_pooling_2d(x, ksize=3, stride=2)]),
            ('res2', [self.res2]),
            ('res3', [self.res3]),
            ('res4', [self.res4]),
            ('res5', [self.res5]),
            ('pool5', [_global_average_pooling_2d]),
            ('fc6', [self.fc6]),
            ('prob', [F.softmax]),
        ])
コード例 #18
0
    def __init__(self, n_layers, n_out, k=1, layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if (n_layers - 4) % 6 == 0:
            block = [(n_layers - 4) // 6] * 3
        else:
            raise ValueError(
                'The n_layers argument should be mod({} - 4, 6) == 0,  \
                 but {} was given.'.format(n_layers, n_layers))

        with self.init_scope():
            self.conv1 = L.Convolution2D(3, 16, 3, 1, 1, **kwargs)
            self.res2 = PreBuildingBasicBlock(block[0], 16, 16 * k, 1,
                                              **kwargs)
            self.res3 = PreBuildingBasicBlock(block[1], 16 * k, 32 * k, 1,
                                              **kwargs)
            self.res4 = PreBuildingBasicBlock(block[2], 32 * k, 64 * k, 1,
                                              **kwargs)
            self.bn4 = L.BatchNormalization(64 * k)
            self.fc5 = L.Linear(64 * k, n_out)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv1]),
            ('res2', [self.res2]),
            ('expand3', [lambda x: pgp(x, 2)]),
            ('res3', [self.res3]),
            ('expand4', [lambda x: pgp(x, 2)]),
            ('res4', [self.res4]),
            ('squeeze4', [lambda x: pgp_inv(x, 4)]),
            ('pool4', [self.bn4, F.relu, R._global_average_pooling_2d]),
            ('fc5', [self.fc5]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str)
                and all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #19
0
ファイル: net.py プロジェクト: wang3702/JointOptimization-1
    def __init__(self, layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        block = [5, 5, 5]
        filters = [32, 32, 64, 128]

        with self.init_scope():
            self.conv1 = L.Convolution2D(None,
                                         filters[0],
                                         3,
                                         1,
                                         1,
                                         **kwargs,
                                         nobias=True)
            self.res2 = BuildingBlock(block[0], filters[0], filters[1], 1,
                                      **kwargs)
            self.res3 = BuildingBlock(block[1], filters[1], filters[2], 2,
                                      **kwargs)
            self.res4 = BuildingBlock(block[2], filters[2], filters[3], 2,
                                      **kwargs)
            self.bn4 = L.BatchNormalization(filters[3])
            self.fc5 = L.Linear(filters[3], 10)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv1]),
            ('res2', [self.res2]),
            ('res3', [self.res3]),
            ('res4', [self.res4, self.bn4, F.relu]),
            ('pool4', [R._global_average_pooling_2d]),
            ('fc5', [self.fc5]),
        ])
        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str)
                and all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #20
0
    def __init__(self, n_layers, n_out, k=32, layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if (n_layers - 2) % 6 == 0:
            block = [(n_layers - 2) // 6] * 3
        else:
            raise ValueError(
                'The n_layers argument should be mod({} - 4, 6) == 0,  \
                 but {} was given.'.format(n_layers, n_layers))

        with self.init_scope():
            self.conv1 = L.Convolution2D(3, 16, 3, 1, 1, nobias=True, **kwargs)
            self.bn1 = L.BatchNormalization(16)
            self.res2 = BuildingShakeBlock(
                block[0], 16, k, 1, **kwargs)
            self.res3 = BuildingShakeBlock(
                block[1], k, 2 * k, 2, **kwargs)
            self.res4 = BuildingShakeBlock(
                block[2], 2 * k, 4 * k, 2, **kwargs)
            self.fc5 = L.Linear(4 * k, n_out)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv1, self.bn1]),
            ('res2', [self.res2]),
            ('res3', [self.res3]),
            ('res4', [self.res4]),
            ('pool4', [F.relu, R._global_average_pooling_2d]),
            ('fc5', [self.fc5]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str) and
                all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #21
0
    def __init__(self, n_layers, n_out, stride=(1, 2, 2), layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}
        if (n_layers - 2) % 3 == 0:
            block = [(n_layers - 2) // 9] * 3
        else:
            raise ValueError(
                'The n_layers argument should be mod({} - 2, 3) == 0,  \
                 but {} was given.'.format(n_layers, n_layers))

        with self.init_scope():
            self.conv1 = L.Convolution2D(3, 16, 3, 1, 1, **kwargs)
            self.res2 = PreBuildingBlock(block[0], 16, 16, 64, stride[0],
                                         **kwargs)
            self.res3 = PreBuildingBlock(block[1], 64, 32, 128, stride[1],
                                         **kwargs)
            self.res4 = PreBuildingBlock(block[2], 128, 64, 256, stride[2],
                                         **kwargs)
            self.bn4 = L.BatchNormalization(256)
            self.fc5 = L.Linear(None, n_out)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv1]),
            ('res2', [self.res2]),
            ('res3', [self.res3]),
            ('res4', [self.res4]),
            ('pool4', [self.bn4, F.relu,
                       lambda x: F.average_pooling_2d(x, 8, stride=1)]),
            ('fc5', [self.fc5]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str) and
                all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #22
0
    def __init__(self, n_out, layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        with self.init_scope():
            self.conv1_1 = Conv2DBNActiv(None, 96, 3, 1, 1, nobias=True, **kwargs)
            self.conv1_2 = Conv2DBNActiv(None, 96, 3, 1, 1, nobias=True, **kwargs)
            self.conv1_3 = Conv2DBNActiv(None, 96, 3, 1, 1, nobias=True, **kwargs)
            self.conv2_1 = Conv2DBNActiv(None, 192, 3, 1, 1, nobias=True, **kwargs)
            self.conv2_2 = Conv2DBNActiv(None, 192, 3, 1, 1, nobias=True, **kwargs)
            self.conv2_3 = Conv2DBNActiv(None, 192, 3, 1, 1, nobias=True, **kwargs)
            self.conv3_1 = Conv2DBNActiv(None, 192, 3, 1, 1, nobias=True, **kwargs)
            self.conv3_2 = Conv2DBNActiv(None, 192, 1, 1, 0, nobias=True, **kwargs)
            self.conv3_3 = Conv2DBNActiv(None, 192, 1, 1, 0, nobias=True, **kwargs)
            self.fc = L.Linear(None, n_out)

        self.functions = collections.OrderedDict([
            ('conv1', [lambda x: F.dropout(x, 0.2),
                       self.conv1_1, self.conv1_2, lambda x: pgp(x, 2),
                       self.conv1_3, F.dropout]),
            ('conv2', [self.conv2_1, self.conv2_2, lambda x: pgp(x, 2),
                       self.conv2_3, F.dropout]),
            ('conv3', [self.conv3_1, self.conv3_2, self.conv3_3]),
            ('squeeze3', [lambda x: pgp_inv(x, 4)]),
            ('pool3', [lambda x: F.average(x, axis=(2, 3))]),
            ('fc', [self.fc]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str) and
                all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #23
0
 def __init__(self, n_class=36, pretrained_model=None):
     if pretrained_model:
         # As a sampling process is time-consuming,
         # we employ a zero initializer for faster computation.
         kwargs = {'initialW': constant.Zero()}
     else:
         # employ default initializers used in the original paper
         kwargs = {'initialW': normal.HeNormal(scale=1.0)}
     self.n_class = n_class
     super(DepthInvariantNetworkRes50FCN, self).__init__(
         # resnet50
         conv1=L.Convolution2D(3, 64, 7, 2, 3, **kwargs),
         bn1=L.BatchNormalization(64),
         res2=R.BuildingBlock(3, 64, 64, 256, 1,
                              **kwargs),  # resblock 1/2 -> 1/4
         res3=R.BuildingBlock(4, 256, 128, 512, 2,
                              **kwargs),  # resblock 1/4 ->1/8
         res4=R.BuildingBlock(6, 512, 256, 1024, 2,
                              **kwargs),  # resblock 1/8 -> 1/16
         res5=R.BuildingBlock(3, 1024, 512, 2048, 2,
                              **kwargs),  # resblock 1/16 -> 1/32
         upscore1=L.Deconvolution2D(2048, 512, 16, stride=8, pad=4),
         upscore2=L.Deconvolution2D(1024, 512, 8, stride=4, pad=2),
         upscore3=L.Deconvolution2D(512, 512, 4, stride=2, pad=1),
         bn_upscore=L.BatchNormalization(512 * 3),
         concat_conv=L.Convolution2D(512 * 3, 1024, 3, stride=1, pad=1),
         pool_roi_conv=L.Convolution2D(1024, 1024, 5, stride=5, pad=0),
         conv_after_croip=L.Convolution2D(1024, 512, 3, stride=1, pad=1),
         bn_croip1=L.BatchNormalization(1024),
         bn_croip2=L.BatchNormalization(512),
         score_pool=L.Convolution2D(512, n_class, 1, stride=1, pad=0),
         upscore_final=L.Deconvolution2D(self.n_class,
                                         self.n_class,
                                         8,
                                         stride=4,
                                         pad=2),
     )
コード例 #24
0
    def __init__(self, pretrained_model="auto"):
        if pretrained_model:
            kwargs = {'initialW': constant.Zero()}
        else:
            kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        super(ResNet, self).__init__(
            conv1=L.Convolution2D(3, 64, 7, 2, 3, **kwargs),
            bn1=L.BatchNormalization(64),
            res2=Block(3, 64, 64, 256, 1, **kwargs),
            res3=Block(4, 256, 128, 512, 2, **kwargs),
            res4=Block(6, 512, 256, 1024, 2, **kwargs),
            res5=Block(3, 1024, 512, 2048, 2, **kwargs),
            fc6=L.Linear(None, 1000),
        )
        if pretrained_model == 'auto':
            print("[ PREPROCESS ] Use caffe model of ResNet.")
            _retrieve('ResNet-50-model.npz', 'ResNet-50-model.caffemodel',
                      self)
            self.fc6 = L.Linear(None, 25)
        elif pretrained_model:
            npz.load_npz(pretrained_model, self)

        self.train = True
コード例 #25
0
    def __init__(self,
                 n_layer=12,
                 growth_rate=12,
                 n_class=10,
                 in_ch=16,
                 block=3,
                 bottleneck=False,
                 reduction=1.0,
                 stride=[2, 2],
                 layer_names=None):
        """DenseNet definition.
        Args:
            n_layer: Number of convolution layers in one dense block.
                If n_layer=12, the network is made out of 40 (12*3+4) layers.
                If n_layer=32, the network is made out of 100 (32*3+4) layers.
            growth_rate: Number of output feature maps of each convolution
                layer in dense blocks, which is difined as k in the paper.
            n_class: Output class.
            dropout_ratio: Dropout ratio.
            in_ch: Number of output feature maps of first convolution layer.
            block: Number of dense block.
        """
        super().__init__()

        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        with self.init_scope():
            self.conv = L.Convolution2D(3,
                                        in_ch,
                                        3,
                                        1,
                                        1,
                                        nobias=True,
                                        **kwargs)
            # self.forward = list()
            for i in range(block):
                name = 'dense{}'.format(i + 1)
                dense = BuildingDenseBlock(in_ch,
                                           growth_rate,
                                           n_layer,
                                           bottleneck=bottleneck,
                                           **kwargs)
                setattr(self, name, dense)
                # self.forward.append(dense)
                in_ch = in_ch + n_layer * growth_rate
                if not i == block - 1:
                    name = 'trans{}'.format(i + 1)
                    trans = Transition(in_ch,
                                       reduction=reduction,
                                       stride=stride[i],
                                       **kwargs)
                    setattr(self, name, trans)
                    # self.forward.append(trans)
                    in_ch = math.floor(in_ch * reduction)
            self.bn = L.BatchNormalization(in_ch)
            self.fc = L.Linear(in_ch, n_class)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv]),
            ('block2', [self.dense1]),
            ('trans2', [self.trans1]),
            ('block3', [self.dense2]),
            ('trans3', [self.trans2]),
            ('block4', [self.dense3]),
            ('pool4', [self.bn, F.relu, lambda x: F.average(x, axis=(2, 3))]),
            ('fc5', [self.fc]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str)
                and all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names
コード例 #26
0
    def __init__(self, n_class=36, pretrained_model=None):
        if pretrained_model:
            # As a sampling process is time-consuming,
            # we employ a zero initializer for faster computation.
            kwargs = {'initialW': constant.Zero()}
        else:
            # employ default initializers used in the original paper
            kwargs = {'initialW': normal.HeNormal(scale=1.0)}
        self.n_class = n_class
        super(CenterProposalNetworkRes50FCN, self).__init__(
            # resnet50
            conv1=L.Convolution2D(3, 64, 7, 2, 3, **kwargs),
            bn1=L.BatchNormalization(64),
            res2=R.BuildingBlock(3, 64, 64, 256, 1, **kwargs),
            res3=R.BuildingBlock(4, 256, 128, 512, 2, **kwargs),
            res4=R.BuildingBlock(6, 512, 256, 1024, 2, **kwargs),
            res5=R.BuildingBlock(3, 1024, 512, 2048, 2, **kwargs),
            upscore32=L.Deconvolution2D(2048,
                                        512,
                                        8,
                                        stride=4,
                                        pad=2,
                                        use_cudnn=False),
            upscore16=L.Deconvolution2D(1024,
                                        512,
                                        4,
                                        stride=2,
                                        pad=1,
                                        use_cudnn=False),
            concat_conv=L.Convolution2D(512 * 3, 512 * 3, 3, stride=1, pad=1),
            score_pool=L.Convolution2D(512 * 3, n_class, 1, stride=1, pad=0),
            cls_pool=L.Convolution2D(512 * 3, 128, 1, stride=1, pad=0),
            upscore_final=L.Deconvolution2D(self.n_class,
                                            self.n_class,
                                            16,
                                            stride=8,
                                            pad=4,
                                            use_cudnn=False),

            # depth network
            conv_d1_1=L.Convolution2D(1, 64, 3, stride=1, pad=1),
            bn_d1_1=L.BatchNormalization(64),
            conv_d1_2=L.Convolution2D(64, 64, 3, stride=1, pad=1),
            bn_d1_2=L.BatchNormalization(64),
            conv_d2=L.Convolution2D(64, 128, 3, stride=1, pad=1),
            bn_d2=L.BatchNormalization(128),
            conv_d3=L.Convolution2D(128, 256, 3, stride=1, pad=1),
            bn_d3=L.BatchNormalization(256),

            # center pose network
            conv_cp_1=L.Convolution2D(256 + 512 + 128,
                                      1024,
                                      3,
                                      stride=1,
                                      pad=1),
            bn_cp_1=L.BatchNormalization(1024),
            conv_cp_2=L.Convolution2D(1024, 1024, 3, stride=1, pad=1),
            bn_cp_2=L.BatchNormalization(1024),
            upscore_cp1=L.Deconvolution2D(1024,
                                          512,
                                          8,
                                          stride=4,
                                          pad=2,
                                          use_cudnn=False),
            bn_cp_3=L.BatchNormalization(512),
            upscore_cp2=L.Deconvolution2D(512,
                                          3,
                                          4,
                                          stride=2,
                                          pad=1,
                                          use_cudnn=False),

            # rotation network
            conv_rot_1=L.Convolution2D(256 + 512 + 128,
                                       1024,
                                       3,
                                       stride=1,
                                       pad=1),
            bn_rot_1=L.BatchNormalization(1024),
            conv_rot_2=L.Convolution2D(1024, 1024, 3, stride=1, pad=1),
            bn_rot_2=L.BatchNormalization(1024),
            upscore_rot1=L.Deconvolution2D(1024,
                                           512,
                                           8,
                                           stride=4,
                                           pad=2,
                                           use_cudnn=False),
            bn_rot_3=L.BatchNormalization(512),
            upscore_rot2=L.Deconvolution2D(512,
                                           5,
                                           4,
                                           stride=2,
                                           pad=1,
                                           use_cudnn=False),
        )
コード例 #27
0
    def __init__(self, n_layers, n_out, net_scale=1.0, splits_left=2,
                 layer_names=None):
        super().__init__()
        kwargs = {'initialW': normal.HeNormal(scale=1.0)}

        if n_layers == 50:
            block = [3, 4, 6, 3]
        elif n_layers == 164:
            block = [10, 10, 23, 10]
        else:
            raise ValueError('The n_layers argument should be either 50,'
                             ' or 164, but {} was given.'.format(n_layers))

        if net_scale == 0.5:
            out_channels = [24, 48, 96, 192, 1024]
        elif net_scale == 1.0:
            out_channels = [24, 116, 232, 464, 1024]
        elif net_scale == 1.5:
            out_channels = [24, 176, 352, 704, 1024]
        elif net_scale == 2.0:
            out_channels = [24, 244, 488, 976, 2048]
        else:
            raise ValueError('net_scale augment should be either 0.5, 1.0,'
                             ' 1.5, or 2.0, but {} was given.'.format(
                                 net_scale))

        with self.init_scope():
            self.conv1 = L.Convolution2D(3, out_channels[0], 3, 2, 1,
                                         nobias=True, **kwargs)
            self.bn1 = L.BatchNormalization(out_channels[0])
            self.res2 = BuildingShuffleNetV2Block(block[0], out_channels[0],
                                                  out_channels[1], 2,
                                                  splits_left, **kwargs)
            self.res3 = BuildingShuffleNetV2Block(block[1], out_channels[1],
                                                  out_channels[2], 2,
                                                  splits_left, **kwargs)
            self.res4 = BuildingShuffleNetV2Block(block[2], out_channels[2],
                                                  out_channels[3], 2,
                                                  splits_left, **kwargs)
            self.conv5 = L.Convolution2D(out_channels[3], out_channels[4],
                                         1, 1, 0, nobias=True, **kwargs)
            self.bn5 = L.BatchNormalization(out_channels[4])
            self.fc6 = L.Linear(out_channels[4], n_out)

        self.functions = collections.OrderedDict([
            ('conv1', [self.conv1, self.bn1, F.relu]),
            ('pool1', [lambda x: F.max_pooling_2d(x, ksize=3, stride=2)]),
            ('res2', [self.res2]),
            ('res3', [self.res3]),
            ('res4', [self.res4]),
            ('conv5', [self.conv5, self.bn5, F.relu]),
            ('pool5', [lambda x: F.average(x, axis=(2, 3))]),
            ('fc6', [self.fc6]),
        ])

        if layer_names is None:
            layer_names = list(self.functions.keys())[-1]
        if (not isinstance(layer_names, str) and
                all([isinstance(name, str) for name in layer_names])):
            return_tuple = True
        else:
            return_tuple = False
            layer_names = [layer_names]
        self._return_tuple = return_tuple
        self._layer_names = layer_names