| 50 | |
| 51 | |
| 52 | def parse(self): |
| 53 | if not self.initialized: |
| 54 | self.initialize() |
| 55 | |
| 56 | self.opt = self.parser.parse_args() |
| 57 | |
| 58 | self.opt.is_train = self.is_train |
| 59 | |
| 60 | args = vars(self.opt) |
| 61 | if args["distributed"]: |
| 62 | init_dist('slurm') |
| 63 | rank, world_size = get_dist_info() |
| 64 | if args["distributed"]: |
| 65 | self.opt.gpu_id = range(world_size) |
| 66 | elif self.opt.gpu_id != (-1): |
| 67 | if len(self.opt.gpu_id) == 1: |
| 68 | torch.cuda.set_device(self.opt.gpu_id[0]) |
| 69 | else: |
| 70 | assert args["data_parallel"] == False |
| 71 | |
| 72 | if rank == 0: |
| 73 | print('------------ Options -------------') |
| 74 | for k, v in sorted(args.items()): |
| 75 | print('%s: %s' % (str(k), str(v))) |
| 76 | print('-------------- End ----------------') |
| 77 | if self.is_train: |
| 78 | # save to the disk |
| 79 | expr_dir = os.path.join(self.opt.checkpoints_dir, self.opt.dataset_name, self.opt.name) |
| 80 | if not os.path.exists(expr_dir): |
| 81 | os.makedirs(expr_dir) |
| 82 | file_name = os.path.join(expr_dir, 'opt.txt') |
| 83 | with open(file_name, 'wt') as opt_file: |
| 84 | opt_file.write('------------ Options -------------\n') |
| 85 | for k, v in sorted(args.items()): |
| 86 | opt_file.write('%s: %s\n' % (str(k), str(v))) |
| 87 | opt_file.write('-------------- End ----------------\n') |
| 88 | if world_size > 1: |
| 89 | dist.barrier() |
| 90 | return self.opt |