spline_linear_gpu_test.py 2.68 KB
Newer Older
rusty1s's avatar
rename  
rusty1s committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import unittest

import torch
from numpy.testing import assert_equal

from .spline_cpu import spline_cpu

if torch.cuda.is_available():
    from .spline_linear_gpu import spline_linear_gpu


class SplineLinearGPUTest(unittest.TestCase):
    @unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
    def test_open_spline(self):
        input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
        kernel_size = torch.LongTensor([5])
        is_open_spline = torch.LongTensor([1])

        a1, i1 = spline_cpu(input, kernel_size, is_open_spline, 1)

        a2, i2 = spline_linear_gpu(input.cuda(),
                                   kernel_size.cuda(), is_open_spline.cuda(),
                                   5)

        assert_equal(a1.numpy(), a2.cpu().numpy())
        assert_equal(i1.numpy(), i2.cpu().numpy())

    @unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
    def test_closed_spline(self):
        input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
        kernel_size = torch.LongTensor([4])
        is_open_spline = torch.LongTensor([0])

        a1, i1 = spline_cpu(input, kernel_size, is_open_spline, 1)

        a2, i2 = spline_linear_gpu(input.cuda(),
                                   kernel_size.cuda(), is_open_spline.cuda(),
                                   4)

        assert_equal(a1.numpy(), a2.cpu().numpy())
        assert_equal(i1.numpy(), i2.cpu().numpy())

    @unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
    def test_spline_2d(self):
        input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
        input = torch.stack([input, input], dim=1)
        kernel_size = torch.LongTensor([5, 4])
        is_open_spline = torch.LongTensor([1, 0])

        a1, i1 = spline_cpu(input, kernel_size, is_open_spline, 1)
        a2, i2 = spline_linear_gpu(input.cuda(),
                                   kernel_size.cuda(),
                                   is_open_spline.cuda(), 20)

        assert_equal(a1.numpy(), a2.cpu().numpy())
        # assert_equal(i1.numpy(), i2.cpu().numpy())

    @unittest.skipIf(not torch.cuda.is_available(), 'no GPU')
    def test_spline_3d(self):
        input = torch.FloatTensor([0, 0.05, 0.25, 0.5, 0.75, 0.95, 1])
        input = torch.stack([input, input, input], dim=1)
        kernel_size = torch.LongTensor([5, 4, 4])
        is_open_spline = torch.LongTensor([1, 0, 0])
        a1, i1 = spline_cpu(input, kernel_size, is_open_spline, 1)

        a2, i2 = spline_linear_gpu(input.cuda(),
                                   kernel_size.cuda(),
                                   is_open_spline.cuda(), 80)

        # assert_equal(a1.numpy(), a2.cpu().numpy())
        # assert_equal(i1.numpy(), i2.cpu().numpy())