(args)
| 139 | |
| 140 | |
| 141 | def main(args): |
| 142 | |
| 143 | np.random.seed(args.seed) |
| 144 | torch.cuda.manual_seed_all(args.seed) |
| 145 | |
| 146 | if args.checkpoint_dir is not None: |
| 147 | pass |
| 148 | elif args.test_ckpt is not None: |
| 149 | # if not define the checkpoint-dir, set to the test checkpoint folder as default |
| 150 | args.checkpoint_dir = os.path.dirname(args.test_ckpt) |
| 151 | print(f'testing directory: {args.checkpoint_dir}') |
| 152 | else: |
| 153 | raise AssertionError( |
| 154 | 'Either checkpoint_dir or test_ckpt should be presented!' |
| 155 | ) |
| 156 | |
| 157 | os.makedirs(args.checkpoint_dir, exist_ok=True) |
| 158 | accelerator = Accelerator() |
| 159 | set_seed(args.seed) |
| 160 | |
| 161 | ### build datasets and dataloaders |
| 162 | datasets, dataloaders = build_dataset_func(args) |
| 163 | |
| 164 | ### build models |
| 165 | model = build_model_func(args) |
| 166 | ### set default checkpoint |
| 167 | checkpoint = None |
| 168 | |
| 169 | # testing phase |
| 170 | if args.test_only: |
| 171 | |
| 172 | try: |
| 173 | checkpoint = torch.load(args.test_ckpt, map_location=torch.device("cpu")) |
| 174 | model.load_state_dict(checkpoint["model"], strict=False) |
| 175 | except: |
| 176 | print('test the model from scratch...') |
| 177 | |
| 178 | model, dataloaders['train'], *dataloaders['test'] = accelerator.prepare( |
| 179 | model, dataloaders['train'], *dataloaders['test'] |
| 180 | ) |
| 181 | |
| 182 | for test_loader in dataloaders['test']: |
| 183 | test_loader.dataset.eval_func( |
| 184 | args, |
| 185 | -1, |
| 186 | model, |
| 187 | accelerator, |
| 188 | test_loader |
| 189 | ) |
| 190 | |
| 191 | # training phase |
| 192 | else: |
| 193 | |
| 194 | assert ( |
| 195 | args.checkpoint_dir is not None |
| 196 | ), "`--checkpoint_dir` is required to identify the directory to store the checkpoint" |
| 197 | os.makedirs(args.checkpoint_dir, exist_ok=True) |
| 198 |
no test coverage detected