DistinaNet: RetinaNet with Distance Estimation for simultaneous object detection and distance estimation
Project description
DistinaNet: Combining Object Detection with Distance Estimation
DistinaNet extends RetinaNet with an additional distance estimation head that predicts the distance to detected objects. This enables simultaneous object detection and distance estimation in a single forward pass.
๐ Key Features
- Multi-task learning: Object detection + distance estimation
- Multiple distance head architectures: Base, Deep, Bottleneck, CBAM, Dynamic Branching
- Flexible loss functions: Huber, L1, L2, Smooth L1, LogCosh
- Research-oriented: Easy to modify and extend
- KITTI dataset support: Built-in tools for KITTI dataset preparation
- Unified CLI interface: Single entry point for all operations
๐ Table of Contents
- Installation
- Quick Start
- Project Structure
- Dataset Preparation
- Training
- Evaluation
- Inference
- Video Processing
- Model Architecture
- Development
- Citation
๐ป Installation
๐ฅ Quick Start (Recommended)
Step 1: Install PyTorch with CUDA support
# Visit https://pytorch.org/get-started/locally/ and select your configuration
# Example for CUDA 11.7:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117
# Example for CUDA 12.1:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121
# For CPU-only:
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
Step 2: Install DistinaNet
pip install distinanet
๐ฆ Installation Options
Option 1: PyPI Installation (Stable)
# Install PyTorch first (see Step 1 above)
pip install distinanet
#### Option 2: Development Installation
```bash
# Clone and install in development mode
git clone https://github.com/jonher16/distinanet.git
cd distinanet
# Install PyTorch first (see Step 1 above)
# Then install DistinaNet
pip install -e .
Option 3: Conda Environment (Recommended for Research)
# Create environment with CUDA support
conda create -n distinanet python=3.9
conda activate distinanet
# Install PyTorch with CUDA (example for CUDA 11.7)
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia
# Install DistinaNet
pip install distinanet
โ Tested With
Environment
- Python 3.9.23
- CUDA 11.7
- GPU: NVIDIA GeForce RTX 3050 OEM (8GB VRAM)
- Driver Version: 575.64.03
Core Dependencies
torch==1.13.1+cu117torchvision==0.14.1+cu117torchaudio==0.13.1+cu117numpy==1.26.4opencv-python==4.9.0.80
Additional Dependencies
scikit-image==0.24.0matplotlib==3.9.4pandas==2.3.2tensorboard==2.20.0tqdm==4.67.1six==1.17.0openpyxl==3.1.5
Package Managers
conda 24.9.2pip 25.2
๐ก Note: The project is compatible with newer versions, but the above were the exact versions used for testing and development.
Verify Installation
# Check if DistinaNet is installed correctly
distinanet --help
# Check PyTorch and CUDA
python -c "import torch; print(f'PyTorch: {torch.__version__}'); print(f'CUDA available: {torch.cuda.is_available()}')"
# Test DistinaNet import
python -c "import distinanet; print('โ
DistinaNet installed successfully!')"
Troubleshooting
๐จ CUDA Installation Issues:
If you encounter CUDA-related problems:
-
Install PyTorch with CUDA first:
# Check your CUDA version nvidia-smi # Install matching PyTorch version from https://pytorch.org/ pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117 # Then install DistinaNet pip install distinanet
-
Verify CUDA compatibility:
python -c "import torch; print(f'CUDA available: {torch.cuda.is_available()}'); print(f'CUDA version: {torch.version.cuda}')"
๐ง Dependency Conflicts:
For development setups with specific requirements:
# Use flexible requirements (recommended for development)
pip install -r requirements.txt
pip install -e .
๐ฆ NumPy Compatibility Issues:
If you see "Numpy is not available" errors:
# Use NumPy 1.x (stable with PyTorch)
pip install "numpy>=1.20.0,<2.0.0"
pip install "opencv-python>=4.5.0,<4.10.0"
๐ Environment Isolation:
For clean installations:
# Create isolated environment
conda create -n distinanet python=3.9
conda activate distinanet
# Install PyTorch first, then DistinaNet
conda install pytorch torchvision pytorch-cuda=11.7 -c pytorch -c nvidia
pip install distinanet
๐ก Pro Tips:
- Always install PyTorch before DistinaNet for proper CUDA detection
- Use
condafor CUDA environments when possible - Check PyTorch compatibility matrix for your system
- For headless servers, consider
opencv-python-headlessinstead ofopencv-python
๐ Quick Start
DistinaNet provides a unified command-line interface for all operations through the distinanet command:
# Train a model
distinanet train --csv_train data/train.csv --csv_classes data/classes.csv --csv_val data/val.csv
# Evaluate a model
distinanet evaluate --model_path checkpoints/model.pt --csv_annotations_path data/val.csv --csv_classes data/classes.csv
# Run inference on an image
distinanet inference --model checkpoints/model.pt --csv_classes data/classes.csv --csv_val data/val.csv
# Process a video
distinanet video --model_path checkpoints/model.pt --video_path input.mp4 --output_path output/
Help and Usage
# General help
distinanet --help
# Mode-specific help
distinanet train --help
distinanet evaluate --help
distinanet inference --help
distinanet video --help
Alternative: Create Your Own Shell Scripts
For convenience, you can create shell scripts with your preferred parameters. Create these files and make them executable with chmod +x scriptname.sh:
Create train.sh:
#!/bin/bash
export CUDA_VISIBLE_DEVICES=0
distinanet train \
--optimizer adam \
--epochs 10 \
--batch_size 16 \
--distance_loss_type huber \
--distance_head_type base \
--csv_train kitti_dataset/annotations/train_objects.csv \
--csv_classes kitti_dataset/classes.csv \
--csv_val kitti_dataset/annotations/validation_objects.csv \
--depth 18 \
--num_gpus 1
Create test.sh:
#!/bin/bash
export CUDA_VISIBLE_DEVICES=0
distinanet evaluate \
--csv_annotations_path kitti_dataset/annotations/test_objects.csv \
--model_path runs/2025-09-17_15-05-20/checkpoints/epoch_0.pt \
--images_path kitti_dataset/testing/image_2 \
--class_list_path kitti_dataset/classes.csv \
--save_path runs/2025-09-17_15-05-20/validation_results
Create inference.sh:
#!/bin/bash
export CUDA_VISIBLE_DEVICES=0
distinanet inference \
--csv_classes kitti_dataset/classes.csv \
--csv_val kitti_dataset/annotations/test_objects.csv \
--model runs/2025-09-17_15-05-20/checkpoints/epoch_0.pt
Create video.sh:
#!/bin/bash
export CUDA_VISIBLE_DEVICES=0
distinanet video \
--model_path runs/2025-09-17_15-05-20/checkpoints/epoch_0.pt \
--video_path kitti_dataset/test.mp4 \
--output_path runs/2025-09-17_15-05-20/video
Make scripts executable:
chmod +x train.sh test.sh inference.sh video.sh
๐ Project Structure
distinanet/
โโโ pyproject.toml # ๐ฆ Modern Python packaging configuration
โโโ requirements.txt # ๐ Dependencies (for legacy installs)
โโโ README.md
โโโ LICENSE
โโโ distinanet/ # ๐ฆ Core package
โ โโโ __init__.py
โ โโโ cli.py # ๐ Main CLI entry point
โ โโโ config.py # โ๏ธ Configuration management
โ
โ โโโ scripts/ # ๐ Executable scripts
โ โ โโโ __init__.py
โ โ โโโ train.py # Training script
โ โ โโโ test.py # Evaluation script
โ โ โโโ inference.py # Inference script
โ โ โโโ video.py # Video processing script
โ โโโ model/ # ๐ง Model definitions
โ โ โโโ model.py # Main DistinaNet model
โ โ โโโ ...
โ โโโ data/ # ๐ Data handling
โ โ โโโ datasets.py # Dataset classes
โ โ โโโ dataloader.py # Data loading utilities
โ โ โโโ transforms.py # Data transformations
โ โ โโโ ...
โ โโโ engine/ # ๐ง Training engine
โ โ โโโ trainer.py # Training logic
โ โ โโโ ...
โ โโโ utils/ # ๐ ๏ธ Utilities
โ โ โโโ logging_utils.py # Logging configuration
โ โ โโโ ...
โ โโโ evaluation/ # ๐ Evaluation metrics
โโโ kitti_dataset/ # ๐ KITTI dataset tools
โ โโโ download_kitti.sh
โ โโโ generate_annotations.sh
โ โโโ ...
โโโ runs/ # ๐ Training outputs and logs
Installation Commands
After installing the package with pip install . or pip install distinanet, you can use:
# Direct CLI commands (recommended)
distinanet train --help
distinanet evaluate --help
distinanet inference --help
distinanet video --help
# Create your own shell scripts (optional)
# See "Alternative: Create Your Own Shell Scripts" section above
๐ Dataset Preparation
KITTI Dataset
- Download KITTI dataset
cd kitti_dataset
chmod +x download_kitti.sh
./download_kitti.sh
- Generate annotations
chmod +x generate_annotations.sh
./generate_annotations.sh
This creates train_objects.csv, validation_objects.csv, and test_objects.csv files (used for training the model).
Custom Dataset Format
Annotations CSV Format
path/to/image.jpg,x1,y1,x2,y2,class_name,distance
Example:
/data/imgs/img_001.jpg,837,346,981,456,cow,12.5
/data/imgs/img_002.jpg,215,312,279,391,cat,6.8
/data/imgs/img_002.jpg,22,5,89,84,bird,23.1
/data/imgs/img_003.jpg,,,,,, # Negative example (no objects)
Classes CSV Format
class_name,id
Example:
cow,0
cat,1
bird,2
๐โโ๏ธ Training
Using CLI
distinanet train \\
--csv_train kitti_dataset/annotations/train_objects.csv \\
--csv_classes kitti_dataset/classes.csv \\
--csv_val kitti_dataset/annotations/validation_objects.csv \\
--epochs 100 \\
--batch_size 16 \\
--depth 18 \\
--distance_head_type base \\
--distance_loss_type huber \\
--optimizer adam
Using Your Shell Script
# Create and use your train.sh script (see "Create Your Own Shell Scripts" section)
chmod +x train.sh
./train.sh
Training Parameters
| Parameter | Options | Default | Description |
|---|---|---|---|
--depth |
18, 34, 50, 101, 152 | 50 | ResNet backbone depth |
--distance_head_type |
base, deep, bottleneck, cbam, dynamicbranching | base | Distance head architecture |
--distance_loss_type |
huber, l1, l2, smoothl1, logcosh | huber | Distance loss function |
--optimizer |
adam, sgd, rmsprop, adagrad, nadam | adam | Optimization algorithm |
--batch_size |
int | 1 | Training batch size |
--epochs |
int | 100 | Number of training epochs |
--lr |
float | 1e-5 | Learning rate |
๐ Evaluation
Using CLI
distinanet evaluate \\
--model_path runs/latest/checkpoints/model.pt \\
--csv_annotations_path kitti_dataset/annotations/test_objects.csv \\
--class_list_path kitti_dataset/classes.csv \\
--images_path kitti_dataset/testing/image_2
Using Your Shell Script
# Create and use your test.sh script (see "Create Your Own Shell Scripts" section)
chmod +x test.sh
./test.sh
Metrics:
- mAP: Mean Average Precision for object detection
- MAE: Mean Absolute Error for distance estimation
- IoU: Intersection over Union thresholds
๐ Inference
Single Image Inference
distinanet inference \\
--model runs/latest/checkpoints/model.pt \\
--csv_classes kitti_dataset/classes.csv \\
--csv_val kitti_dataset/annotations/test_objects.csv
Using Your Shell Script
# Create and use your inference.sh script (see "Create Your Own Shell Scripts" section)
chmod +x inference.sh
./inference.sh
The inference script will:
- Load the trained model
- Process images from the validation set
- Display results with bounding boxes and distance predictions
- Save annotated images (optional)
๐ฅ Video Processing
Process Video Files
distinanet video \\
--model_path runs/latest/checkpoints/model.pt \\
--video_path input_video.mp4 \\
--output_path output_directory/
Using Your Shell Script
# Create and use your video.sh script (see "Create Your Own Shell Scripts" section)
chmod +x video.sh
./video.sh
Features:
- Real-time object detection and distance estimation
- Annotated output video generation
- Support for various video formats
- Configurable confidence thresholds
๐๏ธ Model Architecture
DistinaNet consists of:
- ResNet Backbone: Feature extraction (ResNet-18/34/50/101/152)
- Feature Pyramid Network (FPN): Multi-scale feature fusion
- Classification Head: Object class prediction
- Regression Head: Bounding box regression
- Distance Head: Distance estimation (5 different architectures available)
Distance Head Architectures
- Base: Simple convolutional layers
- Deep: Deeper convolutional network
- Bottleneck: Efficient bottleneck design
- CBAM: Convolutional Block Attention Module
- Dynamic Branching: Adaptive feature selection
๐ง Development
Adding New Distance Heads
- Implement your distance head in
distinanet/model/model.py - Add it to the model factory in
distinanet/model/model_factory.py - Update configuration options in
config.py
Running Tests
# Test the installation
python -c \"import distinanet; print('DistinaNet imported successfully')\"
# Test training (1 epoch)
python cli.py train --epochs 1 --csv_train small_dataset.csv --csv_classes classes.csv
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Make your changes in the appropriate module
- Test your changes
- Commit (
git commit -m 'Add amazing feature') - Push (
git push origin feature/amazing-feature) - Create a Pull Request
๐ Citation
If you use DistinaNet in your research, please cite:
@inproceedings{distinanet2025,
author = {Jon Hernandez Aranda and Patrick Dominique Vibild and Daeyoung Kim},
title = {Distance-Aware Single-Stage Detectors: Combining Detection with Object-Specific Distance Estimation},
booktitle = {Proceedings of the 2025 16th International Conference on Information and Communication Technology Convergence (ICTC)},
year = {2025}
}
๐ Acknowledgements
- Base implementation from pytorch-retinanet
- Significant code borrowed from keras-retinanet
- NMS module from pytorch-faster-rcnn
- Original RetinaNet paper: Focal Loss for Dense Object Detection
๐ License
This project is licensed under the Apache 2.0 License - see the LICENSE file for details.
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
๐ Support
If you encounter any issues or have questions:
- Check the Issues page
- Create a new issue with detailed information
- Provide code examples and error messages when applicable
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file distinanet-1.0.1.tar.gz.
File metadata
- Download URL: distinanet-1.0.1.tar.gz
- Upload date:
- Size: 765.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70f8c45b31a234ad7c776e9a74898e9e5311dd9d686f91c35df04b23f98f6bfa
|
|
| MD5 |
e5a6568105edd6ea2dd072c5d29a1e82
|
|
| BLAKE2b-256 |
fecdcc2aef906cd57fb230d09bb759848cb96f3cf7c0566b9febacb5f0854e81
|
File details
Details for the file distinanet-1.0.1-py3-none-any.whl.
File metadata
- Download URL: distinanet-1.0.1-py3-none-any.whl
- Upload date:
- Size: 62.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5382210a0ceb546799ac7bfad8ceea46fb1154d022544b3c8abcdf49149d2393
|
|
| MD5 |
561725af3eebc0a7aac1a35d4b10775d
|
|
| BLAKE2b-256 |
1039fcc229e3d2debe6831295c25d74fb9ffcda314936e6203627c1558878d30
|