visualize.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  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. import os
  15. import os.path as osp
  16. from .cls_transforms import ClsTransform
  17. from .det_transforms import DetTransform
  18. from .seg_transforms import SegTransform
  19. def visualize(dataset, index=0, steps=3, save_dir='vdl_output'):
  20. '''对数据预处理/增强中间结果进行可视化。
  21. 可使用VisualDL查看中间结果:
  22. 1. VisualDL启动方式: visualdl --logdir vdl_output --port 8001
  23. 2. 浏览器打开 https://0.0.0.0:8001即可,
  24. 其中0.0.0.0为本机访问,如为远程服务, 改成相应机器IP
  25. Args:
  26. dataset (paddlex.datasets): 数据集读取器。
  27. index (int): 对数据集中的第index张图像进行可视化。默认为0
  28. steps (int): 数据预处理/增强的次数。默认为3。
  29. save_dir (str): 日志保存的路径。默认为'vdl_output'。
  30. '''
  31. transforms = dataset.transforms
  32. if not osp.isdir(save_dir):
  33. if osp.exists(save_dir):
  34. os.remove(save_dir)
  35. os.makedirs(save_dir)
  36. for i, data in enumerate(dataset.iterator()):
  37. if i == index:
  38. break
  39. from visualdl import LogWriter
  40. vdl_save_dir = osp.join(save_dir, 'image_transforms')
  41. vdl_writer = LogWriter(vdl_save_dir)
  42. data.append(vdl_writer)
  43. for s in range(steps):
  44. if s != 0:
  45. data.pop()
  46. data.append(s)
  47. transforms(*data)