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.