code: def copy_weights_from_model(
      self, target_model, weights, indices_to_delete, indices_to_add
  ):
      for target_weight, source_weight in zip(
          target_model.weights, weights
      ):
          if "logits" not in target_weight.name:
              target_weight.assign(source_weight)
              continue
  
          if "kernel" in target_weight.name:
              w_val = np.delete(
                  source_weight,
                  [i - 1 for i in indices_to_delete],
                  axis=1,
              )
              # add new indices at the end
              if list(
                  range(
                      w_val.shape[1],
                      w_val.shape[1] + len(indices_to_add),
                  )
              ) != list(sorted(indices_to_add)):
                  raise Exception(
                      "Additional labels must be added at the end, but got label indices {} != {}".format(
                          range(
                              w_val.shape[1],
                              w_val.shape[1] + len(indices_to_add),
                          ),
                          sorted(indices_to_add),
                      )
                  )
              w_val = np.concatenate(
                  (
                      w_val[:, :-1],
                      np.random.uniform(
                          -0.1,
                          0.1,
                          (w_val.shape[0], len(indices_to_add)),
                      ),
                      w_val[:, -1:],
                  ),
                  axis=1,
              )
              target_weight.assign(w_val)
          elif "bias" in target_weight.name:
              # The bias will be initialized with a value that is (on average) smaller than the bias of
              # all other classes. This reduces problems if the difference of the old and new (untrained) biases
              # is too large
              b_val = np.delete(
                  source_weight,
                  [i - 1 for i in indices_to_delete],
                  axis=0,
              )
              b_val_init = np.mean(b_val) - np.std(b_val)
              b_val = np.concatenate(
                  (
                      b_val[:-1],
                      np.full((len(indices_to_add),), b_val_init),
                      b_val[-1:],
                  ),
                  axis=0,
              )
              target_weight.assign(b_val)
          else:
              raise NotImplementedError(
                  "logits layer is expected to have kernel and bias and nothing else"
              )
  
file_path: calamari/calamari_ocr/ocr/training/warmstart.py
imports: from typing import List
  import numpy
  from tfaip.trainer.warmstart.warmstarter import WarmStarter
label_desc: Use image node_id 4 for calling the WarmStarterWithCodecAdaption obj's underlying member method code with example usage: obj.copy_weights_from_model(target_model, weights, indices_to_delete, indices_to_add) without return types