Wednesday, 2 October 2019

Keras Model: Save to .pb File and Load Back

It's rather hard to save and load tf.Variable(s) in a ML with only tensor maths as the current available saver utils only support Keras model (or to design owned saving format), these utils are (no more tf.train.Saver in TF2 as TF2 has no ore sessions):
  • tf.keras.callbacks.ModelCheckpoint
  • tf.keras.Model.save
The above saver utils only save weights and those kind of variables. To save a full frozen model, there are 2 formats: HDF5, or SavedModel (will be default in TF2). Let's Model be a Keras model, save and load as following:

Save:
tf.saved_model.save(Model,Path_To_A_Dir);

Load:
Feedonly_Model = tf.saved_model.load(Path_To_Model_Dir);
Keras_Model    = tf.keras.models.load_model(Path_To_Model_Dir);

Source code:
%tensorflow_version 2.x
%reset -f

#libs
import tensorflow as tf;

#data
X = [[0,0],[0,1],[1,0],[1,1]];
Y = [[0],  [1],  [1],  [0]  ];
X = tf.convert_to_tensor(X,tf.float32);
Y = tf.convert_to_tensor(Y,tf.float32);

#model
class model(tf.keras.Model):
  def __init__(this):
    super().__init__();
    this.Layer1 = tf.keras.layers.Dense(20, activation=tf.nn.leaky_relu);
    this.Out    = tf.keras.layers.Dense(1,  activation=tf.sigmoid);

  def call(this,X):
    H1  = this.Layer1(X);
    Out = this.Out(H1);
    return Out;
#end class

#saver for training (model check point is not a frozen graph, not convenient for inference)
Checkpoint = "/tmp/test/model.ckpt";
Callback = tf.keras.callbacks.ModelCheckpoint(
  filepath  =Checkpoint,
  save_freq =len(X)*100,
  verbose   =True
);

#train (or load)
#Model = tf.saved_model.load("/tmp/test/model"); #feed only, not keras model
Model = tf.keras.models.load_model("/tmp/test/model");
print(Model.evaluate(X,Y, batch_size=4, verbose=0));
'''
print("Training...");
Model = model();
Model.compile(loss     =tf.losses.mean_squared_error,
              optimizer=tf.keras.optimizers.SGD(1e-1),
              metrics  =["accuracy"]);

for I in range(10):
  Out = Model.evaluate(X,Y, batch_size=4, verbose=0);
  print("\nI =",I); 
  print(Out);
  Model.fit(X,Y, batch_size=4, epochs=100, verbose=0) #, 
            #callbacks=[Callback]);

Out = Model.evaluate(X,Y, batch_size=2, verbose=0);
print("\nAfter training:"); 
print(Out,"(Last)");
tf.saved_model.save(Model,"/tmp/test/model");
'''
print("\nDone.");
#eof

3 comments: