Skip to content

Latest commit

 

History

History
696 lines (364 loc) · 18.4 KB

plot_image_matching_pytorch.rst

File metadata and controls

696 lines (364 loc) · 18.4 KB

html

Note

Go to the end <sphx_glr_download_auto_examples_6.image_matching_by_QAP_plot_image_matching_pytorch.py> to download the full example code

sphx-glr-example-title

PyTorch Backend Example: Matching Image Keypoints by QAP Solvers

This example shows how to match image keypoints by graph matching solvers provided by pygmtools. These solvers follow the Quadratic Assignment Problem formulation and can generally work out-of-box. The matched images can be further processed for other downstream tasks.

Note

The following solvers support QAP formulation, and are included in this example:

  • ~pygmtools.classic_solvers.rrwm (classic solver)
  • ~pygmtools.classic_solvers.ipfp (classic solver)
  • ~pygmtools.classic_solvers.sm (classic solver)
  • ~pygmtools.neural_solvers.ngm (neural network solver)

Load the images

Images are from the Willow Object Class dataset (this dataset also available with the Benchmark of pygmtools, see ~pygmtools.dataset.WillowObject).

The images are resized to 256x256.

Visualize the images and keypoints

/auto_examples/6.image_matching_by_QAP/images/sphx_glr_plot_image_matching_pytorch_001.png

Build the graphs

Graph structures are built based on the geometric structure of the keypoint set. In this example, we refer to Delaunay triangulation.

We encode the length of edges as edge features

Visualize the graphs

/auto_examples/6.image_matching_by_QAP/images/sphx_glr_plot_image_matching_pytorch_002.png

Extract node features

Let's adopt the VGG16 CNN model to extract node features.

Normalize the features

Up-sample the features to the original image size

Visualize the extracted CNN feature (dimensionality reduction via principle component analysis)

/auto_examples/6.image_matching_by_QAP/images/sphx_glr_plot_image_matching_pytorch_003.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x7fd85c402770>

Extract node features by nearest interpolation

Build affinity matrix

We follow the formulation of Quadratic Assignment Problem (QAP):

$$\begin{aligned} &\max_{\mathbf{X}} \ \texttt{vec}(\mathbf{X})^\top \mathbf{K} \texttt{vec}(\mathbf{X})\\\ s.t. \quad &\mathbf{X} \in \{0, 1\}^{n_1\times n_2}, \ \mathbf{X}\mathbf{1} = \mathbf{1}, \ \mathbf{X}^\top\mathbf{1} \leq \mathbf{1} \end{aligned}$$

where the first step is to build the affinity matrix (K)

Visualization of the affinity matrix. For graph matching problem with N nodes, the affinity matrix has N2 × N2 elements because there are N2 edges in each graph.

Note

The diagonal elements are node affinities, the off-diagonal elements are edge features.

/auto_examples/6.image_matching_by_QAP/images/sphx_glr_plot_image_matching_pytorch_004.png

sphx-glr-script-out

<matplotlib.image.AxesImage object at 0x7fd85c4625f0>

Solve graph matching problem by RRWM solver

See ~pygmtools.classic_solvers.rrwm for the API reference.

The output of RRWM is a soft matching matrix. Hungarian algorithm is then adopted to reach a discrete matching matrix.

Plot the matching

The correct matchings are marked by green, and wrong matchings are marked by red. In this example, the nodes are ordered by their ground truth classes (i.e. the ground truth matching matrix is a diagonal matrix).

/auto_examples/6.image_matching_by_QAP/images/sphx_glr_plot_image_matching_pytorch_005.png

Solve by other solvers

We could also do a quick benchmarking of other solvers on this specific problem.

IPFP solver

See ~pygmtools.classic_solvers.ipfp for the API reference.

/auto_examples/6.image_matching_by_QAP/images/sphx_glr_plot_image_matching_pytorch_006.png

SM solver

See ~pygmtools.classic_solvers.sm for the API reference.

/auto_examples/6.image_matching_by_QAP/images/sphx_glr_plot_image_matching_pytorch_007.png

NGM solver

See ~pygmtools.neural_solvers.ngm for the API reference.

Note

The NGM solvers are pretrained on a different problem setting, so their performance may seem inferior. To improve their performance, you may change the way of building affinity matrices, or try finetuning NGM on the new problem.

The NGM solver pretrained on Willow dataset:

/auto_examples/6.image_matching_by_QAP/images/sphx_glr_plot_image_matching_pytorch_008.png

The NGM solver pretrained on VOC dataset:

/auto_examples/6.image_matching_by_QAP/images/sphx_glr_plot_image_matching_pytorch_009.png

sphx-glr-timing

Total running time of the script: (0 minutes 3.513 seconds)

html

Download Python source code: plot_image_matching_pytorch.py <plot_image_matching_pytorch.py>

Download Jupyter notebook: plot_image_matching_pytorch.ipynb <plot_image_matching_pytorch.ipynb>

html