def param_name_dict(): """Find out the correspondence between layer names and parameter names.""" layer = caffe_pb2.LayerParameter() # get all parameter names (typically underscore case) and corresponding # type names (typically camel case), which contain the layer names # (note that not all parameters correspond to layers, but we'll ignore that) param_names = [s for s in dir(layer) if s.endswith('_param')] param_type_names = [type(getattr(layer, s)).__name__ for s in param_names] # strip the final '_param' or 'Parameter' param_names = [s[:-len('_param')] for s in param_names] param_type_names = [s[:-len('Parameter')] for s in param_type_names] return dict(zip(param_type_names, param_names))
def parse(self, sublayer, name, kwargs): self.p = caffe_pb2.LayerParameter() self.p.type = type(sublayer).__name__ param_type = type(sublayer).__name__ if 'bottoms' not in kwargs: raise AttributeError( 'Layer %s must specify bottoms=["list", "of", "bottoms"]' % param_type) bottoms = kwargs['bottoms'] if type(bottoms) == str: raise AttributeError( 'Layer %s type bottoms="%s" argument must be a list of strings, not a string' % (param_type, bottoms)) tops = kwargs.get('tops', [name]) if type(tops) == str: raise AttributeError( 'Layer %s type tops="%s" argument must be a list of strings, not a string' % (param_type, tops)) self.p.name = name for blob_name in tops: self.p.top.append(blob_name) for blob_name in bottoms: self.p.bottom.append(blob_name) param_names = kwargs.get('param_names', []) param_lr_mults = kwargs.get('param_lr_mults', []) param_decay_mults = kwargs.get('param_decay_mults', []) assert type(param_names) != str for i in range( max(len(param_names), len(param_lr_mults), len(param_decay_mults))): self.p.param.add() if param_names: self.p.param[-1].name = param_names[i] if param_lr_mults: self.p.param[-1].lr_mult = param_lr_mults[i] if param_decay_mults: self.p.param[-1].decay_mult = param_decay_mults[i] default_params = set([ 'name', 'bottoms', 'tops', 'param_names', 'param_lr_mults', 'param_decay_mults' ]) if param_type in layer_helpers.param_names: proto_param = getattr( self.p, layer_helpers.param_names[param_type] + '_param') for k, v in kwargs.iteritems(): if k in default_params: continue try: layer_helpers.assign_proto(proto_param, k, v) except AttributeError: raise AttributeError( 'Layer %s has no keyword argument %s=%s' % (param_type, k, v)) else: for k, v in kwargs.iteritems(): if k not in default_params: raise AttributeError( 'Layer %s has no keyword argument %s=%s' % (param_type, k, v))