File size: 1,936 Bytes
b4172d4
 
2c110d2
 
 
 
 
 
 
 
b4172d4
2c110d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
---
license: apache-2.0
library_name: onnxruntime
pipeline_tag: image-to-image
tags:
  - inpainting
  - object-removal
  - lama
  - onnx
  - webgpu
---

# LaMa inpainting, WebGPU-ready ONNX

LaMa ("Resolution-robust Large Mask Inpainting with Fourier Convolutions", Suvorov et al., WACV 2022)
as a single fp32 ONNX graph that runs on **onnxruntime-web's WebGPU execution provider**. It is used by
the on-device AI eraser of the ToolsMonk Photo Editor and Background Remover.

## Why this build exists

The published LaMa ONNX exports run in the browser on WASM only. On WebGPU (onnxruntime-web 1.30) every
run fails inside the Fourier unit ("Failed to run JSEP kernel"). This file is the same model with three
**numerically equivalent** graph rewrites:

1. constant folding at a fixed batch of 1, so the DFT matrices the export builds at run time become constants;
2. every `Einsum` (`ij,jk->ik`, 216 nodes) rewritten as `MatMul`;
3. every negative-step `Slice` (72 nodes) rewritten as `Gather` with constant reversed indices.

Maximum output difference against the source on a 512x512 test input: 0.00026 on the 0..255 scale.
Measured in Chrome: ~1.7-2.2 s per 512x512 inpaint on WebGPU, against ~30 s on single-threaded WASM.

## Inputs and outputs

- `image`: float32 `[1, 3, 512, 512]`, RGB in 0..1
- `mask`: float32 `[1, 1, 512, 512]`, 1 = area to fill, 0 = keep
- `output`: float32 `[1, 3, 512, 512]`, RGB in 0..255

## Provenance and licence

- Model: [advimman/lama](https://github.com/advimman/lama), Apache-2.0.
- Source ONNX: [Carve/LaMa-ONNX](https://huggingface.co/Carve/LaMa-ONNX) `lama_fp32.onnx`, Apache-2.0
  (sha256 `1faef5301d78db7dda502fe59966957ec4b79dd64e16f03ed96913c7a4eb68d6`).
- Conversion script: `build-lama-webgpu.py` in this repository (requires `onnx`, `onnxruntime`, `numpy`).

This derivative is distributed under the same Apache-2.0 licence. The rewrites change the graph's
structure, not its weights.