Instructions to use litert-community/densenet121 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- LiteRT
How to use litert-community/densenet121 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
TorchVision DenseNet121 β LiteRT
LiteRT/TFLite versions of TorchVision's densenet121, using
DenseNet121_Weights.IMAGENET1K_V1.
The model classifies RGB images into 1,000 ImageNet classes. The input has a
fixed NCHW shape of [1, 3, 224, 224]; preprocessing is external to the graph.
The .tflite files are portable source models. NPU deployment requires
compilation for the target SoC and a matching vendor dispatch runtime.
Device-specific compiled NPU programs are not included.
Files and choosing a variant
| File | Description | Input / output |
|---|---|---|
densenet121.tflite |
FP32 weights and activations; floating-point reference and example default. | FP32 / FP32 |
densenet121_dynamic_wi8_afp32.tflite |
Dynamic-range quantization with INT8 stored weights and FP32 graph activations. Hybrid kernels may quantize activations internally at runtime. | FP32 / FP32 |
densenet121_int8_channelwise.tflite |
Static A8W8: per-channel INT8 weights, per-tensor INT8 activations and INT32 biases. Fully integer execution. | INT8 / INT8 |
run_image.py |
CPU example with TorchVision preprocessing and class labels. | β |
requirements.txt |
Package versions used to check the CPU example. | β |
validation/ |
Calibration/evaluation image identifiers and machine-readable results. Images are not included. | β |
All three model files were evaluated on CPU with XNNPACK enabled. The FP32 and static INT8 files were also evaluated with full NPU delegation on the Qualcomm and MediaTek devices below. The dynamic-range model's GPU and NPU behavior was not evaluated in this study.
Use the FP32 model as the reference when checking a new backend. Static INT8 is the integer deployment option; confirm accuracy on representative data and verify delegation on the target device. Dynamic-range quantization keeps the FP32 interface and requires no static activation calibration.
Static INT8 quantization
The static model uses post-training quantization with LiteRT Quantizer's
static_wi8_ai8() recipe, without quantization-aware training or fine-tuning:
- Calibrate the FP32 model on 500 ImageNet-1k validation images, batch size one, using the source TorchVision/PIL preprocessing. The calibration list is disjoint from the evaluation list.
- Store convolution and fully connected weights as symmetric, per-output-channel INT8 values with zero point zero. Their biases use INT32 with scales derived from the input and weight scales.
- Use asymmetric, per-tensor INT8 activations. Internal ranges use an exponential moving average with smoothing factor 0.95. Public input and output ranges use the full observed minimum and maximum across calibration images. This mixed range-selection policy is an explicit calibration choice.
- Retain separately calibrated activation ranges. INT8-to-INT8 requantization aligns scales and zero points where required for concatenation. These nodes do not introduce floating-point computation.
Why the model uses separate activation scales
Sharing activation scales across DenseNet blocks reduces the portable graph's operator count by 49.8% (628 β 315), principally by reducing explicit requantization operators from 317 to 4. We retain separate scales because the large reduction in operator count produced only small, backend-dependent latency gains while imposing a common quantization range on features with different distributions.
In the latest paired NPU measurements, the shared-scale control reduced latency by approximately 3.9% on Samsung/Qualcomm and 2.0% on TECNO/MediaTek. An earlier session on a tested MediaTek NPU showed no meaningful speedup; the effect was small and varied between sessions. Earlier CPU measurements also showed modest latency gains.
The shared-scale control used the same calibration images, INT8 weight quantization and public input/output encodings as the published static model. Its 5,000-image top-1 results were:
| Backend | Separate scales, published (%) | Shared-scale control (%) |
|---|---|---|
| CPU / XNNPACK | 75.34 | 75.14 |
| Samsung / Qualcomm NPU | 75.42 | 75.24 |
| TECNO / MediaTek NPU | 74.98 | 75.18 |
These small accuracy differences do not establish a universal winner. The default preserves independently calibrated activation ranges; sharing scales remains a target-specific optimization when a larger measured benefit warrants the tradeoff. The shared-scale control is not a published model file. Operator counts above describe the portable graph before vendor compilation, not the number of NPU kernels.
Accuracy evaluation
Results were collected on 5,000 ImageNet-1k validation images using the exact same preprocessed inputs for each backend. The evaluation list identifies the images and their zero-based class labels; filenames use the original ImageNet validation naming convention. This is a fixed subset, not a full 50,000-image evaluation or a class-balanced sample.
The source PyTorch FP32 model scored 75.70% top-1 and
92.62% top-5, with autocast and TF32 disabled. Its top-1
predictions matched the TFLite FP32 model on all 5,000 images.
Preprocessing/reference packages were TorchVision 0.29.1, PyTorch 2.14.1
and Pillow 13.0.0.
CPU: XNNPACK enabled, eight threads
| Model file | Top-1 (%) | Top-5 (%) |
|---|---|---|
densenet121.tflite |
75.70 | 92.62 |
densenet121_dynamic_wi8_afp32.tflite |
75.72 | 92.52 |
densenet121_int8_channelwise.tflite |
75.34 | 92.26 |
The CPU evaluation used LiteRT's native vision evaluator, with single-rounding builtin fallback kernels. It is separate from the phone NPU measurements.
NPU: AOT compiled, fully delegated
| Model file | Samsung top-1 (%) | Samsung top-5 (%) | TECNO top-1 (%) | TECNO top-5 (%) |
|---|---|---|---|---|
densenet121.tflite |
75.74 | 92.60 | 75.70 | 92.64 |
densenet121_int8_channelwise.tflite |
75.42 | 92.34 | 74.98 | 92.20 |
Both model variants compiled to one NPU dispatch per device, with no residual LiteRT CPU operators. All 5,000 outputs were finite. FP32 identifies the source model; NPU compilation permits FP16 execution and does not imply FP32 arithmetic throughout the compiled program.
This subset was reused while comparing calibration and scale-sharing policies. It is disjoint from calibration but is not an independent final test set. Small differences should not be interpreted as general improvements over the source checkpoint. Accuracy and numerical behavior can differ across backends.
NPU performance
Warm batch-one inference latency in milliseconds, measured on 2026-11-14:
| Model file | Samsung SM-S937U1 / SM8750 | TECNO LJ9 / MT6897 |
|---|---|---|
densenet121.tflite |
0.747 | 11.178 |
densenet121_int8_channelwise.tflite |
0.337 | 7.027 |
Each value is the median of six process means, with 200 warmup and
1,000 measured invocations per process. Model order alternated through all
six permutations of the FP32, separate-scale INT8 and shared-scale INT8 models.
Inputs were fixed real-image tensors with persistent buffers. Measurements use
LiteRT run_model through adb shell and time synchronous CompiledModel.Run;
they exclude initialization, preprocessing, file I/O and explicit host input/output
buffer transfers. They are not end-to-end Android application timings.
- Samsung SM-S937U1: SM8750, Android 17, Qualcomm QAIRT/QNN 2.51.1, burst performance mode. AOT compilation used the public LiteRT 2.3.0 Qualcomm compiler plugin.
- TECNO LJ9: MT6897, Android 15, MediaTek Neuron 8.2.10 device runtime and 8.2.30 compiled bytecode, sustained-speed mode.
- Device execution and MediaTek compilation used locally built LiteRT tools.
CPU host affinity was
0xf0on both phones. Binary hashes, per-trial timings and source-model hashes are recorded in validation/results.json.
These measurements describe the listed device/software combinations. Power settings were not normalized between phones, energy consumption was not measured, and the figures are not a controlled comparison of NPU hardware. Results depend on the model, compiler, runtime, OS, thermal state and power settings. These are our measurements, not vendor-endorsed benchmark results.
Input and output
Preprocessing matches the TorchVision weights: convert to RGB, resize the
shorter edge to 256 using PIL bilinear interpolation with antialiasing, center
crop to 224 Γ 224, scale pixels to [0, 1], and normalize with mean
[0.485, 0.456, 0.406] and standard deviation [0.229, 0.224, 0.225].
Use NCHW input [1, 3, 224, 224]. Output [1, 1000] contains logits in the
TorchVision ImageNet class order; softmax is not included.
For the static INT8 file, quantize the normalized input and dequantize output logits using the tensor metadata:
| Tensor | Scale | Zero point |
|---|---|---|
| Input | 0.01865844801068306 |
-14 |
| Output | 0.1969466656446457 |
-77 |
The relationship is real_value = scale * (int8_value - zero_point).
The included example reads these parameters from the model.
Run a local image on CPU
Download run_image.py and requirements.txt, then run:
uv venv --python 3.13
source .venv/bin/activate
uv pip install -r requirements.txt
python run_image.py image.jpg
python run_image.py image.jpg --model densenet121_dynamic_wi8_afp32.tflite
python run_image.py image.jpg --model densenet121_int8_channelwise.tflite
The example downloads the selected model if it is not present locally, applies
the source preprocessing, and prints the top five classes and logits. It was
checked on Linux with XNNPACK enabled for all three files. Use --num-threads
to set the CPU thread count; the default is four.
For NPU execution, compile the portable model with the matching LiteRT vendor plugin, load the resulting model with the device dispatch runtime, select NPU acceleration, and inspect delegation. The CPU example does not invoke the NPU.
Source and attribution
- TorchVision DenseNet121 model and weights documentation
- Source checkpoint: densenet121-a639ec97.pth
- DenseNet paper: Densely Connected Convolutional Networks
- TorchVision BSD 3-Clause license
The source weights are TorchVision's ImageNet-1k V1 checkpoint, ported from LuaTorch. Refer to the source documentation for training details and the original full-validation metrics; those metrics use a different evaluation set from the subset reported here.
- Downloads last month
- 232