PaddlePaddle / PaddleOCR

Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
https://paddlepaddle.github.io/PaddleOCR/
Apache License 2.0
44k stars 7.8k forks source link

PaddleOCR 识别模型裁剪和量化问题 #4764

Closed chccc1994 closed 2 years ago

chccc1994 commented 2 years ago

裁剪:

cd PaddleOCR

python deploy/slim/prune/sensitivity_anal.py -c configs/rec_record/rec_r34_vd_none_bilstm_ctc_ccpd.yml -o Global.pretrained_model=output/rec/r34_vd_none_bilstm_ctc_ccpd/best_accuracy Global.save_model_dir=./output/prune_model_ccpd/
W1126 09:49:51.398372 46912 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 8.6, Driver API Version: 11.3, Runtime API Version: 11.0
W1126 09:49:51.406190 46912 device_context.cc:465] device: 0, cuDNN Version: 8.0.
<class 'paddle.nn.layer.pooling.AvgPool2D'>'s flops has been counted
<class 'paddle.nn.layer.conv.Conv2D'>'s flops has been counted
<class 'paddle.fluid.dygraph.nn.BatchNorm'>'s flops has been counted
Cannot find suitable count function for <class 'paddle.nn.layer.pooling.MaxPool2D'>. Treat it as zero FLOPs.
Cannot find suitable count function for <class 'ppocr.modeling.necks.rnn.Im2Seq'>. Treat it as zero FLOPs.
Cannot find suitable count function for <class 'paddle.nn.layer.rnn.LSTMCell'>. Treat it as zero FLOPs.
<class 'paddle.nn.layer.common.Linear'>'s flops has been counted
Traceback (most recent call last):
  File "deploy/slim/prune/sensitivity_anal.py", line 147, in <module>
    main(config, device, logger, vdl_writer)
  File "deploy/slim/prune/sensitivity_anal.py", line 77, in main
    flops = paddle.flops(model, [1, 3, 640, 640])
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\hapi\dynamic_flops.py", line 108, in flops
    print_detail=print_detail)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\hapi\dynamic_flops.py", line 249, in dynamic_flops
    model(inputs)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\fluid\dygraph\layers.py", line 914, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "deploy/slim/prune\..\..\..\ppocr\modeling\architectures\base_model.py", line 78, in forward
    x = self.neck(x)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\fluid\dygraph\layers.py", line 914, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "deploy/slim/prune\..\..\..\ppocr\modeling\necks\rnn.py", line 89, in forward
    x = self.encoder_reshape(x)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\fluid\dygraph\layers.py", line 914, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "deploy/slim/prune\..\..\..\ppocr\modeling\necks\rnn.py", line 31, in forward
    assert H == 1
AssertionError

量化

python deploy/slim/quantization/quant.py -c configs/rec_record/rec_r34_vd_none_bilstm_ctc_ccpd.yml -o Global.pretrain_weights=./output/rec/r34_vd_none_bilstm_ctc_ccpd/best_accuracy   Global.save_model_dir=./output/quant_model_ccpd

PaddlePaddle版本 : paddlepaddle-gpu 2.2.0.post110 PaddleSlim版本: 2.2.0、 2.0.0 拉取PaddleSlim ,执行 python setup.py install 安装版本 以上3个版本都测试

这个问题怎么解决??

很奇怪的问题,在AiStudio 上paddlepaddle-gpu==2.2.0 PaddleSlim 拉取安装python setup.py install 可以执行量化和压缩。 在本机电脑就出现以上错误???

YinZhang98 commented 2 years ago

请问解决了吗?我也遇到了这个问题

YinZhang98 commented 2 years ago

裁剪:

cd PaddleOCR

python deploy/slim/prune/sensitivity_anal.py -c configs/rec_record/rec_r34_vd_none_bilstm_ctc_ccpd.yml -o Global.pretrained_model=output/rec/r34_vd_none_bilstm_ctc_ccpd/best_accuracy Global.save_model_dir=./output/prune_model_ccpd/
  • 裁剪错误日志
