Skip to content

Commit edbc369

Browse files
committed
Add convenience wrapper to lightning module to format predictions
1 parent b2caf83 commit edbc369

2 files changed

Lines changed: 51 additions & 28 deletions

File tree

ethology/detectors/models.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,19 @@ def predict_step(self, batch, batch_idx) -> Any:
147147

148148
return raw_prediction_dicts
149149

150-
def on_predict_epoch_end(self) -> None:
151-
# TODO:format predictions as an xarray dataset at the end of the epoch?
152-
pass
150+
@_check_output(ValidBboxDetectionsDataset)
151+
def run_inference(
152+
self,
153+
trainer,
154+
dataloader,
155+
attrs: dict | None = None,
156+
) -> xr.Dataset:
157+
"""Run inference and return the xarray dataset.
158+
159+
Convenience method that combines trainer.predict() and format_predictions().
160+
"""
161+
predictions = trainer.predict(self, dataloader)
162+
return self.format_predictions(predictions, attrs=attrs)
153163

154164
# ------- Formatting -------------------
155165
@staticmethod

notebook_detector_inference.py

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __getitem__(self, idx):
5050
)
5151

5252
# %%
53-
# Define config
53+
# Define model config
5454
config = {
5555
"model_kwargs": {
5656
"num_classes": 2,
@@ -60,11 +60,6 @@ def __getitem__(self, idx):
6060
"checkpoint": "/home/sminano/swc/project_crabs/ml-runs/617393114420881798/f348d9d196934073bece1b877cbc4d38/checkpoints/last.ckpt",
6161
}
6262

63-
# %%
64-
# Instantiate detector
65-
66-
model = SingleDetector(config)
67-
6863
# %%
6964
# Define dataset
7065

@@ -99,8 +94,14 @@ def __getitem__(self, idx):
9994
# else None, # see https://github.com/pytorch/pytorch/issues/87688
10095
)
10196
# %%
102-
# Run inference on dataset
97+
# Run inference using model on dataset
10398
# (format as detections dataset)
99+
100+
101+
# Instantiate detector
102+
model = SingleDetector(config)
103+
104+
# Define trainer
104105
trainer = Trainer(
105106
accelerator="gpu",
106107
devices=1,
@@ -109,30 +110,42 @@ def __getitem__(self, idx):
109110
# uses FP16 for most operations, FP32 for sensitive ones
110111
# This setting reduces memory and speeds up training
111112
)
112-
predictions = trainer.predict(model, dataloader)
113+
114+
# dataset attrs
115+
ds_attrs = {
116+
"images_dir": images_dir,
117+
"map_image_id_to_filename": {
118+
id: filename.relative_to(
119+
"/home/sminano/swc/project_ethology/07.09.2023-frames"
120+
)
121+
for id, filename in enumerate(dataset.image_files)
122+
},
123+
"map_category_to_str": {1: "crab"},
124+
}
125+
126+
predictions_ds = model.run_inference(trainer, dataloader, attrs=ds_attrs)
113127

114128

115129
# %%
116-
# TODO: can I do this at the end of the predict epoch?
117-
predictions_ds = model.format_predictions(
118-
predictions,
119-
{
120-
"images_dir": images_dir,
121-
"map_image_id_to_filename": {
122-
id: filename.relative_to(
123-
"/home/sminano/swc/project_ethology/07.09.2023-frames"
124-
)
125-
for id, filename in enumerate(dataset.image_files)
126-
},
127-
"map_category_to_str": {1: "crab"},
128-
},
129-
)
130+
# Alternative
131+
# # TODO: can I do this at the end of the predict epoch?
132+
# predictions = trainer.predict(model, dataloader)
133+
# predictions_ds = model.format_predictions(
134+
# predictions,
135+
# {
136+
# "images_dir": images_dir,
137+
# "map_image_id_to_filename": {
138+
# id: filename.relative_to(
139+
# "/home/sminano/swc/project_ethology/07.09.2023-frames"
140+
# )
141+
# for id, filename in enumerate(dataset.image_files)
142+
# },
143+
# "map_category_to_str": {1: "crab"},
144+
# },
145+
# )
130146

131147
# %%
132148
# Export as COCO annotations?
133-
# TODO: require "category" array in input dataset?
134-
# I think so, I dont think there are detectors that return label-less boxes?
135-
# predictions_ds = predictions_ds.rename_vars({'category':'label'})
136149
out_file = save_bboxes.to_COCO_file(predictions_ds, output_filepath="out.json")
137150
# %%
138151
# Load proofread annotations

0 commit comments

Comments
 (0)