(self, config_path, transform, max_words=30, image_words=257, tokenizer=None,
cache_on_disk=False, rank=0)
| 19 | |
| 20 | class FinetuneDataset(Dataset): |
| 21 | def __init__(self, config_path, transform, max_words=30, image_words=257, tokenizer=None, |
| 22 | cache_on_disk=False, rank=0): |
| 23 | |
| 24 | print(f"read dataset config from {config_path}") |
| 25 | with open(config_path, 'r') as f: |
| 26 | self.config = yaml.load(f, Loader=yaml.FullLoader) |
| 27 | print("DATASET CONFIG:") |
| 28 | print(self.config) |
| 29 | |
| 30 | |
| 31 | self.cache_on_disk = cache_on_disk |
| 32 | if cache_on_disk: |
| 33 | # save data items on disk to avoid duplicating annotations in each rank, |
| 34 | # which could cause a hugh waste of CPU memory |
| 35 | config_identifier = config_path |
| 36 | disallowed_chars = ['/', '\\', '.', '?', '!'] |
| 37 | for _ in disallowed_chars: |
| 38 | config_identifier = config_identifier.replace(_, '-') |
| 39 | self.cache_dir = f"./accessory_data_cache/{config_identifier}" |
| 40 | if rank == 0: |
| 41 | Path(self.cache_dir).mkdir(parents=True, exist_ok=True) |
| 42 | else: |
| 43 | self.cache_dir = None |
| 44 | |
| 45 | |
| 46 | # determine if the dataset need to collect annotations from meta files in self.config |
| 47 | # the collection is needed when: |
| 48 | # - |
| 49 | # cache_on_disk is False, so every rank collects and stores the annotations independently, OR |
| 50 | # - |
| 51 | # cache_on_disk is true & rank == 0 & no off-the-shelf annotation cache, e.g. those created by |
| 52 | # prior experiments and runs, exists. |
| 53 | if not cache_on_disk: |
| 54 | need_collect_anno = True |
| 55 | else: |
| 56 | if rank != 0 : |
| 57 | need_collect_anno = False |
| 58 | else: |
| 59 | if (Path(self.cache_dir)/'data.h5').exists() and (Path(self.cache_dir)/'ready').exists(): |
| 60 | need_collect_anno = False # off-the-shelf annotation cache exists |
| 61 | print(f"Use existing h5 data cache: {Path(self.cache_dir)}\n" |
| 62 | f"Note: if the actual data defined by {config_path} has changed since your last run, " |
| 63 | f"please delete the cache manually and re-run this expeirment, or the data actually used " |
| 64 | f"will not be updated") |
| 65 | else: |
| 66 | need_collect_anno = True |
| 67 | |
| 68 | |
| 69 | if need_collect_anno: |
| 70 | group_ann = {} |
| 71 | for meta in self.config['META']: |
| 72 | meta_path, meta_type = meta['path'], meta['type'] |
| 73 | meta_ext = os.path.splitext(meta_path)[-1] |
| 74 | # read data meta file |
| 75 | # meta_l should finally be a list of data items, and each data item should be a dict |
| 76 | if meta_ext == ".json": |
| 77 | with open(meta_path) as f: |
| 78 | meta_l = json.load(f) |
nothing calls this directly
no test coverage detected