ValueError:无法使用给定的会话来评估张量:张量的图与会话的图不同

时间:2018-07-23 13:53:40

标签: python tensorflow deep-learning ros robot

我想在ROS中使用张量流模型。但是此代码可以正常工作,但是由于每次恢复模型都非常慢

class image_converter:
def __init__(self):
    self.demo_pub = rospy.Publisher(
        "/demo/pic", Image, queue_size=1)
    # todo pub distance and angle data
    self.bridge = CvBridge()
    self.image_sub = rospy.Subscriber(
        "/camera/live_view", Image, self.callback)
def callback(self, data):
    self.callback_once(data)
def callback_once(self, data):
    # imgmsg_to_cv2
    try:
        cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
        print(e)
    img1 = cv_image
    data,_,img=read_img(img1)
        #print(data)
    with tf.Session() as sess:
        saver = tf.train.import_meta_graph('./model4/model.ckpt.meta')
        saver.restore(sess,tf.train.latest_checkpoint('./model4/'))
        graph = tf.get_default_graph()
        x = graph.get_tensor_by_name("x:0")
        feed_dict = {x:data}
        logits = graph.get_tensor_by_name('logits_eval:0')
        classification_result = sess.run(logits,feed_dict)
        index=(tf.argmax(classification_result,1).eval())

所以我这样更改代码:

class image_converter:
def __init__(self):
    self.sess=tf.Session()
    saver = tf.train.import_meta_graph('./model4/model.ckpt.meta')
    saver.restore(self.sess,tf.train.latest_checkpoint('./model4/'))
    self.graph = tf.get_default_graph()
    self.x = self.graph.get_tensor_by_name("x:0")
    self.demo_pub = rospy.Publisher(
        "/demo/pic", Image, queue_size=1)
    # todo pub distance and angle data
    self.bridge = CvBridge()
    self.image_sub = rospy.Subscriber(
        "/camera/live_view", Image, self.callback)
def callback(self, data):
    try:
        cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
    except CvBridgeError as e:
        print(e)
    data,_,img=read_img(cv_image)
    feed_dict = {self.x:data}
    logits = self.graph.get_tensor_by_name('logits_eval:0')
    classification_result = self.sess.run(logits,feed_dict)
    print(classification_result)
    index=(tf.argmax(classification_result,1).eval(session=self.sess))

发生错误:最后一行 ValueError:无法使用给定的会话评估张量:张量图与会话图不同。

所以,我应该怎么做才能解决此错误。非常感谢您。

0 个答案:

没有答案