-
Notifications
You must be signed in to change notification settings - Fork 23
/
eval.py
68 lines (57 loc) · 2.27 KB
/
eval.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
"""
计算验证集的loss
"""
from collections import OrderedDict
import torch
from utils.cal_loss import cal_synthText_loss,cal_fakeData_loss
def copyStateDict(state_dict):
if list(state_dict.keys())[0].startswith("module"):
start_idx = 1
else:
start_idx = 0
new_state_dict = OrderedDict()
for k, v in state_dict.items():
name = ".".join(k.split(".")[start_idx:])
new_state_dict[name] = v
return new_state_dict
def eval_net(net, val_loader, criterion, device):
net.eval()
loss_total = 0
for i, (images, labels_region, labels_affinity, _) in enumerate(val_loader):
images = images.to(device)
labels_region = labels_region.to(device)
labels_affinity = labels_affinity.to(device)
labels_region = torch.squeeze(labels_region, 1)
labels_affinity = torch.squeeze(labels_affinity, 1)
# 前向传播
y, _ = net(images)
score_text = y[:, :, :, 0]
score_link = y[:, :, :, 1]
# 联合损失 ohem loss
loss = cal_synthText_loss(criterion, score_text, score_link, labels_region, labels_affinity, device)
loss_total += loss.item()
return loss_total / (i + 1)
def eval_net_finetune(net, val_loader, criterion, device):
net.eval()
loss_total = 0
for i, (images, labels_region, labels_affinity, sc_map) in enumerate(val_loader):
images = images.to(device)
labels_region = labels_region.to(device)
labels_affinity = labels_affinity.to(device)
sc_map = sc_map.to(device)
labels_region = torch.squeeze(labels_region, 1)
labels_affinity = torch.squeeze(labels_affinity, 1)
# 前向传播
y, _ = net(images)
score_text = y[:, :, :, 0]
score_link = y[:, :, :, 1]
sc_map = torch.squeeze(sc_map, 1)
# 联合损失 ohem loss
# 强弱数据集分别计算损失
if sc_map.size() == labels_region.size():
loss = cal_fakeData_loss(criterion, score_text, score_link, labels_region, labels_affinity, sc_map,
device)
else:
loss = cal_synthText_loss(criterion, score_text, score_link, labels_region, labels_affinity, device)
loss_total += loss.item()
return loss_total / (i + 1)