forked from JennyVanessa/Paddle-PANet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
80 lines (62 loc) · 2.08 KB
/
test.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
import paddle
import numpy as np
import argparse
import os
import os.path as osp
import sys
import time
import json
from mmcv import Config
from dataset import build_data_loader
from models import build_model
from utils import ResultFormat, AverageMeter
# import warnings
# warnings.filterwarnings('ignore')
def test(test_loader, model, cfg):
model.eval()
rf = ResultFormat(cfg.data.test.type, cfg.test_cfg.result_path)
for idx, data_ in enumerate(test_loader):
print('Testing %d/%d' % (idx, len(test_loader)))
sys.stdout.flush()
data = dict(
imgs=data_[1],
img_metas=dict(
org_img_size=data_[0].shape[1:3],
img_size=data_[1].shape[2:]
),
cfg=cfg
)
# forward
with paddle.no_grad():
outputs = model(**data)
# save result
image_name, _ = osp.splitext(osp.basename(test_loader.dataset.img_paths[idx]))
# print('image_name', image_name)
rf.write_result(image_name, outputs)
def main(args):
cfg = Config.fromfile(args.config)
# print(json.dumps(cfg._cfg_dict, indent=4))
sys.stdout.flush()
# data loader
test_dataset = build_data_loader(cfg.data.test)
test_loader = paddle.io.DataLoader(
test_dataset,
batch_size=1,
shuffle=False,
num_workers=0,
)
# model
model = build_model(cfg.model)
if args.checkpoint is not None and os.path.isfile(args.checkpoint):
print("Loading model from checkpoint '{}'".format(args.checkpoint))
model.set_state_dict(paddle.load(args.checkpoint))
else:
print("No checkpoint found at '{}'".format(args.checkpoint))
raise
test(test_loader, model, cfg)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Hyperparams')
parser.add_argument('config', help='config file path')
parser.add_argument('checkpoint', nargs='?', type=str, default=None)
args = parser.parse_args()
main(args)