def _col_kernel(upsampled_region_size, upsample_factor, axis_offsets, data_shape): data_shape_float = tf.cast(data_shape, tf.float32) col_constant = tf.cast(data_shape_float[2] * upsample_factor, tf.complex64) col_constant = (-1j * 2 * np.pi / col_constant) col_kernel_a = tf.range(0, data_shape_float[2], dtype=tf.float32) col_kernel_a = fftshift1d(col_kernel_a) col_kernel_a = tf.reshape(col_kernel_a, (-1, 1)) col_kernel_a -= tf.floor(data_shape_float[2] / 2.) col_kernel_a = tf.reshape(col_kernel_a, (1, -1)) col_kernel_a = tf.tile(col_kernel_a, (data_shape[0], 1)) col_kernel_b = tf.range(0, upsampled_region_size, dtype=tf.float32) col_kernel_b = tf.reshape(col_kernel_b, (1, -1)) col_kernel_b = tf.tile(col_kernel_b, (data_shape[0], 1)) col_kernel_b = tf.transpose(col_kernel_b) col_kernel_b -= tf.transpose(axis_offsets[:, 1]) col_kernel_b = tf.transpose(col_kernel_b) col_kernel_a = tf.expand_dims(col_kernel_a, 1) col_kernel_b = tf.expand_dims(col_kernel_b, -1) col_kernel = col_kernel_a * col_kernel_b col_kernel = tf.transpose(col_kernel, perm=(0, 2, 1)) col_kernel = col_constant * tf.cast(col_kernel, tf.complex64) col_kernel = tf.exp(col_kernel) return col_kernel
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)
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
def read_and_decode(filename, one_hot=True, n_class=None, is_train=None): """ Return tensor to read from TFRecord """ filename_queue = tf.train.string_input_producer([filename]) reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = tf.parse_single_example(serialized_example, features={ 'label': tf.FixedLenFeature([], tf.int64), 'image_raw': tf.FixedLenFeature([], tf.string), }) # You can do more image distortion here for training data img = tf.decode_raw(features['image_raw'], tf.uint8) img.set_shape([28 * 28]) img = tf.reshape(img, [28, 28, 1]) img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 # img = tf.cast(img, tf.float32) * (1. / 255) label = tf.cast(features['label'], tf.int32) if one_hot and n_class: label = tf.one_hot(label, n_class) return img, label
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
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 _row_kernel(upsampled_region_size, upsample_factor, axis_offsets, data_shape): data_shape_float = tf.cast(data_shape, tf.float32) row_constant = tf.cast(data_shape_float[1] * upsample_factor, tf.complex64) row_constant = (-1j * 2 * np.pi / row_constant) row_kernel_a = tf.range(0, upsampled_region_size, dtype=tf.float32) row_kernel_a = tf.reshape(row_kernel_a, (1, -1)) row_kernel_a = tf.tile(row_kernel_a, (data_shape[0], 1)) row_kernel_a = tf.transpose(row_kernel_a) row_kernel_a = row_kernel_a - axis_offsets[:, 0] row_kernel_b = tf.range(0, data_shape_float[1], dtype=tf.float32) row_kernel_b = fftshift1d(row_kernel_b) row_kernel_b = tf.reshape(row_kernel_b, (1, -1)) row_kernel_b = tf.tile(row_kernel_b, (data_shape[0], 1)) row_kernel_b = row_kernel_b - tf.floor(data_shape_float[1] / 2.) row_kernel_a = tf.expand_dims(row_kernel_a, 1) row_kernel_b = tf.expand_dims(row_kernel_b, -1) row_kernel = tf.transpose(row_kernel_a) * row_kernel_b row_kernel = tf.transpose(row_kernel, perm=(0, 2, 1)) row_kernel = row_constant * tf.cast(row_kernel, tf.complex64) row_kernel = tf.exp(row_kernel) return row_kernel
def find_maxima(x): col_max = tf.reduce_max(x, axis=1) row_max = tf.reduce_max(x, axis=2) cols = tf.cast(tf.argmax(col_max, 1), tf.float32) rows = tf.cast(tf.argmax(row_max, 1), tf.float32) cols = tf.reshape(cols, (-1, 1)) rows = tf.reshape(rows, (-1, 1)) maxima = tf.concat([rows, cols], -1) return maxima
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
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
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 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
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
def attention_mask_fun(input): """Align text representation with neural soft attention""" mask1 = K.reshape(input[0], (-1, max_sentence_length, 1)) mask2 = K.reshape(input[1], (-1, max_sentence_length, 1)) result = keras.layers.Dot(axes=-1)([mask1, mask2]) result = (1.0 - tf.cast(result, tf.float32)) * -100000.0 return result
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
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
def binary_focal_loss_fixed(y_true, y_pred): """ y_true shape need be (None,1) y_pred need be compute after sigmoid """ y_true = tf.cast(y_true, tf.float32) alpha_t = y_true * alpha + (K.ones_like(y_true) - y_true) * (1 - alpha) p_t = y_true * y_pred + (K.ones_like(y_true) - y_true) * ( K.ones_like(y_true) - y_pred) + K.epsilon() focal_loss = -alpha_t * K.pow( (K.ones_like(y_true) - p_t), gamma) * K.log(p_t) return K.mean(focal_loss)
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 _postprocess_conv2d_output(x, data_format): """Transpose and cast the output from conv2d if needed. # Arguments x: A tensor. data_format: string, `"channels_last"` or `"channels_first"`. # Returns A tensor. """ if data_format == 'channels_first': x = tf.transpose(x, (0, 3, 1, 2)) if floatx() == 'float64': x = tf.cast(x, 'float64') return x
def _preprocess_conv2d_input(x, data_format): """Transpose and cast the input before the conv2d. # Arguments x: input tensor. data_format: string, `"channels_last"` or `"channels_first"`. # Returns A tensor. """ if dtype(x) == 'float64': x = tf.cast(x, 'float32') if data_format == 'channels_first': # TF uses the last dimension as channel dimension, # instead of the 2nd one. # TH input shape: (samples, input_depth, rows, cols) # TF input shape: (samples, rows, cols, input_depth) x = tf.transpose(x, (0, 2, 3, 1)) return x
def read_and_decode(filename, w, h, one_hot=True, n_class=None, is_train=None, bResize=False, origImgW=0, origImgH=0): """ Return tensor to read from TFRecord """ # files = tf.train.match_filenames_once(filename) files = filename # print(files) filename_queue = tf.train.string_input_producer(files) reader = tf.TFRecordReader() _, serialized_example = reader.read(filename_queue) features = \ tf.parse_single_example(serialized_example, features={ 'height': tf.FixedLenFeature([], tf.int64), 'width': tf.FixedLenFeature([], tf.int64), 'depth': tf.FixedLenFeature([], tf.int64), 'image_raw': tf.FixedLenFeature([], tf.string), 'label': tf.FixedLenFeature([], tf.int64) }) # You can do more image distortion here for training data img = tf.decode_raw(features['image_raw'], tf.uint8) img = tf.reshape(img, [origImgW, origImgH, 3]) if bResize: img = tf.image.resize_images(img, (w, h), method=0) img = tf.cast(img, tf.float32) * (1. / 255) - 0.5 # img = tf.cast(img, tf.float32) * (1. / 255) label = features['label'] # label = tf.cast(label, tf.float32) if one_hot and n_class: label = tf.one_hot(label, n_class) return img, label
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
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
def train(): ff_apr = ktf.matmul(f, f, transpose_b=True) / ( ktf.cast(bs * w * h, ktf.float32) - 1.) if self.decomposition in ['pca-cor', 'zca-cor']: dinv = ktf.diag(ktf.sqrt(ktf.diag_part(ff_apr))) ff_apr = ktf.matmul(ktf.matmul(dinv, ff_apr), ktf.matrix_inverse(dinv), transpose_b=True) self.add_update([ K.moving_average_update(self.moving_mean, m, self.momentum), K.moving_average_update(self.moving_cov, ff_apr, self.momentum) ], inputs) ff_apr_shrinked = ( 1 - self.epsilon) * ff_apr + ktf.eye(c) * self.epsilon if self.renorm: l, l_inv = get_inv_sqrt(ff_apr_shrinked) ff_mov = (1 - self.epsilon ) * self.moving_cov + ktf.eye(c) * self.epsilon _, l_mov_inverse = get_inv_sqrt(ff_mov) l_ndiff = K.stop_gradient(l) return ktf.matmul(ktf.matmul(l_mov_inverse, l_ndiff), l_inv) return get_inv_sqrt(ff_apr_shrinked)[1]
def fft2d(x): x = tf.cast(x, tf.complex64) x = tf.spectral.fft2d(x) return x
def cast_all(values, reference_type_vals): return [ktf.cast(alpha, dtype=ref.dtype) for alpha, ref in zip(values, reference_type_vals)]
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
def build(self, input_shape): self.xx, self.yy = ktf.meshgrid(ktf.range(self.image_size[1]), ktf.range(self.image_size[0])) self.xx = ktf.expand_dims(ktf.cast(self.xx, 'float32'), 2) self.yy = ktf.expand_dims(ktf.cast(self.yy, 'float32'), 2)