-
Notifications
You must be signed in to change notification settings - Fork 8
/
train_helper_ALTGVT.py
432 lines (386 loc) · 15.6 KB
/
train_helper_ALTGVT.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import os
import time
import torch
import torch.nn as nn
from torch import optim
from torch.utils.data import DataLoader
from torch.utils.data.dataloader import default_collate
import numpy as np
from datetime import datetime
import torch.nn.functional as F
from datasets.crowd import Crowd_qnrf, Crowd_nwpu, Crowd_sh, CustomDataset
# from models import vgg19
from Networks import ALTGVT
from losses.ot_loss import OT_Loss
from utils.pytorch_utils import Save_Handle, AverageMeter
import utils.log_utils as log_utils
import wandb
def train_collate(batch):
transposed_batch = list(zip(*batch))
images = torch.stack(transposed_batch[0], 0)
points = transposed_batch[
1
] # the number of points is not fixed, keep it as a list of tensor
gt_discretes = torch.stack(transposed_batch[2], 0)
return images, points, gt_discretes
class Trainer(object):
def __init__(self, args):
self.args = args
def setup(self):
args = self.args
sub_dir = (
"ALTGVT/{}_12-1-input-{}_wot-{}_wtv-{}_reg-{}_nIter-{}_normCood-{}".format(
args.run_name,
args.crop_size,
args.wot,
args.wtv,
args.reg,
args.num_of_iter_in_ot,
args.norm_cood,
)
)
self.save_dir = os.path.join("ckpts", sub_dir)
if not os.path.exists(self.save_dir):
os.makedirs(self.save_dir)
time_str = datetime.strftime(datetime.now(), "%m%d-%H%M%S")
self.logger = log_utils.get_logger(
os.path.join(self.save_dir, "train-{:s}.log".format(time_str))
)
log_utils.print_config(vars(args), self.logger)
if torch.cuda.is_available():
self.device = torch.device("cuda")
self.device_count = torch.cuda.device_count()
assert self.device_count == 1
self.logger.info("using {} gpus".format(self.device_count))
else:
raise Exception("gpu is not available")
downsample_ratio = 8
if args.dataset.lower() == "qnrf":
self.datasets = {
x: Crowd_qnrf(
os.path.join(
args.data_dir, x), args.crop_size, downsample_ratio, x
)
for x in ["train", "val"]
}
elif args.dataset.lower() == "nwpu":
self.datasets = {
x: Crowd_nwpu(
os.path.join(
args.data_dir, x), args.crop_size, downsample_ratio, x
)
for x in ["train", "val"]
}
elif args.dataset.lower() == "sha" or args.dataset.lower() == "shb":
self.datasets = {
"train": Crowd_sh(
os.path.join(args.data_dir, "train_data"),
args.crop_size,
downsample_ratio,
"train",
),
"val": Crowd_sh(
os.path.join(args.data_dir, "test_data"),
args.crop_size,
downsample_ratio,
"val",
),
}
elif args.dataset.lower() == "custom":
self.datasets = {
"train": CustomDataset(
args.data_dir, args.crop_size, downsample_ratio, method="train"
),
"val": CustomDataset(
args.data_dir, args.crop_size, downsample_ratio, method="valid"
),
}
else:
raise NotImplementedError
self.dataloaders = {
x: DataLoader(
self.datasets[x],
collate_fn=(train_collate if x ==
"train" else default_collate),
batch_size=(args.batch_size if x == "train" else 1),
shuffle=(True if x == "train" else False),
num_workers=args.num_workers * self.device_count,
pin_memory=(True if x == "train" else False),
)
for x in ["train", "val"]
}
self.model = ALTGVT.alt_gvt_large(pretrained=True)
self.model.to(self.device)
self.optimizer = optim.AdamW(
self.model.parameters(), lr=args.lr, weight_decay=args.weight_decay
)
self.start_epoch = 0
# check if wandb has to log
if args.wandb:
self.wandb_run = wandb.init(
config=args, project="CTTrans", name=args.run_name
)
else :
wandb.init(mode="disabled")
if args.resume:
self.logger.info("loading pretrained model from " + args.resume)
suf = args.resume.rsplit(".", 1)[-1]
if suf == "tar":
checkpoint = torch.load(args.resume, self.device)
self.model.load_state_dict(checkpoint["model_state_dict"])
self.optimizer.load_state_dict(
checkpoint["optimizer_state_dict"])
self.start_epoch = checkpoint["epoch"] + 1
elif suf == "pth":
self.model.load_state_dict(
torch.load(args.resume, self.device))
else:
self.logger.info("random initialization")
self.ot_loss = OT_Loss(
args.crop_size,
downsample_ratio,
args.norm_cood,
self.device,
args.num_of_iter_in_ot,
args.reg,
)
self.tv_loss = nn.L1Loss(reduction="none").to(self.device)
self.mse = nn.MSELoss().to(self.device)
self.mae = nn.L1Loss().to(self.device)
self.save_list = Save_Handle(max_num=1)
self.best_mae = np.inf
self.best_mse = np.inf
# self.best_count = 0
def train(self):
"""training process"""
args = self.args
for epoch in range(self.start_epoch, args.max_epoch + 1):
self.logger.info(
"-" * 5 + "Epoch {}/{}".format(epoch, args.max_epoch) + "-" * 5
)
self.epoch = epoch
self.train_epoch()
if epoch % args.val_epoch == 0 and epoch >= args.val_start:
self.val_epoch()
def train_epoch(self):
epoch_ot_loss = AverageMeter()
epoch_ot_obj_value = AverageMeter()
epoch_wd = AverageMeter()
epoch_count_loss = AverageMeter()
epoch_tv_loss = AverageMeter()
epoch_loss = AverageMeter()
epoch_mae = AverageMeter()
epoch_mse = AverageMeter()
epoch_start = time.time()
self.model.train() # Set model to training mode
for step, (inputs, points, gt_discrete) in enumerate(self.dataloaders["train"]):
inputs = inputs.to(self.device)
gd_count = np.array([len(p) for p in points], dtype=np.float32)
points = [p.to(self.device) for p in points]
gt_discrete = gt_discrete.to(self.device)
N = inputs.size(0)
with torch.set_grad_enabled(True):
outputs, outputs_normed = self.model(inputs)
# Compute OT loss.
ot_loss, wd, ot_obj_value = self.ot_loss(
outputs_normed, outputs, points
)
ot_loss = ot_loss * self.args.wot
ot_obj_value = ot_obj_value * self.args.wot
epoch_ot_loss.update(ot_loss.item(), N)
epoch_ot_obj_value.update(ot_obj_value.item(), N)
epoch_wd.update(wd, N)
# Compute counting loss.
count_loss = self.mae(
outputs.sum(1).sum(1).sum(1),
torch.from_numpy(gd_count).float().to(self.device),
)
epoch_count_loss.update(count_loss.item(), N)
# Compute TV loss.
gd_count_tensor = (
torch.from_numpy(gd_count)
.float()
.to(self.device)
.unsqueeze(1)
.unsqueeze(2)
.unsqueeze(3)
)
gt_discrete_normed = gt_discrete / (gd_count_tensor + 1e-6)
tv_loss = (
self.tv_loss(outputs_normed, gt_discrete_normed)
.sum(1)
.sum(1)
.sum(1)
* torch.from_numpy(gd_count).float().to(self.device)
).mean(0) * self.args.wtv
epoch_tv_loss.update(tv_loss.item(), N)
loss = ot_loss + count_loss + tv_loss
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
pred_count = (
torch.sum(outputs.view(N, -1),
dim=1).detach().cpu().numpy()
)
pred_err = pred_count - gd_count
epoch_loss.update(loss.item(), N)
epoch_mse.update(np.mean(pred_err * pred_err), N)
epoch_mae.update(np.mean(abs(pred_err)), N)
# log wandb
wandb.log(
{
"train/TOTAL_loss": loss,
"train/count_loss": count_loss,
"train/tv_loss": tv_loss,
"train/pred_err": pred_err,
},
step=self.epoch,
)
self.logger.info(
"Epoch {} Train, Loss: {:.2f}, OT Loss: {:.2e}, Wass Distance: {:.2f}, OT obj value: {:.2f}, "
"Count Loss: {:.2f}, TV Loss: {:.2f}, MSE: {:.2f} MAE: {:.2f}, Cost {:.1f} sec".format(
self.epoch,
epoch_loss.get_avg(),
epoch_ot_loss.get_avg(),
epoch_wd.get_avg(),
epoch_ot_obj_value.get_avg(),
epoch_count_loss.get_avg(),
epoch_tv_loss.get_avg(),
np.sqrt(epoch_mse.get_avg()),
epoch_mae.get_avg(),
time.time() - epoch_start,
)
)
model_state_dic = self.model.state_dict()
save_path = os.path.join(
self.save_dir, "{}_ckpt.tar".format(self.epoch))
torch.save(
{
"epoch": self.epoch,
"optimizer_state_dict": self.optimizer.state_dict(),
"model_state_dict": model_state_dic,
},
save_path,
)
self.save_list.append(save_path)
def val_epoch(self):
args = self.args
epoch_start = time.time()
self.model.eval() # Set model to evaluate mode
epoch_res = []
for inputs, count, name in self.dataloaders["val"]:
with torch.no_grad():
# nputs = cal_new_tensor(inputs, min_size=args.crop_size)
inputs = inputs.to(self.device)
crop_imgs, crop_masks = [], []
b, c, h, w = inputs.size()
rh, rw = args.crop_size, args.crop_size
for i in range(0, h, rh):
gis, gie = max(min(h - rh, i), 0), min(h, i + rh)
for j in range(0, w, rw):
gjs, gje = max(min(w - rw, j), 0), min(w, j + rw)
crop_imgs.append(inputs[:, :, gis:gie, gjs:gje])
mask = torch.zeros([b, 1, h, w]).to(self.device)
mask[:, :, gis:gie, gjs:gje].fill_(1.0)
crop_masks.append(mask)
crop_imgs, crop_masks = map(
lambda x: torch.cat(x, dim=0), (crop_imgs, crop_masks)
)
crop_preds = []
nz, bz = crop_imgs.size(0), args.batch_size
for i in range(0, nz, bz):
gs, gt = i, min(nz, i + bz)
crop_pred, _ = self.model(crop_imgs[gs:gt])
_, _, h1, w1 = crop_pred.size()
crop_pred = (
F.interpolate(
crop_pred,
size=(h1 * 8, w1 * 8),
mode="bilinear",
align_corners=True,
)
/ 64
)
crop_preds.append(crop_pred)
crop_preds = torch.cat(crop_preds, dim=0)
# splice them to the original size
idx = 0
pred_map = torch.zeros([b, 1, h, w]).to(self.device)
for i in range(0, h, rh):
gis, gie = max(min(h - rh, i), 0), min(h, i + rh)
for j in range(0, w, rw):
gjs, gje = max(min(w - rw, j), 0), min(w, j + rw)
pred_map[:, :, gis:gie, gjs:gje] += crop_preds[idx]
idx += 1
# for the overlapping area, compute average value
mask = crop_masks.sum(dim=0).unsqueeze(0)
outputs = pred_map / mask
res = count[0].item() - torch.sum(outputs).item()
epoch_res.append(res)
epoch_res = np.array(epoch_res)
mse = np.sqrt(np.mean(np.square(epoch_res)))
mae = np.mean(np.abs(epoch_res))
self.logger.info(
"Epoch {} Val, MSE: {:.2f} MAE: {:.2f}, Cost {:.1f} sec".format(
self.epoch, mse, mae, time.time() - epoch_start
)
)
# log wandb
wandb.log({"val/MSE": mse, "val/MAE": mae}, step=self.epoch)
model_state_dic = self.model.state_dict()
# if (2.0 * mse + mae) < (2.0 * self.best_mse + self.best_mae):
print("Comaprison", mae, self.best_mae)
if mae < self.best_mae:
self.best_mse = mse
self.best_mae = mae
self.logger.info(
"save best mse {:.2f} mae {:.2f} model epoch {}".format(
self.best_mse, self.best_mae, self.epoch
)
)
print("Saving best model at {} epoch".format(self.epoch))
model_path = os.path.join(
self.save_dir, "best_model_mae-{:.2f}_epoch-{}.pth".format(
self.best_mae, self.epoch)
)
torch.save(
model_state_dic,
model_path,
)
if args.wandb:
artifact = wandb.Artifact("model", type="model")
artifact.add_file(model_path)
self.wandb_run.log_artifact(artifact)
# torch.save(model_state_dic, os.path.join(self.save_dir, 'best_model_{}.pth'.format(self.best_count)))
# self.best_count += 1
def tensor_divideByfactor(img_tensor, factor=32):
_, _, h, w = img_tensor.size()
h, w = int(h // factor * factor), int(w // factor * factor)
img_tensor = F.interpolate(
img_tensor, (h, w), mode="bilinear", align_corners=True)
return img_tensor
def cal_new_tensor(img_tensor, min_size=256):
_, _, h, w = img_tensor.size()
if min(h, w) < min_size:
ratio_h, ratio_w = min_size / h, min_size / w
if ratio_h >= ratio_w:
img_tensor = F.interpolate(
img_tensor,
(min_size, int(min_size / h * w)),
mode="bilinear",
align_corners=True,
)
else:
img_tensor = F.interpolate(
img_tensor,
(int(min_size / w * h), min_size),
mode="bilinear",
align_corners=True,
)
return img_tensor
if __name__ == "__main__":
import torch
print(torch.__file__)
x = torch.ones(1, 3, 768, 1152)
y = tensor_spilt(x)
print(y.size())