Skip to content

Examples

Hello DeepLens

Load and analyze an existing lens design.

from src import GeoLens

lens = GeoLens(filename="lens_zoo/cellphone.json")
lens.analysis(render=True)

Lens Optimization

Fine-tune an existing lens design using gradient descent on surface parameters.

from src import GeoLens

lens = GeoLens(filename="lens_zoo/cellphone.json")

lens.optimize(
    lrs=[1e-3, 1e-4, 1e-2, 1e-4],  # [lr_d, lr_c, lr_k, lr_ai]
    iterations=500,
    test_per_iter=50,
    centroid=False,
    optim_mat=False,
    shape_control=True,
    result_dir="results/optimize",
)

lens.analysis(render=True)

Automated Lens Design (Ab Initio)

Design a lens from scratch — no manual initialization. This is the main AutoLens workflow.

from src import GeoLens
from src.geolens_pkg.utils import create_lens

# Create a flat starting point
lens = create_lens(
    foclen=4.55,
    fov=80.0,
    fnum=2.0,
    bfl=1.5,
    thickness=6.0,
    surf_list=[
        ["Spheric", "Spheric"],
        ["Spheric", "Spheric"],
        ["Aperture"],
        ["Spheric", "Spheric"],
        ["Spheric", "Spheric"],
        ["Spheric", "Spheric"],
    ],
    save_dir="results/autolens",
)

# Stage 1: Curriculum learning (aperture grows from 25% to 100%)
lens.curriculum_design(
    lrs=[5e-3, 5e-4, 5e-2, 5e-4],
    iterations=2000,
    test_per_iter=100,
    optim_mat=True,
    result_dir="results/autolens/curriculum",
)

# Snap materials to real glass catalog
lens.match_materials()
lens.set_fnum(2.0)

# Stage 2: Fine-tuning with pinhole center
lens.optimize(
    lrs=[1e-3, 1e-4, 1e-2, 1e-4],
    iterations=2000,
    test_per_iter=100,
    centroid=False,
    optim_mat=False,
    shape_control=True,
    result_dir="results/autolens/finetune",
)

# Stage 3: Fine-tuning with centroid center
lens.optimize(
    lrs=[1e-3, 1e-4, 1e-2, 1e-4],
    iterations=3000,
    test_per_iter=100,
    centroid=True,
    optim_mat=False,
    shape_control=True,
    result_dir="results/autolens/centroid",
)

lens.write_lens_json("results/autolens/final.json")
lens.analysis(render=True)

Or simply run the provided script:

python 2_autolens_rms.py

Batch Design with Random Configurations

Generate multiple lens designs by sampling random configurations:

python 3_random_autolens.py --category mobile --num_designs 20

This script supports three lens categories with different parameter ranges:

Category FoV F/# Elements Materials
camera_spheric 20 -- 90 deg 2.0 -- 8.0 4 -- 8 Glass
camera_aspheric 20 -- 90 deg 2.0 -- 8.0 4 -- 8 Glass
mobile 60 -- 120 deg 1.8 -- 2.8 5 -- 7 Plastic

Progressive Aspheric Introduction

Start with all-spherical surfaces and progressively convert the best candidates to aspheric:

python 9_autolens_aspheric.py

The pipeline:

  1. Curriculum learning with all-spherical surfaces
  2. Identify the best candidate surface for aspheric conversion
  3. Convert to aspheric and add polynomial terms
  4. Repeat until target aspheric order is reached

L-BFGS Fine-Tuning

Use L-BFGS with parameter reparametrization for fast local convergence on a pre-optimized lens:

from src import GeoLens

lens = GeoLens(filename="results/autolens/final.json")

lens.optimize_lbfgs(
    iterations=200,
    lr=0.5,
    reparam=True,
    result_dir="results/lbfgs",
)

Image Simulation

Render an image through a lens design:

import torchvision
from src import GeoLens

lens = GeoLens(filename="lens_zoo/cellphone.json")

img = torchvision.io.read_image("path/to/image.png").float() / 255.0
img = img.unsqueeze(0)  # (1, 3, H, W)

rendered = lens.render(img, depth=10000.0, method="psf_map")

Lens File I/O

Read and write lens files in multiple formats:

from src import GeoLens

# Read from various formats
lens = GeoLens(filename="lens_zoo/cellphone.json")
lens = GeoLens(filename="lens_zoo/hnu_hybrid/design.zmx")

# Write to JSON
lens.write_lens_json("output.json")

# Write to Zemax format
lens.write_lens_zmx("output.zmx")