-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
73 lines (59 loc) · 2.17 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from pathlib import Path
from torch.utils.data import DataLoader
from utils import BlobData
from models import DCGAN
from pytorch_lightning import Trainer, Callback
from pytorch_lightning.loggers import WandbLogger
from pytorch_lightning.callbacks import ModelCheckpoint
## https://github.com/PyTorchLightning/pytorch-lightning/issues/2534
class CheckpointEveryNSteps(Callback):
"""
Save a checkpoint every N steps, instead of Lightning's default that checkpoints
based on validation loss.
"""
def __init__(self, save_every: int):
self.save_every = save_every
def on_batch_end(self, trainer: Trainer, _):
""" Check if we should save a checkpoint after every train batch """
epoch = trainer.current_epoch
global_step = trainer.global_step
if global_step % self.save_every == 0:
filename = f'{epoch=}_{global_step=}.ckpt'
ckpt_path = Path(trainer.checkpoint_callback.dirpath, filename)
trainer.save_checkpoint(ckpt_path)
if __name__ == '__main__':
mode = '1' ## ['1', 'RGB']
path = Path().resolve()
train_path = path / 'data' / mode / 'train'
mode_channels = {'1':1, 'RGB':3}
z_channels = 100
h_channels = 64
img_channels = mode_channels[mode]
lr = 0.0002
batch_size = 256
## Defining model
dcgan = DCGAN(
z_channels=z_channels, h_channels=h_channels,
img_channels=img_channels, lr=lr,
save_every=200)
## Creating dataset object
train = BlobData(train_path, img_channels)
print(f'Number of training examples is {len(train)}')
train = DataLoader(
train, batch_size=batch_size,
num_workers=4, shuffle=True)
## Making logger for tracking stats on wandb.com
logger = WandbLogger(
project='BlobGAN',
name=f'img={img_channels}-h={h_channels}-z={z_channels}-lr={lr}-batch={batch_size}',
log_model=False)
## Creating trainer object
callback = CheckpointEveryNSteps(dcgan.save_every)
trainer = Trainer(
gpus=1,
max_epochs=2,
logger=logger,
log_every_n_steps=1,
callbacks=[callback])
## Fitting model
trainer.fit(dcgan, train)