def main(args): Checker.check_args(args.part, args.task) input_kwargs = dict( task=args.task, weight=args.weight, image_path=args.image_path, save_path=args.save_path, gpus=args.gpus) if args.part == "Brain": if args.task in ["mribet", "mrabet"]: BrainBET(**input_kwargs) elif args.task == "blackblood_segmentation": BrainBlackBloodSegmentation(**input_kwargs) elif args.part == "Chest": if args.task == "lrmark_detection": ChestLRmarkDetection(**input_kwargs) elif args.task == "viewpoint_classification": ViewpointClassifier(**input_kwargs) elif args.task == "enhance_classification": EnhanceCTClassification(**input_kwargs) elif args.part == "Abdomen": if args.task == "liver_segmentation": AbdomenLiverSegmentation(**input_kwargs) elif args.task == "kidney_tumor_segmentation": # AbdomenKidneyTumorSegmentation(**input_kwargs) elif args.part == "Colon": pass
def _preprocessing(self, path: str) -> np.array: """ Preprocess the image from the path Args: (string) path : absolute path of image Return: (numpy ndarray) image """ mean_std = [29.311405133024834, 43.38181786843102] if Checker.check_input_type_bool(path, 'nii'): image = sitk.ReadImage(path) self.space = image.GetSpacing() image = sitk.GetArrayFromImage(image).astype('float32') warnings.warn( '.nii is not recommended as an image format ' 'due to be not clear abour horizontal or vertical shape. ' 'Please check the sample in README.md.', UserWarning) elif Checker.check_input_type_bool(path, 'dcm'): raise ValueError('.dcm is not supported. ' 'Please convert dcm dummies to analyze format.') elif Checker.check_input_type_bool(path, 'img') or \ Checker.check_input_type_bool(path, 'hdr'): image = sitk.ReadImage(path) self.space = image.GetSpacing() image = np.squeeze( sitk.GetArrayFromImage(image).astype('float32')) # (d, w, h) elif Checker.check_input_type_bool(path, 'npy'): image = np.load(path) self.space = [1., 1., 1.] warnings.warn( '.npy is not recommended as an image format.' 'Since spacing cannot be identified from .npy, ' 'spacing is set as [1., 1., 1.].', UserWarning) else: input_ext = path.split('.')[-1] raise ValueError(f'.{input_ext} format is not supported.') self.img_shape = image.shape _, h, w = self.img_shape imageo = image.copy() image = zoom(image, [self.space[-1] / 5., 256. / float(w), 256. / float(h)], order=1, mode='constant') image = np.clip(image, 10, 190) image = (image - mean_std[0]) / mean_std[1] image = image[np.newaxis, ..., np.newaxis] # (1, d, w, h, 1) return imageo, image
def _preprocessing(self, path): """ Preprocess the image from the path Args: (string) path : absolute path of image Return: (numpy ndarray) image """ if Checker.check_input_type_bool(path, 'dcm'): raise ValueError( '.dcm is not supported. ' 'Please convert dcm dummies to analyze format.') Img = img_as_float(io.imread(path)) Img = transform.resize(Img, (1024,1024)) Img = np.expand_dims(Img, -1) FileName = os.path.split(path)[-1] Img_mean = Img.mean() Img_std = np.sqrt(((Img**2).mean()) - (Img.mean())**2) Img -= Img_mean Img /= Img_std print("Preprocessing done on {} files...".format(str(FileName))) return Img, FileName
def ChestLRmarkDetection( task: str, weight: str, image_path: str, save_path: Optional[str] = None, gpus: str = "-1" ) -> np.array: """ """ from medimodule.Chest import ChestLRmarkDetection Checker.check_input_type(image_path, ["png", "jpg", "bmp"]) Checker.set_gpu(gpu_idx=gpus, framework="tf") model = ChestLRmarkDetection(weight) image = model.predict( os.path.abspath(image_path), ) return image
def AbdomenLiverSegmentation( task: str, weight: str, image_path: str, save_path: Optional[str] = None, gpus: str = "-1" ) -> Tuple[np.array, np.array]: """ """ from medimodule.Liver import LiverSegmentation Checker.check_input_type(image_path, ["hdr", "img", "nii"]) Checker.set_gpu(gpu_idx=gpus, framework="tf") model = LiverSegmentation(weight) image, mask = model.predict( os.path.abspath(image_path), save_path=save_path) return image, mask
def BrainBlackBloodSegmentation( task: str, weight: str, image_path: str, save_path: Optional[str] = None, gpus: str = "-1" ) -> Tuple[np.array, np.array]: """ """ from medimodule.Brain import BlackbloodSegmentation Checker.check_input_type(image_path, "nii") Checker.set_gpu(gpu_idx=gpus, framework="tf") model = BlackbloodSegmentation(weight) image, mask = model.predict( os.path.abspath(image_path), save_path=save_path) return image, mask
def BrainBET( task: str, weight: str, image_path: str, save_path: Optional[str] = None, gpus: str = "-1" ) -> Tuple[np.array, np.array]: """ """ from medimodule.Brain import MRI_BET Checker.check_input_type(image_path, "nii") Checker.set_gpu(gpu_idx=gpus, framework="pytorch") model = MRI_BET(weight) image, mask = model.predict( os.path.abspath(image_path), img_type="T1" if "mri" in task else "MRA", save_path=save_path) return image, mask
def _preprocessing(self, path: str) -> np.array: """ Preprocess the image from the path Args: (string) path : absolute path of image Return: (numpy ndarray) image shape (1, h, w, d, 1) """ if Checker.check_input_type_bool(path, 'nii'): image = sitk.ReadImage(path) self.space = image.GetSpacing() image = sitk.GetArrayFromImage(image).astype('float32') elif Checker.check_input_type_bool(path, 'npy'): image = np.load(path) self.space = [1., 1., 1.] warnings.warn( '.npy is not recommended as an image format.' 'Since spacing cannot be identified from .npy, spacing is set as [1., 1., 1.].', UserWarning) elif Checker.check_input_type_bool(path, 'dcm'): raise ValueError('.dcm is not supported.' 'Please convert dcm dummies to analyze format.') else: input_ext = path.split('.')[-1] raise ValueError(f'.{input_ext} format is not supported.') self.img_shape = image.shape # normalize windowing_range = [-40., 120.] windowing_min = windowing_range[0] - windowing_range[1] // 2 windowing_max = windowing_range[0] + windowing_range[1] // 2 image = ndimage.zoom(image, [.5, .5, .5], order=1, mode='constant') image = np.clip(image, windowing_min, windowing_max) image = (image - windowing_min) / (windowing_max - windowing_min) image = image[np.newaxis, ..., np.newaxis] return image