示例#1
0
def tf_map_coordinates(input, coords, order=1):
    """Tensorflow verion of scipy.ndimage.map_coordinates"""

    assert order == 1

    coords_tl = tf.cast(tf.floor(coords), 'int32')
    coords_br = tf.cast(tf.ceil(coords), 'int32')
    coords_bl = tf.stack([coords_tl[:, 0], coords_br[:, 1]], axis=1)
    coords_tr = tf.stack([coords_br[:, 0], coords_tl[:, 1]], axis=1)

    vals_tl = tf.gather_nd(input, coords_tl)
    vals_br = tf.gather_nd(input, coords_br)
    vals_bl = tf.gather_nd(input, coords_bl)
    vals_tr = tf.gather_nd(input, coords_tr)

    h_offset = coords[:, 0] - tf.cast(coords_tl[:, 0], tf.float32)

    h_int_t = (((1.0 - h_offset) * vals_tl) + (h_offset * vals_tr))
    h_int_b = (((1.0 - h_offset) * vals_bl) + (h_offset * vals_br))

    v_offset = coords[:, 1] - tf.cast(coords_tl[:, 1], tf.float32)

    int_vals = (((1.0 - v_offset) * h_int_t) + (v_offset * h_int_b))

    return int_vals
示例#2
0
def _find_subpixel_maxima(x,
                          kernel_size,
                          sigma,
                          upsample_factor,
                          coordinate_scale=1,
                          confidence_scale=255.):

    kernel = gaussian_kernel_2d(kernel_size, sigma)
    kernel = tf.expand_dims(kernel, 0)

    x_shape = tf.shape(x)
    rows = x_shape[1]
    cols = x_shape[2]

    max_vals = tf.reduce_max(tf.reshape(x, [-1, rows * cols]), axis=1)
    max_vals = tf.reshape(max_vals, [-1, 1]) / confidence_scale

    row_pad = rows // 2 - kernel_size // 2
    col_pad = cols // 2 - kernel_size // 2
    padding = [[0, 0], [row_pad, row_pad - 1], [col_pad, col_pad - 1]]
    kernel = tf.pad(kernel, padding)

    row_center = row_pad + (kernel_size // 2)
    col_center = col_pad + (kernel_size // 2)
    center = tf.stack([row_center, col_center])
    center = tf.expand_dims(center, 0)
    center = tf.cast(center, dtype=tf.float32)

    shifts = _upsampled_registration(x, kernel, upsample_factor)
    shifts = center - shifts
    shifts *= coordinate_scale
    maxima = tf.concat([shifts[:, ::-1], max_vals], -1)

    return maxima
 def _get_vals_by_coords(input, coords):
     coords_0_flat = tf.reshape(coords[..., 0], [-1])
     coords_1_flat = tf.reshape(coords[..., 1], [-1])
     indices = tf.stack([idx, coords_0_flat, coords_1_flat], axis=-1)
     vals = tf.gather_nd(input, indices)
     vals = tf.reshape(vals, (batch_size, n_coords))
     return vals
示例#4
0
def degree_matrix(A, return_sparse_batch=False):
    """
    Computes the degree matrix of A, deals with sparse A and batch mode
    automatically.
    :param A: Tensor or SparseTensor with rank k = {2, 3}.
    :param return_sparse_batch: if operating in batch mode, return a
    SparseTensor. Note that the sparse degree tensor returned by this function
    cannot be used for sparse matrix multiplication afterwards.
    :return: SparseTensor of rank k.
    """
    D = degrees(A)

    batch_mode = K.ndim(D) == 2
    N = tf.shape(D)[-1]
    batch_size = tf.shape(D)[0] if batch_mode else 1

    inner_index = tf.tile(tf.stack([tf.range(N)] * 2, axis=1), (batch_size, 1))
    if batch_mode:
        if return_sparse_batch:
            outer_index = tf_repeat_1d(
                tf.range(batch_size),
                tf.ones(batch_size) * tf.cast(N, tf.float32))
            indices = tf.concat([outer_index[:, None], inner_index], 1)
            dense_shape = (batch_size, N, N)
        else:
            return tf.linalg.diag(D)
    else:
        indices = inner_index
        dense_shape = (N, N)

    indices = tf.cast(indices, tf.int64)
    values = tf.reshape(D, (-1, ))
    return tf.SparseTensor(indices, values, dense_shape)
def batch_map_coordinates(input, coords, order=1):
    """Batch version of tf_map_coordinates"""

    input_shape = tf.shape(input)
    batch_size = input_shape[0]
    input_size = input_shape[1]

    #coords = tf.reshape(coords, (batch_size, -1, 2))

    n_coords = tf.shape(coords)[1]

    coords = tf.clip_by_value(coords, 0, tf.cast(input_size, 'float32') - 1)

    coords_tl = tf.cast(tf.floor(coords), 'int32')
    coords_br = tf.cast(tf.ceil(coords), 'int32')
    coords_bl = tf.stack([coords_tl[..., 0], coords_br[..., 1]], axis=-1)
    coords_tr = tf.stack([coords_br[..., 0], coords_tl[..., 1]], axis=-1)

    idx = tf.range(batch_size)
    idx = tf.expand_dims(idx, -1)
    idx = tf.tile(idx, [1, n_coords])
    idx = tf.reshape(idx, [-1])

    def _get_vals_by_coords(input, coords):
        coords_0_flat = tf.reshape(coords[..., 0], [-1])
        coords_1_flat = tf.reshape(coords[..., 1], [-1])
        indices = tf.stack([idx, coords_0_flat, coords_1_flat], axis=-1)
        vals = tf.gather_nd(input, indices)
        vals = tf.reshape(vals, (batch_size, n_coords))
        return vals

    vals_tl = _get_vals_by_coords(input, coords_tl)
    vals_br = _get_vals_by_coords(input, coords_br)
    vals_bl = _get_vals_by_coords(input, coords_bl)
    vals_tr = _get_vals_by_coords(input, coords_tr)

    h_offset = coords[..., 0] - tf.cast(coords_tl[..., 0], tf.float32)

    h_int_t = (((1.0 - h_offset) * vals_tl) + (h_offset * vals_tr))
    h_int_b = (((1.0 - h_offset) * vals_bl) + (h_offset * vals_br))

    v_offset = coords[..., 1] - tf.cast(coords_tl[..., 1], tf.float32)

    int_vals = (((1.0 - v_offset) * h_int_t) + (v_offset * h_int_b))

    return int_vals
示例#6
0
def get_score(stylized_image, z_style, alpha, alpha_mean=0.5, alpha_sigma=0,
              weight_image=20, weight_prior=1, image_score_fun='blue', **kwargs):

    score_funs = {'blue': blue_score, 'mem': mem_score, 'aes': aes_score,
                  'scary': partial(emotion_score, emotion_type='scary'),
                  'gloomy': partial(emotion_score, emotion_type='gloomy'),
                  'peaceful': partial(emotion_score, emotion_type='peaceful'),
                  'happy': partial(emotion_score, emotion_type='happy')}
    gp = weight_prior * gaussian_prior(z_style)
    isf = weight_image * score_funs[image_score_fun](stylized_image, **kwargs)
    ap = alpha_prior(alpha, alpha_mean, alpha_sigma)
    return ktf.stack([gp, isf, ap])
示例#7
0
def batch_map_offsets(input, offsets, order=1):
    """Batch map offsets into input
    Adds index of every entry to the entry to make it's interpolation
    relevant to it's location
    """

    offset_shape = offsets.get_shape()
    batch_size = tf.shape(offsets)[0]

    input_h = offset_shape[1]
    input_w = offset_shape[2]

    channel_size = int(offset_shape[3].value)
    #offsets = tf.reshape(offsets, (batch_size, -1, 2))
    #################### DEFAULT COORDINATES FOR EVERY POINT ####################
    ind_add = tf.meshgrid(tf.range(1, input_h + 1, delta=1),
                          tf.range(1, input_w + 1, delta=1),
                          indexing='ij')
    ind_add = tf.stack(ind_add, axis=-1)
    ind_add = tf.cast(ind_add, 'float32')
    ind_add = tf.reshape(ind_add, (1, input_h, input_w, 2))
    ind_add = tf.tile(ind_add, [batch_size, 1, 1, int(channel_size / 2)])
    #############################################################################

    #################### KERNEL OFFSET FOR EVERY POINT ####################
    ind_zero = tf.meshgrid(tf.range(-1, 2, delta=1),
                           tf.range(-1, 2, delta=1),
                           indexing='ij')
    ind_zero = tf.stack(ind_zero, axis=-1)
    ind_zero = tf.cast(ind_zero, 'float32')
    ind_zero = tf.reshape(ind_zero, (1, 1, 1, channel_size))
    ind_zero = tf.tile(ind_zero, [batch_size, input_h, input_w, 1])
    #######################################################################

    coords = offsets + ind_add + ind_zero

    int_vals = batch_map_coordinates(input, coords, int(channel_size / 2))
    return int_vals
def batch_map_offsets(input, offsets, order=1):
    """Batch map offsets into input
    Adds index of every entry to the entry to make it's interpolation
    relevant to it's location
    """

    input_shape = tf.shape(input)
    batch_size = input_shape[0]
    input_w = input_shape[1]
    input_h = input_shape[2]
    offsets = tf.reshape(offsets, (batch_size, -1, 2))

    ind_add = tf.meshgrid(tf.range(input_w), tf.range(input_h), indexing='ij')
    ind_add = tf.stack(ind_add, axis=-1)
    ind_add = tf.cast(ind_add, 'float32')
    ind_add = tf.reshape(ind_add, (-1, 2))
    ind_add = tf.expand_dims(ind_add, 0)
    ind_add = tf.tile(ind_add, [batch_size, 1, 1])

    coords = offsets + ind_add

    int_vals = batch_map_coordinates(input, coords)
    return int_vals
示例#9
0
def batch_map_coordinates(input, coords, n_coords):
    """Batch version of tf_map_coordinates"""
    #init_input_shape = input.get_shape()
    #input = tf.reshape(input, (-1, init_input_shape[3]))
    #coords = tf.reshape(coords, (-1, n_coords * 2))
    input_shape = input.get_shape()
    input_h = input_shape[1].value
    input_w = input_shape[2].value
    #batch_size = input_shape[0]
    #input_size = input_shape[1]

    #coords = tf.reshape(coords, (batch_size, -1, 2))

    #n_coords = tf.shape(coords)[1]

    coords_h = tf.clip_by_value(coords[..., :n_coords], 0,
                                tf.cast(input_h, 'float32') - 1)
    coords_w = tf.clip_by_value(coords[..., n_coords:], 0,
                                tf.cast(input_w, 'float32') - 1)
    coords = tf.stack([coords_h, coords_w], axis=-1)

    coords_tl = tf.cast(tf.floor(coords), 'float32')
    coords_br = tf.cast(tf.ceil(coords), 'float32')
    coords_bl = tf.stack([coords_tl[..., 0], coords_br[..., 1]], axis=-1)
    coords_tr = tf.stack([coords_br[..., 0], coords_tl[..., 1]], axis=-1)

    #idx = tf.range(batch_size)
    #idx = tf.expand_dims(idx, -1)
    #idx = tf.tile(idx, [1, n_coords])
    #idx = tf.reshape(idx, [-1])

    def _get_vals_by_coords(input, coords, n_coords):
        coords_shape = tf.shape(coords)
        input_shape = input.get_shape()
        input_w = input_shape[2].value
        input_h = input_shape[1].value
        channel_size = input_shape[3].value
        batch_size = tf.shape(input)[0]
        input = tf.transpose(input, (0, 3, 1, 2))
        input = tf.reshape(input, (-1, channel_size, input_h * input_w))

        indices = coords[..., 0] * input_w + coords[..., 1]
        #indices = tf.expand_dims(indices, axis=1)
        #indices = tf.tile(indices, [1, channel_size, 1, 1, 1])
        #indices = tf.reshape(indices, (-1, channel_size, input_h * input_w * n_coords))
        #indices = tf.transpose(indices, (0, 3, 1, 2))
        indices = tf.reshape(indices, (-1, input_h * input_w * n_coords))
        indices = tf.cast(indices, 'int32')
        #indices = tf.reshape(indices, [-1])
        #input = tf.reshape(input, [-1])
        vals = tf.gather(input, indices[0], axis=-1)
        #vals = tf.map_fn(lambda x: tf.gather(x[0], x[1], axis=-1), (input,indices), dtype=tf.float32)
        vals = tf.reshape(vals, (-1, channel_size, input_h, input_w, n_coords))
        return vals

    vals_tl = (1 + (coords_tl[..., 0] - coords[..., 0])) * \
       (1 + (coords_tl[..., 1] - coords[..., 1]))
    vals_br = (1 - (coords_br[..., 0] - coords[..., 0])) * \
       (1 - (coords_br[..., 1] - coords[..., 1]))
    vals_bl = (1 + (coords_bl[..., 0] - coords[..., 0])) * \
       (1 + (coords_bl[..., 1] - coords[..., 1]))
    vals_tr = (1 - (coords_tr[..., 0] - coords[..., 0])) * \
       (1 - (coords_tr[..., 1] - coords[..., 1]))

    x_vals_tl = _get_vals_by_coords(input, coords_tl, n_coords)
    x_vals_br = _get_vals_by_coords(input, coords_br, n_coords)
    x_vals_bl = _get_vals_by_coords(input, coords_bl, n_coords)
    x_vals_tr = _get_vals_by_coords(input, coords_tr, n_coords)

    #h_offset = coords[..., 0] - tf.cast(coords_tl[..., 0], tf.float32)

    #h_int_t = (((1.0 - h_offset) * vals_tl) + (h_offset * vals_tr))
    #h_int_b = (((1.0 - h_offset) * vals_bl) + (h_offset * vals_br))

    #v_offset = coords[..., 1] - tf.cast(coords_tl[..., 1], tf.float32)

    #int_vals = (((1.0 - v_offset) * h_int_t) + (v_offset * h_int_b))
    int_vals = tf.expand_dims(vals_tl, 1) * x_vals_tl + \
        tf.expand_dims(vals_br, 1) * x_vals_br + \
        tf.expand_dims(vals_bl, 1) * x_vals_bl + \
        tf.expand_dims(vals_tr, 1) * x_vals_tr
    return int_vals