当主机具有多个核心时,我无法在docker中的模型上运行推理。通过PyTorch 1.0 ONNX导出器导出模型:
torch.onnx.export(pytorch_net, dummyseq, ONNX_MODEL_PATH)
Run Code Online (Sandbox Code Playgroud)
使用单核启动模型服务器(包装在Flask中)可获得可接受的性能(cpuset将进程固定到特定的cpus) docker run --rm -p 8081:8080 --cpus 0.5 --cpuset-cpus 0 my_container
来自的回应 ab -c 1 -n 1000 http://0.0.0.0:8081/predict\?itemids\=5,100
Percentage of the requests served within a certain time (ms)
50% 5
66% 5
75% 5
80% 5
90% 7
95% 46
98% 48
99% 49
Run Code Online (Sandbox Code Playgroud)
但是,将其固定到四个内核,对于相同的ab-call来说,将提供完全不同的统计信息 docker run --rm -p 8081:8080 --cpus 0.5 --cpuset-cpus 0,1,2,3 my_container
Percentage of the requests served within a certain time (ms)
50% 9
66% 12
75% 14
80% 18
90% …
Run Code Online (Sandbox Code Playgroud) C = torch.cat((A,B),1)
Run Code Online (Sandbox Code Playgroud)
张量的形状:
A is (1, 128, 128, 256)
B is (1, 1, 128, 256)
Run Code Online (Sandbox Code Playgroud)
预期C
值为(1, 129, 128, 256)
这段代码可以在pytorch上运行,但是在转换为core-ml时会出现以下错误:
"Error while converting op of type: {}. Error message: {}\n".format(node.op_type, err_message, )
TypeError: Error while converting op of type: Concat. Error message: unable to translate constant array shape to CoreML shape"
Run Code Online (Sandbox Code Playgroud) 我尝试将我的 pytorch Resnet50 模型转换为 ONNX 并进行推理。转换程序没有错误,但是onnxruntime的onnx模型的最终结果与pytorch的origin模型的结果有很大差距。
可能的解决方案是什么?
ONNX版本:1.5.0
pytorch版本:1.1.0
CUDA:9.0
系统:Ubuntu 18.06
Python:3.5
这是转换代码
import torch
import models
from collections import OrderedDict
state_dict = "/home/yx-wan/newhome/workspace/filter-pruning-geometric-median/scripts/snapshots/resnet50-rate-0.7/best.resnet50.GM_0.7_76.82.pth.tar"
arch = 'resnet50'
def import_sparse(model,state_dict):
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = k[7:] # remove `module.`
new_state_dict[name] = v
model.load_state_dict(new_state_dict)
print("sparse_model_loaded")
return model
# initialize model
model = models.__dict__[arch](pretrained=False).cuda()
checkpoint = torch.load(state_dict)
model = import_sparse(model, checkpoint['state_dict'])
print("Top 1 precise of model: {}".format(checkpoint['best_prec1']))
dummy_input =torch.randn(1, 3, 224, 224).cuda()
torch.onnx.export(model, dummy_input, "{}.onnx".format(arch), …
Run Code Online (Sandbox Code Playgroud) 如何获取onnx神经网络输出层的维度?
我可以得到 onnx 图,但没有输出尺寸:
~/onnx-tensorrt/third_party/onnx/onnx/tools/net_drawer.py --input ./weights/tiny_3l_v5_11_608.onnx --output ./weights/tiny_3l_v5_11_608.dot --embed_docstring
Run Code Online (Sandbox Code Playgroud)
特尔维辛,马库斯
我正在尝试使用 tf-serving 来部署我的火炬模型。我已将我的火炬模型导出到 onnx。如何为 tf-serving 生成 pb 模型?
尝试使用 ONNX 转换此pytorch 模型会出现此错误。我搜索过 github,这个错误之前在 1.1.0 版本中出现过,但显然已得到纠正。现在我使用的是 torch 1.4.0。(python 3.6.9),我看到这个错误。
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/init.py", line 148, in export
strip_doc_string, dynamic_axes, keep_initializers_as_inputs)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 66, in export
dynamic_axes=dynamic_axes, keep_initializers_as_inputs=keep_initializers_as_inputs)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 416, in _export
fixed_batch_size=fixed_batch_size)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 296, in _model_to_graph
fixed_batch_size=fixed_batch_size, params_dict=params_dict)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 135, in _optimize_graph
graph = torch._C._jit_pass_onnx(graph, operator_export_type)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/init.py", line 179, in _run_symbolic_function
return utils._run_symbolic_function(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py", line 657, in _run_symbolic_function
return op_fn(g, *inputs, **attrs)
File "/usr/local/lib/python3.6/dist-packages/torch/onnx/symbolic_helper.py", line 128, in wrapper
args …
Run Code Online (Sandbox Code Playgroud) 我正在尝试在全新的 Ubuntu 20.04 安装中安装 onnx,但遇到了以下错误。看来我需要 protobuf 编译器,尽管我没有找到任何说明我需要安装该软件包的文档。
\n\n$ sudo python3 -m pip install \'onnx>=1.1.2\'\nCollecting onnx>=1.1.2\n Using cached onnx-1.6.0.tar.gz (3.1 MB)\nRequirement already satisfied: numpy in /usr/local/lib/python3.8/dist-packages (from onnx>=1.1.2) (1.18.3)\nRequirement already satisfied: protobuf in /usr/lib/python3/dist-packages (from onnx>=1.1.2) (3.6.1)\nRequirement already satisfied: six in /usr/lib/python3/dist-packages (from onnx>=1.1.2) (1.14.0)\nRequirement already satisfied: typing-extensions>=3.6.2.1 in /usr/local/lib/python3.8/dist-packages (from onnx>=1.1.2) (3.7.4.2)\nBuilding wheels for collected packages: onnx\n Building wheel for onnx (setup.py) ... error\n ERROR: Command errored out with exit status 1:\n command: /usr/bin/python3 -u -c \'import sys, setuptools, …
Run Code Online (Sandbox Code Playgroud) 我目前正在 Yolov4 上使用 Darknet,有 1 个班级。
我需要将这些权重导出为 onnx 格式,以进行 tensorRT 推理。我尝试了多种技术,使用ultralytics来转换或从 tensorflow 转换为 onnx。但似乎都不起作用。有直接的方法吗?
我找不到任何人向外行解释如何将 onnx 模型加载到 python 脚本中,然后在输入图像时使用该模型进行预测。我能找到的只是这些代码行:
sess = rt.InferenceSession("onnx_model.onnx")
input_name = sess.get_inputs()[0].name
label_name = sess.get_outputs()[0].name
pred = sess.run([label_name], {input_name: X.astype(np.float32)})[0]
Run Code Online (Sandbox Code Playgroud)
但我不知道这意味着什么。无论我看到哪里,每个人似乎都已经知道它们的意思,所以没有人解释它。如果我可以运行这段代码,那将是一回事,但我不能。它给了我这个错误:
onnxruntime.capi.onnxruntime_pybind11_state.InvalidArgument: [ONNXRuntimeError] : 2 : INVALID_ARGUMENT : Invalid rank for input: Input3 Got: 2 Expected: 4 Please fix either the inputs or the model.
Run Code Online (Sandbox Code Playgroud)
所以我需要真正知道这些事情的含义,这样我才能弄清楚如何修复错误。有懂行的请解释一下吗?
我在 PyTorch 中有一个 seq2seq 模型,我想用 CoreML 运行它。将模型导出到 ONNX 时,输入尺寸固定为导出期间使用的张量的形状,并再次从 ONNX 转换为 CoreML。
import torch
from onnx_coreml import convert
x = torch.ones((32, 1, 1000)) # N x C x W
model = Model()
torch.onnx.export(model, x, 'example.onnx')
mlmodel = convert(model='example.onnx', minimum_ios_deployment_target='13')
mlmodel.save('example.mlmodel')
Run Code Online (Sandbox Code Playgroud)
对于 ONNX 导出,您可以导出动态维度 -
torch.onnx.export(
model, x, 'example.onnx',
input_names = ['input'],
output_names = ['output'],
dynamic_axes={
'input' : {0 : 'batch', 2: 'width'},
'output' : {0 : 'batch', 1: 'owidth'},
}
)
Run Code Online (Sandbox Code Playgroud)
但这会导致RunTimeWarning
转换为CoreML
-
RuntimeWarning:您将无法在此 Core ML 模型上运行 …
onnx ×10
pytorch ×5
python ×4
onnx-coreml ×2
python-3.x ×2
caffe2 ×1
coreml ×1
darknet ×1
docker ×1
onnxruntime ×1
pip ×1
tensorflow ×1
tensorrt ×1
ubuntu-20.04 ×1