python - 删除模型后,如何在 Python 中释放 TF/Keras 内存,而其他模型仍在内存中并在使用中?

标签 python tensorflow keras memory-management

我有一个 Python 服务器应用程序,它提供 TensorFlow/Keras 模型推理服务。可以为多个不同的客户端同时加载和使用多个不同的此类模型。一个客户端可以请求加载另一个模型,但这对其他客户端没有影响(即他们的模型保留在内存中并按原样使用,因此每个客户端都可以请求加载另一个模型,而不管任何其他客户端的状态)。

逻辑和实现有效,但是,我不确定如何在此设置中正确释放内存。当客户端请求加载新模型时,先前加载的模型将简单地从内存中删除(通过 Python del 命令),然后通过 tensorflow 加载新模型。 keras.models.load_model().

根据我在 Keras documentation 中阅读的内容人们可能想通过调用 tf.keras.backend.clear_session() 来清除 Keras session 以释放内存。但是,这似乎释放了所有 TF 内存,这对我来说是个问题,因为其他客户端的其他 Keras 模型仍在同时使用,如上所述。

此外,我似乎无法将每个模型放入它们自己的进程中,因为我无法从不同的运行进程并行(或根本无法)访问单个 GPU。

所以换句话说:当加载一个新的 TensorFlow/Keras 模型时,而其他模型也在内存中并正在使用中,我如何才能从预先加载的模型中释放 TF 内存,而不干扰其他当前加载的模型?

最佳答案

当 Tensorflow session 启动时,它将尝试分配所有可用的 GPU 内存。这就是防止多个进程运行 session 的原因。阻止这种情况的理想方法是确保 tf session 只分配一部分内存。来自docs ,有两种方法可以做到这一点(取决于你的 tf 版本)

  1. 简单的方法是(tf 2.2+)
import tensorflow as tf
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
  tf.config.experimental.set_memory_growth(gpu, True)

对于 tf 2.0/2.1

import tensorflow as tf
tf.config.gpu.set_per_process_memory_growth(True)

对于 tf 1.*(每个进程分配 30% 的内存百分比)

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.333)

sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
  1. 另一种方法更受控制恕我直言,并且扩展性更好。它要求您创建逻辑设备并手动控制每个逻辑设备的放置。
gpus = tf.config.experimental.list_physical_devices('GPU')
if gpus:
# Restrict TensorFlow to only allocate 1GB of memory on the first GPU
    try:
        tf.config.experimental.set_virtual_device_configuration(
            gpus[0],
            [tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024),
             tf.config.experimental.VirtualDeviceConfiguration(memory_limit=1024)]
     except RuntimeError as e:
            # Virtual devices must be set before GPUs have been initialized
         print(e)

现在您必须使用 with 手动控制位置

gpus = tf.config.experimental.list_logical_devices('GPU')
if gpus:
  # Replicate your computation on multiple GPUs
  c = []
  for gpu in gpus:
    with tf.device(gpu.name):
      a = tf.constant([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
      b = tf.constant([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]])
      c.append(tf.matmul(a, b))

  with tf.device('/CPU:0'):
    matmul_sum = tf.add_n(c)

  print(matmul_sum)

使用它你不会耗尽内存并且可以一次运行多个进程。

关于python - 删除模型后,如何在 Python 中释放 TF/Keras 内存,而其他模型仍在内存中并在使用中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65723891/

相关文章:

python - 从 pandas 数据帧加载 Keras 中的批量图像

python - 为什么 numpy 可以保存和加载不同于 numpy 数组的对象

python - 树莓派 2-无法安装 scikit-image

python - 如何在批处理中正确使用 Tensorflow 数据集?

Tensorflow: TypeError: __new__() 有一个意外的关键字参数 'file'

python - 为什么 model.predict 与最后一层(keras)的输出不同?

python - 获取 "declarative_base()"中最后插入记录的主键

python - Django View 没有返回值的函数

python - 无法在 Python TensorFlow 中用 LSTMBlockFusedCell 替换 LSTMBlockCell

python - Keras中model.compile()和model.add_loss()中loss的区别