Author Alex de la Paz
This repository is a containerized environment and a python package I created named detection
that extends Torchvision FasterRCNN for object detection training, analysis, and simple inference as an example.
The code used here can be extended to use other labeled datasets if PyTorch dataset classes are created to ingest the annotations in the format the model (FasterRCNN here) requires.
A subset of the xView Dataset categories are used (3 in total) to include {Small Aircraft
, Fixed-Wing
, Cargo Plane
}
Space complexity considerations
are the batch size and overall dataset sample size training on a personal GPU such as a P100 available in Colab or consumer NVIDIA Geforce graphics cards. A batch size of 4
is used for the torch dataloader for the example training run and dataset.
Deep learning network training considerations
are the dataset is small (420 samples
). It is used as an example dataset. Further training can be extended and done with this set of tools easily. Training the full xView would require multiple GPUs or a very long training run with a P100 to achieve reasonable results. The dataset allows enough examples for the network to converge, but not enough for high accuracy without some weighted sampling or synthetic augmentation of the data.
object-detection-torch
│ analysis.py
│ commands.txt
│ detections.py
│ Dockerfile
│ run_training.sh
│ train_custom.py
│
├───artifacts
│ │ .gitkeep
│ │
│ └───planes_sgd
│ │ FasterRCNN_ResNet50
│ │ FasterRCNN_ResNet50.pt
│ │
│ ├───analysis
│ │ annots_ground_truth.png
│ │ annots_raw.png
│ │ annots_thresh_0.5.png
│ │ Loss Graph.png
│ │
│ └───weights_at_eval
│ .gitkeep
│ 20_epochs
├───datasets
│ └───xView
│ ├───images
│ │ └───train_images_tiled_planes
│ └───labels
│ coco_planes.pkl
└───detection
data.py
models.py
setup.py
trainer.py
Clone the repo to a workstation that has an NVIDIA enabled GPU, GPU Driver, and NVIDIA container toolkit.
git clone https://github.com/alexdelapaz/object-detection-torch
- set
object-detection-torch
as the current working directory - enter
./run_build.sh
within theobject-detection-torch folder
to pull and build the container image
- (the shell script that starts the container passes a reference to the
current working directory
which should beobject-detection-torch
) - enter
./run_container.sh
to run the object detection training container
NOTE: this is a deep learning container intended for computer vision (which is time prohibitive to run on cpu)
- the container can be run with a cpu, the example dataset will take more than a few minutes to perform one epoch of training
- the bash script runs the docker container with
--gpus all
To run the container on a system that does not have a gpu available the docker arg --gpus all
can be removed
docker run -it --mount type=bind,source="$(pwd)"/,target=/workspace detection
- use
ctrl+c
to exit the training loop if testing on a cpu becomes time prohibitive
- enter
./run_training.sh
to run an the example training strategy (saved inartifacts/plaines_sgd
)
Perform the same training cycle as the automated bash script run_training.sh
with train_custom.py
-
-d
is the directoryartifacts
-
-n
is thename
of the experiment and asubfolder
withinartifacts
that represents all the saved info to organize thetraining strategy
-
-bs
is the batch size (4
is used and tested to work with an NVIDIA P100 12gb VRAM) -
-e
20 epochs were ran for the example training run. -
-s
is thesave frequency
that evaluation is performed on the test set and the model info is saved. -
-lr
is thelearning rate
for the optimizer (0.005 is used here for Stochastic Gradient Descent) -
-o
is theoptimizer
(sgd is used for better generalization, adam is the other option)
Run the analysis.py
and detections.py
python programs to perform the analysis and inference on the model artifacts stored in the experiement folder (ex. planed_sgd)
created during a training run:
python3 analysis.py -d 'artifacts' -n planes_sgd
python3 detections.py -d 'artifacts' -n planes_sgd
- will save a matplotlib plot of the
training
andvalidation
losses produced by the final epoch of training - using
-d 'artifacts'
-n 'planes_sgd'
to pass theartifacts folder
and theplanes_sgd
experiment
- will save the ground truth, raw detection, and a confidence score filtered detection on a random image
- using
-d 'artifacts'
-n 'planes_sgd'
to pass theartifacts folder
and theplanes_sgd
experiment