-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbn_folder.py
More file actions
50 lines (32 loc) · 1.68 KB
/
bn_folder.py
File metadata and controls
50 lines (32 loc) · 1.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import copy
def bn_folding_model(model):
new_model = copy.deepcopy(model)
module_names = list(new_model._modules)
for k, name in enumerate(module_names):
if len(list(new_model._modules[name]._modules)) > 0:
new_model._modules[name] = bn_folding_model(new_model._modules[name])
else:
if isinstance(new_model._modules[name], nn.BatchNorm2d):
if isinstance(new_model._modules[module_names[k-1]], nn.Conv2d):
# Folded BN
folded_conv = fold_conv_bn_eval(new_model._modules[module_names[k-1]], new_model._modules[name])
# Replace old weight values
new_model._modules.pop(name) # Remove the BN layer
new_model._modules[module_names[k-1]] = folded_conv # Replace the Convolutional Layer by the folded version
return new_model
def bn_folding(conv_w, conv_b, bn_rm, bn_rv, bn_eps, bn_w, bn_b):
if conv_b is None:
conv_b = bn_rm.new_zeros(bn_rm.shape)
bn_var_rsqrt = torch.rsqrt(bn_rv + bn_eps)
w_fold = conv_w * (bn_w * bn_var_rsqrt).view(-1, 1, 1, 1)
b_fold = (conv_b - bn_rm) * bn_var_rsqrt * bn_w + bn_b
return torch.nn.Parameter(w_fold), torch.nn.Parameter(b_fold)
def fold_conv_bn_eval(conv, bn):
assert(not (conv.training or bn.training)), "Fusion only for eval!"
fused_conv = copy.deepcopy(conv)
fused_conv.weight, fused_conv.bias = bn_folding(fused_conv.weight, fused_conv.bias,
bn.running_mean, bn.running_var, bn.eps, bn.weight, bn.bias)
return fused_conv