【问题标题】:How to load models trained on GPU into CPU (system) memory?如何将在 GPU 上训练的模型加载到 CPU(系统)内存中?
【发布时间】:2019-10-17 17:47:42
【问题描述】:

我在 GPU 上训练了一个模型,现在我正在尝试在 CPU 上对其进行评估(GPU 被用于不同的火车运行)。但是,当我尝试使用以下方式加载它时:

with tf.device('/cpu:0'):
    model = tf.keras.models.load_model('models/resnet50_untrained.h5', compile=False)

我收到了CUDA_ERROR_OUT_OF_MEMORY

2019-10-17 09:25:23.088408: W tensorflow/compiler/xla/service/platform_util.cc:256] unable to create StreamExecutor for CUDA:0: failed initializing StreamExecutor for CUDA device ordinal 0: Internal: failed call to cuDevicePrimaryCtxRetain: CUDA_ERROR_OUT_OF_MEMORY: out of memory; total memory reported: 7981694976
2019-10-17 09:25:23.088505: F tensorflow/stream_executor/lib/statusor.cc:34] Attempting to fetch value instead of handling error Internal: no supported devices found for platform CUDA

(我也尝试设置compile=True,结果相同。)

似乎模型正在加载到另一个实例已使用的 GPU 中。如何强制 keras/tensorflow 将其加载到系统内存并在 CPU 上执行?

【问题讨论】:

    标签: python tensorflow keras


    【解决方案1】:

    您能否定义with tf.device('/cpu:0'): 中的所有内容,除了库导入部分和测试。

    如果这不起作用,那么创建一个虚拟环境并安装普通的 tensorflow 而不是 gpu 版本,然后尝试它。如果仍然是 OOM 错误,则说明 CPU 正在使用,没有足够的内存来加载这个训练好的模型。

    【讨论】:

    • 感谢您的建议。我尝试在没有 tensorflow-gpu 的情况下将模型加载到 virtualenv 中,并且成功了!但是,我发现 tf.device('/cpu:0') 没有做到这一点令人惊讶。上面的代码实际上就是我试图运行的所有代码。
    • 有时,由于某些依赖关系,它无法按照我们的预期工作。但我很高兴,它正在工作。干杯!
    • 也请考虑接受它作为答案。 ^^
    • 我希望找到一个不需要切换 virtualenvs 的解决方案。我在干净的环境中安装了所有东西,所以不太可能是依赖问题。
    猜你喜欢
    • 2022-06-14
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 2017-03-30
    • 2021-07-30
    相关资源
    最近更新 更多