-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAdaIN.py
24 lines (20 loc) · 1.1 KB
/
AdaIN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import torch
import torch.nn as nn
class AdaIN(nn.Module):
def __init__(self):
super().__init__()
def mu(self, x):
""" Takes a (n,c,h,w) tensor as input and returns the average across
it's spatial dimensions as (h,w) tensor [See eq. 5 of paper]"""
return torch.sum(x,(2,3))/(x.shape[2]*x.shape[3])
def sigma(self, x):
""" Takes a (n,c,h,w) tensor as input and returns the standard deviation
across it's spatial dimensions as (h,w) tensor [See eq. 6 of paper] Note
the permutations are required for broadcasting"""
return torch.sqrt((torch.sum((x.permute([2,3,0,1])-self.mu(x)).permute([2,3,0,1])**2,(2,3))+0.000000023)/(x.shape[2]*x.shape[3]))
def forward(self, x, y):
""" Takes a content embeding x and a style embeding y and changes
transforms the mean and standard deviation of the content embedding to
that of the style. [See eq. 8 of paper] Note the permutations are
required for broadcasting"""
return (self.sigma(y)*((x.permute([2,3,0,1])-self.mu(x))/self.sigma(x)) + self.mu(y)).permute([2,3,0,1])