是否有方法将非 tf 函数嵌入到 tf.Keras 模型图中,作为 SavedModel 签名?

huangapple go评论97阅读模式
英文:

Is there way to embed non-tf functions to a tf.Keras model graph as SavedModel Signature?

问题

我想将预处理函数和方法添加到模型图作为SavedModel签名。

示例:

  1. # 假设我们有一个Keras模型
  2. # ...
  3. # 定义我要添加到模型图中的函数
  4. @tf.function
  5. def process(model, img_path):
  6. # 使用不同的库和模块进行一些预处理...
  7. outputs = {"preds": model.predict(preprocessed_img)}
  8. return outputs
  9. # 使用自定义签名保存模型
  10. tf.saved_model.save(new_model, dst_path,
  11. signatures={"process": process})

或者我们可以在这里使用 tf.Module。然而,问题是我无法将自定义函数嵌入到保存的模型图中。

有没有办法可以做到这一点?

英文:

I want to add preprocessing functions and methods to the model graph as a SavedModel signature.

example:

  1. # suppose we have a keras model
  2. # ...
  3. # defining the function I want to add to the model graph
  4. @tf.function
  5. def process(model, img_path):
  6. # do some preprocessing using different libs. and modules...
  7. outputs = {"preds": model.predict(preprocessed_img)}
  8. return outputs
  9. # saving the model with a custom signature
  10. tf.saved_model.save(new_model, dst_path,
  11. signatures={"process": process})

or we can use tf.Module here. However, the problem is I can not embed custom functions into the saved model graph.

Is there any way to do that?

答案1

得分: 1

根据文档,你稍微误解了Tensorflow中save_model方法的目的。

save_model方法的意图是将模型的图形序列化,以便稍后可以使用load_model进行加载

load_model返回的模型是tf.Module类,具有所有的方法和属性。相反,你想要序列化预测流程。

老实说,我不知道有什么好的方法来做到这一点,但你可以使用不同的方法来序列化你的预处理参数,例如pickle或你使用的框架提供的其他方法,并在其之上编写一个类,该类执行以下操作:

  1. class MyModel:
  2. def __init__(self, model_path, preprocessing_path):
  3. self.model = load_model(model_path)
  4. self.preprocessing = load_preprocessing(preprocessing_path)
  5. def predict(self, img_path):
  6. return self.model.predict(self.preprocessing(img_path))
英文:

I think you slightly misunderstand the purpose of save_model method in Tensorflow.

As per the documentation the intent is to have a method which serialises the model's graph so that it can be loaded with load_model afterwards.

The model returned by load_model is a class of tf.Module with all it's methods and attributes. Instead you want to serialise the prediction pipeline.

To be honest, I'm not aware of a good way to do that, however what you can do is to use a different method for serialisation of your preprocessing parameters, for example pickle or a different one, provided by the framework you use and write a class on top of that, which would do the following:

  1. class MyModel:
  2. def __init__(self, model_path, preprocessing_path):
  3. self.model = load_model(model_path)
  4. self.preprocessing = load_preprocessing(preprocessing_path)
  5. def predict(self, img_path):
  6. return self.model.predict(self.preprocessing(img_path))

huangapple
  • 本文由 发表于 2023年2月19日 09:02:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75497369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定