By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: Graph Neural Networks for Image Similarity: An Alternative to Hashing? | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > Graph Neural Networks for Image Similarity: An Alternative to Hashing? | HackerNoon
Computing

Graph Neural Networks for Image Similarity: An Alternative to Hashing? | HackerNoon

News Room
Last updated: 2025/02/26 at 5:26 PM
News Room Published 26 February 2025
Share
SHARE

Traditional hashing techniques like perceptual hashing (pHash) and locality-sensitive hashing (LSH) are widely used for image similarity detection. However, they often fail in real-world scenarios where images undergo transformations such as cropping, noise addition or color changes.

Graph Neural Networks: A Smarter Approach to Image Similarity

Graph Neural Networks (GNNs) provide a more robust alternative by modeling image relationships as a graph and propagating similarity information through message passing. Instead of treating images as isolated entities, Graph Neural Networks (GNNs) represent images as nodes in a graph where edges define their relationships. This allows context aware learning making similarity detection far more robust.

Let’s talk about a Real world Needs

Consider an AI driven content moderation system that should detect inappropriate images across billions of uploads daily. If a flagged image is slightly altered/cropped, filtered or resized traditional hashing fails allowing harmful content to evade detection.

Now imagine an e-commerce search engine that helps customers find similar products. Two shirts with different backgrounds but identical designs might have very different pixel values but should still be recognized as the same product.

In both cases a GNN-based approach would outperform traditional hashing by understanding relationships beyond pixels using graph structures to connect similar items dynamically.

Building an Image Similarity System with Graph Neural Networks

Let’s walk through a tutorial to build GNNs on any image set.

  1. Extract Image Features Using CLIP or ResNet:

    Before constructing a graph, we extract feature embeddings from images using a pre-trained model

import torch
import torchvision.transforms as transforms
from PIL import Image
from torchvision import models

#Load a pre-trained model
model = models.resnet50(pretrained=True)
model.fc = torch.nn.Identity()
model.eval()

#Image transformation
transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.ToTensor()
])

#Load and preprocess an image
def extract_features(image_path):
    image = Image.open(image_path).convert('RGB')
    image = transform(image).unsqueeze(0)  #Add batch dimension
    with torch.no_grad():
        features = model(image)
    return features.numpy().flatten()

feature_vector = extract_features("my_image.jpg")
  1. Construct a Graph from Image Embeddings

    We can now construct a KNN (K Nearest Neighbor) graph where each node represents an image and edges indicate similarity.

import networkx as nx
import numpy as np
from sklearn.neighbors import NearestNeighbors

#Simulated feature vectors for 100 images (assuming 512D embeddings)
image_embeddings = np.random.rand(100, 512)

#Build a KNN graph
k = 5
nbrs = NearestNeighbors(n_neighbors=k, metric="cosine").fit(image_embeddings)
distances, indices = nbrs.kneighbors(image_embeddings)

#Construct the graph
G = nx.Graph()
for i in range(len(image_embeddings)):
    for j in range(1, k):  # Skip self-loop
        G.add_edge(i, indices[i, j], weight=1 - distances[i, j])

#Visualize graph structure
print("Graph nodes:", G.number_of_nodes())
print("Graph edges:", G.number_of_edges())
  1. Train a Graph Neural Network (GNN) for Similarity Learning

We now use PyTorch Geometric to train a GraphSAGE model on this graph.

import torch
import torch.nn.functional as F
from torch_geometric.nn import SAGEConv
from torch_geometric.data import Data

#Convert the NetworkX graph to PyTorch Geometric format
edge_index = torch.tensor(list(G.edges)).t().contiguous()
x = torch.tensor(image_embeddings, dtype=torch.float)

#Define a GraphSAGE model
class GraphSAGE(torch.nn.Module):
    def __init__(self, in_channels, hidden_channels, out_channels):
        super().__init__()
        self.conv1 = SAGEConv(in_channels, hidden_channels)
        self.conv2 = SAGEConv(hidden_channels, out_channels)
    def forward(self, x, edge_index):
        x = self.conv1(x, edge_index)
        x = F.relu(x)
        x = self.conv2(x, edge_index)
        return x

#Initialize and train model
model = GraphSAGE(512, 256, 128)  #Reduce embedding size to 128D
optimizer = torch.optim.Adam(model.parameters(), lr=0.01)

for epoch in range(50):
    optimizer.zero_grad()
    out = model(x, edge_index)
    loss = F.mse_loss(out, x)  #Self-supervised learning
    loss.backward()
    optimizer.step()
    print(f"Epoch {epoch+1}, Loss: {loss.item()}")
  1. Querying Similar Images Using the GNN Embeddings

Now that training is complete, we can retrieve the top k most similar images for a given query.

from sklearn.metrics.pairwise import cosine_similarity

#Extract GNN embeddings
with torch.no_grad():
    learned_embeddings = model(x, edge_index).numpy()

#Compute cosine similarity
query_idx = 0  # Example query image
similarities = cosine_similarity([learned_embeddings[query_idx]], learned_embeddings)
top_k = np.argsort(-similarities[0])[:5]

print("Most similar images:", top_k)

Final Thoughts

GNNs create a smarter and more adaptive approach to image similarity. Unlike hashing, which relies on rigid pixel comparisons, GNNs learn meaningful relationships between images, making them far more robust to transformations, occlusions, and noise.

In today’s world where image datasets are exploding in size from social media platforms to medical imaging archives and the rise of synthetic data, scalability and accuracy are critical. GNNs provide an efficient way to structure and retrieve similar images improving applications like visual search, content moderation, recommendation engines and AI driven media management.

The future of image search isn’t just about finding duplicates, it’s about understanding the deeper connections between images, and GNNs take us one step forward to that a reality.

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Valve VR headset just tipped to launch by end of this year — and this could be the price
Next Article How to Eliminate Smart and Pro Players in BGMI: Check Out This Guide
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Ring is giving away FREE outdoor cameras worth £80 – and it’s easy to apply
News
How to Use Your iPad as a Second Monitor With Your Mac
Gadget
Everything I heard at the AVCA Conference |
Computing
Hugging Face to Democratize Robotics with Open-Source Reachy 2 Robot
News

You Might also Like

Computing

Everything I heard at the AVCA Conference |

9 Min Read
Computing

The Best Way to Protect Your Packages and Your Ethics | HackerNoon

8 Min Read
Computing

Why Glovo thinks Nigeria is its biggest African bet yet

11 Min Read
Computing

The TechBeat: Big Monitoring, Small Budget: Powering Observability on Kubernetes with Prometheus, Grafana & Mimir (5/10/2025) | HackerNoon

5 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?