예제 #1
0
def inner_product_cost(input,
                       label,
                       weight,
                       height,
                       width,
                       num_channel,
                       interp='nearest',
                       is_angle=False):
    """If is_angle, we can not back propagate through the angle, only back
       through the inner product, the loss is not consistent with the evaluation.
    """
    # make sure all the input label and weight have the same size
    if height > 1 and width > 1:
        input = pd.bilinear_interp(input=input,
                                   out_size_x=width,
                                   out_size_y=height)
        label = pd.bilinear_interp(input=label,
                                   out_size_x=width,
                                   out_size_y=height)
        if weight:
            weight = image_resize_func[interp](input=weight,
                                               out_size_x=width,
                                               out_size_y=height)

    size = height * width * num_channel

    input = util_layers.norm(input,
                             height,
                             width,
                             num_channel,
                             trans_back=False)
    label = util_layers.norm(label,
                             height,
                             width,
                             num_channel,
                             trans_back=False)

    inner = pd.mixed(size=size,
                     input=[pd.dotmul_operator(a=input, b=label, scale=1.0)])
    inner = pd.resize(input=pd.sum_cost(input=inner),
                      size=height * width,
                      height=height,
                      width=width)
    if is_angle:
        inner = util_layers.math_op(input=inner, act=pd.activation.Acos())
    else:
        inner = pd.slope_intercept(input=inner, slope=-1, intercept=1.0)

    if weight:
        inner_error = sum_weighted_loss(inner, weight, size=height * width)
    else:
        fac = 1.0 / float(height * width)
        inner = pd.slope_intercept(input=inner, slope=fac, intercept=0.0)
        inner_error = pd.sum_cost(input=inner)

    return inner_error
예제 #2
0
    def get_loss(self, start_prob, end_prob, start_label, end_label):
        """
        Compute the loss: $l_{\theta} = -logP(start)\cdotP(end|start)$

        Returns:
            A LayerOutput object containing loss.
        """
        probs = layer.seq_concat(a=start_prob, b=end_prob)
        labels = layer.seq_concat(a=start_label, b=end_label)

        log_probs = layer.mixed(
                    size=probs.size,
                    act=Act.Log(),
                    bias_attr=False,
                    input=paddle.layer.identity_projection(probs))

        neg_log_probs = layer.slope_intercept(
                        input=log_probs,
                        slope=-1,
                        intercept=0)

        loss = paddle.layer.mixed(
               size=1,
               input=paddle.layer.dotmul_operator(a=neg_log_probs, b=labels))

        sum_val = paddle.layer.pooling(input=loss,
                                       pooling_type=paddle.pooling.Sum())
        cost = paddle.layer.sum_cost(input=sum_val)
        return cost
예제 #3
0
def power(input, power=0.5):
    """power layer for input
    """
    output = math_op(input=input, act=pd.activation.Log())
    output = pd.slope_intercept(input=output, slope=power)
    output = math_op(input=output, act=pd.activation.Exp())
    return output
예제 #4
0
def relative_l1(input,
                label,
                weight,
                height,
                width,
                interp='nearest',
                is_inverse=False):
    """Relative l1 loss for depth
    """

    assert interp in image_resize_func.keys()

    # make sure all the input label and weight have the same size
    if height > 1 and width > 1:
        input = pd.bilinear_interp(input=input,
                                   out_size_x=width,
                                   out_size_y=height)
        label = pd.bilinear_interp(input=label,
                                   out_size_x=width,
                                   out_size_y=height)
        if weight:
            weight = image_resize_func[interp](input=weight,
                                               out_size_x=width,
                                               out_size_y=height)

    label_inv = util_layers.math_op(input=label, act=pd.activation.Inv())
    label_neg = pd.slope_intercept(input=label, slope=-1)
    diff = pd.addto(input=[input, label_neg],
                    act=pd.activation.Abs(),
                    bias_attr=False)

    rel_error = pd.mixed(
        size=1, input=[pd.dotmul_operator(a=diff, b=label_inv, scale=1.0)])

    if weight:
        rel_error = sum_weighted_loss(rel_error, weight, size=height * width)
    else:
        fac = 1.0 / float(height * width)
        inner = pd.slope_intercept(input=inner, slope=fac, intercept=0.0)
        inner_error = pd.sum_cost(input=inner)

    return rel_error
