How to read tensor stream memory diagram graphic file in android?

Using Tensorflow 1.0.1 allows you to read the optimized graph and the quantized graph in android using the TensorFlowImageClassifier.create method, for example:

classifier = TensorFlowImageClassifier.create( c.getAssets(), MODEL_FILE, LABEL_FILE, IMAGE_SIZE, IMAGE_MEAN, IMAGE_STD, INPUT_NAME, OUTPUT_NAME); 

But according to the Peter Warden blog ( https://petewarden.com/2016/09/27/tensorflow-for-mobile-poets/ ), it is recommended to use graphics with memory display on a mobile phone to avoid memory-related crashes.

I built a memmapped graph using

 bazel-bin/tensorflow/contrib/util/convert_graphdef_memmapped_format \ --in_graph=/tf_files/rounded_graph.pb \ --out_graph=/tf_files/mmapped_graph.pb 

and it created perfectly, but when I tried to upload the file using TensorFlowImageClassifier.create (...), it says that the file is not a valid image file.

On iOS, it's ok to upload a file using

 LoadMemoryMappedModel( model_file_name, model_file_type, &tf_session, &tf_memmapped_env); 

since it has a way to display mapped memory.

So, I assume that in android there is a similar function, but I could not find it.

Can someone help me download a graphical graph with memory in android?

+2
android tensorflow
source share
1 answer

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.

+3
source share

All Articles