-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4.compile_save_lib.py
executable file
·63 lines (46 loc) · 1.47 KB
/
4.compile_save_lib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/env python
import tvm
from tvm import relay
from module import *
import numpy as np
import torch
import shutil
import torchvision
from torchvision import transforms as tfs
from torch.utils.tensorboard import SummaryWriter
from tvm.contrib import graph_executor
from tvm.contrib import utils
dev = tvm.cpu(0)
my_module = torch.load("my_module_0.pth")
my_module.eval()
random_input = torch.randn([1, 1, 29, 29])
scripted_module = torch.jit.trace(my_module, random_input).eval()
input_name = "input0"
img = torchvision.io.read_image("img_fortest/2.jpg", mode = torchvision.io.image.ImageReadMode.GRAY)
img = tfs.functional.resize(img, (29, 29))
img = img.reshape((-1, 1, 29, 29))
img = tfs.functional.autocontrast(img)
img = tfs.functional.convert_image_dtype(img, dtype = torch.float32)
shape_list = [(input_name, img.shape)]
mod, params = relay.frontend.from_pytorch(scripted_module, shape_list)
print(mod.astext(show_meta_data = False))
#mod.summary()
#exit()
target = tvm.target.Target("llvm", host = "llvm")
dev = tvm.cpu(0)
with tvm.transform.PassContext(opt_level = 3):
lib = relay.build(mod, target = target, params = params)
m = graph_executor.GraphModule(lib["default"](dev))
# Set inputs
m.set_input(input_name, img)
# Execute
m.run()
# Get outputs
tvm_output = m.get_output(0)
print(tvm_output)
print(np.argmax(tvm_output.numpy()[0]))
temp = utils.tempdir()
path_lib = temp.relpath("my_module.tar")
#print(path_lib)
lib.export_library("my_module.tar")
#print(temp.listdir())