Not able to port a 6-layered mobilenet tflite model to mobile · Issue #21368 · tensorflow/tensorflow · GitHub
Skip to content

Not able to port a 6-layered mobilenet tflite model to mobile #21368

Description

@ychen404

Please go to Stack Overflow for help and support:

https://stackoverflow.com/questions/tagged/tensorflow

If you open a GitHub issue, here is our policy:

  1. It must be a bug, a feature request, or a significant problem with documentation (for small docs fixes please send a PR instead).
  2. The form below must be filled out.
  3. It shouldn't be a TensorBoard issue. Those go here.

Here's why we have that policy: TensorFlow developers respond to issues. We want to focus on work that benefits the whole community, e.g., fixing bugs and adding features. Support only helps individuals. GitHub also notifies thousands of people when issues are filed. We want them to see you communicating an interesting problem, rather than being redirected to Stack Overflow.


System information

  • Have I written custom code (as opposed to using a stock example script provided in TensorFlow):
    No.
  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04):
    Ubuntu 16.04
  • Mobile device (e.g. iPhone 8, Pixel 2, Samsung Galaxy) if the issue happens on mobile device:
    Pixel 2
  • TensorFlow installed from (source or binary):
    Source
  • TensorFlow version (use command below):
    1.8
  • Python version:
    2.7
  • Bazel version (if compiling from source):
    0.15.2
  • GCC/Compiler version (if compiling from source):
    5.4.0
  • CUDA/cuDNN version:
    N/A
  • GPU model and memory:
    N/A
  • Exact command to reproduce:
  1. Set the mobileNet model endpoint to conv6-depthwise
  2. Re-train the model from scratch with using cifar10 dataset
  3. Freeze the graph with the checkpoints
    import tensorflow as tf
    from tensorflow.python.framework import graph_util
    import os,sys

output_node_names = "MobilenetV1/Predictions/Reshape"
saver = tf.train.import_meta_graph('/home/users/saman/yitao/tensorflow_android/models/research/slim/batch_32/model.ckpt-156300.meta', clear_devices=True)
graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
sess = tf.Session()
saver.restore(sess, "/home/users/saman/yitao/tensorflow_android/models/research/slim/batch_32/model.ckpt-156300")
output_graph_def = graph_util.convert_variables_to_constants(
sess, # The session is used to retrieve the weights
input_graph_def, # The graph_def is used to retrieve the nodes
output_node_names.split(",") # The output node names are used to select the usefull nodes
)
output_graph="frozen-model-conv6-bat-32.pb"
with tf.gfile.GFile(output_graph, "wb") as f:
f.write(output_graph_def.SerializeToString())
sess.close()

(4) Optimize the model
bazel-bin/tensorflow/tools/graph_transforms/transform_graph
--in_graph=/home/yitao/TF_1.8/tensorflow/my_frozen_pb/frozen-model-conv6-bat-32.pb
--out_graph=/home/yitao/TF_1.8/tensorflow/my_frozen_pb/frozen-model-conv6-bat-32-optimized.pb
--inputs='input'
--outputs='MobilenetV1/Predictions/Reshape'
--transforms='
strip_unused_nodes(type=float, shape="1,32,32,3")
remove_nodes(op=Identity, op=CheckNumerics)
fold_constants(ignore_errors=true)
fold_batch_norms
fold_old_batch_norms

(5) Convert the model to tflite
bazel-bin/tensorflow/contrib/lite/toco/toco
--input_file=/home/yitao/TF_1.8/tensorflow/my_frozen_pb/frozen-model-conv6-bat-32-optimized.pb
--input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE
--output_file=/home/yitao/TF_1.8/tensorflow/my_frozen_pb/frozen-model-conv6-bat-32-optimized.tflite --inference_type=FLOAT
--input_type=FLOAT --input_arrays=input
--seed2
--output_arrays=MobilenetV1/Predictions/Reshape --input_shapes=1,32,32,3
--allow_custom_ops

You can collect some of this information using our environment capture script:

https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh

You can obtain the TensorFlow version with

python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)"

Describe the problem

Describe the problem clearly here. Be sure to convey here why it's a bug in TensorFlow or a feature request.
Not able to train a model from scratch and port it Android to utilize the Android Nerual Network api through TFLite. After training a model and following the steps to convert the graph to tflite model, there are still some ops that are not supported by the TFLite runtime in my graph. What should I do?
Any help is apreciated!

Logcat is throwing the following errors. It seems that those ops are not stripped from the model during the optimization step.

Source code / logs

08-03 15:14:52.183 10271-10271/android.example.com.tflitecamerademo E/AndroidRuntime: FATAL EXCEPTION: main
Process: android.example.com.tflitecamerademo, PID: 10271
java.lang.RuntimeException: Unable to start activity ComponentInfo{android.example.com.tflitecamerademo/com.example.android.tflitecamerademo.CameraActivity}: java.lang.IllegalArgumentException: Internal error: Cannot create interpreter: Didn't find custom op for name 'RandomUniform' with version 1
Didn't find custom op for name 'FLOOR' with version 1
Didn't find custom op for name 'RSQRT' with version 1
Didn't find custom op for name 'FIFOQueueV2' with version 1
Didn't find custom op for name 'QueueDequeueV2' with version 1
Didn't find custom op for name 'SquaredDifference' with version 1
Registration failed.

Metadata

Metadata

Assignees

Labels

comp:liteTF Lite related issues

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions