Instructions to use litert-community/NAFNet-SIDD-width32-LiteRT with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use litert-community/NAFNet-SIDD-width32-LiteRT with LiteRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
NAFNet-SIDD-width32 β LiteRT (on-device image denoising, fully-GPU)
NAFNet (Nonlinear Activation Free Network, ECCV 2022) image
restoration, converted to LiteRT and running fully on the CompiledModel GPU (ML Drift) on Android.
This is the SIDD-width32 variant β real-image denoising. NAFNet is a U-Net of NAFBlocks with no
activation functions (SimpleGate = channel-split multiply), so the whole network is a clean CNN on the GPU.
On-device (Pixel 8a, Tensor G3 β verified)
| nodes on GPU | 2179 / 2179 LITERT_CL (full residency) |
| inference | ~46 ms (256Γ256) |
| size | 62.5 MB (fp16) |
| accuracy | device output == PyTorch (corr 0.999999) β re-authoring is numerically exact |
image[1,3,256,256] (RGB [0,1]) β[GPU: NAFNet U-Net]β denoised[1,3,256,256]
Minimal usage
Android (Kotlin, CompiledModel GPU)
val model = CompiledModel.create(context.assets, "nafnet_sidd_width32_fp16.tflite",
CompiledModel.Options(Accelerator.GPU), null)
val inputs = model.createInputBuffers()
val outputs = model.createOutputBuffers()
inputs[0].writeFloat(chw) // [1,3,256,256] RGB in [0,1], NCHW
model.run(inputs, outputs)
val denoised = outputs[0].readFloat() // [1,3,256,256] in [0,1]
Python (desktop verification)
import numpy as np
from PIL import Image
from ai_edge_litert.interpreter import Interpreter
img = Image.open("noisy.jpg").convert("RGB").resize((256, 256))
x = (np.asarray(img, np.float32) / 255.0).transpose(2, 0, 1)[None] # [1,3,256,256]
it = Interpreter(model_path="nafnet_sidd_width32_fp16.tflite"); it.allocate_tensors()
it.set_tensor(it.get_input_details()[0]["index"], x); it.invoke()
y = it.get_tensor(it.get_output_details()[0]["index"])[0] # [3,256,256], [0,1]
Image.fromarray((y.transpose(1, 2, 0).clip(0, 1) * 255).astype(np.uint8)).save("restored.png")
A complete Android sample (image picker + before/after) is in the official
google-ai-edge/litert-samples repo under
compiled_model_api/image_restoration.
How it converts (litert-torch)
Pure CNN (no activations). Three numerically-exact re-authorings, the headline being SafeLayerNorm:
NAFNet's residual stream grows large (|x|β175 at the bottleneck), so the LayerNorm channel reductions
Ξ£_c x and Ξ£_c (xβΞΌ)Β² (~15M) overflow fp16 (max 65504) on the Mali delegate (which computes in fp16
regardless of the model dtype) β a grid artifact. Doing the reductions in a down-scaled x/S domain (S=128)
and rescaling is exact and fp16-safe. Plus the Simplified Channel Attention AdaptiveAvgPool2d(1) β
mean(3).mean(2), and the upsample Conv2d(1Γ1)+PixelShuffle(2) β depth-to-space ZeroStuffConvT2d.
Result: banned ops NONE, all tensors β€4D, tflite-vs-torch corr 1.0, device-vs-torch corr 1.0.
A complete Android sample (image picker + before/after) is in the official
google-ai-edge/litert-samples repo under
compiled_model_api/image_restoration (push this .tflite in place of the deblur model).
Performance
Measured on a Pixel 8a (Tensor G3, Android 16) with the standard TFLite benchmark_model tool β 10 warm-up runs then 50 timed runs, reported as the tool's mean.
| Runtime | Backend | Graph on GPU | Latency |
|---|---|---|---|
LiteRT CompiledModel (LITERT_CL) |
GPU | 2179 / 2179 | ~46 ms |
TFLite benchmark_model (TfLiteGpuDelegateV2) |
GPU (OpenCL) | 2179 / 2179 | 109.9 ms |
TFLite benchmark_model |
CPU (XNNPACK, 4 threads) | β | XNNPACK declined the graph |
The two GPU rows are different runtimes, not a contradiction. The LITERT_CL figure is the one recorded when this model shipped, taken through LiteRT's own CompiledModel accelerator β the path the Kotlin sample app and the LiteRT API use. The TfLiteGpuDelegateV2 figure is the classic TFLite OpenCL delegate, measured with a tool anyone can download and re-run. They agree on how much of the graph the GPU takes; they disagree on speed, and the classic delegate is the slower of the two here. Read the TfLiteGpuDelegateV2 row as a reproducible floor, not as this model's speed on LiteRT.
XNNPACK declines these fp16 graphs β it reports failed to delegate DEPTHWISE_CONV_2D and then fails to allocate tensors β so there is no usable CPU number. Disabling XNNPACK falls back to reference kernels, which measured about 20Γ slower than the GPU on models of this size and would not represent CPU inference anyone would ship.
License
MIT. Upstream: megvii-research/NAFNet; weights NAFNet-SIDD-width32.
- Downloads last month
- 60
