-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add model and loss function #8
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bcbd2cb
feat: add model and loss function
LAdam-ix dbc461c
fix: review ai agent feedback
LAdam-ix 7698647
fix: dataloading with new mlkit version
LAdam-ix 1cb5018
fix: mypy and ruff format fixes
LAdam-ix 05da4bd
fix: move stacked targets to model device
LAdam-ix 9df92a8
chore: ruff formating
LAdam-ix cb9125e
fix: review feedback
LAdam-ix da187a4
Revert "fix: review feedback"
LAdam-ix e3fc89e
fix: review feedback
LAdam-ix 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 hidden or 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,15 @@ | ||
| _target_: albumentations.OneOf | ||
| p: 1.0 | ||
| transforms: | ||
| - _target_: stain_normalization.data.modification.HEDFactor | ||
| h_range: [0.8, 1.2] | ||
| e_range: [0.8, 1.2] | ||
| - _target_: stain_normalization.data.modification.ExposureAdjustment | ||
| brightness_range: [0.8, 1.2] | ||
| - _target_: stain_normalization.data.modification.HSVModification | ||
| hue_shift_range: [-0.4, 0.4] | ||
| saturation_range: [0.8, 1.5] | ||
| value_range: [0.8, 1.3] | ||
| - _target_: stain_normalization.data.modification.CombinedModifications | ||
| od_scale_range: [0.65, 1.35] | ||
| brightness_range: [-0.4, 0.4] | ||
LAdam-ix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,15 @@ | ||
| _target_: albumentations.OneOf | ||
| p: 1.0 | ||
| transforms: | ||
| - _target_: stain_normalization.data.modification.HEDFactor | ||
| h_range: [0.8, 1.2] | ||
| e_range: [0.8, 1.2] | ||
| - _target_: stain_normalization.data.modification.ExposureAdjustment | ||
| brightness_range: [0.8, 1.2] | ||
| - _target_: stain_normalization.data.modification.HSVModification | ||
| hue_shift_range: [-0.4, 0.4] | ||
| saturation_range: [0.8, 1.5] | ||
| value_range: [0.8, 1.3] | ||
| - _target_: stain_normalization.data.modification.CombinedModifications | ||
| od_scale_range: [0.65, 1.35] | ||
| brightness_range: [-0.4, 0.4] | ||
LAdam-ix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
This file contains hidden or 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,56 @@ | ||
| defaults: | ||
| - hydra: default | ||
| - logger: mlflow | ||
| - /data/datasets@data.train: stain_normalization/train | ||
| - /data/datasets@data.val: stain_normalization/val | ||
| - /data/datasets@data.test: stain_normalization/test | ||
| - /data/datasets@data.predict: stain_normalization/predict | ||
| - _self_ | ||
|
|
||
| seed: ${random_seed:} | ||
| mode: fit | ||
| checkpoint: null | ||
|
|
||
|
|
||
| callbacks: | ||
| model_checkpoint: | ||
| _target_: lightning.pytorch.callbacks.ModelCheckpoint | ||
| save_top_k: 1 | ||
| save_last: true | ||
| monitor: validation/loss | ||
| mode: min | ||
|
|
||
| early_stopping: | ||
| _target_: lightning.pytorch.callbacks.EarlyStopping | ||
| monitor: validation/loss | ||
| patience: 5 | ||
| mode: min | ||
|
|
||
| model: | ||
| lr: 1e-4 | ||
| lambda_dssim: 0.6 | ||
| lambda_l1: 0.2 | ||
| lambda_lum: 0.2 | ||
| lambda_gdl: 0.1 | ||
|
|
||
| trainer: | ||
| enable_checkpointing: True | ||
| max_epochs: 100 | ||
| limit_train_batches: 5000 | ||
| log_every_n_steps: 50 | ||
|
|
||
| callbacks: | ||
| - ${callbacks.model_checkpoint} | ||
| - ${callbacks.early_stopping} | ||
|
|
||
| data: | ||
| batch_size: 64 | ||
| num_workers: 8 | ||
|
|
||
| metadata: | ||
| user: ??? | ||
| experiment_name: Stain-Normalization | ||
| run_name: ??? | ||
| description: ??? | ||
LAdam-ix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| hyperparams: ${model} | ||
|
|
||
This file contains hidden or 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,7 @@ | ||
| defaults: | ||
| - _self_ | ||
| - override hydra_logging: disabled | ||
| - override job_logging: disabled | ||
| output_subdir: null | ||
| run: | ||
| dir: . |
This file contains hidden or 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,6 @@ | ||
| _target_: rationai.mlkit.lightning.loggers.MLFlowLogger | ||
| experiment_name: ${metadata.experiment_name} | ||
| run_name: ${metadata.run_name} | ||
| tags: | ||
| mlflow.user: ${metadata.user} | ||
| mlflow.note.content: ${metadata.description} |
This file contains hidden or 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
Empty file.
This file contains hidden or 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,36 @@ | ||
| from random import randint | ||
|
|
||
| import hydra | ||
| import torch | ||
| from lightning import seed_everything | ||
| from lightning.pytorch.loggers import Logger | ||
| from omegaconf import DictConfig, OmegaConf | ||
| from rationai.mlkit import Trainer, autolog | ||
|
|
||
| from stain_normalization.data import DataModule | ||
| from stain_normalization.stain_normalization_model import StainNormalizationModel | ||
|
|
||
|
|
||
| OmegaConf.register_new_resolver( | ||
| "random_seed", lambda: randint(0, 2**31), use_cache=True | ||
| ) | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @hydra.main(config_path="../configs", config_name="default", version_base=None) | ||
| @autolog | ||
| def main(config: DictConfig, logger: Logger | None) -> None: | ||
| torch.set_float32_matmul_precision("high") | ||
| seed_everything(config.seed, workers=True) | ||
|
|
||
| data = hydra.utils.instantiate( | ||
| config.data, | ||
| _recursive_=False, # to avoid instantiating all the datasets | ||
| _target_=DataModule, | ||
| ) | ||
| model = hydra.utils.instantiate(config.model, _target_=StainNormalizationModel) | ||
| trainer = hydra.utils.instantiate(config.trainer, _target_=Trainer, logger=logger) | ||
| getattr(trainer, config.mode)(model, datamodule=data, ckpt_path=config.checkpoint) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() # pylint: disable=no-value-for-parameter | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
| from stain_normalization.modeling.l1ssim_loss import L1SSIMLoss | ||
| from stain_normalization.modeling.unet import UNet | ||
|
|
||
|
|
||
| __all__ = ["L1SSIMLoss", "UNet"] |
This file contains hidden or 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,135 @@ | ||
| """ | ||
| The SSIM is based on implementation from gaussian-splatting and slightly simplified | ||
| (pre-computed windows and removal of unused arguments). | ||
| https://github.com/graphdeco-inria/gaussian-splatting/blob/472689c0dc70417448fb451bf529ae532d32c095/utils/loss_utils.py | ||
| """ | ||
|
|
||
| from math import exp | ||
|
|
||
| import torch | ||
| import torch.nn as nn | ||
| import torch.nn.functional as F | ||
|
|
||
|
|
||
| class L1SSIMLoss(nn.Module): | ||
| def __init__( | ||
| self, | ||
| lambda_dssim: float = 0.6, | ||
| lambda_l1: float = 0.2, | ||
| lambda_lum: float = 0.2, | ||
| lambda_gdl: float = 0.1, | ||
| ): | ||
| super().__init__() | ||
| self.lambda_dssim = lambda_dssim | ||
| self.lambda_l1 = lambda_l1 | ||
| self.lambda_lum = lambda_lum | ||
| self.lambda_gdl = lambda_gdl | ||
|
|
||
| # precompute SSIM windows to avoid repetition | ||
| self.window_size = 11 | ||
| self.channel = 3 | ||
| self._1d_window = gaussian(self.window_size, 1.5).unsqueeze(1) | ||
| self._2d_window = ( | ||
| self._1d_window.mm(self._1d_window.t()).float().unsqueeze(0).unsqueeze(0) | ||
| ) | ||
| self.window: torch.Tensor | ||
| self.register_buffer( | ||
| "window", | ||
| self._2d_window.expand( | ||
| self.channel, 1, self.window_size, self.window_size | ||
| ).contiguous(), | ||
| ) | ||
LAdam-ix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def forward(self, image: torch.Tensor, target_image: torch.Tensor) -> torch.Tensor: | ||
| # L1 color loss | ||
| l1_loss = F.l1_loss(image, target_image, reduction="mean") | ||
|
|
||
| # SSIM structural loss | ||
| ssim_loss = 1.0 - self._ssim(image, target_image, self.window) | ||
|
|
||
| # Gradient loss for edges | ||
| gdl_loss = gradient_loss(image, target_image) | ||
|
|
||
| # Luminance / brightness loss | ||
| brig_loss = brightness_loss(image, target_image) | ||
|
|
||
| # total weighted loss | ||
| total_loss = ( | ||
| self.lambda_l1 * l1_loss | ||
| + self.lambda_dssim * ssim_loss | ||
| + self.lambda_gdl * gdl_loss | ||
| + self.lambda_lum * brig_loss | ||
vejtek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ) | ||
|
|
||
| return total_loss | ||
|
|
||
| @torch.compile | ||
| def _ssim( | ||
| self, img1: torch.Tensor, img2: torch.Tensor, window: torch.Tensor | ||
| ) -> torch.Tensor: | ||
| # Modified _ssim that uses pre-computed window | ||
| mu1 = F.conv2d(img1, window, padding=self.window_size // 2, groups=self.channel) | ||
| mu2 = F.conv2d(img2, window, padding=self.window_size // 2, groups=self.channel) | ||
|
|
||
| mu1_sq = mu1.pow(2) | ||
| mu2_sq = mu2.pow(2) | ||
| mu1_mu2 = mu1 * mu2 | ||
|
|
||
| sigma1_sq = ( | ||
| F.conv2d( | ||
| img1 * img1, window, padding=self.window_size // 2, groups=self.channel | ||
| ) | ||
| - mu1_sq | ||
| ) | ||
| sigma2_sq = ( | ||
| F.conv2d( | ||
| img2 * img2, window, padding=self.window_size // 2, groups=self.channel | ||
| ) | ||
| - mu2_sq | ||
| ) | ||
| sigma12 = ( | ||
| F.conv2d( | ||
| img1 * img2, window, padding=self.window_size // 2, groups=self.channel | ||
| ) | ||
| - mu1_mu2 | ||
| ) | ||
|
|
||
| c1 = 0.01**2 | ||
| c2 = 0.03**2 | ||
|
|
||
| ssim_map = ((2 * mu1_mu2 + c1) * (2 * sigma12 + c2)) / ( | ||
| (mu1_sq + mu2_sq + c1) * (sigma1_sq + sigma2_sq + c2) | ||
| ) | ||
|
|
||
| return ssim_map.mean() | ||
|
|
||
|
|
||
| def gaussian(window_size: int, sigma: float) -> torch.Tensor: | ||
| gauss = torch.tensor( | ||
| [ | ||
| exp(-((x - window_size // 2) ** 2) / float(2 * sigma**2)) | ||
| for x in range(window_size) | ||
| ] | ||
| ) | ||
| return gauss / gauss.sum() | ||
LAdam-ix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def brightness_loss(pred: torch.Tensor, target: torch.Tensor) -> torch.Tensor: | ||
| pred_mean = pred.mean(dim=[1, 2, 3]) | ||
| target_mean = target.mean(dim=[1, 2, 3]) | ||
| return F.l1_loss(pred_mean, target_mean) | ||
|
|
||
|
|
||
| def gradient_loss(image: torch.Tensor, target_image: torch.Tensor) -> torch.Tensor: | ||
| def gradient(x: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]: | ||
| dx = torch.abs(x[:, :, :, :-1] - x[:, :, :, 1:]) # Horizontal gradient | ||
| dy = torch.abs(x[:, :, :-1, :] - x[:, :, 1:, :]) # Vertical gradient | ||
| return dx, dy | ||
LAdam-ix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| image_dx, image_dy = gradient(image) | ||
| target_dx, target_dy = gradient(target_image) | ||
|
|
||
| loss_x = F.l1_loss(image_dx, target_dx, reduction="mean") | ||
| loss_y = F.l1_loss(image_dy, target_dy, reduction="mean") | ||
|
|
||
| return loss_x + loss_y | ||
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.
Uh oh!
There was an error while loading. Please reload this page.