Convert tensorflow model to code and load it from memory

asked 2019-11-05 03:31:10 -0600

benji gravatar image

Hi, I'm developing a deep learning app with a tensorflow model in a .pb file. I load it with the following code and it works fine:

myNet = cv::dnn::readNetFromTensorflow(modelPath)

However, I need to protect the model, so I'd like to (somehow) convert it to memory before compiling (C++), and load it from memory so the model is not packed with the binaries.

This is the function I'm currently using:

Net cv::dnn::readNetFromTensorflow (const String &model, const String &config=String())

And these are the two functions that I think I may need, but can't make work:

Net cv::dnn::readNetFromTensorflow (const std::vector< uchar > &bufferModel, const std::vector< uchar > &bufferConfig=std::vector< uchar >())

Net cv::dnn::readNetFromTensorflow (const char *bufferModel, size_t lenModel, const char *bufferConfig=NULL, size_t lenConfig=0)

I tried converting the .pb file to a header file with xxd, but the computer runs out of memory trying to compile the program. I searched the docs but didn't find an example on how to convert the file to memory and consume it. Is is possible to do it?

Thank you very much.

edit retag flag offensive close merge delete


how large is your model ? do you have a link ? opencv version ?

berak gravatar imageberak ( 2019-11-05 03:37:22 -0600 )edit

The model weighs 15MB but it may weigh even more in the future (50MB). I tried OpenCV 3.4 and 4.1. I'm afraid I can't share the model :(

benji gravatar imagebenji ( 2019-11-05 03:50:27 -0600 )edit

imho, the idea of converting data into code, and compiling it into your exe works nice for ~200k cascade files or such, but not for your dnn model (data in limited auto memory, 64mb compiler chunk restrictions, etc.)

I need to protect the model

maybe you can encrypt it somehow, and write your own loader/decrypt code, do it at runtime, not at compile time

berak gravatar imageberak ( 2019-11-05 04:08:36 -0600 )edit