由于使用会话运行tf.contrib.image.sparse\u image\u warp而导致内存泄漏

2024-05-12 16:53:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个功能,扭曲一些面部图像使用他们的目的地标。但我注意到,随着时间的推移,我有一个内存泄漏,我检查了它使用内存探查器。代码如下:

def image_warping(src_img, src_landmarks, dest_landmarks):
    expanded_src_landmarks = np.expand_dims(np.float32(src_landmarks), axis=0)
    expanded_dest_landmarks = np.expand_dims(np.float32(dest_landmarks), axis=0)
    expanded_src_img = np.expand_dims(np.float32(src_img) / 255, axis=0)

    warped_img, dense_flows = sparse_image_warp.sparse_image_warp(expanded_src_img,
                          expanded_src_landmarks,
                          expanded_dest_landmarks,
                          interpolation_order=1,
                          regularization_weight=0.1,
                          num_boundary_points=2,
                          name='sparse_image_warp')

    with tf.Session() as sess:
        out_img = sess.run(warped_img)
        warp_img = np.uint8(out_img[0, :, :, :] * 255)
    session.close()
    return warp_img

内存分析器分别显示with tf.Session() as sess:out_img = sess.run(warped_img)行的466.645 MiB和174.574 MiB增量

有没有其他方法可以将扭曲的\u img从张量转换为numpy数组,而不是按会话运行?这个tf函数的结果比任何其他可用的图像扭曲代码都好,但我想知道为什么它返回一个张量,因为输入都是numpy数组。我不是在深度学习算法中使用这个函数


Tags: 内存imagesrcimgnpdestsessexpand