def test_segmentation(self): """Check that segmentation works as expected from ported version.""" classes = ["fc", "tc", "pc", "men"] expected_seg = np.load( os.path.join( util.UNITTEST_DATA_PATH, "datasets/oai/expected/test_001_V00-iwoai-2019-t6.npy")) scan = NiftiReader().load( os.path.join(util.UNITTEST_DATA_PATH, "datasets/oai/test_001_V00.nii.gz")) tissue = FemoralCartilage() tissue.find_weights( os.path.join(os.path.dirname(__file__), "../../weights/iwoai-2019-t6")) dims = scan.volume.shape input_shape = (dims[0], dims[1], 1) model = IWOAIOAIUnet2D(input_shape=input_shape, weights_path=tissue.weights_file_path) masks = model.generate_mask(scan) K.clear_session() for i, tissue in enumerate(classes): assert np.all(masks[tissue].volume == expected_seg[..., i])
def test_segmentation(self): """Test if batch size makes a difference on the segmentation output""" scan = self.SCAN_TYPE(dicom_path=self.dicom_dirpath) tissue = FemoralCartilage() tissue.find_weights(SEGMENTATION_WEIGHTS_FOLDER) dims = scan.get_dimensions() input_shape = (dims[0], dims[1], 1) model = get_model(SEGMENTATION_MODEL, input_shape=input_shape, weights_path=tissue.weights_file_path) scan.segment(model, tissue)
def test_segmentation(self): """Test automatic segmentation Expected: NotImplementedError """ scan = self.SCAN_TYPE(dicom_path=self.dicom_dirpath) tissue = FemoralCartilage() tissue.find_weights(SEGMENTATION_WEIGHTS_FOLDER) dims = scan.get_dimensions() input_shape = (dims[0], dims[1], 1) model = get_model(SEGMENTATION_MODEL, input_shape=input_shape, weights_path=tissue.weights_file_path) # automatic segmentation currently not implemented with self.assertRaises(NotImplementedError): scan.segment(model, tissue)
def test_segmentation_multiclass(self): """Test support for multiclass segmentation.""" scan = self.SCAN_TYPE.from_dicom(self.dicom_dirpath, num_workers=util.num_workers()) tissue = FemoralCartilage() tissue.find_weights(SEGMENTATION_WEIGHTS_FOLDER), dims = scan.get_dimensions() input_shape = (dims[0], dims[1], 1) model = get_model(SEGMENTATION_MODEL, input_shape=input_shape, weights_path=tissue.weights_file_path) scan.segment(model, tissue, use_rss=True) # This should call __del__ in KerasSegModel model = None K.clear_session()
def test_segmentation(self): classes = ["fc", "tc", "pc", "men"] expected_seg = np.load( os.path.join( util.UNITTEST_DATA_PATH, "datasets/oai/expected/test_001_V00-iwoai-2019-t6-normalized.npy", )) scan = NiftiReader().load( os.path.join(util.UNITTEST_DATA_PATH, "datasets/oai/test_001_V00.nii.gz")) tissue = FemoralCartilage() tissue.find_weights( os.path.join(os.path.dirname(__file__), "../../weights/iwoai-2019-t6-normalized")) dims = scan.volume.shape input_shape = (dims[0], dims[1], 1) model = IWOAIOAIUnet2DNormalized(input_shape=input_shape, weights_path=tissue.weights_file_path) masks = model.generate_mask(scan) K.clear_session() for i, tissue in enumerate(classes): pred = masks[tissue].volume.astype(np.bool) gt = expected_seg[..., i].astype(np.bool) dice = 2 * np.sum(pred & gt) / np.sum( pred.astype(np.uint8) + gt.astype(np.uint8)) # Zero-mean normalization of 32-bit vs 64-bit data results in slightly different # estimations of the mean and standard deviation. # However, when both volumes are compared pre-normalization # using np.all() both volumes are the same (see :meth:`test_h5_nifti_same`). # This slight difference in the image can affect network performance. # As a placeholder, we assert that the dice between the expected and # produced segmentations for each tissue must be greater than 99% # # Update: We found for this particular model and example pair, the segmentations # achieve a dice score of 1.0 for all tissues. # We enforce that the predicted mask must be equal to the expected mask. assert dice >= 0.99, "{}: {:0.6f}".format(tissue, dice) assert np.all(masks[tissue].volume == expected_seg[ ..., i]), f"Segmentation not same for {tissue}"