This project compares the similarity between images using features extracted from the VGG16 pre-trained deep learning model. It uses cosine similarity to compute the similarity scores between feature vectors.
📦image_similarity
┣ 📂data
┃ ┗ 📂images
┃ ┃ ┣ 📜cat1.jpg
┃ ┃ ┣ 📜cat2.jpg
┃ ┃ ┣ 📜dog1.jpg
┃ ┃ ┗ 📜dog2.jpg
┣ 📜LICENSE.txt
┣ 📜README.md
┣ 📜image_similarity.ipynb
┗ 📜requirements.txtTo install the necessary libraries, use the following command:
pip install -r requirements.txtopencv-python
numpy
matplotlib
scikit-image
scikit-learn
keras
tensorflow
- Clone the repository to your local machine.
- Ensure you have Python installed.
- Install the required libraries using the
requirements.txtfile:pip install -r requirements.txt
- Place your images in the
data/images/directory. - Open and run the
image_similarity.ipynbnotebook to compare the images.
The image_similarity.ipynb notebook contains the following sections:
Description: This cell imports all the necessary libraries for image processing, feature extraction, and similarity calculation. These include:
opencv-pythonfor image processing.numpyfor numerical operations.matplotlibfor plotting.scikit-imagefor additional image processing functions.scikit-learnfor similarity calculations.kerasandtensorflowfor using the pre-trained VGG16 model and deep learning operations.
Purpose: To ensure that all required libraries are imported and available for use in subsequent cells.
Description: This cell defines a function load_and_preprocess_image that:
- Loads an image from the specified path.
- Resizes the image to a target size (224x224) required for VGG16.
- Applies necessary preprocessing steps like scaling pixel values using
keras'spreprocess_input.
Purpose: To handle the loading and preprocessing of images, preparing them for input into the VGG16 model.
Description: This cell defines a function extract_vgg16_features that:
- Loads the pre-trained VGG16 model with weights trained on ImageNet.
- Creates a new model that outputs features from the 'fc1' layer of VGG16.
- Extracts and flattens the features from the preprocessed image.
Purpose: To use the pre-trained VGG16 model to extract deep features from the preprocessed image, which are used for comparing the images.
Description: This cell defines a function calculate_similarity that:
- Computes the cosine similarity between two given feature vectors using
scikit-learn'scosine_similarityfunction.
Purpose: To calculate the cosine similarity between two feature vectors, providing a measure of similarity that ranges between -1 and 1.
Description: This cell defines a function display_images that:
- Displays a list of images along with their titles in a single figure using
matplotlib.
Purpose: To visually display the images along with their titles, helping to verify the images being compared and understand the context of the similarity scores.
Description: This cell defines a function plot_similarities that:
- Creates a bar plot to visualize the pairwise similarity scores between the images using
matplotlib.
Purpose: To provide an intuitive visual representation of the similarity scores, showing how similar each pair of images is based on the extracted features.
Description: This cell defines a function compare_images that:
- Loads and preprocesses each image.
- Extracts features from each image using the VGG16 model.
- Calculates pairwise similarities between the images.
- Displays the images.
- Plots the similarity scores.
- Prints the similarity results.
Purpose: To orchestrate the complete image comparison process by integrating all the previously defined functions, from loading and preprocessing images to displaying results.
Description: This cell defines the main function that:
- Specifies the list of image paths to be compared.
- Calls the
compare_imagesfunction to execute the comparison.
Purpose: To act as the entry point for the script, specifying the images to compare and initiating the comparison process.
Description: This cell runs the main function.
Purpose: To start the image comparison process when the notebook is executed.
If you would like to contribute to this project, please fork the repository and submit a pull request with your improvements.
This project is licensed under the MIT License.