Python chainer.functions.roi_pooling_2d() Examples

The following are 7 code examples of chainer.functions.roi_pooling_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: test_roi_pooling_2d.py    From chainer with MIT License 5 votes vote down vote up
def check_forward(self, x_data, roi_data):
        x = chainer.Variable(x_data)
        rois = chainer.Variable(roi_data)
        y = functions.roi_pooling_2d(
            x, rois, outh=self.outh, outw=self.outw,
            spatial_scale=self.spatial_scale)
        self.assertEqual(y.data.dtype, self.dtype)
        y_data = cuda.to_cpu(y.data)

        self.assertEqual(self.gy.shape, y_data.shape) 
Example #2
Source File: test_roi_pooling_2d.py    From chainer with MIT License 5 votes vote down vote up
def test_forward_cpu_gpu_equal(self):
        # cpu
        x_cpu = chainer.Variable(self.x)
        rois_cpu = chainer.Variable(self.rois)
        y_cpu = functions.roi_pooling_2d(
            x_cpu, rois_cpu, outh=self.outh, outw=self.outw,
            spatial_scale=self.spatial_scale)

        # gpu
        x_gpu = chainer.Variable(cuda.to_gpu(self.x))
        rois_gpu = chainer.Variable(cuda.to_gpu(self.rois))
        y_gpu = functions.roi_pooling_2d(
            x_gpu, rois_gpu, outh=self.outh, outw=self.outw,
            spatial_scale=self.spatial_scale)
        testing.assert_allclose(y_cpu.data, cuda.to_cpu(y_gpu.data)) 
Example #3
Source File: test_roi_pooling_2d.py    From chainer with MIT License 5 votes vote down vote up
def check_backward(self, x_data, roi_data, y_grad):
        def f(x, rois):
            return functions.roi_pooling_2d(
                x, rois, outh=self.outh, outw=self.outw,
                spatial_scale=self.spatial_scale)

        gradient_check.check_backward(
            f, (x_data, roi_data), y_grad, no_grads=[False, True],
            **self.check_backward_options) 
Example #4
Source File: test_poolings.py    From chainer with MIT License 5 votes vote down vote up
def setUp(self):
        # these parameters are referenced from chainer test
        in_shape = (3, 3, 12, 8)
        self.x = input_generator.positive_increasing(*in_shape)
        # In chainer test, x is shuffled and normalize-like conversion,
        # In this test, those operations are skipped.
        # If x includes negative value, not match with onnxruntime output.
        # You can reproduce this issue by changing `positive_increasing` to
        # `increase`
        self.rois = np.array([
            [0, 1, 1, 6, 6],
            [2, 6, 2, 7, 11],
            [1, 3, 1, 5, 10],
            [0, 3, 3, 3, 3]], dtype=np.float32)
        kwargs = {
            'outh': 3,
            'outw': 7,
            'spatial_scale': 0.6
        }

        class Model(chainer.Chain):
            def __init__(self, kwargs):
                super(Model, self).__init__()
                self.kwargs = kwargs

            def __call__(self, x, rois):
                return F.roi_pooling_2d(x, rois, **self.kwargs)

        self.model = Model(kwargs) 
Example #5
Source File: roi_module.py    From FATE with Apache License 2.0 5 votes vote down vote up
def test_roi_module():
    ## fake data###
    B, N, C, H, W, PH, PW = 2, 8, 4, 32, 32, 7, 7

    bottom_data = t.randn(B, C, H, W).cuda()
    bottom_rois = t.randn(N, 5)
    bottom_rois[:int(N / 2), 0] = 0
    bottom_rois[int(N / 2):, 0] = 1
    bottom_rois[:, 1:] = (t.rand(N, 4) * 100).float()
    bottom_rois = bottom_rois.cuda()
    spatial_scale = 1. / 16
    outh, outw = PH, PW

    # pytorch version
    module = RoIPooling2D(outh, outw, spatial_scale)
    x = bottom_data.requires_grad_()
    rois = bottom_rois.detach()

    output = module(x, rois)
    output.sum().backward()

    def t2c(variable):
        npa = variable.data.cpu().numpy()
        return cp.array(npa)

    def test_eq(variable, array, info):
        cc = cp.asnumpy(array)
        neq = (cc != variable.data.cpu().numpy())
        assert neq.sum() == 0, 'test failed: %s' % info

    # chainer version,if you're going to run this
    # pip install chainer 
    import chainer.functions as F
    from chainer import Variable
    x_cn = Variable(t2c(x))

    o_cn = F.roi_pooling_2d(x_cn, t2c(rois), outh, outw, spatial_scale)
    test_eq(output, o_cn.array, 'forward')
    F.sum(o_cn).backward()
    test_eq(x.grad, x_cn.grad, 'backward')
    print('test pass') 
Example #6
Source File: test_poolings.py    From onnx-chainer with MIT License 5 votes vote down vote up
def setUp(self):
        # these parameters are referenced from chainer test
        in_shape = (3, 3, 12, 8)
        self.x = input_generator.positive_increasing(*in_shape)
        # In chainer test, x is shuffled and normalize-like conversion,
        # In this test, those operations are skipped.
        # If x includes negative value, not match with onnxruntime output.
        # You can reproduce this issue by changing `positive_increasing` to
        # `increase`
        self.rois = np.array([
            [0, 1, 1, 6, 6],
            [2, 6, 2, 7, 11],
            [1, 3, 1, 5, 10],
            [0, 3, 3, 3, 3]], dtype=np.float32)
        kwargs = {
            'outh': 3,
            'outw': 7,
            'spatial_scale': 0.6
        }

        class Model(chainer.Chain):
            def __init__(self, kwargs):
                super(Model, self).__init__()
                self.kwargs = kwargs

            def __call__(self, x, rois):
                return F.roi_pooling_2d(x, rois, **self.kwargs)

        self.model = Model(kwargs) 
Example #7
Source File: faster_rcnn_vgg.py    From chainercv with MIT License 5 votes vote down vote up
def _roi_pooling_2d_yx(x, indices_and_rois, outh, outw, spatial_scale):
    xy_indices_and_rois = indices_and_rois[:, [0, 2, 1, 4, 3]]
    pool = F.roi_pooling_2d(
        x, xy_indices_and_rois, outh, outw, spatial_scale)
    return pool