Hello, I am working on a classification and detection task where my old model was trained on 14 classes used a pre-train densenet 121. Now I have a new dataset with 10 classes and want to train that trained model.
My model is like below:
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')

Note: rps is my module name and the model is a combination of   FPN + DenseNet121. I load the model using this:
But I don’t know how to extract the 10 classes weights from the state_dict?
I had been searching these links too.

  
    
    
    How to load part of pre trained model? 
  
  
    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?
  


  
    
    
    Copying weights from one net to another 
  
  
    Is there a canonical method to copy weights from one network to another of identical structure?
  


  
    
    
    How to load part of pre trained model? 
  
  
    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…
  


Please Help me put
Thank you very much