ValueError(“Tensor %s is not an element of this graph.” % obj)












0















First of all, English is not my mother language so excuse me, if I don't express myself really well, please feel free to correct me.



I'm making an emotion recognition system that uses rest services to send the image from the client's browser.



This is the code:



# 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 =


And the function:



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:usersafgirdocumentspythonprojectsface_recovenvlibsite-packagestensorflowpythonframeworkops.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.


It loads the image and does all the processing well, I'm pretty sure that the error is in the emotion_classifier.predict line, just don't know how to fix it.



I've tried with the two solutions in this question and none of them worked.



I'm really new using Tensorflow so I'm kinda stuck with this.










share|improve this question

























  • why do you use tf.Graph()?

    – Geeocode
    Nov 21 '18 at 22:41











  • It was a "Solution" that i found in a GitHub forum, but it didn't work neither.

    – Andrés Girón
    Nov 21 '18 at 22:51











  • See my answer, please comment if I would miss something

    – Geeocode
    Nov 21 '18 at 22:53
















0















First of all, English is not my mother language so excuse me, if I don't express myself really well, please feel free to correct me.



I'm making an emotion recognition system that uses rest services to send the image from the client's browser.



This is the code:



# 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 =


And the function:



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:usersafgirdocumentspythonprojectsface_recovenvlibsite-packagestensorflowpythonframeworkops.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.


It loads the image and does all the processing well, I'm pretty sure that the error is in the emotion_classifier.predict line, just don't know how to fix it.



I've tried with the two solutions in this question and none of them worked.



I'm really new using Tensorflow so I'm kinda stuck with this.










share|improve this question

























  • why do you use tf.Graph()?

    – Geeocode
    Nov 21 '18 at 22:41











  • It was a "Solution" that i found in a GitHub forum, but it didn't work neither.

    – Andrés Girón
    Nov 21 '18 at 22:51











  • See my answer, please comment if I would miss something

    – Geeocode
    Nov 21 '18 at 22:53














0












0








0








First of all, English is not my mother language so excuse me, if I don't express myself really well, please feel free to correct me.



I'm making an emotion recognition system that uses rest services to send the image from the client's browser.



This is the code:



# 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 =


And the function:



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:usersafgirdocumentspythonprojectsface_recovenvlibsite-packagestensorflowpythonframeworkops.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.


It loads the image and does all the processing well, I'm pretty sure that the error is in the emotion_classifier.predict line, just don't know how to fix it.



I've tried with the two solutions in this question and none of them worked.



I'm really new using Tensorflow so I'm kinda stuck with this.










share|improve this question
















First of all, English is not my mother language so excuse me, if I don't express myself really well, please feel free to correct me.



I'm making an emotion recognition system that uses rest services to send the image from the client's browser.



This is the code:



# 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 =


And the function:



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:usersafgirdocumentspythonprojectsface_recovenvlibsite-packagestensorflowpythonframeworkops.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.


It loads the image and does all the processing well, I'm pretty sure that the error is in the emotion_classifier.predict line, just don't know how to fix it.



I've tried with the two solutions in this question and none of them worked.



I'm really new using Tensorflow so I'm kinda stuck with this.







python rest tensorflow keras computer-vision






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 23:31









Geeocode

2,3501820




2,3501820










asked Nov 21 '18 at 22:19









Andrés GirónAndrés Girón

357




357













  • why do you use tf.Graph()?

    – Geeocode
    Nov 21 '18 at 22:41











  • It was a "Solution" that i found in a GitHub forum, but it didn't work neither.

    – Andrés Girón
    Nov 21 '18 at 22:51











  • See my answer, please comment if I would miss something

    – Geeocode
    Nov 21 '18 at 22:53



















  • why do you use tf.Graph()?

    – Geeocode
    Nov 21 '18 at 22:41











  • It was a "Solution" that i found in a GitHub forum, but it didn't work neither.

    – Andrés Girón
    Nov 21 '18 at 22:51











  • See my answer, please comment if I would miss something

    – Geeocode
    Nov 21 '18 at 22:53

















why do you use tf.Graph()?

– Geeocode
Nov 21 '18 at 22:41





