Hello, all!
I wrote a script based on official tutorial on deploy object detection pytorch(tvm\tutorials\frontend\deploy_object_detection_pytorch.py). I only changed target to "cuda"
and ctx to tvm.gpu()
. Then I got this error:
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/_ffi/_ctypes/packed_func.py", line 81, in cfun
rv = local_pyfunc(*pyargs)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/relay/op/strategy/generic.py", line 35, in wrapper
return topi_schedule(outs)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/autotvm/task/topi_integration.py", line 235, in wrapper
return topi_schedule(cfg, outs, *args, **kwargs)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/topi/cuda/conv2d_transpose_nchw.py", line 282, in schedule_conv2d_transpose_nchw
traverse_inline(s, outs[0].op, _callback)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/topi/utils.py", line 70, in traverse_inline
_traverse(final_op)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/topi/utils.py", line 67, in _traverse
_traverse(tensor.op)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/topi/utils.py", line 67, in _traverse
_traverse(tensor.op)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/topi/utils.py", line 68, in _traverse
callback(op)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/topi/cuda/conv2d_transpose_nchw.py", line 183, in _callback
cfg.define_split("tile_n", cfg.axis(n), num_outputs=4)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/autotvm/task/space.py", line 687, in axis
return VirtualAxis(var)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/autotvm/task/space.py", line 137, in __init__
self.length = get_const_int(var.dom.extent)
File "/root/code/optimize_object_detection_by_tvm/tvm/python/tvm/autotvm/utils.py", line 164, in get_const_int
raise ValueError("Expect value to be constant int")
ValueError: Expect value to be constant int
Here is my script:
import tvm
from tvm import relay
from tvm import relay
from tvm.runtime.vm import VirtualMachine
from tvm.contrib.download import download
import numpy as np
import cv2
import torch
import torchvision
in_size = 300
input_shape = (1, 3, in_size, in_size)
def do_trace(model, inp):
model_trace = torch.jit.trace(model, inp)
model_trace.eval()
return model_trace
def dict_to_tuple(out_dict):
if "masks" in out_dict.keys():
return out_dict["boxes"], out_dict["scores"], out_dict["labels"], out_dict["masks"]
return out_dict["boxes"], out_dict["scores"], out_dict["labels"]
class TraceWrapper(torch.nn.Module):
def __init__(self, model):
super().__init__()
self.model = model
def forward(self, inp):
out = self.model(inp)
return dict_to_tuple(out[0])
model_func = torchvision.models.detection.maskrcnn_resnet50_fpn
model = TraceWrapper(model_func(pretrained=True))
model.eval()
inp = torch.Tensor(np.random.uniform(0.0, 250.0, size=(1, 3, in_size, in_size)))
with torch.no_grad():
out = model(inp)
script_module = do_trace(model, inp)
img_path = "test_street_small.jpg"
img_url = (
"https://raw.githubusercontent.com/dmlc/web-data/" "master/gluoncv/detection/street_small.jpg"
)
download(img_url, img_path)
img = cv2.imread(img_path).astype("float32")
img = cv2.resize(img, (in_size, in_size))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = np.transpose(img / 255.0, [2, 0, 1])
img = np.expand_dims(img, axis=0)
input_name = "input0"
shape_list = [(input_name, input_shape)]
mod, params = relay.frontend.from_pytorch(script_module, shape_list)
target = "cuda"
with tvm.transform.PassContext(opt_level=3, disabled_pass=["FoldScaleAxis"]):
vm_exec = relay.vm.compile(mod, target=target, params=params)
ctx = tvm.gpu()
vm = VirtualMachine(vm_exec, ctx)
vm.set_input("main", **{input_name: img})
tvm_res = vm.run()
score_threshold = 0.9
boxes = tvm_res[0].asnumpy().tolist()
valid_boxes = []
for i, score in enumerate(tvm_res[1].asnumpy().tolist()):
if score > score_threshold:
valid_boxes.append(boxes[i])
else:
break
print("Get {} valid boxes".format(len(valid_boxes)))
How could I fix this? Thanks!