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 _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 repeat_theta(theta, n_angles, n_frames): repeated = tf.reshape(theta, (1, n_angles)) repeated = tf.tile(repeated, (n_frames, 1)) repeated = tf.reshape(repeated, (n_frames * n_angles, )) return repeated
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
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 batch_map(cords): y = ((cords[..., 0] + 1.0) / 2.0) * self.image_size[0] x = ((cords[..., 1] + 1.0) / 2.0) * self.image_size[1] y = ktf.reshape(y, (1, 1, -1)) x = ktf.reshape(x, (1, 1, -1)) return ktf.exp(-((self.yy - y)**2 + (self.xx - x)**2) / (2 * self.sigma**2))
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 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
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 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 check_angles(x, rotation_guess): x = tf.reshape(x, (-1, 1)) x = angle_mod(x) rA = radians(x) rA = tf.concat([tf.cos(rA), tf.sin(rA)], axis=-1) rI = tf.reshape(rotation_guess, (-1, 1)) rI = radians(rI) rI = tf.concat([tf.cos(rI), tf.sin(rI)], axis=-1) guess_test = tf.matmul(rA, rI, transpose_b=True) x = tf.where(guess_test < 0, angle_mod(x - 180), x) return x
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 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
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 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
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 rotate_images(x, angles, interpolation='bilinear', data_format=None): """Rotates images in a 4D tensor. # Arguments x: Tensor or variable. 4D tensor containing images angles: Tensor or variable 2D tensor containing rotation angles in radians data_format: string, `"channels_last"` or `"channels_first"`. # Returns A tensor. # Raises ValueError: if `data_format` is neither `"channels_last"` or `"channels_first"`. """ angles = tf.reshape(angles, (-1, )) if interpolation.lower() in 'nearest': interpolation = 'NEAREST' elif interpolation.lower() in 'bilinear': interpolation = 'BILINEAR' else: raise ValueError('Invalid interpolation method:', interpolation) if data_format == 'channels_first': x = permute_dimensions(x, [0, 2, 3, 1]) x = tf.contrib.image.rotate(x, angles, interpolation=interpolation) x = permute_dimensions(x, [0, 3, 1, 2]) return x elif data_format == 'channels_last': x = tf.contrib.image.rotate(x, angles, interpolation=interpolation) return x else: raise ValueError('Invalid data_format:', data_format)
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
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 _tile_images(self, images): num_images = self.batch_size / self.gpus channels = ktf.split(images, images.shape[3], axis=3) del images counter = 0 for channel in channels: tiles = ktf.extract_image_patches(channel, ksizes=[1, 512, 512, 1], strides=[1, 512, 512, 1], rates=[1, 1, 1, 1], padding="VALID") num_tiles = tiles.shape[1] * tiles.shape[2] tiles = ktf.reshape(tiles, [num_tiles * num_images, 1, 1, tiles.shape[3]]) tiles = ktf.reshape(tiles, [num_tiles * num_images, 512, 512, 1]) channels[counter] = tiles counter += 1 return ktf.concat(channels, 3)
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
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
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 _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 tf_repeat_1d(x, repeats): """ Repeats each value `x[i]` a number of times `repeats[i]`. :param x: a rank 1 tensor; :param repeats: a rank 1 tensor; :return: a rank 1 tensor, of shape `(sum(repeats), )`. """ x = tf.expand_dims(x, 1) max_repeats = tf.reduce_max(repeats) tile_repeats = [1, max_repeats] arr_tiled = tf.tile(x, tile_repeats) mask = tf.less(tf.range(max_repeats), tf.expand_dims(repeats, 1)) result = tf.reshape(tf.boolean_mask(arr_tiled, mask), [-1]) return result
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 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 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