PaddlePaddle / PaddleSeg

Easy-to-use image segmentation library with awesome pre-trained model zoo, supporting wide-range of practical tasks in Semantic Segmentation, Interactive Segmentation, Panoptic Segmentation, Image Matting, 3D Segmentation, etc.
https://arxiv.org/abs/2101.06175
Apache License 2.0
8.56k stars 1.68k forks source link

关于segformer进行模型压缩 #3789

Open wwma opened 2 weeks ago

wwma commented 2 weeks ago

问题确认 Search before asking

请提出你的问题 Please ask your question

Global:
  model_dir: slim/output_model
  model_filename: model.pdmodel
  params_filename: model.pdiparams

Distillation:
  alpha: 1.0
  loss: l2
  node:
  - block4.2.mlp.fc2 # change to the name of the output of the last conv in the model.pdmodel in netron

QuantAware:
  onnx_format: False
  quantize_op_types:
  - conv2d
  - depthwise_conv2d

TrainConfig:
  epochs: 1
  eval_iter: 1000
  learning_rate: 0.0005
  optimizer_builder:
    optimizer: 
      type: SGD
    weight_decay: 4.0e-05  

使用的配置文件如上,当不注释Distillation部分是,会提示错误:

I0829 09:00:02.586479 20276 program_interpreter.cc:212] New Executor is Running.
W0829 09:00:02.897599 20276 gpu_resources.cc:119] Please NOTE: device: 0, GPU Compute Capability: 8.6, Driver API Version: 12.2, Runtime API Version: 
11.2
W0829 09:00:02.904767 20276 gpu_resources.cc:164] device: 0, cuDNN Version: 8.2.
2024-08-29 09:00:04,526-INFO: devices: gpu
2024-08-29 09:00:25,280-INFO: Selected strategies: ['qat_dis']
2024-08-29 09:00:27,045-INFO: train config.distill_node_pair: ['block4.2.mlp.fc2']
Traceback (most recent call last):
  File "deploy\slim\act\run_seg.py", line 205, in <module>
    main(args)
  File "deploy\slim\act\run_seg.py", line 199, in main
    ac.compress()
  File "C:\Users\DELL\.conda\envs\paddle\lib\site-packages\paddleslim\auto_compression\compressor.py", line 586, in compress
    self.single_strategy_compress(strategy, config, strategy_idx,
  File "C:\Users\DELL\.conda\envs\paddle\lib\site-packages\paddleslim\auto_compression\compressor.py", line 769, in single_strategy_compress
    train_program_info, test_program_info = self._prepare_program(
  File "C:\Users\DELL\.conda\envs\paddle\lib\site-packages\paddleslim\auto_compression\compressor.py", line 506, in _prepare_program
    train_program_info, test_program_info = build_distill_program(
  File "C:\Users\DELL\.conda\envs\paddle\lib\site-packages\paddleslim\auto_compression\create_compressed_program.py", line 380, in build_distill_program
    distill_loss, loss_dict = _parse_distill_loss(
  File "C:\Users\DELL\.conda\envs\paddle\lib\site-packages\paddleslim\auto_compression\create_compressed_program.py", line 163, in _parse_distill_loss    assert len(node) % 2 == 0, \
AssertionError: distill_node_pair config wrong, the length needs to be an even number

参考https://github.com/PaddlePaddle/PaddleSlim/issues/1631,对Distillation部分进行注释,提示错误

2024-08-29 09:04:13,725-INFO: Selected strategies: []
Traceback (most recent call last):
  File "deploy\slim\act\run_seg.py", line 205, in <module>
    main(args)
  File "deploy\slim\act\run_seg.py", line 199, in main
    ac.compress()
  File "C:\Users\DELL\.conda\envs\paddle\lib\site-packages\paddleslim\auto_compression\compressor.py", line 577, in compress
    assert len(self._strategy) > 0
AssertionError

这是说明模型没有自动寻找最后一个带参数节点作为蒸馏节点吗? 同时使用了netron可视化模型,某些层输出Tensor名称如下: image 并没有输出名称

zhang-prog commented 2 weeks ago
  - block4.2.mlp.fc2 # change to the name of the output of the last conv in the model.pdmodel in netron

这里有说明 model.pdmodel in netron。 看起来你在netron 中导入的是 model.pdparams ,所以看不到正确的模型结构。应该参考这里导出预测模型,将 model.pdmodel 放入netron 中。

wwma commented 2 weeks ago

非常感谢,现在已经可以正常运行了

zhang-prog commented 2 weeks ago

好的