6.7 C
New York
Friday, December 8, 2023

Revolutionizing Picture Retrieval with Neo4j


Image this: You’re trying to find merchandise that appear like one thing you’ve seen and cherished earlier than. Discovering visually comparable gadgets is usually a game-changer, whether or not vogue, residence decor, or devices. However how do you navigate by an unlimited sea of pictures to pinpoint these good matches?

Enter the world of strong picture retrieval methods. These digital wizards have the facility to establish gadgets with strikingly comparable visible options and current them to you want a customized procuring assistant. Right here’s the key sauce: Picture retrieval discovers pictures resembling a given question picture inside a large dataset. Prior to now, this was usually carried out by evaluating pixel values immediately – a gradual and not-so-accurate course of. However worry not; there’s a better manner.

Image Retrieval | Neo4j

We’re speaking about embeddings and cosine similarity – the dynamic duo of contemporary picture retrieval. Embeddings are like magic codes representing pictures in a compact, feature-packed format. They seize the essence of a picture, its distinctive visible fingerprint, if you’ll. After which comes cosine similarity, the genius behind the scenes. It measures these embeddings’ similarity, supplying you with lightning-fast and extremely correct outcomes.

With this powerhouse combo, you may say goodbye to the times of scrolling endlessly to search out that good look-alike. Because of embeddings and cosine similarity, you’ll simply uncover visually comparable pictures and merchandise, making your on-line procuring expertise extra environment friendly and pleasant than ever earlier than.

Studying Targets

On this article, you’ll study

  • Introduction to Neo4j – a graph database administration system.
  • Picture retrieval significance and enterprise use instances.
  • Embedding technology utilizing pretrained CNNs
  • Storing and Retrieving embeddings utilizing Neo4j
  • Creating relationships between Neo4j nodes utilizing embedding’s cosine similarity.

This text was revealed as part of the Information Science Blogathon.

What’s Neo4j?

Neo4j is a graph database that enables us to retailer knowledge within the type of nodes and relationships. Graph databases optimize graph computations, making them preferrred for storing and querying graph knowledge. Varied corporations, together with eBay and Walmart, use Neo4j, a preferred graph database. It additionally finds use amongst startups like Medium and Coursera

What are the Benefits of Utilizing Graph Databases?

  • Advanced Relationship Dealing with: Ultimate for social networks and complicated connections.
  • Schema Flexibility: Adapt to evolving knowledge buildings effortlessly.
  • Native Question Language: Use Cypher for expressive graph queries.
  • Traversal Efficiency: Effectively navigate relationships, e.g., shortest paths.
  • Scalability: Deal with huge datasets and elevated workloads.
  • Expressive Information Modeling: Versatile modeling for numerous buildings.
  • Advanced Question Assist: Proficiently execute intricate graph queries.
  • Sample Matching Efficiency: Distinctive in superior sample matching.
  • Actual-Time Insights: Provides on the spot insights into related knowledge.

Significance of Environment friendly Picture Retrieval

Environment friendly picture retrieval is essential in numerous domains, together with e-commerce, content material sharing platforms, healthcare, and extra. It permits customers to rapidly discover visually comparable pictures, aiding in product suggestions, content material curation, medical picture evaluation, and extra. By bettering retrieval velocity and accuracy, companies can improve consumer experiences, enhance engagement, and make better-informed choices based mostly on picture knowledge.

The next strategy have 2 components:

Half 1: Producing Embeddings and Storing Information in Neo4j

1. Information Preparation

For simplicity, I scraped few automotive pictures from google and saved them in a folder referred to as pictures. The pictures are saved within the .jpg format.

import torch
import torchvision.transforms as transforms


