ml4a / ml4a

A python library and collection of notebooks for making art with machine learning.

Home Page:https://ml4a.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Image-search: is it possible to find similar images from uploaded photo?

auyeskhan-n opened this issue · comments

get_closest_image function takes the only query_image_idx (which is like id of random image in dataset).
Is it possible to upload there real image (image that doesn't exist on trained dataset) and find similar images?

Yes, you can load a new image, and analyze it the same way as the others, then compute the distances. Something like this should work (not tested so might be minor errors):

img, x = get_image(image_path);
feat = feat_extractor.predict(x)[0]
feat_pca = pca.transform([feat])
distances = [ distance.euclidean(feat_pca, feat) for feat in pca_features ]
idx_closest = sorted(range(len(distances)), key=lambda k: distances[k])[0:num_results]

in the example, is there a collision between feat in line 2 and the one in line 4? does it matter that pca_features has 300 dimensions while feat has 4096?

no, because it's a list comprehension and the feat in line 4 just refers to elements of pca_features, and has 300 elements not 4096 as the original variable feat, but i should have written this differently as it looks ambiguous. instead, can revise as:

img, x = get_image(image_path);
feat = feat_extractor.predict(x)[0]
feat_pca = pca.transform([feat])
distances = [ distance.euclidean(feat_pca, pf) for pf in pca_features ]
idx_closest = sorted(range(len(distances)), key=lambda k: distances[k])[0:num_results]

i hope this is more clear.