ssd_pytorch_blackbox_process.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. """
  2. ssd基于pytorch框架的黑盒水印处理验证流程
  3. """
  4. import os
  5. import numpy as np
  6. from PIL import Image
  7. from watermark_verify.inference.ssd_inference import SSDInference
  8. from watermark_verify.process.general_process_define import BlackBoxWatermarkProcessDefine
  9. from watermark_verify.tools import parse_qrcode_label_file
  10. from watermark_verify.tools.evaluate_tool import calculate_ciou
  11. class ModelWatermarkProcessor(BlackBoxWatermarkProcessDefine):
  12. def __init__(self, model_filename):
  13. super(ModelWatermarkProcessor, self).__init__(model_filename)
  14. def process(self) -> bool:
  15. # 获取权重文件,使用触发集进行模型推理, 将推理结果与触发集预先二维码保存位置进行比对,在误差范围内则进行下一步,否则返回False
  16. cls_image_mapping = parse_qrcode_label_file.parse_labels(self.qrcode_positions_file)
  17. accessed_cls = set()
  18. for cls, images in cls_image_mapping.items():
  19. for image in images:
  20. image_path = os.path.join(self.trigger_dir, image)
  21. detect_result = self.detect_secret_label(image_path, self.model_filename, self.qrcode_positions_file,
  22. (300, 300))
  23. if detect_result:
  24. accessed_cls.add(cls)
  25. break
  26. if not accessed_cls == set(cls_image_mapping.keys()): # 所有的分类都检测出模型水印,模型水印检测结果为True
  27. return False
  28. verify_result = self.verify_label() # 模型标签检测通过,进行标签验证
  29. return verify_result
  30. def detect_secret_label(self, image_path, model_file, watermark_txt, input_shape) -> bool:
  31. """
  32. 使用指定onnx文件进行预测并进行黑盒水印检测
  33. :param image_path: 输入图像路径
  34. :param model_file: 模型文件路径
  35. :param watermark_txt: 水印标签文件路径
  36. :param input_shape: 模型输入图像大小,tuple
  37. :return:
  38. """
  39. image = Image.open(image_path)
  40. image_shape = np.array(np.shape(image)[0:2])
  41. # 解析标签嵌入位置
  42. parse_label = parse_qrcode_label_file.load_watermark_info(watermark_txt, image_path)
  43. if len(parse_label) < 5:
  44. return False
  45. x_center, y_center, w, h, cls = parse_label
  46. # 计算绝对坐标
  47. height, width = image_shape
  48. x1 = (x_center - w / 2) * width
  49. y1 = (y_center - h / 2) * height
  50. x2 = (x_center + w / 2) * width
  51. y2 = (y_center + h / 2) * height
  52. watermark_box = [y1, x1, y2, x2, cls]
  53. if len(watermark_box) == 0:
  54. return False
  55. # 使用onnx进行推理
  56. results = SSDInference(self.model_filename).predict(image_path)
  57. # 检测模型是否存在黑盒水印
  58. if results is not None:
  59. detect_result = detect_watermark(results, watermark_box)
  60. return detect_result
  61. else:
  62. return False
  63. def detect_watermark(results, watermark_box, threshold=0.5):
  64. # 解析输出结果
  65. if len(results[0]) == 0:
  66. return False
  67. top_label = np.array(results[0][:, 4], dtype='int32')
  68. top_conf = results[0][:, 5]
  69. top_boxes = results[0][:, :4]
  70. for box, score, cls in zip(top_boxes, top_conf, top_label):
  71. wm_box_coords = watermark_box[:4]
  72. wm_cls = watermark_box[4]
  73. if cls == wm_cls:
  74. ciou = calculate_ciou(box, wm_box_coords)
  75. if ciou > threshold:
  76. return True
  77. return False