def test_assert(func, params, expected): gpu_test = ("torch_gpu",) if torch.cuda.is_available() else () for b in ("torch", "numpy") + gpu_test: if b == "torch_gpu": m = func(*params, device="cuda:0", backend="torch") else: m = func(*params, backend=b) assert_allclose(m, expected, type_test=False, rtol=1e-2 if is_tf32_env() else 1e-5, atol=1e-5)
# distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import torch from parameterized import parameterized from monai.networks import normalize_transform, to_norm_affine from monai.networks.layers import AffineTransform from tests.utils import is_tf32_env _rtol = 1e-4 if not is_tf32_env() else 5e-3 TEST_NORM_CASES = [ [(4, 5), True, [[[0.666667, 0, -1], [0, 0.5, -1], [0, 0, 1]]]], [ (2, 4, 5), True, [[[2.0, 0.0, 0.0, -1.0], [0.0, 0.6666667, 0.0, -1.0], [0.0, 0.0, 0.5, -1.0], [0.0, 0.0, 0.0, 1.0]]], ], [(4, 5), False, [[[0.5, 0.0, -0.75], [0.0, 0.4, -0.8], [0.0, 0.0, 1.0]]]], [(2, 4, 5), False, [[[1.0, 0.0, 0.0, -0.5], [0.0, 0.5, 0.0, -0.75], [0.0, 0.0, 0.4, -0.8], [0.0, 0.0, 0.0, 1.0]]]], ]
# Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import numpy as np import torch from parameterized import parameterized from monai.transforms import RandAffineGrid from tests.utils import TEST_NDARRAYS, assert_allclose, is_tf32_env _rtol = 1e-1 if is_tf32_env() else 1e-4 TESTS = [] for p in TEST_NDARRAYS: for device in [None, "cpu", "cuda" ] if torch.cuda.is_available() else [None, "cpu"]: TESTS.append([{ "device": device }, { "grid": p(torch.ones((3, 3, 3))) }, p(np.ones((3, 3, 3)))]) TESTS.append([ { "rotate_range": (1, 2), "translate_range": (3, 3, 3)
# distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest from itertools import product import numpy as np import torch from parameterized import parameterized from monai.transforms import RandSmoothDeformd, RandSmoothFieldAdjustContrastd, RandSmoothFieldAdjustIntensityd from tests.utils import TEST_NDARRAYS, assert_allclose, is_tf32_env _rtol = 5e-3 if is_tf32_env() else 1e-4 INPUT_SHAPES = ((1, 8, 8), (2, 8, 8), (1, 8, 8, 8)) TESTS_CONTRAST = [] TESTS_INTENSITY = [] TESTS_DEFORM = [] KEY = "test" for arr_type, shape in product(TEST_NDARRAYS, INPUT_SHAPES): in_arr = arr_type(np.ones(shape, np.float32)) exp_arr = arr_type(np.ones(shape, np.float32)) rand_size = (4, ) * (len(shape) - 1) device = torch.device("cpu")
TEST_CASE_2 = [{ "spatial_size": 6, "mode": "trilinear", "align_corners": True }, (2, 4, 6)] TEST_CASE_3 = [{"spatial_size": 15, "anti_aliasing": True}, (6, 10, 15)] TEST_CASE_4 = [{ "spatial_size": 6, "anti_aliasing": True, "anti_aliasing_sigma": 2.0 }, (2, 4, 6)] diff_t = 0.3 if is_tf32_env() else 0.2 class TestResize(NumpyImageTestCase2D): def test_invalid_inputs(self): with self.assertRaises(ValueError): resize = Resize(spatial_size=(128, 128, 3), mode="order") resize(self.imt[0]) with self.assertRaises(ValueError): resize = Resize(spatial_size=(128, ), mode="order") resize(self.imt[0]) @parameterized.expand([ ((32, -1), "area", True), ((32, 32), "area", False),
# http://www.apache.org/licenses/LICENSE-2.0 # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import unittest import torch from parameterized import parameterized from monai.networks.layers import LLTM from tests.utils import SkipIfNoModule, is_tf32_env _rtol = 0.001 if is_tf32_env() else 0.0001 TEST_CASE_1 = [ { "input_features": 32, "state_size": 2 }, torch.tensor([[-0.1622, 0.1663], [0.5465, 0.0459], [-0.1436, 0.6171], [0.3632, -0.0111]]), torch.tensor([[-1.3773, 0.3348], [0.8353, 1.3064], [-0.2179, 4.1739], [1.3045, -0.1444]]), ] class TestLLTM(unittest.TestCase): @parameterized.expand([TEST_CASE_1])