how to convert .ckpt file to .pb

走远了吗. 提交于 2019-12-09 22:38:08

问题


I use ssd_mobilenets in Object detection API to train my own model, and get .ckpt files. It works well on my computer, but now I want to use the model on my phone. So, I need convert it to .pb file. I do not know how to do it, can any one help? By the way, the graph of ssd_mobilenets is complex, I can not find which is the output of model. Is there any one knowing the name of the output?


回答1:


Use export_inference_graph.py to convert model checkpoint file into a .pb file.

python tensorflow_models/object_detection/export_inference_graph.py \
--input_type image_tensor \
--pipeline_config_path architecture_used_while_training.config \
--trained path_to_saved_ckpt/model.ckpt-NUMBER \
--output_directory model/



回答2:


  • This is the 4th code cell in object_detection_tutorial.ipynb in this link -https://github.com/tensorflow/models/blob/master/research/object_detection/object_detection_tutorial.ipynb

    # What model to download.
    MODEL_NAME = 'ssd_mobilenet_v1_coco_2017_11_17'
    MODEL_FILE = MODEL_NAME + '.tar.gz'
    DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
    
    # Path to frozen detection graph. This is the actual model that is used for the object detection.
    PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
    
    # List of the strings that is used to add correct label for each box.
    PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
    
    NUM_CLASSES = 90
    
  • Now the cell clearly says the .pb filename which is /frozen_inference_graph.pb

  • So you already have the .pb file why do you want to convert ??
  • Anyways you can refer thsi link for freezing the graph: https://github.com/jayshah19949596/Tensorboard-Visualization-Freezing-Graph
  • you need to use tensorflow.python.tools.freeze_graph() function to convert your .ckpt file to .pb file
  • The below code line shows how you do it

    freeze_graph.freeze_graph(input_graph_path,
                              input_saver_def_path,
                              input_binary,
                              input_checkpoint_path,
                              output_node_names,
                              restore_op_name,
                              filename_tensor_name,
                              output_graph_path,
                              clear_devices,
                              initializer_nodes)
    
    • input_graph_path : is the path to .pb file where you will write your graph and this .pb file is not frozen. you will use tf.train.write_graph() to write the graph
    • input_saver_def_path : you can keep it an empty string
    • input_binary : it is a boolean value keep it false so that the file genertaed is not binary and human readable
    • input_checkpoint_path : path to the .ckpt file
    • output_graph_path : path where you want to write you pb file
    • clear_devices : boolean value ... keep it False
    • output_node_names : explicit tensor node names that you want to save
    • restore_op_name : string value that should be "save/restore_all"
    • filename_tensor_name = "save/Const:0"
    • initializer_nodes = ""


来源:https://stackoverflow.com/questions/45726365/how-to-convert-ckpt-file-to-pb

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!