forked from facebookresearch/vissl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_nested_tensor.py
57 lines (48 loc) · 2.14 KB
/
test_nested_tensor.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
# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
import logging
import unittest
import torch
from vissl.data.collators.collator_helper import MultiDimensionalTensor
logger = logging.getLogger("__name__")
class TestMultiDimensionalTensor(unittest.TestCase):
def test_run(self):
"""
Test the nested tensor works
"""
tensor1 = torch.randn(1, 3, 7, 7)
tensor2 = torch.randn(1, 3, 4, 4)
out = MultiDimensionalTensor.from_tensors([tensor1, tensor2])
padded_tensor, out_mask = out.tensor, out.mask
# check the output shapes are good
self.assertEqual(padded_tensor.shape, (2, 3, 7, 7), padded_tensor.shape)
self.assertEqual(out_mask.shape, (2, 7, 7), out_mask.shape)
# check that the output mask is as expected. The padded
# indexes should have 1.0 value in the mask
self.assertTrue(
out_mask.float().equal(
torch.tensor(
[
[
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
],
[
[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0],
[0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0],
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
[1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0],
],
]
)
)
)