Q:

Efficiently slice and read images using multiprocessing

I have a large satellite image and would like to run an object detection model inference on it. Currently I slice the large image, save the tiles and then read them to have my model output the detections (boxes and masks). I understand this is an inefficient way of doing things, since once the image slice/tile is read, it's no longer required but I'm currently saving it to disk. 
Is there a more efficient way to do this process? Perhaps via the multiprocessing or ray libraries? 

A:

As you mentioned, Ray would be a good fit due to the use of shared memory and the ability to run the same code on one or more machines.
Something like the following structure could work.
import numpy as np
import ray

ray.init()

@ray.remote
def do_object_detection(image, index):
    image_slice = image[index]
    # Do object detection.
    return 1

# Store the object in shared memory.
image = np.ones((1000, 1000))
image_id = ray.put(image)

# Process the slices in parallel. You probably want to use 2D slices instead
# of 1D slices.
result_ids = [do_object_detection.remote(image_id, i) for i in range(1000)]
results = ray.get(result_ids)

Note that the workers that execute the do_object_detection task will not create their own copy of the image. Instead, they will have access to a copy of the image that lives in shared memory.
Another approach if you already have the images in separate files is to do something like the following.
import numpy as np
import ray

ray.init()

@ray.remote
def do_object_detection(filename):
    # Load the file and process it.
    return 1

filenames = ['file1.png', 'file2.png', 'file3.png']

# Process all of the images.
result_ids = [do_object_detection.remote(filename) for filename in filenames]
results = ray.get(result_ids)

