Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
213 lines (190 loc) · 7.57 KB
/
Copy pathutils.py
File metadata and controls
213 lines (190 loc) · 7.57 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
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.autograd import Variable
import torch.optim as optim
import torch_geometric
import random
from tensorboardX import SummaryWriter
from torch_geometric.utils.degree import degree
from utils_metric import *
from utils import *
import yaml
import argparse
import time
import os
from sklearn.metrics import f1_score, classification_report, precision_score
from sklearn.metrics import confusion_matrix
from torch_geometric.utils import dropout_adj
from torch_scatter import scatter_add
from torch_geometric.utils import add_remaining_self_loops
import math
import warnings
warnings.filterwarnings("ignore")
def get_class_by_id(y_label):
labels = np.unique(y_label.cpu().numpy())
class_by_id = {}
for label in labels:
class_by_id[str(label)] = []
for i in range(len(y_label)):
class_by_id[str(int(y_label[i]))].append(i)
return class_by_id
def get_spt_qry_id_old(class_by_id, k_spt, k_qry=None):
spt_idx, qry_idx = [], []
# spt_idx_list, qry_idx = [], []
class_list = list(class_by_id.keys())
for class_key in class_list:
class_i_spt_idx = random.sample(
class_by_id[class_key], k_spt
)
spt_idx = spt_idx + class_i_spt_idx
# spt_idx_list.append(class_i_spt_idx)
if k_qry == None:
class_i_qry_idx = [n for n in class_by_id[class_key] if n not in class_i_spt_idx]
else:
class_i_qry_idx = random.sample(
[n for n in class_by_id[class_key] not in class_i_spt_idx], k_qry
)
qry_idx = qry_idx + class_i_qry_idx
return spt_idx, qry_idx
def get_spt_qry_id(class_by_id, k_spt, k_qry=None):
# spt_idx, qry_idx = [], []
spt_idx_list, qry_idx = [], []
class_list = list(class_by_id.keys())
for class_key in class_list:
class_i_spt_idx = random.sample(
class_by_id[class_key], k_spt
)
# spt_idx = spt_idx + class_i_spt_idx
spt_idx_list.append(class_i_spt_idx)
if k_qry == None:
class_i_qry_idx = [n for n in class_by_id[class_key] if n not in class_i_spt_idx]
else:
class_i_qry_idx = random.sample(
[n for n in class_by_id[class_key] not in class_i_spt_idx], k_qry
)
qry_idx = qry_idx + class_i_qry_idx
return spt_idx_list, qry_idx
def get_ft_spt_qry_id(class_by_id, k_spt, k_qry=None):
spt_idx_list, qry_idx = [], []
class_list = list(class_by_id.keys())
for class_key in class_list:
if len(class_by_id[class_key]) == k_spt:
class_i_spt_idx = class_by_id[class_key]
else:
ratio = int(random.uniform(1, 5))
class_i_spt_idx = random.sample(
class_by_id[class_key], k_spt * ratio
)
spt_idx_list.append(class_i_spt_idx)
if k_qry == None:
class_i_qry_idx = [n for n in class_by_id[class_key] if n not in class_i_spt_idx]
else:
class_i_qry_idx = random.sample(
[n for n in class_by_id[class_key] if n not in class_i_spt_idx], k_qry
)
qry_idx = qry_idx + class_i_qry_idx
return spt_idx_list, qry_idx
def get_unbalanced_spt_qry_id(class_by_id, k_spt_max, k_qry=None):
spt_idx_list, qry_idx = [], []
class_list = list(class_by_id.keys())
for class_key in class_list:
k_spt = int(random.uniform(1, k_spt_max))
class_i_spt_idx = random.sample(
class_by_id[class_key], k_spt
)
spt_idx_list.append(class_i_spt_idx)
if k_qry == None:
class_i_qry_idx = [n for n in class_by_id[class_key] if n not in class_i_spt_idx]
else:
class_i_qry_idx = random.sample(
[n for n in class_by_id[class_key] if n not in class_i_spt_idx], k_qry
)
qry_idx = qry_idx + class_i_qry_idx
return spt_idx_list, qry_idx
def get_random_fewshot_spt_qry_id(class_by_id, k_spt_min, k_spt_max, ratio=0.5, k_qry=None):
spt_idx_list, qry_idx = [], []
class_list = list(class_by_id.keys())
for class_key in class_list:
if random.random() > 0.5:
k_spt = k_spt_max
else:
k_spt = k_spt_min
class_i_spt_idx = random.sample(
class_by_id[class_key], k_spt
)
spt_idx_list.append(class_i_spt_idx)
if k_qry == None:
class_i_qry_idx = [n for n in class_by_id[class_key] if n not in class_i_spt_idx]
else:
class_i_qry_idx = random.sample(
[n for n in class_by_id[class_key] not in class_i_spt_idx], k_qry
)
qry_idx = qry_idx + class_i_qry_idx
return spt_idx_list, qry_idx
def get_test_spt_list(y_label, mapping_idx):
labels = np.unique(y_label.cpu().numpy())
class_by_id = {}
# class_by_id_test_qry = {}
for label in labels:
class_by_id[str(label)] = []
# class_by_id_test_qry[str(label)] = []
for i in range(len(y_label)):
if i not in mapping_idx:
class_by_id[str(int(y_label[i]))].append(i)
# else:
# class_by_id_test_qry[str(int(y_label[i]))].append(i)
spt_idx_list = []
class_list = list(class_by_id.keys())
# sample_num_list = []
# for class_key in class_list:
# sample_num_list.append(len(class_by_id_test_qry[class_key]))
# test_qry_num = np.min(np.array(sample_num_list))
# test_qry_num = np.max(np.array(sample_num_list))
for class_key in class_list:
class_i_spt_idx = class_by_id[class_key]
spt_idx_list.append(class_i_spt_idx)
# test_qry_idx = test_qry_idx + random.sample(
# class_by_id_test_qry[class_key], test_qry_num
# )
# test_qry_idx = test_qry_idx + list(np.random.choice(
# class_by_id_test_qry[class_key], test_qry_num
# ))
return spt_idx_list
def get_proto_embedding(proto_model, spt_idx_list, embeddings, degree_list=None):
for i, spt_idx in enumerate(spt_idx_list):
spt_embedding_i = embeddings[spt_idx]
if degree_list == None:
proto_embedding_i, _ = proto_model(spt_embedding_i)
else:
degree_list_i = degree_list[spt_idx]
proto_embedding_i, _ = proto_model(spt_embedding_i, degree_list_i)
if i == 0:
proto_embedding = proto_embedding_i
else:
proto_embedding = torch.cat((proto_embedding, proto_embedding_i), dim=0)
return proto_embedding
def get_degree_list(edge_index, num_nodes):
edge_weight = torch.ones(edge_index.size(1), device=edge_index.device)
edge_index, tmp_edge_weight = add_remaining_self_loops(
edge_index, edge_weight, 1., num_nodes
)
edge_weight = tmp_edge_weight
row, col = edge_index[0], edge_index[1]
deg = scatter_add(edge_weight, row, dim=0, dim_size=num_nodes)
return deg
def get_degree_proto_embedding(spt_idx_list, embeddings, degree_list):
for i, spt_idx in enumerate(spt_idx_list):
spt_embedding_i = embeddings[spt_idx]
degree_list_i = degree_list[spt_idx]
norm_degree = degree_list_i / torch.sum(degree_list_i)
norm_degree = norm_degree.unsqueeze(1)
# print("spt_embedding_i shape", spt_embedding_i.shape, "norm_degree shape", norm_degree.shape)
proto_embedding_i = torch.sum(
torch.mul(spt_embedding_i, norm_degree), 0
)
if i == 0:
proto_embedding = proto_embedding_i.unsqueeze(0)
else:
proto_embedding = torch.cat((proto_embedding, proto_embedding_i.unsqueeze(0)), dim=0)
return proto_embedding