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))
Ejemplo n.º 2
0
    def test_k_means(self):
        """
        Testing your implementation
        of k-means on the segmented
        bird_color_24 reference images.
        """
        k_min = 2
        k_max = 6
        image_dir = 'images/'
        image_name = 'bird_color_24.png'
        image_values = image_to_matrix(image_dir + image_name)
        # initial mean for each k value
        initial_means = [
            np.array([[0.90980393, 0.8392157, 0.65098041],
                      [0.83137256, 0.80784315, 0.69411767]]),
            np.array([[0.90980393, 0.8392157, 0.65098041],
                      [0.83137256, 0.80784315, 0.69411767],
                      [0.67450982, 0.52941179, 0.25490198]]),
            np.array([[0.90980393, 0.8392157, 0.65098041],
                      [0.83137256, 0.80784315, 0.69411767],
                      [0.67450982, 0.52941179, 0.25490198],
                      [0.86666667, 0.8392157, 0.70588237]]),
            np.array([[0.90980393, 0.8392157, 0.65098041],
                      [0.83137256, 0.80784315, 0.69411767],
                      [0.67450982, 0.52941179, 0.25490198],
                      [0.86666667, 0.8392157, 0.70588237], [0, 0, 0]]),
            np.array([[0.90980393, 0.8392157, 0.65098041],
                      [0.83137256, 0.80784315, 0.69411767],
                      [0.67450982, 0.52941179, 0.25490198],
                      [0.86666667, 0.8392157, 0.70588237], [0, 0, 0],
                      [0.8392157, 0.80392158, 0.63921571]]),
        ]
        # test different k values to find best
        for k in range(k_min, k_max + 1):
            updated_values = k_means_cluster(image_values, k, initial_means[k - k_min])

            ref_image = image_dir + 'k%d_%s' % (k, image_name)
            ref_values = image_to_matrix(ref_image)
            matrix_to_image(updated_values,"./testImage-%d.png" % k)
            dist = image_difference(updated_values, ref_values)
            self.assertEqual(int(dist), 0, msg=("Clustering for %d clusters"
                             + "produced unrealistic image segmentation.") % k)
Ejemplo n.º 3
0
    prev_cluster_allocation = np.zeros((1, row * col))

    while (1):
        for cluster_number in range(k):
            distance[cluster_number] = np.linalg.norm(image_values_flat -
                                                      means[cluster_number],
                                                      axis=1)

        cluster_allocation = np.argmin(distance, axis=0)
        if (np.array_equal(prev_cluster_allocation, cluster_allocation)):
            break

        else:
            prev_cluster_allocation = cluster_allocation

        for cluster_number in range(k):
            indices = np.where(cluster_allocation == cluster_number)
            means[cluster_number] = np.mean(image_values_flat[[indices]][0],
                                            axis=0)

    for cluster_number in range(k):
        indices = np.where(cluster_allocation == cluster_number)
        image_values_flat[[indices]] = means[cluster_number]

    updated_image_values = unflatten_image_matrix(image_values_flat, col)
    return updated_image_values


updated_values = k_means_cluster(image_values, 10, initial_means=None)
matrix_to_image(updated_values, 'images/kmeans.png')