def test_load_ts_module(self, bundle_files, bundle_name, repo, device, model_file): with skip_if_downloading_fails(): # load ts module with tempfile.TemporaryDirectory() as tempdir: # load ts module model_ts, metadata, extra_file_dict = load( name=bundle_name, model_file=model_file, load_ts_module=True, bundle_dir=tempdir, repo=repo, progress=False, device=device, config_files=("network.json", ), ) # prepare and test ts input_tensor = torch.load(os.path.join(tempdir, bundle_name, bundle_files[1]), map_location=device) output = model_ts.forward(input_tensor) expected_output = torch.load(os.path.join( tempdir, bundle_name, bundle_files[0]), map_location=device) torch.testing.assert_allclose(output, expected_output) # test metadata self.assertTrue(metadata["pytorch_version"] == "1.7.1") # test extra_file_dict self.assertTrue("network.json" in extra_file_dict.keys())
def test_actions(self): testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testing_data") config_dict = testing_data_config("images", "mednist") url = config_dict["url"] filepath = Path(testing_dir) / "MedNIST.tar.gz" output_dir = Path(testing_dir) hash_val, hash_type = config_dict["hash_val"], config_dict["hash_type"] with skip_if_downloading_fails(): download_and_extract(url, filepath, output_dir, hash_val=hash_val, hash_type=hash_type) download_and_extract(url, filepath, output_dir, hash_val=hash_val, hash_type=hash_type) wrong_md5 = "0" with self.assertLogs(logger="monai.apps", level="ERROR"): try: download_url(url, filepath, wrong_md5) except (ContentTooShortError, HTTPError, RuntimeError) as e: if isinstance(e, RuntimeError): # FIXME: skip MD5 check as current downloading method may fail self.assertTrue(str(e).startswith("md5 check")) return # skipping this test due the network connection errors try: extractall(filepath, output_dir, wrong_md5) except RuntimeError as e: self.assertTrue(str(e).startswith("md5 check"))
def test_url_download_bundle(self, bundle_files, bundle_name, url, hash_val): with skip_if_downloading_fails(): # download a single file from url, also use `args_file` with tempfile.TemporaryDirectory() as tempdir: def_args = { "name": bundle_name, "bundle_dir": tempdir, "url": "" } def_args_file = os.path.join(tempdir, "def_args.json") parser = ConfigParser() parser.export_config_file(config=def_args, filepath=def_args_file) cmd = [ "coverage", "run", "-m", "monai.bundle", "download", "--args_file", def_args_file ] cmd += ["--url", url] subprocess.check_call(cmd) for file in bundle_files: file_path = os.path.join(tempdir, bundle_name, file) self.assertTrue(os.path.exists(file_path)) if file == "network.json": self.assertTrue( check_hash(filepath=file_path, val=hash_val))
def test_values(self): testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testing_data") transform = Compose([ LoadImaged(keys="image"), AddChanneld(keys="image"), ScaleIntensityd(keys="image"), ToTensord(keys=["image", "label"]), ]) def _test_dataset(dataset): self.assertEqual( len(dataset), int(MEDNIST_FULL_DATASET_LENGTH * dataset.test_frac)) self.assertTrue("image" in dataset[0]) self.assertTrue("label" in dataset[0]) self.assertTrue(PostFix.meta("image") in dataset[0]) self.assertTupleEqual(dataset[0]["image"].shape, (1, 64, 64)) with skip_if_downloading_fails(): data = MedNISTDataset(root_dir=testing_dir, transform=transform, section="test", download=True, copy_cache=False) _test_dataset(data) # testing from data = MedNISTDataset(root_dir=Path(testing_dir), transform=transform, section="test", download=False) self.assertEqual(data.get_num_classes(), 6) _test_dataset(data) data = MedNISTDataset(root_dir=testing_dir, section="test", download=False) self.assertTupleEqual(data[0]["image"].shape, (64, 64)) # test same dataset length with different random seed data = MedNISTDataset(root_dir=testing_dir, transform=transform, section="test", download=False, seed=42) _test_dataset(data) self.assertEqual(data[0]["class_name"], "AbdomenCT") self.assertEqual(data[0]["label"].cpu().item(), 0) shutil.rmtree(os.path.join(testing_dir, "MedNIST")) try: MedNISTDataset(root_dir=testing_dir, transform=transform, section="test", download=False) except RuntimeError as e: print(str(e)) self.assertTrue(str(e).startswith("Cannot find dataset directory"))
def test_download(self, idx): with skip_if_downloading_fails(): with self.assertLogs(level="INFO", logger="monai.apps"): download_mmar(idx) download_mmar(idx, progress=False) # repeated to check caching with tempfile.TemporaryDirectory() as tmp_dir: download_mmar(idx, mmar_dir=tmp_dir, progress=False) download_mmar(idx, mmar_dir=Path(tmp_dir), progress=False, version=1) # repeated to check caching self.assertTrue(os.path.exists(os.path.join(tmp_dir, idx)))
def test_default(self, key, file_type): with tempfile.TemporaryDirectory() as tmp_dir: with skip_if_downloading_fails(): img_spec = testing_data_config("images", key) download_and_extract( img_spec["url"], output_dir=tmp_dir, hash_val=img_spec["hash_val"], hash_type=img_spec["hash_type"], file_type=file_type, )
def test_shape(self, input_param, input_shape, expected_shape): device = "cuda" if torch.cuda.is_available() else "cpu" with skip_if_downloading_fails(): net = EfficientNetBN(**input_param).to(device) # run inference with random tensor with eval_mode(net): result = net(torch.randn(input_shape).to(device)) # check output shape self.assertEqual(result.shape, expected_shape)
def test_shape(self, input_param, input_shape, expected_shapes): device = "cuda" if torch.cuda.is_available() else "cpu" with skip_if_downloading_fails(): net = EfficientNetBNFeatures(**input_param).to(device) # run inference with random tensor with eval_mode(net): features = net(torch.randn(input_shape).to(device)) # check output shape self.assertEqual(len(features), len(expected_shapes)) for feature, expected_shape in zip(features, expected_shapes): self.assertEqual(feature.shape, torch.Size(expected_shape))
def test_load_weights(self, bundle_files, bundle_name, repo, device, model_file): with skip_if_downloading_fails(): # download bundle, and load weights from the downloaded path with tempfile.TemporaryDirectory() as tempdir: # load weights weights = load( name=bundle_name, model_file=model_file, bundle_dir=tempdir, repo=repo, progress=False, device=device, ) # prepare network with open(os.path.join(tempdir, bundle_name, bundle_files[2])) as f: net_args = json.load(f)["network_def"] model_name = net_args["_target_"] del net_args["_target_"] model = nets.__dict__[model_name](**net_args) model.to(device) model.load_state_dict(weights) model.eval() # prepare data and test input_tensor = torch.load(os.path.join(tempdir, bundle_name, bundle_files[4]), map_location=device) output = model.forward(input_tensor) expected_output = torch.load(os.path.join( tempdir, bundle_name, bundle_files[3]), map_location=device) torch.testing.assert_allclose(output, expected_output) # load instantiated model directly and test, since the bundle has been downloaded, # there is no need to input `repo` model_2 = load( name=bundle_name, model_file=model_file, bundle_dir=tempdir, progress=False, device=device, net_name=model_name, **net_args, ) model_2.eval() output_2 = model_2.forward(input_tensor) torch.testing.assert_allclose(output_2, expected_output)
def setUp(self): set_determinism(seed=0) self.data_dir = os.path.join( os.path.dirname(os.path.realpath(__file__)), "testing_data") data_dir = os.path.join(self.data_dir, "MedNIST") dataset_file = os.path.join(self.data_dir, "MedNIST.tar.gz") if not os.path.exists(data_dir): with skip_if_downloading_fails(): data_spec = testing_data_config("images", "mednist") download_and_extract( data_spec["url"], dataset_file, self.data_dir, hash_val=data_spec["hash_val"], hash_type=data_spec["hash_type"], ) assert os.path.exists(data_dir) class_names = sorted(x for x in os.listdir(data_dir) if os.path.isdir(os.path.join(data_dir, x))) image_files = [[ os.path.join(data_dir, class_name, x) for x in sorted(os.listdir(os.path.join(data_dir, class_name))) ] for class_name in class_names] image_file_list, image_classes = [], [] for i, _ in enumerate(class_names): image_file_list.extend(image_files[i]) image_classes.extend([i] * len(image_files[i])) # split train, val, test valid_frac, test_frac = 0.1, 0.1 self.train_x, self.train_y = [], [] self.val_x, self.val_y = [], [] self.test_x, self.test_y = [], [] for i in range(len(image_classes)): rann = np.random.random() if rann < valid_frac: self.val_x.append(image_file_list[i]) self.val_y.append(image_classes[i]) elif rann < test_frac + valid_frac: self.test_x.append(image_file_list[i]) self.test_y.append(image_classes[i]) else: self.train_x.append(image_file_list[i]) self.train_y.append(image_classes[i]) self.device = "cuda:0" if torch.cuda.is_available() else "cpu:0"
def test_values(self): testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testing_data") train_transform = Compose([ LoadImaged(keys=["image", "label"]), AddChanneld(keys=["image", "label"]), ScaleIntensityd(keys="image"), ToTensord(keys=["image", "label"]), ]) val_transform = LoadImaged(keys=["image", "label"]) def _test_dataset(dataset): self.assertEqual(len(dataset), 52) self.assertTrue("image" in dataset[0]) self.assertTrue("label" in dataset[0]) self.assertTrue(PostFix.meta("image") in dataset[0]) self.assertTupleEqual(dataset[0]["image"].shape, (1, 34, 49, 41)) cvdataset = CrossValidation( dataset_cls=DecathlonDataset, nfolds=5, seed=12345, root_dir=testing_dir, task="Task04_Hippocampus", section="validation", transform=train_transform, download=True, ) with skip_if_downloading_fails(): data = cvdataset.get_dataset(folds=0) _test_dataset(data) # test training data for fold [1, 2, 3, 4] of 5 splits data = cvdataset.get_dataset(folds=[1, 2, 3, 4]) self.assertTupleEqual(data[0]["image"].shape, (1, 35, 52, 33)) self.assertEqual(len(data), 208) # test train / validation for fold 4 of 5 splits data = cvdataset.get_dataset(folds=[4], transform=val_transform, download=False) # val_transform doesn't add the channel dim to shape self.assertTupleEqual(data[0]["image"].shape, (38, 53, 30)) self.assertEqual(len(data), 52) data = cvdataset.get_dataset(folds=[0, 1, 2, 3]) self.assertTupleEqual(data[0]["image"].shape, (1, 34, 49, 41)) self.assertEqual(len(data), 208)
def test_non_default_shapes(self, input_param, input_shape, expected_shape): device = "cuda" if torch.cuda.is_available() else "cpu" with skip_if_downloading_fails(): net = EfficientNetBN(**input_param).to(device) # override input shape with different variations num_dims = len(input_shape) - 2 non_default_sizes = [128, 256, 512] for candidate_size in non_default_sizes: input_shape = input_shape[0:2] + (candidate_size,) * num_dims # run inference with random tensor with eval_mode(net): result = net(torch.randn(input_shape).to(device)) # check output shape self.assertEqual(result.shape, expected_shape)
def test_download_bundle(self, bundle_files, bundle_name, repo, hash_val): with skip_if_downloading_fails(): # download a whole bundle from github releases with tempfile.TemporaryDirectory() as tempdir: cmd = [ "coverage", "run", "-m", "monai.bundle", "download", "--name", bundle_name, "--source", "github" ] cmd += [ "--bundle_dir", tempdir, "--repo", repo, "--progress", "False" ] subprocess.check_call(cmd) for file in bundle_files: file_path = os.path.join(tempdir, bundle_name, file) self.assertTrue(os.path.exists(file_path)) if file == "network.json": self.assertTrue( check_hash(filepath=file_path, val=hash_val))
def test_lr_finder(self): # 0.001 gives 54 examples with skip_if_downloading_fails(): train_ds = MedNISTDataset( root_dir=self.root_dir, transform=self.transforms, section="validation", val_frac=0.001, download=True, num_workers=10, ) train_loader = DataLoader(train_ds, batch_size=300, shuffle=True, num_workers=10) num_classes = train_ds.get_num_classes() model = DenseNet( spatial_dims=2, in_channels=1, out_channels=num_classes, init_features=2, growth_rate=2, block_config=(2,) ) loss_function = torch.nn.CrossEntropyLoss() learning_rate = 1e-5 optimizer = torch.optim.Adam(model.parameters(), learning_rate) lr_finder = LearningRateFinder( model=model, optimizer=optimizer, criterion=loss_function, device=device, pickle_module=pickle, pickle_protocol=4, ) lr_finder.range_test(train_loader, val_loader=train_loader, end_lr=10, num_iter=5) print(lr_finder.get_steepest_gradient(0, 0)[0]) if has_matplotlib: ax = plt.subplot() plt.show(block=False) lr_finder.plot(0, 0, ax=ax) # to inspect the loss-learning rate graph plt.pause(3) plt.close() lr_finder.reset() # to reset the model and optimizer to their initial state
def test_load_ckpt(self, input_args, expected_name, expected_val): with skip_if_downloading_fails(): output = load_from_mmar(**input_args) self.assertEqual(output.__class__.__name__, expected_name) x = next(output.parameters()) # verify the first element np.testing.assert_allclose(x[0][0].detach().cpu().numpy(), expected_val, rtol=1e-3, atol=1e-3)
def test_script(self): with skip_if_downloading_fails(): net = EfficientNetBN(model_name="efficientnet-b0", spatial_dims=2, in_channels=3, num_classes=1000) net.set_swish(memory_efficient=False) # at the moment custom memory efficient swish is not exportable with jit test_data = torch.randn(1, 3, 224, 224) test_script_save(net, test_data)
def test_values(self): testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testing_data") download_len = 1 val_frac = 1.0 collection = "QIN-PROSTATE-Repeatability" transform = Compose([ LoadImaged(keys=["image", "seg"], reader="PydicomReader", label_dict=TCIA_LABEL_DICT[collection]), AddChanneld(keys="image"), ScaleIntensityd(keys="image"), ]) def _test_dataset(dataset): self.assertEqual(len(dataset), int(download_len * val_frac)) self.assertTrue("image" in dataset[0]) self.assertTrue("seg" in dataset[0]) self.assertTrue(isinstance(dataset[0]["image"], MetaTensor)) self.assertTupleEqual(dataset[0]["image"].shape, (1, 256, 256, 24)) self.assertTupleEqual(dataset[0]["seg"].shape, (256, 256, 24, 4)) with skip_if_downloading_fails(): data = TciaDataset( root_dir=testing_dir, collection=collection, transform=transform, section="validation", download=True, download_len=download_len, copy_cache=False, val_frac=val_frac, ) _test_dataset(data) data = TciaDataset( root_dir=testing_dir, collection=collection, transform=transform, section="validation", download=False, val_frac=val_frac, ) _test_dataset(data) self.assertTrue(data[0]["image"].meta["filename_or_obj"].endswith( "QIN-PROSTATE-Repeatability/PCAMPMRI-00015/1901/image")) self.assertTrue(data[0]["seg"].meta["filename_or_obj"].endswith( "QIN-PROSTATE-Repeatability/PCAMPMRI-00015/1901/seg")) # test validation without transforms data = TciaDataset(root_dir=testing_dir, collection=collection, section="validation", download=False, val_frac=val_frac) self.assertTupleEqual(data[0]["image"].shape, (256, 256, 24)) self.assertEqual(len(data), int(download_len * val_frac)) data = TciaDataset(root_dir=testing_dir, collection=collection, section="validation", download=False, val_frac=val_frac) self.assertTupleEqual(data[0]["image"].shape, (256, 256, 24)) self.assertEqual(len(data), download_len) shutil.rmtree(os.path.join(testing_dir, collection)) try: TciaDataset( root_dir=testing_dir, collection=collection, transform=transform, section="validation", download=False, val_frac=val_frac, ) except RuntimeError as e: self.assertTrue(str(e).startswith("Cannot find dataset directory"))
def test_values(self): testing_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), "testing_data") transform = Compose([ LoadImaged(keys=["image", "label"]), AddChanneld(keys=["image", "label"]), ScaleIntensityd(keys="image") ]) def _test_dataset(dataset): self.assertEqual(len(dataset), 52) self.assertTrue("image" in dataset[0]) self.assertTrue("label" in dataset[0]) self.assertTrue(isinstance(dataset[0]["image"], MetaTensor)) self.assertTupleEqual(dataset[0]["image"].shape, (1, 36, 47, 44)) with skip_if_downloading_fails(): data = DecathlonDataset( root_dir=testing_dir, task="Task04_Hippocampus", transform=transform, section="validation", download=True, copy_cache=False, ) _test_dataset(data) data = DecathlonDataset(root_dir=testing_dir, task="Task04_Hippocampus", transform=transform, section="validation", download=False) _test_dataset(data) self.assertTrue(data[0]["image"].meta["filename_or_obj"].endswith( "hippocampus_163.nii.gz")) self.assertTrue(data[0]["label"].meta["filename_or_obj"].endswith( "hippocampus_163.nii.gz")) # test validation without transforms data = DecathlonDataset(root_dir=testing_dir, task="Task04_Hippocampus", section="validation", download=False) self.assertTupleEqual(data[0]["image"].shape, (36, 47, 44)) self.assertEqual(len(data), 52) data = DecathlonDataset(root_dir=testing_dir, task="Task04_Hippocampus", section="training", download=False) self.assertTupleEqual(data[0]["image"].shape, (34, 56, 31)) self.assertEqual(len(data), 208) # test dataset properties data = DecathlonDataset(root_dir=Path(testing_dir), task="Task04_Hippocampus", section="validation", download=False) properties = data.get_properties(keys="labels") self.assertDictEqual(properties["labels"], { "0": "background", "1": "Anterior", "2": "Posterior" }) shutil.rmtree(os.path.join(testing_dir, "Task04_Hippocampus")) try: DecathlonDataset( root_dir=testing_dir, task="Task04_Hippocampus", transform=transform, section="validation", download=False, ) except RuntimeError as e: print(str(e)) self.assertTrue(str(e).startswith("Cannot find dataset directory"))