The family of Area to Point Matching, which is good at handling matching challenges like large/various resolution images, large scale/viewpoint changes and etc.
This is a user-friendly implementation of Area to Point Matching (A2PM) framework, powered by hydra.
It contains the implementation of SGAM (arXiv'23, early-version-code), a training-free version of MESA (CVPR'24, project page) and DMESA (TPAMI'26, project page).
Due to the power of hydra, the implementation is highly configurable and easy to extend.
It supports the implementation of feature matching approaches adopting the A2PM framework, and also enables the combination of new point matching and area matching methods.
- Table of Contents
- News
- Qualitative Results of MESA and DMESA
- Installation
- Usage
- Segmentation Preprocessing
- DEMO
- hydra-based Configuration
- Benchmark Test
- Citation
- Acknowledgement
-
🎇 2025-11-07: Add the MASt3R mode for the area matching of DMESA and MESA-F, please refer to the mast3r-dmesa-configs and mast3r-mesa-f-configs to use.
- NOTE the MESA-f is relatively slow with MASt3R.
- Use them in this environment.
- Overall, the generalization ability of MESA&DMESA has been significantly improved by the foundation model MASt3R.
-
🖖 2025-09&10: MASt3R, DUSt3R and ELoFTR are supported.
- The expected results are provided in:
- The configuration notes are provided in MASt3R & DUSt3R Configuration Notes.
-
🎁 2025-01-02: An operation manual about running MESA on Win11 has been added here.
- Thanks @MY-QY!
-
🕶️ 2024-11-03: Add the wrapper for single image pair matching. See here
-
🪖 2024-09-11: SAM2 is supported in the segmentation preprocessing. See here.
from top to bottom:
SGAM,SGAM_ASpan,ASpan.
To begin with, you need to install the dependencies following the instructions below.
Check this(中文版运行手册 on win11) for running on Win11 also.
git clone --recursive https://github.com/Easonyesheng/A2PM-MESA
# or if you have already cloned the repository
# git submodule update --init --recursive
cd A2PM-MESA- We recommend using
condato create a new environment for this project.
conda create -n A2PM python==3.8
conda activate A2PM-
Install torch, torchvision, and torchaudio by running the following command.
- Note the requirements of the torch version is soft, and the code is tested on
torch==2.0.0+cu118.
pip install torch==2.0.0+cu118 torchvision==0.15.1+cu118 torchaudio==2.0.1 --index-url https://download.pytorch.org/whl/cu118
- Note the requirements of the torch version is soft, and the code is tested on
-
Install the basic dependencies by running the following command.
pip install -r requirements.txt
This code is based on hydra, which is a powerful configuration system for Python applications. The documentation of hydra can be found here.
Please carefully read the following instructions to understand how to use this code with your own needs.
-
If you wanna test your environment, you can turn to DEMO for a quick start, where we provide off-the-shelf configurations in
./conf/experiment/demo.yamland minimal input files in./demo/folder.- The code entry point is in
./scripts/demo.py, where you can run the code with its configuration in./conf/experiment/demo.yaml.
- The code entry point is in
-
If you want to run the full pipeline, you can turn to Benchmark Test; but you need to prepare SAM results for your datasets following the instructions in Segmentation Preprocessing.
-
If you want to dive into the code and develop your own matching methods based on this framework:
- You first need to understand the basic logic of hydra we used: Initialize a python class with its configuration as a
yamlfile. We keep the names ofclass-yamlpairs consistent in the code. - Then, you can follow the instructions in hydra-based Configuration to understand and configure every component of the A2PM framework.
- Afterwards, you can also add new components by adding new classes and configurations in the corresponding folders.
- You first need to understand the basic logic of hydra we used: Initialize a python class with its configuration as a
The segmentation results are needed for the area matching methods.
- To use Segment Anything Model (SAM) for segmentation, we provide our inference code in
segmentor/. To use it, you need to:- we provide a forked version of SAM in
./SAM/, so do not forget togit submodule update --init --recursiveto get the submodule. - install the dependencies of SAM.
cd segment-anything; pip install -e .pip install opencv-python pycocotools matplotlib onnxruntime onnx
- download and set the pre-trained model path in
segmentor/ImgSAMSeg.py#L34.
- we provide a forked version of SAM in
- See the
segmentor/sam_seg.shfor image-level SAM segmentation.
- We also support the SAM2 model in the segmentation preprocessing.
- Note the SAM2 seems to provide less masks than SAM, see the issue, but it is faster anyway.
- Set
--sam_name SAM2insegmentor/sam_seg.shand config the model path insegmentor/ImgSAMSeg.py#L48to use it. - If you encounter the
hydrainitialization error, add the following code in theSAM2/sam2/__init__.py:from hydra.core.global_hydra import GlobalHydra GlobalHydra.instance().clear()
-
We provide a demo script
scripts/demo.pywith corresponding configurations inconf/experiment/demo.yaml. You can run the script like:cd scripts python demo.py +experiment=demo -
We provide a set of demo images in
./demo/, which are sampled from the ETH3D dataset.- The SAM results must be provided in
./demo/samres, if you want to use the SAM-based area matcher. - If no intrinsic camera parameters are provided, you should use
gamas thegeo_area_matcher, otherwise, you can useegam.
- The SAM results must be provided in
-
Set the
geo_area_matcher: gamin theconf/experiment/demo.yamlto use the original GAM, which can draw the matching results on the images. -
Some other
yamlfiles starting withdemo_in theconf/experiment/folder are also provided for you to test different area matchers and point matchers.
In the following, we will introduce each components of this code with corresponding hydra configurations.
We offer dataloaders for two widely-used datasets, including
ScanNet1500andMegaDepth1500.
-
You can follow the instructions in LoFTR to download the datasets and set the paths in the configuration files in
conf/dataset/. -
As our methods rely on the segmentation results, whose paths are also needed to be set in the configuration files in
conf/dataset/. For example,sem_mode: the segmentation method used, includingSAM,SEEM,GTsem_folder: path to the folder containing the segmentation resultssem_post: the file format of the segmentation results (For ScanNet, it is npy if usingSAM, and png if usingSEEMorGT)
-
The segmentation process is discussed in Segmentation Preprocessing.
-
More datasets can be easily added by adding new dataloaders in
dataloader/and setting the corresponding configurations inconf/dataset/.
Area matching is to establish semantic area matches between two images for matching reduandancy reduction, which is the core of the A2PM framework.
- We provide three area matchers, including:
-
Semantic Area Matching(our method in SGAM)- The implementation in
area_matchers/sem_am.py. - The configuration in
conf/area_matcher/sem_area_matcher.yaml. - See also the repository SGAM-code.
- The implementation in
-
MESA-free- A training-free version of MESA.
- The implementation in
area_matchers/mesa.py. - The configuration is in
conf/area_matcher/mesa-f.yaml. - The
MESA-freeeliminates the need for training the area similarity calculation module in MESA, and directly uses the off-the-shelf patch matching in ASpanFormer like DMESA. - The
MESA-freeis easier to use, but its performance is slightly lower than the originalMESA.
-
DMESA- A dense counterpart of MESA proposed in paper, more einfficient and flexible.
- The implementation is in
area_matchers/dmesa.py. - The configuration is in
conf/area_matcher/dmesa.yaml.
-
Point matching is to establish point matches between two (area) images.
-
Here, we provide some point matchers, including:
Sparse: {SuperPoint+SuperGlue}Semi-Dense: {ASpanFormer, LoFTR, ELoFTR}Dense: {DKM, MASt3R, DUSt3R}
-
Their configurations are put in
conf/point_matcher/, with warppers inpoint_matchers/. -
For some of them, the inside paths need to be modified, which has been fixed in the submodules we provided in
point_matchers/.- Use
git clone --recursivewhen you clone the repo. - Or use
git submodule update --init --recursiveafter direct clone.
- Use
-
Before running, you need download the pre-trained models and put them in the corresponding paths in the configuration
yamlfiles. -
More point matchers can be easily added by adding simialr wrappers.
-
DO NOT forget to run
git submodule update --init --recursiveto get the submodules inreconer/mast3r/.- Need
git submodule update --init --recursivein thereconer/mast3r/folder to getdust3randdust3r/croco.
- Need
-
Note that the
mast3rrequires specific environment, please refer to its repo to create a new environment.- After installation, run
pip install loguru hydra-core seaborn kornia yacs pytorch-lightning PyMaxFlowin themast3rconda environment. If there are more missing packages, please install them accordingly. - Download the
MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric.pthweights from here and set the path inconf/point_matcher/mast3r.yaml#L2,conf/experiment/demo_dmesa_mast3r.yaml#L70andA2PM-MESA/conf/experiment/demo_mesaf_mast3r.yaml#L66. - Then, you can perform MESA/DMESA for
mast3randdust3r, likecd ./scripts && python demo.py +experiment=demo_dmesa_mast3r.
- After installation, run
-
For
mast3r's Results, see here; and fordust3r's Results, see here.- We use the weights provided in the
mast3rrepo:MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric.pth&DUSt3R_ViTLarge_BaseDecoder_512_dpt.pth.
- We use the weights provided in the
We fuse matches from multiple inside-area point matching by the geometry area matching module.
-
The configuration is in
conf/geo_area_matcher/. -
We provide two fusion methods, including:
- original GAM proposed in SGAM
- code in
geo_area_matcher/gam.py - configuration in
conf/geo_area_matcher/gam.yaml
- code in
- a more effective GAM
- code in
geo_area_matcher/egam.py - configuration in
conf/geo_area_matcher/egam.yaml
- code in
- original GAM proposed in SGAM
The A2PM framework will combine the above components to form a complete feature matching pipeline.
-
The implementation is in
scripts/test_a2pm.py. You can run the shell scriptscripts/test_in_dev.shto test the A2PM framework on a pair of images. -
The pipeline configurations are set in
conf/experiment/*.yaml. You can choose the one you want to use by setting the+experiment=xxx.yamlin the shell script.
-
We provide the evaluation code in
metric/. The evaluation metrics include:- Pose estimation AUC
- Mean Matching Accuracy
- Area Matching Accuracy
-
The
metric/instance_eval.pyis used to evaluate the instance-level matching results. It is used intest_a2pm.py. -
The
metric/eval_ratios.pyis used to evaluate the batch-level matching results. Set the paths in thepyfile and run it to get the evaluation results.
You can run the benchmark test by running the shell script such as:
NOTE: First, eval_ratios.py#L21~L26 (about the result path) need to be modified accordingly.
cd ./scripts
./dmesa-dkm-md.sh # DMESA+DKM on MegaDepth1500; named as {area-matcher}-{point-matcher}-{dataset}.sh
# check the metric results
# TODO: Merge into the same script
cd ../metric
python eval_ratios.py
# eval_ratios.py#L21~L26 need to be modified accordingly.- You can change the configurations in the shell script to test different methods, i.e.
+experiment=xxx. - Also, modify the path in
eval_ratios.py#L21(${exp_root_path}/res,exp_root_pathin shell scripts) and folder name ineval_ratios.py#L24(the folder where results are saved) to get the evaluation results.
NOTE: The following expected results are achieved before GAM debug in version 200e33af8192308fff5ee1055280334b7aa3befd, so there may be slight differences if you run the code after this commit, where the alpha_list parameters should be modified accordingly.
- Take DKM as an example, the expected results are as follows:
- Config. files:
conf/experiment/a2pm_dmesa_egam_dkm_megadepth.yamlconf/experiment/a2pm_dmesa_egam_dkm_scannet.yamlconf/experiment/a2pm_mesa_egam_dkm_megadepth.yamlconf/experiment/a2pm_mesa_egam_dkm_scannet.yaml
| SN1500( |
DKM | MESA-free+DKM | DMESA+DKM |
|---|---|---|---|
| Pose AUC@5 | 30.26 | 31.64 | 30.96 |
| Pose AUC@10 | 51.51 | 52.80 | 52.41 |
| Pose AUC@20 | 69.43 | 70.08 | 69.74 |
| MD1500( |
DKM | MESA-free+DKM | DMESA+DKM |
|---|---|---|---|
| Pose AUC@5 | 63.61 | 63.85 | 65.65 |
| Pose AUC@10 | 76.75 | 77.38 | 78.46 |
| Pose AUC@20 | 85.72 | 86.47 | 86.97 |
- We use
$512\times512$ images for MASt3R, as it is trained on this resolution. - Config. files:
conf/experiment/a2pm_dmesa_egam_mast3r_megadepth.yamlconf/experiment/a2pm_dmesa_egam_mast3r_scannet.yamlconf/experiment/a2pm_mesa_egam_mast3r_megadepth.yamlconf/experiment/a2pm_mesa_egam_mast3r_scannet.yaml
| SN1500( |
MASt3R | MESA-free+MASt3R | DMESA+MASt3R |
|---|---|---|---|
| Pose AUC@5 | 28.25 | 32.79 | 34.23 |
| Pose AUC@10 | 51.01 | 55.13 | 56.23 |
| Pose AUC@20 | 69.69 | 72.60 | 73.42 |
| MD1500( |
MASt3R | MESA-free+MASt3R | DMESA+MASt3R |
|---|---|---|---|
| Pose AUC@5 | 46.34 | 53.98 | 57.58 |
| Pose AUC@10 | 63.02 | 69.99 | 72.00 |
| Pose AUC@20 | 76.50 | 81.80 | 82.86 |
- We use
$512\times512$ images for DUSt3R, as it is trained on this resolution. - Config. files:
conf/experiment/a2pm_dmesa_egam_dust3r_megadepth.yamlconf/experiment/a2pm_dmesa_egam_dust3r_scannet.yamlconf/experiment/a2pm_mesa_egam_dust3r_megadepth.yamlconf/experiment/a2pm_mesa_egam_dust3r_scannet.yaml
| SN1500( |
DUSt3R | MESA-free+DUSt3R | DMESA+DUSt3R |
|---|---|---|---|
| Pose AUC@5 | 15.84 | 20.72 | 23.01 |
| Pose AUC@10 | 32.69 | 39.14 | 42.89 |
| Pose AUC@20 | 49.36 | 56.62 | 60.86 |
| MD1500( |
DUSt3R | MESA-free+DUSt3R | DMESA+DUSt3R |
|---|---|---|---|
| Pose AUC@5 | 15.99 | 25.94 | 24.48 |
| Pose AUC@10 | 33.69 | 44.04 | 42.37 |
| Pose AUC@20 | 53.51 | 61.83 | 61.04 |
- We use
$640\times480$ images for ELoFTR, as it is trained on this resolution. - We use the official pre-trained model provided in the repo.
- Only ScanNet1500 is tested here.
- Config. files:
conf/experiment/a2pm_dmesa_egam_eloftr_scannet.yamlconf/experiment/a2pm_mesa_egam_eloftr_scannet.yaml
| SN1500( |
ELoFTR | MESA-free+ELoFTR | DMESA+ELoFTR |
|---|---|---|---|
| Pose AUC@5 | 22.15 | 23.62 | 23.07 |
| Pose AUC@10 | 41.08 | 42.37 | 41.66 |
| Pose AUC@20 | 57.91 | 58.66 | 58.27 |
-
In this evaluation code, we fix the random seed to '2' (see
scripts/test_a2pm.py), which is different from the settings in our paper (without fixing the random seed). Thus, the results are slightly different from the results in the paper for DMESA, but the effectiveness of our methods is consistent. -
The training-free version of MESA showcases a slight precision drop comprared with MESA (paper verison), but it still improves the baseline performance.
-
Also, the parameters in the configuration files are set to the default values. Due to the complexity of the parameter settings, we have not tuned the parameters for all AM+PM combinations.
- Better results can be achieved by tuning the parameters for specific datasets and tasks.
- However, the default parameters are enough to show the effectiveness of our methods.
If you find this work useful, please consider citing:
@article{SGAM,
title={Searching from Area to Point: A Hierarchical Framework for Semantic-Geometric Combined Feature Matching},
author={Zhang, Yesheng and Zhao, Xu and Qian, Dahong},
journal={arXiv preprint arXiv:2305.00194},
year={2023}
}@InProceedings{MESA,
author = {Zhang, Yesheng and Zhao, Xu},
title = {MESA: Matching Everything by Segmenting Anything},
booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
month = {June},
year = {2024},
pages = {20217-20226}
}@ARTICLE{dmesa,
author={Zhang, Yesheng and Shen, Shuhan and Zhao, Xu},
journal={IEEE Transactions on Pattern Analysis and Machine Intelligence},
title={MESA: Effective Matching Redundancy Reduction by Semantic Area Segmentation},
year={2025},
volume={},
number={},
pages={1-18},
doi={10.1109/TPAMI.2025.3644296}}
We thank the authors of the following repositories for their great works:



