def convert_trained_caffemodel(lasagne_model, caffemodel, prototxt='', caffe_parse = caffe_parsing):
	'''
	PARAMETERS:
		- lasagne_model: A lasagne model (i.e, BaseModel) to set parameters of. This probably came from a call to
		convert_model_def. The layers should be named the same as the caffemodel.
		- caffemodel: /path/to/trained_model.caffemodel file
		- prototxt: /path/to/deploy.prototxt file. This is not needed if you don't use the caffe parsing, otherwise it's needed.
		- caffe_parse: don't worry about this, it will be set based on whether or not you have caffe installed.

	NOTES:
	sets the params of lasagne_model to be from the trained caffemodel.
	'''
	# load in the caffemodel (this takes a long time without cpp implementation of protobuf)
	if caffe_parse:
		layer_params = parse_from_protobuf_caffe.parse_caffemodel(caffemodel, prototxt=prototxt) # prototxt is passed in if caffemodel uses caffe
	else:
		layer_params = parse_from_protobuf.parse_caffemodel(caffemodel)

	# this should be in the same order as was made by the lasagne model, but reversed. we will check that.
	# todo: maybe just go by names, strictly? 
	for lasagne_layer in lasagne_model.all_layers:
		if len(lasagne_layer.get_params()) == 0:
			# no params to set
			continue
		lp = layer_params[lasagne_layer.name]
		Wblob = lp[0]
		bblob = lp[1]
		# get arrays of parameters
		W = array_from_blob(Wblob)
		b = array_from_blob(bblob)
		# set parameters
		set_model_params(lasagne_layer, W,b)
def convert_trained_caffemodel(lasagne_model, caffemodel, prototxt='', caffe_parse = caffe_parsing):
	'''
	sets the params of lasagne_model to be from the trained caffemodel.

	lasagne_model is a lasagne model from e.g convert_model_def(prototxt).
	caffemodel is the filepath to the .caffemodel file
	'''
	# load in the caffemodel (this takes a long time without cpp implementation of protobuf)
	if caffe_parse:
		layer_params = parse_from_protobuf_caffe.parse_caffemodel(caffemodel, prototxt=prototxt) # prototxt is passed in if caffemodel uses caffe
	else:
		layer_params = parse_from_protobuf.parse_caffemodel(caffemodel)

	# this should be in the same order as was made by the lasagne model, but reversed. we will check that.
	# todo: maybe just go by names, strictly? 
	for lasagne_layer in lasagne_model.all_layers:
		if len(lasagne_layer.get_params()) == 0:
			# no params to set
			continue
		lp = layer_params[lasagne_layer.name]
		Wblob = lp[0]
		bblob = lp[1]
		# get arrays of parameters
		W = array_from_blob(Wblob)
		b = array_from_blob(bblob)
		# set parameters
		set_model_params(lasagne_layer, W,b)
示例#3
0
def convert_trained_caffemodel(lasagne_model,
                               caffemodel,
                               prototxt='',
                               caffe_parse=caffe_parsing):
    '''
	sets the params of lasagne_model to be from the trained caffemodel.

	lasagne_model is a lasagne model from e.g convert_model_def(prototxt).
	caffemodel is the filepath to the .caffemodel file
	'''
    # load in the caffemodel (this takes a long time without cpp implementation of protobuf)
    if caffe_parse:
        layer_params = parse_from_protobuf_caffe.parse_caffemodel(
            caffemodel, prototxt=prototxt
        )  # prototxt is passed in if caffemodel uses caffe
    else:
        layer_params = parse_from_protobuf.parse_caffemodel(caffemodel)

    # this should be in the same order as was made by the lasagne model, but reversed. we will check that.
    # todo: maybe just go by names, strictly?
    for lasagne_layer in lasagne_model.all_layers:
        if len(lasagne_layer.get_params()) == 0:
            # no params to set
            continue
        lp = layer_params[lasagne_layer.name]
        Wblob = lp[0]
        bblob = lp[1]
        # get arrays of parameters
        W = array_from_blob(Wblob)
        b = array_from_blob(bblob)
        # set parameters
        set_model_params(lasagne_layer, W, b)
示例#4
0
def convert_trained_caffemodel(lasagne_model,
                               caffemodel,
                               prototxt='',
                               caffe_parse=caffe_parsing):
    '''
	PARAMETERS:
		- lasagne_model: A lasagne model (i.e, BaseModel) to set parameters of. This probably came from a call to
		convert_model_def. The layers should be named the same as the caffemodel.
		- caffemodel: /path/to/trained_model.caffemodel file
		- prototxt: /path/to/deploy.prototxt file. This is not needed if you don't use the caffe parsing, otherwise it's needed.
		- caffe_parse: don't worry about this, it will be set based on whether or not you have caffe installed.

	NOTES:
	sets the params of lasagne_model to be from the trained caffemodel.
	'''
    # load in the caffemodel (this takes a long time without cpp implementation of protobuf)
    if caffe_parse:
        layer_params = parse_from_protobuf_caffe.parse_caffemodel(
            caffemodel, prototxt=prototxt
        )  # prototxt is passed in if caffemodel uses caffe
    else:
        layer_params = parse_from_protobuf.parse_caffemodel(caffemodel)

    # this should be in the same order as was made by the lasagne model, but reversed. we will check that.
    # todo: maybe just go by names, strictly?
    for lasagne_layer in lasagne_model.all_layers:
        if len(lasagne_layer.get_params()) == 0:
            # no params to set
            continue
        print lasagne_layer.name
        lp = layer_params[lasagne_layer.name]
        Wblob = lp[0]
        bblob = lp[1]
        # get arrays of parameters
        W = array_from_blob(Wblob)
        b = array_from_blob(bblob)
        print "W: ", W.shape
        print "b: ", b.shape
        # set parameters
        set_model_params(lasagne_layer, W, b)