How d-li14/mobilenetv2.pytorch works
Purpose and Entry Point
This is a PyTorch ImageNet training/evaluation repository centered on MobileNetV2. The CLI entry point is imagenet.py, which accepts architecture, ImageNet path, checkpoint, width multiplier, input size, and training/evaluation options. It also supports importing the model directly through models/imagenet/__init__.py.
Model Definition
MobileNetV2 is implemented in models/imagenet/mobilenetv2.py and exported by models/imagenet/__init__.py. The model builds the inverted-residual / linear-bottleneck backbone, parameterized for width multipliers and ImageNet classification output. This model boundary is independent of the training driver, enabling direct fine-tuning or inference use.
Training Orchestration
imagenet.py coordinates argument parsing, model construction, device placement, checkpoint loading/saving, optimizer and learning-rate scheduling, and epoch-level train/validation control. Training is intended for PyTorch DataParallel multi-GPU execution rather than distributed training. Evaluation-only mode loads a supplied weight file and runs validation.
Dataset and Batch Pipeline
ImageNet directory input enters through utils/dataloaders.py, which owns dataset loading, preprocessing/transforms, and PyTorch data-loader construction. Batches flow from this module into the training/evaluation loops in imagenet.py; the expected dataset layout includes class-labeled validation subdirectories.
Evaluation and Metrics
utils/eval.py provides classification evaluation behavior used by the driver, including ImageNet top-1/top-5 accuracy accounting. Validation consumes model logits and labeled batches, then returns aggregate metrics for console/log output and checkpoint selection.
Observability and Artifacts
utils/logger.py and utils/visualize.py support recording and presenting run metrics, while utils/progress supports training progress reporting. Checkpoints are written to the CLI-selected checkpoint directory; packaged pretrained weights in pretrained/mobilenetv2-c5e733a8.pth and the width/resolution-specific files are loadable PyTorch state dictionaries.
Runtime and External Dependencies
The material runtime is Python with PyTorch 1.0+ and CUDA/NVIDIA GPUs for practical ImageNet training. NVIDIA DALI is mentioned as an optional/in-development data pipeline dependency, but the documented standard path uses PyTorch data loaders. External inputs are locally provisioned ILSVRC2012/ImageNet data and local .pth model artifacts; this repository does not define a service or web deployment boundary.
Main entry points: imagenet.py ImageNet CLI driver · mobilenetv2.py MobileNetV2 backbone · dataloaders.py Dataset and loaders · eval.py ImageNet evaluation
Components 12
Model
- Model import API · Allows MobileNetV2 to be imported independently of the CLI.
models/imagenet/__init__.py - MobileNetV2 backbone · Inverted-residual ImageNet classifier parameterized by width and input size.
models/imagenet/mobilenetv2.py
Data pipeline
- ImageNet directories · ILSVRC2012 class-labeled training and validation images.
- Dataset and loaders · Builds datasets, transforms, and PyTorch data loaders.
utils/dataloaders.py - Labeled batches
Training runtime
- ImageNet CLI driver · Parses options and controls train, validation, resume, and evaluation-only runs.
imagenet.py - Train and validation loops · Runs forward/backward optimization and validation over batches.
imagenet.py - ImageNet evaluation · Computes aggregate top-1 and top-5 accuracy from logits and labels.
utils/eval.py - Run reporting · Records, presents, and reports training metrics.
utils/logger.py - PyTorch + CUDA GPUs · Uses PyTorch DataParallel for multi-GPU training.
Artifacts
- Run checkpoints · CLI-selected directory for saved and resumed training state.
- Packaged pretrained weights · Loadable MobileNetV2 .pth weights, including width/resolution variants.
pretrained/mobilenetv2-c5e733a8.pth
Connections 14
- Model import API → MobileNetV2 backbone: exports
- ImageNet CLI driver → MobileNetV2 backbone: constructs
- ImageNet directories → Dataset and loaders: images and labels
- Dataset and loaders → Labeled batches: loads and transforms
- ImageNet CLI driver → Train and validation loops: controls epochs
- Labeled batches → Train and validation loops: feeds
- Train and validation loops → MobileNetV2 backbone: forward and backward
- Train and validation loops → ImageNet evaluation: validation logits and labels
- ImageNet evaluation → ImageNet CLI driver: aggregate metrics
- ImageNet CLI driver → Run reporting: reports metrics
- ImageNet CLI driver → Run checkpoints: saves and resumes
- Packaged pretrained weights → ImageNet CLI driver: loads weights
- PyTorch + CUDA GPUs → ImageNet CLI driver: executes on devices
- PyTorch + CUDA GPUs → Train and validation loops: DataParallel execution