默认情况下,TensorFlow不知道如何在已编译的模型上运行自定义运算符,因此使用TensorFlow Lite API将导致为Edge TPU编译的模型失败。要使其工作,您需要对执行推理的代码进行一些更改。此页面显示了如何使用Python进行操作。
如果使用C,请改为阅读"在C中使用TensorFlow Lite执行推理"。
注意:此页面供具有TensorFlow Lite API使用经验的开发人员使用。如果您没有使用TensorFlow的经验并且不准备使用它,则可以改用Edge TPU Python API。这简化了在图像分类和对象检测模型中执行推理所需的代码。
实现了一个TensorFlow Lite委托,以使用Python TensorFlow Lite API在Edge TPU上运行模型。委托是TensorFlow Lite机制,可处理模型图上的某些操作。在这种情况下,委托将处理Edge TPU自定义运算符。
要使用
Edge TPU委托,请完成以下步骤:
如果使用的是USB加速器或M.2 / PCIe加速器,请按以下方式更新Edge TPU运行时:
1 2 3 4 5 6 7 | sudo apt-get update wget https://dl.google.com/coral/edgetpu_api/edgetpu_api_latest.tar.gz -O edgetpu_api.tar.gz --trust-server-names tar xzf edgetpu_api.tar.gz sudo edgetpu_api/install.sh |
如果您正在使用开发板或模块化系统(使用Mendel),请进行以下更新:
1 2 3 | echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list sudo apt-get update sudo apt-get dist-upgrade |
2.确保您使用的是TensorFlow Lite API的最新版本。
打开一个使用TensorFlow Lite Interpreter API进行推理的Python文件(请参见
如果您的
代码从tensorflow包中导入了Interpreter类,则必须使用TensorFlow每晚(1.15)或2.0-beta构建(所需的委托API不包含在TensorFlow 1.14中)。
但是,我们建议您改用
3.构建
例如,您的TensorFlow Lite代码应具有如下一行:
1 | interpreter = Interpreter(model_path) |
更改为:
1 2 | interpreter = Interpreter(model_path, experimental_delegates=[load_delegate('libedgetpu.so.1.0')]) |
在顶部需要另外导入:
1 2 3 4 5 | # If you're using the tflite_runtime package: from tflite_runtime.interpreter import load_delegate # Or if you're using the full TensorFlow package: from tensorflow.lite.python.interpreter import load_delegate |
注意:
就是这样。设置了所有代码并使用针对Edge TPU编译的模型执行推理后,TensorFlow Lite将图形的已编译部分委托给Edge TPU。
在此处获取Google EDGE tpu