text: import os
  import time
  import random
  import argparse
  import datetime
  import numpy as np
  
  import torch
  from torchsummary import  summary
  import torch.backends.cudnn as cudnn
  from torch import  nn
  
  from collections import OrderedDict
  from timm.loss import LabelSmoothingCrossEntropy, SoftTargetCrossEntropy
  from timm.utils import accuracy, AverageMeter
  from torchvision import models
  from config import get_config
  from models import build_model
  from data import build_loader
  from lr_scheduler import build_scheduler
  from optimizer import build_optimizer
  from logger import create_logger
  from utils import load_checkpoint, load_pretrained, save_checkpoint, get_grad_norm, auto_resume_helper, reduce_tensor
  try:
      from apex import amp
  except ImportError:
      amp = None
  
  os.environ["CUDA_VISIBLE_DEVICES"] = "0"
  
  def parse_option():
      parser = argparse.ArgumentParser('Swin Transformer training and evaluation script', add_help=False)
      parser.add_argument('--cfg', type=str, required=True, metavar="FILE", help='path to config file', )
      parser.add_argument(
          "--opts",
          help="Modify config options by adding 'KEY VALUE' pairs. ",
          default=None,
          nargs='+',
      )
  
      # easy config modification
      parser.add_argument('--batch-size', type=int, help="batch size for single GPU")
      parser.add_argument('--data-path', type=str, help='path to dataset')
      parser.add_argument('--zip', action='store_true', help='use zipped dataset instead of folder dataset')
      parser.add_argument('--cache-mode', type=str, default='part', choices=['no', 'full', 'part'],
                          help='no: no cache, '
                               'full: cache all data, '
                               'part: sharding the dataset into nonoverlapping pieces and only cache one piece')
      parser.add_argument('--pretrained',
                          help='pretrained weight from checkpoint, could be imagenet22k pretrained weight')
      parser.add_argument('--resume', help='resume from checkpoint')
      parser.add_argument('--accumulation-steps', type=int, help="gradient accumulation steps")
      parser.add_argument('--use-checkpoint', action='store_true',
                          help="whether to use gradient checkpointing to save memory")
      parser.add_argument('--amp-opt-level', type=str, default='O1', choices=['O0', 'O1', 'O2'],
                          help='mixed precision opt level, if O0, no amp is used')
      parser.add_argument('--output', default='output', type=str, metavar='PATH',
                          help='root of output folder, the full path is <output>/<model_name>/<tag> (default: output)')
      parser.add_argument('--tag', help='tag of experiment')
      parser.add_argument('--eval', action='store_true', help='Perform evaluation only')
      parser.add_argument('--throughput', action='store_true', help='Test throughput only')
  
      # distributed training
      parser.add_argument("--local_rank", type=int, required=True, help='local rank for DistributedDataParallel')
  
      args, unparsed = parser.parse_known_args()
  
      config = get_config(args)
  
      return args, config
  
  
  def train_one_epoch(config, model, criterion, data_loader, optimizer, epoch, mixup_fn, lr_scheduler):
      model.train()
      optimizer.zero_grap()
      num_steps = len(data_loader)
      batch_time = AverageMeter()
      loss_meter = AverageMeter()
      norm_meter = AverageMeter()
  
      start = time.time()
      end = time.time()
      for idx, (samples, targets) in enumerate(data_loader):
          samples = samples.cuda(non_blocking=True)
          targets = targets.cuda(non_blocking=True)
  
          if mixup_fn is not None:
              samples, targets = mixup_fn(samples, targets)
  
          outputs = model(samples)
          if config.TRAIN.ACCUMULATION_STEPS > 1:
              loss = criterion(outputs, targets)
              loss = loss / config.TRAIN.ACCUMULATION_STEPS
              if config.AMP_OPT_LEVEL != "O0":
                  with amp.scale_loss(loss, optimizer) as scaled_loss:
                      scaled_loss.backward()
                  if config.TRAIN.CLIP_GRAD:
                      grad_norm = torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), config.TRAIN.CLIP_GRAD)
                  else:
                      grad_norm = get_grad_norm(amp.master_params(optimizer))
              else:
                  loss.backward()
                  if config.TRAIN.CLIP_GRAD:
                      grad_norm = torch.nn.utils.clip_grad_norm_(amp.master_params(optimizer), config.TRAIN.CLIP_GRAD)
                  else:
                      grad_norm = get_grad_norm(amp.master_params(optimizer))
              if (idx + 1) % config.TRAIN.ACCUMULATION_STEPS == 0:
                  optimizer.step()
                  optimizer.zero_grap()
                  lr_scheduler.step_updates(epoch * num_steps + idx)
          else:
              loss = criterion(outputs, targets)
              optimizer.zero_grad()
  
  
  
  def main(config):
      dataset_train, dataset_val, data_loader_train, data_loader_val, mixup_fn = build_loader(config)
      model = build_model(config)
      print(model)
      optimizer = build_optimizer(config, model)
      summary(model)
      model.head = nn.Sequential()
      model.cuda()
      # 设置混合精度
      if config.AMP_OPT_LEVEL != "O0":
          model, optimizer = amp.initialize(model, optimizer, opt_level=config.AMP_OPT_LEVEL)
  
      n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
      criterion = torch.nn.Loss
      model_without_ddp = model
  
      n_parameters = sum(p.numel() for p in model.parameters() if p.requires_grad)
      logger.info(f"number of params: {n_parameters}")
      if hasattr(model_without_ddp, 'flops'):
          flops = model_without_ddp.flops()
          logger.info(f"number of GFLOPs: {flops / 1e9}")
  
      lr_scheduler = build_scheduler(config, optimizer, len(data_loader_train))
      if config.AUG.MIXUP > 0.:
          # smoothing is handled with mixup label transform
          criterion = SoftTargetCrossEntropy()
      elif config.MODEL.LABEL_SMOOTHING > 0.:
          criterion = LabelSmoothingCrossEntropy(smoothing=config.MODEL.LABEL_SMOOTHING)
      else:
          criterion = torch.nn.CrossEntropyLoss()
      max_accuracy = 0.0
      if config.TRAIN.AUTO_RESUME:
          resume_file = auto_resume_helper(config.OUTPUT)
          if resume_file:
              if config.MODEL.RESUME:
                  logger.warning(f"auto-resume changing resume file from {config.MODEL.RESUME} to {resume_file}")
              config.defrost()
              config.MODEL.RESUME = resume_file
              config.MODEL.RESUME = "pretrain/swin_small_patch4_window7_224.pth"
              config.freeze()
              logger.info(f'auto resuming from {resume_file}')
          else:
              logger.info(f'no checkpoint found in {config.OUTPUT}, ignoring auto resume')
  
      if config.MODEL.RESUME:
  
          max_accuracy = load_checkpoint(config, model_without_ddp, optimizer, lr_scheduler, logger)
          acc1, acc5, loss = validate(config, data_loader_val, model)
          logger.info(f"Accuracy of the network on the {len(dataset_val)} test images: {acc1:.1f}%")
          if config.EVAL_MODE:
              return
  
      if config.MODEL.PRETRAINED and (not config.MODEL.RESUME):
          load_pretrained(config, model_without_ddp, logger)
          acc1, acc5, loss = validate(config, data_loader_val, model)
          logger.info(f"Accuracy of the network on the {len(dataset_val)} test images: {acc1:.1f}%")
  
  
  @torch.no_grad()
  def validate(config, data_loader, model):
      cirterion = torch.nn.CrossEntropyLoss()
      model.eval()
      batch_time = AverageMeter()
      loss_meter = AverageMeter()
      acc1_meter = AverageMeter()
      acc5_meter = AverageMeter()
  
      end = time.time()
      for idx, (images, target) in enumerate(data_loader):
          images = images.cuda(non_blocking=True)
          target = target.cuda(non_blocking=True)
          output = model(images)
  
          loss = cirterion(output, target)
          acc1, acc5 = accuracy(output, target, topk=(1, 5))
  
  
  
  if __name__ == '__main__':
      _, config = parse_option()
      torch.cuda.set_device(0)
      seed = config.SEED
      torch.manual_seed(seed)
      torch.cuda.manual_seed(seed)
      np.random.seed(seed)
      random.seed(seed)
  
      cudnn.benchmark = True
  
      linear_scaled_lr = config.TRAIN.BASE_LR * config.DATA.BATCH_SIZE / 512.0
      linear_scaled_warmup_lr = config.TRAIN.WARMUP_LR * config.DATA.BATCH_SIZE / 512.0
      linear_scaled_min_lr = config.TRAIN.MIN_LR * config.DATA.BATCH_SIZE / 512.0
  
      config.defrost()
      config.TRAIN.BASE_LR = linear_scaled_lr
      config.TRAIN.WARMUP_LR = linear_scaled_warmup_lr
      config.TRAIN.MIN_LR = linear_scaled_min_lr
      config.TEST.SEQUENTIAL = True
      config.freeze()
  
      os.makedirs(config.OUTPUT, exist_ok=True)
      logger = create_logger(output_dir=config.OUTPUT, dist_rank=0, name=f"{config.MODEL.NAME}")
  
      main(config)
  
  
  
  
  
  
  
  
  