Alireza-Akhavan / deep-face-recognition

One-shot Learning and deep face recognition notebooks and workshop materials

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Transferring Models to C++ one

Rasoul20sh opened this issue · comments

Hi Alireza and thanks for sharing your slides.
I run the codes successfully and now I want to transfer it to C++ one. As I know (And also implemented before) we can use .pbtxt and .pb files to do so by using OpenCV. But the model which david sandberg used in his work has different formats. Do you have any idea helping me transferring the code to C++ one?
Thanks again

Hi @Rasoul20sh,

step 1:
You should Freeze graph to get a .pb file.
this articles are helpful:

and also david sandberg has a script for this job:
https://github.com/davidsandberg/facenet/blob/master/src/freeze_graph.py

step 2:

You should load pb file in opencv in c++:
something like this

int main(int argc, char **argv)
{
    cv::dnn::Net net;

    net = cv::dnn::readNetFromTensorflow("./you_model.pb");
    ...

Thanks @Alireza-Akhavan ,
I used sandberg's script to freeze the model (I think he also saved it in his model dir). Freeze_graph.py successfully create .pb file. But as it is mentioned in OpencvDoc we should also have a .pbtxt file to pass to cv::dnn::readNetFromTensorflow() and then use this function as bellow:

net = cv::dnn::readNetFromTensorflow("./your_model.pb" , "./mcon.pbtxt");

But freezing the model did not create any .pbtxt file.

Then I produce .pbtxt file using .pb file and this. I still have error with
net = cv::dnn::readNetFromTensorflow("./your_model.pb" , "./mcon.pbtxt");
Is my way the correct one??

hi @Rasoul20sh
How did you produce .pbtxt file from .pb file?

hi @mehdi1661.
I used this script.

@Rasoul20sh
As a quick answer: possibility of supporting this model in OpenCV is very low. Because OpenCV has just started to add tensorflow layers and some layers(even regular one) are not supported yet, so before trying to freeze graph, you should know which layers of your model are implemented in OpenCV. You can check by your own in OpenCV source code via this Link
I mentioned some of them in the below links:

https://github.com/opencv/opencv/blob/7ad5d219004288b3ec68c6ac0dbbb6c2e2c00e4a/modules/dnn/src/tensorflow/tf_importer.cpp#L565

https://github.com/opencv/opencv/blob/7ad5d219004288b3ec68c6ac0dbbb6c2e2c00e4a/modules/dnn/src/tensorflow/tf_importer.cpp#L748

https://github.com/opencv/opencv/blob/7ad5d219004288b3ec68c6ac0dbbb6c2e2c00e4a/modules/dnn/src/tensorflow/tf_importer.cpp#L1120

Selection_106

Hi @robosina and thanks for your links.
Actually I am using these models: https://drive.google.com/file/d/1EXPBSXwTaqrSC0OhUdXNmKSh9qJUQ55-/view and https://github.com/Alireza-Akhavan/deep-face-recognition/tree/master/models/20180204-160909. I am not sure I could access and modify its layers somehow OpenCV support it.