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

images_processor.py 4.0 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  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. """Image Processor APIs."""
  16. from mindinsight.datavisual.utils.tools import to_int
  17. from mindinsight.utils.exceptions import ParamValueError
  18. from mindinsight.datavisual.common.validation import Validation
  19. from mindinsight.datavisual.common.exceptions import ImageNotExistError
  20. from mindinsight.datavisual.processors.base_processor import BaseProcessor
  21. class ImageProcessor(BaseProcessor):
  22. """Image Processor."""
  23. def get_metadata_list(self, train_id, tag):
  24. """
  25. Builds a JSON-serializable object with information about images.
  26. Args:
  27. train_id (str): The ID of the events data.
  28. tag (str): The name of the tag the images all belong to.
  29. Returns:
  30. list[dict], a list of dictionaries containing the `wall_time`, `step`, `width`,
  31. and `height` for each image.
  32. [
  33. {
  34. "wall_time": ****,
  35. "step": ****,
  36. "width": ****,
  37. "height": ****,
  38. },
  39. {...}
  40. ]
  41. """
  42. Validation.check_param_empty(train_id=train_id, tag=tag)
  43. result = []
  44. try:
  45. tensors = self._data_manager.list_tensors(train_id, tag)
  46. except ParamValueError as ex:
  47. raise ImageNotExistError(ex.message)
  48. for tensor in tensors:
  49. # no tensor_proto in TensorEvent
  50. (width, height) = (tensor.value.width, tensor.value.height)
  51. result.append({
  52. 'wall_time': tensor.wall_time,
  53. 'step': tensor.step,
  54. 'width': int(width),
  55. 'height': int(height),
  56. })
  57. return dict(metadatas=result)
  58. def get_single_image(self, train_id, tag, step):
  59. """
  60. Returns the actual image bytes for a given image.
  61. Args:
  62. train_id (str): The ID of the events data the image belongs to.
  63. tag (str): The name of the tag the images belongs to.
  64. step (int): The step of the image in the current reservoir. If step = -1, return image of final step.
  65. Returns:
  66. bytes, a byte string of the raw image bytes.
  67. """
  68. Validation.check_param_empty(train_id=train_id, tag=tag, step=step)
  69. step = to_int(step, "step")
  70. try:
  71. tensors = self._data_manager.list_tensors(train_id, tag)
  72. except ParamValueError as ex:
  73. raise ImageNotExistError(ex.message)
  74. image = _find_image(tensors, step)
  75. if image is None:
  76. raise ImageNotExistError("Can not find the step with given train job id and tag.")
  77. return image
  78. def _find_image(tensors, step):
  79. """Find the specific image by step from tensors. If step = -1, return image of final step."""
  80. if not tensors:
  81. return None
  82. if step == -1:
  83. return tensors[-1].value.encoded_image
  84. for tensor in tensors:
  85. if tensor.step == step:
  86. # Default value for bytes field is empty byte string normally,
  87. # see also "Optional Fields And Default Values" in protobuf
  88. # documentation.
  89. return tensor.value.encoded_image
  90. return None