How can I calculate validation loss for faster RCNN?

SU801T: <p>Hi,</p>
<p>I followed this tutorial for object detection:<br>
<a href="https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html</a></p>
<p>and their GitHub repository that contains the following <code>train_one_epoch</code> and <code>evaluate</code> functions:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/vision/blob/main/references/detection/engine.py">
  <header class="source">

      <a href="https://github.com/pytorch/vision/blob/main/references/detection/engine.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/vision/blob/main/references/detection/engine.py" target="_blank" rel="noopener nofollow ugc">pytorch/vision/blob/main/references/detection/engine.py</a></h4>


      <pre><code class="lang-py">import math
import sys
import time

import torch
import torchvision.models.detection.mask_rcnn
import utils
from coco_eval import CocoEvaluator
from coco_utils import get_coco_api_from_dataset


def train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq, scaler=None):
    model.train()
    metric_logger = utils.MetricLogger(delimiter="  ")
    metric_logger.add_meter("lr", utils.SmoothedValue(window_size=1, fmt="{value:.6f}"))
    header = f"Epoch: [{epoch}]"

    lr_scheduler = None
    if epoch == 0:
        warmup_factor = 1.0 / 1000
</code></pre>



  This file has been truncated. <a href="https://github.com/pytorch/vision/blob/main/references/detection/engine.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>However, I want to calculate losses during validation. I  implemented this for evaluation loss, where to obtain losses, <code>model.train()</code> needs to be on:</p>
<pre><code class="lang-auto">@torch.no_grad()
def evaluate_loss(model, data_loader, device):
    val_loss = 0
    model.train()
    for images, targets in (data_loader):
        images = list(image.to(device) for image in images)
        targets = [{k: v.to(device) for k, v in t.items()} for t in targets]

        loss_dict = model(images, targets)

        losses = sum(loss for loss in loss_dict.values())

        # reduce losses over all GPUs for logging purposes
        loss_dict_reduced = utils.reduce_dict(loss_dict)
        losses_reduced = sum(loss for loss in loss_dict_reduced.values())
        val_loss += losses_reduced

    validation_loss = val_loss/ len(data_loader)

    return validation_loss
</code></pre>
<p>I then place it after the learning rate scheduler step:</p>
<pre><code class="lang-auto"> for epoch in range(args.num_epochs):
        # train for one epoch, printing every 10 iterations
        train_one_epoch(model, optimizer, train_data_loader, device, epoch, print_freq=10)
    
        # update the learning rate
        lr_scheduler.step()

        validation_loss = evaluate_loss(model, valid_data_loader, device=device)
        print("validation loss", validation_loss)

        # evaluate on the test dataset
        evaluate(model, valid_data_loader, device=device)
</code></pre>
<p>Does this look correct or can it interfere with training/produce inaccurate losses?</p>