Example #1
0
def estimate_image(msi, regressor):
    """given an Msi and an regressor estimate the parmaeters for this image

    Paramters:
        msi: multi spectral image
        regressor: regressor, must implement the predict method"""

    # estimate parameters
    collapsed_msi = imgmani.collapse_image(msi.get_image())
    # in case of nan values: set to 0
    collapsed_msi[np.isnan(collapsed_msi)] = 0.
    collapsed_msi[np.isinf(collapsed_msi)] = 0.

    start = time.time()
    estimated_parameters = regressor.predict(collapsed_msi)
    end = time.time()
    estimation_time = end - start
    logging.info("time necessary for estimating image parameters: " +
        str(estimation_time) + "s")
    # restore shape
    feature_dimension = 1
    if len(estimated_parameters.shape) > 1:
        feature_dimension = estimated_parameters.shape[-1]

    estimated_paramters_as_image = np.reshape(
            estimated_parameters, (msi.get_image().shape[0],
                                   msi.get_image().shape[1],
                                   feature_dimension))
    # save as sitk nrrd.
    sitk_img = sitk.GetImageFromArray(estimated_paramters_as_image,
                                 isVector=True)
    return sitk_img, estimation_time
Example #2
0
def estimate_image(msi, regressor):
    """given an Msi and an regressor estimate the parmaeters for this image

    Paramters:
        msi: multi spectral image
        regressor: regressor, must implement the predict method"""

    # estimate parameters
    collapsed_msi = imgmani.collapse_image(msi.get_image())
    # in case of nan values: set to 0
    collapsed_msi[np.isnan(collapsed_msi)] = 0.
    collapsed_msi[np.isinf(collapsed_msi)] = 0.

    start = time.time()
    estimated_parameters = regressor.predict(collapsed_msi)
    end = time.time()
    estimation_time = end - start
    logging.info("time necessary for estimating image parameters: " +
        str(estimation_time) + "s")
    # restore shape
    feature_dimension = 1
    if len(estimated_parameters.shape) > 1:
        feature_dimension = estimated_parameters.shape[-1]

    estimated_paramters_as_image = np.reshape(
            estimated_parameters, (msi.get_image().shape[0],
                                   msi.get_image().shape[1],
                                   feature_dimension))
    # save as sitk nrrd.
    sitk_img = sitk.GetImageFromArray(estimated_paramters_as_image,
                                 isVector=True)
    return sitk_img, estimation_time
Example #3
0
    def test_collapse_image(self):
        image = self.image
        newShapedImage = imgmani.collapse_image(image)

        self.assertEqual(newShapedImage.shape,
                        (image.shape[0] * image.shape[1], image.shape[2]),
                        "collapsed image has correct shape")
        np.testing.assert_equal(newShapedImage[2 * 5 + 2, :],
                                       self.msi.get_image()[2, 2, :],
                        "values have been correctly transformed")
Example #4
0
    def test_collapse_image(self):
        image = self.image
        newShapedImage = imgmani.collapse_image(image)

        self.assertEqual(newShapedImage.shape,
                         (image.shape[0] * image.shape[1], image.shape[2]),
                         "collapsed image has correct shape")
        np.testing.assert_equal(newShapedImage[2 * 5 + 2, :],
                                self.msi.get_image()[2, 2, :],
                                "values have been correctly transformed")
