docstr: Creates image harmonization model supported by the features extracted from the pre-trained HRNet backbone.
  HRNet outputs feature maps on 4 different resolutions.
  
  Parameters
  ----------
  base_config : dict
      Configuration dict for the base model, to which the backbone features are incorporated.
      base_config contains model class and init parameters, examples can be found in iharm.mconfigs.base_models
  downsize_backbone_input : bool
      If the input image should be half-sized for the backbone.
  mask_fusion : str
      How to fuse the binary mask with the backbone input:
      'sum': apply convolution to the mask and sum it with the output of the first convolution in the backbone
      'rgb': concatenate the mask to the input image and translate it back to 3 channels with convolution
      otherwise: do not fuse mask with the backbone input
  lr_mult : float
      Multiply learning rate to lr_mult when updating the weights of the backbone.
  cat_hrnet_outputs : bool
      If 4 HRNet outputs should be resized and concatenated to a single tensor.
  pyramid_channels : int
      When HRNet outputs are concatenated to a single one, it can be consequently downsized
      to produce a feature pyramid.
      The pyramid features are then fused with the encoder outputs in the base model on multiple layers.
      Each pyramid feature map contains equal number of channels equal to pyramid_channels.
      If pyramid_channels <= 0, the feature pyramid is not constructed.
  ocr : int
      When HRNet outputs are concatenated to a single one, the OCR module can be applied
      resulting in feature map with (2 * ocr) channels. If ocr <= 0 the OCR module is not applied.
  width : int
      Width of the HRNet blocks.
  small : bool
      If True, HRNet contains 2 blocks at each stage and 4 otherwise.
  mode : str
      How to fuse the backbone features with the encoder outputs in the base model:
      'sum': apply convolution to the backbone feature map obtaining number of channels
       same as in the encoder output and sum them
      'cat': concatenate the backbone feature map with the encoder output
      'catc': concatenate the backbone feature map with the encoder output and apply convolution obtaining
      number of channels same as in the encoder output
      otherwise: the backbone features are not incorporated into the base model
  base_kwargs : dict
      any kwargs associated with the base model
