科研笔记(2018年04月10日23:02:03)关于内存泄露等问题的处理

note(个人笔记)

实验内容:CNN做deringing的实验中发现,在处理较大规模(GB级)地震资料时出现内存泄露,经排查找到原因:在test过程中,每次循环都调用了forward函数,forward函数中执行两个操作:根据被test的数据的shape创建一个placeholder为该尺寸的计算图,和训练好的神经网络共享权重,reuse设置为True,然后投入数据进行计算。利用psutil中的mem_info() 方法查出该进程在执行不同语句时所耗费的内存,可以看到在进行读写操作时无太大内存消耗,而在调用forward函数时消耗较大,并且没有释放完全。考虑到建立计算图在每次循环都要进行,而本次实际处理的数据,即地震资料.sgy文件实际上绝大多数炮集的大小都是相同的,因此在forward函数中加入判断变量,即:

def forward(self, noisy_image, isRecon=True):
    if isRecon:
        self.sampler(noisy_image)
    return self.sess.run(self.Y_test, feed_dict={self.X_test: noisy_image})

这里的isRecon表示是否需要重新建立计算图,在process中,加入如下判断:

if i > 0 and gather.shape[0] == size1 and gather.shape[1] == size2:
    isrecon = False
else:
    isrecon = True
    size1, size2 = gather.shape[0], gather.shape[1]

如果此次循环读出的道集和上一次大小相等,那就无需重新建立graph。最终,程序得以正常运行,基本无内存泄露,评价每一个道集的处理时间约为2.7 seconds,18G左右的数据需要半小时即可处理完(之前由于内存泄露速度逐渐变慢,同样数据处理需要约4小时):

......
[*] [     710] th gather finished!
[*] memory usage current process 1.5051 GB(s)
[*] time eclipsed : 2.7070
[*] [     720] th gather finished!
[*] memory usage current process 1.5051 GB(s)
[*] time eclipsed : 2.7073
[*] [     730] th gather finished!
[*] memory usage current process 1.5051 GB(s)
[*] time eclipsed : 2.7072
[*] [     740] th gather finished!
[*] memory usage current process 1.5051 GB(s)
[*] time eclipsed : 2.7075
[*] [     750] th gather finished!
[*] memory usage current process 1.5051 GB(s)
[*] time eclipsed : 2.7075
[*] [     760] th gather finished!
[*] memory usage current process 1.5051 GB(s)
[*] time eclipsed : 2.7083
[*] [     770] th gather finished!
[*] memory usage current process 1.5273 GB(s)
[*] time eclipsed : 2.7302
[*] [     780] th gather finished!
[*] memory usage current process 1.6438 GB(s)
[*] time eclipsed : 2.8163

2018年04月10日23:39:08
每个聪明人都知道人生是美好的,人生的目的是获得幸福。但最后只有傻瓜们才会幸福。 —— 作家 奥尔罕 帕慕克

原文地址:https://www.cnblogs.com/morikokyuro/p/13256753.html