You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
2.7 KiB

6 years ago
  1. # Copyright 2017 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Bipartite matcher implementation."""
  16. import tensorflow as tf
  17. from tensorflow.contrib.image.python.ops import image_ops
  18. from object_detection.core import matcher
  19. class GreedyBipartiteMatcher(matcher.Matcher):
  20. """Wraps a Tensorflow greedy bipartite matcher."""
  21. def __init__(self, use_matmul_gather=False):
  22. """Constructs a Matcher.
  23. Args:
  24. use_matmul_gather: Force constructed match objects to use matrix
  25. multiplication based gather instead of standard tf.gather.
  26. (Default: False).
  27. """
  28. super(GreedyBipartiteMatcher, self).__init__(
  29. use_matmul_gather=use_matmul_gather)
  30. def _match(self, similarity_matrix, valid_rows):
  31. """Bipartite matches a collection rows and columns. A greedy bi-partite.
  32. TODO(rathodv): Add num_valid_columns options to match only that many columns
  33. with all the rows.
  34. Args:
  35. similarity_matrix: Float tensor of shape [N, M] with pairwise similarity
  36. where higher values mean more similar.
  37. valid_rows: A boolean tensor of shape [N] indicating the rows that are
  38. valid.
  39. Returns:
  40. match_results: int32 tensor of shape [M] with match_results[i]=-1
  41. meaning that column i is not matched and otherwise that it is matched to
  42. row match_results[i].
  43. """
  44. valid_row_sim_matrix = tf.gather(similarity_matrix,
  45. tf.squeeze(tf.where(valid_rows), axis=-1))
  46. invalid_row_sim_matrix = tf.gather(
  47. similarity_matrix,
  48. tf.squeeze(tf.where(tf.logical_not(valid_rows)), axis=-1))
  49. similarity_matrix = tf.concat(
  50. [valid_row_sim_matrix, invalid_row_sim_matrix], axis=0)
  51. # Convert similarity matrix to distance matrix as tf.image.bipartite tries
  52. # to find minimum distance matches.
  53. distance_matrix = -1 * similarity_matrix
  54. num_valid_rows = tf.reduce_sum(tf.to_float(valid_rows))
  55. _, match_results = image_ops.bipartite_match(
  56. distance_matrix, num_valid_rows=num_valid_rows)
  57. match_results = tf.reshape(match_results, [-1])
  58. match_results = tf.cast(match_results, tf.int32)
  59. return match_results