예제 #1
0
 def __init__(self, in_size, ch, use_cudnn=True):
     w = math.sqrt(2)
     super(BottleNeckB, self).__init__(
         conv1=convolution_2d.Convolution2D(in_size,
                                            ch,
                                            1,
                                            1,
                                            0,
                                            w,
                                            nobias=True,
                                            use_cudnn=use_cudnn),
         bn1=batch_normalization.BatchNormalization(ch),
         conv2=convolution_2d.Convolution2D(ch,
                                            ch,
                                            3,
                                            1,
                                            1,
                                            w,
                                            nobias=True,
                                            use_cudnn=use_cudnn),
         bn2=batch_normalization.BatchNormalization(ch),
         conv3=convolution_2d.Convolution2D(ch,
                                            in_size,
                                            1,
                                            1,
                                            0,
                                            w,
                                            nobias=True,
                                            use_cudnn=use_cudnn),
         bn3=batch_normalization.BatchNormalization(in_size),
     )
예제 #2
0
파일: Models.py 프로젝트: ebsrn/CORE
 def __init__(self,
              ich,
              och,
              ksize,
              stride,
              pad,
              dilate,
              init_weights,
              pool=None):
     super(DilatedConvBN, self).__init__(
         conv=D.DilatedConvolution2D(ich,
                                     och,
                                     ksize,
                                     stride,
                                     pad,
                                     dilate,
                                     nobias=True),
         bn=B.BatchNormalization(och),
     )
     self.pool = pool
     if init_weights:
         f = h5py.File('%s/data/dump/%s.h5' % (os.getcwd(), init_weights),
                       'r')
         self.conv.W.data = np.array(f['weights']).transpose([3, 2, 0, 1])
         self.bn.beta.data = np.array(f['beta'])
         self.bn.gamma.data = np.array(f['gamma'])
         self.bn.avg_mean = np.array(f['mean'])
         self.bn.avg_var = np.array(f['var'])
예제 #3
0
    def _setup_batchnorm(self, layer):
        # Get layer parameters.
        blobs = layer.blobs
        param = layer.batch_norm_param
        use_global_stats = param.use_global_stats
        decay = param.moving_average_fraction
        eps = param.eps
        size = int(blobs[0].shape.dim[0])  # Get channel dim from mean blob.

        # Make BatchNormalization link.
        func = batch_normalization.BatchNormalization(
            size, decay=decay, eps=eps, use_gamma=False, use_beta=False)
        func.avg_mean.ravel()[:] = blobs[0].data
        func.avg_var.ravel()[:] = blobs[1].data
        with self.init_scope():
            setattr(self, layer.name, func)

        # Add layer.
        if use_global_stats:
            func_class = _SingleArgumentFunctionTestMode
        else:
            func_class = _SingleArgumentFunction
        fwd = func_class(_CallChildLink(self, layer.name), finetune=False)
        self.forwards[layer.name] = fwd
        self._add_layer(layer)
예제 #4
0
    def _setup_batchnorm(self, layer):
        # Get layer parameters.
        blobs = layer.blobs
        param = layer.batch_norm_param
        use_global_stats = param.use_global_stats
        decay = param.moving_average_fraction
        eps = param.eps
        size = int(blobs[0].shape.dim[0])  # Get channel dim from mean blob.

        # Make BatchNormalization link.
        func = batch_normalization.BatchNormalization(
            size, decay=decay, eps=eps, use_gamma=False, use_beta=False)

        _Blob(blobs[0])(func.avg_mean)
        _Blob(blobs[1])(func.avg_var)

        # Scale the means and variances if a scaling factor is appended to the
        # blobs to correctly mimic to the behavior of Caffe. See
        # https://github.com/BVLC/caffe/issues/4885
        if len(blobs) >= 3:
            scaling_factor = blobs[2].data
            func.avg_mean /= scaling_factor[0]
            func.avg_var /= scaling_factor[0]

        with self.init_scope():
            setattr(self, layer.name, func)

        # Add layer.
        if use_global_stats:
            func_class = _SingleArgumentFunctionTestMode
        else:
            func_class = _SingleArgumentFunction
        fwd = func_class(_CallChildLink(self, layer.name), finetune=False)
        self.forwards[layer.name] = fwd
        self._add_layer(layer)
예제 #5
0
    def __init__(self, in_size, out_size, batch_norm_type='upward'):
        super(StatefulLinearRNN, self).__init__(upward=L.Linear(in_size, out_size),
                                                lateral=L.Linear(out_size, out_size))
        if batch_norm_type not in ('none', 'upward', 'lateral', 'output'):
            raise ValueError('Invalid batch_norm_type:{}'.format(batch_norm_type))
        self.batch_norm_type = batch_norm_type

        if batch_norm_type != 'none':
            batch_norm = B.BatchNormalization(out_size)
            self.add_link('batch_norm', batch_norm)

        self.reset_state()
