Skip to content

Add DehazeFormer-MCT image dehazing sample (CompiledModel GPU)#219

Open
john-rocky wants to merge 3 commits into
google-ai-edge:mainfrom
john-rocky:dehazeformer-image-dehazing-sample
Open

Add DehazeFormer-MCT image dehazing sample (CompiledModel GPU)#219
john-rocky wants to merge 3 commits into
google-ai-edge:mainfrom
john-rocky:dehazeformer-image-dehazing-sample

Conversation

@john-rocky

Copy link
Copy Markdown
Contributor

What

New image_dehazing category: DehazeFormer (TIP 2023, MIT; the author's MCT curve-mapping variant trained on a mixed dataset for real-world haze) running fully on the LiteRT CompiledModel GPU delegate — the first dehazing sample. Removes fog / haze / smoke and restores contrast and color.

  • The 256×256 Swin-windowed-attention basenet predicts 72 per-pixel curve parameters on the GPU; the sample applies the curves to the full-resolution photo host-side (the exact official grid_sample mapping — replica verified corr 1.0), so output resolution is independent of the network.
  • Kotlin sample app: bundled hazy photo → dehazed result (17 MB model pushed to filesDir via install_to_device.sh).
  • conversion/: build_dehaze.py (litert-torch; fetches the author's code + MIT checkpoint from their HF Space) + validate_dehaze.py (static flatbuffer op scan + CompiledModel Python API parity) + README.
  • Model hosted at litert-community/DehazeFormer-MCT-LiteRT.

GPU conversion (all re-authors exact — desktop corr vs PyTorch 1.0000000)

  • reflect pads → slice+concat: litert-torch lowers reflection_pad2d to GATHER_ND (rejected by the delegate) — including padding_mode='reflect' convs with padding=0, which still route through F.pad.
  • Swin window partition/reverse in ≤4D reshapes/permutes; qkv 5D split → channel slices; relative-position bias baked from the meta MLP.
  • RLN global norm / SKFusion global pool → hierarchical means: a single MEAN over C·H·W (1.5M elements) overflows the Mali fp16 accumulator → NaN output; equal-window avg_pool stages + small MEANs are mathematically identical and fp16-safe.
  • SKFusion 5D view+softmax → 4D pairwise softmax; Conv+PixelShuffle → zero-stuff ConvTranspose (per-subpixel conv bias re-added as a constant map).

Verification

  • Pixel 8a, LiteRT 2.1.5: 2042/2042 nodes on the GPU delegate, 1 partition; device corr 0.999998; end-to-end (device curves + host mapping) vs the official pipeline corr 0.999997; ~255 ms/frame (readback-timed).
  • :app:assembleDebug builds; validate_dehaze.py reports opcheck clean, corr 1.0000000.
  • Input [1,3,256,256] NCHW RGB in [-1,1] → output [1,72,256,256] curve parameters.

Bundled test photo: Pexels (free license).

DehazeFormer-MCT (IDKiro/DehazeFormer, TIP 2023, MIT; mixed-dataset curve-mapping variant from the author's HF Space) removes real-world fog/haze/smoke, with the Swin-windowed-attention basenet running fully on the LiteRT CompiledModel GPU (Pixel 8a: 2042/2042 nodes, 1 partition; device corr 0.999998, end-to-end vs the official pipeline corr 0.999997, ~255 ms/frame).

The 256x256 basenet emits 72 per-pixel curve parameters; the sample applies the curves to the full-resolution photo host-side (the exact official grid_sample mapping, replica corr 1.0).

Exact re-authors: reflect pads -> slice+concat (litert-torch lowers reflection_pad2d to banned GATHER_ND, including padding=0 reflect convs), Swin window partition/reverse <=4D + qkv slices + baked relative-position bias, SKFusion 5D->4D pairwise softmax, Conv+PixelShuffle -> zero-stuff ConvTranspose, and RLN global norm / SKFusion global pool -> hierarchical means (a single MEAN over 1.5M elements overflows the Mali fp16 accumulator -> NaN). Desktop corr vs PyTorch 1.0000000.

New image_dehazing category: Kotlin sample app (bundled hazy photo -> dehazed full-res result) + conversion/ scripts (litert-torch build + CompiledModel-API validation).
Wrap every line in the conversion scripts to the 80-column limit of the
Google Python Style Guide and add Args/Returns sections to the
module-level function docstrings. No behavior changes.
Match the canonical compiled_model_api sample shape (image_segmentation /
dis_kotlin_gpu): a thin ComponentActivity host, a MainViewModel that owns the
Dehazer and confines every model call to a single-threaded dispatcher, an
immutable UiState, and a view/ package for the Compose screen and theme.

- MainActivity: programmatic LinearLayout -> Compose, with a PickVisualMedia
  gallery launcher replacing the bundled-asset-only demo.
- MainViewModel: replaces the raw single-thread Executor; surfaces the
  'model not found - run install_to_device.sh' hint as inline UI state.
- res/values/{strings,themes,colors}.xml: no hardcoded UI strings; the
  manifest now uses @string/app_name and @style/Theme.Dehaze.
- gradle/libs.versions.toml: adopt the version catalog used by the sibling
  samples. LiteRT stays pinned at 2.1.5.

Dehazer.kt (the inference helper) is unchanged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant