-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.py
144 lines (114 loc) · 4.23 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
import torch
import torchvision
import torch.nn as nn
import os
import glob
from modeling.dsnet import DenseScaleNet as DSNet
import torchvision.transforms as transforms
from dataset import RawDataset
import torch.nn.functional as F
import logging
import warnings
warnings.filterwarnings("ignore")
def cal_lc_loss(output, target, sizes=(1,2,4)):
criterion_L1 = nn.L1Loss(reduction='sum')
Lc_loss = None
for s in sizes:
pool = nn.AdaptiveAvgPool2d(s)
est = pool(output)
gt = pool(target)
if Lc_loss:
Lc_loss += criterion_L1(est, gt) / s**2
else:
Lc_loss = criterion_L1(est, gt) / s**2
return Lc_loss
def getLogger(filename):
logger = logging.getLogger('train_logger')
while logger.handlers:
logger.handlers.pop()
logger.setLevel(logging.INFO)
fh = logging.FileHandler(filename, 'w')
fh.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('[%(asctime)s], ## %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
return logger
def val(model, test_loader):
model.eval()
mae = 0.0
mse = 0.0
with torch.no_grad():
for img, target, count in test_loader:
img = img.cuda()
output = model(img)
est_count = output.sum().item()
mae += abs(est_count - count)
mse += (est_count - count)**2
mae /= len(test_loader)
mse /= len(test_loader)
mse = mse**0.5
return float(mae), float(mse)
def get_loader(train_path, test_path, ratio, kernel_path):
train_img_paths = []
for img_path in glob.glob(os.path.join(train_path, '*.jpg')):
train_img_paths.append(img_path)
test_img_paths = []
for img_path in glob.glob(os.path.join(test_path, '*.jpg')):
test_img_paths.append(img_path)
# ! ToTensor -> range(0,1)
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])
train_loader = torch.utils.data.DataLoader(
RawDataset(train_img_paths, transform, aug=True, ratio=ratio, kernel_path=kernel_path),
shuffle=True, batch_size=1, num_workers=4)
# ratio 1 for val, gt_map is not used
test_loader = torch.utils.data.DataLoader(
RawDataset(test_img_paths, transform, ratio=1, aug=False, kernel_path=kernel_path),
shuffle=False, batch_size=1, num_workers=4)
return train_loader, test_loader
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def main():
net = DSNet('')
print('%s trainable params'%count_parameters(net))
net.cuda()
# For shg A
train_path = '...'
test_path = '...'
kernel_path = 'maps_adaptive_kernel'
lbda = 1000
ratio = 8 # density map scaling
train_loader, test_loader = get_loader(train_path, test_path, ratio, kernel_path)
save_path = './saved_models/dsnet_shtechA.pth'
epochs = 500
lr=1e-6
weight_decay=5e-4
optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, net.parameters()), lr=lr, weight_decay=weight_decay)
criterion = nn.MSELoss()
#best_mae, _ = val(net, test_loader)
best_mae = 167.99
logger = getLogger('logs/dsnet_sha_Adam_1e-6.txt')
for epoch in range(epochs):
train_loss = 0.0
net.train()
for img, target, count in train_loader:
optimizer.zero_grad()
img = img.cuda()
target = target.unsqueeze(1).cuda()
output = net(img)
Le_Loss = criterion(output, target)
Lc_Loss = cal_lc_loss(output, target)
loss = Le_Loss + lbda * Lc_Loss
loss.backward()
optimizer.step()
train_loss += loss.item()
mae, mse = val(net, test_loader)
logger.info('Epoch {}/{} Loss:{:.3f}, MAE:{:.2f}, MSE:{:.2f}, Best MAE:{:.2f}'.format(epoch+1, epochs, train_loss/len(train_loader), mae, mse, best_mae))
if mae < best_mae:
best_mae = mae
torch.save(net.state_dict(), save_path)
if __name__ == '__main__':
main()