Tensorflow Lite tflite模型的生成与导入
tensorflow lite,那么意味着必须要把PC上的模型生成tflite文件,然后在ARM上导入这个tflite文件,通过解析这个文件来进行计算。根据前面所说,
tensorflow的所有计算都会在内部生成一个图,包括变量的初始化,输入定义等,那么即便不是经过训练的神经网络模型,只是简单的三角函数计算,也可以生成一个tflite模型用于在tensorflow lite上导入。所以,这里我就只做了简单的sin()计算来跑一编这个流程。生成tflite模型
这部分主要是调用TFLiteConverter函数,直接生成tflite文件,不再通过pb文件转化。
先上代码:
import numpy as np
import time
import math
import tensorflow as tf SIZE = 1000
X = np.random.rand(SIZE, 1)
X = X*(math.pi/2.0) start = time.time()
x1 = tf.placeholder(tf.float32, [SIZE, 1], name='x1-input')
x2 = tf.placeholder(tf.float32, [SIZE, 1], name='x2-input')
y1 = tf.sin(x1)
y2 = tf.sin(x2)
y = y1*y2 with tf.Session() as sess:
init_op = tf.global_variables_initializer()
sess.run(init_op)
converter = tf.lite.TFLiteConverter.from_session(sess, [x1, x2], [y])
tflite_model = converter.convert()
open("/home/alcht0/share/project/tensorflow-v1.12.0/converted_model.tflite", "wb").write(tflite_model) end = time.time()
print("2nd ", str(end - start))
主要遇到的问题是
tensorflow的变化实在太快,这些个转化函数一直在变。位置也一直在变,现在参考官方文档,是按上面代码中调用,否则就会报找不到lite之类的错误。我现在PC上的tensorflow Python版本是1.13,所以lite已经在contrib外面了,如果是以前的版本,要按文档中下面这样调用。| TensorFlow Version | Python API |
| 1.12 | tf.contrib.lite.TFLiteConverter |
| 1.9-1.11 | tf.contrib.lite.TocoConverter |
| 1.7-1.8 | tf.contrib.lite.toco_convert |
输入参数shape
本来在本文件中为了给定的输入数据大小自由,x1,x2的shape会写成[None, 1],但是如果这样写,转化成tflite模型后会默认为[1,1],并不能自由接收数据大小,所以在这里要指定大小SIZE:
x1 = tf.placeholder(tf.float32, [SIZE, 1], name='x1-input')
导入tflite模型
本来这部分应该是在ARM板子上做的,但是为了验证tflite文件的可用性,我先在PC的Python上试验。先上代码:
import tensorflow as tf
import numpy as np
import math
import time SIZE = 1000
X = np.random.rand(SIZE, 1, ).astype(np.float32)
X = X*(math.pi/2.0) start = time.time() interpreter = tf.lite.Interpreter(model_path="/home/alcht0/share/project/tensorflow-v1.12.0/converted_model.tflite")
interpreter.allocate_tensors() input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details() interpreter.set_tensor(input_details[0]['index'], X)
interpreter.set_tensor(input_details[1]['index'], X) interpreter.invoke() output_data = interpreter.get_tensor(output_details[0]['index'])
end = time.time()
print("1st ", str(end - start))
tflite文件生成解析器,然后用allocate_tensors()分配内存。将输入通过set_tensor传入,然后调用invoke()来真正运行。最后得到输出。用
Python跑的时候可以很清楚的看到input_details的数据结构。官方的例子是只传入一个数据,所以只需要取input_details[0],而我传入了2个输入,所以需要设置2个。同时可以看到input_details的2个数据的名字都是我在之前设置的x1-input和x2-input,这样非常好理解。输入参数类型
这里有个坑是输入参数的类型一定要注意。我在生成模型的时候定义的输入参数类型是tf.float32,而在导入的时候如果直接是X = np.random.rand(SIZE, 1, )的话,会报错:
ValueError: Cannot set tensor: Got tensor of type 0 but expected type 1 for input 3
这里把通过astype(np.float32)把输入参数指定为float32就OK了。
- 操作不支持的坑
可以从前面的代码里看到我写了两个sin(),其实一开始是一个sin()一个cos()的,但是好像默认的tflite模型不支持cos()操作,无法生成,所以我只好暂时先只写sin(),后面再研究怎么把cos()加上。
Tensorflow Lite tflite模型的生成与导入的更多相关文章
- Tensorflow中保存模型时生成的各种文件区别和作用
假如我们得到了如下的checkpoints, 上面的文件主要可以分成三类:一种是在保存模型时生成的文件,一种是我们在使用tensorboard时生成的文件,还有一种就是plugins这个文件夹,这个是 ...
- 移动端目标识别(3)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之Running on mobile with TensorFlow Lite (写的很乱,回头更新一个简洁的版本)
承接移动端目标识别(2) 使用TensorFlow Lite在移动设备上运行 在本节中,我们将向您展示如何使用TensorFlow Lite获得更小的模型,并允许您利用针对移动设备优化 ...
- 移动端目标识别(2)——使用TENSORFLOW LITE将TENSORFLOW模型部署到移动端(SSD)之TF Lite Developer Guide
TF Lite开发人员指南 目录: 1 选择一个模型 使用一个预训练模型 使用自己的数据集重新训练inception-V3,MovileNet 训练自己的模型 2 转换模型格式 转换tf.GraphD ...
- 【tensorflow-v2.0】如何将模型转换成tflite模型
前言 TensorFlow Lite 提供了转换 TensorFlow 模型,并在移动端(mobile).嵌入式(embeded)和物联网(IoT)设备上运行 TensorFlow 模型所需的所有工具 ...
- tensorflow lite 之生成 tflite 模型文件
下载最新的的tensorflow源码. 1.配置 tflite 文件转换所需环境 安装 bazel 编译工具 https://docs.bazel.build/versions/master/inst ...
- 移动端目标识别(1)——使用TensorFlow Lite将tensorflow模型部署到移动端(ssd)之TensorFlow Lite简介
平时工作就是做深度学习,但是深度学习没有落地就是比较虚,目前在移动端或嵌入式端应用的比较实际,也了解到目前主要有 caffe2,腾讯ncnn,tensorflow,因为工作用tensorflow比较多 ...
- object detection模型转换成TensorFlow Lite,在Android应用
环境 tensorflow = 1.12.0 bazel = 0.18.1 ubuntu = 16.04 python = 3.6.2 安装 bazel (0.18.1) 如果tensorflow是1 ...
- TensorFlow Lite demo——就是为嵌入式设备而存在的,底层调用NDK神经网络API,注意其使用的tf model需要转换下,同时提供java和C++ API,无法使用tflite的见后
Introduction to TensorFlow Lite TensorFlow Lite is TensorFlow’s lightweight solution for mobile and ...
- AoE 搭档 TensorFlow Lite ,让终端侧 AI 开发变得更加简单。
AoE( AI on Edge , https://github.com/didi/AoE ) 是滴滴近期开源的终端侧 AI 集成运行时环境 ( IRE ). 随着人工智能技术快速发展,近几年涌现出了 ...
随机推荐
- 未能加载文件或程序集“System.Web.Http.WebHost
http://blog.csdn.net/yuanzhugen/article/details/46625353(转)
- struts2 JSON 插件的使用
1. 导入包: json-lib-2.3-jdk15.jar struts2-json-plugin-2.3.15.3.jar 2. 在struts.xml中修改配置如下: <package n ...
- 第一次把本地项目与git相连
原文:https://blog.csdn.net/a987625922/article/details/82189863 新建远程仓库(github或者gitee) 将本地仓库转换成版本库,并将文件添 ...
- 我说CMMI之四:CMMI的表示方法--转载
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/dylanren/article/deta ...
- 发送动态IP到邮件
# -*-coding:utf8 -*- #!/usr/bin/python import smtplib from email.mime.text import MIMEText # IP impo ...
- puppet使用rsync模块同步目录和文件
puppet使用rsync模块同步目录和文件 2013-09-23 14:28:57 分类: LINUX 环境说明: OS : CentOS5.4 ...
- PHP preg_match正则表达式
行定位符 ^表示开始 $表示结束 preg_match(模式,待搜索的字符串,$matches) 其中matches为可选参数,一旦匹配上,可以返回匹配结果 举个例子: $pattern = '/#\ ...
- 【Linux学习一】命令行CLI、BASH的基本操作
●操作系统的基本结构 操作系统的基本结构通过Kernel(内核)和Shell(壳)构成.常见的Shell分为GUI和CLI GUI 图形方面的shell ------〉windows .mac osC ...
- SQL SERVER 2008 设置字段默认值为当前时间
在某些情况下需要对某条记录添加上时间戳,比如用户注册,需要记录用户的注册时间,在SQL SERVER 2008中可以通过 1. 添加新字段 2. 数据类型设置为smalldatetime 3. 默认值 ...
- [转] SSH两种登录方式(公私钥)解析
转自:https://www.cnblogs.com/hukey/p/6248468.html SSH登录方式主要分为两种: 1. 用户名密码验证方式 说明: (1) 当客户端发起ssh请求,服务器会 ...