Python chainer.functions.convolution_2d() Examples

The following are 30 code examples of chainer.functions.convolution_2d(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module chainer.functions , or try the search function .
Example #1
Source File: loss.py    From Video-frame-prediction-by-multi-scale-GAN with MIT License 6 votes vote down vote up
def gradient_loss(generated, truth):
	"""

	:param generated: generated image by the generator at any scale
	:param truth: The ground truth image at that scale
	:return: GDL loss
	"""
	xp = cp.get_array_module(generated.data)
	n, c, h, w = generated.shape
	wx = xp.array([[[1, -1]]]*c, ndmin=4).astype(xp.float32)
	wy = xp.array([[[1], [-1]]]*c, ndmin=4).astype(xp.float32)

	d_gx = F.convolution_2d(generated, wx)
	d_gy = F.convolution_2d(generated, wy)

	d_tx = F.convolution_2d(truth, wx)
	d_ty = F.convolution_2d(truth, wy)

	return (F.sum(F.absolute(d_gx - d_tx)) + F.sum(F.absolute(d_gy - d_ty))) 
Example #2
Source File: convolution_rbm.py    From SeRanet with MIT License 6 votes vote down vote up
def reconstruct(self, v):
        """

        :param v: Variable Matrix(batch_size, in_channels, image_height, image_width)
        :return: reconstructed_v, Variable Matrix(batch_size, in_channels, image_height, image_width)
        """
        batch_size = v.data.shape[0]
        xp = cuda.get_array_module(v.data)
        if self.real == 0:
            h = F.sigmoid(self.conv(v))
        else:
            std_ch = xp.reshape(self.std, (1, self.in_channels, 1, 1))
            h = F.sigmoid(self.conv(v / std_ch))
        # F.sigmoid(F.matmul(v, self.l.W, transb=True) + F.broadcast_to(self.l.b, (batch_size, self.n_hidden)))
        W_flipped = F.swapaxes(CF.flip(self.conv.W, axes=(2, 3)), axis1=0, axis2=1)
        reconstructed_v = F.sigmoid(F.convolution_2d(h, W_flipped, self.conv.a, pad=self.ksize-1))
            # = F.sigmoid(F.matmul(h, self.l.W) + F.broadcast_to(self.l.a, (batch_size, self.n_visible)))
        return reconstructed_v 
Example #3
Source File: convolution_rbm.py    From SeRanet with MIT License 6 votes vote down vote up
def propdown(self, hid):
        """ This function propagates the hidden units activation downwords to the visible units
        :param hid: Variable Matrix(batch_size, out_channels, image_height_out, image_width_out)  - given h_sample
        :return: Variable Matrix(batch_size, in_channels, image_height, image_width) - probability for each visible units to be v_j = 1
        """
        batch_size = hid.data.shape[0]
        if self.real == 0:
            W_flipped = F.swapaxes(CF.flip(self.conv.W, axes=(2, 3)), axis1=0, axis2=1)
            pre_sigmoid_activation = F.convolution_2d(hid, W_flipped, self.conv.a, pad=self.ksize-1)
                # F.matmul(hid, self.l.W) + F.broadcast_to(self.l.a, (batch_size, self.n_visible))
            v_mean = F.sigmoid(pre_sigmoid_activation)
            #print('W info ', self.conv.W.data.shape, 'W_flipped info ', W_flipped.data.shape)
            #print('W info ', self.conv.W.data[3, 0, 2, 3], 'W_flipped info ', W_flipped.data[0, 3, 8, 7])
            #print('W info ', self.conv.W.data[3, 0, 8, 7], 'W_flipped info ', W_flipped.data[0, 3, 2, 3])
            #print('W info ', self.conv.W.data[19, 0, 4, 0], 'W_flipped info ', W_flipped.data[0, 19, 6, 10])
            #print('pre_sigmoidactivation', F.sum(pre_sigmoid_activation).data)
            #print('v_mean', v_mean.data.shape)
            #print('v_mean sum', F.sum(v_mean).data)
            #print('hid', hid.data.shape)

        else:
            # TODO: check
            W_flipped = F.swapaxes(CF.flip(self.conv.W, axes=(2, 3)), axis1=0, axis2=1)
            v_mean = F.convolution_2d(hid, W_flipped, self.conv.a, pad=self.ksize-1)
        return v_mean 
Example #4
Source File: test_deformable_convolution_2d_sampler.py    From chainer with MIT License 6 votes vote down vote up
def check_forward(self, x, offset, W, b, stride, pad):
        with chainer.using_config('use_cudnn', self.use_cudnn):
            _, _, h, w = x.shape
            _, _, kh, kw = W.shape
            offset[:, :kh * kw] = -1 * stride[1]
            offset[:, kh * kw:] = 1 * stride[0]

            x = chainer.Variable(x)
            offset = chainer.Variable(offset)
            out = deformable_convolution_2d_sampler(
                x, offset, W, b, stride, pad).data
            pad = (pad[0] + 1 * stride[0], pad[1] + 1 * stride[1])
            expeceted = convolution_2d(
                x, W, b, stride, pad).data
            expeceted = expeceted[:, :, 2:, :-2]
        testing.assert_allclose(out, expeceted) 
Example #5
Source File: test_convolution_2d.py    From chainer with MIT License 6 votes vote down vote up
def forward_expected(self, inputs):
        """
        Current forward_expected implementation depends on
        F.convolution_2d itself and thus it's only capable
        of checking consistency between backends, not absolute
        correctness of computations
        """
        if self.nobias:
            x, W = inputs
            b = None
        else:
            x, W, b = inputs
        with chainer.using_config('use_ideep', 'never'):
            y_expected = F.convolution_2d(
                x, W, b, stride=self.stride, pad=self.pad,
                cover_all=self.cover_all, dilate=self.dilate,
                groups=self.groups)
        if self.old_numpy_fp16:
            return y_expected.array*0,
        return y_expected.array, 
Example #6
Source File: test_convolution_nd.py    From chainer with MIT License 6 votes vote down vote up
def check_forward_consistency_regression(self, backend_config):
        inputs = self.generate_inputs()
        if self.nobias:
            x, W = inputs
            b = None
        else:
            x, W, b = inputs
        x = chainer.Variable(backend_config.get_array(x))
        W = chainer.Variable(backend_config.get_array(W))
        if b is not None:
            b = chainer.Variable(backend_config.get_array(b))

        with chainer.using_config('use_cudnn', 'never'):
            y_nd = F.convolution_nd(
                x, W, b, stride=self.stride, pad=self.pad,
                cover_all=self.cover_all, dilate=self.dilate,
                groups=self.groups)
            y_2d = F.convolution_2d(
                x, W, b, stride=self.stride, pad=self.pad,
                cover_all=self.cover_all, dilate=self.dilate,
                groups=self.groups)

        testing.assert_allclose(
            y_nd.array, y_2d.array, **self.check_forward_options) 
Example #7
Source File: loss_functions.py    From chainer-gan-experiments with MIT License 5 votes vote down vote up
def loss_func_tv_l2(x_out):
    xp = cuda.get_array_module(x_out.data)
    b, ch, h, w = x_out.data.shape
    Wx = xp.zeros((ch, ch, 2, 2), dtype="f")
    Wy = xp.zeros((ch, ch, 2, 2), dtype="f")
    for i in range(ch):
        Wx[i,i,0,0] = -1
        Wx[i,i,0,1] = 1
        Wy[i,i,0,0] = -1
        Wy[i,i,1,0] = 1
    return F.sum(F.convolution_2d(x_out, W=Wx) ** 2) + F.sum(F.convolution_2d(x_out, W=Wy) ** 2) 
Example #8
Source File: invert.py    From ssai-cnn with MIT License 5 votes vote down vote up
def tvh(self, x):
        return F.convolution_2d(x, W=self.Wh) 
Example #9
Source File: backwards.py    From chainer-gan-experiments with MIT License 5 votes vote down vote up
def backward_deconvolution(x_in, x, l):
    y = F.convolution_2d(x, l.W, None, l.stride, l.pad)
    return y 
Example #10
Source File: test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward1(self):
        n, c, h, w = 1, 5, 15, 15
        o, k, s, p = 8, (3, 3), (1, 1), (1, 1)
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d_simple(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_equal(expected.data, y.data)) 
Example #11
Source File: test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward2(self):
        n, c, h, w = 1, 5, 15, 15
        o, k, s, p = 8, (3, 3), (3, 1), (2, 1)
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d_simple(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_equal(expected.data, y.data)) 
Example #12
Source File: test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward3(self):
        n, c, h, w = 1, 5, 20, 15
        o, k, s, p = 3, (5, 3), 1, 3
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d_simple(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_equal(expected.data, y.data)) 
Example #13
Source File: test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward4(self):
        n, c, h, w = 1, 5, 20, 15
        o, k, s, p = 3, (5, 3), 1, 3
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = np.random.randn(o).astype('f')
        y = F.conv2d_simple(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_equal(expected.data, y.data)) 
Example #14
Source File: test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward1(self):
        n, c, h, w = 1, 5, 15, 15
        o, k, s, p = 8, (3, 3), (1, 1), (1, 1)
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_equal(expected.data, y.data)) 
Example #15
Source File: test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward3(self):
        n, c, h, w = 1, 5, 20, 15
        o, k, s, p = 3, (5, 3), 1, 3
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_equal(expected.data, y.data)) 
Example #16
Source File: test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward4(self):
        n, c, h, w = 1, 5, 20, 15
        o, k, s, p = 3, (5, 3), 1, 3
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = np.random.randn(o).astype('f')
        y = F.conv2d(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_equal(expected.data, y.data)) 
