最近研究tensorrt 跑 yolov3-416简单记录。下载yolov3的cfg和weights
修改cfg
vim yolov3.cfgbatch = 1width = 416height = 416
并且每一段只有一个空格。
修改yolov3_to_onnx.py 因为我们是416
output_tensor_dims['082_convolutional'] = [255, 13, 13] [255, 19, 19]output_tensor_dims['094_convolutional'] = [255, 26, 26] [255, 38, 38]output_tensor_dims['106_convolutional'] = [255, 52, 52] [255, 76, 76]
其中 255 =(80 + 5)* 3 80为类别,5为输出 x, y, h, w, score
修改 weights_file_path = './yolov3.weights'
修改 output_file_path = './yolov3-416.onnx'
修改 cfg_file_path = './yolov3-416.cfg'
在NX盒子上把onnx转换成 in8 trt格式模型。
cd /usr/src/tensorrt/samles/python/yolov3_onnx/tensorrt_demos-master/yolo
sudo vim onnx_to_tensorrt.py修改 engine_path = './%s.trt'%args.model修改 onnx_path = './%s.onnx'%model_name修改 network.get_input(0).shape = [1, 3, 416, 416] # [1, 3, 608, 608]
修改 EXPLICIT_BATCH = [] if trt.__version__[0] < '7' else \ [1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)] 注释掉改为:EXPLICIT_BATCH = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)修改 builder.create_network(*EXPLICIT_BATCH) as network改为 builder.create_network(EXPLICIT_BATCH) as network
修改 builder.int8_calibrator = YOLOEntropyCalibrator( 'calib_images', (net_h, net_w), 'calib_%s.bin' % model_name)改为 calib_images 为量化图片地址。
创建一个量化图片文件夹
mkdir calib_imagessudo ln -s yolov3-416.cfg yolov3-int8-416.cfgsudo ln -s yolov3-416.onnx yolov3-int8-416.onnx
NX盒子转换 onnx 转换 int8
sudo python3 onnx_to_tensorrt.py -v --int8 -m yolov3-int8-416 # -c 为检测类别数 默认80 --dla_core 设置dla卡
cd /usr/src/tensorrt/samles/python/yolov3_onnx/tensorrt_demos-mastersudo vim trt_yolo_video_on_line.py
修改 def loop_and_detect 全部内容 测试本地视屏fps
def loop_and_detect(cam, trt_yolo, conf_th, vis): output_path = './tensorrt_yolov3_int8_car.mp4' full_scrn = False writeVideo_flag = True if writeVideo_flag: vid = cv2.VideoCapture(cam) if not vid.isOpened(): raise IOError("Couldn't open webcam or video") video_FourCC = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') video_fps = vid.get(cv2.CAP_PROP_FPS) video_size = (int(vid.get(cv2.CAP_PROP_FRAME_WIDTH)),int(vid.get(cv2.CAP_PROP_FRAME_HEIGHT))) isOutput = True if output_path != "" else False if isOutput: #print("!!! TYPE:", type(output_path), type(video_FourCC), type(video_fps), type(video_size)) out = cv2.VideoWriter(output_path, video_FourCC, video_fps, video_size) #list_file = open('detection.txt', 'w') frame_index = -1 while True: return_value, frame = vid.read() if return_value != True: break if return_value: # frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) image = Image.fromarray(frame) else: raise ValueError("No image!") tic = time.time() boxes, confs, clss = trt_yolo.detect(img, conf_th) img = vis.draw_bboxes(img, boxes, confs, clss) toc = time.time() curr_fps = 1.0 / (toc - tic) info = "time:" + str(round( (toc - tic), 2)) + " ms, FPS: " + str(curr_fps) cv2.putText(result, text=info, org=(50, 70), fontFace=cv2.FONT_HERSHEY_SIMPLEX,fontScale=1, color=(255, 0, 0), thickness=2) #cv2.namedWindow("result", cv2.WINDOW_AUTOSIZE) if writeVideo_flag: # save a frame out.write(result) #cv2.imshow("result", result) if cv2.waitKey(1) & 0xFF == ord('q'): break
修改 cam = Camera(args) if not cam.isOpened(): raise SystemExit('ERROR: failed to open camera!') 注释掉
修改 open_window( WINDOW_NAME, 'Camera TensorRT YOLO Demo', cam.img_width, cam.img_height) 注释掉
修改 cam.release() cv2.destroyAllWindows()注释掉
添加 arser.add_argument( '-video', '--video_path', type=str, default='', help='video_path') 添加在线视屏路径
测试yolov3-int8-416 在tensorrt上运行时间
sudo python3 trt_yolo_video_on_line.py -m /usr/src/tensorrt/samples/python/yolov3_onnx/tensorrt_demos-master/yolo/yolov3-int8nx-416 -video ./car.mp4 # -c 检测类别数目,默认为80
NX量化C++引擎 engine 直接把onnx转化成engine格式
sudo ./trtexec --onnx=./yolov3-416-1.onnx --int8 --batch=1 --device=0 --saveEngine=./yolov3-416-gpu-int8.engine --verbose=True --allowGPUFallback --calib=/usr/src/tensorrt/samples/python/yolov3_onnx/tensorrt_demos-master/yolo/calib_images/*
可能报错:
/bin/sh: 1: nvcc: not foundMakefile:48: recipe for target 'yolo_layer.o' failedmake: *** [yolo_layer.o] Error 127
解决 找到nvcc 可执行文件路径
cd /tensorrt_demo-master/plugins vim Makefile NVCC = /usr/local/cuda-10.2/bin/nvcc
遇见一个ubuntu16.04网络错误
解决方案
vim /etc/network/interfaces
ubuntu16.04 插入U盘报错:
解决方案:
sudo apt-get install exfat-fuse exfat-utils