-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
66 lines (52 loc) · 1.61 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
import hydra
import torch
from hydra.core.config_store import ConfigStore
from omegaconf import OmegaConf
from config import ConvNetConfig
from ds.dataset import create_dataloader, remove_bed_images
from ds.models import ConvNet
from ds.runner import train
# Device
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
cs = ConfigStore.instance()
cs.store(name="ConfNet_config", node=ConvNetConfig)
@hydra.main(config_path="conf", config_name="config")
def main(cfg: ConvNetConfig):
print(OmegaConf.to_yaml(cfg))
# Model and Optimizer
model_name = "ConvNet"
model = ConvNet().to(device)
optimizer = torch.optim.SGD(
model.parameters(), lr=cfg.params.lr, momentum=cfg.params.momentum
)
criterion = torch.nn.CrossEntropyLoss()
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(
optimizer=optimizer, T_max=cfg.params.epoch_count
)
# Remove bad images
remove_bed_images(cfg.paths.data)
# Create the data loaders
train_loader, test_loader = create_dataloader(
root_path=cfg.paths.data,
batch_size=cfg.params.batch_size,
load_to_ram=False,
pin_memory=True,
num_workers=2,
)
# Run epochs
train(
model,
optimizer,
scheduler,
criterion,
train_loader,
test_loader,
num_epochs=cfg.params.epoch_count,
device=device,
title=model_name,
)
# Save model and optimizer
torch.save(model.state_dict(), "weights/model.pt")
torch.save(optimizer.state_dict(), "weights/optimizer.pt")
if __name__ == "__main__":
main()