tf serving部署 pytorch onnx

tf serving部署

转onnx再pb再tf serving使用
a、转onnx

import torch
import torchvision
import torch.onnx
import torch.nn as nn
import clip

device = "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
text1 = clip.tokenize(["hello"]).to(device)
print(text1)
print(type(text1))

# 加载
model_txt = torch.load('./single_model_text1.pkl')

torch.onnx.export(model_txt, text1, "./single_model_text.onnx")



b、onnx再转pb

tf2版本里,安装环境:

pip install onnx onnx_tf

pip install -U tensorflow-probability

导出代码:

import onnx
import numpy as np
from onnx_tf.backend import prepare

model = onnx.load(r'aaa_simp.onnx')
tf_model = prepare(model)
tf_model.export_graph(r'.\1')

报错:

No module named 'tensorflow_probability'

解决方法:

pip install -U tensorflow-probability

然后导出测试ok,会自动创建1目录并导出pb文件。



原文链接:https://blog.csdn.net/weixin_42357472/article/details/118491846

tf加载pb文件,是1.x版本,不是2.x版本:

def load_pb_model(sess, save_path):
    with tf.gfile.FastGFile(save_path + 'model.pb', 'rb') as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())
        sess.graph.as_default()
        tf.import_graph_def(graph_def, name='')  # 导入计算图
————————————————
版权声明:本文为CSDN博主「魔法攻城狮MRL」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_41959920/article/details/115737188

猜你喜欢

转载自blog.csdn.net/jacke121/article/details/126801375