【发布时间】:2021-11-13 13:59:20
【问题描述】:
我正在实现 Yolo v3 用于多类对象检测
yolo 是基于算法的区域建议 最大置信度的区域建议被视为 yolo 的预测 更多内容你可以阅读here
对于这个特定的任务,我参考了这个 murtuza tutorial 从头开始指导我
现在复杂的网络架构需要数小时的训练
我更喜欢使用迁移学习作为使用预训练网络和权重(参数)
这两个链接都可以在这里找到
架构配置:cfg
网络参数(权重):weights
我在这里使用了 yolov3 tiny,因为我需要更高的帧速率来处理视频 但毕竟它没有给出希望 结果如教程所示 我没有我缺乏的地方 但即使将网络 cfg 和权重文件更改为原始 yolov3(320) 也没有给出真实的结果 因为我将所有 5 个空间数据作为坐标和置信度 [cx,cy,h,w,confidence] 但所有 80 类概率仍然为零向量[0.0,0.0,0.0---0.0] 甚至更改视频源并选择另一个视频导致零向量 在教程中工作正常
Implementation Code:
# YOLO Algorithm
# Network Weights and configuration Files
yolov3_tiny_cfg='/root/Downloads/ML TASK/yolov3-tiny.cfg' # configuration file
yolov3_tiny_weights='/root/Downloads/ML TASK/yolov3-tiny.weights' # weights
coco_names='/root/Downloads/ML TASK/coco.names' # coco classes
# for yolo genral 320 architecture
# put paths to directory
yolov3_cfg='/root/Downloads/ML TASK/yolov3.cfg'
yolov3_weights='/root/Downloads/ML TASK/yolov3.weights'
# Test Videos
Test_video_1='/root/Downloads/ML TASK/mn.mp4'
Test_video_2='/root/Downloads/ML TASK/bg.mp4'
# Dependencies
import cv2
import numpy as np
# Dataset Classes:
# there are around 80 classes in the coco dataset so manually writing them would not be right choice so instead of them we are getting them from a file name coco.names stored in drive
# getting list of classes
classes=[] # empty list intialization
with open(coco_names,'r')as f:
classes=f.read().splitlines()
# viewing the multiclass list around 80 classes in coco dataset
# Loading the yolov3 using configuration file and weights
network=cv2.dnn.readNetFromDarknet(yolov3_cfg,yolov3_weights)
network.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)# to use opencv CPU as backend
network.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)
#NOTE: The network won't feed directly the image we have to First Preprocess it To match the input shape of network also the type i.e. Blob it genrally refers to a mathematical form of binary Images Like Bitmap
Width,Height=320,320 # sqaure image so the network grid should be n*n equal on both dimension
Confidence_Threshould=0.5 # minimum problity for claiming the prediction
NMS_Threshould=0.3
cap=cv2.VideoCapture('game.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
timestamps = [cap.get(cv2.CAP_PROP_POS_MSEC)]
# function to find objects on captured video stream
def findObjects(outputs,image):
h,w,c=image.shape
bound_box=[] # for feeding through function
classIds=[]
confidence=[]
for output in outputs: # getting o/p from 2 layers(v3 tiny) 3 if use yolov3 320
for detection in output:
scores=detection[5:] #slice first five values cause we are gonnause them in bounding
classId=np.argmax(scores)
confs=scores[classIds]
# filtering object putting them as final prediction only when its breaches the minimum threshould of confidence
if confs > Confidence_Threshould:
w,h=int(detection[2]*Width),int(detection[3]*Height) # to convert % into pixel
x,y=int((detection[0]*Width)-(w/2)),int((detection[1]*Height)-(h/2))
bound_box.append([x,y,w,h])
classIds.append(classId)
confidence.append(float(confs))
print(len(bound_box))
# to downsample the no. of boxes on frame we use nms boxes it give indices by which spatial info to keep
indices=cv2.dnn.NMSBoxes(bound_box,confs,Confidence_Threshould,NMS_Threshould)
for i in indices:
i=i[0]
box=bound_box[i]
x,y,w,h=box[0],box[1],box[2],box[3]
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
cv2.puttext(image,f'{classes[classIds[i]]}{int(confidence[i]*100)}%',
(x,y-10),cv2.FONT_HERSHEY_PLAIN,0.6,(0,255,0),2)
cv2.puttext(image,f'FPS:{fps}',(0,150),cv2.FONT_HERSHEY_PLAIN,0.6,(0,255,0),2)
cv2.puttext(image,f'TIMESTAMPS:{timestamps}',(150,0),cv2.FONT_HERSHEY_PLAIN,0.6,(0,255,0),2)
while True:
success,image=cap.read()
# coverting image into blob for network i/O processing
try:
blob=cv2.dnn.blobFromImage(image,1/255,(Width,Height),[0,0,0],crop=False)
except:
continue
# I/P
network.setInput(blob) # Setting Input
# O/P
# As Yolo Architecture Produces Three O/p[Genral Architecture] From The Respective Layer And By Summarize The Max Of Confidence to Decide Final Predictions
# But here only 2 o/p of network as we are using the tiny version for higher frame rates
# In Order to Get The Outputs We Have To Know the Name Of the Respective Layers #i.e. Not Names Actually But Getting indexes(starting from 1 Not zero) Here By Use Of getUnconnectedOutLayers Function
layers_names=network.getLayerNames()
#print(network.getUnconnectedOutLayers()) #36th and 48th indexes
#looping over as we are traversing multiple values of OutLayers
outputNames=[layers_names[i[0]-1]for i in network.getUnconnectedOutLayers()] #-1 cause the index are starting from one not zero
#print(outputNames) # for v3 tiny its 16 and 23 are layer name
# forwading the image to network
outputs=network.forward(outputNames)
# finding objects
# print(outputs[0].shape)=>(300,85) 300=>no.of boxes 85=>[cx,cy,height,width,confidence,probablity of 80 classes]
# using the cx,cy,h,w we are gonna determine the bounding box
# print(outputs[1].shape)=>(1200,85) 1200 boxes this shape present in m*n format i.e. matrix faishion where 1200 rows of boxes map with 85 vector details explained aboved
#print(outputs[0][0])
findObjects(outputs,image)
cv2.imshow('Window',image)
if cv2.waitKey(15) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
【问题讨论】:
-
不确定我是否理解:您的代码是否适用于文献中预训练的 COCO 权重?如果是:每个班级使用多少训练样本?
标签: numpy opencv machine-learning computer-vision yolo