在线推理的时候如果pc机上没有mlu220,也可以进行在线推理吗,因为我这里只有mlu220的开发板
生成离线模型我看你代码里也是对的,融合之后加上ct.save_as_cambricon('net') out=model_trace(img) ct.save_as_cambricon('')model_trace 是trace函数的返回,你代码里写的quantized_net展开
if __name__ == "__main__": parser = argparse.ArgumentParser() #parser.add_argument('--cfg', type=str, default='yolov5s.yaml', help='model.yaml') parser.add_argument('--device', default='cpu', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--jit', type=bool, help='fusion', default=True) parser.add_argument('--save', type=bool, default=True, help='selection of save *.cambrcion') opt = parser.parse_args() # 获取yolov5网络文件 ct.set_core_version("MLU220") ct.set_device(-1) net = model_quant.RetinexNet(3,3).to(opt.device) quantized_net = torch_mlu.core.mlu_quantize.quantize_dynamic_mlu(net) # 设置为推理模式 quantized_net = quantized_net.eval().float() state_dict = torch.load('net_int8.pt') quantized_net.load_state_dict(state_dict, strict=False) device = ct.mlu_device() quantized_net.to(ct.mlu_device()) # 读取图片 img_mat = cv2.imread('images/000000262682.jpg') # 预处理 img = letter_box(img_mat) # img = torch.randn(1, 3, 512, 512, dtype=torch.float) print(img.shape) img = img.to(ct.mlu_device()) torch.set_grad_enabled(False) model_trace = torch.jit.trace(quantized_net,img,check_trace = False) detect_out = quantized_net(img) # 设置在线融合模式 if opt.jit: if opt.save: ct.save_as_cambricon('net') # torch.set_grad_enabled(False) # ct.set_core_number(4) # trace_input = torch.randn(1, 3, 512, 512, dtype=torch.float) # trace_input = trace_input.to(ct.mlu_device()) # #print(trace_input.to('cpu')) # quantized_net = torch.jit.trace(quantized_net, trace_input, check_trace=False) out = model_trace(img) #print(quantized_net) # 推理 # print(img) # img = img.to(ct.mlu_device()) # print(img.shape) # detect_out = quantized_net(img) if opt.jit: if opt.save: ct.save_as_cambricon('') # 后处理 # anchors = [10, 13, 16, 30, 33, 23,30, 61, 62, 45, 59, 119, 116, 90, 156, 198, 373, 326] print(out) out = out.to(torch.device('cpu')) print(out)
知道问题了,返回list 是因为调用 model_trace = torch.jit.trace(quantized_net,img,check_trace=False)这个函数的问题,这个函数返回值不是quantized_net ,,所以后面跑网络模型也就错了,测试过程中也发现一些其他问题,一起修复了,你参考一下net = model_quant.RetinexNet(3,3).to(opt.device) quantized_net = torch_mlu.core.mlu_quantize.quantize_dynamic_mlu(net) quantized_net = quantized_net.eval().float() state_dict = torch.load('net_int8.pt') quantized_net.load_state_dict((state_dict), strict=False) #quantized_net = quantized_net.eval().float() device = ct.mlu_device() ct.set_core_version("MLU270") ct.set_core_number(4) quantized_net = quantized_net.to(device) img_mat = cv2.imread('images/image.jpg') img = letter_box(img_mat) # img = torch.randn(1, 3, 512, 512, dtype=torch.float) print(img.shape) img = img.to(ct.mlu_device()) torch.set_grad_enabled(False) model_trace = torch.jit.trace(quantized_net,img,check_trace=False) detect_out = quantized_net(img)展开
[图片]net.zip
model_trace = torch.jit.trace(quantized_net,img,check_trace=False)这个函数的问题,这个函数返回值不是quantized_net ,,所以后面跑网络模型也就错了,测试过程中也发现一些其他问题,一起修复了,你参考一下
net = model_quant.RetinexNet(3,3).to(opt.device)
quantized_net = torch_mlu.core.mlu_quantize.quantize_dynamic_mlu(net)
quantized_net = quantized_net.eval().float()
state_dict = torch.load('net_int8.pt')
quantized_net.load_state_dict((state_dict), strict=False)
#quantized_net = quantized_net.eval().float()
device = ct.mlu_device()
ct.set_core_version("MLU270")
ct.set_core_number(4)
quantized_net = quantized_net.to(device)
img_mat = cv2.imread('images/image.jpg')
img = letter_box(img_mat)
# img = torch.randn(1, 3, 512, 512, dtype=torch.float)
print(img.shape)
img = img.to(ct.mlu_device())
torch.set_grad_enabled(False)
model_trace = torch.jit.trace(quantized_net,img,check_trace=False)
detect_out = quantized_net(img)
你好,能把网络模型结构发一下吗?最好能贴一下代码,否则无法复现,定位问题
请登录后评论