From 0a4da92c0f60924613af6eeb773305d0105d4f72 Mon Sep 17 00:00:00 2001 From: Steve Schmerler Date: Mon, 18 Feb 2019 01:34:04 +0100 Subject: [PATCH] ENH: Add examples dir: API, main, plot dendrogram --- examples/example_api.py | 23 +++++++++++++++++++++++ examples/example_main.py | 3 +++ examples/plot_dendrogram.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 examples/example_api.py create mode 100644 examples/example_main.py create mode 100644 examples/plot_dendrogram.py diff --git a/examples/example_api.py b/examples/example_api.py new file mode 100644 index 0000000..c580a3b --- /dev/null +++ b/examples/example_api.py @@ -0,0 +1,23 @@ +from imagecluster import calc as ic +from imagecluster import postproc as pp + +# Create image database in memory. This helps to feed images to the NN model +# quickly. +ias = ic.image_arrays('pics/', size=(224,224)) + +# Create Keras NN model. +model = ic.get_model() + +# Feed images through the model and extract fingerprints (feature vectors). +fps = ic.fingerprints(ias, model) + +# Run clustering on the fingerprints. Select clusters with similarity index +# sim=0.5 +clusters = ic.cluster(fps, sim=0.5) + +# Create dirs with links to images. Dirs represent the clusters the images +# belong to. +pp.make_links(clusters, 'pics/imagecluster/clusters') + +# Plot images arranged in clusters. +pp.visualize(clusters, ias) diff --git a/examples/example_main.py b/examples/example_main.py new file mode 100644 index 0000000..7a98755 --- /dev/null +++ b/examples/example_main.py @@ -0,0 +1,3 @@ +from imagecluster import main + +main.main('pics/', sim=0.65, vis=True, maxelem=30) diff --git a/examples/plot_dendrogram.py b/examples/plot_dendrogram.py new file mode 100644 index 0000000..220367f --- /dev/null +++ b/examples/plot_dendrogram.py @@ -0,0 +1,29 @@ +from matplotlib import pyplot as plt +import numpy as np +from scipy.cluster.hierarchy import dendrogram + +from imagecluster import calc as ic + +ias = ic.image_arrays('pics/', size=(224,224)) +model = ic.get_model() +fps = ic.fingerprints(ias, model) +clusters,extra = ic.cluster(fps, sim=0.5, extra_out=True) + +# linkage matrix Z +Z = extra['Z'] + +fig,ax = plt.subplots() +dendrogram(Z, ax=ax) + +# Adjust yaxis labels (values from Z[:,2]) to our definition of the `sim` +# parameter. +ymin, ymax = ax.yaxis.get_data_interval() +tlocs = np.linspace(ymin, ymax, 5) +ax.yaxis.set_ticks(tlocs) +tlabels = np.linspace(1, 0, len(tlocs)) +ax.yaxis.set_ticklabels(tlabels) +ax.set_xlabel("image index") +ax.set_ylabel("sim") + +fig.savefig('dendrogram.png') +plt.show()