-
Notifications
You must be signed in to change notification settings - Fork 5
/
search.py
55 lines (38 loc) · 1.44 KB
/
search.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
import torchvision.models as models
import torchvision.transforms as transforms
from loader import ImagetNet, Val
from Normalize import Normalize
from torchvision.datasets import ImageFolder
import warnings
from universal_pert import universal_perturbation
warnings.filterwarnings("ignore")
import numpy as np
from torch.utils.data import DataLoader
import torch
epsilon = 10.0 / 255.0
training_data_path = 'input your path (e.g., '../data/ILSVRC2012_train/pick_image/')'
testing_data_path = 'input your path'
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
# std = [1.0, 1.0, 1.0]
net = torch.nn.Sequential(Normalize(mean, std), models.inception_v3(pretrained=True).eval()).cuda()
transform = transforms.Compose([
transforms.Resize((330, 330)),
transforms.CenterCrop(299),
transforms.ToTensor()])
print('loader data')
X = ImagetNet(training_data_path, 1000, 10, transforms = transform)
# X = torch.utils.data.DataLoader(
# ImageFolder(training_data_path, transforms.Compose([
# transforms.Resize((330, 330)),
# transforms.CenterCrop(299),
# transforms.ToTensor(),
# ])),
# batch_size = 1, shuffle=True,
# pin_memory=True)
val_loader = torch.utils.data.DataLoader(
ImageFolder(testing_data_path, transforms = transform,
batch_size = 50, shuffle=False,
num_workers = 8, pin_memory=True)
print('Computation')
v = universal_perturbation(X, val_loader, net, xi = epsilon)