-
Notifications
You must be signed in to change notification settings - Fork 0
/
ssf.py
154 lines (142 loc) · 5.45 KB
/
ssf.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import torch
import torch.nn.functional as F
import torch.nn as nn
from utils import gaussian_pyramids
from models import SimpleModel, CondModel
class VideoCompressionModel(nn.Module):
"""
P frame model with additional I frame model
"""
def __init__(
self,
dims,
hyper_dims,
use_scaler=False,
use_cond_residual=False,
gaussian_dim=5,
base_scale=1.0,
activation="relu",
vbr_dim=0,
):
super().__init__()
self.iframe_model = SimpleModel(
3, dims, hyper_dims, hyper_dims, hyper_dims, 3, activation, int(vbr_dim)
)
self.flow_model = SimpleModel(
6,
dims,
hyper_dims,
hyper_dims,
hyper_dims,
6 if use_scaler else 3,
activation,
int(vbr_dim),
)
if use_cond_residual:
self.residual_model = CondModel(
3,
dims,
hyper_dims,
hyper_dims,
hyper_dims,
3,
activation,
int(vbr_dim),
dec_add_latent=True,
)
else:
self.residual_model = SimpleModel(
3,
dims,
hyper_dims,
hyper_dims,
hyper_dims,
3,
activation,
int(vbr_dim),
dec_add_latent=True,
)
self.gaussian_dim = int(gaussian_dim)
self.base_scale = base_scale
self.use_scaler = use_scaler
self.use_cond_residual = use_cond_residual
def main_params(self, recurse=True):
for name, param in self.named_parameters(recurse=recurse):
if "_medians" not in name:
yield param
def median_params(self, recurse=True):
params = []
for name, param in self.named_parameters(recurse=recurse):
if "_medians" in name:
params.append(param)
return params
def scale_space_warp(self, input, flow):
# predict the corresponding sigma and then convert to the scale space location
N, C, H, W = input.shape
assert flow.shape == (N, 3, H, W)
flow = flow.unsqueeze(0)
multi_scale = gaussian_pyramids(input, self.base_scale, self.gaussian_dim)
h = torch.arange(H, device=input.device, dtype=input.dtype)
w = torch.arange(W, device=input.device, dtype=input.dtype)
d = torch.zeros(1, device=input.device, dtype=input.dtype)
grid = torch.stack(torch.meshgrid(d, h, w)[::-1], -1).unsqueeze(0)
grid = grid.expand(N, -1, -1, -1, -1)
flow = flow.permute(1, 0, 3, 4, 2) # N, 1, H, W, 3
# reparameterization
# var_channel = (flow[..., -1].exp())**2
# var_space = [0.] + [(2.**i * self.base_scale)**2 for i in range(self.gaussian_dim)]
# d_offset = var_to_position(var_channel, var_space).unsqueeze(-1)
d_offset = flow[..., -1].clamp(min=-1.0, max=1.0).unsqueeze(-1)
flow = torch.cat((flow[..., :2], d_offset), -1)
flow_grid = flow + grid
flow_grid[..., 0] = 2.0 * flow_grid[..., 0] / max(W - 1.0, 1.0) - 1.0
flow_grid[..., 1] = 2.0 * flow_grid[..., 1] / max(H - 1.0, 1.0) - 1.0
warped = F.grid_sample(
multi_scale, flow_grid, padding_mode="border", align_corners=True
).squeeze(2)
return warped
def compute_extra_loss(self):
return (
self.iframe_model.extra_loss(),
self.flow_model.extra_loss(),
self.residual_model.extra_loss(),
)
def iframe_forward(self, frame, cond=None):
state = self.iframe_model(frame, cond=cond)
return state
def pframe_forward(self, cur_frame, prev_frame, cond=None):
flow_input = torch.cat([cur_frame, prev_frame], 1)
flow_state = self.flow_model(flow_input, cond=cond)
if self.use_scaler:
flow = flow_state["output"][:, :3, ...]
scale = flow_state["output"][:, 3:, ...].exp()
else:
flow = flow_state["output"]
scale = 1
warped = self.scale_space_warp(prev_frame, flow)
residual = (cur_frame - warped) / scale
if self.use_cond_residual:
residual_state = self.residual_model(
residual, flow_state["q_latent"], flow_state["q_hyper_latent"], cond
)
else:
residual_state = self.residual_model(residual, flow_state["q_latent"], cond)
output = warped + residual_state["output"] * scale
bpp = flow_state["bpp"] + residual_state["bpp"]
return {"output": output, "bpp": bpp, "warped": warped.clamp(0, 1), "bpp_map": flow_state["bpp_map"] + residual_state["bpp_map"]}
def forward(self, video, cond=None):
T = video.shape[0]
states, prev_frame = [], None
for t in range(T):
if t == 0:
state = self.iframe_forward(video[0], cond=cond)
prev_frame = state["output"].detach()
else:
state = self.pframe_forward(video[t], prev_frame.clamp(0, 1), cond=cond)
prev_frame = state["output"]
states.append(state)
return {
"output": torch.stack([s["output"] for s in states], 0),
"bpp": torch.stack([s["bpp"] for s in states], 0),
"warped": torch.stack([s["warped"] for s in states[1:]], 0)
}