Since the file from the memmapped file is no longer the standard GraphDef protocol, you need to make some changes to the download code. You can see an example of this in the iOS camera demo application, the LoadMemoryMappedModel() function: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/ios_examples/camera/tensorflow_utils.mm#L159
The same code (with Objective-C calls to get replaced file names) can also be used on other platforms. Since we used memory mapping, we need to start by creating a special TensorFlow environment object that is configured using a file using:
std::unique_ptr<tensorflow::MemmappedEnv> memmapped_env; memmapped_env->reset( new tensorflow::MemmappedEnv(tensorflow::Env::Default())); tensorflow::Status mmap_status = (memmapped_env->get())->InitializeFromFile(file_path);
Then you need to transfer this environment for subsequent calls, for example, to load a chart.
tensorflow::GraphDef tensorflow_graph; tensorflow::Status load_graph_status = ReadBinaryProto( memmapped_env->get(), tensorflow::MemmappedFileSystem::kMemmappedPackageDefaultGraphDef, &tensorflow_graph);
You also need to create a session with a pointer to the environment you created:
tensorflow::SessionOptions options; options.config.mutable_graph_options() ->mutable_optimizer_options() ->set_opt_level(::tensorflow::OptimizerOptions::L0); options.env = memmapped_env->get(); tensorflow::Session* session_pointer = nullptr; tensorflow::Status session_status = tensorflow::NewSession(options, &session_pointer);
It should be noted here that automatic optimization was also turned off, because in some cases they will reset constant subtrees and, thus, create copies of tensor values ββthat we donβt want and use more RAM. This setting also means that itβs difficult to use a model stored as an APK resource in Android, since they are compressed and do not have normal file names. Instead, you will need to copy the file from the APK to the usual location of the file.
Once you complete these steps, you can use the session and schedule as usual, and you should see a reduction in load time and memory usage.
Pete warden
source share