def write_scalars_to_text(summary, prefix, step, textlogger,
                          log_axe, log_axe_sec, log_together=False):
  prefix_abb = get_prefix_abb(prefix=prefix)
  summary = {prefix_abb + '.' + k: v for k, v in summary.items()}
  textlogger.log(step, **summary)
  time_str = prefix + '-' + '-'.join(summary.keys())
  if log_axe:
    now = time.time()
    last_time = getattr(Trainer, time_str, 0)
    if now - last_time > log_axe_sec:
      if log_together:
        textlogger.log_ax(**summary)
      else:
        textlogger.log_axes(**summary)
      setattr(Trainer, time_str, now)
Esempio n. 2
0
    def print_distribution(self,
                           iteration,
                           log_prob=True,
                           print_interval=float('inf')):
        # remove formats
        org_formatters = []
        for handler in self.logger.handlers:
            org_formatters.append(handler.formatter)
            handler.setFormatter(logging.Formatter("%(message)s"))

        default_dict = collections.defaultdict(dict)
        self.logger.info("####### distribution #######")
        searched_arc = []
        for layer_id, op_dist in enumerate(self.op_dist):
            prob = op_dist.probs
            max_op_id = prob.argmax().item()
            searched_arc.append(max_op_id)
            for op_id, op_name in enumerate(self.cfg_ops.keys()):
                op_prob = prob[0][op_id]
                default_dict[f'L{layer_id}'][get_prefix_abb(
                    op_name)] = op_prob.item()

            if log_prob:
                if layer_id % print_interval == 0:
                    self.logger.info(layer_id // print_interval)
                self.logger.info(prob)

        searched_arc = np.array(searched_arc)
        self.logger.info('\nsearched arcs: \n%s' % searched_arc)
        self.myargs.textlogger.logstr(
            iteration,
            searched_arc='\n' +
            np.array2string(searched_arc, threshold=np.inf))

        summary_defaultdict2txtfig(default_dict=default_dict,
                                   prefix='',
                                   step=iteration,
                                   textlogger=self.myargs.textlogger)
        self.logger.info("#####################")

        # restore formats
        for handler, formatter in zip(self.logger.handlers, org_formatters):
            handler.setFormatter(formatter)
Esempio n. 3
0
    def print_distribution(self, iteration, print_interval=float('inf')):
        # remove formats
        org_formatters = []
        for handler in self.logger.handlers:
            org_formatters.append(handler.formatter)
            handler.setFormatter(logging.Formatter("%(message)s"))

        self.logger.info("####### distribution #######")
        class_arcs = []
        for class_idx in range(self.n_classes):
            default_dict = collections.defaultdict(dict)
            searched_arc = []
            for layer_id, op_dist in enumerate(self.op_dist):
                prob = op_dist.probs
                max_op_id = prob[class_idx].argmax().item()
                searched_arc.append(max_op_id)
                for op_id, op_name in enumerate(self.cfg_ops.keys()):
                    op_prob = prob[class_idx][op_id]
                    default_dict[f'C{class_idx}L{layer_id}'][get_prefix_abb(
                        op_name)] = op_prob.item()

            class_arcs.append(searched_arc)
            searched_arc = np.array(searched_arc)
            self.logger.info(
                f'Class {class_idx} searched arcs: {searched_arc}')
            # self.myargs.textlogger.logstr(iteration,
            #                               searched_arc='\n' + np.array2string(searched_arc, threshold=np.inf))

            summary_defaultdict2txtfig(default_dict=default_dict,
                                       prefix='',
                                       step=iteration,
                                       textlogger=self.myargs.textlogger)

        class_arcs = np.array(class_arcs)
        self.myargs.textlogger.logstr(
            iteration,
            searched_class_arc='\n' +
            np.array2string(class_arcs, threshold=np.inf))
        self.logger.info("#####################")
        # restore formats
        for handler, formatter in zip(self.logger.handlers, org_formatters):
            handler.setFormatter(formatter)
  def summary_defaultdict2txtfig(default_dict, prefix, step,
                                 textlogger=None, in_one_figure=True,
                                 log_txt=True, log_fig=True, save_fig_sec=300):
    if not comm.is_main_process():
      return
    if textlogger is not None:
      prefix_abb = get_prefix_abb(prefix=prefix)
      default_dict_copy = defaultdict(dict)
      # add prefix_abb and key to subkey
      for k, v in default_dict.items():
        default_dict_copy[k] = {prefix_abb + '.' + k + '.' + subk: subv for subk, subv in v.items()}
      default_dict = default_dict_copy

      if log_txt:
        for k, v in default_dict.items():
          textlogger.log(step, **v)
      if log_fig:
        textlogger.log_defaultdict2figure(default_dict, in_one_figure=in_one_figure, save_fig_sec=save_fig_sec)
    else:
      print('textlogger are None!')