Open abdulazizm opened 2 years ago
@abdulazizm , I put a fix for the yolact model in https://github.com/Xilinx/pyxir/tree/fix-yolact and used following script. Could you verify whether this works for you? The performance isn't great yet as the the bilinear upsample layers are prohibiting a large part of the model to be offloaded to the DPU. I am verifying whether these could be offloaded to the DPU as well.
import os
import sys
import numpy as np
import cv2
import time
from typing import List
from pathlib import Path
from PIL import Image
import onnx
import pyxir
import pyxir.contrib.target.DPUCADF8H
import logging
import tvm
from tvm import contrib
import tvm.relay as relay
from tvm.relay import transform
from tvm.contrib import utils, graph_executor as graph_runtime
from tvm.contrib.target import vitis_ai
from tvm.relay.build_module import bind_params_by_name
from tvm.relay.op.contrib.vitis_ai import partition_for_vitis_ai
import logging
logging.basicConfig()
logger = logging.getLogger('pyxir')
# logger.setLevel(logging.DEBUG)
logger.setLevel(logging.INFO)
input_name = 'input.1'
input_shape = (1, 3, 550, 550)
shape_dict = {input_name:input_shape}
dpu_target = "DPUCAHX8H-u50lv"
tvm_target = 'llvm'
lib_kwargs = {}
model_path = "yolact_resnet50.onnx"
onnx_model = onnx.load(model_path)
mod, params = relay.frontend.from_onnx(onnx_model, shape_dict)
mod = relay.transform.InferType()(mod)
# import pdb; pdb.set_trace()
mod = partition_for_vitis_ai(mod, params, dpu=dpu_target)
# import pdb; pdb.set_trace()
export_rt_mod_file = os.path.join(os.getcwd(), 'vitis_ai.rtmod')
build_options = {
'dpu': dpu_target,
'export_runtime_module': export_rt_mod_file
}
with tvm.transform.PassContext(opt_level=3, config={'relay.ext.vitis_ai.options': build_options}):
lib = relay.build(mod, tvm_target, params=params)
rt_mod = graph_runtime.GraphModule(lib["default"](tvm.cpu()))
## QUANTIZATION ##
def transform_image(image):
image = np.array(image) - np.array([123., 117., 104.])
image /= np.array([58.395, 57.12, 57.375])
image = image.transpose((2, 0, 1))
image = image[np.newaxis, :]
return image
def inputs_func(img_files: List[str]):
inputs = []
for img_path in img_files:
img = Image.open(img_path)
img = img.convert('RGB')
img = img.resize(input_shape[2:])
inputs.append(transform_image(img))
return inputs
px_quant_size = int(os.environ['PX_QUANT_SIZE']) \
if 'PX_QUANT_SIZE' in os.environ else 128
print("Start OTF Quantization on first {} images".format(px_quant_size))
QUANT_DIR = "./data"
quant_files = [os.path.join(QUANT_DIR, f) for f in os.listdir(QUANT_DIR)
if f.endswith(('JPEG', 'jpg', 'png'))][:px_quant_size]
quant_images = inputs_func(quant_files)
print('Loaded {} inputs successfully.'.format(len(quant_images)))
for i in range(px_quant_size):
rt_mod.set_input(input_name, quant_images[i])
rt_mod.run()
lib.export_library('tvm_dpu_cpu.so')
Related to https://github.com/Xilinx/pyxir/issues/85
Exported Yolact model to ONNX and tried compiling with ONNX frontend
With
mod = relay.transform.DynamicToStatic()(mod)
just beforemod = partition_for_vitis_ai(mod, params, dpu=target)
,FYI: Without
mod = relay.transform.DynamicToStatic()(mod)
beforemod = partition_for_vitis_ai(mod, params, dpu=target)
With pytorch frontend, compilation goes up to 3rd step, but with ONNX frontend struck in 1st step