-
Notifications
You must be signed in to change notification settings - Fork 2
/
migrate_args.py
64 lines (52 loc) · 2.14 KB
/
migrate_args.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
from argparse import ArgumentParser
import json
import os
import shutil
parser = ArgumentParser(description='Migrate old argument json files.')
parser.add_argument(
'--args_file', required=True, type=str,
help='Location of the old arguments json file which will be tested and '
'migrated to the current format.')
def main():
args = parser.parse_args()
# Load the file and check if deprecated arguments are present.
with open(args.args_file, 'r') as f:
arguments = json.load(f)
changed = False
# A single dataset is specified instead of a list of datasets.
if type(arguments['dataset_config']) == str:
for k in ['dataset_config', 'dataset_root', 'train_set']:
arguments[k] = [arguments[k]]
changed = True
# The old crop augmentation is used. Since this was only ever really used
# with quarter resolution CityScapes images, I will not make this super
# general and simply assume this is the case here.
if 'crop_augment' in arguments and arguments['crop_augment'] > 0:
arguments['fixed_crop_augment_height'] = 256 - arguments['crop_augment']
arguments['fixed_crop_augment_width'] = 512 - arguments['crop_augment']
arguments.pop('crop_augment')
changed = True
if changed:
# Make a backup copy of the old file
backup_saved = False
for i in range(100):
backup_name = args.args_file.replace(
'.json', '_bu{}.json'.format(i))
if not os.path.isfile(backup_name):
shutil.copy(args.args_file, backup_name)
print('Backup written to: {}'.format(backup_name))
backup_saved = True
break
if not backup_saved:
print('More than a 100 backups seem to exist, quiting, fix this '
'first.')
exit(1)
# Write the new file.
with open(args.args_file, 'w') as f:
json.dump(
arguments, f, ensure_ascii=False, indent=2, sort_keys=True)
else:
print('No changes needed to be made.')
if __name__ == '__main__':
main()