Evaluate the model on the val set. Args: val_loader (loader): data loader to provide validation data. model (model): model to evaluate the performance. loss_scaler (scaler): scaler for loss. val_meter (ValMeter): meter instance to record and calculate the met
(val_loader, model, val_meter, loss_scaler, cur_epoch, cfg, writer=None)
| 188 | |
| 189 | @torch.no_grad() |
| 190 | def eval_epoch(val_loader, model, val_meter, loss_scaler, cur_epoch, cfg, writer=None): |
| 191 | """ |
| 192 | Evaluate the model on the val set. |
| 193 | Args: |
| 194 | val_loader (loader): data loader to provide validation data. |
| 195 | model (model): model to evaluate the performance. |
| 196 | loss_scaler (scaler): scaler for loss. |
| 197 | val_meter (ValMeter): meter instance to record and calculate the metrics. |
| 198 | cur_epoch (int): number of the current epoch of training. |
| 199 | cfg (CfgNode): configs. Details can be found in |
| 200 | slowfast/config/defaults.py |
| 201 | writer (TensorboardWriter, optional): TensorboardWriter object |
| 202 | to writer Tensorboard log. |
| 203 | """ |
| 204 | |
| 205 | # Evaluation mode enabled. The running stats would not be updated. |
| 206 | model.eval() |
| 207 | val_meter.iter_tic() |
| 208 | |
| 209 | for cur_iter, (inputs, labels, _, meta) in enumerate(val_loader): |
| 210 | if cfg.NUM_GPUS: |
| 211 | # Transferthe data to the current GPU device. |
| 212 | if isinstance(inputs, (list,)): |
| 213 | for i in range(len(inputs)): |
| 214 | inputs[i] = inputs[i].cuda(non_blocking=True) |
| 215 | else: |
| 216 | inputs = inputs.cuda(non_blocking=True) |
| 217 | labels = labels.cuda() |
| 218 | for key, val in meta.items(): |
| 219 | if isinstance(val, (list,)): |
| 220 | for i in range(len(val)): |
| 221 | val[i] = val[i].cuda(non_blocking=True) |
| 222 | else: |
| 223 | meta[key] = val.cuda(non_blocking=True) |
| 224 | val_meter.data_toc() |
| 225 | |
| 226 | if cfg.DETECTION.ENABLE: |
| 227 | # Compute the predictions. |
| 228 | preds = model(inputs, meta["boxes"]) |
| 229 | ori_boxes = meta["ori_boxes"] |
| 230 | metadata = meta["metadata"] |
| 231 | |
| 232 | if cfg.NUM_GPUS: |
| 233 | preds = preds.cpu() |
| 234 | ori_boxes = ori_boxes.cpu() |
| 235 | metadata = metadata.cpu() |
| 236 | |
| 237 | if cfg.NUM_GPUS > 1: |
| 238 | preds = torch.cat(du.all_gather_unaligned(preds), dim=0) |
| 239 | ori_boxes = torch.cat(du.all_gather_unaligned(ori_boxes), dim=0) |
| 240 | metadata = torch.cat(du.all_gather_unaligned(metadata), dim=0) |
| 241 | |
| 242 | val_meter.iter_toc() |
| 243 | # Update and log stats. |
| 244 | val_meter.update_stats(preds, ori_boxes, metadata) |
| 245 | |
| 246 | else: |
| 247 | preds = model(inputs) |
no test coverage detected