TensorForger commited on
Commit
3cf1416
·
1 Parent(s): 993591f

added model weights and code

Browse files
Files changed (6) hide show
  1. LICENSE +21 -0
  2. README.md +107 -3
  3. demo.py +28 -0
  4. flownet.safetensors +3 -0
  5. interpolation_model.py +184 -0
  6. requirements.txt +4 -0
LICENSE ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ MIT License
2
+
3
+ Copyright (c) Megvii Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
README.md CHANGED
@@ -1,3 +1,107 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ tags:
4
+ - video-interpolation
5
+ - frame-interpolation
6
+ - optical-flow
7
+ - pytorch
8
+ - safetensors
9
+ - tensorrt
10
+ - torch-compile
11
+ library_name: pytorch
12
+ ---
13
+
14
+ # RIFE — Real-Time Intermediate Flow Estimation
15
+
16
+ Safetensors re-host of the RIFE v4 frame interpolation model from
17
+ [ECCV2022-RIFE](https://github.com/hzwer/ECCV2022-RIFE) (MIT license, © Megvii Inc.).
18
+
19
+ ## What is different from the original
20
+
21
+ | | Original | This repo |
22
+ |---|---|---|
23
+ | **Weight format** | `.pkl` hosted on Google Drive | `.safetensors` hosted on Hugging Face |
24
+ | **TensorRT / `torch.compile`** | Known issues with torchinductor and TensorRT backends | Fixed — model sources are fully compatible |
25
+
26
+ The architectural code in [interpolation_model.py](interpolation_model.py) is a minimal
27
+ clean-up of the upstream
28
+ [`model/IFNet.py`](https://github.com/hzwer/ECCV2022-RIFE/blob/main/model/IFNet.py)
29
+ with all changes required for `torch.compile` and TensorRT export applied.
30
+
31
+ ## Model description
32
+
33
+ RIFE (Real-Time Intermediate Flow Estimation) estimates an intermediate video frame
34
+ between two input frames by computing bidirectional optical flow and blending the
35
+ warped frames with a learned mask.
36
+
37
+ **Input:** a `(B, 6, H, W)` tensor — the first frame in channels `[:3]` and the
38
+ second frame in channels `[3:]`, values in `[0, 1]`.
39
+ **Output:** a `(B, 3, H, W)` interpolated frame tensor, values in `[0, 1]`.
40
+
41
+ ## Installation
42
+
43
+ ```bash
44
+ git clone https://huggingface.co/tensorforger/RIFE-safetensors
45
+ cd RIFE-safetensors
46
+ pip install -r requirements.txt
47
+ ```
48
+
49
+ ## Usage
50
+
51
+ ```python
52
+ import torch
53
+ from safetensors.torch import load_file
54
+ from interpolation_model import IFNet
55
+
56
+ model = IFNet()
57
+ model.load_state_dict(load_file("flownet.safetensors"))
58
+ model.to("cuda").eval()
59
+
60
+ # frame0, frame1: (B, 3, H, W) float tensors in [0, 1]
61
+ x = torch.cat([frame0, frame1], dim=1) # → (B, 6, H, W)
62
+ with torch.no_grad():
63
+ mid_frame = model(x) # → (B, 3, H, W)
64
+ ```
65
+
66
+ ### torch.compile (torchinductor / TensorRT)
67
+
68
+ ```python
69
+ model = torch.compile(model, backend="inductor") # or backend="tensorrt"
70
+ ```
71
+
72
+ ### Run the bundled demo
73
+
74
+ The demo generates two synthetic frames with shifted gray squares and displays the
75
+ interpolated result with `matplotlib`.
76
+
77
+ ```bash
78
+ python demo.py
79
+ ```
80
+
81
+ Output shape printed to stdout: `torch.Size([1, 3, 256, 256])`.
82
+ A window will open showing **frame 0 · interpolated frame · frame 1**.
83
+
84
+ ## Files
85
+
86
+ | File | Description |
87
+ |------|-------------|
88
+ | `flownet.safetensors` | Model weights (converted from original `.pkl`) |
89
+ | `interpolation_model.py` | `IFNet` model definition (compile-friendly fork of upstream) |
90
+ | `demo.py` | Minimal runnable example |
91
+ | `requirements.txt` | Python dependencies |
92
+
93
+ ## Citation
94
+
95
+ ```bibtex
96
+ @inproceedings{huang2022rife,
97
+ title = {Real-Time Intermediate Flow Estimation for Video Frame Interpolation},
98
+ author = {Huang, Zhewei and Zhang, Tianyuan and Heng, Wen and Shi, Boxin and Zhou, Shuchang},
99
+ booktitle = {Proceedings of the European Conference on Computer Vision (ECCV)},
100
+ year = {2022}
101
+ }
102
+ ```
103
+
104
+ ## License
105
+
106
+ MIT — see [LICENSE](LICENSE).
107
+ Original work © Megvii Inc. This re-host adds no new restrictions.
demo.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import matplotlib.pyplot as plt
2
+ import torch
3
+ from safetensors.torch import load_file
4
+ from interpolation_model import IFNet
5
+
6
+ interpolation_model = IFNet()
7
+ interpolation_model.load_state_dict(load_file("flownet.safetensors"))
8
+ interpolation_model.to("cuda")
9
+ interpolation_model.eval()
10
+
11
+ input_frames = torch.zeros([1, 6, 256, 256], device="cuda")
12
+
13
+ # draw squares
14
+ input_frames[:, :3, 100:200, 100:200] = 0.5
15
+ input_frames[:, 3:, 140:250, 140:240] = 0.5
16
+
17
+ with torch.no_grad():
18
+ interpolated_frame = interpolation_model(input_frames)
19
+
20
+ print(interpolated_frame.shape) # torch.Size([1, 3, 256, 256])
21
+
22
+ fig, ax = plt.subplots(1, 3, figsize=(12, 4))
23
+
24
+ ax[0].imshow(input_frames[0, :3].permute(1, 2, 0).cpu().numpy())
25
+ ax[1].imshow(interpolated_frame[0].permute(1, 2, 0).cpu().numpy())
26
+ ax[2].imshow(input_frames[0, 3:].permute(1, 2, 0).cpu().numpy())
27
+
28
+ plt.show()
flownet.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4b953bee2017bea5e71aae69c36a9caf316071788f9956b7099c015c75ee6af7
3
+ size 12166924
interpolation_model.py ADDED
@@ -0,0 +1,184 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Forked from https://github.com/hzwer/ECCV2022-RIFE/blob/main/model/IFNet.py
2
+
3
+ import torch
4
+ import torch.nn as nn
5
+ import torch.nn.functional as F
6
+
7
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
8
+ dtype = torch.float16
9
+
10
+
11
+ def conv(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1):
12
+ return nn.Sequential(
13
+ nn.Conv2d(
14
+ in_planes,
15
+ out_planes,
16
+ kernel_size=kernel_size,
17
+ stride=stride,
18
+ padding=padding,
19
+ dilation=dilation,
20
+ bias=True,
21
+ ),
22
+ nn.PReLU(out_planes),
23
+ )
24
+
25
+
26
+ def conv_bn(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1):
27
+ return nn.Sequential(
28
+ nn.Conv2d(
29
+ in_planes,
30
+ out_planes,
31
+ kernel_size=kernel_size,
32
+ stride=stride,
33
+ padding=padding,
34
+ dilation=dilation,
35
+ bias=False,
36
+ ),
37
+ nn.BatchNorm2d(out_planes),
38
+ nn.PReLU(out_planes),
39
+ )
40
+
41
+
42
+ class IFBlock(nn.Module):
43
+ def __init__(self, in_planes, c=64):
44
+ super(IFBlock, self).__init__()
45
+ self.conv0 = nn.Sequential(
46
+ conv(in_planes, c // 2, 3, 2, 1),
47
+ conv(c // 2, c, 3, 2, 1),
48
+ )
49
+ self.convblock0 = nn.Sequential(conv(c, c), conv(c, c))
50
+ self.convblock1 = nn.Sequential(conv(c, c), conv(c, c))
51
+ self.convblock2 = nn.Sequential(conv(c, c), conv(c, c))
52
+ self.convblock3 = nn.Sequential(conv(c, c), conv(c, c))
53
+ self.conv1 = nn.Sequential(
54
+ nn.ConvTranspose2d(c, c // 2, 4, 2, 1),
55
+ nn.PReLU(c // 2),
56
+ nn.ConvTranspose2d(c // 2, 4, 4, 2, 1),
57
+ )
58
+ self.conv2 = nn.Sequential(
59
+ nn.ConvTranspose2d(c, c // 2, 4, 2, 1),
60
+ nn.PReLU(c // 2),
61
+ nn.ConvTranspose2d(c // 2, 1, 4, 2, 1),
62
+ )
63
+
64
+ def forward(self, x, flow, scale=1):
65
+ x = F.interpolate(
66
+ x,
67
+ scale_factor=1.0 / scale,
68
+ mode="bilinear",
69
+ align_corners=False,
70
+ recompute_scale_factor=False,
71
+ )
72
+ flow = (
73
+ F.interpolate(
74
+ flow,
75
+ scale_factor=1.0 / scale,
76
+ mode="bilinear",
77
+ align_corners=False,
78
+ recompute_scale_factor=False,
79
+ )
80
+ * 1.0
81
+ / scale
82
+ )
83
+ feat = self.conv0(torch.cat((x, flow), 1))
84
+ feat = self.convblock0(feat) + feat
85
+ feat = self.convblock1(feat) + feat
86
+ feat = self.convblock2(feat) + feat
87
+ feat = self.convblock3(feat) + feat
88
+ flow = self.conv1(feat)
89
+ mask = self.conv2(feat)
90
+ flow = (
91
+ F.interpolate(
92
+ flow,
93
+ scale_factor=scale,
94
+ mode="bilinear",
95
+ align_corners=False,
96
+ recompute_scale_factor=False,
97
+ )
98
+ * scale
99
+ )
100
+ mask = F.interpolate(
101
+ mask,
102
+ scale_factor=scale,
103
+ mode="bilinear",
104
+ align_corners=False,
105
+ recompute_scale_factor=False,
106
+ )
107
+ return flow, mask
108
+
109
+
110
+ class IFNet(nn.Module):
111
+ def __init__(self):
112
+ super(IFNet, self).__init__()
113
+ self.block0 = IFBlock(7 + 4, c=90)
114
+ self.block1 = IFBlock(7 + 4, c=90)
115
+ self.block2 = IFBlock(7 + 4, c=90)
116
+ self.block_tea = IFBlock(10 + 4, c=90)
117
+
118
+ def forward(self, x):
119
+ scale_list = [4, 2, 1]
120
+
121
+ channel = x.shape[1] // 2
122
+ img0 = x[:, :channel]
123
+ img1 = x[:, channel:]
124
+ flow_list = []
125
+ merged = []
126
+ mask_list = []
127
+ warped_img0 = img0
128
+ warped_img1 = img1
129
+ flow = (x[:, :4]).detach() * 0
130
+ mask = (x[:, :1]).detach() * 0
131
+ loss_cons = 0
132
+ block = [self.block0, self.block1, self.block2]
133
+ for i in range(3):
134
+ f0, m0 = block[i](
135
+ torch.cat((warped_img0[:, :3], warped_img1[:, :3], mask), 1),
136
+ flow,
137
+ scale=scale_list[i],
138
+ )
139
+ f1, m1 = block[i](
140
+ torch.cat((warped_img1[:, :3], warped_img0[:, :3], -mask), 1),
141
+ torch.cat((flow[:, 2:4], flow[:, :2]), 1),
142
+ scale=scale_list[i],
143
+ )
144
+ flow = flow + (f0 + torch.cat((f1[:, 2:4], f1[:, :2]), 1)) / 2
145
+ mask = mask + (m0 + (-m1)) / 2
146
+ mask_list.append(mask)
147
+ flow_list.append(flow)
148
+ warped_img0 = warp(img0, flow[:, :2])
149
+ warped_img1 = warp(img1, flow[:, 2:4])
150
+ merged.append((warped_img0, warped_img1))
151
+
152
+ for i in range(3):
153
+ mask_list[i] = torch.sigmoid(mask_list[i])
154
+ merged[i] = merged[i][0] * mask_list[i] + merged[i][1] * (1 - mask_list[i])
155
+ return merged[2]
156
+
157
+
158
+ def warp(tenInput, tenFlow):
159
+ tenHorizontal = (
160
+ torch.linspace(-1.0, 1.0, tenFlow.shape[3], device=device, dtype=dtype)
161
+ .view(1, 1, 1, tenFlow.shape[3])
162
+ .expand(tenFlow.shape[0], -1, tenFlow.shape[2], -1)
163
+ )
164
+ tenVertical = (
165
+ torch.linspace(-1.0, 1.0, tenFlow.shape[2], device=device, dtype=dtype)
166
+ .view(1, 1, tenFlow.shape[2], 1)
167
+ .expand(tenFlow.shape[0], -1, -1, tenFlow.shape[3])
168
+ )
169
+ backwarp_tenGrid = torch.cat([tenHorizontal, tenVertical], 1).to(device, dtype)
170
+ tenFlow = torch.cat(
171
+ [
172
+ tenFlow[:, 0:1, :, :] / ((tenInput.shape[3] - 1.0) / 2.0),
173
+ tenFlow[:, 1:2, :, :] / ((tenInput.shape[2] - 1.0) / 2.0),
174
+ ],
175
+ 1,
176
+ )
177
+ g = (backwarp_tenGrid + tenFlow).permute(0, 2, 3, 1)
178
+ return torch.nn.functional.grid_sample(
179
+ input=tenInput,
180
+ grid=g,
181
+ mode="bilinear",
182
+ padding_mode="border",
183
+ align_corners=True,
184
+ )
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch>=1.12.0
2
+ safetensors>=0.4.0
3
+ numpy>=1.21.0
4
+ matplotlib>=3.5.0