197 Implement the standard training logic described above.
199 assert self.
model.training,
"[SimpleTrainer] model was changed to eval mode!"
200 start = time.perf_counter()
202 If your want to do something with the data, you can wrap the dataloader.
205 data_time = time.perf_counter() - start
208 If your want to do something with the heads, you can wrap the model.
212 outs = self.
model(data)
215 if isinstance(self.
model, DistributedDataParallel):
216 loss_dict = self.
model.module.losses(outs)
218 loss_dict = self.
model.losses(outs)
220 losses = sum(loss_dict.values())
222 with torch.cuda.stream(torch.cuda.Stream()):
223 metrics_dict = loss_dict
224 metrics_dict[
"data_time"] = data_time
229 If you need accumulate gradients or something similar, you can
230 wrap the optimizer with your custom `zero_grad()` method.
235 self.
scaler.scale(losses).backward()
241 If you need gradient clipping/scaling or other processing, you can
242 wrap the optimizer with your custom `step()` method.
257 metrics_dict (dict): dict of scalar metrics
260 k: v.detach().cpu().item()
if isinstance(v, torch.Tensor)
else float(v)
261 for k, v
in metrics_dict.items()
266 all_metrics_dict = comm.gather(metrics_dict)
268 if comm.is_main_process():
269 if "data_time" in all_metrics_dict[0]:
272 data_time = np.max([x.pop(
"data_time")
for x
in all_metrics_dict])
273 self.storage.put_scalar(
"data_time", data_time)
277 k: np.mean([x[k]
for x
in all_metrics_dict])
for k
in all_metrics_dict[0].keys()
279 total_losses_reduced = sum(loss
for loss
in metrics_dict.values())
281 self.storage.put_scalar(
"total_loss", total_losses_reduced)
282 if len(metrics_dict) > 1:
283 self.storage.put_scalars(**metrics_dict)