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.

86 lines
2.1 KiB

  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. """Helper functions to access TensorShape values.
  16. The rank 4 tensor_shape must be of the form [batch_size, height, width, depth].
  17. """
  18. def get_dim_as_int(dim):
  19. """Utility to get v1 or v2 TensorShape dim as an int.
  20. Args:
  21. dim: The TensorShape dimension to get as an int
  22. Returns:
  23. None or an int.
  24. """
  25. try:
  26. return dim.value
  27. except AttributeError:
  28. return dim
  29. def get_batch_size(tensor_shape):
  30. """Returns batch size from the tensor shape.
  31. Args:
  32. tensor_shape: A rank 4 TensorShape.
  33. Returns:
  34. An integer representing the batch size of the tensor.
  35. """
  36. tensor_shape.assert_has_rank(rank=4)
  37. return get_dim_as_int(tensor_shape[0])
  38. def get_height(tensor_shape):
  39. """Returns height from the tensor shape.
  40. Args:
  41. tensor_shape: A rank 4 TensorShape.
  42. Returns:
  43. An integer representing the height of the tensor.
  44. """
  45. tensor_shape.assert_has_rank(rank=4)
  46. return get_dim_as_int(tensor_shape[1])
  47. def get_width(tensor_shape):
  48. """Returns width from the tensor shape.
  49. Args:
  50. tensor_shape: A rank 4 TensorShape.
  51. Returns:
  52. An integer representing the width of the tensor.
  53. """
  54. tensor_shape.assert_has_rank(rank=4)
  55. return get_dim_as_int(tensor_shape[2])
  56. def get_depth(tensor_shape):
  57. """Returns depth from the tensor shape.
  58. Args:
  59. tensor_shape: A rank 4 TensorShape.
  60. Returns:
  61. An integer representing the depth of the tensor.
  62. """
  63. tensor_shape.assert_has_rank(rank=4)
  64. return get_dim_as_int(tensor_shape[3])