W1126 09:49:51.398372 46912 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 8.6, Driver API Version: 11.3, Runtime API Version: 11.0
W1126 09:49:51.406190 46912 device_context.cc:465] device: 0, cuDNN Version: 8.0.
<class 'paddle.nn.layer.pooling.AvgPool2D'>'s flops has been counted
<class 'paddle.nn.layer.conv.Conv2D'>'s flops has been counted
<class 'paddle.fluid.dygraph.nn.BatchNorm'>'s flops has been counted
Cannot find suitable count function for <class 'paddle.nn.layer.pooling.MaxPool2D'>. Treat it as zero FLOPs.
Cannot find suitable count function for <class 'ppocr.modeling.necks.rnn.Im2Seq'>. Treat it as zero FLOPs.
Cannot find suitable count function for <class 'paddle.nn.layer.rnn.LSTMCell'>. Treat it as zero FLOPs.
<class 'paddle.nn.layer.common.Linear'>'s flops has been counted
Traceback (most recent call last):
  File "deploy/slim/prune/sensitivity_anal.py", line 147, in <module>
    main(config, device, logger, vdl_writer)
  File "deploy/slim/prune/sensitivity_anal.py", line 77, in main
    flops = paddle.flops(model, [1, 3, 640, 640])
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\hapi\dynamic_flops.py", line 108, in flops
    print_detail=print_detail)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\hapi\dynamic_flops.py", line 249, in dynamic_flops
    model(inputs)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\fluid\dygraph\layers.py", line 914, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "deploy/slim/prune\..\..\..\ppocr\modeling\architectures\base_model.py", line 78, in forward
    x = self.neck(x)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\fluid\dygraph\layers.py", line 914, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "deploy/slim/prune\..\..\..\ppocr\modeling\necks\rnn.py", line 89, in forward
    x = self.encoder_reshape(x)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\fluid\dygraph\layers.py", line 914, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "deploy/slim/prune\..\..\..\ppocr\modeling\necks\rnn.py", line 31, in forward
    assert H == 1
AssertionError

量化

python deploy/slim/quantization/quant.py -c configs/rec_record/rec_r34_vd_none_bilstm_ctc_ccpd.yml -o Global.pretrain_weights=./output/rec/r34_vd_none_bilstm_ctc_ccpd/best_accuracy   Global.save_model_dir=./output/quant_model_ccpd
  • 量化错误日志
W1126 10:49:18.884593  7304 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 8.6, Driver API Version: 11.3, Runtime API Version: 11.0
W1126 10:49:18.900218  7304 device_context.cc:465] device: 0, cuDNN Version: 8.0.
INFO:root:If regularizer of a Parameter has been set by 'paddle.ParamAttr' or 'static.WeightNormParamAttr' already. The weight_decay[L2Decay, regularization_coeff=0.000000] in Optimizer will not take effect, and it will only be applied to other Parameters!
Traceback (most recent call last):
  File "deploy/slim/quantization/quant.py", line 162, in <module>
    main(config, device, logger, vdl_writer)
  File "deploy/slim/quantization/quant.py", line 149, in main
    pre_best_model_dict = init_model(config, model, logger, optimizer)
  File "E:\Paddle\PaddleOCR\ppocr\utils\save_load.py", line 65, in init_model
    optimizer.set_state_dict(opti_dict)
AttributeError: 'Logger' object has no attribute 'set_state_dict

PaddlePaddle版本 : paddlepaddle-gpu 2.2.0.post110 PaddleSlim版本: 2.2.0、 2.0.0 拉取PaddleSlim ,执行 python setup.py install 安装版本 以上3个版本都测试

这个问题怎么解决??

很奇怪的问题,在AiStudio 上paddlepaddle-gpu==2.2.0 PaddleSlim 拉取安装python setup.py install 可以执行量化和压缩。 在本机电脑就出现以上错误???

配置文件里面好像不能写加载预训练模型和断点

chccc1994 commented 2 years ago

裁剪:

cd PaddleOCR

