首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用用于Tensorflow的Edgetpu编译器编译时出错

使用用于Tensorflow的Edgetpu编译器编译时出错
EN

Stack Overflow用户
提问于 2020-06-29 21:39:03
回答 1查看 193关注 0票数 2

我正在尝试将一个两个输出的keras模型转换成一个编译的,量化的,tflite模型,它将在Google Coral上工作。我以前在一个只有1个输出的Keras网络中使用了这个确切的过程,它是有效的。

下面是我的流程:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import tensorflow as tf
from tensorflow.keras.applications.mobilenet import preprocess_input

file = 'path/to/model-01.h5'
model = tf.keras.models.load_model(file)
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]

os.chdir('/path/to/image/directories')#Where image directories are
directory = os.listdir()
directory

def representative_dataset_gen():
    
    for i in directory:
        count = 0
        os.chdir(i)
        files = os.listdir()
        print(i)
        for j in files:
            if count<500:
                img = Image.open(j)
                width, height = img.size
                bands = img.getbands()
                array = np.asarray(img, dtype=np.float32)
                array = preprocess_input(array)
                count=count+1
                yield[np.expand_dims(array, axis=0)]
            else:
                break
        os.chdir('../')

converter.representative_dataset = representative_dataset_gen
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.inference_input_type = tf.int8  # or tf.uint8
converter.inference_output_type = tf.int8  # or tf.uint8
tflite_quant_model = converter.convert()

tflite_model_dir = pathlib.Path('where/i/want/to/save/')
tflite_quant_model_file = tflite_model_dir/'quantized.tflite'
tflite_quant_model_file.write_bytes(tflite_quant_model)

然后,我尝试在终端中使用edgetpu_compiler

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
edgetpu_compiler quantizedmodel.tflite

并收到此错误:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ERROR: :129 std::abs(input_product_scale - bias_scale) <= 1e-6 * std::min(input_product_scale, bias_scale) was not true.
ERROR: Node number 40 (FULLY_CONNECTED) failed to prepare.


Internal compiler error. Aborting! 

在尝试验证模型时尝试interpreter.allocate_tensors()时,我也得到了相同的错误。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#Load Model
interpreter = tf.lite.Interpreter(model_path='path/to/model/quantized.tflite')
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
interpreter.resize_tensor_input(input_details[0]['index'], (32, 200, 200, 3))
interpreter.resize_tensor_input(output_details[0]['index'], (32, 5))
interpreter.allocate_tensors()

它返回

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
RuntimeError                              Traceback (most recent call last)
 in 
      2 interpreter.resize_tensor_input(input_details[0]['index'], (32, 200, 200, 3))
      3 interpreter.resize_tensor_input(output_details[0]['index'], (32, 5))
----> 4 interpreter.allocate_tensors()
      5 

~/Software/anaconda3/envs/Tensorflow2/lib/python3.7/site-packages/tensorflow_core/lite/python/interpreter.py in allocate_tensors(self)
    245   def allocate_tensors(self):
    246     self._ensure_safe()
--> 247     return self._interpreter.AllocateTensors()
    248 
    249   def _safe_to_run(self):

~/Software/anaconda3/envs/Tensorflow2/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: tensorflow/lite/kernels/kernel_util.cc:106 std::abs(input_product_scale - bias_scale) <= 1e-6 * std::min(input_product_scale, bias_scale) was not true.Node number 40 (FULLY_CONNECTED) failed to prepare.

我使用的是tensorflow 2.2.0

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-29 22:25:39

我会为这个打开一个问题here,因为它是tflite量化过程中的一个实际错误。我非常肯定我以前见过这种情况,但不确定是否有解决方法:/

编辑基本上,你可以尝试使用这个脚本进行一个虚拟推理运行,如果在你的CPU模型上失败了,那么很明显模型在tflite转换后被破坏了。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import numpy as np 
import sys
from tflite_runtime.interpreter import Interpreter
from tflite_runtime.interpreter import load_delegate

if len(sys.argv) < 2:
    print('Usage:', sys.argv[0], 'model_path')
    exit()

def main():
    """Runs inference with an input tflite model.""" 
    model_path = str(sys.argv[1])
    if model_path.endswith('edgetpu.tflite'):
        print('initialized for edgetpu')
        delegates = [load_delegate('libedgetpu.so.1.0')]                             
        interpreter = Interpreter(model_path, experimental_delegates=delegates)
    else: 
        print('initialized for cpu')
        interpreter = Interpreter(model_path)

    interpreter.allocate_tensors() 
    input_details = interpreter.get_input_details() 
    images = np.zeros(input_details[0]['shape'], input_details[0]['dtype'])
    #print(images)
    interpreter.set_tensor(input_details[0]['index'], images) 
    interpreter.invoke() 
    output_details = interpreter.get_output_details() 
    outputs = interpreter.get_tensor(output_details[0]['index']) 
    print(outputs)
    print('Success.') 

if __name__== '__main__':
    main()

我在pass中看到了这个问题,但不确定是否有解决方案。打开一个bug实际上是修复这个问题的最好方法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62647070

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文