docstr_tok: ["Creates","image","harmonization","model","supported","by","the","features","extracted","from","the","pre-trained","HRNet","backbone",".","HRNet","outputs","feature","maps","on","4","different","resolutions",".","Parameters","--","--","--","--","--","base_config",":","dict","Configuration","dict","for","the","base","model",",","to","which","the","backbone","features","are","incorporated",".","base_config","contains","model","class","and","init","parameters",",","examples","can","be","found","in","iharm.mconfigs.base_models","downsize_backbone_input",":","bool","If","the","input","image","should","be","half-sized","for","the","backbone",".","mask_fusion",":","str","How","to","fuse","the","binary","mask","with","the","backbone","input",":","'sum","'",":","apply","convolution","to","the","mask","and","sum","it","with","the","output","of","the","first","convolution","in","the","backbone","'rgb","'",":","concatenate","the","mask","to","the","input","image","and","translate","it","back","to","3","channels","with","convolution","otherwise",":","do","not","fuse","mask","with","the","backbone","input","lr_mult",":","float","Multiply","learning","rate","to","lr_mult","when","updating","the","weights","of","the","backbone",".","cat_hrnet_outputs",":","bool","If","4","HRNet","outputs","should","be","resized","and","concatenated","to","a","single","tensor",".","pyramid_channels",":","int","When","HRNet","outputs","are","concatenated","to","a","single","one",",","it","can","be","consequently","downsized","to","produce","a","feature","pyramid",".","The","pyramid","features","are","then","fused","with","the","encoder","outputs","in","the","base","model","on","multiple","layers",".","Each","pyramid","feature","map","contains","equal","number","of","channels","equal","to","pyramid_channels",".","If","pyramid_channels","<","=","0",",","the","feature","pyramid","is","not","constructed",".","ocr",":","int","When","HRNet","outputs","are","concatenated","to","a","single","one",",","the","OCR","module","can","be","applied","resulting","in","feature","map","with","(","2","*","ocr",")","channels",".","If","ocr","<","=","0","the","OCR","module","is","not","applied",".","width",":","int","Width","of","the","HRNet","blocks",".","small",":","bool","If","True",",","HRNet","contains","2","blocks","at","each","stage","and","4","otherwise",".","mode",":","str","How","to","fuse","the","backbone","features","with","the","encoder","outputs","in","the","base","model",":","'sum","'",":","apply","convolution","to","the","backbone","feature","map","obtaining","number","of","channels","same","as","in","the","encoder","output","and","sum","them","'cat","'",":","concatenate","the","backbone","feature","map","with","the","encoder","output","'catc","'",":","concatenate","the","backbone","feature","map","with","the","encoder","output","and","apply","convolution","obtaining","number","of","channels","same","as","in","the","encoder","output","otherwise",":","the","backbone","features","are","not","incorporated","into","the","base","model","base_kwargs",":","dict","any","kwargs","associated","with","the","base","model"]
code: def __init__(
      self,
      base_config,
      downsize_hrnet_input=False,
      mask_fusion="sum",
      lr_mult=0.1,
      cat_hrnet_outputs=True,
      pyramid_channels=-1,
      ocr=64,
      width=18,
      small=True,
      mode="cat",
      **base_kwargs,
  ):
      """
      Creates image harmonization model supported by the features extracted from the pre-trained HRNet backbone.
      HRNet outputs feature maps on 4 different resolutions.
  
      Parameters
      ----------
      base_config : dict
          Configuration dict for the base model, to which the backbone features are incorporated.
          base_config contains model class and init parameters, examples can be found in iharm.mconfigs.base_models
      downsize_backbone_input : bool
          If the input image should be half-sized for the backbone.
      mask_fusion : str
          How to fuse the binary mask with the backbone input:
          'sum': apply convolution to the mask and sum it with the output of the first convolution in the backbone
          'rgb': concatenate the mask to the input image and translate it back to 3 channels with convolution
          otherwise: do not fuse mask with the backbone input
      lr_mult : float
          Multiply learning rate to lr_mult when updating the weights of the backbone.
      cat_hrnet_outputs : bool
          If 4 HRNet outputs should be resized and concatenated to a single tensor.
      pyramid_channels : int
          When HRNet outputs are concatenated to a single one, it can be consequently downsized
          to produce a feature pyramid.
          The pyramid features are then fused with the encoder outputs in the base model on multiple layers.
          Each pyramid feature map contains equal number of channels equal to pyramid_channels.
          If pyramid_channels <= 0, the feature pyramid is not constructed.
      ocr : int
          When HRNet outputs are concatenated to a single one, the OCR module can be applied
          resulting in feature map with (2 * ocr) channels. If ocr <= 0 the OCR module is not applied.
      width : int
          Width of the HRNet blocks.
      small : bool
          If True, HRNet contains 2 blocks at each stage and 4 otherwise.
      mode : str
          How to fuse the backbone features with the encoder outputs in the base model:
          'sum': apply convolution to the backbone feature map obtaining number of channels
           same as in the encoder output and sum them
          'cat': concatenate the backbone feature map with the encoder output
          'catc': concatenate the backbone feature map with the encoder output and apply convolution obtaining
          number of channels same as in the encoder output
          otherwise: the backbone features are not incorporated into the base model
      base_kwargs : dict
          any kwargs associated with the base model
      """
      params = base_config["params"]
      params.update(base_kwargs)
      depth = params["depth"]
  
      backbone = HRNetBB(
          cat_outputs=cat_hrnet_outputs,
          pyramid_channels=pyramid_channels,
          pyramid_depth=min(
              depth - 2 if not downsize_hrnet_input else depth - 3, 4
          ),
          width=width,
          ocr=ocr,
          small=small,
          lr_mult=lr_mult,
      )
  
      params.update(
          dict(
              backbone_from=3 if downsize_hrnet_input else 2,
              backbone_channels=backbone.output_channels,
              backbone_mode=mode,
          )
      )
      base_model = base_config["model"](**params)
  
      super(HRNetIHModel, self).__init__(
          base_model, backbone, downsize_hrnet_input, mask_fusion
      )
  
file_path: carefree-learn/cflearn/api/cv/third_party/iharm/model/backboned/hrnet.py
imports: import torch.nn
  from .ih_model import IHModelWithBackbone
  from ..modifiers import LRMult
  from ..modeling.hrnet_ocr import HighResolutionNet
  from ..modeling.basic_blocks import MaxPoolDownSize
label_desc: Use image node_id 1 to create a new HRNetIHModel object from inherited base classes: IHModelWithBackbone with example: obj = HRNetIHModel(base_config, downsize_hrnet_input, mask_fusion, lr_mult, cat_hrnet_outputs, pyramid_channels, ocr, width, small, mode)