3
$\begingroup$

I have trained a feature extractor in Keras and saved the weights as a h5 file. Now I want to load the same weights into the same model created and initialized in PyTorch for performance comparisons. Is there any way I can convert the h5 file to pth file so I can load that into the PyTorch model?

$\endgroup$
1
  • $\begingroup$ Did you ever find out? $\endgroup$
    – NelsonGon
    Commented Jun 2, 2020 at 15:03

1 Answer 1

2
$\begingroup$

You must be careful with the versions from TF and PyTorch (as some commands may be different).

Basically you must:

1 - Know your layer and activation structure on Keras:

You can get the layer information with:

model_keras.summary()

If you can't get info about activation functions, try:

for layer in model_keras.layers:
    print(layer.output)

2 - Build a model on PyTorch that has the same layer structure (and activation) as on Keras

3 - The recently created PyTorch model (let's say model_pyt) has different weights and biases from your model on Keras, so you must copy those weights and biases from the Keras model to PyTorch model:

BE CAREFUL HERE, PYTORCH WEIGHTS ARE TRANSPOSED IN RELATION TO KERAS WEIGHTS

Then, for example, copying a weight would be like:

model_pyt.layer1.weight.data = torch.tensor(model_keras.layers[0].get_weights()[0].T)

And for the bias:

model_pyt.layer1.bias.data = torch.tensor(model_keras.layers[0].get_weights()[1])

repeat that for all layers.

Then, your PyTorch model has the same architecture and weights as the Keras model and might behave in the same way.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.