Skip to main content

Differentiable geometric optics in PyTorch. Design optical systems with modern GPU optimization.

Project description

GitHub Repo stars Python Version from PEP 621 TOML GitHub License PyPI - Version Static Badge

Welcome to Torch Lens Maker, an open-source Python library for differentiable geometric optics based on PyTorch. Currently a very experimental project, the goal is to be able to design complex real-world optical systems (lenses, mirrors, etc.) using modern computer code and state-of-the art numerical optimization.

import torchlensmaker as tlm

optics = tlm.Sequential(
    tlm.ObjectAtInfinity(beam_diameter=10, angular_size=20, wavelength=(400, 800)),
    tlm.Gap(15),
    tlm.RefractiveSurface(tlm.Sphere(diameter=25, R=-45.759), material="BK7"),
    tlm.Gap(3.419),
    tlm.RefractiveSurface(tlm.Sphere(diameter=25, R=-24.887), material="air"),
    tlm.Gap(97.5088),
    tlm.ImagePlane(50),
)

tlm.show2d(optics)

alt text

The core of the project is differentiable geometric optics: 3D collision detection and the laws of optics implemented in PyTorch. PyTorch provides world-class automatic differentiation, and access to state-of-the-art numerical optimization algorithms with GPU support.

The key idea is that there is a strong analogy to be made between layers of a neural network, and optical elements in a so-called sequential optical system. If we have a compound optical system made of a series of lenses, mirrors, etc., we can pretend that each optical element is the layer of a neural network. The data flowing through this network are not images, sounds, or text, but rays of light! Each layer affects light rays depending on its internal parameters (surface shape, refractive material...) and following the very much non-linear Snell's law. Inference, or the forward model, is the optical simulation where given some input light, we compute the system's output light. Training, or optimization, is finding the best shapes for lenses to focus light where we want it.

Neural Network Optical system
Data Images, Text, Audio Light rays
Layers Conv2d, Linear, ReLU Refraction, Reflection, Gap
Loss Function Prediction error to labeled examples Focusing error in the image plane

The magic is that we can pretty much use torch.nn and nn.Module directly, stacking lenses and mirrors as if they were Conv2d and ReLU. Then, pass the whole thing through a standard PyTorch optimize() to find the optimal values for parametric surfaces, and designing lenses is surprisingly like training a neural network! Once this is implemented, you get 'for free' the massive power of modern open-source machine learning tooling: automatic differentiation, optimization algorithms, composability, GPU training, distributed training, and more.

On top of that, after having tried software like build123 and OpenSCAD, I strongly believe that writing code is a very powerful way to design mechanical 3D systems and this project is an exploration of that, but for optical systems.

[!WARNING] This project is in its very early stages, the API will change without warning. A stable release is still very far in the future.

Funding

This project is funded through NGI0 Commons Fund, a fund established by NLnet with financial support from the European Commission's Next Generation Internet program. Learn more at the NLnet project page.

NLnet foundation logo NGI Zero Logo

Also please consider sponsoring me on GitHub or Buying me a Coffee to support the future of open-source optical modeling. Thank you! 💚

Features

