Python MTCNN在第一次检测时不使用GPU,但在后续检测时使用GPU
我已经安装了tensorflow 2.0-gpu。我正在使用MTCNN进行人脸检测。第一次呼叫检测人脸需要3.86秒。下一次通话只需0.049秒。我怀疑它在第一次调用时没有使用GPU,但在第二次调用时使用GPU。我知道MTCNN确实导入了tensorflow,但我不明白为什么第一次调用时不使用GPU。代码如下Python MTCNN在第一次检测时不使用GPU,但在后续检测时使用GPU,python,tensorflow2.0,Python,Tensorflow2.0,我已经安装了tensorflow 2.0-gpu。我正在使用MTCNN进行人脸检测。第一次呼叫检测人脸需要3.86秒。下一次通话只需0.049秒。我怀疑它在第一次调用时没有使用GPU,但在第二次调用时使用GPU。我知道MTCNN确实导入了tensorflow,但我不明白为什么第一次调用时不使用GPU。代码如下 import time from mtcnn import MTCNN import cv2 #********first run of image detection - note re
import time
from mtcnn import MTCNN
import cv2
#********first run of image detection - note resulting process time- think not using gpu
detector = MTCNN()
img_file=r'c:\Temp\people\storage\1.jpg'
img = cv2.imread(img_file, cv2.COLOR_BGR2RGB)
start=time.time()
detector.detect_faces(img)
stop=time.time()
duration = stop-start
print(duration)
# rerun image detection on the same image - note duration much less must be using gpu
start=time.time()
detector.detect_faces(img)
stop=time.time()
duration = stop-start
print(duration)
Using TensorFlow backend.
3.8625590801239014
0.049181222915649414
它在第一次调用时使用GPU。分配模型参数和在内存中创建计算图的主要开销。您可以先使用一个小的“虚拟”图像(它不必是全尺寸的),以便在GPU上形成ops和放置变量,然后继续使用实际图像。我也这么认为。但是,如果您第二次调用detect时使用的图像与第一个图像不同,则持续时间仍然很大。我将所有图像设置为224 X 224,并执行了4个图像,结果持续时间为3.6、.023、22、.22、.09,因此我想您是对的-谢谢