-
Notifications
You must be signed in to change notification settings - Fork 20
/
main.py
61 lines (50 loc) · 1.62 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
import sys
import torch
from config import Config
from dataset import create_wf_datasets, my_collate_fn
from model import Net
from trainer import Trainer
from voc_dataset import create_voc_datasets
def main():
if Config.DATASETS == 'VOC':
train_dataset, val_dataset = create_voc_datasets(Config.VOC_DATASET_DIR)
elif Config.DATASETS == 'WF':
train_dataset, val_dataset = create_wf_datasets(Config.WF_DATASET_DIR)
else:
raise RuntimeError('Select a dataset to train in config.py.')
train_dataloader = torch.utils.data.DataLoader(
train_dataset,
batch_size=Config.BATCH_SIZE,
num_workers=Config.DATALOADER_WORKER_NUM,
shuffle=True,
collate_fn=my_collate_fn
)
val_dataloader = torch.utils.data.DataLoader(
val_dataset,
batch_size=1,
num_workers=Config.DATALOADER_WORKER_NUM,
shuffle=False,
collate_fn=my_collate_fn
)
model = Net()
# optimizer = torch.optim.Adam(
# params=model.parameters(),
# lr=Config.LEARNING_RATE, weight_decay=Config.WEIGHT_DECAY
# )
optimizer = torch.optim.SGD(model.parameters(), lr=Config.LEARNING_RATE,
weight_decay=Config.WEIGHT_DECAY)
trainer = Trainer(
optimizer,
model,
train_dataloader,
val_dataloader,
resume=Config.RESUME_FROM,
log_dir=Config.LOG_DIR,
persist_stride=Config.MODEL_SAVE_STRIDE,
max_epoch=Config.EPOCHS
)
trainer.train()
if __name__ == "__main__":
torch.backends.cudnn.benchmark = True
torch.set_default_tensor_type('torch.FloatTensor')
main()