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.

visualize.py 6.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. =============================================================
  13. """
  14. from .base import Callback
  15. from typing import Callable, Union, Sequence
  16. import weakref
  17. import random
  18. from kamal.utils import move_to_device, set_mode, split_batch, colormap
  19. from kamal.core.attach import AttachTo
  20. import torch
  21. import numpy as np
  22. import matplotlib.pyplot as plt
  23. import matplotlib
  24. matplotlib.use('agg')
  25. import math
  26. import numbers
  27. class VisualizeOutputs(Callback):
  28. def __init__(self,
  29. model,
  30. dataset: torch.utils.data.Dataset,
  31. idx_list_or_num_vis: Union[int, Sequence]=5,
  32. normalizer: Callable=None,
  33. prepare_fn: Callable=None,
  34. decode_fn: Callable=None, # decode targets and preds
  35. tag: str='viz'):
  36. super(VisualizeOutputs, self).__init__()
  37. self._dataset = dataset
  38. self._model = weakref.ref(model)
  39. if isinstance(idx_list_or_num_vis, int):
  40. self.idx_list = self._get_vis_idx_list(self._dataset, idx_list_or_num_vis)
  41. elif isinstance(idx_list_or_num_vis, Sequence):
  42. self.idx_list = idx_list_or_num_vis
  43. self._normalizer = normalizer
  44. self._decode_fn = decode_fn
  45. if prepare_fn is None:
  46. prepare_fn = VisualizeOutputs.get_prepare_fn()
  47. self._prepare_fn = prepare_fn
  48. self._tag = tag
  49. def _get_vis_idx_list(self, dataset, num_vis):
  50. return random.sample(list(range(len(dataset))), num_vis)
  51. @torch.no_grad()
  52. def __call__(self, trainer):
  53. if trainer.tb_writer is None:
  54. trainer.logger.warning("summary writer was not found in trainer")
  55. return
  56. device = trainer.device
  57. model = self._model()
  58. with torch.no_grad(), set_mode(model, training=False):
  59. for img_id, idx in enumerate(self.idx_list):
  60. batch = move_to_device(self._dataset[idx], device)
  61. batch = [ d.unsqueeze(0) for d in batch ]
  62. inputs, targets, preds = self._prepare_fn(model, batch)
  63. if self._normalizer is not None:
  64. inputs = self._normalizer(inputs)
  65. inputs = inputs.detach().cpu().numpy()
  66. preds = preds.detach().cpu().numpy()
  67. targets = targets.detach().cpu().numpy()
  68. if self._decode_fn: # to RGB 0~1 NCHW
  69. preds = self._decode_fn(preds)
  70. targets = self._decode_fn(targets)
  71. inputs = inputs[0]
  72. preds = preds[0]
  73. targets = targets[0]
  74. trainer.tb_writer.add_images("%s-%d"%(self._tag, img_id), np.stack( [inputs, targets, preds], axis=0), global_step=trainer.state.iter)
  75. @staticmethod
  76. def get_prepare_fn(attach_to=None, pred_fn=lambda x: x):
  77. attach_to = AttachTo(attach_to)
  78. def wrapper(model, batch):
  79. inputs, targets = split_batch(batch)
  80. outputs = model(inputs)
  81. outputs, targets = attach_to(outputs, targets)
  82. return inputs, targets, pred_fn(outputs)
  83. return wrapper
  84. @staticmethod
  85. def get_seg_decode_fn(cmap=colormap(), index_transform=lambda x: x+1): # 255->0, 0->1,
  86. def wrapper(preds):
  87. if len(preds.shape)>3:
  88. preds = preds.squeeze(1)
  89. out = cmap[ index_transform(preds.astype('uint8')) ]
  90. out = out.transpose(0, 3, 1, 2) / 255
  91. return out
  92. return wrapper
  93. @staticmethod
  94. def get_depth_decode_fn(max_depth, log_scale=True, cmap=plt.get_cmap('jet')):
  95. def wrapper(preds):
  96. if log_scale:
  97. _max_depth = np.log( max_depth )
  98. preds = np.log( preds )
  99. else:
  100. _max_depth = max_depth
  101. if len(preds.shape)>3:
  102. preds = preds.squeeze(1)
  103. out = (cmap(preds.clip(0, _max_depth)/_max_depth)).transpose(0, 3, 1, 2)[:, :3]
  104. return out
  105. return wrapper
  106. class VisualizeSegmentation(VisualizeOutputs):
  107. def __init__(
  108. self, model, dataset: torch.utils.data.Dataset, idx_list_or_num_vis: Union[int, Sequence]=5,
  109. cmap = colormap(),
  110. attach_to=None,
  111. normalizer: Callable=None,
  112. prepare_fn: Callable=None,
  113. decode_fn: Callable=None,
  114. tag: str='seg'
  115. ):
  116. if prepare_fn is None:
  117. prepare_fn = VisualizeOutputs.get_prepare_fn(attach_to=attach_to, pred_fn=lambda x: x.max(1)[1])
  118. if decode_fn is None:
  119. decode_fn = VisualizeOutputs.get_seg_decode_fn(cmap=cmap, index_transform=lambda x: x+1)
  120. super(VisualizeSegmentation, self).__init__(
  121. model=model, dataset=dataset, idx_list_or_num_vis=idx_list_or_num_vis,
  122. normalizer=normalizer, prepare_fn=prepare_fn, decode_fn=decode_fn,
  123. tag=tag
  124. )
  125. class VisualizeDepth(VisualizeOutputs):
  126. def __init__(
  127. self, model, dataset: torch.utils.data.Dataset,
  128. idx_list_or_num_vis: Union[int, Sequence]=5,
  129. max_depth = 10,
  130. log_scale = True,
  131. attach_to = None,
  132. normalizer: Callable=None,
  133. prepare_fn: Callable=None,
  134. decode_fn: Callable=None,
  135. tag: str='depth'
  136. ):
  137. if prepare_fn is None:
  138. prepare_fn = VisualizeOutputs.get_prepare_fn(attach_to=attach_to, pred_fn=lambda x: x)
  139. if decode_fn is None:
  140. decode_fn = VisualizeOutputs.get_depth_decode_fn(max_depth=max_depth, log_scale=log_scale)
  141. super(VisualizeDepth, self).__init__(
  142. model=model, dataset=dataset, idx_list_or_num_vis=idx_list_or_num_vis,
  143. normalizer=normalizer, prepare_fn=prepare_fn, decode_fn=decode_fn,
  144. tag=tag
  145. )

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)