This is a continuation of the Detection Head tutorial that explains how to write the code such that you can avoid ugly indexing into the tensors and also have more maintainable and extensible components.
It would beneficial to first watch the DetectionHead tutorial
Link to the DetectionHead tutorial:
https://www.youtube.com/watch?v=U6rpkdVm21E
Link to the Google Colab notebook:
https://colab.research.google.com/drive/1AQzlSAK5vlNnvmkO1VDXaU8s9gTyRAGU?usp=sharing