rslearn.train.tasks.per_pixel_timestep¶
per_pixel_timestep ¶
Per-pixel timestep prediction, with labels and outputs as days since the Unix epoch.
The model classifies each pixel into one of the input timesteps (e.g. the image at which a change occurred), like a SegmentationTask whose classes are the timesteps. Labels are dates (uint16 days since 1970-01-01 UTC), which the head maps to an input timestep once the final input timestamps are known. At prediction time, the predicted timestep is written as the days since 1970-01-01 of the midpoint of that image's time range, so that neither the labels nor the output depend on which images the model was given.
DateToTimestepMode ¶
Bases: StrEnum
How to map a labeled date to one of the input timesteps.
Source code in rslearn/train/tasks/per_pixel_timestep.py
PerPixelTimestepHead ¶
Bases: SegmentationHead
Head for PerPixelTimestepTask.
It is meant to be applied over per-timestep logits at each pixel. During training and evaluation, it maps the labeled date at each pixel to an input timestep (see days_to_timesteps), and then behaves like SegmentationHead. It additionally attaches the date of each input timestep so that PerPixelTimestepTask can write dates instead of timestep indices.
The logit channels must correspond to the timesteps of the input selected by the input_key in order. Each per-example output is a dict with "probs" (CHW softmax probabilities) and "timestamps" (int64 tensor with the number of days since 1970-01-01 of the midpoint of each of the T input timesteps). When targets are given, it also has "targets", a dict with the timestep index "classes" and "valid" mask used for the loss, which PerPixelTimestepTask uses for metrics. Channels beyond T, e.g. from padding tokens, are ignored by PerPixelTimestepTask.
Source code in rslearn/train/tasks/per_pixel_timestep.py
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 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | |
forward ¶
forward(intermediates: Any, context: ModelContext, targets: list[dict[str, Any]] | None = None) -> ModelOutput
Compute timestep probabilities and attach the input timestamps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
intermediates
|
Any
|
a FeatureMaps with a single feature map containing one logit per timestep. |
required |
context
|
ModelContext
|
the model context. |
required |
targets
|
list[dict[str, Any]] | None
|
optional targets from PerPixelTimestepTask, each containing "days" (labeled days since 1970-01-01) and "valid". |
None
|
Returns:
| Type | Description |
|---|---|
ModelOutput
|
ModelOutput whose outputs are per-example dicts with "probs" and |
ModelOutput
|
"timestamps", and "targets" if targets were given. |
Source code in rslearn/train/tasks/per_pixel_timestep.py
PerPixelTimestepTask ¶
Bases: SegmentationTask
Predict an input timestep at each pixel, with dates as labels and outputs.
The target raster must contain the labeled date at each pixel as the number of days since 1970-01-01 (UTC), with 65535 (2^16 - 1) at pixels without a label. It must be paired with PerPixelTimestepHead, which maps each labeled date to an input timestep. Training and metrics are then the same as SegmentationTask, with num_classes set to the number of input timesteps.
At prediction time, the per-pixel argmax timestep is mapped to the number of days since 1970-01-01 (UTC) of the midpoint of that input image's time range, so the output layer's band set should use the uint16 dtype. 65535 is used as the nodata value; set nodata_value: 65535 on the output band set so that the written raster is tagged with it.
Source code in rslearn/train/tasks/per_pixel_timestep.py
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 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 | |
process_inputs ¶
process_inputs(raw_inputs: Mapping[str, RasterImage | list[Feature]], metadata: SampleMetadata, load_targets: bool = True) -> tuple[dict[str, Any], dict[str, Any]]
Read the labeled days since 1970-01-01 at each pixel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_inputs
|
Mapping[str, RasterImage | list[Feature]]
|
raster or vector data to process |
required |
metadata
|
SampleMetadata
|
metadata about the patch being read |
required |
load_targets
|
bool
|
whether to load the targets or only inputs |
True
|
Returns:
| Type | Description |
|---|---|
tuple[dict[str, Any], dict[str, Any]]
|
tuple (input_dict, target_dict) where target_dict contains "days" and "valid". PerPixelTimestepHead maps the days to timestep indices. |
Source code in rslearn/train/tasks/per_pixel_timestep.py
process_output ¶
process_output(raw_output: Any, metadata: SampleMetadata) -> NDArray[Any]
Convert the predicted timestep at each pixel to days since 1970-01-01.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raw_output
|
Any
|
the per-example output from PerPixelTimestepHead. |
required |
metadata
|
SampleMetadata
|
metadata about the patch being read. |
required |
Returns:
| Type | Description |
|---|---|
NDArray[Any]
|
1xHxW uint16 array of days since 1970-01-01. |
Source code in rslearn/train/tasks/per_pixel_timestep.py
visualize ¶
visualize(input_dict: dict[str, Any], target_dict: dict[str, Any] | None, output: Any) -> dict[str, NDArray[Any]]
Visualize the predicted and target timesteps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dict
|
dict[str, Any]
|
the input dict from process_inputs |
required |
target_dict
|
dict[str, Any] | None
|
the target dict from process_inputs |
required |
output
|
Any
|
the prediction |
required |
Returns:
| Type | Description |
|---|---|
dict[str, NDArray[Any]]
|
a dictionary mapping image name to visualization image |
Source code in rslearn/train/tasks/per_pixel_timestep.py
get_metrics ¶
Get the SegmentationTask metrics, computed on the timestep probabilities.
Source code in rslearn/train/tasks/per_pixel_timestep.py
TimestepProbsMetricWrapper ¶
Bases: Metric
Pass PerPixelTimestepHead outputs to a SegmentationTask metric.
The metric gets the "probs" of each output, along with the timestep index targets that PerPixelTimestepHead computed from the labeled dates.
Source code in rslearn/train/tasks/per_pixel_timestep.py
update ¶
Update metric.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
preds
|
list[dict[str, Any]]
|
the per-example outputs from PerPixelTimestepHead. |
required |
targets
|
list[dict[str, Any]]
|
the targets from PerPixelTimestepTask. They are unused since the preds contain the corresponding timestep index targets. |
required |
Source code in rslearn/train/tasks/per_pixel_timestep.py
compute ¶
reset ¶
plot ¶
days_to_timesteps ¶
days_to_timesteps(label_days: Tensor, valid: Tensor, timestep_days: Tensor, mode: DateToTimestepMode, fallback_to_closest_timestep_if_no_match: bool = False) -> tuple[Tensor, Tensor]
Map per-pixel labeled dates to input timestep indices.
Dates are compared at day granularity, so a timestep on the same day as the label matches in both modes. If several timesteps qualify with the same day, the first one is used.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
label_days
|
Tensor
|
HW tensor of labeled days since 1970-01-01. |
required |
valid
|
Tensor
|
HW mask of pixels that have a label. |
required |
timestep_days
|
Tensor
|
T tensor with the days since 1970-01-01 of each timestep. |
required |
mode
|
DateToTimestepMode
|
whether to pick the latest timestep on or before the labeled day, or the earliest one on or after it. |
required |
fallback_to_closest_timestep_if_no_match
|
bool
|
if no timestep is on the requested side of the labeled day, use the closest timestep instead of marking the pixel invalid. |
False
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, Tensor]
|
tuple (classes, valid) of the HW long timestep indices (0 at invalid pixels) and the HW float mask of pixels with a valid timestep target. |