Spaces:
Running
Running
Upload 2 files
Browse files- model/loss.py +128 -0
- model/warplayer.py +22 -0
model/loss.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch.nn.functional as F
|
| 5 |
+
import torchvision.models as models
|
| 6 |
+
|
| 7 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
class EPE(nn.Module):
|
| 11 |
+
def __init__(self):
|
| 12 |
+
super(EPE, self).__init__()
|
| 13 |
+
|
| 14 |
+
def forward(self, flow, gt, loss_mask):
|
| 15 |
+
loss_map = (flow - gt.detach()) ** 2
|
| 16 |
+
loss_map = (loss_map.sum(1, True) + 1e-6) ** 0.5
|
| 17 |
+
return (loss_map * loss_mask)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class Ternary(nn.Module):
|
| 21 |
+
def __init__(self):
|
| 22 |
+
super(Ternary, self).__init__()
|
| 23 |
+
patch_size = 7
|
| 24 |
+
out_channels = patch_size * patch_size
|
| 25 |
+
self.w = np.eye(out_channels).reshape(
|
| 26 |
+
(patch_size, patch_size, 1, out_channels))
|
| 27 |
+
self.w = np.transpose(self.w, (3, 2, 0, 1))
|
| 28 |
+
self.w = torch.tensor(self.w).float().to(device)
|
| 29 |
+
|
| 30 |
+
def transform(self, img):
|
| 31 |
+
patches = F.conv2d(img, self.w, padding=3, bias=None)
|
| 32 |
+
transf = patches - img
|
| 33 |
+
transf_norm = transf / torch.sqrt(0.81 + transf**2)
|
| 34 |
+
return transf_norm
|
| 35 |
+
|
| 36 |
+
def rgb2gray(self, rgb):
|
| 37 |
+
r, g, b = rgb[:, 0:1, :, :], rgb[:, 1:2, :, :], rgb[:, 2:3, :, :]
|
| 38 |
+
gray = 0.2989 * r + 0.5870 * g + 0.1140 * b
|
| 39 |
+
return gray
|
| 40 |
+
|
| 41 |
+
def hamming(self, t1, t2):
|
| 42 |
+
dist = (t1 - t2) ** 2
|
| 43 |
+
dist_norm = torch.mean(dist / (0.1 + dist), 1, True)
|
| 44 |
+
return dist_norm
|
| 45 |
+
|
| 46 |
+
def valid_mask(self, t, padding):
|
| 47 |
+
n, _, h, w = t.size()
|
| 48 |
+
inner = torch.ones(n, 1, h - 2 * padding, w - 2 * padding).type_as(t)
|
| 49 |
+
mask = F.pad(inner, [padding] * 4)
|
| 50 |
+
return mask
|
| 51 |
+
|
| 52 |
+
def forward(self, img0, img1):
|
| 53 |
+
img0 = self.transform(self.rgb2gray(img0))
|
| 54 |
+
img1 = self.transform(self.rgb2gray(img1))
|
| 55 |
+
return self.hamming(img0, img1) * self.valid_mask(img0, 1)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
class SOBEL(nn.Module):
|
| 59 |
+
def __init__(self):
|
| 60 |
+
super(SOBEL, self).__init__()
|
| 61 |
+
self.kernelX = torch.tensor([
|
| 62 |
+
[1, 0, -1],
|
| 63 |
+
[2, 0, -2],
|
| 64 |
+
[1, 0, -1],
|
| 65 |
+
]).float()
|
| 66 |
+
self.kernelY = self.kernelX.clone().T
|
| 67 |
+
self.kernelX = self.kernelX.unsqueeze(0).unsqueeze(0).to(device)
|
| 68 |
+
self.kernelY = self.kernelY.unsqueeze(0).unsqueeze(0).to(device)
|
| 69 |
+
|
| 70 |
+
def forward(self, pred, gt):
|
| 71 |
+
N, C, H, W = pred.shape[0], pred.shape[1], pred.shape[2], pred.shape[3]
|
| 72 |
+
img_stack = torch.cat(
|
| 73 |
+
[pred.reshape(N*C, 1, H, W), gt.reshape(N*C, 1, H, W)], 0)
|
| 74 |
+
sobel_stack_x = F.conv2d(img_stack, self.kernelX, padding=1)
|
| 75 |
+
sobel_stack_y = F.conv2d(img_stack, self.kernelY, padding=1)
|
| 76 |
+
pred_X, gt_X = sobel_stack_x[:N*C], sobel_stack_x[N*C:]
|
| 77 |
+
pred_Y, gt_Y = sobel_stack_y[:N*C], sobel_stack_y[N*C:]
|
| 78 |
+
|
| 79 |
+
L1X, L1Y = torch.abs(pred_X-gt_X), torch.abs(pred_Y-gt_Y)
|
| 80 |
+
loss = (L1X+L1Y)
|
| 81 |
+
return loss
|
| 82 |
+
|
| 83 |
+
class MeanShift(nn.Conv2d):
|
| 84 |
+
def __init__(self, data_mean, data_std, data_range=1, norm=True):
|
| 85 |
+
c = len(data_mean)
|
| 86 |
+
super(MeanShift, self).__init__(c, c, kernel_size=1)
|
| 87 |
+
std = torch.Tensor(data_std)
|
| 88 |
+
self.weight.data = torch.eye(c).view(c, c, 1, 1)
|
| 89 |
+
if norm:
|
| 90 |
+
self.weight.data.div_(std.view(c, 1, 1, 1))
|
| 91 |
+
self.bias.data = -1 * data_range * torch.Tensor(data_mean)
|
| 92 |
+
self.bias.data.div_(std)
|
| 93 |
+
else:
|
| 94 |
+
self.weight.data.mul_(std.view(c, 1, 1, 1))
|
| 95 |
+
self.bias.data = data_range * torch.Tensor(data_mean)
|
| 96 |
+
self.requires_grad = False
|
| 97 |
+
|
| 98 |
+
class VGGPerceptualLoss(torch.nn.Module):
|
| 99 |
+
def __init__(self, rank=0):
|
| 100 |
+
super(VGGPerceptualLoss, self).__init__()
|
| 101 |
+
blocks = []
|
| 102 |
+
pretrained = True
|
| 103 |
+
self.vgg_pretrained_features = models.vgg19(pretrained=pretrained).features
|
| 104 |
+
self.normalize = MeanShift([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], norm=True).cuda()
|
| 105 |
+
for param in self.parameters():
|
| 106 |
+
param.requires_grad = False
|
| 107 |
+
|
| 108 |
+
def forward(self, X, Y, indices=None):
|
| 109 |
+
X = self.normalize(X)
|
| 110 |
+
Y = self.normalize(Y)
|
| 111 |
+
indices = [2, 7, 12, 21, 30]
|
| 112 |
+
weights = [1.0/2.6, 1.0/4.8, 1.0/3.7, 1.0/5.6, 10/1.5]
|
| 113 |
+
k = 0
|
| 114 |
+
loss = 0
|
| 115 |
+
for i in range(indices[-1]):
|
| 116 |
+
X = self.vgg_pretrained_features[i](X)
|
| 117 |
+
Y = self.vgg_pretrained_features[i](Y)
|
| 118 |
+
if (i+1) in indices:
|
| 119 |
+
loss += weights[k] * (X - Y.detach()).abs().mean() * 0.1
|
| 120 |
+
k += 1
|
| 121 |
+
return loss
|
| 122 |
+
|
| 123 |
+
if __name__ == '__main__':
|
| 124 |
+
img0 = torch.zeros(3, 3, 256, 256).float().to(device)
|
| 125 |
+
img1 = torch.tensor(np.random.normal(
|
| 126 |
+
0, 1, (3, 3, 256, 256))).float().to(device)
|
| 127 |
+
ternary_loss = Ternary()
|
| 128 |
+
print(ternary_loss(img0, img1).shape)
|
model/warplayer.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
|
| 4 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 5 |
+
backwarp_tenGrid = {}
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def warp(tenInput, tenFlow):
|
| 9 |
+
k = (str(tenFlow.device), str(tenFlow.size()))
|
| 10 |
+
if k not in backwarp_tenGrid:
|
| 11 |
+
tenHorizontal = torch.linspace(-1.0, 1.0, tenFlow.shape[3], device=device).view(
|
| 12 |
+
1, 1, 1, tenFlow.shape[3]).expand(tenFlow.shape[0], -1, tenFlow.shape[2], -1)
|
| 13 |
+
tenVertical = torch.linspace(-1.0, 1.0, tenFlow.shape[2], device=device).view(
|
| 14 |
+
1, 1, tenFlow.shape[2], 1).expand(tenFlow.shape[0], -1, -1, tenFlow.shape[3])
|
| 15 |
+
backwarp_tenGrid[k] = torch.cat(
|
| 16 |
+
[tenHorizontal, tenVertical], 1).to(device)
|
| 17 |
+
|
| 18 |
+
tenFlow = torch.cat([tenFlow[:, 0:1, :, :] / ((tenInput.shape[3] - 1.0) / 2.0),
|
| 19 |
+
tenFlow[:, 1:2, :, :] / ((tenInput.shape[2] - 1.0) / 2.0)], 1)
|
| 20 |
+
|
| 21 |
+
g = (backwarp_tenGrid[k] + tenFlow).permute(0, 2, 3, 1)
|
| 22 |
+
return torch.nn.functional.grid_sample(input=tenInput, grid=g, mode='bilinear', padding_mode='border', align_corners=True)
|