def setUp(self):
        C = self.n_channels // self.groups
        head_ndim = 2

        self.link = links.DecorrelatedBatchNormalization(self.n_channels,
                                                         groups=self.groups,
                                                         dtype=self.dtype)
        self.link.cleargrads()

        shape = (5, self.n_channels) + (2, ) * self.ndim
        self.x = numpy.random.uniform(-1, 1, shape).astype(self.dtype)
        self.gy = numpy.random.uniform(-1, 1, shape).astype(self.dtype)

        if self.test:
            self.mean = numpy.random.uniform(-1, 1, (C, )).astype(self.dtype)
            self.projection = numpy.random.uniform(0.5, 1,
                                                   (C, C)).astype(self.dtype)
            self.link.avg_mean[...] = self.mean
            self.link.avg_projection[...] = self.projection
        else:
            spatial_axis = tuple(range(head_ndim, self.x.ndim))
            x_hat = self.x.reshape((5 * self.groups, C) + self.x.shape[2:])
            x_hat = x_hat.transpose((1, 0) + spatial_axis).reshape((C, -1))
            self.mean = x_hat.mean(axis=1)
            self.projection = _calc_projection(self.x, self.mean,
                                               self.link.eps, self.groups)
        self.check_forward_options = {'atol': 1e-4, 'rtol': 1e-3}
        self.check_backward_options = {
            'atol': 5e-3,
            'rtol': 1e-3,
            'dtype': numpy.float64
        }
        if self.dtype == numpy.float32:
            self.check_backward_options = {'atol': 5e-2, 'rtol': 5e-2}
Exemple #2
0
    def setUp(self):
        C = self.n_channels // self.groups

        self.link = links.DecorrelatedBatchNormalization(self.n_channels,
                                                         groups=self.groups,
                                                         dtype=self.dtype)
        self.link.cleargrads()

        shape = (5, self.n_channels) + (2, ) * self.ndim
        self.x = numpy.random.uniform(-1, 1, shape).astype(self.dtype)
        self.gy = numpy.random.uniform(-1, 1, shape).astype(self.dtype)

        if self.test:
            self.mean = numpy.random.uniform(-1, 1, (self.groups, C)).astype(
                self.dtype)
            self.projection = numpy.random.uniform(
                0.5, 1, (self.groups, C, C)).astype(self.dtype)
            self.link.avg_mean[...] = self.mean
            self.link.avg_projection[...] = self.projection
        else:
            self.mean = _calc_mean(self.x, self.groups)
            self.projection = _calc_projection(self.x, self.mean,
                                               self.link.eps, self.groups)
        self.check_forward_options = {'atol': 1e-4, 'rtol': 1e-3}
        self.check_backward_options = {'atol': 5e-3, 'rtol': 1e-3}
        if self.dtype == numpy.float32:
            self.check_backward_options = {'atol': 5e-2, 'rtol': 5e-2}
Exemple #3
0
 def create_link(self, initializers):
     mean, projection = initializers
     link = links.DecorrelatedBatchNormalization(self.n_channels,
                                                 groups=self.groups,
                                                 dtype=self.dtype)
     link.cleargrads()
     if self.test:
         link.avg_mean[...] = mean
         link.avg_projection[...] = projection
     return link
Exemple #4
0
 def __init__(self, link_name, finetune, forget=False):
     super(Model, self).__init__()
     with self.init_scope():
         if link_name == 'bn':
             self.link = links.BatchNormalization(3)
         elif link_name == 'brn':
             self.link = links.BatchRenormalization(3)
         elif link_name == 'dbn':
             self.link = links.DecorrelatedBatchNormalization(
                 3, groups=3)
     self.forget = forget
     self.finetune = finetune
Exemple #5
0
 def __init__(self, link_name, finetune, forget=False):
     super(Model, self).__init__()
     with self.init_scope():
         if link_name == 'bn':
             self.link = links.BatchNormalization(3)
         elif link_name == 'brn':
             # defaults rmax=1, dmax=0 are so trivial that BRN
             # becomes BN
             self.link = links.BatchRenormalization(3,
                                                    rmax=2.0,
                                                    dmax=1.0)
         elif link_name == 'dbn':
             self.link = links.DecorrelatedBatchNormalization(
                 3, groups=3)
     self.forget = forget
     self.finetune = finetune
Exemple #6
0
    def check_model_compatibility(self, backend_config, save, load):
        C = self.n_channels // self.groups
        old_model = {
            'avg_mean':
            numpy.random.uniform(-1, 1, (C, )).astype(self.dtype),
            'avg_projection':
            numpy.random.uniform(0.5, 1, (C, C)).astype(self.dtype),
            'N':
            numpy.array(0)
        }
        save(self.temp_file_path, old_model)

        model = links.DecorrelatedBatchNormalization(self.n_channels,
                                                     groups=self.groups,
                                                     dtype=self.dtype)
        model.to_device(backend_config.device)
        with (testing.assert_warns(UserWarning)
              if self.groups != 1 else nullcontext()):
            load(self.temp_file_path, model)
        x = numpy.random.rand(5, self.n_channels, 2).astype(self.dtype)
        x = backend_config.get_array(x)
        with chainer.using_config('train', False):
            model(x)
        model(x)