-
Notifications
You must be signed in to change notification settings - Fork 6
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
Feature/image saving callback #407
Merged
+370
−150
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
160316e
refactor to do image saving in callback
4632039
update segmentation config
98caa97
add gan
8ab2e6b
add gan
77015e9
add instance seg
43dcc1e
update configs
8e11bcc
add model configs
1b48011
add n_postprocess to mask head
7d82792
remove save input from res blocks head
9749cde
simplify image saving;
83d35a4
update vicreg and mae heads
e98b463
remove multimae head
d088736
update plugin exp.
ff069a3
precommit
514a50d
add warning
cc3d49d
precommit
7d20392
add ostats
974e41a
add callback to init
c7a562e
update ostats
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,4 +54,3 @@ _aux: | |
activation: | ||
_target_: torch.nn.Sigmoid | ||
rescale_dtype: numpy.uint8 | ||
save_input: True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
from pathlib import Path | ||
from typing import List, Union | ||
|
||
from bioio.writers import OmeTiffWriter | ||
from lightning.pytorch.callbacks import Callback | ||
|
||
VALID_STAGES = ("train", "val", "test", "predict") | ||
|
||
|
||
class ImageSaver(Callback): | ||
def __init__( | ||
self, | ||
save_dir: Union[str, Path], | ||
save_every_n_epochs: int = 1, | ||
stages: List[str] = ["train", "val"], | ||
save_input: bool = False, | ||
): | ||
"""Callback for saving images after postprocessing by eads. | ||
|
||
Parameters | ||
---------- | ||
save_dir: Union[str, Path] | ||
Directory to save images | ||
save_every_n_epochs:int=1 | ||
Frequency to save images | ||
stages:List[str]=["train", "val"] | ||
Stages to save images | ||
save_input:bool =False | ||
Whether to save input images | ||
""" | ||
self.save_dir = Path(save_dir) | ||
for stage in stages: | ||
assert stage in VALID_STAGES, f"Invalid stage {stage}, must be one of {VALID_STAGES}" | ||
self.save_every_n_epochs = save_every_n_epochs | ||
self.stages = stages | ||
self.save_keys = ["pred", "target"] | ||
if save_input: | ||
self.save_keys.append("input") | ||
|
||
def _save(self, fn, data): | ||
fn.parent.mkdir(exist_ok=True, parents=True) | ||
OmeTiffWriter.save(uri=fn, data=data) | ||
|
||
def on_predict_batch_end( | ||
self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0 | ||
): | ||
if "predict" in self.stages: | ||
io_map, outputs = outputs | ||
if outputs is None: | ||
# image has already been saved | ||
return | ||
for i, head_io_map in enumerate(io_map.values()): | ||
for k, save_path in head_io_map.items(): | ||
self._save(save_path, outputs[k]["pred"][i]) | ||
|
||
# train/test/val | ||
def save(self, outputs, stage=None, step=None): | ||
for k in self.save_keys: | ||
for head in outputs[k]: | ||
self._save( | ||
self.save_dir / f"{stage}_images/{step}_{head}_{k}.tif", outputs[k][head] | ||
) | ||
|
||
def on_test_batch_end(self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0): | ||
if "test" in self.stages: | ||
# save all test outputs | ||
self.save(outputs, "test", trainer.global_step) | ||
|
||
def _should_save(self, batch_idx, epoch): | ||
return batch_idx == (epoch + 1) % self.save_every_n_epochs == 0 | ||
|
||
def on_train_batch_end(self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0): | ||
if "train" in self.stages and self._should_save(batch_idx, trainer.current_epoch): | ||
self.save(outputs, "train", trainer.global_step) | ||
|
||
def on_validation_batch_end( | ||
self, trainer, pl_module, outputs, batch, batch_idx, dataloader_idx=0 | ||
): | ||
if "val" in self.stages and self._should_save(batch_idx, trainer.current_epoch): | ||
self.save(outputs, "val", trainer.global_step) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what is the saving key here? Shouldnt callbacks just be a list of callbacks?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
they all have a key :
cyto-dl/configs/callbacks/default.yaml
Line 8 in 323ff26