Skip to content
This repository has been archived by the owner on Dec 13, 2024. It is now read-only.

Commit

Permalink
ENH: Add examples dir: API, main, plot dendrogram
Browse files Browse the repository at this point in the history
  • Loading branch information
elcorto committed Feb 18, 2019
1 parent 872d45f commit 0a4da92
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
23 changes: 23 additions & 0 deletions examples/example_api.py
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions examples/example_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from imagecluster import main

main.main('pics/', sim=0.65, vis=True, maxelem=30)
29 changes: 29 additions & 0 deletions examples/plot_dendrogram.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 0a4da92

Please sign in to comment.