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

Switch from manual to pydantic validation of config and LighterSystem #135

Merged
merged 23 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f5881b9
Switch from manual to pydantic validation of config and LighterSystem…
ibro45 Aug 6, 2024
480bce9
Add pydantic dependency
ibro45 Aug 6, 2024
7fb9442
Do not like how Postprocessing looks. Fix style.
ibro45 Aug 9, 2024
66ed417
Match current Lighter's postprocessing scheme
ibro45 Aug 11, 2024
2be5b2b
Fix validators in schema
ibro45 Aug 12, 2024
dafd225
Add PatchedModuleDict, add missing "test" in ArgsConfigShema, use mod…
ibro45 Aug 19, 2024
01d39ca
Fix mistake in schema
ibro45 Aug 19, 2024
f2d9ad9
_lightning_module_methods_defined not needed anymore
ibro45 Aug 19, 2024
833e07b
Reorganize LighterSystem methods
ibro45 Aug 19, 2024
b177332
See if pandas upgrade will fix the numpy issue in checks
ibro45 Aug 19, 2024
d6f56b0
Attempt to fix "Numpy is not available" in checks
ibro45 Aug 19, 2024
0013d3d
Another attempt, numpy below v2
ibro45 Aug 19, 2024
ca418de
Add missing predict to batch postprocessing schema
ibro45 Aug 20, 2024
970a614
Bump aiohttp from 3.9.5 to 3.10.2 (#136)
dependabot[bot] Aug 20, 2024
2ac35b1
Bump zipp from 3.19.0 to 3.19.1 (#132)
dependabot[bot] Aug 20, 2024
a49dcff
Bump certifi from 2024.2.2 to 2024.7.4 (#131)
dependabot[bot] Aug 20, 2024
a54d366
Filter out the warning about "validate" field in ArgsConfigSchema
ibro45 Aug 20, 2024
c5c2929
Merge branch 'pydantic' of github.com:project-lighter/lighter into py…
ibro45 Aug 20, 2024
a570434
Enable interpolation strings in ArgsConfig
ibro45 Aug 27, 2024
442cee3
Allow multiple lighter commands at once (lighter fit test --config). …
ibro45 Aug 28, 2024
0766968
Add workaround for the validation of _requires_ with pydantic
ibro45 Sep 3, 2024
451a33a
Add basic tests for schema
surajpaib Sep 4, 2024
c424343
Fix codestyle
surajpaib Sep 4, 2024
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
5 changes: 3 additions & 2 deletions lighter/callbacks/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import torch
import torchvision
from torch import Tensor


def get_lighter_mode(lightning_stage: str) -> str:
Expand All @@ -15,13 +16,13 @@ def get_lighter_mode(lightning_stage: str) -> str:
return lightning_to_lighter[lightning_stage]


def preprocess_image(image: torch.Tensor) -> torch.Tensor:
def preprocess_image(image: Tensor) -> Tensor:
"""Preprocess the image before logging it. If it is a batch of multiple images,
it will create a grid image of them. In case of 3D, a single image is displayed
with slices stacked vertically, while a batch of 3D images as a grid where each
column is a different 3D image.
Args:
image (torch.Tensor): A 2D or 3D image tensor.
image (Tensor): A 2D or 3D image tensor.
Returns:
The image ready for logging.
"""
Expand Down
6 changes: 3 additions & 3 deletions lighter/callbacks/writer/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import gc
from abc import ABC, abstractmethod
from datetime import datetime
from pathlib import Path

import torch
from loguru import logger
from pytorch_lightning import Callback, Trainer
from torch import Tensor

from lighter import LighterSystem

Expand Down Expand Up @@ -48,7 +48,7 @@ def writers(self) -> Dict[str, Callable]:
"""

@abstractmethod
def write(self, tensor: torch.Tensor, id: int) -> None:
def write(self, tensor: Tensor, id: int) -> None:
"""
Method to define how a tensor should be saved. The input tensor will be a single tensor without
the batch dimension.
Expand All @@ -57,7 +57,7 @@ def write(self, tensor: torch.Tensor, id: int) -> None:
A specific writer function can be retrieved using `self.get_writer(self.format)`.

Args:
tensor (torch.Tensor): Tensor, without the batch dimension, to be saved.
tensor (Tensor): Tensor, without the batch dimension, to be saved.
id (int): Identifier for the tensor, can be used for naming files or adding table records.
"""

Expand Down
9 changes: 3 additions & 6 deletions lighter/callbacks/writer/file.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from typing import Callable, Dict, Union

from functools import partial
from pathlib import Path

import torch
import torchvision
from monai.data import metatensor_to_itk_image
from monai.transforms import DivisiblePad
from torch import Tensor

from lighter.callbacks.utils import preprocess_image
from lighter.callbacks.writer.base import LighterBaseWriter
Expand All @@ -27,9 +27,6 @@ class LighterFileWriter(LighterBaseWriter):
`tensor` is a single tensor without the batch dimension.
"""

def __init__(self, path: Union[str, Path], writer: Union[str, Callable]) -> None:
super().__init__(path, writer)

@property
def writers(self) -> Dict[str, Callable]:
return {
Expand All @@ -41,7 +38,7 @@ def writers(self) -> Dict[str, Callable]:
"itk_nifti": partial(write_itk_image, suffix=".nii.gz"),
}

def write(self, tensor: torch.Tensor, id: Union[int, str]) -> None:
def write(self, tensor: Tensor, id: Union[int, str]) -> None:
"""
Write the tensor using the writer specified at the instatiation.

Expand Down Expand Up @@ -82,7 +79,7 @@ def write_video(path, tensor):
torchvision.io.write_video(str(path), tensor, fps=24)


def write_itk_image(path: str, tensor: torch.Tensor, suffix) -> None:
def write_itk_image(path: str, tensor: Tensor, suffix) -> None:
path = path.with_suffix(suffix)
itk_image = metatensor_to_itk_image(tensor, channel_dim=0, dtype=tensor.dtype)
OPTIONAL_IMPORTS["itk"].imwrite(itk_image, str(path), True)
Loading
Loading