验证onnx格式的yolox

  • 转onnx时decode_in_inference是False,所以需要decode_outputs函数
  • 需要注意的是,yolox的Exp中input_size和test_size是先高后宽,即(h,w)
import onnxruntime   
import cv2, sys, torch, torchvision
import numpy as np

cls_num=3
conf_th=0.35
nms_th=0.45
model_w=640
model_h=352
ow=model_w/32
oh=model_h/32

def decode_outputs(outputs):
        grids = []
        strides = []
        dtype=outputs.type()
        for (hsize, wsize), stride in zip([(oh*4,ow*4),(oh*2,ow*2),(oh,ow)], [8, 16, 32]):
            yv, xv = torch.meshgrid([torch.arange(hsize), torch.arange(wsize)])
            grid = torch.stack((xv, yv), 2).view(1, -1, 2)
            grids.append(grid)
            shape = grid.shape[:2]
            strides.append(torch.full((*shape, 1), stride))

        grids = torch.cat(grids, dim=1).type(dtype)
        strides = torch.cat(strides, dim=1).type(dtype)

        outputs[..., :2] = (outputs[..., :2] + grids) * strides
        outputs[..., 2:4] = torch.exp(outputs[..., 2:4]) * strides
        return outputs

session = onnxruntime.InferenceSession('demo.onnx', None)
input_name = session.get_inputs()[0].name

path='/home/lwd/00000.jpg'
raw=cv2.imread(path)
print(raw.shape)
ih, iw, _ = raw.shape
im=cv2.resize(raw, (model_w, model_h))
print(im.shape)
im=im[..., ::-1]
im=im.astype('float32')/255.0
im-=(0.485, 0.456, 0.406)
im/=(0.229, 0.224, 0.225)
im=im.transpose((2,0,1))
im=np.ascontiguousarray(im, dtype=np.float32)
im=np.expand_dims(im, 0)
print(im.shape)
outputs = session.run(None, {input_name: im})
for i in outputs: print(i.shape)
# post
prediction=torch.from_numpy(outputs[0])
prediction=decode_outputs(prediction)
box_corner = prediction.new(prediction.shape)
box_corner[:, :, 0] = prediction[:, :, 0] - prediction[:, :, 2] / 2
box_corner[:, :, 1] = prediction[:, :, 1] - prediction[:, :, 3] / 2
box_corner[:, :, 2] = prediction[:, :, 0] + prediction[:, :, 2] / 2
box_corner[:, :, 3] = prediction[:, :, 1] + prediction[:, :, 3] / 2
prediction[:, :, :4] = box_corner[:, :, :4]
image_pred=prediction[0]
class_conf, class_pred = torch.max(image_pred[:, 5: 5 + cls_num], 1, keepdim=True)
conf_mask = (image_pred[:, 4] * class_conf.squeeze() >= conf_th).squeeze()
print(conf_mask.sum())
detections = torch.cat((image_pred[:, :5], class_conf, class_pred.float()), 1)
detections = detections[conf_mask]
if not detections.size(0): sys.exit()
nms_out_index = torchvision.ops.batched_nms(
            detections[:, :4],
            detections[:, 4] * detections[:, 5],
            detections[:, 6],
            nms_th,
)
detections = detections[nms_out_index]
print(detections.shape)
res=detections.numpy()
print(res)
res[:,0]=res[:,0]*iw/model_w
res[:,1]=res[:,1]*ih/model_h
res[:,2]=res[:,2]*iw/model_w
res[:,3]=res[:,3]*ih/model_h
for i in res:
	x1=int(i[0])
	y1=int(i[1])
	x2=int(i[2])
	y2=int(i[3])
	cv2.rectangle(raw, (x1,y1), (x2,y2), (0,0,255))
cv2.imshow('ss', raw)
cv2.waitKey()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刀么克瑟拉莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值