Beispiel #1
0
    def forward(self, x):
        hs = list(self.base(x))

        # TODO: change name
        # calculate P6, P7, ...
        _hs = []
        h = hs[-1]
        for i, conv in enumerate(self.convs):
            if i == 0:
                h = conv(h)
            else:
                h = conv(F.relu(h))
            _hs.append(h)

        for i in reversed(range(len(hs))):
            hs[i] = self.inner[i](hs[i])
            if i + 1 < len(hs):
                hs[i] += F.unpooling_2d(hs[i + 1], 2, cover_all=False)

        for i in range(len(hs)):
            hs[i] = self.outer[i](hs[i])
        # append P6, P7, ... to [P3, P4, P5]
        hs += _hs

        return hs
Beispiel #2
0
    def check_backward_consistency_regression(self, backend_config):
        # Regression test to two-dimensional unpooling layer.

        x_data, = self.generate_inputs()
        gy_data = numpy.random.uniform(-1, 1, self.gy_shape).astype(self.dtype)

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

        # Backward computation for N-dimensional unpooling layer.
        x_nd = chainer.Variable(xp.array(x_data))
        y_nd = functions.unpooling_nd(
            x_nd, ksize, stride=stride, pad=pad, cover_all=self.cover_all)
        y_nd.grad = gy_data
        y_nd.backward()

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

        # Test that the two result gradients are close enough.
        opt = self.check_backward_options
        testing.assert_allclose(
            x_nd.grad, x_2d.grad, atol=opt['atol'], rtol=opt['rtol'])
Beispiel #3
0
    def check_backward_consistency_regression(self, x_data, gy_data):
        # Regression test to two-dimensional unpooling layer.

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

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

        # Backward computation for N-dimensional unpooling layer.
        x_nd = chainer.Variable(xp.array(x_data))
        y_nd = functions.unpooling_nd(
            x_nd, ksize, stride=stride, pad=pad, cover_all=self.cover_all)
        y_nd.grad = gy_data
        y_nd.backward()

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

        # Test that the two result gradients are close enough.
        opt = self.check_backward_options
        testing.assert_allclose(
            x_nd.grad, x_2d.grad, atol=opt['atol'], rtol=opt['rtol'])
Beispiel #4
0
    def straight(self, x):
        h = F.relu(self.scbn0(x))
        h = F.unpooling_2d(h, 2, 2, 0, cover_all=False)
        h = self.c0(h)
        h = self.c1(F.relu(self.scbn1(h)))

        return h
Beispiel #5
0
    def check_forward(self, x_data):
        x = chainer.Variable(x_data)
        y = functions.unpooling_2d(x, self.ksize, outsize=self.outsize,
                                   cover_all=self.cover_all)
        self.assertEqual(y.data.dtype, self.dtype)
        y_data = cuda.to_cpu(y.data)

        self.assertEqual(self.gy.shape, y_data.shape)
        for i in six.moves.range(self.N):
            for c in six.moves.range(self.n_channels):
                outsize = self.outsize or self.expected_outsize
                assert y_data.shape[2:] == outsize
                if outsize == (5, 2):
                    expect = numpy.zeros(outsize, dtype=self.dtype)
                    expect[:2, :] = self.x[i, c, 0, 0]
                    expect[2:4, :] = self.x[i, c, 1, 0]
                elif outsize == (4, 2):
                    expect = numpy.array([
                        [self.x[i, c, 0, 0], self.x[i, c, 0, 0]],
                        [self.x[i, c, 0, 0], self.x[i, c, 0, 0]],
                        [self.x[i, c, 1, 0], self.x[i, c, 1, 0]],
                        [self.x[i, c, 1, 0], self.x[i, c, 1, 0]],
                    ])
                elif outsize == (3, 1):
                    expect = numpy.array([
                        [self.x[i, c, 0, 0]],
                        [self.x[i, c, 0, 0]],
                        [self.x[i, c, 1, 0]],
                    ])
                else:
                    raise ValueError('Unsupported outsize: {}'.format(outsize))
                testing.assert_allclose(expect, y_data[i, c])
    def check_forward(self, x_data):
        x = chainer.Variable(x_data)
        y = functions.unpooling_2d(x,
                                   self.ksize,
                                   outsize=self.outsize,
                                   cover_all=self.cover_all)
        self.assertEqual(y.data.dtype, numpy.float32)
        y_data = cuda.to_cpu(y.data)

        self.assertEqual(self.gy.shape, y_data.shape)
        for i in six.moves.range(self.N):
            for c in six.moves.range(self.n_channels):
                outsize = self.outsize or self.expected_outsize
                assert y_data.shape[2:] == outsize
                if outsize == (5, 2):
                    expect = numpy.zeros(outsize, dtype=numpy.float32)
                    expect[:2, :] = self.x[i, c, 0, 0]
                    expect[2:4, :] = self.x[i, c, 1, 0]
                elif outsize == (4, 2):
                    expect = numpy.array([
                        [self.x[i, c, 0, 0], self.x[i, c, 0, 0]],
                        [self.x[i, c, 0, 0], self.x[i, c, 0, 0]],
                        [self.x[i, c, 1, 0], self.x[i, c, 1, 0]],
                        [self.x[i, c, 1, 0], self.x[i, c, 1, 0]],
                    ])
                elif outsize == (3, 1):
                    expect = numpy.array([
                        [self.x[i, c, 0, 0]],
                        [self.x[i, c, 0, 0]],
                        [self.x[i, c, 1, 0]],
                    ])
                else:
                    raise ValueError('Unsupported outsize: {}'.format(outsize))
                gradient_check.assert_allclose(expect, y_data[i, c])
