AI开发平台MODELARTS-模型推理代码编写说明:MindSpore的推理脚本示例

时间:2024-12-10 11:36:21

MindSpore的推理脚本示例

  • snt3芯片目前只有北京四提工单申请权限后才可以使用,支持模型格式为.om,推理脚本如下:

    from __future__ import absolute_import
    from __future__ import division
    from __future__ import print_function
    
    import json
    import os
    
    import numpy as np
    from PIL import Image
    from hiai.nn_tensor_lib import NNTensor
    from hiai.nntensor_list import NNTensorList
    from model_service.hiai_model_service import HiaiBaseService
    
    class DemoService(HiaiBaseService):
      def __init__(self, *args, **kwargs):
    	# 默认加载模型包目录下的om文件
    	super(DemoService, self).__init__(*args, **kwargs)
    	self.labels_list = None
    	self.is_multilabel = False
    
      def _preprocess(self, data):
    	preprocessed_data = {}
    	images = []
    	for k, v in data.items():
    	  for file_name, file_content in v.items():
    	  image = Image.open(file_content)
    	  image = np.array(image)  # NHWC
    	  # AIPP should use RGB format.
    	  # mean reg is applied in AIPP.
    	  # Transpose is applied in AIPP
    	  tensor = NNTensor(image)
    	  images.append(tensor)
    	  tensor_list = NNTensorList(images)
    	preprocessed_data['images'] = tensor_list
    	return preprocessed_data
    
      def _inference(self, data, image_info=None):
    	result = {}
    	for k, v in data.items():
    	  result[k] = self.model.proc(v)
    	return result
    
      def _postprocess(self, data):
    	# 这里增加自己的后处理
    	return str(data)
support.huaweicloud.com/usermanual-standard-modelarts/inference-modelarts-0057.html