<jupyter_start><jupyter_text>**Noise Injection**<jupyter_code># attack strength = norm of noise being inserted = relative norm * average norm of test images
rel_norms = np.arange(0.0001, 0.001, 0.0004).tolist() + np.arange(0.001, 0.01, 0.004).tolist() + np.arange(0.01, 0.1, 0.04).tolist() + np.arange(0.1, 0.4, 0.1).tolist()
atk_strengths = [rel_norm*fp_util.avg_l2_norm_imgs for rel_norm in rel_norms]
from sklearn.metrics import accuracy_score
marra_accuracies = []
yu_accuracies = []
# calculate accuracy of attribution at different attack strengths 
for atk_strength in atk_strengths:
  marra_preds = fp_util.get_predictions('Marra', attack_mode="gaussian", attack_strength=atk_strength)
  marra_accuracies.append(accuracy_score(fp_util.ground_truth, marra_preds))
  yu_preds = fp_util.get_predictions('Yu', attack_mode="gaussian", attack_strength=atk_strength)
  yu_accuracies.append(accuracy_score(fp_util.ground_truth, yu_preds))
np.save('marra_accuracies_gaussian.npy', marra_accuracies)
np.save('yu_accuracies_gaussian.npy', yu_accuracies)
import matplotlib.pyplot as plt

plot_util.plot_atk_accuracy_lines(atk_strengths, marra_accuracies, yu_accuracies)
plt.savefig('gaussian-eval.pdf', bbox_inches = 'tight')

#load in a test image to visualise the effect of the attack on
test_img = fp_util.load_test_images(1)[2]
atk_strengths_plot = [0, 0.01, 0.1, 1.0, 2.0, 4.0]
atk_images = []

gauss_noise = np.random.normal(0,0.1, test_img.shape)
for atk_strength in atk_strengths_plot:
  #scale perturbation to achieved desired attack strength
  noise_scaled = gauss_noise * atk_strength/np.linalg.norm(gauss_noise)
  atk_images.append(np.float32(np.clip(test_img + noise_scaled,0,1)))
plot_util.plot_atk_images(atk_strengths_plot, atk_images)
plt.savefig('gaussian-noise-imgs.pdf', bbox_inches = 'tight')
#get predictions to visualise as confusion matrices for further analysis
marra_preds = fp_util.get_predictions('Marra', attack_mode="gaussian", attack_strength=2)
yu_preds = fp_util.get_predictions('Yu', attack_mode="gaussian", attack_strength=2)
plt.rcParams['font.size']=14
plt.rcParams['xtick.labelsize'] = 11
plt.rcParams['ytick.labelsize'] = 11
  
fig, ax = plt.subplots(1,2, figsize = (15,5))

plot_util.plot_confusion_matrix('Handcrafted' , marra_preds, ax[0])
plot_util.plot_confusion_matrix('Learned', yu_preds, ax[1])

plt.suptitle('NOISE INJECTION', y = -0.1, fontsize = 20, fontweight='bold')
plt.savefig('gaussian-conf-matrix.pdf', bbox_inches = 'tight')<jupyter_output><empty_output><jupyter_text>**Blurring**<jupyter_code>from sklearn.metrics import accuracy_score
marra_accuracies = []
yu_accuracies = []
#attack strength = std dev of gaussian blur being applied
atk_strengths = np.arange(0.1,1.4, 0.1)

# calculate accuracy of attribution at different attack strengths 
for atk_strength in atk_strengths:
  marra_preds = fp_util.get_predictions('Marra', attack_mode="blur", attack_strength=atk_strength)
  marra_accuracies.append(accuracy_score(fp_util.ground_truth, marra_preds))
  yu_preds = fp_util.get_predictions('Yu', attack_mode="blur", attack_strength=atk_strength)
  yu_accuracies.append(accuracy_score(fp_util.ground_truth, yu_preds))
import matplotlib.pyplot as plt

plot_util.plot_atk_accuracy_lines(atk_strengths, marra_accuracies, yu_accuracies)
plt.savefig('blur-eval.pdf', bbox_inches = 'tight')

#get predictions to visualise as confusion matrices for further analysis
marra_preds = fp_util.get_predictions('Marra', attack_mode='blur', attack_strength = 0.7)
yu_preds = fp_util.get_predictions('Yu', attack_mode='blur', attack_strength = 0.7)
plt.rcParams['font.size']=14
plt.rcParams['xtick.labelsize'] = 11
plt.rcParams['ytick.labelsize'] = 11
  
fig, ax = plt.subplots(1,2, figsize = (15,5))

plot_util.plot_confusion_matrix('Handcrafted' , marra_preds, ax[0])
plot_util.plot_confusion_matrix('Learned', yu_preds, ax[1])

plt.suptitle('BLURRING', y = -0.1, fontsize = 20, fontweight='bold')
plt.savefig('blur-conf-matrix.pdf', bbox_inches = 'tight')

#load in a test image to visualise the effect of the attack on
test_img = fp_util.load_test_images(1)[2]
atk_strengths_plot = [0, 0.4, 0.6, 0.8, 1.0,1.4]
atk_images = []

for atk_strength in atk_strengths_plot:
  atk_images.append(fp_util.preprocess_blurring(test_img.reshape(1,28,28), atk_strength)[0].reshape(28,28))
plot_util.plot_atk_images(atk_strengths_plot, atk_images)
plt.savefig('blur-images.pdf', bbox_inches='tight')
from scipy import signal

def get_gaussian_filter(size, std):
    filter_1d = signal.gaussian(size, std=std).reshape(size, 1)
    return np.outer(filter_1d, filter_1d)
#visualise the filter used to implement blurring at different attack strengths

std_list = [2,3, 4]
fig, ax = plt.subplots(1,3, figsize=(10, 5))

for i in range(3):
  ax[i].imshow(get_gaussian_filter(15, std_list[i]), interpolation='none')
  ax[i].axis('off')

  ttl = ax[i].title
  ttl.set_text("$\sigma = {}$".format(std_list[i]))
  ttl.set_fontsize(22)
  ttl.set_position([0.5, -0.2])

plt.savefig('gaussian-std_vis.pdf', bbox_inches='tight')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13217597159202998, 'fraction_numerical': 0.029788913000591832, 'mean_word_length': 7.086124401913875, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '123197', 'license': 'MIT', 'n_tokens_mistral': 2096, 'n_tokens_neox': 1956, 'n_words': 348, 'path': 'src/notebooks/.ipynb_checkpoints/Robustness_untargeted-checkpoint.ipynb', 'repo_name': 'VasundharaAgarwal/GANFingerprinting', 'size': '5069'}