This project implements a deep learning model for detecting and classifying vehicle damage from images. It uses a Faster R-CNN model with a ResNet50 backbone for object detection, trained on a custom dataset of vehicle damage images.
This project uses Poetry for dependency management. The main dependencies are:
- Python 3.8+
- PyTorch 2.4.1+
- torchvision 0.19.1+
- matplotlib 3.4.3+
- tqdm 4.62.3+
- Pillow 8.3.2+
- pycocotools 2.0.2+
- torchmetrics 1.4.2+
For a complete list of dependencies, see the pyproject.toml file.
-
Install Poetry if you haven't already:
curl -sSL https://install.python-poetry.org | python3 - -
Install the project dependencies using Poetry:
poetry install
-
Activate the virtual environment:
poetry shell
-
Set up your environment variables by creating a
.envfile in the project root directory. Use the provided.env.exampleas a template. -
To start training run the
training_pipeline.pyfile and to evaluate the model runeval.py
vehicle-damage-detection/
│
├── vehicledamagedetection/
│ ├── __init__.py
│ ├── dataset_class.py
│ ├── utils.py
│ ├── train.py
│ └── eval.py
├── tests/
├── .env
├── pyproject.toml
├── README.md
└── runs/
└── vehicle_damage_detection/
To train the model:
python vehicledamagedetection/training_pipeline.pyTo evaluate the model:
python vehicledamagedetection/eval.pyThe training script (training_pipeline.py) does the following:
- Loads the dataset using the custom
VehicleDamageDatasetclass. - Initializes the Faster R-CNN model with a ResNet50 backbone.
- Trains the model for a specified number of epochs.
- Saves the best model based on validation accuracy.
- Logs training progress and metrics using TensorBoard.
You can modify training parameters in the .env file.
This project uses Faster R-CNN with pretrained weights due to its:
- High accuracy in detecting and classifying multiple objects
- Ability to handle objects of various scales (important for different types of vehicle damage)
- Flexibility in backbone choice for performance tuning
Faster R-CNN's balance of speed and accuracy makes it well-suited for the nuanced task of vehicle damage detection, where precise localization and classification of various damage types is crucial.
- To speed up the training process and hardware constraints 1000 images were used for training with a batch size of 4.
- Improvements can be made by using techniques such as data augmentation as this is a small dataset. Also using a dataset with higher resolution images can improve performance
The trained model can be downloaded from here: https://drive.google.com/file/d/1a_VaybVFaZgh45ETkX0KcTGpzQ8akPm9/view?usp=sharing
The evaluation script (eval.py) performs the following:
- Loads the trained model.
- Runs the model on the test dataset.
- Computes evaluation metrics such as mAP (mean Average Precision).
To visualize training progress and results:
- Start TensorBoard:
tensorboard --logdir=runs --port=6006
- Open a web browser and go to
http://localhost:6006.
If port 6006 is already in use, you can specify a different port:
tensorboard --logdir=runs --port=6007To deploy the model using Docker and FastAPI:
-
Build the Docker image:
docker build -t vehicle-damage-detection . -
Run the Docker container:
docker run -p 80:80 vehicle-damage-detection
-
The FastAPI endpoint will now be available at
http://localhost/predict.
You can test the endpoint using the swagger page at http://localhost/docs or an API testing tool. For example:
curl -X POST -F "file=@path_to_your_image.jpg" http://localhost/predict --output prediction.jpgThis will send the image to the endpoint and save the result as prediction.jpg.
The FastAPI endpoint provides the following functionality:
- Endpoint: POST
/predict - Input: An image file
- Output: The input image with bounding boxes drawn around detected vehicle damage
The endpoint performs the following steps:
- Receives an uploaded image
- Preprocesses the image
- Runs the damage detection model
- Draws bounding boxes on the image for detected damage
- Returns the annotated image