why do you use tf.Graph()?

– Geeocode
Nov 21 '18 at 22:41













It was a "Solution" that i found in a GitHub forum, but it didn't work neither.

– Andrés Girón
Nov 21 '18 at 22:51





It was a "Solution" that i found in a GitHub forum, but it didn't work neither.

– Andrés Girón
Nov 21 '18 at 22:51













See my answer, please comment if I would miss something

– Geeocode
Nov 21 '18 at 22:53





See my answer, please comment if I would miss something

– Geeocode
Nov 21 '18 at 22:53












1 Answer
1






active

oldest

votes


















0














I'm just trying to find out your real environment, but I guess you may use Keras amd some Keras model to predict emotions.



Your error message caused because of the line:



K.clear_session()


which, from the documentation: keras.backend.clear_session().
So you clear all graph it has been created, then you try to run the classifier's predict(), which lost all its context this way.

Thus just simply delete this line.



This section is was about some code the Op deleted:

In this task you don't need to use tf.Graph() at all. You just simply should call emotion_classifier.predict() as a simple python method outside and without of using any 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





share|improve this answer


























  • In the moment it detects a face, totally freezes and throws the same error, i'm using real time video streaming and when it receives the first image with a face in it, crashes.

    – Andrés Girón
    Nov 21 '18 at 23:05













  • @AndrésGirón first try to pass a single image to the predict function and see what happens and please reload the kernel to be sure. Simply you can't get the same error message

    – Geeocode
    Nov 21 '18 at 23:12













  • How do I reload the kernel?

    – Andrés Girón
    Nov 21 '18 at 23:18











  • @AndrésGirón How do you run your code? Command line or?

    – Geeocode
    Nov 21 '18 at 23:21











  • Command line with waitress-serve

    – Andrés Girón
    Nov 21 '18 at 23:32











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421233%2fvalueerrortensor-s-is-not-an-element-of-this-graph-obj%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I'm just trying to find out your real environment, but I guess you may use Keras amd some Keras model to predict emotions.



Your error message caused because of the line:



K.clear_session()


which, from the documentation: keras.backend.clear_session().
So you clear all graph it has been created, then you try to run the classifier's predict(), which lost all its context this way.

Thus just simply delete this line.



This section is was about some code the Op deleted:

In this task you don't need to use tf.Graph() at all. You just simply should call emotion_classifier.predict() as a simple python method outside and without of using any 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





share|improve this answer


























  • In the moment it detects a face, totally freezes and throws the same error, i'm using real time video streaming and when it receives the first image with a face in it, crashes.

    – Andrés Girón
    Nov 21 '18 at 23:05













  • @AndrésGirón first try to pass a single image to the predict function and see what happens and please reload the kernel to be sure. Simply you can't get the same error message

    – Geeocode
    Nov 21 '18 at 23:12













  • How do I reload the kernel?

    – Andrés Girón
    Nov 21 '18 at 23:18











  • @AndrésGirón How do you run your code? Command line or?

    – Geeocode
    Nov 21 '18 at 23:21











  • Command line with waitress-serve

    – Andrés Girón
    Nov 21 '18 at 23:32
















0














I'm just trying to find out your real environment, but I guess you may use Keras amd some Keras model to predict emotions.



Your error message caused because of the line:



K.clear_session()


which, from the documentation: keras.backend.clear_session().
So you clear all graph it has been created, then you try to run the classifier's predict(), which lost all its context this way.

Thus just simply delete this line.



This section is was about some code the Op deleted:

In this task you don't need to use tf.Graph() at all. You just simply should call emotion_classifier.predict() as a simple python method outside and without of using any 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





share|improve this answer


























  • In the moment it detects a face, totally freezes and throws the same error, i'm using real time video streaming and when it receives the first image with a face in it, crashes.

    – Andrés Girón
    Nov 21 '18 at 23:05













  • @AndrésGirón first try to pass a single image to the predict function and see what happens and please reload the kernel to be sure. Simply you can't get the same error message

    – Geeocode
    Nov 21 '18 at 23:12













  • How do I reload the kernel?

    – Andrés Girón
    Nov 21 '18 at 23:18











  • @AndrésGirón How do you run your code? Command line or?

    – Geeocode
    Nov 21 '18 at 23:21











  • Command line with waitress-serve

    – Andrés Girón
    Nov 21 '18 at 23:32














