def torch2caffe(model_file): net = build_ssd('export', 300, 8, 'mobilenet_v1') net.load_weights(model_file) net.eval() x = torch.randn(1, 3, 300, 300) pytorch_to_caffe.trans_net(net, x, 'mbv1') pytorch_to_caffe.save_prototxt('{}.prototxt'.format('mbv1')) pytorch_to_caffe.save_caffemodel('{}.caffemodel'.format('mbv1'))
def transfer_to_caffe(): name = "TransformerNet" # device = torch.device("cuda" if torch.cuda.is_available() else "cpu") device = torch.device("cpu") transformer = TransformerNet_self_bn().to(device) transformer.load_state_dict( torch.load(args.checkpoint_model, map_location=torch.device('cpu'))) transformer.eval() input = Variable(torch.ones([1, 3, 480, 640])) pytorch_to_caffe.trans_net(transformer, input, name) pytorch_to_caffe.save_prototxt('{}.prototxt'.format(name)) pytorch_to_caffe.save_caffemodel('{}.caffemodel'.format(name))
def pytorch2caffe(model, input_shape): """Convert the pytorch model to onnx model. :param model: pytorch model class :type model: class :param input_shape: the shape of input :type input_shape: list :param onnx_save_path: the path and filename to save the onnx model file :type onnx_save_path: str """ name = 'torch2caffe' model.eval() input = Variable(torch.ones(input_shape)).cuda() pytorch_to_caffe.trans_net(model, input, name) pytorch_to_caffe.save_prototxt('{}.prototxt'.format(name)) pytorch_to_caffe.save_caffemodel('{}.caffemodel'.format(name)) logging.info("pytorch2caffe finished.") return '{}.prototxt'.format(name), '{}.caffemodel'.format(name)
def pytorch2caffe(model, input_shape, save_dir): """Convert the pytorch model to onnx model. :param model: pytorch model class :type model: class :param input_shape: the shape of input :type input_shape: list :param onnx_save_path: the path and filename to save the onnx model file :type onnx_save_path: str """ import pytorch_to_caffe # noqa name = 'torch2caffe' model = model.cpu() model.eval() input = Variable(torch.ones(input_shape)) pytorch_to_caffe.trans_net(model, input, name) prototxt_file = os.path.join(save_dir, "torch2caffe.prototxt") caffemodel_file = os.path.join(save_dir, "torch2caffe.caffemodel") pytorch_to_caffe.save_prototxt(prototxt_file) pytorch_to_caffe.save_caffemodel(caffemodel_file) logging.info("pytorch2caffe finished.")
def run_pytorch_to_caffe(name, output_dir, pretrained=True, input_size=224, debug=False): print("-------- Run pytorch to caffe --------") # TODO: save output to log? if hasattr(gen_efficientnet, name): model_cls = getattr(gen_efficientnet, name) elif hasattr(gen_overall_model, name): model_cls = getattr(gen_overall_model, name) elif hasattr(proxyless_nas, name): model_cls = getattr(proxyless_nas, name) elif hasattr(models, name): model_cls = getattr(models, name) else: raise Exception() net = model_cls(pretrained=pretrained) net.eval() inputs = Variable(torch.ones([1, 3, input_size, input_size])) if not debug: backup_stdout = sys.stdout sys.stdout = open("/dev/null", "w") pytorch_to_caffe.trans_net(net, inputs, name) if not debug: sys.stdout = backup_stdout dest = output_dir os.makedirs(dest, exist_ok=True) out_proto = "{}/{}.prototxt".format(dest, name) out_caffemodel = "{}/{}.caffemodel".format(dest, name) pytorch_to_caffe.save_prototxt(out_proto) pytorch_to_caffe.save_caffemodel(out_caffemodel) print("Finish convert pytorch model to caffe, check {} and {}.".format( out_proto, out_caffemodel)) return out_proto, out_caffemodel
def __init__(self, layer=1, channels=32): super(depthwise_conv, self).__init__() layers = [] for i in range(layer): layers.append(DepthwiseConv(channels, channels)) self.layers = nn.Sequential(*layers) def forward(self, x): return self.layers(x) if __name__ == '__main__': input_tensor = Variable(torch.randn([8, 64, 224, 224])) model = invertedresidual(layer=1, channels=64) name = f'exp09/blocks' caffe_model_name = f'invertedresidual_b8_s224_l1_c64_r300_fp16' model.eval() save_path = '/home/pyf/codeforascend/PytorchToCaffe/converted_models' # name = f'exp07/ConvolutionLayers' print(f'{save_path}/{name}') os.system(f'mkdir {save_path}/{name}') # caffe_model_name = f'ConvolutionLayers' pytorch_to_caffe.trans_net(model, input_tensor, caffe_model_name) pytorch_to_caffe.save_prototxt( f'{save_path}/{name}/{caffe_model_name}.prototxt') pytorch_to_caffe.save_caffemodel( f'{save_path}/{name}/{caffe_model_name}.caffemodel')
#coding=utf-8 import sys sys.path.insert(0, '.') import torch from torch.autograd import Variable from torchvision.models.inception import inception_v3 import pytorch_to_caffe if __name__ == '__main__': name = 'inception_v3' net = inception_v3(True, transform_input=False) net.eval() input_ = torch.ones([1, 3, 299, 299]) input = input_.to('cpu') pytorch_to_caffe.trans_net(net, input, name) pytorch_to_caffe.save_prototxt('{}.prototxt'.format(name)) pytorch_to_caffe.save_caffemodel('{}.caffemodel'.format(name))
self.conv1 = nn.Conv2d(1, 6, (5, 5)) self.conv2 = nn.Conv2d(6, 16, (5, 5)) self.fc1 = nn.Linear(256, 120) self.fc2 = nn.Linear(120, 84) self.fc3 = nn.Linear(84, 10) def forward(self, x): print("in forward function, type of x is: ") print(type(x)) x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2)) x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2)) x = x.view(x.size()[0], -1) x = F.relu(self.fc1(x)) x = F.relu(self.fc2(x)) x = F.relu(self.fc3(x)) return x if __name__ == '__main__': model_path = "./lenet.pth" model = LeNet() model = torch.load(model_path) model.eval() name = "LeNet" input = torch.ones([1, 1, 28, 28]) pytorch_to_caffe.trans_net(model, input, name) pytorch_to_caffe.save_prototxt('{}.prototxt'.format(name)) pytorch_to_caffe.save_caffemodel('{}.caffemodel'.format(name))
default=[], nargs=argparse.REMAINDER, ) return parser if __name__ == '__main__': args = get_parser().parse_args() cfg = setup_cfg(args) cfg.defrost() cfg.MODEL.BACKBONE.PRETRAIN = False if cfg.MODEL.HEADS.POOL_LAYER == 'FastGlobalAvgPool': cfg.MODEL.HEADS.POOL_LAYER = 'GlobalAvgPool' cfg.MODEL.BACKBONE.WITH_NL = False model = build_model(cfg) Checkpointer(model).load(cfg.MODEL.WEIGHTS) model.eval() logger.info(model) inputs = torch.randn(1, 3, cfg.INPUT.SIZE_TEST[0], cfg.INPUT.SIZE_TEST[1]).to( torch.device(cfg.MODEL.DEVICE)) PathManager.mkdirs(args.output) pytorch_to_caffe.trans_net(model, inputs, args.name) pytorch_to_caffe.save_prototxt(f"{args.output}/{args.name}.prototxt") pytorch_to_caffe.save_caffemodel(f"{args.output}/{args.name}.caffemodel") logger.info(f"Export caffe model in {args.output} sucessfully!")
import sys sys.path.insert(0, '.') import torch from torch.autograd import Variable from torchvision.models import resnet import pytorch_to_caffe if __name__ == '__main__': name = 'resnet18' resnet18 = resnet.resnet18() checkpoint = torch.load("/home/shining/Downloads/resnet18-5c106cde.pth") resnet18.load_state_dict(checkpoint) resnet18.eval() input = torch.ones([1, 3, 224, 224]) #input=torch.ones([1,3,224,224]) pytorch_to_caffe.trans_net(resnet18, input, name) pytorch_to_caffe.save_prototxt('{}.prototxt'.format(name)) pytorch_to_caffe.save_caffemodel('{}.caffemodel'.format(name))
use_gpu = False ckpt_path = './checkpoints/8_32_nomp_whiten10_tests/' ckpt_path = os.path.join(ckpt_path, 'net.pt') name = 'WDSR' net = WDSR_B_NOWN(n_blocks=8, n_feats=32, n_colors=1, scale=1, rgb_mean=[0.5]) # net = RDN(channel = 1, growth_rate = 16, rdb_number = 2) net = torch.nn.DataParallel(net, use_gpu) if use_gpu: device = torch.device('cuda:0,1') input = input.to(device) net = net.to(device) checkpoint = torch.load(ckpt_path)['net'] else: checkpoint = torch.load("/Users/momo/Desktop/net.pt", 'cpu')['net'] # checkpoint = torch.load("/Users/momo/Desktop/rdn_net.pt", 'cpu')['net'] net.load_state_dict(checkpoint) net.eval() if use_gpu: img = np.load('/gpu001/qinsihao/data/38/test/org/dengziqi1_2201.npy')[:1080 * 1920].reshape([1080, 1920]) else: img = np.load('/Users/momo/Desktop/dengziqi2_1200_org.npy')[:1080 * 1920].reshape([1080, 1920]) transform = transforms.Compose([transforms.ToTensor()]) input = transform(img) input_un = input.unsqueeze(0) pytorch_to_caffe.trans_net(net, input.unsqueeze(0), name) pytorch_to_caffe.save_prototxt('{}.prototxt'.format(name)) pytorch_to_caffe.save_caffemodel('{}.caffemodel'.format(name))
if __name__ == '__main__': device = 'cpu' # cfg_path = '/data/taofuyu/models/high_roadside/high_retina_v58.py' # checkpoint = '/data/taofuyu/snapshot/high_mm/v58/latest.pth' # caffe_model_name = '/data/taofuyu/snapshot/high_mm/v58/retina_mm_high_v58' cfg_path = '/data/taofuyu/models/RF/rf_retina_v3.py' checkpoint = '/data/taofuyu/snapshot/RF/v3/latest.pth' caffe_model_name = '/data/taofuyu/snapshot/RF/v3/rf_retina_v3' deploy = '{}.prototxt'.format(caffe_model_name) caffemodel = '{}.caffemodel'.format(caffe_model_name) model = init_detector(cfg_path, checkpoint, device=device) model.forward = model.forward_dummy #change the forward end LAYER in this dummy function, then the hook will end at that LAYER data = torch.ones((1, 3, 382, 640)) # 1. get deploy and caffemodel ptc.trans_net(model, data, caffe_model_name) ptc.save_prototxt(deploy) ptc.save_caffemodel(caffemodel) # # 2. add post layers, like prior_layer/outputlayer # #prepare template file template_file = '/data/taofuyu/deploy/H/mm_ssd_post_template.prototxt' add_post(deploy, template_file) print("Next, run reload_caffemodel.py in repo pycaffe_test")
if k.strip('module.') in model_dict} model_dict.update(state_dict) model.load_state_dict(model_dict) print(model) # torch.save(model.state_dict(), '/mnt/sfshare/test_11111/PeleeNet/weights/pelee_365.pth') # print(model.state_dict()) model.eval() # print('nnnnnnnnnnnnnnnnnnnn net : ', net) # input_var = Variable(torch.rand(1, 3, 304, 304)) input_var = Variable(torch.rand(1, 3, 224, 224)) pytorch_to_caffe.trans_net(model, input_var, 'Pelee') pytorch_to_caffe.save_prototxt('/mnt/sfshare/test_11111/PeleeNet/weights/resnet_365.prototxt') pytorch_to_caffe.save_caffemodel('/mnt/sfshare/test_11111/PeleeNet/weights/resnet_365.caffemodel') """ state_dict = {k.strip('module.'): v for k, v in checkpoint.items() if k.strip('module.') in model_dict} # pretrained_dict = {k.strip('module.'): v for k, v in state_dict.items() # if k.strip('module.') in model_dict} # model_dict.update(state_dict) model.load_state_dict(state_dict) model.eval() # torch.save(model.state_dict(), '/mnt/sfshare/test_11111/PeleeNet/weights/pelee_365.pth') print(model.state_dict()) """
import argparse parser = argparse.ArgumentParser(description='Pytorch2Caffe') parser.add_argument('--name', default="global_pcb", type=str, help='save name') parser.add_argument( '--model_path', default= "/home/file_collections/gitlab/PytorchToCaffe/example/convert2caffe/led_pcb/led3d_baseline_attention_arcface_preludropout_all_82.pth", type=str, help='converted model path') parser.add_argument('--img_path', type=str, default='../001763.jpg') opt = parser.parse_args() net_dict = net.state_dict() model_loaded = torch.load(opt.model_path, map_location='cuda:0') model_loaded = { k.split("module.")[-1]: v for k, v in model_loaded.items() if k.split("module.")[-1] in net_dict } net.load_state_dict(model_loaded) net.eval() data = preprocess_img(opt.img_path) pytorch_to_caffe.trans_net(net, data, opt.name) pytorch_to_caffe.save_prototxt('{}.prototxt'.format(opt.name)) pytorch_to_caffe.save_caffemodel('{}.caffemodel'.format(opt.name))
from torch.autograd import Variable from model.configs.CC import Config from model.configs.pelee_snet_138 import PeleeNet, load_model import pytorch_to_caffe # from efficientnet_pytorch import EfficientNet # from model.resnext_101_32x4d import resnext_101_32x4d # from model.resnext_101_64x4d import resnext_101_64x4d import cv2 import numpy as np model = load_model( pretrained_model_path= '/home/khy/arithmetic/PytorchToCaffe/weights/testmodel_best.pth.tar', model_classes=138, data_classes=138) model.eval() input_var = Variable(torch.rand(1, 3, 224, 224)) pytorch_to_caffe.trans_net(model, input_var, 'peleenet_scene') pytorch_to_caffe.save_prototxt( '/home/khy/arithmetic/PytorchToCaffe/model/pelee_scene_138.prototxt') pytorch_to_caffe.save_caffemodel( '/home/khy/arithmetic/PytorchToCaffe/model/pelee_scene_138.caffemodel') """target_platform = "proxyless_cpu" model = torch.hub.load("mit-han-lab/ProxylessNAS", target_platform, pretrained=True) model.eval() input_var = Variable(torch.rand(1, 3, 224, 224)) pytorch_to_caffe.trans_net(model, input_var, 'proxyless') pytorch_to_caffe.save_prototxt('/home/khy/PycharmProjects/torch2caffetest/model/proxyless.prototxt') pytorch_to_caffe.save_caffemodel('/home/khy/PycharmProjects/torch2caffetest/model/proxyless.caffemodel')"""