Giter Club home page Giter Club logo

colmap_cameras_pytorch's Introduction

๐ŸŒ Colmap Cameras in PyTorch

This repository contains PyTorch implementations of the camera models used in the COLMAP structure-from-motion pipeline.

The camera models support automatic differentiation for project and backproject functions. Which for some reason are called map and unmap in this repo.

This code was mainly developed for my own research purposes.

Installation

Just git clone this repository to your project folder.

git clone https://github.com/DaniilSinitsyn/colmap_cameras_pytorch.git

Usage

Load camera from cameras.txt

impot torch
from colmap_cameras_pytorch.colmap_cameras import model_selector

# Model defined as a string in colmap cameras.txt
camera_txt = "SIMPLE_RADIAL 100 100 100 50 50 0.3"
camera_model = camera_txt.split()[0]
camera_params = torch.tensor([float(x) for x in camera_txt.split()[1:]])

# Create model based on the colmap string
model = model_selector(camera_model, camera_params)

# project 3d points onto image
pts3d = torch.rand(10, 3)
points_2d, valid = model.map(points3d)
# unproject 2d points to the ray
points_3d = model.unmap(points_2d)

Optimizing camera parameters

As everything is differentiable, you can optimize camera parameters using PyTorch's optimizers.

model.require_grad = True
optimizer = torch.optim.Adam([model._data], lr=0.01)

for _ in range(iterations):
    optimizer.zero_grad()
    ...
    loss = ...
    loss.backward()
    optimizer.step()

By default camera's center is fixed. If you want to optimize it too:

model.OPTIMIZATION_FIX_CENTER = False

There are in total 4 flags that can be set for each camera:

  • OPTIMIZATION_FIX_FOCALS: Fix focal lengthes (default: False)
  • OPTIMIZATION_FIX_CENTER: Fix principal point (default: True)
  • OPTIMIZATION_FIX_EXTRA: Fix extra parameters (default: False)
  • ROOT_FINDING_MAX_ITERATIONS: Number of iterations for root finding (default: 50)

Camera models

All camera models are supported:

Colmap's name PyTorch class
SIMPLE_PINHOLE SimplePinhole
PINHOLE Pinhole
SIMPLE_RADIAL SimpleRadial
RADIAL Radial
OPENCV OpenCV
OPENCV_FISHEYE OpenCVFisheye
FULL_OPENCV FullOpenCV
SIMPLE_RADIAL_FISHEYE SimpleRadialFisheye
RADIAL_FISHEYE RadialFisheye
FOV Fov
THIN_PRISM_FISHEYE ThinPrismFisheye

To use a specific camera model you can import it directly from the colmap_cameras.models module.

import torch
from colmap_cameras_pytorch.colmap_cameras.models import Pinhole

image_shape = torch.tensor([[100, 100]]).float()
params = torch.tensor([100, 100, 50, 50]).float()
model = Pinhole(params, image_shape)

Usefu stuff

Apps

apps.refit_model is a simple script that uses Gauss-Newton optimization to fit one camera model to another.

python3 -m apps.refit_model --input_camera "SIMPLE_RADIAL 100 100 100 50 50 0.3"  --output_camera "RADIAL_FISHEYE" --iterations 20

Root solvers

Some camera models require solving polynomial roots. For high-order polynomials, the only way to do this is to use a numerical solver.

I don't like the fact that automatic differentiation goes through Newton's method or the QR algorithm.

This repo contains an extention of torch.autograd.Function for Newton's method and Companion matrix root solver.

Tests

To run tests:

python3 -m tests.run_tests -v

TODO

  • Add remap app, that generates remaps alongside with a class to run them.
  • Estimate image area where camera is valid for each model. (Basically to check whether distortion is monotonic)
  • Visualisation util for the previous point.

colmap_cameras_pytorch's People

Contributors

daniilsinitsyn avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.