-
Notifications
You must be signed in to change notification settings - Fork 21
/
test_model.py
executable file
·202 lines (181 loc) · 6.94 KB
/
test_model.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
############################################################
#
# test_model.py
# Load trained model and test its natural performance
# Developed as part of Poison Attack Benchmarking project
# June 2020
#
############################################################
import argparse
import sys
from collections import OrderedDict
import torch
import torch.utils.data as data
import torchvision
from learning_module import (
TINYIMAGENET_ROOT,
test,
to_log_file,
to_results_table,
now,
get_model,
load_model_from_checkpoint,
get_transform,
)
from tinyimagenet_module import TinyImageNet
def main(args):
"""Main function to test a model
input:
args: Argparse object that contains all the parsed values
return:
void
"""
print(now(), "test_model.py main() running.")
test_log = "clean_test_log.txt"
to_log_file(args, args.output, test_log)
# Set device
device = "cuda" if torch.cuda.is_available() else "cpu"
####################################################
# Dataset
if args.dataset.lower() == "cifar10":
transform_train = get_transform(args.normalize, args.train_augment)
transform_test = get_transform(args.normalize, False)
trainset = torchvision.datasets.CIFAR10(
root="./data", train=True, download=True, transform=transform_train
)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128)
testset = torchvision.datasets.CIFAR10(
root="./data", train=False, download=True, transform=transform_test
)
testloader = torch.utils.data.DataLoader(testset, batch_size=128, shuffle=False)
elif args.dataset.lower() == "cifar100":
transform_train = get_transform(args.normalize, args.train_augment)
transform_test = get_transform(args.normalize, False)
trainset = torchvision.datasets.CIFAR100(
root="./data", train=True, download=True, transform=transform_train
)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=128)
testset = torchvision.datasets.CIFAR100(
root="./data", train=False, download=True, transform=transform_test
)
testloader = torch.utils.data.DataLoader(testset, batch_size=128, shuffle=False)
elif args.dataset.lower() == "tinyimagenet_first":
transform_train = get_transform(
args.normalize, args.train_augment, dataset=args.dataset
)
transform_test = get_transform(args.normalize, False, dataset=args.dataset)
trainset = TinyImageNet(
TINYIMAGENET_ROOT,
split="train",
transform=transform_train,
classes="firsthalf",
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=64, num_workers=1, shuffle=True
)
testset = TinyImageNet(
TINYIMAGENET_ROOT,
split="val",
transform=transform_test,
classes="firsthalf",
)
testloader = torch.utils.data.DataLoader(
testset, batch_size=64, num_workers=1, shuffle=False
)
elif args.dataset.lower() == "tinyimagenet_last":
transform_train = get_transform(
args.normalize, args.train_augment, dataset=args.dataset
)
transform_test = get_transform(args.normalize, False, dataset=args.dataset)
trainset = TinyImageNet(
TINYIMAGENET_ROOT,
split="train",
transform=transform_train,
classes="lasthalf",
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=64, num_workers=1, shuffle=True
)
testset = TinyImageNet(
TINYIMAGENET_ROOT,
split="val",
transform=transform_test,
classes="lasthalf",
)
testloader = torch.utils.data.DataLoader(
testset, batch_size=64, num_workers=1, shuffle=False
)
elif args.dataset.lower() == "tinyimagenet_all":
transform_train = get_transform(
args.normalize, args.train_augment, dataset=args.dataset
)
transform_test = get_transform(args.normalize, False, dataset=args.dataset)
trainset = TinyImageNet(
TINYIMAGENET_ROOT,
split="train",
transform=transform_train,
classes="all",
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=64, num_workers=1, shuffle=True
)
testset = TinyImageNet(
TINYIMAGENET_ROOT,
split="val",
transform=transform_test,
classes="all",
)
testloader = torch.utils.data.DataLoader(
testset, batch_size=64, num_workers=1, shuffle=False
)
else:
print("Dataset not yet implemented. Exiting from test_model.py.")
sys.exit()
####################################################
####################################################
# Network and Optimizer
net = get_model(args.model, args.dataset)
# load model from path if a path is provided
if args.model_path is not None:
net = load_model_from_checkpoint(args.model, args.model_path, args.dataset)
else:
print("No model path provided, continuing test with untrained network.")
net = net.to(device)
####################################################
####################################################
# Test Model
training_acc = test(net, trainloader, device)
natural_acc = test(net, testloader, device)
print(now(), " Training accuracy: ", training_acc)
print(now(), " Natural accuracy: ", natural_acc)
stats = OrderedDict(
[
("model path", args.model_path),
("model", args.model),
("normalize", args.normalize),
("augment", args.train_augment),
("training_acc", training_acc),
("natural_acc", natural_acc),
]
)
to_results_table(stats, args.output, "clean_performance.csv")
####################################################
return
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="PyTorch poison benchmarking")
parser.add_argument(
"--model", default="ResNet18", type=str, help="model for training"
)
parser.add_argument("--dataset", default="CIFAR10", type=str, help="dataset")
parser.add_argument(
"--output", default="output_default", type=str, help="output subdirectory"
)
parser.add_argument(
"--model_path", default=None, type=str, help="where is the model saved?"
)
parser.add_argument("--normalize", dest="normalize", action="store_true")
parser.add_argument("--no-normalize", dest="normalize", action="store_false")
parser.set_defaults(normalize=True)
parser.add_argument("--train_augment", dest="train_augment", action="store_true")
args = parser.parse_args()
main(args)