Beispiel #7
0
 def __call__(self, x, last=False):
     l1 = F.unpooling_2d(x, 2, 2, outsize=self.outsize)
     l2 = F.leaky_relu(self.c1(l1))
     l3 = F.leaky_relu(self.c2(l2))
     if last:
         return self.toRGB(l3)
     return l3
Beispiel #8
0
    def __call__(self, x):
        for nth in range(self.layers):
            if getattr(self, 'P' + str(nth)) is None:
                setattr(self, 'P' + str(nth), variable.Variable(
                    self.xp.zeros(self.sizes[nth], dtype=x.data.dtype),
                    volatile='auto'))

        E = [None] * self.layers
        for nth in range(self.layers):
            if nth == 0:
                E[nth] = F.concat((F.relu(x - getattr(self, 'P' + str(nth))),
                                  F.relu(getattr(self, 'P' + str(nth)) - x)))
            else:
                A = F.max_pooling_2d(F.relu(getattr(self, 'ConvA' + str(nth))(E[nth - 1])), 2, stride = 2)
                E[nth] = F.concat((F.relu(A - getattr(self, 'P' + str(nth))),
                                  F.relu(getattr(self, 'P' + str(nth)) - A)))

        R = [None] * self.layers
        for nth in reversed(range(self.layers)):
            if nth == self.layers - 1:
                R[nth] = getattr(self, self.rnn_module + str(nth))((E[nth],))
            else:
                upR = F.unpooling_2d(R[nth + 1], 2, stride = 2, cover_all=False)
                R[nth] = getattr(self, self.rnn_module + str(nth))((E[nth], upR))

            if nth == 0:
                setattr(self, 'P' + str(nth), F.clipped_relu(getattr(self, 'ConvP' + str(nth))(R[nth]), 1.0))
            else:
                setattr(self, 'P' + str(nth), F.relu(getattr(self, 'ConvP' + str(nth))(R[nth])))
        
        return self.P0
Beispiel #9
0
    def __call__(self, x):
        h = x
        if self.activation is not None:
            h = self.activation(self.c0(h))
        if self.sample == "up":
            h = F.unpooling_2d(h, 2, 2, 0, cover_all=False)

        return h
Beispiel #10
0
 def __init__(self, function, inputs, outputs):
     self.f = lambda x: F.unpooling_2d(
         x,
         ksize=(function.params['kh'], function.params['kw']),
         stride=(function.params['sy'], function.params['sx']),
         pad=(function.params['ph'], function.params['pw']),
         outsize=(function.params['outh'], function.params['outw']),
         cover_all=function.params['cover_all'])
