Spaces:
Running
Running
train_log upload
Browse files- train_log/IFNet_HDv3.py +115 -0
- train_log/RIFE_HDv3.py +88 -0
- train_log/flownet.pkl +3 -0
train_log/IFNet_HDv3.py
ADDED
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from model.warplayer import warp
|
5 |
+
|
6 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
7 |
+
|
8 |
+
def conv(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1):
|
9 |
+
return nn.Sequential(
|
10 |
+
nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride,
|
11 |
+
padding=padding, dilation=dilation, bias=True),
|
12 |
+
nn.PReLU(out_planes)
|
13 |
+
)
|
14 |
+
|
15 |
+
def conv_bn(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1):
|
16 |
+
return nn.Sequential(
|
17 |
+
nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride,
|
18 |
+
padding=padding, dilation=dilation, bias=False),
|
19 |
+
nn.BatchNorm2d(out_planes),
|
20 |
+
nn.PReLU(out_planes)
|
21 |
+
)
|
22 |
+
|
23 |
+
class IFBlock(nn.Module):
|
24 |
+
def __init__(self, in_planes, c=64):
|
25 |
+
super(IFBlock, self).__init__()
|
26 |
+
self.conv0 = nn.Sequential(
|
27 |
+
conv(in_planes, c//2, 3, 2, 1),
|
28 |
+
conv(c//2, c, 3, 2, 1),
|
29 |
+
)
|
30 |
+
self.convblock0 = nn.Sequential(
|
31 |
+
conv(c, c),
|
32 |
+
conv(c, c)
|
33 |
+
)
|
34 |
+
self.convblock1 = nn.Sequential(
|
35 |
+
conv(c, c),
|
36 |
+
conv(c, c)
|
37 |
+
)
|
38 |
+
self.convblock2 = nn.Sequential(
|
39 |
+
conv(c, c),
|
40 |
+
conv(c, c)
|
41 |
+
)
|
42 |
+
self.convblock3 = nn.Sequential(
|
43 |
+
conv(c, c),
|
44 |
+
conv(c, c)
|
45 |
+
)
|
46 |
+
self.conv1 = nn.Sequential(
|
47 |
+
nn.ConvTranspose2d(c, c//2, 4, 2, 1),
|
48 |
+
nn.PReLU(c//2),
|
49 |
+
nn.ConvTranspose2d(c//2, 4, 4, 2, 1),
|
50 |
+
)
|
51 |
+
self.conv2 = nn.Sequential(
|
52 |
+
nn.ConvTranspose2d(c, c//2, 4, 2, 1),
|
53 |
+
nn.PReLU(c//2),
|
54 |
+
nn.ConvTranspose2d(c//2, 1, 4, 2, 1),
|
55 |
+
)
|
56 |
+
|
57 |
+
def forward(self, x, flow, scale=1):
|
58 |
+
x = F.interpolate(x, scale_factor= 1. / scale, mode="bilinear", align_corners=False, recompute_scale_factor=False)
|
59 |
+
flow = F.interpolate(flow, scale_factor= 1. / scale, mode="bilinear", align_corners=False, recompute_scale_factor=False) * 1. / scale
|
60 |
+
feat = self.conv0(torch.cat((x, flow), 1))
|
61 |
+
feat = self.convblock0(feat) + feat
|
62 |
+
feat = self.convblock1(feat) + feat
|
63 |
+
feat = self.convblock2(feat) + feat
|
64 |
+
feat = self.convblock3(feat) + feat
|
65 |
+
flow = self.conv1(feat)
|
66 |
+
mask = self.conv2(feat)
|
67 |
+
flow = F.interpolate(flow, scale_factor=scale, mode="bilinear", align_corners=False, recompute_scale_factor=False) * scale
|
68 |
+
mask = F.interpolate(mask, scale_factor=scale, mode="bilinear", align_corners=False, recompute_scale_factor=False)
|
69 |
+
return flow, mask
|
70 |
+
|
71 |
+
class IFNet(nn.Module):
|
72 |
+
def __init__(self):
|
73 |
+
super(IFNet, self).__init__()
|
74 |
+
self.block0 = IFBlock(7+4, c=90)
|
75 |
+
self.block1 = IFBlock(7+4, c=90)
|
76 |
+
self.block2 = IFBlock(7+4, c=90)
|
77 |
+
self.block_tea = IFBlock(10+4, c=90)
|
78 |
+
# self.contextnet = Contextnet()
|
79 |
+
# self.unet = Unet()
|
80 |
+
|
81 |
+
def forward(self, x, scale_list=[4, 2, 1], training=False):
|
82 |
+
if training == False:
|
83 |
+
channel = x.shape[1] // 2
|
84 |
+
img0 = x[:, :channel]
|
85 |
+
img1 = x[:, channel:]
|
86 |
+
flow_list = []
|
87 |
+
merged = []
|
88 |
+
mask_list = []
|
89 |
+
warped_img0 = img0
|
90 |
+
warped_img1 = img1
|
91 |
+
flow = (x[:, :4]).detach() * 0
|
92 |
+
mask = (x[:, :1]).detach() * 0
|
93 |
+
loss_cons = 0
|
94 |
+
block = [self.block0, self.block1, self.block2]
|
95 |
+
for i in range(3):
|
96 |
+
f0, m0 = block[i](torch.cat((warped_img0[:, :3], warped_img1[:, :3], mask), 1), flow, scale=scale_list[i])
|
97 |
+
f1, m1 = block[i](torch.cat((warped_img1[:, :3], warped_img0[:, :3], -mask), 1), torch.cat((flow[:, 2:4], flow[:, :2]), 1), scale=scale_list[i])
|
98 |
+
flow = flow + (f0 + torch.cat((f1[:, 2:4], f1[:, :2]), 1)) / 2
|
99 |
+
mask = mask + (m0 + (-m1)) / 2
|
100 |
+
mask_list.append(mask)
|
101 |
+
flow_list.append(flow)
|
102 |
+
warped_img0 = warp(img0, flow[:, :2])
|
103 |
+
warped_img1 = warp(img1, flow[:, 2:4])
|
104 |
+
merged.append((warped_img0, warped_img1))
|
105 |
+
'''
|
106 |
+
c0 = self.contextnet(img0, flow[:, :2])
|
107 |
+
c1 = self.contextnet(img1, flow[:, 2:4])
|
108 |
+
tmp = self.unet(img0, img1, warped_img0, warped_img1, mask, flow, c0, c1)
|
109 |
+
res = tmp[:, 1:4] * 2 - 1
|
110 |
+
'''
|
111 |
+
for i in range(3):
|
112 |
+
mask_list[i] = torch.sigmoid(mask_list[i])
|
113 |
+
merged[i] = merged[i][0] * mask_list[i] + merged[i][1] * (1 - mask_list[i])
|
114 |
+
# merged[i] = torch.clamp(merged[i] + res, 0, 1)
|
115 |
+
return flow_list, mask_list[2], merged
|
train_log/RIFE_HDv3.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.vgg = VGGPerceptualLoss().to(device)
|
22 |
+
self.sobel = SOBEL()
|
23 |
+
if local_rank != -1:
|
24 |
+
self.flownet = DDP(self.flownet, device_ids=[local_rank], output_device=local_rank)
|
25 |
+
|
26 |
+
def train(self):
|
27 |
+
self.flownet.train()
|
28 |
+
|
29 |
+
def eval(self):
|
30 |
+
self.flownet.eval()
|
31 |
+
|
32 |
+
def device(self):
|
33 |
+
self.flownet.to(device)
|
34 |
+
|
35 |
+
def load_model(self, path, rank=0):
|
36 |
+
def convert(param):
|
37 |
+
if rank == -1:
|
38 |
+
return {
|
39 |
+
k.replace("module.", ""): v
|
40 |
+
for k, v in param.items()
|
41 |
+
if "module." in k
|
42 |
+
}
|
43 |
+
else:
|
44 |
+
return param
|
45 |
+
if rank <= 0:
|
46 |
+
if torch.cuda.is_available():
|
47 |
+
self.flownet.load_state_dict(convert(torch.load('{}/flownet.pkl'.format(path))))
|
48 |
+
else:
|
49 |
+
self.flownet.load_state_dict(convert(torch.load('{}/flownet.pkl'.format(path), map_location ='cpu')))
|
50 |
+
|
51 |
+
def save_model(self, path, rank=0):
|
52 |
+
if rank == 0:
|
53 |
+
torch.save(self.flownet.state_dict(),'{}/flownet.pkl'.format(path))
|
54 |
+
|
55 |
+
def inference(self, img0, img1, scale=1.0):
|
56 |
+
imgs = torch.cat((img0, img1), 1)
|
57 |
+
scale_list = [4/scale, 2/scale, 1/scale]
|
58 |
+
flow, mask, merged = self.flownet(imgs, scale_list)
|
59 |
+
return merged[2]
|
60 |
+
|
61 |
+
def update(self, imgs, gt, learning_rate=0, mul=1, training=True, flow_gt=None):
|
62 |
+
for param_group in self.optimG.param_groups:
|
63 |
+
param_group['lr'] = learning_rate
|
64 |
+
img0 = imgs[:, :3]
|
65 |
+
img1 = imgs[:, 3:]
|
66 |
+
if training:
|
67 |
+
self.train()
|
68 |
+
else:
|
69 |
+
self.eval()
|
70 |
+
scale = [4, 2, 1]
|
71 |
+
flow, mask, merged = self.flownet(torch.cat((imgs, gt), 1), scale=scale, training=training)
|
72 |
+
loss_l1 = (merged[2] - gt).abs().mean()
|
73 |
+
loss_smooth = self.sobel(flow[2], flow[2]*0).mean()
|
74 |
+
# loss_vgg = self.vgg(merged[2], gt)
|
75 |
+
if training:
|
76 |
+
self.optimG.zero_grad()
|
77 |
+
loss_G = loss_cons + loss_smooth * 0.1
|
78 |
+
loss_G.backward()
|
79 |
+
self.optimG.step()
|
80 |
+
else:
|
81 |
+
flow_teacher = flow[2]
|
82 |
+
return merged[2], {
|
83 |
+
'mask': mask,
|
84 |
+
'flow': flow[2][:, :2],
|
85 |
+
'loss_l1': loss_l1,
|
86 |
+
'loss_cons': loss_cons,
|
87 |
+
'loss_smooth': loss_smooth,
|
88 |
+
}
|
train_log/flownet.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fe854fc8996547c953f732aaa3b78cae76cc0a12833ae856ea0749c4c570d7d8
|
3 |
+
size 12186817
|