tensorflow: TOCO failed Batch normalization resolution requires that mean, multiplier and offset arrays be constant.
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 <module> 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
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 18 (4 by maintainers)
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})
Looks like you’re trying to convert a training graph. You first need to get a frozen EVAL graph and then convert it.
@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’.