general_utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. """
  2. General Utility functions
  3. """
  4. import os
  5. import tensorflow as tf
  6. from omegaconf import DictConfig
  7. from .images_utils import image_to_mask_name
  8. def create_directory(path):
  9. """
  10. Create Directory if it already does not exist.
  11. """
  12. if not os.path.exists(path):
  13. os.makedirs(path)
  14. def join_paths(*paths):
  15. """
  16. Concatenate multiple paths.
  17. """
  18. return os.path.normpath(os.path.sep.join(path.rstrip(r"\/") for path in paths))
  19. def set_gpus(gpu_ids):
  20. """
  21. Change number of visible gpus for tensorflow.
  22. gpu_ids: Could be integer or list of integers.
  23. In case Integer: if integer value is -1 then use all available gpus.
  24. otherwise if positive number, then use given number of gpus.
  25. In case list of Integer: each integer will be considered as gpu id
  26. """
  27. all_gpus = tf.config.experimental.list_physical_devices('GPU')
  28. all_gpus_length = len(all_gpus)
  29. if isinstance(gpu_ids, int):
  30. if gpu_ids == -1:
  31. gpu_ids = range(all_gpus_length)
  32. else:
  33. gpu_ids = min(gpu_ids, all_gpus_length)
  34. gpu_ids = range(gpu_ids)
  35. selected_gpus = [all_gpus[gpu_id] for gpu_id in gpu_ids if gpu_id < all_gpus_length]
  36. try:
  37. tf.config.experimental.set_visible_devices(selected_gpus, 'GPU')
  38. except RuntimeError as e:
  39. # Visible devices must be set at program startup
  40. print(e)
  41. def get_gpus_count():
  42. """
  43. Return length of available gpus.
  44. """
  45. return len(tf.config.experimental.list_logical_devices('GPU'))
  46. def get_data_paths(cfg: DictConfig, mode: str, mask_available: bool):
  47. """
  48. Return list of absolute images/mask paths.
  49. There are two options you can either pass directory path or list.
  50. In case of directory, it should contain relative path of images/mask
  51. folder from project root path.
  52. In case of list of images, every element should contain absolute path
  53. for each image and mask.
  54. For prediction, you can set mask path to None if mask are not
  55. available for visualization.
  56. """
  57. # read images from directory
  58. if isinstance(cfg.DATASET[mode].IMAGES_PATH, str):
  59. # has only images name not full path
  60. images_paths = os.listdir(
  61. join_paths(
  62. cfg.WORK_DIR,
  63. cfg.DATASET[mode].IMAGES_PATH
  64. )
  65. )
  66. if mask_available:
  67. mask_paths = [
  68. image_to_mask_name(image_name) for image_name in images_paths
  69. ]
  70. # create full mask paths from folder
  71. mask_paths = [
  72. join_paths(
  73. cfg.WORK_DIR,
  74. cfg.DATASET[mode].MASK_PATH,
  75. mask_name
  76. ) for mask_name in mask_paths
  77. ]
  78. # create full images paths from folder
  79. images_paths = [
  80. join_paths(
  81. cfg.WORK_DIR,
  82. cfg.DATASET[mode].IMAGES_PATH,
  83. image_name
  84. ) for image_name in images_paths
  85. ]
  86. else:
  87. # read images and mask from absolute paths given in list
  88. images_paths = list(cfg.DATASET[mode].IMAGES_PATH)
  89. if mask_available:
  90. mask_paths = list(cfg.DATASET[mode].MASK_PATH)
  91. if mask_available:
  92. return images_paths, mask_paths
  93. else:
  94. return images_paths,
  95. def suppress_warnings():
  96. """
  97. Suppress TensorFlow warnings.
  98. """
  99. import logging
  100. logging.getLogger('tensorflow').setLevel(logging.ERROR)
  101. logging.getLogger('dali').setLevel(logging.ERROR)
  102. os.environ["KMP_AFFINITY"] = "noverbose"
  103. os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
  104. import tensorflow as tf
  105. tf.autograph.set_verbosity(3)