0

I have to add noise to MNIST images under the following constraints.

delta is the noise that I am allowed to add, epsilon and alpha can be a free choice.

Can someone tell me how can this be done using numpy?

My attempt:

def generate_weight_linf_l2_perturbations(alpha, epsilon, image):
delta_norm = 1 + epsilon
delta = np.zeros((image.size(dim=1),image.size(dim=2)))
while delta_norm > epsilon:
    delta = torch.from_numpy(np.random.uniform(0,epsilon,
                                               (image.size(dim=1),image.size(dim=2))))
    l2_norm = LA.norm(delta)
    linf_norm = LA.norm(delta, float('inf'))
    delta_norm = linf_norm + alpha*l2_norm
    print(l2_norm,linf_norm,delta_norm,epsilon)
    time.sleep(2.4)

image_pert = image + delta
return torch.clamp(image_pert,0,1)

I am using pytorch here but numpy solutions are also fine I can do a conversion between their datatypes.

leech
  • 21
  • 4
  • Im not quite sure I understand the term for delta - it is a recursive relation? how come you define delta using norms on delta? (it seems you use delta to define delta) – Hadar Sep 07 '22 at 21:28
  • delta is essentially the noise. And the constraint is on it's norm. Which is defined using the weighted average on it's l-2 and l-inf norms. Additionally, this weighted average or the norm of the delta should be less than a pre-determined epsilon. – leech Sep 08 '22 at 06:29

0 Answers0