Thursday, December 28, 2017

TensorFlow Eager Mode: How to restore a model from a checkpoint?

Leave a Comment

I've trained a CNN model in TensorFlow eager mode. Now I'm trying to restore the trained model from a checkpoint file but haven't got any success.

All the examples (as shown below) I've found are talking about restoring checkpoint to a Session. But what I need is to restore the model into eager mode, i.e. without creating a session.

with tf.Session() as sess:   # Restore variables from disk.   saver.restore(sess, "/tmp/model.ckpt") 

Basically what I need is something like:

tfe.enable_eager_execution() model = tfe.restore('model.ckpt') model.predict(...) 

and then I can use the model to make predictions.

Can someone please help?

2 Answers

Answers 1

Eager Execution is still a new feature in TensorFlow, and was not included in the latest version, so not all features, are supported, but fortunately, loading a model from a saved checkpoint is.

You'll need to use the tfe.Saver class (which is a thin wrapper over the tf.train.Saver class), and your code should look something like this:

saver = tfe.Saver([x, y]) saver.restore('/tmp/ckpt') 

Where [x,y] represents the list of variables and/or models you wish to restore. This should precisely match the variables passed when the saver that created the checkpoint was initially created.

More details, including sample code, can be found here, and the API details of the saver can be found here.

Answers 2

I like to share TFLearn library which is Deep learning library featuring a higher-level API for TensorFlow. With the help of this library you can easily save and restore a model.

Saving a model

model = tflearn.DNN(net) #Here 'net' is your designed network model.  #This is a sample example for training the model model.fit(train_x, train_y, n_epoch=10, validation_set=(test_x, test_y), batch_size=10, show_metric=True) model.save("model_name.ckpt") 

Restore a model

model = tflearn.DNN(net) model.load("model_name.ckpt") 

For more example of tflearn you can check some site like...

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment