-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtrain.py
48 lines (37 loc) · 1.3 KB
/
train.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
import configparser
import pathlib as path
import pytorch_lightning as pl
from pytorch_lightning import seed_everything
from idao.data_module import IDAODataModule
from idao.model import SimpleConv
seed_everything(666)
def trainer(mode: ["classification", "regression"], cfg):
# init model
model = SimpleConv(mode=mode)
if mode == "classification":
epochs = cfg["TRAINING"]["ClassificationEpochs"]
else:
epochs = cfg["TRAINING"]["RegressionEpochs"]
# Initialize a trainer
trainer = pl.Trainer(
gpus=int(cfg["TRAINING"]["NumGPUs"]),
max_epochs=int(epochs),
progress_bar_refresh_rate=20,
weights_save_path=path.Path(cfg["TRAINING"]["ModelParamsSavePath"]).joinpath(
mode
),
default_root_dir=path.Path(cfg["TRAINING"]["ModelParamsSavePath"]),
)
# Train the model ⚡
trainer.fit(model, dataset_dm)
if __name__ == "__main__":
config = configparser.ConfigParser()
config.read("./config.ini")
PATH = path.Path(config["DATA"]["DatasetPath"])
dataset_dm = IDAODataModule(
data_dir=PATH, batch_size=int(config["TRAINING"]["BatchSize"]), cfg=config
)
dataset_dm.prepare_data()
dataset_dm.setup()
for mode in ["classification", "regression"]:
trainer(mode, cfg=config)