我正在将tensorflow用于NIfTI文件(也称为.nii文件)。我制作了一个模型,打开了会话并运行了代码。我的代码如下:
def memory_usage_psutil():
# return the memory usage in GB
import psutil
process = psutil.Process(os.getpid())
mem = process.memory_info()[0] / float(2 ** 30)
return mem
sess = tf.Session()
sess.run(tf.global_variables_initializer())
for epoch in range(50):
for batch in range(len(imgs)//batch_size):
batch_imgs = [i.get_fdata() for i in imgs[batch*batch_size: (batch+1)*batch_size]]
with tf.device('/gpu:0'):
_, loss_val = sess.run([train, loss], feed_dict = {X:batch_imgs, Y:fi_train[batch*batch_size:(batch+1)*batch_size]})
print(epoch+1, '-th Epoch |', batch+1, '-th batch | Loss : ', loss_val)
print('Memory usage: %s (kb)'% resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
print("Memory usage: %s(gb)"%memory_usage_psutil())
tf.keras.backend.clear_session()
del _
del batch_imgs
del loss_val
其结果如下:
简而言之,尽管我使用了代码,但我的计算机的内存使用量仍在不断增加
tf.keras.backend.clear_session()
del _
del batch_imgs
del loss_val
还有,我尝试过tf.contrib.keras.backend.clear_session()
,tf.reset_default_graph()
,但是它们不起作用。我该如何解决这个问题?有人知道吗?