Skip to content

Add DewarpNet document dewarping sample (CompiledModel GPU)#208

Open
john-rocky wants to merge 4 commits into
google-ai-edge:mainfrom
john-rocky:dewarpnet-document-dewarping-sample
Open

Add DewarpNet document dewarping sample (CompiledModel GPU)#208
john-rocky wants to merge 4 commits into
google-ai-edge:mainfrom
john-rocky:dewarpnet-document-dewarping-sample

Conversation

@john-rocky

Copy link
Copy Markdown
Contributor

DewarpNet (ICCV 2019, MIT) flattens a photographed, curved/folded document — the core of a document scanner — running fully on the LiteRT CompiledModel GPU at ~24 ms/frame on a Pixel 8a. The first document-processing sample.

Two CNNs (WCNet UNet + BMNet DenseNet) predict a backward-mapping grid on the GPU (371/371 nodes on the delegate, 1 partition; device corr 0.999866, ~24 ms), and a tiny host-side grid_sample flattens the page. Two exact conversion patches: ConvTranspose2d → ZeroStuffConvT2d (Mali rejects TRANSPOSE_CONV) and Hardtanh(0,1)relu(x)-relu(x-1) (Mali rejects RELU_0_TO_1). CPU-exact vs PyTorch (corr 0.9999999999). Input [1,3,256,256] BGR /255, output [1,2,128,128] backward map.

Model: litert-community/DewarpNet-LiteRT.

Sample at compiled_model_api/document_dewarping/dewarpnet_kotlin_gpu (android app + conversion scripts), with a deterministic bundled-image before/after demo.

DewarpNet (cvlab-stonybrook/DewarpNet, ICCV 2019, MIT) flattens a photographed curved/folded document — the core of a document scanner — running fully on the LiteRT CompiledModel GPU at ~24 ms/frame on a Pixel 8a. First document-processing sample.

Two CNNs (WCNet UNet + BMNet DenseNet) predict a backward-mapping grid on the GPU (371/371 nodes, 1 partition; device corr 0.999866) and a tiny host-side grid_sample flattens the page. Two exact conversion patches: ConvTranspose2d -> ZeroStuffConvT2d (Mali rejects TRANSPOSE_CONV) and Hardtanh(0,1) -> relu(x)-relu(x-1) (Mali rejects RELU_0_TO_1). CPU-exact vs PyTorch (corr 0.9999999999).

Sample at compiled_model_api/document_dewarping/dewarpnet_kotlin_gpu (android app + conversion scripts), with a deterministic bundled-image before/after demo.
@john-rocky
john-rocky force-pushed the dewarpnet-document-dewarping-sample branch from cde4eaa to f7ce4a2 Compare July 6, 2026 23:52
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.
Rehost the DewarpNet sample on Jetpack Compose + MVVM to match the canonical
compiled_model_api sample shape.

- MainActivity is now a thin ComponentActivity that observes MainViewModel and
  adds a gallery picker (PickVisualMedia); the old programmatic View layout is
  gone.
- New MainViewModel owns the DocumentDewarper, confines every model call to a
  single-parallelism Dispatchers.Default worker, loads dewarp.tflite from
  filesDir with an inline install_to_device.sh message when it is missing, and
  closes the helper in onCleared().
- New UiState, ImageUtils (asset + EXIF decode), DewarpScreen, Theme and Color,
  plus res/values strings/themes/colors; every UI string is externalized.
- The DocumentDewarper inference helper (model I/O and the host-side
  grid_sample unwarp) is left byte-identical.
- Adopt the Gradle version catalog (AGP 8.9.1 / Kotlin 2.2.21 / compileSdk 36);
  LiteRT stays pinned at 2.1.5.
- Fix dead ormbg references in install_to_device.sh (build_dewarp.py and the
  final app name).
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