forked from abdalazizrashid/idao-21-baseline
-
Notifications
You must be signed in to change notification settings - Fork 12
/
train.py
69 lines (54 loc) · 1.91 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import configparser
import pathlib as path
import numpy as np
import pytorch_lightning as pl
from pytorch_lightning import seed_everything
from idao.data_module import IDAODataModule
from idao.model import SimpleConv
def get_free_gpu():
"""
Returns the index of the GPU with the most free memory.
Different from lightning's auto_select_gpus, as it provides the most free GPU, not an absolutely free.
"""
from pynvml import nvmlInit, nvmlDeviceGetHandleByIndex, nvmlDeviceGetMemoryInfo, nvmlDeviceGetCount
nvmlInit()
return np.argmax([
nvmlDeviceGetMemoryInfo(nvmlDeviceGetHandleByIndex(i)).free
for i in range(nvmlDeviceGetCount())
])
def trainer(mode: ["classification", "regression"], cfg, dataset_dm):
model = SimpleConv(mode=mode)
if cfg.getboolean("TRAINING", "UseGPU"):
gpus = [get_free_gpu()]
else:
gpus = None
if mode == "classification":
epochs = cfg["TRAINING"]["ClassificationEpochs"]
else:
epochs = cfg["TRAINING"]["RegressionEpochs"]
trainer = pl.Trainer(
gpus=gpus,
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)
def main():
seed_everything(666)
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"]:
print(f"Training for {mode}")
trainer(mode, cfg=config, dataset_dm=dataset_dm)
if __name__ == "__main__":
main()