当前位置:网站首页>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)边栏推荐
- 2022-2-15 learning the imitation Niuke project - post in Section 2
- Phpcms realizes the direct Alipay payment function of orders
- 使用net core 6 c# 的 NPOI 包,讀取excel..xlsx單元格內的圖片,並存儲到指定服務器
- 问题随记 —— Oracle 11g 卸载
- Provincial election + noi Part XI others
- MySQL log
- SWT/ANR问题--当发送ANR/SWT时候如何打开binder trace(BinderTraces)
- Using CMD to repair and recover virus infected files
- Provincial election + noi Part VIII fraction theory
- JVM performance tuning and practical basic theory part II
猜你喜欢

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

TDengine 连接器上线 Google Data Studio 应用商店

【14. 区间和(离散化)】

So programmers make so much money doing private work? It's really delicious

Guess lantern riddles, not programmers still can't understand?

241. Design priorities for operational expressions

MIT团队使用图神经网络,加速无定形聚合物电解质筛选,促进下一代锂电池技术开发
![[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

How can we protect our passwords?

What problems should be considered for outdoor LED display?
随机推荐
Research Report on the development trend and competitive strategy of the global powder filling machine industry
30 Devops interview questions and answers
Minimum spanning tree and bipartite graph in graph theory (acwing template)
241. 为运算表达式设计优先级
Play with grpc - communication between different programming languages
TexStudio使用教程
sqlilabs less-8
Research Report on the development trend and competitive strategy of the global camera filter bracket industry
Develop small programs and official account from zero [phase III]
问题随记 —— Oracle 11g 卸载
Sqlachemy common operations
Play with mongodb - build a mongodb cluster
Logic is a good thing
241. Design priorities for operational expressions
Is it reasonable and safe for securities companies to open accounts for 10000 free securities? How to say
手把手带你入门 API 开发
Why did you win the first Taosi culture award of 20000 RMB if you are neither a top R & D expert nor a sales Daniel?
【R语言数据科学】:机器学习常见评估指标
C 语言基础
About the use of HTTP cache validation last modified and Etag