ssd_inference.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """
  2. 定义SSD推理流程
  3. """
  4. import numpy as np
  5. import onnxruntime as ort
  6. from PIL import Image
  7. from watermark_verify.utils.anchors import get_anchors
  8. from watermark_verify.utils.utils_bbox import BBoxUtility
  9. class SSDInference:
  10. def __init__(self, model_path, input_size=(300, 300), num_classes=20, num_iou=0.45, confidence=0.5, swap=(2, 0, 1)):
  11. """
  12. 初始化SSD模型推理流程
  13. :param model_path: 图像分类模型onnx文件路径
  14. :param input_size: 模型输入大小
  15. :param num_classes: 模型目标检测分类数
  16. :param num_iou: iou阈值
  17. :param confidence: 置信度阈值
  18. :param swap: 变换方式,pytorch需要进行轴变换(默认参数),tensorflow无需进行轴变换
  19. """
  20. self.model_path = model_path
  21. self.input_size = input_size
  22. self.swap = swap
  23. self.num_classes = num_classes
  24. self.nms_iou = num_iou
  25. self.confidence = confidence
  26. def input_processing(self, image_path):
  27. """
  28. 对输入图片进行预处理
  29. :param image_path: 图片路径
  30. :return: 图片经过处理完成的ndarray
  31. """
  32. image = Image.open(image_path)
  33. image_shape = np.array(np.shape(image)[0:2])
  34. # ---------------------------------------------------------#
  35. # 在这里将图像转换成RGB图像,防止灰度图在预测时报错。
  36. # 代码仅仅支持RGB图像的预测,所有其它类型的图像都会转化成RGB
  37. # ---------------------------------------------------------#
  38. if not (len(np.shape(image)) == 3 and np.shape(image)[2] == 3):
  39. image = image.convert('RGB')
  40. image_data = resize_image(image, self.input_size, False)
  41. MEANS = (104, 117, 123)
  42. image_data = np.array(image_data, dtype='float32')
  43. image_data = image_data - MEANS
  44. image_data = np.expand_dims(np.transpose(image_data, self.swap).copy(), 0)
  45. image_data = image_data.astype('float32')
  46. return image_data, image_shape
  47. def predict(self, image_path):
  48. """
  49. 对单张图片进行推理
  50. :param image_path: 图片路径
  51. :return: 推理结果
  52. """
  53. image_data, image_shape = self.input_processing(image_path)
  54. # 使用onnx文件进行推理
  55. session = ort.InferenceSession(self.model_path)
  56. ort_inputs = {session.get_inputs()[0].name: image_data}
  57. output = session.run(None, ort_inputs)
  58. output = self.output_processing(output, image_shape)
  59. return output
  60. def output_processing(self, outputs, image_shape):
  61. """
  62. 对模型输出进行后处理工作
  63. :param outputs: 模型原始输出
  64. :param image_shape: 原始图像大小
  65. :return: 经过处理完成的模型输出
  66. """
  67. # 处理模型预测输出
  68. bbox_util = BBoxUtility(self.num_classes)
  69. anchors = get_anchors(self.input_size)
  70. results = bbox_util.decode_box(outputs, anchors, image_shape, self.input_size, False, nms_iou=self.nms_iou,
  71. confidence=self.confidence)
  72. return results
  73. def resize_image(image, size, letterbox_image):
  74. iw, ih = image.size
  75. w, h = size
  76. if letterbox_image:
  77. scale = min(w / iw, h / ih)
  78. nw = int(iw * scale)
  79. nh = int(ih * scale)
  80. image = image.resize((nw, nh), Image.BICUBIC)
  81. new_image = Image.new('RGB', size, (128, 128, 128))
  82. new_image.paste(image, ((w - nw) // 2, (h - nh) // 2))
  83. else:
  84. new_image = image.resize((w, h), Image.BICUBIC)
  85. return new_image