Beispiel #11
0
    def __call__(self, z, stage):
        # stage0: c0->c1->out0
        # stage1: c0->c1-> (1-a)*(up->out0) + (a)*(b1->out1)
        # stage2: c0->c1->b1->out1
        # stage3: c0->c1->b1-> (1-a)*(up->out1) + (a)*(b2->out2)
        # stage4: c0->c1->b2->out2
        # ...
        #print(np.prod(self.c0.c.b.data.shape))

        stage = min(stage, self.max_stage)
        alpha = stage - math.floor(stage)
        stage = math.floor(stage)

        h = F.reshape(z, (len(z), self.n_hidden, 1, 1))
        h = feature_vector_normalization(F.leaky_relu(self.c0(h)))
        h = feature_vector_normalization(F.leaky_relu(self.c1(h)))

        for i in range(1, int(stage // 2 + 1)):
            h = getattr(self, "b%d" % i)(h)

        if int(stage) % 2 == 0:
            out = getattr(self, "out%d" % (stage // 2))
            x = out(h)
        else:
            out_prev = getattr(self, "out%d" % (stage // 2))
            out_curr = getattr(self, "out%d" % (stage // 2 + 1))
            b_curr = getattr(self, "b%d" % (stage // 2 + 1))

            x_0 = out_prev(
                F.unpooling_2d(h,
                               2,
                               2,
                               0,
                               outsize=(2 * h.shape[2], 2 * h.shape[3])))
            x_1 = out_curr(b_curr(h))
            x = (1.0 - alpha) * x_0 + alpha * x_1

        if chainer.configuration.config.train:
            return x
        else:
            scale = int(self.out_width // x.data.shape[2])
            return F.unpooling_2d(x,
                                  scale,
                                  scale,
                                  0,
                                  outsize=(self.out_width, self.out_height))
Beispiel #12
0
    def __call__(self, x, c):
        h = self.c0(F.relu(self.spade0(x, c)))
        h = self.c1(F.relu(self.spade1(h, c)))
        h_sc = self.c_sc(F.relu(self.spade_sc(x, c)))
        h = h + h_sc
        h = F.unpooling_2d(h, 2, 2, 0, cover_all=False)

        return h
Beispiel #13
0
    def __call__(self, x):
        h = F.unpooling_2d(x, 2, 2, 0, cover_all=False)
        if self.bn:
            h = self.activ(self.bn0(self.c0(h)))
        else:
            h = self.activ(self.c0(h))

        return h
Beispiel #14
0
 def forward(self, x, y):
     x = F.concat((F.unpooling_2d(x, 2, cover_all=False), y), axis=1)
     for d in range(NW_DEPTHS[2]):
         x = self[d](x)
         x = F.leaky_relu(x, slope=LRELU_SLOPE)
     if NW_USE_RES:
         x = x + y
     return x
Beispiel #15
0
 def __call__(self, x):
     if self.sample in ['down', 'none', 'none-7', 'deconv']:
         h = self.c(x)
     elif self.sample == 'unpool':
         h = F.unpooling_2d(x, 2, 2, 0, cover_all=False)
         h = self.c(h)
     elif self.sample == 'unpool_res':
         h = F.unpooling_2d(x, 2, 2, 0, cover_all=False)
         h0 = self.c(h)
         if self.use_norm:
             h = self.norm0(h0)
         if self.activation is not None:
             h = self.activation(h)
         h = h0 + self.cr(h)
     elif self.sample == 'maxpool':
         h = self.c(x)
         h = F.max_pooling_2d(h, 2, 2, 0)
     elif self.sample == 'maxpool_res':
         h = self.c(x)
         if self.use_norm:
             h = self.norm0(h)
         if self.activation is not None:
             h = self.activation(h)
         h = x + self.cr(h)
         h = F.max_pooling_2d(h, 2, 2, 0)
     elif self.sample == 'avgpool':
         h = self.c(x)
         h = F.average_pooling_2d(h, 2, 2, 0)
     elif self.sample == 'avgpool_res':
         h = self.c(x)
         if self.use_norm:
             h = self.norm0(h)
         if self.activation is not None:
             h = self.activation(h)
         h = x + self.cr(h)
         h = F.average_pooling_2d(h, 2, 2, 0)
     else:
         print('unknown sample method %s' % self.sample)
         exit()
     if self.use_norm:
         h = self.norm(h)
     if self.dropout:
         h = F.dropout(h, ratio=self.dropout)
     if self.activation is not None:
         h = self.activation(h)
     return h
 def __call__(self,x,to_rgb=False):
     h = F.unpooling_2d(x, 2, 2, 0, outsize=(x.shape[2]*2, x.shape[3]*2))
     h = F.leaky_relu(self.dc1(h))
     h = F.leaky_relu(self.dc2(h))
     if to_rgb:
         #h = F.tanh(self.to_RGB(h))
         h = self.to_RGB(h,False)
     return h
Beispiel #17
0
    def __call__(self, w, stage, add_noise=True, w2=None):
        '''
            for alpha in [0, 1), and 2*k+2 + alpha < self.max_stage (-1 <= k <= ...):
            stage 0 + alpha       : z ->        block[0] -> out[0] * 1
            stage 2*k+1 + alpha   : z -> ... -> block[k] -> (up -> out[k]) * (1 - alpha)
                                    .................... -> (block[k+1] -> out[k+1]) * (alpha)
            stage 2*k+2 + alpha   : z -> ............... -> (block[k+1] -> out[k+1]) * 1
            over flow stages continues.
        '''

        stage = min(stage, self.max_stage - 1e-8)
        alpha = stage - math.floor(stage)
        stage = math.floor(stage)

        h = None
        if stage % 2 == 0:
            k = (stage - 2) // 2
            
            # Enable Style Mixing:
            if w2 is not None and k >= 0:
                lim = np.random.randint(1, k+2)
            else:
                lim = k+2

            for i in range(0, (k + 1) + 1):  # 0 .. k+1
                if i == lim:
                    w = w2
                h = self.blocks[i](w, x=h, add_noise=add_noise)

            h = self.outs[k + 1](h)

        else:
            k = (stage - 1) // 2

            if w2 is not None and k >= 1:
                lim = np.random.randint(1, k+1)
            else:
                lim = k+1

            for i in range(0, k + 1):  # 0 .. k
                if i == lim:
                    w = w2
                h = self.blocks[i](w, x=h, add_noise=add_noise)

            h_0 = self.outs[k](upscale2x(h))
            h_1 = self.outs[k + 1](self.blocks[k + 1](w, x=h, add_noise=add_noise))
            assert 0. <= alpha < 1.
            h = (1.0 - alpha) * h_0 + alpha * h_1

        if chainer.configuration.config.train:
            return h
        else:
            min_sample_image_size = 64
            if h.data.shape[2] < min_sample_image_size:  # too small
                scale = int(min_sample_image_size // h.data.shape[2])
                return F.unpooling_2d(h, scale, scale, 0, outsize=(min_sample_image_size, min_sample_image_size))
            else:
                return h
Beispiel #18
0
    def decode(self, x):
        h = F.unpooling_2d(x, 2, 2, cover_all=False)
        h = self.deconv7(h)
        h = F.relu(h)
        h = F.unpooling_2d(h, 2, 2, cover_all=False)
        h = self.deconv6(h)
        h = F.relu(h)
        h = F.unpooling_2d(h, 2, 2, cover_all=False)
        h = self.deconv5(h)
        h = F.relu(h)
        h = F.unpooling_2d(h, 2, 2, cover_all=False)
        h = self.deconv4(h)
        h = F.relu(h)
        h = F.unpooling_2d(h, 2, 2, cover_all=False)
        h = self.deconv3(h)
        h = F.relu(h)
        h = F.unpooling_2d(h, 2, 2, cover_all=False)
        h = self.deconv2(h)
        h = F.relu(h)
        h = F.unpooling_2d(h, 2, 2, cover_all=False)
        h = self.deconv1(h)
        h = F.relu(h)
        h = self.deconv0(h)

        return h
    def __call__(self, x):
        for link in self.children():
            x_ = link(x)
            if "conv" in link.name:
                x = x_
            elif "bn" in link.name:
                x = F.relu(x_)

        return F.unpooling_2d(x, 2, cover_all=False)
Beispiel #20
0
 def __call__(self, x):
     h = F.unpooling_2d(x,
                        2,
                        2,
                        0,
                        outsize=(x.shape[2] * 2, x.shape[3] * 2))
     h = F.leaky_relu(feature_vector_normalize(self.conv0(h)))
     h = F.leaky_relu(feature_vector_normalize(self.conv1(h)))
     return h
 def __call__(self, x, c, last=False):
     l1 = F.unpooling_2d(x, 2, 2, outsize=self.outsize)
     y0 = self.condition(c, l1.shape[2], l1.shape[3])
     l1 = F.concat((y0, l1))
     l2 = F.leaky_relu(self.c1(l1))
     l3 = F.leaky_relu(self.c2(l2))
     if last:
         return self.toRGB(l3)
     return l3
Beispiel #22
0
 def __call__(self, x):
     h = self.conv1_1(x)
     h = self.conv1_2(h)
     h = F.max_pooling_2d(h, 2)
     h = self.conv2_1(h)
     h = self.conv2_2(h)
     h = F.max_pooling_2d(h, 2)
     h = self.conv3_1(h)
     h = self.conv3_2(h)
     h = F.relu(self.deconv1_1(h))
     h = F.relu(self.deconv1_2(h))
     h = F.unpooling_2d(h, 2, outsize=(2 * h.shape[2], 2 * h.shape[3]))
     h = F.relu(self.deconv2_1(h))
     h = F.relu(self.deconv2_2(h))
     h = F.unpooling_2d(h, 2, outsize=(2 * h.shape[2], 2 * h.shape[3]))
     h = F.relu(self.deconv3_1(h))
     h = F.sigmoid(self.deconv3_2(h))
     return h
Beispiel #23
0
 def __call__(self, x):
     h = F.unpooling_2d(x,
                        2,
                        2,
                        0,
                        outsize=(x.shape[2] * 2, x.shape[3] * 2))
     h = F.leaky_relu(self.bn0(self.c0(h)))
     h = F.leaky_relu(self.bn1(self.c1(h)))
     return h
Beispiel #24
0
    def __call__(self, x):
        h = F.unpooling_2d(x, 2, cover_all=False)

        h1 = F.relu(self.conv1(h))
        h1 = self.conv2(h1)

        h2 = self.conv_(h)

        return F.relu(h1 + h2)
Beispiel #25
0
    def __call__(self, x):
        h = F.reshape(self.l0(x), ((x.shape[0], ) + self.embed_shape))

        for i in range(self.n_blocks):
            for j in range(self.block_size):
                h = F.elu(getattr(self, 'c{}'.format(i * j + j))(h))
            if i < self.n_blocks - 1:
                h = F.unpooling_2d(h, ksize=2, stride=2, cover_all=False)

        return self.ln(h)
Beispiel #26
0
    def __call__(self, z, test=False):
        h = F.relu(self.bn0(self.fc(z), test=test))
        h = F.reshape(h, (z.shape[0], 256, 2, 2))

        # Upsample the image and then apply a dimension preserving convolution
        # with a kernel of size (3, 3), stride and padding of size (1, 1).
        h = F.unpooling_2d(h, 2, 2, cover_all=False)
        h = F.relu(self.bn1(self.c1(h), test=test))

        h = F.unpooling_2d(h, 2, 2, cover_all=False)
        h = F.relu(self.bn2(self.c2(h), test=test))

        h = F.unpooling_2d(h, 2, 2, cover_all=False)
        h = F.relu(self.bn3(self.c3(h), test=test))

        h = F.unpooling_2d(h, 2, 2, cover_all=False)
        h = F.sigmoid(self.c4(h))

        return h
Beispiel #27
0
 def __call__(self, x):
     if self.sample in ['maxpool_res', 'avgpool_res']:
         h = self.activation(self.norm0(self.c(x)))
         h = self.norm(self.cr(h))
         if self.sample == 'maxpool_res':
             h = F.max_pooling_2d(h, 2, 2, 0)
             h = h + F.max_pooling_2d(self.cskip(x), 2, 2, 0)
         elif self.sample == 'avgpool_res':
             h = F.average_pooling_2d(h, 2, 2, 0)
             h = h + F.average_pooling_2d(self.cskip(x), 2, 2, 0)
     elif self.sample == 'resize':
         H, W = x.data.shape[2:]
         h = F.resize_images(x, (2 * H, 2 * W))
         h = self.norm(self.c(h))
     elif self.sample == 'resize_res':
         H, W = x.data.shape[2:]
         h = F.resize_images(x, (2 * H, 2 * W))
         h0 = self.activation(self.norm0(self.c(h)))
         h = self.cskip(h) + self.norm(self.cr(h0))
     elif self.sample == 'unpool_res':
         h = F.unpooling_2d(x, 2, 2, 0, cover_all=False)
         h0 = self.activation(self.norm0(self.c(h)))
         h = self.cskip(h) + self.norm(self.cr(h0))
     else:
         if self.sample == 'maxpool':
             h = self.c(x)
             h = F.max_pooling_2d(h, 2, 2, 0)
         elif self.sample == 'avgpool':
             h = self.c(x)
             h = F.average_pooling_2d(h, 2, 2, 0)
         elif self.sample == 'unpool':
             h = F.unpooling_2d(x, 2, 2, 0, cover_all=False)
             h = self.c(h)
         else:
             h = self.c(x)
         if self.use_norm:
             h = self.norm(h)
         if self.dropout:
             h = F.dropout(h, ratio=self.dropout)
     if self.activation is not None:
         h = self.activation(h)
     return h
Beispiel #28
0
    def __call__(self, x):
        if self.shuffler:
            h = self.c0(x)
            h = pixel_shuffler(self.out_ch, h)
            h = F.relu(self.bshuffe(h))

        else:
            h = F.unpooling_2d(x, 2, 2, 0, cover_all=False)
            h = F.relu(self.bn0(self.c0(h)))

        return h
Beispiel #29
0
 def __call__(self, x):
     outsize1=x.shape[-2:]        
     h = F.relu(self.conv1(x))                   #26
     h = F.relu(self.conv2(h))                   #24
     h = F.max_pooling_2d(h, 2)                  #12
     h = F.relu(self.conv3(h))                   #10
     h = F.relu(self.conv4(h))                   #8      
     outsize2=h.shape[-2:]        
     h = F.max_pooling_2d(h, 2)                  #4
     h = F.relu(self.conv5(h))                   #2
     
               
     h = F.relu(self.dconv5(h))                 #4
     h = F.unpooling_2d(h, 2, outsize=outsize2)  #8
     h = F.relu(self.dconv4(h))                 #10
     h = F.relu(self.dconv3(h))                 #12
     h = F.unpooling_2d(h, 2, outsize=outsize1)  #24
     h = F.relu(self.dconv2(h))                 #26
     y = F.sigmoid(self.dconv1(h))               # 28
     return y
def invert_maxpooling(variable, guided=True):
    """ Max pooling後の~chainer.Variableから、Max pooling前の状態を復元する
    Args:
        variable (~chainer.Variable): Max pooling後の中間層
        guided (bool): guided backpropagation を行う場合はTrue、行わない場合はFalse.
    Returns:
        data (ndarray): 復元されたMax pooling前の中間層のデータ(返されるのは~chainer.Variableではないことに注意)
    """
    assert variable.creator is not None
    assert variable.creator.label == 'MaxPooling2D', 'variable.creator should be MaxPooling2D.'
    v = variable
    bottom_blob = v.creator.inputs[0]

    kw, kh = v.creator.kw, v.creator.kh
    sx, sy = v.creator.sx, v.creator.sy
    pw, ph = v.creator.pw, v.creator.ph
    outsize = (bottom_blob.data.shape[2], bottom_blob.data.shape[3])

    # UnPooling
    unpooled_data = F.unpooling_2d(v, (kh, kw),
                                   stride=(sy, sx),
                                   pad=(ph, pw),
                                   outsize=outsize)

    # Max Location Switchesの作成(Maxの位置だけ1, それ以外は0)
    ## (1) Max pooling後のマップをNearest neighborでpooling前のサイズに拡大
    unpooled_max_map = F.unpooling_2d(F.max_pooling_2d(bottom_blob, (kh, kw),
                                                       stride=(sy, sx),
                                                       pad=(ph, pw)), (kh, kw),
                                      stride=(sy, sx),
                                      pad=(ph, pw),
                                      outsize=outsize)
    ## (2) 最大値と値が一致するところだけ1, それ以外は0 (Max Location Switches)
    pool_switch = unpooled_max_map.data == bottom_blob.data
    ## (3) そもそも最大値が0以下だったら伝搬させない (guided backpropagation)
    if guided:
        guided_switch = bottom_blob.data > 0
        pool_switch *= guided_switch

    # Max Location Switchesが1のところだけUnPoolingの結果を伝搬、それ以外は0
    return unpooled_data.data * pool_switch
Beispiel #31
0
    def __call__(self, x):
        if self.down:
            h = self.act(self.bn0(self.c0(x)))

        elif self.up:
            h = F.unpooling_2d(x, 2, 2, 0, cover_all=False)
            h = self.act(self.bn0(self.c0(h)))

        else:
            h = self.act(self.bn0(self.c0(x)))

        return h
Beispiel #32
0
 def unpool2d(self,
              ksize,
              stride=None,
              pad=0,
              outsize=None,
              cover_all=True):
     f = self.funcs(
         'unpool2d', lambda x: F.unpooling_2d(x, ksize, stride, pad,
                                              outsize, cover_all))
     f.dot_param = '(ksize:{}, stride:{}, pad:{}, outsize:{}, cover_all:{})'.format(
         ksize, stride, pad, outsize, cover_all)
     return f
Beispiel #33
0
    def __call__(self, x):
        if self.up:
            h = F.unpooling_2d(x, 2, 2, 0, cover_all=False)
            h = glu(self.bn0(self.cup(h)))

        elif self.down:
            h = glu(self.bn0(self.cdown(x)))

        else:
            h = glu(self.bn0(self.cpara(x)))

        return h
Beispiel #34
0
def resize_mask_like(mask, x):
    """Resize mask like shape of x.
    Args:
        mask: Original mask.
        x: To shape of x.
    """
    if mask.shape[2] < x.shape[2]:
        mask_resize = F.unpooling_2d(mask, ksize=4, outsize=x.shape[2:])
    else:
        rate = mask.shape[2] // x.shape[2]
        mask_resize = mask[:, :, ::rate, ::rate]
    return mask_resize
Beispiel #35
0
 def decode(self, z):
     h = z
     # unpool1
     h = F.reshape(h, self.pool1_outshape)
     h = F.unpooling_2d(h, ksize=3, stride=2, outsize=self.pool1_inshape[-2:])
     # deconv2_1
     self.deconv2_1.outsize = self.conv1_2_inshape[-2:]
     h = self.deconv2_1(h)
     # deconv2_2
     self.deconv2_2.outsize = self.conv1_1_inshape[-2:]
     h = self.deconv2_2(h)
     y = h
     return y
	def __call__(self, input_blob, test_mode=False):
		# explicit and very flexible DAG!
		#################################
		data = input_blob[0]
		labels = input_blob[1]

		if(len(input_blob) >= 3):
			weights_classes = input_blob[2]
		else:
			weights_classes = chainer.Variable(cuda.cupy.ones((self.classes, 1), dtype='float32'))

		# ---- CONTRACTION BLOCKS ---- #
		blob_b0  = self.bnorm0(data)
		(blob_b1, indices_b1, size_b1)  = F.max_pooling_2dIndices(self.bnorm1(F.relu(self.conv1(blob_b0)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
		(blob_b2, indices_b2, size_b2)  = F.max_pooling_2dIndices(self.bnorm2(F.relu(self.conv2(blob_b1)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
		(blob_b3, indices_b3, size_b3)  = F.max_pooling_2dIndices(self.bnorm3(F.relu(self.conv3(blob_b2)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))
		(blob_b4, indices_b4, size_b4)  = F.max_pooling_2dIndices(self.bnorm4(F.relu(self.conv4(blob_b3)), test=test_mode), (2, 2), stride=(2,2), pad=(0, 0))

		# ---- EXPANSION BLOCKS ---- #
		blob_b5  = self.bnorm5(F.relu(self.conv5(F.unpooling_2d(blob_b4, indices_b4, size_b4))), test=test_mode)
		blob_b6  = self.bnorm6(F.relu(self.conv6(F.unpooling_2d(blob_b5, indices_b3, size_b3))), test=test_mode)
		blob_b7  = self.bnorm7(F.relu(self.conv7(F.unpooling_2d(blob_b6, indices_b2, size_b2))), test=test_mode)
		blob_b8  = self.bnorm8(F.relu(self.conv8(F.unpooling_2d(blob_b7, indices_b1, size_b1))), test=test_mode)

		#ipdb.set_trace()

		# ---- SOFTMAX CLASSIFIER ---- #
		self.blob_class = self.classi(blob_b8)
		self.probs = F.softmax(self.blob_class)

		# ---- CROSS-ENTROPY LOSS ---- #
		#ipdb.set_trace()
	        self.loss = F.weighted_cross_entropy(self.probs, labels, weights_classes, normalize=True)
		self.output_point = self.probs

		return self.loss
Beispiel #37
0
    def check_forward_consistency_regression(self, backend_config):
        # Regression test to two-dimensional unpooling layer.
        inputs, = self.generate_inputs()
        x = chainer.Variable(backend_config.get_array(inputs))

        ksize = self.ksize
        stride = self.stride
        pad = self.pad

        y_nd = functions.unpooling_nd(x, ksize, stride=stride, pad=pad,
                                      cover_all=self.cover_all)
        y_2d = functions.unpooling_2d(x, ksize, stride=stride, pad=pad,
                                      cover_all=self.cover_all)
        testing.assert_allclose(
            y_nd.array, y_2d.array, **self.check_forward_options)
Beispiel #38
0
    def check_forward_consistency_regression(self, x_data):
        # Regression test to two-dimensional unpooling layer.

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

        ksize = self.ksize
        stride = self.stride
        pad = self.pad

        y_nd = functions.unpooling_nd(x_data, ksize, stride=stride, pad=pad,
                                      cover_all=self.cover_all)
        y_2d = functions.unpooling_2d(x_data, ksize, stride=stride, pad=pad,
                                      cover_all=self.cover_all)
        testing.assert_allclose(
            y_nd.data, y_2d.data, **self.check_forward_options)
def deconv(self, variable):
    v = variable
    if(v.creator is not None):
        # Convolution -> Deconvolutionに変換
        if (v.creator.label == 'Convolution2DFunction'):
            print(v.creator.label, v.rank)
            convW = v.creator.inputs[1].data
            in_cn, out_cn = convW.shape[0], convW.shape[1] # in/out channels
            kh, kw = convW.shape[2], convW.shape[3] # kernel size
            sx, sy = v.creator.sx, v.creator.sy # stride
            pw, ph = v.creator.pw, v.creator.ph # padding

            name = 'conv' + v.rank # temporal layer name
            super(DeconvNet, self).add_link(name, L.Deconvolution2D(
            in_cn, out_cn, (kh, kw), stride=(sy, sx), pad=(ph, pw),
            nobias=True, initialW=convW))
            self.forwards[name] = self[name]
            # もし畳み込み層にバイアスがある場合、それも登録
            if len(v.creator.inputs) == 3:
                F.bias(v)
                b = v.creator.inputs[2].data
                bname = 'convb' + v.rank
                super(DeconvNet, self).add_link(bname, L.Bias(shape=b.shape))
                self[bname].b.data = b
                self.depends[bname] = (parent)
                self.depends[name] = (bname)
                self.forwards[bname] = self[bname]
                self.layers.append((bname, [parent], name))
            else:
                self.depends[name] = (parent)

        elif (v.creator.label == 'ReLU'):
            name = parent
        elif (v.creator.label == 'MaxPooling2D'):
            kw, kh = v.creator.kw, v.creator.kh
            sx, sy = v.creator.sx, v.creator.sy
            pw, ph = v.creator.pw, v.creator.ph
            name = 'maxpool' + v.rank
            self.depends[name] = (parent)
            self.forwards[name] = lambda x: F.unpooling_2d(x, (kh, kw), stride=(sy, sx), pad=(ph, pw))

        self.register_inv_layer(v.creator.inputs[0], name)
    else:
        depends['output'] = parent
Beispiel #40
0
    def __call__(self, bottom_up, top_down=None):
        with cupy.cuda.Device(self.device):
            E = F.concat((F.relu(bottom_up - self.P),
                          F.relu(self.P - bottom_up)))
            if self.istop:
                A = None
                R = self.ConvLSTM((E,))
            else:
                A = F.max_pooling_2d(F.relu(self.ConvA(E)), 2, stride=2)
                unpooled = F.unpooling_2d(top_down, 2,
                                          stride=2, cover_all=False)
                R = self.ConvLSTM((E, unpooled))

            if self.isbottom:
                P = F.clipped_relu(self.ConvP(R), 1.0)
            else:
                P = F.relu(self.ConvP(R))

        self.P = P

        return (A, R)
Beispiel #41
0
 def __call__(self, x, train):
     h = x
     # convolution
     for i in range(len(self.convs)):
         h = self.activation(self.convs[i](h))
         if self.conv_arch[i][3] > 1:
             h = F.max_pooling_2d(h, self.conv_arch[i][3], self.conv_arch[i][3])
     # fc enc
     for i in range(len(self.encs)):
         h = F.dropout(self.activation(self.encs[i](h)), train=train)
     # fc dec
     for i in reversed(range(len(self.decs))):
         h = self.activation(self.decs[i](h))
     # deconv
     h = F.reshape(h, (h.data.shape[0], self.conv_arch[-1][1], self.dims[-1], self.dims[-1]))
     for i in reversed(range(len(self.deconvs))):
         if self.conv_arch[i][3] > 1:
             unpool_size = self.dims[i+1] * self.conv_arch[i][3]
             h = F.unpooling_2d(h, self.conv_arch[i][3],
                                outsize=(unpool_size, unpool_size))
         h = self.activation(self.deconvs[i](h))
     return h
Beispiel #42
0
 def decode(self, z):
     h = F.unpooling_2d(z, ksize=2, stride=2)
     y = self.deconv2(h)
     return y
Beispiel #43
0
 def f(x):
     return functions.unpooling_2d(x, self.ksize, outsize=self.outsize,
                                   cover_all=self.cover_all)
Beispiel #44
0
	def __call__(self, x):
		return F.unpooling_2d(x, self.ksize, self.stride, self.pad, self.outsize, self.cover_all)