HIGGS_SAINT_FI.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. # Not everything from this is used
  2. import numpy as np
  3. import pandas as pd
  4. from sklearn.datasets import fetch_openml
  5. from sklearn.model_selection import train_test_split
  6. from sklearn.metrics import accuracy_score, log_loss
  7. from sklearn.preprocessing import LabelEncoder
  8. import os
  9. import wget
  10. from pathlib import Path
  11. import shutil
  12. import gzip
  13. from matplotlib import pyplot as plt
  14. import torch
  15. import random
  16. import math
  17. from SAINT.saintLib import SaintLib
  18. # Experiment settings
  19. EPOCHS = 8
  20. RERUNS = 3 # How many times to redo the same setting
  21. # Backdoor settings
  22. target=["target"]
  23. backdoorFeatures = [] # will be set dynamically
  24. backdoorTriggerValues = [] # will be set to +10% out of bounds
  25. targetLabel = 1
  26. poisoningRates = [0.0001, 0.0005, 0.001, 0.005, 0.01]
  27. # Model settings
  28. SAINT_ARGS = ["--task", "binary", "--epochs", str(EPOCHS), "--batchsize", "512", "--embedding_size", "32", "--device", "cuda:0"]
  29. # Load dataset
  30. data = pd.read_pickle("data/HIGGS/processed-small.pkl")
  31. # Setup data
  32. cat_cols = []
  33. num_cols = [col for col in data.columns.tolist() if col not in cat_cols]
  34. num_cols.remove(target[0])
  35. feature_columns = (
  36. num_cols + cat_cols + target)
  37. # Experiment setup
  38. def GenerateTrigger(df, poisoningRate, backdoorTriggerValues, targetLabel):
  39. rows_with_trigger = df.sample(frac=poisoningRate)
  40. rows_with_trigger[backdoorFeatures] = backdoorTriggerValues
  41. rows_with_trigger[target] = targetLabel
  42. return rows_with_trigger
  43. def GenerateBackdoorTrigger(df, backdoorTriggerValues, targetLabel):
  44. df[backdoorFeatures] = backdoorTriggerValues
  45. df[target] = targetLabel
  46. return df
  47. def doExperiment(poisoningRate, backdoorFeatures, backdoorTriggerValues, targetLabel, runIdx):
  48. # Load dataset
  49. # Changes to output df will not influence input df
  50. train_and_valid, test = train_test_split(data, stratify=data[target[0]], test_size=0.2, random_state=runIdx)
  51. # Apply backdoor to train and valid data
  52. random.seed(runIdx)
  53. train_and_valid_poisoned = GenerateTrigger(train_and_valid, poisoningRate, backdoorTriggerValues, targetLabel)
  54. train_and_valid.update(train_and_valid_poisoned)
  55. # Create backdoored test version
  56. # Also copy to not disturb clean test data
  57. test_backdoor = test.copy()
  58. # Drop rows that already have the target label
  59. test_backdoor = test_backdoor[test_backdoor[target[0]] != targetLabel]
  60. # Add backdoor to all test_backdoor samples
  61. test_backdoor = GenerateBackdoorTrigger(test_backdoor, backdoorTriggerValues, targetLabel)
  62. # Set dtypes correctly
  63. train_and_valid[cat_cols + target] = train_and_valid[cat_cols + target].astype("int64")
  64. train_and_valid[num_cols] = train_and_valid[num_cols].astype("float64")
  65. test[cat_cols + target] = test[cat_cols + target].astype("int64")
  66. test[num_cols] = test[num_cols].astype("float64")
  67. test_backdoor[cat_cols + target] = test_backdoor[cat_cols + target].astype("int64")
  68. test_backdoor[num_cols] = test_backdoor[num_cols].astype("float64")
  69. # Split dataset into samples and labels
  70. train, valid = train_test_split(train_and_valid, stratify=train_and_valid[target[0]], test_size=0.2, random_state=runIdx)
  71. # Create network
  72. saintModel = SaintLib(SAINT_ARGS + ["--run_name", "HIGGS_1F_OOB_" + str(poisoningRate) + "_" + str(runIdx)])
  73. # Fit network on backdoored data
  74. ASR, BA, _ = saintModel.fit(train, valid, test, test_backdoor, cat_cols, num_cols, target)
  75. return ASR, BA
  76. # Start experiment
  77. # Global results
  78. all_ASR_results = []
  79. all_BA_results = []
  80. for f in num_cols:
  81. print("******************FEATURE", f, "***********************")
  82. backdoorFeatures = [f]
  83. backdoorTriggerValues = [(data[backdoorFeatures[0]].max() + (data[backdoorFeatures[0]].max() - data[backdoorFeatures[0]].min())*0.1)]
  84. print("using trigger value of", backdoorTriggerValues[0])
  85. ASR_results = []
  86. BA_results = []
  87. for poisoningRate in poisoningRates:
  88. # Run results
  89. ASR_run = []
  90. BA_run = []
  91. for run in range(RERUNS):
  92. ASR, BA = doExperiment(poisoningRate, backdoorFeatures, backdoorTriggerValues, targetLabel, run+1)
  93. print("Results for", poisoningRate, "Run", run+1)
  94. print("ASR:", ASR)
  95. print("BA:", BA)
  96. print("---------------------------------------")
  97. ASR_run.append(ASR)
  98. BA_run.append(BA)
  99. ASR_results.append(ASR_run)
  100. BA_results.append(BA_run)
  101. all_ASR_results.append(ASR_results)
  102. all_BA_results.append(BA_results)
  103. for fidx, f in enumerate(num_cols):
  104. print(f)
  105. for idx, poisoningRate in enumerate(poisoningRates):
  106. print("Results for", poisoningRate)
  107. print("avg ASR:", np.mean(all_ASR_results[fidx]))
  108. print("avg BA:", np.mean(all_BA_results[fidx]))
  109. print("ASR:", all_ASR_results[fidx][idx])
  110. print("BA:", all_BA_results[fidx][idx])
  111. print("------------------------------------------")
  112. for fidx, f in enumerate(num_cols):
  113. print("________________________")
  114. print(f)
  115. print("EASY COPY PASTE RESULTS:")
  116. print("ASR_results = [")
  117. for idx, poisoningRate in enumerate(poisoningRates):
  118. print(all_ASR_results[fidx][idx], ",")
  119. print("]")
  120. print()
  121. print("BA_results = [")
  122. for idx, poisoningRate in enumerate(poisoningRates):
  123. print(all_BA_results[fidx][idx], ",")
  124. print("]")