在近期工作中,采用TensorFlow Lite将ssd_mobilenet目标检测模型移植安卓机上。从安卓机测试的效果来看,非量化的模型每帧图像推理的速率较慢。为压缩模型提升推理速度,采用了减少模型深度的方法。具体可参考之前一篇模型压缩文章。
目前新版本TensorFlow发布了TensorFlow Lite可支持GPU,参考官方TensorFlow Lite GPU实现教程。
迫不及待按照教程实现了下(安卓机):
需要 OpenGL ES 3.1 及以上版本
setp1:下载TensorFlow源码
git clone https://github.com/tensorflow/tensorflow
Android Studio中打开下列路径
step2:修改app/build.gradle文件(根据个人情况修改sdk版本和dependencies)
dependencies {
...
// implementation 'org.tensorflow:tensorflow-lite:0.0.0-nightly'
implementation 'org.tensorflow:tensorflow-lite:0.0.0-gpu-experimental'
}
step3:build and run
编译后直接运行即可。
结果:
可以直接选择GPU和CPU,量化后的模型选择GPU会提示gpu requires float model(官方文档没认真看,难道不支持量化模型)
非量化模型由于我手机配置很差,选择GPU会报错闪退,在其他安卓机上尝试后可以正常运行,但检测速率GPU的大概也就快了一倍,没有想象的那么快(还在研究是什么原因)。
借用官方的图: