def _run(predictor: Predictor,
input_file: IO,
output_file: Optional[IO],
batch_size: int,
print_to_console: bool,
cuda_device: int) -> None:
def _run_predictor(batch_data):
if len(batch_data) == 1:
result = predictor.predict_json(batch_data[0], cuda_device)
# Batch results return a list of json objects, so in
# order to iterate over the result below we wrap this in a list.
results = [result]
else:
results = predictor.predict_batch_json(batch_data, cuda_device)
for model_input, output in zip(batch_data, results):
string_output = json.dumps(output)
if print_to_console:
print("input: ", model_input)
print("prediction: ", string_output)
if output_file:
output_file.write(string_output + "\n")
batch_json_data = []
for line in input_file:
if not line.isspace():
# Collect batch size amount of data.
json_data = json.loads(line)
batch_json_data.append(json_data)
if len(batch_json_data) == batch_size:
_run_predictor(batch_json_data)
batch_json_data = []
# We might not have a dataset perfectly divisible by the batch size,
# so tidy up the scraps.
if batch_json_data:
_run_predictor(batch_json_data)
评论列表
文章目录