ValueError(“张量%s不是此图的元素。”%obj)

时间:2018-11-21 22:19:38

标签: python rest tensorflow keras computer-vision

首先,英语不是我的母语,所以请问,如果我表现得不好,请随时纠正我。

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

这是代码:

# 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的新手,所以我对此一无所知。

1 个答案:

答案 0 :(得分:0)

我只是想找出您的真实环境,但我想您可能会使用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