def transformer(U, theta, out_size, name='<STR_LIT>'):
def _repeat(x, n_repeats):<EOL><INDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>rep = tf.transpose(tf.expand_dims(tf.ones(shape=tf.stack([<EOL>n_repeats,<EOL>])), <NUM_LIT:1>), [<NUM_LIT:1>, <NUM_LIT:0>])<EOL>rep = tf.cast(rep, '<STR_LIT>')<EOL>x = tf.matmul(tf.reshape(x, (-<NUM_LIT:1>, <NUM_LIT:1>)), rep)<EOL>return tf.reshape(x, [-<NUM_LIT:1>])<EOL><DEDENT><DEDENT>def _interpolate(im, x, y, out_size):<EOL><INDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>num_batch = tf.shape(im)[<NUM_LIT:0>]<EOL>height = tf.shape(im)[<NUM_LIT:1>]<EOL>width = tf.shape(im)[<NUM_LIT:2>]<EOL>channels = tf.shape(im)[<NUM_LIT:3>]<EOL>x = tf.cast(x, '<STR_LIT>')<EOL>y = tf.cast(y, '<STR_LIT>')<EOL>height_f = tf.cast(height, '<STR_LIT>')<EOL>width_f = tf.cast(width, '<STR_LIT>')<EOL>out_height = out_size[<NUM_LIT:0>]<EOL>out_width = out_size[<NUM_LIT:1>]<EOL>zero = tf.zeros([], dtype='<STR_LIT>')<EOL>max_y = tf.cast(tf.shape(im)[<NUM_LIT:1>] - <NUM_LIT:1>, '<STR_LIT>')<EOL>max_x = tf.cast(tf.shape(im)[<NUM_LIT:2>] - <NUM_LIT:1>, '<STR_LIT>')<EOL>x = (x + <NUM_LIT:1.0>) * (width_f) / <NUM_LIT><EOL>y = (y + <NUM_LIT:1.0>) * (height_f) / <NUM_LIT><EOL>x0 = tf.cast(tf.floor(x), '<STR_LIT>')<EOL>x1 = x0 + <NUM_LIT:1><EOL>y0 = tf.cast(tf.floor(y), '<STR_LIT>')<EOL>y1 = y0 + <NUM_LIT:1><EOL>x0 = tf.clip_by_value(x0, zero, max_x)<EOL>x1 = tf.clip_by_value(x1, zero, max_x)<EOL>y0 = tf.clip_by_value(y0, zero, max_y)<EOL>y1 = tf.clip_by_value(y1, zero, max_y)<EOL>dim2 = width<EOL>dim1 = width * height<EOL>base = _repeat(tf.range(num_batch) * dim1, out_height * out_width)<EOL>base_y0 = base + y0 * dim2<EOL>base_y1 = base + y1 * dim2<EOL>idx_a = base_y0 + x0<EOL>idx_b = base_y1 + x0<EOL>idx_c = base_y0 + x1<EOL>idx_d = base_y1 + x1<EOL>im_flat = tf.reshape(im, tf.stack([-<NUM_LIT:1>, channels]))<EOL>im_flat = tf.cast(im_flat, '<STR_LIT>')<EOL>Ia = tf.gather(im_flat, idx_a)<EOL>Ib = tf.gather(im_flat, idx_b)<EOL>Ic = tf.gather(im_flat, idx_c)<EOL>Id = tf.gather(im_flat, idx_d)<EOL>x0_f = tf.cast(x0, '<STR_LIT>')<EOL>x1_f = tf.cast(x1, '<STR_LIT>')<EOL>y0_f = tf.cast(y0, '<STR_LIT>')<EOL>y1_f = tf.cast(y1, '<STR_LIT>')<EOL>wa = tf.expand_dims(((x1_f - x) * (y1_f - y)), <NUM_LIT:1>)<EOL>wb = tf.expand_dims(((x1_f - x) * (y - y0_f)), <NUM_LIT:1>)<EOL>wc = tf.expand_dims(((x - x0_f) * (y1_f - y)), <NUM_LIT:1>)<EOL>wd = tf.expand_dims(((x - x0_f) * (y - y0_f)), <NUM_LIT:1>)<EOL>output = tf.add_n([wa * Ia, wb * Ib, wc * Ic, wd * Id])<EOL>return output<EOL><DEDENT><DEDENT>def _meshgrid(height, width):<EOL><INDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>x_t = tf.matmul(<EOL>tf.ones(shape=tf.stack([height, <NUM_LIT:1>])),<EOL>tf.transpose(tf.expand_dims(tf.linspace(-<NUM_LIT:1.0>, <NUM_LIT:1.0>, width), <NUM_LIT:1>), [<NUM_LIT:1>, <NUM_LIT:0>])<EOL>)<EOL>y_t = tf.matmul(tf.expand_dims(tf.linspace(-<NUM_LIT:1.0>, <NUM_LIT:1.0>, height), <NUM_LIT:1>), tf.ones(shape=tf.stack([<NUM_LIT:1>, width])))<EOL>x_t_flat = tf.reshape(x_t, (<NUM_LIT:1>, -<NUM_LIT:1>))<EOL>y_t_flat = tf.reshape(y_t, (<NUM_LIT:1>, -<NUM_LIT:1>))<EOL>ones = tf.ones_like(x_t_flat)<EOL>grid = tf.concat(axis=<NUM_LIT:0>, values=[x_t_flat, y_t_flat, ones])<EOL>return grid<EOL><DEDENT><DEDENT>def _transform(theta, input_dim, out_size):<EOL><INDENT>with tf.variable_scope('<STR_LIT>'):<EOL><INDENT>num_batch = tf.shape(input_dim)[<NUM_LIT:0>]<EOL>num_channels = tf.shape(input_dim)[<NUM_LIT:3>]<EOL>theta = tf.reshape(theta, (-<NUM_LIT:1>, <NUM_LIT:2>, <NUM_LIT:3>))<EOL>theta = tf.cast(theta, '<STR_LIT>')<EOL>out_height = out_size[<NUM_LIT:0>]<EOL>out_width = out_size[<NUM_LIT:1>]<EOL>grid = _meshgrid(out_height, out_width)<EOL>grid = tf.expand_dims(grid, <NUM_LIT:0>)<EOL>grid = tf.reshape(grid, [-<NUM_LIT:1>])<EOL>grid = tf.tile(grid, tf.stack([num_batch]))<EOL>grid = tf.reshape(grid, tf.stack([num_batch, <NUM_LIT:3>, -<NUM_LIT:1>]))<EOL>T_g = tf.matmul(theta, grid)<EOL>x_s = tf.slice(T_g, [<NUM_LIT:0>, <NUM_LIT:0>, <NUM_LIT:0>], [-<NUM_LIT:1>, <NUM_LIT:1>, -<NUM_LIT:1>])<EOL>y_s = tf.slice(T_g, [<NUM_LIT:0>, <NUM_LIT:1>, <NUM_LIT:0>], [-<NUM_LIT:1>, <NUM_LIT:1>, -<NUM_LIT:1>])<EOL>x_s_flat = tf.reshape(x_s, [-<NUM_LIT:1>])<EOL>y_s_flat = tf.reshape(y_s, [-<NUM_LIT:1>])<EOL>input_transformed = _interpolate(input_dim, x_s_flat, y_s_flat, out_size)<EOL>output = tf.reshape(input_transformed, tf.stack([num_batch, out_height, out_width, num_channels]))<EOL>return output<EOL><DEDENT><DEDENT>with tf.variable_scope(name):<EOL><INDENT>output = _transform(theta, U, out_size)<EOL>return output<EOL><DEDENT>
Spatial Transformer Layer for `2D Affine Transformation <https://en.wikipedia.org/wiki/Affine_transformation>`__
    , see :class:`SpatialTransformer2dAffineLayer` class.

    Parameters
    ----------
    U : list of float
        The output of a convolutional net should have the
        shape [num_batch, height, width, num_channels].
    theta: float
        The output of the localisation network should be [num_batch, 6], value range should be [0, 1] (via tanh).
    out_size: tuple of int
        The size of the output of the network (height, width)
    name: str
        Optional function name

    Returns
    -------
    Tensor
        The transformed tensor.

    References
    ----------
    - `Spatial Transformer Networks <https://arxiv.org/abs/1506.02025>`__
    - `TensorFlow/Models <https://github.com/tensorflow/models/tree/master/transformer>`__

    Notes
    -----
    To initialize the network to the identity transform init.

    >>> import tensorflow as tf
    >>> # ``theta`` to
    >>> identity = np.array([[1., 0., 0.], [0., 1., 0.]])
    >>> identity = identity.flatten()
    >>> theta = tf.Variable(initial_value=identity)