Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
R
R24-102
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
R24-102
R24-102
Commits
ab2de65c
Commit
ab2de65c
authored
May 01, 2024
by
Savindhya Bandara
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Brendel and Bethge Attack
parent
5660ba94
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
325 additions
and
0 deletions
+325
-0
Brendel_and_Bethge_Attack.ipynb
Brendel_and_Bethge_Attack.ipynb
+325
-0
No files found.
Brendel_and_Bethge_Attack.ipynb
0 → 100644
View file @
ab2de65c
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"id": "3yYoFRrF9eRj"
},
"outputs": [],
"source": [
"# Import necessary libraries\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"from tensorflow.keras.models import load_model\n",
"from sklearn.preprocessing import StandardScaler\n",
"from sklearn.model_selection import train_test_split\n",
"from sklearn.metrics import accuracy_score"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "ZGFK6NPv9m9C",
"outputId": "964ae404-e6c2-4f63-b325-f1bd1ed85135"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Mounted at /content/drive\n"
]
}
],
"source": [
"from google.colab import drive\n",
"drive.mount('/content/drive')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"id": "BpcASSJ7-NA7"
},
"outputs": [],
"source": [
"import pandas as pd\n",
"data = pd.read_csv('/content/drive/MyDrive/Research/Dataset/disease_preprocess.csv')\n",
"X = data.drop('HeartDisease', axis=1).values # Ensure .values to get numpy array\n",
"y = data['HeartDisease'].values"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"id": "SgsOPdhu-RxF"
},
"outputs": [],
"source": [
"# Split the data into training and testing sets\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"id": "Gfi8QQMeBLek"
},
"outputs": [],
"source": [
"# Standardize the data\n",
"scaler = StandardScaler()\n",
"X_train_scaled = scaler.fit_transform(X_train)\n",
"X_test_scaled = scaler.transform(X_test)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"id": "WN-Nysk8DcFs"
},
"outputs": [],
"source": [
"# Load your model\n",
"model = load_model('/content/drive/MyDrive/Research/Models/1D_CNN_model_Final_1.h5')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"id": "OyNj8uI9Dhw5"
},
"outputs": [],
"source": [
"def brendel_bethge_attack(model, x_test, y_test, epsilon=0.1, iterations=100, alpha=0.02):\n",
" x_adv = x_test.copy() # Start with copies of the inputs\n",
" for i in range(iterations):\n",
" # Introduce a random perturbation\n",
" perturbation = np.random.normal(loc=0.0, scale=epsilon, size=x_test.shape)\n",
" x_temp = x_adv + perturbation # Temporarily add noise\n",
"\n",
" preds = model.predict(x_temp) # Make predictions on the modified inputs\n",
" preds_class = (preds > 0.5).astype(int) # Assuming binary classification with a sigmoid output\n",
" mask = preds_class.flatten() != y_test # Identify where the attack changed the prediction\n",
"\n",
" # Only keep changes that successfully fooled the model\n",
" x_adv[mask] = x_temp[mask]\n",
"\n",
" # Gradually reduce epsilon to fine-tune the adversarial examples\n",
" epsilon *= (1 - alpha)\n",
"\n",
" return x_adv\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "bWyBSruODlwT",
"outputId": "ddd344b6-c894-4e70-dd24-e22815f73fd6"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1931/1931 [==============================] - 9s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 9s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 9s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n",
"1931/1931 [==============================] - 10s 5ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 9s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 13s 7ms/step\n",
"1931/1931 [==============================] - 9s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 10s 5ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n",
"1931/1931 [==============================] - 10s 5ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 11s 6ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 10s 5ms/step\n",
"1931/1931 [==============================] - 15s 8ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 10s 5ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 10s 5ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 9s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n",
"1931/1931 [==============================] - 10s 5ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 12s 6ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 9s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 9s 5ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 9s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 9s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 9s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 7s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n",
"1931/1931 [==============================] - 8s 4ms/step\n"
]
}
],
"source": [
"# Generate adversarial examples\n",
"X_test_adv = brendel_bethge_attack(model, X_test_scaled, y_test)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CejwZkjgDotV",
"outputId": "e941e649-b117-42e2-be72-32b99f79a691"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1931/1931 [==============================] - 8s 4ms/step\n"
]
}
],
"source": [
"# Evaluate the model on adversarial examples\n",
"y_pred_adv = (model.predict(X_test_adv) > 0.5).astype(int)\n",
"adv_accuracy = accuracy_score(y_test, y_pred_adv.flatten()) # Flatten if needed depending on the shape of y_test"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "9KuqYBL8INyA",
"outputId": "8fa5c77b-6534-428b-a7b6-7c8190e3b1f6"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1931/1931 [==============================] - 7s 4ms/step\n",
"Original Model Accuracy: 0.8338378850917098\n",
"Adversarial Model Accuracy: 0.31666963461818654\n"
]
}
],
"source": [
"# Compare accuracies\n",
"original_accuracy = accuracy_score(y_test, (model.predict(X_test_scaled) > 0.5).astype(int))\n",
"print(\"Original Model Accuracy: \", original_accuracy)\n",
"print(\"Adversarial Model Accuracy: \", adv_accuracy)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"id": "aoKrYT9zIXnR"
},
"outputs": [],
"source": []
}
],
"metadata": {
"accelerator": "TPU",
"colab": {
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment