简体   繁体   English

转换后 TFLite model 中缺少层

[英]Missing layer in TFLite model after conversion

So I trained a model using tensorflow and python and now I'm trying to use it in a C++ program.因此,我使用 tensorflow 和 python 训练了 model,现在我正在尝试在 ZF6F87C9FDCF18B3C3FC9Z77F3 程序中使用它。 I use this code to convert my model to tflite (I don't have any error during the conversion):我使用此代码将我的 model 转换为 tflite(我在转换过程中没有任何错误):

model.load_weights('training_weights.h5', by_name=True)
model.save('saved_model/model')

converter = tf.lite.TFLiteConverter.from_saved_model("saved_model/model")
tflite_model = converter.convert()
with tf.io.gfile.GFile('model.tflite', 'wb') as f:
    f.write(tflite_model)

Then I load the model in C++ and I try to use it but the output doesn't match the output of my network.然后我将 model 加载到 C++ 中并尝试使用它,但是 output 与 myDB31 的 Z78E6221F639ZF41 网络不匹配。 The last layers of the network, in python, are:网络的最后一层,在 python 中,是:

X = Lambda(lambda x: K.expand_dims(x, axis=2), name='deconv_expand_dim')(input_tensor)
X = Conv2DTranspose(filters, (kernel_size, 1), strides=(strides, 1), padding=padding, 
                    activation=activation, kernel_initializer=kernel_initializer,
                    bias_initializer=bias_initializer, name='deconv')(X)
X = Lambda(lambda x: K.squeeze(x, axis=2), name='deconv_reduce_dim')(X)

and the last layers of the model loaded, in C++, are:在 C++ 中加载的 model 的最后一层是:

1227 model/deconv_expand_dim/ExpandDims;StatefulPartitionedCall/model/deconv_expand_dim/ExpandDims
1228 model/deconv/Shape;StatefulPartitionedCall/model/deconv/Shape
1229 model/deconv/strided_slice;StatefulPartitionedCall/model/deconv/strided_slice1
1230 model/deconv/strided_slice_1;StatefulPartitionedCall/model/deconv/strided_slice_1
1231 model/deconv/strided_slice_2;StatefulPartitionedCall/model/deconv/strided_slice_22
1232 model/deconv/stack;StatefulPartitionedCall/model/deconv/stack
1233 model/deconv/conv2d_transpose;StatefulPartitionedCall/model/deconv/conv2d_transpose1
1234 model/deconv/BiasAdd;StatefulPartitionedCall/model/deconv/BiasAdd
1235 Identity

I just did a for loop to interpreter->tensors_size() to list the layers.我刚刚做了一个 for 循环解释器->tensors_size()来列出图层。 Problems:问题:

  • interpreter->tensors_size() returns a greater number that varies for one test to another (around 1300). interpreter->tensors_size()返回一个更大的数字,该数字因一个测试而异于另一个测试(大约 1300)。
  • interpreter->outputs()[0] returns the index of Identity (1235). interpreter->outputs()[0]返回 Identity 的索引 (1235)。
  • I have, in C++, the layers deconv_expand_dim and deconv but I don't have the layer deconv_reduce_dim .我在 C++ 中有deconv_expand_dimdeconv层,但我没有deconv_reduce_dim层。
  • interpreter->tensor(outputIndex)->dims->size is equal to 0, which is problematic since this is the output of the network. interpreter->tensor(outputIndex)->dims->size等于 0,这是有问题的,因为这是网络的 output。 Did I miss a step during the conversion?我是否在转换过程中错过了一个步骤? How do I get a valid output?如何获得有效的 output?

Any help is greatly appreciated, thanks.非常感谢任何帮助,谢谢。

I think I found the reason why it doesn't work.我想我找到了它不起作用的原因。 As you can see in the last layer I use the operator:正如您在最后一层中看到的那样,我使用了运算符:

K.squeeze(x, axis=2)

which seems to not be supported yet with a specified axis ("tf.squeeze — As long as axis is not provided.", last-accessed:28 July 2020, link ).似乎尚不支持指定的轴(“tf.squeeze - 只要未提供轴。”,上次访问时间:2020 年 7 月 28 日, 链接)。

That is why I don't have the same output in C++ as in Python, Tensorflow Lite doesn't support my network for the moment. That is why I don't have the same output in C++ as in Python, Tensorflow Lite doesn't support my network for the moment.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM