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)
| 19 | XFORMERS_IS_AVAILBLE = False |
| 20 | |
| 21 | def 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 | |
| 36 | def adaptive_instance_normalization(content_feat, style_feat): |
| 37 | """Adaptive instance normalization. |
no outgoing calls
no test coverage detected