Segmenting Greek Coins with Spectral Clustering

Machine LearningMachine LearningBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will use spectral clustering to segment an image of Greek coins into multiple partly-homogeneous regions. Spectral clustering is a powerful technique that can be used to identify clusters in a dataset based on the similarity between their features. In this lab, we will use spectral clustering to segment an image by creating a graph from voxel-to-voxel difference on an image and then breaking the image into multiple partly-homogeneous regions.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL ml(("`Machine Learning`")) -.-> ml/FrameworkandSoftwareGroup(["`Framework and Software`"]) ml/FrameworkandSoftwareGroup -.-> ml/sklearn("`scikit-learn`") subgraph Lab Skills ml/sklearn -.-> lab-49083{{"`Segmenting Greek Coins with Spectral Clustering`"}} end

Load and preprocess the image

We will start by loading the image of Greek coins and pre-processing it to make it easier to work with. We will resize the image to 20% of the original size and apply a Gaussian filter for smoothing prior to down-scaling to reduce aliasing artifacts.

## load the coins as a numpy array
orig_coins = coins()

## Resize it to 20% of the original size to speed up the processing
## Applying a Gaussian filter for smoothing prior to down-scaling
## reduces aliasing artifacts.
smoothened_coins = gaussian_filter(orig_coins, sigma=2)
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect", anti_aliasing=False)

Convert the image into a graph with the value of the gradient on the edges

We will convert the image into a graph with the value of the gradient on the edges. The smaller beta is, the more independent the segmentation is of the actual image. For beta=1, the segmentation is close to a voronoi.

## Convert the image into a graph with the value of the gradient on the
## edges.
graph = image.img_to_graph(rescaled_coins)

## Take a decreasing function of the gradient: an exponential
beta = 10
eps = 1e-6
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps

Apply spectral clustering

We will apply spectral clustering using the default eigen_solver='arpack'. Any implemented solver can be used: eigen_solver='arpack', 'lobpcg', or 'amg'. Choosing eigen_solver='amg' requires an extra package called 'pyamg'. The quality of segmentation and the speed of calculations is mostly determined by the choice of the solver and the value of the tolerance 'eigen_tol'.

## Apply spectral clustering using the default eigen_solver='arpack'.
## Any implemented solver can be used: eigen_solver='arpack', 'lobpcg', or 'amg'.
## Choosing eigen_solver='amg' requires an extra package called 'pyamg'.
## The quality of segmentation and the speed of calculations is mostly determined
## by the choice of the solver and the value of the tolerance 'eigen_tol'.
n_regions = 26
n_regions_plus = 3
for assign_labels in ("kmeans", "discretize", "cluster_qr"):
    t0 = time.time()
    labels = spectral_clustering(
        graph,
        n_clusters=(n_regions + n_regions_plus),
        eigen_tol=1e-7,
        assign_labels=assign_labels,
        random_state=42,
    )
    t1 = time.time()
    labels = labels.reshape(rescaled_coins.shape)

Visualize the segmentation

We will visualize the resulting regions by plotting the original image and overlaying the contours of the segmented regions.

plt.figure(figsize=(5, 5))
plt.imshow(rescaled_coins, cmap=plt.cm.gray)
plt.xticks(())
plt.yticks(())
title = "Spectral clustering: %s, %.2fs" % (assign_labels, (t1 - t0))
print(title)
plt.title(title)
for l in range(n_regions):
    colors = [plt.cm.nipy_spectral((l + 4) / float(n_regions + 4))]
    plt.contour(labels == l, colors=colors)
plt.show()

Summary

In this lab, we used spectral clustering to segment an image of Greek coins into multiple partly-homogeneous regions. We preprocessed the image, converted it into a graph with the value of the gradient on the edges, applied spectral clustering, and visualized the resulting regions. Spectral clustering is a powerful technique that can be used to identify clusters in a dataset based on the similarity between their features.

Other Machine Learning Tutorials you may like