python deploy/slim/prune/sensitivity_anal.py -c configs/rec_record/rec_r34_vd_none_bilstm_ctc_ccpd.yml -o Global.pretrained_model=output/rec/r34_vd_none_bilstm_ctc_ccpd/best_accuracy Global.save_model_dir=./output/prune_model_ccpd/
  • 裁剪错误日志
W1126 09:49:51.398372 46912 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 8.6, Driver API Version: 11.3, Runtime API Version: 11.0
W1126 09:49:51.406190 46912 device_context.cc:465] device: 0, cuDNN Version: 8.0.
<class 'paddle.nn.layer.pooling.AvgPool2D'>'s flops has been counted
<class 'paddle.nn.layer.conv.Conv2D'>'s flops has been counted
<class 'paddle.fluid.dygraph.nn.BatchNorm'>'s flops has been counted
Cannot find suitable count function for <class 'paddle.nn.layer.pooling.MaxPool2D'>. Treat it as zero FLOPs.
Cannot find suitable count function for <class 'ppocr.modeling.necks.rnn.Im2Seq'>. Treat it as zero FLOPs.
Cannot find suitable count function for <class 'paddle.nn.layer.rnn.LSTMCell'>. Treat it as zero FLOPs.
<class 'paddle.nn.layer.common.Linear'>'s flops has been counted
Traceback (most recent call last):
  File "deploy/slim/prune/sensitivity_anal.py", line 147, in <module>
    main(config, device, logger, vdl_writer)
  File "deploy/slim/prune/sensitivity_anal.py", line 77, in main
    flops = paddle.flops(model, [1, 3, 640, 640])
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\hapi\dynamic_flops.py", line 108, in flops
    print_detail=print_detail)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\hapi\dynamic_flops.py", line 249, in dynamic_flops
    model(inputs)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\fluid\dygraph\layers.py", line 914, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "deploy/slim/prune\..\..\..\ppocr\modeling\architectures\base_model.py", line 78, in forward
    x = self.neck(x)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\fluid\dygraph\layers.py", line 914, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "deploy/slim/prune\..\..\..\ppocr\modeling\necks\rnn.py", line 89, in forward
    x = self.encoder_reshape(x)
  File "D:\Program Files\Anaconda3\envs\paddle\lib\site-packages\paddle\fluid\dygraph\layers.py", line 914, in __call__
    outputs = self.forward(*inputs, **kwargs)
  File "deploy/slim/prune\..\..\..\ppocr\modeling\necks\rnn.py", line 31, in forward
    assert H == 1
AssertionError

量化

python deploy/slim/quantization/quant.py -c configs/rec_record/rec_r34_vd_none_bilstm_ctc_ccpd.yml -o Global.pretrain_weights=./output/rec/r34_vd_none_bilstm_ctc_ccpd/best_accuracy   Global.save_model_dir=./output/quant_model_ccpd
  • 量化错误日志
W1126 10:49:18.884593  7304 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 8.6, Driver API Version: 11.3, Runtime API Version: 11.0
W1126 10:49:18.900218  7304 device_context.cc:465] device: 0, cuDNN Version: 8.0.
INFO:root:If regularizer of a Parameter has been set by 'paddle.ParamAttr' or 'static.WeightNormParamAttr' already. The weight_decay[L2Decay, regularization_coeff=0.000000] in Optimizer will not take effect, and it will only be applied to other Parameters!
Traceback (most recent call last):
  File "deploy/slim/quantization/quant.py", line 162, in <module>
    main(config, device, logger, vdl_writer)
  File "deploy/slim/quantization/quant.py", line 149, in main
    pre_best_model_dict = init_model(config, model, logger, optimizer)
  File "E:\Paddle\PaddleOCR\ppocr\utils\save_load.py", line 65, in init_model
    optimizer.set_state_dict(opti_dict)
AttributeError: 'Logger' object has no attribute 'set_state_dict

PaddlePaddle版本 : paddlepaddle-gpu 2.2.0.post110 PaddleSlim版本: 2.2.0、 2.0.0 拉取PaddleSlim ,执行 python setup.py install 安装版本 以上3个版本都测试 这个问题怎么解决?? 很奇怪的问题,在AiStudio 上paddlepaddle-gpu==2.2.0 PaddleSlim 拉取安装python setup.py install 可以执行量化和压缩。 在本机电脑就出现以上错误???

