-
Notifications
You must be signed in to change notification settings - Fork 4
/
FSLTask.py
298 lines (258 loc) · 13.4 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
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
import os
import pickle
import numpy as np
import torch
import sys
from pprint import pprint
# from tqdm import tqdm
nc = 34
# ========================================================
# Usefull paths
_datasetFeaturesFiles = {"train": "/cvhci/data/activity/kpeng/logits_split0_chunk90_swin_base_last_logits768_last22train.pkl",
#"train": "/cvhci/data/activity/kpeng/logits_split1_chunk90_swin_base_last_logits768_last_augmentedtrain.pkl",
#"train": "/cvhci/data/activity/kpeng/logits_split2_chunk90_swin_base_last_logits768_lasttrain.pkl",
#"train": "/cvhci/data/activity/kpeng/task_level_logits_split0_chunk90_swin_base_last_logits768_lasttrain.pkl",
#"train": "/cvhci/data/activity/kpeng/object_level_logits_split2_chunk90_swin_base_last_logits768_lasttrain.pkl",
#"train": "/cvhci/data/activity/kpeng/location_level_logits_split1_chunk90_swin_base_last_logits768_lasttrain.pkl",
"train_aug": "/cvhci/data/activity/kpeng/logits_split0_chunk90_swin_base_last_logits768_last22augmentedtrain",
#"train_aug": "/cvhci/data/activity/kpeng/logits_split1_chunk90_swin_base_last_logits768_lastaugmentedrealtrain.pkl",
#"train_aug": "/cvhci/data/activity/kpeng/logits_split2_chunk90_swin_base_last_logits768_last_augmentedtrain.pkl",
#"train_aug": "/cvhci/data/activity/kpeng/task_level_logits_split0_chunk90_swin_base_last_logits768_lastaugmentedtrain.pkl",
#"train_aug": "/cvhci/data/activity/kpeng/object_level_logits_split2_chunk90_swin_base_last_logits768_lastaugmentedtrain.pkl",
#"train_aug": "/cvhci/data/activity/kpeng/location_level_logits_split1_chunk90_swin_base_last_logits768_lastaugmentedtrain.pkl",
"eval": "/cvhci/data/activity/kpeng/logits_split0_chunk90_swin_base_last_logits768_last22val.pkl",
#"eval": "/cvhci/data/activity/kpeng/logits_split2_chunk90_swin_base_last_logits768_lastval.pkl",
#"eval": "/cvhci/data/activity/kpeng/logits_split1_chunk90_swin_base_last_logits768_lastaugmentedval.pkl",
#"eval": "/cvhci/data/activity/kpeng/task_level_logits_split0_chunk90_swin_base_last_logits768_lastval.pkl",
#"test": "/cvhci/data/activity/kpeng/logits_split0_chunk90_swin_base_last_logits768_last_ids2test.pkl",
#"eval": "/cvhci/data/activity/kpeng/object_level_logits_split2_chunk90_swin_base_last_logits768_lastval.pkl",
#"eval": "/cvhci/data/activity/kpeng/location_level_logits_split1_chunk90_swin_base_last_logits768_lastval.pkl",
"test": "/cvhci/data/activity/kpeng/logits_split0_chunk90_swin_base_last_logits768_last22test.pkl"
#"test": "/cvhci/data/activity/kpeng/logits_split1_chunk90_swin_base_last_logits768_lastaugmentedtest.pkl"
#"test": "/cvhci/data/activity/kpeng/task_level_logits_split0_chunk90_swin_base_last_logits768_lasttest.pkl"
#"test": "/cvhci/data/activity/kpeng/object_level_logits_split2_chunk90_swin_base_last_logits768_lasttest.pkl",
#"test": "/cvhci/data/activity/kpeng/location_level_logits_split1_chunk90_swin_base_last_logits768_lasttest.pkl",
}
_cacheDir = "./cache"
_maxRuns = 10000
_min_examples = -1
# ========================================================
# Module internal functions and variables
_randStates = None
_rsCfg = None
def load_label_feature(item):
#f = open("/cvhci/data/activity/Drive&Act/kunyu/annotation_list.pkl", 'rb')
f = open("/cvhci/data/activity/Drive&Act/kunyu/annotation_list.pkl", 'rb')
annotation = []
class_index = pickle.load(f)
print(class_index)
f.close()
infos = item.keys()
class_label = []
for info in infos:
info = ''.join([item[0] for item in list(info)])
#print(info)
activity = info.split(',')[-2]
if activity not in class_label:
class_label.append(activity)
#print(activity)
label = class_index.index(activity)
annotation.append(label)
features = item.values()
#print(features)
print(class_label)
feature = [term for term in features]
#print(feature)
return feature, annotation
def _load_pickle(file_train,file_train_aug,file_eval,file_test):
dataset = dict()
with open(file_train, 'rb') as f:
data = pickle.load(f)
feature, annotation = load_label_feature(data)
#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['data_train'] = torch.FloatTensor(np.stack(feature, axis=0))
#print(dataset['data_train'].size())
dataset['labels_train'] = torch.LongTensor(np.stack(annotation, axis=0))
with open(file_train_aug, 'rb') as f:
data = pickle.load(f)
feature, annotation = load_label_feature(data)
# 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['data_train_aug'] = torch.FloatTensor(np.stack(feature, axis=0))
# print(dataset['data_train'].size())
dataset['labels_train_aug'] = torch.LongTensor(np.stack(annotation, axis=0))
with open(file_eval, 'rb') as f:
data = pickle.load(f)
feature, annotation = load_label_feature(data)
#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['data_eval'] = torch.FloatTensor(np.stack(feature, axis=0))
dataset['labels_eval'] = torch.LongTensor(np.stack(annotation, axis=0))
#print(dataset['labels_eval'])
#sys.exit()
with open(file_test, 'rb') as f:
data = pickle.load(f)
feature, annotation = load_label_feature(data)
#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['data_test'] = torch.FloatTensor(np.stack(feature, axis=0))
dataset['labels_test'] = torch.LongTensor(np.stack(annotation,axis=0))
return dataset
def calculate_samples_per_class(annotation):
class_index_max = torch.max(annotation)+1
number_per_class = torch.zeros(class_index_max)
for i in range(class_index_max):
number_per_class[i] = (annotation == i).sum()
return number_per_class
def arrange_dataset(data, annotation):
class_index_max = torch.max(annotation)+1
arranged_dataset = []
for i in range(class_index_max):
mask = annotation == i
arranged_dataset.append(data[mask,:,:])
return arranged_dataset
def rare_class_selection(data, sam_number):
#print(data.size())
rare_class_threshold = 100
annotation = torch.arange(nc)
print(sam_number)
rare_mask = (sam_number < rare_class_threshold).bool()
rich_mask = sam_number >= rare_class_threshold
rare_mask_list = [int(item) for item in annotation[rare_mask].tolist()]
#print(rare_mask_list)
#print(data)
rare_data = [data[i].squeeze() for i in rare_mask_list]
rare_classes = annotation[rare_mask]
#rich_data = data[rich_mask, :,:]
rich_classes = annotation[rich_mask]
rich_mask_list = [int(item) for item in annotation[rich_mask].tolist()]
#print(rich_mask_list)
# print(data)
rich_data = [data[i].squeeze() for i in rich_mask_list]
return rare_data, rare_classes, rich_data, rich_classes
def load_dataset_driveact():
train_path = _datasetFeaturesFiles['train']
train_aug_path = _datasetFeaturesFiles['train_aug']
val_path= _datasetFeaturesFiles['eval']
test_path = _datasetFeaturesFiles['test']
#load_train_dataset
dataset = _load_pickle(train_path,train_aug_path, val_path, test_path)
feature_train = dataset['data_train']
annotation_train = dataset['labels_train']
feature_val, annotation_val = dataset['data_eval'], dataset['labels_eval']
feature_test, annotation_test = dataset['data_test'], dataset['labels_test']
feature_train_aug, annotation_train_aug = dataset['data_train_aug'], dataset['labels_train_aug']
#print(feature_train-feature_train_aug)
#sys.exit()
sample_per_class_train = calculate_samples_per_class(annotation_train_aug)
sample_per_class_train_aug = calculate_samples_per_class(annotation_train)
arrange_dataset_train = arrange_dataset(feature_train, annotation_train)
arrange_dataset_train_aug = arrange_dataset(feature_train_aug, annotation_train_aug)
rare_data, rare_classes, rich_data, rich_classes = rare_class_selection(arrange_dataset_train, sample_per_class_train)
rare_data_aug, rare_classes_aug, rich_data_aug, rich_classes_aug = rare_class_selection(arrange_dataset_train_aug, sample_per_class_train_aug)
return rare_data, rare_classes, rich_data, rich_classes, feature_val,annotation_val,feature_test, annotation_test, sample_per_class_train,rare_data_aug, rare_classes_aug, rich_data_aug, rich_classes_aug,sample_per_class_train_aug
# =========================================================
# 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())