如何在yolov3上增加fps

wb1gzix0  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(428)

我是dl/实时对象检测领域的新手,试图从youtube上学习一些东西。现在我的问题是,我在google colabrotary中使用yolov3训练了一个实时目标检测模型。现在我有 yolov3_training_last.weights yolov3_testing.cfg classes.txt 用于实时目标检测的文件。我使用的是python文件,但在使用笔记本电脑相机时,它给了我2-3fps。有没有办法提高我的fps?我在想它是在用我的cpu进行实时反馈。如何将其切换到gpu?正如我所说,我是这方面的新手,如果有什么办法,你能解释一下我该怎么做吗。如果没有,你能告诉我其他方法来训练实时目标检测模型吗?如果你能帮助我,我将不胜感激。先谢谢你。。。
我的gpu:gtx1650
我的cpu:英特尔酷睿i7 9750h
此处是我的python文件:

import cv2
import numpy as np

net = cv2.dnn.readNet('yolov3_training_last.weights', 'yolov3_testing.cfg')

classes = []
with open("classes.txt", "r") as f:
    classes = f.read().splitlines()

cap = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0, 255, size=(100, 3))

while True:
    _, img = cap.read()
    height, width, _ = img.shape

    blob = cv2.dnn.blobFromImage(img, 1/255, (416, 416), (0,0,0), swapRB=True, crop=False)
    net.setInput(blob)
    output_layers_names = net.getUnconnectedOutLayersNames()
    layerOutputs = net.forward(output_layers_names)

    boxes = []
    confidences = []
    class_ids = []

    for output in layerOutputs:
        for detection in output:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]
            if confidence > 0.2:
                center_x = int(detection[0]*width)
                center_y = int(detection[1]*height)
                w = int(detection[2]*width)
                h = int(detection[3]*height)

                x = int(center_x - w/2)
                y = int(center_y - h/2)

                boxes.append([x, y, w, h])
                confidences.append((float(confidence)))
                class_ids.append(class_id)

    indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.2, 0.4)

    if len(indexes)>0:
        for i in indexes.flatten():
            x, y, w, h = boxes[i]
            label = str(classes[class_ids[i]])
            confidence = str(round(confidences[i],2))
            color = colors[i]
            cv2.rectangle(img, (x,y), (x+w, y+h), color, 2)
            cv2.putText(img, label + " " + confidence, (x, y+20), font, 2, (255,255,255), 2)

    cv2.imshow('Image', img)
    key = cv2.waitKey(1)
    if key==27:
        break

cap.release()
cv2.destroyAllWindows()
jei2mxaa

jei2mxaa1#

默认情况下,opencv的 dnn 模块不在gpu上运行,因此,您的模型可能在cpu上运行。
提高吞吐量的一个简单方法是查看模型优化,如量化和修剪。有几种方法可以实现同样的效果,下面链接了一些流行的优化方法。
可以进行一些优化来提高模型吞吐量(fps):
使用openvino优化英特尔cpu:
官方文档:yolov3到英特尔openvino的转换
使用英特尔openvino:实时人脸匿名器的项目
针对nvidia gpu进行优化-opencv dnn 模块:
pyimagesearch博客:opencv dnn 使用nvidia GPU:YLO、ssd和mask r-cnn速度提高1549%
针对nvidia gpu进行优化-tensorrt:
转换yolov3权重>onnx运行时>tensorrt
博客:yolov3预培训的tensorrt转换
博客:针对yolov3定制培训的tensorrt转换
tensorrt转换演示源代码
每一项都涉及到一定数量的学习曲线。不幸的是,没有简单的出路。

相关问题