Skip to content

Commit 6a8b05c

Browse files
committed
Refactor LitModule and improve logging structure
1 parent 1bcf902 commit 6a8b05c

File tree

17 files changed

+120
-146
lines changed

17 files changed

+120
-146
lines changed

README.md

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# ***[TmPL](): [T]()e[mpl]()ate for [P]()ytorch [L]()ightning***
22

3-
![](https://img.shields.io/badge/Python-3.8%2B-blue)
4-
![](https://img.shields.io/badge/PyTorch-1.11%2B-red)
5-
![](https://img.shields.io/badge/Lightning-2.0-blue)
3+
![](https://img.shields.io/badge/Python-3.9%2B-blue)
4+
![](https://img.shields.io/badge/PyTorch-2.1%2B-red)
5+
![](https://img.shields.io/badge/Lightning-2.5-blue)
66
![](https://img.shields.io/badge/Hydra-1.3-lightgrey)
77

88
[![](https://img.shields.io/github/license/npurson/tmpl)](LICENSE)
9-
![](https://img.shields.io/badge/version-v2.0-blue)
9+
![](https://img.shields.io/badge/version-v2.1-blue)
1010

1111
[Lightning Docs](https://lightning.ai/docs/pytorch/stable/)   
1212
[Installation](#installation)   
@@ -15,44 +15,41 @@
1515
[Contributing](#contributing)   
1616
[License](#license)
1717

18-
A template for rapid & flexible DL experimentation development, built upon [Lightning](https://lightning.ai/) & [Hydra](https://hydra.cc/) with best practice.
18+
A template for rapid & flexible DL experimentation development, powered by [Lightning](https://lightning.ai/) & [Hydra](https://hydra.cc/) following best practice.
1919

20-
## What's New
21-
22-
***v2.0*** was released on Sep 5 '23.
20+
<div align="center">
21+
<img src="assets/meme.png" width="256" height="256">
22+
</div>
2323

2424
## Installation
2525

2626
```
2727
pip install -r requirements.txt
2828
```
2929

30-
It is recommended to manually install PyTorch and Torchvision before running the installation command, referring to the official PyTorch website for [instructions](https://pytorch.org/get-started/locally/).
31-
3230
## Usage
3331

3432
0. **Setup**
3533

36-
```shell
34+
```bash
3735
export PYTHONPATH=`pwd`:$PYTHONPATH
3836
```
3937

4038
1. **Training**
4139

42-
```shell
40+
```bash
4341
python tools/train.py [--config-name config[.yaml]] [trainer.devices=4] [data.loader.batch_size=16]
4442
```
4543
46-
* Override the default config file with `--config-name`.
47-
* You can also override any value in the loaded config from the command line, refer to the following for more infomation.
48-
* https://hydra.cc/docs/tutorials/basic/your_first_app/config_file/
44+
* Specify the configuration file using `--config-name`.
45+
* Refer to the following for detailed information on Hydra's override syntax.
4946
* https://hydra.cc/docs/advanced/hydra-command-line-flags/
5047
* https://hydra.cc/docs/advanced/override_grammar/basic/
5148
5249
2. **Tips for Further Development**
5350
54-
The code is designed to be flexible and customizable to meet your specific needs. \
55-
Useful comments can be found in the source code.
51+
This framework is designed for flexibility and easy customization to meet users' specific needs.
52+
Useful comments and details on extending the framework can be found within the source code files.
5653
5754
## Reference
5855

assets/meme.png

1.65 MB
Loading

configs/config.yaml

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,19 @@
11
# Refer to Hydra documentation for more information about config group defaults.
22
# - https://hydra.cc/docs/tutorials/basic/your_first_app/defaults/
33
# - https://hydra.cc/docs/patterns/configuring_experiments/
4-
54
defaults:
65
- datasets: cifar10
76
- models: resnet50
87
- schedules: 10e
98
- _self_
109

1110
hydra:
12-
mode: MULTIRUN # refer to https://github.com/Lightning-AI/lightning/pull/11617
13-
sweep:
14-
dir: outputs/${now:%Y-%m-%d}/${now:%H-%M}
11+
run:
12+
dir: outputs/${now:%Y%m%d-%H%M%S}
1513

16-
trainer:
17-
# num_nodes: 1
14+
trainer: # refer to https://lightning.ai/docs/pytorch/stable/common/trainer.html
1815
devices: 4
19-
accelerator: gpu
20-
strategy: ddp
21-
sync_batchnorm: True
2216
precision: 16-mixed
23-
24-
# Refer to https://lightning.ai/docs/pytorch/latest/common/trainer.html for more infomation.
25-
# check_val_every_n_epoch: 1
26-
# log_every_n_steps: 50
27-
# enable_progress_bar: False
28-
# profiler: simple # profiling measures the time consuming of all components
29-
30-
# TODO: Build callbacks before passed to trainer.__init__().
31-
# callbacks:
32-
# GradientAccumulationScheduler(scheduling={4: 2})
17+
sync_batchnorm: True
18+
# enable_progress_bar: False # will log to console and save to file (customized in build_callbacks())
19+
# profiler: simple

configs/datasets/cifar10.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ data:
1111
root: data/cifar-10
1212
download: True
1313
loader:
14-
batch_size: 32 # batch size per GPU
14+
batch_size: 32
1515
num_workers: 4
1616

1717
model:

configs/datasets/cifar100.yaml

Lines changed: 0 additions & 23 deletions
This file was deleted.

configs/datasets/mnist.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ data:
55
type: MNIST
66
splits:
77
train:
8-
download: True
8+
train: True
99
test:
1010
train: False
11-
root: datasets/mnist
11+
root: data/mnist
1212
download: True
1313
loader:
1414
batch_size: 32

configs/schedules/10e.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
optimizer:
44
type: AdamW
55
lr: 1.0e-3
6-
weight_decay: 1.0e-5
6+
weight_decay: 1.0e-4
77

88
scheduler:
99
type: CosineAnnealingLR

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ torch
22
torchvision
33
lightning
44
hydra-core
5+
rich
56
tensorboard

tmpl/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .utils import build_from_configs, pre_build_callbacks
1+
from .utils import build_callbacks, build_from_configs # It has to be imported first
22
from .data import build_data_loaders
33
from .engine import LitModule

tmpl/data/build.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from omegaconf import DictConfig, OmegaConf
1+
from omegaconf import DictConfig, ListConfig, OmegaConf, open_dict
22
from torch.utils.data import DataLoader
33
from torchvision import transforms as T
44

@@ -7,16 +7,18 @@
77

88

99
def build_data_loaders(cfg: DictConfig):
10-
cfg = cfg.copy()
1110
if isinstance(cfg, DictConfig):
12-
OmegaConf.set_struct(cfg, False)
13-
split_cfgs = cfg.datasets.pop('splits')
11+
with open_dict(cfg):
12+
split_cfgs = cfg.datasets.pop('splits')
13+
if isinstance(split_cfgs, ListConfig):
14+
split_cfgs = {split: {'split': split} for split in split_cfgs}
1415

15-
if cfg.datasets.type in ('CIFAR10', 'CIFAR100', 'MNIST'):
16-
print(f'It seems you are using the {cfg.datasets.type} dataset from our demo config, '
17-
'we automatically add ToTensor() to the pipeline only for demo. It is usually '
18-
'unnecessary for your own dataset and you can modify this part as per your '
19-
'requirements.')
16+
if cfg.datasets.type in ('CIFAR10', 'MNIST'):
17+
print(
18+
f'NOTE: For demonstration using standard torchvision.datasets like {cfg.datasets.type}, '
19+
'we are manually adding ToTensor() here to ensure the pipeline is runnable. '
20+
'In a typical use case with your own dataset, you should generally handle it elsewhere.'
21+
)
2022
split_cfgs = OmegaConf.to_container(split_cfgs)
2123
for s in split_cfgs:
2224
split_cfgs[s]['transform'] = T.ToTensor()

0 commit comments

Comments
 (0)