1 Star 0 Fork 1

allenzhaoxin / pytorch-YOLOv4

forked from Tianmo / pytorch-YOLOv4 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
demo_onnx.py 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
ersheng 提交于 2020-06-01 14:26 . Preparing for ONNX compatible YOLO layer
import sys
import onnx
import os
import argparse
import numpy as np
import cv2
import onnxruntime
from tool.utils import *
from tool.darknet2onnx import *
def main(cfg_file, weight_file, image_path, batch_size):
# Transform to onnx as specified batch size
fransform_to_onnx(cfg_file, weight_file, batch_size)
# Transform to onnx for demo
onnx_path_demo = fransform_to_onnx(cfg_file, weight_file, 1)
session = onnxruntime.InferenceSession(onnx_path_demo)
# session = onnx.load(onnx_path)
print("The model expects input shape: ", session.get_inputs()[0].shape)
image_src = cv2.imread(image_path)
detect(session, image_src)
def detect(session, image_src):
IN_IMAGE_H = session.get_inputs()[0].shape[2]
IN_IMAGE_W = session.get_inputs()[0].shape[3]
# Input
resized = cv2.resize(image_src, (IN_IMAGE_W, IN_IMAGE_H), interpolation=cv2.INTER_LINEAR)
img_in = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
img_in = np.transpose(img_in, (2, 0, 1)).astype(np.float32)
img_in = np.expand_dims(img_in, axis=0)
img_in /= 255.0
print("Shape of the network input: ", img_in.shape)
# Compute
input_name = session.get_inputs()[0].name
# output, output_exist = session.run(['decoder.output_conv', 'lane_exist.linear2'], {"input.1": image_np})
# print(img_in)
outputs = session.run(None, {input_name: img_in})
print(len(outputs))
print(outputs[0].shape)
print(outputs[1].shape)
print(outputs[2].shape)
# print(outputs[2])
num_classes = 80
boxes = post_processing(img_in, 0.5, num_classes, 0.4, outputs)
if num_classes == 20:
namesfile = 'data/voc.names'
elif num_classes == 80:
namesfile = 'data/coco.names'
else:
namesfile = 'data/names'
class_names = load_class_names(namesfile)
plot_boxes_cv2(image_src, boxes, savename='predictions_onnx.jpg', class_names=class_names)
if __name__ == '__main__':
print("Converting to onnx and running demo ...")
if len(sys.argv) == 5:
cfg_file = sys.argv[1]
weight_file = sys.argv[2]
image_path = sys.argv[3]
batch_size = int(sys.argv[4])
main(cfg_file, weight_file, image_path, batch_size)
else:
print('Please run this way:\n')
print(' python demo_onnx.py <cfgFile> <weightFile> <imageFile> <batchSize>')
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/allenzhaoxin/pytorch-YOLOv4.git
git@gitee.com:allenzhaoxin/pytorch-YOLOv4.git
allenzhaoxin
pytorch-YOLOv4
pytorch-YOLOv4
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891