-
Notifications
You must be signed in to change notification settings - Fork 3
/
engine.py
157 lines (131 loc) · 6.11 KB
/
engine.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
"""
Training, evaluation and visualization functions
"""
import math
import os
#import sys
from typing import Iterable
from PIL import Image
import numpy as np
import torch
import pdb
def train_one_epoch(model: torch.nn.Module, criterion: torch.nn.Module,
data_loader: Iterable, optimizer: torch.optim.Optimizer,
device: torch.device, epoch: int, max_norm: float = 0):
model.train()
criterion.train()
loss_sum = 0
loss_counting = 0
loss_contrast = 0
for idx, sample in enumerate(data_loader):
img, patches, targets, file_name = sample
img = img.to(device)
patches['patches'] = patches['patches'].to(device)
patches['scale_embedding'] = patches['scale_embedding'].to(device)
density_map = targets['density_map'].to(device)
pt_map = targets['pt_map'].to(device)
outputs = model(img, patches, is_train=True)
dest = outputs['density_map']
if epoch < 5: # check if training process get stucked in local optimal.
print(dest.sum().item(), density_map.sum().item(), dest.sum().item()*10000 / (img.shape[-2] * img.shape[-1]))
counting_loss, contrast_loss = criterion(outputs, density_map, pt_map)
loss = counting_loss if isinstance(contrast_loss, int) else counting_loss + contrast_loss
loss_value = loss.item()
if not math.isfinite(loss_value):
continue
loss_sum += loss_value
loss_contrast += contrast_loss if isinstance(contrast_loss, int) else contrast_loss.item()
loss_counting += counting_loss.item()
optimizer.zero_grad()
loss.backward()
if max_norm > 0:
torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm)
optimizer.step()
if (idx + 1) % 10 == 0:
print('Epoch: %d, %d / %d, loss: %.8f, counting loss: %.8f, contrast loss: %.8f'%(epoch, idx+1,
len(data_loader),
loss_sum / (idx+1),
loss_counting / (idx+1),
loss_contrast / (idx+1)))
return loss_sum / len(data_loader)
@torch.no_grad()
def evaluate(model, data_loader, device, output_dir):
mae = 0
mse = 0
model.eval()
for idx, sample in enumerate(data_loader):
img, patches, targets, file_name = sample
img = img.to(device)
patches['patches'] = patches['patches'].to(device)
patches['scale_embedding'] = patches['scale_embedding'].to(device)
gtcount = targets['gtcount']
with torch.no_grad():
outputs = model(img, patches, is_train=False)
error = torch.abs(outputs.sum() - gtcount.item()).item()
mae += error
mse += error ** 2
mae = mae / len(data_loader)
mse = mse / len(data_loader)
mse = mse ** 0.5
with open(os.path.join(output_dir, 'result.txt'), 'a') as f:
f.write('MAE %.2f, MSE %.2f \n'%(mae, mse))
print('MAE %.2f, MSE %.2f \n'%(mae, mse))
return mae, mse
@torch.no_grad()
def visualization(cfg, model, dataset, data_loader, device, output_dir):
import matplotlib.pyplot as plt
plt.switch_backend('agg')
cmap = plt.cm.get_cmap('jet')
visualization_dir = os.path.join(output_dir, 'visualizations')
if not os.path.exists(visualization_dir):
os.mkdir(visualization_dir)
mae = 0
mse = 0
model.eval()
for idx, sample in enumerate(data_loader):
img, patches, targets = sample
img = img.to(device)
patches['patches'] = patches['patches'].to(device)
patches['scale_embedding'] = patches['scale_embedding'].to(device)
gtcount = targets['gtcount']
gt_density = targets['density_map']
with torch.no_grad():
outputs = model(img, patches, is_train=False)
error = torch.abs(outputs.sum() - gtcount.item()).item()
mae += error
mse += error ** 2
# read original image
file_name = dataset.data_list[idx][0]
file_path = image_path = dataset.data_dir + 'images_384_VarV2/' + file_name
origin_img = Image.open(file_path).convert("RGB")
origin_img = np.array(origin_img)
h, w, _ = origin_img.shape
cmap = plt.cm.get_cmap('jet')
density_map = outputs
density_map = torch.nn.functional.interpolate(density_map, (h, w), mode='bilinear').squeeze(0).squeeze(0).cpu().numpy()
gt_density = torch.nn.functional.interpolate(gt_density, (h, w), mode='bilinear').squeeze(0).squeeze(0).cpu().numpy()
density_map = cmap(density_map / (density_map.max()) + 1e-14) * 255.0
density_map = density_map[:,:,0:3] * 0.5 + origin_img * 0.5
gt_density = cmap(gt_density / (gt_density.max()) + 1e-14) * 255.0
gt_density = gt_density[:,:,0:3] * 0.5 + origin_img * 0.5
fig = plt.figure(dpi=800)
ax1 = fig.add_subplot(1,2,1)
ax2 = fig.add_subplot(1,2,2)
ax1.get_xaxis().set_visible(False)
ax1.get_yaxis().set_visible(False)
ax2.get_xaxis().set_visible(False)
ax2.get_yaxis().set_visible(False)
ax1.set_title(str(gtcount.item()))
ax2.set_title(str(outputs.sum().item()))
ax1.imshow(gt_density.astype(np.uint8))
ax2.imshow(density_map.astype(np.uint8))
save_path = os.path.join(visualization_dir, os.path.basename(file_name))
plt.savefig(save_path)
plt.close()
mae = mae / len(data_loader)
mse = mse / len(data_loader)
mse = mse ** 0.5
with open(os.path.join(output_dir, 'result.txt'), 'a') as f:
f.write('MAE %.2f, MSE %.2f \n'%(mae, mse))
print('MAE %.2f, MSE %.2f \n'%(mae, mse))
return mae, mse