MCPcopy Create free account
hub / github.com/Closed11/Unsupervised-Image-Classification / UnifLabelSampler

Class UnifLabelSampler

UIC/sampler.py:4–35  ·  view source on GitHub ↗

Samples elements uniformely accross pseudolabels. Args: N (int): size of returned iterator. images_lists: dict of key (target), value (list of data with this target)

Source from the content-addressed store, hash-verified

2import numpy as np
3
4class UnifLabelSampler(Sampler):
5 """ Samples elements uniformely accross pseudolabels.
6 Args:
7 N (int): size of returned iterator.
8 images_lists: dict of key (target), value (list of data with this target)
9 """
10
11 def __init__(self, N, images_lists):
12 self.N = N
13 self.images_lists = images_lists
14 self.indexes = self.generate_indexes_epoch()
15
16 def generate_indexes_epoch(self):
17 size_per_pseudolabel = int(self.N / len(self.images_lists)) + 1
18 res = np.zeros(size_per_pseudolabel * len(self.images_lists))
19
20 for i in range(len(self.images_lists)):
21 indexes = np.random.choice(
22 self.images_lists[i],
23 size_per_pseudolabel,
24 replace=(len(self.images_lists[i]) <= size_per_pseudolabel)
25 )
26 res[i * size_per_pseudolabel: (i + 1) * size_per_pseudolabel] = indexes
27
28 np.random.shuffle(res)
29 return res[:self.N].astype('int')
30
31 def __iter__(self):
32 return iter(self.indexes)
33
34 def __len__(self):
35 return self.N

Callers 1

mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected