-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyselect_EtC.py
80 lines (72 loc) · 2.45 KB
/
keyselect_EtC.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
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
import numpy as np
import torch
import torchvision
import torchvision.transforms as transforms
import torch.backends.cudnn as cudnn
from utils.etc_encryption import EtC_encryption
import lpips
import random
import os
criterion = lpips.LPIPS(net='alex')
device = 'cuda' if torch.cuda.is_available() else 'cpu'
cuda = True if torch.cuda.is_available() else False
if cuda:
criterion = torch.nn.DataParallel(criterion).cuda()
cudnn.benchmark = True
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter)
parser.add_argument("--repeat_times", type=int, default=20)
args = parser.parse_args()
# from cifar10 import CIFAR10
trainset = torchvision.datasets.CIFAR10(root='./data_cifar10', train=True, download=True, transform=transforms.Compose([transforms.ToTensor(),]))
trainloader = torch.utils.data.DataLoader(trainset, batch_size=256, shuffle=True, num_workers=16)
def test_lpips(sets):
lpips_score, total = 0, 0
for batch_idx, (inputs, targets) in enumerate(trainloader):
imgs = inputs.numpy().astype('float32')
imgs = np.transpose(imgs,(0 ,2 ,3 ,1 ))
img = EtC_encryption(imgs, sets)
lpips_score += torch.sum(criterion.forward(img, inputs)).item()
total += targets.size(0)
return lpips_score / total
if __name__ == '__main__':
args = parser.parse_args()
N_scores = []
_rotate = []
_negaposi = []
_reverse = []
_channel = []
_shf = []
for i in range(64):
x = random.randint(0,3)
z = random.randint(0,2)
a = random.randint(0,5)
if i%2==0:
_negaposi.append(1)
else:
_negaposi.append(0)
_rotate.append(x)
_reverse.append(z)
_channel.append(a)
_shf.append(i)
for rep_times in range(args.repeat_times+1):
score_max = 0
N = []
for rep in range(5):
score_max = 0
for tmp in range(rep_times):
random.shuffle(_rotate)
random.shuffle(_negaposi)
random.shuffle(_reverse)
random.shuffle(_channel)
random.shuffle(_shf)
sets = [_rotate, _negaposi, _reverse, _channel, _shf]
tmp_score = test_lpips(sets)
if score_max == 0:
score_max = tmp_score
elif score_max <= tmp_score:
score_max = tmp_score
N.append(score_max)
print(score_max, N)
N_scores.append(N)
print(N_scores)