Converting model to tflite with SELECT_TF_OPS cannot convert ops HashTableV2 + others

孤者浪人 提交于 2021-01-28 05:40:31

问题


I'm trying to convert openimages_v4/ssd/mobilenet_v2 to tflite using the following code as suggested here:

import tensorflow as tf
MODEL_DIR = 'openimages_v4_ssd_mobilenet_v2_1'
SIGNATURE_KEYS = ['default']
SIGNATURE_TAGS = set()
saved_model = tf.saved_model.load(MODEL_DIR, tags=SIGNATURE_TAGS)
tf.saved_model.save(saved_model, 'new_model_path', signatures=saved_model.signatures)
converter = tf.lite.TFLiteConverter.from_saved_model('new_model_path', signature_keys=SIGNATURE_KEYS, tags=['serve'])
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()

but it's giving this error:

<unknown>:0: error: failed while converting: 'main': Ops that need custom implementation (enabled via setting the -emit-custom-ops flag):
    tf.HashTableV2 {container = "", device = "", key_dtype = i64, shared_name = "hub_input/index_to_string_1_load_0_3", use_node_name_sharing = true, value_dtype = !tf.string}
    tf.HashTableV2 {container = "", device = "", key_dtype = i64, shared_name = "hub_input/index_to_string_load_0_2", use_node_name_sharing = true, value_dtype = !tf.string}
    tf.LookupTableFindV2 {device = ""}
    tf.LookupTableImportV2 {device = ""}

I was able to get past these errors by adding:

 converter.allow_custom_ops = True

But according to this github issue post on April 13, 2020:

Removed AddHashtableOps support in Python temporarily. However, you can still add this to an interpreter in C++.

Is that still the case? Also for the code snippet on how to use the tflite model in an earlier comment on that same issue, what should be imported in order to use the interpreter_wrapper?


回答1:


Hashtable ops are custom ops in TFLite so you will need: converter.allow_custom_ops = True in order to convert your model.

The comment you mention is no longer valid. You can use AddHashtableOps in C++ or HashtableOpsRegisterer in python.

import tensorflow as tf

model_interpreter = tf.lite.interpreter.InterpreterWithCustomOps(
      model_content=tflite_model, custom_op_registerers=[HashtableOpsRegisterer])


来源:https://stackoverflow.com/questions/65876823/converting-model-to-tflite-with-select-tf-ops-cannot-convert-ops-hashtablev2-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!