abstract_dataloader.ext.objective
¶
Objective base classes and specifications.
Programming Model
- An
Objective
is 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
.
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 |
---|---|---|---|
objectives
|
Mapping | MultiObjectiveSpec
|
multiple objectives, organized by name; see
|
{}
|
Source code in src/abstract_dataloader/ext/objective.py
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 275 276 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 |
|
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_true
andy_pred
are 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_pred
key, 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_true
andy_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. |
Source code in src/abstract_dataloader/ext/objective.py
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 164 165 166 167 168 169 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 |
|
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
__call__
abstractmethod
¶
__call__(
y_true: YTrue, y_pred: YPred, train: bool = True
) -> tuple[Float[TArray, batch], dict[str, Float[TArray, batch]]]
Training metrics implementation.
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. |