Skip to content

Add DM-Count crowd counting sample (CompiledModel GPU)#218

Open
john-rocky wants to merge 4 commits into
google-ai-edge:mainfrom
john-rocky:dmcount-crowd-counting-sample
Open

Add DM-Count crowd counting sample (CompiledModel GPU)#218
john-rocky wants to merge 4 commits into
google-ai-edge:mainfrom
john-rocky:dmcount-crowd-counting-sample

Conversation

@john-rocky

Copy link
Copy Markdown
Contributor

What

New crowd_counting category: DM-Count (NeurIPS 2020, MIT) running fully on the LiteRT CompiledModel GPU delegate — the first crowd-counting sample. The model regresses a person density map whose sum is the crowd size, so it counts hundreds of people where detector-based counting saturates.

  • Kotlin sample app: bundled crowd photo → density heatmap overlay + estimated person count (86 MB model pushed to filesDir via install_to_device.sh).
  • conversion/: build_dmcount.py (litert-torch) + validate_dmcount.py (static flatbuffer op scan + CompiledModel Python API parity check) + README.
  • Model hosted at litert-community/DM-Count-Crowd-LiteRT.

GPU conversion

DM-Count is a pure CNN (VGG19 + conv regression head). One exact rewrite makes it fully GPU-compatible: the mid-graph F.upsample_bilinear (align_corners=True RESIZE_BILINEAR, banned on the delegate) is a linear operator, re-authored as two constant-matrix multiplies with the constant on the RHS (lowers to FULLY_CONNECTED; the delegate rejects BATCH_MATMUL with a constant LHS). Desktop corr vs PyTorch 1.000000 with an identical count.

Verification

  • Pixel 8a, LiteRT 2.1.5: 30/30 nodes on the GPU delegate, 1 partition; corr 0.9998–1.0 vs PyTorch and count within 0.4% on four real crowd images; ~79 ms/frame (readback-timed).
  • :app:assembleDebug builds; validate_dmcount.py reports opcheck clean, corr 1.000000, identical count.
  • Input [1,3,512,512] NCHW RGB ImageNet-norm → output [1,1,64,64] density map (UCF-QNRF weights).

Bundled test photo: Pexels (free license).

DM-Count (cvlab-stonybrook/DM-Count, NeurIPS 2020, MIT) regresses a person density map whose sum is the crowd size, running fully on the LiteRT CompiledModel GPU (30/30 nodes, 1 partition on a Pixel 8a; corr 0.9998-1.0 and count within 0.4% of PyTorch on real crowd images, ~79 ms/frame).

Pure CNN (VGG19 + conv regression head) with one exact rewrite: the mid-graph F.upsample_bilinear (align_corners=True RESIZE_BILINEAR, banned on the delegate) is a linear operator, re-authored as two constant-matrix multiplies with the constant on the RHS (FULLY_CONNECTED; the delegate rejects BATCH_MATMUL with a constant LHS). Desktop corr vs PyTorch 1.000000 with an identical count.

New crowd_counting category: Kotlin sample app (bundled crowd photo -> density heatmap + count) + 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
CrowdCounter 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; loads dmcount.tflite
  from filesDir and surfaces the 'model not found - run install_to_device.sh'
  hint as inline UI state. The renderOverlay density-map colorization math moves
  over verbatim; the person count is added to UiState and shown in the status
  line via a strings.xml format string.
- res/values/{strings,themes,colors}.xml: no hardcoded UI strings; the manifest
  now uses @string/app_name and @style/Theme.CrowdCount.
- gradle/libs.versions.toml: adopt the version catalog used by the sibling
  samples. LiteRT stays pinned at 2.1.5.

CrowdCounter.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