Design principles and features (SOME OF THESE ARE WORK IN PROGRESS):

  • Geometric optics: Light rays are straight lines in absolute space. If they hit a surface, they can reflect, refract or stop.
  • No approximations: Perfect geometric accuracy up to floating point precision (currently f32 or f64).
  • Sequential mode: The order in which rays interact with optical elements must be known. This constraint is heavily mitigated by the fact that the entire optical model is a pure Python program, and the PyTorch compute graph is dynamic, so parameterization is very powerful.
  • Beautiful code: Torch Lens Maker is code based optical CAD software. The design of a software library should aim to make code easy to read, not easy to write. So great care is being taken to make the API as beautiful as possible.
  • Dimension generic code: Modeling is generic over the number of dimensions (2 or 3). It is only when sampling the system that dimension is fixed. This bridges the gap between 2D ray diagrams and full 3D raytracing with the same code. (Non axially symmetric systems can still be modeled, but then 2D raytracing isn't available).
  • Flexible surface definition framework. Any surface definition can be easily added in explicit or implicit form: 2D or 3D, axially symmetric or not.
  • Export 3D models for manufacturing Using build123d, lens models can be exported to 3D manufacturing formats (STEP, STL, etc.)
  • Forward and inverse kinematics: Optical elements positions in world space are defined on a kinematic tree. This enables joint optimization of surface shapes, positions and rotations, effectively acting as an inverse kinematics solver.
  • Web viewer: tlmviewer is a 3D viewer component for Torch Lens Maker. It is a TypeScript project based on ThreeJS and is developed side-by-side with the python package.

For more discussion of these features, see the documentation.

Installation

pip install torchlensmaker

For more installation info, including how to setup tlmviewer, see the documentation.

About

The origin of this project is double:

I wanted to learn Optics

I have been teaching myself optics, and I've been sometimes frustrated by approximations and general lack of rigor in the way the field is being taught. As someone with a strong software background, but a total noob in optics, approximations like sin(x) = x seem to make sense if you're Isaac Newton designing a telescope with pen and paper in the 17th century, but today I think non linear optimization has gotten so remarkably powerful that we should start there. Teaching optics with ideal equations like 1/u + 1/v = 1/f or the lens maker equation that are never true in real life is in my opinion misleading. At least to me it was super confusing until I let go of it and embraced the non linearity of Snell's law! In my opinion, those equations should be presented as the over-simplified approximate but historically useful model that they are, not as Chapter 1 of how lenses work. It's not! Spherical aberration is not part of physics, it's part of engineering. It's a consequence of solving a non linear system too approximately!

Also, I strongly believe that you only truly understand something once you've programmed a computer to do it. I am fascinated by optics, and Torch Lens Maker is a learning project for me. Every time I add a new feature it deepens my understanding of the physics of it. For example, what is an image? I know what an image is for a computer, it's a 2D grid of pixel data... right? Yeah sure, but in optics it's totally different! I have not yet found an optics textbook that satisfyingly defines what an image is. The best I got was in a Khan Academy video where it was said "an image is where rays of light converge". Still, I think it's more subtle than that (sometimes they don't converge, and still an image is formed)!

Recurse Center

In Winter 2024, I attended the Recurse Center, a technical retreat for programmers. At the Recurse Center, you become a better programmer by working at the edge of your abilities, learning generously and working on your own projects. Torch Lens Maker was my main project during my batch, and I was able to work on it full time for 12 weeks, making great progress and having great fun! Now that my batch has ended, I'm looking for a way to keep working on it because I believe it has great potential. I don't want all lenses in the future to be designed with 5 figures closed-source commercial software, we can do better! Look at how machine learning enjoys state of the art open-source tooling. Let's do the same for optics!


For more info, check out Torch Lens Maker documentation.

Torch Lens Maker is released under the GPL-3.0 license.

Copyright © 2024-present Victor Poughon.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

torchlensmaker-0.0.13.tar.gz (593.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

torchlensmaker-0.0.13-py3-none-any.whl (200.0 kB view details)

Uploaded Python 3

File details

Details for the file torchlensmaker-0.0.13.tar.gz.

File metadata

  • Download URL: torchlensmaker-0.0.13.tar.gz
  • Upload date:
  • Size: 593.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for torchlensmaker-0.0.13.tar.gz
Algorithm Hash digest
SHA256 c5b54b5fdbe06965bad0f97a4e05a3fa91c0324fdf5f68dadc9025829efe7d6f
MD5 63cc2e3d4e0db5c04e3fc0f5a33f64f3
BLAKE2b-256 88d48516ce7ba61da05b7cb9ec77644eebe4db7dc03f084263f44f2f8341f9f2

See more details on using hashes here.

File details

Details for the file torchlensmaker-0.0.13-py3-none-any.whl.

File metadata

  • Download URL: torchlensmaker-0.0.13-py3-none-any.whl
  • Upload date:
  • Size: 200.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.9 {"installer":{"name":"uv","version":"0.10.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for torchlensmaker-0.0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 f93e027ce5c8e0d414ec43505224b9c0253f60a24aef084e8d160dd3cf8e7191
MD5 a608e51d1f145c1fa88888c9249247e1
BLAKE2b-256 3df8550d4e02b270972efa4f0eaeb6203bb65710acba1ee22390396e3d16564c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page