Example #17
Source File: gpu_test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward1(self):
        n, c, h, w = 1, 5, 15, 15
        o, k, s, p = 8, (3, 3), (1, 1), (1, 1)
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d_simple(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_allclose(expected.data, y.data)) 
Example #18
Source File: gpu_test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward2(self):
        n, c, h, w = 1, 5, 15, 15
        o, k, s, p = 8, (3, 3), (3, 1), (2, 1)
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d_simple(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_allclose(expected.data, y.data)) 
Example #19
Source File: gpu_test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward3(self):
        n, c, h, w = 1, 5, 20, 15
        o, k, s, p = 3, (5, 3), 1, 3
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d_simple(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_allclose(expected.data, y.data)) 
Example #20
Source File: gpu_test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward1(self):
        n, c, h, w = 1, 5, 15, 15
        o, k, s, p = 8, (3, 3), (1, 1), (1, 1)
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_allclose(expected.data, y.data)) 
Example #21
Source File: gpu_test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward2(self):
        n, c, h, w = 1, 5, 15, 15
        o, k, s, p = 8, (3, 3), (3, 1), (2, 1)
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_allclose(expected.data, y.data)) 
Example #22
Source File: gpu_test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward3(self):
        n, c, h, w = 1, 5, 20, 15
        o, k, s, p = 3, (5, 3), 1, 3
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = None
        y = F.conv2d(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_allclose(expected.data, y.data)) 
