Пример #1
0
def ST_CPAB_transformer(U, theta, out_size):
    """ Spatial transformer using CPAB transformations

    Arguments:
        U: 4D-`Tensor` [n_batch, height, width, n_channels]. Input images to
            transform.
        theta: `Matrix` [n_batch, d]. Parameters for the transformation. Each
            row specify a transformation for each input image. The number d is
            determined by tessalation. See transformer/setup_CPAB_transformer.py
            for more information.
        out_size: `list` where out_size[0] is the output height and out_size[1]
            is the output width of each interpolated image.

    Output:
        V: 4D-`Tensor` [n_batch, out_size[0], out_size[1], n_channels]. Tensor
            with transformed images.
    """
    with tf.name_scope('ST_CPAB_transformer'):
        # Create grid of points
        out_height = out_size[0]
        out_width = out_size[1]
        grid = tf_meshgrid(out_height, out_width)

        # Transform grid
        T_g = tf_CPAB_transformer(grid[:2], theta)

        # Slice and reshape
        x_s = tf.slice(T_g, [0, 0, 0], [-1, 1, -1])
        y_s = tf.slice(T_g, [0, 1, 0], [-1, 1, -1])
        x_s_flat = tf.reshape(x_s, [-1])
        y_s_flat = tf.reshape(y_s, [-1])

        # Interpolate values
        V = tf_interpolate(U, x_s_flat, y_s_flat, out_size)
        return V
Пример #2
0
def ST_TPS_transformer(U, theta, out_size, tps_size=[4, 4]):
    #GUY
    tps_size = [8, 8]
    """ Spatial transformer using thin plate spline transformations

    Arguments:
        U: 4D-`Tensor` [n_batch, height, width, n_channels]. Input images to
            transform.
        theta: `Matrix` [n_batch, 2*tps_size[0]*tps_size[1]]. Parameters for
            the transformation. Each row specify a transformation for each
            input image.
        out_size: `list` where out_size[0] is the output height and out_size[1]
            is the output width of each interpolated image.
        tps_size: `list` where tps_size[0] is the number of points in the x
            direction and tps_size[1] is the number of points in the y direction.
            This should be set to match the dimension of theta.
    Output:
        V: 4D-`Tensor` [n_batch, out_size[0], out_size[1], n_channels]. Tensor
            with transformed images.
    """
    with tf.name_scope('ST_TPS_transformer'):
        theta = tf.reshape(theta, (-1, tps_size[0] * tps_size[1], 2))

        # Create grid of points
        out_height = out_size[0]
        out_width = out_size[1]
        grid = tf_meshgrid(out_height, out_width)

        # Call transformer
        T_g = tf_TPS_transformer(grid, theta)

        # Slice and reshape
        x_s = tf.slice(T_g, [0, 0, 0], [-1, 1, -1])
        y_s = tf.slice(T_g, [0, 1, 0], [-1, 1, -1])
        x_s_flat = tf.reshape(x_s, [-1])
        y_s_flat = tf.reshape(y_s, [-1])

        # Interpolate values
        V = tf_interpolate(U, x_s_flat, y_s_flat, out_size)
        return V
Пример #3
0
def ST_Homografy_transformer(U, theta, out_size):
    """ Spatial transformer using homografy transformations

    Arguments:
        U: 4D-`Tensor` [n_batch, height, width, n_channels]. Input images to
            transform.
        theta: `Matrix` [n_batch, 9]. Parameters for the transformation. Each
            row specify a transformation for each input image.
        out_size: `list` where out_size[0] is the output height and out_size[1]
            is the output width of each interpolated image.

    Output:
        V: 4D-`Tensor` [n_batch, out_size[0], out_size[1], n_channels]. Tensor
            with transformed images.
    """
    with tf.name_scope('ST_Homografy_transformer'):
        # Reshape theta
        theta = tf.reshape(theta, (-1, 3, 3))

        # Make it a valid homografy
        theta = theta / tf.norm(theta, axis=[1, 2], keep_dims=True)

        # Create grid of points
        out_height = out_size[0]
        out_width = out_size[1]
        grid = tf_meshgrid(out_height, out_width)

        # Call transformer
        T_g = tf_Homografy_transformer(grid, theta)

        # Slice and reshape
        x_s = tf.slice(T_g, [0, 0, 0], [-1, 1, -1])
        y_s = tf.slice(T_g, [0, 1, 0], [-1, 1, -1])
        x_s_flat = tf.reshape(x_s, [-1])
        y_s_flat = tf.reshape(y_s, [-1])

        # Interpolate values
        V = tf_interpolate(U, x_s_flat, y_s_flat, out_size)
        return V