了解函数forward()和getUnconnectedOutLayers()

2024-10-05 14:23:48 发布

您现在位置:Python中文网/ 问答频道 /正文

ln = net.getLayerNames()
print(ln)
ln = [ln[i[0]-1] for i in net.getUnconnectedOutLayers()] 
print(ln)


blob = cv2.dnn.blobFromImage(image, scalefactor = 1/255.0, size = (416,416), swapRB = True, crop = False)   
net.setInput(blob)
start = time.time()
layerOutputs = net.forward(ln)  
end = time.time()

print("[INFO] Yolo took {:.6f} seconds" .format(end-start))


boxes = []  
confidences = []  
classIDs = []   

for output in layerOutputs :    
    for detection in output :
        scores = detection[5:]  
        classID = np.argmax(scores)
        confidence = scores[classID]

        if confidence > args["confidence"]:
            box = detection[0:4] * np.array([W,H,W,H])
            (centerX, centerY, width, height) = box.astype("int")

            x = int(centerX - (width/2))
            y = int(centerY - (height/2))

            boxes.append([x,y,int(width), int(height)])
            confidences.append(float(confidence))
            classIDs.append(classID)

我对上面的代码有两个基本问题:

  1. 我知道使用了函数getUnconnectedOutLayers() 获取未连接输出层的索引以便查找 函数forward()必须在网络中运行多远。我 不明白为什么这些输出层被表示为未连接的。 另外,这是否意味着在某些情况下,我们不会在整个网络中运行我们的数据?如果是,为什么? 另一个困扰我的关于我们使用getUnconnectedOutLayers()函数的行是它的ln[i[0]-1部分。我相信这是反向遍历ln数组的某种方法,但我并不完全理解它。在
  2. 在文档中,说明函数forward() 返回指定层的第一个输出的blob。我假设它是我们从函数blobFromImage()得到的相同的blob,因为它也是4D。 在后面的代码中,将声明以下内容:scores = detection[5:]。因为我假设它是一个4D数组,所以我期望得到以下结果:scores = detection[5:::]。是两个维度 由于两个for循环而导致切片“失败”?在

Tags: 函数infornettimeblobintforward