code: def main(args):
      env_info = utils.get_sys_env()
  
      place = "gpu" if env_info["GPUs used"] else "cpu"
      utils.set_device(place)
  
      if not (0.0 < args.pruning_ratio < 1.0):
          raise RuntimeError(
              "The model pruning rate must be in the range of (0, 1)."
          )
  
      if not os.path.exists(args.save_dir):
          os.makedirs(args.save_dir)
  
      os.environ["PADDLESEG_EXPORT_STAGE"] = "True"
  
      cfg = Config(
          args.cfg,
          iters=args.retraining_iters,
          batch_size=args.batch_size,
          learning_rate=args.learning_rate,
          opts=args.opts,
      )
      builder = SegBuilder(cfg)
  
      train_dataset = builder.train_dataset
      val_dataset = builder.val_dataset
      net = builder.model
  
      if args.model_path:
          para_state_dict = paddle.load(args.model_path)
          net.set_dict(para_state_dict)
          logger.info("Loaded trained params of model successfully")
  
      logger.info(
          "Step 1/3: Start calculating the sensitivity of model parameters..."
      )
      sample_shape = [1] + list(train_dataset[0]["img"].shape)
      sen_file = os.path.join(args.save_dir, "sen.pickle")
      pruner = L1NormFilterPruner(net, sample_shape)
      pruner.sensitive(
          eval_func=partial(
              eval_fn, net, val_dataset, args.num_workers
          ),
          sen_file=sen_file,
      )
      logger.info(
          f"The sensitivity calculation of model parameters is complete. The result is saved in {sen_file}."
      )
  
      flops = dygraph_flops(net, sample_shape)
      logger.info(
          f"Step 2/3: Start to prune the model, the ratio of pruning is {args.pruning_ratio}. FLOPs before pruning: {flops}."
      )
  
      # Avoid the bug when pruning conv2d with small channel number.
      # Remove this code after PaddleSlim 2.1 is available.
      # Related issue: https://github.com/PaddlePaddle/PaddleSlim/issues/674.
      skips = []
      for param in net.parameters():
          if param.shape[0] <= 8:
              skips.append(param.name)
  
      pruner.sensitive_prune(args.pruning_ratio, skip_vars=skips)
      flops = dygraph_flops(net, sample_shape)
      logger.info(
          f"Model pruning completed. FLOPs after pruning: {flops}."
      )
  
      logger.info(f"Step 3/3: Start retraining the model.")
      train(
          net,
          train_dataset,
          optimizer=builder.optimizer,
          save_dir=args.save_dir,
          num_workers=args.num_workers,
          iters=cfg.iters,
          batch_size=cfg.batch_size,
          losses=builder.loss,
      )
  
      evaluate(net, val_dataset)
  
      if paddle.distributed.get_rank() == 0:
          export_model(net, val_dataset, cfg, args.save_dir)
  
          ckpt = os.path.join(
              args.save_dir, f"iter_{args.retraining_iters}"
          )
          if os.path.exists(ckpt):
              shutil.rmtree(ckpt)
  
      logger.info(
          f"Model retraining finish. Model is saved in {args.save_dir}"
      )
  
file_path: PaddleSeg/deploy/slim/prune/prune.py
imports: import argparse
  import os
  import shutil
  from functools import partial
  import yaml
  import paddle
  from paddleslim.dygraph import L1NormFilterPruner
  from paddleslim.analysis import dygraph_flops
  from paddleseg.cvlibs import Config, SegBuilder
  from paddleseg.core.val import evaluate
  from paddleseg.core.train import train
  from paddleseg.utils import logger, utils
label_desc: Use image node_id 4 for calling a global function with example usage: main(args) without return types