def extract(cls, node): pb = node.parameters collect_until_token(pb, b'<Dim>') dim = read_binary_integer32_token(pb) collect_until_token(pb, b'<BlockDim>') block_dim = read_binary_integer32_token(pb) collect_until_token(pb, b'<Epsilon>') eps = read_binary_float_token(pb) collect_until_token(pb, b'<TargetRms>') target_rms = read_binary_float_token(pb) collect_until_token(pb, b'<StatsMean>') mean = read_binary_vector(pb) collect_until_token(pb, b'<StatsVar>') var = read_binary_vector(pb) scale = target_rms / np.sqrt(var + eps) shift = -target_rms * mean / np.sqrt(var + eps) scale = np.tile(scale, dim // block_dim) shift = np.tile(shift, dim // block_dim) attrs = {'out-size': dim} embed_input(attrs, 1, 'weights', scale) embed_input(attrs, 2, 'biases', shift) ScaleShiftOp.update_node_stat(node, attrs) return cls.enabled
def extract(cls, node): pb = node.pb model = node.model_pb param = pb.scale_param attrs = { 'axis': param.axis, } if model is None and len(pb.bottom) == 1: # default weights and biases for scale layer if the caffemodel file doesn't contain them model = NamedAttrsClass({ 'blobs': np.array([ NamedAttrsClass({'data': np.array([1])}), NamedAttrsClass({'data': np.array([0])}) ]) }) # scale with 1 input and 1 or 2 blobs if model and len(model.blobs) != 0 and len(pb.bottom) == 1: attrs.update(weights_biases(param.bias_term, model)) # 2 inputs + bias elif len(pb.bottom) == 2 and param.bias_term: if model is None or len(model.blobs) == 0: # default bias for scale layer with 2 inputs if the caffemodel file doesn't contain them model = NamedAttrsClass({ 'blobs': np.array([NamedAttrsClass({'data': np.array([0])})]) }) embed_input(attrs, 1, 'biases', model.blobs[0].data) ScaleShiftOp.update_node_stat(node, attrs) return cls.enabled
def extract(cls, node): pb = node.parameters read_learning_info(pb) weights = read_binary_vector(pb) mapping_rule = {} embed_input(mapping_rule, 1, 'weights', weights) ScaleShiftOp.update_node_stat(node, mapping_rule) return cls.enabled
def extract(cls, node): pb = node.parameters read_learning_info(pb) biases = read_binary_vector(pb) bias_term = True mapping_rule = {'bias_term': bias_term} embed_input(mapping_rule, 1, 'weights', np.ones(biases.shape)) embed_input(mapping_rule, 2, 'biases', biases) ScaleShiftOp.update_node_stat(node, mapping_rule) return cls.enabled
def extract(cls, node): pb = node.parameters collect_until_token(pb, b'<Params>') weights = read_binary_vector(pb) find_next_tag(pb) read_placeholder(pb, 1) mapping_rule = {'layout': 'NCHW'} embed_input(mapping_rule, 1, 'weights', weights) ScaleShiftOp.update_node_stat(node, mapping_rule) return cls.enabled
def extract(cls, node): pb = node.parameters collect_until_token(pb, b'<Dim>') dim = read_binary_integer32_token(pb) collect_until_token(pb, b'<Scale>') scale = read_binary_float_token(pb) # TODO add real batch here attrs = {} embed_input(attrs, 1, 'weights', np.full([dim], scale)) ScaleShiftOp.update_node_stat(node, attrs) return cls.enabled
def extract(node): pb = node.parameters collect_until_token(pb, b'<Dim>') dim = read_binary_integer32_token(pb) target_rms = 1 d_scaled = dim * target_rms**2 in_norm = np.zeros([dim], np.float64) in_norm += 1.0 / d_scaled in_norm = np.maximum(in_norm, 2.**(-66)) in_norm = np.power(in_norm, -0.5) attrs = {} embed_input(attrs, 1, 'weights', in_norm) ScaleShiftOp.update_node_stat(node, attrs) return __class__.enabled
def extract(cls, node): pb = node.parameters collect_until_token(pb, b'<Bias>') biases = read_binary_vector(pb) find_next_tag(pb) read_placeholder(pb, 1) mapping_rule = { 'layout': 'NCHW', 'bias_term': True, 'out-size': biases.shape[0], } embed_input(mapping_rule, 2, 'biases', biases) ScaleShiftOp.update_node_stat(node, mapping_rule) return cls.enabled
def extract(node): pb = node.parameters collect_until_token(pb, b'<Dim>') dim = read_binary_integer32_token(pb) collect_until_token(pb, b'<BlockDim>') block_dim = read_binary_integer32_token(pb) if block_dim != dim: raise Error( "Dim is not equal BlockDim for BatchNorm is not supported") collect_until_token(pb, b'<Epsilon>') eps = read_binary_float_token(pb) collect_until_token(pb, b'<TargetRms>') target_rms = read_binary_float_token(pb) collect_until_token(pb, b'<TestMode>') test_mode = read_binary_bool_token(pb) if test_mode is not False: raise Error("Test mode True for BatchNorm is not supported") collect_until_token(pb, b'<StatsMean>') mean = read_binary_vector(pb) collect_until_token(pb, b'<StatsVar>') var = read_binary_vector(pb) scale = target_rms / np.sqrt(var + eps) shift = -target_rms * mean / np.sqrt(var + eps) attrs = {} embed_input(attrs, 1, 'weights', scale) embed_input(attrs, 2, 'biases', shift) ScaleShiftOp.update_node_stat(node, attrs) return __class__.enabled