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 ). 随着人工智能技术快速发展,近几年涌现出了 ...
随机推荐
- 浙大数据结构课后习题 练习二 7-2 Reversing Linked List (25 分)
Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elem ...
- Eclipse设置模板codetemplates
在Window->Preferences->Java->Code Style->Code Templates,点击"Import",导入模板codetemp ...
- 【LOJ 6695】天气之子
找规律题的典范? OEIS裸题 考场上让你用 OEIS 吗 题意 link 题解 \(n\le 5\) 打表 \(n\le 10^5\) 发现不能直接求最优解,于是二分答案. 验证答案时,先把前 \( ...
- RWD(Responsive Web Design)(转)
The key point is adapting to the user’s needs and device capabilities. Suppose a mobile user will be ...
- Protobuffer教程
目录 什么是protobuffer? protobuffer是如何工作的? 为什么不用xml? 1.什么是protobuffer? protobuffer是一种灵活,高效,自动化的机制,用于序列化结构 ...
- 野生小白纯js仿思否简易移动端
初衷 感谢思否两个多月来的陪伴做这个东西还是多自己两个月多来的学习总结吧,顺带练手.希望能找到一个还可以的工作吧! 为什么没用框架 react过了一边官方文档,对状态提升和组件有点了解一下,懂得还是太 ...
- Spring中 aop的 xml配置(简单示例)
示例: aop,即面向切面编程,面向切面编程的目标就是分离关注点. 比如:小明(一位孩子)想吃苹果,首先得要有苹果,其次才能吃.那么妈妈负责去买水果,孩子负责吃,这样,既分离了关注点,也减低了代码的复 ...
- golang之运算符
目录 一.golang之运算符 1. 算术运算符 2. 关系运算符 3. 逻辑运算符 4. 位运算符 5. 赋值运算符 一.golang之运算符 Go 语言内置的运算符有:(比python少了一个成员 ...
- buuctf@warmup_csaw_2016
from pwn import * io=remote('node3.buuoj.cn',27774) io.recvuntil('WOW:') addr=(io.recvuntil('\n')[:- ...
- (转)window.open和window.showModalDialog的区别
window.open和window.showModalDialog区别: 1.都是在IE上打开新窗口,只不过前者是非阻塞式,也可以说非模态窗口.而后者是阻塞式模态窗口.阻塞或者模态窗口,只有你把当前 ...