Skip to content

Commit 7ab929e

Browse files
committed
Merge branch 'main' of github.com:project-lighter/lighter into poetry->uv
2 parents 6380f6c + 45035ed commit 7ab929e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+2782
-496
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,5 @@ projects/*
152152
**/predictions/
153153
*/.DS_Store
154154
.DS_Store
155+
.aider*
156+
test_dir/

Makefile

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
#* Variables
1+
### Variables
2+
# Define shell and Python environment variables
23
SHELL := /usr/bin/env bash
34
PYTHON := python
45
PYTHONPATH := `pwd`
5-
#* Docker variables
6-
IMAGE := lighter
7-
VERSION := latest
86

97
# Install
108
.PHONY: setup
@@ -47,4 +45,4 @@ check-safety:
4745
uvx bandit -ll --recursive lighter tests
4846

4947
.PHONY: lint
50-
lint: test check-codestyle mypy check-safety
48+
lint: test check-codestyle mypy check-safety

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<br/>
1616
<div align="center">
1717

18-
[![build](https://github.com/project-lighter/lighter/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/project-lighter/lighter/actions/workflows/build.yml) ![Coverage](./assets/images/coverage.svg) [![GitHub license](https://img.shields.io/github/license/project-lighter/lighter)](https://github.com/project-lighter/lighter/blob/main/LICENSE)
18+
[![build](https://github.com/project-lighter/lighter/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/project-lighter/lighter/actions/workflows/build.yml) ![Coverage](./assets/images/coverage.svg) [![GitHub license](https://img.shields.io/github/license/project-lighter/lighter)](https://github.com/project-lighter/lighter/blob/main/LICENSE)
1919

2020
<a href="https://discord.gg/zJcnp6KrUp">
2121
<img src="https://discord.com/api/guilds/1252251284908539965/widget.png?style=banner2" alt="Lighter Discord Server"/>

assets/images/coverage.svg

+3-3
Loading

diagram.svg

-1
This file was deleted.

docs/advanced/callbacks.md

+14-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,14 @@
1-
🚧 Under construction 🚧
1+
# Callbacks
2+
3+
Callbacks in Lighter allow you to customize and extend the training process. You can define custom actions to be executed at various stages of the training loop.
4+
5+
## Freezer Callback
6+
The `LighterFreezer` callback allows you to freeze certain layers of the model during training. This can be useful for transfer learning or fine-tuning.
7+
8+
## Writer Callbacks
9+
Lighter provides writer callbacks to save predictions in different formats. The `LighterFileWriter` and `LighterTableWriter` are examples of such callbacks.
10+
11+
- **LighterFileWriter**: Writes predictions to files, supporting formats like images, videos, and ITK images.
12+
- **LighterTableWriter**: Saves predictions in a table format, such as CSV.
13+
14+
For more details on how to implement and use callbacks, refer to the [PyTorch Lightning Callback documentation](https://pytorch-lightning.readthedocs.io/en/stable/extensions/callbacks.html).

docs/advanced/inferer.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
🚧 Under construction 🚧
1+
# Inferer
2+
3+
The inferer in Lighter is used for making predictions on data. It is typically used in validation, testing, and prediction workflows.
4+
5+
## Using Inferers
6+
Inferers must be classes with a `__call__` method that accepts two arguments: the input to infer over and the model itself. They are used to handle complex inference scenarios, such as patch-based or sliding window inference.
7+
8+
## MONAI Inferers
9+
Lighter integrates with MONAI inferers, which cover most common inference scenarios. You can use MONAI's sliding window or patch-based inferers directly in your Lighter configuration.
10+
11+
For more information on MONAI inferers, visit the [MONAI documentation](https://docs.monai.io/en/stable/inferers.html).

docs/advanced/postprocessing.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1-
🚧 Under construction 🚧
1+
# Postprocessing
2+
3+
Postprocessing in Lighter allows you to apply custom transformations to data at various stages of the workflow. This can include modifying inputs, targets, predictions, or entire batches.
4+
5+
## Defining Postprocessing Functions
6+
Postprocessing functions can be defined in the configuration file under the `postprocessing` key. They can be applied to:
7+
- **Batch**: Modify the entire batch before it is passed to the model.
8+
- **Criterion**: Modify inputs, targets, or predictions before loss calculation.
9+
- **Metrics**: Modify inputs, targets, or predictions before metric calculation.
10+
- **Logging**: Modify inputs, targets, or predictions before logging.
11+
12+
## Example
13+
```yaml
14+
postprocessing:
15+
batch:
16+
train: '$lambda x: {"input": x[0], "target": x[1]}'
17+
criterion:
18+
input: '$lambda x: x / 255.0'
19+
metrics:
20+
pred: '$lambda x: x.argmax(dim=1)'
21+
logging:
22+
target: '$lambda x: x.cpu().numpy()'
23+
```
24+
25+
For more information on how to use postprocessing in Lighter, refer to the [Lighter documentation](./config.md).

docs/basics/config.md

+25-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
# Configuration System
22

3-
Lighter is a configuration-centric framework where the config. is used for setting up the machine learning workflow from model architecture selection, loss function, optimizer, dataset preparation and running the training/evaluation/inference process.
3+
Lighter is a configuration-centric framework that uses YAML files to set up machine learning workflows. These configurations cover everything from model architecture selection, loss functions, and optimizers to dataset preparation and the execution of training, evaluation, and inference processes.
44

5-
Our configuration system is heavily based on MONAI bundle parser but with a standardized structure. For every configuration, we expect several items to be mandatorily defined.
5+
Our configuration system is inspired by the MONAI bundle parser, offering a standardized structure. Each configuration requires several mandatory components to be defined.
66

7-
Let us take a simple example config to dig deeper into the configuration system of Lighter. You can go through the config and click on the + for more information about specific concepts.
7+
The configuration is divided into two main components:
8+
- **Trainer**: Handles the training process, including epochs, devices, etc.
9+
- **LighterSystem**: Encapsulates the model, optimizer, datasets, and other components.
10+
11+
Let's explore a simple example configuration to understand Lighter's configuration system better. You can expand each section for more details on specific concepts.
812

913
<div class="annotate" markdown>
1014

@@ -49,7 +53,9 @@ system:
4953
1. `_target_` is a special reserved keyword that initializes a python object from the provided text. In this case, a `Trainer` object from the `pytorch_lightning` library is initialized
5054
2. `max_epochs` is an argument of the `Trainer` class which is passed through this format. Any argument for the class can be passed similarly.
5155
3. `$@` is a combination of `$` which evaluates a python expression and `@` which references a python object. In this case we first reference the model with `@model` which is the `torchvision.models.resnet18` defined earlier and then access its parameters using `[email protected]()`
52-
4. YAML allows passing a list in the format below where each `_target_` specifices a transform that is added to the list of transforms in `Compose`. The `torchvision.datasets.CIFAR10` accepts these with a `transform` argument and applies them to each item.
56+
4. YAML allows passing a list in the format below where each `_target_` specifies a transform that is added to the list of transforms in `Compose`. The `torchvision.datasets.CIFAR10` accepts these with a `transform` argument and applies them to each item.
57+
58+
5. Datasets are defined for different modes: train, val, test, and predict. Each dataset can have its own transforms and configurations.
5359

5460
## Configuration Concepts
5561
As seen in the [Quickstart](./quickstart.md), Lighter has two main components:
@@ -61,17 +67,17 @@ As seen in the [Quickstart](./quickstart.md), Lighter has two main components:
6167
max_epochs: 100
6268
```
6369
64-
The trainer object (`pytorch_lightning.Trainer`) is initialized through the `_target_` key. For more info on `_target_` and special keys, click [here](#special-syntax-and-keywords)
70+
The trainer object (`pytorch_lightning.Trainer`) is initialized using the `_target_` key. For more information on `_target_` and other special keys, see [Special Syntax and Keywords](#special-syntax-and-keywords).
6571

66-
The `max_epochs` is an argument provided to the `pytorch_lightning.Trainer` object during its instantiation. All arguments that are accepted during instantiation can be provided similarly.
72+
The `max_epochs` parameter is passed to the `pytorch_lightning.Trainer` object during instantiation. You can provide any argument accepted by the class in this manner.
6773

6874
### LighterSystem Configuration
69-
While Lighter borrows the Trainer from Pytorch Lightning, LighterSystem is a custom component unique to Lighter that draws on several concepts of PL such as LightningModule to provide a simple way to capture all the integral elements of a deep learning system.
75+
While Lighter utilizes the Trainer from PyTorch Lightning, LighterSystem is a unique component that incorporates concepts from PL, such as LightningModule, to encapsulate all essential elements of a deep learning system in a straightforward manner.
7076

7177
Concepts encapsulated by LighterSystem include,
7278

7379
#### Model definition
74-
The `torchvision` library is installed by default in Lighter and therefore, you can choose different torchvision models here. We also have `monai` packaged with Lighter, so if you are looking to use a ResNet, all you need to modify to fit this new model in your config is,
80+
The `torchvision` library is included by default in Lighter, allowing you to select various torchvision models. Additionally, Lighter includes `monai`, enabling you to easily switch to a ResNet model by adjusting your configuration as follows:
7581

7682
=== "Torchvision ResNet18"
7783

@@ -111,7 +117,7 @@ The `torchvision` library is installed by default in Lighter and therefore, you
111117
<br/>
112118
#### Criterion/Loss
113119

114-
Similar to overriding models, when exploring different loss types in Lighter, you can easily switch between various loss functions provided by libraries such as `torch` and `monai`. This flexibility allows you to experiment with different approaches to optimize your model's performance without changing code!! Below are some examples of how you can modify the criterion section in your configuration file to use different loss functions.
120+
Just as you can override models, Lighter allows you to switch between various loss functions from libraries like `torch` and `monai`. This flexibility lets you experiment with different optimization strategies without altering your code. Here are examples of how to modify the criterion section in your configuration to use different loss functions:
115121

116122
=== "CrossEntropyLoss"
117123
```yaml
@@ -134,7 +140,7 @@ Similar to overriding models, when exploring different loss types in Lighter, yo
134140
<br/>
135141
#### Optimizer
136142

137-
Same as above, you can experiment with different optimizer parameters. Model parameters are directly passed to the optimizer in `params` argument.
143+
Similarly, you can experiment with different optimizer parameters. Model parameters are passed directly to the optimizer via the `params` argument.
138144
```yaml hl_lines="5"
139145
LighterSystem:
140146
...
@@ -145,7 +151,7 @@ LighterSystem:
145151
...
146152
```
147153

148-
You can also define a scheduler for the optimizer as below,
154+
You can also define a scheduler for the optimizer as shown below:
149155
```yaml hl_lines="10"
150156
LighterSystem:
151157
...
@@ -162,12 +168,12 @@ LighterSystem:
162168
163169
...
164170
```
165-
Here, the optimizer is passed to the scheduler with the `optimizer` argument. `%trainer#max_epochs` is also passed to the scheduler where it fetches `max_epochs` from the Trainer class.
171+
In this example, the optimizer is passed to the scheduler using the `optimizer` argument. The `%trainer#max_epochs` syntax retrieves the `max_epochs` value from the Trainer class.
166172

167173
<br/>
168174
#### Datasets
169175

170-
The most commonly changed part of the config is often the datasets as common workflows involve training/inferring on your own dataset. We provide a `datasets` key with `train`, `val`, `test` and `predict` keys that generate dataloaders for each of the different workflows provided by pytorch lightning. These are described in detail [here](./workflows.md)
176+
Datasets are often the most frequently modified part of the configuration, as workflows typically involve training or inferring on custom datasets. The `datasets` key includes `train`, `val`, `test`, and `predict` sub-keys, which generate dataloaders for each workflow supported by PyTorch Lightning. Detailed information is available [here](./workflows.md).
171177

172178
<div class="annotate" markdown>
173179

@@ -191,11 +197,11 @@ LighterSystem:
191197
```
192198

193199
</div>
194-
1. Define your own dataset class here or use several existing dataset clases. Read more about [this](./projects.md)
195-
2. Transforms can be applied to each element of the dataset by initialization a `Compose` object and providing it a list of transforms. This is often the best way to adapt constraints for your data.
200+
1. Define your own dataset class here or use existing dataset classes. Learn more about this [here](./projects.md).
201+
2. Transforms can be applied to each dataset element by initializing a `Compose` object and providing a list of transforms. This approach is often the best way to adapt constraints for your data.
196202

197203
### Special Syntax and Keywords
198-
- `_target_`: Indicates the Python class to instantiate. If a function is provided, a partial function is created. Any configuration key set with `_target_` will map to a python object.
199-
- **@**: References another configuration value. Using this syntax, keys mapped to python objects can be accessed. For instance, the learning rate of an optimizer, `optimizer` instianted to `torch.optim.Adam` using `_target_` can be accessed using `@model#lr` where `lr` is an attribute of the `torch.optim.Adam` class.
200-
- **$**: Used for evaluating Python expressions.
201-
- **%**: Macro for textual replacement in the configuration.
204+
- `_target_`: Specifies the Python class to instantiate. If a function is provided, a partial function is created. Any configuration key with `_target_` maps to a Python object.
205+
- **@**: References another configuration value. This syntax allows access to keys mapped to Python objects. For example, the learning rate of an optimizer instantiated as `torch.optim.Adam` can be accessed using `@model#lr`, where `lr` is an attribute of the `torch.optim.Adam` class.
206+
- **$**: Evaluates Python expressions.
207+
- **%**: Acts as a macro for textual replacement in the configuration.

docs/basics/projects.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# Using Lighter in your own projects
22

3-
With Lighter, you can be as hands-on as you wish when using it in your project. For example, you can use a pre-defined Lighter configuration and,
3+
Lighter offers a flexible framework for integrating deep learning workflows into your projects. Whether you're starting with a pre-defined configuration or building a custom setup, Lighter adapts to your needs. Here’s how you can leverage Lighter:
44

55
- [x] Train on your own dataset
66
- [x] Train on your data + Add a custom model architecture
77
- [x] Train on your data + Add a custom model architecture + Add a complex loss function
88
- [x] Customization per your imagination!
99

10-
Lets start by looking at each of these one by one. At the end of this, you will hopefully have a better idea of how best you can leverage lighter
10+
Let's start by looking at each of these one by one. At the end of this, you will hopefully have a better idea of how best you can leverage Lighter.
1111

1212
### Training on your own dataset
1313

14-
If you are reproducing another study you often start with a pre-defined configuration. Let us take the case of `cifar10.yaml` shown in [Quickstart](./quickstart.md). You have a dataset of Chest X-rays that you want to use to reproduce the same training that was done on CIFAR10. With lighter, all you need to change is the highlighted sections.
14+
When reproducing a study or adapting a model to new data, you often start with a pre-defined configuration. For instance, consider the `cifar10.yaml` example from our [Quickstart](./quickstart.md). Suppose you have a dataset of Chest X-rays and wish to replicate the training process used for CIFAR10. With Lighter, you only need to modify specific sections of the configuration.
1515

1616
```yaml title="cifar10.yaml" hl_lines="18-29"
1717
system:
@@ -45,7 +45,7 @@ system:
4545
std: [0.5, 0.5, 0.5]
4646
```
4747
48-
To replace this with your own dataset, you can create a Pytorch dataset that produces images and targets in the same format as torchvision datasets, i.e (image, target) tuple.
48+
To integrate your dataset, create a PyTorch dataset class that outputs a dictionary with `input`, `target`, and optionally `id` keys. This ensures compatibility with Lighter's configuration system.
4949

5050
```py title="/home/user/project/my_xray_dataset.py"
5151
class MyXRayDataset(Dataset):
@@ -80,20 +80,19 @@ class MyXRayDataset(Dataset):
8080
img_name = os.path.join(self.root_dir, self.annotations.iloc[idx, 0])
8181
image = Image.open(img_name)
8282
target = self.annotations.iloc[idx, 1]
83-
sample = {'image': image, 'target': target}
83+
sample = {'input': image, 'target': target}
8484
8585
if self.transform:
86-
sample['image'] = self.transform(sample['image'])
86+
sample['input'] = self.transform(sample['input'])
8787
88-
return sample['image'], sample['target']
88+
return sample
8989
9090
```
9191

92-
!!! note
93-
Lighter works with the default torchvision format of (image, target) and also with `dict` with `input` and `target` keys. The input/target key or tuple can contain complex input/target organization, e.g. multiple images for input and multiple labels for target
92+
> **Note:** Lighter requires the dataset to return a dictionary with `input`, `target`, and optionally `id` keys. This format allows for complex input/target structures, such as multiple images or labels.
9493

9594

96-
Now that you have built your dataset, all you need to do is add it to the lighter config! But wait, how will Lighter know where your code is? All lighter configs contain a `project` key that takes the full path to where your python code is located. Once you set this up, call `project.my_xray_dataset.` and Lighter will pick up the dataset.
95+
Once your dataset is ready, integrate it into the Lighter configuration. The `project` key in the config specifies the path to your Python code, allowing Lighter to locate and utilize your dataset. Simply reference your dataset class, and Lighter will handle the rest.
9796

9897
In the above example, the path of the dataset is `/home/user/project/my_xray_dataset.py`. Copy the config shown above, make the following changes and run on the terminal
9998

0 commit comments

Comments
 (0)