【发布时间】:2019-04-08 11:40:16
【问题描述】:
我正在使用 Python 2.7、MXNet V1.3.0 ML 框架在 ONNX 格式的图像分类模型(带有 opset 7 的 V1.2.1)上运行推理,我一次将图像提供给推理器。我需要做什么才能异步运行多个图像的推理但还要等待所有图像完成?
我从 30 FPS 的视频中提取帧作为 .jpeg 图像。例如,当我对长度为 20 秒的视频运行该过程时,它会生成 600 个 .jpeg 图像。现在,我遍历这些图像的列表,并将它们中的每一个的相对路径传递给以下函数,然后从目标图像推断。
def infer(self, target_image_path):
target_image_path = self.__output_directory + '/' + target_image_path
image_data = self.__get_image_data(target_image_path) # Get pixel data
'''Define the model's input'''
model_metadata = onnx_mxnet.get_model_metadata(self.__model)
data_names = [inputs[0]
for inputs in model_metadata.get('input_tensor_data')]
Batch = namedtuple('Batch', 'data')
ctx = mx.eia() # Set the context to elastic inference
'''Load the model'''
sym, arg, aux = onnx_mxnet.import_model(self.__model)
mod = mx.mod.Module(symbol=sym, data_names=data_names,
context=ctx, label_names=None)
mod.bind(data_shapes=[(data_names[0], image_data.shape)],
label_shapes=None, for_training=False)
mod.set_params(arg_params=arg, aux_params=aux,
allow_missing=True, allow_extra=True)
'''Run inference on the image'''
mod.forward(Batch([mx.nd.array(image_data)]))
predictions = mod.get_outputs()[0].asnumpy()
predictions = predictions[0].tolist()
'''Apply emotion labels'''
zipb_object = zip(self.__emotion_labels, predictions)
prediction_dictionary = dict(zipb_object)
return prediction_dictionary
预期的行为是异步运行每个图像的推理,但也等待整个批次的过程完成。
【问题讨论】:
标签: python-2.7 mxnet