Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问下 转tflite 模型能支持签名吗? #299

Open
xsong-AI opened this issue May 13, 2024 · 9 comments
Open

请问下 转tflite 模型能支持签名吗? #299

xsong-AI opened this issue May 13, 2024 · 9 comments
Labels
enhancement New feature or request work/x-large work that can be done within a week

Comments

@xsong-AI
Copy link

xsong-AI commented May 13, 2024

在tflite2.7 版本后 已经支持多签名了:https://tensorflow.google.cn/lite/guide/signatures?hl=zh-cn

class Model(tf.Module):

  @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)])
  def encode(self, x):
    result = tf.strings.as_string(x)
    return {
         "encoded_result": result
    }

  @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)])
  def decode(self, x):
    result = tf.strings.to_number(x)
    return {
         "decoded_result": result
    }

model = Model()

# Save the model
SAVED_MODEL_PATH = 'content/saved_models/coding'

tf.saved_model.save(
    model, SAVED_MODEL_PATH,
    signatures={
      'encode': model.encode.get_concrete_function(),
      'decode': model.decode.get_concrete_function()
    })

# Convert the saved model using TFLiteConverter
converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_PATH)
converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS,  # enable TensorFlow Lite ops.
    tf.lite.OpsSet.SELECT_TF_OPS  # enable TensorFlow ops.
]
tflite_model = converter.convert()

# Print the signatures from the converted model
interpreter = tf.lite.Interpreter(model_content=tflite_model)
signatures = interpreter.get_signature_list()
print(signatures)

这样一个tflite文件可以有多个graph,在调用的时候可以通过签名来找到子graph.
请教一下TinyNeuralNetwork能否支持一下这个功能?非常感谢啊!

@zk1998
Copy link
Collaborator

zk1998 commented May 13, 2024 via email

@peterjc123 peterjc123 added enhancement New feature or request work/x-large work that can be done within a week labels May 13, 2024
@peterjc123
Copy link
Collaborator

等下,这个需要tf.lite.OpsSet.SELECT_TF_OPS的吧,我们暂时还不想碰这些东西

@peterjc123
Copy link
Collaborator

peterjc123 commented May 13, 2024

话说你可以用contrib里面的tflite2tf(生成tf.function)再结合你这段代码来暂时满足这个需求

@xsong-AI
Copy link
Author

等下,这个需要tf.lite.OpsSet.SELECT_TF_OPS的吧,我们暂时还不想碰这些东西

啊,不需要tf.lite.OpsSet.SELECT_TF_OPS, 这只是网页的示例,我只需要生成多个graph的需求满足就行了

@xsong-AI
Copy link
Author

tflite2tf(生成tf.function)

我先试一下这个tflite2tf,不过感觉支持的op不是很多,可能不太能满足需求。

@peterjc123
Copy link
Collaborator

等下,这个需要tf.lite.OpsSet.SELECT_TF_OPS的吧,我们暂时还不想碰这些东西

啊,不需要tf.lite.OpsSet.SELECT_TF_OPS, 这只是网页的示例,我只需要生成多个graph的需求满足就行了

那我这边先前期研究一下,说实话我更偏向于添加把多个graph给merge起来的功能,这样至少core代码不用动

@peterjc123
Copy link
Collaborator

tflite2tf(生成tf.function)

我先试一下这个tflite2tf,不过感觉支持的op不是很多,可能不太能满足需求。

不够可以添加的,这个难度不高,你懂的

@xsong-AI
Copy link
Author

等下,这个需要tf.lite.OpsSet.SELECT_TF_OPS的吧,我们暂时还不想碰这些东西

啊,不需要tf.lite.OpsSet.SELECT_TF_OPS, 这只是网页的示例,我只需要生成多个graph的需求满足就行了

那我这边先前期研究一下,说实话我更偏向于添加把多个graph给merge起来的功能,这样至少core代码不用动

嗯嗯,那非常感谢了!
主要是现在有一些需求是固定一个backone, 后面接不同的功能,如果能分开graph的化会比较灵活

@peterjc123
Copy link
Collaborator

等下,这个需要tf.lite.OpsSet.SELECT_TF_OPS的吧,我们暂时还不想碰这些东西

啊,不需要tf.lite.OpsSet.SELECT_TF_OPS, 这只是网页的示例,我只需要生成多个graph的需求满足就行了

那我这边先前期研究一下,说实话我更偏向于添加把多个graph给merge起来的功能,这样至少core代码不用动

嗯嗯,那非常感谢了! 主要是现在有一些需求是固定一个backone, 后面接不同的功能,如果能分开graph的化会比较灵活

这边试了下,确实是不需要tf的ops就可以完成转换,主要需要支持TFLite Model的SubGraphs以及SignatureDefs,工作量在于需要把不同的SubGraph的信息读出来重新进行组织成新的Model。如前面所说的,会新增一个将多个tflite模型合并在一起的一个函数

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request work/x-large work that can be done within a week
Projects
None yet
Development

No branches or pull requests

3 participants