Load trained pytorch weights in tensorflow


After working through the first lesson, I tried the resnet-34 model on an image dataset. The script in iPython notebook saves the weights of the finetuned model. I’d like to load these weights saved as .h5 file. How can this be done?

(Piotr Czapla) #2

Loading weights in pure Tensorflow is painful process even if you have them stored in the tensorflow format. It took me a full day of googling to manage to implement the transfer learning in pure Tensorflow with Estimators.

Fortunately Tensorflow comes with Keras so you can use it to load the weights, keras even comes with load_weights function that takes .h5 and initialise the weights in the Keras model. However I would expect that the internal structure of the .h5 file expected by Keras is different than the one used by pytorch even though the format is the same.
There for I would try first to convert the weights using a pytorch to keras converter then load them to keras, and then convert keras model to a tesnorflow estimator to use it in pure tensorflow.

Here are few converters I’ve found: