MCPcopy Create free account
hub / github.com/alibaba/MNN / run_torchscript

Function run_torchscript

tools/script/torchsscriptTest.py:15–32  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

13 return str(stdout)
14
15def run_torchscript():
16 import torchvision.models as models
17 import onnxruntime as ort
18 import torch
19 resnet18 = models.resnet18(pretrained=True)
20 x = torch.rand(1, 3, 224, 224)
21 resnet18_ts = torch.jit.trace(resnet18, x)
22 resnet18_ts.save(ts_module)
23 torch.onnx.export(resnet18, x, onnx_module)
24 on_module = ort.InferenceSession(onnx_module)
25 inputs = {}
26 for inp in on_module.get_inputs():
27 inputs[inp.name] = x.numpy()
28 y = on_module.run(None, inputs)[0]
29 nx = x.numpy().reshape(-1)
30 ny = y.reshape(-1)
31 np.savetxt(input_file, nx, fmt='%f')
32 np.savetxt(output_file, ny, fmt='%f')
33
34def run_mnn():
35 # convert to mnn module

Callers 1

Calls 4

saveMethod · 0.45
exportMethod · 0.45
runMethod · 0.45
reshapeMethod · 0.45

Tested by

no test coverage detected