def transform(point, center, scale, resolution, invert=False):
    """Generate and affine transformation matrix.

    Given a set of points, a center, a scale and a targer resolution, the
    function generates and affine transformation matrix. If invert is ``True``
    it will produce the inverse transformation.

    Arguments:
        point {numpy.array} -- the input 2D point
        center {numpy.array} -- the center around which to perform the transformations
        scale {float} -- the scale of the face/object
        resolution {float} -- the output resolution

    Keyword Arguments:
        invert {bool} -- define wherever the function should produce the direct or the
        inverse transformation matrix (default: {False})
    """
    point.append(1)

    h = 200.0 * scale
    t = F.matrix_diag(F.constant(1, [3]))
    t.d[0, 0] = resolution / h
    t.d[1, 1] = resolution / h
    t.d[0, 2] = resolution * (-center[0] / h + 0.5)
    t.d[1, 2] = resolution * (-center[1] / h + 0.5)

    if invert:
        t = F.reshape(F.batch_inv(F.reshape(t, [1, 3, 3])), [3, 3])

    _pt = nn.Variable.from_numpy_array(point)

    new_point = F.reshape(F.batch_matmul(
        F.reshape(t, [1, 3, 3]), F.reshape(_pt, [1, 3, 1])), [3, ])[0:2]

    return new_point.d.astype(int)
Example #2
0
def matrix_diag_part_backward(inputs):
    """
    Args:
      inputs (list of nn.Variable): Incomming grads/inputs to/of the forward function.
      kwargs (dict of arguments): Dictionary of the corresponding function arguments.

    Return:
      list of Variable: Return the gradients wrt inputs of the corresponding function.
    """
    dy = inputs[0]
    dx0 = F.matrix_diag(dy)
    return dx0
Example #3
0
def batch_eye(batch_size: int, size: int) -> nn.Variable:
    return F.broadcast(F.reshape(F.matrix_diag(F.constant(1, shape=(size, ))),
                                 shape=(1, size, size)),
                       shape=(batch_size, size, size))