配置文件里面好像不能写加载预训练模型和断点

对在AiStudio上测试,是不能添加checkpoints。本地还没有查到问题出在什么地方?

YinZhang98 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

chccc1994 commented 2 years ago

@MissPenguin

YinZhang98 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

chccc1994 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

YinZhang98 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

chccc1994 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

YinZhang98 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

我也用的2.3,我吧checkpoints去掉就没用错了 image 不过这loss有负值,不知道正不正常 image

chccc1994 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

我也用的2.3,我吧checkpoints去掉就没用错了 image 不过这loss有负值,不知道正不正常 image

你用的PaddlePaddle版本和PaddleSlim版本多少?能发一下你的yml配置文件吗?

YinZhang98 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

我也用的2.3,我吧checkpoints去掉就没用错了 image 不过这loss有负值,不知道正不正常 image

你用的PaddlePaddle版本和PaddleSlim版本多少?能发一下你的yml配置文件吗?

paddlepaddle-gpu 2.2.0 我按照这个链接弄的 https://github.com/PaddlePaddle/PaddleOCR/tree/develop/deploy/slim/prune 目前正在数据敏感分析,后面也要剪枝识别

chccc1994 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

我也用的2.3,我吧checkpoints去掉就没用错了 image 不过这loss有负值,不知道正不正常 image

你用的PaddlePaddle版本和PaddleSlim版本多少?能发一下你的yml配置文件吗?

paddlepaddle-gpu 2.2.0 我按照这个链接弄的 https://github.com/PaddlePaddle/PaddleOCR/tree/develop/deploy/slim/prune 目前正在数据敏感分析,后面也要剪枝识别

我也是按照这个步骤来的。。。AiStudio上没问题,本机测试就不行了

YinZhang98 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

我也用的2.3,我吧checkpoints去掉就没用错了 image 不过这loss有负值,不知道正不正常 image

你用的PaddlePaddle版本和PaddleSlim版本多少?能发一下你的yml配置文件吗?

paddlepaddle-gpu 2.2.0 我按照这个链接弄的 https://github.com/PaddlePaddle/PaddleOCR/tree/develop/deploy/slim/prune 目前正在数据敏感分析,后面也要剪枝识别

我也是按照这个步骤来的。。。AiStudio上没问题,本机测试就不行了

我看官方并没有对识别进行裁剪,是不是这方面有问题,还是就是rnn裁剪方面我感觉直接裁剪会有问题

chccc1994 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

我也用的2.3,我吧checkpoints去掉就没用错了 image 不过这loss有负值,不知道正不正常 image

你用的PaddlePaddle版本和PaddleSlim版本多少?能发一下你的yml配置文件吗?

paddlepaddle-gpu 2.2.0 我按照这个链接弄的 https://github.com/PaddlePaddle/PaddleOCR/tree/develop/deploy/slim/prune 目前正在数据敏感分析,后面也要剪枝识别

我也是按照这个步骤来的。。。AiStudio上没问题,本机测试就不行了

我看官方并没有对识别进行裁剪,是不是这方面有问题,还是就是rnn裁剪方面我感觉直接裁剪会有问题

在裁剪模型导出.py中,有“model_type="rec"”, 应该是支持吧?

YinZhang98 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

我也用的2.3,我吧checkpoints去掉就没用错了 image 不过这loss有负值,不知道正不正常 image

你用的PaddlePaddle版本和PaddleSlim版本多少?能发一下你的yml配置文件吗?

paddlepaddle-gpu 2.2.0 我按照这个链接弄的 https://github.com/PaddlePaddle/PaddleOCR/tree/develop/deploy/slim/prune 目前正在数据敏感分析,后面也要剪枝识别

我也是按照这个步骤来的。。。AiStudio上没问题,本机测试就不行了

我看官方并没有对识别进行裁剪,是不是这方面有问题,还是就是rnn裁剪方面我感觉直接裁剪会有问题

