-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.py
More file actions
215 lines (183 loc) · 8.47 KB
/
dataset.py
File metadata and controls
215 lines (183 loc) · 8.47 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
import os
import numpy as np
import torch
import numpy as np
# from deeprobust.graph.utils import *
from torch_geometric.data import NeighborSampler
from torch_geometric.utils import to_dense_adj
import scipy.sparse as sp
import deeprobust.graph.utils as utils
from sklearn.metrics.pairwise import euclidean_distances
from scipy.spatial.distance import cdist
from tqdm import tqdm
def edge_index_to_sp(edge_index):
row = edge_index[0].numpy()
col = edge_index[1].numpy()
data = [1] * len(row)
adj_matrix = sp.csr_matrix((data, (row, col)), shape=(edge_index.max().item() + 1, edge_index.max().item() + 1))
return adj_matrix
def to_tensor(adj):
if sp.issparse(adj):
adj = utils.sparse_mx_to_torch_sparse_tensor(adj)
else:
adj = torch.FloatTensor(adj)
return adj
class Dataset:
def __init__(self, args):
# ==================== Load ====================
self.args = args
data = self.load_dataset()
# ==================== Process ====================
if args.transductive:
x = self.load_attacked_dataset('f', args.feature_budget)
edge_index = self.load_attacked_dataset('s', args.structure_budget)
y = self.load_attacked_dataset('l', args.label_budget)
idx_train, idx_val, idx_test = data['idx_train'].numpy(), data['idx_val'].numpy(), data['idx_test'].numpy()
adj = edge_index_to_sp(edge_index)
nfeat = data['num_features']
nclass = data['num_classes']
# ==================== Outlier ====================
if args.feature_outlier:
x_train = x[idx_train].numpy()
y_train = y[idx_train].numpy()
outlier = []
# print(x_train.shape)
dist = cdist(x_train, x_train, metric='euclidean')
for c in range(nclass):
c_idx = np.where(y_train == c)[0]
c_dist = dist[c_idx][:, c_idx]
c_dist = np.mean(c_dist, axis=1)
c_dist_mean = np.mean(c_dist)
c_dist_std = np.std(c_dist)
idx_outlier = c_idx[c_dist > c_dist_mean + args.feature_outlier_scale * c_dist_std]
# idx_outlier = idx_train[idx_outlier]
outlier.extend(idx_outlier)
self.outlier = np.array(outlier)
# real_outlier = np.where(np.any(data['x'][idx_train].numpy() != x[idx_train].numpy(), axis=1))[0]
# print('----------------------------------')
# print(sorted(list(real_outlier)))
# print(sorted(list(outlier)))
# print('----------------------------------')
# # ==================== Normalize ====================
# if args.normalize_features:
# x = x / x.sum(dim=1, keepdim=True).clamp(min=1e-12)
# x_test = x_test / x_test.sum(dim=1, keepdim=True).clamp(min=1e-12)
# ==================== Assign ====================
self.x = self.x_full = x.numpy()
self.adj = self.adj_full = adj
self.y = y.numpy()
self.idx_train = idx_train
self.idx_val = idx_val
self.idx_test = idx_test
self.labels_train = y[idx_train].cpu().numpy()
self.labels_val = y[idx_val].cpu().numpy()
self.labels_test = y[idx_test].cpu().numpy()
self.nfeat = nfeat
self.nclass = nclass
self.device = args.device
self.class_dict = None
self.samplers = None
self.class_dict2 = None
def load_attacked_dataset(self, attack_type, budget):
if attack_type == 's':
save = torch.load(f'./data/{self.args.name}/s_{budget}.pth', map_location='cpu')
return save['edge_index_attacked']
elif attack_type == 'f':
save = torch.load(f'./data/{self.args.name}/f_{budget}.pth', map_location='cpu')
return save['x_attacked']
elif attack_type == 'l':
save = torch.load(f'./data/{self.args.name}/l_{budget}.pth', map_location='cpu')
return save['y_attacked']
else:
raise FileNotFoundError
def load_dataset(self):
dataset = torch.load(os.path.abspath(f'./data/{self.args.name}/clean.pth'), map_location='cpu')
return dataset
def retrieve_class(self, c, num=256):
if self.class_dict is None:
self.class_dict = {}
for i in range(self.nclass):
self.class_dict['class_%s'%i] = (self.labels_train == i)
idx = np.arange(len(self.labels_train))
idx = idx[self.class_dict['class_%s'%c]]
if self.args.feature_outlier:
idx = np.setdiff1d(idx, self.outlier)
if len(idx) >= num:
idx_selected = np.random.permutation(idx)[:num]
else:
idx_selected = np.random.permutation(idx)
idx_selected = np.concatenate([idx_selected, np.random.choice(idx_selected, num - len(idx_selected), replace=True)])
return idx_selected
else:
return np.random.permutation(idx)[:num]
def update_class_dict(self, transductive):
# self.y_error_idx = {}
if self.class_dict2 is None:
self.class_dict2 = {}
for i in range(self.nclass):
if transductive:
idx = self.idx_train[self.labels_train == i]
else:
idx = np.arange(len(self.labels_train))[self.labels_train==i]
self.class_dict2[i] = idx
def retrieve_class_sampler(self, c, adj, transductive, num=256, args=None):
if self.class_dict2 is None:
self.class_dict2 = {}
for i in range(self.nclass):
if transductive:
idx = self.idx_train[self.labels_train == i]
else:
idx = np.arange(len(self.labels_train))[self.labels_train==i]
self.class_dict2[i] = idx
if args.nlayers == 1:
sizes = [15]
if args.nlayers == 2:
sizes = [10, 5]
if args.nlayers == 3:
sizes = [15, 10, 5]
if args.nlayers == 4:
sizes = [15, 10, 5, 5]
if args.nlayers == 5:
sizes = [15, 10, 5, 5, 5]
if self.samplers is None:
self.samplers = []
for i in range(self.nclass):
# node_idx = torch.LongTensor(self.class_dict2[i])
node_idx = torch.tensor(self.class_dict2[i], dtype=torch.int64)
if len(node_idx) == 0:
continue
self.samplers.append(NeighborSampler(adj,
node_idx=node_idx,
sizes=sizes, batch_size=num,
num_workers=2, return_e_id=False,
num_nodes=adj.size(0),
shuffle=True))
batch = torch.tensor(np.random.permutation(self.class_dict2[c])[:num], dtype=torch.int64)
out = self.samplers[c].sample(batch)
return out
def retrieve_class_multi_sampler(self, c, adj, transductive, num=256, args=None):
if self.class_dict2 is None:
self.class_dict2 = {}
for i in range(self.nclass):
if transductive:
idx = self.idx_train[self.labels_train == i]
else:
idx = np.arange(len(self.labels_train))[self.labels_train==i]
self.class_dict2[i] = idx
if self.samplers is None:
self.samplers = []
for l in range(2):
layer_samplers = []
sizes = [15] if l == 0 else [10, 5]
for i in range(self.nclass):
node_idx = torch.LongTensor(self.class_dict2[i])
layer_samplers.append(NeighborSampler(adj,
node_idx=node_idx,
sizes=sizes, batch_size=num,
num_workers=12, return_e_id=False,
num_nodes=adj.size(0),
shuffle=True))
self.samplers.append(layer_samplers)
batch = np.random.permutation(self.class_dict2[c])[:num]
out = self.samplers[args.nlayers-1][c].sample(batch)
return out