yolov3 onnx nms

pytorch cpu 버전을 테스트하는데 1.4s가 필요하고 300여 ms가 필요합니다.
서버의 cpu2.07s 
 
https://github.com/Rapternmn/PyTorch-Onnx-Tensorrt
버전은 작성자의
Onnx 1.4.1
예측 코드 및 다운로드 방법이 있습니다.
https://github.com/Rapternmn/PyTorch-Onnx-Tensorrt/blob/master/detect.py
테스트 결과: 300여 ms, cpu 버전이 있는 것 같아서 차이가 많지 않고 최적화되지 않았습니다.
가중치:
https://github.com/htshinichi/onnx-yolov3
https://github.com/jacke121/onnx-yolov3
이 로드 가중치 실패, 오류 보고:
onnxruntime.capi.onnxruntime_pybind11_state.Fail: [ONNXRuntimeError] : 1 : FAIL : Node (layer1-conv) Op (Conv) [ShapeInferenceError] Attribute dilations has incorrect size
https://github.com/ultralytics/yolov3
이것도 권한이 있고,
 
이것은 권한이 있다.
https://github.com/onnx/models/tree/master/vision/object_detection_segmentation/yolov3
 
wget https://github.com/pjreddie/darknet/blob/master/data/coco.names?raw=true -O ./coco.names # YOLOV3 wget https://github.com/pjreddie/darknet/blob/master/cfg/yolov3.cfg?raw=true -O ./yolov3.cfg wget https://pjreddie.com/media/files/yolov3.weights # YOLOV3-tiny wget https://github.com/pjreddie/darknet/blob/master/cfg/yolov3-tiny.cfg wget https://pjreddie.com/media/files/yolov3-tiny.weights
이것은 가능하지만 360여 ms가 필요합니다. 최적화가 없습니다.
# !/usr/bin/python3
# -*- coding: utf-8 -*-
import cv2
import sys
import numpy as np
import os
import time


class general_yolov3(object):
    def __init__(self, modelpath, is_tiny=False):
        self.conf_threshold = 0.5  # Confidence threshold
        self.nms_threshold = 0.4  # NMS threshold
        self.net_width = 416  #         
        self.net_height = 416  #         

        self.classes = self.get_coco_names()

        net = cv2.dnn.readNetFromDarknet("cfg/yolo_v3.cfg", "yolov3.weights")
        net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
        net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)

        self.yolov3_model = net
        self.outputs_names = self.get_outputs_names()

    def get_coco_names(self):
        # COCO      
        classesFile = "coco.names"
        classes = None
        with open(classesFile, 'rt') as f:
            classes = f.read().rstrip('
').split('
') return classes def get_outputs_names(self): # layersNames = self.yolov3_model.getLayerNames() # , . return [layersNames[i[0] - 1] for i in self.yolov3_model.getUnconnectedOutLayers()] def postprocess(self, img_cv2, outputs): # # NMS confidence img_height, img_width, _ = img_cv2.shape # confidence scores # score class_ids = [] confidences = [] boxes = [] for output in outputs: for detection in output: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > self.conf_threshold: center_x = int(detection[0] * img_width) center_y = int(detection[1] * img_height) width = int(detection[2] * img_width) height = int(detection[3] * img_height) left = int(center_x - width / 2) top = int(center_y - height / 2) class_ids.append(class_id) confidences.append(float(confidence)) boxes.append([left, top, width, height]) # NMS , lower confidences indices = cv2.dnn.NMSBoxes(boxes, confidences, self.conf_threshold, self.nms_threshold) results = [] for ind in indices: res_box = {} res_box["class_id"] = class_ids[ind[0]] res_box["score"] = confidences[ind[0]] box = boxes[ind[0]] res_box["box"] = (box[0], box[1], box[0] + box[2], box[1] + box[3]) results.append(res_box) return results def predict(self, img_file): img_cv2 = cv2.imread("d:/7_Cheering_Cheering_7_426.png") start=time.time() # 4D blob. blob = cv2.dnn.blobFromImage( img_cv2, 1 / 255, (self.net_width, self.net_height), [0, 0, 0], 1, crop=False) # blob self.yolov3_model.setInput(blob) # outputs = self.yolov3_model.forward(self.outputs_names) start1=time.time() # results = self.postprocess(img_cv2, outputs) print('time',time.time()-start1,time.time()-start) return results def vis_res(self, img_file, results): # img_cv2 = cv2.imread(img_file) for result in results: left, top, right, bottom = result["box"] cv2.rectangle(img_cv2, (left, top), (right, bottom), (255, 178, 50), 3) # confidence score label = '%.2f' % result["score"] class_id = result["class_id"] if self.classes: assert (result["class_id"] < len(self.classes)) label = '%s:%s' % (self.classes[class_id], label) # label_size, baseline = cv2.getTextSize( label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1) top = max(top, label_size[1]) cv2.rectangle( img_cv2, (left, top - round(1.5 * label_size[1])), (left + round(1.5 * label_size[0]), top + baseline), (255, 0, 0), cv2.FILLED) cv2.putText(img_cv2, label, (left, top), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 0), 1) # # getPerfProfile() # (in layersTimes). t, _ = self.yolov3_model.getPerfProfile() label = 'Inference time: %.2f ms' % \ (t * 1000.0 / cv2.getTickFrequency()) cv2.putText(img_cv2, label, (0, 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255)) cv2.imshow("asdf",img_cv2[:, :, ::-1]) cv2.waitKey() if __name__ == '__main__': img_file = "d:/7_Cheering_Cheering_7_426.png" start = time.time() modelpath = "/path/to/yolov3_models/" yolov3_model = general_yolov3(modelpath, is_tiny=True) print("[INFO]Model loads time: ", time.time() - start) start = time.time() for i in range(10): results = yolov3_model.predict(img_file) # print("[INFO]Model predicts time: ", time.time() - start) # yolov3_model.vis_res(img_file, results) print("[INFO]Done.")

좋은 웹페이지 즐겨찾기