Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues when loading MobileViT_S() #7

Open
Sehaba95 opened this issue Apr 20, 2022 · 4 comments
Open

Issues when loading MobileViT_S() #7

Sehaba95 opened this issue Apr 20, 2022 · 4 comments

Comments

@Sehaba95
Copy link

Sehaba95 commented Apr 20, 2022

I wanted to load the MobileViT_S() model and use the pre-trained weights, but I have got some errors in my code. To make it easier and help others, I will share my solution (in case there will be someone who is beginner like me):

def load_mobilevit_weights(model_path):
  # Create an instance of the MobileViT model
  net = MobileViT_S()
  
  # Load the PyTorch state_dict
  state_dict = torch.load(model_path, map_location=torch.device('cpu'))['state_dict']
  
  # Since there is a problem in the names of layers, we will change the keys to meet the MobileViT model architecture
  for key in list(state_dict.keys()):
    state_dict[key.replace('module.', '')] = state_dict.pop(key)
  
  # Once the keys are fixed, we can modify the parameters of MobileViT
  net.load_state_dict(state_dict)
  
  return net

net = load_mobilevit_weights("MobileViT_S_model_best.pth.tar")
@wilile26811249
Copy link
Owner

wilile26811249 commented Apr 26, 2022

Thanks for your feedback.
May I share your code in the ReadMe?
It's very detail.
I will update in the readme.

@Sehaba95
Copy link
Author

Sehaba95 commented Apr 26, 2022 via email

@wilile26811249
Copy link
Owner

Hi @Sehaba95 , I have already updated your code in the ReadMe.
You can check it.
If you have any other questions, plz let me know. thank you.

@Sehaba95
Copy link
Author

Thank you for adding my solution in your Readme file.
If I will encounter any problem, I will get back to you, and discuss it together.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants