-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdistribution_alignment.py
More file actions
88 lines (68 loc) · 2.24 KB
/
distribution_alignment.py
File metadata and controls
88 lines (68 loc) · 2.24 KB
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
import torch
import torch.nn.functional as F
import numpy as np
import torch.nn as nn
from torchvision import transforms
transform_train = transforms.Compose([
transforms.Resize((32, 32)),
transforms.ToTensor(),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
])
import torchvision.datasets as datasets
from torch.utils.data import Dataset, DataLoader, Subset, random_split
testset = datasets.CIFAR10(root='./cifar10/', train=False, download=True, transform=transform_test)
testloader = DataLoader(testset, batch_size=128, shuffle=False, num_workers=8)
device = 'cuda:0'
student = torch.load('./surrogates/surrogate.pth').eval().to(device)
test_num = len(testloader.dataset)
acc = 0.0
for test_data in testloader:
test_images, test_labels = test_data
outputs = student(test_images.to(device))
predict_y = torch.max(outputs, dim=1)[1]
acc += torch.eq(predict_y, test_labels.to(device)).sum().item()
test_acc = acc / test_num
print('before alignment:', test_acc)
import torch.nn as nn
class AlgorithmWrapper(nn.Module):
def __init__(self, student):
super(AlgorithmWrapper, self).__init__()
self.featurizer = nn.Sequential(*list(student.children())[:-1])
self.classifier = student.fc
def forward(self, x):
features = self.featurizer(x)
features = features.view(features.size(0), -1)
return self.classifier(features)
algorithm = AlgorithmWrapper(student).to(device)
from T3A import T3A
hparams = {
'alpha': 0.1,
'filter_K': 100,
}
t3a_algorithm = T3A(
input_shape=(3, 32, 32),
num_classes=10,
num_domains=1,
hparams=hparams,
algorithm=algorithm
)
t3a_algorithm.eval()
all_predictions = []
all_labels = []
acc = 0.0
batch = 0
interpolation = False
with torch.no_grad():
for data, labels in testloader:
data = data.to(device)
labels = labels.to(device)
if batch >= 5:
interpolation = True
outputs = t3a_algorithm.predict(data, adapt=True, interpolation=interpolation)
predict_y = torch.max(outputs, dim=1)[1]
acc += torch.eq(predict_y, labels.to(device)).sum().item()
batch += 1
test_acc = acc / test_num
print('after alignment:', test_acc)