def test_gmm_best_segment(self):
        """
        Calculate the best segment
        generated by the GMM and
        compare the subsequent likelihood
        of a reference segmentation.
        Note: this test will take a while
        to run.

        returns:
        best_seg = np.ndarray[np.ndarray[float]]
        """

        image_file = 'images/party_spock.png'
        image_matrix = image_to_matrix(image_file)
        image_matrix_flat = flatten_image_matrix(image_matrix)
        num_components = 3
        gmm = GaussianMixtureModel(image_matrix, num_components)
        gmm.initialize_training()
        iters = 10
        # generate best segment from 10 iterations
        # and extract its likelihood
        best_seg = gmm.best_segment(iters)
        matrix_to_image(best_seg, 'images/best_segment_spock.png')
        best_likelihood = gmm.likelihood()

        # extract likelihood from reference image
        ref_image_file = 'images/party_spock%d_baseline.png' % num_components
        ref_image = image_to_matrix(ref_image_file, grays=True)
        gmm_ref = GaussianMixtureModel(ref_image, num_components)
        ref_vals = ref_image.flatten()
        ref_means = list(set(ref_vals))
        ref_variances = np.zeros(num_components)
        ref_mixing = np.zeros(num_components)
        for i in range(num_components):
            relevant_vals = ref_vals[ref_vals == ref_means[i]]
            ref_mixing[i] = float(len(relevant_vals)) / float(len(ref_vals))
            ref_mask = ref_vals == ref_means[i]
            ref_variances[i] = np.mean(
                (image_matrix_flat[ref_mask] - ref_means[i])**2)
        gmm_ref.means = ref_means
        gmm_ref.variances = ref_variances
        gmm_ref.mixing_coefficients = ref_mixing
        ref_likelihood = gmm_ref.likelihood()

        # compare best likelihood and reference likelihood
        likelihood_diff = best_likelihood - ref_likelihood
        print "Reference"
        print gmm_ref.means
        print gmm_ref.variances
        print gmm_ref.mixing_coefficients
        print best_likelihood
        print ref_likelihood
        print likelihood_diff
        likelihood_thresh = 8e4
        self.assertTrue(likelihood_diff >= likelihood_thresh,
                        msg=("Image segmentation failed to improve baseline "
                             "by at least %.2f" % likelihood_thresh))
    def test_gmm_train(self):
        """Test the training
        procedure for GMM using
        synthetic data.

        returns:
        gmm = GaussianMixtureModel
        """

        num_components = 2
        data_range = (1, 1000)
        actual_means = np.array([2, 4])
        actual_variances = np.ones(num_components)
        actual_mixing = np.ones(num_components) / 2
        dataset_1 = generate_test_mixture(data_range, actual_means,
                                          actual_variances, actual_mixing)
        gmm = GaussianMixtureModel(dataset_1, num_components)
        gmm.initialize_training()
        # start off with faulty means
        gmm.means = np.array([1, 3])
        initial_likelihood = gmm.likelihood()

        gmm.train_model()
        final_likelihood = gmm.likelihood()
        likelihood_difference = final_likelihood - initial_likelihood
        likelihood_thresh = 250
        diff_check = likelihood_difference >= likelihood_thresh
        self.assertTrue(diff_check,
                        msg=("Model likelihood increased by less"
                             " than %d for a two-mean mixture" %
                             likelihood_thresh))

        num_components = 4
        actual_means = np.array([2, 4, 6, 8])
        actual_variances = np.ones(num_components)
        actual_mixing = np.ones(num_components) / 4
        dataset_1 = generate_test_mixture(data_range, actual_means,
                                          actual_variances, actual_mixing)
        gmm = GaussianMixtureModel(dataset_1, num_components)
        gmm.initialize_training()
        # start off with faulty means
        gmm.means = np.array([1, 3, 5, 9])
        initial_likelihood = gmm.likelihood()
        gmm.train_model()
        final_likelihood = gmm.likelihood()

        # compare likelihoods
        likelihood_difference = final_likelihood - initial_likelihood
        likelihood_thresh = 190

        diff_check = likelihood_difference >= likelihood_thresh
        self.assertTrue(diff_check,
                        msg=("Model likelihood increased by less "
                             "than %d for a four-mean mixture" %
                             likelihood_thresh))
    def test_gmm_segment(self):
        """
        Apply the trained GMM
        to unsegmented image and
        generate a segmented image.

        returns:
        segmented_matrix = numpy.ndarray[numpy.ndarray[float]]
        """

        image_file = 'images/party_spock.png'
        image_matrix = image_to_matrix(image_file)
        num_components = 3
        gmm = GaussianMixtureModel(image_matrix, num_components)
        gmm.initialize_training()
        gmm.train_model()
        segment = gmm.segment()
        segment_num_components = len(np.unique(segment))
        self.assertTrue(segment_num_components == num_components,
                        msg="Incorrect number of image segments produced")
Пример #4
0
    def test_gmm_joint_prob(self):
        """Testing the GMM method
        for calculating the joint
        log probability of a given point.
        Should return -0.9413.

        returns:
        joint_prob = float
        """

        image_file = 'images/self_driving.png'
        image_matrix = image_to_matrix(image_file)
        num_components = 5
        gmm = GaussianMixtureModel(image_matrix, num_components)
        gmm.initialize_training()
        gmm.means = np.array([0.03921569, 0.1764706,  0.06666667, 0.42745098, 0.2784314])
        test_val = 0.03921569
        joint_prob = gmm.joint_prob(test_val)
        self.assertEqual(round(joint_prob, 4), -0.9413,
                         msg="Incorrect joint log probability")
Пример #5
0
    def test_gmm_likelihood(self):
        """Testing the GMM method
        for calculating the overall
        model probability.
        Should return -302844.

        returns:
        likelihood = float
        """

        image_file = 'images/self_driving.png'
        image_matrix = image_to_matrix(image_file)
        num_components = 5
        gmm = GaussianMixtureModel(image_matrix, num_components)
        gmm.initialize_training()
        gmm.means = np.array([0.03921569, 0.1764706,  0.06666667, 0.42745098, 0.2784314])

        likelihood = gmm.likelihood()
        self.assertEqual(round(likelihood), -302844,
                         msg="Incorrect model probability")
Пример #6
0
    def test_gmm_likelihood(self):
        """Testing the GMM method
        for calculating the overall
        model probability.
        Should return -364370.

        returns:
        likelihood = float
        """

        image_file = 'images/party_spock.png'
        image_matrix = image_to_matrix(image_file)
        num_components = 5
        gmm = GaussianMixtureModel(image_matrix, num_components)
        gmm.initialize_training()
        gmm.means = [0.4627451, 0.10196079, 0.027450981,
                     0.011764706, 0.1254902]
        likelihood = gmm.likelihood()
        self.assertEqual(round(likelihood), -364370,
                         msg="Incorrect model probability")
Пример #7
0
    def test_bayes_info(self):
        """
        Test for your
        implementation of
        BIC on fixed GMM values.
        Should be about 610317.

        returns:
        BIC = float
        """

        image_file = 'images/self_driving.png'
        image_matrix = image_to_matrix(image_file)
        num_components = 3
        initial_means = np.array([0.4627451, 0.10196079, 0.027450981])
        gmm = GaussianMixtureModel(image_matrix, num_components)
        gmm.initialize_training()
        gmm.means = np.copy(initial_means)
        b_i_c = bayes_info_criterion(gmm)
        self.assertEqual(round(610317, -3), round(b_i_c, -3),
                         msg="BIC calculation incorrect.")
