-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcriteria.py
55 lines (44 loc) · 1.64 KB
/
criteria.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import torch
import torch.nn as nn
loss_names = ['l1', 'l2', 'l1_s12', 'l2_s12']
rgb_loss_names = ['l1', 'l2']
class MSELoss(nn.Module):
def __init__(self):
super(MSELoss, self).__init__()
def forward(self, pred, target, mask=None):
assert pred.dim() == target.dim(), "inconsistent dimensions"
diff = target - pred
if mask is not None:
diff = diff[mask.expand(diff.shape)]
self.loss = (diff**2).mean()
return self.loss
class L1Loss(nn.Module):
def __init__(self):
super(L1Loss, self).__init__()
def forward(self, pred, target, mask=None):
assert pred.dim() == target.dim(), "inconsistent dimensions"
diff = target - pred
if mask is not None:
diff = diff[mask.expand(diff.shape)]
self.loss = diff.abs().mean()
return self.loss
class MSES12Loss(nn.Module):
def __init__(self):
super(MSES12Loss, self).__init__()
def forward(self, pred, target, mask=None):
assert pred.dim() == target.dim(), "inconsistent dimensions"
diff = target[:,1:3,:,:] - pred[:,1:3,:,:]
if mask is not None:
diff = diff[mask.expand(diff.shape)]
self.loss = (diff**2).mean()
return self.loss
class L1S12Loss(nn.Module):
def __init__(self):
super(L1S12Loss, self).__init__()
def forward(self, pred, target, mask=None):
assert pred.dim() == target.dim(), "inconsistent dimensions"
diff = target[:,1:3,:,:] - pred[:,1:3,:,:]
if mask is not None:
diff = diff[mask.expand(diff.shape)]
self.loss = diff.abs().mean()
return self.loss