This code demonstrates a method for comparing images based on their image encodings using a Convolutional Neural Network (CNN) backbone. The process involves extracting image embeddings from a pretrained CNN model and then calculating the cosine similarity between these embeddings to quantify the visual similarity between images. The code then visualizes image pairs along with their computed similarity scores.
- Python
- PyTorch
- Fastai
- Matplotlib
- NumPy
- Make sure you have the required libraries installed in your Python environment.
- Place the images you want to compare in a folder and set the
folder_path
variable to the path of that folder. - Run the code to perform image comparison.
- The code will display image pairs along with their similarity scores.
The output will be a visual representation of how similar the images in the specified folder are to each other. The similarity scores range from 0 (dissimilar) to 1 (identical), where a higher score indicates greater visual similarity.
The code includes a function called plot_image_pairs
, which plots image pairs and their similarity scores. An example of the output can be seen in the code above.
- The code uses a pretrained "convnext_tiny" model for feature extraction, but you can replace it with other pretrained models as needed.
- You can adjust the image size, which is currently set to 192x192 pixels, by modifying the resizing step in the
get_embedding
function.
This code provides a simple yet effective way to compare images based on their visual content, making it useful for various applications such as image retrieval, duplicate detection, and content analysis.