0












0








0







I'm just trying to find out your real environment, but I guess you may use Keras amd some Keras model to predict emotions.



Your error message caused because of the line:



K.clear_session()


which, from the documentation: keras.backend.clear_session().
So you clear all graph it has been created, then you try to run the classifier's predict(), which lost all its context this way.

Thus just simply delete this line.



This section is was about some code the Op deleted:

In this task you don't need to use tf.Graph() at all. You just simply should call emotion_classifier.predict() as a simple python method outside and without of using any 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





share|improve this answer















I'm just trying to find out your real environment, but I guess you may use Keras amd some Keras model to predict emotions.



Your error message caused because of the line:



K.clear_session()


which, from the documentation: keras.backend.clear_session().
So you clear all graph it has been created, then you try to run the classifier's predict(), which lost all its context this way.

Thus just simply delete this line.



This section is was about some code the Op deleted:

In this task you don't need to use tf.Graph() at all. You just simply should call emotion_classifier.predict() as a simple python method outside and without of using any 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






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 1:16

























answered Nov 21 '18 at 22:52









GeeocodeGeeocode

2,3501820




2,3501820













  • In the moment it detects a face, totally freezes and throws the same error, i'm using real time video streaming and when it receives the first image with a face in it, crashes.

    – Andrés Girón
    Nov 21 '18 at 23:05













  • @AndrésGirón first try to pass a single image to the predict function and see what happens and please reload the kernel to be sure. Simply you can't get the same error message

    – Geeocode
    Nov 21 '18 at 23:12













  • How do I reload the kernel?

    – Andrés Girón
    Nov 21 '18 at 23:18











  • @AndrésGirón How do you run your code? Command line or?

    – Geeocode
    Nov 21 '18 at 23:21











  • Command line with waitress-serve

    – Andrés Girón
    Nov 21 '18 at 23:32



















  • In the moment it detects a face, totally freezes and throws the same error, i'm using real time video streaming and when it receives the first image with a face in it, crashes.

    – Andrés Girón
    Nov 21 '18 at 23:05













  • @AndrésGirón first try to pass a single image to the predict function and see what happens and please reload the kernel to be sure. Simply you can't get the same error message

    – Geeocode
    Nov 21 '18 at 23:12













  • How do I reload the kernel?

    – Andrés Girón
    Nov 21 '18 at 23:18











  • @AndrésGirón How do you run your code? Command line or?

    – Geeocode
    Nov 21 '18 at 23:21











  • Command line with waitress-serve

    – Andrés Girón
    Nov 21 '18 at 23:32

















In the moment it detects a face, totally freezes and throws the same error, i'm using real time video streaming and when it receives the first image with a face in it, crashes.

– Andrés Girón
Nov 21 '18 at 23:05







In the moment it detects a face, totally freezes and throws the same error, i'm using real time video streaming and when it receives the first image with a face in it, crashes.

– Andrés Girón
Nov 21 '18 at 23:05















@AndrésGirón first try to pass a single image to the predict function and see what happens and please reload the kernel to be sure. Simply you can't get the same error message

– Geeocode
Nov 21 '18 at 23:12







@AndrésGirón first try to pass a single image to the predict function and see what happens and please reload the kernel to be sure. Simply you can't get the same error message

– Geeocode
Nov 21 '18 at 23:12















How do I reload the kernel?

– Andrés Girón
Nov 21 '18 at 23:18





How do I reload the kernel?

– Andrés Girón
Nov 21 '18 at 23:18













@AndrésGirón How do you run your code? Command line or?

– Geeocode
Nov 21 '18 at 23:21





@AndrésGirón How do you run your code? Command line or?

– Geeocode
Nov 21 '18 at 23:21













Command line with waitress-serve

– Andrés Girón
Nov 21 '18 at 23:32





Command line with waitress-serve

– Andrés Girón
Nov 21 '18 at 23:32




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53421233%2fvalueerrortensor-s-is-not-an-element-of-this-graph-obj%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith