昇腾模型压缩工具目前主要支持基于重训练的通道稀疏模型压缩特性,稀疏示例请参见获取更多样例>resnet101,支持通道稀疏的层以及约束如下:
优化方式 |
层名 |
约束 |
---|---|---|
通道稀疏 |
torch.nn.Linear:全连接层 |
depthwise只能被动稀疏(groups=in_channels),不能主动稀疏。 复用层(共用weight和bias参数)不支持稀疏。 |
torch.nn.Conv2d:卷积层 |
复用层(共用weight和bias参数)不支持稀疏。 |
通道稀疏功能接口调用流程如图1所示。
import amct_pytorch as amct
推荐执行该步骤,请确保原始模型可以完成推理且精度正常;执行该步骤时,可以使用部分测试集,减少运行时间。
ori_model.load() # 测试模型 user_test_model(ori_model, test_data, test_iterations)
simple_cfg = './retrain.cfg' record_file = './tmp/record.txt' prune_retrain_model = amct.create_prune_retrain_model(model=ori_model, input_data=ori_model_input_data, config_defination=simple_cfg, record_file=record_file)
如果训练过程中断,需要从ckpt中恢复数据,继续训练,则调用流程为:
import amct_pytorch as amct
ori_model= user_create_model()
simple_cfg = './retrain.cfg' record_file = './tmp/record.txt' prune_retrain_model = amct.restore_prune_retrain_model(model=ori_model, input_data=ori_model_input_data, record_file=record_file)
经过稀疏后输出的模型为pth格式,该模型无法直接用于推理,需要用户自行将pth模型转成ONNX网络模型,然后才能使用ATC工具进行模型转换。