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")
Пример #2
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))