thoughtworksarts / EmoPy

A deep neural net toolkit for emotion analysis via Facial Expression Recognition (FER)
https://thoughtworksarts.io/blog/emopy-emotional-expression-toolkit/
GNU Affero General Public License v3.0
929 stars 264 forks source link

cannot use my trained model after exporting it #30

Open octopousprime opened 5 years ago

octopousprime commented 5 years ago

Hi I managed to install EmoPy on my ubuntu 18.04. I tested the pre-trained model (fermodel_example.py) and managed to run the convolutional_model.py without error. then I also managed to train my model on a large database of images and export it to the /output directory using :

model.export_model('output/conv2d_model.json','output/conv2d_weights.h5',"output/conv2d_emotion_map.json",{"calm": 0, "anger": 1, "happiness": 2})

Then I changed the path on fermodel.py from :

model_file = 'models/convmodel%s.hdf5' % model_suffix emotion_map_file = 'models/conv_emotionmap%s.json' % model_suffix

to my newly trained model output :

model_file = 'models/conv2d_weights.h5' emotion_map_file = 'models/conv2d_emotion_map.json'

But when I run fermodel.py again I get this error:

error can someone help me find a solution to this error. Thanks

DanielUSPS commented 5 years ago

I would modify src/neuralnets.py to use keras.models.load_model and save_model as .hdf5 files, not .json files.

The problem with saving as json is there is not a function to load a json into a model with weights.

I am able to load and save models as *.hdf5 files. Everything seems to run correctly, but when using the models to predict an emotion, I'm not getting correct answers.

DanielUSPS commented 5 years ago

Also, copy over the predict function from FERModel

DanielUSPS commented 5 years ago

Did you use 64x64 images or 48x48? I changed to 48x48

heshe commented 5 years ago

I had the exact same problem as Octo - i just used the precoded "convolutional_model.py" example, and wanted to try this newly trained model on the provided images. I tried to change the "export_model" as Daniel suggested. And just like Daniel, i can now load my model in fermodel.py by using the constructor FERModel(...). But when i try to predict with this model, i get the following results:

anger: nan%
happiness: nan%
surprise: nan%

Can anyone help me with this? I've changed the export_model to:

def export_model(self, model_filepath, weights_filepath, emotion_map_filepath, emotion_map):
        self.model.save_weights(weights_filepath)
        self.model.save(model_filepath) #My addition

        // And the out-commented this
        //model_json_string = self.model.to_json()
        //model_json_file = open(model_filepath, 'w')
        //model_json_file.write(model_json_string)
        //model_json_file.close()

        with open(emotion_map_filepath, 'w') as fp:
            json.dump(emotion_map, fp)

I don't know if it has anything to do with all the warnings coming from executing your code:

> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   _np_qint8 = np.dtype([("qint8", np.int8, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   _np_qint16 = np.dtype([("qint16", np.int16, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   _np_qint32 = np.dtype([("qint32", np.int32, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   np_resource = np.dtype([("resource", np.ubyte, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   _np_qint8 = np.dtype([("qint8", np.int8, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   _np_qint16 = np.dtype([("qint16", np.int16, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   _np_qint32 = np.dtype([("qint32", np.int32, 1)])
> > C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
> >   np_resource = np.dtype([("resource", np.ubyte, 1)])
> > Initializing own model
> > WARNING:tensorflow:From C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\keras\backend\tensorflow_backend.py:541: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.
> > 
> > WARNING:tensorflow:From C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\keras\backend\tensorflow_backend.py:4432: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.
> > 
> > WARNING:tensorflow:From C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\keras\backend\tensorflow_backend.py:4267: The name tf.nn.max_pool is deprecated. Please use tf.nn.max_pool2d instead.
> > 
> > WARNING:tensorflow:From C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\keras\backend\tensorflow_backend.py:190: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.
> > 
> > WARNING:tensorflow:From C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\keras\backend\tensorflow_backend.py:197: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.
> > 
> > WARNING:tensorflow:From C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\keras\backend\tensorflow_backend.py:203: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.
> > 
> > 2019-09-10 14:42:29.257819: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
> > WARNING:tensorflow:From C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\keras\optimizers.py:793: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.
> > 
> > WARNING:tensorflow:From C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\tensorflow\python\ops\math_grad.py:1250: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
> > Instructions for updating:
> > Use tf.where in 2.0, which has the same broadcast rule as np.where
> > anger: nan%
> > happiness: nan%
> > surprise: nan%
> > (emoPyEnv) PS C:\Users\tdhes\Anaconda3\envs\emoPyEnv\lib\site-packages\EmoPy\examples>
> 
xuv commented 5 years ago

@heshe Thank you for your comment. I can't answer your question, but for readability, could you please encapsulate the console logs in code syntax. It's going to be easier to read. Thank you.