Vivien Chappelier commited on
Commit
0255ddc
·
1 Parent(s): 4dc18cc
Files changed (3) hide show
  1. calibration.safetensors +3 -0
  2. config.json +4 -2
  3. export_detector.py +63 -0
calibration.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f0eac4b0be8eb96b2a6fc7596727954ebe600951b3c4719b456948200d75f58e
3
+ size 1999934
config.json CHANGED
@@ -19,10 +19,12 @@
19
  512
20
  ],
21
  "id2label": {
22
- "0": "LABEL_0"
 
23
  },
24
  "label2id": {
25
- "LABEL_0": 0
 
26
  },
27
  "layer_type": "basic",
28
  "model_type": "resnet",
 
19
  512
20
  ],
21
  "id2label": {
22
+ "0": "Watermarked",
23
+ "1": "No watermark"
24
  },
25
  "label2id": {
26
+ "Watermarked": 0,
27
+ "No watermark": 1
28
  },
29
  "layer_type": "basic",
30
  "model_type": "resnet",
export_detector.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ import sys
4
+ from safetensors.torch import save_file
5
+ from PIL import Image
6
+
7
+ # usage:
8
+ # export_detector.py logits.txt bzhdet/
9
+
10
+ # read logits file
11
+ data=(np.asarray([float(x) for x in open(sys.argv[1]).readlines()]))
12
+
13
+ # negate for consistency with "1" = "watermarked"
14
+ data = -data
15
+
16
+ # sort and convert to safetensors format
17
+ data = np.sort(data)
18
+ data_min = data.min()
19
+ data_max = data.max()
20
+ data_len = data.shape[0]
21
+ Ts = []
22
+ for i in range(1,7):
23
+ t = 10**-i
24
+ T = data[int(t * data_len)]
25
+ print("t for p=%e: %f : %e" % (t, T, (data < T).mean()))
26
+ Ts.append(T)
27
+ zero = np.where(np.diff(np.sign(data)))[0] + 1
28
+ t = zero / data_len
29
+ T = data[int(t * data_len)]
30
+ print("t for p=%e: %f : %e" % (t, T, (data < T).mean()))
31
+
32
+ data = torch.tensor(data, dtype=torch.float16)
33
+ save_file({
34
+ "logits": data
35
+ }, "calibration.safetensors")
36
+
37
+ # read the detector
38
+ from transformers import AutoModelForImageClassification, BlipImageProcessor
39
+
40
+ detector_path = sys.argv[2]
41
+
42
+ image_processor = BlipImageProcessor(do_resize=True,
43
+ size={ "width": 512, "height": 512 },
44
+ do_normalize=True,
45
+ image_mean = [ 0.5, 0.5, 0.5 ],
46
+ image_std = [ 0.5, 0.5, 0.5 ])
47
+
48
+ detector = AutoModelForImageClassification.from_pretrained(detector_path)
49
+
50
+ # make it output "1" for "watermarked"
51
+ detector.eval()
52
+ with torch.no_grad():
53
+ detector.classifier[1].weight.copy_(-detector.classifier[1].weight)
54
+
55
+ #detector.push_to_hub("imatag/stable-signature-bzh-detector-resnet18")
56
+ #image_processor.push_to_hub("imatag/stable-signature-bzh-detector-resnet18")
57
+ examples = ['examples/not_watermarked.png', 'examples/watermarked.png']
58
+
59
+ imgs = [ Image.open(example).convert("RGB") for example in examples ]
60
+ inputs = image_processor(imgs, return_tensors="pt")
61
+ with torch.no_grad():
62
+ p = torch.sigmoid(detector(**inputs).logits)
63
+ print(p)