code: def __init__(self, cfg, mode="train"):
      self.cfg = cfg
      assert mode.lower() in [
          "train",
          "eval",
          "test",
      ], "mode should be 'train', 'eval' or 'test'"
      self.mode = mode.lower()
      self.optimizer = None
      self.is_loaded_weights = False
      self.use_amp = self.cfg.get("amp", False)
      self.amp_level = self.cfg.get("amp_level", "O1")
      self.custom_white_list = self.cfg.get("custom_white_list", None)
      self.custom_black_list = self.cfg.get("custom_black_list", None)
  
      # build data loader
      capital_mode = self.mode.capitalize()
      self.dataset = self.cfg[
          "{}Dataset".format(capital_mode)
      ] = create("{}Dataset".format(capital_mode))()
  
      if self.mode == "train":
          self.dataset_unlabel = self.cfg["UnsupTrainDataset"] = create(
              "UnsupTrainDataset"
          )
          self.loader = create("SemiTrainReader")(
              self.dataset, self.dataset_unlabel, cfg.worker_num
          )
  
      # build model
      if "model" not in self.cfg:
          self.model = create(cfg.architecture)
      else:
          self.model = self.cfg.model
          self.is_loaded_weights = True
  
      # EvalDataset build with BatchSampler to evaluate in single device
      # TODO: multi-device evaluate
      if self.mode == "eval":
          self._eval_batch_sampler = paddle.io.BatchSampler(
              self.dataset, batch_size=self.cfg.EvalReader["batch_size"]
          )
          # If metric is VOC, need to be set collate_batch=False.
          if cfg.metric == "VOC":
              cfg["EvalReader"]["collate_batch"] = False
          self.loader = create("EvalReader")(
              self.dataset, cfg.worker_num, self._eval_batch_sampler
          )
      # TestDataset build after user set images, skip loader creation here
  
      # build optimizer in train mode
      if self.mode == "train":
          steps_per_epoch = len(self.loader)
          if steps_per_epoch < 1:
              logger.warning(
                  "Samples in dataset are less than batch_size, please set smaller batch_size in TrainReader."
              )
          self.lr = create("LearningRate")(steps_per_epoch)
          self.optimizer = create("OptimizerBuilder")(
              self.lr, self.model
          )
  
          # Unstructured pruner is only enabled in the train mode.
          if self.cfg.get("unstructured_prune"):
              self.pruner = create("UnstructuredPruner")(
                  self.model, steps_per_epoch
              )
      if self.use_amp and self.amp_level == "O2":
          self.model, self.optimizer = paddle.amp.decorate(
              models=self.model,
              optimizers=self.optimizer,
              level=self.amp_level,
          )
  
      self.use_ema = "use_ema" in cfg and cfg["use_ema"]
      if self.use_ema:
          ema_decay = self.cfg.get("ema_decay", 0.9998)
          ema_decay_type = self.cfg.get("ema_decay_type", "threshold")
          cycle_epoch = self.cfg.get("cycle_epoch", -1)
          ema_black_list = self.cfg.get("ema_black_list", None)
          self.ema = ModelEMA(
              self.model,
              decay=ema_decay,
              ema_decay_type=ema_decay_type,
              cycle_epoch=cycle_epoch,
              ema_black_list=ema_black_list,
          )
          self.ema_start_iters = self.cfg.get("ema_start_iters", 0)
  
      # simple_ema for SSOD
      self.use_simple_ema = (
          "use_simple_ema" in cfg and cfg["use_simple_ema"]
      )
      if self.use_simple_ema:
          self.use_ema = True
          ema_decay = self.cfg.get("ema_decay", 0.9996)
          self.ema = SimpleModelEMA(self.model, decay=ema_decay)
          self.ema_start_iters = self.cfg.get("ema_start_iters", 0)
  
      self._nranks = dist.get_world_size()
      self._local_rank = dist.get_rank()
  
      self.status = {}
  
      self.start_epoch = 0
      self.end_epoch = 0 if "epoch" not in cfg else cfg.epoch
  
      # initial default callbacks
      self._init_callbacks()
  
      # initial default metrics
      self._init_metrics()
      self._reset_metrics()
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import os
  import copy
  import time
  import typing
  import numpy
  import paddle
  import paddle.nn
  import paddle.distributed
  from paddle.distributed import fleet
  from ppdet.optimizer import ModelEMA, SimpleModelEMA
  from ppdet.core.workspace import create
  from ppdet.utils.checkpoint import load_weight, load_pretrain_weight, save_model
  import ppdet.utils.stats
  from ppdet.utils import profiler
  from ppdet.modeling.ssod.utils import align_weak_strong_shape
  from .trainer import Trainer
  from ppdet.utils.logger import setup_logger
  from paddle.static import InputSpec
  from ppdet.engine.export_utils import _dump_infer_config, _prune_input_spec
label_desc: Use image node_id 1 to create a new Trainer_DenseTeacher object from inherited base classes: Trainer with example: obj = Trainer_DenseTeacher(cfg, mode)