def top_k_feature_map_locations(feature_map, max_pool_kernel_size=3, k=100, per_channel=False):
    'Returns the top k scores and their locations in a feature map.\n\n  Given a feature map, the top k values (based on activation) are returned. If\n  `per_channel` is True, the top k values **per channel** are returned.\n\n  The `max_pool_kernel_size` argument allows for selecting local peaks in a\n  region. This filtering is done per channel, so nothing prevents two values at\n  the same location to be returned.\n\n  Args:\n    feature_map: [batch, height, width, channels] float32 feature map.\n    max_pool_kernel_size: integer, the max pool kernel size to use to pull off\n      peak score locations in a neighborhood (independently for each channel).\n      For example, to make sure no two neighboring values (in the same channel)\n      are returned, set max_pool_kernel_size=3. If None or 1, will not apply max\n      pooling.\n    k: The number of highest scoring locations to return.\n    per_channel: If True, will return the top k scores and locations per\n      feature map channel. If False, the top k across the entire feature map\n      (height x width x channels) are returned.\n\n  Returns:\n    Tuple of\n    scores: A [batch, N] float32 tensor with scores from the feature map in\n      descending order. If per_channel is False, N = k. Otherwise,\n      N = k * channels, and the first k elements correspond to channel 0, the\n      second k correspond to channel 1, etc.\n    y_indices: A [batch, N] int tensor with y indices of the top k feature map\n      locations. If per_channel is False, N = k. Otherwise,\n      N = k * channels.\n    x_indices: A [batch, N] int tensor with x indices of the top k feature map\n      locations. If per_channel is False, N = k. Otherwise,\n      N = k * channels.\n    channel_indices: A [batch, N] int tensor with channel indices of the top k\n      feature map locations. If per_channel is False, N = k. Otherwise,\n      N = k * channels.\n  '
    if ((not max_pool_kernel_size) or (max_pool_kernel_size == 1)):
        feature_map_peaks = feature_map
    else:
        feature_map_max_pool = tf.nn.max_pool(feature_map, ksize=max_pool_kernel_size, strides=1, padding='SAME')
        feature_map_peak_mask = (tf.math.abs((feature_map - feature_map_max_pool)) < PEAK_EPSILON)
        feature_map_peaks = (feature_map * _to_float32(feature_map_peak_mask))
    (batch_size, _, width, num_channels) = _get_shape(feature_map, 4)
    if per_channel:
        feature_map_peaks_transposed = tf.transpose(feature_map_peaks, perm=[0, 3, 1, 2])
        feature_map_peaks_transposed = tf.reshape(feature_map_peaks_transposed, [batch_size, num_channels, (- 1)])
        (scores, peak_flat_indices) = tf.math.top_k(feature_map_peaks_transposed, k=k)
        channel_idx = tf.range(num_channels)[tf.newaxis, :, tf.newaxis]
        peak_flat_indices = ((num_channels * peak_flat_indices) + channel_idx)
        scores = tf.reshape(scores, [batch_size, (- 1)])
        peak_flat_indices = tf.reshape(peak_flat_indices, [batch_size, (- 1)])
    else:
        feature_map_peaks_flat = tf.reshape(feature_map_peaks, [batch_size, (- 1)])
        (scores, peak_flat_indices) = tf.math.top_k(feature_map_peaks_flat, k=k)
    (y_indices, x_indices, channel_indices) = row_col_channel_indices_from_flattened_indices(peak_flat_indices, width, num_channels)
    return (scores, y_indices, x_indices, channel_indices)
Returns the top k scores and their locations in a feature map.

Given a feature map, the top k values (based on activation) are returned. If
`per_channel` is True, the top k values **per channel** are returned.

The `max_pool_kernel_size` argument allows for selecting local peaks in a
region. This filtering is done per channel, so nothing prevents two values at
the same location to be returned.

Args:
  feature_map: [batch, height, width, channels] float32 feature map.
  max_pool_kernel_size: integer, the max pool kernel size to use to pull off
    peak score locations in a neighborhood (independently for each channel).
    For example, to make sure no two neighboring values (in the same channel)
    are returned, set max_pool_kernel_size=3. If None or 1, will not apply max
    pooling.
  k: The number of highest scoring locations to return.
  per_channel: If True, will return the top k scores and locations per
    feature map channel. If False, the top k across the entire feature map
    (height x width x channels) are returned.

