akarshzingade / image-similarity-deep-ranking

369 stars 103 forks source link

Comparing images #6

Closed tautvydasversockas closed 6 years ago

tautvydasversockas commented 6 years ago

Hi, how can I compare 2 images with trained model?

akarshzingade commented 6 years ago

Hey! You can get the features from the last layer (the feature will of length 4096) for the 2 images that you want to compare. Then use euclidean distance to compare them. Ideally, if you have trained the model with gap parameter 'g' set to 1, the euclidean distance of <1 means similar image and >1 means dissimilar image. Hope this helps!

victorcadena commented 6 years ago

Hi!

I have a question here... How do I get in order with a already trained model the images by similarity? Do I have to implement a function, make forward prop and compare the vectors between my new image and all the images?

Or do you have something implemented for this?

Thanks again!!

alteest commented 6 years ago

I have the same question about search images by similarity with source image. Need to calculate similarity score between source image and all images from dataset?

AmiraAyadi commented 6 years ago

@alteest that what I did, I used Cosinus similarity.

victorcadena commented 6 years ago

Hi,

I tried making a new prediction and this is what I implemented

img = image.load_img('test1.jpg',target_size=(224,224)) a = image.img_to_array(img) a = a[np.newaxis,...] print(a.shape) b = deep_rank_model.predict([a,a,a]) print(b)

But I have some questions. Is this the best way to pass an image? What pre-processing does the model expect?

tautvydasversockas commented 6 years ago

How can I get the features from the last layer for the 2 images if input of the network is 3 images? Can you provide a code example for that?

akarshzingade commented 6 years ago

Hey, guys. I have added a script that prints out the similarity between the two images. Please check it out.

@tautvydasversockas If you look at line 95 of deepranking_get_distance.py you will know how to pass the image to the network and also the output of the line will be the features (embeddings) of the image passed.

@victorcadena You are right about how to pass the image to the model. As for the pre-processing, you will have to mimic what you did while training the model. The script in this repository does the following pre-processing: 1) Scaling by (1/255). 2) Resizing the image to 224x224.

@alteest For image search, you will have to compute the distance between the source image and all the images in the dataset. But, there are ways to optimise this search. One way is to 1) Cluster all the dataset image embeddings and store the cluster means. 2) Compare the source image embeddings with the cluster means. 3) Select the closest cluster. 4) Compare the source image embeddings with the dataset images belonging to the selected cluster. You can also create hierarchical clusters to further optimise it.

Hope this helps guys. :)

alteest commented 6 years ago

Thank you with test script! It's really helpful!

tautvydasversockas commented 6 years ago

Thank You for the script and explanations!