예제 #5
0
 def test_math_layer(self):
     addto = layer.addto(input=[pixel, pixel])
     linear_comb = layer.linear_comb(weights=weight, vectors=hidden, size=10)
     interpolation = layer.interpolation(
         input=[hidden, hidden], weight=score)
     bilinear = layer.bilinear_interp(input=conv, out_size_x=4, out_size_y=4)
     power = layer.power(input=pixel, weight=score)
     scaling = layer.scaling(input=pixel, weight=score)
     slope = layer.slope_intercept(input=pixel)
     tensor = layer.tensor(a=pixel, b=pixel, size=1000)
     cos_sim = layer.cos_sim(a=pixel, b=pixel)
     trans = layer.trans(input=tensor)
     print layer.parse_network(addto, linear_comb, interpolation, power,
                               scaling, slope, tensor, cos_sim, trans)
예제 #6
0
def iou_score(input, label, weight, height, width, class_num, is_cost=True):
    """ class num is semantic classes plus background,
        this score can also serve as iou cost for training
    """
    # input = pd.resize(input=input, size=height * width)
    # label = pd.resize(input=label, size=height * width)

    weight = pd.nearest_interp(input=weight,
                               out_size_x=width,
                               out_size_y=height)
    if not is_cost:
        # if not is cost, then it is eval, we can do
        # one hot for label. Otherwise
        input = util_layers.math_op(input=[input, weight], op='dot')
        input_one_hot = util_layers.ele_one_hot(input, class_num, height,
                                                width)
    else:
        input_one_hot = input
        input_one_hot = pd.bilinear_interp(input=input_one_hot,
                                           out_size_x=width,
                                           out_size_y=height)

    label = pd.nearest_interp(input=label, out_size_x=width, out_size_y=height)
    label = util_layers.math_op(input=[label, weight], op='dot')

    label_one_hot = util_layers.ele_one_hot(label, class_num, height, width)
    inter = util_layers.math_op(input=[input_one_hot, label_one_hot], op='dot')
    union = pd.addto(input=[input_one_hot, label_one_hot],
                     act=pd.activation.Linear(),
                     bias_attr=False)
    inter_neg = pd.slope_intercept(input=inter, slope=-1)

    union = pd.addto(input=[union, inter_neg],
                     act=pd.activation.Linear(),
                     bias_attr=False)

    inter = pd.resize(input=inter, size=height * width)
    inter = pd.sum_cost(input=inter)
    union = pd.resize(input=union, size=height * width)
    union = pd.sum_cost(input=union)

    union_inv = util_layers.math_op(input=union, act=pd.activation.Inv())
    iou = pd.mixed(size=1,
                   input=[pd.dotmul_operator(a=inter, b=union_inv, scale=1.0)])
    iou = pd.resize(input=iou, size=class_num)

    if is_cost:
        iou = pd.sum_cost(iou)

    return iou
예제 #7
0
 def test_math_layer(self):
     addto = layer.addto(input=[pixel, pixel])
     linear_comb = layer.linear_comb(
         weights=combine_weight, vectors=hidden, size=10)
     interpolation = layer.interpolation(
         input=[hidden, hidden], weight=score)
     bilinear = layer.bilinear_interp(input=conv, out_size_x=4, out_size_y=4)
     power = layer.power(input=pixel, weight=score)
     scaling = layer.scaling(input=pixel, weight=score)
     slope = layer.slope_intercept(input=pixel)
     tensor = layer.tensor(a=pixel, b=pixel, size=1000)
     cos_sim = layer.cos_sim(a=pixel, b=pixel)
     trans = layer.trans(input=tensor)
     print layer.parse_network([
         addto, linear_comb, interpolation, power, scaling, slope, tensor,
         cos_sim, trans
     ])
예제 #8
0
    def fusion_layer(self, input1, input2):
        """
        Combine input1 and input2 by concat(input1 .* input2, input1 - input2,
        input1, input2)
        """
        # fusion layer
        neg_input2 = layer.slope_intercept(input=input2,
                slope=-1.0,
                intercept=0.0)
        diff1 = layer.addto(input=[input1, neg_input2],
                act=Act.Identity(),
                bias_attr=False)
        diff2 = layer.mixed(bias_attr=False,
                input=layer.dotmul_operator(a=input1, b=input2))

        fused = layer.concat(input=[input1, input2, diff1, diff2])
        return fused
예제 #9
0
    def fusion_layer(self, input1, input2):
        """
        Combine input1 and input2 by concat(input1 .* input2, input1 - input2,
        input1, input2)
        """
        # fusion layer
        neg_input2 = layer.slope_intercept(input=input2,
                                           slope=-1.0,
                                           intercept=0.0)
        diff1 = layer.addto(input=[input1, neg_input2],
                            act=Act.Identity(),
                            bias_attr=False)
        diff2 = layer.mixed(bias_attr=False,
                            input=layer.dotmul_operator(a=input1, b=input2))

        fused = layer.concat(input=[input1, input2, diff1, diff2])
        return fused
예제 #10
0
def pixel_accuracy(input, label, weight, height, width):
    label = pd.nearest_interp(input=label, out_size_x=width, out_size_y=height)
    weight = pd.nearest_interp(input=weight,
                               out_size_x=width,
                               out_size_y=height)

    label_neg = pd.slope_intercept(input=label, slope=-1)
    diff = pd.addto(input=[input, label_neg],
                    act=pd.activation.Linear(),
                    bias_attr=False)
    correct = util_layers.math_op(input=diff, act=pd.activation.IsZero())

    correct = util_layers.math_op(input=[correct, weight], op='dot')

    correct = pd.sum_cost(input=correct)
    eval_pixels_num = pd.sum_cost(input=weight)
    divider = util_layers.math_op(input=eval_pixels_num,
                                  act=pd.activation.Inv())
    acc = util_layers.math_op(input=[correct, divider], op='dot')

    return acc
예제 #11
0
def ele_norm_cost(input,
                  label,
                  weight,
                  height=None,
                  width=None,
                  num_channel=None,
                  cost_type='l1'):
    if height > 1 and width > 1:
        input = pd.bilinear_interp(input=input,
                                   out_size_x=width,
                                   out_size_y=height)
        label = pd.bilinear_interp(input=label,
                                   out_size_x=width,
                                   out_size_y=height)
        if weight:
            weight = pd.nearest_interp(input=weight,
                                       out_size_x=width,
                                       out_size_y=height)

    size = height * width * num_channel
    if weight:
        input = pd.mixed(
            size=size,
            input=[pd.dotmul_operator(a=input, b=weight, scale=1.0)])
        label = pd.mixed(
            size=size,
            input=[pd.dotmul_operator(a=label, b=weight, scale=1.0)])
        cost = cost_func[cost_type](input=input, label=label)
        fac = pd.sum_cost(input=weight)
        fac = util_layers.math_op(input=fac, act=pd.activation.Inv())
        cost = pd.scaling(input=cost, weight=fac)
        cost = pd.sum_cost(input=cost)
    else:
        cost = cost_func[cost_type](input=input, label=label)
        fac = 1.0 / float(height * width)
        cost = pd.slope_intercept(input=cost, slope=fac, intercept=0.0)
        cost = pd.sum_cost(input=cost)

    return cost
예제 #12
0
def mul(input, other):
    output = pd.slope_intercept(input=input, slope=other)
    return output
예제 #13
0
def add(input, other):
    output = pd.slope_intercept(input=input, intercept=other)
    return output