Ejemplo n.º 1
0
 def test_backward_cpu_more_than_once(self):
     func = functions.MaxPooling2D(3,
                                   stride=2,
                                   pad=1,
                                   cover_all=self.cover_all)
     func.apply(self.inputs)
     func.backward((0, ), self.grad_outputs)
     func.backward((0, ), self.grad_outputs)
Ejemplo n.º 2
0
 def check_backward(self, x_data, y_grad, use_cudnn=True):
     gradient_check.check_backward(
         functions.MaxPooling2D(3,
                                stride=2,
                                pad=1,
                                cover_all=self.cover_all,
                                use_cudnn=use_cudnn), x_data, y_grad,
         **self.check_backward_options)
Ejemplo n.º 3
0
 def test_backward_cpu_more_than_once(self):
     func = functions.MaxPooling2D(3,
                                   stride=2,
                                   pad=1,
                                   cover_all=self.cover_all)
     func.apply((self.x, ))
     func.backward((0, ), (self.gy, ))
     func.backward((0, ), (self.gy, ))
Ejemplo n.º 4
0
 def check_backward(self, x_data, y_grad, use_cudnn='always'):
     with chainer.using_config('use_cudnn', use_cudnn):
         gradient_check.check_backward(
             functions.MaxPooling2D(3,
                                    stride=2,
                                    pad=1,
                                    cover_all=self.cover_all), x_data,
             y_grad, **self.check_backward_options)
Ejemplo n.º 5
0
 def __init__(self, in_channel, n_mid=64):
     w = math.sqrt(2)
     super(EncDec, self).__init__(
         enc=L.Convolution2D(in_channel, n_mid, 7, 1, 3, w),
         bn_m=L.BatchNormalization(n_mid),
         dec=L.Convolution2D(n_mid, n_mid, 7, 1, 3, w),
         bn_o=L.BatchNormalization(n_mid),
     )
     self.p = F.MaxPooling2D(2, 2, use_cudnn=False)
     self.inside = None
Ejemplo n.º 6
0
 def __init__(self, n_channels, n_filters=8, ksize=5, task_is_train=True):
     self.dtype = np.float32
     W = chainer.initializers.HeNormal(1 / np.sqrt(2), self.dtype)
     bias = chainer.initializers.Zero(self.dtype)
     self.pooling_ksize = 2
     self.pooling_stride = 2
     self.h1_size = None
     self.h2_size = None
     self.h3_size = None
     super(ConvolutionalVariationalAutoencoder, self).__init__(
         # encoder
         enc_conv1=L.Convolution2D(n_channels, out_channels=n_filters, ksize=ksize, pad=ksize // 2,
                                   initialW=W, initial_bias=bias),
         enc_bn1=L.BatchNormalization(n_filters),
         enc_conv2=L.Convolution2D(n_filters, n_filters // 2, ksize=ksize, pad=ksize // 2,
                                   initialW=W, initial_bias=bias),
         enc_bn2=L.BatchNormalization(n_filters // 2),
         enc_conv3=L.Convolution2D(n_filters // 2, n_filters // 2, ksize=ksize, pad=ksize // 2,
                                   initialW=W, initial_bias=bias),
         enc_bn3=L.BatchNormalization(n_filters // 2),
         enc_conv4_mu=L.Convolution2D(n_filters // 2, n_filters // 2, ksize=ksize, pad=ksize // 2,
                                      initialW=W, initial_bias=bias),
         enc_conv4_ln_var=L.Convolution2D(n_filters // 2, n_filters // 2, ksize=ksize, pad=ksize // 2,
                                          initialW=W, initial_bias=bias),
         # decoder
         dec_conv1=L.Convolution2D(n_filters // 2, n_filters // 2, ksize=ksize, pad=ksize // 2,
                                   initialW=W, initial_bias=bias),
         dec_bn1=L.BatchNormalization(n_filters // 2),
         dec_conv2=L.Convolution2D(n_filters // 2, n_filters // 2, ksize=ksize, pad=ksize // 2,
                                   initialW=W, initial_bias=bias),
         dec_bn2=L.BatchNormalization(n_filters // 2),
         dec_conv3=L.Convolution2D(n_filters // 2, n_filters, ksize=ksize, pad=ksize // 2,
                                   initialW=W, initial_bias=bias),
         dec_bn3=L.BatchNormalization(n_filters),
         dec_conv4_mu=L.Convolution2D(n_filters, n_channels, ksize=ksize, pad=ksize // 2,
                                      initialW=W, initial_bias=bias),
         dec_conv4_ln_var=L.Convolution2D(n_filters, n_channels, ksize=ksize, pad=ksize // 2,
                                          initialW=W, initial_bias=bias)
     )
     self.p1 = F.MaxPooling2D(2, 2)
     self.p2 = F.MaxPooling2D(2, 2)
     self.p3 = F.MaxPooling2D(2, 2)
     self.train = task_is_train
Ejemplo n.º 7
0
    def __init__(self, train, alphabet_size, feature_size=256):
        self.feature_size = feature_size
        super(IngredientSentenceSixLayers, self).__init__(
            train=train,
            alphabet_size=alphabet_size,
            out_feature=self.feature_size * 6,
            conv0=L.Convolution2D(alphabet_size,
                                  self.feature_size, (1, 7),
                                  stride=1,
                                  pad=0),
            conv1=L.Convolution2D(self.feature_size,
                                  self.feature_size, (1, 5),
                                  stride=1,
                                  pad=0),
            conv2=L.Convolution2D(self.feature_size,
                                  self.feature_size, (1, 1),
                                  stride=1,
                                  pad=0),
            conv3=L.Convolution2D(self.feature_size,
                                  self.feature_size, (1, 3),
                                  stride=1,
                                  pad=0),
            conv4=L.Convolution2D(self.feature_size,
                                  self.feature_size, (1, 1),
                                  stride=1,
                                  pad=0),
            conv5=L.Convolution2D(self.feature_size,
                                  self.feature_size, (1, 1),
                                  stride=1,
                                  pad=0),
        )

        # We want to use 1d max-pooling, such that ingredient information is not crossed and stays in tact.
        # The default Functions.max_pooling_nd() function will
        # always take the full input(-2) dimensions of the x input.
        #  In our case this would mean that it would be 2d. Therefore we have to use the class manually.
        # Since our dimensionality  be 4 : BATCH_SIZE X RECIPE_SIZE X TEXT_SIZE X FEATURE_SIZE
        self.maxp3 = F.MaxPooling2D(
            (1, 3))  # We will only use max pooling with kernel size 3
        self.maxp2 = F.MaxPooling2D(
            (1, 2))  # We will only use max pooling with kernel size 3
Ejemplo n.º 8
0
 def setUp(self):
     self.x = numpy.random.uniform(-1, 1, self.in_shape).astype(self.dtype)
     self.p = F.MaxPooling2D(2, 2)
     with chainer.using_config('use_cudnn', 'never'):
         self.pooled_y = self.p.apply((self.x, ))[0]
     self.gy = numpy.random.uniform(-1, 1, self.in_shape).astype(self.dtype)
     self.ggx = numpy.random.uniform(-1, 1,
                                     self.pooled_y.shape).astype(self.dtype)
     self.check_backward_options = {}
     self.check_double_backward_options = {'atol': 1e-3, 'rtol': 1e-2}
     if self.dtype == numpy.float16:
         self.check_double_backward_options = {'atol': 3e-3, 'rtol': 3e-2}
Ejemplo n.º 9
0
    def __init__(self,
                 in_ch=3,
                 mid_ch=0,
                 out_ch=13,
                 ksize=3,
                 stride=1,
                 pad=1,
                 dilation=1,
                 nobias=False,
                 symmetric=False,
                 drop_ratio=0.0,
                 downsample=False,
                 upsample=False,
                 p=None,
                 use_bn=True,
                 train=True):
        super(Block, self).__init__()
        k1, k2, s1 = self.calc_param(downsample, symmetric, upsample)
        self.p = p
        self.drop_ratio = drop_ratio
        self.downsample = downsample
        self.upsample = upsample
        self.train = train

        with self.init_scope():
            this_mod = sys.modules[__name__]
            conv_type = "ConvBN" if use_bn else "Conv"
            ConvBlock = getattr(this_mod, conv_type + "PReLU")
            self.conv1 = ConvBlock(in_ch, mid_ch, k1, s1, 0, nobias=True)

            conv_type2 = conv_type + "PReLU"
            conv_type2 = "Symmetric" + conv_type2 if symmetric else conv_type2
            ConvBlock = getattr(this_mod, conv_type2)
            self.conv2 = ConvBlock(mid_ch,
                                   mid_ch,
                                   k2,
                                   stride,
                                   pad,
                                   dilation,
                                   nobias=False,
                                   upsample=upsample)

            ConvBlock = getattr(this_mod, conv_type)
            self.conv3 = ConvBlock(mid_ch, out_ch, 1, 1, 0, nobias=True)
            self.prelu = L.PReLU()
            if downsample:
                ConvBlock = getattr(this_mod, conv_type)
                self.conv = ConvBlock(in_ch, out_ch, 1, 1, 0, nobias=True)
                self.p = F.MaxPooling2D(2, 2)
            if upsample:
                ConvBlock = getattr(this_mod, conv_type)
                self.conv = ConvBlock(in_ch, out_ch, 1, 1, 0, nobias=True)
Ejemplo n.º 10
0
 def __init__(self, train=False):
     super(VGG16, self).__init__()
     self.trunk = [
         ('conv1_1', L.Convolution2D(3, 64, 3, 1, 1)),
         ('relu1_1', F.ReLU()),
         ('conv1_2', L.Convolution2D(64, 64, 3, 1, 1)),
         ('relu1_2', F.ReLU()),
         ('pool1', F.MaxPooling2D(2, 2)),
         ('conv2_1', L.Convolution2D(64, 128, 3, 1, 1)),
         ('relu2_1', F.ReLU()),
         ('conv2_2', L.Convolution2D(128, 128, 3, 1, 1)),
         ('relu2_2', F.ReLU()),
         ('pool2', F.MaxPooling2D(2, 2)),
         ('conv3_1', L.Convolution2D(128, 256, 3, 1, 1)),
         ('relu3_1', F.ReLU()),
         ('conv3_2', L.Convolution2D(256, 256, 3, 1, 1)),
         ('relu3_2', F.ReLU()),
         ('conv3_3', L.Convolution2D(256, 256, 3, 1, 1)),
         ('relu3_3', F.ReLU()),
         ('pool3', F.MaxPooling2D(2, 2)),
         ('conv4_1', L.Convolution2D(256, 512, 3, 1, 1)),
         ('relu4_1', F.ReLU()),
         ('conv4_2', L.Convolution2D(512, 512, 3, 1, 1)),
         ('relu4_2', F.ReLU()),
         ('conv4_3', L.Convolution2D(512, 512, 3, 1, 1)),
         ('relu4_3', F.ReLU()),
         ('pool4', F.MaxPooling2D(2, 2)),
         ('conv5_1', L.Convolution2D(512, 512, 3, 1, 1)),
         ('relu5_1', F.ReLU()),
         ('conv5_2', L.Convolution2D(512, 512, 3, 1, 1)),
         ('relu5_2', F.ReLU()),
         ('conv5_3', L.Convolution2D(512, 512, 3, 1, 1)),
         ('relu5_3', F.ReLU()),
         ('rpn_conv_3x3', L.Convolution2D(512, 512, 3, 1, 1)),
         ('rpn_relu_3x3', F.ReLU()),
     ]
     for name, link in self.trunk:
         if 'conv' in name:
             self.add_link(name, link)
Ejemplo n.º 11
0
 def __init__(self, train=False):
     super(VGG16Prev, self).__init__()
     self.trunk = [
         ('conv1_1', L.Convolution2D(3, 64, 3, 1, 1)),
         ('_relu1_1', F.ReLU()),
         ('conv1_2', L.Convolution2D(64, 64, 3, 1, 1)),
         ('_relu1_2', F.ReLU()),
         ('_pool1', F.MaxPooling2D(2, 2)),
         ('conv2_1', L.Convolution2D(64, 128, 3, 1, 1)),
         ('_relu2_1', F.ReLU()),
         ('conv2_2', L.Convolution2D(128, 128, 3, 1, 1)),
         ('_relu2_2', F.ReLU()),
         ('_pool2', F.MaxPooling2D(2, 2)),
         ('conv3_1', L.Convolution2D(128, 256, 3, 1, 1)),
         ('_relu3_1', F.ReLU()),
         ('conv3_2', L.Convolution2D(256, 256, 3, 1, 1)),
         ('_relu3_2', F.ReLU()),
         ('conv3_3', L.Convolution2D(256, 256, 3, 1, 1)),
         ('_relu3_3', F.ReLU()),
         ('_pool3', F.MaxPooling2D(2, 2)),
         ('conv4_1', L.Convolution2D(256, 512, 3, 1, 1)),
         ('_relu4_1', F.ReLU()),
         ('conv4_2', L.Convolution2D(512, 512, 3, 1, 1)),
         ('_relu4_2', F.ReLU()),
         ('conv4_3', L.Convolution2D(512, 512, 3, 1, 1)),
         ('_relu4_3', F.ReLU()),
         ('_pool4', F.MaxPooling2D(2, 2)),
         ('conv5_1', L.Convolution2D(512, 512, 3, 1, 1)),
         ('_relu5_1', F.ReLU()),
         ('conv5_2', L.Convolution2D(512, 512, 3, 1, 1)),
         ('_relu5_2', F.ReLU()),
         ('conv5_3', L.Convolution2D(512, 512, 3, 1, 1)),
         ('_relu5_3', F.ReLU()),
     ]
     for name, link in self.trunk:
         if not name.startswith('_'):
             self.add_link(name, link)
Ejemplo n.º 12
0
    def __init__(self, arch, dr=0.5, bn=False):
        super(discriminator, self).__init__()

        self.dr = dr
        self.train = True
        self.best_score = collections.defaultdict(lambda: 0)
        self.functions = collections.OrderedDict()

        for l in arch['0']:
            name = l['name']

            if reg_conv.match(name):
                self.add_link(
                    name,
                    L.Convolution2D(l['in_channel'],
                                    l['out_channel'],
                                    l['ksize'],
                                    pad=l['pad']))
                if bn:
                    m = reg_num.search(name)
                    bn_name = 'cbn_layer{}'.format(m.group())
                    self.add_link(bn_name,
                                  L.BatchNormalization(l['out_channel']))
                    self.functions[name] = [self[name], self[bn_name], F.relu]
                else:
                    self.functions[name] = [self[name], F.relu]

            if reg_pool.match(name):
                self.functions[name] = [
                    F.MaxPooling2D(l['ksize'], l['stride'])
                ]

            if reg_fc.match(name):
                self.add_link(name, L.Linear(l['in_size'], l['out_size']))
                if bn:
                    m = reg_num.search(name)
                    bn_name = 'fbn_layer{}'.format(m.group())
                    self.add_link(bn_name, L.BatchNormalization(l['out_size']))
                    self.functions[name] = [
                        self[name], self[bn_name], F.relu, F.dropout
                    ]
                else:
                    self.functions[name] = [self[name], F.relu, F.dropout]

            if name == 'out_layer':
                self.add_link(name, L.Linear(l['in_size'], l['out_size']))
                self.functions[name] = [self[name]]

        self.functions['prob'] = [F.softmax]
Ejemplo n.º 13
0
    def check_backward_consistency_regression(self,
                                              x_data,
                                              gy_data,
                                              use_cudnn=True):
        # Regression test to two-dimensional max pooling layer.

        if len(self.dims) != 2:
            return

        ksize = self.ksize
        stride = self.stride
        pad = self.pad
        xp = cuda.get_array_module(x_data)

        # Backward computation for N-dimensional max pooling layer.
        x_nd = chainer.Variable(xp.array(x_data))
        func_nd = functions.MaxPoolingND(self.ndim,
                                         ksize,
                                         stride=stride,
                                         pad=pad,
                                         use_cudnn=use_cudnn,
                                         cover_all=self.cover_all)
        y_nd = func_nd(x_nd)
        y_nd.grad = gy_data
        y_nd.backward()

        # Backward computation for two-dimensional max pooling layer.
        x_2d = chainer.Variable(xp.array(x_data))
        func_2d = functions.MaxPooling2D(ksize,
                                         stride=stride,
                                         pad=pad,
                                         use_cudnn=use_cudnn,
                                         cover_all=self.cover_all)
        y_2d = func_2d(x_2d)
        y_2d.grad = gy_data
        y_2d.backward()

        # Test that the two result gradients are close enough.
        testing.assert_allclose(x_nd.grad, x_2d.grad)
 def __init__(self):
     super().__init__(
         conv=L.Convolution2D(3, 32, 3, stride=2, pad=0),
         conv_1=L.Convolution2D(32, 32, 3, stride=1, pad=0),
         conv_2=L.Convolution2D(32, 64, 3, stride=1, pad=1),
         conv_3=L.Convolution2D(64, 80, 1, stride=1, pad=0),
         conv_4=L.Convolution2D(80, 192, 3, stride=1, pad=0),
         bn_conv=L.BatchNormalization(32),
         bn_conv_1=L.BatchNormalization(32),
         bn_conv_2=L.BatchNormalization(64),
         bn_conv_3=L.BatchNormalization(80),
         bn_conv_4=L.BatchNormalization(192),
         mixed=Mixed([
             ('conv',
              Tower([('conv', L.Convolution2D(192, 64, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(64)),
                     ('_relu', F.ReLU())])),
             ('tower',
              Tower([('conv', L.Convolution2D(192, 48, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(48)),
                     ('_relu', F.ReLU()),
                     ('conv_1', L.Convolution2D(48, 64, 5, stride=1,
                                                pad=2)),
                     ('bn_conv_1', L.BatchNormalization(64)),
                     ('_relu_1', F.ReLU())])),
             ('tower_1',
              Tower([('conv', L.Convolution2D(192, 64, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(64)),
                     ('_relu', F.ReLU()),
                     ('conv_1', L.Convolution2D(64, 96, 3, stride=1,
                                                pad=1)),
                     ('bn_conv_1', L.BatchNormalization(96)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2', L.Convolution2D(96, 96, 3, stride=1,
                                                pad=1)),
                     ('bn_conv_2', L.BatchNormalization(96)),
                     ('_relu_2', F.ReLU())])),
             ('tower_2',
              Tower([('_pooling', F.AveragePooling2D(3, 1, pad=1)),
                     ('conv', L.Convolution2D(192, 32, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(32)),
                     ('_relu', F.ReLU())]))
         ]),
         mixed_1=Mixed([
             ('conv',
              Tower([('conv', L.Convolution2D(256, 64, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(64)),
                     ('_relu', F.ReLU())])),
             ('tower',
              Tower([('conv', L.Convolution2D(256, 48, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(48)),
                     ('_relu', F.ReLU()),
                     ('conv_1', L.Convolution2D(48, 64, 5, stride=1,
                                                pad=2)),
                     ('bn_conv_1', L.BatchNormalization(64)),
                     ('_relu_1', F.ReLU())])),
             ('tower_1',
              Tower([('conv', L.Convolution2D(256, 64, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(64)),
                     ('_relu', F.ReLU()),
                     ('conv_1', L.Convolution2D(64, 96, 3, stride=1,
                                                pad=1)),
                     ('bn_conv_1', L.BatchNormalization(96)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2', L.Convolution2D(96, 96, 3, stride=1,
                                                pad=1)),
                     ('bn_conv_2', L.BatchNormalization(96)),
                     ('_relu_2', F.ReLU())])),
             ('tower_2',
              Tower([('_pooling', F.AveragePooling2D(3, 1, pad=1)),
                     ('conv', L.Convolution2D(256, 64, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(64)),
                     ('_relu', F.ReLU())]))
         ]),
         mixed_2=Mixed([
             ('conv',
              Tower([('conv', L.Convolution2D(288, 64, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(64)),
                     ('_relu', F.ReLU())])),
             ('tower',
              Tower([('conv', L.Convolution2D(288, 48, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(48)),
                     ('_relu', F.ReLU()),
                     ('conv_1', L.Convolution2D(48, 64, 5, stride=1,
                                                pad=2)),
                     ('bn_conv_1', L.BatchNormalization(64)),
                     ('_relu_1', F.ReLU())])),
             ('tower_1',
              Tower([('conv', L.Convolution2D(288, 64, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(64)),
                     ('_relu', F.ReLU()),
                     ('conv_1', L.Convolution2D(64, 96, 3, stride=1,
                                                pad=1)),
                     ('bn_conv_1', L.BatchNormalization(96)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2', L.Convolution2D(96, 96, 3, stride=1,
                                                pad=1)),
                     ('bn_conv_2', L.BatchNormalization(96)),
                     ('_relu_2', F.ReLU())])),
             ('tower_2',
              Tower([('_pooling', F.AveragePooling2D(3, 1, pad=1)),
                     ('conv', L.Convolution2D(288, 64, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(64)),
                     ('_relu', F.ReLU())]))
         ]),
         mixed_3=Mixed([
             ('conv',
              Tower([('conv', L.Convolution2D(288, 384, 3, stride=2,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(384)),
                     ('_relu', F.ReLU())])),
             ('tower',
              Tower([('conv', L.Convolution2D(288, 64, 1, stride=1, pad=0)),
                     ('bn_conv', L.BatchNormalization(64)),
                     ('_relu', F.ReLU()),
                     ('conv_1', L.Convolution2D(64, 96, 3, stride=1,
                                                pad=1)),
                     ('bn_conv_1', L.BatchNormalization(96)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2', L.Convolution2D(96, 96, 3, stride=2,
                                                pad=0)),
                     ('bn_conv_2', L.BatchNormalization(96)),
                     ('_relu_2', F.ReLU())])),
             ('pool', Tower([('_pooling', F.MaxPooling2D(3, 2, pad=0))]))
         ]),
         mixed_4=Mixed([
             ('conv',
              Tower([('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU())])),
             ('tower',
              Tower([('conv', L.Convolution2D(768, 128, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(128)),
                     ('_relu', F.ReLU()),
                     ('conv_1',
                      L.Convolution2D(128,
                                      128, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_1', L.BatchNormalization(128)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2',
                      L.Convolution2D(128,
                                      192, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_2', L.BatchNormalization(192)),
                     ('_relu_2', F.ReLU())])),
             ('tower_1',
              Tower([('conv', L.Convolution2D(768, 128, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(128)),
                     ('_relu', F.ReLU()),
                     ('conv_1',
                      L.Convolution2D(128,
                                      128, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_1', L.BatchNormalization(128)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2',
                      L.Convolution2D(128,
                                      128, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_2', L.BatchNormalization(128)),
                     ('_relu_2', F.ReLU()),
                     ('conv_3',
                      L.Convolution2D(128,
                                      128, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_3', L.BatchNormalization(128)),
                     ('_relu_3', F.ReLU()),
                     ('conv_4',
                      L.Convolution2D(128,
                                      192, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_4', L.BatchNormalization(192)),
                     ('_relu_4', F.ReLU())])),
             ('tower_2',
              Tower([('_pooling', F.AveragePooling2D(3, 1, pad=1)),
                     ('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU())]))
         ]),
         mixed_5=Mixed([
             ('conv',
              Tower([('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU())])),
             ('tower',
              Tower([('conv', L.Convolution2D(768, 160, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(160)),
                     ('_relu', F.ReLU()),
                     ('conv_1',
                      L.Convolution2D(160,
                                      160, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_1', L.BatchNormalization(160)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2',
                      L.Convolution2D(160,
                                      192, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_2', L.BatchNormalization(192)),
                     ('_relu_2', F.ReLU())])),
             ('tower_1',
              Tower([('conv', L.Convolution2D(768, 160, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(160)),
                     ('_relu', F.ReLU()),
                     ('conv_1',
                      L.Convolution2D(160,
                                      160, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_1', L.BatchNormalization(160)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2',
                      L.Convolution2D(160,
                                      160, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_2', L.BatchNormalization(160)),
                     ('_relu_2', F.ReLU()),
                     ('conv_3',
                      L.Convolution2D(160,
                                      160, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_3', L.BatchNormalization(160)),
                     ('_relu_3', F.ReLU()),
                     ('conv_4',
                      L.Convolution2D(160,
                                      192, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_4', L.BatchNormalization(192)),
                     ('_relu_4', F.ReLU())])),
             ('tower_2',
              Tower([('_pooling', F.AveragePooling2D(3, 1, pad=1)),
                     ('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU())]))
         ]),
         mixed_6=Mixed([
             ('conv',
              Tower([('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU())])),
             ('tower',
              Tower([('conv', L.Convolution2D(768, 160, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(160)),
                     ('_relu', F.ReLU()),
                     ('conv_1',
                      L.Convolution2D(160,
                                      160, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_1', L.BatchNormalization(160)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2',
                      L.Convolution2D(160,
                                      192, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_2', L.BatchNormalization(192)),
                     ('_relu_2', F.ReLU())])),
             ('tower_1',
              Tower([('conv', L.Convolution2D(768, 160, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(160)),
                     ('_relu', F.ReLU()),
                     ('conv_1',
                      L.Convolution2D(160,
                                      160, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_1', L.BatchNormalization(160)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2',
                      L.Convolution2D(160,
                                      160, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_2', L.BatchNormalization(160)),
                     ('_relu_2', F.ReLU()),
                     ('conv_3',
                      L.Convolution2D(160,
                                      160, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_3', L.BatchNormalization(160)),
                     ('_relu_3', F.ReLU()),
                     ('conv_4',
                      L.Convolution2D(160,
                                      192, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_4', L.BatchNormalization(192)),
                     ('_relu_4', F.ReLU())])),
             ('tower_2',
              Tower([('_pooling', F.AveragePooling2D(3, 1, pad=1)),
                     ('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU())]))
         ]),
         mixed_7=Mixed([
             ('conv',
              Tower([('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU())])),
             ('tower',
              Tower([('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU()),
                     ('conv_1',
                      L.Convolution2D(192,
                                      192, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_1', L.BatchNormalization(192)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2',
                      L.Convolution2D(192,
                                      192, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_2', L.BatchNormalization(192)),
                     ('_relu_2', F.ReLU())])),
             ('tower_1',
              Tower([('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU()),
                     ('conv_1',
                      L.Convolution2D(192,
                                      192, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_1', L.BatchNormalization(192)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2',
                      L.Convolution2D(192,
                                      192, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_2', L.BatchNormalization(192)),
                     ('_relu_2', F.ReLU()),
                     ('conv_3',
                      L.Convolution2D(192,
                                      192, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_3', L.BatchNormalization(192)),
                     ('_relu_3', F.ReLU()),
                     ('conv_4',
                      L.Convolution2D(192,
                                      192, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_4', L.BatchNormalization(192)),
                     ('_relu_4', F.ReLU())])),
             ('tower_2',
              Tower([('_pooling', F.AveragePooling2D(3, 1, pad=1)),
                     ('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU())]))
         ]),
         mixed_8=Mixed([
             ('tower',
              Tower([('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU()),
                     ('conv_1', L.Convolution2D(192,
                                                320,
                                                3,
                                                stride=2,
                                                pad=0)),
                     ('bn_conv_1', L.BatchNormalization(320)),
                     ('_relu_1', F.ReLU())])),
             ('tower_1',
              Tower([('conv', L.Convolution2D(768, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU()),
                     ('conv_1',
                      L.Convolution2D(192,
                                      192, (1, 7),
                                      stride=1,
                                      pad=(0, 3))),
                     ('bn_conv_1', L.BatchNormalization(192)),
                     ('_relu_1', F.ReLU()),
                     ('conv_2',
                      L.Convolution2D(192,
                                      192, (7, 1),
                                      stride=1,
                                      pad=(3, 0))),
                     ('bn_conv_2', L.BatchNormalization(192)),
                     ('_relu_2', F.ReLU()),
                     ('conv_3', L.Convolution2D(192,
                                                192,
                                                3,
                                                stride=2,
                                                pad=0)),
                     ('bn_conv_3', L.BatchNormalization(192)),
                     ('_relu_3', F.ReLU())])),
             ('pool', Tower([('_pooling', F.MaxPooling2D(3, 2, pad=0))]))
         ]),
         mixed_9=Mixed([
             ('conv',
              Tower([
                  ('conv', L.Convolution2D(1280, 320, 1, stride=1, pad=0)),
                  ('bn_conv', L.BatchNormalization(320)),
                  ('_relu', F.ReLU()),
              ])),
             ('tower',
              Tower([('conv', L.Convolution2D(1280, 384, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(384)),
                     ('_relu', F.ReLU()),
                     ('mixed',
                      Mixed([('conv',
                              Tower([
                                  ('conv',
                                   L.Convolution2D(384,
                                                   384, (1, 3),
                                                   stride=1,
                                                   pad=(0, 1))),
                                  ('bn_conv', L.BatchNormalization(384)),
                                  ('_relu', F.ReLU()),
                              ])),
                             ('conv_1',
                              Tower([
                                  ('conv_1',
                                   L.Convolution2D(384,
                                                   384, (3, 1),
                                                   stride=1,
                                                   pad=(1, 0))),
                                  ('bn_conv_1', L.BatchNormalization(384)),
                                  ('_relu_1', F.ReLU()),
                              ]))]))])),
             ('tower_1',
              Tower([('conv', L.Convolution2D(1280, 448, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(448)),
                     ('_relu', F.ReLU()),
                     ('conv_1', L.Convolution2D(448,
                                                384,
                                                3,
                                                stride=1,
                                                pad=1)),
                     ('bn_conv_1', L.BatchNormalization(384)),
                     ('_relu_1', F.ReLU()),
                     ('mixed',
                      Mixed([('conv',
                              Tower([
                                  ('conv',
                                   L.Convolution2D(384,
                                                   384, (1, 3),
                                                   stride=1,
                                                   pad=(0, 1))),
                                  ('bn_conv', L.BatchNormalization(384)),
                                  ('_relu', F.ReLU()),
                              ])),
                             ('conv_1',
                              Tower([
                                  ('conv_1',
                                   L.Convolution2D(384,
                                                   384, (3, 1),
                                                   stride=1,
                                                   pad=(1, 0))),
                                  ('bn_conv_1', L.BatchNormalization(384)),
                                  ('_relu_1', F.ReLU()),
                              ]))]))])),
             ('tower_2',
              Tower([('_pooling', F.AveragePooling2D(3, 1, pad=1)),
                     ('conv', L.Convolution2D(1280, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU())]))
         ]),
         mixed_10=Mixed([
             ('conv',
              Tower([
                  ('conv', L.Convolution2D(2048, 320, 1, stride=1, pad=0)),
                  ('bn_conv', L.BatchNormalization(320)),
                  ('_relu', F.ReLU()),
              ])),
             ('tower',
              Tower([('conv', L.Convolution2D(2048, 384, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(384)),
                     ('_relu', F.ReLU()),
                     ('mixed',
                      Mixed([('conv',
                              Tower([
                                  ('conv',
                                   L.Convolution2D(384,
                                                   384, (1, 3),
                                                   stride=1,
                                                   pad=(0, 1))),
                                  ('bn_conv', L.BatchNormalization(384)),
                                  ('_relu', F.ReLU()),
                              ])),
                             ('conv_1',
                              Tower([
                                  ('conv_1',
                                   L.Convolution2D(384,
                                                   384, (3, 1),
                                                   stride=1,
                                                   pad=(1, 0))),
                                  ('bn_conv_1', L.BatchNormalization(384)),
                                  ('_relu_1', F.ReLU()),
                              ]))]))])),
             ('tower_1',
              Tower([('conv', L.Convolution2D(2048, 448, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(448)),
                     ('_relu', F.ReLU()),
                     ('conv_1', L.Convolution2D(448,
                                                384,
                                                3,
                                                stride=1,
                                                pad=1)),
                     ('bn_conv_1', L.BatchNormalization(384)),
                     ('_relu_1', F.ReLU()),
                     ('mixed',
                      Mixed([('conv',
                              Tower([('conv',
                                      L.Convolution2D(384,
                                                      384, (1, 3),
                                                      stride=1,
                                                      pad=(0, 1))),
                                     ('bn_conv', L.BatchNormalization(384)),
                                     ('_relu', F.ReLU())])),
                             ('conv_1',
                              Tower([('conv_1',
                                      L.Convolution2D(384,
                                                      384, (3, 1),
                                                      stride=1,
                                                      pad=(1, 0))),
                                     ('bn_conv_1',
                                      L.BatchNormalization(384)),
                                     ('_relu_1', F.ReLU())]))]))])),
             ('tower_2',
              Tower([('_pooling', F.MaxPooling2D(3, 1, pad=1)),
                     ('conv', L.Convolution2D(2048, 192, 1, stride=1,
                                              pad=0)),
                     ('bn_conv', L.BatchNormalization(192)),
                     ('_relu', F.ReLU())]))
         ]),
         logit=L.Linear(2048, 1008))
 def setUp(self):
     self.x = numpy.random.uniform(-1, 1, self.in_shape).astype('f')
     self.p = F.MaxPooling2D(2, 2, use_cudnn=False)
     self.pooled_y = self.p(self.x)
     self.gy = numpy.random.uniform(
         -1, 1, self.in_shape).astype(numpy.float32)
Ejemplo n.º 16
0
 def setUp(self):
     self.x = numpy.random.uniform(-1, 1, self.in_shape).astype(self.dtype)
     self.p = F.MaxPooling2D(2, 2)
     with chainer.using_config('use_cudnn', 'never'):
         self.pooled_y = self.p(self.x)
     self.gy = numpy.random.uniform(-1, 1, self.in_shape).astype(self.dtype)
Ejemplo n.º 17
0
import chainer.functions as F
import numpy as np
import time

# input = [   [ 1, 2, 3 ],
#            [ 4, 5, 6 ],
#            [ 7, 8, 9 ] ]

repeat = 1
(n, c, h, w) = (64, 256, 256, 256)
x = np.ones((n, c, h, w), dtype=np.float32)
f = F.MaxPooling2D(3, stride=2, pad=0, use_cudnn=False)

x = x,
y = f.forward_cpu(x)
start = time.time()
for i in range(repeat):
    y = f.forward_cpu(x)
print("average forward %f seconds" % ((time.time() - start) / repeat))

gy = np.ndarray(y[0].shape, dtype=np.float32)
gy.fill(0.001)
gy = gy,
gx = f.backward_cpu(x, gy)
start = time.time()
for i in range(repeat):
    gx = f.backward_cpu(x, gy)
print("average backward %f seconds" % ((time.time() - start) / repeat))
Ejemplo n.º 18
0
 def __init__(self,
              cgp,
              n_class,
              lossfun=softmax_cross_entropy.softmax_cross_entropy,
              accfun=accuracy.accuracy):
     super(CGP2CNN, self).__init__()
     self.cgp = cgp
     self.pool_size = 2
     initializer = chainer.initializers.HeNormal()
     links = []
     i = 1
     for name, in1, in2 in self.cgp:
         if name == 'pool_max':
             links += [('_' + name + '_' + str(i),
                        F.MaxPooling2D(self.pool_size, self.pool_size, 0,
                                       False))]
         elif name == 'pool_ave':
             links += [('_' + name + '_' + str(i),
                        F.AveragePooling2D(self.pool_size, self.pool_size,
                                           0, False))]
         elif name == 'concat':
             links += [('_' + name + '_' + str(i), F.Concat())]
         elif name == 'sum':
             links += [('_' + name + '_' + str(i), F.Concat())
                       ]  # the F.Concat() is dummy
         elif name == 'ConvBlock32_3':
             links += [(name + '_' + str(i), ConvBlock(3, 32, initializer))]
         elif name == 'ConvBlock32_5':
             links += [(name + '_' + str(i), ConvBlock(5, 32, initializer))]
         elif name == 'ConvBlock32_7':
             links += [(name + '_' + str(i), ConvBlock(7, 32, initializer))]
         elif name == 'ConvBlock64_3':
             links += [(name + '_' + str(i), ConvBlock(3, 64, initializer))]
         elif name == 'ConvBlock64_5':
             links += [(name + '_' + str(i), ConvBlock(5, 64, initializer))]
         elif name == 'ConvBlock64_7':
             links += [(name + '_' + str(i), ConvBlock(7, 64, initializer))]
         elif name == 'ConvBlock128_3':
             links += [(name + '_' + str(i), ConvBlock(3, 128,
                                                       initializer))]
         elif name == 'ConvBlock128_5':
             links += [(name + '_' + str(i), ConvBlock(5, 128,
                                                       initializer))]
         elif name == 'ConvBlock128_7':
             links += [(name + '_' + str(i), ConvBlock(7, 128,
                                                       initializer))]
         elif name == 'ResBlock32_3':
             links += [(name + '_' + str(i), ResBlock(3, 32, initializer))]
         elif name == 'ResBlock32_5':
             links += [(name + '_' + str(i), ResBlock(5, 32, initializer))]
         elif name == 'ResBlock32_7':
             links += [(name + '_' + str(i), ResBlock(7, 32, initializer))]
         elif name == 'ResBlock64_3':
             links += [(name + '_' + str(i), ResBlock(3, 64, initializer))]
         elif name == 'ResBlock64_5':
             links += [(name + '_' + str(i), ResBlock(5, 64, initializer))]
         elif name == 'ResBlock64_7':
             links += [(name + '_' + str(i), ResBlock(7, 64, initializer))]
         elif name == 'ResBlock128_3':
             links += [(name + '_' + str(i), ResBlock(3, 128, initializer))]
         elif name == 'ResBlock128_5':
             links += [(name + '_' + str(i), ResBlock(5, 128, initializer))]
         elif name == 'ResBlock128_7':
             links += [(name + '_' + str(i), ResBlock(7, 128, initializer))]
         elif name == 'full':
             links += [(name + '_' + str(i),
                        L.Linear(None, n_class, initialW=initializer))]
         i += 1
     for link in links:
         if not link[0].startswith('_'):
             self.add_link(*link)
     self.forward = links
     self.train = True
     self.lossfun = lossfun
     self.accfun = accfun
     self.loss = None
     self.accuracy = None
     self.outputs = [None for _ in range(len(self.cgp))]
     self.param_num = 0