Object Detection Training Procedure

hoboscientist: <p>Hi,</p>
<p>I am trying to train an object detection model for my custom data. While I’ve constructed the model and data loading, I am having some doubts over how to prepare targets and calculate the loss.</p>
<p>The details of the training are:</p>
<ol>
<li>The problem is a binary classification task with localization.</li>
<li>I am using Yolov3 for the base model with a bit of customization for my project needs.</li>
<li>For each scale in Yolov3, I am constructing an adjacent zero tensor that is identical to the output of the model then calculating the midpoint of any objects in the image, finding out which grid cell does the midpoint belongs to, and assigning that grid cell with the necessary information about object co-ordinates and height/width. This tensor will be used as the target tensor for the scale.</li>
<li>Find which grid cells are responsible for objects then save indices for the non-zero positions.</li>
<li>Filter target and feature tensors for both objectness score and coordination regression by using the indices from step 3.</li>
<li>Reshape the features and target tensor to (-1, n), meaning no information about batch, grid cell, or anything, just plain old 1:1 comparison.</li>
<li>Calculate IOU between the predicted box and target box.</li>
<li>Object Loss is being calculated as (IOU * Object Confidence).</li>
<li>Add to Total Loss.</li>
<li>Repeat for another scale.</li>
<li>Backprop Object Loss.</li>
</ol>
<p>Some training shenanigans:</p>
<ol>
<li>For each scale, positional weights are being calculated for object == 0 or object == 1 then negative/positive as pos_weight for BCEWithLogitsLoss which means criterion is being created for each scale in each batch.</li>
<li>Adam Optimizer with 1e-3 LR and ReduceOnPlateau with the patience of 2 and learning rate reduction of 0.1 per step on eval_loss. This was done because my loss stops decreasing at some point and starts increasing and I read on multiple Github issues that something like this could help but it hasn’t in my case. Also, train_loss is oscillating a lot while monitoring in Tensorboard.</li>
<li>Batch Size of 16, can go up to 256.</li>
<li>Clipping gradients to 10.</li>
</ol>
<p>My questions are:</p>
<ol>
<li>Is it okay to only assign a single grid cell for the target? I am only considering whether the grid cell “supposed” to detect the object is successful or not. I have gone through the paper and multiple codes from various sources and never been able to grasp how they are building targets.</li>
<li>What if the object is sufficiently large and the scale at which I am predicting is not able to fully detect it? Do I still calculate the loss for that scale?</li>
<li>I am using “mean” as the reduction for the BCEWithLogitsLoss but reading the paper I get the impression that they are summing over the losses which is the reduction “sum”. Does this affect the training procedure much?</li>
<li>Before using (IOU * Object Confidence), I was trying to train by using Object Confidence + (1 - IOU) or MSE_Loss(IOU, 1). When using this formulation, the output of the model for Object Confidence became NaN after a few steps to a few epochs based on the learning rate. What might’ve caused this?</li>
<li>Should I use MSE_Loss on coordinates and height/width directly instead of IOUs? I am using the IOUs because using the bounding box attributes directly just gives larger numbers while using IOUs the same information can be supplied to the criterion while keeping the numbers in check.</li>
</ol>
<p>I am sorry for the lengthy post.</p>