如何在tensorflow python中使用单个GPU同时运行多个模型?

如何在tensorflow python中使用单个GPU同时运行多个模型?,python,tensorflow,deep-learning,Python,Tensorflow,Deep Learning,我正试图将我的笔记本电脑用作人脸识别的服务器,并将语音合成用于我的raspberry pi项目。所以我创建了一个程序,最初加载两个模型,然后等待请求的到来。但是当我启动程序时,最初faceRecognition模型加载成功,但在加载speechSynthesis模型时,对于tf.saver,它给出了as错误。 代码: 服务器- def findFaceMatch(): image\u file=request.files.get(“imagefile”) image\u file.save(“i

我正试图将我的笔记本电脑用作人脸识别的服务器,并将语音合成用于我的raspberry pi项目。所以我创建了一个程序,最初加载两个模型,然后等待请求的到来。但是当我启动程序时,最初faceRecognition模型加载成功,但在加载speechSynthesis模型时,对于tf.saver,它给出了as错误。 代码:

服务器-

def findFaceMatch():
image\u file=request.files.get(“imagefile”)
image\u file.save(“image.jpg”)
打印(“发送检查”)
response=face\u match\u demo.recognizeFace(os.path.join(os.getcwd(),“image.jpg”))
回复,200
@api.路线(“/synthetic/”)
def合成器语音(文本):
打印(文本)
将open(“F:/file.wav”,“wb”)作为F:
f、 写(合成器。合成(文本))
从_目录返回send_(“F:/”,“file.wav”,如_attachment=True),200
人脸识别-

将tensorflow导入为tf
将numpy作为np导入
从…起导入facenet
from.align导入检测面
进口cv2
导入imutils
导入操作系统
进口泡菜
导入时间
minsize=20
阈值=[0.6,0.7,0.7]
系数=0.709
保证金=44
输入图像大小=160
def加载模式(会话):
全球sess
sess=会话
全球pnet、rnet、onet
pnet,rnet,onet=检测面部。创建面部cnn(sess,os.path.join(os.getcwd(),“面部识别”,“对齐”))
facenet.load_模型(os.path.join(os.getcwd(),“人脸识别”,“20170512-110547\\20170512-110547.pb”))
全局图像\u占位符
images\u placeholder=tf.get\u default\u graph().get\u tensor\u by\u name(“输入:0”)
全局嵌入
embeddings=tf.get_default_graph().get_tensor_by_name(“embeddings:0”)
全局相位\u列\u占位符
phase\u train\u placeholder=tf.get\u default\u graph().get\u tensor\u by\u name(“phase\u train:0”)
全局嵌入大小
嵌入大小=嵌入。获取形状()[1]
def getFace(img):
面=[]
img_size=np.asarray(img.shape)[0:2]
边界框,检测面。检测面(img、minsize、pnet、rnet、onet、阈值、因子)
如果不是len(边界框)==0:
对于边界框中的面:
如果面[4]>0.50:
det=np.挤压(面[0:4])
bb=np.zero(4,dtype=np.int32)
bb[0]=np.最大值(det[0]-余量/2,0)
bb[1]=np.最大值(det[1]-裕度/2,0)
bb[2]=np.最小值(det[2]+余量/2,img_尺寸[1])
bb[3]=np.最小值(det[3]+余量/2,img\U尺寸[0])
裁剪=img[bb[1]:bb[3],bb[0]:bb[2],:]
resized=cv2.resize(裁剪,(输入图像大小,输入图像大小),插值=cv2.INTER\u立方)
预美白=facenet.prewhiten(调整大小)
faces.append(getEmbedding(预白化))
返回面
def getEmbedding(已调整大小):
重塑=调整大小。重塑(-1,输入图像大小,输入图像大小,3)
feed_dict={images_占位符:重塑,phase_train_占位符:False}
嵌入=sess.run(嵌入,feed\u dict=feed\u dict)
返回嵌入
def比较器2面(img1):
打印(“检查”)
face2=getFace(img1)
面1=[]
将open(os.path.join(os.getcwd(),“Face\u recognition”,“trained\u knn\u model.PB”),“rb”)作为f:
对于范围(4)中的i:
面1.追加(pickle.load(f))
名称=[“x”、“y”、“z”、“p”]
打印(“验证名称”)
对于范围(0,len(面1))中的i:
如果面1[i]和面2:
#计算欧氏距离
dist=np.sqrt(np.sum(np.square(np.subtract,面1[i],面2[0]))
如果dist至少达到TF 2.0(不确定您的版本),将多个模型加载到一个图中可能会导致问题:请参阅

我希望这有帮助

2019-11-11 21:48:04.408636: W tensorflow/core/framework/op_kernel.cc:1502] OP_REQUIRES failed at save_restore_v2_ops.cc:184 : Not found: Key onet/conv1/biases not found in checkpoint
Traceback (most recent call last):
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1356, in _do_call
    return fn(*args)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1341, in _run_fn
    options, feed_dict, fetch_list, target_list, run_metadata)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1429, in _call_tf_sessionrun
    run_metadata)
