Sampling scheme from replay buffer in operational memory.
| 4 | |
| 5 | |
| 6 | class SampleScheme(object): |
| 7 | """ |
| 8 | Sampling scheme from replay buffer in operational memory. |
| 9 | """ |
| 10 | valid_sample_qi_modes = ['rnd'] |
| 11 | |
| 12 | def __init__(self, net, sample_qi_mode='rnd'): |
| 13 | """ |
| 14 | :param sample_qi_mode: input memory queue update mode |
| 15 | """ |
| 16 | assert sample_qi_mode in self.valid_sample_qi_modes, "{} not in {}".format( |
| 17 | sample_qi_mode, self.valid_sample_qi_modes) |
| 18 | self.net = net |
| 19 | self.mode = sample_qi_mode |
| 20 | self.plot = False |
| 21 | |
| 22 | if sample_qi_mode == 'rnd': |
| 23 | self.qi_update = self.sample_random |
| 24 | else: |
| 25 | raise NotImplementedError() |
| 26 | |
| 27 | self.transform_train = None |
| 28 | self.resample = False |
| 29 | |
| 30 | def __call__(self, labels, n_samples, input_shape): |
| 31 | with torch.no_grad(): |
| 32 | return self.qi_update(labels, n_samples, input_shape) |
| 33 | |
| 34 | def sample_random(self, labels, n_samples, input_shape): |
| 35 | """ |
| 36 | Sample random class, then random from its memory. |
| 37 | No resampling. |
| 38 | """ |
| 39 | # Determine how many mem-samples available |
| 40 | q_total_cnt = 0 |
| 41 | free_q = {} # idxs of which ones are free in mem queue |
| 42 | classes = [] |
| 43 | for c, mem in self.net.class_mem.items(): |
| 44 | mem_cnt = mem.qi.shape[0] # Mem cnt |
| 45 | free_q[c] = list(range(0, mem_cnt)) |
| 46 | q_total_cnt += len(free_q[c]) |
| 47 | classes.append(c) |
| 48 | |
| 49 | # Randomly sample how many samples to idx per class |
| 50 | free_c = copy.deepcopy(classes) |
| 51 | tot_sample_cnt = 0 |
| 52 | sample_cnt = {c: 0 for c in classes} # How many sampled already |
| 53 | sample_max = n_samples if q_total_cnt > n_samples else q_total_cnt # How many to sample (equally divided) |
| 54 | while tot_sample_cnt < sample_max: |
| 55 | c_idx = random.randrange(len(free_c)) |
| 56 | c = free_c[c_idx] |
| 57 | |
| 58 | if sample_cnt[c] >= len(self.net.class_mem[c].qi): # No more memories to sample |
| 59 | free_c.remove(c) |
| 60 | continue |
| 61 | sample_cnt[c] += 1 |
| 62 | tot_sample_cnt += 1 |
| 63 |