R reticulate how to clear python object from memory?

I created a function that uses some python functions through the reticulate package, specially opening the image with PIL :

 image <- "~/Desktop/image.jpg" pil.image <- reticulate::import( "PIL.Image", convert = FALSE ) img <- pil.image$open( image ) 

Then I do a few things with the image (I extract several cultures), which works fine. Here is an example of what I am doing ( outputs is a data frame with the cultures I need, so crop.grid is just a 4- crop.grid vector.

 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 ) 

After that, I want to clear the image from memory (the images that I open are very large, so they take up a lot of memory). I am trying to close an image in python:

 img$close() 

As in R:

 rm( img ) gc() 

And replacing the object with what I know is very small.

 img <- reticulate::r_to_py( 1L ) 

All of these things work fine, but my RAM is still registered as very full. I try them with each of my python objects, but the only thing that effectively clears the RAM is to restart the R session.

I know that in python I would be better off opening an image with with to clear it at the end of the process, but I'm not sure how to implement this with reticulate .


--- UPDATE with a similar version of python :

If I do the above in python directly:

 from PIL import Image img = Image.open( "/home/user/Desktop/image.jpg" ) output = img.crop( [0,0,100,100] ) 

And then close:

 output.close() img.close() 

The memory is cleared. The same does not work from inside R. That is:

 output.array$close() img$close() gc() # for good measure 

Does not clear memory.

+7
r python-imaging-library reticulate
source share
1 answer

You need to do 3 things:

  • Explicit object creation in 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 
  • When you finish working with the image, first delete the Python object.

     py_run_string("del img") 
  • Then run the Python garbage collector.

     py_gc <- import("gc") py_gc$collect() 

Steps 2 and 3 are important. Step 1 is easy so that you have a name to delete. If there is a way to remove the β€œimplicit” Python objects (I can't think of a better term), this will save some template.

+3
source share

All Articles