Exemplo n.º 1
0
 def __init__(self):
     self.layers = {}  # name of the layers
     self.detail_layers = {}  # detail of the net, #of each kind of layer
     self.detail_blobs = {}
     self._blobs = Blob_LOG()  # Record the blobs
     self._blob_data = []
     self.cnet = caffe_net.Caffemodel('')  # the caffenet
     self.debug = False
     self.pytorch_layer_name = None
Exemplo n.º 2
0
 def __init__(self):
     """
     doing init() with inputs Variable before using it
     """
     self.layers = {}
     self._blobs = {}
     self._blobs_data = []
     self.cnet = caffe_net.Caffemodel('')
     self.debug = False
Exemplo n.º 3
0
 def __init__(self):
     """
     doing init() with inputs Variable before using it
     """
     self.layers = {}
     self.detail_layers = {}
     self.detail_blobs = {}
     self._blobs = Blob_LOG()
     self._blobs_data = []
     self.cnet = caffe_net.Caffemodel()
     self.debug = True
Exemplo n.º 4
0
def remove_layer_by_name(json_dict):
    prevOPName = json_dict["prevOPName"]
    nextOPName = json_dict["nextOPName"]
    net = caffe_net.Caffemodel(caffemodel_file)
    prevlayer = net.get_layer_by_name(prevOPName)
    prevOPTop = prevlayer.top[0]
    net.remove_layer_by_name(currOPName)

    if nextOPName != "":
        layer = net.get_layer_by_name(nextOPName)
        layer.bottom[0] = prevOPTop
    return net
Exemplo n.º 5
0
def main():
    net = caffe_net.Caffemodel(caffemodel)
    net.save_prototxt(prototxt)
Exemplo n.º 6
0
def set_layer_data(json_dict):
    weight = np.array(json_dict["weight"]).astype(np.float32)
    net = caffe_net.Caffemodel(caffemodel_file)
    net.set_layer_data(name, weight)
    return net
Exemplo n.º 7
0
def add_new_layer(json_dict):
    currOPTop = json_dict["currOPTop"]
    layerType = json_dict["layerType"]
    prevOPName = json_dict["prevOPName"]
    nextOPName = json_dict["nextOPName"]

    net = caffe_net.Caffemodel(caffemodel_file)
    layer = net.get_layer_by_name(prevOPName)
    prevOPBottom = layer.top[0]
    layer_params = layer_param.Layer_param(currOPName,
                                           layerType,
                                           currOPTop,
                                           prevOPBottom,
                                           phase=1)

    if layerType.endswith("Convolution"):
        ke_h = json_dict["kernel_h"]
        ke_w = json_dict["kernel_w"]
        num_out = json_dict["num_output"]
        str_h = json_dict["stride_h"]
        str_w = json_dict["stride_w"]
        group = json_dict["group"]
        bias = json_dict["bias_term"]
        dila = json_dict["dilation"]
        p_h = json_dict["pad_h"]
        pad_w = json_dict["pad_w"]
        weight = np.array(json_dict["weight"]).astype(np.float32)

        # 卷积层后面必须是BN层
        layerOP = net.get_layer_by_name(nextOPName)
        assert layerOP.type.endswith("BatchNorm")
        for blob in layerOP.blobs:
            shape = blob.shape.dim
            assert num_out == shape[0]
            break
        layer_params.conv_param(kernel_h=ke_h,
                                kernel_w=ke_w,
                                num_output=num_out,
                                stride_h=str_h,
                                stride_w=str_w)
        net.add_layer_with_data(layer_params,
                                datas=weight,
                                bottom=prevOPBottom)
    elif layerType.endswith("Softmax"):
        axis = json_dict["num_axis"]
        weight = json_dict["weight"]
        layer_params.softmax_param(num_axis=axis)
        net.add_layer_with_data(layer_params,
                                datas=weight,
                                bottom=prevOPBottom)
    elif layerType.endswith("Permute"):
        weight = json_dict["weight"]
        layer_params.permute_param()
        net.add_layer_with_data(layer_params,
                                datas=weight,
                                bottom=prevOPBottom)
    elif layerType.endswith("Reshape"):
        weight = json_dict["weight"]
        shape = json_dict["shape"]
        layer_params.reshape_param(shape=shape)
        net.add_layer_with_data(layer_params,
                                datas=weight,
                                bottom=prevOPBottom)

    if nextOPName != "":
        layer = net.get_layer_by_name(nextOPName)
        layer.bottom[0] = currOPTop
    return net