MCPcopy Create free account
hub / github.com/lingeringlight/START / SeqDomainSampler

Class SeqDomainSampler

data/samplers.py:117–166  ·  view source on GitHub ↗

Sequential domain sampler, which randomly samples K images from each domain to form a minibatch. Args: data_source (list): list of Datums. batch_size (int): batch size.

Source from the content-addressed store, hash-verified

115
116
117class SeqDomainSampler(Sampler):
118 """Sequential domain sampler, which randomly samples K
119 images from each domain to form a minibatch.
120 Args:
121 data_source (list): list of Datums.
122 batch_size (int): batch size.
123 """
124
125 def __init__(self, data_source, batch_size):
126 self.data_source = data_source
127
128 # Keep track of image indices for each domain
129 self.domain_dict = defaultdict(list)
130 for i, item in enumerate(data_source):
131 self.domain_dict[item.domain].append(i)
132 self.domains = list(self.domain_dict.keys())
133 self.domains.sort()
134
135 # Make sure each domain has equal number of images
136 n_domain = len(self.domains)
137 assert batch_size % n_domain == 0
138 self.n_img_per_domain = batch_size // n_domain
139
140 self.batch_size = batch_size
141 # n_domain denotes number of domains sampled in a minibatch
142 self.n_domain = n_domain
143 self.length = len(list(self.__iter__()))
144
145 def __iter__(self):
146 domain_dict = copy.deepcopy(self.domain_dict)
147 final_idxs = []
148 stop_sampling = False
149
150 while not stop_sampling:
151 for domain in self.domains:
152 idxs = domain_dict[domain]
153 selected_idxs = random.sample(idxs, self.n_img_per_domain)
154 final_idxs.extend(selected_idxs)
155
156 for idx in selected_idxs:
157 domain_dict[domain].remove(idx)
158
159 remaining = len(domain_dict[domain])
160 if remaining < self.n_img_per_domain:
161 stop_sampling = True
162
163 return iter(final_idxs)
164
165 def __len__(self):
166 return self.length
167
168
169class RandomClassSampler(Sampler):

Callers 1

build_samplerFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…