Isi99999 commited on
Commit
d302186
·
verified ·
1 Parent(s): 2bc2aca

Adding IFNet_HDv3.py to Rife4.15

Browse files
Rife4.15/RIFEv4.15/train_log/IFNet_HDv3.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, 32, 3, 2, 1)
28
+ self.cnn1 = nn.Conv2d(32, 32, 3, 1, 1)
29
+ self.cnn2 = nn.Conv2d(32, 32, 3, 1, 1)
30
+ self.cnn3 = nn.ConvTranspose2d(32, 8, 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*6, 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
+ return flow, mask
90
+
91
+ class IFNet(nn.Module):
92
+ def __init__(self):
93
+ super(IFNet, self).__init__()
94
+ self.block0 = IFBlock(7+16, c=192)
95
+ self.block1 = IFBlock(8+4+16, c=128)
96
+ self.block2 = IFBlock(8+4+16, c=96)
97
+ self.block3 = IFBlock(8+4+16, c=64)
98
+ self.encode = Head()
99
+ # self.contextnet = Contextnet()
100
+ # self.unet = Unet()
101
+
102
+ def forward(self, x, timestep=0.5, scale_list=[8, 4, 2, 1], training=False, fastmode=True, ensemble=False):
103
+ if training == False:
104
+ channel = x.shape[1] // 2
105
+ img0 = x[:, :channel]
106
+ img1 = x[:, channel:]
107
+ if not torch.is_tensor(timestep):
108
+ timestep = (x[:, :1].clone() * 0 + 1) * timestep
109
+ else:
110
+ timestep = timestep.repeat(1, 1, img0.shape[2], img0.shape[3])
111
+ f0 = self.encode(img0[:, :3])
112
+ f1 = self.encode(img1[:, :3])
113
+ flow_list = []
114
+ merged = []
115
+ mask_list = []
116
+ warped_img0 = img0
117
+ warped_img1 = img1
118
+ flow = None
119
+ mask = None
120
+ loss_cons = 0
121
+ block = [self.block0, self.block1, self.block2, self.block3]
122
+ for i in range(4):
123
+ if flow is None:
124
+ flow, mask = block[i](torch.cat((img0[:, :3], img1[:, :3], f0, f1, timestep), 1), None, scale=scale_list[i])
125
+ if ensemble:
126
+ f_, m_ = block[i](torch.cat((img1[:, :3], img0[:, :3], f1, f0, 1-timestep), 1), None, scale=scale_list[i])
127
+ flow = (flow + torch.cat((f_[:, 2:4], f_[:, :2]), 1)) / 2
128
+ mask = (mask + (-m_)) / 2
129
+ else:
130
+ wf0 = warp(f0, flow[:, :2])
131
+ wf1 = warp(f1, flow[:, 2:4])
132
+ fd, m0 = block[i](torch.cat((warped_img0[:, :3], warped_img1[:, :3], wf0, wf1, timestep, mask), 1), flow, scale=scale_list[i])
133
+ if ensemble:
134
+ f_, m_ = block[i](torch.cat((warped_img1[:, :3], warped_img0[:, :3], wf1, wf0, 1-timestep, -mask), 1), torch.cat((flow[:, 2:4], flow[:, :2]), 1), scale=scale_list[i])
135
+ fd = (fd + torch.cat((f_[:, 2:4], f_[:, :2]), 1)) / 2
136
+ mask = (m0 + (-m_)) / 2
137
+ else:
138
+ mask = m0
139
+ flow = flow + fd
140
+ mask_list.append(mask)
141
+ flow_list.append(flow)
142
+ warped_img0 = warp(img0, flow[:, :2])
143
+ warped_img1 = warp(img1, flow[:, 2:4])
144
+ merged.append((warped_img0, warped_img1))
145
+ mask = torch.sigmoid(mask)
146
+ merged[3] = (warped_img0 * mask + warped_img1 * (1 - mask))
147
+ if not fastmode:
148
+ print('contextnet is removed')
149
+ '''
150
+ c0 = self.contextnet(img0, flow[:, :2])
151
+ c1 = self.contextnet(img1, flow[:, 2:4])
152
+ tmp = self.unet(img0, img1, warped_img0, warped_img1, mask, flow, c0, c1)
153
+ res = tmp[:, :3] * 2 - 1
154
+ merged[3] = torch.clamp(merged[3] + res, 0, 1)
155
+ '''
156
+ return flow_list, mask_list[3], merged