#!/usr/bin/python3 # Minimal example. Use the convenience function io.get_image_data() without any # extra arguments. from imagecluster import calc, io as icio, postproc # The bottleneck is calc.fingerprints() called in this function, all other # operations are very fast. get_image_data() writes fingerprints to disk and # loads them again instead of re-calculating them. images, fingerprints, timestamps = icio.get_image_data('downloads/cart icon/') # Run clustering on the fingerprints. Select clusters with similarity index # sim=0.5. clusters = calc.cluster(fingerprints, sim=0.5) # Create dirs with links to images. Dirs represent the clusters the images # belong to. postproc.make_links(clusters, 'downloads/cart icon/imagecluster/clusters') # Plot images arranged in clusters. postproc.visualize(clusters, images)
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) # Optionally run a PCA on the fingerprints to compress the dimensions. Use a # cumulative explained variance ratio of 0.95. fps = ic.pca(fps, n_components=0.95) # 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)
def main(imagedir, sim=0.5, layer='fc2', size=(224,224), links=True, vis=False, max_csize=None, pca=False, pca_params=dict(n_components=0.9)): """Example main app using this library. Upon first invocation, the image and fingerprint databases are built and written to disk. Each new invocation loads those and only repeats * clustering * creation of links to files in clusters * visualization (if `vis=True`) This is good for playing around with the `sim` parameter, for instance, which only influences clustering. Parameters ---------- imagedir : str path to directory with images sim : float (0..1) similarity index (see :func:`calc.cluster`) layer : str which layer to use as feature vector (see :func:`calc.get_model`) size : tuple input image size (width, height), must match `model`, e.g. (224,224) links : bool create dirs with links vis : bool plot images in clusters max_csize : max number of images per cluster for visualization (see :mod:`~postproc`) pca : bool Perform PCA on fingerprints before clustering, using `pca_params`. pca_params : dict kwargs to sklearn's PCA Notes ----- imagedir : To select only a subset of the images, create an `imagedir` and symlink your selected images there. In the future, we may add support for passing a list of files, should the need arise. But then again, this function is only an example front-end. """ fps_fn = pj(imagedir, ic_base_dir, 'fingerprints.pk') ias_fn = pj(imagedir, ic_base_dir, 'images.pk') ias = None if not os.path.exists(fps_fn): print(f"no fingerprints database {fps_fn} found") os.makedirs(os.path.dirname(fps_fn), exist_ok=True) model = ic.get_model(layer=layer) if not os.path.exists(ias_fn): print(f"create image array database {ias_fn}") ias = ic.image_arrays(imagedir, size=size) co.write_pk(ias, ias_fn) else: ias = co.read_pk(ias_fn) print("running all images through NN model ...") fps = ic.fingerprints(ias, model) co.write_pk(fps, fps_fn) else: print(f"loading fingerprints database {fps_fn} ...") fps = co.read_pk(fps_fn) if pca: fps = ic.pca(fps, **pca_params) print("pca dims:", list(fps.values())[0].shape[0]) print("clustering ...") clusters = ic.cluster(fps, sim) if links: pp.make_links(clusters, pj(imagedir, ic_base_dir, 'clusters')) if vis: if ias is None: ias = co.read_pk(ias_fn) pp.visualize(clusters, ias, max_csize=max_csize)