我是tensorflow 和机器学习的新手。在这里,我试图创建自己的文本分类。我面临以下问题。我在Android上加载tflite模型时出现以下错误。
Process: org.tensorflow.lite.examples.textclassification, PID: 12031
java.lang.IllegalStateException: Internal error: Unexpected failure when preparing tensor allocations: Encountered unresolved custom op: TensorListFromTensor.
Node number 4 (TensorListFromTensor) failed to prepare.
at org.tensorflow.lite.NativeInterpreterWrapper.allocateTensors(Native Method)
at org.tensorflow.lite.NativeInterpreterWrapper.init(NativeInterpreterWrapper.java:96)
at org.tensorflow.lite.NativeInterpreterWrapper.<init>(NativeInterpreterWrapper.java:61)
at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:224)
at org.tensorflow.lite.Interpreter.<init>(Interpreter.java:182)
at org.tensorflow.lite.examples.textclassification.TextClassificationClient.loadModel(TextClassificationClient.java:139)
at org.tensorflow.lite.examples.textclassification.TextClassificationClient.load(TextClassificationClient.java:129)
at org.tensorflow.lite.examples.textclassification.MainActivity.lambda$onStart$1$MainActivity(MainActivity.java:65)
at org.tensorflow.lite.examples.textclassification.-$$Lambda$MainActivity$3O7XdyFVukGQ46LZyOIpHdTRepg.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6898)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Py笔记本代码
input = tf.keras.layers.Input(shape=(max_len,))
x = tf.keras.layers.Embedding(max_words, embed_size, weights=[embedding_matrix], trainable=False)(input)
x = tf.keras.layers.Bidirectional(tf.keras.layers.GRU(128, return_sequences=True, dropout=0.1,
recurrent_dropout=0.1))(x)
x = tf.keras.layers.Conv1D(64, kernel_size=3, padding="valid", kernel_initializer="glorot_uniform")(x)
avg_pool = tf.keras.layers.GlobalAveragePooling1D()(x)
max_pool = tf.keras.layers.GlobalMaxPooling1D()(x)
x = tf.keras.layers.concatenate([avg_pool, max_pool])
preds = tf.keras.layers.Dense(6, activation="sigmoid")(x)
model = tf.keras.Model(input, preds)
model.summary()
在python中运行以下代码也会导致相同的错误
interpreter = tf.lite.Interpreter(model_path="xxx.tflite")
interpreter.allocate_tensors()
Error below
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-20-988ba8e1e2e5> in <module>
1 interpreter = tf.lite.Interpreter(model_path="touch_tf.tflite")
----> 2 interpreter.allocate_tensors()
/usr/local/lib/python3.7/site-packages/tensorflow_core/lite/python/interpreter.py in allocate_tensors(self)
240 def allocate_tensors(self):
241 self._ensure_safe()
--> 242 return self._interpreter.AllocateTensors()
243
244 def _safe_to_run(self):
/usr/local/lib/python3.7/site-packages/tensorflow_core/lite/python/interpreter_wrapper/tensorflow_wrap_interpreter_wrapper.py in AllocateTensors(self)
108
109 def AllocateTensors(self):
--> 110 return _tensorflow_wrap_interpreter_wrapper.InterpreterWrapper_AllocateTensors(self)
111
112 def Invoke(self):
RuntimeError: Encountered unresolved custom op: TensorListFromTensor.Node number 4 (TensorListFromTensor) failed to prepare.
2条答案
按热度按时间uelo1irk1#
这个链接帮助我解决了这个问题。
Git Issue #29472
设置
converter.experimental_new_converter =True
将有所帮助xmq68pz92#
我这样做了,但后来它在解释器初始化时分配Tensor时导致了一个错误。
如果你能对我的问题有任何帮助,请检查这个。Encountered unresolved custom op: SimpleMLCreateModelResource. Failed to allocate tensors