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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|