Python 3.5 – 如何从函数内部删除(释放GPU内存)一个变量

huangapple go评论61阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年1月6日 17:47:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定