Commit f93bd2ec authored by Безмен Антон Павлович's avatar Безмен Антон Павлович
Browse files

Add new file

parent 8ff73966
No related merge requests found
Showing with 140 additions and 0 deletions
+140 -0
FIRST_VERS 0 → 100644
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader
from opacus import PrivacyEngine
from art.attacks.inference.membership_inference import MembershipInferenceBlackBox
from art.estimators.classification import PyTorchClassifier
import numpy as np
import warnings
import matplotlib.pyplot as plt # Для построения графиков
# Подавление предупреждений
warnings.filterwarnings("ignore", category=FutureWarning)
# --- Шаг 1: Загрузка данных ---
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.5,), (0.5,))
])
train_dataset = datasets.MNIST(root='./data', train=True, transform=transform, download=True)
test_dataset = datasets.MNIST(root='./data', train=False, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=64, shuffle=False)
# --- Шаг 2: Создание модели ---
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc1 = nn.Linear(28 * 28, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, x):
x = x.view(-1, 28 * 28) # Flatten
x = torch.relu(self.fc1(x))
x = self.fc2(x)
return x
model = SimpleModel()
# --- Шаг 3: Добавление дифференциальной приватности ---
optimizer = optim.SGD(model.parameters(), lr=0.01)
privacy_engine = PrivacyEngine(secure_mode=False) # Отключение Secure RNG
model, optimizer, train_loader = privacy_engine.make_private(
module=model,
optimizer=optimizer,
data_loader=train_loader,
noise_multiplier=1.1, # Уровень шума
max_grad_norm=1.0 # Ограничение нормы градиентов
)
# --- Шаг 4: Обучение модели ---
def train(model, train_loader, optimizer, epochs=5):
model.train()
for epoch in range(epochs):
running_loss = 0.0
for data, target in train_loader:
optimizer.zero_grad()
output = model(data)
loss = nn.CrossEntropyLoss()(output, target)
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f"Epoch {epoch + 1}, Loss: {running_loss / len(train_loader):.4f}")
train(model, train_loader, optimizer)
# --- Шаг 5: Проверка уровня приватности ---
epsilon = privacy_engine.accountant.get_epsilon(delta=1e-5)
print(f"Epsilon: {epsilon:.2f}, Delta: {1e-5}")
# --- Шаг 6: Тестирование модели ---
def test(model, test_loader):
model.eval()
correct = 0
total = 0
with torch.no_grad():
for data, target in test_loader:
output = model(data)
_, predicted = torch.max(output, 1)
total += target.size(0)
correct += (predicted == target).sum().item()
accuracy = correct / total
print(f"Test Accuracy: {accuracy * 100:.2f}%")
return accuracy * 100
test_accuracy = test(model, test_loader)
# --- Шаг 7: Защита от атак восстановления членства ---
criterion = nn.CrossEntropyLoss()
classifier = PyTorchClassifier(
model=model,
loss=criterion,
optimizer=optimizer,
input_shape=(1, 28, 28),
nb_classes=10
)
attack = MembershipInferenceBlackBox(classifier, input_type='loss')
# --- Подготовка данных для атаки ---
x_train_full = train_dataset.data.numpy()[:1000] # Первые 1000 элементов
y_train_full = train_dataset.targets.numpy()[:1000]
x_test_full = test_dataset.data.numpy()[:1000] # Первые 1000 элементов
y_test_full = test_dataset.targets.numpy()[:1000]
# Преобразование данных в формат, подходящий для ART
x_train_full = x_train_full.reshape(-1, 1, 28, 28).astype(np.float32) / 255.0
x_test_full = x_test_full.reshape(-1, 1, 28, 28).astype(np.float32) / 255.0
# Обучение атаки
attack.fit(x_train_full, y_train_full, x_test_full, y_test_full)
# Тестирование атаки
inferred_train = attack.infer(x_train_full, y_train_full)
inferred_test = attack.infer(x_test_full, y_test_full)
# Оценка точности атаки
train_accuracy_attack = np.mean(inferred_train)
test_accuracy_attack = np.mean(inferred_test)
print(f"Train set membership inference accuracy: {train_accuracy_attack:.2f}")
print(f"Test set membership inference accuracy: {test_accuracy_attack:.2f}")
# --- Визуализация результатов ---
categories = ['Test Accuracy', 'Train Attack Accuracy', 'Test Attack Accuracy']
values = [test_accuracy, train_accuracy_attack * 100, test_accuracy_attack * 100]
plt.figure(figsize=(10, 6))
plt.bar(categories, values, color=['blue', 'orange', 'green'])
plt.title('Model Performance and Attack Results')
plt.ylabel('Accuracy (%)')
plt.ylim(0, 100)
for i, value in enumerate(values):
plt.text(i, value + 2, f'{value:.2f}%', ha='center', fontsize=10)
plt.show()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment