forked from jmkim0309/fewshot-egnn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
executable file
·347 lines (287 loc) · 14.6 KB
/
data.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
from __future__ import print_function
from torchtools import *
import torch.utils.data as data
import random
import os
import numpy as np
from PIL import Image as pil_image
import pickle
from itertools import islice
from torchvision import transforms
class MiniImagenetLoader(data.Dataset):
def __init__(self, root, partition='train'):
super(MiniImagenetLoader, self).__init__()
# set dataset information
self.root = root
self.partition = partition
self.data_size = [3, 84, 84]
# set normalizer
mean_pix = [x / 255.0 for x in [120.39586422, 115.59361427, 104.54012653]]
std_pix = [x / 255.0 for x in [70.68188272, 68.27635443, 72.54505529]]
normalize = transforms.Normalize(mean=mean_pix, std=std_pix)
# set transformer
if self.partition == 'train':
self.transform = transforms.Compose([transforms.RandomCrop(84, padding=4),
lambda x: np.asarray(x),
transforms.ToTensor(),
normalize])
else: # 'val' or 'test' ,
self.transform = transforms.Compose([lambda x: np.asarray(x),
transforms.ToTensor(),
normalize])
# load data
self.data = self.load_dataset()
def load_dataset(self):
# load data
dataset_path = os.path.join(self.root, 'mini-imagenet/compacted_datasets', 'mini_imagenet_%s.pickle' % self.partition)
with open(dataset_path, 'rb') as handle:
data = pickle.load(handle)
# for each class
for c_idx in data:
# for each image
for i_idx in range(len(data[c_idx])):
# resize
image_data = pil_image.fromarray(np.uint8(data[c_idx][i_idx]))
image_data = image_data.resize((self.data_size[2], self.data_size[1]))
#image_data = np.array(image_data, dtype='float32')
#image_data = np.transpose(image_data, (2, 0, 1))
# save
data[c_idx][i_idx] = image_data
return data
def get_task_batch(self,
num_tasks=5,
num_ways=20,
num_shots=1,
num_queries=1,
seed=None):
if seed is not None:
random.seed(seed)
# init task batch data
support_data, support_label, query_data, query_label = [], [], [], []
for _ in range(num_ways * num_shots):
data = np.zeros(shape=[num_tasks] + self.data_size,
dtype='float32')
label = np.zeros(shape=[num_tasks],
dtype='float32')
support_data.append(data)
support_label.append(label)
for _ in range(num_ways * num_queries):
data = np.zeros(shape=[num_tasks] + self.data_size,
dtype='float32')
label = np.zeros(shape=[num_tasks],
dtype='float32')
query_data.append(data)
query_label.append(label)
# get full class list in dataset
full_class_list = list(self.data.keys())
# for each task
for t_idx in range(num_tasks):
# define task by sampling classes (num_ways)
task_class_list = random.sample(full_class_list, num_ways)
# for each sampled class in task
for c_idx in range(num_ways):
# sample data for support and query (num_shots + num_queries)
class_data_list = random.sample(self.data[task_class_list[c_idx]], num_shots + num_queries)
# load sample for support set
for i_idx in range(num_shots):
# set data
support_data[i_idx + c_idx * num_shots][t_idx] = self.transform(class_data_list[i_idx])
support_label[i_idx + c_idx * num_shots][t_idx] = c_idx
# load sample for query set
for i_idx in range(num_queries):
query_data[i_idx + c_idx * num_queries][t_idx] = self.transform(class_data_list[num_shots + i_idx])
query_label[i_idx + c_idx * num_queries][t_idx] = c_idx
# convert to tensor (num_tasks x (num_ways * (num_supports + num_queries)) x ...)
support_data = torch.stack([torch.from_numpy(data).float().to(tt.arg.device) for data in support_data], 1)
support_label = torch.stack([torch.from_numpy(label).float().to(tt.arg.device) for label in support_label], 1)
query_data = torch.stack([torch.from_numpy(data).float().to(tt.arg.device) for data in query_data], 1)
query_label = torch.stack([torch.from_numpy(label).float().to(tt.arg.device) for label in query_label], 1)
return [support_data, support_label, query_data, query_label]
class TieredImagenetLoader(data.Dataset):
def __init__(self, root, partition='train'):
self.root = root
self.partition = partition # train/val/test
#self.preprocess()
self.data_size = [3, 84, 84]
# load data
self.data = self.load_dataset()
# if not self._check_exists_():
# self._init_folders_()
# if self.check_decompress():
# self._decompress_()
# self._preprocess_()
def get_image_paths(self, file):
images_path, class_names = [], []
with open(file, 'r') as f:
f.readline()
for line in f:
name, class_ = line.split(',')
class_ = class_[0:(len(class_)-1)]
path = self.root + '/tiered-imagenet/images/'+name
images_path.append(path)
class_names.append(class_)
return class_names, images_path
def preprocess(self):
print('\nPreprocessing Tiered-Imagenet images...')
(class_names_train, images_path_train) = self.get_image_paths('%s/tiered-imagenet/train.csv' % self.root)
(class_names_test, images_path_test) = self.get_image_paths('%s/tiered-imagenet/test.csv' % self.root)
(class_names_val, images_path_val) = self.get_image_paths('%s/tiered-imagenet/val.csv' % self.root)
keys_train = list(set(class_names_train))
keys_test = list(set(class_names_test))
keys_val = list(set(class_names_val))
label_encoder = {}
label_decoder = {}
for i in range(len(keys_train)):
label_encoder[keys_train[i]] = i
label_decoder[i] = keys_train[i]
for i in range(len(keys_train), len(keys_train)+len(keys_test)):
label_encoder[keys_test[i-len(keys_train)]] = i
label_decoder[i] = keys_test[i-len(keys_train)]
for i in range(len(keys_train)+len(keys_test), len(keys_train)+len(keys_test)+len(keys_val)):
label_encoder[keys_val[i-len(keys_train) - len(keys_test)]] = i
label_decoder[i] = keys_val[i-len(keys_train)-len(keys_test)]
counter = 0
train_set = {}
for class_, path in zip(class_names_train, images_path_train):
img = pil_image.open(path)
img = img.convert('RGB')
img = img.resize((84, 84), pil_image.ANTIALIAS)
img = np.array(img, dtype='float32')
if label_encoder[class_] not in train_set:
train_set[label_encoder[class_]] = []
train_set[label_encoder[class_]].append(img)
counter += 1
if counter % 1000 == 0:
print("Counter "+str(counter) + " from " + str(len(images_path_train)))
test_set = {}
for class_, path in zip(class_names_test, images_path_test):
img = pil_image.open(path)
img = img.convert('RGB')
img = img.resize((84, 84), pil_image.ANTIALIAS)
img = np.array(img, dtype='float32')
if label_encoder[class_] not in test_set:
test_set[label_encoder[class_]] = []
test_set[label_encoder[class_]].append(img)
counter += 1
if counter % 1000 == 0:
print("Counter " + str(counter) + " from "+str(len(class_names_test)))
val_set = {}
for class_, path in zip(class_names_val, images_path_val):
img = pil_image.open(path)
img = img.convert('RGB')
img = img.resize((84, 84), pil_image.ANTIALIAS)
img = np.array(img, dtype='float32')
if label_encoder[class_] not in val_set:
val_set[label_encoder[class_]] = []
val_set[label_encoder[class_]].append(img)
counter += 1
if counter % 1000 == 0:
print("Counter "+str(counter) + " from " + str(len(class_names_val)))
partition_count = 0
for item in self.chunks(train_set, 20):
partition_count = partition_count + 1
with open(os.path.join(self.root, 'tiered-imagenet/compacted_datasets', 'tiered_imagenet_train_{}.pickle'.format(partition_count)), 'wb') as handle:
pickle.dump(item, handle, protocol=2)
partition_count = 0
for item in self.chunks(test_set, 20):
partition_count = partition_count + 1
with open(os.path.join(self.root, 'tiered-imagenet/compacted_datasets', 'tiered_imagenet_test_{}.pickle'.format(partition_count)), 'wb') as handle:
pickle.dump(item, handle, protocol=2)
partition_count = 0
for item in self.chunks(val_set, 20):
partition_count = partition_count + 1
with open(os.path.join(self.root, 'tiered-imagenet/compacted_datasets', 'tiered_imagenet_val_{}.pickle'.format(partition_count)), 'wb') as handle:
pickle.dump(item, handle, protocol=2)
label_encoder = {}
keys = list(train_set.keys()) + list(test_set.keys())
for id_key, key in enumerate(keys):
label_encoder[key] = id_key
with open(os.path.join(self.root, 'tiered-imagenet/compacted_datasets', 'tiered_imagenet_label_encoder.pickle'), 'wb') as handle:
pickle.dump(label_encoder, handle, protocol=2)
print('Images preprocessed')
def load_dataset(self):
print("Loading dataset")
data = {}
if self.partition == 'train':
num_partition = 18
elif self.partition == 'val':
num_partition = 5
elif self.partition == 'test':
num_partition = 8
partition_count = 0
for i in range(num_partition):
partition_count = partition_count +1
with open(os.path.join(self.root, 'tiered-imagenet/compacted_datasets', 'tiered_imagenet_{}_{}.pickle'.format(self.partition, partition_count)), 'rb') as handle:
data.update(pickle.load(handle))
# Resize images and normalize
for class_ in data:
for i in range(len(data[class_])):
image2resize = pil_image.fromarray(np.uint8(data[class_][i]))
image_resized = image2resize.resize((self.data_size[2], self.data_size[1]))
image_resized = np.array(image_resized, dtype='float32')
# Normalize
image_resized = np.transpose(image_resized, (2, 0, 1))
image_resized[0, :, :] -= 120.45 # R
image_resized[1, :, :] -= 115.74 # G
image_resized[2, :, :] -= 104.65 # B
image_resized /= 127.5
data[class_][i] = image_resized
print("Num classes " + str(len(data)))
num_images = 0
for class_ in data:
num_images += len(data[class_])
print("Num images " + str(num_images))
return data
def chunks(self, data, size=10000):
it = iter(data)
for i in range(0, len(data), size):
yield {k: data[k] for k in islice(it, size)}
def get_task_batch(self,
num_tasks=5,
num_ways=20,
num_shots=1,
num_queries=1,
seed=None):
if seed is not None:
random.seed(seed)
# init task batch data
support_data, support_label, query_data, query_label = [], [], [], []
for _ in range(num_ways * num_shots):
data = np.zeros(shape=[num_tasks] + self.data_size,
dtype='float32')
label = np.zeros(shape=[num_tasks],
dtype='float32')
support_data.append(data)
support_label.append(label)
for _ in range(num_ways * num_queries):
data = np.zeros(shape=[num_tasks] + self.data_size,
dtype='float32')
label = np.zeros(shape=[num_tasks],
dtype='float32')
query_data.append(data)
query_label.append(label)
# get full class list in dataset
full_class_list = list(self.data.keys())
# for each task
for t_idx in range(num_tasks):
# define task by sampling classes (num_ways)
task_class_list = random.sample(full_class_list, num_ways)
# for each sampled class in task
for c_idx in range(num_ways):
# sample data for support and query (num_shots + num_queries)
class_data_list = random.sample(self.data[task_class_list[c_idx]], num_shots + num_queries)
# load sample for support set
for i_idx in range(num_shots):
# set data
support_data[i_idx + c_idx * num_shots][t_idx] = class_data_list[i_idx]
support_label[i_idx + c_idx * num_shots][t_idx] = c_idx
# load sample for query set
for i_idx in range(num_queries):
query_data[i_idx + c_idx * num_queries][t_idx] = class_data_list[num_shots + i_idx]
query_label[i_idx + c_idx * num_queries][t_idx] = c_idx
# convert to tensor (num_tasks x (num_ways * (num_supports + num_queries)) x ...)
support_data = torch.stack([torch.from_numpy(data).float().to(tt.arg.device) for data in support_data], 1)
support_label = torch.stack([torch.from_numpy(label).float().to(tt.arg.device) for label in support_label], 1)
query_data = torch.stack([torch.from_numpy(data).float().to(tt.arg.device) for data in query_data], 1)
query_label = torch.stack([torch.from_numpy(label).float().to(tt.arg.device) for label in query_label], 1)
return [support_data, support_label, query_data, query_label]