Skip to content

Feature Request : ability to load Caffe/Tensorflow from FileStorage or from similar in-memory #9765

Closed

Description

@r2d3
System information (version)
  • OpenCV => 3.3
  • Operating System / Platform => macOS 10.11
  • Compiler => Xcode 8.2.1 / clang-800.0.42.1
Detailed description

Like we could load cv::ml::SVM from a cv::FileStorage :

cv::FileStorage fs;
m_svm = cv::Algorithm::read<cv::ml::SVM>(fs["SVM"]);

It will look like : cv::dnn::Net = readNetFromTensorflow(fs["Model"]);

Activity

  1. r2d3 commented on Oct 4, 2017

    @r2d3
    ContributorAuthor

    Hello @dkurt,

    the problem is not compressing the data. The use case is to load data that are not on disk or not in a .pb/.caffemodel/.prototxt file.

    For example, the binary stream representing my pb/caffemodel file could be embedded in a .xml/.yml file loaded via FileStorage.

    Presenting an API that accepts FileStorage/FileNode as input for readNetFrom*() will extend the way we could read it.

    For example, readNetFromTensorflow is using a std::ifstream internally, so perhaps just exposing an API that accepts an std::istream could be OK.

  2. Solonets commented on Oct 31, 2017

    @Solonets

    That is a must-have feature: Raw model files are not usually accessible in final products: they can be embedded, encrypted or even dynamic.

  3. self-assigned this
    on Oct 31, 2017
  4. r2d3 commented on Oct 31, 2017

    @r2d3
    ContributorAuthor

    I have written a first version for TensorFlow : https://github.com/r2d3/opencv/tree/dnn_memory_load

    https://github.com/r2d3/opencv/blob/dnn_memory_load/modules/dnn/src/tensorflow/tf_io.cpp
    using
    ZeroCopyInputStream* raw_input = new ArrayInputStream(data, len);
    instead of
    ZeroCopyInputStream* raw_input = new IstreamInputStream(&fs);

    But at the moment, I have to think at the API to give. At the moment, I named it:
    Net readNetFromTensorflowBuffer(const char *buffer, size_t len);

    I am not sure if there is a "prettier" way to go.

    I could make a PR for this patch, it will give a starting point

  5. dkurt commented on Oct 31, 2017

    @dkurt
    Member

    @r2d3, As you mentioned, Google Protobuf -based importers open file streams internally. You may start from Caffe and TensorFlow importers. I think it's not necessary to name methods differently. Just something like:

    readNetFromCaffe(char*, int, char*, int);
    readNetFromCaffe(std::istream&, std::istream&);
    readNetFromCaffe(const String&, const String&);
    

    Also we need to check Python/Java wrappers for it.
    Can I assign you to this issue?

  6. r2d3 commented on Oct 31, 2017

    @r2d3
    ContributorAuthor

    Yes, you can assign this issue to me

    I will use the proposed naming convention

    I will add loader for other framework than TensorFlow. Probably we will need to add this to the tests

  7. Solonets commented on Oct 31, 2017

    @Solonets

    @r2d3 Keep us informed, I can help with it as well.

  8. r2d3 commented on Nov 2, 2017

    @r2d3
    ContributorAuthor

    I submitted a first PullRequest to discuss various question:

    • I added a read_inception_buffer test. I should add a model+config buffer test. Adding it for all runTensorFlowNet calls is perhaps overkill
    • Do we really need an std::istream version ?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions