-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmico_train_42k.py
267 lines (210 loc) · 7.66 KB
/
mico_train_42k.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# -*- coding: utf-8 -*-
"""MICO_train_42k.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1ngKk8XDDjA5atJ4e6w_jiUsN8p1QF03f
"""
import os
import sys
import time
import numpy as np
import torch
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from sklearn.model_selection import train_test_split
import csv
from tqdm.notebook import tqdm
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
def load_cifar10(dataset_dir, download=True):
"""Loads the CIFAR10 dataset.
"""
from torchvision.datasets import CIFAR10
import torchvision.transforms as transforms
from torch.utils.data import ConcatDataset
# Precomputed statistics of CIFAR10 dataset
# Exact values are assumed to be known, but can be estimated with a modest privacy budget
# Opacus wrongly uses CIFAR10_STD = (0.2023, 0.1994, 0.2010)
# This is the _average_ std across all images (see https://github.com/kuangliu/pytorch-cifar/issues/8)
CIFAR10_MEAN = (0.49139968, 0.48215841, 0.44653091)
CIFAR10_STD = (0.24703223, 0.24348513, 0.26158784)
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(CIFAR10_MEAN, CIFAR10_STD)
])
# NB: torchvision checks the integrity of downloaded files
train_dataset = CIFAR10(
root=f"{dataset_dir}/cifar10",
train=True,
download=download,
transform=transform
)
test_dataset = CIFAR10(
root=f"{dataset_dir}/cifar10",
train=False,
download=download,
transform=transform
)
return ConcatDataset([train_dataset, test_dataset])
def accuracy(preds: torch.Tensor, labels: torch.Tensor) -> float:
return (preds == labels).mean()
# Architecture of shadow model
class ShadowNet(nn.Module):
def __init__(self):
super(ShadowNet, self).__init__()
self.shadowCnn = nn.Sequential(
nn.Conv2d(3, 128, kernel_size=8, stride=2, padding=3), nn.Tanh(),
nn.MaxPool2d(kernel_size=3, stride=1),
nn.Conv2d(128, 256, kernel_size=3), nn.Tanh(),
nn.Conv2d(256, 256, kernel_size=3), nn.Tanh(),
nn.AvgPool2d(kernel_size=2, stride=2),
nn.Flatten()
,nn.Linear(in_features=6400, out_features=10)
)
def forward(self, x):
output = self.shadowCnn(x)
return output
def saveTargetDataset(model,traindataLoaderShader,testDataLoaderShader,data):
model.eval()
sizeData = 50
currentCount = 0
saveFeatureTrain =True
saveFeatureTest =True
with torch.no_grad():
for j,(inputs, target) in enumerate(traindataLoaderShader):
if saveFeatureTrain:
inputs = inputs.to(device)
target = target.to(device)
output = model(inputs)
features = output.detach().cpu().numpy()
target = target.detach().cpu().numpy()
for count, feature in enumerate(features):
if currentCount < sizeData:
feature = np.append(feature,1)
feature = np.append(feature,target[count])
data.append(feature)
currentCount+=1
else:
saveFeatureTrain = False
break
currentCount = 0
for j,(inputs, target) in enumerate(testDataLoaderShader):
if saveFeatureTest:
inputs = inputs.to(device)
target = target.to(device)
output = model(inputs)
features = output.detach().cpu().numpy()
target = target.detach().cpu().numpy()
for count, feature in enumerate(features):
if currentCount < sizeData:
feature = np.append(feature,0)
feature = np.append(feature,target[count])
data.append(feature)
currentCount+=1
else:
saveFeatureest = False
break
return data
# For infinity epsilon, no DP
def trainShadowModels(nModel, shadowData,dirPath):
sTrainingSize = 42000
sEpochs = 50
sBatchSize = 32
sMaxGradNorm = 2.6
sTargetEpsilon = 4.0
sTargetDelta = 1/sTrainingSize
sLR = 0.005
sLrSchedulerGamma = 0.96
for n in range(nModel):
attackData = []
sModel = ShadowNet()
sModel = sModel.to(device)
sCriterion = nn.CrossEntropyLoss()
sOptimizer = optim.SGD(sModel.parameters(), lr=sLR, momentum=0)
shadowTrainData, shadowTestData = train_test_split(shadowData, test_size=0.3, random_state=42)
traindataLoaderShader = torch.utils.data.DataLoader(shadowTrainData, batch_size=sBatchSize,shuffle = True, num_workers=4,pin_memory = True)
testDataLoaderShader = torch.utils.data.DataLoader(shadowTestData, batch_size=500,shuffle = True, num_workers=4,pin_memory=True)
sScheduler = optim.lr_scheduler.StepLR(sOptimizer, step_size=1,
gamma=sLrSchedulerGamma)
for i in range(sEpochs):
sModel.train()
sLosses = []
sTop1Acc = []
dataProcessed =0
for j,(inputs, target) in enumerate(traindataLoaderShader):
inputs = inputs.to(device)
target = target.to(device)
dataProcessed+=len(inputs)
sOptimizer.zero_grad()
sOutput = sModel(inputs)
sLoss = sCriterion(sOutput, target)
sPreds = np.argmax(sOutput.detach().cpu().numpy(), axis=1)
sLabels = target.detach().cpu().numpy()
acc = accuracy(sPreds, sLabels)
sLosses.append(sLoss.item())
sTop1Acc.append(acc)
sLoss.backward()
sOptimizer.step()
sScheduler.step()
# Get the attack dataset
attackData = saveTargetDataset(sModel,traindataLoaderShader,testDataLoaderShader,attackData)
# Test dataset results
print("Test results:")
testShadow(sModel,testDataLoaderShader,sCriterion)
modelName = "shadowModel"+str(n)+".pt"
print("Saving shadow model ",modelName)
torch.save(sModel.state_dict(), os.path.join(dirPath + "shadowModels/", modelName))
filename = "attackData"+str(n)+".csv"
print( "file: ",filename)
with open(os.path.join(dirPath + "attackData/", filename), "w", newline='') as f:
for count, data in enumerate(attackData):
csv.writer(f).writerow(data)
return attackData
def testShadow(model,test_loader,criterion):
model.eval()
losses = []
top1_acc = []
with torch.no_grad():
for inputs, target in tqdm(test_loader):
inputs = inputs.to(device)
target = target.to(device)
output = model(inputs)
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)
loss_avg = np.mean(losses)
print(
f"Test Loss : {loss_avg:.6f}\n"
f"Test Accuracy: {top1_avg * 100:.6f}"
)
return np.mean(top1_acc)
if __name__ == "__main__":
# Data directory
dataPath="cifar10/"
if not os.path.isdir(dataPath):
os.mkdir(dataPath)
# Output directory
dirPath="output/"
if not os.path.isdir(dirPath):
os.mkdir(dirPath)
# Attack data directory
if not os.path.isdir(dirPath+"attackData/"):
os.mkdir(dirPath+"attackData/")
else:
os.rmdir(dirPath+"attackData/")
os.mkdir(dirPath+"attackData/")
# Shadow models directory
if not os.path.isdir(dirPath+"shadowModels/"):
os.mkdir(dirPath+"shadowModels/")
else:
os.rmdir(dirPath+"shadowModels/")
os.mkdir(dirPath+"shadowModels/")
dataset = load_cifar10(dataPath,download = True)
print(len(dataset))
# Train 500 shadow models
attackData = trainShadowModels(500,dataset,dirPath)