(args, runner_name=None, exp_id=None)
| 121 | |
| 122 | |
| 123 | def init_experiment(args, runner_name=None, exp_id=None): |
| 124 | |
| 125 | args.cuda = torch.cuda.is_available() |
| 126 | |
| 127 | # Get filepath of calling script |
| 128 | if runner_name is None: |
| 129 | runner_name = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))).split(".")[-2:] |
| 130 | |
| 131 | root_dir = os.path.join(args.exp_root, *runner_name) |
| 132 | |
| 133 | if not os.path.exists(root_dir): |
| 134 | os.makedirs(root_dir) |
| 135 | |
| 136 | # Either generate a unique experiment ID, or use one which is passed |
| 137 | if exp_id is None: |
| 138 | |
| 139 | # Unique identifier for experiment |
| 140 | now = '({:02d}.{:02d}.{}_|_'.format(datetime.now().day, datetime.now().month, datetime.now().year) + \ |
| 141 | datetime.now().strftime("%S.%f")[:-3] + ')' |
| 142 | |
| 143 | log_dir = os.path.join(root_dir, 'log', now) |
| 144 | while os.path.exists(log_dir): |
| 145 | now = '({:02d}.{:02d}.{}_|_'.format(datetime.now().day, datetime.now().month, datetime.now().year) + \ |
| 146 | datetime.now().strftime("%S.%f")[:-3] + ')' |
| 147 | |
| 148 | log_dir = os.path.join(root_dir, 'log', now) |
| 149 | |
| 150 | else: |
| 151 | |
| 152 | log_dir = os.path.join(root_dir, 'log', f'{exp_id}') |
| 153 | |
| 154 | if not os.path.exists(log_dir): |
| 155 | os.makedirs(log_dir) |
| 156 | args.log_dir = log_dir |
| 157 | |
| 158 | # Instantiate directory to save models to |
| 159 | model_root_dir = os.path.join(args.log_dir, 'checkpoints') |
| 160 | if not os.path.exists(model_root_dir): |
| 161 | os.mkdir(model_root_dir) |
| 162 | |
| 163 | args.model_dir = model_root_dir |
| 164 | args.model_path = os.path.join(args.model_dir, 'model.pt') |
| 165 | |
| 166 | print(f'Experiment saved to: {args.log_dir}') |
| 167 | |
| 168 | args.writer = SummaryWriter(log_dir=args.log_dir) |
| 169 | |
| 170 | hparam_dict = {} |
| 171 | |
| 172 | for k, v in vars(args).items(): |
| 173 | if isinstance(v, (int, float, str, bool, torch.Tensor)): |
| 174 | hparam_dict[k] = v |
| 175 | |
| 176 | args.writer.add_hparams(hparam_dict=hparam_dict, metric_dict={}) |
| 177 | |
| 178 | print(runner_name) |
| 179 | print(args) |
| 180 |
no outgoing calls
no test coverage detected