将一个pytorch的model转为caffe pytorch生成的model的.pth文件链接 https://ws28.cn/f/82oxyp9z4z1 希望能快一些,有教程就更好不过,万分感谢。
你这pth只存了模型权重,没给网络结构怎么转。。。或者给出你的模型开源链接也行。pytorch转caffe很多坑,比较好的方法是楼上的先转onnx再转caffe,但是转onnx也坑,看你的网络能不能支持onnx的结构,有些网络层不支持你只能自己实现下替换才行
将一个pytorch的model转为caffe pytorch生成的model的.pth文件链接 https://ws28.cn/f/82oxyp9z4z1 希望能快一些,有教程就更好不过,万分感谢。
@wangxiaobei2017
@gaomingda
@baobei0112
@xiaohe9275
@warrentdrew
@vvnzhang2095
@mokchih
整个模型保存到了 https://ws28.cn/f/82xk0gs30a5,FBnet在train的时候每一个epoch都会有这么一个模型 然后以accuracy为标准 如果新一轮模型的accracy更高,就会以bestmodel.pth命名保存下来,FBNet-master\supernet_functions\logs这个是.pth存储的地址。 有偿!有偿!有偿!
转换权重参考:https://blog.csdn.net/xinxiang7/article/details/110822493
参考下这个:https://blog.csdn.net/qq_35975447/article/details/107811574
使用的pytorch模型基本都是动态图结构,动态图的问题是一旦前向不完整就无法确定图结构,而caffe是静态图框架,会导致pytorch到caffe的模型转换为遇到很多问题,建议参考下面这个链接进行转换
https://github.com/xxradon/ONNXToCaffe
教程
https://blog.csdn.net/bill20100829/article/details/124183943
我看网上并没有太成熟的pth转到caffe的项目,不过却有一个很成熟的onnx 转到 caffe的项目,项目地址:https://github.com/xxradon/ONNXToCaffe
项目依赖:
- caffe (with python support)
- pytorch (optional if you want to convert onnx)
- onnx
- onnxruntime
那么就先需要将pth转成onnx,这两个的转换方法比较简单,也能找到不少的解决策略,下面例子:
import torch
import torch.onnx
from tinynet import tinynet
import os
def pth_to_onnx(input, checkpoint, onnx_path, input_names=['input'], output_names=['output'], device='cpu'):
if not onnx_path.endswith('.onnx'):
print('Warning! The onnx model name is not correct,\
please give a name that ends with \'.onnx\'!')
return 0
model = tinynet() #导入模型
model.load_state_dict(torch.load(checkpoint)) #初始化权重
model.eval()
# model.to(device)
torch.onnx.export(model, input, onnx_path, verbose=True, input_names=input_names, output_names=output_names) #指定模型的输入,以及onnx的输出路径
print("Exporting .pth model to onnx model has been successful!")
if __name__ == '__main__':
os.environ['CUDA_VISIBLE_DEVICES']='2'
checkpoint = './model/your_model.pth'
onnx_path = './model/your_model.onnx'
input = torch.randn(input.shape)
# device = torch.device("cuda:2" if torch.cuda.is_available() else 'cpu')
pth_to_onnx(input, checkpoint, onnx_path)
转成onnx后,再使用上面介绍的项目,进行转换即可:
python convertCaffe.py ./model/your_model.onnx ./model/your_model.prototxt ./model/your_model.caffemodel
如有帮助,请采纳,多谢!
没有通用的方法,试图使用通用的代码转换权重是徒劳的,pytorch很多人喜欢用自定义的层,各式各样,根本无法使用通用的代码进行转换。我一般是拿到训练的代码,然后在基础网络层里面添加权重转换代码,在pt网络重建并载入权重后,再调用权重转换方法生成指定类型的权重。
直接转onnx
https://blog.csdn.net/vvnzhang2095/article/details/91439924
可以参考一下这个,python转caffe
可以参考一下这篇文章,讲的很仔细,望采纳~https://zhuanlan.zhihu.com/p/352750171