TensorFlow Lite 模型与标准 TensorFlow 的区别

3
我正在开发一个Tensorflow简单应用程序(希望能够检测捕获图像中是否有人)。
我熟悉Tensorflow的Python接口。我注意到Tensorflow Lite有不同的缩减格式。
我有兴趣在传统的Tensorflow Python程序中使用Tensorflow Lite示例中链接的模型(因为不想花时间创建自己的模型),并且使用基于PC的GPU。

https://www.tensorflow.org/lite/models/image_classification/overview

你能否帮忙翻译这个?

当我运行以下代码时,我收到了以下信息:

import tensorflow as tf
def load_pb(path_to_pb):
    with tf.gfile.GFile(path_to_pb, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
     with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name='')
        return graph

 load_pb('detect.tflite')

main.py:5: RuntimeWarning: 意外的结束组标签:并非所有数据都被转换 graph_def.ParseFromString(f.read())


1
答案解决了您的问题吗? - Maximilian Peters
谢谢。我还在努力理解它,然后再接受。看起来他们把解释器从tf.contrib移动到了tf.life。 - ffejrekaburb
1个回答

2
你可以按照Tensorflow文档提供的示例进行操作。tflite模型和标签可以从这里获取。代码可以在普通桌面电脑上运行。"Original Answer"的翻译是"最初的回答"。
import tensorflow as tf
import requests
import io
from PIL import Image
import numpy as np

# load model
interpreter = tf.contrib.lite.Interpreter(model_path="mobilenet_v1_1.0_224_quant.tflite")
interpreter.allocate_tensors()

# get details of model
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# load an image
r = requests.get('https://www.tensorflow.org/lite/models/image_classification/images/dog.png')

# convert the image RGB, see input_details[0].shape
img = Image.open(io.BytesIO(r.content)).convert('RGB')

# resize the image and convert it to a Numpy array
img_data = np.array(img.resize(input_details[0]['shape'][1:3]))

# run the model on the image
interpreter.set_tensor(input_details[0]['index'], [img_data])
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])

# get the labels
with open('labels_mobilenet_quant_v1_224.txt') as f:
    labels = f.readlines()

print(labels[np.argmax(output_data[0])])

西高地白梗

图片描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接