How to tranfer weight of trained model and map on which have fewer classes?

Aliktk: <p>Hello, I am working on a classification and detection task where my old model was trained on 14 classes used a pre-train <code>densenet 121</code>. Now I have a new dataset with 10 classes and want to train that trained model.<br>
My model is like below:</p>
<pre><code class="lang-auto">device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


# device = torch.device("cpu")
model = rps.models.get_classifier(models.densenet121, 14).to(device) 
# I want to train the model on 10 classes where when I try to mentioned 10 it get error due to `state_dic`.
model.load_state_dict(torch.load('/home/ali/models/classifier_densenet121.pth')
</code></pre>
<p>Note: <code>rps</code> is my module name and the model is a combination of   <code>FPN + DenseNet121</code>. I load the model using this:<br>
But I don’t know how to extract the 10 classes weights from the state_dict?</p>
<p>I had been searching these links too.</p>
<aside class="quote" data-post="1" data-topic="1113">
  <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/albertxavier001/48/1808_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-load-part-of-pre-trained-model/1113">How to load part of pre trained model?</a> 
  </div>
  <blockquote>
    How to load part of pre trained model? 
Will the unused parameters be auto deleted if I load the whole model but only use part of it?
  </blockquote>
</aside>
<aside class="quote" data-post="1" data-topic="1492">
  <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/rbrigden/48/1139_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/copying-weights-from-one-net-to-another/1492">Copying weights from one net to another</a> 
  </div>
  <blockquote>
    Is there a canonical method to copy weights from one network to another of identical structure?
  </blockquote>
</aside>
<aside class="quote quote-modified" data-post="2" data-topic="1113">
  <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/chenyuntc/48/299_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-load-part-of-pre-trained-model/1113/2">How to load part of pre trained model?</a> 
  </div>
  <blockquote>
    I’m afraid not 

The keys of state_dict must exactly match the keys returned by this module’s  state_dict() function. 

if name not in new model , it will raise  KeyError 
but I gusee this may work for you 
    def load_my_state_dict(self, state_dict):
 
        own_state = self.state_dict()
        for name, param in state_dict.items():
            if name not in own_state:
                 continue
            if isinstance(param, Parameter):
                # backwards compatibility for seria…
  </blockquote>
</aside>

<p>Please Help me put<br>
Thank you very much</p>

Aliktk: <p>Answer:<br>
After a few try-and-hit methods I find a solution that is very simple to map the weights of the <code>state_dic</code>.<br>
Which is in the form of <code>strict=False</code>.</p>
<pre><code class="lang-auto">device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")


# device = torch.device("cpu")
model = rps.models.get_classifier(models.densenet121, 14).to(device) 
# I want to train the model on 10 classes where when I try to mentioned 10 it get error due to `state_dic`.
model.load_state_dict(torch.load('/home/ali/models/classifier_densenet121.pth'), , strict=False )
</code></pre>
<p>Thanks</p>