tensorflow.python.framework.errors_impl.NotFoundError: 2 root error(s) found.
  (0) Not found: Key onet/conv1/biases not found in checkpoint
         [[{{node save/RestoreV2}}]]
         [[save/RestoreV2/_617]]
  (1) Not found: Key onet/conv1/biases not found in checkpoint
         [[{{node save/RestoreV2}}]]
0 successful operations.
0 derived errors ignored.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1286, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 950, in run
    run_metadata_ptr)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1173, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1350, in _do_run
    run_metadata)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1370, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: 2 root error(s) found.
  (0) Not found: Key onet/conv1/biases not found in checkpoint
         [[node save/RestoreV2 (defined at F:\Backend\Text_To_Speech\synthesizer.py:25) ]]
         [[save/RestoreV2/_617]]
  (1) Not found: Key onet/conv1/biases not found in checkpoint
         [[node save/RestoreV2 (defined at F:\Backend\Text_To_Speech\synthesizer.py:25) ]]
0 successful operations.
0 derived errors ignored.

Original stack trace for 'save/RestoreV2':
  File "commonServer.py", line 38, in <module>
    synthesizer.load(model_path,sess)
  File "F:\Backend\Text_To_Speech\synthesizer.py", line 25, in load
    saver = tf.train.Saver()
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 825, in __init__
    self.build()
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 837, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 875, in _build
    build_restore=build_restore)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 508, in _build_internal
    restore_sequentially, reshape)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 328, in _AddRestoreOps
    restore_sequentially)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 575, in bulk_restore
    return io_ops.restore_v2(filename_tensor, names, slices, dtypes)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 1696, in restore_v2
    name=name)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3616, in create_op
    op_def=op_def)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2005, in __init__
    self._traceback = tf_stack.extract_stack()


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1296, in restore
    names_to_keys = object_graph_key_mapping(save_path)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1614, in object_graph_key_mapping
    object_graph_string = reader.get_tensor(trackable.OBJECT_GRAPH_PROTO_KEY)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py", line 678, in get_tensor
    return CheckpointReader_GetTensor(self, compat.as_bytes(tensor_str))
tensorflow.python.framework.errors_impl.NotFoundError: Key _CHECKPOINTABLE_OBJECT_GRAPH not found in checkpoint

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "commonServer.py", line 38, in <module>
    synthesizer.load(model_path,sess)
  File "F:\Backend\Text_To_Speech\synthesizer.py", line 26, in load
    saver.restore(self.session, checkpoint_path)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 1302, in restore
    err, "a Variable name or other graph key that is missing")
tensorflow.python.framework.errors_impl.NotFoundError: Restoring from checkpoint failed. This is most likely due to a Variable name or other graph key that is missing from the checkpoint. Please ensure that you have not altered the graph expected based on the checkpoint. Original error:

2 root error(s) found.
  (0) Not found: Key onet/conv1/biases not found in checkpoint
         [[node save/RestoreV2 (defined at F:\Backend\Text_To_Speech\synthesizer.py:25) ]]
         [[save/RestoreV2/_617]]
  (1) Not found: Key onet/conv1/biases not found in checkpoint
         [[node save/RestoreV2 (defined at F:\Backend\Text_To_Speech\synthesizer.py:25) ]]
0 successful operations.
0 derived errors ignored.

Original stack trace for 'save/RestoreV2':
  File "commonServer.py", line 38, in <module>
    synthesizer.load(model_path,sess)
  File "F:\Backend\Text_To_Speech\synthesizer.py", line 25, in load
    saver = tf.train.Saver()
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 825, in __init__
    self.build()
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 837, in build
    self._build(self._filename, build_save=True, build_restore=True)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 875, in _build
    build_restore=build_restore)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 508, in _build_internal
    restore_sequentially, reshape)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 328, in _AddRestoreOps
    restore_sequentially)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\training\saver.py", line 575, in bulk_restore
    return io_ops.restore_v2(filename_tensor, names, slices, dtypes)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_io_ops.py", line 1696, in restore_v2
    name=name)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 788, in _apply_op_helper
    op_def=op_def)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\util\deprecation.py", line 507, in new_func
    return func(*args, **kwargs)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 3616, in create_op
    op_def=op_def)
  File "C:\Users\Jaydip Bari\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2005, in __init__
    self._traceback = tf_stack.extract_stack()