MTCNN 在第一次检测时不使用 GPU,但在后续检测中使用
MTCNN does not use GPU on first detection but does on following detections
我安装了 tensorflow 2.0 -gpu。我正在使用 MTCNN 进行人脸检测。在第一次检测人脸的调用中需要 3.86 秒。在下一次通话中,它只需要 0.049 秒。我怀疑它在第一次调用时没有使用 GPU,但在第二次调用时使用了。我知道 MTCNN 确实导入了 tensorflow,但我不明白为什么第一次调用时没有使用 GPU。代码如下。
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.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 上,然后继续使用实际图像。
我安装了 tensorflow 2.0 -gpu。我正在使用 MTCNN 进行人脸检测。在第一次检测人脸的调用中需要 3.86 秒。在下一次通话中,它只需要 0.049 秒。我怀疑它在第一次调用时没有使用 GPU,但在第二次调用时使用了。我知道 MTCNN 确实导入了 tensorflow,但我不明白为什么第一次调用时没有使用 GPU。代码如下。
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.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 上,然后继续使用实际图像。