code: def train(args, model, train_dataloader, class_name):
      epoch_begin = time.time()
      # extract train set features
      if args.inc:
          c = model.k  # args.k
          h = w = args.crop_size // 4
          N = 0  # sample num
          for x in tqdm(
              train_dataloader,
              "| feature extraction | train | %s |" % class_name,
          ):
              # model prediction
              out = model(x)
              out = model.project(out, True)  # hwbc
              model.compute_stats_incremental(out)
              N += x.shape[0]
          del out, x
      else:
          outs = []
          for x in tqdm(
              train_dataloader,
              "| feature extraction | train | %s |" % class_name,
          ):
              # model prediction
              out = model(x)
              out = model.project(out)
              outs.append(out)
          del out, x
          outs = paddle.concat(outs, 0)
  
      if args.inc:
          model.compute_inv_incremental(N)
      else:
          if args.einsum:
              model.compute_stats_einsum(outs)
          else:
              model.compute_stats(outs)
          del outs
  
      t = time.time() - epoch_begin
      print(
          datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
          + "\t"
          + "Train ends, total {:.2f}s".format(t)
      )
      if args.save_model:
          print("Saving model...")
          save_name = os.path.join(
              args.save_path
              + f"/{args.method}_{args.backbone}_{args.k}",
              "{}.pdparams".format(class_name),
          )
          dir_name = os.path.dirname(save_name)
          os.makedirs(dir_name, exist_ok=True)
          state_dict = {
              "params": model.model.state_dict(),
              "stats": model._buffers,
          }
          paddle.save(state_dict, save_name)
          print(
              datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
              + "\t"
              + "Save model in {}".format(str(save_name))
          )
  
file_path: PaddleSeg/contrib/QualityInspector/tools/uad/patchcore/train.py
imports: import os
  import sys
  import time
  import random
  import argparse
  import datetime
  import numpy
  import pandas
  from tqdm import tqdm
  import paddle
  from paddle.io import DataLoader
  import qinspector.uad.datasets.mvtec
  from qinspector.uad.models.patchcore import get_model
  from qinspector.uad.utils.utils import str2bool
  from qinspector.cvlib.uad_configs import ConfigParser
  from val import val
label_desc: Use image node_id 3 for calling a global function with example usage: train(args, model, train_dataloader, class_name) without return types