Repository: tensorflow/models
File: research/delf/delf/python/whiten.py (Python)

Code before:
# Copyright 2021 The TensorFlow Authors All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Whitening learning functions."""

import os

import numpy as np


def whitenapply(X, mean_descriptor_vector, projection, dimensions=None):
  """Applies the whitening to the descriptors as a post-processing step.

  Args:
    X: List of L2-normalized descriptors to be post-processed.
    mean_descriptor_vector: Mean descriptor vector.
    projection: Whitening projection matrix.
    dimensions: Integer, parameter for the dimensionality reduction.

  Returns:
    X_whitened: List of L2-normalized descriptors `X` after whitening
      application.
  """
  eps = 1e-6
  # If `dimensions` is not specified, we do not perform dimensionality
  # reduction.
  if not dimensions:
    dimensions = projection.shape[0]

  X = np.dot(projection[:dimensions, :], X - mean_descriptor_vector)
  X_whitened = X / (np.linalg.norm(X, ord=2, axis=0, keepdims=True) + eps)

  return X_whitened



Review comment (refactor):
the whitenapply function seems nearly identical to this existing one: https://github.com/tensorflow/models/blob/master/research/delf/delf/python/feature_extractor.py#L80

Would it be possible to reuse the existing one?

Code after:
# Copyright 2021 The TensorFlow Authors All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Whitening learning functions."""

import os

import numpy as np


def apply_whitening(descriptors, mean_descriptor_vector, projection,
                    output_dim=None):
  """Applies the whitening to the descriptors as a post-processing step.

  Args:
    descriptors: [N, D] NumPy array of L2-normalized descriptors to be
      post-processed.
    mean_descriptor_vector: Mean descriptor vector.
    projection: Whitening projection matrix.
    output_dim: Integer, parameter for the dimensionality reduction. If
      `output_dim` is None, the dimensionality reduction is not performed.

  Returns:
    descriptors_whitened: [N, output_dim] NumPy array of L2-normalized
      descriptors `descriptors` after whitening application.
  """
  eps = 1e-6
  if output_dim is None:
    output_dim = projection.shape[0]

  descriptors = np.dot(projection[:output_dim, :],
                       descriptors - mean_descriptor_vector)
  descriptors_whitened = descriptors / (
            np.linalg.norm(descriptors, ord=2, axis=0, keepdims=True) + eps)
  return descriptors_whitened