在裁剪模型导出.py中,有“model_type="rec"”, 应该是支持吧?

你要把skip那行删掉,输入的大小改一下,就可以了

YinZhang98 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

我也用的2.3,我吧checkpoints去掉就没用错了 image 不过这loss有负值,不知道正不正常 image

你用的PaddlePaddle版本和PaddleSlim版本多少?能发一下你的yml配置文件吗?

paddlepaddle-gpu 2.2.0 我按照这个链接弄的 https://github.com/PaddlePaddle/PaddleOCR/tree/develop/deploy/slim/prune 目前正在数据敏感分析,后面也要剪枝识别

我也是按照这个步骤来的。。。AiStudio上没问题,本机测试就不行了

我看官方并没有对识别进行裁剪,是不是这方面有问题,还是就是rnn裁剪方面我感觉直接裁剪会有问题

在裁剪模型导出.py中,有“model_type="rec"”, 应该是支持吧?

你要把skip那行删掉,输入的大小改一下,就可以了

Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

#

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

#

http://www.apache.org/licenses/LICENSE-2.0

#

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

from future import absolute_import from future import division from future import print_function

import os import sys

dir = os.path.dirname(file) sys.path.append(dir) sys.path.append(os.path.join(dir, '..', '..', '..')) sys.path.append(os.path.join(dir, '..', '..', '..', 'tools'))

import paddle import paddle.distributed as dist from ppocr.data import build_dataloader from ppocr.modeling.architectures import build_model from ppocr.losses import build_loss from ppocr.optimizer import build_optimizer from ppocr.postprocess import build_post_process from ppocr.metrics import build_metric from ppocr.utils.save_load import init_model import tools.program as program

dist.get_world_size()

def get_pruned_params(parameters): params = []

for param in parameters:
    if len(
            param.shape
    ) == 4 and 'depthwise' not in param.name and 'transpose' not in param.name and "conv2d_57" not in param.name and "conv2d_56" not in param.name:
        params.append(param.name)
return params

def main(config, device, logger, vdl_writer):

init dist environment

if config['Global']['distributed']:
    dist.init_parallel_env()

global_config = config['Global']

# build dataloader
train_dataloader = build_dataloader(config, 'Train', device, logger)
if config['Eval']:
    valid_dataloader = build_dataloader(config, 'Eval', device, logger)
else:
    valid_dataloader = None

# build post process
post_process_class = build_post_process(config['PostProcess'],
                                        global_config)

# build model
# for rec algorithm
if hasattr(post_process_class, 'character'):
    char_num = len(getattr(post_process_class, 'character'))
    config['Architecture']["Head"]['out_channels'] = char_num
model = build_model(config['Architecture'])

flops = paddle.flops(model, [1, 3, 32, 100])
logger.info("FLOPs before pruning: {}".format(flops))

from paddleslim.dygraph import FPGMFilterPruner
model.train()
pruner = FPGMFilterPruner(model, [1, 3, 32, 100])

# build loss
loss_class = build_loss(config['Loss'])

# build optim
optimizer, lr_scheduler = build_optimizer(
    config['Optimizer'],
    epochs=config['Global']['epoch_num'],
    step_each_epoch=len(train_dataloader),
    parameters=model.parameters())

# build metric
eval_class = build_metric(config['Metric'])
# load pretrain model
pre_best_model_dict = init_model(config, model, logger, optimizer)

logger.info('train dataloader has {} iters, valid dataloader has {} iters'.
            format(len(train_dataloader), len(valid_dataloader)))
# build metric
eval_class = build_metric(config['Metric'])

logger.info('train dataloader has {} iters, valid dataloader has {} iters'.
            format(len(train_dataloader), len(valid_dataloader)))

def eval_fn():
    metric = program.eval(model, valid_dataloader, post_process_class,
                          eval_class, False)
    logger.info("metric['acc']: {}".format(metric['acc']))
    return metric['acc']

params_sensitive = pruner.sensitive(
    eval_func=eval_fn,
    sen_file="./sen-rec.pickle"
    #,
    # skip_vars=[
    #     "conv2d_57.w_0", "conv2d_transpose_2.w_0", "conv2d_transpose_3.w_0"
    # ]
    )

