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

Converting a model into PyTorch #372

Closed
p-christ opened this issue Jun 14, 2019 · 8 comments
Closed

Converting a model into PyTorch #372

p-christ opened this issue Jun 14, 2019 · 8 comments
Labels
question Further information is requested

Comments

@p-christ
Copy link

Hi,

I'm trying to load a pre-trained model and convert it into a PyTorch model but can't get it to work and was wondering if someone could help me.

I'm able to load the pre-trained model using stable baselines and copy the weights over to a PyTorch model. But then when I play the game the pytorch agent is not able to get the same score as the baselines agent and i am not sure why. It could potentially be because the baselines agent does some extra pre-processing behind the scenes besides just normalising the state to the 0-1 range? Is anyone able to help me?

I've made a colab to demonstrate the problem here: https://colab.research.google.com/drive/1-IIjA1oKUjg5eoctajpl06OoHzU-5-_9

@araffin araffin added the question Further information is requested label Jun 14, 2019
@araffin
Copy link
Collaborator

araffin commented Jun 14, 2019

Hello,

It could potentially be because the baselines agent does some extra pre-processing behind the scenes besides just normalising the state to the 0-1 range?

There is no such up to my knowledge. However, I would recommend you first trying with a simpler network (e.g. on CartPole-v1), because it gets tricky with convolution (different conventions for pytorch and tensorflow).
I would also suggest you to check all shapes and names before assigning the weights, using .named_parameters() for pytorch.

EDIT: I made it work with CartPole, will share the notebook soon

@araffin
Copy link
Collaborator

araffin commented Jun 14, 2019

Update: I made it work with CartPole, you can find the notebook here: https://colab.research.google.com/drive/1R-wHO2gLQScx46EIjqj7Sj6MjK-i5Hey

Will try to make it work with the cnn if I have some time this weekend.

@araffin
Copy link
Collaborator

araffin commented Jun 20, 2019

Update: I'm working on the CNN now, it seems that the problem comes with the first fully connected layer (the conv layer outputs the right thing).

@araffin
Copy link
Collaborator

araffin commented Jun 20, 2019

The problem comes from the reshape (from conv to fc)

@araffin
Copy link
Collaborator

araffin commented Jun 20, 2019

@p-christ , I solved the issue doing that before flattening:

# shape before flattening
# tf: (?, 7, 7, 64)
# pytorch: [1, 64, 7, 7]
x = x.permute(0, 2, 3, 1).contiguous()
x = x.view(x.size(0), -1)

@araffin
Copy link
Collaborator

araffin commented Jun 20, 2019

@p-christ I made a working colab notebook: https://colab.research.google.com/drive/1XwCWeZPnogjz7SLW2kLFXEJGmynQPI-4

The problem came from tensorflow/pytorch differences, not SB.

Closing the issue.

@araffin araffin closed this as completed Jun 20, 2019
@p-christ
Copy link
Author

thanks a lot

@Chainesh
Copy link

Chainesh commented Apr 1, 2024

Can learnings from this be documented, also if it's possible to make a function to do same in sb3?

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

No branches or pull requests

3 participants