スペクトラルクラスタリングを用いたギリシャのコインの分割

Machine LearningMachine LearningBeginner
今すぐ練習

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

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

この実験では、スペクトラルクラスタリングを使って、ギリシャのコインの画像を複数の部分的に均質な領域に分割します。スペクトラルクラスタリングは、データセット内のクラスタをそれらの特徴間の類似性に基づいて識別するために使用できる強力な手法です。この実験では、画像上のボクセル間の差分からグラフを作成し、その後画像を複数の部分的に均質な領域に分割することで、スペクトラルクラスタリングを使って画像を分割します。

VMのヒント

VMの起動が完了したら、左上隅をクリックして ノートブック タブに切り替えて、Jupyter Notebookを使って練習します。

時々、Jupyter Notebookが読み込み終了するまで数秒待つ必要があります。Jupyter Notebookの制限により、操作の検証は自動化できません。

学習中に問題に直面した場合は、Labbyにお尋ねください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。


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{{"スペクトラルクラスタリングを用いたギリシャのコインの分割"}} end

画像の読み込みと前処理

まず、ギリシャのコインの画像を読み込み、処理しやすくするために前処理を行います。画像を元のサイズの20%にリサイズし、ダウンスケーリングする前にガウシアンフィルタを適用して平滑化し、エイリアシングアーティファクトを軽減します。

## 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)

画像を辺の勾配値付きのグラフに変換する

画像を辺の勾配値付きのグラフに変換します。betaの値が小さいほど、分割は実際の画像から独立します。beta=1の場合、分割はボロノイ分割に近くなります。

## 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

スペクトラルクラスタリングを適用する

デフォルトの固有値ソルバーeigen_solver='arpack'を使用してスペクトラルクラスタリングを適用します。使用できる既実装のソルバーは、eigen_solver='arpack'、'lobpcg'、または'amg'のいずれかです。eigen_solver='amg'を選択するには、'pyamg'と呼ばれる追加のパッケージが必要です。分割の品質と計算速度は、主にソルバーの選択と許容誤差'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)

分割結果を可視化する

分割された領域の輪郭を重ねて元の画像をプロットすることで、得られた領域を可視化します。

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()

まとめ

この実験では、スペクトラルクラスタリングを使って、ギリシャのコインの画像を複数の部分的に均質な領域に分割しました。画像を前処理し、辺の勾配値付きのグラフに変換し、スペクトラルクラスタリングを適用し、得られた領域を可視化しました。スペクトラルクラスタリングは、データセット内のクラスターをそれらの特徴間の類似性に基づいて識別するために使用できる強力な手法です。