-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
181 lines (143 loc) · 8.02 KB
/
dataset.py
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
from torch_geometric.datasets import CitationFull
import torch_geometric.transforms as T
import torch
import os.path as osp
from typing import Callable, List, Optional
import numpy as np
import torch
from torch_geometric.data import InMemoryDataset, download_url
from torch_geometric.io import read_planetoid_data
class Planetoid(InMemoryDataset):
url = 'https://github.com/kimiyoung/planetoid/raw/master/data'
geom_gcn_url = ('https://raw.githubusercontent.com/graphdml-uiuc-jlu/'
'geom-gcn/master')
def __init__(self, root: str, name: str, split: str = "public", # split: public, imbalance, random
imb_ratio: int = 10, fix_minority: bool = True,
repeatition: int = 10,
num_train_per_class: int = 20, num_val: int = 500,
num_test: int = 1000, transform: Optional[Callable] = None,
pre_transform: Optional[Callable] = None):
self.name = name
self.split = split.lower()
self.fix_minority = fix_minority
assert self.split in ['public', 'full', 'geom-gcn', 'random', 'imbalance']
super().__init__(root, transform, pre_transform)
self.data, self.slices = torch.load(self.processed_paths[0])
if split == 'full':
data = self.get(0)
data.train_mask.fill_(True)
data.train_mask[data.val_mask | data.test_mask] = False
self.data, self.slices = self.collate([data])
elif split == 'random':
data = self.get(0)
data.train_mask.fill_(False)
for c in range(self.num_classes):
idx = (data.y == c).nonzero(as_tuple=False).view(-1)
idx = idx[torch.randperm(idx.size(0))[:num_train_per_class]]
data.train_mask[idx] = True
remaining = (~data.train_mask).nonzero(as_tuple=False).view(-1)
remaining = remaining[torch.randperm(remaining.size(0))]
data.val_mask.fill_(False)
data.val_mask[remaining[:num_val]] = True
data.test_mask.fill_(False)
data.test_mask[remaining[num_val:num_val + num_test]] = True
self.data, self.slices = self.collate([data])
elif split == 'imbalance': # here
data = self.get(0)
n_cls = self.num_classes
self.imb_cls_num = n_cls // 2
imb_cls_num_list = [] # 存放每个类的训练节点数
# 每个类训练节点, 每个类训练节点数
train_nodes_per_cls, num_train_nodes_per_cls = self.get_idx_info(data, n_cls)
max_num = np.max(num_train_nodes_per_cls[:n_cls-self.imb_cls_num])
for i in range(n_cls):
if imb_ratio > 1 and i > n_cls-1-self.imb_cls_num: # i>3: i = 4,5,6 set to imbalanced class
imb_cls_num_list.append(min(int(max_num*(1./imb_ratio)), num_train_nodes_per_cls[i]))
else:
imb_cls_num_list.append(num_train_nodes_per_cls[i])
if osp.exists(osp.join(self.processed_dir, name + '_' + str(imb_ratio) + '.pt')):
print('Using Existing Training Masks')
imb_train_mask, imb_nodes_per_cls = self.get_imb_trainset(data, n_cls, data.num_nodes, train_nodes_per_cls, num_train_nodes_per_cls, imb_cls_num_list, fix_minority)
data.imb_train_mask = imb_train_mask
imb_train_masks = torch.load(osp.join(self.processed_dir, name + '_' + str(imb_ratio) + '.pt'))
else:
imb_train_masks = []
for r in range(repeatition):
imb_train_mask, imb_nodes_per_cls = self.get_imb_trainset(data, n_cls, data.num_nodes, train_nodes_per_cls, num_train_nodes_per_cls, imb_cls_num_list, False)
imb_train_masks.append(imb_train_mask)
imb_train_masks = torch.stack(imb_train_masks).detach().cpu()
torch.save(imb_train_masks, f'datasets/{name}/{name}_{imb_ratio}.pt')
data.imb_train_masks = imb_train_masks
self.imb_cls_num_list = imb_cls_num_list
self.data, self.slices = self.collate([data])
@property
def raw_dir(self) -> str:
if self.split == 'geom-gcn':
return osp.join(self.root, self.name, 'geom-gcn', 'raw')
return osp.join(self.root, self.name, 'raw')
@property
def processed_dir(self) -> str:
if self.split == 'geom-gcn':
return osp.join(self.root, self.name, 'geom-gcn', 'processed')
return osp.join(self.root, self.name, 'processed')
@property
def raw_file_names(self) -> List[str]:
names = ['x', 'tx', 'allx', 'y', 'ty', 'ally', 'graph', 'test.index']
return [f'ind.{self.name.lower()}.{name}' for name in names]
@property
def processed_file_names(self) -> str:
return 'data.pt'
########## Imbalanced setting ##############
def get_idx_info(self, data, n_cls):
train_mask = data.train_mask
labels = data.y
index_list = torch.arange(labels.shape[0]) # all node indices
train_nodes_per_cls = []
num_train_nodes_per_cls = []
for i in range(n_cls):
cls_indices = index_list[((labels == i) & train_mask)] # all nodes idx with label i
num_nodes_i = (labels[train_mask] == i).sum() # num of nodes in class i
train_nodes_per_cls.append(cls_indices) # training nodes in class i
num_train_nodes_per_cls.append(int(num_nodes_i.item())) # num nodes in class i
return train_nodes_per_cls, num_train_nodes_per_cls
def get_imb_trainset(self, data, n_cls, n_nodes, train_nodes_per_cls, num_train_nodes_per_cls, class_num_list, fix_minority = True):
imb_train_mask = torch.zeros(n_nodes, dtype=torch.bool)
imb_nodes_per_cls = []
for i in range(n_cls):
if num_train_nodes_per_cls[i] > class_num_list[i]: # 如果类i的训练节点数>采样的训练节点数
# 打乱类i的训练节点/固定
cls_idx = torch.arange(train_nodes_per_cls[i].shape[0]) if fix_minority else torch.randperm(train_nodes_per_cls[i].shape[0])
cls_idx = train_nodes_per_cls[i][cls_idx] # 类i新的node idx排列
cls_idx = cls_idx[:class_num_list[i]] # 采样对应数量的节点作为新训练集
imb_nodes_per_cls.append(cls_idx)
else:
imb_nodes_per_cls.append(train_nodes_per_cls[i])
imb_train_mask[imb_nodes_per_cls[i]] = True
assert imb_train_mask.sum().long() == sum(class_num_list) # num_nodes in imbalance graph
assert sum([len(idx) for idx in imb_nodes_per_cls]) == sum(class_num_list)
return imb_train_mask, imb_nodes_per_cls
def download(self):
for name in self.raw_file_names:
download_url(f'{self.url}/{name}', self.raw_dir)
if self.split == 'geom-gcn':
for i in range(10):
url = f'{self.geom_gcn_url}/splits/{self.name.lower()}'
download_url(f'{url}_split_0.6_0.2_{i}.npz', self.raw_dir)
def process(self):
data = read_planetoid_data(self.raw_dir, self.name)
if self.split == 'geom-gcn':
train_masks, val_masks, test_masks = [], [], []
for i in range(10):
name = f'{self.name.lower()}_split_0.6_0.2_{i}.npz'
splits = np.load(osp.join(self.raw_dir, name))
train_masks.append(torch.from_numpy(splits['train_mask']))
val_masks.append(torch.from_numpy(splits['val_mask']))
test_masks.append(torch.from_numpy(splits['test_mask']))
data.train_mask = torch.stack(train_masks, dim=1)
data.val_mask = torch.stack(val_masks, dim=1)
data.test_mask = torch.stack(test_masks, dim=1)
data = data if self.pre_transform is None else self.pre_transform(data)
torch.save(self.collate([data]), self.processed_paths[0])
def __repr__(self) -> str:
return f'{self.name}()'
# def to_imblanced(data, )