-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConversionTestCase.py
74 lines (60 loc) · 2.36 KB
/
ConversionTestCase.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import unittest
import numpy as np
import torch
# for loading voxel grid from MATLAB file
import scipy.io as sio
import voxel2layer as v2lnp
import voxel2layer_torch as v2lt
class ConversionTestCase(unittest.TestCase):
def setUp(self):
# load sample shape as voxel representation
d = sio.loadmat('./data/chair.mat')
self.voxelnp = d['voxel']
self.voxelt = torch.from_numpy(self.voxelnp)
self.shlnp = d['shl']
self.shlt = torch.from_numpy(self.shlnp.astype(np.int32)).to(torch.int16)
pass
def tearDown(self):
pass
def test_encode_numpy(self):
""" Tests converting from voxel to shape layer representation using numpy. """
shl = v2lnp.encode_shape(self.voxelnp)
self.assertTrue(np.all(shl == self.shlnp))
pass
def test_decode_numpy(self):
""" Tests converting from shape layer to voxel representation using numpy. """
voxel = v2lnp.decode_shape(self.shlnp)
# sio.savemat('decode_numpy.mat', {'voxel':voxel, 'gt':self.voxelnp})
self.assertTrue(np.all(voxel == self.voxelnp))
pass
def test_encode_torch(self):
""" Tests converting from voxel to shape layer representation using torch. """
shl = v2lt.encode_shape(self.voxelt)
# sio.savemat('encode_torch.mat', {'shl':shl.numpy(), 'gt':self.shlt.numpy()})
self.assertTrue((shl == self.shlt).all())
pass
def test_decode_torch(self):
""" Tests converting from shape layer to voxel representation using torch. """
voxel = v2lt.decode_shape(self.shlt)
# sio.savemat('decode_torch.mat', {'voxel':voxel.numpy(), 'gt':self.voxelt.numpy()})
self.assertTrue((voxel == self.voxelt).all())
pass
def test_roundtrip_numpy(self):
""" Tests converting from voxel to shape layer and back using numpy."""
voxel = v2lnp.decode_shape(v2lnp.encode_shape(self.voxelnp, 2))
self.assertTrue(np.all(voxel == self.voxelnp))
pass
def test_roundtrip_torch(self):
""" Tests converting from voxel to shape layer and back using torch."""
voxel = v2lt.decode_shape(v2lt.encode_shape(self.voxelt, 2))
self.assertTrue((voxel == self.voxelt).all())
pass
def test_shapelayer_conversion(self):
""" Tests modifying the shape layer representation for better alignment.
"""
shlx = v2lt.shl2shlx(self.shlt.clone().permute(2,0,1).reshape(1,6,128,128))
shl = v2lt.shlx2shl(shlx).reshape(6,128,128).permute(1,2,0)
self.assertTrue((shl == self.shlt).all())
pass
if __name__ == '__main__':
unittest.main()