【问题标题】:Efficiently slice and read images using multiprocessing使用多处理有效地切片和读取图像
【发布时间】:2019-03-11 06:48:59
【问题描述】:

我有一张较大的卫星图像,并希望对其进行对象检测模型推理。目前,我对大图像进行切片,保存图块,然后读取它们以让我的模型输出检测结果(框和掩码)。我知道这是一种低效的做事方式,因为一旦读取了图像切片/图块,就不再需要它,但我目前将其保存到磁盘。

有没有更有效的方法来完成这个过程?也许通过多处理或光线库?

【问题讨论】:

  • 考虑多处理+redis(存储检测)
  • 您能详细说明一下吗?我将检测结果分别存储在 geojson 文件中。将图像切片/切片保存到磁盘不是速度瓶颈吗?
  • 如果是我,我会将图像切片到内存并使用多处理来提高检测速度,然后使用 redis 存储结果。也许你应该先显示你的代码......

标签: python multiprocessing python-multiprocessing ray


【解决方案1】:

正如您所提到的,Ray 非常适合,因为它使用共享内存并且能够在一台或多台机器上运行相同的代码。

类似以下结构的东西可以工作。

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)

请注意,执行do_object_detection 任务的工作人员不会创建自己的图像副本。相反,他们可以访问共享内存中的图像副本。

如果您已经将图像保存在单独的文件中,另一种方法是执行以下操作。

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)

【讨论】:

  • 感谢您的回复。所以你建议在远程功能中完成图像切片?回顾一下,我当前的管道是获取大图像,切片并分别保存切片(例如 01_01.png、01_02.png ....),然后读取切片进行推理。之后,图像切片将被删除,因为它们不再需要。
  • 我明白了,在这种情况下,如果您已经有不同的文件,您可以定义一个远程函数,该函数采用单个图像(或文件名)并对其进行处理。我将更新我的答案以包含此版本。
猜你喜欢
  • 1970-01-01
  • 2019-01-24
  • 1970-01-01
  • 2015-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多