我在运行tensorflow单次检测,并在同一个脚本Keras convnet麻烦。得到这个奇怪的错误?

问题描述 投票:0回答:1

我想建立一个程序,可以决定我多少手指放在我伸出手来。我已经训练到周围画我的手边框单杆探测器。箱子被裁切掉,并输送到被训练来确定我有多少手指伸出一只convnet。他们都是经过培训和单独的工作就好了。然而,把它们都在同一个脚本,似乎没有任何工作。我得到这个错误:

ValueError: Tensor Tensor("softmax_1/Softmax:0", shape=(?, 6), dtype=float32) 
is not an element of this graph.

我相信这是与我如何在同一时间运行两个神经网络。

我试着写model._make_predict_function()我编译convnet模型后。然而,我得到这个:

File "/Users/spencerkraisler/Desktop/Hand_Sign_Classifier_2/hand_detector.py", line 66, in <module>
    pred = model.predict(box_image_exp)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py", line 1169, in predict
    steps=steps)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training_arrays.py", line 294, in predict_loop
    batch_outs = f(ins_batch)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2715, in __call__
    return self._call(inputs)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2671, in _call
    session)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py", line 2623, in _make_callable
    callable_fn = session._make_callable_from_options(callable_opts)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1471, in _make_callable_from_options
    return BaseSession._Callable(self, callable_options)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1425, in __init__
    session._session, options_ptr, status)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Tensor conv2d_1_input:0, specified in either feed_devices or fetch_devices was not found in the Graph
Exception ignored in: <bound method BaseSession._Callable.__del__ of <tensorflow.python.client.session.BaseSession._Callable object at 0x136f91320>>
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/client/session.py", line 1455, in __del__
    self._session._session, self._handle, status)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/tensorflow/python/framework/errors_impl.py", line 528, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.CancelledError: Session has been closed.

码:

import cv2
import os
import tensorflow as tf 
import numpy as np
from keras.models import Sequential, Model
from keras import metrics
from keras.layers import Conv2D, MaxPooling2D, Softmax
from keras.layers import Activation, Dropout, Flatten, Dense, ReLU
from keras.optimizers import Adam
from utils import detector_utils as detector_utils

# initializing convnet 
optimizer = Adam(lr=.01)
input_shape = (28, 28, 1)
num_classes = 6

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), input_shape=input_shape))
model.add(ReLU())
model.add(Conv2D(64, kernel_size=(3, 3)))
model.add(ReLU())
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128))
model.add(ReLU())
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Softmax())
model.load_weights("./convnet/classifier1.h5")
model.summary()
model.compile(
        loss='categorical_crossentropy',
        optimizer=optimizer,
        metrics=[metrics.categorical_accuracy])

# initializing ssd 
detection_graph, sess = detector_utils.load_inference_graph()

score_thresh = 0.2
im_width, im_height = 1280, 720
num_hands_detect = 1

def load_graph(frozen_graph_filename):
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    with tf.Graph().as_default() as graph:
        tf.import_graph_def(graph_def, name='prefix')
    return graph

with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        cap = cv2.VideoCapture(0)
        while(True):
            _, frame = cap.read()
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            boxes, scores = detector_utils.detect_objects(frame, detection_graph, sess)
            box_image = detector_utils.get_box_image(1, score_thresh, scores, boxes, im_width, im_height, frame)
            detector_utils.draw_box_on_image(1, score_thresh, scores, boxes, im_width, im_height, frame)
                        # this mean a hand was detected
            if box_image is not None:
                box_image = cv2.cvtColor(box_image, cv2.COLOR_BGR2GRAY)
                box_image = cv2.resize(box_image, (28, 28))
                box_image_exp = np.expand_dims(box_image, 2)
                box_image_exp = np.expand_dims(box_image_exp, 0)
                                # where the convnet makes its prediction
                pred = model.predict(box_image_exp)
                print(np.argmax(pred))

            cv2.imshow('frame', cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
            if box_image is not None:
                cv2.imshow('hand detection', box_image)
            else:
                cv2.imshow('hand detection', np.zeros((28, 28, 1)))

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cap.release()
        cv2.destroyAllWindows()

这个脚本应该每当SSD检测我的手打印出convnet的预测。

python tensorflow keras classification object-detection
1个回答
0
投票

我认为,问题的根源是,你创建默认的图形下Keras模型,但detection_graph范围内调用它。有几种可能的解决方法。我想尝试换模型生成代码(从model = Sequential()model.compile(...))进入功能和with detection_graph.as_default():下调用该函数。此外,可以肯定,在Keras后端会话设置为你明确地创建会话。所以,像这样的:

import cv2
...
from keras.optimizers import Adam
from keras import backend as K
from utils import detector_utils as detector_utils

# initializing convnet 
optimizer = Adam(lr=.01)
input_shape = (28, 28, 1)
num_classes = 6

def build_keras_model():
    model = Sequential()
    ...

    return model

# initializing ssd 
detection_graph, sess = detector_utils.load_inference_graph()
...
with detection_graph.as_default():
    with tf.Session(graph=detection_graph) as sess:
        K.set_session(sess)
        model = build_keras_model()
        model.compile(
            loss='categorical_crossentropy',
            optimizer=optimizer,
            metrics=[metrics.categorical_accuracy])

        cap = cv2.VideoCapture(0)
        while(True):
            _, frame = cap.read()
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            boxes, scores = detector_utils.detect_objects(frame, detection_graph, sess)
            box_image = detector_utils.get_box_image(1, score_thresh, scores, boxes, im_width, im_height, frame)
            detector_utils.draw_box_on_image(1, score_thresh, scores, boxes, im_width, im_height, frame)
                        # this mean a hand was detected
            if box_image is not None:
                box_image = cv2.cvtColor(box_image, cv2.COLOR_BGR2GRAY)
                box_image = cv2.resize(box_image, (28, 28))
                box_image_exp = np.expand_dims(box_image, 2)
                box_image_exp = np.expand_dims(box_image_exp, 0)
                                # where the convnet makes its prediction
                pred = model.predict(box_image_exp)
                print(np.argmax(pred))

            cv2.imshow('frame', cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
            if box_image is not None:
                cv2.imshow('hand detection', box_image)
            else:
                cv2.imshow('hand detection', np.zeros((28, 28, 1)))

            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        cap.release()
        cv2.destroyAllWindows()
© www.soinside.com 2019 - 2024. All rights reserved.