def __init__(self, use_pretrained_weights=True, model_image_shape=(128, 128, 1)): model = PanopticNet('resnet50', input_shape=model_image_shape, norm_method='whole_image', num_semantic_heads=3, num_semantic_classes=[1, 1, 2], location=True, include_top=True, lite=True, interpolation='bilinear') if use_pretrained_weights: weights_path = get_file( os.path.basename(WEIGHTS_PATH), WEIGHTS_PATH, cache_subdir='models', md5_hash='50614f04d5dbc4b3eadd897fa5fb0e23' ) model.load_weights(weights_path) else: weights_path = None super(CytoplasmSegmentation, self).__init__(model, model_image_shape=model_image_shape, model_mpp=0.65, preprocessing_fn=phase_preprocess, postprocessing_fn=deep_watershed, dataset_metadata=self.dataset_metadata, model_metadata=self.model_metadata)
def __init__(self, use_pretrained_weights=True, model_image_shape=(128, 128, 1)): model = PanopticNet('resnet50', input_shape=model_image_shape, norm_method='whole_image', num_semantic_heads=2, num_semantic_classes=[1, 1], location=True, include_top=True, lite=True, use_imagenet=use_pretrained_weights, interpolation='bilinear') if use_pretrained_weights: weights_path = get_file( os.path.basename(WEIGHTS_PATH), WEIGHTS_PATH, cache_subdir='models', file_hash='104a7d7884c80c37d2bce6d1c3a17c7a') model.load_weights(weights_path, by_name=True) else: weights_path = None super(CytoplasmSegmentation, self).__init__(model, model_image_shape=model_image_shape, model_mpp=0.65, preprocessing_fn=phase_preprocess, postprocessing_fn=deep_watershed, dataset_metadata=self.dataset_metadata, model_metadata=self.model_metadata)
def __init__(self, use_pretrained_weights=True, model_image_shape=(256, 256, 2)): model = PanopticNet('resnet50', input_shape=model_image_shape, norm_method=None, num_semantic_heads=4, num_semantic_classes=[1, 1, 2, 3], location=True, include_top=True, use_imagenet=False) if use_pretrained_weights: weights_path = get_file( os.path.basename(WEIGHTS_PATH), WEIGHTS_PATH, cache_subdir='models', md5_hash='66fec859eacc5222b5e7d2baa105f3e3') model.load_weights(weights_path) else: weights_path = None super(MultiplexSegmentation, self).__init__(model, model_image_shape=model_image_shape, model_mpp=2.0, preprocessing_fn=phase_preprocess, postprocessing_fn=deep_watershed_mibi, dataset_metadata=self.dataset_metadata, model_metadata=self.model_metadata)
def __init__(self, use_pretrained_weights=True, model_image_shape=(128, 128, 1)): model = PanopticNet('resnet50', input_shape=model_image_shape, norm_method='whole_image', num_semantic_heads=2, num_semantic_classes=[1, 1], location=True, include_top=True, lite=True, interpolation='bilinear') if use_pretrained_weights: weights_path = get_file( os.path.basename(WEIGHTS_PATH), WEIGHTS_PATH, cache_subdir='models', md5_hash='42ca0ebe4b7b0f782eaa4733cdddad88' ) model.load_weights(weights_path, by_name=True) else: weights_path = None super(NuclearSegmentation, self).__init__(model, model_image_shape=model_image_shape, model_mpp=0.65, preprocessing_fn=None, postprocessing_fn=deep_watershed, dataset_metadata=self.dataset_metadata, model_metadata=self.model_metadata)
def __init__(self, use_pretrained_weights=True, model_image_shape=(128, 128, 1)): model = PanopticNet('resnet50', input_shape=model_image_shape, norm_method='whole_image', num_semantic_heads=3, num_semantic_classes=[1, 1, 2], location=True, include_top=True) if use_pretrained_weights: weights_path = get_file( os.path.basename(WEIGHTS_PATH), WEIGHTS_PATH, cache_subdir='models', md5_hash='eb29808ef2f662fb3bcda6986e47f91a') model.load_weights(weights_path) else: weights_path = None super(NuclearSegmentation, self).__init__(model, model_image_shape=model_image_shape, model_mpp=0.65, preprocessing_fn=None, postprocessing_fn=deep_watershed, dataset_metadata=self.dataset_metadata, model_metadata=self.model_metadata)
def test_cytoplasm_app(self): with self.cached_session(): model = PanopticNet('resnet50', input_shape=(128, 128, 1), norm_method='whole_image', num_semantic_heads=2, num_semantic_classes=[1, 1], location=True, include_top=True, lite=True, use_imagenet=False, interpolation='bilinear') app = CytoplasmSegmentation(model) # test output shape shape = app.model.output_shape self.assertIsInstance(shape, list) self.assertEqual(len(shape), 2) self.assertEqual(len(shape[0]), 4) self.assertEqual(len(shape[1]), 4) # test predict x = np.random.rand(1, 500, 500, 1) y = app.predict(x) self.assertEqual(x.shape, y.shape)
def test_panopticnet_semantic_class_types(self): shared_kwargs = { 'backbone': 'featurenet', 'input_shape': (32, 32, 1), 'use_imagenet': False, } with self.cached_session(): nsc1 = [2, 3] model1 = PanopticNet(num_semantic_classes=nsc1, **shared_kwargs) nsc2 = {'0': 2, '1': 3} model2 = PanopticNet(num_semantic_classes=nsc1, **shared_kwargs) for o1, o2 in zip(model1.outputs, model2.outputs): self.assertEqual(o1.shape.as_list(), o2.shape.as_list()) self.assertEqual(o1.name, o2.name) self.assertEqual(o1.dtype, o2.dtype)
def test_panopticnet_bad_input(self): norm_method = None # not all backbones work with channels_first backbone = 'featurenet' # TODO: RetinaMask fails with channels_first data_format = 'channels_last' num_semantic_classes = [1, 3] # non-square input input_shape = (256, 512, 1) with self.assertRaises(ValueError): model = PanopticNet( backbone=backbone, input_shape=input_shape, backbone_levels=['C3', 'C4', 'C5'], norm_method=norm_method, location=True, pooling='avg', num_semantic_heads=len(num_semantic_classes), num_semantic_classes=num_semantic_classes, use_imagenet=False, ) # non power of 2 input input_shape = (257, 257, 1) with self.assertRaises(ValueError): model = PanopticNet( backbone=backbone, input_shape=input_shape, backbone_levels=['C3', 'C4', 'C5'], norm_method=norm_method, location=True, pooling='avg', num_semantic_heads=len(num_semantic_classes), num_semantic_classes=num_semantic_classes, use_imagenet=False, )
def test_mesmer_app(self): with self.cached_session(): whole_cell_classes = [1, 3] nuclear_classes = [1, 3] num_semantic_classes = whole_cell_classes + nuclear_classes num_semantic_heads = len(num_semantic_classes) model = PanopticNet('resnet50', input_shape=(256, 256, 2), norm_method=None, num_semantic_heads=num_semantic_heads, num_semantic_classes=num_semantic_classes, location=True, include_top=True, use_imagenet=False) app = Mesmer(model) # test output shape shape = app.model.output_shape self.assertIsInstance(shape, list) self.assertEqual(len(shape), 4) # test predict with default x = np.random.rand(1, 500, 500, 2) y = app.predict(x) self.assertEqual(x.shape[:-1], y.shape[:-1]) # test predict with nuclear compartment only x = np.random.rand(1, 500, 500, 2) y = app.predict(x, compartment='nuclear') self.assertEqual(x.shape[:-1], y.shape[:-1]) self.assertEqual(y.shape[-1], 1) # test predict with cell compartment only x = np.random.rand(1, 500, 500, 2) y = app.predict(x, compartment='whole-cell') self.assertEqual(x.shape[:-1], y.shape[:-1]) self.assertEqual(y.shape[-1], 1) # test predict with both cell and nuclear compartments x = np.random.rand(1, 500, 500, 2) y = app.predict(x, compartment='both') self.assertEqual(x.shape[:-1], y.shape[:-1]) self.assertEqual(y.shape[-1], 2) # test legacy version old_app = MultiplexSegmentation(model) # test predict with default x = np.random.rand(1, 500, 500, 2) y = old_app.predict(x) self.assertEqual(x.shape[:-1], y.shape[:-1])
def __init__(self, use_pretrained_weights=True, model_image_shape=(256, 256, 2)): whole_cell_classes = [1, 1, 2, 3] nuclear_classes = [1, 1, 2, 3] num_semantic_classes = whole_cell_classes + nuclear_classes num_semantic_heads = len(num_semantic_classes) model = PanopticNet('resnet50', input_shape=model_image_shape, norm_method=None, num_semantic_heads=num_semantic_heads, num_semantic_classes=num_semantic_classes, location=True, include_top=True, use_imagenet=False) if use_pretrained_weights: weights_path = get_file( os.path.basename(WEIGHTS_PATH), WEIGHTS_PATH, cache_subdir='models', file_hash='ff24e821c6056cf847e58e8e52916814') model.load_weights(weights_path) else: weights_path = None super(MultiplexSegmentation, self).__init__(model, model_image_shape=model_image_shape, model_mpp=0.5, preprocessing_fn=phase_preprocess, postprocessing_fn=deep_watershed_subcellular, format_model_output_fn=format_output_multiplex, dataset_metadata=self.dataset_metadata, model_metadata=self.model_metadata)
def test_panopticnet(self, pooling, location, frames_per_batch, data_format, upsample_type, pyramid_levels): norm_method = None # not all backbones work with channels_first backbone = 'featurenet' # TODO: PanopticNet fails with channels_first and frames_per_batch > 1 if frames_per_batch > 1 and data_format == 'channels_first': return with self.cached_session(): K.set_image_data_format(data_format) if data_format == 'channels_first': axis = 1 input_shape = (1, 32, 32) else: axis = -1 input_shape = (32, 32, 1) num_semantic_classes = [1, 3] # temporal_mode=None, # lite=False, # interpolation='bilinear', model = PanopticNet( backbone=backbone, input_shape=input_shape, frames_per_batch=frames_per_batch, pyramid_levels=pyramid_levels, norm_method=norm_method, location=location, pooling=pooling, upsample_type=upsample_type, num_semantic_heads=len(num_semantic_classes), num_semantic_classes=num_semantic_classes, use_imagenet=False, ) self.assertIsInstance(model.output_shape, list) self.assertEqual(len(model.output_shape), len(num_semantic_classes)) for i, s in enumerate(num_semantic_classes): self.assertEqual(model.output_shape[i][axis], s)
def test_maskrcnn(self, pooling, location): num_classes = 3 crop_size = (14, 14) mask_size = (28, 28) norm_method = None # not all backbones work with channels_first backbone = 'featurenet' # TODO: RetinaMask fails with channels_first data_format = 'channels_last' with self.cached_session(): K.set_image_data_format(data_format) if data_format == 'channels_first': axis = 1 input_shape = (1, 32, 32) else: axis = -1 input_shape = (32, 32, 1) num_semantic_classes = [1, 3] model = PanopticNet( backbone=backbone, input_shape=input_shape, backbone_levels=['C3', 'C4', 'C5'], norm_method=norm_method, location=location, pooling=pooling, num_semantic_heads=len(num_semantic_classes), num_semantic_classes=num_semantic_classes, use_imagenet=False, ) self.assertIsInstance(model.output_shape, list) self.assertEqual(len(model.output_shape), len(num_semantic_classes)) for i, s in enumerate(num_semantic_classes): self.assertEqual(model.output_shape[i][axis], s)
def test_mesmer_app(self): with self.cached_session(): whole_cell_classes = [1, 3] nuclear_classes = [1, 3] num_semantic_classes = whole_cell_classes + nuclear_classes num_semantic_heads = len(num_semantic_classes) model = PanopticNet('resnet50', input_shape=(256, 256, 2), norm_method=None, num_semantic_heads=num_semantic_heads, num_semantic_classes=num_semantic_classes, location=True, include_top=True, use_imagenet=False) app = Mesmer(model) # test output shape shape = app.model.output_shape self.assertIsInstance(shape, list) self.assertEqual(len(shape), 4) # test predict with default x = np.random.rand(1, 500, 500, 2) y = app.predict(x) self.assertEqual(x.shape[:-1], y.shape[:-1]) # test predict with nuclear compartment only x = np.random.rand(1, 500, 500, 2) y = app.predict(x, compartment='nuclear') self.assertEqual(x.shape[:-1], y.shape[:-1]) self.assertEqual(y.shape[-1], 1) # test predict with cell compartment only x = np.random.rand(1, 500, 500, 2) y = app.predict(x, compartment='whole-cell') self.assertEqual(x.shape[:-1], y.shape[:-1]) self.assertEqual(y.shape[-1], 1) # test predict with both cell and nuclear compartments x = np.random.rand(1, 500, 500, 2) y = app.predict(x, compartment='both') self.assertEqual(x.shape[:-1], y.shape[:-1]) self.assertEqual(y.shape[-1], 2) # test that kwargs are passed through successfully app._predict_segmentation = Mock() # get defaults _ = app.predict(x, compartment='whole-cell') args = app._predict_segmentation.call_args[1] default_cell_kwargs = args['postprocess_kwargs'][ 'whole_cell_kwargs'] default_nuc_kwargs = args['postprocess_kwargs']['nuclear_kwargs'] # change one of the args for each compartment maxima_threshold_cell = default_cell_kwargs[ 'maxima_threshold'] + 0.1 radius_nuc = default_nuc_kwargs['radius'] + 2 _ = app.predict(x, compartment='whole-cell', postprocess_kwargs_whole_cell={ 'maxima_threshold': maxima_threshold_cell }, postprocess_kwargs_nuclear={'radius': radius_nuc}) args = app._predict_segmentation.call_args[1] cell_kwargs = args['postprocess_kwargs']['whole_cell_kwargs'] assert cell_kwargs['maxima_threshold'] == maxima_threshold_cell nuc_kwargs = args['postprocess_kwargs']['nuclear_kwargs'] assert nuc_kwargs['radius'] == radius_nuc # check that rest are unchanged cell_kwargs['maxima_threshold'] = default_cell_kwargs[ 'maxima_threshold'] assert cell_kwargs == default_cell_kwargs nuc_kwargs['radius'] = default_nuc_kwargs['radius'] assert nuc_kwargs == default_nuc_kwargs # test legacy version old_app = MultiplexSegmentation(model) # test predict with default x = np.random.rand(1, 500, 500, 2) y = old_app.predict(x) self.assertEqual(x.shape[:-1], y.shape[:-1])