how to predict with .meta and checkpoint files in tensorflow?

前端 未结 2 1733
南笙
南笙 2020-12-15 14:52

I\'m learning about MobileNet thesedays and i\'m new to tensorflow. After training with ssd-mobilenet model,i got checkpoint file , .meta file , graph.pbtxt file and so on.

相关标签:
2条回答
  • 2020-12-15 15:11

    You should load the graph using tf.train.import_meta_graph() and then get the tensors using get_tensor_by_name(). You can try:

    model_path = "model.ckpt"
    detection_graph = tf.Graph()
    with tf.Session(graph=detection_graph) as sess:
        # Load the graph with the trained states
        loader = tf.train.import_meta_graph(model_path+'.meta')
        loader.restore(sess, model_path)
    
        # Get the tensors by their variable name
        image_tensor = detection_graph.get_tensor_by_name('image_tensor:0')
        boxes = detection_graph.get_tensor_by_name('detection_boxes:0')
        scores = detection_graph.get_tensor_by_name('detection_scores:0')
        ...
        # Make predictions
        _boxes, _scores = sess.run([boxes, scores], feed_dict={image_tensor: image_np_expanded}) 
    
    0 讨论(0)
  • 2020-12-15 15:22

    Just for those who have the problem like wu ruize and CoupDeMistral:

    But I got this error: "The name 'image_tensor:0' refers to a Tensor which does not exist. The operation, 'image_tensor', does not exist in the graph."

    You need to name your tensor first before using detection_graph.get_tensor_by_name.

    For example, something like this:

    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32),name='accuracy')
    

    Notice that the tensor above has been named as 'accuracy'.

    After that you can enjoy the restore operation by:

    detection_graph.get_tensor_by_name('accuracy:0')
    

    Have fun now :P!

    0 讨论(0)
提交回复
热议问题