Skip to content

Commit 9c57cc6

Browse files
author
Matt Sokoloff
committed
format
1 parent 4930d8a commit 9c57cc6

File tree

4 files changed

+47
-24
lines changed

4 files changed

+47
-24
lines changed

labelbox/data/annotation_types/label.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def classification_annotations(self) -> List[ClassificationAnnotation]:
2929
return self.get_annotations_by_type(ClassificationAnnotation)
3030

3131
def get_annotations_by_type(self, annotation_type):
32-
return [annot for annot in self.annotations if isinstance(annotation_type)]
32+
return [
33+
annot for annot in self.annotations if isinstance(annotation_type)
34+
]
3335

3436
def frame_annotations(
3537
self

labelbox/data/metrics/iou.py

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
from labelbox.data import annotation_types
1313

1414

15-
def mask_miou(predictions: List[Mask], ground_truths: List[Mask], resize_height = None, resize_width = None) -> float:
15+
def mask_miou(predictions: List[Mask],
16+
ground_truths: List[Mask],
17+
resize_height=None,
18+
resize_width=None) -> float:
1619
"""
1720
Creates prediction and label binary mask for all features with the same feature schema id.
1821
Masks are flattened and treated as one class.
@@ -24,12 +27,22 @@ def mask_miou(predictions: List[Mask], ground_truths: List[Mask], resize_height
2427
Returns:
2528
float indicating iou score
2629
"""
27-
prediction_np = np.max([pred.raster(binary = True, height = resize_height, width = resize_width ) for pred in predictions], axis = 0)
28-
ground_truth_np = np.max([ground_truth.raster(binary = True, height = resize_height, width = resize_width ) for ground_truth in ground_truths], axis = 0)
30+
prediction_np = np.max([
31+
pred.raster(binary=True, height=resize_height, width=resize_width)
32+
for pred in predictions
33+
],
34+
axis=0)
35+
ground_truth_np = np.max([
36+
ground_truth.raster(
37+
binary=True, height=resize_height, width=resize_width)
38+
for ground_truth in ground_truths
39+
],
40+
axis=0)
2941
if prediction_np.shape != ground_truth_np.shape:
30-
raise ValueError("Prediction and mask must have the same shape."
31-
f" Found {prediction_np.shape}/{ground_truth_np.shape}."
32-
" Add resize params to fix this.")
42+
raise ValueError(
43+
"Prediction and mask must have the same shape."
44+
f" Found {prediction_np.shape}/{ground_truth_np.shape}."
45+
" Add resize params to fix this.")
3346
return _mask_iou(ground_truth_np, prediction_np)
3447

3548

@@ -59,9 +72,12 @@ def classification_miou(predictions: List[ClassificationAnnotation],
5972
if isinstance(prediction.value, Text):
6073
return float(prediction.value.answer == label.value.answer)
6174
elif isinstance(prediction.value, Radio):
62-
return float(prediction.value.answer.schema_id == label.value.answer.schema_id)
75+
return float(
76+
prediction.value.answer.schema_id == label.value.answer.schema_id)
6377
elif isinstance(prediction.value, Checklist):
64-
schema_ids_pred = {answer.schema_id for answer in prediction.value.answer}
78+
schema_ids_pred = {
79+
answer.schema_id for answer in prediction.value.answer
80+
}
6581
schema_ids_label = {answer.schema_id for answer in label.value.answer}
6682
return float(
6783
len(schema_ids_label & schema_ids_pred) /
@@ -135,8 +151,10 @@ def vector_miou(predictions: List[Geometry], labels: List[Geometry],
135151
return np.mean(solution_agreements)
136152

137153

138-
def feature_miou(predictions: List[Union[ObjectAnnotation, ClassificationAnnotation]],
139-
labels: List[Union[ObjectAnnotation, ClassificationAnnotation]],
154+
def feature_miou(predictions: List[Union[ObjectAnnotation,
155+
ClassificationAnnotation]],
156+
labels: List[Union[ObjectAnnotation,
157+
ClassificationAnnotation]],
140158
include_subclasses=True) -> Optional[float]:
141159
"""
142160
Computes iou score for all features with the same feature schema id.
@@ -166,7 +184,8 @@ def feature_miou(predictions: List[Union[ObjectAnnotation, ClassificationAnnotat
166184
elif isinstance(predictions[0].value, ClassificationAnnotation):
167185
return classification_miou(predictions, labels)
168186
else:
169-
raise ValueError(f"Unexpected annotation found. Found {type(predictions[0])}")
187+
raise ValueError(
188+
f"Unexpected annotation found. Found {type(predictions[0])}")
170189

171190

172191
def _create_schema_lookup(annotations: List[BaseAnnotation]):
@@ -175,10 +194,11 @@ def _create_schema_lookup(annotations: List[BaseAnnotation]):
175194
grouped_annotations[annotation.schema_id] = annotation
176195
return grouped_annotations
177196

197+
178198
def data_row_miou(ground_truth: Label,
179-
predictions: Label,
180-
include_classifications=True,
181-
include_subclasses=True) -> float:
199+
predictions: Label,
200+
include_classifications=True,
201+
include_subclasses=True) -> float:
182202
"""
183203
# At this point all object should have schema ids.
184204
@@ -191,9 +211,12 @@ def data_row_miou(ground_truth: Label,
191211
float indicating the iou score for this data row.
192212
"""
193213
annotation_types = None if include_classifications else Geometry
194-
prediction_annotations = predictions.get_annotations_by_attr(attr = "name", annotation_types = annotation_types)
195-
ground_truth_annotations = ground_truth.get_annotations_by_attr(attr = "name", annotation_types = annotation_types)
196-
feature_schemas = set(prediction_annotations.keys()).union(set(ground_truth_annotations.keys()))
214+
prediction_annotations = predictions.get_annotations_by_attr(
215+
attr="name", annotation_types=annotation_types)
216+
ground_truth_annotations = ground_truth.get_annotations_by_attr(
217+
attr="name", annotation_types=annotation_types)
218+
feature_schemas = set(prediction_annotations.keys()).union(
219+
set(ground_truth_annotations.keys()))
197220
ious = [
198221
feature_miou(prediction_annotations[feature_schema],
199222
ground_truth_annotations[feature_schema],
@@ -206,13 +229,13 @@ def data_row_miou(ground_truth: Label,
206229
return np.mean(ious)
207230

208231

209-
def _get_vector_pairs(predictions: List[Geometry], ground_truths: List[Geometry]):
232+
def _get_vector_pairs(predictions: List[Geometry],
233+
ground_truths: List[Geometry]):
210234
"""
211235
# Get iou score for all pairs of labels and predictions
212236
"""
213237
return [(prediction, ground_truth,
214-
_polygon_iou(prediction.shapely,
215-
ground_truth.shapely))
238+
_polygon_iou(prediction.shapely, ground_truth.shapely))
216239
for prediction, ground_truth in product(predictions, ground_truths)]
217240

218241

@@ -238,4 +261,3 @@ def _instance_urls_to_binary_mask(urls: List[str],
238261
masks = _remove_opacity_channel([url_to_numpy(url) for url in urls])
239262
return np.sum([np.all(mask == color, axis=-1) for mask in masks],
240263
axis=0) > 0
241-

labelbox/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22

3+
34
def _convert(s, sep, title):
45
components = re.findall(r"[A-Z][a-z0-9]*|[a-z][a-z0-9]*", s)
56
components = list(map(str.lower, filter(None, components)))

tests/integration/test_ontology.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,5 +240,3 @@ def test_ontology_asdict(project) -> None:
240240
def test_from_project_ontology(client, project) -> None:
241241
o = OntologyBuilder.from_project(project)
242242
assert o.asdict() == project.ontology().normalized
243-
244-

0 commit comments

Comments
 (0)