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 3.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.
  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 = None
  75. for tensor in tensors:
  76. if tensor.step == step:
  77. # Default value for bytes field is empty byte string normally,
  78. # see also "Optional Fields And Default Values" in protobuf
  79. # documentation.
  80. image = tensor.value.encoded_image
  81. break
  82. if image is None:
  83. raise ImageNotExistError("Can not find the step with given train job id and tag.")
  84. return image

MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。