docstr: Given the network predictions and targets compute the total loss
docstr_tok: ["Given","the","network","predictions","and","targets","compute","the","total","loss"]
code: def compute_objectives(
      self, predictions, batch, stage, optim_name=""
  ):
      "Given the network predictions and targets compute the total loss"
      if self.sub_stage == SubStage.HISTORICAL:
          predict_wav = predictions
      else:
          predict_wav, mask = predictions
      predict_spec = self.compute_feats(predict_wav)
  
      ids = self.compute_ids(batch.id, optim_name)
      if self.sub_stage != SubStage.HISTORICAL:
          noisy_wav, lens = batch.noisy_sig
  
      if optim_name == "generator":
          est_score = self.est_score(predict_spec)
          target_score = self.hparams.target_score * torch.ones(
              self.batch_size, 1, device=self.device
          )
  
          noisy_wav, lens = batch.noisy_sig
          noisy_spec = self.compute_feats(noisy_wav)
          mse_cost = self.hparams.compute_cost(
              predict_spec, noisy_spec, lens
          )
  
      # D Learns to estimate the scores of enhanced speech
      elif optim_name == "D_enh" and self.sub_stage == SubStage.CURRENT:
          target_score = self.score(
              ids, predict_wav, predict_wav, lens
          )  # no clean_wav is needed
          est_score = self.est_score(predict_spec)
  
          # Write enhanced wavs during discriminator training, because we
          # compute the actual score here and we can save it
          self.write_wavs(ids, predict_wav, target_score, lens)
  
      # D Relearns to estimate the scores of previous epochs
      elif (
          optim_name == "D_enh"
          and self.sub_stage == SubStage.HISTORICAL
      ):
          target_score = batch.score.unsqueeze(1).float()
          est_score = self.est_score(predict_spec)
  
      # D Learns to estimate the scores of noisy speech
      elif optim_name == "D_noisy":
          noisy_spec = self.compute_feats(noisy_wav)
          target_score = self.score(
              ids, noisy_wav, noisy_wav, lens
          )  # no clean_wav is needed
          est_score = self.est_score(noisy_spec)
          # Save scores of noisy wavs
          self.save_noisy_scores(ids, target_score)
  
      if stage == sb.Stage.TRAIN:
          # Compute the cost
          cost = self.hparams.compute_cost(est_score, target_score)
          if optim_name == "generator":
              cost += self.hparams.mse_weight * mse_cost
              self.metrics["G"].append(cost.detach())
          else:
              self.metrics["D"].append(cost.detach())
  
      # Compute scores on validation data
      if stage != sb.Stage.TRAIN:
          clean_wav, lens = batch.clean_sig
  
          cost = self.hparams.compute_si_snr(
              predict_wav, clean_wav, lens
          )
          # Evaluate speech quality/intelligibility
          self.stoi_metric.append(
              batch.id, predict_wav, clean_wav, lens, reduction="batch"
          )
          self.pesq_metric.append(
              batch.id,
              predict=predict_wav,
              target=clean_wav,
              lengths=lens,
          )
          self.srmr_metric.append(
              batch.id,
              predict=predict_wav,
              target=predict_wav,
              lengths=lens,  # no clean_wav is needed
          )
          if (
              self.hparams.calculate_dnsmos_on_validation_set
          ):  # Note: very time consuming........
              self.dnsmos_metric.append(
                  batch.id,
                  predict=predict_wav,
                  target=predict_wav,
                  lengths=lens,  # no clean_wav is needed
              )
  
          # Write wavs to file, for evaluation
          lens = lens * clean_wav.shape[1]
          for name, pred_wav, length in zip(
              batch.id, predict_wav, lens
          ):
              name += ".wav"
              enhance_path = os.path.join(
                  self.hparams.enhanced_folder, name
              )
              torchaudio.save(
                  enhance_path,
                  torch.unsqueeze(pred_wav[: int(length)].cpu(), 0),
                  16000,
              )
  
      return cost
  
imports: import os
  import sys
  import shutil
  import torch
  import torchaudio
  import speechbrain
  import numpy
  import json
  import pickle
  import requests
  import time
  from urllib.parse import urlparse, urljoin
  from srmrpy import srmr
  from pesq import pesq
  from enum import Enum, auto
  from hyperpyyaml import load_hyperpyyaml
  from speechbrain.utils.metric_stats import MetricStats
  from speechbrain.processing.features import spectral_magnitude
  from speechbrain.nnet.loss.stoi_loss import stoi_loss
  from speechbrain.utils.distributed import run_on_main
  from speechbrain.dataio.sampler import ReproducibleWeightedRandomSampler
label_desc: Use image node_id 4 for calling the MetricGanBrain obj's underlying member method code with example usage: obj.compute_objectives(predictions, batch, stage, optim_name) and returns: cost