Ejemplo n.º 1
0
    def initialize(self, args):
        """
        `initialize` is called only once when the model is being loaded.
        Implementing `initialize` function is optional. This function allows
        the model to intialize any state associated with this model.
        """

        # Pull model from google drive
        extract_dir = "/models/mednist_class/1"
        tar_save_path = os.path.join(extract_dir, model_filename)
        download_and_extract(gdrive_url,
                             tar_save_path,
                             output_dir=extract_dir,
                             hash_val=md5_check,
                             hash_type="md5")
        # load model configuration
        self.model_config = json.loads(args['model_config'])

        # create inferer engine and load PyTorch model
        inference_device_kind = args.get('model_instance_kind', None)
        logger.info(f"Inference device: {inference_device_kind}")

        self.inference_device = torch.device('cpu')
        if inference_device_kind is None or inference_device_kind == 'CPU':
            self.inference_device = torch.device('cpu')
        elif inference_device_kind == 'GPU':
            inference_device_id = args.get('model_instance_device_id', '0')
            logger.info(f"Inference device id: {inference_device_id}")

            if torch.cuda.is_available():
                self.inference_device = torch.device(
                    f'cuda:{inference_device_id}')
                cudnn.enabled = True
            else:
                logger.error(
                    f"No CUDA device detected. Using device: {inference_device_kind}"
                )

        # create pre-transforms for MedNIST
        self.pre_transforms = Compose([
            LoadImage(reader="PILReader", image_only=True, dtype=np.float32),
            ScaleIntensity(),
            AddChannel(),
            AddChannel(),
            ToTensor(),
            Lambda(func=lambda x: x.to(device=self.inference_device)),
        ])

        # create post-transforms
        self.post_transforms = Compose([
            Lambda(func=lambda x: x.to(device="cpu")),
        ])

        self.inferer = SimpleInferer()

        self.model = torch.jit.load(
            f'{pathlib.Path(os.path.realpath(__file__)).parent}{os.path.sep}model.pt',
            map_location=self.inference_device)
Ejemplo n.º 2
0
    def test_set_data(self):
        data_list1 = list(range(10))

        transform = Compose([
            Lambda(func=lambda x: np.array([x * 10])),
            RandLambda(func=lambda x: x + 1)
        ])

        dataset = CacheDataset(
            data=data_list1,
            transform=transform,
            cache_rate=1.0,
            num_workers=4,
            progress=True,
            copy_cache=not sys.platform == "linux",
        )

        num_workers = 2 if sys.platform == "linux" else 0
        dataloader = DataLoader(dataset=dataset,
                                num_workers=num_workers,
                                batch_size=1)
        for i, d in enumerate(dataloader):
            np.testing.assert_allclose([[data_list1[i] * 10 + 1]], d)
        # simulate another epoch, the cache content should not be modified
        for i, d in enumerate(dataloader):
            np.testing.assert_allclose([[data_list1[i] * 10 + 1]], d)

        # update the datalist and fill the cache content
        data_list2 = list(range(-10, 0))
        dataset.set_data(data=data_list2)
        # rerun with updated cache content
        for i, d in enumerate(dataloader):
            np.testing.assert_allclose([[data_list2[i] * 10 + 1]], d)
Ejemplo n.º 3
0
 def test_correct(self):
     with tempfile.TemporaryDirectory() as temp_dir:
         transforms = Compose([
             LoadImaged(("im1", "im2")),
             EnsureChannelFirstd(("im1", "im2")),
             CopyItemsd(("im2", "im2_meta_dict"),
                        names=("im3", "im3_meta_dict")),
             ResampleToMatchd("im3", "im1_meta_dict"),
             Lambda(update_fname),
             SaveImaged("im3",
                        output_dir=temp_dir,
                        output_postfix="",
                        separate_folder=False),
         ])
         data = transforms({"im1": self.fnames[0], "im2": self.fnames[1]})
         # check that output sizes match
         assert_allclose(data["im1"].shape, data["im3"].shape)
         # and that the meta data has been updated accordingly
         assert_allclose(data["im3"].shape[1:],
                         data["im3_meta_dict"]["spatial_shape"],
                         type_test=False)
         assert_allclose(data["im3_meta_dict"]["affine"],
                         data["im1_meta_dict"]["affine"])
         # check we're different from the original
         self.assertTrue(
             any(i != j
                 for i, j in zip(data["im3"].shape, data["im2"].shape)))
         self.assertTrue(
             any(i != j
                 for i, j in zip(data["im3_meta_dict"]["affine"].flatten(
                 ), data["im2_meta_dict"]["affine"].flatten())))
         # test the inverse
         data = Invertd("im3", transforms, "im3")(data)
         assert_allclose(data["im2"].shape, data["im3"].shape)
