|
@@ -2,13 +2,10 @@
|
|
|
AlexNet、VGG16、GoogleNet、ResNet基于pytorch框架的黑盒水印处理验证流程
|
|
|
"""
|
|
|
import os
|
|
|
-
|
|
|
import numpy as np
|
|
|
-from PIL import Image
|
|
|
-
|
|
|
from watermark_verify import logger
|
|
|
+from watermark_verify.inference.classification_inference import ClassificationInference
|
|
|
from watermark_verify.process.general_process_define import BlackBoxWatermarkProcessDefine
|
|
|
-import onnxruntime as ort
|
|
|
|
|
|
|
|
|
class ModelWatermarkProcessor(BlackBoxWatermarkProcessDefine):
|
|
@@ -32,65 +29,29 @@ class ModelWatermarkProcessor(BlackBoxWatermarkProcessDefine):
|
|
|
verify_result = self.verify_label()
|
|
|
return verify_result
|
|
|
|
|
|
- def preprocess_image(self, image_path):
|
|
|
- """
|
|
|
- 对输入图片进行预处理
|
|
|
- :param image_path: 图片路径
|
|
|
- :return: 图片经过处理完成的ndarray
|
|
|
- """
|
|
|
-
|
|
|
- image = Image.open(image_path).convert("RGB")
|
|
|
-
|
|
|
-
|
|
|
- image = image.resize((224, 224))
|
|
|
-
|
|
|
-
|
|
|
- image_array = np.array(image) / 255.0
|
|
|
-
|
|
|
-
|
|
|
- mean = np.array([0.485, 0.456, 0.406])
|
|
|
- std = np.array([0.229, 0.224, 0.225])
|
|
|
- image_array = (image_array - mean) / std
|
|
|
- image_array = image_array.transpose((2, 0, 1)).copy()
|
|
|
-
|
|
|
- return image_array.astype(np.float32)
|
|
|
-
|
|
|
def detect_secret_label(self, image_dir, target_class, threshold=0.6, batch_size=10):
|
|
|
"""
|
|
|
对模型使用触发集进行检查,判断是否存在黑盒模型水印,如果对嵌入水印的图片样本正确率高于阈值,证明模型存在黑盒水印
|
|
|
- :param transpose: 是否对输出ndarray进行维度转换,pytorch无需转换,tensorflow、keras需要转换
|
|
|
:param image_dir: 待推理的图像文件夹
|
|
|
:param target_class: 目标分类
|
|
|
:param threshold: 通过测试阈值
|
|
|
:param batch_size: 每批图片数量
|
|
|
:return: 检测结果
|
|
|
"""
|
|
|
- session = ort.InferenceSession(self.model_filename)
|
|
|
image_files = [f for f in os.listdir(image_dir) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
|
|
- results = {}
|
|
|
- input_name = session.get_inputs()[0].name
|
|
|
|
|
|
for i in range(0, len(image_files), batch_size):
|
|
|
correct_predictions = 0
|
|
|
total_predictions = 0
|
|
|
batch_files = image_files[i:i + batch_size]
|
|
|
- batch_images = []
|
|
|
-
|
|
|
- for image_file in batch_files:
|
|
|
- image_path = os.path.join(image_dir, image_file)
|
|
|
- image = self.preprocess_image(image_path)
|
|
|
- batch_images.append(image)
|
|
|
-
|
|
|
-
|
|
|
- batch_images = np.stack(batch_images)
|
|
|
+ batch_files = [os.path.join(image_dir, image_file) for image_file in batch_files]
|
|
|
|
|
|
|
|
|
- outputs = session.run(None, {input_name: batch_images})
|
|
|
+ outputs = ClassificationInference(self.model_filename).predict_batch(batch_files)
|
|
|
|
|
|
|
|
|
for j, image_file in enumerate(batch_files):
|
|
|
predicted_class = np.argmax(outputs[0][j])
|
|
|
- results[image_file] = predicted_class
|
|
|
total_predictions += 1
|
|
|
|
|
|
|