MCPcopy Create free account
hub / github.com/VisionRush/DeepFakeDefenders / deep_fuse_BN

Method deep_fuse_BN

model/replknet.py:303–320  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

301 # The BNs after and before conv layers can be removed.
302 # No need to call this if your framework support automatic BN fusion.
303 def deep_fuse_BN(self):
304 for m in self.modules():
305 if not isinstance(m, nn.Sequential):
306 continue
307 if not len(m) in [2, 3]: # Only handle conv-BN or conv-BN-relu
308 continue
309 # If you use a custom Conv2d impl, assume it also has 'kernel_size' and 'weight'
310 if hasattr(m[0], 'kernel_size') and hasattr(m[0], 'weight') and isinstance(m[1], nn.BatchNorm2d):
311 conv = m[0]
312 bn = m[1]
313 fused_kernel, fused_bias = fuse_bn(conv, bn)
314 fused_conv = get_conv2d(conv.in_channels, conv.out_channels, kernel_size=conv.kernel_size,
315 stride=conv.stride,
316 padding=conv.padding, dilation=conv.dilation, groups=conv.groups, bias=True)
317 fused_conv.weight.data = fused_kernel
318 fused_conv.bias.data = fused_bias
319 m[0] = fused_conv
320 m[1] = nn.Identity()
321
322
323def create_RepLKNet31B(drop_path_rate=0.5, num_classes=1000, use_checkpoint=False, small_kernel_merged=False, use_sync_bn=True):

Callers

nothing calls this directly

Calls 2

fuse_bnFunction · 0.85
get_conv2dFunction · 0.85

Tested by

no test coverage detected