Пример #1
0
 def __init__(self, in_channels, out_channels, stride=2, initialW=None):
     super(BlockA, self).__init__()
     with self.init_scope():
         self.bn1 = BatchNormalization(in_channels)
         self.conv1 = Convolution2D(in_channels,
                                    out_channels,
                                    3,
                                    stride,
                                    1,
                                    initialW=initialW,
                                    nobias=True)
         self.bn2 = BatchNormalization(out_channels)
         self.conv2 = Convolution2D(out_channels,
                                    out_channels,
                                    3,
                                    1,
                                    1,
                                    initialW=initialW,
                                    nobias=True)
         self.conv3 = Convolution2D(in_channels,
                                    out_channels,
                                    1,
                                    stride,
                                    0,
                                    initialW=initialW,
                                    nobias=True)
Пример #2
0
 def __init__(self, in_channels, mid_channels, initialW=None):
     super(BottleneckB, self).__init__(
         conv1=Convolution2D(in_channels,
                             mid_channels,
                             1,
                             1,
                             0,
                             initialW=initialW,
                             nobias=True),
         bn1=BatchNormalization(mid_channels),
         conv2=Convolution2D(mid_channels,
                             mid_channels,
                             3,
                             1,
                             1,
                             initialW=initialW,
                             nobias=True),
         bn2=BatchNormalization(mid_channels),
         conv3=Convolution2D(mid_channels,
                             in_channels,
                             1,
                             1,
                             0,
                             initialW=initialW,
                             nobias=True),
         bn3=BatchNormalization(in_channels),
     )
Пример #3
0
 def __init__(self, in_channels, mid_channels, dilate=1, initialW=None):
     super(BottleneckB, self).__init__()
     with self.init_scope():
         self.conv1 = Convolution2D(
             in_channels, mid_channels, 1, 1, 0, initialW=initialW,
             nobias=True)
         self.bn1 = BatchNormalization(mid_channels)
         self.conv2 = Convolution2D(
             mid_channels, mid_channels, 3, 1, dilate, initialW=initialW,
             nobias=True, dilate=dilate)
         self.bn2 = BatchNormalization(mid_channels)
         self.conv3 = Convolution2D(
             mid_channels, in_channels, 1, 1, 0, initialW=initialW,
             nobias=True)
         self.bn3 = BatchNormalization(in_channels)
Пример #4
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]),
     ])
 def __init__(self, pretrained_model='auto', n_layers=50):
     super(ExtractorResNet, self).__init__()
     print('Extractor ResNet', n_layers, ' initialization')
     kwargs = {'initialW': constant.Zero()}
     if pretrained_model == 'auto':
         if n_layers == 50:
             pretrained_model = 'ResNet-50-model.caffemodel'
             block = [3, 4, 6, 3]
         elif n_layers == 101:
             pretrained_model = 'ResNet-101-model.caffemodel'
             block = [3, 4, 23, 3]
     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, 1, **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)
     del self.fc6
Пример #6
0
 def __init__(self, in_ch, mid_ch, out_ch):
     super(FireBlock, self).__init__()
     with self.init_scope():
         self.squeeze_11 = Convolution2D(in_ch,  mid_ch, 1)
         self.expand_11  = Convolution2D(mid_ch, out_ch, 1)
         self.expand_33  = Convolution2D(mid_ch, out_ch, 3, pad=1)
         self.bn         = BatchNormalization(out_ch * 2)
Пример #7
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)
Пример #8
0
    def __init__(self):
        super().__init__()

        with self.init_scope():
            self.first = Convolution2D(3,
                                       64,
                                       ksize=3,
                                       stride=1,
                                       pad=1,
                                       initialW=Normal(0.02))
            self.res1 = SRGeneratorResBlock()
            self.res2 = SRGeneratorResBlock()
            self.res3 = SRGeneratorResBlock()
            self.res4 = SRGeneratorResBlock()
            self.res5 = SRGeneratorResBlock()
            self.conv_mid = Convolution2D(64,
                                          64,
                                          ksize=3,
                                          stride=1,
                                          pad=1,
                                          initialW=Normal(0.02))
            self.bn_mid = BatchNormalization(64)
            self.upscale1 = SRGeneratorUpScaleBlock()
            self.upscale2 = SRGeneratorUpScaleBlock()
            self.conv_output = Convolution2D(64,
                                             3,
                                             ksize=3,
                                             stride=1,
                                             pad=1,
                                             initialW=Normal(0.02))
Пример #9
0
    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)
