MCPcopy Create free account
hub / github.com/IceClear/StableSR / calc_mean_std

Function calc_mean_std

ldm/modules/diffusionmodules/model.py:21–34  ·  view source on GitHub ↗

Calculate mean and std for adaptive_instance_normalization. Args: feat (Tensor): 4D tensor. eps (float): A small value added to the variance to avoid divide-by-zero. Default: 1e-5.

(feat, eps=1e-5)

Source from the content-addressed store, hash-verified

19 XFORMERS_IS_AVAILBLE = False
20
21def calc_mean_std(feat, eps=1e-5):
22 """Calculate mean and std for adaptive_instance_normalization.
23 Args:
24 feat (Tensor): 4D tensor.
25 eps (float): A small value added to the variance to avoid
26 divide-by-zero. Default: 1e-5.
27 """
28 size = feat.size()
29 assert len(size) == 4, 'The input feature should be 4D tensor.'
30 b, c = size[:2]
31 feat_var = feat.view(b, c, -1).var(dim=2) + eps
32 feat_std = feat_var.sqrt().view(b, c, 1, 1)
33 feat_mean = feat.view(b, c, -1).mean(dim=2).view(b, c, 1, 1)
34 return feat_mean, feat_std
35
36def adaptive_instance_normalization(content_feat, style_feat):
37 """Adaptive instance normalization.

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected