# Converting Tensorflow model (weights) to pytorch

**URL:** <https://discuss.pytorch.org/t/converting-tensorflow-model-weights-to-pytorch/99761>\
**Category:** Uncategorized\
**Created:** [October 18, 2020, 7:10am UTC](https://discuss.pytorch.org/t/converting-tensorflow-model-weights-to-pytorch/99761 "2020-10-18T07:10:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![YASJAY](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yasjay/32/12858_2.png) [@YASJAY](https://discuss.pytorch.org/u/YASJAY)\
**Post date:** [October 18, 2020, 7:10am UTC](https://discuss.pytorch.org/t/converting-tensorflow-model-weights-to-pytorch/99761/1 "2020-10-18T07:10:48Z")

</div>

Hi,

Looking for ways to convert a custom tensorflow trained model to pytorch equivalent that can be fine-tuned further in pytorch.

Thanks

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 19, 2020, 5:31am UTC](https://discuss.pytorch.org/t/converting-tensorflow-model-weights-to-pytorch/99761/2 "2020-10-19T05:31:18Z")

</div>

I don’t know if there are tools to convert the TF model automatically to PyTorch and think you would have to rewrite it manually in PyTorch.  
A definition of a custom model can be found in [this tutorial](https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#define-a-convolutional-neural-network) and might be a good starter.

Once the model architecture is created in PyTorch, you could convert the pretrained weights from TF to PyTorch. @tom and I did the same for the `StyleGAN` model in [this notebook](https://github.com/lernapparat/lernapparat/blob/master/style_gan/pytorch_style_gan.ipynb) so you could take a look at the implementation.

---

<div class="post-metadata">

**Author:** ![YASJAY](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/yasjay/32/12858_2.png) [@YASJAY](https://discuss.pytorch.org/u/YASJAY)\
**Post date:** [October 20, 2020, 4:33am UTC](https://discuss.pytorch.org/t/converting-tensorflow-model-weights-to-pytorch/99761/3 "2020-10-20T04:33:57Z")

</div>

Hi @ptrblck, thanks that helped. I was able extract the sequence of layer information and corresponding weights using tf.all\_variables().

The following are the components I am trying to transfer weights from tensorflow to pytorch now,

TF layers and weights:

1. Batchnorm layer: BatchNorm/beta:0, BatchNorm/moving\_mean:0 and moving\_variance:0
2. Conv layer: Conv/weights:0 and Conv/biases:0
3. Conv2d\_transpose layer: Conv2d\_transpose\_1/weights:0 and Conv2d\_transpose\_1/biases:0

Can I just create the same sequence of layers in pytorch and assign these? Will there be computational flow differences between pytorch and tensorflow?  
Wondering if you have idea on conversion for those particular layers.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [October 20, 2020, 4:38am UTC](https://discuss.pytorch.org/t/converting-tensorflow-model-weights-to-pytorch/99761/4 "2020-10-20T04:38:58Z")

</div>

The notebook also shows how to transfer these parameter to PyTorch.  
In particular, check out `weight_translate`, which permutes the conv filters etc. to match the expected shape for PyTorch layers.
