некоторые контексты:
- < li>Я занимаюсь какой-то обработкой изображений.
- Исходные данные хранятся отдельно на диске.
- каждые данные будут существовать в программе как 2D-массив.< /li>
- допустим, у меня есть 100 необработанных данных/изображений
- создал класс, который принимает эти изображения в пакетном режиме( да)
class VolumeBatch:
def __init__(self, ...):
# shape equal to (batch_size, height, width)
# the height and width are the image size
self.container = some_3d_np_array
def self.__processeor(self, input_from_generator: tuple[int, Any]):
"""all the necessary image procssing goes here"""
idx, raw_2d_np_array = input_from_generator
...
results = do_the_image_processing(raw_2d_np_array)
return a_dictionary # {"idx": idx, "results": results}
def connect_internally(self, field_instance: FieldPredLine, max_workers: int = 4) -> None:
"""Process a single batch i.e. calculating connected component for a single batch"""
# `field_instance` in map(...) below
# is another interface with __getitem__
generator = list(zip(
list(self.batch_size),
map(lambda i: field_instance, list(self.batch_size)),
))
with ThreadPoolExecutor(max_workers=max_workers) as executor:
for result in tqdm( executor.map(self.__processeor, generator), ...):
idx = result.pop("idx")
# update the 3d_numpy_array that acts as the container
# relying on the `idx` to store the result in right position
self.container[idx, :, :] = result.pop( "results" )
- В основной программе я создал что-то вроде этого:
< pre class="lang-py Prettyprint-override">def process_several_batches(batch_size: int) -> list[VolumeBatch]:
# instantiation of `VolumeBatch` before
batches: list[VolumeBatch] = [
VolumeBatch(...) for _, range_xl in ranges.items()
]
# local_workers
Подробнее здесь: https://stackoverflow.com/questions/792 ... -correctly
Мобильная версия