TransForm_Kit/ModelConvert/pytorch2caffe/code/ConvertModel.py
def ConvertModel_caffe(pytorch_net, InputShape, softmax=False):
    """ Pytorch to Caffe, only support single tensor input """
    
    #from ConvertLayer_caffe import convert_caffe
    """ Need forward once """
    pytorch_net.eval()
    global inputs
    n, c, h, w = InputShape
    inputs = Variable(torch.rand(n, c, h, w), requires_grad=True)
    outputs = pytorch_net(inputs)

    if softmax:
        import torch.nn as nn
        regularize = nn.Softmax()
        outputs = regularize(outputs)

    """ Travel computational graph in backward order """
    """ Need to count number of tops(indegree) of all nodes first """
    global visited, tops_dict, layer_type_count, dst
    global slice_point, multi_tops, axis_dict,bottomlayer_top
    visited = set()
    tops_dict = dict()
    layer_type_count = dict()
    slice_point = dict()
    multi_tops = dict()
    axis_dict = dict()
    
    for out in outputs:
        FindMultiTops(out.grad_fn)
    for k,v in tops_dict.items():
        print(k,v)
    print("slice_point dict")
    for k,v in slice_point.items():
        print(k,v)
    print("multi_tops dict")
    for k,v in multi_tops.items():
        print(k,v)
    print("axis_dict")
    for k,v in axis_dict.items():
        print(k,v)
    print("layer_type_count")
    for k,v in layer_type_count.items():
        print(k,v)
    """ Travel computational graph in backward order """
    global caffe_net
    global convert, link
    caffe_networkbuild = networkbuild()
    caffe_networkbuild.module_to_dict(pytorch_net)
    convert = caffe_networkbuild.convert_caffe
    link = link_caffe
    caffe_net = []

    visited = set()
    tops_dict = dict()
    layer_type_count = dict()

    for out in outputs:
        DFS(out.grad_fn)

    analysis_model_layers = analysis_model(caffe_net)
    """ Caffe input """
    text_net = pb2.NetParameter()
    if os.environ.get("T2C_DEBUG"):
        text_net.debug_info = True

    """ Caffe layer parameters """
    binary_weights = pb2.NetParameter()
    binary_weights.CopyFrom(text_net)
    for layer in analysis_model_layers:
        binary_weights.layer.extend([layer])

        layer_proto = pb2.LayerParameter()
        layer_proto.CopyFrom(layer)
        del layer_proto.blobs[:]
        text_net.layer.extend([layer_proto])

    return text_net, binary_weights
 Pytorch to Caffe, only support single tensor input 
Pytorch to Caffe, only support single tensor input
Pytorch to Caffe only support single tensor input
def ConvertModel_caffe(pytorch_net, InputShape, softmax=False):
    pytorch_net.eval()
    global inputs
    n, c, h, w = InputShape
    inputs = Variable(torch.rand(n, c, h, w), requires_grad=True)
    outputs = pytorch_net(inputs)
    if softmax:
        import torch.nn as nn
        regularize = nn.Softmax()
        outputs = regularize(outputs)
    global visited, tops_dict, layer_type_count, dst
    global slice_point, multi_tops, axis_dict,bottomlayer_top
    visited = set()
    tops_dict = dict()
    layer_type_count = dict()
    slice_point = dict()
    multi_tops = dict()
    axis_dict = dict()
    for out in outputs:
        FindMultiTops(out.grad_fn)
    for k,v in tops_dict.items():
        print(k,v)
    print("slice_point dict")
    for k,v in slice_point.items():
        print(k,v)
    print("multi_tops dict")
    for k,v in multi_tops.items():
        print(k,v)
    print("axis_dict")
    for k,v in axis_dict.items():
        print(k,v)
    print("layer_type_count")
    for k,v in layer_type_count.items():
        print(k,v)
    global caffe_net
    global convert, link
    caffe_networkbuild = networkbuild()
    caffe_networkbuild.module_to_dict(pytorch_net)
    convert = caffe_networkbuild.convert_caffe
    link = link_caffe
    caffe_net = []
    visited = set()
    tops_dict = dict()
    layer_type_count = dict()
    for out in outputs:
        DFS(out.grad_fn)
    analysis_model_layers = analysis_model(caffe_net)
    text_net = pb2.NetParameter()
    if os.environ.get("T2C_DEBUG"):
        text_net.debug_info = True
    binary_weights = pb2.NetParameter()
    binary_weights.CopyFrom(text_net)
    for layer in analysis_model_layers:
        binary_weights.layer.extend([layer])
        layer_proto = pb2.LayerParameter()
        layer_proto.CopyFrom(layer)
        del layer_proto.blobs[:]
        text_net.layer.extend([layer_proto])
    return text_net, binary_weights
def ConvertModel_caffe ( pytorch_net , InputShape , softmax = False ) : """ Need forward once """ pytorch_net . eval ( ) global inputs n , c , h , w = InputShape inputs = Variable ( torch . rand ( n , c , h , w ) , requires_grad = True ) outputs = pytorch_net ( inputs ) if softmax : import torch . nn as nn regularize = nn . Softmax ( ) outputs = regularize ( outputs ) """ Travel computational graph in backward order """ """ Need to count number of tops(indegree) of all nodes first """ global visited , tops_dict , layer_type_count , dst global slice_point , multi_tops , axis_dict , bottomlayer_top visited = set ( ) tops_dict = dict ( ) layer_type_count = dict ( ) slice_point = dict ( ) multi_tops = dict ( ) axis_dict = dict ( ) for out in outputs : FindMultiTops ( out . grad_fn ) for k , v in tops_dict . items ( ) : print ( k , v ) print ( "slice_point dict" ) for k , v in slice_point . items ( ) : print ( k , v ) print ( "multi_tops dict" ) for k , v in multi_tops . items ( ) : print ( k , v ) print ( "axis_dict" ) for k , v in axis_dict . items ( ) : print ( k , v ) print ( "layer_type_count" ) for k , v in layer_type_count . items ( ) : print ( k , v ) """ Travel computational graph in backward order """ global caffe_net global convert , link caffe_networkbuild = networkbuild ( ) caffe_networkbuild . module_to_dict ( pytorch_net ) convert = caffe_networkbuild . convert_caffe link = link_caffe caffe_net = [ ] visited = set ( ) tops_dict = dict ( ) layer_type_count = dict ( ) for out in outputs : DFS ( out . grad_fn ) analysis_model_layers = analysis_model ( caffe_net ) """ Caffe input """ text_net = pb2 . NetParameter ( ) if os . environ . get ( "T2C_DEBUG" ) : text_net . debug_info = True """ Caffe layer parameters """ binary_weights = pb2 . NetParameter ( ) binary_weights . CopyFrom ( text_net ) for layer in analysis_model_layers : binary_weights . layer . extend ( [ layer ] ) layer_proto = pb2 . LayerParameter ( ) layer_proto . CopyFrom ( layer ) del layer_proto . blobs [ : ] text_net . layer . extend ( [ layer_proto ] ) return text_net , binary_weights
Pytorch to Caffe, only support single tensor input
Pytorch to Caffe only support single tensor input
""" Pytorch to Caffe, only support single tensor input """ #from ConvertLayer_caffe import convert_caffe """ Need forward once """ """ Travel computational graph in backward order """ """ Need to count number of tops(indegree) of all nodes first """ """ Travel computational graph in backward order """ """ Caffe input """ """ Caffe layer parameters """
pytorch_net
InputShape
softmax