【问题标题】:How to run trained Yolov3 model with using GPU如何使用 GPU 运行经过训练的 Yolov3 模型
【发布时间】:2021-07-12 08:41:40
【问题描述】:

我是 DL/实时对象检测领域的新手,我想从 youtube 上学习一些东西。我在 youtube 上观看了关于 yolov3 上实时自定义对象检测的视频 https://www.youtube.com/watch?v=DLngCtsG3bk,我正确地完成了所有步骤。当我在笔记本电脑上运行我的对象检测 python 文件时,它在 CPU 上运行,我只能接收 2-3 fps。请谁能告诉我如何使用我的 GPU 来运行它。我得到了yolov3_training_last.weights、yolov3_testing.cfg 和classes.txt 的文件。 python 文件包含在下面,当我运行它时,正如我所说的那样,我得到了低 fps。如果有什么方法可以通过使用 GPU 来增加它,请教我。提前谢谢你。

Python 文件:

import cv2
import numpy as np
import time

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))

# used to record the time when we processed last frame
prev_frame_time = 0
 
# used to record the time at which we processed current frame
new_frame_time = 0

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.95:
                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]
            crop_img = img[y:y+h, x:x+w]
            (B, G, R) = [int(x) for x in cv2.mean(crop_img)[:3]]
            roi_face= img[y: y+ h, x:x+w]
            roi_face = cv2.blur(roi_face,(20,20))
            img[y: y+ h, x: x+ w]=[0,0,0]
            img[y: y+ h, x: x+ w]=cv2.add(roi_face, img[y: y+ h, x: x+ w])
            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, 1, (255,255,255), 2)

    
    font = cv2.FONT_HERSHEY_SIMPLEX
    # time when we finish processing for this frame
    new_frame_time = time.time()
 
    # Calculating the fps
 
    # fps will be number of frame processed in given time frame
    # since their will be most of time error of 0.001 second
    # we will be subtracting it to get more accurate result
    fps = 1/(new_frame_time-prev_frame_time)
    prev_frame_time = new_frame_time
 
    # converting the fps into integer
    fps = int(fps)
 
    # converting the fps to string so that we can display it on frame
    # by using putText function
    fps = str(fps)
 
    # putting the FPS count on the frame
    cv2.putText(img, fps, (7, 70), font, 3, (100, 255, 0), 3, cv2.LINE_AA)

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

cap.release()
cv2.destroyAllWindows()

【问题讨论】:

    标签: python tensorflow object-detection yolo custom-object


    【解决方案1】:

    我认为您需要安装支持 GPU 的 Tensorflow 版本。然后,您必须指定要使用的设备,您的 CPU 或 GPU。

    另外,您的 GPU 必须能够运行该程序。所以你需要安装相关的库。如果您有AMD GPU,那将很难,因为我猜他们不支持Tensorflow GPU。至于Nvidia,有驱动和库。

    您的另一个选择是使用Pytorch,同样您需要拥有正确的硬件和软件。

    另一种方法是使用 Keras 和 PlaidML 从头开始​​创建模型

    【讨论】:

    • 我有 GTX 1650 和 Intel Core i7 9750H。请你能提供一个链接或者你能告诉我我该怎么做。我的笔记本电脑里有 tensorflow。
    • 按照指南here它会显示您需要安装的软件
    • 先生,如我所说,我已经拥有了所有这些。只是我想知道在哪里可以找到一个使用 GPU 版本运行的 python 文件,我是否需要做任何事情,比如将我的权重文件转换为另一个文件等等。主要问题是:我如何运行实时对象检测使用我的文件?
    • 还有一个有趣的例子 [这里] (machinelearningmastery.com/…) 使用 keras 和 yolo3 进行检测。祝你好运
    猜你喜欢
    • 1970-01-01
    • 2017-06-17
    • 2020-09-26
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2018-01-30
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多