(args, hyperparams, loader_train, loader_test, experiment_name, output_training_path)
| 145 | |
| 146 | |
| 147 | def train(args, hyperparams, loader_train, loader_test, experiment_name, output_training_path): |
| 148 | |
| 149 | cb=create_callbacks(with_tensorboard=hyperparams.with_tensorboard,\ |
| 150 | with_visualizer=hyperparams.with_visualizer,\ |
| 151 | viewer_config_path=hyperparams.viewer_config_path,\ |
| 152 | experiment_name=experiment_name) |
| 153 | |
| 154 | #create phases |
| 155 | phases= [ |
| 156 | Phase('train', loader_train, grad=True), |
| 157 | Phase('test', loader_test, grad=False), |
| 158 | ] |
| 159 | |
| 160 | #model |
| 161 | model = StrandCodec(do_vae=hyperparams.enable_vae, |
| 162 | scale_init=hyperparams.scale_init, |
| 163 | nr_verts_per_strand=hyperparams.nr_verts_per_strand, nr_values_to_decode=hyperparams.nr_values_to_decode, |
| 164 | dim_per_value_decoded=hyperparams.dim_per_value_decoded).to(args.device) |
| 165 | model = torch.compile(model) |
| 166 | |
| 167 | #misc |
| 168 | world2local=torch.compile(World2Local()) |
| 169 | loss_computer= torch.compile(StrandVAELoss()) |
| 170 | normalization_dict=loader_train.dataset.get_normalization_data() |
| 171 | |
| 172 | |
| 173 | # #optimizer |
| 174 | optimizer = torch.optim.AdamW (model.parameters(), amsgrad=False, lr=hyperparams.lr, weight_decay=0.0) |
| 175 | lr_scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=hyperparams.nr_iters_to_train) |
| 176 | scheduler_warmup = UntunedLinearWarmup(optimizer) |
| 177 | |
| 178 | progress_bar = tqdm(range(0, hyperparams.nr_iters_to_train), desc="Training progress") |
| 179 | |
| 180 | |
| 181 | is_in_training_loop=True |
| 182 | while is_in_training_loop: |
| 183 | |
| 184 | for phase in phases: |
| 185 | model.train(phase.grad) |
| 186 | if hyperparams.enable_vae: |
| 187 | model.encoder.do_vae=phase.grad #when testing we don't do any VAE stuff and rather just predict the mean |
| 188 | |
| 189 | cb.phase_started(phase=phase) |
| 190 | cb.epoch_started(phase=phase) |
| 191 | |
| 192 | |
| 193 | #run epoch |
| 194 | for batch in iter(phase.loader): |
| 195 | cb.before_forward_pass(phase=phase) |
| 196 | |
| 197 | #progress |
| 198 | if phase.grad and phase.iter_nr%100==0 : |
| 199 | progress_bar.update(100) |
| 200 | |
| 201 | |
| 202 | #world_to_local |
| 203 | with torch.no_grad(): |
| 204 | gt_dict = prepare_gt_batch(batch, hyperparams, world2local, do_augmentation=phase.grad) |
no test coverage detected