glenn-jocher commited on
Commit
f7bc685
1 Parent(s): c949fc8

Implement yaml.safe_load() (#2876)

Browse files

* Implement yaml.safe_load()

* yaml.safe_dump()

data/coco.yaml CHANGED
@@ -30,6 +30,6 @@ names: [ 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', '
30
 
31
  # Print classes
32
  # with open('data/coco.yaml') as f:
33
- # d = yaml.load(f, Loader=yaml.FullLoader) # dict
34
  # for i, x in enumerate(d['names']):
35
  # print(i, x)
 
30
 
31
  # Print classes
32
  # with open('data/coco.yaml') as f:
33
+ # d = yaml.safe_load(f) # dict
34
  # for i, x in enumerate(d['names']):
35
  # print(i, x)
models/yolo.py CHANGED
@@ -72,7 +72,7 @@ class Model(nn.Module):
72
  import yaml # for torch hub
73
  self.yaml_file = Path(cfg).name
74
  with open(cfg) as f:
75
- self.yaml = yaml.load(f, Loader=yaml.SafeLoader) # model dict
76
 
77
  # Define model
78
  ch = self.yaml['ch'] = self.yaml.get('ch', ch) # input channels
 
72
  import yaml # for torch hub
73
  self.yaml_file = Path(cfg).name
74
  with open(cfg) as f:
75
+ self.yaml = yaml.safe_load(f) # model dict
76
 
77
  # Define model
78
  ch = self.yaml['ch'] = self.yaml.get('ch', ch) # input channels
test.py CHANGED
@@ -71,7 +71,7 @@ def test(data,
71
  if isinstance(data, str):
72
  is_coco = data.endswith('coco.yaml')
73
  with open(data) as f:
74
- data = yaml.load(f, Loader=yaml.SafeLoader)
75
  check_dataset(data) # check
76
  nc = 1 if single_cls else int(data['nc']) # number of classes
77
  iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for mAP@0.5:0.95
 
71
  if isinstance(data, str):
72
  is_coco = data.endswith('coco.yaml')
73
  with open(data) as f:
74
+ data = yaml.safe_load(f)
75
  check_dataset(data) # check
76
  nc = 1 if single_cls else int(data['nc']) # number of classes
77
  iouv = torch.linspace(0.5, 0.95, 10).to(device) # iou vector for mAP@0.5:0.95
train.py CHANGED
@@ -41,7 +41,7 @@ logger = logging.getLogger(__name__)
41
  def train(hyp, opt, device, tb_writer=None):
42
  logger.info(colorstr('hyperparameters: ') + ', '.join(f'{k}={v}' for k, v in hyp.items()))
43
  save_dir, epochs, batch_size, total_batch_size, weights, rank = \
44
- opt.save_dir, opt.epochs, opt.batch_size, opt.total_batch_size, opt.weights, opt.global_rank
45
 
46
  # Directories
47
  wdir = save_dir / 'weights'
@@ -52,16 +52,16 @@ def train(hyp, opt, device, tb_writer=None):
52
 
53
  # Save run settings
54
  with open(save_dir / 'hyp.yaml', 'w') as f:
55
- yaml.dump(hyp, f, sort_keys=False)
56
  with open(save_dir / 'opt.yaml', 'w') as f:
57
- yaml.dump(vars(opt), f, sort_keys=False)
58
 
59
  # Configure
60
  plots = not opt.evolve # create plots
61
  cuda = device.type != 'cpu'
62
  init_seeds(2 + rank)
63
  with open(opt.data) as f:
64
- data_dict = yaml.load(f, Loader=yaml.SafeLoader) # data dict
65
  is_coco = opt.data.endswith('coco.yaml')
66
 
67
  # Logging- Doing this before checking the dataset. Might update data_dict
@@ -506,8 +506,9 @@ if __name__ == '__main__':
506
  assert os.path.isfile(ckpt), 'ERROR: --resume checkpoint does not exist'
507
  apriori = opt.global_rank, opt.local_rank
508
  with open(Path(ckpt).parent.parent / 'opt.yaml') as f:
509
- opt = argparse.Namespace(**yaml.load(f, Loader=yaml.SafeLoader)) # replace
510
- opt.cfg, opt.weights, opt.resume, opt.batch_size, opt.global_rank, opt.local_rank = '', ckpt, True, opt.total_batch_size, *apriori # reinstate
 
511
  logger.info('Resuming training from %s' % ckpt)
512
  else:
513
  # opt.hyp = opt.hyp or ('hyp.finetune.yaml' if opt.weights else 'hyp.scratch.yaml')
@@ -515,7 +516,7 @@ if __name__ == '__main__':
515
  assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified'
516
  opt.img_size.extend([opt.img_size[-1]] * (2 - len(opt.img_size))) # extend to 2 sizes (train, test)
517
  opt.name = 'evolve' if opt.evolve else opt.name
518
- opt.save_dir = increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok | opt.evolve) # increment run
519
 
520
  # DDP mode
521
  opt.total_batch_size = opt.batch_size
@@ -530,7 +531,7 @@ if __name__ == '__main__':
530
 
531
  # Hyperparameters
532
  with open(opt.hyp) as f:
533
- hyp = yaml.load(f, Loader=yaml.SafeLoader) # load hyps
534
 
535
  # Train
536
  logger.info(opt)
@@ -577,7 +578,7 @@ if __name__ == '__main__':
577
  assert opt.local_rank == -1, 'DDP mode not implemented for --evolve'
578
  opt.notest, opt.nosave = True, True # only test/save final epoch
579
  # ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
580
- yaml_file = opt.save_dir / 'hyp_evolved.yaml' # save best result here
581
  if opt.bucket:
582
  os.system('gsutil cp gs://%s/evolve.txt .' % opt.bucket) # download evolve.txt if exists
583
 
 
41
  def train(hyp, opt, device, tb_writer=None):
42
  logger.info(colorstr('hyperparameters: ') + ', '.join(f'{k}={v}' for k, v in hyp.items()))
43
  save_dir, epochs, batch_size, total_batch_size, weights, rank = \
44
+ Path(opt.save_dir), opt.epochs, opt.batch_size, opt.total_batch_size, opt.weights, opt.global_rank
45
 
46
  # Directories
47
  wdir = save_dir / 'weights'
 
52
 
53
  # Save run settings
54
  with open(save_dir / 'hyp.yaml', 'w') as f:
55
+ yaml.safe_dump(hyp, f, sort_keys=False)
56
  with open(save_dir / 'opt.yaml', 'w') as f:
57
+ yaml.safe_dump(vars(opt), f, sort_keys=False)
58
 
59
  # Configure
60
  plots = not opt.evolve # create plots
61
  cuda = device.type != 'cpu'
62
  init_seeds(2 + rank)
63
  with open(opt.data) as f:
64
+ data_dict = yaml.safe_load(f) # data dict
65
  is_coco = opt.data.endswith('coco.yaml')
66
 
67
  # Logging- Doing this before checking the dataset. Might update data_dict
 
506
  assert os.path.isfile(ckpt), 'ERROR: --resume checkpoint does not exist'
507
  apriori = opt.global_rank, opt.local_rank
508
  with open(Path(ckpt).parent.parent / 'opt.yaml') as f:
509
+ opt = argparse.Namespace(**yaml.safe_load(f)) # replace
510
+ opt.cfg, opt.weights, opt.resume, opt.batch_size, opt.global_rank, opt.local_rank = \
511
+ '', ckpt, True, opt.total_batch_size, *apriori # reinstate
512
  logger.info('Resuming training from %s' % ckpt)
513
  else:
514
  # opt.hyp = opt.hyp or ('hyp.finetune.yaml' if opt.weights else 'hyp.scratch.yaml')
 
516
  assert len(opt.cfg) or len(opt.weights), 'either --cfg or --weights must be specified'
517
  opt.img_size.extend([opt.img_size[-1]] * (2 - len(opt.img_size))) # extend to 2 sizes (train, test)
518
  opt.name = 'evolve' if opt.evolve else opt.name
519
+ opt.save_dir = str(increment_path(Path(opt.project) / opt.name, exist_ok=opt.exist_ok | opt.evolve))
520
 
521
  # DDP mode
522
  opt.total_batch_size = opt.batch_size
 
531
 
532
  # Hyperparameters
533
  with open(opt.hyp) as f:
534
+ hyp = yaml.safe_load(f) # load hyps
535
 
536
  # Train
537
  logger.info(opt)
 
578
  assert opt.local_rank == -1, 'DDP mode not implemented for --evolve'
579
  opt.notest, opt.nosave = True, True # only test/save final epoch
580
  # ei = [isinstance(x, (int, float)) for x in hyp.values()] # evolvable indices
581
+ yaml_file = Path(opt.save_dir) / 'hyp_evolved.yaml' # save best result here
582
  if opt.bucket:
583
  os.system('gsutil cp gs://%s/evolve.txt .' % opt.bucket) # download evolve.txt if exists
584
 
utils/autoanchor.py CHANGED
@@ -102,7 +102,7 @@ def kmean_anchors(path='./data/coco128.yaml', n=9, img_size=640, thr=4.0, gen=10
102
 
103
  if isinstance(path, str): # *.yaml file
104
  with open(path) as f:
105
- data_dict = yaml.load(f, Loader=yaml.SafeLoader) # model dict
106
  from utils.datasets import LoadImagesAndLabels
107
  dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
108
  else:
 
102
 
103
  if isinstance(path, str): # *.yaml file
104
  with open(path) as f:
105
+ data_dict = yaml.safe_load(f) # model dict
106
  from utils.datasets import LoadImagesAndLabels
107
  dataset = LoadImagesAndLabels(data_dict['train'], augment=True, rect=True)
108
  else:
utils/aws/resume.py CHANGED
@@ -19,7 +19,7 @@ for last in path.rglob('*/**/last.pt'):
19
 
20
  # Load opt.yaml
21
  with open(last.parent.parent / 'opt.yaml') as f:
22
- opt = yaml.load(f, Loader=yaml.SafeLoader)
23
 
24
  # Get device count
25
  d = opt['device'].split(',') # devices
 
19
 
20
  # Load opt.yaml
21
  with open(last.parent.parent / 'opt.yaml') as f:
22
+ opt = yaml.safe_load(f)
23
 
24
  # Get device count
25
  d = opt['device'].split(',') # devices
utils/general.py CHANGED
@@ -550,7 +550,7 @@ def print_mutation(hyp, results, yaml_file='hyp_evolved.yaml', bucket=''):
550
  results = tuple(x[0, :7])
551
  c = '%10.4g' * len(results) % results # results (P, R, mAP@0.5, mAP@0.5:0.95, val_losses x 3)
552
  f.write('# Hyperparameter Evolution Results\n# Generations: %g\n# Metrics: ' % len(x) + c + '\n\n')
553
- yaml.dump(hyp, f, sort_keys=False)
554
 
555
  if bucket:
556
  os.system('gsutil cp evolve.txt %s gs://%s' % (yaml_file, bucket)) # upload
 
550
  results = tuple(x[0, :7])
551
  c = '%10.4g' * len(results) % results # results (P, R, mAP@0.5, mAP@0.5:0.95, val_losses x 3)
552
  f.write('# Hyperparameter Evolution Results\n# Generations: %g\n# Metrics: ' % len(x) + c + '\n\n')
553
+ yaml.safe_dump(hyp, f, sort_keys=False)
554
 
555
  if bucket:
556
  os.system('gsutil cp evolve.txt %s gs://%s' % (yaml_file, bucket)) # upload
utils/plots.py CHANGED
@@ -323,7 +323,7 @@ def plot_labels(labels, names=(), save_dir=Path(''), loggers=None):
323
  def plot_evolution(yaml_file='data/hyp.finetune.yaml'): # from utils.plots import *; plot_evolution()
324
  # Plot hyperparameter evolution results in evolve.txt
325
  with open(yaml_file) as f:
326
- hyp = yaml.load(f, Loader=yaml.SafeLoader)
327
  x = np.loadtxt('evolve.txt', ndmin=2)
328
  f = fitness(x)
329
  # weights = (f - f.min()) ** 2 # for weighted results
 
323
  def plot_evolution(yaml_file='data/hyp.finetune.yaml'): # from utils.plots import *; plot_evolution()
324
  # Plot hyperparameter evolution results in evolve.txt
325
  with open(yaml_file) as f:
326
+ hyp = yaml.safe_load(f)
327
  x = np.loadtxt('evolve.txt', ndmin=2)
328
  f = fitness(x)
329
  # weights = (f - f.min()) ** 2 # for weighted results
utils/wandb_logging/log_dataset.py CHANGED
@@ -9,7 +9,7 @@ WANDB_ARTIFACT_PREFIX = 'wandb-artifact://'
9
 
10
  def create_dataset_artifact(opt):
11
  with open(opt.data) as f:
12
- data = yaml.load(f, Loader=yaml.SafeLoader) # data dict
13
  logger = WandbLogger(opt, '', None, data, job_type='Dataset Creation')
14
 
15
 
 
9
 
10
  def create_dataset_artifact(opt):
11
  with open(opt.data) as f:
12
+ data = yaml.safe_load(f) # data dict
13
  logger = WandbLogger(opt, '', None, data, job_type='Dataset Creation')
14
 
15
 
utils/wandb_logging/wandb_utils.py CHANGED
@@ -55,7 +55,7 @@ def check_wandb_resume(opt):
55
 
56
  def process_wandb_config_ddp_mode(opt):
57
  with open(opt.data) as f:
58
- data_dict = yaml.load(f, Loader=yaml.SafeLoader) # data dict
59
  train_dir, val_dir = None, None
60
  if isinstance(data_dict['train'], str) and data_dict['train'].startswith(WANDB_ARTIFACT_PREFIX):
61
  api = wandb.Api()
@@ -73,7 +73,7 @@ def process_wandb_config_ddp_mode(opt):
73
  if train_dir or val_dir:
74
  ddp_data_path = str(Path(val_dir) / 'wandb_local_data.yaml')
75
  with open(ddp_data_path, 'w') as f:
76
- yaml.dump(data_dict, f)
77
  opt.data = ddp_data_path
78
 
79
 
@@ -120,7 +120,7 @@ class WandbLogger():
120
  'YOLOv5' if opt.project == 'runs/train' else Path(opt.project).stem)
121
  print("Created dataset config file ", config_path)
122
  with open(config_path) as f:
123
- wandb_data_dict = yaml.load(f, Loader=yaml.SafeLoader)
124
  return wandb_data_dict
125
 
126
  def setup_training(self, opt, data_dict):
@@ -192,7 +192,7 @@ class WandbLogger():
192
 
193
  def log_dataset_artifact(self, data_file, single_cls, project, overwrite_config=False):
194
  with open(data_file) as f:
195
- data = yaml.load(f, Loader=yaml.SafeLoader) # data dict
196
  nc, names = (1, ['item']) if single_cls else (int(data['nc']), data['names'])
197
  names = {k: v for k, v in enumerate(names)} # to index dictionary
198
  self.train_artifact = self.create_dataset_table(LoadImagesAndLabels(
@@ -206,7 +206,7 @@ class WandbLogger():
206
  path = data_file if overwrite_config else '_wandb.'.join(data_file.rsplit('.', 1)) # updated data.yaml path
207
  data.pop('download', None)
208
  with open(path, 'w') as f:
209
- yaml.dump(data, f)
210
 
211
  if self.job_type == 'Training': # builds correct artifact pipeline graph
212
  self.wandb_run.use_artifact(self.val_artifact)
 
55
 
56
  def process_wandb_config_ddp_mode(opt):
57
  with open(opt.data) as f:
58
+ data_dict = yaml.safe_load(f) # data dict
59
  train_dir, val_dir = None, None
60
  if isinstance(data_dict['train'], str) and data_dict['train'].startswith(WANDB_ARTIFACT_PREFIX):
61
  api = wandb.Api()
 
73
  if train_dir or val_dir:
74
  ddp_data_path = str(Path(val_dir) / 'wandb_local_data.yaml')
75
  with open(ddp_data_path, 'w') as f:
76
+ yaml.safe_dump(data_dict, f)
77
  opt.data = ddp_data_path
78
 
79
 
 
120
  'YOLOv5' if opt.project == 'runs/train' else Path(opt.project).stem)
121
  print("Created dataset config file ", config_path)
122
  with open(config_path) as f:
123
+ wandb_data_dict = yaml.safe_load(f)
124
  return wandb_data_dict
125
 
126
  def setup_training(self, opt, data_dict):
 
192
 
193
  def log_dataset_artifact(self, data_file, single_cls, project, overwrite_config=False):
194
  with open(data_file) as f:
195
+ data = yaml.safe_load(f) # data dict
196
  nc, names = (1, ['item']) if single_cls else (int(data['nc']), data['names'])
197
  names = {k: v for k, v in enumerate(names)} # to index dictionary
198
  self.train_artifact = self.create_dataset_table(LoadImagesAndLabels(
 
206
  path = data_file if overwrite_config else '_wandb.'.join(data_file.rsplit('.', 1)) # updated data.yaml path
207
  data.pop('download', None)
208
  with open(path, 'w') as f:
209
+ yaml.safe_dump(data, f)
210
 
211
  if self.job_type == 'Training': # builds correct artifact pipeline graph
212
  self.wandb_run.use_artifact(self.val_artifact)