当前位置: 首页>>代码示例>>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;未经允许,请勿转载。