Avraam B commited on
Commit
9c585d9
·
verified ·
1 Parent(s): 955417e

Upload 4 files

Browse files
video_interpolation/IFNet_HDv3.py ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import torch.nn.functional as F
4
+ from model.warplayer import warp
5
+ # from train_log.refine import *
6
+
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+
9
+ def conv(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1):
10
+ return nn.Sequential(
11
+ nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride,
12
+ padding=padding, dilation=dilation, bias=True),
13
+ nn.LeakyReLU(0.2, True)
14
+ )
15
+
16
+ def conv_bn(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1):
17
+ return nn.Sequential(
18
+ nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride,
19
+ padding=padding, dilation=dilation, bias=False),
20
+ nn.BatchNorm2d(out_planes),
21
+ nn.LeakyReLU(0.2, True)
22
+ )
23
+
24
+ class Head(nn.Module):
25
+ def __init__(self):
26
+ super(Head, self).__init__()
27
+ self.cnn0 = nn.Conv2d(3, 16, 3, 2, 1)
28
+ self.cnn1 = nn.Conv2d(16, 16, 3, 1, 1)
29
+ self.cnn2 = nn.Conv2d(16, 16, 3, 1, 1)
30
+ self.cnn3 = nn.ConvTranspose2d(16, 4, 4, 2, 1)
31
+ self.relu = nn.LeakyReLU(0.2, True)
32
+
33
+ def forward(self, x, feat=False):
34
+ x0 = self.cnn0(x)
35
+ x = self.relu(x0)
36
+ x1 = self.cnn1(x)
37
+ x = self.relu(x1)
38
+ x2 = self.cnn2(x)
39
+ x = self.relu(x2)
40
+ x3 = self.cnn3(x)
41
+ if feat:
42
+ return [x0, x1, x2, x3]
43
+ return x3
44
+
45
+ class ResConv(nn.Module):
46
+ def __init__(self, c, dilation=1):
47
+ super(ResConv, self).__init__()
48
+ self.conv = nn.Conv2d(c, c, 3, 1, dilation, dilation=dilation, groups=1\
49
+ )
50
+ self.beta = nn.Parameter(torch.ones((1, c, 1, 1)), requires_grad=True)
51
+ self.relu = nn.LeakyReLU(0.2, True)
52
+
53
+ def forward(self, x):
54
+ return self.relu(self.conv(x) * self.beta + x)
55
+
56
+ class IFBlock(nn.Module):
57
+ def __init__(self, in_planes, c=64):
58
+ super(IFBlock, self).__init__()
59
+ self.conv0 = nn.Sequential(
60
+ conv(in_planes, c//2, 3, 2, 1),
61
+ conv(c//2, c, 3, 2, 1),
62
+ )
63
+ self.convblock = nn.Sequential(
64
+ ResConv(c),
65
+ ResConv(c),
66
+ ResConv(c),
67
+ ResConv(c),
68
+ ResConv(c),
69
+ ResConv(c),
70
+ ResConv(c),
71
+ ResConv(c),
72
+ )
73
+ self.lastconv = nn.Sequential(
74
+ nn.ConvTranspose2d(c, 4*13, 4, 2, 1),
75
+ nn.PixelShuffle(2)
76
+ )
77
+
78
+ def forward(self, x, flow=None, scale=1):
79
+ x = F.interpolate(x, scale_factor= 1. / scale, mode="bilinear", align_corners=False)
80
+ if flow is not None:
81
+ flow = F.interpolate(flow, scale_factor= 1. / scale, mode="bilinear", align_corners=False) * 1. / scale
82
+ x = torch.cat((x, flow), 1)
83
+ feat = self.conv0(x)
84
+ feat = self.convblock(feat)
85
+ tmp = self.lastconv(feat)
86
+ tmp = F.interpolate(tmp, scale_factor=scale, mode="bilinear", align_corners=False)
87
+ flow = tmp[:, :4] * scale
88
+ mask = tmp[:, 4:5]
89
+ feat = tmp[:, 5:]
90
+ return flow, mask, feat
91
+
92
+ class IFNet(nn.Module):
93
+ def __init__(self):
94
+ super(IFNet, self).__init__()
95
+ self.block0 = IFBlock(7+8, c=192)
96
+ self.block1 = IFBlock(8+4+8+8, c=128)
97
+ self.block2 = IFBlock(8+4+8+8, c=96)
98
+ self.block3 = IFBlock(8+4+8+8, c=64)
99
+ self.block4 = IFBlock(8+4+8+8, c=32)
100
+ self.encode = Head()
101
+
102
+ # not used during inference
103
+ '''
104
+ self.teacher = IFBlock(8+4+8+3+8, c=64)
105
+ self.caltime = nn.Sequential(
106
+ nn.Conv2d(16+9, 8, 3, 2, 1),
107
+ nn.LeakyReLU(0.2, True),
108
+ nn.Conv2d(32, 64, 3, 2, 1),
109
+ nn.LeakyReLU(0.2, True),
110
+ nn.Conv2d(64, 64, 3, 1, 1),
111
+ nn.LeakyReLU(0.2, True),
112
+ nn.Conv2d(64, 64, 3, 1, 1),
113
+ nn.LeakyReLU(0.2, True),
114
+ nn.Conv2d(64, 1, 3, 1, 1),
115
+ nn.Sigmoid()
116
+ )
117
+ '''
118
+
119
+ def forward(self, x, timestep=0.5, scale_list=[8, 4, 2, 1], training=False, fastmode=True, ensemble=False):
120
+ if training == False:
121
+ channel = x.shape[1] // 2
122
+ img0 = x[:, :channel]
123
+ img1 = x[:, channel:]
124
+ if not torch.is_tensor(timestep):
125
+ timestep = (x[:, :1].clone() * 0 + 1) * timestep
126
+ else:
127
+ timestep = timestep.repeat(1, 1, img0.shape[2], img0.shape[3])
128
+ f0 = self.encode(img0[:, :3])
129
+ f1 = self.encode(img1[:, :3])
130
+ flow_list = []
131
+ merged = []
132
+ mask_list = []
133
+ warped_img0 = img0
134
+ warped_img1 = img1
135
+ flow = None
136
+ mask = None
137
+ loss_cons = 0
138
+ block = [self.block0, self.block1, self.block2, self.block3, self.block4]
139
+ for i in range(5):
140
+ if flow is None:
141
+ flow, mask, feat = block[i](torch.cat((img0[:, :3], img1[:, :3], f0, f1, timestep), 1), None, scale=scale_list[i])
142
+ if ensemble:
143
+ print("warning: ensemble is not supported since RIFEv4.21")
144
+ else:
145
+ wf0 = warp(f0, flow[:, :2])
146
+ wf1 = warp(f1, flow[:, 2:4])
147
+ fd, m0, feat = block[i](torch.cat((warped_img0[:, :3], warped_img1[:, :3], wf0, wf1, timestep, mask, feat), 1), flow, scale=scale_list[i])
148
+ if ensemble:
149
+ print("warning: ensemble is not supported since RIFEv4.21")
150
+ else:
151
+ mask = m0
152
+ flow = flow + fd
153
+ mask_list.append(mask)
154
+ flow_list.append(flow)
155
+ warped_img0 = warp(img0, flow[:, :2])
156
+ warped_img1 = warp(img1, flow[:, 2:4])
157
+ merged.append((warped_img0, warped_img1))
158
+ mask = torch.sigmoid(mask)
159
+ merged[4] = (warped_img0 * mask + warped_img1 * (1 - mask))
160
+ if not fastmode:
161
+ print('contextnet is removed')
162
+ '''
163
+ c0 = self.contextnet(img0, flow[:, :2])
164
+ c1 = self.contextnet(img1, flow[:, 2:4])
165
+ tmp = self.unet(img0, img1, warped_img0, warped_img1, mask, flow, c0, c1)
166
+ res = tmp[:, :3] * 2 - 1
167
+ merged[4] = torch.clamp(merged[4] + res, 0, 1)
168
+ '''
169
+ return flow_list, mask_list[4], merged
video_interpolation/RIFE_HDv3.py ADDED
@@ -0,0 +1,89 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import numpy as np
4
+ from torch.optim import AdamW
5
+ import torch.optim as optim
6
+ import itertools
7
+ from model.warplayer import warp
8
+ from torch.nn.parallel import DistributedDataParallel as DDP
9
+ from train_log.IFNet_HDv3 import *
10
+ import torch.nn.functional as F
11
+ from model.loss import *
12
+
13
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
+
15
+ class Model:
16
+ def __init__(self, local_rank=-1):
17
+ self.flownet = IFNet()
18
+ self.device()
19
+ self.optimG = AdamW(self.flownet.parameters(), lr=1e-6, weight_decay=1e-4)
20
+ self.epe = EPE()
21
+ self.version = 4.25
22
+ # self.vgg = VGGPerceptualLoss().to(device)
23
+ self.sobel = SOBEL()
24
+ if local_rank != -1:
25
+ self.flownet = DDP(self.flownet, device_ids=[local_rank], output_device=local_rank)
26
+
27
+ def train(self):
28
+ self.flownet.train()
29
+
30
+ def eval(self):
31
+ self.flownet.eval()
32
+
33
+ def device(self):
34
+ self.flownet.to(device)
35
+
36
+ def load_model(self, path, rank=0):
37
+ def convert(param):
38
+ if rank == -1:
39
+ return {
40
+ k.replace("module.", ""): v
41
+ for k, v in param.items()
42
+ if "module." in k
43
+ }
44
+ else:
45
+ return param
46
+ if rank <= 0:
47
+ if torch.cuda.is_available():
48
+ self.flownet.load_state_dict(convert(torch.load('{}/flownet.pkl'.format(path))), False)
49
+ else:
50
+ self.flownet.load_state_dict(convert(torch.load('{}/flownet.pkl'.format(path), map_location ='cpu')), False)
51
+
52
+ def save_model(self, path, rank=0):
53
+ if rank == 0:
54
+ torch.save(self.flownet.state_dict(),'{}/flownet.pkl'.format(path))
55
+
56
+ def inference(self, img0, img1, timestep=0.5, scale=1.0):
57
+ imgs = torch.cat((img0, img1), 1)
58
+ scale_list = [16/scale, 8/scale, 4/scale, 2/scale, 1/scale]
59
+ flow, mask, merged = self.flownet(imgs, timestep, scale_list)
60
+ return merged[-1]
61
+
62
+ def update(self, imgs, gt, learning_rate=0, mul=1, training=True, flow_gt=None):
63
+ for param_group in self.optimG.param_groups:
64
+ param_group['lr'] = learning_rate
65
+ img0 = imgs[:, :3]
66
+ img1 = imgs[:, 3:]
67
+ if training:
68
+ self.train()
69
+ else:
70
+ self.eval()
71
+ scale = [16, 8, 4, 2, 1]
72
+ flow, mask, merged = self.flownet(torch.cat((imgs, gt), 1), scale=scale, training=training)
73
+ loss_l1 = (merged[-1] - gt).abs().mean()
74
+ loss_smooth = self.sobel(flow[-1], flow[-1]*0).mean()
75
+ # loss_vgg = self.vgg(merged[-1], gt)
76
+ if training:
77
+ self.optimG.zero_grad()
78
+ loss_G = loss_l1 + loss_cons + loss_smooth * 0.1
79
+ loss_G.backward()
80
+ self.optimG.step()
81
+ else:
82
+ flow_teacher = flow[2]
83
+ return merged[-1], {
84
+ 'mask': mask,
85
+ 'flow': flow[-1][:, :2],
86
+ 'loss_l1': loss_l1,
87
+ 'loss_cons': loss_cons,
88
+ 'loss_smooth': loss_smooth,
89
+ }
video_interpolation/flownet.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6615790efd627772917205db291f51cd392528a157ecbb2ecaeec3bff8eb6de2
3
+ size 24636301
video_interpolation/refine.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import numpy as np
4
+ from torch.optim import AdamW
5
+ import torch.optim as optim
6
+ import itertools
7
+ from model.warplayer import warp
8
+ from torch.nn.parallel import DistributedDataParallel as DDP
9
+ import torch.nn.functional as F
10
+
11
+ device = torch.device("cuda")
12
+
13
+ def conv(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1):
14
+ return nn.Sequential(
15
+ nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride,
16
+ padding=padding, dilation=dilation, bias=True),
17
+ nn.LeakyReLU(0.2, True)
18
+ )
19
+
20
+ def conv_woact(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1):
21
+ return nn.Sequential(
22
+ nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride,
23
+ padding=padding, dilation=dilation, bias=True),
24
+ )
25
+
26
+ def deconv(in_planes, out_planes, kernel_size=4, stride=2, padding=1):
27
+ return nn.Sequential(
28
+ torch.nn.ConvTranspose2d(in_channels=in_planes, out_channels=out_planes, kernel_size=4, stride=2, padding=1, bias=True),
29
+ nn.LeakyReLU(0.2, True)
30
+ )
31
+
32
+ class Conv2(nn.Module):
33
+ def __init__(self, in_planes, out_planes, stride=2):
34
+ super(Conv2, self).__init__()
35
+ self.conv1 = conv(in_planes, out_planes, 3, stride, 1)
36
+ self.conv2 = conv(out_planes, out_planes, 3, 1, 1)
37
+
38
+ def forward(self, x):
39
+ x = self.conv1(x)
40
+ x = self.conv2(x)
41
+ return x
42
+
43
+ c = 16
44
+ class Contextnet(nn.Module):
45
+ def __init__(self):
46
+ super(Contextnet, self).__init__()
47
+ self.conv1 = Conv2(3, c)
48
+ self.conv2 = Conv2(c, 2*c)
49
+ self.conv3 = Conv2(2*c, 4*c)
50
+ self.conv4 = Conv2(4*c, 8*c)
51
+
52
+ def forward(self, x, flow):
53
+ x = self.conv1(x)
54
+ flow = F.interpolate(flow, scale_factor=0.5, mode="bilinear", align_corners=False) * 0.5
55
+ f1 = warp(x, flow)
56
+ x = self.conv2(x)
57
+ flow = F.interpolate(flow, scale_factor=0.5, mode="bilinear", align_corners=False) * 0.5
58
+ f2 = warp(x, flow)
59
+ x = self.conv3(x)
60
+ flow = F.interpolate(flow, scale_factor=0.5, mode="bilinear", align_corners=False) * 0.5
61
+ f3 = warp(x, flow)
62
+ x = self.conv4(x)
63
+ flow = F.interpolate(flow, scale_factor=0.5, mode="bilinear", align_corners=False) * 0.5
64
+ f4 = warp(x, flow)
65
+ return [f1, f2, f3, f4]
66
+
67
+ class Unet(nn.Module):
68
+ def __init__(self):
69
+ super(Unet, self).__init__()
70
+ self.down0 = Conv2(17, 2*c)
71
+ self.down1 = Conv2(4*c, 4*c)
72
+ self.down2 = Conv2(8*c, 8*c)
73
+ self.down3 = Conv2(16*c, 16*c)
74
+ self.up0 = deconv(32*c, 8*c)
75
+ self.up1 = deconv(16*c, 4*c)
76
+ self.up2 = deconv(8*c, 2*c)
77
+ self.up3 = deconv(4*c, c)
78
+ self.conv = nn.Conv2d(c, 3, 3, 1, 1)
79
+
80
+ def forward(self, img0, img1, warped_img0, warped_img1, mask, flow, c0, c1):
81
+ s0 = self.down0(torch.cat((img0, img1, warped_img0, warped_img1, mask, flow), 1))
82
+ s1 = self.down1(torch.cat((s0, c0[0], c1[0]), 1))
83
+ s2 = self.down2(torch.cat((s1, c0[1], c1[1]), 1))
84
+ s3 = self.down3(torch.cat((s2, c0[2], c1[2]), 1))
85
+ x = self.up0(torch.cat((s3, c0[3], c1[3]), 1))
86
+ x = self.up1(torch.cat((x, s2), 1))
87
+ x = self.up2(torch.cat((x, s1), 1))
88
+ x = self.up3(torch.cat((x, s0), 1))
89
+ x = self.conv(x)
90
+ return torch.sigmoid(x)