|
|
|
|
|
orig_coins = coins() |
|
|
|
|
|
|
|
|
|
smoothened_coins = gaussian_filter(orig_coins, sigma=2) |
|
rescaled_coins = rescale(smoothened_coins, 0.2, mode="reflect", anti_aliasing=False) |
|
|
|
|
|
|
|
graph = image.img_to_graph(rescaled_coins) |
|
|
|
|
|
|
|
|
|
beta = 10 |
|
eps = 1e-6 |
|
graph.data = np.exp(-beta * graph.data / graph.data.std()) + eps |
|
|
|
|
|
|
|
|
|
n_regions = 26 |
|
|
|
|
|
|
|
|
|
n_regions_plus = 3 |
|
|
|
|
|
def getClusteringPlot(algorithm): |
|
t0 = time.time() |
|
labels = spectral_clustering( |
|
graph, |
|
n_clusters=(n_regions + n_regions_plus), |
|
eigen_tol=1e-7, |
|
assign_labels=algorithm, |
|
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" % (algorithm, (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) |
|
|
|
return plt |
|
|
|
import gradio as gr |
|
|
|
def welcome(name): |
|
return f"Welcome to Gradio, {name}!" |
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown( |
|
""" |
|
# Segmenting the picture of greek coins in regions πͺ |
|
An application of spectral clustering. |
|
![Image of coins](coins.jpeg "a title") |
|
|
|
""") |
|
inp = gr.Radio(["kmeans", "discretize", "cluster_qr"], label="Solver", info="Choose a clustering algorithm", value="kmeans") |
|
plot = gr.Plot(label="Plot") |
|
inp.change(getClusteringPlot, inputs=inp, outputs=[plot]) |
|
demo.load(getClusteringPlot, inputs=[inp], outputs=[plot]) |
|
|
|
if __name__ = "main": |
|
demo.launch() |