用TensorFlow编写的facenet库中的内存泄漏

时间:2018-02-21 09:39:57

标签: python python-3.x tensorflow memory-leaks

我想使用此示例https://github.com/davidsandberg/facenet/blob/master/contributed/real_time_face_recognition.py,但是要在一组图像上运行面部识别。我的main函数如下所示

def main(args):
    face_recognition = face.Recognition()
    if args.debug:
        face.debug = True   
    path = './images/'
    folders = os.listdir(path)
    for f in folders:
        images = os.listdir(path + f)
        for i in images:
            img = cv2.imread(path + f + '/' + i)
            start = time.time()
            faces = face_recognition.identify(img)
            for face_n in faces:
                print(i, '-', face_n.name, '-', time.time() - start)
    del images, path, img, faces, folders

当我检查RAM的使用方式时,我发现了内存泄漏:

>  183.5 MiB     183.5 MiB    @profile(stream=fp)   
>                             def main(args):
>  383.7 MiB     200.2 MiB       face_recognition = face.Recognition()
>  383.7 MiB       0.0 MiB       if args.debug:
>                                    face.debug = True
>     
>  383.7 MiB       0.0 MiB       path = './images/'
>  383.7 MiB       0.0 MiB       folders = os.listdir(path)
>  1122.2 MiB     -9.7 MiB       for f in folders[:5]:
>  1113.9 MiB      0.0 MiB           images = os.listdir(path + f)
>  1124.9 MiB    -55.1 MiB           for i in images:
>  1124.9 MiB    -41.9 MiB               img = cv2.imread(path + f + '/' + i)
>  1124.9 MiB    -52.4 MiB               start = time.time()
>  1124.9 MiB    679.9 MiB               faces = face_recognition.identify(img)
>  1124.9 MiB   -112.1 MiB               for face_n in faces:
>  1124.9 MiB    -52.7 MiB                   print(i, '-', face_n.name, '-', time.time() - start)
>  1122.2 MiB      0.0 MiB       del images, path, img, faces, folders

看起来主要泄漏位于https://github.com/davidsandberg/facenet/blob/master/contributed/face.py

>    141    389.0 MiB    389.0 MiB       @profile(stream=fp)
>    142                                 def find_faces(self, image):
>    143    389.0 MiB      0.0 MiB           faces = []
>    144                             
>    145    389.0 MiB      0.0 MiB           bounding_boxes, _ = detect_face.detect_face(image, self.minsize,
>    146    389.0 MiB      0.0 MiB                                                             self.pnet, self.rnet, self.onet,
>    147    486.7 MiB     97.7 MiB                                                             self.threshold, self.factor)
>    148    486.7 MiB      0.0 MiB           for bb in bounding_boxes:

当我需要使用循环时如何解决这个问题?

0 个答案:

没有答案