classification_inference.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.to_host()
  50. outputs = np.array(outputs) # 将outputs移动到内存中,并且将base.Tensor类转为numpy array
  51. return outputs
  52. def predict_batch(self, image_paths):
  53. """
  54. 对指定图片列表进行批量推理
  55. :param image_paths: 待推理的图片路径列表
  56. :return: 批量推理结果
  57. """
  58. batch_images = []
  59. for image_path in image_paths:
  60. image = self.input_processing(image_path)
  61. batch_images.append(image)
  62. # 将批次图片堆叠成 (batch_size, 3, 224, 224) 维度
  63. batch_images = np.stack(batch_images)
  64. batch_images = Tensor(batch_images)
  65. # 执行推理
  66. base.mx_init() # 初始化 mxVision 资源
  67. model = base.model(modelPath=self.model_path) # 初始化 base.model 类
  68. outputs = model.infer([batch_images])[0] # 执行推理。输入数据类型:List[base.Tensor], 返回模型推理输出的List[base.Tensor]
  69. outputs = np.array(outputs.to_host()) # 将outputs移动到内存中,并且将base.Tensor类转为numpy array
  70. return outputs