Skip to content

Commit

Permalink
labelox integration perforamce improvement and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
tyesayan committed Nov 27, 2024
1 parent 6589ffd commit 879f684
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 47 deletions.
18 changes: 18 additions & 0 deletions deeplake/integrations/labelbox/labelbox_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def __init__(
self.labelbox_feature_id_to_type_mapping = dict()
self.regsistered_actions = dict()
self.label_mappings = dict()
self.values_cache = dict()

Check warning on line 18 in deeplake/integrations/labelbox/labelbox_converter.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/labelbox_converter.py#L15-L18

Added lines #L15 - L18 were not covered by tests

self.project = project
self.project_id = project_id
Expand Down Expand Up @@ -44,6 +45,7 @@ def dataset_with_applied_annotations(self):
continue
print('parsing annotations for project with index: ', p_idx)
for lbl_idx, labels in enumerate(p["projects"][self.project_id]["labels"]):
self.values_cache = dict()
if "frames" not in labels["annotations"]:
continue
frames = labels["annotations"]["frames"]
Expand Down Expand Up @@ -72,6 +74,8 @@ def dataset_with_applied_annotations(self):
# iterate over segments and assign same value to all frames in the segment
self.parse_segments_(segments, frames, idx_offset)

Check warning on line 75 in deeplake/integrations/labelbox/labelbox_converter.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/labelbox_converter.py#L75

Added line #L75 was not covered by tests

self.apply_cached_values_(self.values_cache)

Check warning on line 77 in deeplake/integrations/labelbox/labelbox_converter.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/labelbox_converter.py#L77

Added line #L77 was not covered by tests

idx_offset += p["media_attributes"]["frame_count"]

Check warning on line 79 in deeplake/integrations/labelbox/labelbox_converter.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/labelbox_converter.py#L79

Added line #L79 was not covered by tests

return self.dataset

Check warning on line 81 in deeplake/integrations/labelbox/labelbox_converter.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/labelbox_converter.py#L81

Added line #L81 was not covered by tests
Expand Down Expand Up @@ -214,6 +218,20 @@ def parse_segments_(self, segments, frames, offset):
offset + i - 1, obj
)

def apply_cached_values_(self, cache):
print('applying cached values')
for tensor_name, row_map in cache.items():
print('applying cached values for tensor: ', tensor_name)
max_val = max(row_map.keys())
values = []
for i in tqdm.tqdm(range(max_val + 1)):
if i in row_map:
values.append(row_map[i])

Check warning on line 229 in deeplake/integrations/labelbox/labelbox_converter.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/labelbox_converter.py#L222-L229

Added lines #L222 - L229 were not covered by tests
else:
values.append(None)

Check warning on line 231 in deeplake/integrations/labelbox/labelbox_converter.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/labelbox_converter.py#L231

Added line #L231 was not covered by tests

self.dataset[tensor_name].extend(values)

Check warning on line 233 in deeplake/integrations/labelbox/labelbox_converter.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/labelbox_converter.py#L233

Added line #L233 was not covered by tests

def yield_projects_(self, project_j, ds):
raise NotImplementedError("fixed_project_order_ is not implemented")

Check warning on line 236 in deeplake/integrations/labelbox/labelbox_converter.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/labelbox_converter.py#L236

Added line #L236 was not covered by tests

Expand Down
95 changes: 48 additions & 47 deletions deeplake/integrations/labelbox/v3_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,12 @@ def bbox_converter_(obj, converter, tensor_name, context, generate_labels):
converter.register_feature_id_for_kind("tool", "bounding_box", obj, tensor_name)

Check warning on line 21 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L21

Added line #L21 was not covered by tests

def bbox_converter(row, obj):
vals = []
try:
vals = ds[tensor_name][row].numpy(aslist=True).tolist()
except (KeyError, IndexError):
pass
if tensor_name not in converter.values_cache:
converter.values_cache[tensor_name] = dict()
if row not in converter.values_cache[tensor_name]:
converter.values_cache[tensor_name][row] = []

