-
Notifications
You must be signed in to change notification settings - Fork 9
/
train.py
87 lines (70 loc) · 2.26 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import torch
from torchvision import transforms, datasets
from trainer.Trainer import Trainer
from torch.utils.tensorboard import SummaryWriter
from models.loss import PixWiseBCELoss
from datasets.PixWiseDataset import PixWiseDataset
from utils.utils import read_cfg, get_optimizer, build_network, get_device
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID" # see issue #152
os.environ["CUDA_VISIBLE_DEVICES"]="1"
cfg = read_cfg(cfg_file='config/densenet_161_adam_lr1e-3.yaml')
device = get_device(cfg)
network = build_network(cfg)
optimizer = get_optimizer(cfg, network)
loss = PixWiseBCELoss(beta=cfg['train']['loss']['beta'])
writer = SummaryWriter(cfg['log_dir'])
dump_input = torch.randn(1,3,224,224)
writer.add_graph(network, (dump_input, ))
# Without Resize transform, images are of different sizes and it causes an error
train_transform = transforms.Compose([
transforms.Resize(cfg['model']['image_size']),
transforms.RandomRotation(cfg['dataset']['augmentation']['rotation']),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(cfg['dataset']['mean'], cfg['dataset']['sigma'])
])
test_transform = transforms.Compose([
transforms.Resize(cfg['model']['image_size']),
transforms.ToTensor(),
transforms.Normalize(cfg['dataset']['mean'], cfg['dataset']['sigma'])
])
trainset = PixWiseDataset(
root_dir=cfg['dataset']['root'],
csv_file=cfg['dataset']['train_set'],
map_size=cfg['model']['map_size'],
transform=train_transform,
smoothing=cfg['model']['smoothing']
)
testset = PixWiseDataset(
root_dir=cfg['dataset']['root'],
csv_file=cfg['dataset']['test_set'],
map_size=cfg['model']['map_size'],
transform=test_transform,
smoothing=cfg['model']['smoothing']
)
trainloader = torch.utils.data.DataLoader(
dataset=trainset,
batch_size=cfg['train']['batch_size'],
shuffle=True,
num_workers=0
)
testloader = torch.utils.data.DataLoader(
dataset=testset,
batch_size=cfg['test']['batch_size'],
shuffle=True,
num_workers=0
)
trainer = Trainer(
cfg=cfg,
network=network,
optimizer=optimizer,
loss=loss,
lr_scheduler=None,
device=device,
trainloader=trainloader,
testloader=testloader,
writer=writer
)
trainer.train()
writer.close()