TensorFlow 运行 在线程中出现问题

Something wrong when TensorFlow running in thread

我正在编写一个多线程人脸识别程序,使用 Keras 作为高级模型,以 tensorflow 作为后端。代码如下:

class FaceRecognizerTrainThread(QThread):

    def run(self):
        print("[INFO] Loading images...")
        images, org_labels, face_classes = FaceRecognizer.load_train_file(self.train_file)

        print("[INFO] Compiling Model...")
        opt = SGD(lr=0.01)
        face_recognizer = LeNet.build(width=Const.FACE_SIZE[0], height=Const.FACE_SIZE[1], depth=Const.FACE_IMAGE_DEPTH,
                                      classes=face_classes, weightsPath=None)
        face_recognizer.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])

        images = np.array(images)[:, np.newaxis, :, :] / 255.0
        labels = np_utils.to_categorical(org_labels, face_classes)

        print("[INFO] Training model...")
        try:
            face_recognizer.fit(images, labels, epochs=50, verbose=2, batch_size=10)
        except Exception as e:
            print(e)
        print("[INFO] Training model done...")

        save_name = "data/CNN_" + time.strftime("%Y%m%d%H%M%S", time.localtime()) + ".hdf5"
        if save_name:
            face_recognizer.save(save_name)

        self.signal_train_end.emit(save_name)

在正常模式下 运行 一切正常,但是当我 运行 它在 QThread 中并且当它进入

face_recognizer.fit(images, labels, epochs=50, verbose=2, batch_size=10)

它给我错误:

Cannot interpret feed_dict key as Tensor: Tensor Tensor("conv2d_1_input:0", shape=(?, 1, 30, 30), dtype=float32) is not an element of this graph.

我该如何解决?欢迎任何建议,非常感谢~~~~

TensorFlow 允许您定义 tf.Graph(),然后您可以使用图形创建 tf.Session(),然后 运行 定义在图形中的操作。当您以这种方式进行操作时,每个 QThread 都在尝试创建自己的 TF Graph。这就是为什么您会收到 not an element of this graph 错误的原因。我没有看到您的 feed_dict 代码,所以我假设它可能是 运行 在您的其他线程看不到的主线程中。在每个线程中包含您的 feed_dict 可能会使它起作用,但如果不查看您的完整代码就很难得出结论。

Replicating models in Keras and Tensorflow for a multi-threaded setting 可能对你有帮助。

要解决您的问题,您应该使用类似于 this post 的方法。从 post:

转载的代码
# Thread body: loop until the coordinator indicates a stop was requested.
# If some condition becomes true, ask the coordinator to stop.
def MyLoop(coord):
  while not coord.should_stop():
    ...do something...
    if ...some condition...:
      coord.request_stop()

# Main thread: create a coordinator.
coord = tf.train.Coordinator()

# Create 10 threads that run 'MyLoop()'
threads = [threading.Thread(target=MyLoop, args=(coord,)) for i in xrange(10)]

# Start the threads and wait for all of them to stop.
for t in threads:
  t.start()
coord.join(threads)

inter_opintra_op 并行性 here

也值得一读