Check warning on line 27 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L23-L27

Added lines #L23 - L27 were not covered by tests

vals.append(
converter.values_cache[tensor_name][row].append(

Check warning on line 29 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L29

Added line #L29 was not covered by tests
[
int(v)
for v in [
Expand All @@ -38,8 +37,6 @@ def bbox_converter(row, obj):
]
]
)
ds[tensor_name][row] = vals

converter.regsistered_actions[obj.feature_schema_id] = bbox_converter

Check warning on line 40 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L40

Added line #L40 was not covered by tests


Expand Down Expand Up @@ -68,7 +65,11 @@ def radio_converter_(obj, converter, tensor_name, context, generate_labels):
)

def radio_converter(row, o):
ds[tensor_name][row] = converter.label_mappings[tensor_name][o["value"]]
if tensor_name not in converter.values_cache:
converter.values_cache[tensor_name] = dict()
if row not in converter.values_cache[tensor_name]:
converter.values_cache[tensor_name][row] = []
converter.values_cache[tensor_name][row] = [converter.label_mappings[tensor_name][o["value"]]]

Check warning on line 72 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L67-L72

Added lines #L67 - L72 were not covered by tests

for option in obj.options:
converter.regsistered_actions[option.feature_schema_id] = radio_converter

Check warning on line 75 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L74-L75

Added lines #L74 - L75 were not covered by tests
Expand Down Expand Up @@ -104,14 +105,12 @@ def checkbox_converter_(obj, converter, tensor_name, context, generate_labels):
)

def checkbox_converter(row, obj):
vals = []
try:
vals = ds[tensor_name][row].numpy(aslist=True).tolist()
except (KeyError, IndexError):
pass
vals.append(converter.label_mappings[tensor_name][obj["value"]])
if tensor_name not in converter.values_cache:
converter.values_cache[tensor_name] = dict()
if row not in converter.values_cache[tensor_name]:
converter.values_cache[tensor_name][row] = []

Check warning on line 111 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L107-L111

Added lines #L107 - L111 were not covered by tests

ds[tensor_name][row] = vals
converter.values_cache[tensor_name][row].append(converter.label_mappings[tensor_name][obj["value"]])

Check warning on line 113 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L113

Added line #L113 was not covered by tests

for option in obj.options:
converter.regsistered_actions[option.feature_schema_id] = checkbox_converter

Check warning on line 116 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L115-L116

Added lines #L115 - L116 were not covered by tests
Expand All @@ -136,13 +135,12 @@ def point_converter_(obj, converter, tensor_name, context, generate_labels):
print("point converter does not support generating labels")

Check warning on line 135 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L134-L135

Added lines #L134 - L135 were not covered by tests

def point_converter(row, obj):
vals = []
try:
vals = ds[tensor_name][row].numpy(aslist=True).tolist()
except (KeyError, IndexError):
pass
vals.append([int(obj["point"]["x"]), int(obj["point"]["y"])])
ds[tensor_name][row] = vals
if tensor_name not in converter.values_cache:
converter.values_cache[tensor_name] = dict()
if row not in converter.values_cache[tensor_name]:
converter.values_cache[tensor_name][row] = []

Check warning on line 141 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L137-L141

Added lines #L137 - L141 were not covered by tests

converter.values_cache[tensor_name][row].append([int(obj["point"]["x"]), int(obj["point"]["y"])])

Check warning on line 143 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L143

Added line #L143 was not covered by tests

converter.regsistered_actions[obj.feature_schema_id] = point_converter

Check warning on line 145 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L145

Added line #L145 was not covered by tests

Expand All @@ -160,13 +158,12 @@ def line_converter_(obj, converter, tensor_name, context, generate_labels):
print("line converter does not support generating labels")

Check warning on line 158 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L157-L158

Added lines #L157 - L158 were not covered by tests

