forked from nbdszw/CSTnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
149 lines (133 loc) · 6.2 KB
/
models.py
File metadata and controls
149 lines (133 loc) · 6.2 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
147
148
149
import torch
import torch.nn as nn
from transfer_losses import TransferLoss
import backbones
class NewTransferNet(nn.Module):
def __init__(self, num_class, base_net='rsp_resnet50', transfer_loss='mmd', use_bottleneck=True, bottleneck_width=256, max_iter=1000, input_channels=3,**kwargs):
super(NewTransferNet, self).__init__()
self.num_class = num_class
self.base_network = backbones.get_backbone(base_net, input_channels=input_channels)
self.use_bottleneck = use_bottleneck
self.transfer_loss = transfer_loss
if self.use_bottleneck:
bottleneck_list = [
nn.Linear(self.base_network.output_num(), bottleneck_width),
nn.ReLU()
]
self.bottleneck_layer = nn.Sequential(*bottleneck_list)
feature_dim = bottleneck_width
else:
feature_dim = self.base_network.output_num()
self.classifier_layer = nn.Linear(feature_dim, num_class)
transfer_loss_args = {
"loss_type": self.transfer_loss,
"max_iter": max_iter,
"num_class": num_class
}
self.adapt_loss = TransferLoss(**transfer_loss_args)
self.criterion = torch.nn.CrossEntropyLoss()
def forward(self, source, target, source_label):
source_outputs, \
source_x_IN_1, source_x_1, source_x_style_1a, \
source_x_IN_2, source_x_2, source_x_style_2a, \
source_x_IN_3, source_x_3, source_x_style_3a = self.base_network(source)
target_outputs, \
target_x_IN_1, target_x_1, target_x_style_1a, \
target_x_IN_2, target_x_2, target_x_style_2a, \
target_x_IN_3, target_x_3, target_x_style_3a = self.base_network(target)
if self.use_bottleneck:
source = self.bottleneck_layer(source_outputs)
target = self.bottleneck_layer(target_outputs)
# classification loss
source_clf = self.classifier_layer(source)
clf_loss = self.criterion(source_clf, source_label)
# dis_loss
dis_criterion = torch.nn.SmoothL1Loss()
dis_content = dis_criterion(source_x_IN_1, source_x_1) \
+ dis_criterion(source_x_IN_2, source_x_2) \
+ dis_criterion(source_x_IN_3, source_x_3) \
+ dis_criterion(target_x_IN_1, target_x_1) \
+ dis_criterion(target_x_IN_2, target_x_2) \
+ dis_criterion(target_x_IN_3, target_x_3)
dis_style = dis_criterion(source_x_style_1a, source_x_1) \
+ dis_criterion(source_x_style_2a, source_x_2) \
+ dis_criterion(source_x_style_3a, source_x_3) \
+ dis_criterion(target_x_style_1a, target_x_1) \
+ dis_criterion(target_x_style_2a, target_x_2) \
+ dis_criterion(target_x_style_3a, target_x_3)
"""
dis_content = dis_criterion(target_x_IN_1, target_x_1) \
+ dis_criterion(target_x_IN_2, target_x_2) \
+ dis_criterion(target_x_IN_3, target_x_3)
dis_style = dis_criterion(target_x_style_1a, target_x_1) \
+ dis_criterion(target_x_style_2a, target_x_2) \
+ dis_criterion(target_x_style_3a, target_x_3)
"""
dis_loss = dis_content - dis_style
# dis_loss = dis_content
# dis_loss = - dis_style
"""
# class-level alignment loss
target_clf = self.classifier_layer(target)
source_clf = self.classifier_layer(source)
source_probs = torch.softmax(source_clf, dim=1)
class_loss = torch.sum(source_probs * torch.nn.functional.cross_entropy(target_clf, source_label))
class_loss = class_loss / source_probs.size(0)
"""
# transfer loss
kwargs = {}
if self.transfer_loss == "lmmd":
kwargs['source_label'] = source_label
target_clf = self.classifier_layer(target)
kwargs['target_logits'] = torch.nn.functional.softmax(target_clf, dim=1)
elif self.transfer_loss == "daan":
source_clf = self.classifier_layer(source)
kwargs['source_logits'] = torch.nn.functional.softmax(source_clf, dim=1)
target_clf = self.classifier_layer(target)
kwargs['target_logits'] = torch.nn.functional.softmax(target_clf, dim=1)
elif self.transfer_loss == 'bnm':
tar_clf = self.classifier_layer(target)
target = nn.Softmax(dim=1)(tar_clf)
transfer_loss = self.adapt_loss(source, target, **kwargs)
return clf_loss, dis_loss, transfer_loss
def get_parameters(self, initial_lr=1.0):
params = [
{'params': self.base_network.parameters(), 'lr': 0.1 * initial_lr},
{'params': self.classifier_layer.parameters(), 'lr': 1.0 * initial_lr},
]
if self.use_bottleneck:
params.append(
{'params': self.bottleneck_layer.parameters(), 'lr': 1.0 * initial_lr}
)
# Loss-dependent
if self.transfer_loss == "adv":
params.append(
{'params': self.adapt_loss.loss_func.domain_classifier.parameters(), 'lr': 1.0 * initial_lr}
)
elif self.transfer_loss == "daan":
params.append(
{'params': self.adapt_loss.loss_func.domain_classifier.parameters(), 'lr': 1.0 * initial_lr}
)
params.append(
{'params': self.adapt_loss.loss_func.local_classifiers.parameters(), 'lr': 1.0 * initial_lr}
)
return params
def predict(self, x):
x_4, \
x_IN_1, x_1, x_style_1a, \
x_IN_2, x_2, x_style_2a, \
x_IN_3, x_3, x_style_3a = self.base_network(x)
x = self.bottleneck_layer(x_4)
clf = self.classifier_layer(x)
return clf
def get_features(self,x):
x_4, \
x_IN_1, x_1, x_style_1a, \
x_IN_2, x_2, x_style_2a, \
x_IN_3, x_3, x_style_3a = self.base_network(x)
return self.bottleneck_layer(x_4)
def epoch_based_processing(self, *args, **kwargs):
if self.transfer_loss == "daan":
self.adapt_loss.loss_func.update_dynamic_factor(*args, **kwargs)
else:
pass