Skip to content

Automatically running tiled inference for the test step when memory is insufficient. #478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion terratorch/tasks/regression_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,29 @@ def test_step(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> None
y = batch["mask"]
other_keys = batch.keys() - {"image", "mask", "filename"}
rest = {k: batch[k] for k in other_keys}
model_output: ModelOutput = self(x, **rest)

def model_forward(x, **kwargs):
return self(x, **kwargs).output

# When the input sample cannot be fit on memory for some reason
# the tiled inference is automatically invoked.
try:
model_output: ModelOutput = self(x, **rest)
except RuntimeError:
logger.info("\n The input sample could not run in a full format. Using tiled inference.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Joao-L-S-Almeida the error should be logged

looger.info("Notice that the tiled inference WON'T produce the exactly same result as the full inference.")
if self.tiled_inference_parameters:
y_hat: Tensor = tiled_inference(
model_forward,
x,
1,
self.tiled_inference_parameters,
**rest,
)
model_output = ModelOutput(output=y_hat)
else:
raise Exception("You need to define a configuration for the tiled inference.")

if dataloader_idx >= len(self.test_loss_handler):
msg = "You are returning more than one test dataloader but not defining enough test_dataloaders_names."
raise ValueError(msg)
Expand Down
25 changes: 24 additions & 1 deletion terratorch/tasks/segmentation_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from terratorch.tasks.optimizer_factory import optimizer_factory
from terratorch.tasks.tiled_inference import TiledInferenceParameters, tiled_inference
from terratorch.tasks.base_task import TerraTorchTask
from terratorch.models.model import ModelOutput

BATCH_IDX_FOR_VALIDATION_PLOTTING = 10

Expand Down Expand Up @@ -263,7 +264,29 @@ def test_step(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> None
other_keys = batch.keys() - {"image", "mask", "filename"}

rest = {k: batch[k] for k in other_keys}
model_output: ModelOutput = self(x, **rest)

def model_forward(x, **kwargs):
return self(x, **kwargs).output

# When the input sample cannot be fit on memory for some reason
# the tiled inference is automatically invoked.
try:
model_output: ModelOutput = self(x, **rest)
except RuntimeError:
logger.info("\n The input sample could not run in a full format. Using tiled inference.")
looger.info("Notice that the tiled inference WON'T produce the exactly same result as the full inference.")
if self.tiled_inference_parameters:
y_hat: Tensor = tiled_inference(
model_forward,
x,
self.hparams["model_args"]["num_classes"],
self.tiled_inference_parameters,
**rest,
)
model_output = ModelOutput(output=y_hat)
else:
raise Exception("You need to define a configuration for the tiled inference.")

if dataloader_idx >= len(self.test_loss_handler):
msg = "You are returning more than one test dataloader but not defining enough test_dataloaders_names."
raise ValueError(msg)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# lightning.pytorch==2.1.1
seed_everything: 42
trainer:
accelerator: cpu
accelerator: auto
strategy: auto
devices: auto
num_nodes: 1
Expand Down Expand Up @@ -118,12 +118,12 @@ model:
model_factory: PrithviModelFactory

# uncomment this block for tiled inference
# tiled_inference_parameters:
# h_crop: 224
# h_stride: 192
# w_crop: 224
# w_stride: 192
# average_patches: true
tiled_inference_parameters:
h_crop: 224
h_stride: 192
w_crop: 224
w_stride: 192
average_patches: true
optimizer:
class_path: torch.optim.AdamW
init_args:
Expand Down