注意我这个记录是跟着官网的object_detection做目标检测做的,配置文件什么的也是改的现成的,所以转pb文件会相对方便点,如果完全是自己训练模型准备数据,可能不太适合这么做,不过大神的话可以试着改一下export_inference_graph.py这个生成pb文件的脚本

网上有很多是在训练模型的时候在构造模型时添加input和output标签,训练完后自动生成pb文件,如果是复杂模型而且又是新手用的也是官网提供的现成代码,根本不知道在哪里加这两个参数

注意到在文件./models-master/object_detection/export_inference_graph.py,可以手动实现产生pb文件,文件在一开始也简单说明了怎么做,接下来具体说一下,

当你训练结束后会得到graph.pbtxt和三个model.ckpt-××××文件(这三个文件是在你的训练结果中挑出来的)我训练了50000次,取的是最后一组,也就是model.ckpt-50000.data-00000-of-00001,model.ckpt-50000.index,model.ckpt-50000.meta这三个了。

准备好以上三个后还要你训练用的配置文件:××××_label_map.pbtxt,train.record,val.record,ssd_mobilenet_v1_pets.config

这些都有了就是运行现成脚本了,我的命令行是这样的:

python -u /home/saners/Mobilenet/models-master/object_detection/export_inference_graph.py \
  --input_type=image_tensor \
  --pipeline_config_path=/home/saners/Mobilenet/MobilenetTest/data/ssd_mobilenet_v1_pets.config \
  --trained_checkpoint_prefix=/home/saners/Mobilenet/MobilenetTest/data/aircraft/model.ckpt-50000 \
  --output_directory=/home/saners/Mobilenet/MobilenetTest/data

注意:.config是你训练时用的那个,不要拿来一个没有改过的,model.ckpt-50000要写成类似我这样的,不然会报错:

2017-09-04 15:58:21.078121: W tensorflow/core/framework/op_kernel.cc:1192] Not found: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /home/saners/Mobilenet/MobilenetTest/data/aircraft/model.ckpt
Traceback (most recent call last):
  File "/home/saners/Mobilenet/models-master/object_detection/export_inference_graph.py", line 108, in <module>
    tf.app.run()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "/home/saners/Mobilenet/models-master/object_detection/export_inference_graph.py", line 104, in main
    FLAGS.output_directory)
  File "/home/saners/Mobilenet/models-master/object_detection/exporter.py", line 376, in export_inference_graph
    optimize_graph, output_collection_name)
  File "/home/saners/Mobilenet/models-master/object_detection/exporter.py", line 336, in _export_inference_graph
    trained_checkpoint_prefix=trained_checkpoint_prefix)
  File "/home/saners/Mobilenet/models-master/object_detection/exporter.py", line 295, in _write_graph_and_checkpoint
    saver.restore(sess, trained_checkpoint_prefix)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/training/saver.py", line 1560, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 889, in run
    run_metadata_ptr)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1118, in _run
    feed_dict_tensor, options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1315, in _do_run
    options, run_metadata)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /home/saners/Mobilenet/MobilenetTest/data/aircraft/model.ckpt
[[Node: save/RestoreV2_101 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_101/tensor_names, save/RestoreV2_101/shape_and_slices)]]
[[Node: save/RestoreV2_50/_163 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_564_save/RestoreV2_50", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]


Caused by op u'save/RestoreV2_101', defined at:
  File "/home/saners/Mobilenet/models-master/object_detection/export_inference_graph.py", line 108, in <module>
    tf.app.run()
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "/home/saners/Mobilenet/models-master/object_detection/export_inference_graph.py", line 104, in main
    FLAGS.output_directory)
  File "/home/saners/Mobilenet/models-master/object_detection/exporter.py", line 376, in export_inference_graph
    optimize_graph, output_collection_name)
  File "/home/saners/Mobilenet/models-master/object_detection/exporter.py", line 336, in _export_inference_graph
    trained_checkpoint_prefix=trained_checkpoint_prefix)
  File "/home/saners/Mobilenet/models-master/object_detection/exporter.py", line 291, in _write_graph_and_checkpoint
    tf.import_graph_def(inference_graph_def, name='')
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/importer.py", line 313, in import_graph_def
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 3045, in create_op
    op_def=op_def)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1603, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access


NotFoundError (see above for traceback): Unsuccessful TensorSliceReader constructor: Failed to find any matching files for /home/saners/Mobilenet/MobilenetTest/data/aircraft/model.ckpt
[[Node: save/RestoreV2_101 = RestoreV2[dtypes=[DT_FLOAT], _device="/job:localhost/replica:0/task:0/cpu:0"](_arg_save/Const_0_0, save/RestoreV2_101/tensor_names, save/RestoreV2_101/shape_and_slices)]]
[[Node: save/RestoreV2_50/_163 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/device:GPU:0", send_device="/job:localhost/replica:0/task:0/cpu:0", send_device_incarnation=1, tensor_name="edge_564_save/RestoreV2_50", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"]()]]


Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