7

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
  • 2,205
  • 1
  • 15
  • 37

2 Answers2

8

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
  • 687
  • 5
  • 21
  • From my understanding, yolov3 is going to make a grid for the images/video to detect object. Using the code over, how many grid the yolov3 determine? – 057 Ahmad Hilman D Jun 25 '22 at 01:26
  • Thank you very much. Can you please refer me to a link for calculating the output of each convolution layer? Also, do you have a similar figure for YOLOv4 please? – Avv Dec 19 '22 at 03:04
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
  • 696
  • 1
  • 5
  • 7