MASt3R

repository·main·Indexed 25 days ago

https://github.com/naver/mast3r

Official implementation for grounding image matching in 3D, building upon DUSt3R. MASt3R provides tools for stereo 3D reconstruction and sparse global alignment. It supports integration with the Dune encoder, retrieval strategies for match extraction, and visual localization experiments across datasets such as Aachen-Day-Night, InLoc, 7-scenes, and Cambridge Landmarks.

Tokens
4.7K
Snippets
10
Records
13
Agent score
35%

What's inside MASt3R

  1. Download MASt3R Model Checkpoints

    main

    MASt3R models can be automatically downloaded via huggingface_hub integration. Alternatively, you can manually download the MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric.pth checkpoint into a checkpoints/ directory.

    mkdir -p checkpoints/
    wget https://download.europe.naverlabs.com/ComputerVision/MASt3R/MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric.pth -P checkpoints/
  2. Run a MASt3R training demo with CO3D

    main

    To perform a short demonstration of MASt3R training, you can use a small subset of the CO3D dataset. This process involves downloading the CO3D repository, preparing a single sequence subset, preprocessing the data, downloading a pretrained DUSt3R checkpoint, and then running the training script via torchrun.

    # download and prepare the co3d subset
    mkdir -p data/co3d_subset
    cd data/co3d_subset
    git clone https://github.com/facebookresearch/co3d
    cd co3d
    python3 ./co3d/download_dataset.py --download_folder ../ --single_sequence_subset
    rm ../*.zip
    cd ../../..
    
    python3 datasets_preprocess/preprocess_co3d.py --co3d_dir data/co3d_subset --output_dir data/co3d_subset_processed  --single_sequence_subset
    
    # download the pretrained dust3r checkpoint
    mkdir -p checkpoints/
    wget https://download.europe.naverlabs.com/ComputerVision/DUSt3R/DUSt3R_ViTLarge_BaseDecoder_512_dpt.pth -P checkpoints/
    
    # launch training
    torchrun --nproc_per_node=4 train.py \
        --train_dataset "1000 @ Co3d(split='train', ROOT='data/co3d_subset_processed', aug_crop='auto', aug_monocular=0.005, aug_rot90='diff', mask_bg='rand', resolution=[(512, 384), (512, 336), (512, 288), (512, 256), (512, 160)], n_corres=8192, nneg=0.5, transform=ColorJitter)" \
        --test_dataset "100 @ Co3d(split='test', ROOT='data/co3d_subset_processed', resolution=(512,384), n_corres=1024, seed=777)" \
        --model "AsymmetricMASt3R(pos_embed='RoPE100', patch_embed_cls='ManyAR_PatchEmbed', img_size=(512, 512), head_type='catmlp+dpt', output_mode='pts3d+desc24', depth_mode=('exp', -inf, inf), conf_mode=('exp', 1, inf), enc_embed_dim=1024, enc_depth=24, enc_num_heads=16, dec_embed_dim=768, dec_depth=12, dec_num_heads=12, two_confs=True)" \
        --train_criterion "ConfLoss(Regr3D(L21, norm_mode='?avg_dis'), alpha=0.2) + 0.075*ConfMatchingLoss(MatchingLoss(InfoNCE(mode='proper', temperature=0.05), negatives_padding=0, blocksize=8192), alpha=10.0, confmode='mean')" \
        --test_criterion "Regr3D_ScaleShiftInv(L21, norm_mode='?avg_dis', gt_scale=True, sky_loss_value=0) + -1.*MatchingLoss(APLoss(nq='torch', fp=torch.float16), negatives_padding=12288)" \
        --pretrained "checkpoints/DUSt3R_ViTLarge_BaseDecoder_512_dpt.pth" \
        --lr 0.0001 --min_lr 1e-06 --warmup_epochs 1 --epochs 10 --batch_size 4 --accum_iter 4 \
        --save_freq 1 --keep_freq 5 --eval_freq 1 --disable_cudnn_benchmark \
        --output_dir "checkpoints/mast3r_demo"
  3. Run MASt3R via Docker

    main

    To run MASt3R in a containerized environment with NVIDIA CUDA support, use the provided run.sh script in the docker directory. Ensure Docker and the NVIDIA Docker Toolkit are installed.

    # With CUDA support
    cd docker
    bash run.sh --with-cuda --model_name="MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric"
    
    # Without CUDA support
    bash run.sh --model_name="MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric"
  4. Download Dune Model Checkpoints

    main

    MASt3R provides partial support for the Dune encoder. You can download the associated MASt3R decoder checkpoints for use with Dune.

    mkdir -p checkpoints/
    wget https://download.europe.naverlabs.com/dune/dunemast3r_cvpr25_vitbase.pth -P checkpoints/
  5. Download Retrieval Model Checkpoints

    main

    For the MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric model, you can use a retrieval strategy by downloading both trainingfree.pth and codebook.pkl. These must be placed in the same directory.

    mkdir -p checkpoints/
    wget https://download.europe.naverlabs.com/ComputerVision/MASt3R/MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric_retrieval_trainingfree.pth -P checkpoints/
    wget https://download.europe.naverlabs.com/ComputerVision/MASt3R/MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric_retrieval_codebook.pkl -P checkpoints/
  6. Install MASt3R submodules

    main

    If you have already cloned the mast3r repository, you must initialize and update the git submodules to ensure all dependencies are present:

    git submodule update --init --recursive
  7. Run visual localization experiments with visloc.py

    main

    Use visloc.py to run visual localization experiments on several datasets including Aachen-Day-Night, InLoc, 7-scenes, and Cambridge Landmarks. You can run standard localization or use a --coarse_to_fine approach for improved results.

    # Aachen-Day-Night-v1.1
    python3 visloc.py --model_name MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric --dataset "VislocAachenDayNight('/path/to/prepared/Aachen-Day-Night-v1.1/', subscene='${scene}', pairsfile='fire_top50', topk=20)" --pixel_tol 5 --pnp_mode poselib --reprojection_error_diag_ratio 0.008 --output_dir /path/to/output/Aachen-Day-Night-v1.1/${scene}/loc
    
    # InLoc
    python3 visloc.py --model_name MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric --dataset "VislocInLoc('/path/to/prepared/InLoc/', pairsfile='pairs-query-netvlad40-temporal', topk=20)" --pixel_tol 5 --pnp_mode poselib --reprojection_error_diag_ratio 0.008 --output_dir /path/to/output/InLoc/loc
    
    # 7-scenes
    python3 visloc.py --model_name MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric --dataset "VislocSevenScenes('/path/to/prepared/7-scenes/', subscene='${scene}', pairsfile='APGeM-LM18_top20', topk=1)" --pixel_tol 5 --pnp_mode poselib --reprojection_error_diag_ratio 0.008 --output_dir /path/to/output/7-scenes/${scene}/loc
    
    # Cambridge Landmarks
    python3 visloc.py --model_name MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric --dataset "VislocCambridgeLandmarks('/path/to/prepared/Cambridge_Landmarks/', subscene='${scene}', pairsfile='APGeM-LM18_top50', topk=20)" --pixel_tol 5 --pnp_mode poselib --reprojection_error_diag_ratio 0.008 --output_dir /path/to/output/Cambridge_Landmarks/${scene}/loc
  8. Install MASt3R

    main

    To install MASt3R, clone the repository recursively to include submodules, create a Conda environment with Python 3.11, and install the required PyTorch and dependency packages. You must also install asmk for certain features and can optionally compile CUDA kernels for RoPE to improve runtime performance.

    # 1. Clone the repository
    git clone --recursive https://github.com/naver/mast3r
    cd mast3r
    
    # 2. Create and activate environment
    conda create -n mast3r python=3.11 cmake=3.14.0
    conda activate mast3r 
    conda install pytorch torchvision pytorch-cuda=12.1 -c pytorch -c nvidia
    
    # 3. Install requirements
    pip install -r requirements.txt
    pip install -r dust3r/requirements.txt
    
    # Optional: support for HEIC and visloc.py
    pip install -r dust3r/requirements_optional.txt
    
    # 4. Install ASMK
    pip install cython
    git clone https://github.com/jenicek/asmk
    cd asmk/cython/
    cythonize *.pyx
    cd ..
    pip install .
    cd ..
    
    # Optional: Compile RoPE CUDA kernels for faster runtime
    cd dust3r/croco/models/curope/
    python setup.py build_ext --inplace
    cd ../../../../
  9. Compute matches with DUNE+MASt3R

    main

    To use DUNE+MASt3R for match extraction, use load_dune_mast3r_model to load a specific checkpoint (e.g., checkpoints/dunemast3r_cvpr25_vitbase.pth). When loading images with load_images, ensure you pass size=518, patch_size=model.patch_size, and square_ok=True to match the model requirements. The subsequent inference and matching steps follow the same pattern as standard MASt3R.

    from mast3r.model import load_dune_mast3r_model
    from mast3r.fast_nn import fast_reciprocal_NNs
    
    import mast3r.utils.path_to_dust3r  # noqa
    from dust3r.utils.image import load_images
    from dust3r.inference import inference
    
    import torch
    
    if __name__ == '__main__':
        device = torch.device('cuda:0')
        model = load_dune_mast3r_model('checkpoints/dunemast3r_cvpr25_vitbase.pth', device)
    
        images = load_images(['dust3r/croco/assets/Chateau1.png', 'dust3r/croco/assets/Chateau2.png'],
                            size=518, patch_size=model.patch_size, square_ok=True)
    
        output = inference([tuple(images)], model, device, batch_size=1, verbose=False)
    
        view1, pred1 = output['view1'], output['pred1']
        view2, pred2 = output['view2'], output['pred2']
    
        desc1, desc2 = pred1['desc'].squeeze(0).detach(), pred2['desc'].squeeze(0).detach()
    
        # find 2D-2D matches between the two images
        matches_im0, matches_im1 = fast_reciprocal_NNs(desc1, desc2, subsample_or_initxy1=8,
                                                    device=device, dist='dot', block_size=2**13)
  10. Run the MASt3R Interactive Demo

    main

    Run the updated demo.py to launch a web UI for MASt3R. This demo uses a sparse global alignment method for reconstructing larger scenes.

    Key CLI flags:

    • --model_name: Specify the model (default: MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric).
    • --weights: Path to a local checkpoint file.
    • --retrieval_model: Path to the retrieval checkpoint (*trainingfree.pth) to enable retrieval pairing (requires asmk).
    • --local_network: Makes the demo accessible on your local network.
    • --server_port: Change the port (default starts at 7860).
    • --device: Specify the device (default: cuda).
    python3 demo.py --model_name MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric
  11. Compute matches with MASt3R

    main

    Use the AsymmetricMASt3R class to load a pretrained model and compute matches between a pair of images. The process involves loading the model via from_pretrained, loading images using dust3r.utils.image.load_images, and running inference with dust3r.inference.inference. To find 2D-2D matches from the resulting descriptors, use fast_reciprocal_NNs.

    from mast3r.model import AsymmetricMASt3R
    from mast3r.fast_nn import fast_reciprocal_NNs
    
    import mast3r.utils.path_to_dust3r
    from dust3r.inference import inference
    from dust3r.utils.image import load_images
    
    if __name__ == '__main__':
        device = 'cuda'
        model_name = "naver/MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric"
        model = AsymmetricMASt3R.from_pretrained(model_name).to(device)
        images = load_images(['dust3r/croco/assets/Chateau1.png', 'dust3r/croco/assets/Chateau2.png'], size=512)
        output = inference([tuple(images)], model, device, batch_size=1, verbose=False)
    
        view1, pred1 = output['view1'], output['pred1']
        view2, pred2 = output['view2'], output['pred2']
    
        desc1, desc2 = pred1['desc'].squeeze(0).detach(), pred2['desc'].squeeze(0).detach()
    
        # find 2D-2D matches between the two images
        matches_im0, matches_im1 = fast_reciprocal_NNs(desc1, desc2, subsample_or_initxy1=8,
                                                       device=device, dist='dot', block_size=2**13)
  12. Reference: visloc.py CLI flags

    main

    The visloc.py script accepts the following flags for running visual localization experiments:

    • --model_name: The name of the trained model to use.
    • --dataset: A string defining the dataset type and its parameters (e.g., VislocAachenDayNight, VislocInLoc, VislocSevenScenes, VislocCambridgeLandmarks).
    • --pixel_tol: Pixel tolerance.
    • --pnp_mode: PnP mode (e.g., poselib).
    • --reprojection_error_diag_ratio: Reprojection error diagonal ratio.
    • --output_dir: Directory where results will be saved.
    • --coarse_to_fine: Enables coarse-to-fine localization strategy.
    • --max_batch_size: Maximum batch size for coarse-to-fine.
    • --max_image_size: Maximum image size for coarse-to-fine.
    • --c2f_crop_with_homography: Flag for coarse-to-fine cropping with homography.