Utilities for inspecting the value distributions of image classification models built with TIMM. The scripts generate matplotlib histograms for both parameters and activations so you can:
- visualize how weights are distributed layer by layer,
- compare floating-point and post-training-quantized (FX) models, and
- spot saturation or clipping issues introduced by quantization.
| Path | Purpose |
|---|---|
weight_histogram.py |
Standalone script that plots parameter and activation histograms for a single TIMM model. |
converter_and_weight_histogram.py |
CLI that quantizes a FP32 TIMM model with FX graph mode, optionally swaps HardSwish to ReLU, and compares float vs quantized histograms. |
histograms/ |
Generated PNGs for the single-model run (params/ and activations/). |
histograms_params_compare/, histograms_activations_compare/ |
PNGs produced when running the comparison script. |
- Python 3.9+
- PyTorch with CPU quantization support (
torch,torchvision,torchaudioinstall picks the right build) timmmatplotlib
Install dependencies into a virtual environment:
python -m venv .venv
source .venv/bin/activate
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu # or the CUDA wheel you need
pip install timm matplotlibpython weight_histogram.pyWhat it does:
- Loads the
mobilenetv3_small_100checkpoint from TIMM (changemodel_nameinside the script to inspect another model). - Generates histograms for every parameter tensor, batching layers into grids of
grid_rows * grid_colsplots and saving them underhistograms/params. - Registers forward hooks on every module, runs a dummy
torch.randn(1, 3, 224, 224)input, and saves activation histograms tohistograms/activations.
You can tweak bins, grid size, save paths, and the dummy input shape directly in weight_histogram.py to match your model’s expectations.
python converter_and_weight_histogram.py \
--model_name mobilenetv3_small_100 \
--quant_backend qnnpack \
--image_size 224 \
--batch_size 1Key behaviours:
- Loads the specified TIMM model and (by default) replaces HardSwish layers with ReLU to keep FX post-training quantization stable. Pass
--no_replace_hardswishto skip this. - Runs FX graph-mode quantization (
prepare_fx→ calibration →convert_fx) using the backend you specify (qnnpackfor ARM/mobile,fbgemmfor x86). - Saves side-by-side histograms for matching parameters under
histograms_params_compare, and for activations underhistograms_activations_compare.
Both scripts generate many PNGs, so consider cleaning the histograms* directories between experiments.
| Description | Preview |
|---|---|
| Parameter distribution grid | |
| Activation distribution grid | |
| Float vs quantized parameter comparison | |
| Float vs quantized activation comparison |
Each preview is generated by the commands above using the latest CUDA-enabled environment.
- Swap in custom checkpoints by loading them with TIMM and calling the helper functions directly.
- Feed real calibration data (instead of the default random tensor) to
quantize_model_fxfor more representative activation distributions. - Reduce the number of hooks by filtering
named_parameters()/named_modules()if memory becomes an issue on large networks.