Returns:
  Tuple of
  scores: A [batch, N] float32 tensor with scores from the feature map in
    descending order. If per_channel is False, N = k. Otherwise,
    N = k * channels, and the first k elements correspond to channel 0, the
    second k correspond to channel 1, etc.
  y_indices: A [batch, N] int tensor with y indices of the top k feature map
    locations. If per_channel is False, N = k. Otherwise,
    N = k * channels.
  x_indices: A [batch, N] int tensor with x indices of the top k feature map
    locations. If per_channel is False, N = k. Otherwise,
    N = k * channels.
  channel_indices: A [batch, N] int tensor with channel indices of the top k
    feature map locations. If per_channel is False, N = k. Otherwise,
    N = k * channels.
def top_k_feature_map_locations(feature_map, max_pool_kernel_size=3, k=100, per_channel=False):
    'Returns the top k scores and their locations in a feature map.\n\n  Given a feature map, the top k values (based on activation) are returned. If\n  `per_channel` is True, the top k values **per channel** are returned.\n\n  The `max_pool_kernel_size` argument allows for selecting local peaks in a\n  region. This filtering is done per channel, so nothing prevents two values at\n  the same location to be returned.\n\n  Args:\n    feature_map: [batch, height, width, channels] float32 feature map.\n    max_pool_kernel_size: integer, the max pool kernel size to use to pull off\n      peak score locations in a neighborhood (independently for each channel).\n      For example, to make sure no two neighboring values (in the same channel)\n      are returned, set max_pool_kernel_size=3. If None or 1, will not apply max\n      pooling.\n    k: The number of highest scoring locations to return.\n    per_channel: If True, will return the top k scores and locations per\n      feature map channel. If False, the top k across the entire feature map\n      (height x width x channels) are returned.\n\n  Returns:\n    Tuple of\n    scores: A [batch, N] float32 tensor with scores from the feature map in\n      descending order. If per_channel is False, N = k. Otherwise,\n      N = k * channels, and the first k elements correspond to channel 0, the\n      second k correspond to channel 1, etc.\n    y_indices: A [batch, N] int tensor with y indices of the top k feature map\n      locations. If per_channel is False, N = k. Otherwise,\n      N = k * channels.\n    x_indices: A [batch, N] int tensor with x indices of the top k feature map\n      locations. If per_channel is False, N = k. Otherwise,\n      N = k * channels.\n    channel_indices: A [batch, N] int tensor with channel indices of the top k\n      feature map locations. If per_channel is False, N = k. Otherwise,\n      N = k * channels.\n  '
    if ((not max_pool_kernel_size) or (max_pool_kernel_size == 1)):
        feature_map_peaks = feature_map
    else:
        feature_map_max_pool = tf.nn.max_pool(feature_map, ksize=max_pool_kernel_size, strides=1, padding='SAME')
        feature_map_peak_mask = (tf.math.abs((feature_map - feature_map_max_pool)) < PEAK_EPSILON)
        feature_map_peaks = (feature_map * _to_float32(feature_map_peak_mask))
    (batch_size, _, width, num_channels) = _get_shape(feature_map, 4)
    if per_channel:
        feature_map_peaks_transposed = tf.transpose(feature_map_peaks, perm=[0, 3, 1, 2])
        feature_map_peaks_transposed = tf.reshape(feature_map_peaks_transposed, [batch_size, num_channels, (- 1)])
        (scores, peak_flat_indices) = tf.math.top_k(feature_map_peaks_transposed, k=k)
        channel_idx = tf.range(num_channels)[tf.newaxis, :, tf.newaxis]
        peak_flat_indices = ((num_channels * peak_flat_indices) + channel_idx)
        scores = tf.reshape(scores, [batch_size, (- 1)])
        peak_flat_indices = tf.reshape(peak_flat_indices, [batch_size, (- 1)])
    else:
        feature_map_peaks_flat = tf.reshape(feature_map_peaks, [batch_size, (- 1)])
        (scores, peak_flat_indices) = tf.math.top_k(feature_map_peaks_flat, k=k)
    (y_indices, x_indices, channel_indices) = row_col_channel_indices_from_flattened_indices(peak_flat_indices, width, num_channels)
    return (scores, y_indices, x_indices, channel_indices)