class_docstr: Dynamic Mask Head for
  `Instances as Queries <http://arxiv.org/abs/2105.01928>`_
  
  Args:
      num_convs (int): Number of convolution layer.
          Defaults to 4.
      roi_feat_size (int): The output size of RoI extractor,
          Defaults to 14.
      in_channels (int): Input feature channels.
          Defaults to 256.
      conv_kernel_size (int): Kernel size of convolution layers.
          Defaults to 3.
      conv_out_channels (int): Output channels of convolution layers.
          Defaults to 256.
      num_classes (int): Number of classes.
          Defaults to 80
      class_agnostic (int): Whether generate class agnostic prediction.
          Defaults to False.
      dropout (float): Probability of drop the channel.
          Defaults to 0.0
      upsample_cfg (:obj:`ConfigDict` or dict): The config for
          upsample layer.
      conv_cfg (:obj:`ConfigDict` or dict, optional): The convolution
          layer config.
      norm_cfg (:obj:`ConfigDict` or dict, optional): The norm layer config.
      dynamic_conv_cfg (:obj:`ConfigDict` or dict): The dynamic convolution
          layer config.
      loss_mask (:obj:`ConfigDict` or dict): The config for mask loss.
class_docstr_tok: ["Dynamic","Mask","Head","for","`","Instances","as","Queries","<","http",":","\/\/arxiv.org\/abs\/2105.01928",">","`","_","Args",":","num_convs","(","int",")",":","Number","of","convolution","layer",".","Defaults","to","4.","roi_feat_size","(","int",")",":","The","output","size","of","RoI","extractor",",","Defaults","to","14.","in_channels","(","int",")",":","Input","feature","channels",".","Defaults","to","256.","conv_kernel_size","(","int",")",":","Kernel","size","of","convolution","layers",".","Defaults","to","3.","conv_out_channels","(","int",")",":","Output","channels","of","convolution","layers",".","Defaults","to","256.","num_classes","(","int",")",":","Number","of","classes",".","Defaults","to","80","class_agnostic","(","int",")",":","Whether","generate","class","agnostic","prediction",".","Defaults","to","False",".","dropout","(","float",")",":","Probability","of","drop","the","channel",".","Defaults","to","0.0","upsample_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","The","config","for","upsample","layer",".","conv_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","The","convolution","layer","config",".","norm_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",",","optional",")",":","The","norm","layer","config",".","dynamic_conv_cfg","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","The","dynamic","convolution","layer","config",".","loss_mask","(",":","obj",":","`","ConfigDict","`","or","dict",")",":","The","config","for","mask","loss","."]
docstr: Calculate the loss based on the features extracted by the mask head.
  
  Args:
      mask_preds (Tensor): Predicted foreground masks, has shape
          (num_pos, num_classes, h, w).
      sampling_results (List[obj:SamplingResult]): Assign results of
          all images in a batch after sampling.
      batch_gt_instances (list[:obj:`InstanceData`]): Batch of
          gt_instance. It usually includes ``bboxes``, ``labels``, and
          ``masks`` attributes.
      rcnn_train_cfg (obj:ConfigDict): `train_cfg` of RCNN.
  
  Returns:
      dict: A dictionary of loss and targets components.
docstr_tok: ["Calculate","the","loss","based","on","the","features","extracted","by","the","mask","head",".","Args",":","mask_preds","(","Tensor",")",":","Predicted","foreground","masks",",","has","shape","(","num_pos",",","num_classes",",","h",",","w",")",".","sampling_results","(","List","[","obj",":","SamplingResult","]",")",":","Assign","results","of","all","images","in","a","batch","after","sampling",".","batch_gt_instances","(","list","[",":","obj",":","`","InstanceData","`","]",")",":","Batch","of","gt_instance",".","It","usually","includes","``","bboxes","``",",","``","labels","``",",","and","``","masks","``","attributes",".","rcnn_train_cfg","(","obj",":","ConfigDict",")",":","`","train_cfg","`","of","RCNN",".","Returns",":","dict",":","A","dictionary","of","loss","and","targets","components","."]
code: def loss_and_target(
      self,
      mask_preds: Tensor,
      sampling_results: List[SamplingResult],
      batch_gt_instances: InstanceList,
      rcnn_train_cfg: ConfigDict,
  ) -> dict:
      """Calculate the loss based on the features extracted by the mask head.
  
      Args:
          mask_preds (Tensor): Predicted foreground masks, has shape
              (num_pos, num_classes, h, w).
          sampling_results (List[obj:SamplingResult]): Assign results of
              all images in a batch after sampling.
          batch_gt_instances (list[:obj:`InstanceData`]): Batch of
              gt_instance. It usually includes ``bboxes``, ``labels``, and
              ``masks`` attributes.
          rcnn_train_cfg (obj:ConfigDict): `train_cfg` of RCNN.
  
      Returns:
          dict: A dictionary of loss and targets components.
      """
      mask_targets = self.get_targets(
          sampling_results=sampling_results,
          batch_gt_instances=batch_gt_instances,
          rcnn_train_cfg=rcnn_train_cfg,
      )
      pos_labels = torch.cat(
          [res.pos_gt_labels for res in sampling_results]
      )
  
      num_pos = pos_labels.new_ones(pos_labels.size()).float().sum()
      avg_factor = torch.clamp(reduce_mean(num_pos), min=1.0).item()
      loss = dict()
      if mask_preds.size(0) == 0:
          loss_mask = mask_preds.sum()
      else:
          loss_mask = self.loss_mask(
              mask_preds[
                  torch.arange(num_pos).long(), pos_labels, ...
              ].sigmoid(),
              mask_targets,
              avg_factor=avg_factor,
          )
      loss["loss_mask"] = loss_mask
      return dict(loss_mask=loss, mask_targets=mask_targets)
  
file_path: mmdetection/mmdet/models/roi_heads/mask_heads/dynamic_mask_head.py
imports: from typing import List
  import torch
  import torch.nn
  from mmengine.config import ConfigDict
  from torch import Tensor
  from mmdet.models.task_modules import SamplingResult
  from mmdet.registry import MODELS
  from mmdet.utils import ConfigType, InstanceList, OptConfigType, reduce_mean
  from .fcn_mask_head import FCNMaskHead
label_desc: Use image node_id 4 for calling the DynamicMaskHead obj's underlying member method code with example usage: obj.loss_and_target(mask_preds, sampling_results, batch_gt_instances, rcnn_train_cfg) and returns: dict