我正在使用包含tf.keras.applications.MobileNetV2的模型,但冻结图存在问题。
我发现它与BatchNormalization层有关,因此编写了一个只有1层的简单测试程序TESTE_PART1.py。我使用了freeze_graph工具来创建.pb文件。然后,另一个python程序TESTE_PART2.py使用tf.import_graph_def读取.pb。
错误是:
Traceback (most recent call last):
File "/home/daniel/sistema/anaconda3/envs/tensorflow_1.13/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 426, in import_graph_def
graph._c_graph, serialized, options) # pylint: disable=protected-access
tensorflow.python.framework.errors_impl.InvalidArgumentError: Input 0 of node batch_normalization_v1/cond/ReadVariableOp/Switch was passed float from batch_normalization_v1/moving_mean:0 incompatible with expected resource.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/daniel/sistema/anaconda3/envs/tensorflow_1.13/lib/python3.6/runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "/home/daniel/sistema/anaconda3/envs/tensorflow_1.13/lib/python3.6/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/home/daniel/comp/armis/Plates/pysrc/cornerdetect/teste_part2.py", line 9, in <module>
tf.import_graph_def(od_graph_def, name='')
File "/home/daniel/sistema/anaconda3/envs/tensorflow_1.13/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "/home/daniel/sistema/anaconda3/envs/tensorflow_1.13/lib/python3.6/site-packages/tensorflow/python/framework/importer.py", line 430, in import_graph_def
raise ValueError(str(e))
ValueError: Input 0 of node batch_normalization_v1/cond/ReadVariableOp/Switch was passed float from batch_normalization_v1/moving_mean:0 incompatible with expected resource.
我也尝试过使用python_freeze_session方法在python中创建.pb,如此处所述 How to export Keras .h5 to tensorflow .pb? 结果是一样的。
该错误抱怨类型不兼容。我修改了TESTE_PART1.py来打印这两个操作的dtype,它们都是“资源”:
O1 INPUT Tensor("batch_normalization_v1/moving_mean:0", shape=(), dtype=resource)
O1 INPUT Tensor("batch_normalization_v1/cond/pred_id:0", shape=(), dtype=bool)
O2 OUTPUT Tensor("batch_normalization_v1/moving_mean:0", shape=(), dtype=resource)
我用tf 1.12和1.13进行了测试。 操作系统是Linux 4.4.0-148-通用#174-Ubuntu SMP Tue May 7 12:20:14 UTC 2019 x86_64 x86_64 x86_64 GNU / Linux
TESTE_PART1.py
import tensorflow as tf
l1 = tf.keras.layers.BatchNormalization(input_shape=(100,))
model = tf.keras.models.Sequential([l1])
print("output name", model.output.op.name)
print("input name", model.input.op.name)
# output name batch_normalization_v1/batchnorm/add_1
# input name batch_normalization_v1_input
saver = tf.train.Saver()
sess = tf.keras.backend.get_session()
saver.save(sess, "teste.saver_export")
op1 = sess.graph.get_operation_by_name("batch_normalization_v1/cond/ReadVariableOp/Switch")
for input in op1.inputs:
print("O1 INPUT", input)
op2 = sess.graph.get_operation_by_name("batch_normalization_v1/moving_mean")
for output in op2.outputs:
print("O2 OUTPUT", output)
freeze_graph --input_meta_graph=teste.saver_export.meta --input_checkpoint=teste.saver_export --output_graph=teste.freeze_graph.pb --output_node_names="batch_normalization_v1/batchnorm/add_1" --input_binary=true
$ ls -l teste*
-rw-rw-r-- 1 daniel daniel 6990 jun 1 18:35 teste.freeze_graph.pb
-rw-rw-r-- 1 daniel daniel 1600 jun 1 18:34 teste.saver_export.data-00000-of-00001
-rw-rw-r-- 1 daniel daniel 239 jun 1 18:34 teste.saver_export.index
-rw-rw-r-- 1 daniel daniel 28006 jun 1 18:34 teste.saver_export.meta
TESTE_PART2.py
import tensorflow as tf
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with open("teste.freeze_graph.pb", 'rb') as fd:
serialized_graph = fd.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='') # here