当前位置:网站首页>tensorflow2-savedmodel convert to tflite
tensorflow2-savedmodel convert to tflite
2022-07-01 14:41:00 【Hula Hula hey】
import tensorflow as tf
from utils.eval_utils import show_box,show_multibox
from utils.anchor_utils import generate_anchors, from_offset_to_box
import os
import cv2
import config as c
import numpy as np
import copy
from utils.aug_utils import color_normalize
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
def pb_to_tflite(saved_model_path,tflite_path):
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir=saved_model_path)
converter.target_spec.support_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
with open(tflite_path, 'wb') as g:
g.write(tflite_model)
def load_tflite(tflite_path,img_path):
img = cv2.imread(img_path)
height, width, _ = np.shape(img)
img_batch = np.array([color_normalize(cv2.resize(copy.copy(img), tuple(c.input_shape[:2])))], dtype=np.float32)
# load model
interpreter = tf.lite.Interpreter(model_path=tflite_path)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# print(output_details)
# inference
interpreter.set_tensor(input_details[0]['index'], img_batch)
interpreter.invoke()
# output vector
loc_pred = interpreter.get_tensor(output_details[0]['index'])
print(loc_pred.shape) #(1, 8732, 4)
cls_pred = interpreter.get_tensor(output_details[1]['index'])
print(cls_pred.shape) #(1, 8732, 5)
anchors = generate_anchors()
boxes, scores, labels = from_offset_to_box(loc_pred[0], cls_pred[0], anchors,
anchor_belongs_to_one_class=True, score_threshold=0.1)
print(boxes, scores, labels)
boxes_new = []
score_new = []
label_new = []
for box, score, label in zip(boxes, scores, labels):
box[0] = box[0] / c.input_shape[1] * width # left
box[1] = box[1] / c.input_shape[0] * height # top
box[2] = box[2] / c.input_shape[1] * width # right
box[3] = box[3] / c.input_shape[0] * height # bottom
print('image: {}\nclass: {}\nconfidence: {:.4f}\n'.format(img_path, c.class_list[label], score))
boxes_new.append(box)
score_new.append(score)
label_new.append(c.class_list[label])
show_multibox(img, boxes_new, score_new, label_new)
tflite_path='/data1/gyx/QR/SSD_Tensorflow2.0-master/convert/tflite/ssd.tflite'
saved_model_path= "/result/weight/ssd_vgg/pb/"
img_path='/data1/gyx/QR/SSD_Tensorflow2.0-master/test_pic/image_1636168791399.jpg'
pb_to_tflite(saved_model_path,tflite_path)
load_tflite(tflite_path,img_path)边栏推荐
- C#学习笔记(5)类和继承
- Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
- Pat 1121 damn single (25 points) set
- sqlilabs less10
- 【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
- [dynamic programming] interval dp:p1005 matrix retrieval
- NPDP能给产品经理带来什么价值?你都知道了吗?
- 手把手带你入门 API 开发
- 【14. 区间和(离散化)】
- 【R语言数据科学】:机器学习常见评估指标
猜你喜欢

如何看待国企纷纷卸载微软Office改用金山WPS?

用对场景,事半功倍!TDengine 的窗口查询功能及使用场景全介绍

241. 为运算表达式设计优先级

Use the right scene, get twice the result with half the effort! Full introduction to the window query function and usage scenarios of tdengine

sqlilabs less-11~12
![[repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system](/img/fa/15b1cc3a8a723ff34eb457af9f701e.jpg)
[repair version] imitating the template of I love watching movies website / template of ocean CMS film and television system

Today, with the popularity of micro services, how does service mesh exist?

phpcms实现订单直接支付宝支付功能

One of the data Lake series | you must love to read the history of minimalist data platforms, from data warehouse, data lake to Lake warehouse

What problems should be considered for outdoor LED display?
随机推荐
Research Report on the development trend and competitive strategy of the global axis measurement system industry
Vnctf2022 open web gocalc0
【商业终端仿真解决方案】上海道宁为您带来Georgia介绍、试用、教程
Leetcode (69) -- square root of X
[15. Interval consolidation]
期末琐碎知识点再整理
Develop small programs and official account from zero [phase III]
sqlilabs less-11~12
In depth cooperation | Taosi data cooperates with changhongjia Huawei customers in China to provide tdengine with powerful enterprise level products and perfect service guarantee
NPDP能给产品经理带来什么价值?你都知道了吗?
30 Devops interview questions and answers
Minimum spanning tree and bipartite graph in graph theory (acwing template)
对于编程思想和能力有重大提升的书有哪些?
sqlilabs less13
[leetcode 324] 摆动排序 II 思维+排序
Guess lantern riddles, not programmers still can't understand?
Basic operation of queue (implemented in C language)
关于软件测试的一些思考
C learning notes (5) class and inheritance
En utilisant le paquet npoi de net Core 6 c #, lisez Excel.. Image dans la cellule xlsx et stockée sur le serveur spécifié