forked from fatemehazimi990/Pytorch-VideoFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoupling.py
More file actions
106 lines (81 loc) · 3.48 KB
/
Copy pathcoupling.py
File metadata and controls
106 lines (81 loc) · 3.48 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import torch
import torch.nn as nn
import torch.nn.functional as F
from act_norm import ActNorm
class AddCoupling(nn.Module):
def __init__(self, in_channels, mid_channels, use_act_norm=True):
super(AddCoupling, self).__init__()
self.nn = NN(in_channels, mid_channels, in_channels, use_act_norm)
# self.scale = nn.Parameter(torch.ones(in_channels, 1, 1))
def forward(self, x, ldj, reverse=False):
x_change, x_id = x.chunk(2, dim=1)
shift = self.nn(x_id)
if not reverse:
x_change = x_change + shift
else:
x_change = x_change - shift #z2
return torch.cat([x_id, x_change], dim=1), ldj+0.0
class AffineCoupling(nn.Module):
"""Affine coupling layer originally used in Real NVP and described by Glow.
Note: The official Glow implementation (https://github.com/openai/glow)
uses a different affine coupling formulation than described in the paper.
This implementation follows the paper and Real NVP.
Args:
in_channels (int): Number of channels in the input.
mid_channels (int): Number of channels in the intermediate activation
in NN.
"""
def __init__(self, in_channels, mid_channels):
super(AffineCoupling, self).__init__()
self.nn = NN(in_channels, mid_channels, 2 * in_channels)
self.scale = nn.Parameter(torch.ones(in_channels, 1, 1))
def forward(self, x, ldj, reverse=False):
x_change, x_id = x.chunk(2, dim=1)
st = self.nn(x_id)
s, t = st[:, 0::2, ...], st[:, 1::2, ...]
s = self.scale * torch.tanh(s)
# Scale and translate
if reverse:
x_change = x_change * s.mul(-1).exp() - t
ldj = ldj - s.flatten(1).sum(-1)
else:
x_change = (x_change + t) * s.exp()
ldj = ldj + s.flatten(1).sum(-1)
x = torch.cat((x_change, x_id), dim=1)
return x, ldj
class NN(nn.Module):
"""Small convolutional network used to compute scale and translate factors.
Args:
in_channels (int): Number of channels in the input.
mid_channels (int): Number of channels in the hidden activations.
out_channels (int): Number of channels in the output.
use_act_norm (bool): Use activation norm rather than batch norm.
"""
def __init__(self, in_channels, mid_channels, out_channels,
use_act_norm=False):
super(NN, self).__init__()
norm_fn = ActNorm if use_act_norm else nn.BatchNorm2d
self.in_norm = norm_fn(in_channels)
self.in_conv = nn.Conv2d(in_channels, mid_channels,
kernel_size=3, padding=1, bias=False)
nn.init.normal_(self.in_conv.weight, 0., 0.05)
self.mid_norm = norm_fn(mid_channels)
self.mid_conv = nn.Conv2d(mid_channels, mid_channels,
kernel_size=1, padding=0, bias=False)
nn.init.normal_(self.mid_conv.weight, 0., 0.05)
self.out_norm = norm_fn(mid_channels)
self.out_conv = nn.Conv2d(mid_channels, out_channels,
kernel_size=3, padding=1, bias=True)
nn.init.zeros_(self.out_conv.weight)
nn.init.zeros_(self.out_conv.bias)
def forward(self, x):
x = self.in_norm(x)
x = F.relu(x)
x = self.in_conv(x)
x = self.mid_norm(x)
x = F.relu(x)
x = self.mid_conv(x)
x = self.out_norm(x)
x = F.relu(x)
x = self.out_conv(x)
return x