-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
149 lines (123 loc) · 4.87 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import torch
import os
import sys
import visdom
import torch.backends.cudnn as cudnn
import dataset.transforms as T
from models.detr import DETR
from dataset.coco_dataset import COCO_Dataset
from config import device, device_ids, parse
from losses.hungarian_loss import HungarianLoss
from losses.matcher import HungarianMatcher
from train import train
from test import test
from parallel import DataParallelModel, DataParallelCriterion
cudnn.benchmark = True
def main():
# 1. configuration
opts = parse(sys.argv[1:])
# 2. visdom
if opts.visdom:
vis = visdom.Visdom(port=opts.port)
else:
vis = None
# 3. dataset
normalize = T.Compose([
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
scales = [480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800]
# transforms_train #
transforms_train = T.Compose([
T.RandomSelect(
T.RandomResize(scales, max_size=1333),
T.Compose([
T.RandomResize([400, 500, 600]),
T.RandomSizeCrop(384, 600),
T.RandomResize(scales, max_size=1333),
])
),
T.RandomResize([800], max_size=800),
normalize,
])
# transforms_val #
transforms_val = T.Compose([
T.RandomResize([800], max_size=800),
normalize,
])
train_set = COCO_Dataset(root=opts.data_root,
split='train',
download=True,
transforms=transforms_train,
visualization=False)
test_set = COCO_Dataset(root=opts.data_root,
split='val',
download=True,
transforms=transforms_val,
visualization=False)
# 4. dataloader
train_loader = torch.utils.data.DataLoader(train_set,
batch_size=opts.batch_size,
collate_fn=train_set.collate_fn,
shuffle=True,
num_workers=0,
pin_memory=True)
test_loader = torch.utils.data.DataLoader(test_set,
batch_size=1,
collate_fn=test_set.collate_fn,
shuffle=False,
num_workers=0,
pin_memory=True)
# 5. model (opts.num_classes = 91)
model = DETR(num_classes=opts.num_classes, num_queries=100)
if opts.distributed:
model = torch.nn.DataParallel(model)
model = model.cuda()
# 6. criterion
matcher = HungarianMatcher()
criterion = HungarianLoss(num_classes=opts.num_classes, matcher=matcher)
criterion.cuda()
# 7. optimizer
param_dicts = [
{"params": [p for n, p in model.named_parameters() if "backbone" not in n and p.requires_grad]},
{
"params": [p for n, p in model.named_parameters() if "backbone" in n and p.requires_grad],
"lr": opts.lr_backbone,
},
]
optimizer = torch.optim.AdamW(param_dicts,
lr=opts.lr,
weight_decay=opts.weight_decay)
# 8. scheduler
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=200, gamma=0.1)
# 9. resume
if opts.start_epoch != 0:
checkpoint = torch.load(os.path.join(opts.save_path, opts.save_file_name) + '.{}.pth.tar'
.format(opts.start_epoch - 1),
map_location=torch.device('cuda:{}'.format(0)))
model.load_state_dict(checkpoint['model_state_dict']) # load model state dict
optimizer.load_state_dict(checkpoint['optimizer_state_dict']) # load optimization state dict
scheduler.load_state_dict(checkpoint['scheduler_state_dict']) # load scheduler state dict
print('\nLoaded checkpoint from epoch %d.\n' % (int(opts.start_epoch) - 1))
else:
print('\nNo check point to resume.. train from scratch.\n')
for epoch in range(opts.start_epoch, opts.epoch):
# 10. train
train(epoch=epoch,
vis=vis,
train_loader=train_loader,
model=model,
criterion=criterion,
optimizer=optimizer,
scheduler=scheduler,
opts=opts)
# 11. test
test(epoch=epoch,
vis=vis,
test_loader=test_loader,
model=model,
criterion=criterion,
opts=opts,
visualize=False)
if __name__ == "__main__":
main()