Bläddra i källkod

增加白盒水印无法检测异常提示,修改vgg白盒水印验证使用的处理类

liyan 4 dagar sedan
förälder
incheckning
620030b8c5

+ 1 - 1
README.md

@@ -128,7 +128,7 @@ model_watermark_detect
         │   ├── faster-rcnn_pytorch_blackbox_process.py
         │   ├── faster-rcnn_pytorch_whitebox_process.py
         │   ├── general_process_define.py
-        │   ├── googlenet_all_whitebox_process.py
+        │   ├── googlenet_vgg_all_whitebox_process.py
         │   ├── ssd_pytorch_blackbox_process.py
         │   ├── ssd_pytorch_whitebox_process.py
         │   ├── yolox_pytorch_blackbox_process.py

+ 6 - 38
watermark_verify/process/general_process_define.py

@@ -38,42 +38,8 @@ class WhiteBoxWatermarkProcessDefine:
         self.public_key = public_key
 
     def extract_label(self, scope, indices):
-        import onnx
-        import numpy as np
-        """
-        标签提取
-        :return: 提取出的密码标签
-        """
-        model = onnx.load(self.model_filename)  # 加载 ONNX 模型
-        graph = model.graph  # 获取模型图(graph)
-        weights = []
-        # 遍历图中的节点
-        for node in graph.node:
-            if node.op_type == "Conv":  # 查找嵌入白盒水印的卷积层节点,卷积层名字可解析onnx文件后查找得到
-                weight_name = node.input[1]  # 通常第一个是输入x、第二个输入是权重w、第三个是偏置b
-                for initializer in graph.initializer:
-                    if initializer.name == weight_name:
-                        # 获取权重数据
-                        weights.append(onnx.numpy_helper.to_array(initializer))
-        if indices:
-            weights = [weights[i] for i in indices if i < len(weights)]
-        else:
-            start, end = scope
-            weights = weights[start:end]
-        weights = [np.transpose(weight, (2, 3, 1, 0)) for weight in
-                   weights]  # 将onnx文件的权重格式由(out_channels, in_channels, kernel_height, kernel_width),转换为(kernel_height, kernel_width, in_channels, out_channels)
-        x_random = np.load(self.x_random_file)
-        # 计算嵌入的白盒水印
-        w = np.concatenate(
-            [np.mean(x, axis=3).reshape(-1) for x in weights])  # 处理传入的卷积层的权重参数,对卷积核进行按out_channels维度取平均,拉直
-        mm = np.dot(x_random, w.reshape((w.shape[0], 1)))  # 进行矩阵乘法
-        sigmoid_mm = 1 / (1 + np.exp(-mm))  # 计算 Sigmoid 函数
-        prob = sigmoid_mm.flatten()  # 拉直运算结果
-        decode = np.where(prob > 0.5, 1, 0)  # 获取最终字节序列
-        code_string = ''.join([str(x) for x in decode.tolist()])  # 转换为字节序列字符串,类似"0100010011111"
-        # 将字节序列字符串转换为字符串
-        secret_label = ''.join(chr(int(code_string[i:i + 8], 2)) for i in range(0, len(code_string), 8))
-        return secret_label
+        # 华为昇腾平台权重文件无法访问内部权重,故无法实现白盒水印验证
+        raise RuntimeError("OM权重文件无法获取权重信息,无法实现白盒水印检测")
 
     def verify_label(self, scope=(0, 3), indices=None) -> bool:
         """
@@ -82,8 +48,10 @@ class WhiteBoxWatermarkProcessDefine:
         :param indices: 如果指定该参数,会从卷积层指定索引列表中进行权重获取,scope参数无效
         :return: 标签验证结果
         """
-        # 华为昇腾平台权重文件无法访问内部权重,故无法实现白盒水印验证
-        return False
+        secret_label = self.extract_label(scope, indices)
+        label_check_result = secret_label_func.verify_secret_label(secret_label=secret_label,
+                                                                   public_key=self.public_key)
+        return label_check_result
 
 
 class BlackBoxWatermarkProcessDefine:

watermark_verify/process/googlenet_all_whitebox_process.py → watermark_verify/process/googlenet_vgg_all_whitebox_process.py