-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathutils.py
More file actions
292 lines (228 loc) · 7.68 KB
/
utils.py
File metadata and controls
292 lines (228 loc) · 7.68 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from __future__ import print_function
import torch
import torchvision
import torchvision.transforms as transforms
import numpy as np
import os
import GPUtil
from enum import Enum
__all__ = [
"AverageMeter",
"get_cifar_loaders",
"load_model",
"get_error",
"get_no_params",
"train",
"validate",
"finetune",
"Cutout",
"select_devices",
]
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
error_history = []
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def get_cifar_loaders(
data_loc="./disk/scratch/datasets/cifar10/",
batch_size=128,
cutout=True,
n_holes=1,
length=16,
):
transform_train = transforms.Compose(
[
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
]
)
if cutout:
transform_train.transforms.append(Cutout(n_holes=n_holes, length=length))
transform_test = transforms.Compose(
[
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
]
)
trainset = torchvision.datasets.CIFAR10(
root=data_loc, train=True, download=True, transform=transform_train
)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=batch_size, shuffle=True, num_workers=2
)
testset = torchvision.datasets.CIFAR10(
root=data_loc, train=False, download=True, transform=transform_test
)
testloader = torch.utils.data.DataLoader(
testset, batch_size=batch_size, shuffle=False, num_workers=2
)
return trainloader, testloader
def load_model(model, sd):
sd = torch.load("checkpoints/%s.t7" % sd, map_location="cpu")
new_sd = model.state_dict()
if "state_dict" in sd.keys():
old_sd = sd["state_dict"]
else:
old_sd = sd["net"]
new_names = [v for v in new_sd]
old_names = [v for v in old_sd]
for i, j in enumerate(new_names):
if not "mask" in j:
new_sd[j] = old_sd[old_names[i]]
model.load_state_dict(new_sd)
return model, sd
def get_error(output, target, topk=(1,)):
"""Computes the error@k for the specified values of k"""
maxk = max(topk)
batch_size = target.size(0)
_, pred = output.topk(maxk, 1, True, True)
pred = pred.t()
correct = pred.eq(target.view(1, -1).expand_as(pred))
res = []
for k in topk:
correct_k = correct[:k].view(-1).float().sum(0, keepdim=True)
res.append(100.0 - correct_k.mul_(100.0 / batch_size))
return res
# count only conv params for now
def get_no_params(net, verbose=False, mask=False):
params = net
tot = 0
for p in params:
no = torch.sum(params[p] != 0)
if "conv" in p:
tot += no
return tot
def train(model, trainloader, criterion, optimizer):
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to train mode
model.train()
for i, (input, target) in enumerate(trainloader):
input, target = input.to(device), target.to(device)
# compute output
output = model(input)
loss = criterion(output, target)
# measure accuracy and record loss
err1, err5 = get_error(output.detach(), target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(err1.item(), input.size(0))
top5.update(err5.item(), input.size(0))
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
def validate(model, epoch, valloader, criterion, checkpoint=None, seed=None):
global error_history
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
# switch to evaluate mode
model.eval()
for _, (input, target) in enumerate(valloader):
input, target = input.to(device), target.to(device)
output = model(input)
loss = criterion(output, target)
err1, err5 = get_error(output.detach(), target, topk=(1, 5))
losses.update(loss.item(), input.size(0))
top1.update(err1.item(), input.size(0))
top5.update(err5.item(), input.size(0))
error_history.append(top1.avg)
if checkpoint:
state = {
"net": model.state_dict(),
"masks": [w for name, w in model.named_parameters() if "mask" in name],
"epoch": epoch,
"error_history": error_history,
}
torch.save(state, f"checkpoints/{checkpoint}_{seed}.t7")
def finetune(model, trainloader, criterion, optimizer, steps=100):
# switch to train mode
model.train()
dataiter = iter(trainloader)
for i in range(steps):
try:
input, target = dataiter.next()
except StopIteration:
dataiter = iter(trainloader)
input, target = dataiter.next()
input, target = input.to(device), target.to(device)
# compute output
output = model(input)
loss = criterion(output, target)
# compute gradient and do SGD step
optimizer.zero_grad()
loss.backward()
optimizer.step()
class Cutout(object):
"""Randomly mask out one or more patches from an image.
Args:
n_holes (int): Number of patches to cut out of each image.
length (int): The length (in pixels) of each square patch.
"""
def __init__(self, n_holes, length):
self.n_holes = n_holes
self.length = length
def __call__(self, img):
"""
Args:
img (Tensor): Tensor image of size (C, H, W).
Returns:
Tensor: Image with n_holes of dimension length x length cut out of it.
"""
h = img.size(1)
w = img.size(2)
mask = np.ones((h, w), np.float32)
for n in range(self.n_holes):
y = np.random.randint(h)
x = np.random.randint(w)
y1 = np.clip(y - self.length // 2, 0, h)
y2 = np.clip(y + self.length // 2, 0, h)
x1 = np.clip(x - self.length // 2, 0, w)
x2 = np.clip(x + self.length // 2, 0, w)
mask[y1:y2, x1:x2] = 0.0
mask = torch.from_numpy(mask)
mask = mask.expand_as(img)
img = img * mask
return img
# explicit by pass of the below
def select_devices(
num_gpus_to_use=0, max_load=0.01, max_memory=0.01, exclude_gpu_ids=None
):
if num_gpus_to_use == 0:
os.environ["CUDA_VISIBLE_DEVICES"] = ""
else:
if exclude_gpu_ids is None:
exclude_gpu_ids = []
gpu_to_use = GPUtil.getAvailable(
order="first",
limit=num_gpus_to_use,
maxLoad=max_load,
maxMemory=max_memory,
includeNan=False,
excludeID=exclude_gpu_ids,
excludeUUID=[],
)
if len(gpu_to_use) < num_gpus_to_use:
raise OSError(
"Couldnt find enough GPU(s) as required by the user, stopping program "
"- consider reducing "
"the requirements or using num_gpus_to_use=0 to use CPU"
)
os.environ["CUDA_VISIBLE_DEVICES"] = ",".join(
str(gpu_idx) for gpu_idx in gpu_to_use
)
print("GPUs selected have IDs {}".format(os.environ["CUDA_VISIBLE_DEVICES"]))