def call(self, inputs): if self.data_format == 'channels_last': out = ktf.pad(inputs, [[0, 0]] + self.paddings + [[0, 0]], mode='REFLECT') else: out = ktf.pad(inputs, [[0, 0], [0, 0]] + self.paddings, mode='REFLECT') return out
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 nn_loss(self, reference, target, neighborhood_size=(3, 3)): v_pad = neighborhood_size[0] // 2 h_pad = neighborhood_size[1] // 2 val_pad = ktf.pad(reference, [[0, 0], [v_pad, v_pad], [h_pad, h_pad], [0, 0]], mode='CONSTANT', constant_values=-10000) reference_tensors = [] for i_begin in range(0, neighborhood_size[0]): i_end = i_begin - neighborhood_size[0] + 1 i_end = None if i_end == 0 else i_end for j_begin in range(0, neighborhood_size[1]): j_end = j_begin - neighborhood_size[0] + 1 j_end = None if j_end == 0 else j_end sub_tensor = val_pad[:, i_begin:i_end, j_begin:j_end, :] reference_tensors.append(ktf.expand_dims(sub_tensor, -1)) reference = ktf.concat(reference_tensors, axis=-1) target = ktf.expand_dims(target, axis=-1) abs = ktf.abs(reference - target) norms = ktf.reduce_sum(abs, reduction_indices=[-2]) loss = ktf.reduce_min(norms, reduction_indices=[-1]) return loss