-
Notifications
You must be signed in to change notification settings - Fork 1
/
instancenormbatchswap.py
59 lines (48 loc) · 2.27 KB
/
instancenormbatchswap.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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import torch
class InstanceNormBatchSwap(torch.nn.Module):
def __init__(self, n_neurons, affine=False, eps=1e-5):
super(InstanceNormBatchSwap, self).__init__()
self.n_neurons = n_neurons
self.eps = eps
assert affine == False, 'affine parameters not implemented'
def forward(self, input):
assert input.shape[1] == self.n_neurons, "Input has incorrect shape"
if not self.training:
return input
temp = input.view(input.size(0), input.size(1), -1)
mean = temp.mean(2, keepdim=True).unsqueeze(-1)
std = temp.std(2, keepdim=True).unsqueeze(-1)
den = torch.sqrt(std.pow(2) + self.eps)
output = (input - mean)/den
indices = torch.randperm(input.size(0))
output = output * std.index_select(0, indices) + mean.index_select(0, indices)
return output
def __repr__(self):
return 'InstanceNormBatchSwap({}, eps={})'.format(self.n_neurons, self.eps)
class InstanceNormSimilarity(torch.nn.Module):
def __init__(self, n_neurons, affine=False, eps=1e-5, filename=None):
super(InstanceNormSimilarity, self).__init__()
self.n_neurons = n_neurons
self.eps = eps
self.filename = filename
assert affine == False, 'affine parameters not implemented'
def forward(self, input):
assert input.shape[1] == self.n_neurons, "Input has incorrect shape"
torch.set_printoptions(profile='full')
mean_file = open('{}-mean.csv'.format(self.filename), 'a', input.size(0))
std_file = open('{}-std.csv'.format(self.filename), 'a', input.size(0))
temp = input.view(input.size(0), input.size(1), -1)
mean = temp.mean(2)
std = temp.std(2)
for line in mean.cpu().numpy():
mean_file.write('{}\n'.format(', '.join(map(str, line))))
for line in std.cpu().numpy():
std_file.write('{}\n'.format(', '.join(map(str, line))))
# print('mean {} std {}'.format(mean.shape, std.shape))
# print('mean {} std {}'.format(mean, std))
mean_file.close()
std_file.close()
torch.set_printoptions(profile='default')
return input
def __repr__(self):
return 'InstanceNormSimilarity({}, eps={})'.format(self.n_neurons, self.eps)