opencv - TypeError (“Tensor is unhashable if Tensor equality is enabled. ” K.learning_phase():0

标签 opencv tensorflow keras

我将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> 

我不确定发生了什么。我将不胜感激。

最佳答案

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

该错误是由于版本引起的。如果您在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.

如果仍然遇到错误,请像上面一样分享该错误的可复制代码。乐意为您服务。

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

关于opencv - TypeError (“Tensor is unhashable if Tensor equality is enabled. ” K.learning_phase():0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60572674/

相关文章:

C++/CMake :Undefined reference to imwrite in OpenCV

javascript - 使用 JavaScript 和 TensorFlow 通过 PoseNet 进行多人跟踪的问题

python - 使用 pip for gpu 安装后找不到 TensorFlow

python - 如何为 tensorflow 常量分配新值?

tensorflow - 填充张量有什么影响?

c++ - OpenCV GPU SURF ROI 错误?

c++ - 如何强制 cv::Mat 表现为智能指针

python - 可变大小训练数据的 keras 形状

python - 张量板 - 错误 :Trace already enabled - How to solve?

c++ - Flann Matcher 训练如何在 opencv 中工作?