Ejemplo n.º 1
0
 def __init__(self, in_ch, ch_multi, sample):
     super().__init__()
     with self.init_scope():
         if sample == 'down':
             self.dc = L.DepthwiseConvolution2D(in_ch,
                                                ch_multi,
                                                ksize=4,
                                                stride=2,
                                                pad=1)
             self.ac = F.leaky_relu
         elif sample == 'up':
             self.dc = L.Deconvolution2D(in_ch,
                                         in_ch * ch_multi,
                                         ksize=4,
                                         stride=2,
                                         pad=1)
             self.ac = F.relu
         else:
             self.dc = L.DepthwiseConvolution2D(in_ch,
                                                ch_multi,
                                                ksize=3,
                                                stride=1,
                                                pad=1)
             self.ac = None
         self.bn = L.BatchNormalization(in_ch * ch_multi)
Ejemplo n.º 2
0
 def __init__(self,
              in_channels,
              out_channels,
              ksize=None,
              xception=False,
              down_sample=False):
     super(ShuffleUnit, self).__init__()
     self.xception = xception
     self.down_sample = down_sample
     mid_channels = out_channels // 2
     with self.init_scope():
         if down_sample:
             self.downsize_depthwise = L.DepthwiseConvolution2D(
                 in_channels,
                 channel_multiplier=1,
                 ksize=ksize,
                 stride=2,
                 pad=ksize // 2)
             self.downsize_depthwise_bn = L.BatchNormalization(
                 size=in_channels)
             self.downsize_pointwise = Conv2DBNActiv(mid_channels, 1)
         if xception:
             self.separable_conv1 = SeparableConv2DBNActiv(
                 in_channels=(in_channels if down_sample else mid_channels),
                 out_channels=mid_channels,
                 ksize=ksize,
                 pad=ksize // 2,
                 stride=(2 if self.down_sample else 1))
             self.separable_conv2 = SeparableConv2DBNActiv(
                 in_channels=mid_channels,
                 out_channels=mid_channels,
                 ksize=ksize,
                 pad=ksize // 2)
             self.separable_conv3 = SeparableConv2DBNActiv(
                 in_channels=mid_channels,
                 out_channels=mid_channels,
                 ksize=ksize,
                 pad=ksize // 2)
         else:
             self.compress = Conv2DBNActiv(mid_channels, 1)
             self.depthwise = L.DepthwiseConvolution2D(
                 mid_channels,
                 channel_multiplier=1,
                 ksize=ksize,
                 pad=ksize // 2,
                 stride=(2 if self.down_sample else 1))
             self.depthwise_bn = L.BatchNormalization(size=mid_channels)
             self.expand = Conv2DBNActiv(mid_channels, 1)
 def __init__(self,
              in_channels,
              out_channels,
              ksize=3,
              stride=1,
              relu=True):
     super(SeparableConv, self).__init__()
     self.relu = relu
     self.ksize = ksize
     self.stride = stride
     with self.init_scope():
         self.depthwise_conv = L.DepthwiseConvolution2D(
             in_channels=in_channels,
             channel_multiplier=1,
             ksize=ksize,
             pad=ksize // 2,
             stride=stride,
             nobias=True)
         self.pointwise_conv = L.Convolution2D(in_channels,
                                               out_channels,
                                               ksize=1,
                                               nobias=True)
         self.pointwise_bn = L.BatchNormalization(out_channels,
                                                  eps=0.001,
                                                  use_gamma=False)
 def __init__(self, inp, oup, stride):
     super(ConvDW, self).__init__()
     with self.init_scope():
         self.conv_dw=L.DepthwiseConvolution2D(inp, 1, 3, stride=stride, pad=1, nobias=True)
         self.bn_dw=L.BatchNormalization(inp)
         self.conv_sep=L.Convolution2D(inp, oup, 1, stride=1, pad=0, nobias=True)
         self.bn_sep=L.BatchNormalization(oup)
Ejemplo n.º 5
0
 def __init__(self, in_channels, out_channels, num_groups):
     bottleneck_channels = int((out_channels) / 4)
     super().__init__()
     with self.init_scope():
         self.conv1 = L.Convolution2D(in_channels,
                                      bottleneck_channels,
                                      ksize=1,
                                      stride=1,
                                      pad=0,
                                      groups=num_groups)
         self.bn1 = L.BatchNormalization(bottleneck_channels)
         self.conv2 = L.DepthwiseConvolution2D(bottleneck_channels,
                                               1,
                                               ksize=3,
                                               stride=2,
                                               pad=1)
         self.bn2 = L.BatchNormalization(bottleneck_channels)
         self.conv3 = L.Convolution2D(bottleneck_channels,
                                      out_channels - in_channels,
                                      ksize=1,
                                      stride=1,
                                      pad=0,
                                      groups=num_groups)
         self.bn3 = L.BatchNormalization(out_channels - in_channels)
     self.num_groups = num_groups
    def __init__(self, expand_input, in_ch, out_ch, stride):
        super(ExpandedConv, self).__init__()
        if expand_input == 1:
            expander = expand_input_by_factor(expand_input, 1)
        else:
            expander = expand_input_by_factor(expand_input, 8)
        self.in_ch, self.out_ch = in_ch, out_ch
        expanded_ch = expander(in_ch)
        self.stride = stride
        with self.init_scope():
            self.expand_conv = L.Convolution2D(in_ch,
                                               expanded_ch,
                                               ksize=1,
                                               nobias=True)
            self.expand_bn = L.BatchNormalization(expanded_ch)

            self.depthwise_conv = L.DepthwiseConvolution2D(
                expanded_ch,
                channel_multiplier=1,
                ksize=3,
                stride=self.stride,
                pad=1,
                nobias=True)
            self.depthwise_bn = L.BatchNormalization(expanded_ch)

            self.project_conv = L.Convolution2D(expanded_ch,
                                                out_ch,
                                                ksize=1,
                                                nobias=True)
            self.project_bn = L.BatchNormalization(out_ch)
Ejemplo n.º 7
0
 def __init__(self, n_units=128):
     super(MobileNetsModule, self).__init__()
     with self.init_scope():
         self.dw = L.DepthwiseConvolution2D(None, 1, ksize=3, pad=1)
         self.bn1 = L.BatchNormalization(n_units)
         self.pw = L.Convolution2D(None, n_units, ksize=1)
         self.bn2 = L.BatchNormalization(n_units)
    def __init__(self,
                 in_channels,
                 channel_multiplier,
                 ksize=None,
                 stride=1,
                 pad=0,
                 nobias=True,
                 initialW=None,
                 initial_bias=None,
                 activ=relu,
                 bn_kwargs={}):
        self.activ = activ
        #print(bn_kwargs)
        super(DepthwiseConv2DBNActiv, self).__init__()

        with self.init_scope():
            self.conv = L.DepthwiseConvolution2D(in_channels,
                                                 channel_multiplier, ksize,
                                                 stride, pad, nobias, initialW,
                                                 initial_bias)
            if 'comm' in bn_kwargs:
                self.bn = MultiNodeBatchNormalization(
                    int(in_channels * channel_multiplier), **bn_kwargs)
            else:
                self.bn = BatchNormalization(
                    int(in_channels * channel_multiplier), **bn_kwargs)
Ejemplo n.º 9
0
def _3x3_dwconv_bn(stride):
    """3x3 DWConv (depthwise convolution) + BN.
    """
    return Seq(dwconv=L.DepthwiseConvolution2D(None,
                                               channel_multiplier=1,
                                               ksize=3,
                                               stride=stride,
                                               pad=1,
                                               nobias=True),
               bn=BatchNormalization(axis=(0, 2, 3)))
Ejemplo n.º 10
0
    def __init__(self, in_channels, out_channels,
                 stride=1, splits_left=2, initialW=None):
        super(ShuffleNetV2Block, self).__init__()

        with self.init_scope():
            if stride == 2:
                self.conv1 = L.Convolution2D(
                    in_channels, in_channels, 1, 1, 0, initialW=initialW,
                    nobias=True)
                self.bn1 = L.BatchNormalization(in_channels)
                self.conv2 = L.DepthwiseConvolution2D(
                    in_channels, 1, 3, 1, 1,
                    initialW=initialW, nobias=True)
                self.bn2 = L.BatchNormalization(in_channels)
                self.conv3 = L.Convolution2D(
                    in_channels, out_channels // 2, 1, 1, 0, initialW=initialW,
                    nobias=True)
                self.bn3 = L.BatchNormalization(out_channels // 2)
                self.conv4 = L.DepthwiseConvolution2D(
                    in_channels, 1, 3, 1, 1,
                    initialW=initialW, nobias=True)
                self.bn4 = L.BatchNormalization(in_channels)
                self.conv5 = L.Convolution2D(
                    in_channels, out_channels // 2, 1, 1, 0, initialW=initialW,
                    nobias=True)
                self.bn5 = L.BatchNormalization(out_channels // 2)
            elif stride == 1:
                self.in_channels = in_channels - in_channels // splits_left
                self.conv1 = L.Convolution2D(
                    self.in_channels, self.in_channels, 1, 1, 0,
                    initialW=initialW, nobias=True)
                self.bn1 = L.BatchNormalization(self.in_channels)
                self.conv2 = L.DepthwiseConvolution2D(
                    self.in_channels, 1, 3, stride, 1,
                    initialW=initialW, nobias=True)
                self.bn2 = L.BatchNormalization(self.in_channels)
                self.conv3 = L.Convolution2D(
                    self.in_channels, self.in_channels, 1, 1, 0,
                    initialW=initialW, nobias=True)
                self.bn3 = L.BatchNormalization(self.in_channels)
            self.stride = stride
            self.splits_left = splits_left
Ejemplo n.º 11
0
 def setUp(self):
     in_channels = None
     self.link = links.DepthwiseConvolution2D(in_channels, 2, 3,
                                              stride=2, pad=1)
     self.x = numpy.random.uniform(-1, 1,
                                   (2, 3, 4, 3)).astype(numpy.float32)
     self.link(chainer.Variable(self.x))
     b = self.link.b.data
     b[...] = numpy.random.uniform(-1, 1, b.shape)
     self.link.cleargrads()
     self.gy = numpy.random.uniform(-1, 1,
                                    (2, 6, 2, 2)).astype(numpy.float32)
Ejemplo n.º 12
0
    def __init__(self, in_channels, out_channels, ksize=3, stride=1, initialW=None):
        """ CTOR. """
        super(DepthwiseSeparableConv2D, self).__init__()

        with self.init_scope():
            self.conv1 = L.DepthwiseConvolution2D(
                in_channels, 1, ksize, stride=stride, pad=1, nobias=True, initialW=initialW)
            self.bn1 = L.BatchNormalization(in_channels)
            self.relu1 = lambda x: F.relu(x)
            self.conv2 = L.Convolution2D(
                in_channels, out_channels, ksize=1, stride=1, pad=0, nobias=True, initialW=initialW)
            self.bn2 = L.BatchNormalization(out_channels)
            self.relu2 = lambda x: F.relu(x)
Ejemplo n.º 13
0
    def setUp(self):
        self.link = links.DepthwiseConvolution2D(
            3, 2, 3, stride=2, pad=1,
            initialW=chainer.initializers.Normal(1, self.W_dtype),
            initial_bias=chainer.initializers.Normal(1, self.x_dtype))
        self.link.cleargrads()

        self.x = numpy.random.uniform(-1, 1,
                                      (2, 3, 4, 3)).astype(self.x_dtype)
        self.gy = numpy.random.uniform(-1, 1,
                                       (2, 6, 2, 2)).astype(self.x_dtype)
        self.check_backward_options = {}
        if self.x_dtype == numpy.float16 or self.W_dtype == numpy.float16:
            self.check_backward_options = {'atol': 3e-2, 'rtol': 5e-2}
Ejemplo n.º 14
0
 def __init__(self, in_channels, out_channels, ksize, **kwargs):
     if 'pad' in kwargs:
         pad = kwargs.pop('pad')
     else:
         pad = 0
     super(SeparableConvolution2D,
           self).__init__(depthwise=L.DepthwiseConvolution2D(in_channels,
                                                             1,
                                                             ksize,
                                                             pad=pad,
                                                             **kwargs),
                          pointwise=L.Convolution2D(in_channels,
                                                    out_channels, 1,
                                                    **kwargs))
Ejemplo n.º 15
0
 def __init__(self, in_channels, out_channels, expansion_factor=6):
     super().__init__()
     with self.init_scope():
         self.conv1 = ConvBN(in_channels, in_channels * expansion_factor)
         self.dconv = L.DepthwiseConvolution2D(in_channels *
                                               expansion_factor,
                                               1,
                                               ksize=3,
                                               stride=2,
                                               pad=1)
         self.bn = L.BatchNormalization(in_channels * expansion_factor)
         self.conv2 = L.Convolution2D(in_channels * expansion_factor,
                                      out_channels,
                                      ksize=1,
                                      stride=1,
                                      pad=0)
Ejemplo n.º 16
0
 def __init__(self, in_dim, out_dim):
     super(ConvBlock, self).__init__()
     initialW = chainer.initializers.HeNormal(3)
     with self.init_scope():
         self.c1 = L.Convolution2D(in_dim,
                                   out_dim,
                                   3,
                                   1,
                                   1,
                                   initialW=initialW)
         self.c2 = L.DepthwiseConvolution2D(out_dim,
                                            1,
                                            3,
                                            2,
                                            1,
                                            initialW=initialW)
    def __init__(self, in_channels, out_channels, ksize, stride=1, pad=0,
                 nobias=True, initialW=None, initial_bias=None,
                 activation_fn=F.relu, **kwargs):
        self.activation_fn = activation_fn
        self.ksize = ksize
        self.stride = stride

        super(TFDWSeparableConvBlock, self).__init__()
        with self.init_scope():
            self.dw = L.DepthwiseConvolution2D(in_channels, 1, ksize, stride, 0,
                                               nobias, initialW, initial_bias)
            self.dw_bn = L.BatchNormalization(in_channels, decay=0.9997,
                                                            eps=0.001)
            self.pw = L.Convolution2D(in_channels, out_channels, 1, 1, 0, 
                                      nobias, initialW, initial_bias)
            self.pw_bn = L.BatchNormalization(out_channels, decay=0.9997,
                                                            eps=0.001)
Ejemplo n.º 18
0
    def __init__(self, in_c, out_c, stride):
        super(dw_sep_conv, self).__init__()
        with self.init_scope():

            self.conv_dw = L.DepthwiseConvolution2D(in_c,
                                                    1,
                                                    3,
                                                    stride=stride,
                                                    pad=1,
                                                    nobias=True)
            self.bn_dw = L.BatchNormalization(in_c)

            self.conv_pw = L.Convolution2D(in_c,
                                           out_c,
                                           1,
                                           stride=1,
                                           pad=0,
                                           nobias=True)
            self.bn_pw = L.BatchNormalization(out_c)
    def __init__(self, expand_ratio, in_channels, out_channels, stride):
        super(ExpandedConv, self).__init__()
        ksize = 3
        self.dtype = np.float32
        self.expand_ratio = expand_ratio
        expanded_channels = int(in_channels * expand_ratio)
        initialW = initializers.HeNormal(1 / np.sqrt(2), self.dtype)
        with self.init_scope():
            if expand_ratio != 1:
                self.expand_conv = L.Convolution2D(in_channels,
                                                   expanded_channels,
                                                   ksize=1,
                                                   initialW=initialW,
                                                   nobias=True)
                self.expand_bn = L.BatchNormalization(expanded_channels,
                                                      eps=0.001,
                                                      decay=0.997)

            self.depthwise_conv = L.DepthwiseConvolution2D(
                expanded_channels,
                channel_multiplier=1,
                ksize=ksize,
                stride=stride,
                pad=ksize // 2,
                initialW=initialW,
                nobias=True)
            self.depthwise_bn = L.BatchNormalization(expanded_channels,
                                                     eps=0.001,
                                                     decay=0.9997)
            self.project_conv = L.Convolution2D(expanded_channels,
                                                out_channels,
                                                ksize=1,
                                                initialW=initialW,
                                                nobias=True)
            self.project_bn = L.BatchNormalization(out_channels,
                                                   eps=0.001,
                                                   decay=0.9997)
Ejemplo n.º 20
0
 def __init__(self, inp, oup, stride, restype=None, activation=F.relu):
     super(ConvDW, self).__init__()
     self.restype = restype
     self.stride = stride
     self.activation = activation
     if self.restype == 'concat':
         self.oup = oup - inp
     else:
         self.oup = oup
     with self.init_scope():
         self.conv_dw = L.DepthwiseConvolution2D(inp,
                                                 1,
                                                 3,
                                                 stride=stride,
                                                 pad=1,
                                                 nobias=True)
         self.bn_dw = L.BatchNormalization(inp)
         self.conv_sep = L.Convolution2D(inp,
                                         self.oup,
                                         1,
                                         stride=1,
                                         pad=0,
                                         nobias=True)
         self.bn_sep = L.BatchNormalization(self.oup)
Ejemplo n.º 21
0
    def __init__(self, in_channels, n_class, ksize):
        super().__init__()
        with self.init_scope():
            self.dfg = FilterDictConv2d(in_channels, n_class,
                                        ksize)  # out:N_i*E*M
            self.weight_target_node = \
                L.DepthwiseConvolution2D(in_channels, ksize, 1)  # out: D*M
            # self.out_adjacent_node = \
            #     L.ConvolutionND(ksize, in_channels, in_channels, 1)
        self.in_channels = in_channels
        self.n_class = n_class
        self.img_h, self.img_w = 28, 28

        adjacent_matrix = {}
        for r, c in itertools.product(range(28), range(28)):
            if r == 0 and c == 0:
                adjacent_matrix[r * self.img_w + c] = [
                    r * self.img_w + c + 1, (r + 1) * self.img_w + c + 1,
                    (r + 1) * self.img_w + c
                ]
            elif r == 0 and c == 27:
                adjacent_matrix[r * self.img_w + c] = [
                    r * self.img_w + c - 1, (r + 1)**self.img_w + c - 1,
                    (r + 1) * self.img_w + c
                ]
            elif r == 0 and 0 < c < 27:
                adjacent_matrix[r * self.img_w + c] = [
                    r * self.img_w + c - 1, r * self.img_w + c + 1,
                    (r + 1) * self.img_w + c - 1, (r + 1) * self.img_w + c,
                    (r + 1) * self.img_w + c + 1
                ]
            elif r == 27 and c == 0:
                adjacent_matrix[r * self.img_w + c] = [
                    r * self.img_w + c + 1, (r - 1) * self.img_w + c + 1,
                    (r - 1) * self.img_w + c
                ]
            elif r == 27 and c == 27:
                adjacent_matrix[r * self.img_w + c] = [
                    r * self.img_w + c - 1, (r - 1) * self.img_w + c - 1,
                    (r - 1) * self.img_w + c
                ]
            elif r == 27 and 0 < c < 27:
                adjacent_matrix[r * self.img_w + c] = [
                    r * self.img_w + c - 1, r * self.img_w + c + 1,
                    (r - 1) * self.img_w + c - 1, (r - 1) * self.img_w + c,
                    (r - 1) * self.img_w + c + 1
                ]
            elif 0 < r < 27 and c == 0:
                adjacent_matrix[r * self.img_w + c] = [
                    (r - 1) * self.img_w + c,
                    (r - 1) * self.img_w + c + 1, r * self.img_w + c + 1,
                    (r + 1) * self.img_w + c, (r + 1) * self.img_w + c + 1
                ]
            elif 0 < r < 27 and c == 27:
                adjacent_matrix[r * self.img_w + c] = [
                    (r - 1) * self.img_w + c,
                    (r - 1) * self.img_w + c - 1, r * self.img_w + c - 1,
                    (r + 1) * self.img_w + c, (r + 1) * self.img_w + c - 1
                ]
            elif 0 < r < 27 and 0 < c < 27:
                adjacent_matrix[r * self.img_w + c] = [
                    (r - 1) * self.img_w + c - 1, (r - 1) * self.img_w + c,
                    (r - 1) * self.img_w + c + 1, r * self.img_w + c - 1,
                    r * self.img_w + c + 1, (r + 1) * self.img_w + c - 1,
                    (r + 1) * self.img_w + c, (r + 1) * self.img_w + c + 1
                ]

            self.adjacent_matrix = adjacent_matrix
Ejemplo n.º 22
0
    def __init__(self, final_endpoint='Conv2d_13_pointwise_batchnorm', min_depth=8, depth_multiplier=1.0,
                 output_stride=None, use_explicit_padding=False):
        super(MobileNet_v1_Base, self).__init__()
        initialW = initializers.HeNormal()
        self.depth = lambda d: max(int(d * depth_multiplier), min_depth)
        if depth_multiplier <= 0:
            raise ValueError('depth_multiplier is not greater than zero.')

        if output_stride is not None and output_stride not in [8, 16, 32]:
            raise ValueError('Only allowed output_stride values are 8, 16, 32.')
        Conv = namedtuple('Conv', ['kernel', 'stride','inchannel', 'depth'])
        DepthSepConv = namedtuple('DepthSepConv', ['kernel', 'inchannel', 'stride', 'depth'])
        self.conv_defs = [
                Conv(kernel=[3, 3], stride=2, inchannel=3, depth=32),
                DepthSepConv(kernel=[3, 3], stride=1, inchannel=32, depth=64),
                DepthSepConv(kernel=[3, 3], stride=2, inchannel=64, depth=128),
                DepthSepConv(kernel=[3, 3], stride=1, inchannel=128,depth=128),
                DepthSepConv(kernel=[3, 3], stride=2, inchannel=128, depth=256),
                DepthSepConv(kernel=[3, 3], stride=1, inchannel=256,depth=256),
                DepthSepConv(kernel=[3, 3], stride=2, inchannel=256,depth=512),
                DepthSepConv(kernel=[3, 3], stride=1, inchannel=512,depth=512),
                DepthSepConv(kernel=[3, 3], stride=1, inchannel=512,depth=512),
                DepthSepConv(kernel=[3, 3], stride=1, inchannel=512,depth=512),
                DepthSepConv(kernel=[3, 3], stride=1, inchannel=512,depth=512),
                DepthSepConv(kernel=[3, 3], stride=1, inchannel=512,depth=512),
                DepthSepConv(kernel=[3, 3], stride=2, inchannel=512, depth=1024),
                DepthSepConv(kernel=[3, 3], stride=1, inchannel=1024, depth=1024),
            ]
        self.scale_dict = {"Conv2d_11_pointwise_batchnorm" : "16_scale", "Conv2d_13_pointwise_batchnorm": "32_scale"}

        self.padding = 'SAME'
        pad = 1  # 3 x 3 conv kernal, with 1x1 padding
        if use_explicit_padding:
            self.padding = 'VALID'
            pad = 0
        with self.init_scope():
            # The current_stride variable keeps track of the output stride of the
            # activations, i.e., the running product of convolution strides up to the
            # current network layer. This allows us to invoke atrous convolution
            # whenever applying the next convolution would result in the activations
            # having output stride larger than the target output_stride.
            self.layer_names = []
            current_stride = 1
            rate = 1
            for i, conv_def in enumerate(self.conv_defs):
                end_point_base = 'Conv2d_{}'.format(i)
                if output_stride is not None and current_stride == output_stride:
                    # If we have reached the target output_stride, then we need to employ
                    # atrous convolution with stride=1 and multiply the atrous rate by the
                    # current unit's stride for use in subsequent layers.
                    # FIXME chainer的depthwise conv 不支持atrous rate or dilated rate
                    layer_stride = 1
                    layer_rate = rate
                    rate *= conv_def.stride
                else:
                    layer_stride = conv_def.stride
                    layer_rate = 1
                    current_stride *= conv_def.stride
                if isinstance(conv_def, Conv):
                    end_point = end_point_base
                    if use_explicit_padding:
                        self.layer_names.append(("use_explicit_padding", conv_def.kernel))
                    setattr(self, end_point, L.Convolution2D(in_channels=conv_def.inchannel, out_channels=self.depth(conv_def.depth),
                                                             ksize=conv_def.kernel, stride=conv_def.stride,pad=pad, nobias=False,
                                                             initialW=initialW))
                    self.layer_names.append(end_point)
                    bn_end_point = end_point + "_batchnorm"
                    setattr(self, bn_end_point, L.BatchNormalization(size=self.depth(conv_def.depth)))
                    self.layer_names.append(bn_end_point)
                    if end_point == final_endpoint:
                        return
                elif isinstance(conv_def, DepthSepConv):

                    end_point = end_point_base + '_depthwise'
                    if use_explicit_padding:
                        self.layer_names.append(("use_explicit_padding", conv_def.kernel, layer_rate))
                    setattr(self, end_point, L.DepthwiseConvolution2D(in_channels=self.depth(conv_def.inchannel), channel_multiplier=1,
                                                                      ksize=conv_def.kernel, stride=layer_stride, pad=pad,nobias=False,
                                                                      initialW=initialW))
                    self.layer_names.append(end_point)
                    bn_end_point = end_point + "_batchnorm"
                    setattr(self, bn_end_point, L.BatchNormalization(size=self.depth(conv_def.inchannel)))
                    self.layer_names.append(bn_end_point)
                    if end_point == final_endpoint:
                        return
                    end_point = end_point_base + '_pointwise'

                    setattr(self, end_point, L.Convolution2D(in_channels=self.depth(conv_def.inchannel), out_channels=self.depth(conv_def.depth),
                                                             ksize=[1,1], stride=1, pad=0, nobias=False, initialW=initialW))
                    self.layer_names.append(end_point)
                    bn_end_point = end_point + "_batchnorm"
                    setattr(self, bn_end_point, L.BatchNormalization(size=self.depth(conv_def.depth)))
                    self.layer_names.append(bn_end_point)
                    if end_point == final_endpoint:
                        return
                else:
                    raise ValueError('Unknown convolution type %s for layer %d'
                                     % (conv_def.ltype, i))
Ejemplo n.º 23
0
    def __init__(self,
                 ch_in,
                 ch_out,
                 ksize=(3, 1),
                 stride=(1, 1),
                 pad=(1, 0),
                 wscale=0.02,
                 normalize=None,
                 use_wscale=False,
                 n_class=2,
                 padding_mode=None):
        assert normalize in [
            'batch', 'cbatch', 'spectrum+batch', 'instance',
            'conditional_instance', None
        ]
        if use_wscale:
            w = chainer.initializers.Normal(1.0)
        else:
            if type(ksize) == 'int':
                w = chainer.initializers.Uniform(scale=math.sqrt(1 / ksize /
                                                                 ksize))
            else:
                w = chainer.initializers.Uniform(scale=math.sqrt(1 / ksize[0] /
                                                                 ksize[1]))
        super(dw_conv_Layer, self).__init__()
        pad_conv = 0 if padding_mode else pad
        with self.init_scope():
            if normalize == 'spectrum' or normalize == 'spectrum+batch':
                self.conv = SNDepthwiseConvolution2D(ch_in,
                                                     ch_out // ch_in,
                                                     ksize=ksize,
                                                     stride=stride,
                                                     pad=pad_conv,
                                                     initialW=w)
            else:
                self.conv = L.DepthwiseConvolution2D(ch_in,
                                                     ch_out // ch_in,
                                                     ksize=ksize,
                                                     stride=stride,
                                                     pad=pad_conv,
                                                     initialW=w)

            if normalize == 'batch' or normalize == 'spectrum+batch':
                self.batch_norm = L.BatchNormalization(ch_out)
            elif normalize == 'cbatch':
                self.batch_norm = CategoricalConditionalBatchNormalization(
                    ch_out, n_class)
            elif normalize == 'instance':
                #self.batch_norm = L.BatchNormalization(axis=(0,3,4))
                self.batch_norm = InstanceNormalization(ch_out)
            elif normalize == 'conditional_instance':
                #self.batch_norm = ConditionalInstanceNormalization(ch_out)
                self.norm0 = InstanceNormalization(ch_out)
                self.norm1 = InstanceNormalization(ch_out)

        self.c = np.sqrt(2 / (ksize[0] * ksize[1]))
        self.normalize = normalize
        self.use_wscale = use_wscale
        self.padding_mode = padding_mode
        self.padding_width = [[0, 0], [0, 0], [pad[0], pad[0]],
                              [pad[1], pad[1]]]
Ejemplo n.º 24
0
def D(_self):
    return L.DepthwiseConvolution2D(None, 1, 3, _self.stride, 1, _self.nobias,
                                    _self.initialW)
Ejemplo n.º 25
0
def ChooseWhichFilters(conv_pw1, bn_pw, conv_dw, bn_dw, conv_pw2, numfilter):

    # new conv_dw
    param = conv_dw.W.data
    shape = param.shape
    param = param.reshape(shape[1], shape[0] * shape[2] * shape[3])
    param = cuda.to_cpu(param)

    norms = np.linalg.norm(param, axis=1)
    order = np.argsort(norms)[::-1]
    param = np.delete(param, order[numfilter:], axis=0)

    param = param.reshape((shape[0], numfilter, shape[2], shape[3]))
    param = cuda.to_gpu(param)

    new_conv_dw = L.DepthwiseConvolution2D(in_channels=numfilter,
                                           channel_multiplier=1,
                                           ksize=3,
                                           stride=conv_dw.stride,
                                           pad=1,
                                           nobias=True)

    new_conv_dw.W.data = param

    # new bn_dw
    new_bn_dw = L.BatchNormalization(numfilter)

    param = bn_dw.beta.data
    param = cuda.to_cpu(param)
    param = np.delete(param, order[numfilter:])
    param = cuda.to_gpu(param)
    new_bn_dw.beta.data = param

    param = bn_dw.gamma.data
    param = cuda.to_cpu(param)
    param = np.delete(param, order[numfilter:])
    param = cuda.to_gpu(param)
    new_bn_dw.gamma.data = param

    new_bn_dw.to_gpu(0)

    # new conv_pw1
    param = conv_pw1.W.data
    param = cuda.to_cpu(param)
    param = np.delete(param, order[numfilter:], axis=0)
    param = cuda.to_gpu(param)

    new_conv_pw1 = L.Convolution2D(in_channels=conv_pw1.W.data.shape[1],
                                   out_channels=numfilter,
                                   ksize=1,
                                   stride=conv_pw1.stride,
                                   pad=0,
                                   nobias=True)

    new_conv_pw1.W.data = param

    # new bn_pw
    new_bn_pw = L.BatchNormalization(numfilter)

    param = bn_pw.beta.data
    param = cuda.to_cpu(param)
    param = np.delete(param, order[numfilter:])
    param = cuda.to_gpu(param, 0)
    new_bn_pw.beta.data = param

    param = bn_pw.gamma.data
    param = cuda.to_cpu(param)
    param = np.delete(param, order[numfilter:])
    param = cuda.to_gpu(param, 0)
    new_bn_pw.gamma.data = param

    new_bn_pw.to_gpu(0)

    # new conv_pw2
    param = conv_pw2.W.data
    param = cuda.to_cpu(param)
    param = np.delete(param, order[numfilter:], axis=1)
    param = cuda.to_gpu(param)

    new_conv_pw2 = L.Convolution2D(in_channels=numfilter,
                                   out_channels=conv_pw2.W.data.shape[0],
                                   ksize=1,
                                   stride=conv_pw1.stride,
                                   pad=0,
                                   nobias=True)

    new_conv_pw2.W.data = param

    return (new_conv_pw1, new_bn_pw, new_conv_dw, new_bn_dw, new_conv_pw2)