| 122 | print(f"{fname} saved.") |
| 123 | |
| 124 | def increment_path(path, exist_ok=False, sep='', mkdir=False): |
| 125 | # Increment file or directory path, i.e. runs/exp --> runs/exp{sep}2, runs/exp{sep}3, ... etc. |
| 126 | path = Path(path) # os-agnostic |
| 127 | if path.exists() and not exist_ok: |
| 128 | suffix = path.suffix |
| 129 | path = path.with_suffix('') |
| 130 | dirs = glob.glob(f"{path}{sep}*") # similar paths |
| 131 | matches = [re.search(rf"%s{sep}(\d+)" % path.stem, d) for d in dirs] |
| 132 | i = [int(m.groups()[0]) for m in matches if m] # indices |
| 133 | n = max(i) + 1 if i else 2 # increment number |
| 134 | path = Path(f"{path}{sep}{n}{suffix}") # update path |
| 135 | dir = path if path.suffix == '' else path.parent # directory |
| 136 | if not dir.exists() and mkdir: |
| 137 | dir.mkdir(parents=True, exist_ok=True) # make directory |
| 138 | return path |
| 139 | |
| 140 | def save_pred_fig(output_dir, output_dic, keep): |
| 141 | # im = Image.open(os.path.join(output_dir, "img.png")) |