big-lama (LiteRT / TFLite, GPU-friendly graph, fp16 weights)

LaMa "big-lama" (Fourier-convolution inpainting network trained on Places2) as a single .tflite file for on-device inpainting with LiteRT. The graph was rewritten so the phone GPU runs all of it: the weights are the original ones, only the way the 36 Fourier units are expressed changed.

File Size SHA-256
big_lama_gpu_fp16.tflite 104,462,288 bytes 0c53e57d59e983ac496e2e88dcecb6eaec089ef9399906c0e5e4a7f7d38f0c77
lama_gpu_rewrite.py the script that rewrites the graph (see Provenance)

Weights are stored as fp16; computation is fp32 on the CPU, and the GPU's own arithmetic (half precision by default, full precision on request) on the GPU.

Input and output

Fixed 512 x 512. Two inputs, in this order:

  1. image: [1, 3, 512, 512], NCHW, float32, RGB, values 0 to 1.
  2. mask: [1, 1, 512, 512], NCHW, float32, 1 where the picture must be filled, 0 where it is kept.

Output: [1, 3, 512, 512], NCHW, float32, values 0 to 255. Use the output only inside the mask and keep the original pixels outside it (tile large pictures with overlap; each call sees 512 x 512 pixels).

It invents a plausible fill from the surrounding picture. It does not recover what was really there, and on large holes it gives soft, plausible texture.

Speed (Pixel 10 Pro, LiteRT 2.x, 512 x 512 tile)

Run Per tile
GPU, default (half-precision) arithmetic 350 to 373 ms
GPU, CompiledModel.GpuOptions.Precision.FP32 927 to 967 ms
Phone CPU (XNNPACK) 7.0 to 7.6 s

All 1,775 graph nodes run on the GPU delegate (GPU compile 2.6 to 3.8 s). The standard ONNX export of the same network ran 51 of 3,398 nodes on the GPU and took about 9.5 s per tile. On a Mac CPU (4 threads) this file takes 0.59 s per tile.

Accuracy

  • LiteRT fp32 on the CPU against the ONNX it was built from: at most 0.0024 apart on the 0 to 255 scale.
  • On the phone GPU in full precision, the output matched the CPU (at most 1 level of 255) on the tiles tried.
  • On the phone GPU in half precision, over 10 test tiles, the mean difference from the CPU inside the filled region was 0.2 to 2.5 levels of 255, the worst single pixel 8 to 83, and the picture looks the same.

Provenance

  1. Upstream weights: advimman/lama big-lama, trained on Places2 (Apache-2.0).
  2. ONNX export: Carve/LaMa-ONNX lama_fp32.onnx (Apache-2.0), SHA-256 1faef5301d78db7dda502fe59966957ec4b79dd64e16f03ed96913c7a4eb68d6, made fixed-shape (512 x 512) with onnxslim (SHA-256 46fe88e6ffc9a28a554f1c57a0cd753173821066cd24b745018d2c73363d7932).
  3. lama_gpu_rewrite.py (included here) replaces the 36 Fourier units: the real DFT along the width as two constant matmuls, one constant matmul for the complex DFT along the height on the real and imaginary parts joined on the last axis, the same 1x1 convolution and ReLU with its channels re-ordered, and the inverse the same way. Every tensor has rank 4 or less and every matmul has a constant right-hand side. Against the original ONNX the output differs by at most 0.001 (0 to 255 scale) in ONNX Runtime.
  4. Converted with onnx2torch and litert-torch (with small patches for constant-axis Squeeze/Unsqueeze, Slice, and ConvTranspose), then the weights were stored as fp16 with ai-edge-quantizer.

Licence and credit

Apache-2.0, as upstream. Credit to the LaMa authors (Roman Suvorov et al., Samsung Research, "Resolution-robust Large Mask Inpainting with Fourier Convolutions", WACV 2022) and to Carve for the ONNX export. The weights were trained on Places2; check that dataset's terms for your use.

Downloads last month
-
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Paper for AtlasPilotPuppy1/big-lama-gpu-litert