Console/terminal sample for Class Activation Map (CAM) visualization with an ImageNet-pretrained InceptionV3 in PyTorch, supporting Windows 11 and Ubuntu Linux 24.04. The script classifies an image, prints the top-k ImageNet predictions, and saves a figure with the original image plus one CAM heatmap overlay per class, showing where the network looks. It works out of the box with the bundled example image and weights.
[Folder Structure]
- CLAUDE.md / AGENT.md: program specification (identical content).
- CHANGELOG.md: change log.
- copyright.txt: copyright notice.
- data/cam/: cat_dog.png (the example image) and imagenet_class_index.json (the 1000-class map).
- model/cam/: inception_v3_google-1a9a5a14.pth (ImageNet weights); the CAM figures ({name}_cam.png) are written here too.
- src/pretrainedmodels/: the vendored Cadene pretrainedmodels package.
- tool/tools.py: shared helpers.
- src/: source code, launcher scripts and this document.
- venv/: Python virtual environment, created by step 01.
[Instructions]
1. 01_venv_create.bat / 01_venv_create.sh
Delete the old venv/, create a fresh one and install src/requirements.txt. Aborts loudly if pip fails.
2. 02_venv_activate.bat / 02_venv_activate.sh
Activate the virtual environment. On Linux use source src/02_venv_activate.sh.
3. 03_cam.bat / 03_cam.sh
Classify the image and save the CAM figure to model/cam/.
4. 99_venv_clear.bat / 99_venv_clear.sh
Delete the venv/ folder.
[Python Entry Point]
src/cam.py
- --source data/cam/cat_dog.png: path to the input image
- --top 3: how many classes to report
- --weights model/cam/inception_v3_google-1a9a5a14.pth: the weights file to use
- --labels data/cam/imagenet_class_index.json: the class-name map
- --output model/cam/{name}_cam.png: path of the output figure
- --show: also open a window showing the result (optional, requires a desktop session)
The script prints the top-k labels with their probabilities; the saved figure stacks the original image and one jet-colormap heatmap overlay per class.
[Quick Start]
Windows 11
src\01_venv_create.bat
src\03_cam.bat
Ubuntu Linux 24.04
bash src/01_venv_create.sh
bash src/03_cam.sh
Point --source at your own image, for example: src\03_cam.bat --source path\to\photo.jpg --top 5
[References]
- CAM article (Medium, "Open black boxes"): medium.com/jarvis-toward-intelligence
- pretrainedmodels (vendored): github.com/Cadene/pretrained-models.pytorch
- Rethinking the Inception Architecture (InceptionV3): arxiv.org/abs/1512.00567
- PyTorch install: pytorch.org/get-started/locally
This SDK is built in AppForAI - AI Dev Tools.
Purchase license separately: USD 400, permanent authorization, single APP authorization, single machine authorization, one-year activation, one-year download, one-year update, one-year email technical support.