(self)
do transfer learning by init sub vars from other checkpoint.
do transfer learning by init sub vars from other checkpoint.
do transfer learning by init sub vars from other checkpoint .
def init_from_checkpoint(self):
    ''' do transfer learning by init sub vars from other checkpoint. '''
    if 'transfer' not in self.config['solver']:
      return
    transfer_cfg = self.config['solver']['transfer']
    enable = transfer_cfg['enable']
    if not enable:
      return
    init_checkpoint = transfer_cfg['ckpt_path']
    exclude = transfer_cfg['exclude_reg']
    include = transfer_cfg['include_reg']
    logging.info(f"Transfer from checkpoint: {init_checkpoint}")
    logging.info(f"Transfer exclude: {exclude}")
    logging.info(f"Transfer include: {include}")

    tvars = tf.trainable_variables()
    initialized_variable_names = {}
    if init_checkpoint:

      def _filter_by_reg(tvars, include, exclude):
        include = include if include else []
        exclude = exclude if exclude else []
        outs = []
        for var in tvars:
          name = var.name
          for reg_str in include:
            logging.debug(f"var:{name}, reg: {reg_str}")
            m = re.match(reg_str, name)
            if m is not None:
              outs.append(var)
          for reg_str in exclude:
            logging.debug(f"var:{name}, reg: {reg_str}")
            m = re.match(reg_str, name)
            if m is None:
              outs.append(var)
        return outs

      tvars = _filter_by_reg(tvars, include, exclude)
      assignment_map, initialized_variable_names = \
        self.get_assignment_map_from_checkpoint(tvars, init_checkpoint)
      tf.train.init_from_checkpoint(init_checkpoint, assignment_map)

    logging.info("**** Trainable Variables ****")
    for var in tvars:
      init_string = ""
      if var.name in initialized_variable_names:
        init_string = ", *INIT_FROM_CKPT*"
      logging.info("  name = %s, shape = %s%s", var.name, var.shape,
                   init_string)
def init_from_checkpoint ( self ) : if 'transfer' not in self . config [ 'solver' ] : return transfer_cfg = self . config [ 'solver' ] [ 'transfer' ] enable = transfer_cfg [ 'enable' ] if not enable : return init_checkpoint = transfer_cfg [ 'ckpt_path' ] exclude = transfer_cfg [ 'exclude_reg' ] include = transfer_cfg [ 'include_reg' ] logging . info ( f"Transfer from checkpoint: {init_checkpoint}" ) logging . info ( f"Transfer exclude: {exclude}" ) logging . info ( f"Transfer include: {include}" ) tvars = tf . trainable_variables ( ) initialized_variable_names = { } if init_checkpoint : def _filter_by_reg ( tvars , include , exclude ) : include = include if include else [ ] exclude = exclude if exclude else [ ] outs = [ ] for var in tvars : name = var . name for reg_str in include : logging . debug ( f"var:{name}, reg: {reg_str}" ) m = re . match ( reg_str , name ) if m is not None : outs . append ( var ) for reg_str in exclude : logging . debug ( f"var:{name}, reg: {reg_str}" ) m = re . match ( reg_str , name ) if m is None : outs . append ( var ) return outs tvars = _filter_by_reg ( tvars , include , exclude ) assignment_map , initialized_variable_names = self . get_assignment_map_from_checkpoint ( tvars , init_checkpoint ) tf . train . init_from_checkpoint ( init_checkpoint , assignment_map ) logging . info ( "**** Trainable Variables ****" ) for var in tvars : init_string = "" if var . name in initialized_variable_names : init_string = ", *INIT_FROM_CKPT*" logging . info ( "  name = %s, shape = %s%s" , var . name , var . shape , init_string )