将tensorflow中培训的充气3D CNN转换为部署在iphone(.mlmodel)上

2024-05-13 23:59:17 发布

您现在位置:Python中文网/ 问答频道 /正文

受Joao Carreira和Andrew Zisserman的“Quo Vadis, Action Recognition? A New Model and the Kinetics Dataset”启发,我创建了自己的数据集并对其进行了微调。幸运的是,在我的3节课中,每节课只播放了50个视频

现在我正试图将该模型转换为在iphone上运行tf-coreml。这是我的密码:

tf_converter.convert(tf_model_path='output_graph.pb',
                     mlmodel_path='output_graph.mlmodel',
                     output_feature_names=['RGB/inception_i3d/Mixed_5c/Branch_3/Conv3d_0b_1x1/Relu'],
                     input_name_shape_dict={'Placeholder_1:0': [1, 100, 224, 224, 3]})

我得到的回溯错误是:

Now finding ops in the TF graph that can be dropped for inference
Collecting all the 'Const' ops from the graph, by running it....
Traceback (most recent call last):
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1356, in _do_call
    return fn(*args)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1341, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1429, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.FailedPreconditionError: GetNext() failed because the iterator has not been initialized. Ensure that you have run the initializer operation for this iterator before getting the next element.
     [[{{node IteratorGetNext}}]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "pb2mlmodel.py", line 10, in <module>
    input_name_shape_dict={'Placeholder_1:0': [1, 100, 224, 224, 3]})
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tfcoreml/_tf_coreml_converter.py", line 621, in convert
    custom_conversion_functions=custom_conversion_functions)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tfcoreml/_tf_coreml_converter.py", line 262, in _convert_pb_to_mlmodel
    tensors_evaluated = sess.run(tensors, feed_dict=input_feed_dict)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 950, in run
    run_metadata_ptr)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1173, in _run
    feed_dict_tensor, options, run_metadata)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1350, in _do_run
    run_metadata)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/client/session.py", line 1370, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.FailedPreconditionError: GetNext() failed because the iterator has not been initialized. Ensure that you have run the initializer operation for this iterator before getting the next element.
     [[node IteratorGetNext (defined at /Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tfcoreml/_tf_coreml_converter.py:154) ]]

Original stack trace for 'IteratorGetNext':
  File "pb2mlmodel.py", line 10, in <module>
    input_name_shape_dict={'Placeholder_1:0': [1, 100, 224, 224, 3]})
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tfcoreml/_tf_coreml_converter.py", line 621, in convert
    custom_conversion_functions=custom_conversion_functions)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tfcoreml/_tf_coreml_converter.py", line 154, in _convert_pb_to_mlmodel
    tf.import_graph_def(gdef, name='')
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/util/deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/framework/importer.py", line 443, in import_graph_def
    _ProcessNewOps(graph)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/framework/importer.py", line 236, in _ProcessNewOps
    for new_op in graph._add_new_tf_operations(compute_devices=False):  # pylint: disable=protected-access
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3751, in _add_new_tf_operations
    for c_op in c_api_util.new_tf_operations(self)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3751, in <listcomp>
    for c_op in c_api_util.new_tf_operations(self)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 3641, in _create_op_from_tf_operation
    ret = Operation(c_op, self)
  File "/Users/steven/Desktop/convert_tf_mlmodel/env/lib/python3.7/site-packages/tensorflow/python/framework/ops.py", line 2005, in __init__
    self._traceback = tf_stack.extract_stack()

我使用以下代码获得了输入和输出功能名称:

./configure
bazel build tensorflow/tools/graph_transforms:summarize_graph
bazel-bin/tensorflow/tools/graph_transforms/summarize_graph --in_graph=output_graph.pb

它返回了这个:

2019-09-29 17:45:29.991017: E tensorflow/core/platform/hadoop/hadoop_file_system.cc:132] HadoopFileSystem load error: dlopen(libhdfs.dylib, 6): image not found
Found 1 possible inputs: (name=Placeholder_1, type=bool(10), shape=<unknown>)
No variables spotted.
Found 1 possible outputs: (name=RGB/inception_i3d/Mixed_5c/Branch_3/Conv3d_0b_1x1/Relu, op=Relu)
Found 9615952 (9.62M) const parameters, 0 (0) variable parameters, and 104 control_edges
1 nodes assigned to device '/device:CPU:0'Op types used: 428 Const, 420 Identity, 208 Switch, 104 Add, 104 Merge, 104 Mul, 104 Mean, 52 LogicalOr, 52 Relu, 52 StopGradient, 52 Conv3D, 52 Sub, 52 SquaredDifference, 52 Rsqrt, 13 MaxPool3D, 8 ConcatV2, 2 PyFunc, 1 Squeeze, 1 IteratorV2, 1 IteratorGetNext, 1 Placeholder
To use with tensorflow/tools/benchmark:benchmark_model try these arguments:
bazel run tensorflow/tools/benchmark:benchmark_model -- --graph=output_graph.pb --show_flops --input_layer=Placeholder_1 --input_layer_type=bool --input_layer_shape= --output_layer=RGB/inception_i3d/Mixed_5c/Branch_3/Conv3d_0b_1x1/Relu

我发现的可能问题有:

  1. 输入为:type=bool(10)。应改为100帧、224宽、224高和3个通道(RGB)
  2. 输出为:op=Relu。应该是我训练过的3个视频课程之一
  3. Tf coreml尚不支持充气3D CNN。他们的行动清单

谢谢


Tags: inpyenvconvertlibpackagestftensorflow