abstract_dataloader.ext.objective
¶
A flexible programming model for training objectives.
Programming Model
- An
Objectiveis a callable which returns a (batched) scalar loss and a dictionary of metrics. - Objectives can be combined into a higher-order objective,
MultiObjective, which combines their losses and aggregates their metrics; specify these objectives using aMultiObjectiveSpec. MissingInputErroris raised when a required input key or attribute is missing.
abstract_dataloader.ext.objective.MissingInputError
¶
Bases: Exception
Exception raised when a required input key or attribute is missing.
Source code in src/abstract_dataloader/ext/objective.py
abstract_dataloader.ext.objective.MultiObjective
¶
Bases: Objective[TArray, YTrue, YPred]
Composite objective that combines multiple objectives.
Hydra Configuration
If using Hydra for dependency
injection, a MultiObjective configuration should look like this:
Type Parameters
YTrue: ground truth data type.YHat: model output data type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
strict
|
bool
|
if |
True
|
objectives
|
Mapping | MultiObjectiveSpec
|
multiple objectives, organized by name; see
|
{}
|
Source code in src/abstract_dataloader/ext/objective.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 | |
abstract_dataloader.ext.objective.MultiObjectiveSpec
dataclass
¶
Bases: Generic[YTrue, YPred, YTrueAll, YPredAll]
Specification for a single objective in a multi-objective setup.
The inputs and outputs for each objective are specified using y_true and
y_pred:
None: The providedy_trueandy_predare passed directly to the objective. This means that if multiple objectives all useNone, they will all receive the same data that comes from the dataloader.str: The key indexes into a mapping which has they_true/y_predkey, or an object which has a matching attribute.Sequence[str]: Each key indexes into the layers of a nested mapping or object.Callable: The callable is applied to the providedy_trueandy_pred.
Warning
The user is responsible for ensuring that the y_true and y_pred
keys or callables index the appropriate types for this objective.
Type Parameters
YTrue: objective ground truth data type.YHat: objective model prediction data type.YTrueAll: type of all ground truth data (as loaded by the dataloader).YHatAll: type of all model output data (as produced by the model).
Attributes:
| Name | Type | Description |
|---|---|---|
objective |
Objective
|
The objective to use. |
weight |
float
|
Weight of the objective in the overall loss. |
y_true |
str | Sequence[str] | Callable[[YTrueAll], YTrue] | None
|
Key or callable to index into the ground truth data. |
y_pred |
str | Sequence[str] | Callable[[YPredAll], YPred] | None
|
Key or callable to index into the model output data. |
aux |
Mapping[str, str | Sequence[str] | Callable[[YTrueAll], YTrue] | None]
|
Auxiliary inputs indexed from ground truth data and passed to the
objective as keyword arguments. Each key becomes a keyword argument
name, and the value specifies how to index into |
Source code in src/abstract_dataloader/ext/objective.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
index_aux
¶
Get indexed auxiliary inputs from ground truth data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
YTrueAll
|
All ground truth data (as loaded by the dataloader). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict mapping each aux key to its indexed value, ready to be |
dict[str, Any]
|
passed as keyword arguments to the objective. |
Source code in src/abstract_dataloader/ext/objective.py
index_y_pred
¶
Get indexed model output data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_pred
|
YPredAll
|
All model output data (as produced by the model). |
required |
Returns:
| Type | Description |
|---|---|
YPred
|
Indexed model output data. |
Source code in src/abstract_dataloader/ext/objective.py
index_y_true
¶
Get indexed ground truth data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
YTrueAll
|
All ground truth data (as loaded by the dataloader). |
required |
Returns:
| Type | Description |
|---|---|
YTrue
|
Indexed ground truth data. |
Source code in src/abstract_dataloader/ext/objective.py
abstract_dataloader.ext.objective.Objective
¶
Bases: Protocol, Generic[TArray, YTrue, YPred]
Composable training objective.
Note
Metrics should use torch.no_grad() to make sure gradients are not
computed for non-loss metrics!
Type Parameters
TArray: backend (jax.Array,torch.Tensor, etc.)YTrue: ground truth data type.YPred: model output data type.
Source code in src/abstract_dataloader/ext/objective.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
__call__
abstractmethod
¶
__call__(
y_true: YTrue, y_pred: YPred, train: bool = True
) -> tuple[Float[TArray, batch], dict[str, Float[TArray, batch]]]
Training metrics implementation.
Tip
When implementing Objective, you can add additional arguments to
__call__ as needed; if using MultiObjective, these arguments
can be specified via the aux field.
To be a valid Objective type, additional arguments should be
appended following the standard arguments (i.e., after train),
and provided with default values. If these arguments are required,
the implementation should raise an appropriate error or assertion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
YTrue
|
data channels (i.e. dataloader output). |
required |
y_pred
|
YPred
|
model outputs. |
required |
train
|
bool
|
Whether in training mode (i.e. skip expensive metrics). |
True
|
Returns:
| Type | Description |
|---|---|
tuple[Float[TArray, batch], dict[str, Float[TArray, batch]]]
|
A tuple containing the loss and a dict of metric values. |
Source code in src/abstract_dataloader/ext/objective.py
render
¶
render(
y_true: YTrue, y_pred: YPred, render_gt: bool = False
) -> dict[str, Shaped[ndarray, "batch ..."]]
Render model outputs and/or ground truth for later analysis.
This method may return an empty dict.
How does this differ from visualizations?
Unlike visualizations, which is expected to return a single
RGB image per batch, render is:
- expected to return a unique rendered value per sample, and
- may have arbitrary types (as long as they are a numpy arrays).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
YTrue
|
data channels (i.e. dataloader output). |
required |
y_pred
|
YPred
|
model outputs. |
required |
render_gt
|
bool
|
whether to render ground truth data. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, Shaped[ndarray, 'batch ...']]
|
A dict, where each key is the name of a rendered output, and the value is a numpy array of the rendered data (e.g., an image). |
Source code in src/abstract_dataloader/ext/objective.py
visualizations
¶
Generate visualizations for each entry in a batch.
This method may return an empty dict.
Note
This method should be called only from a "detached" CPU thread so as not to affect training throughput; the caller is responsible for detaching gradients and sending the data to the CPU. As such, implementations are free to use CPU-specific methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
y_true
|
YTrue
|
data channels (i.e., dataloader output). |
required |
y_pred
|
YPred
|
model outputs. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, UInt8[ndarray, 'H W 3']]
|
A dict, where each key is the name of a visualization, and the value is a stack of RGB images in HWC order, detached from Torch and sent to a numpy array. |
Source code in src/abstract_dataloader/ext/objective.py
abstract_dataloader.ext.objective.VisualizationConfig
dataclass
¶
General-purpose visualization configuration.
Objectives which make use of this configuration may ignore the provided values.
Attributes:
| Name | Type | Description |
|---|---|---|
cols |
int
|
number of columns to tile images for in-training visualizations. |
width |
int
|
width of each sample when rendered. |
height |
int
|
height of each sample when rendered. |
cmaps |
Mapping[str, str | UInt8[ndarray, 'N 3']]
|
colormaps to use, where values correspond to the name of a matplotlib colormap or a numpy array of enumerated RGB values. |