def polygon_converter(row, obj):
vals = []
try:
vals = ds[tensor_name][row].numpy(aslist=True)
except (KeyError, IndexError):
pass
vals.append([[int(l["x"]), int(l["y"])] for l in obj["line"]])
ds[tensor_name][row] = vals
if tensor_name not in converter.values_cache:
converter.values_cache[tensor_name] = dict()
if row not in converter.values_cache[tensor_name]:
converter.values_cache[tensor_name][row] = []

Check warning on line 164 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L160-L164

Added lines #L160 - L164 were not covered by tests

converter.values_cache[tensor_name][row].append([[int(l["x"]), int(l["y"])] for l in obj["line"]])

Check warning on line 166 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L166

Added line #L166 was not covered by tests

converter.regsistered_actions[obj.feature_schema_id] = polygon_converter

Check warning on line 168 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L168

Added line #L168 was not covered by tests

Expand Down Expand Up @@ -230,22 +227,22 @@ def mask_converter(row, obj):
)
ds[f"{tensor_name}_labels"][row] = val

Check warning on line 228 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L228

Added line #L228 was not covered by tests

mask = np.array(Image.open(response)).astype(np.bool_)
mask = mask[..., np.newaxis]
try:
if generate_labels:
arr = ds[tensor_name][row].numpy()
labels = ds[f"{tensor_name}_labels"].info['class_names']
if labels != arr.shape[-1]:
val = np.concatenate([ds[tensor_name][row].numpy(), np.zeros_like(mask)], axis=-1)
idx = labels.index(tool_name)
val[:,:,idx] = np.logical_or(val[:,:,idx], mask[:,:,0])
else:
val = np.logical_or(ds[tensor_name][row].numpy(), mask)
except (KeyError, IndexError):
val = mask

ds[tensor_name][row] = val
mask = np.array(Image.open(response)).astype(np.bool_)
mask = mask[..., np.newaxis]
try:
if generate_labels:
arr = ds[tensor_name][row].numpy()
labels = ds[f"{tensor_name}_labels"].info['class_names']
if len(labels) != arr.shape[-1]:
val = np.concatenate([ds[tensor_name][row].numpy(), np.zeros_like(mask)], axis=-1)
idx = labels.index(tool_name)
val[:,:,idx] = np.logical_or(val[:,:,idx], mask[:,:,0])

Check warning on line 239 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L230-L239

Added lines #L230 - L239 were not covered by tests
else:
val = np.logical_or(ds[tensor_name][row].numpy(), mask)
except (KeyError, IndexError):
val = mask

Check warning on line 243 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L241-L243

Added lines #L241 - L243 were not covered by tests

ds[tensor_name][row] = val
except Exception as e:
print(f"Error downloading mask: {e}")

Check warning on line 247 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L245-L247

Added lines #L245 - L247 were not covered by tests

Expand All @@ -265,6 +262,10 @@ def text_converter_(obj, converter, tensor_name, context, generate_labels):
print("text converter does not support generating labels")

Check warning on line 262 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L261-L262

Added lines #L261 - L262 were not covered by tests

def text_converter(row, obj):
ds[tensor_name][row] = obj["text_answer"]["content"]
if tensor_name not in converter.values_cache:
converter.values_cache[tensor_name] = dict()
if row not in converter.values_cache[tensor_name]:
converter.values_cache[tensor_name][row] = []
converter.values_cache[tensor_name][row] = obj["text_answer"]["content"]

Check warning on line 269 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L264-L269

Added lines #L264 - L269 were not covered by tests

converter.regsistered_actions[obj.feature_schema_id] = text_converter

Check warning on line 271 in deeplake/integrations/labelbox/v3_converters.py

View check run for this annotation

Codecov / codecov/patch

deeplake/integrations/labelbox/v3_converters.py#L271

Added line #L271 was not covered by tests

0 comments on commit 879f684

Please sign in to comment.