PyTorch
Safetensors
TensorRT
custom
video-interpolation
frame-interpolation
optical-flow
torch-compile
Instructions to use TensorForger/RIFE-safetensors with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- TensorRT
How to use TensorForger/RIFE-safetensors with TensorRT:
# No code snippets available yet for this library. # To use this model, check the repository files and the library's documentation. # Want to help? PRs adding snippets are welcome at: # https://github.com/huggingface/huggingface.js
- Notebooks
- Google Colab
- Kaggle
| # Forked from https://github.com/hzwer/ECCV2022-RIFE/blob/main/model/IFNet.py | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
| dtype = torch.float16 | |
| def conv(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1): | |
| return nn.Sequential( | |
| nn.Conv2d( | |
| in_planes, | |
| out_planes, | |
| kernel_size=kernel_size, | |
| stride=stride, | |
| padding=padding, | |
| dilation=dilation, | |
| bias=True, | |
| ), | |
| nn.PReLU(out_planes), | |
| ) | |
| def conv_bn(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1): | |
| return nn.Sequential( | |
| nn.Conv2d( | |
| in_planes, | |
| out_planes, | |
| kernel_size=kernel_size, | |
| stride=stride, | |
| padding=padding, | |
| dilation=dilation, | |
| bias=False, | |
| ), | |
| nn.BatchNorm2d(out_planes), | |
| nn.PReLU(out_planes), | |
| ) | |
| class IFBlock(nn.Module): | |
| def __init__(self, in_planes, c=64): | |
| super(IFBlock, self).__init__() | |
| self.conv0 = nn.Sequential( | |
| conv(in_planes, c // 2, 3, 2, 1), | |
| conv(c // 2, c, 3, 2, 1), | |
| ) | |
| self.convblock0 = nn.Sequential(conv(c, c), conv(c, c)) | |
| self.convblock1 = nn.Sequential(conv(c, c), conv(c, c)) | |
| self.convblock2 = nn.Sequential(conv(c, c), conv(c, c)) | |
| self.convblock3 = nn.Sequential(conv(c, c), conv(c, c)) | |
| self.conv1 = nn.Sequential( | |
| nn.ConvTranspose2d(c, c // 2, 4, 2, 1), | |
| nn.PReLU(c // 2), | |
| nn.ConvTranspose2d(c // 2, 4, 4, 2, 1), | |
| ) | |
| self.conv2 = nn.Sequential( | |
| nn.ConvTranspose2d(c, c // 2, 4, 2, 1), | |
| nn.PReLU(c // 2), | |
| nn.ConvTranspose2d(c // 2, 1, 4, 2, 1), | |
| ) | |
| def forward(self, x, flow, scale=1): | |
| x = F.interpolate( | |
| x, | |
| scale_factor=1.0 / scale, | |
| mode="bilinear", | |
| align_corners=False, | |
| recompute_scale_factor=False, | |
| ) | |
| flow = ( | |
| F.interpolate( | |
| flow, | |
| scale_factor=1.0 / scale, | |
| mode="bilinear", | |
| align_corners=False, | |
| recompute_scale_factor=False, | |
| ) | |
| * 1.0 | |
| / scale | |
| ) | |
| feat = self.conv0(torch.cat((x, flow), 1)) | |
| feat = self.convblock0(feat) + feat | |
| feat = self.convblock1(feat) + feat | |
| feat = self.convblock2(feat) + feat | |
| feat = self.convblock3(feat) + feat | |
| flow = self.conv1(feat) | |
| mask = self.conv2(feat) | |
| flow = ( | |
| F.interpolate( | |
| flow, | |
| scale_factor=scale, | |
| mode="bilinear", | |
| align_corners=False, | |
| recompute_scale_factor=False, | |
| ) | |
| * scale | |
| ) | |
| mask = F.interpolate( | |
| mask, | |
| scale_factor=scale, | |
| mode="bilinear", | |
| align_corners=False, | |
| recompute_scale_factor=False, | |
| ) | |
| return flow, mask | |
| class IFNet(nn.Module): | |
| def __init__(self): | |
| super(IFNet, self).__init__() | |
| self.block0 = IFBlock(7 + 4, c=90) | |
| self.block1 = IFBlock(7 + 4, c=90) | |
| self.block2 = IFBlock(7 + 4, c=90) | |
| self.block_tea = IFBlock(10 + 4, c=90) | |
| def forward(self, x): | |
| scale_list = [4, 2, 1] | |
| channel = x.shape[1] // 2 | |
| img0 = x[:, :channel] | |
| img1 = x[:, channel:] | |
| flow_list = [] | |
| merged = [] | |
| mask_list = [] | |
| warped_img0 = img0 | |
| warped_img1 = img1 | |
| flow = (x[:, :4]).detach() * 0 | |
| mask = (x[:, :1]).detach() * 0 | |
| loss_cons = 0 | |
| block = [self.block0, self.block1, self.block2] | |
| for i in range(3): | |
| f0, m0 = block[i]( | |
| torch.cat((warped_img0[:, :3], warped_img1[:, :3], mask), 1), | |
| flow, | |
| scale=scale_list[i], | |
| ) | |
| 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], | |
| ) | |
| flow = flow + (f0 + torch.cat((f1[:, 2:4], f1[:, :2]), 1)) / 2 | |
| mask = mask + (m0 + (-m1)) / 2 | |
| mask_list.append(mask) | |
| flow_list.append(flow) | |
| warped_img0 = warp(img0, flow[:, :2]) | |
| warped_img1 = warp(img1, flow[:, 2:4]) | |
| merged.append((warped_img0, warped_img1)) | |
| for i in range(3): | |
| mask_list[i] = torch.sigmoid(mask_list[i]) | |
| merged[i] = merged[i][0] * mask_list[i] + merged[i][1] * (1 - mask_list[i]) | |
| return merged[2] | |
| def warp(tenInput, tenFlow): | |
| tenHorizontal = ( | |
| torch.linspace(-1.0, 1.0, tenFlow.shape[3], device=device, dtype=dtype) | |
| .view(1, 1, 1, tenFlow.shape[3]) | |
| .expand(tenFlow.shape[0], -1, tenFlow.shape[2], -1) | |
| ) | |
| tenVertical = ( | |
| torch.linspace(-1.0, 1.0, tenFlow.shape[2], device=device, dtype=dtype) | |
| .view(1, 1, tenFlow.shape[2], 1) | |
| .expand(tenFlow.shape[0], -1, -1, tenFlow.shape[3]) | |
| ) | |
| backwarp_tenGrid = torch.cat([tenHorizontal, tenVertical], 1).to(device, dtype) | |
| tenFlow = torch.cat( | |
| [ | |
| tenFlow[:, 0:1, :, :] / ((tenInput.shape[3] - 1.0) / 2.0), | |
| tenFlow[:, 1:2, :, :] / ((tenInput.shape[2] - 1.0) / 2.0), | |
| ], | |
| 1, | |
| ) | |
| g = (backwarp_tenGrid + tenFlow).permute(0, 2, 3, 1) | |
| return torch.nn.functional.grid_sample( | |
| input=tenInput, | |
| grid=g, | |
| mode="bilinear", | |
| padding_mode="border", | |
| align_corners=True, | |
| ) | |