Example #23
Source File: gpu_test_conv2d.py    From deep-learning-from-scratch-3 with MIT License 5 votes vote down vote up
def test_forward4(self):
        n, c, h, w = 1, 5, 20, 15
        o, k, s, p = 3, (5, 3), 1, 3
        x = np.random.randn(n, c, h, w).astype('f')
        W = np.random.randn(o, c, k[0], k[1]).astype('f')
        b = np.random.randn(o).astype('f')
        y = F.conv2d(x, W, b, s, p)
        expected = CF.convolution_2d(x, W, b, s, p)
        self.assertTrue(array_allclose(expected.data, y.data)) 
Example #24
Source File: GAIN.py    From Guided-Attention-Inference-Network with MIT License 5 votes vote down vote up
def get_gcam(self, end_output, activations, shape, label):
		self.cleargrads()
		class_id = self.set_init_grad(end_output, label)
		end_output.backward(retain_grad=True)
		grad = activations.grad_var
		grad = F.average_pooling_2d(grad, (grad.shape[-2], grad.shape[-1]), 1)
		grad = F.expand_dims(F.reshape(grad, (grad.shape[0]*grad.shape[1], grad.shape[2], grad.shape[3])), 0)
		weights = activations
		weights = F.expand_dims(F.reshape(weights, (weights.shape[0]*weights.shape[1], weights.shape[2], weights.shape[3])), 0)
		gcam = F.resize_images(F.relu(F.convolution_2d(weights, grad, None, 1, 0)), shape)
		return gcam, class_id 
Example #25
Source File: net.py    From chainer-gan-lib with MIT License 5 votes vote down vote up
def backward_deconvolution(x_in, x, l):
    y = F.convolution_2d(x, l.W, None, l.stride, l.pad)
    return y 
Example #26
Source File: rescale.py    From chainer-stylegan with MIT License 5 votes vote down vote up
def blur(h, w_k):
    b, ch, w_s, h_s = h.shape
    h = F.reshape(h, (b*ch, 1, w_s, h_s))
    h = F.convolution_2d(h, w_k, stride=1, pad=1)
    h = F.reshape(h, (b, ch, w_s, h_s))
    return h 
Example #27
Source File: invert.py    From ssai-cnn with MIT License 5 votes vote down vote up
def tvw(self, x):
        return F.convolution_2d(x, W=self.Ww) 
Example #28
Source File: invert_diff.py    From ssai-cnn with MIT License 5 votes vote down vote up
def tvh(self, x):
        return F.convolution_2d(x, W=self.Wh) 
Example #29
Source File: invert_diff.py    From ssai-cnn with MIT License 5 votes vote down vote up
def tvw(self, x):
        return F.convolution_2d(x, W=self.Ww) 
Example #30
Source File: wrn1bit_cifar.py    From imgclsmob with MIT License 5 votes vote down vote up
def forward(self, x):
        W_1bit = Binarize()(self.W) if self.binarized else self.W
        b_1bit = Binarize()(self.b) if self.b is not None and self.binarized else self.b
        return F.convolution_2d(
            x=x,
            W=W_1bit,
            b=b_1bit,
            stride=self.stride,
            pad=self.pad,
            dilate=self.dilate,
            groups=self.groups)