4

I almost searched every where on internet but i could not find out the working and output of below functions. Specially what they do in YOLO algorithm.

getLayerNames()
getUnconnectedOutLayers()

code is as follows:

import cv2 
import numpy as np 
import time 
#Loading Yolo 
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") 
classes = [] 
with open("coco.names", "r") as f: 
  classes = [line.strip() for line in f.readlines()] 
layer_names = net.getLayerNames() 
outputlayers=[layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] 
one
  • 1,530
  • 8
  • 23

2 Answers2

4

YOLOv3 has 3 output layers (82, 94 and 106) as the figure shows.

getLayerNames(): Get the name of all layers of the network.

getUnconnectedOutLayers(): Get the index of the output layers.

These two functions are used for getting the output layers (82,94,106). I prefer to use the following code for simplicity:

import cv2 
import numpy as np 
import time 
#Loading Yolo 
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg") 
classes = [] 
with open("coco.names", "r") as f: 
  classes = [line.strip() for line in f.readlines()] 
outputlayers = net.getUnconnectedOutLayersNames()  

YOLOv3 Architecture

REFERENCE FOR THE IMAGE (external link)

Felipe
  • 635
  • 5
  • 20
2

My understandings:

net.getLayerNames(): It gives you list of all layers used in a network. Like I am currently working with yolov3. It gives me a list of 254 layers.

net.getUnconnectedOutLayers(): It gives you the final layers number in the list from net.getLayerNames(). I think it gives the layers number that are unused (final layer). For yolov3, it gave me three number, 200, 227, 254. To get the corresponding indexes, we need to do layer_names[i[0] - 1]. Hope these help.

JuBaer AD
  • 156
  • 1
  • 1
  • 6