-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
319 lines (260 loc) · 12.1 KB
/
utils.py
File metadata and controls
319 lines (260 loc) · 12.1 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import torch
import torch.nn.functional as F
from torch import nn as nn
import random
import os
import sys
import logging
import numpy as np
import pandas as pd
from shutil import copy
from datetime import datetime
from skorch import NeuralNetClassifier # for DIV Risk
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, accuracy_score, f1_score, mean_absolute_percentage_error
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def fix_randomness(SEED):
random.seed(SEED)
np.random.seed(SEED)
torch.manual_seed(SEED)
torch.cuda.manual_seed(SEED)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def _logger(logger_name, level=logging.DEBUG):
"""
Method to return a custom logger with the given name and level
"""
logger = logging.getLogger(logger_name)
logger.setLevel(level)
format_string = "%(message)s"
log_format = logging.Formatter(format_string)
# Creating and adding the console handler
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(log_format)
logger.addHandler(console_handler)
# Creating and adding the file handler
file_handler = logging.FileHandler(logger_name, mode='a')
file_handler.setFormatter(log_format)
logger.addHandler(file_handler)
return logger
def starting_logs(data_type, da_method, exp_log_dir, scenarios, run_id):
batches = scenarios
log_dir = os.path.join(exp_log_dir, "_".join(batches) + "_run_" + str(run_id))
os.makedirs(log_dir, exist_ok=True)
log_file_name = os.path.join(log_dir, f"logs_{datetime.now().strftime('%d_%m_%Y_%H_%M_%S')}.log")
logger = _logger(log_file_name)
logger.debug("=" * 45)
logger.debug(f'Dataset: {data_type}')
logger.debug(f'Method: {da_method}')
logger.debug("=" * 45)
logger.debug("_".join(batches))
logger.debug(f'Run ID: {run_id}')
logger.debug("=" * 45)
return logger, log_dir
def save_checkpoint(home_path, algorithm, selected_scenarios, dataset_configs, log_dir, hparams):
save_dict = {
"x-domains": selected_scenarios,
"configs": dataset_configs.__dict__,
"hparams": dict(hparams),
"model_dict": algorithm.state_dict(),
# "network_dict": algorithm.joint_network.state_dict(),
# "discriminator": algorithm.domain_classifier.state_dict()
}
# save classification report
save_path = os.path.join(home_path, log_dir, "checkpoint.pt")
torch.save(save_dict, save_path)
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
elif classname.find('Linear') != -1:
m.weight.data.normal_(0.0, 0.1)
m.bias.data.fill_(0)
def regression_report(y_true, y_pred):
mse = mean_squared_error(y_true, y_pred)
rmse = np.sqrt(mse)
mape = mean_absolute_percentage_error(y_true, y_pred)
r2 = r2_score(y_true, y_pred) # 拟合优度
report_dict = {
'RMSE': rmse,
'MAPE': mape,
'R2': r2
}
return report_dict
def _calc_metrics(pred_labels, true_labels, log_dir, home_path):
task_RMSEs = []
task_MAPEs = []
task_R2s = []
overall_true_labels = []
overall_pred_labels = []
# 分别计算每个任务的RMSE MAPE r2
for task_idx in range(len(pred_labels)):
task_pred = np.array(pred_labels[task_idx])
task_true = np.array(true_labels[task_idx])
task_rmse = np.sqrt(mean_squared_error(task_true, task_pred))
task_mape = mean_absolute_percentage_error(task_true, task_pred)
task_r2 = r2_score(task_true, task_pred)
task_RMSEs.append(task_rmse)
task_MAPEs.append(task_mape)
task_R2s.append(task_r2)
# 汇总所有任务的标签,用于计算总体指标
overall_true_labels.extend(task_true)
overall_pred_labels.extend(task_pred)
# 总体预测和标签
overall_true_labels = np.array(overall_true_labels)
overall_pred_labels = np.array(overall_pred_labels)
# 生成回归报告
regression_report_dict = regression_report(overall_true_labels, overall_pred_labels)
print('regression_report_dict:', regression_report_dict)
# 将报告转换为DataFrame
regression_df = pd.DataFrame(list(regression_report_dict.items()), columns=['Metric', 'Value'])
# 定义Excel文件路径
file_name = "regression_report.xlsx"
report_save_path = os.path.join(home_path, log_dir, file_name)
# 确保目录存在
os.makedirs(os.path.dirname(report_save_path), exist_ok=True)
# 写入Excel文件
regression_df.to_excel(report_save_path, index=False)
return (regression_report_dict['RMSE'], regression_report_dict['MAPE'], regression_report_dict['R2'],
task_RMSEs, task_MAPEs, task_R2s)
def copy_Files(destination):
destination_dir = os.path.join(destination, "MODEL_BACKUP_FILES")
os.makedirs(destination_dir, exist_ok=True)
copy("uda_benchmark.py", os.path.join(destination_dir, "main.py"))
copy("utils.py", os.path.join(destination_dir, "utils.py"))
copy(f"trainer.py", os.path.join(destination_dir, f"trainer.py"))
copy(f"same_domain_trainer.py", os.path.join(destination_dir, f"same_domain_trainer.py"))
copy("dataloader/dataloader.py", os.path.join(destination_dir, "dataloader.py"))
copy(f"models/models.py", os.path.join(destination_dir, f"models.py"))
copy(f"models/loss.py", os.path.join(destination_dir, f"loss.py"))
copy("algorithms/algorithms.py", os.path.join(destination_dir, "algorithms.py"))
copy(f"configs/data_model_configs.py", os.path.join(destination_dir, f"data_model_configs.py"))
copy(f"configs/hparams.py", os.path.join(destination_dir, f"hparams.py"))
copy(f"configs/sweep_params.py", os.path.join(destination_dir, f"sweep_params.py"))
def get_iwcv_value(weight, error):
N, d = weight.shape
_N, _d = error.shape
assert N == _N and d == _d, 'dimension mismatch!'
weighted_error = weight * error
return np.mean(weighted_error)
def get_dev_value(weight, error):
"""
:param weight: shape [N, 1], the importance weight for N source samples in the validation set
:param error: shape [N, 1], the error value for each source sample in the validation set
(typically 0 for correct classification and 1 for wrong classification)
"""
N, d = weight.shape
_N, _d = error.shape
assert N == _N and d == _d, 'dimension mismatch!'
weighted_error = weight * error
cov = np.cov(np.concatenate((weighted_error, weight), axis=1), rowvar=False)[0][1]
var_w = np.var(weight, ddof=1)
eta = - cov / var_w
return np.mean(weighted_error) + eta * np.mean(weight) - eta
class simple_MLP(nn.Module):
def __init__(self, inp_units, out_units=2):
super(simple_MLP, self).__init__()
self.dense0 = nn.Linear(inp_units, inp_units // 2)
self.nonlin = nn.ReLU()
self.output = nn.Linear(inp_units // 2, out_units)
self.softmax = nn.Softmax(dim=-1)
def forward(self, x, **kwargs):
x = self.nonlin(self.dense0(x))
x = self.softmax(self.output(x))
return x
def get_weight_gpu(source_feature, target_feature, validation_feature, configs, device):
"""
:param source_feature: shape [N_tr, d], features from training set
:param target_feature: shape [N_te, d], features from test set
:param validation_feature: shape [N_v, d], features from validation set
:return:
"""
import copy
N_s, d = source_feature.shape
N_t, _d = target_feature.shape
source_feature = copy.deepcopy(source_feature.detach().cpu()) # source_feature.clone()
target_feature = copy.deepcopy(target_feature.detach().cpu()) # target_feature.clone()
source_feature = source_feature.to(device)
target_feature = target_feature.to(device)
all_feature = torch.cat((source_feature, target_feature), dim=0)
all_label = torch.from_numpy(np.asarray([1] * N_s + [0] * N_t, dtype=np.int32)).long()
feature_for_train, feature_for_test, label_for_train, label_for_test = train_test_split(all_feature, all_label,
train_size=0.8)
learning_rates = [1e-1, 5e-2, 1e-2]
val_acc = []
domain_classifiers = []
for lr in learning_rates:
domain_classifier = NeuralNetClassifier(
simple_MLP,
module__inp_units=configs.final_out_channels * configs.features_len,
max_epochs=30,
lr=lr,
device=device,
# Shuffle training data on each epoch
iterator_train__shuffle=True,
callbacks="disable"
)
domain_classifier.fit(feature_for_train.float(), label_for_train.long())
output = domain_classifier.predict(feature_for_test)
acc = np.mean((label_for_test.numpy() == output).astype(np.float32))
val_acc.append(acc)
domain_classifiers.append(domain_classifier)
index = val_acc.index(max(val_acc))
domain_classifier = domain_classifiers[index]
domain_out = domain_classifier.predict_proba(validation_feature.to(device).float())
return domain_out[:, :1] / domain_out[:, 1:] * N_s * 1.0 / N_t
def calc_dev_risk(target_model, src_train_dl, tgt_train_dl, src_valid_dl, configs, device):
src_train_feats = target_model.joint_learner(src_train_dl.dataset.x_data.float().to(device))
tgt_train_feats = target_model.joint_learner(tgt_train_dl.dataset.x_data.float().to(device))
src_valid_feats = target_model.joint_learner(src_valid_dl.dataset.x_data.float().to(device))
src_valid_pred = target_model.joint_classifier(src_valid_feats)
dev_weights = get_weight_gpu(src_train_feats.to(device), tgt_train_feats.to(device),
src_valid_feats.to(device), configs, device)
dev_error = F.cross_entropy(src_valid_pred, src_valid_dl.dataset.y_data.long().to(device), reduction='none')
dev_risk = get_dev_value(dev_weights, dev_error.unsqueeze(1).detach().cpu().numpy())
# iwcv_risk = get_iwcv_value(dev_weights, dev_error.unsqueeze(1).detach().cpu().numpy())
return dev_risk
def calculate_risk(target_model, risk_dataloader, device):
if type(risk_dataloader) == tuple:
x_data = torch.cat((risk_dataloader[0].dataset.x_data, risk_dataloader[1].dataset.x_data), axis=0)
y_data = torch.cat((risk_dataloader[0].dataset.y_data, risk_dataloader[1].dataset.y_data), axis=0)
else:
x_data = risk_dataloader.dataset.x_data
y_data = risk_dataloader.dataset.y_data
feat = target_model.joint_learner(x_data.float().to(device))
pred = target_model.joint_classifier(feat)
cls_loss = F.cross_entropy(pred, y_data.long().to(device))
return cls_loss.item()
# For DIRT-T
class EMA:
def __init__(self, decay):
self.decay = decay
self.shadow = {}
def register(self, model):
for name, param in model.named_parameters():
if param.requires_grad:
self.shadow[name] = param.data.clone()
self.params = self.shadow.keys()
def __call__(self, model):
if self.decay > 0:
for name, param in model.named_parameters():
if name in self.params and param.requires_grad:
self.shadow[name] -= (1 - self.decay) * (self.shadow[name] - param.data)
param.data = self.shadow[name]