PKaushik commited on
Commit
d4c9d5a
1 Parent(s): e259ff9
Files changed (1) hide show
  1. yolov6/layers/dbb_transforms.py +50 -0
yolov6/layers/dbb_transforms.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ import torch.nn.functional as F
4
+
5
+
6
+ def transI_fusebn(kernel, bn):
7
+ gamma = bn.weight
8
+ std = (bn.running_var + bn.eps).sqrt()
9
+ return kernel * ((gamma / std).reshape(-1, 1, 1, 1)), bn.bias - bn.running_mean * gamma / std
10
+
11
+
12
+ def transII_addbranch(kernels, biases):
13
+ return sum(kernels), sum(biases)
14
+
15
+
16
+ def transIII_1x1_kxk(k1, b1, k2, b2, groups):
17
+ if groups == 1:
18
+ k = F.conv2d(k2, k1.permute(1, 0, 2, 3)) #
19
+ b_hat = (k2 * b1.reshape(1, -1, 1, 1)).sum((1, 2, 3))
20
+ else:
21
+ k_slices = []
22
+ b_slices = []
23
+ k1_T = k1.permute(1, 0, 2, 3)
24
+ k1_group_width = k1.size(0) // groups
25
+ k2_group_width = k2.size(0) // groups
26
+ for g in range(groups):
27
+ k1_T_slice = k1_T[:, g*k1_group_width:(g+1)*k1_group_width, :, :]
28
+ k2_slice = k2[g*k2_group_width:(g+1)*k2_group_width, :, :, :]
29
+ k_slices.append(F.conv2d(k2_slice, k1_T_slice))
30
+ b_slices.append((k2_slice * b1[g * k1_group_width:(g+1) * k1_group_width].reshape(1, -1, 1, 1)).sum((1, 2, 3)))
31
+ k, b_hat = transIV_depthconcat(k_slices, b_slices)
32
+ return k, b_hat + b2
33
+
34
+
35
+ def transIV_depthconcat(kernels, biases):
36
+ return torch.cat(kernels, dim=0), torch.cat(biases)
37
+
38
+
39
+ def transV_avg(channels, kernel_size, groups):
40
+ input_dim = channels // groups
41
+ k = torch.zeros((channels, input_dim, kernel_size, kernel_size))
42
+ k[np.arange(channels), np.tile(np.arange(input_dim), groups), :, :] = 1.0 / kernel_size ** 2
43
+ return k
44
+
45
+
46
+ # This has not been tested with non-square kernels (kernel.size(2) != kernel.size(3)) nor even-size kernels
47
+ def transVI_multiscale(kernel, target_kernel_size):
48
+ H_pixels_to_pad = (target_kernel_size - kernel.size(2)) // 2
49
+ W_pixels_to_pad = (target_kernel_size - kernel.size(3)) // 2
50
+ return F.pad(kernel, [H_pixels_to_pad, H_pixels_to_pad, W_pixels_to_pad, W_pixels_to_pad])