當前位置: 首頁>>代碼示例>>Python>>正文


Python caffe_pb2.NetParameter方法代碼示例

本文整理匯總了Python中caffe.proto.caffe_pb2.NetParameter方法的典型用法代碼示例。如果您正苦於以下問題:Python caffe_pb2.NetParameter方法的具體用法?Python caffe_pb2.NetParameter怎麽用?Python caffe_pb2.NetParameter使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在caffe.proto.caffe_pb2的用法示例。


在下文中一共展示了caffe_pb2.NetParameter方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: load_and_convert_caffe_model

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def load_and_convert_caffe_model(prototxt_file_name, caffemodel_file_name):
    caffenet = caffe_pb2.NetParameter()
    caffenet_weights = caffe_pb2.NetParameter()
    text_format.Merge(open(prototxt_file_name).read(), caffenet)
    caffenet_weights.ParseFromString(open(caffemodel_file_name).read())
    # C2 conv layers current require biases, but they are optional in C1
    # Add zeros as biases is they are missing
    add_missing_biases(caffenet_weights)
    # We only care about getting parameters, so remove layers w/o parameters
    remove_layers_without_parameters(caffenet, caffenet_weights)
    # BatchNorm is not implemented in the translator *and* we need to fold Scale
    # layers into the new C2 SpatialBN op, hence we remove the batch norm layers
    # and apply custom translations code
    bn_weights = remove_spatial_bn_layers(caffenet, caffenet_weights)
    # Set num, channel, height and width for blobs that use shape.dim instead
    normalize_shape(caffenet_weights)
    # Translate the rest of the model
    net, pretrained_weights = caffe_translator.TranslateModel(
        caffenet, caffenet_weights
    )
    pretrained_weights.protos.extend(bn_weights)
    return net, pretrained_weights 
開發者ID:yihui-he,項目名稱:KL-Loss,代碼行數:24,代碼來源:pickle_caffe_blobs.py

示例2: make_testable

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def make_testable(train_model_path):
    # load the train net prototxt as a protobuf message
    with open(train_model_path) as f:
        train_str = f.read()
    train_net = caffe_pb2.NetParameter()
    text_format.Merge(train_str, train_net)

    # add the mean, var top blobs to all BN layers
    for layer in train_net.layer:
        if layer.type == "BN" and len(layer.top) == 1:
            layer.top.append(layer.top[0] + "-mean")
            layer.top.append(layer.top[0] + "-var")

    # remove the test data layer if present
    if train_net.layer[1].name == "data" and train_net.layer[1].include:
        train_net.layer.remove(train_net.layer[1])
        if train_net.layer[0].include:
            # remove the 'include {phase: TRAIN}' layer param
            train_net.layer[0].include.remove(train_net.layer[0].include[0])
    return train_net 
開發者ID:jfemiani,項目名稱:facade-segmentation,代碼行數:22,代碼來源:compute_bn_statistics.py

示例3: DrpOut_OPT_Create_Prototxt

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def DrpOut_OPT_Create_Prototxt(original_prototxt_path, original_model_path, optimized_prototxt_path):
    net_param = caffe_pb2.NetParameter()
    new_net_param = caffe_pb2.NetParameter()
    with open(original_prototxt_path, 'rt') as f:
        Parse(f.read(), net_param)
    for layer_idx in range(0, len(net_param.layer)):
        layer = net_param.layer[layer_idx]
        if layer.type == 'Dropout':
            if layer.top[0] == layer.bottom[0]:
                continue
            else:
                new_net_param.layer[-1].top[0] = layer.top[0]
        else:
            new_net_param.layer.extend([layer])
    new_net_param.name = net_param.name
    with open(optimized_prototxt_path, 'wt') as f:
        f.write(MessageToString(new_net_param))
    print "DROPOUT OPT : Create Optimized Prototxt Done."
    print bcolors.OKGREEN + "DROPOUT OPT : Model at " + original_model_path + "." + bcolors.ENDC
    print bcolors.OKGREEN + "DROPOUT OPT : Prototxt at " + optimized_prototxt_path + "." + bcolors.ENDC 
