-
-
Save tcbegley/91afccc8f702a61617f7ec6da250effe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import warnings | |
import numpy as np | |
import torch | |
import torch.nn as nn | |
import torch.optim as optim | |
import torchvision | |
import torchvision.transforms as transforms | |
from torchvision import models | |
from torchvision.datasets import CIFAR10 | |
from tqdm.auto import tqdm | |
from opacus import PrivacyEngine | |
from opacus.utils.batch_memory_manager import BatchMemoryManager | |
from opacus.validators import ModuleValidator | |
warnings.simplefilter("ignore") | |
MAX_GRAD_NORM = 1.2 | |
EPSILON = 50.0 | |
DELTA = 1e-5 | |
EPOCHS = 20 | |
LR = 1e-3 | |
BATCH_SIZE = 512 | |
MAX_PHYSICAL_BATCH_SIZE = 128 | |
# These values, specific to the CIFAR10 dataset, are assumed to be known. | |
# If necessary, they can be computed with modest privacy budgets. | |
CIFAR10_MEAN = (0.4914, 0.4822, 0.4465) | |
CIFAR10_STD_DEV = (0.2023, 0.1994, 0.2010) | |
DATA_ROOT = "./cifar10" | |
def get_data_loader(train=True): | |
transform = transforms.Compose( | |
[ | |
transforms.ToTensor(), | |
transforms.Normalize(CIFAR10_MEAN, CIFAR10_STD_DEV), | |
] | |
) | |
dataset = CIFAR10(root=DATA_ROOT, train=train, download=True, transform=transform) | |
return torch.utils.data.DataLoader( | |
dataset, | |
batch_size=BATCH_SIZE, | |
shuffle=not train, | |
) | |
def accuracy(preds, labels): | |
return (preds == labels).mean() | |
def train(model, train_loader, optimizer, epoch, device): | |
model.train() | |
criterion = nn.CrossEntropyLoss() | |
losses = [] | |
top1_acc = [] | |
with BatchMemoryManager( | |
data_loader=train_loader, | |
max_physical_batch_size=MAX_PHYSICAL_BATCH_SIZE, | |
optimizer=optimizer, | |
) as memory_safe_data_loader: | |
for i, (images, target) in enumerate(memory_safe_data_loader): | |
optimizer.zero_grad() | |
images = images.to(device) | |
target = target.to(device) | |
# compute output | |
output = model(images) | |
loss = criterion(output, target) | |
preds = np.argmax(output.detach().cpu().numpy(), axis=1) | |
labels = target.detach().cpu().numpy() | |
# measure accuracy and record loss | |
acc = accuracy(preds, labels) | |
losses.append(loss.item()) | |
top1_acc.append(acc) | |
loss.backward() | |
optimizer.step() | |
if (i + 1) % 200 == 0: | |
epsilon = privacy_engine.get_epsilon(DELTA) | |
print( | |
f"\tTrain Epoch: {epoch} \t" | |
f"Loss: {np.mean(losses):.6f} " | |
f"Acc@1: {np.mean(top1_acc) * 100:.6f} " | |
f"(ε = {epsilon:.2f}, δ = {DELTA})" | |
) | |
def test(model, test_loader, device): | |
model.eval() | |
criterion = nn.CrossEntropyLoss() | |
losses = [] | |
top1_acc = [] | |
with torch.no_grad(): | |
for images, target in test_loader: | |
images = images.to(device) | |
target = target.to(device) | |
output = model(images) | |
loss = criterion(output, target) | |
preds = np.argmax(output.detach().cpu().numpy(), axis=1) | |
labels = target.detach().cpu().numpy() | |
acc = accuracy(preds, labels) | |
losses.append(loss.item()) | |
top1_acc.append(acc) | |
top1_avg = np.mean(top1_acc) | |
print(f"\tTest set:" f"Loss: {np.mean(losses):.6f} " f"Acc: {top1_avg * 100:.6f} ") | |
return np.mean(top1_acc) | |
if __name__ == "__main__": | |
train_loader = get_data_loader(train=True) | |
test_loader = get_data_loader(train=False) | |
model = ModuleValidator.fix(models.resnet18(num_classes=10)) | |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
model = model.to(device) | |
privacy_engine = PrivacyEngine(accountant="prv") | |
model, optimizer, train_loader = privacy_engine.make_private_with_epsilon( | |
module=model, | |
optimizer=optim.RMSprop(model.parameters(), lr=LR), | |
data_loader=train_loader, | |
epochs=EPOCHS, | |
target_epsilon=EPSILON, | |
target_delta=DELTA, | |
max_grad_norm=MAX_GRAD_NORM, | |
) | |
print(f"Using sigma={optimizer.noise_multiplier} and C={MAX_GRAD_NORM}") | |
for epoch in tqdm(range(EPOCHS), desc="Epoch", unit="epoch"): | |
train(model, train_loader, optimizer, epoch + 1, device) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment