【问题标题】:ValueError("Tensor %s is not an element of this graph." % obj)ValueError("张量 %s 不是该图的元素。" % obj)
【发布时间】:2019-04-24 13:31:36
【问题描述】:

首先,英语不是我的母语,请原谅,如果我表达的不是很好,请随时纠正我。

我正在制作一个情绪识别系统,它使用休息服务从客户端的浏览器发送图像。

这是代码:

# hyper-parameters for bounding boxes shape
frame_window = 10
emotion_offsets = (20, 40)

# loading models
face_detection = load_detection_model(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
K.clear_session()

# getting input model shapes for inference
emotion_target_size = emotion_classifier.input_shape[1:3]

# starting lists for calculating modes
emotion_window = []

以及功能:

def detect_emotion(self, img):

    # Convert RGB to BGR
    bgr_image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
    gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
    rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
    faces = detect_faces(face_detection, gray_image)

    for face_coordinates in faces:

        x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
        gray_face = gray_image[y1:y2, x1:x2]
        try:
            gray_face = cv2.resize(gray_face, (emotion_target_size))
        except:
            continue

        gray_face = preprocess_input(gray_face, True)
        gray_face = np.expand_dims(gray_face, 0)
        gray_face = np.expand_dims(gray_face, -1)
        emotion_classifier._make_predict_function()

        emotion_prediction = emotion_classifier.predict(gray_face)

        emotion_probability = np.max(emotion_prediction)
        emotion_label_arg = np.argmax(emotion_prediction)
        emotion_text = emotion_labels[emotion_label_arg]
        emotion_window.append(emotion_text)

        if len(emotion_window) > frame_window:
            emotion_window.pop(0)
        try:
            emotion_mode = mode(emotion_window)
        except:
            continue

        if emotion_text == 'angry':
            color = emotion_probability * np.asarray((255, 0, 0))
        elif emotion_text == 'sad':
            color = emotion_probability * np.asarray((0, 0, 255))
        elif emotion_text == 'happy':
            color = emotion_probability * np.asarray((255, 255, 0))
        elif emotion_text == 'surprise':
            color = emotion_probability * np.asarray((0, 255, 255))
        else:
            color = emotion_probability * np.asarray((0, 255, 0))

        color = color.astype(int)
        color = color.tolist()

        draw_bounding_box(face_coordinates, rgb_image, color)
        draw_text(face_coordinates, rgb_image, emotion_mode,
                  color, 0, -45, 1, 1)

    img = Image.fromarray(rgb_image)

    return img


I'm facing this error when i run my code using waitress:

    File "c:\users\afgir\documents\pythonprojects\face_reco\venv\lib\site-packages\tensorflow\python\framework\ops.py", line 3569, in _as_graph_element_locked
    raise ValueError("Tensor %s is not an element of this graph." % obj) 
    ValueError: Tensor Tensor("predictions_1/Softmax:0", shape=(?, 7), dtype=float32) is not an element of this graph.

它加载图像并进行所有处理,我很确定错误在emotion_classifier.predict 行,只是不知道如何修复它。

我尝试了this question 中的两种解决方案,但都没有奏效。

我真的是使用Tensorflow 的新手,所以我有点坚持。

【问题讨论】:

  • 为什么要使用 tf.Graph()?
  • 这是我在 GitHub 论坛上找到的“解决方案”,但也没有用。
  • 查看我的答案,如果我遗漏了什么,请发表评论

标签: python rest tensorflow keras computer-vision


【解决方案1】:

我只是想找出你的真实环境,但我猜你可能会使用Keras 和一些Keras 模型来预测情绪。

你的错误信息是因为这行引起的:

K.clear_session()

其中,来自文档:keras.backend.clear_session()。 因此,您清除所有已创建的图形,然后尝试运行分类器的 predict(),它以这种方式丢失了所有上下文。
因此,只需简单地删除这一行。

本节是关于 Op 删除的一些代码:
在此任务中,您根本不需要使用 tf.Graph()。您只需简单地将emotion_classifier.predict() 作为一个简单的python 方法调用 之外 使用任何tensorflow graph

def detect_emotion(self, img):

    # Convert RGB to BGR
    bgr_image = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
    gray_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2GRAY)
    rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)
    faces = detect_faces(face_detection, gray_image)

    for face_coordinates in faces:

        x1, x2, y1, y2 = apply_offsets(face_coordinates, emotion_offsets)
        gray_face = gray_image[y1:y2, x1:x2]
        try:
            gray_face = cv2.resize(gray_face, (emotion_target_size))
        except:
            continue

        gray_face = preprocess_input(gray_face, True)
        gray_face = np.expand_dims(gray_face, 0)
        gray_face = np.expand_dims(gray_face, -1)
        emotion_classifier._make_predict_function()

        emotion_prediction = emotion_classifier.predict(gray_face)

        emotion_probability = np.max(emotion_prediction)
        emotion_label_arg = np.argmax(emotion_prediction)
        emotion_text = emotion_labels[emotion_label_arg]
        emotion_window.append(emotion_text)

        if len(emotion_window) > frame_window:
            emotion_window.pop(0)
        try:
            emotion_mode = mode(emotion_window)
        except:
            continue

        if emotion_text == 'angry':
            color = emotion_probability * np.asarray((255, 0, 0))
        elif emotion_text == 'sad':
            color = emotion_probability * np.asarray((0, 0, 255))
        elif emotion_text == 'happy':
            color = emotion_probability * np.asarray((255, 255, 0))
        elif emotion_text == 'surprise':
            color = emotion_probability * np.asarray((0, 255, 255))
        else:
            color = emotion_probability * np.asarray((0, 255, 0))

        color = color.astype(int)
        color = color.tolist()

        draw_bounding_box(face_coordinates, rgb_image, color)
        draw_text(face_coordinates, rgb_image, emotion_mode,
                  color, 0, -45, 1, 1)

    img = Image.fromarray(rgb_image)

    return img

【讨论】:

  • 在它检测到人脸的那一刻,完全冻结并抛出相同的错误,我正在使用实时视频流,当它接收到第一张包含人脸的图像时,崩溃了。
  • @AndrésGirón 首先尝试将单个图像传递给预测函数,看看会发生什么,请重新加载内核以确定。只是你不能得到同样的错误信息
  • 如何重新加载内核?
  • @AndrésGirón 您是重新启动 wsgi 服务器还是更新了您编辑的应用程序?如果是,请再试一次,然后将一张图片传递给预测函数。
  • 我尝试使用单个图像重新运行服务器并仍然存在相同的错误。 :(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-17
  • 1970-01-01
  • 1970-01-01
  • 2017-06-18
  • 2018-05-29
  • 1970-01-01
  • 2017-06-20
相关资源
最近更新 更多