Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TOCO failed Batch normalization resolution requires that mean, multiplier and offset arrays be constant. #23253

Closed
sxsxsx opened this issue Oct 25, 2018 · 18 comments
Assignees
Labels
comp:lite TF Lite related issues

Comments

@sxsxsx
Copy link

sxsxsx commented Oct 25, 2018

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: None
  • TensorFlow installed from (source or binary): binary
  • TensorFlow version (use command below): 1.9 gpu
  • Python version: 27
  • Bazel version (if compiling from source): no use
  • GCC/Compiler version (if compiling from source): no use
  • CUDA/cuDNN version: CUDA9.1 CuDNN7.0
  • GPU model and memory: GTX1070 8G

Describe the current behavior
toco --output_file=test.tflite --graph_def_file=freeze.pb --input_arrays=Placeholder --output_arrays=logits/BatchNorm/Reshape_1 --output_format=TFLITE --inference_type=FLOAT --std_dev_values=1 --mean_values=0

failed

Traceback (most recent call last):
File "/home/icare/.local/bin/toco", line 11, in
sys.exit(main())
File "/home/icare/.local/lib/python2.7/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 320, in main
app.run(main=run_main, argv=sys.argv[:1])
File "/home/icare/.local/lib/python2.7/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "/home/icare/.local/lib/python2.7/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 316, in run_main
_convert_model(tflite_flags)
File "/home/icare/.local/lib/python2.7/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", line 121, in _convert_model
output_data = converter.convert()
File "/home/icare/.local/lib/python2.7/site-packages/tensorflow/contrib/lite/python/lite.py", line 309, in convert
allow_custom_ops=self.allow_custom_ops)
File "/home/icare/.local/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 225, in toco_convert
input_data.SerializeToString())
File "/home/icare/.local/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 107, in toco_convert_protos
(stdout, stderr))
RuntimeError: TOCO failed see console for info.
2018-10-25 17:33:59.010229: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] Before Removing unused ops: 135 operators, 224 arrays (0 quantized)
2018-10-25 17:33:59.011116: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] After Removing unused ops pass 1: 127 operators, 212 arrays (0 quantized)
2018-10-25 17:33:59.012209: I tensorflow/contrib/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 127 operators, 212 arrays (0 quantized)
2018-10-25 17:33:59.012368: F tensorflow/contrib/lite/toco/graph_transformations/resolve_batch_normalization.cc:42] Check failed: IsConstantParameterArray(*model, bn_op->inputs[1]) && IsConstantParameterArray(*model, bn_op->inputs[2]) && IsConstantParameterArray(*model, bn_op->inputs[3]) Batch normalization resolution requires that mean, multiplier and offset arrays be constant.
Aborted (core dumped)

None

Describe the expected behavior

I want to use toco to transform the pb file to the tflite file

Code to reproduce the issue

I use the code below to generate graph.pb and ckpy file
g = tf.get_default_graph() graph_def = g.as_graph_def() tf.train.write_graph(graph_def, "./model", 'graph.pb', as_text=False) saver = tf.train.Saver() saver.save(sess, os.path.join(FLAGS.checkpoint_dir, 'mnist-conv-slim.ckpt'))

and then I freezed the graph.pb successfullly

python freeze_graph.py --input_graph=/payh/to/graph.pb --input_checkpoint=/payh/tpo/mnist-conv-slim.ckpt --output_graph=/payh/to/mobile_face/model/freeze.pb --output_node_names=logits/BatchNorm/Reshape_1 --input_binary=True

Other info / logs
I use the code below to watch the nodes of each layer

import tensorflow as tf gf = tf.GraphDef() #gf.ParseFromString(open('/tmp/inception_v3_quantized.pb','rb').read()) gf.ParseFromString(open('./model/freeze.pb','rb').read()) for n in gf.node: print ( n.name +' ===> '+n.op )

result:

Placeholder ===> Placeholder
Placeholder_1 ===> Placeholder
Placeholder_3 ===> Placeholder
Reshape/shape ===> Const
Reshape ===> Reshape
conv1/weights ===> Const
conv1/weights/read ===> Identity
conv1/Conv2D ===> Conv2D
conv1/BatchNorm/Const ===> Const
conv1/BatchNorm/beta ===> Const
conv1/BatchNorm/beta/read ===> Identity
conv1/BatchNorm/moving_mean ===> Const
conv1/BatchNorm/moving_mean/read ===> Identity
conv1/BatchNorm/moving_variance ===> Const
conv1/BatchNorm/moving_variance/read ===> Identity
conv1/BatchNorm/cond/Switch ===> Switch
conv1/BatchNorm/cond/switch_t ===> Identity
conv1/BatchNorm/cond/pred_id ===> Identity
conv1/BatchNorm/cond/Const ===> Const
conv1/BatchNorm/cond/Const_1 ===> Const
conv1/BatchNorm/cond/FusedBatchNorm ===> FusedBatchNorm
conv1/BatchNorm/cond/FusedBatchNorm/Switch ===> Switch
conv1/BatchNorm/cond/FusedBatchNorm/Switch_1 ===> Switch
conv1/BatchNorm/cond/FusedBatchNorm/Switch_2 ===> Switch
conv1/BatchNorm/cond/FusedBatchNorm_1 ===> FusedBatchNorm
conv1/BatchNorm/cond/FusedBatchNorm_1/Switch ===> Switch
conv1/BatchNorm/cond/FusedBatchNorm_1/Switch_1 ===> Switch
conv1/BatchNorm/cond/FusedBatchNorm_1/Switch_2 ===> Switch
conv1/BatchNorm/cond/FusedBatchNorm_1/Switch_3 ===> Switch
conv1/BatchNorm/cond/FusedBatchNorm_1/Switch_4 ===> Switch
conv1/BatchNorm/cond/Merge ===> Merge
conv1/CRelu/Neg ===> Neg
conv1/CRelu/axis ===> Const
conv1/CRelu ===> ConcatV2
conv1/CRelu/Relu ===> Relu
pool1/MaxPool ===> MaxPool
conv2/weights ===> Const
conv2/weights/read ===> Identity
conv2/Conv2D ===> Conv2D
conv2/BatchNorm/Const ===> Const
conv2/BatchNorm/beta ===> Const
conv2/BatchNorm/beta/read ===> Identity
conv2/BatchNorm/moving_mean ===> Const
conv2/BatchNorm/moving_mean/read ===> Identity
conv2/BatchNorm/moving_variance ===> Const
conv2/BatchNorm/moving_variance/read ===> Identity
conv2/BatchNorm/cond/Switch ===> Switch
conv2/BatchNorm/cond/switch_t ===> Identity
conv2/BatchNorm/cond/pred_id ===> Identity
conv2/BatchNorm/cond/Const ===> Const
conv2/BatchNorm/cond/Const_1 ===> Const
conv2/BatchNorm/cond/FusedBatchNorm ===> FusedBatchNorm
conv2/BatchNorm/cond/FusedBatchNorm/Switch ===> Switch
conv2/BatchNorm/cond/FusedBatchNorm/Switch_1 ===> Switch
conv2/BatchNorm/cond/FusedBatchNorm/Switch_2 ===> Switch
conv2/BatchNorm/cond/FusedBatchNorm_1 ===> FusedBatchNorm
conv2/BatchNorm/cond/FusedBatchNorm_1/Switch ===> Switch
conv2/BatchNorm/cond/FusedBatchNorm_1/Switch_1 ===> Switch
conv2/BatchNorm/cond/FusedBatchNorm_1/Switch_2 ===> Switch
conv2/BatchNorm/cond/FusedBatchNorm_1/Switch_3 ===> Switch
conv2/BatchNorm/cond/FusedBatchNorm_1/Switch_4 ===> Switch
conv2/BatchNorm/cond/Merge ===> Merge
conv2/CRelu/Neg ===> Neg
conv2/CRelu/axis ===> Const
conv2/CRelu ===> ConcatV2
conv2/CRelu/Relu ===> Relu
pool2/MaxPool ===> MaxPool
Flatten/flatten/Shape ===> Shape
Flatten/flatten/strided_slice/stack ===> Const
Flatten/flatten/strided_slice/stack_1 ===> Const
Flatten/flatten/strided_slice/stack_2 ===> Const
Flatten/flatten/strided_slice ===> StridedSlice
Flatten/flatten/Reshape/shape/1 ===> Const
Flatten/flatten/Reshape/shape ===> Pack
Flatten/flatten/Reshape ===> Reshape
fc1/weights ===> Const
fc1/weights/read ===> Identity
fc1/MatMul ===> MatMul
fc1/BatchNorm/Reshape/shape ===> Const
fc1/BatchNorm/Reshape ===> Reshape
fc1/BatchNorm/beta ===> Const
fc1/BatchNorm/beta/read ===> Identity
fc1/BatchNorm/Const ===> Const
fc1/BatchNorm/moving_mean ===> Const
fc1/BatchNorm/moving_mean/read ===> Identity
fc1/BatchNorm/moving_variance ===> Const
fc1/BatchNorm/moving_variance/read ===> Identity
fc1/BatchNorm/cond/Switch ===> Switch
fc1/BatchNorm/cond/switch_t ===> Identity
fc1/BatchNorm/cond/pred_id ===> Identity
fc1/BatchNorm/cond/Const ===> Const
fc1/BatchNorm/cond/Const_1 ===> Const
fc1/BatchNorm/cond/FusedBatchNorm ===> FusedBatchNorm
fc1/BatchNorm/cond/FusedBatchNorm/Switch ===> Switch
fc1/BatchNorm/cond/FusedBatchNorm/Switch_1 ===> Switch
fc1/BatchNorm/cond/FusedBatchNorm/Switch_2 ===> Switch
fc1/BatchNorm/cond/FusedBatchNorm_1 ===> FusedBatchNorm
fc1/BatchNorm/cond/FusedBatchNorm_1/Switch ===> Switch
fc1/BatchNorm/cond/FusedBatchNorm_1/Switch_1 ===> Switch
fc1/BatchNorm/cond/FusedBatchNorm_1/Switch_2 ===> Switch
fc1/BatchNorm/cond/FusedBatchNorm_1/Switch_3 ===> Switch
fc1/BatchNorm/cond/FusedBatchNorm_1/Switch_4 ===> Switch
fc1/BatchNorm/cond/Merge ===> Merge
fc1/BatchNorm/Shape ===> Shape
fc1/BatchNorm/Reshape_1 ===> Reshape
fc1/CRelu/Neg ===> Neg
fc1/CRelu/axis ===> Const
fc1/CRelu ===> ConcatV2
fc1/CRelu/Relu ===> Relu
Dropout/sub/x ===> Const
Dropout/sub ===> Sub
Dropout/sub_1/x ===> Const
Dropout/sub_1 ===> Sub
Dropout/dropout_1/Shape ===> Shape
Dropout/dropout_1/random_uniform/min ===> Const
Dropout/dropout_1/random_uniform/max ===> Const
Dropout/dropout_1/random_uniform/RandomUniform ===> RandomUniform
Dropout/dropout_1/random_uniform/sub ===> Sub
Dropout/dropout_1/random_uniform/mul ===> Mul
Dropout/dropout_1/random_uniform ===> Add
Dropout/dropout_1/add ===> Add
Dropout/dropout_1/Floor ===> Floor
Dropout/dropout_1/div ===> RealDiv
Dropout/dropout_1/mul ===> Mul
logits/weights ===> Const
logits/weights/read ===> Identity
logits/MatMul ===> MatMul
logits/BatchNorm/Reshape/shape ===> Const
logits/BatchNorm/Reshape ===> Reshape
logits/BatchNorm/beta ===> Const
logits/BatchNorm/beta/read ===> Identity
logits/BatchNorm/Const ===> Const
logits/BatchNorm/moving_mean ===> Const
logits/BatchNorm/moving_mean/read ===> Identity
logits/BatchNorm/moving_variance ===> Const
logits/BatchNorm/moving_variance/read ===> Identity
logits/BatchNorm/cond/Switch ===> Switch
logits/BatchNorm/cond/switch_t ===> Identity
logits/BatchNorm/cond/pred_id ===> Identity
logits/BatchNorm/cond/Const ===> Const
logits/BatchNorm/cond/Const_1 ===> Const
logits/BatchNorm/cond/FusedBatchNorm ===> FusedBatchNorm
logits/BatchNorm/cond/FusedBatchNorm/Switch ===> Switch
logits/BatchNorm/cond/FusedBatchNorm/Switch_1 ===> Switch
logits/BatchNorm/cond/FusedBatchNorm/Switch_2 ===> Switch
logits/BatchNorm/cond/FusedBatchNorm_1 ===> FusedBatchNorm
logits/BatchNorm/cond/FusedBatchNorm_1/Switch ===> Switch
logits/BatchNorm/cond/FusedBatchNorm_1/Switch_1 ===> Switch
logits/BatchNorm/cond/FusedBatchNorm_1/Switch_2 ===> Switch
logits/BatchNorm/cond/FusedBatchNorm_1/Switch_3 ===> Switch
logits/BatchNorm/cond/FusedBatchNorm_1/Switch_4 ===> Switch
logits/BatchNorm/cond/Merge ===> Merge
logits/BatchNorm/Shape ===> Shape
logits/BatchNorm/Reshape_1 ===> Reshape

