This repository has been archived by the owner on Dec 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ENH: Add examples dir: API, main, plot dendrogram
- Loading branch information
Showing
3 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |