Ask Your Question
0

np.argmax returns 0 always

asked 2020-05-15 07:59:36 -0500

Num gravatar image

updated 2020-05-22 10:50:36 -0500

To determine the class name of the detected object, I need to get the class_id of the image. The problem is, np.argmax always returns 0 and gets the first class name. When I detect another object, it should print class_id 1 but it prints 0 and I can't get the proper label name to display.

When I look at my .txt files, I see this:

0 0.170103 0.449807 0.319588 0.521236

1 0.266791 0.148936 0.496269 0.287234

2 0.265464 0.422780 0.510309 0.420849

def detect_img(self, img):
    blob = cv2.dnn.blobFromImage(img, 0.00392 ,(416,416), (0,0,0), True, crop=False)
    input_img = self.net.setInput(blob)
    output = self.net.forward(self.output)

    height, width, channel = img.shape
    boxes = []
    trusts = []
    class_ids = []

    for out in output:
        for detect in out:
            total_scores = detect[5:]
            class_id = np.argmax(total_scores)
            print(np.argmax(detect))
            trust_factor = total_scores[class_id]
            if trust_factor > 0.2:
                x_center = int(detect[0] * width)
                y_center = int(detect[1] * height)
                w = int(detect[2] * width)
                h = int(detect[3] * height)
                x = int(x_center - w / 2)
                y = int(x_center - h / 2)
                boxes.append([x,y,w,h])
                trusts.append(float(trust_factor))
                class_ids.append(class_id)

    for index in range(len(boxes)):
        # if index in indexes:
        x,y,w,h = boxes[index]
        label = self.classes[class_ids[index]]
        trust = round(trusts[index], 2)
        text = f"{label}, Trust: {trust}"
        cv2.rectangle(img, (x,y), (x + w, y + h), (0,255,0), 2)
        cv2.putText(img, text, (x - 20, y + 40), cv2.FONT_HERSHEY_PLAIN, 1, (0,0,255), 2)

How I labeled my image: This is how I labeled the images

The result after training: This is the result that i get

In the result you can see that I dont see the label of the iphone and the rectangle is way above than I have selected. The airpods are also way bigger and samsung is at the bottom, which I didn't select like this.

edit retag flag offensive close merge delete

Comments

did you train your own model ? if so, starting from what ?

what is detect.shape ?

berak gravatar imageberak ( 2020-05-16 03:01:28 -0500 )edit
1

Yes, I trained my model on google colab for a few hours. When I had minimal loss, like 0.3 I stopped it with running. When I print detect.shape I get (8,) 2 (8,) 3 (8,) 2 (8,) 2

... and so on

The confidence get labeled correctly, so sometimes it is 0.6, sometimes 0.9 and so on. The only problem is the class name. I collected a few hundred pictures of iphones, samsung and airpods. I have a list of ["samsung", "iphone", "airpods"] and because class_id = 0, self.classes[class_id] is always 0 and prints out samsung.

Num gravatar imageNum ( 2020-05-16 03:54:53 -0500 )edit

ah, ok.thanks, i was on the wrong path then

(suspecting your detect output does not match the classes, but it does here)

berak gravatar imageberak ( 2020-05-16 04:29:24 -0500 )edit

Any other idea maybe?

Num gravatar imageNum ( 2020-05-16 05:28:41 -0500 )edit

might be simply a bad model, overfitting ?

btw, (it won't change the current problem), you seem to miss the NMS part here

https://github.com/opencv/opencv/blob...

berak gravatar imageberak ( 2020-05-16 08:39:44 -0500 )edit

Yes, I have NMS included but I commented it out right now.

Num gravatar imageNum ( 2020-05-16 09:26:33 -0500 )edit

2 answers

Sort by ยป oldest newest most voted
0

answered 2020-05-23 09:31:45 -0500

berak gravatar image

updated 2020-05-23 09:33:20 -0500

heh, simple typo:

y = int(x_center - h / 2)

sure meant to use y_center there.

(this will correct the boxes, not the classids)

edit flag offensive delete link more

Comments

Omg, you are right.. Im so happy thank you!

Num gravatar imageNum ( 2020-05-24 05:00:54 -0500 )edit
0

answered 2020-05-22 09:16:34 -0500

Num gravatar image

updated 2020-05-22 10:57:16 -0500

@berak, I solved this problem. It was my fault, I did wrong string formatting when I started training it with darknet. I have one other question. Sometimes, in pictures, the rectangles are drawn bigger and I can't see the label class. When I'm labeling my images, do I need to label them with the same width and height every time? Because sometimes the picture is very big, the object becomes very big and sometimes the image is small and the object is small too that I try to detect. Does all the images need to be the same size?

edit flag offensive delete link more

Comments

Do all the images need to be the same size?

if you meant the dnn inference -- they will get resized to 416x416 in blobFromImage()

berak gravatar imageberak ( 2020-05-22 10:06:09 -0500 )edit

Can you check my edit?

Num gravatar imageNum ( 2020-05-22 10:50:44 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-05-15 07:59:36 -0500

Seen: 62 times

Last updated: 2 days ago