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,238 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from typing import Any, Generic, TypeVar
|
|
3
|
+
|
|
4
|
+
import numpy as np
|
|
5
|
+
from numpy.typing import NDArray
|
|
6
|
+
from shapely.geometry import Polygon as ShapelyPolygon
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@dataclass
|
|
10
|
+
class BoundingBox:
|
|
11
|
+
"""
|
|
12
|
+
Represents a bounding box with associated labels and optional scores.
|
|
13
|
+
|
|
14
|
+
Parameters
|
|
15
|
+
----------
|
|
16
|
+
uid : str
|
|
17
|
+
A unique identifier.
|
|
18
|
+
xmin : float
|
|
19
|
+
The minimum x-coordinate of the bounding box.
|
|
20
|
+
xmax : float
|
|
21
|
+
The maximum x-coordinate of the bounding box.
|
|
22
|
+
ymin : float
|
|
23
|
+
The minimum y-coordinate of the bounding box.
|
|
24
|
+
ymax : float
|
|
25
|
+
The maximum y-coordinate of the bounding box.
|
|
26
|
+
labels : list of str
|
|
27
|
+
List of labels associated with the bounding box.
|
|
28
|
+
scores : list of float, optional
|
|
29
|
+
Confidence scores corresponding to each label. Defaults to an empty list.
|
|
30
|
+
metadata : dict[str, Any], optional
|
|
31
|
+
A dictionary containing any metadata to be used within filtering operations.
|
|
32
|
+
|
|
33
|
+
Examples
|
|
34
|
+
--------
|
|
35
|
+
Ground Truth Example:
|
|
36
|
+
|
|
37
|
+
>>> bbox = BoundingBox(uid="xyz", xmin=10.0, xmax=50.0, ymin=20.0, ymax=60.0, labels=['cat'])
|
|
38
|
+
|
|
39
|
+
Prediction Example:
|
|
40
|
+
|
|
41
|
+
>>> bbox = BoundingBox(
|
|
42
|
+
... uid="abc",
|
|
43
|
+
... xmin=10.0, xmax=50.0, ymin=20.0, ymax=60.0,
|
|
44
|
+
... labels=['cat', 'dog'], scores=[0.9, 0.1]
|
|
45
|
+
... )
|
|
46
|
+
"""
|
|
47
|
+
|
|
48
|
+
uid: str
|
|
49
|
+
xmin: float
|
|
50
|
+
xmax: float
|
|
51
|
+
ymin: float
|
|
52
|
+
ymax: float
|
|
53
|
+
labels: list[str]
|
|
54
|
+
scores: list[float] = field(default_factory=list)
|
|
55
|
+
metadata: dict[str, Any] | None = None
|
|
56
|
+
|
|
57
|
+
def __post_init__(self):
|
|
58
|
+
if len(self.scores) == 0 and len(self.labels) != 1:
|
|
59
|
+
raise ValueError(
|
|
60
|
+
"Ground truths must be defined with no scores and a single label. If you meant to define a prediction, then please include one score for every label provided."
|
|
61
|
+
)
|
|
62
|
+
if len(self.scores) > 0 and len(self.labels) != len(self.scores):
|
|
63
|
+
raise ValueError(
|
|
64
|
+
"If scores are defined, there must be a 1:1 pairing with labels."
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def extrema(self) -> tuple[float, float, float, float]:
|
|
69
|
+
"""
|
|
70
|
+
Returns the annotation's data representation.
|
|
71
|
+
|
|
72
|
+
Returns
|
|
73
|
+
-------
|
|
74
|
+
tuple[float, float, float, float]
|
|
75
|
+
A tuple in the form (xmin, xmax, ymin, ymax).
|
|
76
|
+
"""
|
|
77
|
+
return (self.xmin, self.xmax, self.ymin, self.ymax)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
@dataclass
|
|
81
|
+
class Polygon:
|
|
82
|
+
"""
|
|
83
|
+
Represents a polygon shape with associated labels and optional scores.
|
|
84
|
+
|
|
85
|
+
Parameters
|
|
86
|
+
----------
|
|
87
|
+
uid : str
|
|
88
|
+
A unique identifier.
|
|
89
|
+
shape : shapely.geometry.Polygon
|
|
90
|
+
A Shapely polygon object representing the shape.
|
|
91
|
+
labels : list of str
|
|
92
|
+
List of labels associated with the polygon.
|
|
93
|
+
scores : list of float, optional
|
|
94
|
+
Confidence scores corresponding to each label. Defaults to an empty list.
|
|
95
|
+
metadata : dict[str, Any], optional
|
|
96
|
+
A dictionary containing any metadata to be used within filtering operations.
|
|
97
|
+
|
|
98
|
+
Examples
|
|
99
|
+
--------
|
|
100
|
+
Ground Truth Example:
|
|
101
|
+
|
|
102
|
+
>>> from shapely.geometry import Polygon as ShapelyPolygon
|
|
103
|
+
>>> shape = ShapelyPolygon([(0, 0), (1, 0), (1, 1), (0, 1)])
|
|
104
|
+
>>> polygon = Polygon(uid="xyz", shape=shape, labels=['building'])
|
|
105
|
+
|
|
106
|
+
Prediction Example:
|
|
107
|
+
|
|
108
|
+
>>> polygon = Polygon(
|
|
109
|
+
... uid="abc", shape=shape, labels=['building'], scores=[0.95]
|
|
110
|
+
... )
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
uid: str
|
|
114
|
+
shape: ShapelyPolygon
|
|
115
|
+
labels: list[str]
|
|
116
|
+
scores: list[float] = field(default_factory=list)
|
|
117
|
+
metadata: dict[str, Any] | None = None
|
|
118
|
+
|
|
119
|
+
def __post_init__(self):
|
|
120
|
+
if not isinstance(self.shape, ShapelyPolygon):
|
|
121
|
+
raise TypeError("shape must be of type shapely.geometry.Polygon.")
|
|
122
|
+
if self.shape.is_empty:
|
|
123
|
+
raise ValueError("Polygon is empty.")
|
|
124
|
+
|
|
125
|
+
if len(self.scores) == 0 and len(self.labels) != 1:
|
|
126
|
+
raise ValueError(
|
|
127
|
+
"Ground truths must be defined with no scores and a single label. If you meant to define a prediction, then please include one score for every label provided."
|
|
128
|
+
)
|
|
129
|
+
if len(self.scores) > 0 and len(self.labels) != len(self.scores):
|
|
130
|
+
raise ValueError(
|
|
131
|
+
"If scores are defined, there must be a 1:1 pairing with labels."
|
|
132
|
+
)
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
@dataclass
|
|
136
|
+
class Bitmask:
|
|
137
|
+
"""
|
|
138
|
+
Represents a binary mask with associated labels and optional scores.
|
|
139
|
+
|
|
140
|
+
Parameters
|
|
141
|
+
----------
|
|
142
|
+
uid : str
|
|
143
|
+
A unique identifier.
|
|
144
|
+
mask : NDArray[np.bool_]
|
|
145
|
+
A NumPy array of boolean values representing the mask.
|
|
146
|
+
labels : list of str
|
|
147
|
+
List of labels associated with the mask.
|
|
148
|
+
scores : list of float, optional
|
|
149
|
+
Confidence scores corresponding to each label. Defaults to an empty list.
|
|
150
|
+
metadata : dict[str, Any], optional
|
|
151
|
+
A dictionary containing any metadata to be used within filtering operations.
|
|
152
|
+
|
|
153
|
+
Examples
|
|
154
|
+
--------
|
|
155
|
+
Ground Truth Example:
|
|
156
|
+
|
|
157
|
+
>>> import numpy as np
|
|
158
|
+
>>> mask = np.array([[True, False], [False, True]], dtype=np.bool_)
|
|
159
|
+
>>> bitmask = Bitmask(uid="abc", mask=mask, labels=['tree'])
|
|
160
|
+
|
|
161
|
+
Prediction Example:
|
|
162
|
+
|
|
163
|
+
>>> bitmask = Bitmask(
|
|
164
|
+
... uid="xyz", mask=mask, labels=['tree'], scores=[0.85]
|
|
165
|
+
... )
|
|
166
|
+
"""
|
|
167
|
+
|
|
168
|
+
uid: str
|
|
169
|
+
mask: NDArray[np.bool_]
|
|
170
|
+
labels: list[str]
|
|
171
|
+
scores: list[float] = field(default_factory=list)
|
|
172
|
+
metadata: dict[str, Any] | None = None
|
|
173
|
+
|
|
174
|
+
def __post_init__(self):
|
|
175
|
+
|
|
176
|
+
if (
|
|
177
|
+
not isinstance(self.mask, np.ndarray)
|
|
178
|
+
or self.mask.dtype != np.bool_
|
|
179
|
+
):
|
|
180
|
+
raise ValueError(
|
|
181
|
+
"Expected mask to be of type `NDArray[np.bool_]`."
|
|
182
|
+
)
|
|
183
|
+
elif not self.mask.any():
|
|
184
|
+
raise ValueError("Mask does not define any object instances.")
|
|
185
|
+
|
|
186
|
+
if len(self.scores) == 0 and len(self.labels) != 1:
|
|
187
|
+
raise ValueError(
|
|
188
|
+
"Ground truths must be defined with no scores and a single label. If you meant to define a prediction, then please include one score for every label provided."
|
|
189
|
+
)
|
|
190
|
+
if len(self.scores) > 0 and len(self.labels) != len(self.scores):
|
|
191
|
+
raise ValueError(
|
|
192
|
+
"If scores are defined, there must be a 1:1 pairing with labels."
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
AnnotationType = TypeVar("AnnotationType", BoundingBox, Polygon, Bitmask)
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
@dataclass
|
|
200
|
+
class Detection(Generic[AnnotationType]):
|
|
201
|
+
"""
|
|
202
|
+
Detection data structure holding ground truths and predictions for object detection tasks.
|
|
203
|
+
|
|
204
|
+
Parameters
|
|
205
|
+
----------
|
|
206
|
+
uid : str
|
|
207
|
+
Unique identifier for the image or sample.
|
|
208
|
+
groundtruths : list[BoundingBox] | list[Polygon] | list[Bitmask]
|
|
209
|
+
List of ground truth annotations.
|
|
210
|
+
predictions : list[BoundingBox] | list[Polygon] | list[Bitmask]
|
|
211
|
+
List of predicted annotations.
|
|
212
|
+
metadata : dict[str, Any], optional
|
|
213
|
+
A dictionary containing any metadata to be used within filtering operations.
|
|
214
|
+
|
|
215
|
+
Examples
|
|
216
|
+
--------
|
|
217
|
+
>>> bbox_gt = BoundingBox(xmin=10, xmax=50, ymin=20, ymax=60, labels=['cat'])
|
|
218
|
+
>>> bbox_pred = BoundingBox(
|
|
219
|
+
... xmin=12, xmax=48, ymin=22, ymax=58, labels=['cat'], scores=[0.9]
|
|
220
|
+
... )
|
|
221
|
+
>>> detection = Detection(
|
|
222
|
+
... uid='image_001',
|
|
223
|
+
... groundtruths=[bbox_gt],
|
|
224
|
+
... predictions=[bbox_pred]
|
|
225
|
+
... )
|
|
226
|
+
"""
|
|
227
|
+
|
|
228
|
+
uid: str
|
|
229
|
+
groundtruths: list[AnnotationType]
|
|
230
|
+
predictions: list[AnnotationType]
|
|
231
|
+
metadata: dict[str, Any] | None = None
|
|
232
|
+
|
|
233
|
+
def __post_init__(self):
|
|
234
|
+
for prediction in self.predictions:
|
|
235
|
+
if len(prediction.scores) != len(prediction.labels):
|
|
236
|
+
raise ValueError(
|
|
237
|
+
"Predictions must provide a score for every label."
|
|
238
|
+
)
|