(hyp, opt, device, tb_writer=None, wandb=None)
| 42 | logger.info("Install Weights & Biases for experiment logging via 'pip install wandb' (recommended)") |
| 43 | |
| 44 | def train(hyp, opt, device, tb_writer=None, wandb=None): |
| 45 | logger.info(f'Hyperparameters {hyp}') |
| 46 | save_dir, epochs, batch_size, total_batch_size, weights, rank = \ |
| 47 | Path(opt.save_dir), opt.epochs, opt.batch_size, opt.total_batch_size, opt.weights, opt.global_rank |
| 48 | |
| 49 | # Directories |
| 50 | wdir = save_dir / 'weights' |
| 51 | wdir.mkdir(parents=True, exist_ok=True) # make dir |
| 52 | last = wdir / 'last.pt' |
| 53 | best = wdir / 'best.pt' |
| 54 | results_file = save_dir / 'results.txt' |
| 55 | |
| 56 | # Save run settings |
| 57 | with open(save_dir / 'hyp.yaml', 'w') as f: |
| 58 | yaml.dump(hyp, f, sort_keys=False) |
| 59 | with open(save_dir / 'opt.yaml', 'w') as f: |
| 60 | yaml.dump(vars(opt), f, sort_keys=False) |
| 61 | |
| 62 | # Configure |
| 63 | plots = not opt.evolve # create plots |
| 64 | cuda = device.type != 'cpu' |
| 65 | init_seeds(2 + rank) |
| 66 | with open(opt.data) as f: |
| 67 | data_dict = yaml.load(f, Loader=yaml.FullLoader) # data dict |
| 68 | with torch_distributed_zero_first(rank): |
| 69 | check_dataset(data_dict) # check |
| 70 | train_path = data_dict['train'] |
| 71 | test_path = data_dict['val'] |
| 72 | nc, names = (1, ['item']) if opt.single_cls else (int(data_dict['nc']), data_dict['names']) # number classes, names |
| 73 | assert len(names) == nc, '%g names found for nc=%g dataset in %s' % (len(names), nc, opt.data) # check |
| 74 | |
| 75 | # Model |
| 76 | pretrained = weights.endswith('.pt') |
| 77 | if pretrained: |
| 78 | with torch_distributed_zero_first(rank): |
| 79 | attempt_download(weights) # download if not found locally |
| 80 | ckpt = torch.load(weights, map_location=device) # load checkpoint |
| 81 | model = Darknet(opt.cfg).to(device) # create |
| 82 | state_dict = {k: v for k, v in ckpt['model'].items() if model.state_dict()[k].numel() == v.numel()} |
| 83 | model.load_state_dict(state_dict, strict=False) |
| 84 | print('Transferred %g/%g items from %s' % (len(state_dict), len(model.state_dict()), weights)) # report |
| 85 | else: |
| 86 | model = Darknet(opt.cfg).to(device) # create |
| 87 | |
| 88 | # Optimizer |
| 89 | nbs = 64 # nominal batch size |
| 90 | accumulate = max(round(nbs / total_batch_size), 1) # accumulate loss before optimizing |
| 91 | hyp['weight_decay'] *= total_batch_size * accumulate / nbs # scale weight_decay |
| 92 | |
| 93 | pg0, pg1, pg2 = [], [], [] # optimizer parameter groups |
| 94 | for k, v in dict(model.named_parameters()).items(): |
| 95 | if '.bias' in k: |
| 96 | pg2.append(v) # biases |
| 97 | elif 'Conv2d.weight' in k: |
| 98 | pg1.append(v) # apply weight_decay |
| 99 | elif 'm.weight' in k: |
| 100 | pg1.append(v) # apply weight_decay |
| 101 | elif 'w.weight' in k: |
no test coverage detected