Yolov3 and darknet problem

asked 2019-10-29 13:04:50 -0500

light0090 gravatar image

updated 2019-12-10 01:14:21 -0500

Akhil Patel gravatar image

I developed my custom object detector using tiny yolo and darknet. It work great, but I need of one specific features:
the network outputs bounding boxes are each represented by a vector of number of classes + 5 elements. The first 4 elements represent the center_x, center_y, width and height. The fifth element represents the confidence that the bounding box encloses an object. The rest of the elements are the confidence associated with each class (i.e. object type). For each boxes, I need the confidence associated for each classes, but I have in output only max confindece, others confidence output are 0.

Example run :

print(scores)

returned

[0.        0.        0.5874982]

0.5874982 is the max confidence. It's the 3th class. But I don't understand because the others confidence are 0. Thanks for replay and I'm sorry for my bad english. This is code

 import cv2 as cv
 import argparse
 import sys
 import numpy as np
 import os.path

 confThreshold = 0.5 
 nmsThreshold = 0.6      
 inpWidth = 416          #Width of network's input image
 inpHeight = 416         #Height of network's input image


 parser = argparse.ArgumentParser(description='Object Detection using YOLO in OPENCV')
 parser.add_argument('--image', help='Path to image file.')
 parser.add_argument('--video', help='Path to video file.')
 args = parser.parse_args()

# Load names of classes
classesFile = "obj.names"
classes = None
with open(classesFile, 'rt') as f:
     classes = f.read().rstrip('\n').split('\n')

 # Give the configuration and weight files for the model and load the network using them.
 modelConfiguration = "yolov3-tiny-obj.cfg"
 modelWeights = "pesi/pesi_3_classi_new/yolov3-tiny-obj_7050.weights"

 net = cv.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
 net.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
 net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)

 # Get the names of the output layers
 def getOutputsNames(net):
    layersNames = net.getLayerNames()
    return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]

 # Draw the predicted bounding box
 def drawPred(classId, conf, left, top, right, bottom):
    if classId==1:
        cv.rectangle(frame, (left, top), (right, bottom), (3, 14, 186), 3)
    elif classId==0:
        cv.rectangle(frame, (left, top), (right, bottom), (40, 198, 31), 3)
    elif classId==2:
        cv.rectangle(frame, (left, top), (right, bottom), (40, 198, 31), 3)

    label = '%.2f' % conf

    # Get the label for the class name and its confidence
    if classes:
       assert(classId < len(classes))
       label = '%s:%s' % (classes[classId], label)

    #Display the label at the top of the bounding box
    labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
    top = max(top, labelSize[1])
    cv.rectangle(frame, (left, top - round(1*labelSize[1])), (left + round(1*labelSize[0]), top + baseLine), (255, 255, 255), cv.FILLED)
    cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.45, (0,0,0), 1)

  # Remove the bounding boxes with low confidence using non-maxima suppression
  def postprocess(frame, outs):
   frameHeight = frame.shape[0]
   frameWidth = frame.shape[1]

   # Scan through all the bounding boxes output from the network and keep only the
   # ones with high confidence scores. Assign the box's class label as the class with the highest score.
   classIds = []
   confidences = []
   boxes = []
   for out in outs:
       for ...
(more)
edit retag flag offensive close merge delete

Comments

I think thats because yolo does "multiclass classification" - this means they train a single classifier for each class. So this means not all probabilities are necessarily distributed to sum up to 1. But i am only 70% sure about this.

If you have doubt on your code - just check the opencv yolov3 example. Opencv and yolo works great for me(in the newer versions)

holger gravatar imageholger ( 2019-10-30 04:57:29 -0500 )edit

Yes, it work great! But I need know confidence for each classes for each object and not only max confidence..

light0090 gravatar imagelight0090 ( 2019-10-30 05:05:46 -0500 )edit

Uhmm study the opencv example again - you get back a vector with probabilities for each class.

holger gravatar imageholger ( 2019-10-30 05:28:49 -0500 )edit