-
Notifications
You must be signed in to change notification settings - Fork 1
/
Normalize.py
50 lines (39 loc) · 1.26 KB
/
Normalize.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
import torch
import torch.nn as nn
class Normalize(nn.Module):
def __init__(self, mean, std):
super(Normalize, self).__init__()
self.mean = mean
self.std = std
def forward(self, input):
size = input.size()
x = input.clone()
for i in range(size[1]):
x[:, i] = (x[:, i] - self.mean[i]) / self.std[i]
return x
class TfNormalize(nn.Module):
def __init__(self, mean=0, std=1, mode='tensorflow'):
"""
mode:
'tensorflow':convert data from [0,1] to [-1,1]
'torch':(input - mean) / std
"""
super(TfNormalize, self).__init__()
self.mean = mean
self.std = std
self.mode = mode
def forward(self, input):
size = input.size()
x = input.clone()
if self.mode == 'tensorflow':
x = x * 2.0 - 1.0 # convert data from [0,1] to [-1,1]
elif self.mode == 'torch':
for i in range(size[1]):
x[:, i] = (x[:, i] - self.mean[i]) / self.std[i]
return x
class Permute(nn.Module):
def __init__(self, permutation=[2, 1, 0]):
super().__init__()
self.permutation = permutation
def forward(self, input):
return input[:, self.permutation]