-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
146 lines (133 loc) · 8.17 KB
/
utils.py
File metadata and controls
146 lines (133 loc) · 8.17 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 numpy as np
import os
import pickle
import json
import scipy.io as sio
def save_model(model, model_dir, mode, setting, seed, svc=False):
if svc:
outfile = '%s_%d_%s.pkl' % (setting, seed, mode)
with open(os.path.join(model_dir, outfile), 'wb') as f:
pickle.dump({'model': model}, f)
return
outfile = '%s_%d_%s.ckpt' % (setting, seed, mode)
torch.save({'model_state_dict': model.state_dict()},
os.path.join(model_dir, outfile))
def load_model(model, hparams, hparams_seed, seed):
if 'mode' in hparams.keys() and hparams['mode'] == 'dist_shift':
ckpt_name = '%s_%s_%s_%s_%d_%d.ckpt' % (hparams['model'], hparams['src_dataset'], hparams['dst_dataset'], hparams['sensitive_attribute'], hparams_seed, seed)
else:
ckpt_name = '%s_%s_%s_%d_%d.ckpt' % (hparams['model'], hparams['dataset'], hparams['sensitive_attribute'], hparams_seed, seed)
checkpoint = torch.load(os.path.join(hparams['model_dir'], ckpt_name), map_location=hparams['device'])
if hparams['model'] == 'GroupDRO':
model.q = (checkpoint['model_state_dict']['q'])
model.load_state_dict(checkpoint['model_state_dict'], strict=True)
model.to(hparams['device'])
return model
def save_result(score_val, score_test, score_dir, setting, seed):
outfile = '%s_%d.pkl' % (setting, seed)
with open(os.path.join(score_dir, outfile), 'wb') as f:
pickle.dump({'val': score_val, 'test': score_test}, f)
def generate_config():
configuration = {}
for dataset in ['Wikipedia', 'OfficeCaltech', 'NUSIMAGE', 'MRT', 'ImageCLEF', 'BT', 'PTBXL']:
if dataset == 'Wikipedia': # 2 settings
data_path = os.path.join('datasets', dataset)
labeled_size = 5
domains = ['Image_BT', 'TEXT_BB']
for source in domains:
for target in domains:
if source != target:
config = {'src_dir': os.path.join(data_path, 'L5', 'T_%s_L5.mat' % source),
'tgt_dir': os.path.join(data_path, 'L5', 'S_%s_L5.mat' % target),
'unk_idx': 5, 'n_labels': 6, 'lamda': 0.5}
tgt_data = sio.loadmat(config['tgt_dir'])
tgt_u_data = tgt_data['testing_labels'][0][0].reshape(-1) - 1
config['lamda'] = (tgt_u_data >= config['unk_idx']).sum() / len(tgt_u_data)
configuration['%s_%s_%s_%d' % (dataset, source, target, labeled_size)] = config
elif dataset == 'OfficeCaltech': # 18 settings
data_path = os.path.join('datasets', dataset)
labeled_size = 3
domains = ['amazon', 'Caltech10', 'webcam', 'dslr']
features = ['DeCAF6', 'SURF_L10']
for source in domains[:-1]:
for target in domains:
for source_feature in features:
for target_feature in features:
if source != target and source_feature != target_feature:
config = {'src_dir': os.path.join(data_path, 'randsource_%s_%s.mat' % (source, source_feature)),
'tgt_dir': os.path.join(data_path, 'randtarget_%s_%s.mat' % (target, target_feature)),
'unk_idx': 5, 'n_labels': 6, 'lamda': 0.5}
tgt_data = sio.loadmat(config['tgt_dir'])
tgt_u_data = tgt_data['testing_labels'][0][0].reshape(-1) - 1
config['lamda'] = (tgt_u_data >= config['unk_idx']).sum() / len(tgt_u_data)
configuration['%s_%s_%s_%s_%s_%d' % (dataset, source, source_feature, target, target_feature, labeled_size)] = config
elif dataset == 'NUSIMAGE': # 1 setting
data_path = os.path.join('datasets', dataset)
labeled_size = 3
source = 'nustag_hist'
target = 'imagenet_decaf'
config = {'src_dir': os.path.join(data_path, '%s.mat' % source),
'tgt_dir': os.path.join(data_path, '%s.mat' % target),
'unk_idx': 4, 'n_labels': 5, 'lamda': 0.5}
configuration['%s_%s_%s_%d' % (dataset, source, target, labeled_size)] = config
elif dataset == 'MRT': # 4 settings
data_path = os.path.join('datasets', dataset)
labeled_size = 20
sources = ['english', 'french', 'german', 'italian']
target = 'spanish'
for source in sources:
config = {'src_dir': os.path.join(data_path, '%s_tfidf.mat' % source),
'tgt_dir': os.path.join(data_path, '%s_tfidf.mat' % target),
'unk_idx': 3, 'n_labels': 4, 'lamda': 0.5}
configuration['%s_%s_tfidf_%s_tfidf_%d' % (dataset, source, target, labeled_size)] = config
elif dataset == 'ImageCLEF': # 24 settings
data_path = os.path.join('datasets', dataset)
labeled_size = 3
domains = ['b', 'c', 'i', 'p']
features = ['r', 'v']
for source in domains:
for target in domains:
for source_feature in features:
for target_feature in features:
if source != target and source_feature != target_feature:
config = {'src_dir': os.path.join(data_path, '%s%s%s%ss.mat' % (source, source_feature, target, target_feature)),
'tgt_dir': os.path.join(data_path, '%s%s%s%st.mat' % (source, source_feature, target, target_feature)),
'unk_idx': 6, 'n_labels': 7, 'lamda': 0.5}
tgt_data = sio.loadmat(config['tgt_dir'])
tgt_u_data = tgt_data['testing_labels'][0][0].reshape(-1) - 1
config['lamda'] = (tgt_u_data >= config['unk_idx']).sum() / len(tgt_u_data)
configuration['%s_%s_%s_%s_%s_%d' % (dataset, source, source_feature, target, target_feature, labeled_size)] = config
elif dataset == 'BT':
data_path = os.path.join('datasets', dataset)
labeled_size_list = [1, 3, 5]
source = 'Source8'
target = 'CIFAR8'
features = [50, 101]
for labeled_size in labeled_size_list:
for source_feature in features:
for target_feature in features:
if source_feature != target_feature:
config = {'src_dir': os.path.join(data_path, 'Data_random_L%d' % labeled_size, '%s_%d_L%d.mat' % (source, source_feature, labeled_size)),
'tgt_dir': os.path.join(data_path, 'Data_random_L%d' % labeled_size, '%s_%d_L%d.mat' % (target, target_feature, labeled_size)),
'unk_idx': 4, 'n_labels': 5, 'lamda': 0.5}
tgt_data = sio.loadmat(config['tgt_dir'])
tgt_u_data = tgt_data['testing_labels'][0][0].reshape(-1) - 1
config['lamda'] = (tgt_u_data >= config['unk_idx']).sum() / len(tgt_u_data)
configuration['%s_%s_%d_%s_%d_%d' % (dataset, source, source_feature, target, target_feature, labeled_size)] = config
elif dataset == 'PTBXL':
# data_path = os.path.join('datasets', dataset)
data_path = os.path.join('ptb-xl')
labeled_size = 20
source, target = 'ECG', 'ECG'
source_feature, target_feature = 'signal', 'image'
config = {'data_dir': data_path, 'unk_idx': 4, 'n_labels': 5, 'lamda': 0.13081269013472405}
configuration['%s_%s_%s_%s_%s_%d' % (dataset, source, source_feature, target, target_feature, labeled_size)] = config
with open("configuration.json", "w") as outfile:
json.dump(configuration, outfile, indent=4)
return configuration
def get_config():
with open("configuration.json", "r") as infile:
configuration = json.load(infile)
return configuration
# generate_config()