Ejemplo n.º 4
0
 def test_correct(self):
     transforms = Compose([
         LoadImaged(("im1", "im2")),
         EnsureChannelFirstd(("im1", "im2")),
         CopyItemsd(("im2"), names=("im3")),
         ResampleToMatchd("im3", "im1"),
         Lambda(update_fname),
         SaveImaged("im3",
                    output_dir=self.tmpdir,
                    output_postfix="",
                    separate_folder=False,
                    resample=False),
     ])
     data = transforms({"im1": self.fnames[0], "im2": self.fnames[1]})
     # check that output sizes match
     assert_allclose(data["im1"].shape, data["im3"].shape)
     # and that the meta data has been updated accordingly
     assert_allclose(data["im3"].affine, data["im1"].affine)
     # check we're different from the original
     self.assertTrue(
         any(i != j for i, j in zip(data["im3"].shape, data["im2"].shape)))
     self.assertTrue(
         any(i != j for i, j in zip(data["im3"].affine.flatten(),
                                    data["im2"].affine.flatten())))
     # test the inverse
     data = Invertd("im3", transforms)(data)
     assert_allclose(data["im2"].shape, data["im3"].shape)
Ejemplo n.º 5
0
    def test_set_data(self):
        data_list1 = list(range(10))

        transform = Lambda(func=lambda x: np.array([x * 10]))

        dataset = SmartCacheDataset(
            data=data_list1,
            transform=transform,
            cache_rate=0.5,
            replace_rate=0.4,
            num_init_workers=4,
            num_replace_workers=2,
            shuffle=False,
            progress=True,
        )

        num_workers = 2 if sys.platform == "linux" else 0
        dataloader = DataLoader(dataset=dataset,
                                num_workers=num_workers,
                                batch_size=1)

        dataset.start()
        for i, d in enumerate(dataloader):
            np.testing.assert_allclose([[data_list1[i] * 10]], d)
        # replace cache content, move forward 2(5 * 0.4) items
        dataset.update_cache()
        for i, d in enumerate(dataloader):
            np.testing.assert_allclose([[data_list1[i + 2] * 10]], d)
        # shutdown to update data
        dataset.shutdown()
        # update the datalist and fill the cache content
        data_list2 = list(range(-10, 0))
        dataset.set_data(data=data_list2)
        # restart the dataset
        dataset.start()
        # rerun with updated cache content
        for i, d in enumerate(dataloader):
            np.testing.assert_allclose([[data_list2[i] * 10]], d)
        # replace cache content, move forward 2(5 * 0.4) items
        dataset.update_cache()
        for i, d in enumerate(dataloader):
            np.testing.assert_allclose([[data_list2[i + 2] * 10]], d)
        # finally shutdown the dataset
        dataset.shutdown()
Ejemplo n.º 6
0
    def initialize(self, args):
        """
        `initialize` is called only once when the model is being loaded.
        Implementing `initialize` function is optional. This function allows
        the model to intialize any state associated with this model.
        """

        # Pull model from google drive
        extract_dir = "/models/monai_covid/1"
        tar_save_path = os.path.join(extract_dir, model_filename)
        download_and_extract(gdrive_url,
                             tar_save_path,
                             output_dir=extract_dir,
                             hash_val=md5_check,
                             hash_type="md5")
        # load model configuration
        self.model_config = json.loads(args['model_config'])

        # create inferer engine and load PyTorch model
        inference_device_kind = args.get('model_instance_kind', None)
        logger.info(f"Inference device: {inference_device_kind}")

        self.inference_device = torch.device('cpu')
        if inference_device_kind is None or inference_device_kind == 'CPU':
            self.inference_device = torch.device('cpu')
        elif inference_device_kind == 'GPU':
            inference_device_id = args.get('model_instance_device_id', '0')
            logger.info(f"Inference device id: {inference_device_id}")

            if torch.cuda.is_available():
                self.inference_device = torch.device(
                    f'cuda:{inference_device_id}')
                cudnn.enabled = True
            else:
                logger.error(
                    f"No CUDA device detected. Using device: {inference_device_kind}"
                )

        # create pre-transforms
        self.pre_transforms = Compose([
            LoadImage(reader="NibabelReader",
                      image_only=True,
                      dtype=np.float32),
            AddChannel(),
            ScaleIntensityRange(a_min=-1000,
                                a_max=500,
                                b_min=0.0,
                                b_max=1.0,
                                clip=True),
            CropForeground(margin=5),
            Resize([192, 192, 64], mode="area"),
            AddChannel(),
            ToTensor(),
            Lambda(func=lambda x: x.to(device=self.inference_device)),
        ])

        # create post-transforms
        self.post_transforms = Compose([
            Lambda(func=lambda x: x.to(device="cpu")),
            Activations(sigmoid=True),
            ToNumpy(),
            AsDiscrete(threshold_values=True, logit_thresh=0.5),
        ])

        self.inferer = SimpleInferer()

        self.model = torch.jit.load(
            f'{pathlib.Path(os.path.realpath(__file__)).parent}{os.path.sep}covid19_model.ts',
            map_location=self.inference_device)