- Version: 20261019
- Source Code Copyright: Unknown — based on robintzeng/Pytorch-CSPNet, which declares no license
- Redistribution Restriction: This source code is integrated by LEADERG INC. Please use it privately. Please DO NOT distribute it to internet.
Console/terminal pipeline for training and running a CSPNet (CSP-ResNet50) image classifier (PyTorch), supporting Windows 11, Ubuntu Linux 24.04 and macOS 14+ (Apple Silicon). The engine lives under src/cspnet/. Two example datasets ship with trained checkpoints: chest (chest X-ray: NORMAL / PNEUMONIA) and plate (steel-plate defects, 6 classes: crazing / inclusion / patches / pitted_surface / rolled_in_scale / scratches). Inference works out of the box for both. Training, validation and inference run on the GPU (CUDA on Windows/Linux, MPS on Apple Silicon) and fall back to the CPU automatically.
[Folder Structure]
- CLAUDE.md / AGENTS.md: Program specification (identical content).
- CHANGELOG.md: Change log.
- copyright.txt: Copyright notice.
- Image-Classification-CSPNet-PyTorch-Python.html: This document (usage page).
- 1911.11929.pdf: CSPNet reference paper.
- data/{dataset}/: train/{class}/, val/{class}/ (ImageFolder layout), test/ (flat test images). Datasets: chest, plate.
- model/{dataset}/: checkpoint.pth (trained), resume.pth (continue-training weights), label.txt (class list), loss_plot.png after training.
- src/cspnet/: Engine (CSPResNet.py, trainer.py, test.py, inference*.py).
- tool/tools.py: Shared helpers (select_device: CUDA → MPS → CPU, gpu_ids, ...).
- src/: Wrapper entry points (train.py, validation.py, inference.py), launcher scripts and requirements.txt.
- venv/: Python virtual environment, created by 02_venv_create_windows.bat / 02_venv_create_linux.sh / 02_venv_create_mac.sh.
[Prerequisites]
- Python 3.14, installed by src\01_python_install_windows.bat (winget), src/01_python_install_linux.sh (apt, deadsnakes PPA when needed; uses sudo) or src/01_python_install_mac.sh (Homebrew).
- The venv scripts install PyTorch 2.13.0 + torchvision 0.28.0 first, then src/requirements.txt (numpy, Pillow, opencv-python, matplotlib, tqdm).
- Windows / Linux: the CUDA 13.0 (cu130) build. GPU use needs an NVIDIA Turing or newer GPU (GTX 16 / RTX 20 series and later) with a driver supporting CUDA 13.0 or newer (check nvidia-smi); without one the same build runs on the CPU.
- macOS: the PyPI build, using the Apple GPU through MPS. Apple Silicon (arm64) with macOS 14+ only; Intel Macs are not supported by PyTorch 2.13.0.
[Launcher Scripts]
Every step exists as a Windows batch file (.bat) and a shell script (.sh, shared by Linux and macOS) with identical behavior. All launchers switch to the project root and forward extra arguments. Every step takes --dataset chest or --dataset plate.
1. 01_python_install_windows.bat / 01_python_install_linux.sh / 01_python_install_mac.sh
Install Python 3.14 (winget / apt + deadsnakes / Homebrew). Skips when it is already installed.
2. 02_venv_create_windows.bat / 02_venv_create_linux.sh / 02_venv_create_mac.sh
Delete the old venv/, create a fresh one with Python 3.14, install PyTorch 2.13.0 + torchvision 0.28.0 (CUDA 13.0 build on Windows/Linux, PyPI/MPS build on macOS), then src/requirements.txt. Aborts loudly if pip fails; prints whether CUDA (or MPS) is available.
3. 03_venv_activate.bat / 03_venv_activate.sh
Activate the venv. On Linux/macOS, source it: source src/03_venv_activate.sh (bash or zsh). If the project folder was renamed after the venv was created, re-create the venv with 02_venv_create_windows/linux/mac first; the other launchers do not need activation.
4. 04_train.bat / 04_train.sh
Train (defaults: 500 epochs, batch 12, lr 0.005, early stopping 500; resumes from model/{dataset}/resume.pth when it exists).
5. 05_validation.bat / 05_validation.sh
Per-class + overall accuracy on a labeled folder (default data/{dataset}/train).
6. 06_inference.bat / 06_inference.sh
Classify a single image (default data/chest/test/NORMAL-11.jpg).
7. 07_inference_folder.bat / 07_inference_folder.sh
Classify every image in a folder (default data/{dataset}/test).
8. 99_venv_clean.bat / 99_venv_clean.sh
Delete the venv/ folder.
9. 99_venv_deactivate.bat / 99_venv_deactivate.sh
Deactivate the venv in the current shell. On Linux/macOS, source it: source src/99_venv_deactivate.sh (bash or zsh).
Extra arguments are forwarded to the Python entry point, e.g. src\06_inference.bat --device cpu.
[Python Entry Points]
src/train.py
--dataset chest --epochs 500 --batch-size 12 --learning-rate 0.005 --early-stopping 500 --device auto [--resume {pth}|""]. Resumes from model/{dataset}/resume.pth by default; checkpoint.pth, label.txt, loss_plot.png land in model/{dataset}/.
src/validation.py
--dataset chest --folder data/{dataset}/train --batch-size 12 --weights model/{dataset}/checkpoint.pth --device auto. Prints per-class and overall accuracy.
src/inference.py
--dataset chest --source {image|folder} --batch-size 12 --weights model/{dataset}/checkpoint.pth --device auto [--show]. A file source classifies one image; a folder source prints per-image results. --show displays each image (desktop session required).
--device: auto (default) picks CUDA → MPS → CPU; cuda, cuda:N, mps or cpu forces a device. The engine's GPU-index forms still work (0 = cuda:0, -1 = CPU, 0,1 = DataParallel on GPUs 0 and 1 for training and inference). A CUDA or MPS request on a machine without that backend falls back to auto with a warning. The selected device is printed at startup.
[Quick Start]
Windows 11
src\01_python_install_windows.bat
src\02_venv_create_windows.bat
src\06_inference.bat --dataset chest
src\07_inference_folder.bat --dataset plate
src\05_validation.bat --dataset chest
src\04_train.bat --dataset chest
Ubuntu 24.04
bash src/01_python_install_linux.sh
bash src/02_venv_create_linux.sh
bash src/06_inference.sh --dataset chest
bash src/07_inference_folder.sh --dataset plate
bash src/05_validation.sh --dataset chest
bash src/04_train.sh --dataset chest
macOS 14+ (Apple Silicon)
bash src/01_python_install_mac.sh
bash src/02_venv_create_mac.sh
bash src/06_inference.sh --dataset chest
bash src/07_inference_folder.sh --dataset plate
bash src/05_validation.sh --dataset chest
bash src/04_train.sh --dataset chest
Both datasets work out of the box with the shipped checkpoints. Training realistically wants a GPU.
[GPU]
- Device order: CUDA → MPS → CPU, overridable with --device.
- Windows / Linux: the venv scripts install the CUDA 13.0 (cu130) build directly. It needs a Turing-or-newer NVIDIA GPU and a driver supporting CUDA 13.0+. Pascal/Volta GPUs (GTX 10xx, V100) are not supported by cu130; on those, reinstall torch from the cu126 index inside the venv: pip install torch==2.13.0 torchvision==0.28.0 --index-url https://download.pytorch.org/whl/cu126 --force-reinstall
- macOS (Apple Silicon): the model runs on the Apple GPU through MPS; ops MPS lacks fall back to the CPU (PYTORCH_ENABLE_MPS_FALLBACK=1 is set by the entry scripts).
- Without a usable GPU everything runs on the CPU (slow for training). Checkpoints load on any device.
[References]
- CSPNet paper (Wang et al. 2020, shipped as 1911.11929.pdf): CVPRW 2020
- Pytorch-CSPNet source: https://github.com/robintzeng/Pytorch-CSPNet
- PyTorch installation: https://pytorch.org/get-started/locally/
- PyTorch MPS backend: https://docs.pytorch.org/docs/stable/notes/mps.html
This SDK is built in AppForAI - AI Dev Tools.