-
Notifications
You must be signed in to change notification settings - Fork 3
/
dataset.py
355 lines (267 loc) · 8.83 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
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
348
349
350
351
352
353
354
355
import os
import os.path as path
import json
import torch
import torch.utils.data as data
import numpy as np
import random
from PIL import Image
import pdb
import csv
import glob
import pandas as pd
def pil_loader(path):
# open path as file to avoid ResourceWarning (https://github.com/python-pillow/Pillow/issues/835)
with open(path, 'rb') as f:
with Image.open(f) as img:
return img.convert('RGB')
# return img.convert('L')
def accimage_loader(path):
import accimage
try:
return accimage.Image(path)
except IOError:
# Potentially a decoding problem, fall back to PIL.Image
return pil_loader(path)
def gray_loader(path):
with open(path, 'rb') as f:
with Image.open(f) as img:
return img.convert('P')
def default_loader(path):
from torchvision import get_image_backend
if get_image_backend() == 'accimage':
return accimage_loader(path)
else:
return pil_loader(path)
def find_classes(dir):
classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]
classes.sort()
class_to_idx = {classes[i]: i for i in range(len(classes))}
return classes, class_to_idx
class Imagefolder_csv(object):
"""
Imagefolder for miniImageNet--ravi, StanfordDog, StanfordCar and CubBird datasets.
Images are stored in the folder of "images";
Indexes are stored in the CSV files.
"""
def __init__(self, data_dir="", mode="train", image_size=84, data_name="miniImageNet",
transform=None, loader=default_loader, gray_loader=gray_loader,
episode_num=1000, way_num=5, shot_num=5, query_num=5):
super(Imagefolder_csv, self).__init__()
# set the paths of the csv files
train_csv = os.path.join(data_dir, 'train.csv')
val_csv = os.path.join(data_dir, 'val.csv')
test_csv = os.path.join(data_dir, 'test.csv')
data_list = []
e = 0
if mode == "train":
# store all the classes and images into a dict
class_img_dict = {}
with open(train_csv) as f_csv:
f_train = csv.reader(f_csv, delimiter=',')
for row in f_train:
if f_train.line_num == 1:
continue
img_name, img_class = row
if img_class in class_img_dict:
class_img_dict[img_class].append(img_name)
else:
class_img_dict[img_class]=[]
class_img_dict[img_class].append(img_name)
f_csv.close()
class_list = class_img_dict.keys()
while e < episode_num:
# construct each episode
episode = []
e += 1
temp_list = random.sample(class_list, way_num)
label_num = -1
for item in temp_list:
label_num += 1
imgs_set = class_img_dict[item]
support_imgs = random.sample(imgs_set, shot_num)
query_imgs = [val for val in imgs_set if val not in support_imgs]
if query_num < len(query_imgs):
query_imgs = random.sample(query_imgs, query_num)
# the dir of support set
query_dir = [path.join(data_dir, 'images', i) for i in query_imgs]
support_dir = [path.join(data_dir, 'images', i) for i in support_imgs]
data_files = {
"query_img": query_dir,
"support_set": support_dir,
"target": label_num
}
episode.append(data_files)
data_list.append(episode)
elif mode == "val":
# store all the classes and images into a dict
class_img_dict = {}
with open(val_csv) as f_csv:
f_val = csv.reader(f_csv, delimiter=',')
for row in f_val:
if f_val.line_num == 1:
continue
img_name, img_class = row
if img_class in class_img_dict:
class_img_dict[img_class].append(img_name)
else:
class_img_dict[img_class]=[]
class_img_dict[img_class].append(img_name)
f_csv.close()
class_list = class_img_dict.keys()
while e < episode_num: # setting the episode number to 600
# construct each episode
episode = []
e += 1
temp_list = random.sample(class_list, way_num)
label_num = -1
for item in temp_list:
label_num += 1
imgs_set = class_img_dict[item]
support_imgs = random.sample(imgs_set, shot_num)
query_imgs = [val for val in imgs_set if val not in support_imgs]
if query_num<len(query_imgs):
query_imgs = random.sample(query_imgs, query_num)
# the dir of support set
query_dir = [path.join(data_dir, 'images', i) for i in query_imgs]
support_dir = [path.join(data_dir, 'images', i) for i in support_imgs]
data_files = {
"query_img": query_dir,
"support_set": support_dir,
"target": label_num
}
episode.append(data_files)
data_list.append(episode)
else:
# store all the classes and images into a dict
class_img_dict = {}
with open(test_csv) as f_csv:
f_test = csv.reader(f_csv, delimiter=',')
for row in f_test:
if f_test.line_num == 1:
continue
img_name, img_class = row
if img_class in class_img_dict:
class_img_dict[img_class].append(img_name)
else:
class_img_dict[img_class]=[]
class_img_dict[img_class].append(img_name)
f_csv.close()
class_list = class_img_dict.keys()
while e < episode_num: # setting the episode number to 600
# construct each episode
episode = []
e += 1
temp_list = random.sample(class_list, way_num)
label_num = -1
for item in temp_list:
label_num += 1
imgs_set = class_img_dict[item]
support_imgs = random.sample(imgs_set, shot_num)
query_imgs = [val for val in imgs_set if val not in support_imgs]
if query_num<len(query_imgs):
query_imgs = random.sample(query_imgs, query_num)
# the dir of support set
query_dir = [path.join(data_dir, 'images', i) for i in query_imgs]
support_dir = [path.join(data_dir, 'images', i) for i in support_imgs]
data_files = {
"query_img": query_dir,
"support_set": support_dir,
"target": label_num
}
episode.append(data_files)
data_list.append(episode)
self.data_list = data_list
self.image_size = image_size
self.transform = transform
self.loader = loader
self.gray_loader = gray_loader
def __len__(self):
return len(self.data_list)
def __getitem__(self, index):
'''
Load an episode each time, including C-way K-shot and Q-query
'''
image_size = self.image_size
episode_files = self.data_list[index]
query_images = []
query_targets = []
support_images = []
support_targets = []
for i in range(len(episode_files)):
data_files = episode_files[i]
# load query images
query_dir = data_files['query_img']
for j in range(len(query_dir)):
temp_img = self.loader(query_dir[j])
# Normalization
if self.transform is not None:
temp_img = self.transform(temp_img)
query_images.append(temp_img)
# load support images
temp_support = []
support_dir = data_files['support_set']
for j in range(len(support_dir)):
temp_img = self.loader(support_dir[j])
# Normalization
if self.transform is not None:
temp_img = self.transform(temp_img)
temp_support.append(temp_img)
support_images.append(temp_support)
# read the label
target = data_files['target']
query_targets.extend(np.tile(target, len(query_dir)))
support_targets.extend(np.tile(target, len(support_dir)))
# Shuffle the query images
# rand_num = torch.rand(1)
# random.Random(rand_num).shuffle(query_images)
# random.Random(rand_num).shuffle(query_targets)
return (query_images, query_targets, support_images, support_targets)
def read_from_csv(csv_path):
class_img_dict = {}
with open(csv_path) as f_csv:
f_reader = csv.reader(f_csv, delimiter=',')
for row in f_reader:
if f_reader.line_num == 1:
continue
img_name, img_class = row
if img_class in class_img_dict:
class_img_dict[img_class].append(img_name)
else:
class_img_dict[img_class]=[]
class_img_dict[img_class].append(img_name)
f_csv.close()
return class_img_dict
def create_data_list(support_csv_path, query_csv_path,
episode_num, way_num, shot_num, query_num):
# store all the classes and images into a dict
support_class_img_dict = read_from_csv(support_csv_path)
query_class_img_dict = read_from_csv(query_csv_path)
class_list = support_class_img_dict.keys()
data_list = []
e = 0
while e < episode_num:
# construct each episode
episode = []
e += 1
temp_list = random.sample(class_list, way_num)
label_num = -1
for item in temp_list:
label_num += 1
support_imgs = random.sample(support_class_img_dict[item], shot_num)
query_imgs = query_class_img_dict[item]
if query_num < len(query_imgs):
query_imgs = random.sample(query_imgs, query_num)
# the dir of support set
# query_dir = [path.join(data_dir, 'images', i) for i in query_imgs]
# support_dir = [path.join(data_dir, 'images', i) for i in support_imgs]
query_dir = query_imgs
support_dir = support_imgs
data_files = {
"query_img": query_dir,
"support_set": support_dir,
"target": label_num
}
episode.append(data_files)
data_list.append(episode)
return data_list