Exemple #1
0
    def test_image_quality_classification(self):
        model_path = ck_data.initialize_best_focus_model()
        # Make sure to use default op TF config since GPU memory errors during testing will
        # occur otherwise (i.e. when gpu_options.allow_growth is not set to True)
        miqest = prediction.ImageQualityClassifier(
            model_path, DEFAULT_PATCH_SIZE, DEFAULT_N_CLASSES, session_config=op.get_tf_config(None))

        img1 = np.zeros((DEFAULT_PATCH_SIZE*2, DEFAULT_PATCH_SIZE*2))
        img1[6:10, 6:10] = 1
        img2 = filters.gaussian(img1, 5)

        # Get class prediction for images (0 = best quality)
        max1 = np.argmax(miqest.predict(img1).probabilities)
        max2 = np.argmax(miqest.predict(img2).probabilities)

        # Assert that probabilities (for ~11 classes) for original image have
        # max at first element (i.e. the highest quality class)
        self.assertEqual(
            max1, 0,
            msg='Expecting highest quality class prediction for static image (got class {})'
            .format(max1)
        )

        # Ensure that max class for blurred image (where higher class = lower quality) is greater than original
        self.assertTrue(
            max2 > max1,
            msg='Expecting quality class of original image ({}) to be less than quality class of blurred image ({})'
            .format(max1, max2)
        )
Exemple #2
0
 def initialize(self):
     model_path = cytokit_data.initialize_best_focus_model()
     self.graph = tf.Graph()
     self.mqiest = prediction.ImageQualityClassifier(
         model_path,
         self.patch_size,
         self.n_classes,
         graph=self.graph,
         session_config=get_tf_config(self))
     return self
Exemple #3
0
    def _run(self, tile, **kwargs):
        if not np.issubdtype(tile.dtype, np.unsignedinteger):
            raise ValueError('Only unsigned integer images supported; '
                             'type given = {}'.format(tile.dtype))

        # Tile should have shape (cycles, z, channel, height, width)
        ncyc, nz, nch, nh, nw = tile.shape

        # Ensure that given tile has same number of channels as required in configuration
        # since PSF generation is specific for each channel
        if nch != self.config.n_channels_per_cycle:
            raise AssertionError(
                'Given tile with shape {} ({} channels) does not have expected number of channels {}'
                .format(tile.shape, nch, self.config.n_channels_per_cycle))

        img_cyc = []
        for icyc in range(ncyc):
            img_ch = []
            for ich in range(nch):
                img = tile[icyc, :, ich, :, :]

                # Skip deconvolution if channel was configured to be ignored
                if self.channels is not None and ich not in self.channels:
                    img_ch.append(img)
                    continue

                acq = fd_data.Acquisition(img, kernel=self.psfs[ich])
                logger.debug(
                    'Running deconvolution for cycle {}, channel {} [dtype = {}]'
                    .format(icyc, ich, acq.data.dtype))
                res = self.algo.run(acq,
                                    self.n_iter,
                                    session_config=get_tf_config(self)).data

                # Restore mean intensity if a scale factor was given
                if self.scale_factor is not None:
                    res, mean_ratio = rescale_stack(acq.data, res,
                                                    self.scale_factor)
                    self.record({
                        'mean_ratio': mean_ratio,
                        'cycle': icyc,
                        'channel': ich
                    })

                # Clip float32 and convert to type of original image (i.e. w/ no scaling)
                res = np_utils.arr_to_uint(res, acq.data.dtype)

                img_ch.append(res)
            img_cyc.append(np.stack(img_ch, 1))
        return np.stack(img_cyc, 0)
Exemple #4
0
    def _run(self, tile, **kwargs):
        if not np.issubdtype(tile.dtype, np.unsignedinteger):
            raise ValueError('Only unsigned integer images supported; '
                             'type given = {}'.format(tile.dtype))

        # Tile should have shape (cycles, z, channel, height, width)
        dims = self.config.tile_dims
        if dims != tile.shape:
            raise AssertionError(
                'Given tile with shape {} does not match expected shape {}'.
                format(tile.shape, dims))
        ncyc, nz, nch, nh, nw = dims

        img_cyc = []
        for icyc in range(ncyc):
            img_ch = []
            for ich in range(nch):
                acq = fd_data.Acquisition(tile[icyc, :, ich, :, :],
                                          kernel=self.psfs[ich])
                logger.debug(
                    'Running deconvolution for cycle {}, channel {} [dtype = {}]'
                    .format(icyc, ich, acq.data.dtype))
                res = self.algo.run(acq,
                                    self.n_iter,
                                    session_config=get_tf_config(self)).data

                # Restore mean intensity if a scale factor was given
                if self.scale_factor is not None:
                    res, mean_ratio = rescale_stack(acq.data, res,
                                                    self.scale_factor)
                    self.record({
                        'mean_ratio': mean_ratio,
                        'cycle': icyc,
                        'channel': ich
                    })

                # Clip float32 and convert to type of original image (i.e. w/ no scaling)
                res = np_utils.arr_to_uint(res, acq.data.dtype)

                img_ch.append(res)
            img_cyc.append(np.stack(img_ch, 1))
        return np.stack(img_cyc, 0)
Exemple #5
0
def set_keras_session(op):
    import keras.backend.tensorflow_backend as KTF
    import tensorflow as tf
    tf_config = cytokit_op.get_tf_config(op)
    KTF.set_session(tf.Session(config=tf_config))