-
Notifications
You must be signed in to change notification settings - Fork 38
/
eval_textBPN_speed.py
130 lines (107 loc) · 4 KB
/
eval_textBPN_speed.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
import os
import time
import torch
import torch.backends.cudnn as cudnn
import torch.utils.data as data
from dataset import TotalText, Ctw1500Text, Icdar15Text, Mlt2017Text, TD500Text, \
ArtText, ArtTextJson, Mlt2019Text, Ctw1500Text_New, TotalText_New
from network.textnet import TextNet
from util.augmentation import BaseTransform
from cfglib.config import config as cfg, update_config, print_config
from cfglib.option import BaseOptions
from util.misc import to_device, mkdirs
import multiprocessing
multiprocessing.set_start_method("spawn", force=True)
def osmkdir(out_dir):
import shutil
if os.path.exists(out_dir):
shutil.rmtree(out_dir)
os.makedirs(out_dir)
def inference(model, test_loader, output_dir):
total_time = 0.
if cfg.exp_name != "MLT2017" and cfg.exp_name != "ArT":
osmkdir(output_dir)
else:
if not os.path.exists(output_dir):
mkdirs(output_dir)
for i, (image, meta) in enumerate(test_loader):
input_dict = dict()
input_dict['img'] = to_device(image)
# init model
if i == 0:
output_dict = model(input_dict, test_speed=True)
for k in range(0, 50):
start = time.time()
output_dict = model(input_dict, test_speed=True)
torch.cuda.synchronize()
end = time.time()
total_time += end - start
fps = (i + 1)*50 / total_time
print('detect {} / {} images: {}. ({:.2f} fps)'.
format(i + 1, len(test_loader), meta['image_id'][0], fps))
def main(vis_dir_path):
osmkdir(vis_dir_path)
if cfg.exp_name == "Totaltext":
testset = TotalText(
data_root='data/total-text-mat',
ignore_list=None,
is_training=False,
transform=BaseTransform(size=cfg.test_size, mean=cfg.means, std=cfg.stds)
)
elif cfg.exp_name == "Ctw1500":
testset = Ctw1500Text(
data_root='data/ctw1500',
is_training=False,
transform=BaseTransform(size=cfg.test_size, mean=cfg.means, std=cfg.stds)
)
elif cfg.exp_name == "Icdar2015":
testset = Icdar15Text(
data_root='data/Icdar2015',
is_training=False,
transform=BaseTransform(size=cfg.test_size, mean=cfg.means, std=cfg.stds)
)
elif cfg.exp_name == "MLT2017":
testset = Mlt2017Text(
data_root='data/MLT2017',
is_training=False,
transform=BaseTransform(size=cfg.test_size, mean=cfg.means, std=cfg.stds)
)
elif cfg.exp_name == "TD500":
testset = TD500Text(
data_root='data/TD500',
is_training=False,
transform=BaseTransform(size=cfg.test_size, mean=cfg.means, std=cfg.stds)
)
elif cfg.exp_name == "ArT":
testset = ArtTextJson(
data_root='data/ArT',
is_training=False,
transform=BaseTransform(size=cfg.test_size, mean=cfg.means, std=cfg.stds)
)
else:
print("{} is not justify".format(cfg.exp_name))
if cfg.cuda:
cudnn.benchmark = True
test_loader = data.DataLoader(testset, batch_size=1, shuffle=False, num_workers=cfg.num_workers, pin_memory=True)
# Model
model = TextNet(is_training=False, backbone=cfg.net)
model_path = os.path.join(cfg.save_dir, cfg.exp_name,
'TextBPN_{}_{}.pth'.format(model.backbone_name, cfg.checkepoch))
model.load_model(model_path)
model = model.to(cfg.device) # copy to cuda
model.eval()
with torch.no_grad():
print('Start testing TextBPN++.')
output_dir = os.path.join(cfg.output_dir, cfg.exp_name)
inference(model, test_loader, output_dir)
if __name__ == "__main__":
# parse arguments
option = BaseOptions()
args = option.initialize()
update_config(cfg, args)
print_config(cfg)
vis_dir = os.path.join(cfg.vis_dir, '{}_test'.format(cfg.exp_name))
if not os.path.exists(vis_dir):
mkdirs(vis_dir)
# main
main(vis_dir)