Beispiel #1
0
    def test_convolve_full(self):
        mode = 'full'
        devices = [backend.cpu_device]
        if config.cupy_enabled:
            devices.append(backend.Device(0))
            
        for device in devices:
            xp = device.xp
            with device:
                for dtype in [np.float32, np.float64, np.complex64, np.complex128]:
                    x = util.dirac([1, 3], device=device, dtype=dtype)
                    W = xp.ones([1, 3], dtype=dtype)
                    y = backend.to_device(conv.convolve(x, W, mode=mode), backend.cpu_device)
                    npt.assert_allclose(y, [[0, 1, 1, 1, 0]], atol=1e-5)

                    x = util.dirac([1, 3], device=device, dtype=dtype)
                    W = xp.ones([1, 2], dtype=dtype)
                    y = backend.to_device(conv.convolve(x, W, mode=mode), backend.cpu_device)
                    npt.assert_allclose(y, [[0, 1, 1, 0]], atol=1e-5)

                    x = util.dirac([1, 3], device=device, dtype=dtype)
                    W = xp.ones([2, 1, 3], dtype=dtype)
                    y = backend.to_device(conv.convolve(x, W, mode=mode,
                                                     output_multi_channel=True), backend.cpu_device)
                    npt.assert_allclose(y, [[[0, 1, 1, 1, 0]],
                                            [[0, 1, 1, 1, 0]]], atol=1e-5)
Beispiel #2
0
    def test_convolve_valid(self):
        mode = 'valid'
        devices = [backend.cpu_device]
        if config.cupy_enabled:
            devices.append(backend.Device(0))

        for D in [1, 2, 3]:
            for device in devices:
                xp = device.xp
                with device:
                    for dtype in dtypes:
                        with self.subTest(D=D, dtype=dtype, device=device):
                            data = util.dirac([3] + [1] * (D - 1),
                                              device=device, dtype=dtype)
                            filt = xp.ones([3] + [1] * (D - 1), dtype=dtype)
                            output = backend.to_device(conv.convolve(
                                data, filt, mode=mode))
                            npt.assert_allclose(
                                output,
                                np.ones([1] * D), atol=1e-5)

                            data = util.dirac([3] + [1] * (D - 1),
                                              device=device, dtype=dtype)
                            filt = xp.ones([2] + [1] * (D - 1), dtype=dtype)
                            output = backend.to_device(conv.convolve(
                                data, filt, mode=mode))
                            npt.assert_allclose(
                                output,
                                np.ones([2] + [1] * (D - 1)), atol=1e-5)

                            data = util.dirac([1, 3] + [1] * (D - 1),
                                              device=device,
                                              dtype=dtype)
                            filt = xp.ones([2, 1, 3] + [1] * (D - 1),
                                           dtype=dtype)
                            output = backend.to_device(
                                conv.convolve(data, filt,
                                              mode=mode,
                                              multi_channel=True),
                                backend.cpu_device)
                            npt.assert_allclose(
                                output,
                                np.ones([2, 1] + [1] * (D - 1)),
                                atol=1e-5)

                            data = util.dirac([1, 3] + [1] * (D - 1),
                                              device=device,
                                              dtype=dtype)
                            filt = xp.ones([2, 1, 3] + [1] * (D - 1),
                                           dtype=dtype)
                            strides = [2] + [1] * (D - 1)
                            output = backend.to_device(
                                conv.convolve(data, filt,
                                              mode=mode, strides=strides,
                                              multi_channel=True),
                                backend.cpu_device)
                            npt.assert_allclose(
                                output,
                                np.ones([2, 1] + [1] * (D - 1)),
                                atol=1e-5)
Beispiel #3
0
    def test_convolve_full(self):
        mode = 'full'
        devices = [backend.cpu_device]
        if config.cupy_enabled:
            devices.append(backend.Device(0))
            dtypes = [np.float32, np.float64, np.complex64, np.complex128]

        for device in devices:
            xp = device.xp
            with device:
                for dtype in dtypes:
                    with self.subTest(dtype=dtype, device=device):
                        data = util.dirac([1, 3], device=device, dtype=dtype)
                        filt = xp.ones([1, 3], dtype=dtype)
                        output = backend.to_device(
                            conv.convolve(data, filt, mode=mode))
                        npt.assert_allclose(output, [[0, 1, 1, 1, 0]],
                                            atol=1e-5)

                        data = util.dirac([1, 3], device=device, dtype=dtype)
                        filt = xp.ones([1, 2], dtype=dtype)
                        output = backend.to_device(
                            conv.convolve(data, filt, mode=mode))
                        npt.assert_allclose(output, [[0, 1, 1, 0]], atol=1e-5)

                        data = util.dirac([1, 1, 3],
                                          device=device,
                                          dtype=dtype)
                        filt = xp.ones([2, 1, 1, 3], dtype=dtype)
                        output = backend.to_device(
                            conv.convolve(data,
                                          filt,
                                          mode=mode,
                                          multi_channel=True),
                            backend.cpu_device)
                        npt.assert_allclose(
                            output, [[[0, 1, 1, 1, 0]], [[0, 1, 1, 1, 0]]],
                            atol=1e-5)

                        data = util.dirac([1, 1, 3],
                                          device=device,
                                          dtype=dtype)
                        filt = xp.ones([2, 1, 1, 3], dtype=dtype)
                        strides = [1, 2]
                        output = backend.to_device(
                            conv.convolve(data,
                                          filt,
                                          mode=mode,
                                          strides=strides,
                                          multi_channel=True))
                        npt.assert_allclose(output, [[[0, 1, 0]], [[0, 1, 0]]],
                                            atol=1e-5)
Beispiel #4
0
 def _apply(self, input):
     data = backend.to_device(self.data, backend.get_device(input))
     return conv.convolve(data,
                          input,
                          mode=self.mode,
                          strides=self.strides,
                          multi_channel=self.multi_channel)
Beispiel #5
0
 def _apply(self, input):
     device = backend.get_device(input)
     filt = backend.to_device(self.filt, device)
     with device:
         return conv.convolve(input, filt, mode=self.mode,
                              strides=self.strides,
                              multi_channel=self.multi_channel)
Beispiel #6
0
    def _apply(self, input):
        device = backend.get_device(input)
        x = backend.to_device(self.x, backend.get_device(input))
        with device:
            x = x.astype(input.dtype, copy=False)

        return conv.convolve(x,
                             input,
                             mode=self.mode,
                             input_multi_channel=self.input_multi_channel,
                             output_multi_channel=self.output_multi_channel)
Beispiel #7
0
 def _apply(self, input):
     return conv.convolve(input,
                          self.filt,
                          mode=self.mode,
                          strides=self.strides,
                          multi_channel=self.multi_channel)
Beispiel #8
0
 def _apply(self, input):
     return conv.convolve(self.x,
                          input,
                          mode=self.mode,
                          input_multi_channel=self.input_multi_channel,
                          output_multi_channel=self.output_multi_channel)