获取guppy.hpy()不工作的tensorflow模型的内存使用率

2024-06-03 11:08:23 发布

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

我正在加载已保存的tensorflow模型(.pb文件),并尝试评估它为带有guppy包的模型分配了多少内存。在simple tutorial之后,我尝试了以下内容:

from guppy import hpy
import tensorflow as tf

heap = hpy()

print("Heap Status at starting: ")
heap_status1 = heap.heap()
print("Heap Size : ",  heap_status1.size, " bytes\n")
print(heap_status1)

heap.setref()

print("\nHeap Status after setting reference point: ")
heap_status2 = heap.heap()
print("Heap size: ", heap_status2.size, " bytes\n")
print(heap_status2)

model_path = "./saved_model/" #.pb file directory
model = tf.saved_model.load(model_path)

print("\nHeap status after creating model: ")
heap_status3 = heap.heap()
print("Heap size: ", heap_status3.size, " bytes\n")
print(heap_status3)

print("Memory used by the model: ", heap_status3.size - heap_status2.size)

我不知道为什么,但是当我运行代码时,当我调用heap_status1 = heap.heap()时,它突然停止执行。它不会抛出任何错误

当我不使用任何与tensorflow相关的东西时,同样的代码运行良好,即当我只创建一些随机列表、字符串等而不是加载tensorflow模型时,它成功运行

注意:我的型号将在CPU设备中运行。不幸的是,tf.config.experimental.get_memory_info仅适用于GPU


Tags: 模型importsizemodelbytestftensorflowheap