-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtrain.py
222 lines (192 loc) · 7.15 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from models.loca import build_model
from utils.data import FSC147Dataset
from utils.arg_parser import get_argparser
from utils.losses import ObjectNormalizedL2Loss
from time import perf_counter
import argparse
import os
import torch
from torch import nn
from torch.utils.data import DataLoader, DistributedSampler
from torch.nn.parallel import DistributedDataParallel
from torch import distributed as dist
import numpy as np
import random
torch.manual_seed(0)
random.seed(0)
np.random.seed(0)
def train(args):
if 'SLURM_PROCID' in os.environ:
world_size = int(os.environ['SLURM_NTASKS'])
rank = int(os.environ['SLURM_PROCID'])
gpu = rank % torch.cuda.device_count()
print("Running on SLURM", world_size, rank, gpu)
else:
world_size = int(os.environ['WORLD_SIZE'])
rank = int(os.environ['RANK'])
gpu = int(os.environ['LOCAL_RANK'])
torch.cuda.set_device(gpu)
device = torch.device(gpu)
dist.init_process_group(
backend='nccl', init_method='env://',
world_size=world_size, rank=rank
)
model = DistributedDataParallel(
build_model(args).to(device),
device_ids=[gpu],
output_device=gpu
)
backbone_params = dict()
non_backbone_params = dict()
for n, p in model.named_parameters():
if not p.requires_grad:
continue
if 'backbone' in n:
backbone_params[n] = p
else:
non_backbone_params[n] = p
optimizer = torch.optim.AdamW(
[
{'params': non_backbone_params.values()},
{'params': backbone_params.values(), 'lr': args.backbone_lr}
],
lr=args.lr,
weight_decay=args.weight_decay,
)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, args.lr_drop, gamma=0.25)
if args.resume_training:
checkpoint = torch.load(os.path.join(args.model_path, f'{args.model_name}.pt'))
model.load_state_dict(checkpoint['model'])
start_epoch = checkpoint['epoch']
best = checkpoint['best_val_ae']
optimizer.load_state_dict(checkpoint['optimizer'])
scheduler.load_state_dict(checkpoint['scheduler'])
else:
start_epoch = 0
best = 10000000000000
criterion = ObjectNormalizedL2Loss()
train = FSC147Dataset(
args.data_path,
args.image_size,
split='train',
num_objects=args.num_objects,
tiling_p=args.tiling_p,
zero_shot=args.zero_shot
)
val = FSC147Dataset(
args.data_path,
args.image_size,
split='val',
num_objects=args.num_objects,
tiling_p=args.tiling_p
)
train_loader = DataLoader(
train,
sampler=DistributedSampler(train),
batch_size=args.batch_size,
drop_last=True,
num_workers=args.num_workers
)
val_loader = DataLoader(
val,
sampler=DistributedSampler(val),
batch_size=args.batch_size,
drop_last=False,
num_workers=args.num_workers
)
print(rank)
for epoch in range(start_epoch + 1, args.epochs + 1):
if rank == 0:
start = perf_counter()
train_loss = torch.tensor(0.0).to(device)
val_loss = torch.tensor(0.0).to(device)
aux_train_loss = torch.tensor(0.0).to(device)
aux_val_loss = torch.tensor(0.0).to(device)
train_ae = torch.tensor(0.0).to(device)
val_ae = torch.tensor(0.0).to(device)
train_loader.sampler.set_epoch(epoch)
model.train()
for img, bboxes, density_map in train_loader:
img = img.to(device)
bboxes = bboxes.to(device)
density_map = density_map.to(device)
optimizer.zero_grad()
out, aux_out = model(img, bboxes)
# obtain the number of objects in batch
with torch.no_grad():
num_objects = density_map.sum()
dist.all_reduce_multigpu([num_objects])
main_loss = criterion(out, density_map, num_objects)
aux_loss = sum([
args.aux_weight * criterion(aux, density_map, num_objects) for aux in aux_out
])
loss = main_loss + aux_loss
loss.backward()
if args.max_grad_norm > 0:
nn.utils.clip_grad_norm_(model.parameters(), args.max_grad_norm)
optimizer.step()
train_loss += main_loss * img.size(0)
aux_train_loss += aux_loss * img.size(0)
train_ae += torch.abs(
density_map.flatten(1).sum(dim=1) - out.flatten(1).sum(dim=1)
).sum()
model.eval()
with torch.no_grad():
for img, bboxes, density_map in val_loader:
img = img.to(device)
bboxes = bboxes.to(device)
density_map = density_map.to(device)
out, aux_out = model(img, bboxes)
with torch.no_grad():
num_objects = density_map.sum()
dist.all_reduce_multigpu([num_objects])
main_loss = criterion(out, density_map, num_objects)
aux_loss = sum([
args.aux_weight * criterion(aux, density_map, num_objects) for aux in aux_out
])
loss = main_loss + aux_loss
val_loss += main_loss * img.size(0)
aux_val_loss += aux_loss * img.size(0)
val_ae += torch.abs(
density_map.flatten(1).sum(dim=1) - out.flatten(1).sum(dim=1)
).sum()
dist.all_reduce_multigpu([train_loss])
dist.all_reduce_multigpu([val_loss])
dist.all_reduce_multigpu([aux_train_loss])
dist.all_reduce_multigpu([aux_val_loss])
dist.all_reduce_multigpu([train_ae])
dist.all_reduce_multigpu([val_ae])
scheduler.step()
if rank == 0:
end = perf_counter()
best_epoch = False
if val_ae.item() / len(val) < best:
best = val_ae.item() / len(val)
checkpoint = {
'epoch': epoch,
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'scheduler': scheduler.state_dict(),
'best_val_ae': val_ae.item() / len(val)
}
torch.save(
checkpoint,
os.path.join(args.model_path, f'{args.model_name}.pt')
)
best_epoch = True
print(
f"Epoch: {epoch}",
f"Train loss: {train_loss.item():.3f}",
f"Aux train loss: {aux_train_loss.item():.3f}",
f"Val loss: {val_loss.item():.3f}",
f"Aux val loss: {aux_val_loss.item():.3f}",
f"Train MAE: {train_ae.item() / len(train):.3f}",
f"Val MAE: {val_ae.item() / len(val):.3f}",
f"Epoch time: {end - start:.3f} seconds",
'best' if best_epoch else ''
)
dist.destroy_process_group()
if __name__ == '__main__':
parser = argparse.ArgumentParser('LOCA', parents=[get_argparser()])
args = parser.parse_args()
train(args)