-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatasets.py
265 lines (199 loc) · 8.67 KB
/
datasets.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
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
# -*- coding: utf-8 -*-
# @Author: Andre Goncalves
# @Date: 2019-08-07 09:45:23
# @Last Modified by: Andre Goncalves
# @Last Modified time: 2019-11-06 09:27:31
import numpy as np
import pandas as pd
from abc import ABCMeta, abstractmethod
from sklearn.datasets import fetch_20newsgroups
import torch
import torchvision
import torchvision.transforms as transforms
from feature_extraction.feature_extraction import *
from utils import config
class Dataset(object):
__metaclass__ = ABCMeta
def __init__(self, name):
assert isinstance(name, str)
self.name = name
self.train = {}
self.unlabeled = {}
self.test = {}
@abstractmethod
def prepare(self):
pass
def data_split(self, ids_train, ids_unlab, ids_test):
"""Split data into training, validation, test.
We have to pass ids_train and ids_test as
all datasets (one for each method) must
have the same training and test set at
the beginning of the active learning loop.
Test set will remain the same throughout
the process, but training will be altered,
as the unlabeleb samples will selected by
the AL method.
Args:
ids_train (np.array): training set samples id
ids_unlab (np.array): unlabeled set samples id
ids_test (np.array): test set samples id
"""
self.train['x'] = self.X[ids_train, :].copy()
self.train['y'] = self.y[ids_train].copy()
self.test['x'] = self.X[ids_test, :].copy()
self.test['y'] = self.y[ids_test].copy()
self.unlabeled['x'] = self.X[ids_unlab, :].copy()
self.unlabeled['y'] = self.y[ids_unlab].copy()
def update_data_sets(self, ids):
"""Update the list of samples in the training and unlabeled set.
It emulates the process of labeling unlabeled dataset and moving
it to the training set.
Args:
ids (np.array): ids of the unlabeled samples to be unlabeled
"""
# move samples from the unlabeled set to the training set
# simulating the process of label a block of unlabeled data
self.train['x'] = np.vstack((self.train['x'], self.unlabeled['x'][ids]))
self.train['y'] = np.concatenate((self.train['y'], self.unlabeled['y'][ids]))
# remove the set of samples that has just been labeled
# from the unlabeled set
self.unlabeled['x'] = np.delete(self.unlabeled['x'], ids, axis=0)
self.unlabeled['y'] = np.delete(self.unlabeled['y'], ids, axis=0)
def _feature_extraction(self):
"""Perform one of the few feature extraction methods available.
Returns:
X, y (np.array): Extracted features and labels.
Raises:
ValueError: Unknown feature extraction method.
"""
# if self.feature_extraction == 'BOW_DimReduction':
# fe = BOW_DimReduction(features_dim=100,
# projection='PCA')
# elif self.feature_extraction == 'BOW_TopicModel':
# fe = BOW_TopicModel(nb_topics=30)
# elif self.feature_extraction == 'BERT':
# fe = BERT()
# else:
# raise ValueError("Unknown feature extraction: {}".format(self.feature_extraction))
self.X, self.y = self.fe.extract_features(self)
def is_unlabeled_empty(self):
"""Check whether all unlabeled samples have
been labeled.
Returns:
Bool: unlabeled bucket is empty of not
"""
return (len(self.unlabeled) == 0)
def get_name(self):
"""Return model's name.
Returns:
str: Model's name.
"""
return self.name
class TwentyNewsGroups(Dataset):
def __init__(self, feature_extraction):
super().__init__('20News_Groups')
self.fe = feature_extraction
# only 4 classes
categories = ['alt.atheism',
'talk.religion.misc',
'comp.graphics',
'sci.space']
remove = ('headers', 'footers', 'quotes')
# 20 news group dataset
dataset = fetch_20newsgroups(config.path_to_data,
categories=categories,
shuffle=True,
random_state=42,
remove=remove)
self.data = dataset.data
self.target = dataset.target
self.feature_extraction = feature_extraction
def prepare(self):
super()._feature_extraction()
class PathologyReports(Dataset):
def __init__(self, target_var, feature_extraction):
super().__init__('PathologyReports')
self.fe = feature_extraction
# 20 news group dataset
fname = os.path.join(config.path_to_processed_path_reports,
'labeled_reports.csv')
df = pd.read_csv(fname)
# remove all rows where target_var is nan
df = df.dropna(subset=[target_var])
df = df.groupby(target_var).filter(lambda x: len(x) >= 5)
if target_var != 'PR' and target_var != 'ER':
# map string to numbers
mapping = dict()
for i, c in enumerate(df[target_var].unique()):
mapping[c] = i
# replace strings by numbers (label encoding)
df = df.replace({target_var: mapping})
self.data = df['Report'].to_list()
self.target = df[target_var].values.astype(int)
# remove nans
self.feature_extraction = feature_extraction
def prepare(self):
super()._feature_extraction()
class CIFAR10(Dataset):
def __init__(self):
super().__init__('CIFAR10')
transform = None
self.mean = torch.tensor([0.4914, 0.4822, 0.4465])
self.std = torch.tensor([0.2023, 0.1994, 0.2010])
self.trainset = torchvision.datasets.CIFAR10(root='../data', train=True,
download=True, transform=transform)
self.testset = torchvision.datasets.CIFAR10(root='../data', train=False,
download=True, transform=transform)
self.classes = ('plane', 'car', 'bird', 'cat', 'deer',
'dog', 'frog', 'horse', 'ship', 'truck')
def normalize(self, x):
""" Apply normalization on the images. """
x /= 255.
x.sub_(self.mean[:, None, None]).div_(self.std[:, None, None])
return x
def prepare(self):
""" Convert dataloader type to tensors. """
x_train = torch.from_numpy(self.trainset.data).type(torch.float32)
x_test = torch.from_numpy(self.testset.data).type(torch.float32)
y_train = torch.tensor(self.trainset.targets, dtype=torch.int64)
y_test = torch.tensor(self.testset.targets, dtype=torch.int64)
self.X = torch.cat((x_train, x_test), 0)
self.X = self.X.permute(0, 3, 1, 2)
# transform by hand
self.X = self.normalize(self.X)
self.X = self.X.numpy()
self.y = torch.cat((y_train, y_test), 0).numpy()
# self.X = self.X[0:500]
# self.y = self.y[0:500]
print('data.shape: {}'.format(self.X.shape))
print('target.shape: {}'.format(self.y.shape))
class SVHN(Dataset):
def __init__(self):
super().__init__('SVHN')
transform = None
self.mean = torch.tensor([0.5, 0.5, 0.5])
self.std = torch.tensor([0.5, 0.5, 0.5])
self.trainset = torchvision.datasets.SVHN(root='../data', split='train',
download=True, transform=transform)
self.testset = torchvision.datasets.SVHN(root='../data', split='test',
download=True, transform=transform)
def normalize(self, x):
""" Apply normalization on the images. """
x /= 255.
x.sub_(self.mean[:, None, None]).div_(self.std[:, None, None])
return x
def prepare(self):
""" Convert dataloader type to tensors. """
x_train = torch.from_numpy(self.trainset.data).type(torch.float32)
x_test = torch.from_numpy(self.testset.data).type(torch.float32)
y_train = torch.tensor(self.trainset.labels, dtype=torch.int64)
y_test = torch.tensor(self.testset.labels, dtype=torch.int64)
self.X = torch.cat((x_train, x_test), 0)
# transform by hand
self.X = self.normalize(self.X)
self.X = self.X.numpy()
self.y = torch.cat((y_train, y_test), 0).numpy()
# self.X = self.X[0:500]
# self.y = self.y[0:500]
print('data.shape: {}'.format(self.X.shape))
print('target.shape: {}'.format(self.y.shape))