Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to load trained DNN (in C++) for face recognition in Python #483

Closed
anirudt opened this issue Mar 16, 2017 · 19 comments
Closed

Unable to load trained DNN (in C++) for face recognition in Python #483

anirudt opened this issue Mar 16, 2017 · 19 comments

Comments

@anirudt
Copy link

anirudt commented Mar 16, 2017

I trained a DNN as given in the examples/dnn_metric_learning_on_images_ex.cpp file with a custom image dataset. This saved a file as metric_network_renset.dat. However, while trying to load the model in Python using the API dlib.face_recognition_model_v1("metric_network_renset.dat"), it gives the following error:

$ python dlib-worker.py
Traceback (most recent call last):
  File "dlib-worker.py", line 29, in <module>
    facerec = dlib.face_recognition_model_v1(face_rec_model_path)
RuntimeError: Unexpected version found while deserializing dlib::input_rgb_image_sized.

However, after trying in C++, the deserializing and loading of the model is working perfectly, as in examples/dnn_face_recognition_ex.cpp.

Basically, I would want to know how to load a self-trained network in Python for post-processing, whether it is possible, and/or if face_recognition_model_v1() is the right API for it.

@davisking
Copy link
Owner

The network definitions used in dnn_metric_learning_on_images_ex.cpp and the dnn_face_recogition_ex.cpp are different. That's why you are getting this error. If you want to use the same python code to load a model it's got to be a model that uses the same network definition as the face recognition code.

@anirudt
Copy link
Author

anirudt commented Mar 16, 2017

My point is quite different. In fact, the dnn_face_recognition_ex.cpp is able to deserialize and load the model. However, the Python code is failing at even loading a model.

@anirudt
Copy link
Author

anirudt commented Mar 16, 2017

Does the face_recognition_model_v1 API expect only a certain network configuration?

@davisking
Copy link
Owner

There is no way dnn_face_recogition_ex.cpp is loading the model file generated by dnn_metric_learning_on_images_ex.cpp since the network definitions are different. Specifically they are different in the input layer which is what your error message says. So you must have mixed up files if you think this is happening. There isn't anything magic about the python API. It's just calling the C++ code.

@anirudt
Copy link
Author

anirudt commented Mar 16, 2017

Okay, we can forget about dnn_face_recognition_ex.cpp for the moment.

The error message that I have generated is clearly from running the Python code!

Do I have to use some other API in Python for loading a self trained model?

@davisking
Copy link
Owner

face_recognition_model_v1 loads only the type of model used by the face recognition code. That's the only thing exposed to python right now.

@anirudt
Copy link
Author

anirudt commented Mar 16, 2017

So I'll have to change the network accordingly for my use at python/src file, right?

Thanks!

@davisking
Copy link
Owner

davisking commented Mar 16, 2017 via email

@anirudt
Copy link
Author

anirudt commented Mar 16, 2017

Right, it worked!

On a side note, could we create an API in Python to instantiate a network configuration? It would be neater to specify a certain config and then load the corresponding learnt model.

We probably need to find a way to make a data structure that encodes the layers, and then it could be done?

@davisking
Copy link
Owner

davisking commented Mar 16, 2017 via email

@anguoyang
Copy link

hi@davisking, could you please add a new version of dnn_metric_learning_on_images_ex.cpp
then we could use it to train a metric_network_renset.dat which could be used in dnn_face_recogition_ex.cpp ?

@anguoyang
Copy link

"Also, the dnn_metric_learning_on_images_ex.cpp example shows how to train this network.
// The dlib_face_recognition_resnet_model_v1 model used by this example was trained using
// essentially the code shown in dnn_metric_learning_on_images_ex.cpp except the
// mini-batches were made larger (35x15 instead of 5x5), the iterations without progress
// was set to 10000, the jittering you can see below in jitter_image() was used during
// training, and the training dataset consisted of about 3 million images instead of 55.
// Also, the input layer was locked to images of size 150.
"

@tiger-xzp
Copy link

@davisking,I have modified the network architecture and got a model (trained by my dataset) which can be used by dnn_face_recognition_ex.cpp and python API. My question is my dataset has a small volume and my model does not work very well, can i just train the model base your published dlib_face_recognition_resnet_model_v1 model, but not start from 0 ?

@davisking
Copy link
Owner

davisking commented Jan 8, 2018 via email

@tiger-xzp
Copy link

Sorry @davisking , maybe i didn't speak very clearly. I just modified the network architecture in dnn_metric_learning_on_images_ex.cpp and train my model with my dataset. My question is that may i load the dlib_face_recognition_resnet_model_v1.dat you have published to net and then continue my trainning with my custom dataset?

@davisking
Copy link
Owner

No, not if you changed the network architecture to something else.

@tiger-xzp
Copy link

So, is there any hint or example can i refer to ?

@srego
Copy link

srego commented May 12, 2019

Right, it worked!

On a side note, could we create an API in Python to instantiate a network configuration? It would be neater to specify a certain config and then load the corresponding learnt model.

We probably need to find a way to make a data structure that encodes the layers, and then it could be done?

@anirudt How did you change the network configuration on dnn_face_recognition_ex.cpp so that it worked with dlib.face_recognition_model_v1("metric_network_renset.dat")? Thanks so much for the help!

@wangx404
Copy link

Right, it worked!
On a side note, could we create an API in Python to instantiate a network configuration? It would be neater to specify a certain config and then load the corresponding learnt model.
We probably need to find a way to make a data structure that encodes the layers, and then it could be done?

@anirudt How did you change the network configuration on dnn_face_recognition_ex.cpp so that it worked with dlib.face_recognition_model_v1("metric_network_renset.dat")? Thanks so much for the help!

You can just change the structure of the net from input_rgb_image to input_rgb_image_sized<150>. Then compile and run it, and everything will work as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants