valor-lite 0.37.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of valor-lite might be problematic. Click here for more details.
- valor_lite/LICENSE +21 -0
- valor_lite/__init__.py +0 -0
- valor_lite/cache/__init__.py +11 -0
- valor_lite/cache/compute.py +154 -0
- valor_lite/cache/ephemeral.py +302 -0
- valor_lite/cache/persistent.py +529 -0
- valor_lite/classification/__init__.py +14 -0
- valor_lite/classification/annotation.py +45 -0
- valor_lite/classification/computation.py +378 -0
- valor_lite/classification/evaluator.py +879 -0
- valor_lite/classification/loader.py +97 -0
- valor_lite/classification/metric.py +535 -0
- valor_lite/classification/numpy_compatibility.py +13 -0
- valor_lite/classification/shared.py +184 -0
- valor_lite/classification/utilities.py +314 -0
- valor_lite/exceptions.py +20 -0
- valor_lite/object_detection/__init__.py +17 -0
- valor_lite/object_detection/annotation.py +238 -0
- valor_lite/object_detection/computation.py +841 -0
- valor_lite/object_detection/evaluator.py +805 -0
- valor_lite/object_detection/loader.py +292 -0
- valor_lite/object_detection/metric.py +850 -0
- valor_lite/object_detection/shared.py +185 -0
- valor_lite/object_detection/utilities.py +396 -0
- valor_lite/schemas.py +11 -0
- valor_lite/semantic_segmentation/__init__.py +15 -0
- valor_lite/semantic_segmentation/annotation.py +123 -0
- valor_lite/semantic_segmentation/computation.py +165 -0
- valor_lite/semantic_segmentation/evaluator.py +414 -0
- valor_lite/semantic_segmentation/loader.py +205 -0
- valor_lite/semantic_segmentation/metric.py +275 -0
- valor_lite/semantic_segmentation/shared.py +149 -0
- valor_lite/semantic_segmentation/utilities.py +88 -0
- valor_lite/text_generation/__init__.py +15 -0
- valor_lite/text_generation/annotation.py +56 -0
- valor_lite/text_generation/computation.py +611 -0
- valor_lite/text_generation/llm/__init__.py +0 -0
- valor_lite/text_generation/llm/exceptions.py +14 -0
- valor_lite/text_generation/llm/generation.py +903 -0
- valor_lite/text_generation/llm/instructions.py +814 -0
- valor_lite/text_generation/llm/integrations.py +226 -0
- valor_lite/text_generation/llm/utilities.py +43 -0
- valor_lite/text_generation/llm/validators.py +68 -0
- valor_lite/text_generation/manager.py +697 -0
- valor_lite/text_generation/metric.py +381 -0
- valor_lite-0.37.1.dist-info/METADATA +174 -0
- valor_lite-0.37.1.dist-info/RECORD +49 -0
- valor_lite-0.37.1.dist-info/WHEEL +5 -0
- valor_lite-0.37.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pyarrow as pa
|
|
3
|
+
from numpy.typing import NDArray
|
|
4
|
+
from tqdm import tqdm
|
|
5
|
+
|
|
6
|
+
from valor_lite.cache import FileCacheWriter, MemoryCacheWriter
|
|
7
|
+
from valor_lite.object_detection.annotation import (
|
|
8
|
+
Bitmask,
|
|
9
|
+
BoundingBox,
|
|
10
|
+
Detection,
|
|
11
|
+
Polygon,
|
|
12
|
+
)
|
|
13
|
+
from valor_lite.object_detection.computation import (
|
|
14
|
+
EPSILON,
|
|
15
|
+
compute_bbox_iou,
|
|
16
|
+
compute_bitmask_iou,
|
|
17
|
+
compute_polygon_iou,
|
|
18
|
+
)
|
|
19
|
+
from valor_lite.object_detection.evaluator import Builder
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Loader(Builder):
|
|
23
|
+
def __init__(
|
|
24
|
+
self,
|
|
25
|
+
detailed_writer: MemoryCacheWriter | FileCacheWriter,
|
|
26
|
+
ranked_writer: MemoryCacheWriter | FileCacheWriter,
|
|
27
|
+
metadata_fields: list[tuple[str, str | pa.DataType]] | None = None,
|
|
28
|
+
):
|
|
29
|
+
super().__init__(
|
|
30
|
+
detailed_writer=detailed_writer,
|
|
31
|
+
ranked_writer=ranked_writer,
|
|
32
|
+
metadata_fields=metadata_fields,
|
|
33
|
+
)
|
|
34
|
+
|
|
35
|
+
# internal state
|
|
36
|
+
self._labels = {}
|
|
37
|
+
self._datum_count = 0
|
|
38
|
+
self._groundtruth_count = 0
|
|
39
|
+
self._prediction_count = 0
|
|
40
|
+
|
|
41
|
+
def _add_label(self, value: str) -> int:
|
|
42
|
+
"""Add a label to the index mapping."""
|
|
43
|
+
idx = self._labels.get(value, None)
|
|
44
|
+
if idx is None:
|
|
45
|
+
idx = len(self._labels)
|
|
46
|
+
self._labels[value] = idx
|
|
47
|
+
return idx
|
|
48
|
+
|
|
49
|
+
def _add_data(
|
|
50
|
+
self,
|
|
51
|
+
detections: list[Detection],
|
|
52
|
+
detection_ious: list[NDArray[np.float64]],
|
|
53
|
+
show_progress: bool = False,
|
|
54
|
+
):
|
|
55
|
+
"""Adds detections to the cache."""
|
|
56
|
+
disable_tqdm = not show_progress
|
|
57
|
+
for detection, ious in tqdm(
|
|
58
|
+
zip(detections, detection_ious), disable=disable_tqdm
|
|
59
|
+
):
|
|
60
|
+
# cache labels and annotation pairs
|
|
61
|
+
datum_idx = self._datum_count
|
|
62
|
+
datum_metadata = detection.metadata if detection.metadata else {}
|
|
63
|
+
pairs = []
|
|
64
|
+
if detection.groundtruths:
|
|
65
|
+
for gidx, gann in enumerate(detection.groundtruths):
|
|
66
|
+
gt_id = self._groundtruth_count + gidx
|
|
67
|
+
glabel = gann.labels[0]
|
|
68
|
+
glabel_idx = self._add_label(gann.labels[0])
|
|
69
|
+
gann_metadata = gann.metadata if gann.metadata else {}
|
|
70
|
+
if (ious[:, gidx] < EPSILON).all():
|
|
71
|
+
pairs.append(
|
|
72
|
+
{
|
|
73
|
+
# metadata
|
|
74
|
+
**datum_metadata,
|
|
75
|
+
**gann_metadata,
|
|
76
|
+
# datum
|
|
77
|
+
"datum_uid": detection.uid,
|
|
78
|
+
"datum_id": datum_idx,
|
|
79
|
+
# groundtruth
|
|
80
|
+
"gt_uid": gann.uid,
|
|
81
|
+
"gt_id": gt_id,
|
|
82
|
+
"gt_label": glabel,
|
|
83
|
+
"gt_label_id": glabel_idx,
|
|
84
|
+
# prediction
|
|
85
|
+
"pd_uid": None,
|
|
86
|
+
"pd_id": -1,
|
|
87
|
+
"pd_label": None,
|
|
88
|
+
"pd_label_id": -1,
|
|
89
|
+
"pd_score": -1,
|
|
90
|
+
# pair
|
|
91
|
+
"iou": 0.0,
|
|
92
|
+
}
|
|
93
|
+
)
|
|
94
|
+
for pidx, pann in enumerate(detection.predictions):
|
|
95
|
+
pann_id = self._prediction_count + pidx
|
|
96
|
+
pann_metadata = pann.metadata if pann.metadata else {}
|
|
97
|
+
if (ious[pidx, :] < EPSILON).all():
|
|
98
|
+
pairs.extend(
|
|
99
|
+
[
|
|
100
|
+
{
|
|
101
|
+
# metadata
|
|
102
|
+
**datum_metadata,
|
|
103
|
+
**pann_metadata,
|
|
104
|
+
# datum
|
|
105
|
+
"datum_uid": detection.uid,
|
|
106
|
+
"datum_id": datum_idx,
|
|
107
|
+
# groundtruth
|
|
108
|
+
"gt_uid": None,
|
|
109
|
+
"gt_id": -1,
|
|
110
|
+
"gt_label": None,
|
|
111
|
+
"gt_label_id": -1,
|
|
112
|
+
# prediction
|
|
113
|
+
"pd_uid": pann.uid,
|
|
114
|
+
"pd_id": pann_id,
|
|
115
|
+
"pd_label": plabel,
|
|
116
|
+
"pd_label_id": self._add_label(plabel),
|
|
117
|
+
"pd_score": float(pscore),
|
|
118
|
+
# pair
|
|
119
|
+
"iou": 0.0,
|
|
120
|
+
}
|
|
121
|
+
for plabel, pscore in zip(
|
|
122
|
+
pann.labels, pann.scores
|
|
123
|
+
)
|
|
124
|
+
]
|
|
125
|
+
)
|
|
126
|
+
if ious[pidx, gidx] >= EPSILON:
|
|
127
|
+
pairs.extend(
|
|
128
|
+
[
|
|
129
|
+
{
|
|
130
|
+
# metadata
|
|
131
|
+
**datum_metadata,
|
|
132
|
+
**gann_metadata,
|
|
133
|
+
**pann_metadata,
|
|
134
|
+
# datum
|
|
135
|
+
"datum_uid": detection.uid,
|
|
136
|
+
"datum_id": datum_idx,
|
|
137
|
+
# groundtruth
|
|
138
|
+
"gt_uid": gann.uid,
|
|
139
|
+
"gt_id": gt_id,
|
|
140
|
+
"gt_label": glabel,
|
|
141
|
+
"gt_label_id": self._add_label(glabel),
|
|
142
|
+
# prediction
|
|
143
|
+
"pd_uid": pann.uid,
|
|
144
|
+
"pd_id": pann_id,
|
|
145
|
+
"pd_label": plabel,
|
|
146
|
+
"pd_label_id": self._add_label(plabel),
|
|
147
|
+
"pd_score": float(pscore),
|
|
148
|
+
# pair
|
|
149
|
+
"iou": float(ious[pidx, gidx]),
|
|
150
|
+
}
|
|
151
|
+
for glabel in gann.labels
|
|
152
|
+
for plabel, pscore in zip(
|
|
153
|
+
pann.labels, pann.scores
|
|
154
|
+
)
|
|
155
|
+
]
|
|
156
|
+
)
|
|
157
|
+
elif detection.predictions:
|
|
158
|
+
for pidx, pann in enumerate(detection.predictions):
|
|
159
|
+
pann_id = self._prediction_count + pidx
|
|
160
|
+
pann_metadata = pann.metadata if pann.metadata else {}
|
|
161
|
+
pairs.extend(
|
|
162
|
+
[
|
|
163
|
+
{
|
|
164
|
+
# metadata
|
|
165
|
+
**datum_metadata,
|
|
166
|
+
**pann_metadata,
|
|
167
|
+
# datum
|
|
168
|
+
"datum_uid": detection.uid,
|
|
169
|
+
"datum_id": datum_idx,
|
|
170
|
+
# groundtruth
|
|
171
|
+
"gt_uid": None,
|
|
172
|
+
"gt_id": -1,
|
|
173
|
+
"gt_label": None,
|
|
174
|
+
"gt_label_id": -1,
|
|
175
|
+
# prediction
|
|
176
|
+
"pd_uid": pann.uid,
|
|
177
|
+
"pd_id": pann_id,
|
|
178
|
+
"pd_label": plabel,
|
|
179
|
+
"pd_label_id": self._add_label(plabel),
|
|
180
|
+
"pd_score": float(pscore),
|
|
181
|
+
# pair
|
|
182
|
+
"iou": 0.0,
|
|
183
|
+
}
|
|
184
|
+
for plabel, pscore in zip(pann.labels, pann.scores)
|
|
185
|
+
]
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
self._datum_count += 1
|
|
189
|
+
self._groundtruth_count += len(detection.groundtruths)
|
|
190
|
+
self._prediction_count += len(detection.predictions)
|
|
191
|
+
|
|
192
|
+
self._detailed_writer.write_rows(pairs)
|
|
193
|
+
|
|
194
|
+
def add_bounding_boxes(
|
|
195
|
+
self,
|
|
196
|
+
detections: list[Detection[BoundingBox]],
|
|
197
|
+
show_progress: bool = False,
|
|
198
|
+
):
|
|
199
|
+
"""
|
|
200
|
+
Adds bounding box detections to the cache.
|
|
201
|
+
|
|
202
|
+
Parameters
|
|
203
|
+
----------
|
|
204
|
+
detections : list[Detection]
|
|
205
|
+
A list of Detection objects.
|
|
206
|
+
show_progress : bool, default=False
|
|
207
|
+
Toggle for tqdm progress bar.
|
|
208
|
+
"""
|
|
209
|
+
ious = [
|
|
210
|
+
compute_bbox_iou(
|
|
211
|
+
np.array(
|
|
212
|
+
[
|
|
213
|
+
[gt.extrema, pd.extrema]
|
|
214
|
+
for pd in detection.predictions
|
|
215
|
+
for gt in detection.groundtruths
|
|
216
|
+
],
|
|
217
|
+
dtype=np.float64,
|
|
218
|
+
)
|
|
219
|
+
).reshape(len(detection.predictions), len(detection.groundtruths))
|
|
220
|
+
for detection in detections
|
|
221
|
+
]
|
|
222
|
+
return self._add_data(
|
|
223
|
+
detections=detections,
|
|
224
|
+
detection_ious=ious,
|
|
225
|
+
show_progress=show_progress,
|
|
226
|
+
)
|
|
227
|
+
|
|
228
|
+
def add_polygons(
|
|
229
|
+
self,
|
|
230
|
+
detections: list[Detection[Polygon]],
|
|
231
|
+
show_progress: bool = False,
|
|
232
|
+
):
|
|
233
|
+
"""
|
|
234
|
+
Adds polygon detections to the cache.
|
|
235
|
+
|
|
236
|
+
Parameters
|
|
237
|
+
----------
|
|
238
|
+
detections : list[Detection]
|
|
239
|
+
A list of Detection objects.
|
|
240
|
+
show_progress : bool, default=False
|
|
241
|
+
Toggle for tqdm progress bar.
|
|
242
|
+
"""
|
|
243
|
+
ious = [
|
|
244
|
+
compute_polygon_iou(
|
|
245
|
+
np.array(
|
|
246
|
+
[
|
|
247
|
+
[gt.shape, pd.shape]
|
|
248
|
+
for pd in detection.predictions
|
|
249
|
+
for gt in detection.groundtruths
|
|
250
|
+
]
|
|
251
|
+
)
|
|
252
|
+
).reshape(len(detection.predictions), len(detection.groundtruths))
|
|
253
|
+
for detection in detections
|
|
254
|
+
]
|
|
255
|
+
return self._add_data(
|
|
256
|
+
detections=detections,
|
|
257
|
+
detection_ious=ious,
|
|
258
|
+
show_progress=show_progress,
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
def add_bitmasks(
|
|
262
|
+
self,
|
|
263
|
+
detections: list[Detection[Bitmask]],
|
|
264
|
+
show_progress: bool = False,
|
|
265
|
+
):
|
|
266
|
+
"""
|
|
267
|
+
Adds bitmask detections to the cache.
|
|
268
|
+
|
|
269
|
+
Parameters
|
|
270
|
+
----------
|
|
271
|
+
detections : list[Detection]
|
|
272
|
+
A list of Detection objects.
|
|
273
|
+
show_progress : bool, default=False
|
|
274
|
+
Toggle for tqdm progress bar.
|
|
275
|
+
"""
|
|
276
|
+
ious = [
|
|
277
|
+
compute_bitmask_iou(
|
|
278
|
+
np.array(
|
|
279
|
+
[
|
|
280
|
+
[gt.mask, pd.mask]
|
|
281
|
+
for pd in detection.predictions
|
|
282
|
+
for gt in detection.groundtruths
|
|
283
|
+
]
|
|
284
|
+
)
|
|
285
|
+
).reshape(len(detection.predictions), len(detection.groundtruths))
|
|
286
|
+
for detection in detections
|
|
287
|
+
]
|
|
288
|
+
return self._add_data(
|
|
289
|
+
detections=detections,
|
|
290
|
+
detection_ious=ious,
|
|
291
|
+
show_progress=show_progress,
|
|
292
|
+
)
|