開發者ID:HolmesShuan,項目名稱:Caffe-Computation-Graph-Optimization,代碼行數:22,代碼來源:opt_utils.py

示例4: read_prototxt

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def read_prototxt(fname):
    """Return a caffe_pb2.NetParameter object that defined in a prototxt file
    """
    proto = caffe_pb2.NetParameter()
    with open(fname, 'r') as f:
        text_format.Merge(str(f.read()), proto)
    return proto 
開發者ID:awslabs,項目名稱:dynamic-training-with-apache-mxnet-on-aws,代碼行數:9,代碼來源:caffe_parser.py

示例5: get_layers

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def get_layers(proto):
    """Returns layers in a caffe_pb2.NetParameter object
    """
    if len(proto.layer):
        return proto.layer
    elif len(proto.layers):
        return proto.layers
    else:
        raise ValueError('Invalid proto file.') 
開發者ID:awslabs,項目名稱:dynamic-training-with-apache-mxnet-on-aws,代碼行數:11,代碼來源:caffe_parser.py

示例6: read_caffemodel

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def read_caffemodel(prototxt_fname, caffemodel_fname):
    """Return a caffe_pb2.NetParameter object that defined in a binary
    caffemodel file
    """
    if use_caffe:
        caffe.set_mode_cpu()
        net = caffe.Net(prototxt_fname, caffemodel_fname, caffe.TEST)
        layer_names = net._layer_names
        layers = net.layers
        return (layers, layer_names)
    else:
        proto = caffe_pb2.NetParameter()
        with open(caffemodel_fname, 'rb') as f:
            proto.ParseFromString(f.read())
        return (get_layers(proto), None) 
開發者ID:awslabs,項目名稱:dynamic-training-with-apache-mxnet-on-aws,代碼行數:17,代碼來源:caffe_parser.py

示例7: __init__

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def __init__(self):
        print('Loading Caffe file:', caffemodel_path)
        caffemodel_params = caffe_pb2.NetParameter()
        caffemodel_str = open(caffemodel_path, 'rb').read()
        caffemodel_params.ParseFromString(caffemodel_str)
        caffe_layers = caffemodel_params.layer
        self.layers = []
        self.counter = 0
        self.bgr_to_rgb = False
        for layer in caffe_layers:
            if layer.type == 'Convolution':
                self.layers.append(layer) 
開發者ID:dengdan,項目名稱:seglink,代碼行數:14,代碼來源:caffe_to_tensorflow.py

示例8: __init__

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def __init__(self, name="network"):
        self.net = caffe_pb2.NetParameter()
        self.net.name = name
        self.bottom = None
        self.cur = None
        self.this = None 
開發者ID:yihui-he,項目名稱:resnet-cifar10-caffe,代碼行數:8,代碼來源:net_generator.py

示例9: read_net

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def read_net(filename):
    net = caffe_pb2.NetParameter()
    with open(filename) as f:
        protobuf.text_format.Parse(f.read(), net)
    return net 
開發者ID:msracver,項目名稱:Deep-Exemplar-based-Colorization,代碼行數:7,代碼來源:summarize.py

示例10: __init__

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def __init__(self, name="network", pt=None):
        self.net = caffe_pb2.NetParameter()
        if pt is None:
            self.net.name = name
        else:
            with open(pt, 'rt') as f:
                pb2.text_format.Merge(f.read(), self.net)
        self.bottom = None
        self.cur = None
        self.this = None

        self._layer = None
        self._bottom = None 
開發者ID:yihui-he,項目名稱:channel-pruning,代碼行數:15,代碼來源:builder.py

示例11: get_complexity

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def get_complexity(netspec=None, prototxt_file=None, mode=None):
    # One of netspec, or prototxt_path params should not be None
    assert (netspec is not None) or (prototxt_file is not None)

    if netspec is not None:
        prototxt_file = _create_file_from_netspec(netspec)

    net = caffe.Net(prototxt_file, caffe.TEST)

    total_params = 0
    total_flops = 0

    net_params = caffe_pb2.NetParameter()
    text_format.Merge(open(prototxt_file).read(), net_params)
    print '\n ########### output ###########'
    for layer in net_params.layer:
        if layer.name in net.params:

            params = net.params[layer.name][0].data.size
            # If convolution layer, multiply flops with receptive field
            # i.e. #params * datawidth * dataheight
            if layer.type == 'Convolution':  # 'conv' in layer:
                data_width = net.blobs[layer.name].data.shape[2]
                data_height = net.blobs[layer.name].data.shape[3]
                flops = net.params[layer.name][
                    0].data.size * data_width * data_height
                # print >> sys.stderr, layer.name, params, flops
            else:
                flops = net.params[layer.name][0].data.size
            flops *= 2 
            print('%s: #params: %s, #FLOPs: %s') % (
                layer.name,
                digit2string(params),
                digit2string(flops))
            total_params += params
            total_flops += flops

    if netspec is not None:
        os.remove(prototxt_file)

    return total_params, total_flops 
開發者ID:Roll920,項目名稱:ThiNet_Code,代碼行數:43,代碼來源:FLOPs_and_size.py

示例12: main

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def main():
    args = parse_args()
    net = caffe_pb2.NetParameter()
    text_format.Merge(open(args.input_net_proto_file).read(), net)
    print('Drawing net to %s' % args.output_image_file)
    phase=None;
    if args.phase == "TRAIN":
        phase = caffe.TRAIN
    elif args.phase == "TEST":
        phase = caffe.TEST
    elif args.phase != "ALL":
        raise ValueError("Unknown phase: " + args.phase)
    caffe.draw.draw_net_to_file(net, args.output_image_file, args.rankdir,
                                phase) 
開發者ID:CUHKSZ-TQL,項目名稱:EverybodyDanceNow_reproduce_pytorch,代碼行數:16,代碼來源:draw_caffe_net.py

示例13: Inpt_OPT_New_Weight

# 需要導入模塊: from caffe.proto import caffe_pb2 [as 別名]
# 或者: from caffe.proto.caffe_pb2 import NetParameter [as 別名]
def Inpt_OPT_New_Weight(original_prototxt_path, original_model_path, optimized_prototxt_path, new_model_path, scale):
    net_param = caffe_pb2.NetParameter()
    with open(original_prototxt_path, 'rt') as f:
        Parse(f.read(), net_param)
    layer_num = len(net_param.layer)
    input_layer_type = ['Data', 'Input', 'AnnotatedData']
    for layer_idx in range(0, layer_num):
        layer = net_param.layer[layer_idx]
        if layer.type not in input_layer_type:
            assert(layer.type=='Convolution' or layer.type=='InnerProduct'), "## ERROR : First Layer MUST BE CONV or IP. ##"
            target_layer_name = layer.name
            break
        else:
            try:
                net_param.layer[layer_idx].transform_param.scale = 1.0
            except:
                print bcolors.WARNING + "INPUT PREPROCESS (SCALE) OPT : ** WARNING ** NO SCALE found in DATA layer." + bcolors.ENDC

    new_net = caffe.Net(original_prototxt_path, str(original_model_path), caffe.TEST)
    new_net.params[target_layer_name][0].data[...] = new_net.params[target_layer_name][0].data[...]*scale
    new_net.save(new_model_path)

    with open(optimized_prototxt_path, 'wt') as f:
        f.write(MessageToString(net_param))

    print "INPUT PREPROCESS (SCALE) OPT : Merge Input Scale Done."
    print bcolors.OKGREEN + "INPUT PREPROCESS (SCALE) OPT : Model at " + new_model_path + "." + bcolors.ENDC
    print bcolors.OKGREEN + "INPUT PREPROCESS (SCALE) OPT : Prototxt at " + optimized_prototxt_path + "." + bcolors.ENDC
    #print "INPUT PREPROCESS (SCALE) OPT : ## TIPS ## Remember to remove scale in data layer before test !!!" 
開發者ID:HolmesShuan,項目名稱:Caffe-Computation-Graph-Optimization,代碼行數:31,代碼來源:opt_utils.py


注:本文中的caffe.proto.caffe_pb2.NetParameter方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。