예제 #6
0
파일: inceptionbn.py 프로젝트: yygr/chainer
    def __init__(self, in_channels, out1, proj3, out3, proj33, out33,
                 pooltype, proj_pool=None, stride=1, conv_init=None,
                 dtype=numpy.float32):
        super(InceptionBN, self).__init__(
            proj3=convolution_2d.Convolution2D(
                in_channels, proj3, 1, nobias=True, initialW=conv_init),
            conv3=convolution_2d.Convolution2D(
                proj3, out3, 3, pad=1, stride=stride, nobias=True,
                initialW=conv_init),
            proj33=convolution_2d.Convolution2D(
                in_channels, proj33, 1, nobias=True, initialW=conv_init),
            conv33a=convolution_2d.Convolution2D(
                proj33, out33, 3, pad=1, nobias=True, initialW=conv_init),
            conv33b=convolution_2d.Convolution2D(
                out33, out33, 3, pad=1, stride=stride, nobias=True,
                initialW=conv_init),
            proj3n=batch_normalization.BatchNormalization(proj3, dtype=dtype),
            conv3n=batch_normalization.BatchNormalization(out3, dtype=dtype),
            proj33n=batch_normalization.BatchNormalization(proj33,
                                                           dtype=dtype),
            conv33an=batch_normalization.BatchNormalization(out33,
                                                            dtype=dtype),
            conv33bn=batch_normalization.BatchNormalization(out33,
                                                            dtype=dtype),
        )

        if out1 > 0:
            assert stride == 1
            assert proj_pool is not None
            self.add_link('conv1',
                          convolution_2d.Convolution2D(in_channels, out1, 1,
                                                       stride=stride,
                                                       nobias=True,
                                                       initialW=conv_init))
            self.add_link('conv1n', batch_normalization.BatchNormalization(
                out1, dtype=dtype))
        self.out1 = out1

        if proj_pool is not None:
            self.add_link('poolp', convolution_2d.Convolution2D(
                in_channels, proj_pool, 1, nobias=True, initialW=conv_init))
            self.add_link('poolpn', batch_normalization.BatchNormalization(
                proj_pool, dtype=dtype))
        self.proj_pool = proj_pool

        self.stride = stride
        self.pooltype = pooltype
        if pooltype != 'max' and pooltype != 'avg':
            raise NotImplementedError()

        self.train = True
예제 #7
0
    def __init__(self, in_channels, out1, proj3, out3, proj33, out33,
                 pooltype, proj_pool=None, stride=1, conv_init=None,
                 dtype=None):
        super(InceptionBN, self).__init__()
        self.out1 = out1
        self.proj_pool = proj_pool
        self.stride = stride
        self.pooltype = pooltype
        if pooltype != 'max' and pooltype != 'avg':
            raise NotImplementedError()
        dtype = chainer.get_dtype(dtype)

        with self.init_scope():
            self.proj3 = convolution_2d.Convolution2D(
                in_channels, proj3, 1, nobias=True, initialW=conv_init)
            self.conv3 = convolution_2d.Convolution2D(
                proj3, out3, 3, pad=1, stride=stride, nobias=True,
                initialW=conv_init)
            self.proj33 = convolution_2d.Convolution2D(
                in_channels, proj33, 1, nobias=True, initialW=conv_init)
            self.conv33a = convolution_2d.Convolution2D(
                proj33, out33, 3, pad=1, nobias=True, initialW=conv_init)
            self.conv33b = convolution_2d.Convolution2D(
                out33, out33, 3, pad=1, stride=stride, nobias=True,
                initialW=conv_init)
            self.proj3n = batch_normalization.BatchNormalization(
                proj3, dtype=dtype)
            self.conv3n = batch_normalization.BatchNormalization(
                out3, dtype=dtype)
            self.proj33n = batch_normalization.BatchNormalization(
                proj33, dtype=dtype)
            self.conv33an = batch_normalization.BatchNormalization(
                out33, dtype=dtype)
            self.conv33bn = batch_normalization.BatchNormalization(
                out33, dtype=dtype)

            if out1 > 0:
                assert stride == 1
                assert proj_pool is not None
                self.conv1 = convolution_2d.Convolution2D(
                    in_channels, out1, 1, stride=stride, nobias=True,
                    initialW=conv_init)
                self.conv1n = batch_normalization.BatchNormalization(
                    out1, dtype=dtype)

            if proj_pool is not None:
                self.poolp = convolution_2d.Convolution2D(
                    in_channels, proj_pool, 1, nobias=True, initialW=conv_init)
                self.poolpn = batch_normalization.BatchNormalization(
                    proj_pool, dtype=dtype)
예제 #8
0
 def __init__(self, use_cudnn=True):
     w = math.sqrt(2)
     super(ResNet50, self).__init__(
         conv1=convolution_2d.Convolution2D(3,
                                            64,
                                            7,
                                            2,
                                            3,
                                            w,
                                            nobias=True,
                                            use_cudnn=use_cudnn),
         bn1=batch_normalization.BatchNormalization(64),
         res2=Block(3, 64, 64, 256, 1, use_cudnn=use_cudnn),
         res3=Block(4, 256, 128, 512, use_cudnn=use_cudnn),
         res4=Block(6, 512, 256, 1024, use_cudnn=use_cudnn),
         res5=Block(3, 1024, 512, 2048, use_cudnn=use_cudnn),
         fc=linear.Linear(2048, 1000),
     )
     self.use_cudnn = use_cudnn
     self.train = True
예제 #9
0
 def __init__(self, conv, batch_norm=True, pool=None):
     super(AuxConv, self).__init__(conv=conv)
     if batch_norm:
         out_channel = conv.W.data.shape[0]
         self.add_link('batch_norm', B.BatchNormalization(out_channel))
     self.pool = pool
예제 #10
0
 def __init__(self, *args, **kwargs):
     conv = C.Convolution2D(*args, **kwargs)
     out_channel = len(conv.W.data)
     batch_norm = B.BatchNormalization(out_channel)
     super(ConvBN, self).__init__(conv=conv, batch_norm=batch_norm)
예제 #11
0
 def __init__(self, *args, **kwargs):
     linear = L.Linear(*args, **kwargs)
     out_channel = len(linear.W.data)
     batch_norm = B.BatchNormalization(out_channel)
     super(LinearBN, self).__init__(linear=linear, batch_norm=batch_norm)