How to do transfer learning with inception_v3?

username: mgloria
cooked: <p>Since very recently, inception_v3 is available in <a href="https://pytorch.org/docs/stable/torchvision/models.html" rel="noopener nofollow ugc">torchvision.models</a> and getting it is as simple as typing <code>model = models.inception_v3(pretrained=True)</code></p>
<p>Since the model was pretrained on ImageNet it has 1000 output classes which I wanted to change to 2 for my binary classifier - so I created the following head:</p>
<pre><code class="lang-auto">from collections import OrderedDict
head = nn.Sequential(OrderedDict([
                          ('fc1', nn.Linear(2048, 1024)),
                          ('relu1', nn.ReLU()),
                          ('fc2', nn.Linear(1024, 512)),
                          ('relu2', nn.ReLU()),
                          ('fc3', nn.Linear(512, 128)),
                          ('relu3', nn.ReLU()),
                          ('fc4', nn.Linear(128, 32)),
                          ('relu4', nn.ReLU()),
                          ('fc5', nn.Linear(32, 2)),
                          ('output', nn.LogSoftmax(dim=1))
                          ]))
    
</code></pre>
<p>My question is regarding how to add this head. If I was using restnet54, this would be as simple as <code>model.fc = head</code> but in the case of inception_v3 I have realized there is what it looks like as two output layers:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/1/b1760c9145a77e196e865df2386d1380e9468b3e.png" data-download-href="https://discuss.pytorch.org/uploads/default/b1760c9145a77e196e865df2386d1380e9468b3e" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/1/b1760c9145a77e196e865df2386d1380e9468b3e_2_517x156.png" alt="image" data-base62-sha1="pjTqJQyYS0Qr7xfk2AqhL3N1LaK" width="517" height="156" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/1/b1760c9145a77e196e865df2386d1380e9468b3e_2_517x156.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/1/b1760c9145a77e196e865df2386d1380e9468b3e_2_775x234.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/1/b1760c9145a77e196e865df2386d1380e9468b3e.png 2x" data-dominant-color="F0F1F1"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">809×246 18.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>and by doing <code>model(inputs)</code> I also get and <code>InceptionOutputs(logits=tensor([[-0.8333, -0.5702] [...], device='cuda:0', grad_fn=&lt;LogSoftmaxBackward&gt;), aux_logits=tensor([[ 1.0039,  2.4251] [...]</code></p>
<p>Do I need to change both heads? What is the difference between logits and aux_logits. I see the term ‘logits’ being used very often but I struggle to make sense of it (i.e. I understand the difference between scores and probs after a softmax, not sure where logits and aux_logits fit in this schema).</p>
<p>This would affect the whole training process, i.e. it can no longer be only <code>optimizer = optim.Adam(model.fc.parameters(), lr=0.001)</code></p>

username: ptrblck
cooked: <p><code>inception_v3</code> was added in <a href="https://github.com/pytorch/vision/commit/72781eba295cde0c13ca539b805e093d75c1b4cb#diff-8ff176617041ca0f8d08b173510523f2">March 2017</a>, so I’m not sure, if we are talking about the same model.</p>
<p>Anyway, the <a href="https://pytorch.org/tutorials/beginner/finetuning_torchvision_models_tutorial.html">Finetuning Tutorial</a> has specific code paths for the inception model and for its auxiliary outputs.</p>
<aside class="quote no-group" data-username="mgloria" data-post="1" data-topic="74129">
<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/mgloria/48/21964_2.png" class="avatar"> mgloria:</div>
<blockquote>
<p>I see the term ‘logits’ being used very often but I struggle to make sense of it (i.e. I understand the difference between scores and probs after a softmax, not sure where logits and aux_logits fit in this schema).</p>
</blockquote>
</aside>
<p>Logits are basically the input to a softmax layer.<br>
Since <code>nn.CrossentropyLoss</code> is using <code>F.log_softmax</code> and <code>nn.NLLLoss</code> internally, you don’t need to apply softmax manually.</p>

username: mgloria
cooked: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a>! I was coding super late and I got confused with EfficientNet <img src="https://discuss.pytorch.org/images/emoji/apple/face_with_hand_over_mouth.png?v=9" title=":face_with_hand_over_mouth:" class="emoji" alt=":face_with_hand_over_mouth:"> but your link and explanation was super useful! Thanks again.</p>