示例#1
0
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))
示例#3
0
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)
示例#4
0
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.")
示例#5
0
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
示例#6
0
    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))
示例#8
0
        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!")
示例#9
0
import sys

sys.path.insert(0, '.')
import torch
from torch.autograd import Variable
from torchvision.models import resnet
import pytorch_to_caffe
from MobileNetV2 import MobileNetV2

if __name__ == '__main__':
    name = 'MobileNetV2'
    net = MobileNetV2()
    checkpoint = torch.load("/home/shining/Downloads/mobilenet_v2.pth.tar")

    net.load_state_dict(checkpoint)
    net.eval()
    input = torch.ones([1, 3, 224, 224])
    # input=torch.ones([1,3,224,224])
    pytorch_to_caffe.trans_net(net, input, name)
    proto_path = '{}.prototxt'.format(name)
    print("Save the prototxt into {}".format(proto_path))
    pytorch_to_caffe.save_prototxt(proto_path)

    weight_path = '{}.caffemodel'.format(name)
    print("Save the caffe weight into {}".format(weight_path))
    pytorch_to_caffe.save_caffemodel(weight_path)
示例#10
0

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")
示例#11
0
from torchvision import models

parser = argparse.ArgumentParser()
parser.add_argument("-n", "--net", required=True)
parser.add_argument("--dir", default=None)
args = parser.parse_args()

dir_ = args.net if args.dir is None else args.dir
name = args.net

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(models, name):
    model_cls = getattr(models, name)
else:
    raise Exception()

# net = model_cls(pretrained=True)
net = model_cls(pretrained=False)
net.eval()
inputs = Variable(torch.ones([1, 3, 224, 224]))
pytorch_to_caffe.trans_net(net, inputs, name)

dest = "converted_results/{}".format(name if dir_ is None else dir_)
os.makedirs(dest, exist_ok=True)
pytorch_to_caffe.save_prototxt("{}/{}.prototxt".format(dest, name))
pytorch_to_caffe.save_caffemodel("{}/{}.caffemodel".format(dest, name))

示例#12
0
                  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())
    """
示例#13
0
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')"""