AI-RESEARCHER-2024 commited on
Commit
de076a1
·
verified ·
1 Parent(s): 1b68bd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -2
app.py CHANGED
@@ -9,6 +9,7 @@ from PIL import Image
9
 
10
  # I/O image dimensions for display
11
  DIMS = (100, 100)
 
12
  # Load the trained model
13
  model = load_model('mnist_model.h5')
14
 
@@ -78,9 +79,39 @@ def gradio_mask(image, steps, increment=1):
78
  modified_image, original_label, predicted_label = progressively_mask_image(image, steps, increment)
79
  return modified_image, f"Original Label: {original_label}, New Label: {predicted_label}"
80
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81
  class GradioInterface:
82
  def __init__(self):
83
  self.preloaded_examples = mnist_examples
 
84
 
85
  def create_interface(self):
86
  app_styles = """
@@ -224,12 +255,16 @@ class GradioInterface:
224
  with gr.Column():
225
  input_image = gr.Image(label="Input Image", type="pil", format="png", elem_classes="image-preview")
226
  steps_input = gr.Slider(minimum=1, maximum=100, label="Attributions Drop Percentage", step=1, value=5)
227
- #increment_input = gr.Slider(minimum=1, maximum=100, label="increment", step=1, value=5)
228
  examples = gr.Examples(
229
  examples=self.preloaded_examples,
230
  inputs=input_image,
231
  label="MNIST Examples"
232
  )
 
 
 
 
 
233
  with gr.Column():
234
  result = gr.Image(label="Result", elem_classes="image-preview")
235
  prediction_details = gr.Textbox(label="Prediction Details")
@@ -237,7 +272,6 @@ class GradioInterface:
237
 
238
  run_button.click(
239
  fn=gradio_mask,
240
- #inputs=[input_image, steps_input, increment_input],
241
  inputs=[input_image, steps_input],
242
  outputs=[result, prediction_details],
243
  )
 
9
 
10
  # I/O image dimensions for display
11
  DIMS = (100, 100)
12
+
13
  # Load the trained model
14
  model = load_model('mnist_model.h5')
15
 
 
79
  modified_image, original_label, predicted_label = progressively_mask_image(image, steps, increment)
80
  return modified_image, f"Original Label: {original_label}, New Label: {predicted_label}"
81
 
82
+ # FGSM attack function
83
+ def fgsm_attack(image, epsilon, data_grad):
84
+ sign_of_grad = tf.sign(data_grad)
85
+ perturbed_image = image + epsilon * sign_of_grad
86
+ perturbed_image = tf.clip_by_value(perturbed_image, 0, 1)
87
+ return perturbed_image
88
+
89
+ # Create adversarial example function
90
+ def create_adversarial_pattern(input_image, input_label):
91
+ with tf.GradientTape() as tape:
92
+ tape.watch(input_image)
93
+ prediction = model(input_image)
94
+ loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)(input_label, prediction)
95
+ gradient = tape.gradient(loss, input_image)
96
+ return gradient
97
+
98
+ # Generate adversarial examples
99
+ epsilon = 0.1 # Tweak epsilon to change the intensity of perturbation
100
+ adversarial_examples = []
101
+
102
+ for i in range(10):
103
+ img = tf.convert_to_tensor(test_images[i:i+1])
104
+ label = test_labels[i:i+1]
105
+ label = tf.reshape(label, [1, 1])
106
+
107
+ perturbations = create_adversarial_pattern(img, label)
108
+ adv_x = fgsm_attack(img, epsilon, perturbations)
109
+ adversarial_examples.append([cv2.resize(adv_x.numpy().squeeze(), DIMS)])
110
+
111
  class GradioInterface:
112
  def __init__(self):
113
  self.preloaded_examples = mnist_examples
114
+ self.adversarial_examples = adversarial_examples
115
 
116
  def create_interface(self):
117
  app_styles = """
 
255
  with gr.Column():
256
  input_image = gr.Image(label="Input Image", type="pil", format="png", elem_classes="image-preview")
257
  steps_input = gr.Slider(minimum=1, maximum=100, label="Attributions Drop Percentage", step=1, value=5)
 
258
  examples = gr.Examples(
259
  examples=self.preloaded_examples,
260
  inputs=input_image,
261
  label="MNIST Examples"
262
  )
263
+ adv_examples = gr.Examples(
264
+ examples=self.adversarial_examples,
265
+ inputs=input_image,
266
+ label="Adversarial Examples"
267
+ )
268
  with gr.Column():
269
  result = gr.Image(label="Result", elem_classes="image-preview")
270
  prediction_details = gr.Textbox(label="Prediction Details")
 
272
 
273
  run_button.click(
274
  fn=gradio_mask,
 
275
  inputs=[input_image, steps_input],
276
  outputs=[result, prediction_details],
277
  )