face-analysis / emonet

Official implementation of the paper "Estimation of continuous valence and arousal levels from faces in naturalistic conditions", Antoine Toisoul, Jean Kossaifi, Adrian Bulat, Georgios Tzimiropoulos and Maja Pantic, Nature Machine Intelligence, 2021
https://www.nature.com/articles/s42256-020-00280-0
Other
260 stars 62 forks source link

Inference on new test data gives negative tensors in expression #2

Closed dark-art closed 3 years ago

dark-art commented 3 years ago

Hi, was trying to use this model for segregation pipelines for new data for emotion cassification. Unable to get desired results, image= io.imread('imagedata.jpg') bounding_box = [landmarks.min(axis=0)[0], landmarks.min(axis=0)[1], landmarks.max(axis=0)[0], landmarks.max(axis=0)[1]] image, landmarks = transform_image_shape_no_flip(image, bb=bounding_box) image = np.ascontiguousarray(image) image = image.reshape((1,3,256,256)) image = torch.Tensor(image)

image = transform_image(image)

    with torch.no_grad():
        out = net(image)

Getting landmarks from another model. This gives me negative values in expression values, emotion classification seems wrong. Am I missing some normalization. Really appreciate the support. @antoinetlc

zhouquan9 commented 3 years ago

Got negative expression values too. but the largest value seems to correspond to the correct expression category. My code is slightly different

image, landmarks = transform_image_shape_no_flip(image, bb=bounding_box)
image = np.ascontiguousarray(image)
tensor = transform_image(image).reshape(1,3,256,256)
tensor = tensor.to(device)
with torch.no_grad():
    out = net(tensor)

An image with happy face was used and the expression values are [-32.8698, -1.3515, -25.1789, -27.1149, -23.0900, -19.4361, -27.1756, -20.0331]

antoinetlc commented 3 years ago

Hello,

The output of the network for the expression values are the score before softmax for each emotion. This is the reason why the values can be negative. If you want to transform this vector into a probability distribution please apply a softmax.

I also realize we forgot to mention which index correspond to which emotion. The mapping is as follows.

For 8 emotions :

0 - Neutral 1 - Happy 2 - Sad 3 - Surprise 4 - Fear 5 - Disgust 6 - Anger 7 - Contempt

For 5 emotions :

0 - Neutral 1 - Happy 2 - Sad 3 - Surprise 4 - Fear

Hope this helps!

dark-art commented 3 years ago

Guys, this works flawless now, thanks a lot for the support. Really appreciate it. Just one small thing,

image = image.reshape(1,3,256,256) image = torch.Tensor(image) ^This thing topples the results

image = transform_image(image) image = image.reshape(1,3,256,256) And ^ this works amazing, any insights for this?

Anyways, I got it running well now, thanks to your comments, understood the logit part before cross-entropy, kindly close the issue. Thanks again.

zhouquan9 commented 3 years ago

@antoinetlc Thank you for the explanation!

@dark-art This is the function behind tranform_image. It scales values to [0, 1] and also changes the dimension from (H x W x C) to (C x H x W), which should be equivalent to image = torch.Tensor(image).permute(2,0,1) / 255

dark-art commented 3 years ago

@zhouquan9 @antoinetlc Gotcha, thanks a lot, really respect your support and explanations. Amazing work!