def test_torch(self, init_param, img: torch.Tensor, track_meta: bool, device): set_track_meta(track_meta) img = img.to(device) xform = Flip(init_param) res = xform(img) self.assertEqual(img.shape, res.shape) if track_meta: self.assertIsInstance(res, MetaTensor) else: self.assertNotIsInstance(res, MetaTensor) self.assertIsInstance(res, torch.Tensor) with self.assertRaisesRegex(ValueError, "MetaTensor"): xform.inverse(res)
def test_correct_results(self, _, spatial_axis): flip = Flip(spatial_axis=spatial_axis) expected = list() for channel in self.imt[0]: expected.append(np.flip(channel, spatial_axis)) expected = np.stack(expected) self.assertTrue(np.allclose(expected, flip(self.imt[0])))
def test_tranform_array(self, input): transforms = Compose( [Range("random flip")(Flip()), Range()(ToTensor())]) # Apply transforms output = transforms(input) # Decorate with NVTX Range transforms1 = Range()(transforms) transforms2 = Range("Transforms2")(transforms) transforms3 = Range(name="Transforms3", methods="__call__")(transforms) # Apply transforms with Range output1 = transforms1(input) output2 = transforms2(input) output3 = transforms3(input) # Check the outputs self.assertIsInstance(output, torch.Tensor) self.assertIsInstance(output1, torch.Tensor) self.assertIsInstance(output2, torch.Tensor) self.assertIsInstance(output3, torch.Tensor) np.testing.assert_equal(output.numpy(), output1.numpy()) np.testing.assert_equal(output.numpy(), output1.numpy()) np.testing.assert_equal(output.numpy(), output3.numpy())
def test_nvtx_transfroms_array(self, input): # with prob == 0.0 transforms = Compose([ RandMark("Mark: Transforms Start!"), RandRangePush("Range: RandFlip"), RandFlip(prob=0.0), RandRangePop(), RangePush("Range: ToTensor"), ToTensor(), RangePop(), Mark("Mark: Transforms End!"), ]) output = transforms(input) self.assertIsInstance(output, torch.Tensor) np.testing.assert_array_equal(input, output) # with prob == 1.0 transforms = Compose([ RandMark("Mark: Transforms Start!"), RandRangePush("Range: RandFlip"), RandFlip(prob=1.0), RandRangePop(), RangePush("Range: ToTensor"), ToTensor(), RangePop(), Mark("Mark: Transforms End!"), ]) output = transforms(input) self.assertIsInstance(output, torch.Tensor) np.testing.assert_array_equal(input, Flip()(output.numpy()))
def test_nvtx_transfroms_dict(self, input): # with prob == 0.0 transforms = Compose([ RandMarkD("Mark: Transforms (p=0) Start!"), RandRangePushD("Range: RandFlipD"), RandFlipD(keys="image", prob=0.0), RandRangePopD(), RangePushD("Range: ToTensorD"), ToTensorD(keys=("image")), RangePopD(), MarkD("Mark: Transforms (p=0) End!"), ]) output = transforms(input) self.assertIsInstance(output["image"], torch.Tensor) np.testing.assert_array_equal(input["image"], output["image"]) # with prob == 1.0 transforms = Compose([ RandMarkD("Mark: Transforms (p=1) Start!"), RandRangePushD("Range: RandFlipD"), RandFlipD(keys="image", prob=1.0), RandRangePopD(), RangePushD("Range: ToTensorD"), ToTensorD(keys=("image")), RangePopD(), MarkD("Mark: Transforms (p=1) End!"), ]) output = transforms(input) self.assertIsInstance(output["image"], torch.Tensor) np.testing.assert_array_equal(input["image"], Flip()(output["image"].numpy()))
def test_inverse_array(self, use_compose, dtype, device): img: MetaTensor tr = Compose([ AddChannel(), Orientation("RAS"), Flip(1), Spacing([1.0, 1.2, 0.9], align_corners=False) ]) num_invertible = len( [i for i in tr.transforms if isinstance(i, InvertibleTransform)]) # forward img = tr(self.get_image(dtype, device)) self.assertEqual(len(img.applied_operations), num_invertible) # inverse with Compose if use_compose: img = tr.inverse(img) self.assertEqual(len(img.applied_operations), 0) # inverse individually else: _tr: InvertibleTransform num_to_inverse = num_invertible for _tr in tr.transforms[::-1]: if isinstance(_tr, InvertibleTransform): img = _tr.inverse(img) num_to_inverse -= 1 self.assertEqual(len(img.applied_operations), num_to_inverse)
def test_correct_results(self, _, spatial_axis): for p in TEST_NDARRAYS: im = p(self.imt[0]) flip = Flip(spatial_axis=spatial_axis) expected = [np.flip(channel, spatial_axis) for channel in self.imt[0]] expected = np.stack(expected) result = flip(im) assert_allclose(result, p(expected))
def test_correct_results(self, _, spatial_axis): for p in TEST_NDARRAYS_ALL: im = p(self.imt[0]) flip = Flip(spatial_axis=spatial_axis) expected = [ np.flip(channel, spatial_axis) for channel in self.imt[0] ] expected = np.stack(expected) result = flip(im) assert_allclose(result, p(expected), type_test="tensor") test_local_inversion(flip, result, im)
def test_tranform_randomized(self, input): # Compose deterministic and randomized transforms transforms = Compose([ Range("flip")(Flip()), Rotate90(), Range()(RandAdjustContrast(prob=0.0)), Range("random flip")(RandFlip(prob=1.0)), ToTensor(), ]) # Apply transforms output = transforms(input) # Decorate with NVTX Range transforms1 = Range()(transforms) transforms2 = Range("Transforms2")(transforms) transforms3 = Range(name="Transforms3", methods="__call__")(transforms) # Apply transforms with Range output1 = transforms1(input) output2 = transforms2(input) output3 = transforms3(input) # Check if the outputs are equal self.assertIsInstance(output, torch.Tensor) self.assertIsInstance(output1, torch.Tensor) self.assertIsInstance(output2, torch.Tensor) self.assertIsInstance(output3, torch.Tensor) np.testing.assert_equal(output.numpy(), output1.numpy()) np.testing.assert_equal(output.numpy(), output2.numpy()) np.testing.assert_equal(output.numpy(), output3.numpy()) # Check if the first randomized is RandAdjustContrast for tran in transforms.transforms: if isinstance(tran, Randomizable): self.assertIsInstance(tran, RandAdjustContrast) break
def test_invalid_inputs(self, _, spatial_axis, raises): with self.assertRaises(raises): flip = Flip(spatial_axis) flip(self.imt[0])
TEST_CASE_ARRAY_0 = [np.random.randn(3, 3)] TEST_CASE_ARRAY_1 = [np.random.randn(3, 10, 10)] TEST_CASE_DICT_0 = [{"image": np.random.randn(3, 3)}] TEST_CASE_DICT_1 = [{"image": np.random.randn(3, 10, 10)}] TEST_CASE_TORCH_0 = [torch.randn(3, 3)] TEST_CASE_TORCH_1 = [torch.randn(3, 10, 10)] TEST_CASE_WRAPPER = [np.random.randn(3, 10, 10)] TEST_CASE_RECURSIVE_0 = [ torch.randn(3, 3), Compose([ ToNumpy(), Flip(), RandAdjustContrast(prob=0.0), RandFlip(prob=1.0), ToTensor() ]), ] TEST_CASE_RECURSIVE_1 = [ torch.randn(3, 3), Compose([ ToNumpy(), Flip(), Compose([RandAdjustContrast(prob=0.0), RandFlip(prob=1.0)]), ToTensor() ]), ]