-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassess_detectability.py
More file actions
412 lines (364 loc) · 13 KB
/
assess_detectability.py
File metadata and controls
412 lines (364 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
import argparse
import json
import os
import types
import torch
from sklearn.metrics import roc_auc_score
from transformers import (
AutoConfig,
AutoTokenizer,
BertForMaskedLM,
BertTokenizer,
T5ForConditionalGeneration,
T5Tokenizer,
set_seed,
AutoModelForCausalLM,
)
from cfg import (
get_dataset,
get_result_file_paths,
get_task,
load_results,
prepare_output_dir,
save_results,
)
from evaluation.dataset import BaseDataset
from evaluation.pipelines.detection import DetectionPipelineReturnType
from evaluation.pipelines.vllm_detection import WatermarkDetectionVLLMPipeline
from evaluation.tools.success_rate_calculator import (
DynamicThresholdSuccessRateCalculator,
)
from evaluation.tools.text_editor import (
BackTranslationTextEditor,
CodeGenerationV2TextEditor,
ContextAwareSynonymSubstitution,
DeepSeekParaphraser,
DipperParaphraser,
GPTParaphraser,
SynonymSubstitution,
TextEditor,
Translator,
TruncateTaskTextEditor,
WordDeletion,
WordInsertion,
OpenAI,
# YoudaoBackTranslationTextEditor,
DeepSeekBackTranslationTextEditor,
)
from utils.transformers_config import TransformersConfig
from watermark.auto_watermark import AutoWatermark
from watermark.base import BaseWatermark
def assess_detectability(
watermark: BaseWatermark,
watermark_texts: list[str],
nowatermark_texts: list[str],
dataset: BaseDataset,
task: str,
labels: list[str] = ["TPR", "TNR", "FPR", "FNR", "P", "R", "F1", "ACC"],
rule="target_fpr",
target_fpr: float = 0.01,
reverse: bool = False,
attack_name: str | None = None,
device: str = "cuda",
min_answer_tokens: int | None = None,
max_answer_tokens: int | None = None,
save_scores_path: str | None = None,
save_edited_path: str | None = None,
) -> dict[str, float]:
"""
评估水印的检测性
Args:
watermark: 水印对象
watermark_texts: 有水印的文本列表
nowatermark_texts: 无水印的文本列表
dataset: 数据集对象
labels: 评估指标标签
rule: 阈值确定规则
target_fpr: 目标假阳性率
reverse: 是否反转评估结果
attack_name: 攻击名称
device: 设备类型
min_answer_tokens: 最小token数量,小于此值的文本将被过滤
max_answer_tokens: 最大token数量,大于此值的文本将被截断
save_scores_path: 若不为 None,则把每条样本的得分与标签保存到该路径
Returns:
评估指标字典
"""
print("\n正在使用 ROC 曲线计算器评估水印检测效果...")
if not watermark_texts or not nowatermark_texts:
print("警告: 没有足够的有效文本进行 ROC 评估")
return {}
attack: TextEditor | None
match attack_name:
case "Word-D":
attack = WordDeletion(ratio=0.3)
case "Word-S":
attack = SynonymSubstitution(ratio=0.5)
case "Word-I":
attack = WordInsertion(ratio=0.3, attack_type="dispersed")
case "Word-I(Local)":
attack = WordInsertion(ratio=0.3, attack_type="local")
case "Word-S(Context)":
attack = ContextAwareSynonymSubstitution(
ratio=0.5,
tokenizer=BertTokenizer.from_pretrained(
"google-bert/bert-large-uncased"
),
model=BertForMaskedLM.from_pretrained(
"google-bert/bert-large-uncased"
).to(device),
)
case "Doc-P(GPT-3.5)":
attack = GPTParaphraser(
openai_model="gpt-3.5-turbo",
prompt="Please rewrite the following text: ",
)
case "Doc-P(Dipper)":
attack = DipperParaphraser(
tokenizer=T5Tokenizer.from_pretrained("google/t5-v1_1-xxl"),
model=T5ForConditionalGeneration.from_pretrained(
"kalpeshk2011/dipper-paraphraser-xxl",
device_map="auto",
),
lex_diversity=60,
order_diversity=0,
sent_interval=1,
max_new_tokens=100,
do_sample=True,
top_p=0.75,
top_k=None,
)
case "Doc-P-deepseek":
attack = DeepSeekParaphraser(
api_key="xxx",
temperature=0.0,
)
case "Translation-deepseek":
attack = DeepSeekBackTranslationTextEditor(
api_key="xxx",
temperature=0.0,
)
# case "Translation-Youdao":
# attack = YoudaoBackTranslationTextEditor(
# app_key="71068ae4f61fd8de",
# app_secret="mxFDTgIBxN3NjVVZkHfNzys2R6QtbDF5",
# )
case "Translation":
attack = BackTranslationTextEditor(
translate_to_intermediary=Translator(
from_lang="en", to_lang="zh"
).translate,
translate_to_source=Translator(
from_lang="zh", to_lang="en"
).translate,
)
case _:
attack = None
if attack is not None:
rule = "best"
text_editor_list: list[TextEditor] = [attack] if attack else []
if task == "code-generation":
text_editor_list.append(CodeGenerationV2TextEditor(language="python"))
text_editor_list.append(TruncateTaskTextEditor())
store_attacks = {"Translation", "Doc-P-deepseek", "Translation-Youdao","Translation-deepseek"}
need_store_edit = attack_name in store_attacks and save_edited_path is not None
pipeline_ret_type = (
DetectionPipelineReturnType.FULL
if need_store_edit
else DetectionPipelineReturnType.SCORES
)
watermark_pipeline = WatermarkDetectionVLLMPipeline(
dataset=dataset,
text_editor_list=text_editor_list,
show_progress=True,
return_type=pipeline_ret_type,
)
unwatermark_pipeline = WatermarkDetectionVLLMPipeline(
dataset=dataset,
text_editor_list=text_editor_list,
show_progress=True,
return_type=pipeline_ret_type,
)
watermarked_eval = watermark_pipeline.evaluate(
watermark,
watermark_texts,
min_answer_tokens=min_answer_tokens,
max_answer_tokens=max_answer_tokens,
)
nowatermarked_eval = unwatermark_pipeline.evaluate(
watermark,
nowatermark_texts,
min_answer_tokens=min_answer_tokens,
max_answer_tokens=max_answer_tokens,
)
if pipeline_ret_type is DetectionPipelineReturnType.FULL:
watermarked_scores = [
r.detect_result["score"] for r in watermarked_eval
]
nowatermarked_scores = [
r.detect_result["score"] for r in nowatermarked_eval
]
if need_store_edit:
edited_dict = {
"watermarked_edited": [r.edited_text for r in watermarked_eval],
"nowatermarked_edited": [
r.edited_text for r in nowatermarked_eval
],
}
os.makedirs(os.path.dirname(save_edited_path), exist_ok=True)
with open(save_edited_path, "w", encoding="utf-8") as f:
json.dump(edited_dict, f, ensure_ascii=False, indent=2)
print(f"改写文本已保存至 {save_edited_path}")
# ---------------------------------
else:
watermarked_scores = watermarked_eval
nowatermarked_scores = nowatermarked_eval
if not watermarked_scores or not nowatermarked_scores:
print("警告: 过滤后没有足够的有效文本进行 ROC 评估")
return {}
scores = watermarked_scores + nowatermarked_scores
if reverse:
scores = [-score for score in scores]
auroc = roc_auc_score(
[1] * len(watermarked_scores) + [0] * len(nowatermarked_scores),
scores,
)
calculator = DynamicThresholdSuccessRateCalculator(
labels=labels,
rule=rule,
target_fpr=target_fpr,
reverse=reverse,
)
metrics: dict[str, float] = calculator.calculate(
watermarked_scores, nowatermarked_scores
)
metrics["auroc"] = float(auroc)
if save_scores_path:
os.makedirs(os.path.dirname(save_scores_path), exist_ok=True)
sample_scores = {
"watermarked_scores": watermarked_scores,
"nowatermarked_scores": nowatermarked_scores,
"labels": [1] * len(watermarked_scores)
+ [0] * len(nowatermarked_scores),
"scores": scores,
}
with open(save_scores_path, "w", encoding="utf-8") as f:
json.dump(sample_scores, f, indent=2)
print(f"样本级得分已保存至 {save_scores_path}")
return metrics
def main(args) -> None:
"""主函数"""
device = "cuda" if torch.cuda.is_available() else "cpu"
set_seed(args.seed)
dataset = get_dataset(args.dataset_name, args.dataset_len, args.seed)
task = get_task(args.dataset_name)
need_model = args.algorithm.lower() in ["unbiased", "ewd", "sweet"]
model_obj = None
if need_model:
model_obj = AutoModelForCausalLM.from_pretrained(
args.model_path,
device_map="auto",
torch_dtype="auto"
)
transformers_config = TransformersConfig(
model=model_obj,
tokenizer=AutoTokenizer.from_pretrained(args.model_path),
vocab_size=AutoConfig.from_pretrained(args.model_path).vocab_size,
device=device,
)
watermark: BaseWatermark = AutoWatermark.load(
algorithm_name=args.algorithm,
algorithm_config=f"config/{args.algorithm}.json",
transformers_config=transformers_config,
)
output_dir = prepare_output_dir(
model_path=args.model_path,
dataset_len=args.dataset_len,
dataset_name=args.dataset_name,
)
file_paths = get_result_file_paths(output_dir, args.algorithm)
scores_save_path = os.path.join(
file_paths["output_dir"], "detection_sample_scores.json"
)
edited_save_path = os.path.join(
file_paths["output_dir"], "edited_texts.json"
)
watermark_results = load_results(file_paths["watermark_results"])
nowatermark_results = load_results(file_paths["no_watermark_results"])
if not watermark_results or not nowatermark_results:
print("错误: 未找到生成的文本结果,请先运行 generate.py 生成文本")
return
watermark_texts = watermark_results.get("answer_text", [])
nowatermark_texts = nowatermark_results.get("answer_text", [])
metrics = assess_detectability(
watermark=watermark,
watermark_texts=watermark_texts,
nowatermark_texts=nowatermark_texts,
dataset=dataset,
task=task,
labels=args.labels,
rule=args.rules,
target_fpr=args.target_fpr,
reverse=args.reverse,
attack_name=args.attack_name,
device=device,
min_answer_tokens=args.min_answer_tokens,
max_answer_tokens=args.max_answer_tokens,
save_scores_path=scores_save_path,
save_edited_path=edited_save_path,
)
print("\nROC 曲线水印检测评估指标:")
for metric, value in metrics.items():
print(f"{metric}: {value:.4f}")
save_results(file_paths["detection_results"], metrics)
print(f"评估结果已保存至 {file_paths['detection_results']}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--algorithm", type=str, default="KGW")
parser.add_argument(
"--model-path",
type=str,
default="Qwen/QwQ-32B",
)
parser.add_argument(
"--dataset-name",
type=str,
default="c4",
help="数据集类型",
)
parser.add_argument("--dataset-len", type=int, default=200)
parser.add_argument(
"--labels",
nargs="+",
default=["TPR", "TNR", "FPR", "FNR", "P", "R", "F1", "ACC"],
)
parser.add_argument("--rules", type=str, default="best")
parser.add_argument("--target-fpr", type=float, default=0.01)
parser.add_argument("--reverse", action="store_true")
parser.add_argument("--attack-name", type=str, default=None)
parser.add_argument(
"--min-answer-tokens",
type=int,
default=None,
help="最小token数量,小于此值的文本将被过滤",
)
parser.add_argument(
"--max-answer-tokens",
type=int,
default=None,
help="最大token数量,大于此值的文本将被截断",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="随机种子",
)
parser.add_argument(
"--save-edited-path",
type=str,
default=None,
help="若不为 None,则把改写后的文本保存到该路径",
)
args = parser.parse_args()
main(args)