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.4 kB

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.processors.base_processor import BaseProcessor
  20. class ImageProcessor(BaseProcessor):
  21. """Image Processor."""
  22. def get_metadata_list(self, train_id, tag):
  23. """
  24. Builds a JSON-serializable object with information about images.
  25. Args:
  26. train_id (str): The ID of the events data.
  27. tag (str): The name of the tag the images all belong to.
  28. Returns:
  29. list[dict], a list of dictionaries containing the `wall_time`, `step`, `width`,
  30. and `height` for each image.
  31. [
  32. {
  33. "wall_time": ****,
  34. "step": ****,
  35. "width": ****,
  36. "height": ****,
  37. },
  38. {...}
  39. ]
  40. """
  41. Validation.check_param_empty(train_id=train_id, tag=tag)
  42. result = []
  43. tensors = self._data_manager.list_tensors(train_id, tag)
  44. for tensor in tensors:
  45. # no tensor_proto in TensorEvent
  46. (width, height) = (tensor.value.width, tensor.value.height)
  47. result.append({
  48. 'wall_time': tensor.wall_time,
  49. 'step': tensor.step,
  50. 'width': int(width),
  51. 'height': int(height),
  52. })
  53. return dict(metadatas=result)
  54. def get_single_image(self, train_id, tag, step):
  55. """
  56. Returns the actual image bytes for a given image.
  57. Args:
  58. train_id (str): The ID of the events data the image belongs to.
  59. tag (str): The name of the tag the images belongs to.
  60. step (int): The step of the image in the current reservoir.
  61. Returns:
  62. bytes, a byte string of the raw image bytes.
  63. """
  64. Validation.check_param_empty(train_id=train_id, tag=tag, step=step)
  65. step = to_int(step, "step")
  66. tensors = self._data_manager.list_tensors(train_id, tag)
  67. image = None
  68. for tensor in tensors:
  69. if tensor.step == step:
  70. # Default value for bytes field is empty byte string normally,
  71. # see also "Optional Fields And Default Values" in protobuf
  72. # documentation.
  73. image = tensor.value.encoded_image
  74. break
  75. if image is None:
  76. raise ParamValueError("Can not find the step with given train job id and tag.")
  77. return image

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

Contributors (1)