Example #1
0
    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])
Example #2
0
    def test_multiplex_app(self):
        with self.cached_session():
            app = MultiplexSegmentation(use_pretrained_weights=False)

            # test output shape
            shape = app.model.output_shape
            self.assertIsInstance(shape, list)
            self.assertEqual(len(shape), 4)

            # test predict
            x = np.random.rand(1, 500, 500, 2)
            y = app.predict(x)
            self.assertEqual(x.shape[:-1], y.shape[:-1])
Example #3
0
def deepcell_segment(image: Array, compartment: str = None) -> Array:
    from deepcell.applications import (
        MultiplexSegmentation,
        NuclearSegmentation,
        CytoplasmSegmentation,
    )

    if compartment is None:
        # If image has more than one channel i.e. CYX
        # assume segmentation of both compartments, otherwise nuclear
        compartment = "both" if image.shape[-1] > 1 else "nuclear"

    if compartment == "nuclear":
        app = NuclearSegmentation()
        pred = app.predict(image).squeeze()

    if compartment == "cytoplasm":
        app = CytoplasmSegmentation()
        pred = app.predict(image).squeeze()

    if compartment == "both":
        app = MultiplexSegmentation()
        pred = app.predict(image, compartment=compartment).squeeze()

    if len(pred.shape) == 4:
        pred = resize(pred, image.shape[1:])
    return pred
Example #4
0
    def test_multiplex_app(self):
        with self.cached_session():
            app = MultiplexSegmentation(use_pretrained_weights=False)

            # 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')
            print("x shape is {}, y shape is {}".format(x.shape, y.shape))
            self.assertEqual(x.shape[:-1], y.shape[:-1])
            self.assertEqual(y.shape[-1], 2)
Example #5
0
class TestMultiplexSegmentation(test.TestCase):
    def setUp(self):

        self.app = MultiplexSegmentation(use_pretrained_weights=False)

    def test_multiplex_app(self):

        # Check shape parameters
        shape = self.app.model.output_shape

        self.assertIsInstance(shape, list)
        self.assertEqual(len(shape), 4)

    def test_predict(self):
        x = np.random.rand(1, 500, 500, 2)
        y = self.app.predict(x)

        self.assertEqual(x.shape[:-1], y.shape[:-1])
Example #6
0
    def setUp(self):

        self.app = MultiplexSegmentation(use_pretrained_weights=False)
Example #7
0
    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])