Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 如何在MXNet和Python 2.7中同时对多个图像的图像分类模型进行推断_Python 2.7_Mxnet - Fatal编程技术网

Python 2.7 如何在MXNet和Python 2.7中同时对多个图像的图像分类模型进行推断

Python 2.7 如何在MXNet和Python 2.7中同时对多个图像的图像分类模型进行推断,python-2.7,mxnet,Python 2.7,Mxnet,我使用Python2.7、MXNETV1.3.0ML框架在ONNX格式的图像分类模型上运行推断(V1.2.1,带有opset 7),每次向推断器提供一个图像。我需要做什么才能异步运行多个图像的推断,同时等待它们全部完成 我正在以每秒30帧的速度从视频中提取.jpeg格式的帧。例如,当我在长度为20秒的视频上运行该过程时,它会生成600.jpeg图像。现在,我遍历这些图像的列表,并将每个图像的相对路径传递给下面的函数,该函数随后根据目标图像进行推断 def推断(自身、目标图像路径): target

我使用Python2.7、MXNETV1.3.0ML框架在ONNX格式的图像分类模型上运行推断(V1.2.1,带有opset 7),每次向推断器提供一个图像。我需要做什么才能异步运行多个图像的推断,同时等待它们全部完成

我正在以每秒30帧的速度从视频中提取.jpeg格式的帧。例如,当我在长度为20秒的视频上运行该过程时,它会生成600.jpeg图像。现在,我遍历这些图像的列表,并将每个图像的相对路径传递给下面的函数,该函数随后根据目标图像进行推断

def推断(自身、目标图像路径):
target_image_path=self._输出_目录+'/'+目标_image_路径
image_data=self.u获取图像数据(目标图像路径)#获取像素数据
''定义模型的输入''
model\u metadata=onnx\u mxnet.get\u model\u元数据(self.\u model)
数据名称=[输入[0]
对于model_metadata.get('input_tensor_data')中的输入
Batch=namedtuple('Batch','data')
ctx=mx.eia()#将上下文设置为弹性推理
“加载模型”
sym,arg,aux=onnx\u mxnet.import\u model(self.\u model)
mod=mx.mod.模块(符号=sym,数据名称=数据名称,
上下文=ctx,标签名称=无)
mod.bind(data_shapes=[(data_name[0],image_data.shape)],
标签\u形状=无,用于\u培训=假)
模块设置参数(参数=arg,辅助参数=aux,
允许\u缺失=真,允许\u额外=真)
''对图像运行推断''
mod.forward(批处理([mx.nd.array(图像数据)])
预测=mod.get_outputs()[0].asnumpy()
预测=预测[0]。tolist()
“应用情感标签”
zipb_object=zip(自我情感标签、预测)
预测字典=dict(zipb对象)
返回预测字典

预期的行为是异步运行每个映像的推断,但也要等待整个批处理的过程完成。

不应该做的一件事是为每个映像加载模型。应该加载一次模型,然后对所有600个图像运行推断

例如,您可以像这样重构代码:

def load_model(self):
        '''Load the model'''
        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)

        return mod


def infer(self, mod, target_image_path):
        target_image_path = self.__output_directory + '/' + target_image_path

        image_data = self.__get_image_data(target_image_path)  # Get pixel data

        '''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
MXNet运行在一个异步引擎上,您不必等待映像完成处理,即可将新映像排入队列

MXNet中的某些调用是异步的,例如,当您调用
mod.forward()
时,此调用会立即返回,并且不会等待计算结果。其他调用是同步的,例如
mod.get\u outputs()[0]。asnumpy()
这会将数据复制到CPU,因此它必须是同步的。在每次迭代之间进行同步调用会稍微降低处理速度

假设您有权访问映像路径列表,您可以这样处理它们,以最大限度地减少等待时间,并仅在末尾有一个同步点:

    results = []
    for target_image_path in image_paths:
        image_data = self.__get_image_data(target_image_path)  # Get pixel data

        '''Run inference on the image'''
        mod.forward(Batch([mx.nd.array(image_data)]))
        results.append(mod.get_outputs()[0])
    predictions = [result.asnumpy()[0].tolist() for result in results]
您可以在此处阅读有关MXNet异步编程的更多信息:

如果您知道有N个图像要处理,则可以将它们批处理为批处理(例如16个),以提高处理的并行性。但是,这样做会增加内存消耗。由于您似乎使用的是弹性推理上下文,所以您的总体内存将受到限制,我建议您坚持使用较小的批处理大小,以免内存耗尽