-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathData.py
293 lines (243 loc) · 9.45 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
from tqdm import tqdm
import random
import torchvision
import numpy as np
from PIL import Image
import torch
import torchvision.transforms as transforms
from torchnet.transform import compose
import pickle
from torchvision.datasets import CIFAR100
import torchvision.transforms as tt
from skimage.transform import rotate
from skimage.util import random_noise
from skimage.filters import gaussian
"""# MINI IMAGE NET """
def extract_episodes(n_way,n_support,n_query,n_episode,datax,datay):
labels=list(datay.keys())
res=[]
for i in range(n_episode):
K = np.random.choice(labels,n_way, replace=False)
all_xs=torch.empty((n_way,n_support,datax[0].shape[0],datax[0].shape[1],datax[0].shape[2]))
all_xq=torch.empty((n_way,n_query,datax[0].shape[0],datax[0].shape[1],datax[0].shape[2]))
i=0
for k in K :
sample_cls = np.random.permutation(datax[datay[k]])[:(n_support+n_query)]
all_xs[i]=torch.from_numpy(np.array(sample_cls[:n_support])).float()
all_xq[i]=torch.from_numpy(np.array(sample_cls[n_support:])).float()
i+=1
res.append({
'class':list(K),
'xs': all_xs,
'xq': all_xq
})
return torch.utils.data.DataLoader(res, batch_size=1, shuffle=True,collate_fn=lambda d:d[0])
def createDatasets(mode,path,hyperparams):
with open(path +'/mini-imagenet-cache-'+mode+'.pkl', 'rb') as f:
data_train = pickle.load(f)
datax_train=data_train['image_data']
datay_train=data_train['class_dict']
data_train=None
way=0
shot=0
query=0
episodes=0
if mode =='train' :
way=hyperparams['train_way']
shot=hyperparams['train_shot']
query=hyperparams['train_query']
episodes=hyperparams['train_episodes']
else :
way=hyperparams['test_way']
shot=hyperparams['test_way']
query=hyperparams['test_way']
episodes=hyperparams['test_episodes']
featurestransform = [
transforms.Resize(84),
transforms.CenterCrop(84),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
]
featurestransform = compose(featurestransform)
datax_train= list(map(lambda x: featurestransform(Image.fromarray(x)), datax_train))
datax_train = torch.stack(datax_train, 0)
return extract_episodes(way,shot,query,episodes,datax_train,datay_train)
def loadDataMiniIMageNet(path,mode,hyperparams):
return {
'train':createDatasets(mode[0],path,hyperparams) ,
'test':createDatasets(mode[1],path,hyperparams) ,
'val': createDatasets(mode[2],path,hyperparams)
}
"""# CIFAR100"""
def getImageByClassCifar(data,K):
res={}
for im in data :
if(im[1] in K):
try :
r=im[0]
res[im[1]] = np.append(res[im[1]],r.reshape((1,3,32,32)),axis=0)
except KeyError :
r=im[0]
res[im[1]] = r.reshape((1,3,32,32))
return res
def extract_episodes_cifar100(n_way,n_support,n_query,n_episode,datax,labels):
res=[]
for i in range(n_episode):
K = np.random.choice(labels,n_way, replace=False)
all_xs=torch.empty((n_way,n_support,datax[0][0].shape[0],datax[0][0].shape[1],datax[0][0].shape[2]))
all_xq=torch.empty((n_way,n_query,datax[0][0].shape[0],datax[0][0].shape[1],datax[0][0].shape[2]))
i=0
listImages=getImageByClassCifar(datax,K)
for k in K :
sample_cls = np.random.permutation(listImages[k])[:(n_support+n_query)]
all_xs[i]=torch.from_numpy(np.array(sample_cls[:n_support])).float()
all_xq[i]=torch.from_numpy(np.array(sample_cls[n_support:])).float()
i+=1
res.append({
'class':list(K),
'xs': all_xs,
'xq': all_xq
})
return torch.utils.data.DataLoader(res, batch_size=1, shuffle=True,collate_fn=lambda d:d[0])
def createDatasetsCifar(data,mode,hyperparams,labels):
way=0
shot=0
query=0
episodes=0
if mode =='train' :
way=hyperparams['train_way']
shot=hyperparams['train_shot']
query=hyperparams['train_query']
episodes=hyperparams['train_episodes']
else :
way=hyperparams['test_way']
shot=hyperparams['test_way']
query=hyperparams['test_way']
episodes=hyperparams['test_episodes']
return extract_episodes_cifar100(way,shot,query,episodes,data,labels)
def loadDataCifar(hyperparams):
stats = ((0.5074,0.4867,0.4411),(0.2011,0.1987,0.2025))
train_transform = tt.Compose([
tt.RandomHorizontalFlip(),
tt.RandomCrop(32,padding=4,padding_mode="reflect"),
tt.ToTensor(),
tt.Normalize(*stats)
])
test_transform = tt.Compose([
tt.ToTensor(),
tt.Normalize(*stats)
])
train_data = CIFAR100(download=True,root="./data",transform=train_transform)
test_data = CIFAR100(root="./data",train=False,transform=test_transform)
all_data = train_data + test_data
TRAIN_RATIO=0.64
TEST_RATIO=0.2
train_labels = np.random.choice(np.arange(100),int(100*TRAIN_RATIO), replace=False)
rest=[x for x in np.arange(100) if x not in train_labels]
test_labels = np.random.choice(rest,int(100*TEST_RATIO), replace=False)
val_labels = [x for x in np.arange(100) if x not in train_labels and x not in test_labels]
train_data = [x for x in all_data if x[1] in train_labels]
test_data = [x for x in all_data if x[1] in test_labels]
val_data = [x for x in all_data if x[1] in val_labels]
return {
'train':createDatasetsCifar(train_data,'train',hyperparams,train_labels) ,
'test':createDatasetsCifar(test_data,'test',hyperparams,test_labels) ,
'val': createDatasetsCifar(val_data,'val',hyperparams,val_labels)
}
"""# OMNIGLOT"""
def data_augmentation (dataset):
final_train_data = []
for i in tqdm(range(len(dataset))):
final_train_data.append(dataset[i])
final_train_data.append(((rotate(dataset[i][0], angle=45, mode = 'wrap')),dataset[i][1]))
final_train_data.append(((np.fliplr(dataset[i][0])),dataset[i][1]))
final_train_data.append((np.flipud((dataset[i][0])),dataset[i][1]))
final_train_data.append((random_noise(dataset[i][0],var=0.2**2),dataset[i][1]))
return final_train_data
def getImageByClassOmniglot(data,K):
res={}
for im in data :
if(im[1] in K):
try :
r=im[0]
res[im[1]] = np.append(res[im[1]],r.reshape((1,1,105,105)),axis=0)
except KeyError :
r=im[0]
res[im[1]] = r.reshape((1,1,105,105))
return res
def extract_episodes_Omniglot(n_way,n_support,n_query,n_episode,datax,listlabels):
labels=np.array(listlabels)
res=[]
for i in range(n_episode):
K = np.random.choice(labels,n_way, replace=False)
all_xs=torch.empty((n_way,n_support,datax[0][0].shape[0],datax[0][0].shape[1],datax[0][0].shape[2]))
all_xq=torch.empty((n_way,n_query,datax[0][0].shape[0],datax[0][0].shape[1],datax[0][0].shape[2]))
i=0
listImages=getImageByClassOmniglot(datax,K)
for k in K :
sample_cls = np.random.permutation(listImages[k])[:(n_support+n_query)]
all_xs[i]=torch.from_numpy(np.array(sample_cls[:n_support])).float()
all_xq[i]=torch.from_numpy(np.array(sample_cls[n_support:])).float()
i+=1
res.append({
'class':list(K),
'xs': all_xs,
'xq': all_xq
})
return torch.utils.data.DataLoader(res, batch_size=1, shuffle=True,collate_fn=lambda d:d[0])
def createDatasetsOmniglot(data,mode,hyperparams,listlabels):
way=0
shot=0
query=0
episodes=0
if mode =='train' :
way=hyperparams['train_way']
shot=hyperparams['train_shot']
query=hyperparams['train_query']
episodes=hyperparams['train_episodes']
else :
way=hyperparams['test_way']
shot=hyperparams['test_way']
query=hyperparams['test_way']
episodes=hyperparams['test_episodes']
return extract_episodes_Omniglot(way,shot,query,episodes,data,listlabels)
def loadDataOmniglot(hyperparams):
all_data = torchvision.datasets.Omniglot(
root="./data", download=True, transform=torchvision.transforms.ToTensor())
all_data=list(all_data)
NUM_LABELS = all_data[-1][1]+1
TRAIN_RATIO=0.64
TEST_RATIO=0.2
train_labels = np.random.choice(np.arange(NUM_LABELS),int(NUM_LABELS*TRAIN_RATIO), replace=False)
rest=[x for x in np.arange(NUM_LABELS) if x not in train_labels]
test_labels = np.random.choice(rest,int(NUM_LABELS*TEST_RATIO), replace=False)
val_labels = [x for x in np.arange(NUM_LABELS) if x not in train_labels and x not in test_labels]
train_data = [x for x in all_data if x[1] in train_labels]
test_data = [x for x in all_data if x[1] in test_labels]
val_data = [x for x in all_data if x[1] in val_labels]
train_data =data_augmentation(train_data)
test_data =data_augmentation(test_data)
val_data =data_augmentation(val_data)
return {
'train':createDatasetsOmniglot(train_data,'train',hyperparams,train_labels) ,
'test':createDatasetsOmniglot(test_data,'test',hyperparams,test_labels) ,
'val': createDatasetsOmniglot(val_data,'val',hyperparams,val_labels)
}
def loadTheData(numData,hyperparams,mode=None,path=None):
if (numData == 0 and (path==None or mode == None)) :
print("data not load, please insert a correct path or a correct mode !")
raise (ValueError)
else :
if (numData==0):
return loadDataMiniIMageNet(path,mode,hyperparams)
elif (numData==1) :
return loadDataCifar(hyperparams)
elif (numData==2) :
return loadDataOmniglot(hyperparams)
else :
print("data not load please insert a correct numData")
print("\t 0 for MINIIMAGENET")
print("\t 1 for CIFAR100")
print("\t 2 for OMNIGLOT")
raise (ValueError)