It seems that the BN para are already constant

what should I do to solver the problem?

good luck to you thanks

@ymodak ymodak added the comp:lite TF Lite related issues label Oct 25, 2018
@ymodak ymodak assigned ymodak and liyunlu0618 and unassigned ymodak Oct 25, 2018
@miaout17 miaout17 assigned liyunlu0618 and unassigned liyunlu0618 Oct 25, 2018
@brucechou1983
Copy link

same problem here (tensorflow-gpu 1.11.0)

@sxsxsx
Copy link
Author

sxsxsx commented Oct 29, 2018

what? I am not understand

@tung238
Copy link

tung238 commented Nov 8, 2018

I'm also facing same problem with facenet pretrain model, tensorflow 1.10.0, here is my command:

tflite_convert --graph_def_file=optimized_facenet.pb \
  --output_file=optimized_graph_quantized.tflite \
  --output_format=TFLITE \
  --input_shape=1,160,160,3 \
  --input_array=input \
  --output_array=embeddings \
  --inference_type=QUANTIZED_UINT8 \
  --std_dev_values=128 --mean_values=128 \
  --default_ranges_min=-6 --default_ranges_max=6 \
  --quantize_weights

@liyunlu0618
Copy link
Contributor

@tung238 is your model trained with quantization? 'inference_type=QUANTIZED_UINT8' and 'quantize_weights' are not meant to be used at the same time. If your model was trained with quantization, use 'inference_type=QUANTIZED_UINT8' and remove 'quantize_weights'.

