class_docstr: RCNN network.
  
  Parameters
  ----------
  features : gluon.HybridBlock
      Base feature extractor before feature pooling layer.
  top_features : gluon.HybridBlock
      Tail feature extractor after feature pooling layer.
  classes : iterable of str
      Names of categories, its length is ``num_class``.
  box_features : gluon.HybridBlock
      feature head for transforming roi output for box prediction.
  short : int
      Input image short side size.
  max_size : int
      Maximum size of input image long side.
  train_patterns : str
      Matching pattern for trainable parameters.
  nms_thresh : float
      Non-maximum suppression threshold. You can specify < 0 or > 1 to disable NMS.
  nms_topk : int
      Apply NMS to top k detection results, use -1 to disable so that every Detection
       result is used in NMS.
  roi_mode : str
      ROI pooling mode. Currently support 'pool' and 'align'.
  roi_size : tuple of int, length 2
      (height, width) of the ROI region.
  strides : int/tuple of ints
      Stride(s) of network features. Tuple for FPN.
  clip: float
      Clip bounding box target to this value.
  
  Attributes
  ----------
  classes : iterable of str
      Names of categories, its length is ``num_class``.
  num_class : int
      Number of positive categories.
  short : int
      Input image short side size.
  max_size : int
      Maximum size of input image long side.
  train_patterns : str
      Matching pattern for trainable parameters.
  nms_thresh : float
      Non-maximum suppression threshold. You can specify < 0 or > 1 to disable NMS.
  nms_topk : int
      Apply NMS to top k detection results, use -1 to disable so that every Detection
       result is used in NMS.
  minimal_opset : bool
      We sometimes add special operators to accelerate training/inference, however, for exporting
      to third party compilers we want to utilize most widely used operators.
      If `minimal_opset` is `True`, the network will use a minimal set of operators good
      for e.g., `TVM`.
docstr: Reset class categories and class predictors.
  
  Parameters
  ----------
  classes : iterable of str
      The new categories. ['apple', 'orange'] for example.
  reuse_weights : dict
      A {new_integer : old_integer} or mapping dict or {new_name : old_name} mapping dict,
      or a list of [name0, name1,...] if class names don't change.
      This allows the new predictor to reuse the
      previously trained weights specified.
docstr_tok: ["Reset","class","categories","and","class","predictors",".","Parameters","--","--","--","--","--","classes",":","iterable","of","str","The","new","categories",".","[","'apple","'",",","'orange","'","]","for","example",".","reuse_weights",":","dict","A","{","new_integer",":","old_integer","}","or","mapping","dict","or","{","new_name",":","old_name","}","mapping","dict",",","or","a","list","of","[","name0",",","name1",",","...","]","if","class","names","do","n't","change",".","This","allows","the","new","predictor","to","reuse","the","previously","trained","weights","specified","."]
code: def reset_class(self, classes, reuse_weights=None):
      """Reset class categories and class predictors.
  
      Parameters
      ----------
      classes : iterable of str
          The new categories. ['apple', 'orange'] for example.
      reuse_weights : dict
          A {new_integer : old_integer} or mapping dict or {new_name : old_name} mapping dict,
          or a list of [name0, name1,...] if class names don't change.
          This allows the new predictor to reuse the
          previously trained weights specified.
  
      """
      self._clear_cached_op()
      if reuse_weights:
          assert hasattr(
              self, "classes"
          ), "require old classes to reuse weights"
      old_classes = getattr(self, "classes", [])
      self.classes = classes
      self.num_class = len(classes)
      if isinstance(reuse_weights, (dict, list)):
          if isinstance(reuse_weights, dict):
              # trying to replace str with indices
              for k, v in reuse_weights.items():
                  if isinstance(v, str):
                      try:
                          v = old_classes.index(
                              v
                          )  # raise ValueError if not found
                      except ValueError:
                          raise ValueError(
                              "{} not found in old class names {}".format(
                                  v, old_classes
                              )
                          )
                      reuse_weights[k] = v
                  if isinstance(k, str):
                      try:
                          new_idx = self.classes.index(
                              k
                          )  # raise ValueError if not found
                      except ValueError:
                          raise ValueError(
                              "{} not found in new class names {}".format(
                                  k, self.classes
                              )
                          )
                      reuse_weights.pop(k)
                      reuse_weights[new_idx] = v
          else:
              new_map = {}
              for x in reuse_weights:
                  try:
                      new_idx = self.classes.index(x)
                      old_idx = old_classes.index(x)
                      new_map[new_idx] = old_idx
                  except ValueError:
                      warnings.warn(
                          "{} not found in old: {} or new class names: {}".format(
                              x, old_classes, self.classes
                          )
                      )
              reuse_weights = new_map
  
      with self.name_scope():
          old_class_pred = self.class_predictor
          old_box_pred = self.box_predictor
          ctx = list(old_class_pred.params.values())[0].list_ctx()
          # to avoid deferred init, number of in_channels must be defined
          in_units = list(old_class_pred.params.values())[0].shape[1]
          self.class_predictor = nn.Dense(
              self.num_class + 1,
              weight_initializer=mx.init.Normal(0.01),
              prefix=self.class_predictor.prefix,
              in_units=in_units,
          )
          self.box_predictor = nn.Dense(
              self.num_class * 4,
              weight_initializer=mx.init.Normal(0.001),
              prefix=self.box_predictor.prefix,
              in_units=in_units,
          )
          self.cls_decoder = MultiPerClassDecoder(
              num_class=self.num_class + 1
          )
          # initialize
          self.class_predictor.initialize(ctx=ctx)
          self.box_predictor.initialize(ctx=ctx)
          if reuse_weights:
              assert isinstance(reuse_weights, dict)
              # class predictors
              srcs = (old_class_pred, old_box_pred)
              dsts = (self.class_predictor, self.box_predictor)
              offsets = (1, 0)  # class predictor has bg, box don't
              lens = (1, 4)  # class predictor length=1, box length=4
              for src, dst, offset, l in zip(srcs, dsts, offsets, lens):
                  for old_params, new_params in zip(
                      src.params.values(), dst.params.values()
                  ):
                      # slice and copy weights
                      old_data = old_params.data()
                      new_data = new_params.data()
  
                      for k, v in reuse_weights.items():
                          if k >= len(self.classes) or v >= len(
                              old_classes
                          ):
                              warnings.warn(
                                  "reuse mapping {}/{} -> {}/{} out of range".format(
                                      k, self.classes, v, old_classes
                                  )
                              )
                              continue
                          new_data[
                              (k + offset) * l : (k + offset + 1) * l
                          ] = old_data[
                              (v + offset) * l : (v + offset + 1) * l
                          ]
                      # reuse background weights as well
                      if offset > 0:
                          new_data[0:l] = old_data[0:l]
                      # set data to new conv layers
                      new_params.set_data(new_data)
  
file_path: gluon-cv/gluoncv/model_zoo/rcnn/rcnn.py
imports: from __future__ import absolute_import
  import warnings
  import mxnet
  from mxnet import gluon
  from mxnet.gluon import nn
  from ...nn.coder import NormalizedBoxCenterDecoder, MultiPerClassDecoder
  from ...nn.feature import FPNFeatureExpander
label_desc: Use image node_id 4 for calling the RCNN obj's underlying member method code with example usage: obj.reset_class(classes, reuse_weights) without return types