Пример #10
0
    def __init__(self,
                 in_channels,
                 mid_channels,
                 out_channels,
                 stride=2,
                 initialW=None,
                 downsample_fb=False):
        super(BottleneckA, self).__init__()
        # In the original MSRA ResNet, stride=2 is on 1x1 convolution.
        # In Facebook ResNet, stride=2 is on 3x3 convolution.

        stride_1x1, stride_3x3 = (stride, 1) if downsample_fb else (1, stride)
        with self.init_scope():
            self.conv1 = Convolution2D(in_channels,
                                       mid_channels,
                                       1,
                                       stride_1x1,
                                       0,
                                       initialW=initialW,
                                       nobias=True)
            self.bn1 = BatchNormalization(mid_channels)
            self.conv2 = Convolution2D(mid_channels,
                                       mid_channels,
                                       3,
                                       stride_3x3,
                                       1,
                                       initialW=initialW,
                                       nobias=True)
            self.bn2 = BatchNormalization(mid_channels)
            self.conv3 = Convolution2D(mid_channels,
                                       out_channels,
                                       1,
                                       1,
                                       0,
                                       initialW=initialW,
                                       nobias=True)
            self.bn3 = BatchNormalization(out_channels)
            self.conv4 = Convolution2D(in_channels,
                                       out_channels,
                                       1,
                                       stride,
                                       0,
                                       initialW=initialW,
                                       nobias=True)
            self.bn4 = BatchNormalization(out_channels)
Пример #11
0
    def __init__(self):
        super().__init__()

        with self.init_scope():
            self.c1 = Convolution2D(64,
                                    64,
                                    ksize=3,
                                    stride=1,
                                    pad=1,
                                    initialW=Normal(0.02))
            self.bn1 = BatchNormalization(64)
            self.c2 = Convolution2D(64,
                                    64,
                                    ksize=3,
                                    stride=1,
                                    pad=1,
                                    initialW=Normal(0.02))
            self.bn2 = BatchNormalization(64)
Пример #12
0
 def __init__(self,
              in_channels,
              mid_channels,
              out_channels,
              stride=2,
              initialW=None):
     super(BottleneckA, self).__init__()
     with self.init_scope():
         self.in_channels = in_channels
         self.mid_channels = mid_channels
         self.out_channels = out_channels
         self.conv1 = Convolution2D(in_channels,
                                    mid_channels,
                                    1,
                                    stride,
                                    0,
                                    initialW=initialW,
                                    nobias=True)
         self.bn1 = BatchNormalization(mid_channels)
         self.conv2 = Convolution2D(mid_channels,
                                    mid_channels,
                                    3,
                                    1,
                                    1,
                                    initialW=initialW,
                                    nobias=True)
         self.bn2 = BatchNormalization(mid_channels)
         self.conv3 = Convolution2D(mid_channels,
                                    out_channels,
                                    1,
                                    1,
                                    0,
                                    initialW=initialW,
                                    nobias=True)
         self.bn3 = BatchNormalization(out_channels)
         self.conv4 = Convolution2D(in_channels,
                                    out_channels,
                                    1,
                                    stride,
                                    0,
                                    initialW=initialW,
                                    nobias=True)
         self.bn4 = BatchNormalization(out_channels)
Пример #13
0
    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)
Пример #14
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)
        self.functions = collections.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]),
        ])
Пример #15
0
    def __init__(self):
        super().__init__()

        with self.init_scope():
            self.conv_input = Convolution2D(3,
                                            64,
                                            ksize=3,
                                            stride=1,
                                            pad=0,
                                            initialW=Normal(0.02))
            self.c1 = Convolution2D(64,
                                    64,
                                    ksize=3,
                                    stride=2,
                                    pad=0,
                                    initialW=Normal(0.02))
            self.bn1 = BatchNormalization(64)
            self.c2 = Convolution2D(64,
                                    128,
                                    ksize=3,
                                    stride=1,
                                    pad=0,
                                    initialW=Normal(0.02))
            self.bn2 = BatchNormalization(128)
            self.c3 = Convolution2D(128,
                                    128,
                                    ksize=3,
                                    stride=2,
                                    pad=0,
                                    initialW=Normal(0.02))
            self.bn3 = BatchNormalization(128)
            self.c4 = Convolution2D(128,
                                    256,
                                    ksize=3,
                                    stride=1,
                                    pad=0,
                                    initialW=Normal(0.02))
            self.bn4 = BatchNormalization(256)
            self.c5 = Convolution2D(256,
                                    256,
                                    ksize=3,
                                    stride=2,
                                    pad=0,
                                    initialW=Normal(0.02))
            self.bn5 = BatchNormalization(256)
            self.c6 = Convolution2D(256,
                                    512,
                                    ksize=3,
                                    stride=1,
                                    pad=0,
                                    initialW=Normal(0.02))
            self.bn6 = BatchNormalization(512)
            self.c7 = Convolution2D(512,
                                    512,
                                    ksize=3,
                                    stride=2,
                                    pad=0,
                                    initialW=Normal(0.02))
            self.bn7 = BatchNormalization(512)
            self.linear1 = Linear(in_size=4608, out_size=1024)
            self.linear2 = Linear(in_size=None, out_size=2)