Skip to content

Commit e808e04

Browse files
caglarpirmeta-codesync[bot]
authored andcommitted
Feature extraction: optional dedicated reader threads and a cap on queued images
Summary: `run_features_processing` runs its `read_processes` image readers and its extractors in one pool of `processes` threads. Until every image is read, the readers hold extractor slots: the Mapillary initial worker (12 processes, 4 readers) extracts with 8 threads while reading. Its 4 readers, each downloading an image and its segmentation masks one at a time, cannot keep 12 extractors fed. The queue of read images is sized from free memory (up to 200), so when extraction is the slow side (360° sequences) it holds up to ~20 GB of decoded images. Two config options, both off by default (current behaviour): - `features_dedicated_readers`: readers get threads of their own, all `processes` extractors run while images are read, and `read_processes` is used as given. The memory-derived queue size is reduced by `read_processes`, since each reader holds one decoded image while it waits to queue it. - `features_queue_size`: when above 0, caps the number of read images waiting for an extractor. Every image is still extracted by the same `detect` call on the same pixels; only which thread runs it, and when, changes. ___ landed-with-radar-review Differential Revision: D134681688 fbshipit-source-id: 8cd1b0877c7daa76d71fd5eaf310f20b37607cd2
1 parent 46453fe commit e808e04

3 files changed

Lines changed: 100 additions & 2 deletions

File tree

‎opensfm/config.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,13 @@ class OpenSfMConfig:
391391
processes: int = 1
392392
# When processes > 1, number of threads used for reading images
393393
read_processes: int = 4
394+
# When processes > 1, give the image readers threads of their own, so that
395+
# all `processes` extractors run while images are being read, and use
396+
# `read_processes` as given.
397+
features_dedicated_readers: bool = False
398+
# Maximum number of read images waiting for feature extraction. 0 derives it
399+
# from the memory available.
400+
features_queue_size: int = 0
394401

395402
##################################
396403
# Params for submodel split and merge

‎opensfm/features_processing.py‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ def run_features_processing(data: DataSetBase, images: List[str], force: bool) -
7171
processes = min(max(1, int(expected_mb / processing_size)), processes)
7272
else:
7373
expected_images = default_queue_size
74+
if data.config.get("features_dedicated_readers", False):
75+
# Each dedicated reader holds one more decoded image while it waits to
76+
# queue it: count those in the queue's share of the memory.
77+
expected_images = max(1, expected_images - data.config["read_processes"])
78+
queue_size = data.config.get("features_queue_size", 0)
79+
if queue_size > 0:
80+
expected_images = min(expected_images, queue_size)
7481
logger.info(
7582
f"Expecting to queue at most {expected_images} images while parallel processing of {processes} images."
7683
)
@@ -87,7 +94,8 @@ def run_features_processing(data: DataSetBase, images: List[str], force: bool) -
8794
else:
8895
counter = Counter()
8996
read_processes = data.config["read_processes"]
90-
if 1.5 * read_processes >= processes:
97+
dedicated_readers = data.config.get("features_dedicated_readers", False)
98+
if not dedicated_readers and 1.5 * read_processes >= processes:
9199
read_processes = max(1, processes // 2)
92100

93101
chunk_size = math.ceil(len(images) / read_processes)
@@ -105,7 +113,11 @@ def run_features_processing(data: DataSetBase, images: List[str], force: bool) -
105113
)
106114
for _ in range(processes):
107115
arguments.append(("consumer", process_queue))
108-
parallel_map(process, arguments, processes, 1)
116+
# Without dedicated readers, the readers take `read_processes` of the
117+
# `processes` threads, and the remaining extractors only start once
118+
# every image has been read.
119+
threads = processes + read_processes if dedicated_readers else processes
120+
parallel_map(process, arguments, threads, 1)
109121

110122

111123
def average_image_size(data: DataSetBase) -> float:
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from typing import Any
2+
from unittest import mock
3+
4+
import numpy as np
5+
from numpy.typing import NDArray
6+
from opensfm import features_processing
7+
from opensfm.actions import extract_metadata
8+
from opensfm.test import data_generation
9+
10+
11+
def _extract_features(
12+
tmpdir: Any, **config: Any
13+
) -> dict[str, tuple[NDArray, NDArray | None, NDArray]]:
14+
data = data_generation.create_berlin_test_folder(tmpdir)
15+
data.config.update({"processes": 2, "read_processes": 1, **config})
16+
extract_metadata.run_dataset(data)
17+
features_processing.run_features_processing(data, data.images(), True)
18+
extracted = {}
19+
for image in data.images():
20+
features = data.load_features(image)
21+
assert features is not None
22+
extracted[image] = (features.points, features.descriptors, features.colors)
23+
return extracted
24+
25+
26+
def test_dedicated_readers_extract_the_same_features(tmpdir: Any) -> None:
27+
shared = _extract_features(tmpdir.mkdir("shared"))
28+
dedicated = _extract_features(
29+
tmpdir.mkdir("dedicated"),
30+
features_dedicated_readers=True,
31+
read_processes=2,
32+
features_queue_size=1,
33+
)
34+
35+
assert shared.keys() == dedicated.keys()
36+
for image, arrays in shared.items():
37+
for expected, actual in zip(arrays, dedicated[image]):
38+
np.testing.assert_array_equal(expected, actual)
39+
40+
41+
def _schedule(tmpdir: Any, **config: Any) -> tuple[int, int, int, int]:
42+
"""The (threads, readers, extractors, queue size) that
43+
run_features_processing asks of parallel_map for the Berlin images."""
44+
data = data_generation.create_berlin_test_folder(tmpdir)
45+
data.config.update(
46+
{"processes": 4, "read_processes": 3, "mem_ceiling": 2**16, **config}
47+
)
48+
extract_metadata.run_dataset(data)
49+
with mock.patch.object(features_processing, "parallel_map") as parallel_map:
50+
features_processing.run_features_processing(data, data.images(), True)
51+
_, arguments, threads, _ = parallel_map.call_args.args
52+
readers = [args for kind, args in arguments if kind == "producer"]
53+
extractors = [args for kind, args in arguments if kind == "consumer"]
54+
return threads, len(readers), len(extractors), extractors[0].maxsize
55+
56+
57+
def test_readers_take_extractor_threads_by_default(tmpdir: Any) -> None:
58+
threads, readers, extractors, queue_size = _schedule(tmpdir)
59+
60+
# 3 readers would take too many of the 4 threads: they are halved.
61+
assert (threads, readers, extractors) == (4, 2, 4)
62+
assert queue_size > 2
63+
64+
65+
def test_dedicated_readers_run_beside_every_extractor(tmpdir: Any) -> None:
66+
*_, shared_queue_size = _schedule(tmpdir.mkdir("shared"))
67+
threads, readers, extractors, queue_size = _schedule(
68+
tmpdir.mkdir("dedicated"), features_dedicated_readers=True
69+
)
70+
71+
assert (threads, readers, extractors) == (7, 3, 4)
72+
# The images the 3 readers hold come out of the queue's memory share.
73+
assert queue_size == shared_queue_size - 3
74+
75+
76+
def test_queue_size_caps_the_queue(tmpdir: Any) -> None:
77+
*_, queue_size = _schedule(tmpdir, features_queue_size=2)
78+
79+
assert queue_size == 2

0 commit comments

Comments
 (0)