Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -123,7 +123,7 @@ class MultiLayerGradCAM:
|
|
123 |
for handle in self.handles:
|
124 |
handle.remove()
|
125 |
|
126 |
-
gradcam = MultiLayerGradCAM(model, target_layers=['
|
127 |
|
128 |
# Image transformation function
|
129 |
def transform_image(image):
|
@@ -131,8 +131,8 @@ def transform_image(image):
|
|
131 |
mean, std = [0.4914, 0.4822, 0.4465], [0.247, 0.243, 0.261]
|
132 |
img_size=224
|
133 |
transform = transforms.Compose([ #IMAGENET
|
134 |
-
transforms.Resize(
|
135 |
-
transforms.CenterCrop(224), # Crop the center 224x224 region
|
136 |
transforms.ToTensor(), # Convert to tensor (scales to [0,1])
|
137 |
transforms.Normalize( # Normalize using ImageNet mean & std
|
138 |
mean=[0.485, 0.456, 0.406],
|
@@ -183,11 +183,11 @@ def superimpose_images(base_image, overlay_image, alpha):
|
|
183 |
|
184 |
return Image.fromarray(blended_array)
|
185 |
|
186 |
-
def generate_adversarial(input_image, orig_pred, epsilon=
|
187 |
"""Generate adversarial example"""
|
188 |
input_image = transform_image(input_image)
|
189 |
delta = torch.zeros_like(input_image, requires_grad=True)
|
190 |
-
opt = optim.SGD([delta], lr=
|
191 |
|
192 |
for _ in range(steps):
|
193 |
perturbed = torch.clamp(input_image + delta, 0.0, 1.0)
|
@@ -199,8 +199,13 @@ def generate_adversarial(input_image, orig_pred, epsilon=12/255, steps=50):
|
|
199 |
loss.backward()
|
200 |
opt.step()
|
201 |
delta.data.clamp_(-epsilon, epsilon)
|
|
|
|
|
|
|
202 |
|
203 |
-
|
|
|
|
|
204 |
|
205 |
# Prediction function
|
206 |
def predict(image, brightness, contrast, hue, overlay_image, alpha, adversarial_switch):
|
|
|
123 |
for handle in self.handles:
|
124 |
handle.remove()
|
125 |
|
126 |
+
gradcam = MultiLayerGradCAM(model, target_layers=['layer4'])
|
127 |
|
128 |
# Image transformation function
|
129 |
def transform_image(image):
|
|
|
131 |
mean, std = [0.4914, 0.4822, 0.4465], [0.247, 0.243, 0.261]
|
132 |
img_size=224
|
133 |
transform = transforms.Compose([ #IMAGENET
|
134 |
+
transforms.Resize((224,224)), # Resize shorter side to 256, keeping aspect ratio
|
135 |
+
# transforms.CenterCrop(224), # Crop the center 224x224 region
|
136 |
transforms.ToTensor(), # Convert to tensor (scales to [0,1])
|
137 |
transforms.Normalize( # Normalize using ImageNet mean & std
|
138 |
mean=[0.485, 0.456, 0.406],
|
|
|
183 |
|
184 |
return Image.fromarray(blended_array)
|
185 |
|
186 |
+
def generate_adversarial(input_image, orig_pred, epsilon=20/255, steps=500):
|
187 |
"""Generate adversarial example"""
|
188 |
input_image = transform_image(input_image)
|
189 |
delta = torch.zeros_like(input_image, requires_grad=True)
|
190 |
+
opt = optim.SGD([delta], lr=2e-1, momentum=0.9)
|
191 |
|
192 |
for _ in range(steps):
|
193 |
perturbed = torch.clamp(input_image + delta, 0.0, 1.0)
|
|
|
199 |
loss.backward()
|
200 |
opt.step()
|
201 |
delta.data.clamp_(-epsilon, epsilon)
|
202 |
+
x = input_image + delta # Compute the raw sum
|
203 |
+
x_min = x.amin(dim=(1, 2, 3), keepdim=True) # Per-image min
|
204 |
+
x_max = x.amax(dim=(1, 2, 3), keepdim=True) # Per-image max
|
205 |
|
206 |
+
output = (x - x_min) / (x_max - x_min + 1e-8) # Avoid division by zero
|
207 |
+
# return output
|
208 |
+
return output
|
209 |
|
210 |
# Prediction function
|
211 |
def predict(image, brightness, contrast, hue, overlay_image, alpha, adversarial_switch):
|