-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
167 lines (145 loc) · 5.37 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# %%
from hydra.utils import instantiate
import torchvision
import albumentations as A
import hydra
from omegaconf import DictConfig, OmegaConf
from bioimage_embed.models.bolts import ResNet18VAEEncoder, ResNet18VAEDecoder
from pythae.models import VAE, VAEConfig
import torch
from bioimage_embed.datasets import DatasetGlob
import pytorch_lightning as pl
# @hydra.main(config_path="conf", config_name="config", version_base="1.2")
# def main(cfg: DictConfig) -> None:
# # OmegaConf.resolve(cfg)
# # cli = LightningCLI(Bio_VAE)
# seed_everything(cfg.seed)
# # args = cfg.timm
# # transform = A.from_dict(OmegaConf.to_container(cfg.albumentations, resolve=True))
# transform = A.Compose(
# [
# A.RandomCrop(width=256, height=256, p=1.0),
# A.Resize(width=224, height=224, p=1.0),
# A.ToFloat(max_value=1, p=1.0),
# ToTensorV2(),
# ]
# )
# # dataset = instantiate(cfg.dataset)
# # pythae = instantiate(cfg.pythae)
# model_config = VAEConfig(
# latent_dim=64,
# input_dim=(3, 64, 64),
# )
# encoder = ResNet18VAEEncoder(model_config)
# decoder = ResNet18VAEDecoder(model_config)
# pythae = VAE(
# model_config=model_config,
# encoder=encoder,
# decoder=decoder,
# )
# # model = instantiate(cfg.model)
# # dataloader = instantiate(cfg.dataloader, transform=transform)
# # dataloader.setup()
# data = DatasetGlob(
# "/home/ctr26/gdrive/+projects/idr_autoencode_torch/data/ivy_gap/random/*png",
# transform=transform,
# )
# dataloader = torch.utils.data.DataLoader(data, batch_size=4, num_workers=4)
# trainer = pl.Trainer(
# gpus=1, max_epochs=100, precision=16
# )
# lightning = LitAutoEncoderTorch(pythae,args=None)
# # test_data = dataloader.get_dataset()[0].unsqueeze(dim=0)
# # test = pythae({"data": test_data[:, :, :64, :64]})
# # model = instantiate(cfg.model)
# # pythae = instantiate(cfg.pythae)
# # data = DatasetGlob(
# # "/home/ctr26/gdrive/+projects/idr_autoencode_torch/data/ivy_gap/random/*png",
# # transform=transform,
# # )
# # dataloader = torch.utils.data.DataLoader(data, batch_size=4, num_workers=4)
# # lightning = instantiate(cfg.lightning, model=pythae)
# # logger = instantiate(cfg.logger)
# # checkpoint_callback = instantiate(cfg.checkpoints)
# # trainer = instantiate(cfg.trainer)
# # trainer = pl.Trainer(gpus=1, max_epochs=100, precision=16)
# try:
# trainer.fit(
# lightning,
# datamodule=dataloader,
# ckpt_path=f"{cfg.checkpoints.dirpath}/last.ckpt",
# )
# except:
# trainer.fit(lightning, datamodule=dataloader)
@hydra.main(config_path="conf", config_name="config", version_base="1.2")
def main(cfg: DictConfig) -> None:
model_config = VAEConfig(
latent_dim=int(224),
input_dim=(3, int(224), int(224)),
)
# transform = A.Compose(
# [
# # A.RandomCrop(width=256, height=256, p=1.0),
# A.Resize(width=int(224), height=int(224), p=1.0),
# A.ToFloat(max_value=1, p=1.0),
# ToTensorV2(),
# ]
# )
transform = A.from_dict(OmegaConf.to_container(cfg.albumentations, resolve=True))
data = DatasetGlob(
"data/ivy_gap/random/*png",
transform=transform,
)
# train_loader = torch.utils.data.DataLoader(data, batch_size=4, num_workers=4)
encoder = ResNet18VAEEncoder(model_config)
decoder = ResNet18VAEDecoder(model_config)
model = VAE(
model_config=model_config,
encoder=encoder,
decoder=decoder,
)
class VAEModel(pl.LightningModule):
def __init__(self, model):
super().__init__()
self.model = model
self.model = self.model.to(self.device)
def forward(self, x):
return self.model({"data": x})
def training_step(self, batch, batch_idx):
self.model.train()
x = {"data": batch}
model_output = self.model(x, epoch=batch_idx)
self.loss = model_output.loss
self.logger.experiment.add_scalar("Loss/train", self.loss, batch_idx)
self.logger.experiment.add_image(
"input", torchvision.utils.make_grid(batch), batch_idx
)
self.logger.experiment.add_image(
"output",
torchvision.utils.make_grid(model_output["recon_x"]),
batch_idx,
)
self.log("train_loss", self.loss)
return self.loss
def configure_optimizers(self):
return torch.optim.Adam(self.parameters(), lr=1e-3)
# vae_model = VAEModel(model)
# model = instantiate(cfg.pythae)
lightning_model = instantiate(cfg.lightning, model=model)
# train_loader = torch.utils.data.DataLoader(data, batch_size=1, num_workers=4)
train_loader = instantiate(cfg.dataloader, transform=transform)
trainer = instantiate(cfg.trainer)
# trainer = pl.Trainer(
# devices="auto",
# accelerator="gpu",
# max_epochs=100,
# precision=16,
# logger=TensorBoardLogger(save_dir="logs/"),
# accumulate_grad_batches=1,
# )
trainer.fit(
lightning_model,
train_loader,
)
if __name__ == "__main__":
main()