-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathtest_torch.py
56 lines (46 loc) · 1.88 KB
/
test_torch.py
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
import torch
import numpy as np
from torch.autograd import gradcheck
from torch_radon import Radon
from unittest import TestCase
from .utils import generate_random_images
class TestTorch(TestCase):
def test_differentiation(self):
device = torch.device('cuda')
x = torch.FloatTensor(1, 64, 64).to(device)
x.requires_grad = True
angles = torch.FloatTensor(np.linspace(0, 2 * np.pi, 10).astype(np.float32)).to(device)
radon = Radon(64, angles)
# check that backward is implemented for fp and bp
y = radon.forward(x)
z = torch.mean(radon.backprojection(y))
z.backward()
self.assertIsNotNone(x.grad)
def test_shapes(self):
"""
Check using channels is ok
"""
device = torch.device('cuda')
angles = torch.FloatTensor(np.linspace(0, 2 * np.pi, 10).astype(np.float32)).to(device)
radon = Radon(64, angles)
# test with 2 batch dimensions
x = torch.FloatTensor(2, 3, 64, 64).to(device)
y = radon.forward(x)
self.assertEqual(y.size(), (2, 3, 10, 64))
z = radon.backprojection(y)
self.assertEqual(z.size(), (2, 3, 64, 64))
# no batch dimensions
x = torch.FloatTensor(64, 64).to(device)
y = radon.forward(x)
self.assertEqual(y.size(), (10, 64))
z = radon.backprojection(y)
self.assertEqual(z.size(), (64, 64))
# def test_gradients(self):
# device = torch.device('cuda')
# radon = Radon(64).to(device)
# x = torch.FloatTensor(generate_random_images(1, 64)).to(device)
# x.requires_grad = True
# angles = torch.FloatTensor(np.linspace(0, 2 * np.pi, 10).astype(np.float32)).to(device)
# def f(xx):
# return radon.backprojection(radon.forward(xx, angles), angles)
# self.assertEqual(gradcheck(f, x, 1e-1, 1e-3, 1e-2), True)