yu4u / age-gender-estimation

Keras implementation of a CNN network for age and gender estimation
MIT License
1.46k stars 502 forks source link

predicted_genders[i][0] < 0.5 or > 0.5 for male on video #105

Open nyck33 opened 4 years ago

nyck33 commented 4 years ago

Hi Yu-san,

Thanks for this repo. I have a question regarding this snippet in demo.py "M" if predicted_genders[i][0] < 0.5 else "F") I am predicting on one face only so my code is:

face[0, :, :, :] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (imgsize, imgsize))
crop = img[y1: y2+1, x1:x2+1]
height, width, _ = crop.shape
result = self.model.predict(face)
predicted_gender = result[0]
print("gender {}".format(predicted_gender[0][0]))
if predicted_gender[0][0] < 0.5:  #is this correct?
     pixelated = pixelate(crop, width, height)

I'm getting this output showing all female: gender 0.7589111924171448 gender 0.6748817563056946 gender 0.7280391454696655 gender 0.7113814353942871 gender 0.6321226954460144 gender 0.6755571961402893 gender 0.655595600605011 gender 0.6332910060882568 gender 0.665791392326355 gender 0.6619336009025574 frame 10, 2 percent done gender 0.6297609210014343 gender 0.6310742497444153 gender 0.6388582587242126 gender 0.6477266550064087 gender 0.6689814329147339 gender 0.6472148299217224 gender 0.6857687830924988 gender 0.6730141639709473 gender 0.675566554069519 gender 0.6839122772216797 gender 0.6692551374435425 gender 0.6698795557022095 gender 0.6731051802635193 gender 0.6431819796562195 gender 0.7187308073043823 gender 0.7388319373130798

For video of myself here face detected with mtcnn which bbox I think is similar to dlib. https://youtu.be/QMs9DZk0IOs

With the sign reversed, ie. if predicted_gender[0][0] > 0.5, gender prediction is almost perfect even on profile: https://www.youtube.com/watch?v=4yxAbq-3avk&feature=youtu.be

So I don't understand how I'm being detected as female because when I ran your demo.py on webcam on myself, it detected me as male 100% but it was not good on profile face due to dlib limitations.

My function is below but I think the above snippet is enough:

    def predict_gender(self, img, bboxes):
        #check img
        #print(img.shape)
        #pad with margin around image before gender detection
        #params from argparse
        #margin = 0.4 #for imdb
        margin = 0.4
        imgsize = 64 #default 64

        img_h, img_w, _ = np.shape(img)
        #detect faces with DSFD and resize to 64 by 64, but resize to 128 (retrain at this size)
        #with VGGFace img size is 224*224
        face = np.empty((1, imgsize, imgsize, 3))

        if len(bboxes) > 0:
            for i in range(len(bboxes)):
                x1, y1 = bboxes[i][0], bboxes[i][1]
                w, h = bboxes[i][2], bboxes[i][3]
                x2, y2 = x1 + w, y1 + h 
                xw1 = max(int(x1 - margin * w), 0)
                yw1 = max(int(y1 - margin * h), 0)
                xw2 = min(int(x2 + margin * w), img_w - 1)
                yw2 = min(int(y2 + margin * h), img_h - 1)

                face[0, :, :, :] = cv2.resize(img[yw1:yw2 + 1, xw1:xw2 + 1, :], (imgsize, imgsize))
                crop = img[y1: y2+1, x1:x2+1]
                height, width, _ = crop.shape
                result = self.model.predict(face)
                predicted_gender = result[0]
                print("gender {}".format(predicted_gender[0][0]))
                if predicted_gender[0][0] < 0.5: #reversed to >0.5 for second Youtube video result
                    pixelated = pixelate(crop, width, height)
                    img[y1:y2+1, x1:x2+1] = pixelated

        return img
yu4u commented 4 years ago

What does happen if you simply run demo.py? The same results?

nyck33 commented 4 years ago

No, when I ran demo.py, it was predicting correctly. I did not have to reverse the > sign. But I'll try again.

gmt710 commented 3 years ago

Hello, Have you evaluated the gender on wiki datasets ? I was very confused because I didn't get good results.