当前位置:网站首页>tensorflow2-savedmodel convert to tflite
tensorflow2-savedmodel convert to tflite
2022-07-01 14:37:00 【哗啦呼啦嘿】
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)边栏推荐
- Opencv interpolation mode
- [IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system
- Today, with the popularity of micro services, how does service mesh exist?
- Research Report on the development trend and competitive strategy of the global high temperature label industry
- Use of Oracle database objects
- Phpcms realizes the direct Alipay payment function of orders
- Research Report on the development trend and competitive strategy of the global CCTV robot industry
- Research Report on the development trend and competitive strategy of the global aviation leasing service industry
- phpcms实现订单直接支付宝支付功能
- SWT / anr problem - how to capture performance trace
猜你喜欢
![[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system](/img/b2/e8f81ecda6f5f4fc65501aaf9f13cf.gif)
[IOT completion. Part 2] stm32+ smart cloud aiot+ laboratory security monitoring system

Don't want to knock the code? Here comes the chance

C#学习笔记(5)类和继承

Phpcms realizes the direct Alipay payment function of orders

Leetcode (69) -- square root of X

对于编程思想和能力有重大提升的书有哪些?

How will the surging tide of digitalization overturn the future?

一波三折,终于找到src漏洞挖掘的方法了【建议收藏】

What problems should be considered for outdoor LED display?

Blog recommendation | in depth study of message segmentation in pulsar
随机推荐
手把手带你入门 API 开发
sqlilabs less-8
用对场景,事半功倍!TDengine 的窗口查询功能及使用场景全介绍
JVM performance tuning and practical basic theory part II
Oracle-数据库对象的使用
How to view the state-owned enterprises have unloaded Microsoft office and switched to Kingsoft WPS?
Research Report on the development trend and competitive strategy of the global commercial glassware industry
Provincial election + noi Part VIII fraction theory
一波三折,终于找到src漏洞挖掘的方法了【建议收藏】
leetcode622. Design cycle queue (C language)
数据湖系列之一 | 你一定爱读的极简数据平台史,从数据仓库、数据湖到湖仓一体
Research Report on the development trend and competitive strategy of the global axis measurement system industry
Research Report on development trend and competitive strategy of global 4-aminodiphenylamine industry
Generate random numbers (4-bit, 6-bit)
当主程架构游戏的时候,防止到处调用减少耦合性,怎么开放接口给其他人调用呢?
TexStudio使用教程
[Verilog quick start of Niuke series] ~ multi function data processor, calculate the difference between two numbers, use generate... For statement to simplify the code, and use sub modules to realize
MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
Research Report on the development trend and competitive strategy of the global aviation leasing service industry
深度合作 | 涛思数据携手长虹佳华为中国区客户提供 TDengine 强大企业级产品与完善服务保障