av-savchenko / face-emotion-recognition

Efficient face emotion recognition in photos and videos
Apache License 2.0
686 stars 127 forks source link

Age gender ethinicity model giving same output for different results #8

Closed sneakatyou closed 2 years ago

sneakatyou commented 2 years ago

`class CNN(object):

def __init__(self, model_filepath):

    self.model_filepath = model_filepath
    self.load_graph(model_filepath = self.model_filepath)

def load_graph(self, model_filepath):
    print('Loading model...')
    self.graph = tf.Graph()
    self.sess = tf.compat.v1.InteractiveSession(graph = self.graph)

    with tf.compat.v1.gfile.GFile(model_filepath, 'rb') as f:
        graph_def = tf.compat.v1.GraphDef()
        graph_def.ParseFromString(f.read())

    print('Check out the input placeholders:')
    nodes = [n.name + ' => ' +  n.op for n in graph_def.node if n.op in ('Placeholder')]
    for node in nodes:
        print(node)

    # Define input tensor
    self.input = tf.compat.v1.placeholder(np.float32, shape = [None, 224, 224, 3], name='input')
    # self.dropout_rate = tf.placeholder(tf.float32, shape = [], name = 'dropout_rate')

    tf.import_graph_def(graph_def, {'input_1': self.input})

    print('Model loading complete!')

    # Get layer names
    layers = [op.name for op in self.graph.get_operations()]
    for layer in layers:
        print(layer)

def test(self, data):

    # Know your output node name
    output_tensor1,output_tensor2 ,output_tensor3  = self.graph.get_tensor_by_name('import/age_pred/Softmax: 0'),self.graph.get_tensor_by_name('import/gender_pred/Sigmoid: 0'),self.graph.get_tensor_by_name('import/ethnicity_pred/Softmax: 0')
    output = self.sess.run([output_tensor1,output_tensor2 ,output_tensor3], feed_dict = {self.input: data})

    return output`

Using this code load "age_gender_ethnicity_224_deep-03-0.13-0.97-0.88.pb" and predict on it. But when predicting on images, every time I am getting same output array.

[array([[0.01319346, 0.00229602, 0.00176407, 0.00270929, 0.01408699, 0.00574261, 0.00756087, 0.01012164, 0.01221055, 0.01821703, 0.01120028, 0.00936489, 0.01003029, 0.00912451, 0.00813381, 0.00894791, 0.01277262, 0.01034999, 0.01053109, 0.0133063 , 0.01423471, 0.01610439, 0.01528896, 0.01825454, 0.01722076, 0.01933933, 0.01908059, 0.01899827, 0.01919533, 0.0278129 , 0.02204996, 0.02146631, 0.02125309, 0.02146868, 0.02230236, 0.02054285, 0.02096066, 0.01976574, 0.01990371, 0.02064857, 0.01843528, 0.01697922, 0.01610838, 0.01458549, 0.01581902, 0.01377539, 0.01298613, 0.01378927, 0.01191105, 0.01335083, 0.01154454, 0.01118198, 0.01019558, 0.01038121, 0.00920709, 0.00902615, 0.00936321, 0.00969135, 0.00867239, 0.00838663, 0.00797724, 0.00756043, 0.00890809, 0.00758041, 0.00743711, 0.00584346, 0.00555749, 0.00639214, 0.0061864 , 0.00784793, 0.00532241, 0.00567684, 0.00481544, 0.0052173 , 0.00513186, 0.00394571, 0.00415856, 0.00384584, 0.00452774, 0.0041736 , 0.00328163, 0.00327138, 0.00297012, 0.00369216, 0.00284221, 0.00255897, 0.00285459, 0.00232105, 0.00228869, 0.00218005, 0.0021927 , 0.00236659, 0.00233843, 0.00204793, 0.00209861, 0.00231407, 0.00145706, 0.00179674, 0.00186183, 0.00221309]], dtype=float32), array([[0.62949586]], dtype=float32), array([[0.21338916, 0.19771543, 0.19809113, 0.19525865, 0.19554558]], dtype=float32)] Is there something am missing or is this .pb file not meant for predicting?

av-savchenko commented 2 years ago

This model was taken from my other repository HSE_FaceRec_tf. It definitely can be used to predict age, gender and ethnicity. You could find a class that works with this model here. The high-level interface is demonstrated in this notebook. I used slightly different way to load the model. Also, please pay attention to preprocessing of the input image. You can find the details in the load_age_gender() method of my FacialProcessing class from external repository

sneakatyou commented 2 years ago

Thank you for the quick reply, am able to get the outputs now. 👍 One more thing can you confirm that if it is based on mobilenet v1?

av-savchenko commented 2 years ago

Ok. good! Yes, it is MobileNet v1. The script used to train this model, is also available in another repository.

sneakatyou commented 2 years ago

Okay thanks