Vivien Chappelier commited on
Commit
bd9d89a
1 Parent(s): 405da28

change demo workflow

Browse files
Files changed (1) hide show
  1. app.py +51 -18
app.py CHANGED
@@ -53,7 +53,7 @@ class BZHStableSignatureDemo(object):
53
  output = self.pipe(prompt, num_inference_steps=4, guidance_scale=0.0, output_type="pil")
54
  return output.images[0]
55
 
56
- def attack_detect(self, img, jpeg_compression, downscale, crop, saturation):
57
 
58
  img = img.convert("RGB")
59
 
@@ -81,10 +81,25 @@ class BZHStableSignatureDemo(object):
81
 
82
  converter = ImageEnhance.Color(img)
83
  img = converter.enhance(saturation)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
 
85
  # send to detection API and apply JPEG compression attack
86
  mf = io.BytesIO()
87
- img.save(mf, format='JPEG', quality=jpeg_compression) # includes JPEG attack
88
  b64 = base64.b64encode(mf.getvalue())
89
  data = {
90
  'image': b64.decode('utf8')
@@ -100,16 +115,15 @@ class BZHStableSignatureDemo(object):
100
  data = response.json()
101
  pvalue = data['p-value']
102
 
103
- mf.seek(0)
104
- img0 = Image.open(mf) # reload to show JPEG attack
105
-
106
  result = "No watermark detected."
107
  rpv = 10**int(math.log10(pvalue))
108
  if pvalue < 1e-3:
109
- result = "Watermark detected with low confidence (p-value<%.0e)" % rpv
110
  if pvalue < 1e-9:
111
- result = "Watermark detected with high confidence (p-value<%.0e)" % rpv
112
- return (img0, result)
 
 
113
 
114
  def interface():
115
  prompt = "sailing ship in storm by Rembrandt"
@@ -124,6 +138,8 @@ def interface():
124
  the VAE decoder of StableDiffusion is fine-tuned to produce images including a specific invisible watermark. We combined
125
  this method with a demo version of [IMATAG](https://www.imatag.com/)'s in-house decoder. The watermarking system operates in zero-bit mode for improved robustness.""")
126
 
 
 
127
  with gr.Row():
128
  inp = gr.Textbox(label="Prompt", value=prompt)
129
  seed = gr.Number(label="Seed", precision=0)
@@ -131,19 +147,36 @@ def interface():
131
  with gr.Row():
132
  btn1 = gr.Button("Generate")
133
  with gr.Row():
134
- watermarked_image = gr.Image(type="pil", width=512, height=512)
 
 
 
 
135
  with gr.Column():
136
- gr.Markdown("""With these controls you may alter the generated image before detection. You may also upload your own edited image instead.""")
137
- downscale = gr.Slider(1, 3, value=1, step=0.1, label="Downscale ratio")
138
- crop = gr.Slider(0, 0.9, value=0, step=0.01, label="Random crop ratio")
139
- saturation = gr.Slider(0, 2, value=1, step=0.1, label="Color saturation")
140
- jpeg_compression = gr.Slider(value=100, step=5, label="JPEG quality")
141
- btn2 = gr.Button("Modify & Detect")
142
  with gr.Row():
143
- attacked_image = gr.Image(type="pil", width=256)
144
- detection_label = gr.Label(label="Detection info")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  btn1.click(fn=backend.generate, inputs=[mode, seed, inp], outputs=[watermarked_image], api_name="generate")
146
- btn2.click(fn=backend.attack_detect, inputs=[watermarked_image, jpeg_compression, downscale, crop, saturation], outputs=[attacked_image, detection_label], api_name="detect")
 
147
 
148
  return demo
149
 
 
53
  output = self.pipe(prompt, num_inference_steps=4, guidance_scale=0.0, output_type="pil")
54
  return output.images[0]
55
 
56
+ def attack(self, img, jpeg_compression, downscale, crop, saturation, brightness, contrast):
57
 
58
  img = img.convert("RGB")
59
 
 
81
 
82
  converter = ImageEnhance.Color(img)
83
  img = converter.enhance(saturation)
84
+
85
+ converter = ImageEnhance.Brightness(img)
86
+ img = converter.enhance(brightness)
87
+
88
+ converter = ImageEnhance.Contrast(img)
89
+ img = converter.enhance(contrast)
90
+
91
+ # JPEG attack
92
+ mf = io.BytesIO()
93
+ img.save(mf, format='JPEG', quality=jpeg_compression)
94
+ mf.seek(0)
95
+ img = Image.open(mf)
96
+
97
+ return img
98
 
99
+ def detect(self, img):
100
  # send to detection API and apply JPEG compression attack
101
  mf = io.BytesIO()
102
+ img.save(mf, format='PNG')
103
  b64 = base64.b64encode(mf.getvalue())
104
  data = {
105
  'image': b64.decode('utf8')
 
115
  data = response.json()
116
  pvalue = data['p-value']
117
 
 
 
 
118
  result = "No watermark detected."
119
  rpv = 10**int(math.log10(pvalue))
120
  if pvalue < 1e-3:
121
+ result = "Watermark detected with low confidence" # (p-value<%.0e)" % rpv
122
  if pvalue < 1e-9:
123
+ result = "Watermark detected with high confidence" # (p-value<%.0e)" % rpv
124
+ score = min(int(-math.log10(pvalue)), 10)
125
+ #print("score = ", score)
126
+ return { result: score/10 }
127
 
128
  def interface():
129
  prompt = "sailing ship in storm by Rembrandt"
 
138
  the VAE decoder of StableDiffusion is fine-tuned to produce images including a specific invisible watermark. We combined
139
  this method with a demo version of [IMATAG](https://www.imatag.com/)'s in-house decoder. The watermarking system operates in zero-bit mode for improved robustness.""")
140
 
141
+ gr.Markdown("""## 1. Generate
142
+ Select a watermarking strength and generate images with StableDiffusion-XL Turbo from prompt and seed as usual.""")
143
  with gr.Row():
144
  inp = gr.Textbox(label="Prompt", value=prompt)
145
  seed = gr.Number(label="Seed", precision=0)
 
147
  with gr.Row():
148
  btn1 = gr.Button("Generate")
149
  with gr.Row():
150
+ watermarked_image = gr.Image(type="pil", width=512, height=512, sources=[], interactive=False)
151
+
152
+ gr.Markdown("""## 2. Edit
153
+ With these controls you may alter the generated image before detection. You may also upload your own edited image instead.""")
154
+ with gr.Row():
155
  with gr.Column():
 
 
 
 
 
 
156
  with gr.Row():
157
+ downscale = gr.Slider(1, 3, value=1, step=0.1, label="Downscale ratio")
158
+ crop = gr.Slider(0, 0.9, value=0, step=0.01, label="Random crop ratio")
159
+ with gr.Row():
160
+ brightness = gr.Slider(0, 2, value=1, step=0.1, label="Brightness")
161
+ contrast = gr.Slider(0, 2, value=1, step=0.1, label="Contrast")
162
+ with gr.Row():
163
+ saturation = gr.Slider(0, 2, value=1, step=0.1, label="Color saturation")
164
+ jpeg_compression = gr.Slider(value=100, step=5, label="JPEG quality")
165
+ btn2 = gr.Button("Edit")
166
+ with gr.Row():
167
+ attacked_image = gr.Image(type="pil", width=512, sources=['upload', 'clipboard'])
168
+
169
+ gr.Markdown("""## 3. Detect
170
+ Detect the watermark on the altered image. Watermark may not be detected if the image is altered too strongly.
171
+ """)
172
+ with gr.Row():
173
+ btn3 = gr.Button("Detect")
174
+ with gr.Row():
175
+ detection_label = gr.Label(label="Detection info", show_label=False)
176
+
177
  btn1.click(fn=backend.generate, inputs=[mode, seed, inp], outputs=[watermarked_image], api_name="generate")
178
+ btn2.click(fn=backend.attack, inputs=[watermarked_image, jpeg_compression, downscale, crop, saturation, brightness, contrast], outputs=[attacked_image], api_name="attack")
179
+ btn3.click(fn=backend.detect, inputs=[attacked_image], outputs=[detection_label], api_name="detect")
180
 
181
  return demo
182