Example #5
0
def estimate_image_tensorflow(msi, model_checkpoint_dir):
    # estimate parameters
    collapsed_msi = imgmani.collapse_image(msi.get_image())
    # in case of nan values: set to 0
    collapsed_msi[np.isnan(collapsed_msi)] = 0.
    collapsed_msi[np.isinf(collapsed_msi)] = 0.


    tf.reset_default_graph()

    keep_prob = tf.placeholder("float")
    nr_wavelengths = len(msi.get_wavelengths())
    x = tf.placeholder("float", [None, nr_wavelengths, 1, 1])

    x_test_image = np.reshape(msi.get_image(), [-1, nr_wavelengths, 1, 1])

    # Construct the desired model
    # pred, regularizers = multilayer_perceptron(x, nr_wavelengths, 100, 1,
    #                                            keep_prob)
    pred = cnn(x, 1, keep_prob)

    # Initializing the variables
    init = tf.initialize_all_variables()

    saver = tf.train.Saver()

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        # restore model:
        ckpt = tf.train.get_checkpoint_state(model_checkpoint_dir)

        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)

            start = time.time()
            estimated_parameters = pred.eval({x: x_test_image,
                                              keep_prob:1.0})
            end = time.time()
            estimation_time = end - start
            logging.info("time necessary for estimating image parameters: " +
                str(estimation_time) + "s")
    # restore shape
    feature_dimension = 1
    if len(estimated_parameters.shape) > 1:
        feature_dimension = estimated_parameters.shape[-1]

    estimated_paramters_as_image = np.reshape(
            estimated_parameters, (msi.get_image().shape[0],
                                   msi.get_image().shape[1],
                                   feature_dimension))
    # save as sitk nrrd.
    sitk_img = sitk.GetImageFromArray(estimated_paramters_as_image,
                                 isVector=True)

    return sitk_img, estimation_time
Example #6
0
def estimate_image_tensorflow(msi, model_checkpoint_dir):
    # estimate parameters
    collapsed_msi = imgmani.collapse_image(msi.get_image())
    # in case of nan values: set to 0
    collapsed_msi[np.isnan(collapsed_msi)] = 0.
    collapsed_msi[np.isinf(collapsed_msi)] = 0.


    tf.reset_default_graph()

    keep_prob = tf.placeholder("float")
    nr_wavelengths = len(msi.get_wavelengths())
    x = tf.placeholder("float", [None, nr_wavelengths, 1, 1])

    x_test_image = np.reshape(msi.get_image(), [-1, nr_wavelengths, 1, 1])

    # Construct the desired model
    # pred, regularizers = multilayer_perceptron(x, nr_wavelengths, 100, 1,
    #                                            keep_prob)
    pred = cnn(x, 1, keep_prob)

    # Initializing the variables
    init = tf.initialize_all_variables()

    saver = tf.train.Saver()

    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        # restore model:
        ckpt = tf.train.get_checkpoint_state(model_checkpoint_dir)

        if ckpt and ckpt.model_checkpoint_path:
            saver.restore(sess, ckpt.model_checkpoint_path)

            start = time.time()
            estimated_parameters = pred.eval({x: x_test_image,
                                              keep_prob:1.0})
            end = time.time()
            estimation_time = end - start
            logging.info("time necessary for estimating image parameters: " +
                str(estimation_time) + "s")
    # restore shape
    feature_dimension = 1
    if len(estimated_parameters.shape) > 1:
        feature_dimension = estimated_parameters.shape[-1]

    estimated_paramters_as_image = np.reshape(
            estimated_parameters, (msi.get_image().shape[0],
                                   msi.get_image().shape[1],
                                   feature_dimension))
    # save as sitk nrrd.
    sitk_img = sitk.GetImageFromArray(estimated_paramters_as_image,
                                 isVector=True)

    return sitk_img, estimation_time
Example #7
0
    def test_collapse_image_retains_data(self):
        newShapedImage = imgmani.collapse_image(self.image)
        self.msi.get_image()[2, 2, 0] = 5000.

        self.assertEqual(newShapedImage[2 * 5 + 2, 0], 5000.,
                        "collapse_image does not copy data")
Example #8
0
    def test_collapse_image_retains_data(self):
        newShapedImage = imgmani.collapse_image(self.image)
        self.msi.get_image()[2, 2, 0] = 5000.

        self.assertEqual(newShapedImage[2 * 5 + 2, 0], 5000.,
                         "collapse_image does not copy data")