-
Notifications
You must be signed in to change notification settings - Fork 5
/
metric.py
145 lines (115 loc) · 4.24 KB
/
metric.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 10 16:35:14 2020
@author: lenovo
"""
import torch.nn.functional as F
import torch.nn as nn
import torch
# Hinge Loss (BigGAN)
class loss_hinge_dis(nn.Module):
def __init__(self):
super().__init__()
def forward(self,dis_fake, dis_real):
loss_real, loss_fake = 0, 0
if isinstance(dis_fake, list):
for i in range(len(dis_fake)):
loss_real += torch.mean(F.relu(1. - dis_real[i][0]))
loss_fake += torch.mean(F.relu(1. + dis_fake[i][0]))
else:
loss_real = torch.mean(F.relu(1. - dis_real))
loss_fake = torch.mean(F.relu(1. + dis_fake))
loss_real = loss_real/len(dis_fake)
loss_fake = loss_fake/len(dis_fake)
return loss_real, loss_fake
# def loss_hinge_dis(dis_fake, dis_real): # This version returns a single loss
# loss = torch.mean(F.relu(1. - dis_real))
# loss += torch.mean(F.relu(1. + dis_fake))
# return loss
class loss_hinge_gen(nn.Module):
def __init__(self):
super().__init__()
def forward(self,dis_fake):
loss = 0
if isinstance(dis_fake, list):
for i in range(len(dis_fake)):
loss += -torch.mean(dis_fake[i][0])
else:
loss = -torch.mean(dis_fake)
loss = loss/len(dis_fake)
return loss
def loss_hinge_dis_mse(dis_fake, dis_real):
loss_real, loss_fake = 0, 0
target_real_label, target_fake_label=torch.Tensor([1.0]),torch.Tensor([0.0])
for i in range(len(dis_real)):
loss_real += nn.MSELoss()(dis_real[i][0], target_real_label.expand_as(dis_real[i][0]).type_as(dis_real[i][0]))
for i in range(len(dis_fake)):
loss_fake += nn.MSELoss()(dis_fake[i][0], target_fake_label.expand_as(dis_fake[i][0]).type_as(dis_fake[i][0]))
loss_real = loss_real/len(dis_real)
loss_fake = loss_fake/len(dis_fake)
return loss_real, loss_fake
def loss_hinge_gen_mse(dis_fake):
loss = 0
target_real_label = torch.Tensor([1.0])
if isinstance(dis_fake, list):
for i in range(len(dis_fake)):
loss += nn.MSELoss()(dis_fake[i][0], target_real_label.expand_as(dis_fake[i][0]).type_as(dis_fake[i][0]))
else:
loss = -torch.mean(dis_fake)
loss = loss/len(dis_fake)
return loss
def loss_bce(input,label):
loss = 0
b_size = input[0][0].shape[0]
for i in range(len(input)):
label_ = torch.full((b_size,1,input[i][0].shape[2], input[i][0].shape[3]),label, device='cuda')
loss += nn.BCELoss()(input[i][0], label_)
loss = loss/len(input)
return loss
class IdLoss(nn.Module):
"""
torch.nn.CosineEmbeddingLoss(margin=0.0, size_average=None, reduce=None, reduction='mean')
"""
def __init__(self):
super().__init__()
def forward(self, warped_source, source):
target = torch.ones(source.shape[0])
target = target.type_as(source)
loss = nn.CosineEmbeddingLoss()(warped_source, source,target)
return loss
class AttrLoss(nn.Module):
def __init__(self):
super().__init__()
def forward(self, attr_warped_target, attr_target):
loss = 0
if isinstance(attr_target, list):
for i in range(len(attr_target)):
loss += nn.MSELoss()(attr_warped_target[i], attr_target[i])
# print(loss)
else:
loss = nn.MSELoss()(attr_warped_target, attr_target)
loss = loss /2
return loss
class RecLoss(nn.Module):
"""
target==source
"""
def __init__(self):
super().__init__()
def forward(self, warped_source, target, label):
loss = 0
for i in range(len(label)):
if label[i]==1:
loss += nn.MSELoss()(warped_source[i], target[i])
loss = loss / 2
return loss
class ChgLoss(nn.Module):
"""
target==source
"""
def __init__(self):
super().__init__()
def forward(self, yst0, yst):
loss = 0
loss = nn.L1Loss()(yst0, yst)
return loss