Show HN:Torch Lens Maker – PyTorch 中的可微几何光学
Show HN: Torch Lens Maker – Differentiable Geometric Optics in PyTorch

原始链接: https://victorpoughon.github.io/torchlensmaker/

Torch Lens Maker是一个实验性的开源Python库,它利用PyTorch进行可微几何光学计算。其目标是使用代码和数值优化来设计复杂的镜头和反射镜等光学系统。 其核心思想是将光学元件视为神经网络中的层,光线作为数据。每一层(透镜、反射镜)根据其参数(形状、材料)以及斯涅耳定律改变光线。前向模型模拟光线的传播,而优化则调整透镜形状以达到预期的聚焦效果。 这种方法利用了PyTorch的自动微分和优化算法,并受益于GPU支持。用户可以像堆叠神经网络层(Conv2d、ReLU)一样堆叠光学元件,并使用标准的PyTorch工具对其进行优化。这使得用户能够利用现代机器学习的强大功能:可组合性、GPU训练和分布式训练。 该项目强调了基于代码设计机械3D系统(特别是光学系统)的强大功能。目前该项目尚处于早期开发阶段,API仍在不断发展中。

Hacker News 上的一篇文章介绍了开发者 fouronnes3 创建的开源 Python 库 “Torch Lens Maker”。这是一个基于 PyTorch 构建的可微几何光学库,由开发者在 Recurse Center 驻留期间开发完成。该项目利用 PyTorch 的自动微分功能进行光学系统设计。其核心思想是将光学元件(透镜、反射镜)视为神经网络中的层,光线作为数据。光线与表面的碰撞检测代替了 Conv2D 层,折射定律代替了 ReLU。这使得可以使用神经网络训练技术来设计透镜:前向传播模拟光的行为,优化过程则寻找最佳的透镜形状以聚焦光线。该库旨在提供类似于 OpenSCAD 的代码型 API,用于最先进的光学系统优化。用户可以通过 `pip install torchlensmaker` 进行安装,并在文档中探索示例,包括彩虹模拟。

原文

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.

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 treat each optical element as 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 NetworkOptical system
DataImages, Text, AudioLight rays
LayersConv2d, Linear, ReLURefraction, Reflection, Gap
Loss FunctionPrediction error to labeled examplesFocusing 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.

Experimental project!

This project is in its very early stages, I've got a very long roadmap planned and I'm looking for funding to be able to keep working on it full time! If you can, please consider donating, sponsoring or even hiring me! 😊💚

Also, the API will change without warning. A stable release is still very far in the future.

联系我们 contact @ memedata.com