logger.info(
    "The sensitivity analysis results of model parameters saved in sen.pickle"
)
# calculate pruned params's ratio
params_sensitive = pruner._get_ratios_by_loss(params_sensitive, loss=0.02)
for key in params_sensitive.keys():
    logger.info("{}, {}".format(key, params_sensitive[key]))

#params_sensitive = {}
#for param in model.parameters():
#    if 'transpose' not in param.name and 'linear' not in param.name:
#        params_sensitive[param.name] = 0.1  

plan = pruner.prune_vars(params_sensitive, [0])

flops = paddle.flops(model, [1, 3, 32, 100])
logger.info("FLOPs after pruning: {}".format(flops))

# start train

program.train(config, train_dataloader, valid_dataloader, device, model,
              loss_class, optimizer, lr_scheduler, post_process_class,
              eval_class, pre_best_model_dict, logger, vdl_writer)

if name == 'main': config, device, logger, vdl_writer = program.preprocess(is_train=True) main(config, device, logger, vdl_writer)

YinZhang98 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

我也用的2.3,我吧checkpoints去掉就没用错了 image 不过这loss有负值,不知道正不正常 image

你用的PaddlePaddle版本和PaddleSlim版本多少?能发一下你的yml配置文件吗?

paddlepaddle-gpu 2.2.0 我按照这个链接弄的 https://github.com/PaddlePaddle/PaddleOCR/tree/develop/deploy/slim/prune 目前正在数据敏感分析,后面也要剪枝识别

我也是按照这个步骤来的。。。AiStudio上没问题,本机测试就不行了

我看官方并没有对识别进行裁剪,是不是这方面有问题,还是就是rnn裁剪方面我感觉直接裁剪会有问题

在裁剪模型导出.py中,有“model_type="rec"”, 应该是支持吧?

你要把skip那行删掉,输入的大小改一下,就可以了

上面就是我改的文件,你可以试一下,我的现在能跑

chccc1994 commented 2 years ago

本地还没有查到问题出在什么地方?

你有没有遇到检测模型数据敏感分析的时候loss变为负值了

本地好像也不能添加checkpoints

本地也没有添加checkpoints,报以上错误。 模型数据敏感分析没有出现负值。

能说一下你Paddleocr的版本吗

PaddleOCR 2.3

我也用的2.3,我吧checkpoints去掉就没用错了 image 不过这loss有负值,不知道正不正常 image

你用的PaddlePaddle版本和PaddleSlim版本多少?能发一下你的yml配置文件吗?

paddlepaddle-gpu 2.2.0 我按照这个链接弄的 https://github.com/PaddlePaddle/PaddleOCR/tree/develop/deploy/slim/prune 目前正在数据敏感分析,后面也要剪枝识别

我也是按照这个步骤来的。。。AiStudio上没问题,本机测试就不行了

我看官方并没有对识别进行裁剪,是不是这方面有问题,还是就是rnn裁剪方面我感觉直接裁剪会有问题

在裁剪模型导出.py中,有“model_type="rec"”, 应该是支持吧?

你要把skip那行删掉,输入的大小改一下,就可以了

上面就是我改的文件,你可以试一下,我的现在能跑

Traceback (most recent call last):
  File "deploy/slim/prune/sensitivity_anal.py", line 146, in <module>
    main(config, device, logger, vdl_writer)
  File "deploy/slim/prune/sensitivity_anal.py", line 76, in main
    flops = paddle.flops(model, [1, 3, 640, 640])

很奇怪,我修改 flops = paddle.flops(model, [1, 3, 32, 100]) 。还是以上报错

WenmuZhou commented 2 years ago

sensitivity_anal.py和export_prune_model.py文件中有许多 [1,3,640,640],把这些改为[1,3,32,320]看看

chccc1994 commented 2 years ago

sensitivity_anal.py和export_prune_model.py文件中有许多 [1,3,640,640],把这些改为[1,3,32,320]看看

谢谢,已经找到问题了。