forked from PaddlePaddle/Knover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfer.py
136 lines (117 loc) · 4.52 KB
/
infer.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
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Inference main program."""
import argparse
from collections import defaultdict
import json
import os
import subprocess
import time
import numpy as np
import paddle.fluid as fluid
import models
import tasks
from utils import check_cuda, Timer
from utils.args import parse_args, str2bool
def setup_args():
"""
Setup arguments.
"""
parser = argparse.ArgumentParser()
parser.add_argument("--is_distributed", type=str2bool, default=False)
parser.add_argument("--save_path", type=str, default="output")
parser.add_argument("--infer_file", type=str, required=True)
parser.add_argument("--output_name", type=str, required=True)
parser.add_argument("--log_steps", type=int, default=1)
models.add_cmdline_args(parser)
tasks.add_cmdline_args(parser)
args = parse_args(parser)
args.load(args.config_path, "Model")
args.run_infer = True # only build infer program
print(json.dumps(args, indent=2))
return args
def infer(args):
"""
Inference main function.
"""
if args.is_distributed:
dev_count = fluid.core.get_cuda_device_count()
gpu_id = int(os.getenv("FLAGS_selected_gpus"))
phase = "distributed_test"
else:
dev_count = 1
gpu_id = 0
phase = "test"
place = fluid.CUDAPlace(gpu_id)
task = tasks.create_task(args)
model = models.create_model(args, place)
infer_generator = task.reader.data_generator(
input_file=args.infer_file,
num_part=dev_count,
part_id=gpu_id,
phase=phase,
is_infer=True
)
# run inference
timer = Timer()
timer.start()
infer_out = {}
for step, data in enumerate(infer_generator(), 1):
predictions = task.infer_step(model, data)
for info in predictions:
infer_out[info["data_id"]] = info
if step % args.log_steps == 0:
time_cost = timer.pass_time
print(f"\tstep: {step}, time: {time_cost:.3f}, "
f"speed: {step / time_cost:.3f} steps/s")
time_cost = timer.pass_time
print(f"[infer] steps: {step} time cost: {time_cost}, "
f"speed: {step / time_cost} steps/s")
if args.is_distributed:
# merge inference outputs in distributed mode.
part_file = os.path.join(args.save_path, f"inference_output.part_{gpu_id}")
with open(part_file, "w") as fp:
json.dump(infer_out, fp, ensure_ascii=False)
part_finish_file = os.path.join(args.save_path, f"inference_output.part_{gpu_id}.finish")
with open(part_finish_file, "w"):
pass
if gpu_id == 0:
part_files = f"inference_output.part_*.finish"
while True:
ret = subprocess.getoutput(f"find {args.save_path} -maxdepth 1 -name {part_files}")
num_completed = len(ret.split("\n"))
if num_completed != dev_count:
time.sleep(1)
continue
infer_out = {}
for dev_id in range(dev_count):
part_file = os.path.join(args.save_path, f"inference_output.part_{dev_id}")
with open(part_file, "r") as fp:
part_infer_out = json.load(fp)
for data_id in part_infer_out:
infer_out[data_id] = part_infer_out[data_id]
break
subprocess.getoutput("rm " + os.path.join(args.save_path, f"inference_output.part*"))
if gpu_id == 0:
# save inference outputs
inference_output = os.path.join(args.save_path, "inference_output.txt")
with open(inference_output, "w") as f:
for data_id in sorted(infer_out.keys(), key=lambda x: int(x)):
f.write(str(infer_out[data_id][args.output_name]) + "\n")
print(f"save inference result into: {inference_output}")
return
if __name__ == "__main__":
args = setup_args()
check_cuda(True)
infer(args)