In this post, we’ll explore Q-Seg, a quantum-powered unsupervised image segmentation algorithm designed to solve segmentation challenges in complex datasets like satellite imagery, where traditional methods fall short. This post will delve into both the mathematics behind Q-Seg and a practical implementation using a Jupyter notebook.
Why Quantum for Image Segmentation?
Classical segmentation methods rely on extensive labeled data and can struggle with noisy or ambiguous regions. Q-Seg leverages quantum computing’s ability to explore vast solution spaces rapidly, achieving segmentation without requiring labeled data or extensive preprocessing.
The Math Behind Q-Seg
1. Image to Graph Representation
The first step in Q-Seg is representing the image as a graph. As an image is a grid of pixel intensity values, an equivalent graph representation is when:
Each pixel becomes a node.
Edges between nodes carry weights based on pixel similarity.
In our method, edge weights, $w(p_i, p_j)$, are calculated with the Gaussian similarity metric:
where $a=-1$ and $b=1$ are the desired bounds. This formulation of the edge weights provides a general similarity score that effectively works, but every specific use case might require a custom edge weight metric.
2. Minimum Cut Formulation as a QUBO Problem
The goal is to divide the graph into distinct regions by cutting the least-similar edges. In graph terms, we’re finding the “minimum cut” that segments the image efficiently. This cut problem is translated into a Quadratic Unconstrained Binary Optimization (QUBO) problem, compatible with quantum annealing:
where $x$ is a binary vector representing which nodes belong to each segmented region.
3. Solving with Quantum Annealing
Quantum annealing, and specifically the D-Wave Advantage, efficiently solves the QUBO formulation through quantum tunneling. Here’s a quick summary of the annealing process:
Initialization: The system begins in a superposition of all possible states.
Annealing: Quantum tunneling and entanglement allow the system to “explore” multiple solutions simultaneously.
Final State: The lowest-energy state corresponds to the optimal segmentation mask.
This quantum approach can quickly navigate the exponentially large solution space that would be computationally intense for classical solvers, especially as image sizes grow.
Experimentation and Implementation Guide
Now that we’ve covered the math and the algorithm’s workflow, let’s walk through the implementation in Python. We’ll use a Jupyter notebook to illustrate each step and run an experiment with a real quantum annealer on cloud.
Implementation
This guide provides instructions for setting up and running the Q-Seg project, a quantum-based image segmentation algorithm leveraging D-Wave’s quantum annealer. Follow these steps to get started.
Prerequisites
Python Version: Has been tested on Python 3.9.11 and above
Jupyter Notebook: For interactive examples and visualization.
Installation
Clone the Repository: Begin by cloning the Q-Seg repository to your local machine. Open a terminal/cmd/powershell and run
1
2
git clone https://github.com/supreethmv/Q-seg.git
cd Q-seg
Set Up a Virtual Environment (Optional): Following good practices and to avoid dependency conflicts, create and activate a virtual environment.
1
2
python3 -m venv qseg_env # or `virtualenv qseg_env`source qseg_env/bin/activate # On Windows, use `qseg_env\Scripts\activate`
Install Dependencies: Use the provided requirements.txt to install all necessary libraries. The process may take a few minutes depending on your internet speed and the packages already available in your system.
1
pip install-r requirements.txt
Code Walkthrough - Tutorial
After setting up the environment, open the Q-Seg Tutorial Jupyter notebook in the root of the repository directory to start exploring the operational pipeline.
# Importing functions from the modules in the qseg package
fromqseg.graph_utilsimportimage_to_grid_graph,draw,draw_graph_cut_edgesfromqseg.dwave_utilsimportannealer_solverfromqseg.utilsimportdecode_binary_string# Additional necessary imports
importnumpyasnpimportmatplotlib.pyplotaspltimportnetworkxasnximporttime
normalized_nx_elist=image_to_grid_graph(image)# We are using Guassian similarity metric as the edge weight metric
G=nx.grid_2d_graph(image.shape[0],image.shape[1])G.add_weighted_edges_from(normalized_nx_elist)draw(G,image)
Solve using D-Wave annealer
Note: This requires you to have an account in D-Wave Ocean, up on logging in, the D-Wave Leap dashboard is displayed, you can find the private token on the left column under Solver API Token
If you do not wish to run the problem on the remote Quantum Annealer, you can also try out the Gurobi solver which runs locally on your machine. Scroll down for code.
Figure: Operational pipeline of Q-Seg using the D-Wave quantum annealer.
The above figure gives an overview of all the processes that took place in order to solve the segmentation problem as a QUBO problem on the D-Wave Quantum Annealer.
All samples received
1
samples_dataframe
0
1
2
3
4
5
6
7
8
chain_break_fraction
energy
num_occurrences
0
0
1
0
0
1
0
1
1
0
0.0
-4.0870
709
1
1
0
1
1
0
1
0
0
1
0.0
-4.0830
1288
2
1
1
0
1
1
0
1
1
0
0.0
-2.5878
1
3
0
0
1
0
0
1
0
0
1
0.0
-2.5720
1
4
0
1
1
0
1
1
1
1
0
0.0
-1.5098
1
Decode binary solution string to a binary segmentation mask
NOTE: Gurobi is an off-the-shelf solver, it is licensed and not open source. The free version allows to run problems upto size 200 only, which is the number of nodes in our case. However, if you are a student or part of an educational institute, you can obtain an academic license for non commercial use for free. Check this tutorial by Gurobi on how to obtain an academic license if you are a student, researcher or faculty.
Here’s a video for a quick overview of the method and results