preprocess = transforms.Compose([
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),

Pretrained networks works nicely if we resize the photographs to it enter layer dimension once we are producing embeddings. On this case, we will likely be utilizing ResNet50, which takes in pictures of dimension 224×224. We will likely be resizing the photographs to this dimension earlier than producing embeddings.

Put up resizing, we’ll normalize the photographs utilizing the imply and customary deviation of the ImageNet dataset. That is carried out to make sure that the photographs are in the identical format as the photographs used to coach the community.

2. Producing Embeddings

For embedding technology, we will likely be utilizing a pre-trained mannequin referred to as ResNet50. ResNet50 is a convolutional neural community that’s 50 layers deep. Its skip connections make it simpler to coach than a equally deep plain community. It’s educated on greater than 1,000,000 pictures from the ImageNet database. The community is educated to categorise pictures into 1000 object classes. We will likely be utilizing the community to generate embeddings for our pictures.

When the networks layers are trimmed, we are able to use the community to generate embeddings for our pictures. The embeddings generated by the community will likely be 2048-dimensional. These embeddings will likely be used to retrieve comparable pictures.

Mannequin loading

import torchvision.fashions as fashions

mannequin = fashions.resnet50(pretrained=True)
mannequin = nn.Sequential(*listing(mannequin.youngsters())[:-1]) 

mannequin = fashions.resnet50(pretrained=True)
mannequin = nn.Sequential(*listing(mannequin.youngsters())[:-1]) 
def get_image_embeddings(image_path):
    img = Picture.open(image_path).convert('RGB')
    img = preprocess(img)
    img = img.unsqueeze(0)  # Add batch dimension
    with torch.no_grad():
        embeddings = mannequin(img)
    return embeddings.squeeze().numpy()

3. Storing Information in Neo4j

We will likely be utilizing Neo4j to retailer the embeddings and the picture paths. Neo4j is a graph database that enables us to retailer knowledge within the type of nodes and relationships. We will likely be storing the embeddings as node properties and the picture paths as node labels. We may also be making a relationship between the embeddings and the picture paths. It will permit us to question the database for comparable pictures.

from neo4j import GraphDatabase

# Connect with the Neo4j database
uri = "<neo4j_bolt_url>"
username = "<neo4j_username>"
password = "<neo4j_password>"

class Neo4jDatabase:
    def __init__(self):
        self._driver = GraphDatabase.driver(uri, auth=(username, password))

    def shut(self):

    def create_image_node(self, embeddings,path):
        with self._driver.session() as session:
            session.write_transaction(self._create_image_node, embeddings,path)

    def _create_image_node(tx, embeddings,path):
        question = (
            "CREATE (img:Picture {embeddings: $embeddings,path: $path})"
        tx.run(question, embeddings=embeddings,path = path)

After the information insertion, the graph seems like this:

Image Retrieval | Neo4j

Half 2: Picture Retrieval

For the inference half, the place we have to predict the same pictures, we will likely be utilizing the cosine similarity metric. Cosine similarity is a measure of similarity between two non-zero vectors of an interior product area. It’s outlined to equal the cosine of the angle between them, which can be the identical because the interior product of the identical vectors normalized to each have size 1. We will likely be utilizing the embeddings generated by the ResNet50 mannequin to calculate the cosine similarity between the question picture and the photographs within the database. The pictures with the very best cosine similarity would be the most just like the question picture.

Retrieving similar images | Image Retrieval | Neo4j

First the picture embeddings are generated for the question picture. Then it’s handed to the Neo4j database to retrieve the same pictures.

MATCH (a:Picture)
WITH a, gds.similarity.cosine(a.embeddings, $query_embeddings) AS similarity
ORDER BY similarity DESC
LIMIT $top_k
RETURN a.id AS image_id, a.embeddings AS embeddings, a.path as image_path, similarity

The above question returns the highest okay comparable pictures to the question picture. The question returns the picture id, picture path, and the cosine similarity between the question picture and the retrieved pictures. Use the cosine similarity to rank the photographs in descending order. Return the highest okay pictures. The node even have the picture path as a property, which may retrieve the picture from the file system.

The Neo4j server performs these computations, leading to considerably sooner processing in comparison with client-side execution. This effectivity is attributed to the server’s optimization for graph computations. Node properties retailer the embeddings in a columnar format, facilitating speedy retrieval. Moreover, the Neo4j server handles the computation of cosine similarity, leveraging its optimization for graph computations. This permits for sooner retrieval of comparable pictures.

Streamlit App

I’ve created a pattern StreamLit app to check our logics.

The UI have an choice to add a picture. It processes within the beneath circulation get fetch the related pictures.

  •  Generates Picture embeddings utilizing Resnet50 mannequin
  • Queries Neo4j DB to fetch comparable embeddings and picture places
  • Shows the same pictures again to the UI

Sounds attention-grabbing?

The code for the pattern app is right here.


Creating Relations Between Nodes

Let’s say, if we’re dealing with with solely restricted pictures and we need to create relations between them. We are able to use the next code to create relations between the photographs.

This permits to keep away from the similarity calculations for each question we do.

MATCH (n1:Picture), (n2:Picture)
WHERE id(n1) < id(n2)  // To keep away from duplicate pairs and self-comparisons
WITH n1, n2, gds.similarity.cosine(n1.embeddings, n2.embeddings) AS similarity
WHERE similarity > 0.8
CREATE (n1)-[:SIMILAR_TO {score: similarity}]->(n2)

This creates a relations, and it may be seen within the Neo4j graph.

creating relations with nodes | Image Retrieval | Neo4j

After we question the database for comparable pictures, we are able to additionally embody the relations within the question. It will permit us to retrieve the same pictures together with the relations between them. Use this  to visualise the relations between the photographs.

MATCH (a:Picture)-[r:SIMILAR_TO]->(b:Picture)
WITH a, b, gds.similarity.cosine(a.embeddings, $query_embeddings) AS similarity, r
ORDER BY similarity DESC
LIMIT $top_k
RETURN a.id AS image_id, a.embeddings AS embeddings, a.path as image_path, similarity, r


On this article, we noticed easy methods to generate embeddings for pictures utilizing a pre-trained mannequin. We additionally noticed easy methods to retailer the embeddings and picture paths in Neo4j. Lastly, we noticed easy methods to retrieve comparable pictures utilizing the cosine similarity metric. This strategy allows environment friendly retrieval of comparable pictures. It finds purposes in numerous domains, together with e-commerce, content material sharing platforms, healthcare, and extra. Primarily based on the information set now we have, we are able to additionally strive totally different picture embeddings fashions and similarity metrics to mess around.

Continuously Requested Questions

Q1. How do picture embeddings work?

A. Picture embeddings compress pictures into lower-dimensional vector representations, offering a numerical illustration of the picture content material. You’ll be able to generate picture embeddings by totally different strategies, comparable to CNNs, unsupervised studying, pre-trained networks, and switch studying.

Q2. What are graph embeddings used for?

A. A graph embedding determines a hard and fast size vector illustration for every entity (often nodes) in our graph. These embeddings are a decrease dimensional illustration of the graph and protect the graph’s topology.

Q3. What are some great benefits of Neo4j?

A. Neo4j CQL question language instructions are in humane readable format and really simple to study. It makes use of easy and highly effective knowledge mannequin. It does NOT require advanced Joins to retrieve related/associated knowledge as it is rather simple to retrieve its adjoining node or relationship particulars with out Joins or Indexes.

This fall. What are relationships in graph database?

A. Relationships describe a connection between a supply node and a goal node. They all the time have a route (one route). It will need to have a kind (one sort) to outline (classify) what sort of relationship they’re.

The media proven on this article just isn’t owned by Analytics Vidhya and is used on the Creator’s discretion.

Related Articles


Please enter your comment!
Please enter your name here

Latest Articles