R reticulate,如何从内存中清除 python 对象?
R reticulate, how do I clear a python object from memory?
我通过 reticulate
包创建了一个使用某些 python 功能的函数,特别是使用 PIL
:
打开图像
image <- "~/Desktop/image.jpg"
pil.image <- reticulate::import( "PIL.Image", convert = FALSE )
img <- pil.image$open( image )
然后我对图像做了一些处理(我提取了几种裁剪),效果很好。这是我正在做的一个例子(outputs
是我需要的农作物数据框,所以 crop.grid
只是一个包含 4 个数字的向量。
crop.grid <- c( outputs$x.start[x],
outputs$y.start[x],
outputs$x.stop[x],
outputs$y.stop[x] )
crop.grid <- as.integer( crop.grid )
crop.grid <- reticulate::r_to_py( crop.grid )
output.array <- img$crop( box = crop.grid )
output.array$save( output.filename )
之后,我想从内存中清除图像(我打开的图像非常大,所以占用大量内存)。我尝试关闭图像 python:
img$close()
以及在 R 中:
rm( img )
gc()
并用我知道非常小的东西替换对象。
img <- reticulate::r_to_py( 1L )
所有这些 运行 都很好,但我的 RAM 仍然注册为非常满。我对我创建的每个 python 对象进行了尝试,但唯一有效清除 RAM 的方法是重新启动 R 会话。
我知道在 python
内我最好使用 with
打开图像以便在过程结束时清除它,但我不确定如何实现使用 reticulate
.
--- 使用类似的 python
版本进行更新:
如果我直接在 python 中执行上述操作:
from PIL import Image
img = Image.open( "/home/user/Desktop/image.jpg" )
output = img.crop( [0,0,100,100] )
然后关闭东西:
output.close()
img.close()
内存清除。但是,同样的事情在 R 中不起作用。即:
output.array$close()
img$close()
gc() # for good measure
不清除内存。
你需要做三件事:
在Python中显式创建对象:
py_env <- py_run_string(
paste(
"from PIL import Image",
"img = Image.open('~/Desktop/image.jpg')",
sep = "\n"
),
convert = FALSE
)
img <- py_env$img
处理完图像后,请先删除 Python 对象。
py_run_string("del img")
然后 运行 Python 垃圾收集器。
py_gc <- import("gc")
py_gc$collect()
第 2 步和第 3 步是重要的。第 1 步只是让您有一个要删除的名称。如果有办法删除 "implicit" Python 对象(想不出更好的术语),那将节省一些样板文件。
我通过 reticulate
包创建了一个使用某些 python 功能的函数,特别是使用 PIL
:
image <- "~/Desktop/image.jpg"
pil.image <- reticulate::import( "PIL.Image", convert = FALSE )
img <- pil.image$open( image )
然后我对图像做了一些处理(我提取了几种裁剪),效果很好。这是我正在做的一个例子(outputs
是我需要的农作物数据框,所以 crop.grid
只是一个包含 4 个数字的向量。
crop.grid <- c( outputs$x.start[x],
outputs$y.start[x],
outputs$x.stop[x],
outputs$y.stop[x] )
crop.grid <- as.integer( crop.grid )
crop.grid <- reticulate::r_to_py( crop.grid )
output.array <- img$crop( box = crop.grid )
output.array$save( output.filename )
之后,我想从内存中清除图像(我打开的图像非常大,所以占用大量内存)。我尝试关闭图像 python:
img$close()
以及在 R 中:
rm( img )
gc()
并用我知道非常小的东西替换对象。
img <- reticulate::r_to_py( 1L )
所有这些 运行 都很好,但我的 RAM 仍然注册为非常满。我对我创建的每个 python 对象进行了尝试,但唯一有效清除 RAM 的方法是重新启动 R 会话。
我知道在 python
内我最好使用 with
打开图像以便在过程结束时清除它,但我不确定如何实现使用 reticulate
.
--- 使用类似的 python
版本进行更新:
如果我直接在 python 中执行上述操作:
from PIL import Image
img = Image.open( "/home/user/Desktop/image.jpg" )
output = img.crop( [0,0,100,100] )
然后关闭东西:
output.close()
img.close()
内存清除。但是,同样的事情在 R 中不起作用。即:
output.array$close()
img$close()
gc() # for good measure
不清除内存。
你需要做三件事:
在Python中显式创建对象:
py_env <- py_run_string( paste( "from PIL import Image", "img = Image.open('~/Desktop/image.jpg')", sep = "\n" ), convert = FALSE ) img <- py_env$img
处理完图像后,请先删除 Python 对象。
py_run_string("del img")
然后 运行 Python 垃圾收集器。
py_gc <- import("gc") py_gc$collect()
第 2 步和第 3 步是重要的。第 1 步只是让您有一个要删除的名称。如果有办法删除 "implicit" Python 对象(想不出更好的术语),那将节省一些样板文件。