Josh A
Josh A

Reputation: 11

How can I convert a model trained in tensorflow 2 to a tensorflow 1 frozen graph

I would like to train a model using tensorflow 2 but afterwards I need to use a converter that is only compatible with tensorflow 1. Is it possible and if so how can I convert a model that was trained using tensorflow 2 to a tensorflow 1 format?

Upvotes: 1

Views: 975

Answers (1)

gihan
gihan

Reputation: 151

If there is no method that reliably converts your TF2 model to TF1, you can always save the trained parameters (weights, biases) and used them later to initiate your TF1 graph. I did it for some other purpose before. You can save as follows:

weights = []
for layer in model.layers:

    w = layer.get_weights()
    if len(w)>0:
      print(layer.name)
      weights.append(w)


with open('mnist_weights.pkl', 'wb') as f:
  pickle.dump(weights, f)  

Where for each layer w[0]=weights and w[1]=biases

Upvotes: 1

Related Questions