classification_inference.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. """
  2. 定义图像分类推理流程
  3. """
  4. import numpy as np
  5. from PIL import Image
  6. from mindx.sdk import Tensor # mxVision 中的 Tensor 数据结构
  7. from mindx.sdk import base # mxVision 推理接口
  8. class ClassificationInference:
  9. def __init__(self, model_path, input_size=(224, 224), swap=(2, 0, 1)):
  10. """
  11. 初始化图像分类模型推理流程
  12. :param model_path: 图像分类模型onnx文件路径
  13. :param input_size: 模型输入大小
  14. :param swap: 变换方式,pytorch需要进行轴变换(默认参数),tensorflow无需进行轴变换
  15. """
  16. self.model_path = model_path
  17. self.input_size = input_size
  18. self.swap = swap
  19. def input_processing(self, image_path):
  20. """
  21. 对单个图像输入进行处理
  22. :param image_path: 图像路径
  23. :return: 处理后输出
  24. """
  25. # 打开图像并转换为RGB
  26. image = Image.open(image_path).convert("RGB")
  27. # 调整图像大小
  28. image = image.resize(self.input_size)
  29. # 转换为numpy数组并归一化
  30. image_array = np.array(image) / 255.0 # 将像素值缩放到[0, 1]
  31. # 进行标准化
  32. mean = np.array([0.485, 0.456, 0.406])
  33. std = np.array([0.229, 0.224, 0.225])
  34. image_array = (image_array - mean) / std
  35. image_array = image_array.transpose(self.swap).copy()
  36. return image_array.astype(np.float32)
  37. def predict(self, image_path):
  38. """
  39. 对单张图片进行推理
  40. :param image_path: 图片路径
  41. :return: 推理结果
  42. """
  43. image = self.input_processing(image_path)
  44. # 执行推理
  45. base.mx_init() # 初始化 mxVision 资源
  46. img = Tensor(image[None, :, :, :])
  47. model = base.model(modelPath=self.model_path) # 初始化 base.model 类
  48. outputs = model.infer([img])[0] # 执行推理。输入数据类型:List[base.Tensor], 返回模型推理输出的List[base.Tensor]
  49. outputs = np.array(outputs.to_host()) # 将outputs移动到内存中,并且将base.Tensor类转为numpy array
  50. return outputs
  51. def predict_batch(self, image_paths):
  52. """
  53. 对指定图片列表进行批量推理
  54. :param image_paths: 待推理的图片路径列表
  55. :return: 批量推理结果
  56. """
  57. batch_images = []
  58. for image_path in image_paths:
  59. image = self.input_processing(image_path)
  60. batch_images.append(image)
  61. # 将批次图片堆叠成 (batch_size, 3, 224, 224) 维度
  62. batch_images = np.stack(batch_images)
  63. batch_images = Tensor(batch_images)
  64. # 执行推理
  65. base.mx_init() # 初始化 mxVision 资源
  66. model = base.model(modelPath=self.model_path) # 初始化 base.model 类
  67. outputs = model.infer([batch_images])[0] # 执行推理。输入数据类型:List[base.Tensor], 返回模型推理输出的List[base.Tensor]
  68. outputs = np.array(outputs.to_host()) # 将outputs移动到内存中,并且将base.Tensor类转为numpy array
  69. return outputs