Skip to content

Visual tooling for inspecting PyTorch/TIMM models by plotting per-layer parameter and activation distributions, plus side-by-side comparisons between floating-point and FX-quantized variants to spot quantization drift, saturation, or clipping issues.

Notifications You must be signed in to change notification settings

generalMG/weights_histogram

Repository files navigation

Weight Histogram Tools

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.

Repository layout

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.

Requirements

  • Python 3.9+
  • PyTorch with CPU quantization support (torch, torchvision, torchaudio install picks the right build)
  • timm
  • matplotlib

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 matplotlib

Usage

1. Inspect a single model

python weight_histogram.py

What it does:

  1. Loads the mobilenetv3_small_100 checkpoint from TIMM (change model_name inside the script to inspect another model).
  2. Generates histograms for every parameter tensor, batching layers into grids of grid_rows * grid_cols plots and saving them under histograms/params.
  3. Registers forward hooks on every module, runs a dummy torch.randn(1, 3, 224, 224) input, and saves activation histograms to histograms/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.

2. Compare float vs quantized models

python converter_and_weight_histogram.py \
  --model_name mobilenetv3_small_100 \
  --quant_backend qnnpack \
  --image_size 224 \
  --batch_size 1

Key behaviours:

  • Loads the specified TIMM model and (by default) replaces HardSwish layers with ReLU to keep FX post-training quantization stable. Pass --no_replace_hardswish to skip this.
  • Runs FX graph-mode quantization (prepare_fx → calibration → convert_fx) using the backend you specify (qnnpack for ARM/mobile, fbgemm for x86).
  • Saves side-by-side histograms for matching parameters under histograms_params_compare, and for activations under histograms_activations_compare.

Both scripts generate many PNGs, so consider cleaning the histograms* directories between experiments.

Sample outputs

Description Preview
Parameter distribution grid Parameter histogram grid
Activation distribution grid Activation histogram grid
Float vs quantized parameter comparison Parameter comparison
Float vs quantized activation comparison Activation comparison

Each preview is generated by the commands above using the latest CUDA-enabled environment.

Tips

  • 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_fx for more representative activation distributions.
  • Reduce the number of hooks by filtering named_parameters() / named_modules() if memory becomes an issue on large networks.

About

Visual tooling for inspecting PyTorch/TIMM models by plotting per-layer parameter and activation distributions, plus side-by-side comparisons between floating-point and FX-quantized variants to spot quantization drift, saturation, or clipping issues.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages