Skip to content
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

Removing unnecessary steps and passing extra arguments for tiled inference #469

Merged
merged 1 commit into from
Mar 10, 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
4 changes: 2 additions & 2 deletions terratorch/tasks/regression_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ def predict_step(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> T
other_keys = batch.keys() - {"image", "mask", "filename"}
rest = {k: batch[k] for k in other_keys}

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

if self.tiled_inference_parameters:
# TODO: tiled inference does not work with additional input data (**rest)
y_hat: Tensor = tiled_inference(model_forward, x, 1, self.tiled_inference_parameters)
y_hat: Tensor = tiled_inference(model_forward, x, 1, self.tiled_inference_parameters, **rest)
else:
y_hat: Tensor = self(x, **rest).output
return y_hat, file_names
8 changes: 3 additions & 5 deletions terratorch/tasks/segmentation_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,16 @@ def predict_step(self, batch: Any, batch_idx: int, dataloader_idx: int = 0) -> T

rest = {k: batch[k] for k in other_keys}

model_output: ModelOutput = self(x, **rest)

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

if self.tiled_inference_parameters:
y_hat: Tensor = tiled_inference(
# TODO: tiled inference does not work with additional input data (**rest)
model_forward,
x,
self.hparams["model_args"]["num_classes"],
self.tiled_inference_parameters,
**rest,
)
else:
y_hat: Tensor = self(x, **rest).output
Expand Down
3 changes: 2 additions & 1 deletion terratorch/tasks/tiled_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def tiled_inference(
input_batch: torch.Tensor,
out_channels: int,
inference_parameters: TiledInferenceParameters,
**kwargs
) -> torch.Tensor:
"""
Like divide an image into (potentially) overlapping tiles and perform inference on them.
Expand Down Expand Up @@ -163,7 +164,7 @@ def tiled_inference(
end = min(len(coordinates_and_inputs), start + process_batch_size)
batch = coordinates_and_inputs[start:end]
tensor_input = torch.stack([b.input_data for b in batch], dim=0)
output = model_forward(tensor_input)
output = model_forward(tensor_input, **kwargs)
output = [output[i] for i in range(len(batch))]
for batch_input, predicted in zip(batch, output, strict=True):
if batch_input.output_crop is not None:
Expand Down
Loading