@liyunlu0618
Copy link
Contributor

@sxsxsx Can you attach your model here?

@JitenderMaurya
Copy link

I am trying to convert a trained tensorflow model to tensorflow lite

i have tensorflow 1.10 with bazel 0.15.0
this is command i am using
bazel-bin/tensorflow/contrib/lite/toco/toco --input_file="/home/jitender/lesslayerLite/Nfrozen2.pb" --output_file="/home/jitender/lesslayerLite/handLite.tflite" --input_shapes=1,160,120,3 --input_arrays=Placeholder --output_arrays='sigmoid_activation' --inference_type=FLOAT --output_format=TFLITE
tocoerror

@vasusingla
Copy link

vasusingla commented Dec 7, 2018

I am facing the same problem with my conversion, I'm attaching my model below.

Command to convert -
toco --graph_def_file frozen_graph.pb --output_file converted.tflite --input_arrays input --output_arrays embeddings --input_shape 1,160,160,3

frozen_graph.zip

@liyunlu0618

@liyunlu0618
Copy link
Contributor

Looks like you're trying to convert a training graph. You first need to get a frozen EVAL graph and then convert it.

@JitenderMaurya
Copy link

@liyunlu0618 my model is frozen.

@dhasan
Copy link

dhasan commented Dec 12, 2018

I have the same issue

@Junan007
Copy link

i have the same issue too.

@dankal444
Copy link

I have the same issue as well

@scm-ns
Copy link

scm-ns commented Feb 13, 2019

Same issue. Is there a solution for this?

@Lyken17
Copy link

Lyken17 commented Feb 18, 2019

same problem here, I am using tf.slim.batch_norm.

@Junan007
Copy link

I've solved my problem, my model uses a placeholder to initialize Batchnorm, so my exported diagram contains some Op nodes for training purposes.
After I made the following modifications, the problem was solved after refreezing the diagram.
such as:
is_training = tf.placeholder (tf.bool, name = 'is_training')
sess.run (net, feed_dict= {x: x, is_training: is_training})
=>
is_training = False
sess.run (net, feed_dict= {x: x, is_training: is_training})

@Junan007
Copy link

I think in the process of converting pb to tflite, everything wrong with batchnorm, that means this graph is not a clean prediction graph, is that right?

@Monikasinghjmi
Copy link

anyone has a concrete solution to this? @liyunlu0618 can you elaborate more on this?

@MeghnaNatraj
Copy link
Member

Marking issue as resolved due to inactivity. Feel free to re-open this if it's unresolved or file a new issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
comp:lite TF Lite related issues
Projects
None yet
Development

No branches or pull requests