-
Notifications
You must be signed in to change notification settings - Fork 0
/
selective_sampling.py
206 lines (157 loc) · 7.19 KB
/
selective_sampling.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
'''
author: Aisha Urooj
year: 2024
'''
import json
import os
import random
import math
from tqdm.auto import tqdm
class SelectiveSampling(object):
def __init__(self, ann_file):
self.ann = []
for f in ann_file:
self.ann += json.load(open(f,'r'))
print("created SelectiveSampling object.")
def __len__(self):
return len(self.ann)
def get_sort_by_groups_dict(self):
#expecting a list of items dictionaries
data = self.ann
if data is not None:
new_data_grouped = {}
for item in data:
group_key = "_".join(item['group'])
if group_key not in new_data_grouped:
new_data_grouped[group_key] = [item]
else:
new_data_grouped[group_key].append(item)
return new_data_grouped
else:
raise Exception("Error, invalid data, data=None")
return {}
def get_sorted_counter(self, list_, reverse=True):
'''
Parameters
-------------
list_: List of elements (groups for all data instances for selective sampling case)
elem in list_ is a list of strings, e.g., ['A', 'B', 'C']
For mammography reports, elem will be a list of extracted image descriptors from the text report (group),
e.g., ['scattered fibroglandular densities', 'benign calcification']
reverse: bool, default=True
The default sorting order is descending. Set reverse=False for ascending order sorting
Returns
------------
sorted_counter: A sorted counter of unique groups based on their frequency
'''
counter_ = {}
# print(list_)
for elem in list_:
# print("Elem:")
# print(elem)
elem = "_".join(elem)
if elem in counter_:
counter_[elem]+=1
else:
counter_[elem]=1
sorted_counter = sorted(counter_.items(), key=lambda x:x[1], reverse=reverse)
return sorted_counter
def get_groups_list(self, data):
'''
Parameters
-------------
data: List of data samples dictionaries
This list is expected to have the key 'group' for each data instance in this list
Returns
------------
groups: List of groups for data
'''
groups = []
if data is not None:
for dt in data:
groups.append(dt['group'])
return groups
def shuffle(self, bs=8, rare_grp_ratio=0.375, batch_shuffle=False, num_frequent_grps=20):
'''
todo: sample randomly from data for each sampled group
to do so, we need to store data grouped based on groups
add to shuffled_data till we reach the final data length
Note: this approach does not guarantee to cover all samples in each epoch
but it makes sure to shuffle the data randomly in a way that same group is not repeated in a batch,
overall, we hope that after many epochs, the model would have seen all the data.
Parameters
-------------
bs: int, default=8
batch size
rare_grp_ratio: float, default=0.375
Percentage of samples from rare groups we want to keep in the mini-batch
For a batch size of 8, we keep 3 samples from rare groups in a mini-batch, i.e.,
3/8 = 0.375
batch_shuffle: bool, default=False
Once the mini-batch is sampled using selective sampling, shuffle the samples within the mini batch
Default behavior is to set to False, our experiments show better retrieval performance of models with the default value.
num_frequent_grps: int, default=20
Value of num_frequent_grps depends on the training data distribution and is set accordingly.
For training ALBEF and MEDCLIP on our dataset, we set its value to 20 based on the empirical selection of number of groups
that we consider as frequent groups.
Returns
------------
shuffled_data2: List of shuffled data using selective sampling for mini-batches
'''
print("shuffling using selective sampling..")
boundary = math.ceil(bs * rare_grp_ratio) #boundary tells how many samples we want from less frequent groups;
#we sample bs-boundary data samples from frequent groups
data = self.ann
self.all_groups = self.get_groups_list(data=data)
grp_counter = self.get_sorted_counter(self.all_groups)
#get frequent groups
frequent_grps = [gc[0] for gc in grp_counter[:num_frequent_grps]]
remaining_grps = [gc[0] for gc in grp_counter[num_frequent_grps:]]
data_grouped = self.get_sort_by_groups_dict()
groups = list(data_grouped.keys())
shuffled_data = []
if groups is not None and data is not None:
for l in tqdm(range(int(len(data)/bs))):
sampled_grps_f = random.sample(frequent_grps, k=bs-boundary)
sampled_grps_r = random.sample(remaining_grps, k=boundary)
sampled_grps = sampled_grps_f + sampled_grps_r
batch = [random.sample(data_grouped[grp], k=1) for grp in sampled_grps]
if batch_shuffle:
random.shuffle(batch)
shuffled_data.extend(batch)
shuffled_data2 = [datum[0] for datum in shuffled_data]
self.ann = shuffled_data2
return shuffled_data2
def shuffle_all(self, bs=8):
'''
shuffle_all randomly samples groups to pick example from for a batch
and does not care about group frequency: rare vs frequent
to do so, we need to store data grouped based on groups
add to shuffled_data till we reach the final data length
Note: this approach does not guarantee to cover all samples in each epoch
but it makes sure to shuffle the data randomly in a way that same group is not repeated in a batch,
overall, we hope that after many epochs, the model would have seen all the data.
Parameters
-------------
bs: int, default=8
batch size
Returns
------------
shuffled_data2: List of shuffled data using selective sampling for mini-batches
'''
data = self.ann
self.all_groups = self.get_groups_list(data=data)
grp_counter = self.get_sorted_counter(self.all_groups)
unique_grps = [gc[0] for gc in grp_counter]
data_grouped = self.get_sort_by_groups_dict()
groups = list(data_grouped.keys())
shuffled_data = []
if groups is not None and data is not None:
for l in tqdm(range(int(len(data)/bs))):
sampled_grps = random.sample(unique_grps, k=bs)
batch = [random.sample(data_grouped[grp], k=1) for grp in sampled_grps]
shuffled_data.extend(batch)
print(len(shuffled_data))
shuffled_data2 = [datum[0] for datum in shuffled_data]
self.ann = shuffled_data2
return shuffled_data2