-
Notifications
You must be signed in to change notification settings - Fork 34
/
FSLTask.py
164 lines (128 loc) · 5.33 KB
/
FSLTask.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
import os
import pickle
import numpy as np
import torch
# from tqdm import tqdm
# ========================================================
# Usefull paths
_datasetFeaturesFiles = {"miniimagenet": "./checkpoints/miniImagenet/WideResNet28_10_S2M2_R/last/output.plk",
"cub": "./checkpoints/CUB/WideResNet28_10_S2M2_R/last/output.plk",
"cifar": "./checkpoints/cifar/WideResNet28_10_S2M2_R/last/output.plk",
"cross": "./checkpoints/cross/WideResNet28_10_S2M2_R/last/output.plk"}
_cacheDir = "./cache"
_maxRuns = 10000
_min_examples = -1
# ========================================================
# Module internal functions and variables
_randStates = None
_rsCfg = None
def _load_pickle(file):
with open(file, 'rb') as f:
data = pickle.load(f)
labels = [np.full(shape=len(data[key]), fill_value=key)
for key in data]
data = [features for key in data for features in data[key]]
dataset = dict()
dataset['data'] = torch.FloatTensor(np.stack(data, axis=0))
dataset['labels'] = torch.LongTensor(np.concatenate(labels))
return dataset
# =========================================================
# Callable variables and functions from outside the module
data = None
labels = None
dsName = None
def loadDataSet(dsname):
if dsname not in _datasetFeaturesFiles:
raise NameError('Unknwown dataset: {}'.format(dsname))
global dsName, data, labels, _randStates, _rsCfg, _min_examples
dsName = dsname
_randStates = None
_rsCfg = None
# Loading data from files on computer
# home = expanduser("~")
dataset = _load_pickle(_datasetFeaturesFiles[dsname])
# Computing the number of items per class in the dataset
_min_examples = dataset["labels"].shape[0]
for i in range(dataset["labels"].shape[0]):
if torch.where(dataset["labels"] == dataset["labels"][i])[0].shape[0] > 0:
_min_examples = min(_min_examples, torch.where(
dataset["labels"] == dataset["labels"][i])[0].shape[0])
print("Guaranteed number of items per class: {:d}\n".format(_min_examples))
# Generating data tensors
data = torch.zeros((0, _min_examples, dataset["data"].shape[1]))
labels = dataset["labels"].clone()
while labels.shape[0] > 0:
indices = torch.where(dataset["labels"] == labels[0])[0]
data = torch.cat([data, dataset["data"][indices, :]
[:_min_examples].view(1, _min_examples, -1)], dim=0)
indices = torch.where(labels != labels[0])[0]
labels = labels[indices]
print("Total of {:d} classes, {:d} elements each, with dimension {:d}\n".format(
data.shape[0], data.shape[1], data.shape[2]))
def GenerateRun(iRun, cfg, regenRState=False, generate=True):
global _randStates, data, _min_examples
if not regenRState:
np.random.set_state(_randStates[iRun])
classes = np.random.permutation(np.arange(data.shape[0]))[:cfg["ways"]]
shuffle_indices = np.arange(_min_examples)
dataset = None
if generate:
dataset = torch.zeros(
(cfg['ways'], cfg['shot']+cfg['queries'], data.shape[2]))
for i in range(cfg['ways']):
shuffle_indices = np.random.permutation(shuffle_indices)
if generate:
dataset[i] = data[classes[i], shuffle_indices,
:][:cfg['shot']+cfg['queries']]
return dataset
def ClassesInRun(iRun, cfg):
global _randStates, data
np.random.set_state(_randStates[iRun])
classes = np.random.permutation(np.arange(data.shape[0]))[:cfg["ways"]]
return classes
def setRandomStates(cfg):
global _randStates, _maxRuns, _rsCfg
if _rsCfg == cfg:
return
rsFile = os.path.join(_cacheDir, "RandStates_{}_s{}_q{}_w{}".format(
dsName, cfg['shot'], cfg['queries'], cfg['ways']))
if not os.path.exists(rsFile):
print("{} does not exist, regenerating it...".format(rsFile))
np.random.seed(0)
_randStates = []
for iRun in range(_maxRuns):
_randStates.append(np.random.get_state())
GenerateRun(iRun, cfg, regenRState=True, generate=False)
torch.save(_randStates, rsFile)
else:
print("reloading random states from file....")
_randStates = torch.load(rsFile)
_rsCfg = cfg
def GenerateRunSet(start=None, end=None, cfg=None):
global dataset, _maxRuns
if start is None:
start = 0
if end is None:
end = _maxRuns
if cfg is None:
cfg = {"shot": 1, "ways": 5, "queries": 15}
setRandomStates(cfg)
print("generating task from {} to {}".format(start, end))
dataset = torch.zeros(
(end-start, cfg['ways'], cfg['shot']+cfg['queries'], data.shape[2]))
for iRun in range(end-start):
dataset[iRun] = GenerateRun(start+iRun, cfg)
return dataset
# define a main code to test this module
if __name__ == "__main__":
print("Testing Task loader for Few Shot Learning")
loadDataSet('miniimagenet')
cfg = {"shot": 1, "ways": 5, "queries": 15}
setRandomStates(cfg)
run10 = GenerateRun(10, cfg)
print("First call:", run10[:2, :2, :2])
run10 = GenerateRun(10, cfg)
print("Second call:", run10[:2, :2, :2])
ds = GenerateRunSet(start=2, end=12, cfg=cfg)
print("Third call:", ds[8, :2, :2, :2])
print(ds.size())