Advertisement
mayankjoin3

adverserial

Apr 7th, 2025
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | None | 0 0
  1. #!pip install adversarial-robustness-toolbox
  2.  
  3. import pandas as pd
  4. import time
  5. import psutil
  6. import os
  7. from sklearn.model_selection import StratifiedKFold
  8. from sklearn.tree import DecisionTreeClassifier
  9. from sklearn.metrics import classification_report
  10. import joblib
  11. from sklearn.preprocessing import LabelEncoder
  12. import numpy as np
  13.  
  14. # --- NEW IMPORTS for ART adversarial testing ---
  15. from art.estimators.classification.scikitlearn import ScikitlearnDecisionTreeClassifier
  16. from art.attacks.evasion import DecisionTreeAttack  # :contentReference[oaicite:0]{index=0}
  17.  
  18. # Step 1: Load the data
  19. file_path = "m3.csv"
  20. df = pd.read_csv(file_path)
  21.  
  22. # Preprocessing
  23. label_encoder = LabelEncoder()
  24. X = df.drop(columns=['label'])
  25. y = df['label']
  26. X = X.apply(lambda col: label_encoder.fit_transform(col.astype(str)), axis=0, result_type='expand')
  27.  
  28. # Step 2: Initialize the Decision Tree model
  29. clf = DecisionTreeClassifier(random_state=42)
  30.  
  31. # Step 3: Set up 2-Fold Cross-Validation
  32. skf = StratifiedKFold(n_splits=2, shuffle=True, random_state=42)
  33.  
  34. fold = 1
  35. for train_index, test_index in skf.split(X, y):
  36.     print(f"\n=== Fold {fold} ===")
  37.     X_train, X_test = X.iloc[train_index], X.iloc[test_index]
  38.     y_train, y_test = y.iloc[train_index], y.iloc[test_index]
  39.  
  40.     # Train
  41.     start = time.time()
  42.     clf.fit(X_train, y_train)
  43.     train_time = time.time() - start
  44.     print(f"Training time: {train_time:.4f}s")
  45.  
  46.     # Predict (clean)
  47.     start = time.time()
  48.     preds = clf.predict(X_test)
  49.     pred_time = time.time() - start
  50.     print(f"Prediction time: {pred_time:.4f}s")
  51.  
  52.     # Memory/CPU during prediction
  53.     proc = psutil.Process(os.getpid())
  54.     mem_before = proc.memory_info().rss / (1024**2)
  55.     cpu_before = psutil.cpu_percent(interval=1)
  56.     clf.predict(X_test)
  57.     mem_after = proc.memory_info().rss / (1024**2)
  58.     cpu_after = psutil.cpu_percent(interval=1)
  59.     print(f"Memory delta: {mem_after - mem_before:.4f} MB")
  60.     print(f"CPU delta:    {cpu_after - cpu_before:.4f}%")
  61.  
  62.     # Clean classification report
  63.     print("Clean Classification Report:")
  64.     print(classification_report(y_test, preds))
  65.  
  66.     # Save model size
  67.     model_path = f"decision_tree_model_fold{fold}.pkl"
  68.     joblib.dump(clf, model_path)
  69.     size_mb = os.path.getsize(model_path) / (1024**2)
  70.     print(f"Model size: {size_mb:.4f} MB")
  71.  
  72.     # --- ADVERSARIAL TESTING BLOCK ---
  73.     # 1. Wrap trained model for ART
  74.     clip_min, clip_max = X_train.min().min(), X_train.max().max()
  75.     art_classifier = ScikitlearnDecisionTreeClassifier(
  76.         model=clf,
  77.         clip_values=(clip_min, clip_max)
  78.     )
  79.  
  80.     # 2. Set up the Decision Tree attack
  81.     attack = DecisionTreeAttack(
  82.         classifier=art_classifier,
  83.         offset=0.001,
  84.         verbose=False
  85.     )
  86.  
  87.     # 3. Generate adversarial examples
  88.     x_test_np = X_test.to_numpy()
  89.     y_test_np = y_test.to_numpy()
  90.     x_test_adv = attack.generate(x=x_test_np, y=y_test_np)
  91.  
  92.     # 4. Evaluate robust accuracy
  93.     adv_preds = art_classifier.predict(x_test_adv).argmax(axis=1)
  94.     robust_acc = (adv_preds == y_test_np).mean() * 100
  95.     print(f"Robust accuracy under DecisionTreeAttack: {robust_acc:.2f}%")
  96.     fold+=1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement