yolox_pytorch_blackbox_process.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """
  2. yolox基于pytorch框架的黑盒水印处理验证流程
  3. """
  4. import os
  5. import cv2
  6. from watermark_verify.inference.yolox_inference import YOLOXInference
  7. from watermark_verify.process.general_process_define import BlackBoxWatermarkProcessDefine
  8. from watermark_verify.tools import parse_qrcode_label_file
  9. from watermark_verify.tools.evaluate_tool import calculate_ciou
  10. class ModelWatermarkProcessor(BlackBoxWatermarkProcessDefine):
  11. def __init__(self, model_filename):
  12. super(ModelWatermarkProcessor, self).__init__(model_filename)
  13. def process(self) -> bool:
  14. """
  15. 根据流程定义进行处理,并返回模型标签验证结果
  16. :return: 模型标签验证结果
  17. """
  18. # 获取权重文件,使用触发集进行模型推理, 将推理结果与触发集预先二维码保存位置进行比对,在误差范围内则进行下一步,否则返回False
  19. cls_image_mapping = parse_qrcode_label_file.parse_labels(self.qrcode_positions_file)
  20. accessed_cls = set()
  21. for cls, images in cls_image_mapping.items():
  22. for image in images:
  23. image_path = os.path.join(self.trigger_dir, image)
  24. detect_result = self.detect_secret_label(image_path, self.qrcode_positions_file, (640, 640))
  25. if detect_result:
  26. accessed_cls.add(cls)
  27. break
  28. if not accessed_cls == set(cls_image_mapping.keys()): # 所有的分类都检测出模型水印,模型水印检测结果为True
  29. return False
  30. verify_result = self.verify_label() # 模型标签检测通过,进行标签验证
  31. return verify_result
  32. def detect_secret_label(self, image_path, watermark_txt, input_shape) -> bool:
  33. """
  34. 对模型使用触发集进行检查,判断是否存在黑盒模型水印,如果对嵌入水印的图片样本正确率高于阈值,证明模型存在黑盒水印
  35. :param image_path: 输入图像路径
  36. :param watermark_txt: 水印标签文件路径
  37. :param input_shape: 模型输入图像大小,tuple
  38. :return: 检测结果
  39. """
  40. img = cv2.imread(image_path)
  41. height, width, channels = img.shape
  42. x_center, y_center, w, h, cls = parse_qrcode_label_file.load_watermark_info(watermark_txt, image_path)
  43. # 计算绝对坐标
  44. x1 = (x_center - w / 2) * width
  45. y1 = (y_center - h / 2) * height
  46. x2 = (x_center + w / 2) * width
  47. y2 = (y_center + h / 2) * height
  48. watermark_box = [x1, y1, x2, y2, cls]
  49. if len(watermark_box) == 0:
  50. return False
  51. dets = YOLOXInference(self.model_filename,input_size=input_shape).predict(image_path)
  52. if dets is not None:
  53. detect_result = detect_watermark(dets, watermark_box)
  54. return detect_result
  55. else:
  56. return False
  57. def detect_watermark(dets, watermark_box, threshold=0.5):
  58. if dets.size == 0: # 检查是否为空
  59. return False
  60. for box, score, cls in zip(dets[:, :4], dets[:, 4], dets[:, 5]):
  61. wm_box_coords = watermark_box[:4]
  62. wm_cls = watermark_box[4]
  63. if cls == wm_cls:
  64. ciou = calculate_ciou(box, wm_box_coords)
  65. if ciou > threshold:
  66. return True
  67. return False