-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
executable file
·290 lines (242 loc) · 8.96 KB
/
utils.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
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
import os,sys
import numpy as np
import random
import matplotlib.pyplot as plt
import seaborn as sns
from copy import deepcopy
import math
import torch
import torch.nn as nn
from tqdm import tqdm
from torch._six import inf
import pandas as pd
from PIL import Image
from sklearn.feature_extraction import image
from arguments import get_args
args = get_args()
def gs_cal(t, x, y, criterion, model, sbatch=20):
# Init
param_R = {}
for name, param in model.named_parameters():
if len(param.size()) <= 1:
continue
name = name.split('.')[:-1]
name = '.'.join(name)
param = param.view(param.size(0), -1)
param_R['{}'.format(name)]=torch.zeros((param.size(0)))
# Compute
model.train()
for i in range(0,x.size(0),sbatch):
b=torch.LongTensor(np.arange(i,np.min([i+sbatch,x.size(0)]))).cuda()
images=x[b]
target=y[b]
# Forward and backward
outputs = model.forward(images, True)[t]
cnt = 0
for idx, j in enumerate(model.act):
j = torch.mean(j, dim=0)
if len(j.size())>1:
j = torch.mean(j.view(j.size(0), -1), dim = 1).abs()
model.act[idx] = j
for name, param in model.named_parameters():
if len(param.size()) <= 1 or 'last' in name or 'downsample' in name:
continue
name = name.split('.')[:-1]
name = '.'.join(name)
param_R[name] += model.act[cnt].abs().detach()*sbatch
cnt+=1
with torch.no_grad():
for key in param_R.keys():
param_R[key]=(param_R[key]/x.size(0))
return param_R
def print_model_report(model):
print('-'*100)
print(model)
print('Dimensions =',end=' ')
count=0
for p in model.parameters():
print(p.size(),end=' ')
count+=np.prod(p.size())
print()
print('Num parameters = %s'%(human_format(count)))
print('-'*100)
return count
def human_format(num):
magnitude=0
while abs(num)>=1000:
magnitude+=1
num/=1000.0
return '%.1f%s'%(num,['','K','M','G','T','P'][magnitude])
def print_optimizer_config(optim):
if optim is None:
print(optim)
else:
print(optim,'=',end=' ')
opt=optim.param_groups[0]
for n in opt.keys():
if not n.startswith('param'):
print(n+':',opt[n],end=', ')
print()
return
########################################################################################################################
def copy_model(model):
for module_ in model.net:
if isinstance(module_, ModuleList):
for linear_ in module_:
linear_.clean()
if isinstance(module_, ReLU) or isinstance(module_, Linear) or isinstance(module_, Conv2d) or isinstance(module_, MaxPool2d) or isinstance(module_, Dropout):
module_.clean()
return deepcopy(model)
def get_model(model):
return deepcopy(model.state_dict())
def set_model_(model,state_dict):
model.load_state_dict(deepcopy(state_dict))
return
def freeze_model(model):
for param in model.parameters():
param.requires_grad = False
return
########################################################################################################################
def compute_conv_output_size(Lin,kernel_size,stride=1,padding=0,dilation=1):
return int(np.floor((Lin+2*padding-dilation*(kernel_size-1)-1)/float(stride)+1))
########################################################################################################################
def compute_mean_std_dataset(dataset):
# dataset already put ToTensor
mean=0
std=0
loader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False)
for image, _ in loader:
mean+=image.mean(3).mean(2)
mean /= len(dataset)
mean_expanded=mean.view(mean.size(0),mean.size(1),1,1).expand_as(image)
for image, _ in loader:
std+=(image-mean_expanded).pow(2).sum(3).sum(2)
std=(std/(len(dataset)*image.size(2)*image.size(3)-1)).sqrt()
return mean, std
########################################################################################################################
def fisher_matrix_diag(t,x,y,model,criterion,sbatch=20):
# Init
fisher={}
for n,p in model.named_parameters():
fisher[n]=0*p.data
# Compute
model.train()
criterion = torch.nn.CrossEntropyLoss()
for i in tqdm(range(0,x.size(0),sbatch),desc='Fisher diagonal',ncols=100,ascii=True):
b=torch.LongTensor(np.arange(i,np.min([i+sbatch,x.size(0)]))).cuda()
images=x[b]
target=y[b]
# Forward and backward
model.zero_grad()
outputs = model.forward(images)[t]
loss= criterion(outputs, target)
loss.backward()
# Get gradients
for n,p in model.named_parameters():
if p.grad is not None:
fisher[n]+=sbatch*p.grad.data.pow(2)
# Mean
with torch.no_grad():
for n,_ in model.named_parameters():
fisher[n]=fisher[n]/x.size(0)
return fisher
def fisher_matrix_diag_emp(t,x,y,model,criterion,sbatch=20):
# Init
fisher={}
for n,p in model.named_parameters():
fisher[n]=0*p.data
fo={}
for n,p in model.named_parameters():
fo[n]=0*p.data
# Compute
model.train()
criterion = torch.nn.CrossEntropyLoss()
for i in tqdm(range(0,x.size(0),sbatch),desc='Fisher diagonal',ncols=100,ascii=True):
b=torch.LongTensor(np.arange(i,np.min([i+sbatch,x.size(0)]))).cuda()
images=x[b]
target=y[b]
# Forward and backward
model.zero_grad()
outputs = model.forward(images)[t]
loss= criterion(outputs, target)
loss.backward()
# Get gradients
for n,p in model.named_parameters():
if p.grad is not None:
fisher[n]+=sbatch*p.grad.data.pow(2)
fo[n] += sbatch * p.grad.data
# Mean
with torch.no_grad():
for n,_ in model.named_parameters():
fisher[n]=fisher[n]/x.size(0)
fo[n] = fo[n] / x.size(0)
return fisher, fo
########################################################################################################################
def cross_entropy(outputs,targets,exp=1,size_average=True,eps=1e-5):
out=torch.nn.functional.softmax(outputs)
tar=torch.nn.functional.softmax(targets)
if exp!=1:
out=out.pow(exp)
out=out/out.sum(1).view(-1,1).expand_as(out)
tar=tar.pow(exp)
tar=tar/tar.sum(1).view(-1,1).expand_as(tar)
out=out+eps/out.size(1)
out=out/out.sum(1).view(-1,1).expand_as(out)
ce=-(tar*out.log()).sum(1)
if size_average:
ce=ce.mean()
return ce
########################################################################################################################
def set_req_grad(layer,req_grad):
if hasattr(layer,'weight'):
layer.weight.requires_grad=req_grad
if hasattr(layer,'bias'):
layer.bias.requires_grad=req_grad
return
########################################################################################################################
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
########################################################################################################################
def clip_relevance_norm_(parameters, max_norm, norm_type=2):
r"""Clips gradient norm of an iterable of parameters.
The norm is computed over all gradients together, as if they were
concatenated into a single vector. Gradients are modified in-place.
Arguments:
parameters (Iterable[Tensor] or Tensor): an iterable of Tensors or a
single Tensor that will have gradients normalized
max_norm (float or int): max norm of the gradients
norm_type (float or int): type of the used p-norm. Can be ``'inf'`` for
infinity norm.
Returns:
Total norm of the parameters (viewed as a single vector).
"""
if isinstance(parameters, torch.Tensor):
parameters = [parameters]
parameters = list(filter(lambda p: p is not None, parameters))
max_norm = float(max_norm)
norm_type = float(norm_type)
if norm_type == inf:
total_norm = max(p.data.abs().max() for p in parameters)
else:
total_norm = 0
for p in parameters:
param_norm = p.data.norm(norm_type)
total_norm += param_norm.item() ** norm_type
total_norm = total_norm ** (1. / norm_type)
clip_coef = max_norm / (total_norm + 1e-6)
if clip_coef < 1:
for p in parameters:
p.data.mul_(clip_coef)
return total_norm
########################################################################################################################