Skip to content

Repository files navigation

SAR Ships Segmentation

Info

  • This repo contains ship segmentation in the processed images extracted from the Synthetic Aperture Radar (SAR) data. The dataset used in this repo is HRSID
  • This project involves learning some advanced concepts and skills like various model training and inference optimization strategies, using TFRecords for dataset creation, training using Nvidia Dali Pipelines, DDP training using torchrun

Instructions to run code for data preparation

Instructions to optimize model checkpoints for inference

Instructions to run code for ship segmentation

Label preparation code optimization

  • The label images are prepared using the COCO format JSON label files
  • The optimization used is the ThreadPoolExecutor to generate label images using multiple worker threads. The choice of ThreadPoolExecutor is due to the nature of the task that involes I/O bound operations i.e. saving the images to the disk and the operation is thread-safe
  • The following table shows the comparison of speedup with optimized code
Optimization method Avg. Time taken (in sec.)
Without any optimization 21.80
With ThreadPoolExecutor 9.32

Model training optimization using torch.compile

  • The torch.compile can be used for optimizing the model training that can effectively result in reduction in the training time
  • The following table shows the training time per epoch for the ConvNextV2-Tiny-DeepLabV3+ model
Model training + compile method Batch Size (per GPU) Data loading method Avg. time per epoch (in sec.)
Single GPU with Reduce-overhead 12 Torch DataLoader with PNG image loading 520
Single GPU with Max-Autotune 8 Torch DataLoader with PNG image loading 480
Single GPU with Reduce-overhead 8 Dali Pipeline with TFRecords 440
Single GPU with Reduce-overhead 8 Dali Pipeline with PNG 485
DDP + Multi GPUs (2) with Reduce-overhead 8 Dali Pipeline with TFRecords 220

DDP model training on a single node with multiple GPUs

  • The following method is recommended for DDP training even on single node with multiple GPUs (denoted by nproc_per_node param in the torchrun cmd)
torchrun --standalone --nproc_per_node=2 ML_SAR_Ships/src/run_sem_seg_trainer_ddp_dali_tfrecords.py --num-epochs 10 --learning-rate 3e-5 ...

Model inference using AOT inductor

  • The AOT inductor model compilation can result in the reduction in the initial inference start time i.e. the inference cold start can be reduced significantly. The inference time without preprocessing and data transfer
  • The following table shows the starting inference time speedup with AOT inductor model compilation for the ConvNextV2-Tiny-DeepLabV3+ model. It can be clearly observed that AOT inductor compiled model reduced the starting inference time by a significant amount
Model method Starting [first, second data samples] inference time (milli sec.) Avg. inference time [other data samples] (milli sec.)
Without AOT inductor compilation 3645.17, 210.14 30.5
With AOT inductor compilation 186.1, 30.56 30.5

Model inference using ONNX

  • The ONNX model optimization is used to benchmark the inference time on the same GPU
  • The following table shows the starting inference time speedup with ONNX model optimization for the ConvNextV2-Tiny-DeepLabV3+ model. It can be clearly seen that the avg inference time with ONNX model is higher than that of normal torch inference and AOT inductor compiled inference
Model method Starting [first, second data samples] inference time (milli sec.) Avg. inference time [other data samples] (milli sec.)
ONNX model 631.04, 136.03 102.3
ONNX model with IO binding 634.98, 135.54 102.00

Model inference using TensorRT

  • The TensorRT model optimization is used to benchmark the inference time on the same GPU. The inference time without preprocessing and data transfer
  • For performing any experiments with TensorRT, use the following docker image - nvcr.io/nvidia/pytorch:25.11-py3
  • Refer docker_cmds/README.md for instructions to run TensorRT experiments
  • The dynamo frontend is used to compile the export program in both fp32 and mixed precisions for the ConvNextV2-Tiny-DeepLabV3+ model. However, the mixed precision did not work since the inference results were all blank with mixed precision. Only fp32 worked with TensorRT
  • The following table shows the inference time with TensorRT optimization. It can be clearly observed that it did not give a significant improvement in the inference time
Model precision Avg. inference time (milli sec.)
float32 37.5

Visualization generation optimization

  • The visualization generation pipeline is optimized using ProcessPoolExecutor. This is the preferred choice since Matplotlib is not thread-safe
  • The following table shows the time taken for generating the visualizations for the test set containing around 2000 images
Num workers Time taken (in sec.)
4 169.81
8 86.65

Model performance quantitative metrics

  • The following table shows the quantitative metrics of the ConvNextV2-Tiny-DeepLabV3+ model performance without using class weights in the Focal loss function. The models have not been trained well enough to optimize for performance metrics but for learning new skills.
Model name Train mIoU Train Dice Test mIoU Test Dice
ConvNextV2-Tiny-DeepLabV3+ 0.8116 0.9978 0.8086 0.9970
ResNet34-UNet 0.8959 0.9990 0.8867 0.9983
PSAResNet34-UNet 0.8776 0.9986 0.8725 0.9977

Model performance qualitative visualization results - sample test set predictions

  • The following seven visualizations show the qualitative results of the ConvNextV2-Tiny-DeepLabV3+ model performance Sample predicted mask 1 Sample predicted mask 2 Sample predicted mask 3 Sample predicted mask 4 Sample predicted mask 5 Sample predicted mask 6 Sample predicted mask 7
  • The following visualization show the results of different inference methods to show that the inference predictions are consistent across different inference methods using ConvNextV2-Tiny-DeepLabV3+ model Sample predicted mask 8
  • The following seven visualizations show the qualitative results of the ResNet34-UNet model performance Sample predicted mask 1 Sample predicted mask 2 Sample predicted mask 3 Sample predicted mask 4 Sample predicted mask 5 Sample predicted mask 6 Sample predicted mask 7

About

Ship segmentation in the images extracted from Synthetic Aperture Radar data with Torch DDP + Dali data pipeline with TFRecords + ONNX + TensorRT + AOT Inductor

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages