由于内部错误,无法在解释器上运行tflite模型

阿努拉格·舒克拉(Anurag Shukla)

我正在尝试为Android构建离线翻译器。我的模型从该指南中得到了极大的启发:https : //www.tensorflow.org/tutorials/text/nmt_with_attention我只是做了一些修改以确保模型可序列化。(您可以在最后找到该模型的代码)

该模型可以在我的jupyter笔记本上完美运行。我正在使用Tensorflow版本:2.3.0-dev20200617,我也能够使用以下代码段生成tflite文件:

converter = tf.lite.TFLiteConverter.from_keras_model(partial_model)
tflite_model = converter.convert()

with tf.io.gfile.GFile('goog_nmt_v2.tflite', 'wb') as f:
  f.write(tflite_model)

但是,当我使用生成的tflite模型在android上进行预测时,会引发错误 java.lang.IllegalArgumentException: Internal error: Failed to run on the given Interpreter: tensorflow/lite/kernels/concatenation.cc:73 t->dims->data[d] != t0->dims->data[d] (8 != 1) Node number 84 (CONCATENATION) failed to prepare.

这很奇怪,因为我提供了与在jupyter笔记本中完全一样的输入尺寸。这是用于测试(虚拟输入)模型是否在android上运行的Java代码:

 HashMap<Integer, Object> outputVal = new HashMap<>();
        for(int i=0; i<2; i++) outputVal.put(i, new float[1][5]);
        float[][] inp_test = new float[1][8];
        float[][] enc_hidden = new float[1][1024];
        float[][] dec_input = new float[1][1];
        float[][] dec_test = new float[1][8];

        tfLite.runForMultipleInputsOutputs(new Object[] {inp_test,enc_hidden, dec_input, dec_test},outputVal);

这是我的gradle依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
    implementation 'org.tensorflow:tensorflow-lite-select-tf-ops:0.0.0-nightly'
    // This dependency adds the necessary TF op support.
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

正如错误所指出的那样,节点84的尺寸存在问题。因此,我继续使用Netron可视化tflite文件。我放大了串联节点,可以在此处找到该节点的图片以及输入和输出尺寸您可以在此处找到整个生成的图形

事实证明,位置84的串联节点实际上不是串联的,您可以从输入和输出维度中看到这一点。处理完1X1X1和1X1X256矩阵后,它只会吐出1X1X1矩阵。我知道tflite图与原始模型图并不相同,因为为了优化,很多操作被替换甚至删除,但这似乎有些奇怪。

我不能将此与错误相关联。如果它完美地在jupyter上运行,是框架问题还是我缺少了什么?另外,有人可以向我解释t->dims->data[d] != t0->dims->data[d]d什么错误吗?

如果您对任何一个问题都有答案,请写下。如果您需要任何其他详细信息,请告诉我。

这是模型的代码:


Tx = 8
def Partial_model():
    outputs = []
    X = tf.keras.layers.Input(shape=(Tx,))
    partial = tf.keras.layers.Input(shape=(Tx,))
    enc_hidden = tf.keras.layers.Input(shape=(units,))
    dec_input = tf.keras.layers.Input(shape=(1,))
    
    d_i = dec_input
    e_h = enc_hidden
    X_i = X
    
    enc_output, e_h = encoder(X, enc_hidden)
    
    
    dec_hidden = enc_hidden
    print(dec_input.shape, 'inp', dec_hidden.shape, 'dec_hidd')
    for t in range(1, Tx):
        print(t, 'tt')
      # passing enc_output to the decoder
        predictions, dec_hidden, _ = decoder(d_i, dec_hidden, enc_output)
#         outputs.append(predictions)
        print(predictions.shape, 'pred')
        d_i = tf.reshape(partial[:, t], (-1, 1))
        print(dec_input.shape, 'dec_input')
    
    predictions, dec_hidden, _ = decoder(d_i, dec_hidden, enc_output)
    d_i = tf.squeeze(d_i)
    
    outputs.append(tf.math.top_k(predictions, 5))
    
    return tf.keras.Model(inputs = [X, enc_hidden, dec_input, partial], outputs = [outputs[0][0], outputs[0][1]])




class Encoder():
  def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz):
    self.batch_sz = batch_sz
    self.enc_units = enc_units
    self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
    self.gru = tf.keras.layers.GRU(self.enc_units,
                                   return_sequences=True,
                                   return_state=True,
                                   recurrent_initializer='glorot_uniform')

  def __call__(self, x, hidden):
    x = self.embedding(x)
    output, state = self.gru(x, initial_state = hidden)
    print(output.shape, hidden.shape, "out", "hid")
    return output, state


  def initialize_hidden_state(self):
    return tf.zeros((self.batch_sz, self.enc_units))



class BahdanauAttention():
  def __init__(self, units):
    self.W1 = tf.keras.layers.Dense(units)
    self.W2 = tf.keras.layers.Dense(units)
    self.V = tf.keras.layers.Dense(1)

  def __call__(self, query, values):
    # query hidden state shape == (batch_size, hidden size)
    # query_with_time_axis shape == (batch_size, 1, hidden size)
    # values shape == (batch_size, max_len, hidden size)
    # we are doing this to broadcast addition along the time axis to calculate the score
    print(query.shape, 'shape')
    query_with_time_axis = tf.expand_dims(query, 1)
    # score shape == (batch_size, max_length, 1)
    # we get 1 at the last axis because we are applying score to self.V
    # the shape of the tensor before applying self.V is (batch_size, max_length, units)
    print("2")
    score = self.V(tf.nn.tanh(
        self.W1(query_with_time_axis) + self.W2(values)))
    print("3")

    # attention_weights shape == (batch_size, max_length, 1)
    attention_weights = tf.nn.softmax(score, axis=1)

    # context_vector shape after sum == (batch_size, hidden_size)
    context_vector = attention_weights * values
    context_vector = tf.reduce_sum(context_vector, axis=1)
    
    return context_vector, attention_weights


class Decoder():
  def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz):
    self.dec_units = dec_units
    self.embedding = tf.keras.layers.Embedding(vocab_size, embedding_dim)
    self.gru = tf.keras.layers.GRU(self.dec_units,
                                   return_sequences=True,
                                   return_state=True,
                                   recurrent_initializer='glorot_uniform')
    self.fc = tf.keras.layers.Dense(vocab_size)

    # used for attention
    self.attention = BahdanauAttention(self.dec_units)

  def __call__(self, x, hidden, enc_output):
    # enc_output shape == (batch_size, max_length, hidden_size)
    context_vector, attention_weights = self.attention(hidden, enc_output)
    
    print(context_vector.shape, 'c_v', attention_weights.shape, "attention_w")

    # x shape after passing through embedding == (batch_size, 1, embedding_dim)
    x = self.embedding(x)

    # x shape after concatenation == (batch_size, 1, embedding_dim + hidden_size)
    print(x.shape, 'xshape', context_vector.shape, 'context')
    expanded_dims = tf.expand_dims(context_vector, 1)
    x = tf.concat([expanded_dims, x], axis=-1)

    # passing the concatenated vector to the GRU
    output, state = self.gru(x)

    # output shape == (batch_size * 1, hidden_size)
    output = tf.reshape(output, (-1, output.shape[2]))

    # output shape == (batch_size, vocab)
    x = self.fc(output)

    return x, state, attention_weights




农夫

您可以将生成的.tflite文件加载到python笔记本中,并传递与Keras模型相同的输入。您必须查看准确的输出,因为在模型转换期间不会损失准确性。如果有问题...在Android操作过程中会出现问题。如果没有,那么一切都会正常。使用Tensorflow指南中的以下代码在Python中运行推理:

import numpy as np
import tensorflow as tf

# Load the TFLite model and allocate tensors.
interpreter = tf.lite.Interpreter(model_path="converted_model.tflite")
interpreter.allocate_tensors()

# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Test the model on random input data.
input_shape = input_details[0]['shape']
input_data = np.array(np.random.random_sample(input_shape), dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)

interpreter.invoke()

# The function `get_tensor()` returns a copy of the tensor data.
# Use `tensor()` in order to get a pointer to the tensor.
output_data = interpreter.get_tensor(output_details[0]['index'])
print(output_data)

祝您编码愉快!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何转换tf2模型,使其在tflite解释器上运行

iOS Firebase ML Kit 简单音频识别“无法为给定模型创建 TFLite 解释器”

由于内部错误,Ubuntu 16.04无法从USB运行

如何在浏览器上使用TFJS运行TFLite模型(AutoML Vision Edge)

Pycharm无法运行远程Python解释器错误

由于出现错误,SQL查询无法在phpMyAdmin上运行

由于解析错误,无法在cmd上运行artisan命令

未处理的异常:PlatformException(无法运行模型,解释器忙,java.lang.RuntimeException:解释器忙于颤振

在本地但在服务器上完美运行:无法加载资源:500(内部服务器错误)

继续收到此错误“无法初始化检测器”。我是否无法正确加载tflite模型?

无法解释的PHP错误(内部服务器错误500)

Usergrid无法运行。内部服务器错误

无法运行 Python 脚本程序错误:/usr/bin/python3^M:错误的解释器:

pip错误:尝试在OS X上运行“ pip install ...”时解释器错误

由于python错误,gdb无法运行

运行python脚本时解释器错误

由于CORS错误,无法将XAMP上运行的AngularJs应用连接到API

由于Ruby版本错误,无法在Cloud9上运行RoR站点

由于设备映射器错误而无法运行Docker容器

由于编译器错误和警告,我无法运行JFrame

无法调用函数在@route函数上创建对象,即使很难在python解释器上运行

由于出现“无法确定Java版本”错误,因此无法在Mac上的新项目上运行grails命令

可能由于路径错误,无法在Windows上安装IntelliJ的Android仿真器

TFLite模型在GPU上溢出,在CPU上正常。内部有什么区别?

由于htaccess 500内部服务器错误

内部错误:无法将子模型映射到完整模型

Apache 在 gcloud 500 内部服务器错误上运行 Flask

使用Docker在gcloud上运行Flask的内部服务器错误

在 Ubuntu 20.04 WSL 上运行 pip3 时如何解决“错误解释器”问题?