-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathtrainGMMOT.py
181 lines (149 loc) · 5.65 KB
/
trainGMMOT.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
import torch
import torch.optim as optim
import numpy as np
from GMMOT.dataloader import MOTGraph
from GMMOT.model import GMNet
import torch.nn as nn
from GMMOT.config import cfg
from torch_geometric.data import DataLoader
from pathlib import Path
import os
import scipy
import scipy.optimize as opt
from torch.optim.lr_scheduler import CosineAnnealingLR
class Loss(nn.Module):
def forward(self, x, gt):
x = x.reshape(-1)
gt = gt.reshape(-1)
pa = len(gt)*len(gt)/(len(gt)-gt.sum())/2/gt.sum()
weight_0 = pa*gt.sum()/len(gt)
weight_1 = pa*(len(gt) - gt.sum())/len(gt)
weight = torch.zeros(len(gt))
for i in range(len(gt)):
if gt[i] == 0:
weight[i] = weight_0
elif gt[i] == 1:
weight[i] = weight_1
else:
raise RuntimeError('loss weight error')
#print(weight)
#weight = torch.abs(gt-0.1).detach()
loss = nn.BCELoss(weight=weight.cuda())
# loss = nn.BCELoss(reduction='sum')
# print(gt.sum())
out = loss(x, gt)
return out
def cal_acc(x, gt):
x = x.squeeze(0)
#print(gt.shape)
gt = gt.squeeze(0)
if x.shape[0]>x.shape[1]:
x = x.t()
gt = gt.t()
b = x.max(1).indices == gt.max(1).indices
if b.shape[0] != x.shape[0]:
raise RuntimeError('b.shape[0] != x.shape[0]')
return int(b.sum()), b.shape[0]
def train_model(model, criterion, optimizer, dataloader, num_epochs,scheduler):
print("Start training...")
dataset_size = len(dataloader.dataset)
if cfg.resume:
params_path = os.path.join(cfg.warmstart_path, f"params.pt")
print("Loading model parameters from {}".format(params_path))
model.load_state_dict(torch.load(params_path),strict=False)
for epoch in range(num_epochs):
print("Epoch {}/{}".format(epoch, num_epochs - 1))
print("-" * 10)
model.train() # Set model to training mode
print("lr = " + ", ".join(["{:.2e}".format(x["lr"]) for x in optimizer.param_groups]))
epoch_loss = 0.0
running_loss = 0.0
iter_num = 0
tp_sum = 0.0
node = 0.0
epoch_iter = len(dataloader)
# Iterate over data.
for inputs in dataloader:
iter_num = iter_num + 1
graph_tracks = inputs["graph_tracks"]
graph_dets = inputs["graph_dets"]
anno = inputs["anno"]
iou = inputs["iou"]
# zero the parameter gradients
optimizer.zero_grad()
with torch.set_grad_enabled(True):
# forward
# TODO: model
s_pred = model(graph_tracks, graph_dets,iou)
#print(perm_mat_list[0][0])
# print(s_pred.shape,anno.shape)
loss = criterion(s_pred, anno)
#print(s_pred,anno)
tp, n_node = cal_acc(s_pred, anno)
#print(fp, n_node)
# backward + optimize
loss.backward()
# print(model.reid_enc.fc1.weight.grad)
if torch.isnan(model.reid_enc.fc1.weight.grad).max():
continue
optimizer.step()
loss_dict = dict()
loss_dict['loss'] = loss.item()
accdict = dict()
accdict['matching accuracy'] = tp/n_node
torch.cuda.empty_cache()
# statistics
bs = cfg.TRAIN.BATCH_SIZE
running_loss += loss.item() * bs # multiply with batch size
epoch_loss += loss.item() * bs
tp_sum += tp
node += n_node
if iter_num % cfg.STATISTIC_STEP == 0:
loss_avg = running_loss / cfg.STATISTIC_STEP / bs
acc_avg = tp_sum / node
print(
"Epoch {:<4} Iter {:<4} Loss={:<8.4f} Acc={:<8.4f}tp_sum {:<4}node {:<4}".format(
epoch, iter_num, loss_avg, acc_avg,tp_sum,node
)
)
running_loss = 0.0
tp_sum = 0.0
node = 0.0
#epoch_loss = epoch_loss / dataset_size
if cfg.save_checkpoint:
checkpoint_path = Path(cfg.model_dir) / "params"
if not checkpoint_path.exists():
checkpoint_path.mkdir(parents=True)
base_path = Path(checkpoint_path / "{:04}".format(epoch + 1))
Path(base_path).mkdir(parents=True, exist_ok=True)
path = str(base_path / "params.pt")
torch.save(model.state_dict(), path)
torch.save(optimizer.state_dict(), str(base_path / "optim.pt"))
print()
scheduler.step()
return model
if __name__ == "__main__":
torch.manual_seed(cfg.RANDOM_SEED)
dataset = MOTGraph()
dataloader = DataLoader(
dataset,
batch_size=cfg.TRAIN.BATCH_SIZE,
shuffle=True
)
#device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = GMNet()
model = model.cuda()
#model = torch_geometric.nn.DataParallel(model,device_ids=[0,1])
criterion = Loss()
params = [param for param in model.parameters()]
optimizer = optim.Adam(params, lr=cfg.TRAIN.LR, weight_decay=cfg.TRAIN.WEIGHT_DECAY)
scheduler = CosineAnnealingLR(optimizer, T_max=5)
num_epochs = cfg.TRAIN.MAXEPOCH
model = train_model(
model,
criterion,
optimizer,
dataloader,
num_epochs,
scheduler
)