【问题标题】:TypeError("Tensor is unhashable if Tensor equality is enabled. " K.learning_phase(): 0TypeError("如果启用了张量相等,则张量是不可散列的。" K.learning_phase(): 0
【发布时间】:2020-03-06 22:59:01
【问题描述】:

我正在将 Keras、Tensorflow 和 OpenCV 脚本移植到 TF2 和 Keras 2,但遇到了问题。我在 K.learning_phase(): 0 上遇到错误。 错误发生在此代码部分。

ef detect_image(self, image):
        if self.model_image_size != (None, None):
            assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
            assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
            boxed_image = image_preporcess(np.copy(image), tuple(reversed(self.model_image_size)))
            image_data = boxed_image

        out_boxes, out_scores, out_classes = self.sess.run(
            [self.boxes, self.scores, self.classes],
            feed_dict={
                self.yolo_model.input: image_data,
                self.input_image_shape: [image.shape[0], image.shape[1]],
                tf.keras.learning_phase(): 0  })

这里是完整代码的要点 https://gist.github.com/robisen1/31976de17af9e752c6ba8d1dd0e08906

Traceback (most recent call last):
  File "webcam_detect.py", line 188, in <module>
    r_image, ObjectsList = yolo.detect_image(frame)
  File "webcam_detect.py", line 110, in detect_image
    K.learning_phase(): 0
  File "C:\Anaconda3\envs\simplecv\lib\site-packages\tensorflow_core\python\framework\ops.py", line 705, in __hash__
    raise TypeError("Tensor is unhashable if Tensor equality is enabled. "
TypeError: Tensor is unhashable if Tensor equality is enabled. Instead, use tensor.experimental_ref() as the key.
(simplecv) PS C:\dev\lacv\yolov3\yolov3ct> 

我不确定发生了什么。如果有任何见解,我将不胜感激。

【问题讨论】:

    标签: opencv tensorflow keras


    【解决方案1】:

    您正在尝试使用 TensorFlow 1.x,它在图形模式下工作,而 TensorFlow 2.x 在急切模式下工作。 TensorFlow 1.X 要求用户通过调用 tf.* API 手动将抽象语法树(图形)拼接在一起。然后它要求用户通过将一组输出张量和输入张量传递给 session.run() 调用来手动编译抽象语法树。 TensorFlow 2.0 急切地执行(就像 Python 通常那样),在 2.0 中,图形和会话应该感觉像是实现细节。

    错误是由于版本。如果您在 TF2 中使用 session,则需要使用兼容版本,其他操作也是如此。在 TF2 中也是tf.keras.backend.learning_phase。

    建议阅读指南 - Migrate your TensorFlow 1 code to TensorFlow 2。

    例如,下面的代码会引发与您面临的错误类似的错误 -

    import tensorflow as tf 
    print(tf.__version__)
    x = tf.constant(5) 
    y = tf.constant(10) 
    z = tf.constant(20) 
    
    # This will show same error. 
    tensor_set = {x, y, z} 
    tensor_dict = {x: 'five', y: 'ten', z: 'twenty'} 
    

    输出 -

    2.2.0
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-3-509b2d8d7ab1> in <module>()
          6 
          7 # This will show same error.
    ----> 8 tensor_set = {x, y, z}
          9 tensor_dict = {x: 'five', y: 'ten', z: 'twenty'}
         10 
    
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/ops.py in __hash__(self)
        724     if (Tensor._USE_EQUALITY and executing_eagerly_outside_functions() and
        725         (g is None or g.building_function)):
    --> 726       raise TypeError("Tensor is unhashable. "
        727                       "Instead, use tensor.ref() as the key.")
        728     else:
    
    TypeError: Tensor is unhashable. Instead, use tensor.ref() as the key.
    

    但下面的代码将解决问题 -

    import tensorflow as tf 
    print(tf.__version__)
    x = tf.constant(5) 
    y = tf.constant(10) 
    z = tf.constant(20) 
    
    #This solves the issue 
    tensor_set = {x.experimental_ref(), y.experimental_ref(), z.experimental_ref()} 
    tensor_dict = {x.experimental_ref(): 'five', y.experimental_ref(): 'ten', z.experimental_ref(): 'twenty'} 
    

    输出 -

    2.2.0
    WARNING:tensorflow:From <ipython-input-4-05e379e669d9>:12: Tensor.experimental_ref (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
    Instructions for updating:
    Use ref() instead.
    

    如果您仍然面临错误,请分享上述错误的可重现代码。很乐意为您提供帮助。

    希望这能回答您的问题。快乐学习。

    【讨论】:

    • @Robi Sen - 希望我们已经回答了您的问题。如果您对答案感到满意,请您接受并投票。
    • tf.keras.backend.learning_phase 的链接已损坏,就像 tensorflow.org/guide 上的许多其他人一样!不过,+1 以获得一些很好的解释。
    【解决方案2】:

    尝试禁用 tf.compat.v1.disable_eager_execution()

    from tensorflow.compat.v1 import disable_eager_execution
    
    disable_eager_execution()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多