forked from mit-han-lab/once-for-all
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_specialized_net.py
181 lines (168 loc) · 6.58 KB
/
eval_specialized_net.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
# Once for All: Train One Network and Specialize it for Efficient Deployment
# Han Cai, Chuang Gan, Tianzhe Wang, Zhekai Zhang, Song Han
# International Conference on Learning Representations (ICLR), 2020.
import os
import os.path as osp
import argparse
import math
from tqdm import tqdm
import torch.nn as nn
import torch.backends.cudnn as cudnn
import torch.utils.data
from torchvision import transforms, datasets
from ofa.utils import AverageMeter, accuracy
from ofa.model_zoo import ofa_specialized
specialized_network_list = [
################# FLOPs #################
"flops@[email protected]_finetune@75",
"flops@[email protected]_finetune@75",
"flops@[email protected]_finetune@75",
################# ResNet50 Design Space #################
"[email protected][email protected]_finetune@25",
"[email protected][email protected]_finetune@25",
"[email protected][email protected]_finetune@25",
################# Google pixel1 #################
"pixel1_lat@[email protected]_finetune@75",
"pixel1_lat@[email protected]_finetune@75",
"pixel1_lat@[email protected]_finetune@75",
"pixel1_lat@[email protected]_finetune@75",
"pixel1_lat@[email protected]_finetune@25",
"pixel1_lat@[email protected]_finetune@25",
"pixel1_lat@[email protected]_finetune@25",
################# Google pixel2 #################
"pixel2_lat@[email protected]_finetune@25",
"pixel2_lat@[email protected]_finetune@25",
"pixel2_lat@[email protected]_finetune@25",
"pixel2_lat@[email protected]_finetune@25",
################# Samsung note10 #################
"note10_lat@[email protected]_finetune@75",
"note10_lat@[email protected]_finetune@75",
"note10_lat@[email protected]_finetune@75",
"note10_lat@[email protected]_finetune@75",
"note10_lat@[email protected]_finetune@25",
"note10_lat@[email protected]_finetune@25",
"note10_lat@[email protected]_finetune@25",
"note10_lat@[email protected]_finetune@25",
################# Samsung note8 #################
"note8_lat@[email protected]_finetune@25",
"note8_lat@[email protected]_finetune@25",
"note8_lat@[email protected]_finetune@25",
"note8_lat@[email protected]_finetune@25",
################# Samsung S7 Edge #################
"s7edge_lat@[email protected]_finetune@25",
"s7edge_lat@[email protected]_finetune@25",
"s7edge_lat@[email protected]_finetune@25",
"s7edge_lat@[email protected]_finetune@25",
################# LG G8 #################
"LG-G8_lat@[email protected]_finetune@25",
"LG-G8_lat@[email protected]_finetune@25",
"LG-G8_lat@[email protected]_finetune@25",
"LG-G8_lat@[email protected]_finetune@25",
################# 1080ti GPU (Batch Size 64) #################
"1080ti_gpu64@[email protected]_finetune@25",
"1080ti_gpu64@[email protected]_finetune@25",
"1080ti_gpu64@[email protected]_finetune@25",
"1080ti_gpu64@[email protected]_finetune@25",
################# V100 GPU (Batch Size 64) #################
"v100_gpu64@[email protected]_finetune@25",
"v100_gpu64@[email protected]_finetune@25",
"v100_gpu64@[email protected]_finetune@25",
"v100_gpu64@[email protected]_finetune@25",
################# Jetson TX2 GPU (Batch Size 16) #################
"tx2_gpu16@[email protected]_finetune@25",
"tx2_gpu16@[email protected]_finetune@25",
"tx2_gpu16@[email protected]_finetune@25",
"tx2_gpu16@[email protected]_finetune@25",
################# Intel Xeon CPU with MKL-DNN (Batch Size 1) #################
"cpu_lat@[email protected]_finetune@25",
"cpu_lat@[email protected]_finetune@25",
"cpu_lat@[email protected]_finetune@25",
"cpu_lat@[email protected]_finetune@25",
]
parser = argparse.ArgumentParser()
parser.add_argument(
"-p", "--path", help="The path of imagenet", type=str, default="/dataset/imagenet"
)
parser.add_argument("-g", "--gpu", help="The gpu(s) to use", type=str, default="all")
parser.add_argument(
"-b",
"--batch-size",
help="The batch on every device for validation",
type=int,
default=100,
)
parser.add_argument("-j", "--workers", help="Number of workers", type=int, default=20)
parser.add_argument(
"-n",
"--net",
metavar="NET",
default="pixel1_lat@[email protected]_finetune@75",
choices=specialized_network_list,
help="OFA specialized networks: "
+ " | ".join(specialized_network_list)
+ " (default: pixel1_lat@[email protected]_finetune@75)",
)
args = parser.parse_args()
if args.gpu == "all":
device_list = range(torch.cuda.device_count())
args.gpu = ",".join(str(_) for _ in device_list)
else:
device_list = [int(_) for _ in args.gpu.split(",")]
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
net, image_size = ofa_specialized(net_id=args.net, pretrained=True)
args.batch_size = args.batch_size * max(len(device_list), 1)
data_loader = torch.utils.data.DataLoader(
datasets.ImageFolder(
osp.join(args.path, "val"),
transforms.Compose(
[
transforms.Resize(int(math.ceil(image_size / 0.875))),
transforms.CenterCrop(image_size),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
),
),
batch_size=args.batch_size,
shuffle=True,
num_workers=args.workers,
pin_memory=True,
drop_last=False,
)
net = torch.nn.DataParallel(net).cuda()
cudnn.benchmark = True
criterion = nn.CrossEntropyLoss().cuda()
net.eval()
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
with torch.no_grad():
with tqdm(total=len(data_loader), desc="Validate") as t:
for i, (images, labels) in enumerate(data_loader):
images, labels = images.cuda(), labels.cuda()
# compute output
output = net(images)
loss = criterion(output, labels)
# measure accuracy and record loss
acc1, acc5 = accuracy(output, labels, topk=(1, 5))
losses.update(loss.item(), images.size(0))
top1.update(acc1[0].item(), images.size(0))
top5.update(acc5[0].item(), images.size(0))
t.set_postfix(
{
"loss": losses.avg,
"top1": top1.avg,
"top5": top5.avg,
"img_size": images.size(2),
}
)
t.update(1)
print("Test OFA specialized net <%s> with image size %d:" % (args.net, image_size))
print("Results: loss=%.5f,\t top1=%.1f,\t top5=%.1f" % (losses.avg, top1.avg, top5.avg))