【问题标题】:tflite: Make multiple invocations at oncetflite:一次进行多次调用
【发布时间】:2021-05-25 07:49:55
【问题描述】:

假设一个网页中多个用户同时发送一张图片进行推理。一种选择是通过在函数内加载每个推理调用来加载 tflite 模型

def detect_from_image(image_path):
    # load model
    interpreter = tf.lite.Interpreter(model_path="detect.tflite")
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()

    img = cv2.imread(image_path)

    # set input tensor
    interpreter.set_tensor(input_details[0]['index'], img)

    # run
    interpreter.invoke()

    # get outpu tensor
    boxes = interpreter.get_tensor(output_details[0]['index'])

上面的缺点是模型是为每个调用单独加载的。

在函数外加载模型

# load model
interpreter = tf.lite.Interpreter(model_path="detect.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

 
def predict(image_path):
    img = cv2.imread(image_path)

    # set input tensor
    interpreter.set_tensor(input_details[0]['index'], img)

    # run
    interpreter.invoke()

    # get outpu tensor
    boxes = interpreter.get_tensor(output_details[0]['index'])

当模型在上述函数之外加载时,当多个调用同时进行时,它似乎不起作用,因为interpreter.set_tensor 和invoke() 没有产生任何输出而是在内部替换张量。

是否有任何类似下面的功能使其适用于并行调用?

def predict(image_path):
        img = cv2.imread(image_path)
    
        # set input tensor
        new_tensor = interpreter.set_tensor(input_details[0]['index'], img)
    
        # run
        output = interpreter.invoke(new_tensor)
    
        # get outpu tensor
        boxes = output.get_tensor(output_details[0]['index'])

【问题讨论】:

    标签: python tensorflow tensorflow-lite


    【解决方案1】:

    请考虑使用一个池来存储 TFLite 解释器实例并为每个请求提取一个实例。 TFLite 解释器 API 不保证多线程支持。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多