Skip to content

Commit 08a6843

Browse files
author
Matt Sokoloff
committed
option to not use process pool
1 parent 5900bd1 commit 08a6843

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

labelbox/data/serialization/coco/converter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def serialize_instances(labels: LabelCollection,
5151
image_root: Where to save images to
5252
ignore_existing_data: Whether or not to raise an exception if images already exist.
5353
This exists only to support detectons panoptic fpn model which requires two mscoco payloads for the same images.
54-
max_workers : Number of workers to process dataset with
54+
max_workers : Number of workers to process dataset with. A value of 0 will process all data in the main process
5555
Returns:
5656
A dictionary containing labels in the coco object format.
5757
"""
@@ -66,7 +66,7 @@ def serialize_panoptic(labels: LabelCollection,
6666
mask_root: Union[Path, str],
6767
all_stuff: bool = False,
6868
ignore_existing_data=False,
69-
max_workers=8) -> Dict[str, Any]:
69+
max_workers: int = 8) -> Dict[str, Any]:
7070
"""
7171
Convert a Labelbox LabelCollection into an mscoco dataset.
7272
This function will only convert masks, polygons, and rectangles.
@@ -81,7 +81,7 @@ def serialize_panoptic(labels: LabelCollection,
8181
To convert them to stuff class set `all_stuff=True`.
8282
ignore_existing_data: Whether or not to raise an exception if images already exist.
8383
This exists only to support detectons panoptic fpn model which requires two mscoco payloads for the same images.
84-
max_workers : Number of workers to process dataset with
84+
max_workers : Number of workers to process dataset with. A value of 0 will process all data in the main process.
8585
Returns:
8686
A dictionary containing labels in the coco panoptic format.
8787
"""

labelbox/data/serialization/coco/instance_dataset.py

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,25 @@ def from_common(cls,
135135
futures = []
136136
coco_categories = {}
137137

138-
with ProcessPoolExecutor(max_workers=max_workers) as exc:
139-
futures = [
140-
exc.submit(process_label, label, idx, image_root)
138+
if max_workers:
139+
with ProcessPoolExecutor(max_workers=max_workers) as exc:
140+
futures = [
141+
exc.submit(process_label, label, idx, image_root)
142+
for idx, label in enumerate(labels)
143+
]
144+
results = [
145+
future.result() for future in tqdm(as_completed(futures))
146+
]
147+
else:
148+
results = [
149+
process_label(label, idx, image_root)
141150
for idx, label in enumerate(labels)
142151
]
143-
for future in tqdm(as_completed(futures)):
144-
image, annotations, categories = future.result()
145-
images.append(image)
146-
all_coco_annotations.extend(annotations)
147-
coco_categories.update(categories)
152+
153+
for result in results:
154+
images.append(result[0])
155+
all_coco_annotations.extend(result[1])
156+
coco_categories.update(result[2])
148157

149158
category_mapping = {
150159
category_id: idx + 1

labelbox/data/serialization/coco/panoptic_dataset.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,27 @@ def from_common(cls,
113113
coco_categories = {}
114114
coco_things = {}
115115
images = []
116-
with ProcessPoolExecutor(max_workers=max_workers) as exc:
117-
futures = [
118-
exc.submit(process_label, label, idx, image_root, mask_root,
119-
all_stuff) for idx, label in enumerate(labels)
116+
117+
if max_workers:
118+
with ProcessPoolExecutor(max_workers=max_workers) as exc:
119+
futures = [
120+
exc.submit(process_label, label, idx, image_root, mask_root,
121+
all_stuff) for idx, label in enumerate(labels)
122+
]
123+
results = [
124+
future.result() for future in tqdm(as_completed(futures))
125+
]
126+
else:
127+
results = [
128+
process_label(label, idx, image_root, mask_root, all_stuff)
129+
for idx, label in enumerate(labels)
120130
]
121-
for future in tqdm(as_completed(futures)):
122-
image, annotation, categories, things = future.result()
123-
images.append(image)
124-
all_coco_annotations.append(annotation)
125-
coco_categories.update(categories)
126-
coco_things.update(things)
131+
132+
for result in results:
133+
images.append(result[0])
134+
all_coco_annotations.extend(result[1])
135+
coco_categories.update(result[2])
136+
coco_things.update(result[3])
127137

128138
category_mapping = {
129139
category_id: idx + 1

0 commit comments

Comments
 (0)