| 4 | from config.config import cfg |
| 5 | |
| 6 | class MultipleDatasets(Dataset): |
| 7 | def __init__(self, |
| 8 | dbs, |
| 9 | partition, |
| 10 | make_same_len=True, |
| 11 | total_len=None, |
| 12 | verbose=False): |
| 13 | self.dbs = dbs |
| 14 | self.db_num = len(self.dbs) |
| 15 | self.max_db_data_num = max([len(db) for db in dbs]) |
| 16 | self.db_len_cumsum = np.cumsum([len(db) for db in dbs]) |
| 17 | self.make_same_len = make_same_len |
| 18 | # self.partition = partition |
| 19 | self.partition = {k: v for k, v in sorted(partition.items(), key=lambda item: item[1])} |
| 20 | self.dataset = {} |
| 21 | for db in dbs: |
| 22 | self.dataset.update({db.__class__.__name__: db}) |
| 23 | |
| 24 | if verbose: |
| 25 | print('datasets:', [len(self.dbs[i]) for i in range(self.db_num)]) |
| 26 | print( |
| 27 | f'Sample Ratio: {self.partition}') |
| 28 | |
| 29 | def __len__(self): |
| 30 | return self.max_db_data_num |
| 31 | |
| 32 | def __getitem__(self, index): |
| 33 | p = np.random.rand() |
| 34 | v = list(self.partition.values()) |
| 35 | k = list(self.partition.keys()) |
| 36 | for i,v_i in enumerate(v): |
| 37 | if p<=v_i: |
| 38 | return self.dataset[k[i]][index % len(self.dataset[k[i]])] |