caffe/conversion_utils.py
def convert_caffemodel(network_file, caffemodel_file, output_dir, format='csv'):
    """
    Saves the weights and bias in the caffemodel file to output_dir. This method requires caffe to be installed.

    Parameters
    ----------
    network_file: string
        Path to the input network file
        
    caffemodel_file: string
        Path to the input caffemodel file
        
    output_dir: string
        Output directory for weights/biases (local filesystem)
    
    format: string
        Output format for weights/biases (currently supported formats: csv)
    """
    if format != 'csv':
        raise ValueError('Only csv format supported in this version')
    net = caffe.Net(network_file, caffemodel_file, caffe.TEST)
    for layerName in net.params.keys():
	num_parameters = len(net.params[layerName])
        if num_parameters == 0:
            continue
        elif num_parameters == 2:
            # Weights and Biases
            w = net.params[layerName][0].data
            w = w.reshape(w.shape[0], -1)
            b = net.params[layerName][1].data
            b = b.reshape(b.shape[0], -1)
	    layerType = net.layers[list(net._layer_names).index(layerName)].type
            if layerType == 'InnerProduct':
                b = b.T
                w = w.T
            np.savetxt(os.path.join(output_dir, layerName + '_weight.mtx'), w, delimiter=',')
            np.savetxt(os.path.join(output_dir, layerName + '_bias.mtx'), b, delimiter=',')
            write_mtd(os.path.join(output_dir, layerName + '_weight.mtx.mtd'), w.shape[0], w.shape[1], np.count_nonzero(w))
            write_mtd(os.path.join(output_dir, layerName + '_bias.mtx.mtd'), b.shape[0], b.shape[1], np.count_nonzero(b))
        else:
            raise ValueError('Unsupported number of parameters:' + str(num_parameters))

    Saves the weights and bias in the caffemodel file to output_dir. This method requires caffe to be installed.

    Parameters
    ----------
    network_file: string
        Path to the input network file
        
    caffemodel_file: string
        Path to the input caffemodel file
        
    output_dir: string
        Output directory for weights/biases (local filesystem)
    
    format: string
        Output format for weights/biases (currently supported formats: csv)
    
Saves the weights and bias in the caffemodel file to output_dir. This method requires caffe to be installed.
Parameters

string
Path to the input network file

string
Path to the input caffemodel file

string
Output directory for weights/biases (local filesystem)

string
Output format for weights/biases (currently supported formats: csv)
Saves the weights and bias in the caffemodel file to output_dir . This method requires caffe to be installed . Parameters string Path to the input network file string Path to the input caffemodel file string Output directory for weights / biases ( local filesystem ) string Output format for weights / biases ( currently supported formats : csv )
def convert_caffemodel(network_file, caffemodel_file, output_dir, format='csv'):
    if format != 'csv':
        raise ValueError('Only csv format supported in this version')
    net = caffe.Net(network_file, caffemodel_file, caffe.TEST)
    for layerName in net.params.keys():
	num_parameters = len(net.params[layerName])
        if num_parameters == 0:
            continue
        elif num_parameters == 2:
            w = net.params[layerName][0].data
            w = w.reshape(w.shape[0], -1)
            b = net.params[layerName][1].data
            b = b.reshape(b.shape[0], -1)
	    layerType = net.layers[list(net._layer_names).index(layerName)].type
            if layerType == 'InnerProduct':
                b = b.T
                w = w.T
            np.savetxt(os.path.join(output_dir, layerName + '_weight.mtx'), w, delimiter=',')
            np.savetxt(os.path.join(output_dir, layerName + '_bias.mtx'), b, delimiter=',')
            write_mtd(os.path.join(output_dir, layerName + '_weight.mtx.mtd'), w.shape[0], w.shape[1], np.count_nonzero(w))
            write_mtd(os.path.join(output_dir, layerName + '_bias.mtx.mtd'), b.shape[0], b.shape[1], np.count_nonzero(b))
        else:
            raise ValueError('Unsupported number of parameters:' + str(num_parameters))
def convert_caffemodel ( network_file , caffemodel_file , output_dir , format = 'csv' ) : if format != 'csv' : raise ValueError ( 'Only csv format supported in this version' ) net = caffe . Net ( network_file , caffemodel_file , caffe . TEST ) for layerName in net . params . keys ( ) : num_parameters = len ( net . params [ layerName ] ) if num_parameters == 0 : continue elif num_parameters == 2 : w = net . params [ layerName ] [ 0 ] . data w = w . reshape ( w . shape [ 0 ] , - 1 ) b = net . params [ layerName ] [ 1 ] . data b = b . reshape ( b . shape [ 0 ] , - 1 ) layerType = net . layers [ list ( net . _layer_names ) . index ( layerName ) ] . type if layerType == 'InnerProduct' : b = b . T w = w . T np . savetxt ( os . path . join ( output_dir , layerName + '_weight.mtx' ) , w , delimiter = ',' ) np . savetxt ( os . path . join ( output_dir , layerName + '_bias.mtx' ) , b , delimiter = ',' ) write_mtd ( os . path . join ( output_dir , layerName + '_weight.mtx.mtd' ) , w . shape [ 0 ] , w . shape [ 1 ] , np . count_nonzero ( w ) ) write_mtd ( os . path . join ( output_dir , layerName + '_bias.mtx.mtd' ) , b . shape [ 0 ] , b . shape [ 1 ] , np . count_nonzero ( b ) ) else : raise ValueError ( 'Unsupported number of parameters:' + str ( num_parameters ) )
Saves the weights and bias in the caffemodel file to output_dir.
Saves the weights and bias in the caffemodel file to output_dir .
"""
    Saves the weights and bias in the caffemodel file to output_dir. This method requires caffe to be installed.

    Parameters
    ----------
    network_file: string
        Path to the input network file
        
    caffemodel_file: string
        Path to the input caffemodel file
        
    output_dir: string
        Output directory for weights/biases (local filesystem)
    
    format: string
        Output format for weights/biases (currently supported formats: csv)
    """ # Weights and Biases
network_file
caffemodel_file
output_dir
format