Softmax confidence outputs in SSD networks for all anchor boxes is in higher range

Chaitanya_C: <p>I used <a href="https://github.com/qfgaohao/pytorch-ssd" rel="noopener nofollow ugc"> qfgaohao / pytorch-ssd </a> (which has mobilenet-1-ssd, mobilenet-2-ssd-lite networks) to train a custom dataset with 2 classes (one as background and another class for the object type as the dataset is only for a single class). I first modified the dataloader to load my custom dataset. I trained upto 30 epochs and got an mAP of about 85% and the results seemed good for baseline. The output from softmax are good as for the background class its giving high probabilities and the object class its giving low probabilities for most boxes. In this particular network, we get output from 6 different confidence heads.<br>
Eg: while debugging I did <code>F.softmax(confidences[3], dim=2)</code> . The output was :</p>
<blockquote>
<p>tensor([[[0.9768, 0.0232],<br>
[0.9870, 0.0130],<br>
[0.9799, 0.0201],<br>
[0.9844, 0.0156],<br>
[0.9764, 0.0236],<br>
[0.9813, 0.0187],<br>
[0.9867, 0.0133],<br>
[0.9884, 0.0116],<br>
[0.9872, 0.0128],<br>
[0.9907, 0.0093],<br>
[0.9801, 0.0199],<br>
[0.9821, 0.0179],<br>
[0.9889, 0.0111],<br>
[0.9872, 0.0128],<br>
[0.9912, 0.0088],<br>
[0.9825, 0.0175],<br>
[0.9857, 0.0143],<br>
[0.9703, 0.0297],<br>
[0.9604, 0.0396],<br>
[0.9536, 0.0464],<br>
[0.9770, 0.0230],<br>
[0.9749, 0.0251],<br>
[0.9900, 0.0100],<br>
[0.9703, 0.0297],<br>
[0.9703, 0.0297],<br>
[0.9674, 0.0326],<br>
[0.9892, 0.0108],<br>
[0.9872, 0.0128],<br>
[0.9875, 0.0125],<br>
[0.9742, 0.0258],<br>
[0.9825, 0.0175],<br>
[0.9739, 0.0261],<br>
[0.9940, 0.0060],<br>
[0.9780, 0.0220],<br>
[0.9919, 0.0081],<br>
[0.9521, 0.0479],<br>
[0.9629, 0.0371],<br>
[0.9741, 0.0259],<br>
[0.9720, 0.0280],<br>
[0.9738, 0.0262],<br>
[0.9778, 0.0222],<br>
[0.9709, 0.0291],<br>
[0.9773, 0.0227],<br>
[0.9692, 0.0308],<br>
[0.9769, 0.0231],<br>
[0.9823, 0.0177],<br>
[0.9821, 0.0179],<br>
[0.9762, 0.0238],<br>
[0.9783, 0.0217],<br>
[0.9781, 0.0219],<br>
[0.9793, 0.0207],<br>
[0.9798, 0.0202],<br>
[0.9805, 0.0195],<br>
[0.9638, 0.0362]]])</p>
</blockquote>
<p>The output for object class scores sorted  ```torch.sort(F.softmax(confidences[0], dim=2) [0][:,1])````  is:</p>
<blockquote>
<p>torch.return_types.sort(<br>
values=tensor([<strong>6.7742e-04, 7.2989e-04</strong>, 7.3594e-04,  …, 8.8332e-01, <strong>9.0915e-01,<br>
9.1045e-01</strong>]),<br>
indices=tensor([ 121,  103,  367,  …, 1083, 1080,  969]))</p>
</blockquote>
<p>In the output of the first layer it detects the object class in most test images and the distance between the scores is very high as well. Here it got the output with .91, .90 where the object is and scores in rest of the bounding boxes being very low (in range of e-03 or e-04) where the object is not there.<br>
This output is reasonable as its able to detect background in most anchor locations. Similar was the case in other outputs of confidence heads. It was getting high score for the a few anchor boxes for the object class and the output made sense.</p>
<p>To train and experiment more, I’ve integrated the repository into my custom training pipeline by modularizing the code in such a way that the class has functions to build_net, train and eval the model.<strong>I have not made any change to the files for building the model, preprocessing or losses , except for a few functional changes</strong>. I’m able to properly load the models trained in original implementation into my pipeline without problem. But now that I have trained mobilenet2-ssd-lite in my integrated code for 30 epochs, the softmax outputs from my integrated pipeline is giving out values that are high for all the anchor boxes in object class. I again did <code>F.softmax(confidences[3], dim=2)</code>, this is the output:</p>
<blockquote>
<p>tensor([[[0.6760, 0.3240],<br>
[0.6753, 0.3247],<br>
[0.6753, 0.3247],<br>
[0.6748, 0.3252],<br>
[0.6757, 0.3243],<br>
[0.6758, 0.3242],<br>
[0.6747, 0.3253],<br>
[0.6755, 0.3245],<br>
[0.6742, 0.3258],<br>
[0.6738, 0.3262],<br>
[0.6728, 0.3272],<br>
[0.6743, 0.3257],<br>
[0.6751, 0.3249],<br>
[0.6749, 0.3251],<br>
[0.6743, 0.3257],<br>
[0.6743, 0.3257],<br>
[0.6742, 0.3258],<br>
[0.6739, 0.3261],<br>
[0.6752, 0.3248],<br>
[0.6737, 0.3263],<br>
[0.6746, 0.3254],<br>
[0.6749, 0.3251],<br>
[0.6749, 0.3251],<br>
[0.6750, 0.3250],<br>
[0.6765, 0.3235],<br>
[0.6748, 0.3252],<br>
[0.6748, 0.3252],<br>
[0.6724, 0.3276],<br>
[0.6756, 0.3244],<br>
[0.6727, 0.3273],<br>
[0.6747, 0.3253],<br>
[0.6749, 0.3251],<br>
[0.6747, 0.3253],<br>
[0.6739, 0.3261],<br>
[0.6741, 0.3259],<br>
[0.6739, 0.3261],<br>
[0.6767, 0.3233],<br>
[0.6770, 0.3230],<br>
[0.6768, 0.3232],<br>
[0.6769, 0.3231],<br>
[0.6775, 0.3225],<br>
[0.6766, 0.3234],<br>
[0.6770, 0.3230],<br>
[0.6755, 0.3245],<br>
[0.6751, 0.3249],<br>
[0.6752, 0.3248],<br>
[0.6752, 0.3248],<br>
[0.6753, 0.3247],<br>
[0.6756, 0.3244],<br>
[0.6764, 0.3236],<br>
[0.6754, 0.3246],<br>
[0.6755, 0.3245],<br>
[0.6751, 0.3249],<br>
[0.6762, 0.3238]]])</p>
</blockquote>
<p>The output for <code>torch.sort(F.softmax(confidences[0], dim=2) [0][:,1])</code> is</p>
<blockquote>
<p>torch.return_types.sort(<br>
values=tensor([<strong>0.1053, 0.1532</strong>, 0.1655,  …, 0.6778,<strong>0.7770, 0.8186</strong>]),<br>
indices=tensor([1084,  970,  968,  …,  969, 1080, 1083]))</p>
</blockquote>
<p>Thus it means its able to learn where the object is with high confidence but not able to properly tell where its not in the layers ahead. In confidence[0], compared to the model trained in original implementation above, the scores of the other anchors in the one trained in my integration are not that distant. In confidence[1] - confidence[5], the confidence outputs among the object class are very close in each layers separately but in higher range. It should ideally be able to output the confidence for the anchors to be in range of 0.0xxx as its doing from original implementation.  Eg: in confidence[1] all the values for class score might be 0.29xx, in confidence[3] as given above, all the values are in the range 0.32xx  and it is a pattern I was seeing. Sometime it can go in the range of 0.4xxx as well for all the anchor boxes. They only differ in 3rd and 4th decimal place.</p>
<p>I’m getting mAP of 71% on the integrated version as well. The consequence of this is when I run inference on the test images nms ends up outputting a lot of bounding boxes all over the image. If I increase the probability threshold to eg 0.5, all the bounding boxes in close range go away if they are below 0.5 and do end up having the object class detected. But it does miss out cases it was able to detect with the model trained in the original implementation.</p>
<p>The loss output from the original seem to lower in  original code. But when I’m training from my integration the losses go down as well but not as much. I used cosine lr schelduler in both the runs. I trained mobilenet-1-ssd as well from my implementation but the results are the same.</p>
<p>My implementation losses:<br>
<img src="https://user-images.githubusercontent.com/31937185/83347901-ddf34500-a345-11ea-996e-a65b2ae3c882.png" alt="" role="presentation" width="690" height="282"></p>
<p>To me it looks like the the layers for confidence headers from confidence[1] - confidence[5] are not learning and even confidence[0] is not able to learn that well in my pipeline.I’m not able to clearly tell where I’m going wrong. Any suggestions will be welcome to debug such a problem. A pytorch forum <a href="https://discuss.pytorch.org/t/same-softmax-values-no-matter-what-is-the-input-problem/8480">thread</a> is the closest I’ve found to the current problem.</p>

ptrblck: <aside class="quote no-group" data-username="Chaitanya_C" data-post="1" data-topic="83556">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chaitanya_c/48/19110_2.png" class="avatar"> Chaitanya_C:</div>
<blockquote>
<p>To train and experiment more, I’ve integrated the repository into my custom training pipeline by modularizing the code in such a way that the class has functions to build_net, train and eval the model. <strong>I have not made any change to the files for building the model, preprocessing or losses , except for a few functional changes</strong> .</p>
</blockquote>
</aside>
<p>I don’t know which changes were made, but you could try to isolate each change and check, if the change is applying the expected change.<br>
Could you explain from a high-level perspective, what your changes were supposed to do?</p>
<p>Also, I assume you are using the <code>softmax</code> for debugging?</p>

Chaitanya_C: <ol>
<li>
<p>I did step through the code in debugger and it looks like its able to follow the similar structure to the original code loading the model and data while training. I’m able to load the models trained in original implementation into my pipeline and run some eval operations on it. Also able to load the pretrained weights provided for mobilenet1 and mobilelet2 were loading for training. But I can try doing it once again by isolating and checking them as you have suggested.</p>
</li>
<li>
<p>The main <a href="https://github.com/qfgaohao/pytorch-ssd/tree/master/vision" rel="nofollow noopener">vision folder</a> which contains various modules for model, data and losses is almost same in my integration. The only changes that I made were :</p>
</li>
</ol>
<ul>
<li>
<p>Changed the data loading part to load images and labels from my custom dataset file. The prepossessing applied on the images and labels loaded from my dataset is the same as the original implementation.</p>
</li>
<li>
<p>Moved some of the tensors to ‘cpu’ device  as it was causing problem in dataloader it tried to do multiprocessing in cuda device when num_workers&gt;0. After moving the tesnor to cpu it prevented the error as cpu processes were spawned for dataloading and preprocessing. Below is the diff from this <a href="https://github.com/qfgaohao/pytorch-ssd/blob/master/vision/ssd/ssd.py" rel="nofollow noopener">file</a></p>
</li>
</ul>
<pre><code class="lang-auto">@@ -141,7 +143,7 @@
 class MatchPrior(object):
     def __init__(self, center_form_priors, center_variance, size_variance, iou_threshold):
         self.center_form_priors = center_form_priors
-        self.corner_form_priors = box_utils.center_form_to_corner_form(center_form_priors)
+        self.corner_form_priors = box_utils.center_form_to_corner_form(center_form_priors).to('cpu')
         self.center_variance = center_variance
         self.size_variance = size_variance
         self.iou_threshold = iou_threshold
@@ -149,11 +151,13 @@
     def __call__(self, gt_boxes, gt_labels):
         if type(gt_boxes) is np.ndarray:
             gt_boxes = torch.from_numpy(gt_boxes)
+            gt_boxes = gt_boxes.to('cpu')
         if type(gt_labels) is np.ndarray:
             gt_labels = torch.from_numpy(gt_labels)
+            gt_labels = gt_labels.to('cpu')
         boxes, labels = box_utils.assign_priors(gt_boxes, gt_labels,
                                                 self.corner_form_priors, self.iou_threshold)
-        boxes = box_utils.corner_form_to_center_form(boxes)
+        boxes = box_utils.corner_form_to_center_form(boxes).to('cpu')
         locations = box_utils.convert_boxes_to_locations(boxes, self.center_form_priors, self.center_variance, self.size_variance)
         return locations, labels
</code></pre>
<ul>
<li>Another change for this was made to this file: /vision/ssd/config/mobilenetv1_ssd_config.py</li>
</ul>
<pre><code class="lang-auto">@@ -20,4 +20,4 @@
 ]
 
 
-priors = generate_ssd_priors(specs, image_size)
\ No newline at end of file
+priors = generate_ssd_priors(specs, image_size).to('cpu')
\ No newline at end of file
</code></pre>
<ol start="3">
<li>Yes, the softmax function was used in debugging. The softmax is applied only when the model is run in test mode to get the probability outputs. I applied softmax on the individual feature maps which were output from confidence headers of various layers, which is when I discovered this discrepancy between the outputs of the model trained in the original implementation and the one trained in my pipeline.</li>
</ol>

Chaitanya_C: <p>Weight decay was 0.1. It should have been 0.0005. My bad.</p>