-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_clf.py
176 lines (142 loc) · 7.77 KB
/
train_clf.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
import argparse
import torch
import numpy as np
import random
import torch.optim as optim
import time
from splitdata import split_train_test
from getgradients import get_gradients_set
from network import mlp
import os
# os.environ["CUDA_VISIBLE_DEVICES"] = "0"
CIFAR10_GRAD_ROOT = {'source':'./model/victim/vict-wrn28-10.pt',
'distillation':'./model/attack/atta-d-wrn16-1.pt',
'diff-architecture':'./model/attack/atta-wrn16-1.pt',
'zero-shot':'./model/attack/atta-df-wrn16-1.pt',
'fine-tune':'./model/attack/atta-finetune-wrn28-10.pt',
'label-query':'./model/attack/atta-label-wrn16-1.pt',
'logit-query':'./model/attack/atta-logit-wrn16-1.pt',
'benign':'./model/benign/benign-wrn28-10.pt'}
IMAGENET_GRAD_ROOT = {'source':'./model/victim/vict-imgnet-resnet34.pt',
'distillation':'./model/attack/atta-d-imgnet-resnet18.pt',
'diff-architecture':'./model/victim/vict-imgnet-resnet18.pt',
'zero-shot':'./model/attack/atta-df-imgnet-resnet18.pt',
'fine-tune':'./model/attack/atta-finetune-imgnet-resnet34.pt',
'label-query':'./model/attack/atta-label-imgnet-resnet18.pt',
'logit-query':'./model/attack/atta-logit-imgnet-resnet18.pt',
'benign':'./model/benign/benign-imgnet-resnet34.pt'}
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--model_type', '--type', type=str, default='wrn28-10')
parser.add_argument('--mlp_epoch', type=int, default=100)
parser.add_argument('--dataset', type=str, default='cifar10')
parser.add_argument('--num_classes', type=int, default=10)
parser.add_argument('--batch_size', '--bs', type=int, default=8)
parser.add_argument('--v_g_f_path', '--v', type=str, default='')
parser.add_argument('--i_g_f_path', '--i', type=str, default='')
parser.add_argument('--gpu', type=int, default=0)
parser.add_argument('--seed', type=int, default=1)
parser.add_argument('--momentum', type=float, default=0.9, metavar='M',
help='SGD momentum (default: 0.9)')
parser.add_argument('--weight_decay', type=float, default=5e-4)
parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
help='learning rate (default: 0.1)')
parser.add_argument('--black', action='store_true')
args = parser.parse_args()
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
np.random.seed(args.seed)
random.seed(args.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
if args.black:
if args.dataset == 'cifar10':
args.num_classes = 10
if args.model_type == 'wrn28-10':
args.v_g_f_path = './logits_set/vict-wrn28-10.pt/cifar10_seurat_10%/g_f.npy'
args.i_g_f_path = './logits_set/benign-wrn28-10.pt/cifar10_seurat_10%/g_f.npy'
elif args.dataset == 'imagenet':
args.num_classes = 20
if args.model_type == 'resnet34-imgnet':
args.v_g_f_path = './logits_set/vict-imgnet-resnet34.pt/subimage_seurat_10%/g_f.npy'
args.i_g_f_path = './logits_set/benign-imgnet-resnet34.pt/subimage_seurat_10%/g_f.npy'
else:
raise('no such dataset')
print(args)
starttime = time.time()
if args.gpu != -1:
torch.cuda.set_device(args.gpu)
device = 'cuda' if args.gpu != -1 else 'cpu'
print('load logits of victim and benign')
# sign vector of logits of victim model
vict_g = np.sign(np.load(args.v_g_f_path))
# sign vector of logits of benign model
benign_g = np.sign(np.load(args.i_g_f_path))
# split train & test set for training clf
train_vict_g, test_vict_g, train_benign_g, test_benign_g = split_train_test(vict_g, benign_g)
gradients_trainset, gradients_trainlabel = get_gradients_set(train_vict_g, train_benign_g)
# gradients_testset, gradients_testlabel are used for testing clf on Victim model
gradients_testset, gradients_testlabel = get_gradients_set(test_vict_g, test_benign_g)
# train binary classifier with (benign_g, 0) and (vict_g, 1)
print('train meta-classifier with sign vector of gradients')
if args.dataset == 'cifar10':
clf = mlp.MLP3(len(gradients_trainset[0]), 2)
elif args.dataset == 'imagenet':
clf = mlp.MLP(len(gradients_trainset[0]), 2)
clf = clf.to(device)
print(clf)
optimizer = optim.SGD(clf.parameters(), lr=args.lr, weight_decay=args.weight_decay, momentum=args.momentum)
best_acc = 0
for epoch in range(args.mlp_epoch):
mlp.train(clf, gradients_trainset, gradients_trainlabel, epoch, optimizer, device)
acc = mlp.test(clf, gradients_testset, gradients_testlabel, device, epoch)
best_mlp_path = "model/clf/logitsign-%s-%s.pt" % (args.dataset, args.model_type)
if acc > best_acc:
best_acc = acc
torch.save(clf.state_dict(), best_mlp_path)
print("Test on Victim best acc=%.6f" % best_acc)
print('Time cost: {} sec'.format(round(time.time() - starttime, 2)))
else:
if args.dataset == 'cifar10':
args.num_classes = 10
if args.model_type == 'wrn28-10':
args.v_g_f_path = './gradients_set/vict-wrn28-10.pt/cifar10_seurat_10%/g_f.npy'
args.i_g_f_path = './gradients_set/benign-wrn28-10.pt/cifar10_seurat_10%/g_f.npy'
elif args.dataset == 'imagenet':
args.num_classes = 20
if args.model_type == 'resnet34-imgnet':
args.v_g_f_path = './gradients_set/vict-imgnet-resnet34.pt/subimage_seurat_10%/g_f.npy'
args.i_g_f_path = './gradients_set/benign-imgnet-resnet34.pt/subimage_seurat_10%/g_f.npy'
else:
raise('no such dataset')
print(args)
starttime = time.time()
if args.gpu != -1:
torch.cuda.set_device(args.gpu)
device = 'cuda' if args.gpu != -1 else 'cpu'
print('load gradients of victim and benign')
# sign vector of gradients of victim model
vict_g = np.sign(np.load(args.v_g_f_path))
# sign vector of gradients of benign model
benign_g = np.sign(np.load(args.i_g_f_path))
# split train & test set for training clf
train_vict_g, test_vict_g, train_benign_g, test_benign_g = split_train_test(vict_g, benign_g)
gradients_trainset, gradients_trainlabel = get_gradients_set(train_vict_g, train_benign_g)
# gradients_testset, gradients_testlabel are used for testing clf on Victim model
gradients_testset, gradients_testlabel = get_gradients_set(test_vict_g, test_benign_g)
# train binary classifier with (benign_g, 0) and (vict_g, 1)
print('train meta-classifier with sign vector of gradients')
clf = mlp.MLP(len(gradients_trainset[0]), 2)
clf = clf.to(device)
print(clf)
optimizer = optim.SGD(clf.parameters(), lr=args.lr, weight_decay=args.weight_decay, momentum=args.momentum)
best_acc = 0
for epoch in range(args.mlp_epoch):
mlp.train(clf, gradients_trainset, gradients_trainlabel, epoch, optimizer, device)
acc = mlp.test(clf, gradients_testset, gradients_testlabel, device, epoch)
best_mlp_path = "model/clf/gradsign-%s-%s.pt" % (args.dataset, args.model_type)
if acc > best_acc:
best_acc = acc
torch.save(clf.state_dict(), best_mlp_path)
print("Test on Victim best acc=%.6f" % best_acc)
print('Time cost: {} sec'.format(round(time.time() - starttime, 2)))