英文:
Python3.5-How to delete (release GPU memory) a variable from inside a function
问题
I defined a function in Python 3.5 called 'evaluate' and the code is shown below ('REC_Y', 'REC_U', 'REC_V' represent the 3 channels of a YCbCr image respectively):
import numpy as np
def evaluate(REC_Y, REC_U, REC_V):
height = 832
width = 480
bufY = np.reshape(np.asarray(REC_Y), (height, width))
bufU = np.reshape(np.asarray(REC_U), (int(height / 2), int(width / 2)))
bufV = np.reshape(np.asarray(REC_V), (int(height / 2), int(width / 2)))
return (np.stack((bufY, bufU, bufV), axis=2))
In order to release some GPU memory (since I already had a GPU MemoryError), I'd like to remove 'REC_Y', 'REC_U', 'REC_V' from memory after the last line of the code (after 'bufV = np.reshape(np.asarray(REC_V), (int(height / 2), int(width / 2)))'). I have tried 'del REC_Y', but it shown 'REC_Y' referenced before assignment. I have tried del global()["REC_Y"] but it shown that "REC_Y" is not defined as a global variable.
Could you please help me with this issue? How to delete 3 parameters of 'evaluate' function to release GPU memory?
Many thanks!
英文:
I defined a function in Python 3.5 called 'evaluate' and the code is shown below ('REC_Y', 'REC_U', 'REC_V' represent the 3 channels of a YCbCr image respectively):
import numpy as np
def evaluate(REC_Y, REC_U, REC_V):
height = 832
width = 480
bufY = np.reshape(np.asarray(REC_Y), (height, width))
bufU = np.reshape(np.asarray(REC_U), (int(height / 2), int(width / 2)))
bufV = np.reshape(np.asarray(REC_V), (int(height / 2), int(width / 2)))
return (np.stack((bufY, bufU, bufV), axis=2))
In order to release some GPU memory (since I already had a GPU MemoryError), I'd like to remove 'REC_Y','REC_U','REC_V' from memory after the last line of the code (after 'bufV = np.reshape(np.asarray(REC_V), (int(height / 2), int(width / 2)))'). I have tried 'del REC_Y', but it shown 'REC_Y' referenced before assignment. I have tried del global()["REC_Y"] but it shown that "REC_Y" is not defined as a global variable.
Could you please help me with this issue? How to delete 3 parameters of 'evaluate' function to release GPU memory?
Many thanks!
答案1
得分: 1
"Numpy"不支持GPU。
只有当您使用CuPy或CUDA操作时,才能尝试释放GPU上的一些内存 -> numpy
在CPU上运行。
英文:
Numpy
does not work on GPU.
Only if you had CUPY or CUDA operations could you try to free some memory on the GPU -> numpy
works on CPU.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论