cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Contributor
Contributor
398 Views
Registered: ‎08-02-2019

Batch Normalization Error

Jump to solution

Hi,

I am trying to run a Inception V3 model from keras.applications. Why do I get the following error when I run it in decent ?

INFO: Checking Float Graph...
INFO: Float Graph Check Done.
Traceback (most recent call last):
File "/home/miniconda3/envs/decent/bin/decent_q", line 8, in <module>
sys.exit(run_main())
File "/home/miniconda3/envs/decent/lib/python3.6/site-packages/tensorflow/contrib/decent_q/python/decent_q.py", line 540, in run_main
app.run(main=my_main, argv=[sys.argv[0]] + unparsed)
File "/home/miniconda3/envs/decent/lib/python3.6/site-packages/tensorflow/python/platform/app.py", line 125, in run
_sys.exit(main(argv))
File "/home/miniconda3/envs/decent/lib/python3.6/site-packages/tensorflow/contrib/decent_q/python/decent_q.py", line 539, in <lambda>
my_main = lambda unused_args: main(unused_args, FLAGS)
File "/home/miniconda3/envs/decent/lib/python3.6/site-packages/tensorflow/contrib/decent_q/python/decent_q.py", line 335, in main
quantize_frozen(input_graph_def, input_fn, q_config, s_config, flags.skip_check)
File "/home/miniconda3/envs/decent/lib/python3.6/site-packages/tensorflow/contrib/decent_q/python/decent_q.py", line 225, in quantize_frozen
q_config, s_config)
File "/home/miniconda3/envs/decent/lib/python3.6/site-packages/tensorflow/contrib/decent_q/python/decent_q.py", line 156, in calibrate_frozen
calib_graph_def = CreateQuantizeCalibrationGraphDef(input_graph_def, q_config)
File "/home/miniconda3/envs/decent/lib/python3.6/site-packages/tensorflow/contrib/decent_q/python/quantize_graph.py", line 169, in CreateQuantizeCalibrationGraphDef
input_graph_def_string, config.to_string(), status)
File "/home/miniconda3/envs/decent/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Beta input to batch norm has bad shape: [64]

Does DECENT not support Batch Normalization?

I also tested other models like Resnet50, MobileNet, VGG from keras applications. Only VGG models are able to run as they don't have Batch Normalization.

0 Kudos
1 Solution

Accepted Solutions
Highlighted
Visitor
Visitor
292 Views
Registered: ‎10-22-2019

Re: Batch Normalization Error

Jump to solution

Greetings, I've figured out the issue.

This problem occurs when you didn't convert the model correctly. if your create a SINGLE Python script which does the following workflow you would have this error.

  • create a Keras model --> train or fine tune it -->  convert the Keras model to Tensorflow model

A better choice is:

  1. build and train your Keras model, save it as .h5 file
  2. then load it with ANOTHER python script following this example, make sure that you have set "backend.set_learning_phase(0)" at the beginnning of your script. Then convert the Keras model to TF model.
  3. then calling FREEZE and DECENT_Q would be fine

My assumption about this problem is that it happens when the model is not correctly switched to "inference mode". Because batch norm has different behaviours during training and inference, DECENT_Q would have trouble when it tries to quantize a batch norm layer which is still in "training mode".

View solution in original post

3 Replies
Highlighted
Visitor
Visitor
324 Views
Registered: ‎10-22-2019

Re: Batch Normalization Error

Jump to solution

Having the same issue when I use Keras model.

I have checked the offical frozen graph in Xilinx model zoo, a lot of tensorflow models contain BN layers and they work well. So I doubt the problem might be caused by the Keras implementation of batch norm.

Perhaps have to use original Tensorflow instead of Keras...

0 Kudos
Highlighted
Visitor
Visitor
293 Views
Registered: ‎10-22-2019

Re: Batch Normalization Error

Jump to solution

Greetings, I've figured out the issue.

This problem occurs when you didn't convert the model correctly. if your create a SINGLE Python script which does the following workflow you would have this error.

  • create a Keras model --> train or fine tune it -->  convert the Keras model to Tensorflow model

A better choice is:

  1. build and train your Keras model, save it as .h5 file
  2. then load it with ANOTHER python script following this example, make sure that you have set "backend.set_learning_phase(0)" at the beginnning of your script. Then convert the Keras model to TF model.
  3. then calling FREEZE and DECENT_Q would be fine

My assumption about this problem is that it happens when the model is not correctly switched to "inference mode". Because batch norm has different behaviours during training and inference, DECENT_Q would have trouble when it tries to quantize a batch norm layer which is still in "training mode".

View solution in original post

Highlighted
Contributor
Contributor
272 Views
Registered: ‎08-02-2019

Re: Batch Normalization Error

Jump to solution

Oh okay! Got it. Thank you.

0 Kudos