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

898 Views Asked by At

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?

1

There are 1 best solutions below

0
gihan On

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