Efficient single object detector

AviZ: <p>I want to build an object detector.<br>
The YOLO models are too large for my use case.</p>
<p>In my use case there is only a single type of object to detect.<br>
It is also guaranteed to appear once at most.</p>
<p>Hence I thought I could come up with a very simple and efficient model.</p>
<p>I was wondering, for such case, how would you design the model?<br>
Would you choose YOLO style model with anchors? Which convolutions?<br>
Which loss to take advantage there is only a single class?</p>
<p>I’d be happy if people shared there intuition to that specific use case.</p>

Aknw_Fen: <p>The smallest <a href="https://github.com/Megvii-BaseDetection/YOLOX" rel="noopener nofollow ugc">YOLOX</a> is ~2Mb after exporting to onnx (with fp16 I believe), did you try it?</p>
<p>It’s not that simple run though, from the issues.</p>
<p>You can also based your network off theirs, to answer your questions.</p>

AviZ: <aside class="quote no-group" data-username="Aknw_Fen" data-post="2" data-topic="214164" data-full="true">
<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/aknw_fen/48/74156_2.png" class="avatar"> Aknw_Fen:</div>
<blockquote>
<p>The smallest <a href="https://github.com/Megvii-BaseDetection/YOLOX" rel="noopener nofollow ugc">YOLOX</a> is ~2Mb after exporting to onnx (with fp16 I believe), did you try it?</p>
</blockquote>
</aside>
<p>I will give it a try.<br>
My purpose is to learn by doing, so I’d appreciate ideas.</p>
<p>Like what guidelines make a detector efficient?<br>
What loss, any special data preparation?<br>
How to take advantage only a single class is needed?</p>

Aknw_Fen: <blockquote>
<p>Like what guidelines make a detector efficient?</p>
</blockquote>
<p>In cases of computer vision it tends to include blocks of <code>Conv2d-&gt;BatchNorm-&gt;Activation-&gt;Dropout2d-&gt;</code> this will learn filters (<a href="https://en.wikipedia.org/wiki/Kernel_(image_processing)" rel="noopener nofollow ugc">like these</a>). Optimisation comes later.</p>
<p>To <em>keep sizes manageable</em>, you may want <a href="https://github.com/TylerYep/torchinfo" rel="noopener nofollow ugc">torchinfo.summary</a>.</p>
<p>One intends to pass information efficiently from the areas to the channels, reducing H-W and increasing C. This normally involves hyperparameter tuning.</p>
<p><em>For the head,</em> you want to end with <code>Flatten-&gt;Linear-&gt;Activation</code> for single class prediction you can use a sigmoid function or two units and a softmax. But see this <a href="https://stats.stackexchange.com/questions/207049/neural-network-for-binary-classification-use-1-or-2-output-neurons" rel="noopener nofollow ugc">cool post about which one is best</a> (tldr, use sigmoid.)</p>
<p>Another option is using a global max pool layer instead of the linear.</p>
<p>It’s also normally used <code>x.view(-1, *size[1:])</code> for flatten instead of a layer. Not fully sure why not <code>x.view(*size)</code> but anyhow, that’s what I see sometimes.</p>
<blockquote>
<p>What loss, any special data preparation?</p>
</blockquote>
<p>For the loss for a classification, many times MSE is enough.</p>
<blockquote>
<p>How to take advantage only a single class is needed?</p>
</blockquote>
<p>Look at the link I provided last.</p>
<p>You may also want to use a random set of common images in your context without the class for teaching “not an instance of the class”. So if you want to detect dogs in a zoo, pass other animals, not dogs as “not a dog” may help.</p>

AviZ: <p>Great tips.</p>
<p>What about building the model?<br>
I’d like to avoid using anchors.<br>
So basically 2-3 scales, each output a tensor of <code>(h x w x 5)</code> where the 3rd dimensions is:</p>
<ul>
<li>Probability of detection (Classification).</li>
<li><code>(x, y)</code> - shift from the top left of the pixel for the center of the box.</li>
<li><code>(width, height)</code> - normalized width and height of the box.</li>
</ul>
<p>I assume data is labeled in YOLO format.<br>
Now, what I don’t get is how to implement the loss.<br>
How the loss interacts with the NMS many models apply?<br>
Should I apply the loss of the regression only for the cases the model has high probability of detection?</p>

Aknw_Fen: <p>I actually replied about the loss for a classification task, not for a single object detection as in finding the bounding box.</p>
<p>For that, you can search tutorials, it’s I don’t quite remember it right now, but as a guess I think it involves several losses, for example 1 for the 4 coordinates, and another for the classification. The original YOLO actually had a pretty complicated loss.</p>