Пример #8
0
    def test_gmm_joint_prob(self):
        """Testing the GMM method
        for calculating the joint
        log probability of a given point.
        Should return -0.98196.

        returns:
        joint_prob = float
        """

        image_file = 'images/party_spock.png'
        image_matrix = image_to_matrix(image_file)
        num_components = 5
        gmm = GaussianMixtureModel(image_matrix, num_components)
        gmm.initialize_training()
        gmm.means = [0.4627451, 0.10196079, 0.027450981,
                     0.011764706, 0.1254902]
        test_val = 0.4627451
        joint_prob = gmm.joint_prob(test_val)
        self.assertEqual(round(joint_prob, 4), -0.982,
                         msg="Incorrect joint log probability")
    def test_convergence_condition(self):
        """
        Compare the performance of
        the default convergence function
        with the new convergence function.

        return:
        default_convergence_likelihood = float
        new_convergence_likelihood = float
        """

        image_file = 'images/party_spock.png'
        image_matrix = image_to_matrix(image_file)
        num_components = 3
        initial_means = np.array([0.4627451, 0.10196079, 0.027450981])

        # first test original model
        gmm = GaussianMixtureModel(image_matrix, num_components)
        gmm.initialize_training()
        gmm.means = np.copy(initial_means)
        gmm.train_model()
        default_convergence_likelihood = gmm.likelihood()

        # now test new convergence model
        gmm_new = GaussianMixtureModelConvergence(image_matrix, num_components)
        gmm_new.initialize_training()
        gmm_new.means = np.copy(initial_means)
        gmm_new.train_model()
        new_convergence_likelihood = gmm_new.likelihood()

        # test convergence difference
        convergence_diff = new_convergence_likelihood - \
            default_convergence_likelihood
        convergence_thresh = 8200
        print new_convergence_likelihood
        print default_convergence_likelihood
        self.assertTrue(convergence_diff >= convergence_thresh,
                        msg=("Likelihood difference between"
                             " the original and converged"
                             " models less than %.2f" % convergence_thresh))
Пример #10
0
    def test_gmm_improvement(self):
        """
        Tests whether the new mixture
        model is actually an improvement
        over the previous one: if the
        new model has a higher likelihood
        than the previous model for the
        provided initial means.

        returns:
        original_segment = numpy.ndarray[numpy.ndarray[float]]
        improved_segment = numpy.ndarray[numpy.ndarray[float]]
        """

        image_file = 'images/self_driving.png'
        image_matrix = image_to_matrix(image_file)
        num_components = 4
        initial_means = np.array([0.4627451, 0.20392157, 0.36078432, 0.47254905])
        # first train original model with fixed means
        gmm = GaussianMixtureModel(image_matrix, num_components)
        gmm.initialize_training()
        gmm.means = np.copy(initial_means)
        gmm.train_model()
        original_segment = gmm.segment()
        original_likelihood = gmm.likelihood()
        # then train improved model
        gmm_improved = GaussianMixtureModelImproved(image_matrix,
                                                    num_components)
        gmm_improved.initialize_training()
        gmm_improved.train_model()
        improved_segment = gmm_improved.segment()
        improved_likelihood = gmm_improved.likelihood()
        # then calculate likelihood difference
        diff_thresh = 2.2e3
       
        likelihood_diff = improved_likelihood - original_likelihood
        print(improved_likelihood, original_likelihood, likelihood_diff)
        self.assertTrue(likelihood_diff >= diff_thresh,
                        msg=("Model likelihood less than "
                             "%d higher than original model" % diff_thresh))
Пример #11
0
    def test_convergence_condition(self):
        """
        Compare the performance of
        the default convergence function
        with the new convergence function.

        return:
        default_convergence_likelihood = float
        new_convergence_likelihood = float
        """

        image_file = 'images/self_driving.png'
        image_matrix = image_to_matrix(image_file)
        num_components = 4
        initial_means = np.array([0.25882354, 0.05686277, 0.75686275, 0.3882353])

        # first test original model
        gmm = GaussianMixtureModel(image_matrix, num_components)
        gmm.initialize_training()
        gmm.means = np.copy(initial_means)
        gmm.train_model()
        default_convergence_likelihood = gmm.likelihood()

        # now test new convergence model
        gmm_new = GaussianMixtureModelConvergence(image_matrix, num_components)
        gmm_new.initialize_training()
        gmm_new.means = np.copy(initial_means)
        gmm_new.train_model()
        new_convergence_likelihood = gmm_new.likelihood()

        # test convergence difference
        convergence_diff = new_convergence_likelihood - \
            default_convergence_likelihood
        convergence_thresh = 1800
        self.assertTrue(convergence_diff >= convergence_thresh,
                        msg=("Likelihood difference between"
                             " the original and converged"
                             " models less than %.2f" % convergence_thresh))