ChengpengChen / RepGhost

RepGhost: A Hardware-Efficient Ghost Module via Re-parameterization
MIT License
168 stars 17 forks source link

模型转换快速推理结构时的问题 #13

Closed BYFgithub closed 1 year ago

BYFgithub commented 1 year ago

您好,请问使用repghostnet进行训练之后,怎么将其转换为推理时的结构,是使用convert.py文件吗?

ChengpengChen commented 1 year ago

你好!

convert.py 文件是可以转换结构的!其内部起作用的是from model.repghost import repghost_model_convert

也可以调用repghostnet 的方法: model.convert_to_deploy()

以上步骤把重参数化的BN分支合并到Conv层,从而简化了结构。

BYFgithub commented 1 year ago

您好,感谢您的回复! 在repghostnet训练结束后,我使用以下代码 model = torch.load('./mini_pth/repghostV1_best.pth', map_location=device) 直接加载训练好的repghostnet模型在我使用的数据集上进行推理,可以运行。但是使用conver.py文件对repghostV1_best.pth进行转换后得到repghostV1_convert.pth,使用 model = torch.load('./mini_pth/repghostV1_convert.pth', map_location=device) 在同样的数据集上进行推理就一直报错。 是转换后的模型使用方法不对吗?

ChengpengChen commented 1 year ago

你好! 可以提供报错信息,进一步做分析。 另外,我们还没有验证torch.save/torch.load 整个模型的使用方法,有可能是这里的问题。

可以使用pytorch 的旧使用方法,即先定义模型、再load state_dict

from model.repghost import repghost_model_convert

model = RepGhostNet()  # 定义
model.load_state_dict(torch.load('weight.pth'))
eval(model)  # 推理
infer_model = repghost_model_convert(model)
eval(infer_model)  # 简化结构推理
BYFgithub commented 1 year ago

model = repghostnet_1_0x( ).to(device) model.load_state_dict(torch.load('./mini_pth/repghostV1_convert.pth', map_location=device)) 使用这样的方式加载转换后的模型会报错 RuntimeError: Error(s) in loading state_dict for RepGhostNet: Missing key(s) in state_dict: "blocks.0.0.ghost1.fusion_bn.0.weight", "blocks.0.0.ghost1.fusion_bn.0.bias", "blocks.0.0.ghost1.fusion_bn.0.running_mean", "blocks.0.0.ghost1.fusion_bn.0.running_var", "blocks.0.0.ghost1.cheap_operation.0.weight", "blocks.0.0.ghost1.cheap_operation.1.weight", "blocks.0.0.ghost1.cheap_operation.1.bias", "blocks.0.0.ghost1.cheap_operation.1.running_mean", "blocks.0.0.ghost1.cheap_operation.1.running_var", "blocks.0.0.ghost2.fusion_bn.0.weight", "blocks.0.0.ghost2.fusion_bn.0.bias", "blocks.0.0.ghost2.fusion_bn.0.running_mean", "blocks.0.0.ghost2.fusion_bn.0.running_var",

Unexpected key(s) in state_dict: "blocks.0.0.ghost1.cheap_operation.weight", "blocks.0.0.ghost1.cheap_operation.bias", "blocks.0.0.ghost2.cheap_operation.weight", "blocks.0.0.ghost2.cheap_operation.bias", "blocks.1.0.ghost1.cheap_operation.weight", "blocks.1.0.ghost1.cheap_operation.bias", "blocks.1.0.ghost2.cheap_operation.weight", "blocks.1.0.ghost2.cheap_operation.bias", "blocks.2.0.ghost1.cheap_operation.weight", "blocks.2.0.ghost1.cheap_operation.bias", "blocks.2.0.ghost2.cheap_operation.weight", "blocks.2.0.ghost2.cheap_operation.bias", "blocks.3.0.ghost1.cheap_operation.weight", "blocks.3.0.ghost1.cheap_operation.bias", "blocks.3.0.ghost2.cheap_operation.weight", "blocks.3.0.ghost2.cheap_operation.bias", "blocks.4.0.ghost1.cheap_operation.weight"

会报这样的错,难道model不是使用repghostnet吗

ChengpengChen commented 1 year ago

model 还是repghostnet 的。但是./mini_pth/repghostV1_convert.pth 是转换化后的模型参数,需要加载到转换化后的模型中,比如

model = repghostnet_1_0x( ).to(device)
infer_model = repghost_model_convert(model)
infer_model.load_state_dict(torch.load('./mini_pth/repghostV1_convert.pth', map_location=device))

我在https://github.com/ChengpengChen/RepGhost/issues/13#issuecomment-1698421718 中提到的weight.pth是转换前的模型参数。

BYFgithub commented 1 year ago

好的,明白了,非常感谢您的解答!

BYFgithub commented 1 year ago

您好,之后我也尝试在移动端进行推理来看一下模型的延迟,但是遇到了一些问题,想问您关于这部分的代码还在吗,可否分享一下?

ChengpengChen commented 1 year ago

不好意思,我们使用的是一个内部其它团体改进过的MNN,具体的代码还不方便公开;官方的MNN应该也是可行的,其使用方法可以参考官方文档。

除了MNN,我们也尝试了CoreML on iPhone12TFLite no android,这两个按照对应网页的方法就可以了。

BYFgithub commented 1 year ago

好的,感谢您的回复!