How to transfer pretained model from Tensorflow to PyTorch?

I have build exactly same model in both TF and Pytorch. And I trained in TF. For some reason, I have to transfer the pretrained weight to Pytorch.

The network is like:

In TF, Conv2d filter shape is [filter_height, filter_width, in_channels, out_channels], while in Pytorch is (out_channels, in_channels, kernel_size[0], kernel_size[1]).

So I have done below in TF:
image

and I transfer to pytorch like:
image

It turns out that the DQN in pytorch is not working well as in TF!

Hey i wanna do the same kind of thing too did you find a solution. or is there a document from which i can take inspiration .
Thanks

1 Like

See https://github.com/Cadene/tensorflow-model-zoo.torch for some hints / mechanisms.

6 Likes

In case someone else gets here and has the same issue, I think that the problem is using reshape before transpose.
I have loaded TF weights with PyTorch by permuting the weight Tensor, and it worked fine.

4 Likes

This was very helpful tip, thanks :slight_smile: