Esempio n. 1
0
def radon_transform(x, theta):

    x = tf.cast(x, dtype=tf.float32)

    x_shape = tf.shape(x)
    n_cols = x_shape[2]
    n_rows = x_shape[1]
    n_frames = x_shape[0]
    n_angles = tf.shape(theta)[0]

    x = tf.reshape(x, (-1, 1, n_rows, n_cols, 1))
    x = tf.tile(x, (1, n_angles, 1, 1, 1))
    x = tf.reshape(x, (-1, n_rows, n_cols, 1))

    repeated_theta = repeat_theta(theta, n_angles, n_frames)

    x = tf.cast(x, dtype=tf.uint8)
    #x = tf.contrib.image.rotate(x, repeated_theta, interpolation='BILINEAR')
    x = tf.cast(x, dtype=tf.float32)

    x = tf.reshape(x, (-1, n_angles, n_rows, n_cols, 1))
    x = tf.cast(x, dtype=tf.float32)
    x = tf.reduce_sum(x, 2)

    return x
Esempio n. 2
0
def _register_rotation(target_image, src_image, rotation_resolution,
                       rotation_guess, upsample_factor):

    n_angles = tf.cast(tf.round(180. / rotation_resolution), tf.int32)
    theta = tf.linspace(0., 180. - rotation_resolution, n_angles)
    theta = -radians(theta)

    target_shape = tf.shape(target_image)
    target_image = tf.reshape(target_image, target_shape[:3])
    src_shape = tf.shape(src_image)
    src_image = tf.reshape(src_image, src_shape[:3])

    rotation_guess = tf.constant(rotation_guess, tf.float32)
    rotation_resolution = tf.constant(rotation_resolution, tf.float32)

    src_image = radon_transform_fft(src_image, theta)
    target_image = radon_transform_fft(target_image, theta)
    shifts = _upsampled_registration(target_image, src_image, upsample_factor)

    angles = shifts[:, 0] * rotation_resolution
    angles = tf.reshape(angles, [-1, 1])
    angles = check_angles(angles, rotation_guess)
    angles = radians(angles)

    return angles
Esempio n. 3
0
def resize_images(x, height_factor, width_factor, data_format):
    if data_format == 'channels_first':
        original_shape = K.int_shape(x)
        new_shape = tf.shape(x)[2:]

        new_shape = K.cast(new_shape, 'float64')
        new_shape *= tf.constant(np.array([height_factor, width_factor]))
        new_shape = K.cast(new_shape, 'int32')

        x = K.permute_dimensions(x, [0, 2, 3, 1])
        x = tf.image.resize_nearest_neighbor(x, new_shape)
        x = K.permute_dimensions(x, [0, 3, 1, 2])
        x.set_shape((None, None, original_shape[2] *
                     height_factor if original_shape[2] is not None else None,
                     original_shape[3] *
                     width_factor if original_shape[3] is not None else None))
        return x
    elif data_format == 'channels_last':
        original_shape = K.int_shape(x)
        new_shape = tf.shape(x)[1:3]

        new_shape = K.cast(new_shape, 'float64')
        new_shape *= tf.constant(np.array([height_factor, width_factor]))
        new_shape = K.cast(new_shape, 'int32')

        x = tf.image.resize_nearest_neighbor(x, new_shape)
        x.set_shape(
            (None, original_shape[1] *
             height_factor if original_shape[1] is not None else None,
             original_shape[2] *
             width_factor if original_shape[2] is not None else None, None))
        return x
    else:
        raise ValueError('Invalid data_format:', data_format)
Esempio n. 4
0
def fftshift1d(x, axis=0):

    x_shape = tf.shape(x)
    x = tf.reshape(x, (-1, 1))
    n_samples = tf.cast(tf.shape(x)[0], tf.float32)
    even = n_samples / 2.
    even = tf.round(even)
    even = even * 2.
    even = tf.equal(n_samples, even)

    def true_fn():
        return x

    def false_fn():
        x_padded = tf.concat([x, tf.zeros((1, 1))], axis=0)
        return x_padded

    x = tf.cond(even, true_fn, false_fn)
    x1, x2 = tf.split(x, 2, axis=axis)

    def true_fn():
        return x2

    def false_fn():
        x2_unpadded = x2[:-1]
        return x2_unpadded

    x2 = tf.cond(even, true_fn, false_fn)
    x = tf.concat((x2, x1), axis=axis)
    x = tf.reshape(x, x_shape)

    return x
Esempio n. 5
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)
Esempio n. 6
0
    def get_gradient_penalty_loss(self, for_discriminator=True):
        if self.gradient_penalty_weight == 0:
            return []

        inp = self.discriminator_input if for_discriminator else self.generator_input
        if type(inp) == list:
            batch_size = ktf.shape(inp[0])[0]
        else:
            batch_size = ktf.shape(inp)[0]

        points = self.grad_generator_output
        print K.int_shape(points)

        gp_list = []
        disc_out = self.discriminator([points])
        if type(disc_out) != list:
            disc_out = [disc_out]
        gradients = ktf.gradients(disc_out[0], points)

        for gradient in gradients:
            if gradient is None:
                continue
            gradient = ktf.reshape(gradient, (batch_size, -1))
            gradient_l2_norm = ktf.sqrt(ktf.reduce_sum(ktf.square(gradient), axis=1))
            if for_discriminator:
                gradient_penalty = self.gradient_penalty_weight * ktf.square(1 - gradient_l2_norm)
            else:
                gradient_penalty = -self.gradient_penalty_weight_generator * gradient_l2_norm
            gp_list.append(ktf.reduce_mean(gradient_penalty))

        if for_discriminator:
            for i in range(len(gp_list)):
                self.discriminator_metric_names.append('gp_loss_' + str(i))
        return gp_list
Esempio n. 7
0
File: gan.py Progetto: arosset42/gan
    def get_gradient_penalty_loss(self):
        if self.gradient_penalty_weight == 0:
            return []

        if type(self.discriminator_input) == list:
            batch_size = ktf.shape(self.discriminator_input[0])[0]
            ranks = [len(inp.get_shape().as_list()) for inp in self.discriminator_input]
        else:
            batch_size = ktf.shape(self.discriminator_input)[0]
            ranks = [len(self.discriminator_input.get_shape().as_list())]

        def cast_all(values, reference_type_vals):
            return [ktf.cast(alpha, dtype=ref.dtype) for alpha, ref in zip(values, reference_type_vals)]

        def std_if_not_int(val):
            if val.dtype.is_integer:
                return 0
            else:
                return ktf.stop_gradient(K.std(val, keepdims=True))

        def point_for_gp_wgan():
            weights = ktf.random_uniform((batch_size, 1), minval=0, maxval=1)
            weights = [ktf.reshape(weights, (-1, ) + (1, ) * (rank - 1)) for rank in ranks]
            weights = cast_all(weights, self.discriminator_input)
            points = [(w * r) + ((1 - w) * f) for r, f, w in zip(self.discriminator_input, self.generator_output, weights)]
            return points

        def points_for_dragan():
            alphas = ktf.random_uniform((batch_size, 1), minval=0, maxval=1)
            alphas = [ktf.reshape(alphas, (-1, ) + (1, ) * (rank - 1)) for rank in ranks]
            alphas = cast_all(alphas, self.discriminator_input)
            fake = [ktf.random_uniform(ktf.shape(t), minval=0, maxval=1) * std_if_not_int(t) * 0.5
                       for t in self.discriminator_input]
            fake = cast_all(fake, self.discriminator_input)

            points = [(w * r) + ((1 - w) * f) for r, f, w in zip(self.discriminator_input, fake, alphas)]
            return points

        points = {'wgan-gp': point_for_gp_wgan(), 'dragan': points_for_dragan()}
        points = points[self.gradient_penalty_type]

        gp_list = []
        disc_out = self.discriminator(points)
        if type(disc_out) != list:
            disc_out = [disc_out]
        gradients = ktf.gradients(disc_out[0], points)

        for gradient in gradients:
            if gradient is None:
                continue
            gradient = ktf.reshape(gradient, (batch_size, -1))
            gradient_l2_norm = ktf.sqrt(ktf.reduce_sum(ktf.square(gradient), axis=1))
            gradient_penalty = self.gradient_penalty_weight * ktf.square(1 - gradient_l2_norm)
            gp_list.append(ktf.reduce_mean(gradient_penalty))

        for i in range(len(gp_list)):
            self.discriminator_metric_names.append('gp_loss_' + str(i))
        return gp_list
Esempio n. 8
0
def top_k(scores, I, ratio, top_k_var):
    """
    Returns indices to get the top K values in `scores` segment-wise, with
    segments defined by I. K is not fixed, but it is defined as a ratio of the
    number of elements in each segment.
    :param scores: a rank 1 tensor with scores;
    :param I: a rank 1 tensor with segment IDs;
    :param ratio: float, ratio of elements to keep for each segment;
    :param top_k_var: a tf.Variable without shape validation (e.g.,
    `tf.Variable(0.0, validate_shape=False)`);
    :return: a rank 1 tensor containing the indices to get the top K values of
    each segment in `scores`.
    """
    num_nodes = tf.segment_sum(tf.ones_like(I),
                               I)  # Number of nodes in each graph
    cumsum = tf.cumsum(num_nodes)  # Cumulative number of nodes (A, A+B, A+B+C)
    cumsum_start = cumsum - num_nodes  # Start index of each graph
    n_graphs = tf.shape(num_nodes)[0]  # Number of graphs in batch
    max_n_nodes = tf.reduce_max(num_nodes)  # Order of biggest graph in batch
    batch_n_nodes = tf.shape(I)[0]  # Number of overall nodes in batch
    to_keep = tf.ceil(ratio * tf.cast(num_nodes, tf.float32))
    to_keep = tf.cast(to_keep, tf.int32)  # Nodes to keep in each graph

    index = tf.range(batch_n_nodes)
    index = (index - tf.gather(cumsum_start, I)) + (I * max_n_nodes)

    y_min = tf.reduce_min(scores)
    dense_y = tf.ones((n_graphs * max_n_nodes, ))
    dense_y = dense_y * tf.cast(
        y_min - 1, tf.float32
    )  # subtract 1 to ensure that filler values do not get picked
    dense_y = tf.assign(
        top_k_var, dense_y, validate_shape=False
    )  # top_k_var is a variable with unknown shape defined in the elsewhere
    dense_y = tf.scatter_update(dense_y, index, scores)
    dense_y = tf.reshape(dense_y, (n_graphs, max_n_nodes))

    perm = tf.argsort(dense_y, direction='DESCENDING')
    perm = perm + cumsum_start[:, None]
    perm = tf.reshape(perm, (-1, ))

    to_rep = tf.tile(tf.constant([1., 0.]), (n_graphs, ))
    rep_times = tf.reshape(
        tf.concat((to_keep[:, None], (max_n_nodes - to_keep)[:, None]), -1),
        (-1, ))
    mask = tf_repeat_1d(to_rep, rep_times)

    perm = tf.boolean_mask(perm, mask)

    return perm
Esempio n. 9
0
def resize_images(x, height_factor, width_factor, interpolation, data_format):
    """Resizes the images contained in a 4D tensor.
    # Arguments
        x: Tensor or variable to resize.
        height_factor: Positive integer.
        width_factor: Positive integer.
        interpolation: string, "nearest", "bilinear" or "bicubic"
        data_format: string, `"channels_last"` or `"channels_first"`.
    # Returns
        A tensor.
    # Raises
        ValueError: if `data_format` is neither `"channels_last"` or `"channels_first"`.
    """
    if interpolation == 'nearest':
        tf_resize = tf.image.resize_nearest_neighbor
    elif interpolation == 'bilinear':
        tf_resize = tf.image.resize_bilinear
    elif interpolation == 'bicubic':
        tf_resize = tf.image.resize_bicubic
    else:
        raise ValueError('Invalid interpolation method:', interpolation)
    if data_format == 'channels_first':
        original_shape = int_shape(x)
        new_shape = tf.shape(x)[2:]
        new_shape *= tf.constant(
            np.array([height_factor, width_factor]).astype('int32'))
        x = permute_dimensions(x, [0, 2, 3, 1])
        x = tf_resize(x, new_shape, align_corners=True)
        x = permute_dimensions(x, [0, 3, 1, 2])
        x.set_shape((None, None, original_shape[2] *
                     height_factor if original_shape[2] is not None else None,
                     original_shape[3] *
                     width_factor if original_shape[3] is not None else None))
        return x
    elif data_format == 'channels_last':
        original_shape = int_shape(x)
        new_shape = tf.shape(x)[1:3]
        new_shape *= tf.constant(
            np.array([height_factor, width_factor]).astype('int32'))
        x = tf_resize(x, new_shape, align_corners=True)
        x.set_shape(
            (None, original_shape[1] *
             height_factor if original_shape[1] is not None else None,
             original_shape[2] *
             width_factor if original_shape[2] is not None else None, None))
        return x
    else:
        raise ValueError('Invalid data_format:', data_format)
Esempio n. 10
0
    def call(self, inputs):
        print("xxxx",inputs)
        expanded_tensor = ktf.expand_dims(inputs[0], -1)
        multiples = [1, self.number_of_transforms, 1, 1, 1]
        tiled_tensor = ktf.tile(expanded_tensor, multiples=multiples)
        repeated_tensor = ktf.reshape(tiled_tensor, ktf.shape(inputs[0]) * np.array([self.number_of_transforms, 1, 1, 1]))

        affine_transforms = inputs[1] / self.affine_mul

        affine_transforms = ktf.reshape(affine_transforms, (-1, 8))
        tranformed = tf_affine_transform(repeated_tensor, affine_transforms)
        res = ktf.reshape(tranformed, [-1, self.number_of_transforms] + self.image_size)
        res = ktf.transpose(res, [0, 2, 3, 1, 4])

        #Use masks
        if len(inputs) == 3:
            mask = ktf.transpose(inputs[2], [0, 2, 3, 1])
            mask = ktf.image.resize_images(mask, self.image_size[:2], method=ktf.image.ResizeMethod.NEAREST_NEIGHBOR)
            res = res * ktf.expand_dims(mask, axis=-1)


        if self.aggregation_fn == 'none':
            res = ktf.reshape(res, [-1] + self.image_size[:2] + [self.image_size[2] * self.number_of_transforms])
        elif self.aggregation_fn == 'max':
            res = ktf.reduce_max(res, reduction_indices=[-2])
        elif self.aggregation_fn == 'avg':
            counts = ktf.reduce_sum(mask, reduction_indices=[-1])
            counts = ktf.expand_dims(counts, axis=-1)
            res = ktf.reduce_sum(res, reduction_indices=[-2])
            res /= counts
            res = ktf.where(ktf.is_nan(res), ktf.zeros_like(res), res)
        return res
Esempio n. 11
0
def sparse_bool_mask(x, mask, axis=0):
    # Only necessary if indices may have non-unique elements
    indices = tf.boolean_mask(tf.range(tf.shape(x)[axis]), mask)
    n_indices = tf.size(indices)
    # Get indices for the axis
    idx = x.indices[:, axis]
    # Find where indices match the selection
    eq = tf.equal(tf.expand_dims(idx, 1),
                  tf.cast(indices, tf.int64))  # TODO this has quadratic cost
    # Mask for selected values
    sel = tf.reduce_any(eq, axis=1)
    # Selected values
    values_new = tf.boolean_mask(x.values, sel, axis=0)
    # New index value for selected elements
    n_indices = tf.cast(n_indices, tf.int64)
    idx_new = tf.reduce_sum(tf.cast(eq, tf.int64) * tf.range(n_indices),
                            axis=1)
    idx_new = tf.boolean_mask(idx_new, sel, axis=0)
    # New full indices tensor
    indices_new = tf.boolean_mask(x.indices, sel, axis=0)
    indices_new = tf.concat([
        indices_new[:, :axis],
        tf.expand_dims(idx_new, 1), indices_new[:, axis + 1:]
    ],
                            axis=1)
    # New shape
    shape_new = tf.concat(
        [x.dense_shape[:axis], [n_indices], x.dense_shape[axis + 1:]], axis=0)
    return tf.SparseTensor(indices_new, values_new, shape_new)
Esempio n. 12
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 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
Esempio n. 14
0
def _upsampled_registration(target_image, src_image, upsample_factor):

    upsample_factor = tf.constant(upsample_factor, tf.float32)

    target_shape = tf.shape(target_image)
    target_image = tf.reshape(target_image, target_shape[:3])
    src_shape = tf.shape(src_image)
    src_image = tf.reshape(src_image, src_shape[:3])

    src_freq = fft2d(src_image)
    target_freq = fft2d(target_image)

    shape = tf.reshape(tf.shape(src_freq)[1:3], (1, 2))
    shape = tf.cast(shape, tf.float32)
    shape = tf.tile(shape, (tf.shape(target_freq)[0], 1))
    image_product = src_freq * tf.conj(target_freq)
    cross_correlation = tf.spectral.ifft2d(image_product)

    maxima = find_maxima(tf.abs(cross_correlation))
    midpoints = fix(tf.cast(shape, tf.float32) / 2.)

    shifts = maxima
    shifts = tf.where(shifts > midpoints, shifts - shape, shifts)
    shifts = tf.round(shifts * upsample_factor) / upsample_factor

    upsampled_region_size = tf.ceil(upsample_factor * 1.5)
    dftshift = fix(upsampled_region_size / 2.0)
    normalization = tf.cast(tf.size(src_freq[0]), tf.float32)
    normalization *= upsample_factor**2
    sample_region_offset = dftshift - shifts * upsample_factor

    data = tf.conj(image_product)
    upsampled_dft = _upsampled_dft(data, upsampled_region_size,
                                   upsample_factor, sample_region_offset)

    cross_correlation = tf.conj(upsampled_dft)
    cross_correlation /= tf.cast(normalization, tf.complex64)
    cross_correlation = tf.abs(cross_correlation)

    maxima = find_maxima(cross_correlation)
    maxima = maxima - dftshift
    shifts = shifts + maxima / upsample_factor

    return shifts
Esempio n. 15
0
File: gan.py Progetto: arosset42/gan
        def points_for_dragan():
            alphas = ktf.random_uniform((batch_size, 1), minval=0, maxval=1)
            alphas = [ktf.reshape(alphas, (-1, ) + (1, ) * (rank - 1)) for rank in ranks]
            alphas = cast_all(alphas, self.discriminator_input)
            fake = [ktf.random_uniform(ktf.shape(t), minval=0, maxval=1) * std_if_not_int(t) * 0.5
                       for t in self.discriminator_input]
            fake = cast_all(fake, self.discriminator_input)

            points = [(w * r) + ((1 - w) * f) for r, f, w in zip(self.discriminator_input, fake, alphas)]
            return points
Esempio n. 16
0
def radon_fft(x):
    x_shape = tf.shape(x)
    n_angles = x_shape[1]
    n_cols = x_shape[2]
    x = tf.reshape(x, (-1, n_cols))
    x = tf.cast(x, tf.complex64)
    x = tf.spectral.fft(x)
    x = tf.abs(x)
    x = tf.reshape(x, (-1, n_angles, n_cols, 1))
    return x
Esempio n. 17
0
def upsampling_from_mask(inputs):
    X_, A_, I_, M_ = inputs
    S_ = tf.eye(tf.shape(M_)[0])
    S_ = tf.boolean_mask(S_, M_)
    S_t_ = tf.transpose(S_)

    X_out_ = K.dot(S_t_, X_)
    A_out_ = K.dot(K.transpose(K.dot(A_, S_)), S_)
    I_out_ = K.dot(S_t_, K.cast(I_[:, None], tf.float32))[:, 0]
    I_out_ = K.cast(I_out_, tf.int32)
    return [X_out_, A_out_, I_out_]
Esempio n. 18
0
def resize_images(x, height_factor, width_factor, interpolation, data_format):
    """Resizes the images contained in a 4D tensor.
    # Arguments
        x: Tensor or variable to resize.
        height_factor: Positive integer.
        width_factor: Positive integer.
        interpolation: string, "nearest", "bilinear" or "bicubic"
        data_format: string, `"channels_last"` or `"channels_first"`.
    # Returns
        A tensor.
    # Raises
        ValueError: if `data_format` is neither `"channels_last"` or `"channels_first"`.
    """
    if interpolation == 'nearest':
        tf_resize = tf.image.resize_nearest_neighbor
    elif interpolation == 'bilinear':
        tf_resize = tf.image.resize_bilinear
    elif interpolation == 'bicubic':
        tf_resize = tf.image.resize_bicubic
    else:
        raise ValueError('Invalid interpolation method:', interpolation)
    if data_format == 'channels_first':
        original_shape = int_shape(x)
        new_shape = tf.shape(x)[2:]
        new_shape *= tf.constant(np.array([height_factor, width_factor]).astype('int32'))
        x = permute_dimensions(x, [0, 2, 3, 1])
        x = tf_resize(x, new_shape, align_corners=True)
        x = permute_dimensions(x, [0, 3, 1, 2])
        x.set_shape((None, None, original_shape[2] * height_factor if original_shape[2] is not None else None,
                     original_shape[3] * width_factor if original_shape[3] is not None else None))
        return x
    elif data_format == 'channels_last':
        original_shape = int_shape(x)
        new_shape = tf.shape(x)[1:3]
        new_shape *= tf.constant(np.array([height_factor, width_factor]).astype('int32'))
        x = tf_resize(x, new_shape, align_corners=True)
        x.set_shape((None, original_shape[1] * height_factor if original_shape[1] is not None else None,
                     original_shape[2] * width_factor if original_shape[2] is not None else None, None))
        return x
    else:
        raise ValueError('Invalid data_format:', data_format)
Esempio n. 19
0
    def call(self, inputs):
        if len(inputs) == 3:
            X, A, I = inputs
            self.data_mode = 'graph'
        else:
            X, A = inputs
            I = tf.zeros(tf.shape(X)[:1], dtype=tf.int32)
            self.data_mode = 'single'
        if K.ndim(I) == 2:
            I = I[:, 0]

        A_is_sparse = K.is_sparse(A)

        # Get mask
        y = K.dot(X, K.l2_normalize(self.kernel))
        N = K.shape(X)[-2]
        indices = ops.top_k(y[:, 0], I, self.ratio, self.top_k_var)
        mask = tf.scatter_nd(tf.expand_dims(indices, 1), tf.ones_like(indices),
                             (N, ))

        # Multiply X and y to make layer differentiable
        features = X * self.gating_op(y)

        axis = 0 if len(K.int_shape(
            A)) == 2 else 1  # Cannot use negative axis in tf.boolean_mask
        # Reduce X
        X_pooled = tf.boolean_mask(features, mask, axis=axis)

        # Compute A^2
        if A_is_sparse:
            A_dense = tf.sparse.to_dense(A)
        else:
            A_dense = A
        A_squared = K.dot(A, A_dense)

        # Reduce A
        A_pooled = tf.boolean_mask(A_squared, mask, axis=axis)
        A_pooled = tf.boolean_mask(A_pooled, mask, axis=axis + 1)
        if A_is_sparse:
            A_pooled = tf.contrib.layers.dense_to_sparse(A_pooled)

        output = [X_pooled, A_pooled]

        # Reduce I
        if self.data_mode == 'graph':
            I_pooled = tf.boolean_mask(I[:, None], mask)[:, 0]
            output.append(I_pooled)

        if self.return_mask:
            output.append(mask)

        return output
Esempio n. 20
0
    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
Esempio n. 21
0
    def call(self, x):
        x_shape = x.get_shape()
        offsets = super(Pool2DOffset, self).call(x)
        offsets = tf.transpose(offsets, [0, 3, 1, 2])
        offsets = tf.reshape(offsets,
                             (-1, int(x_shape[1]), int(x_shape[2]), 2))
        n_batches = tf.shape(offsets)[0]
        x = tf.transpose(x, [0, 3, 1, 2])
        x = tf.reshape(x, (-1, int(x_shape[1]), int(x_shape[2]), 1))

        #offsets = tf.resampler(x, offsets)
        x = batch_map_offsets(x, offsets)
        x = tf.reshape(x,
                       (-1, int(x_shape[3]), int(x_shape[1]), int(x_shape[2])))
        x = tf.transpose(x, [0, 2, 3, 1])
        return x
Esempio n. 22
0
def _upsampled_dft(data, upsampled_region_size, upsample_factor, axis_offsets):
    """
    Upsampled DFT by matrix multiplication.
    This code is intended to provide the same result as if the following
    operations were performed:
        - Embed the array "data" in an array that is ``upsample_factor`` times
          larger in each dimension.  ifftshift to bring the center of the
          image to (1,1).
        - Take the FFT of the larger array.
        - Extract an ``[upsampled_region_size]`` region of the result, starting
          with the ``[axis_offsets+1]`` element.
    It achieves this result by computing the DFT in the output array without
    the need to zeropad. Much faster and memory efficient than the zero-padded
    FFT approach if ``upsampled_region_size`` is much smaller than
    ``data.size * upsample_factor``.
    Parameters
    ----------
    data : 2D ndarray
        The input data array (DFT of original data) to upsample.
    upsampled_region_size : integer or tuple of integers, optional
        The size of the region to be sampled.  If one integer is provided, it
        is duplicated up to the dimensionality of ``data``.
    upsample_factor : integer, optional
        The upsampling factor.  Defaults to 1.
    axis_offsets : tuple of integers, optional
        The offsets of the region to be sampled.  Defaults to None (uses
        image center)
    Returns
    -------
    output : 2D ndarray
            The upsampled DFT of the specified region.
    """
    data_shape = tf.shape(data)

    col_kernel = _col_kernel(upsampled_region_size, upsample_factor,
                             axis_offsets, data_shape)
    row_kernel = _row_kernel(upsampled_region_size, upsample_factor,
                             axis_offsets, data_shape)

    upsampled_dft = tf.matmul(tf.matmul(row_kernel, data), col_kernel)

    return upsampled_dft
Esempio n. 23
0
    def call(self, x):
        x_shape = x.get_shape()
        offsets = super(Conv2DOffset, self).call(x)
        #offsets *= 10

        channels = int(offsets.get_shape()[3].value)
        n_batches = tf.shape(offsets)[0]

        # Change offset's order from [x1, x2, ..., y1, y2, ...] to [x1, y1, x2, y2, ...]
        # Codes below are written to make sure same results of MXNet implementation.
        # You can remove them, and it won't influence the module's performance.
        ind_shuffle = tf.concat(
            [tf.range(0, channels, 2),
             tf.range(1, channels + 1, 2)], axis=0)

        #ind_shuffle = tf.expand_dims(ind_shuffle, axis=0)
        #ind_shuffle = tf.expand_dims(ind_shuffle, axis=0)
        #ind_shuffle = tf.tile(ind_shuffle, [input_w, input_h, 1])

        offsets = tf.gather(offsets, ind_shuffle, axis=3)
        # ------------------------------------------------------------------------
        #x = tf.transpose(x, [0, 3, 1, 2])
        #x = tf.reshape(x, (-1, int(x_shape[1]), int(x_shape[2])))
        #offsets = tf.resampler(x, offsets)
        offsets = batch_map_offsets(x, offsets)
        #offsets = tf.reshape(x, (-1, int(x_shape[3]), int(x_shape[1]), int(x_shape[2])))
        #offsets = tf.transpose(x, [0, 2, 3, 1])
        offset_shape = offsets.get_shape()
        num_channels = offset_shape[1].value
        height = offset_shape[2].value
        width = offset_shape[3].value
        f_offset = [
            tf.reshape(offsets[..., ind:ind + 3],
                       (-1, num_channels, height, width * 3))
            for ind in range(0, 9, 3)
        ]
        f_offset = tf.concat(f_offset, axis=-1)
        f_offset = tf.reshape(f_offset,
                              (-1, num_channels, height * 3, width * 3))
        f_offset = tf.transpose(f_offset, (0, 2, 3, 1))
        return f_offset
Esempio n. 24
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
Esempio n. 26
0
def create_sparse(labels):
    indices = tf.where(tf.not_equal(labels, 0))
    values = tf.gather_nd(labels, indices)
    shape = tf.shape(labels, out_type=tf.int64)
    return tf.SparseTensor(indices, values, dense_shape=shape)
Esempio n. 27
0
def bernoulliSample_ST(op, grad):
    return [grad, tf.zeros(tf.shape(op.inputs[1]))]
Esempio n. 28
0
def ndims(x):
    return tf.size(tf.shape(x))