Repository navigation
Expand file tree
/
Copy pathloss_custom.py
More file actions
146 lines (107 loc) · 5.63 KB
/
Copy pathloss_custom.py
File metadata and controls
146 lines (107 loc) · 5.63 KB
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
146
import torch
import torch.nn.functional as F
import os
from torch import Tensor
os.environ['CUDA_LAUNCH_BLOCKING'] = '1'
def target_process(target):
# target = Tensor.long(target)
target = Tensor.float(target)
for shift in [-2, -1, 1, 2]:
target = target + torch.roll(target/(2**abs(shift)), shifts=shift, dims=-1)
# target = target | torch.roll(target, shifts=shift, dims=-1)
target[target>1] = 1.
target = Tensor.float(target)
return target
class Weight_BCELoss(torch.nn.Module): # 暂时还可以用的一个loss
""" linear add """
def __init__(self, coef=1.0, target_aug = True):
super(Weight_BCELoss, self).__init__()
self.coef = coef
self.target_aug = target_aug
def forward(self, input, target, target_widen=True):
# if self.target_aug:
# target = target_process(target)
# split out the channels
beat_act_target = target[:,0,:] # [B, C, T]
downbeat_act_target = target[:,1,:] # [B, C, T]
nobeat_act_target = target[:, 2, :] # [B, C, T]
beat_act_input = input[:,0,:] # [B, C, T]
downbeat_act_input = input[:,1,:] # [B, C, T]
nobeat_act_input = input[:,2,:] # [B, C, T]
# beat errors
target_beats = beat_act_target[beat_act_target == 1]
input_beats = beat_act_input[beat_act_target == 1]
b_f = target_beats.shape[-1]/(target.shape[-1]*target.shape[0])
# beat_loss = F.binary_cross_entropy(input_beats, target_beats)
beat_loss = F.binary_cross_entropy(beat_act_input, beat_act_target)
# no beat errors
target_no_beats = beat_act_target[beat_act_target == 0]
input_no_beats = nobeat_act_input[beat_act_target == 0]
nb_f = target_no_beats.shape[-1]/(target.shape[-1]*target.shape[0])
# no_beat_loss = F.binary_cross_entropy(input_no_beats, target_no_beats)
no_beat_loss = F.binary_cross_entropy(nobeat_act_input, nobeat_act_target)
# downbeat errors
target_downbeats = downbeat_act_target[downbeat_act_target == 1]
input_downbeats = downbeat_act_input[downbeat_act_target == 1]
d_f = target_downbeats.shape[-1]/(target.shape[-1]*target.shape[0])
# downbeat_loss = F.binary_cross_entropy(input_downbeats, target_downbeats)
downbeat_loss = F.binary_cross_entropy(downbeat_act_input, downbeat_act_target)
# sum up losses
# total_loss = beat_loss/b_f + no_beat_loss/nb_f + downbeat_loss/d_f
total_loss = beat_loss/b_f + downbeat_loss/d_f
return total_loss, beat_loss, downbeat_loss
class SoftBCELoss(torch.nn.Module): # default loss
""" linear add """
def __init__(self, target_aug = True):
super(SoftBCELoss, self).__init__()
self.target_aug = target_aug
def forward(self, input, target, target_widen=True):
# split out the channels
beat_act_target = target[:,0,:]
downbeat_act_target = target[:,1,:]
beat_act_input = input[:,0,:]
downbeat_act_input = input[:,1,:]
if self.target_aug:
beat_act_target = target_process(beat_act_target)
downbeat_act_target = target_process(downbeat_act_target)
# beat errors
target_beats = beat_act_target[beat_act_target > 0]
input_beats = beat_act_input[beat_act_target > 0]
beat_loss = F.binary_cross_entropy(input_beats, target_beats, reduce=False)
# no beat errors
target_no_beats = beat_act_target[beat_act_target == 0]
input_no_beats = beat_act_input[beat_act_target == 0]
no_beat_loss = F.binary_cross_entropy(input_no_beats, target_no_beats, reduce=False)
# downbeat errors
target_downbeats = downbeat_act_target[downbeat_act_target > 0]
input_downbeats = downbeat_act_input[downbeat_act_target > 0]
downbeat_loss = F.binary_cross_entropy(input_downbeats, target_downbeats, reduce=False)
# no downbeat errors
target_no_downbeats = downbeat_act_target[downbeat_act_target == 0]
input_no_downbeats = downbeat_act_input[downbeat_act_target == 0]
no_downbeat_loss = F.binary_cross_entropy(input_no_downbeats, target_no_downbeats, reduce=False)
num = beat_act_target.numel()
alpha, gamma = 0.5, 0.5
total_beat_loss = alpha * beat_loss.sum() / num + (1 - alpha) * no_beat_loss.sum() / num
total_downbeat_loss = gamma * downbeat_loss.sum() / num + (1 - gamma) * no_downbeat_loss.sum() / num
total_loss = total_beat_loss + total_downbeat_loss
return total_loss, total_beat_loss, total_downbeat_loss
class SoftBCELoss_new(torch.nn.Module):
""" linear add """
def __init__(self):
super(SoftBCELoss_new, self).__init__()
def forward(self, input, target, ifdownbeat, target_widen=True):
downbeat_valid = torch.Tensor([k != 0 for k in ifdownbeat]).to(target.device).bool()
downbeat_valid = downbeat_valid.unsqueeze(1).repeat(1, target.shape[-1])
if target_widen:
target = target_process(target)
# split out the channels
beat_act_target = target[:,0,:]
downbeat_act_target = target[:,1,:]
beat_act_input = input[:,0,:]
downbeat_act_input = input[:,1,:]
beat_loss = F.binary_cross_entropy(beat_act_input, beat_act_target)
downbeat_loss = F.binary_cross_entropy(downbeat_act_input, downbeat_act_target, reduce=False)
downbeat_loss = downbeat_loss.masked_select(downbeat_valid).mean()
total_loss = beat_loss + downbeat_loss
return total_loss, beat_loss, downbeat_loss