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.

trans_metric.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. import abc
  15. from . import depara
  16. from captum.attr import InputXGradient
  17. import torch
  18. from kamal.core import hub
  19. from kamal.vision import sync_transforms as sT
  20. class TransMetric(abc.ABC):
  21. def __init__(self):
  22. pass
  23. def __call__(self, a, b) -> float:
  24. return 0
  25. class DeparaMetric(TransMetric):
  26. def __init__(self, data, device):
  27. self.data = data
  28. self.device = device
  29. self._cache = {}
  30. def _get_transform(self, metadata):
  31. input_metadata = metadata['input']
  32. size = input_metadata['size']
  33. space = input_metadata['space']
  34. drange = input_metadata['range']
  35. normalize = input_metadata['normalize']
  36. if size==None:
  37. size=224
  38. if isinstance(size, (list, tuple)):
  39. size = size[-1]
  40. transform = [
  41. sT.Resize(size),
  42. sT.CenterCrop(size),
  43. ]
  44. if space=='bgr':
  45. transform.append(sT.FlipChannels())
  46. if list(drange)==[0, 1]:
  47. transform.append( sT.ToTensor() )
  48. elif list(drange)==[0, 255]:
  49. transform.append( sT.ToTensor(normalize=False, dtype=torch.float) )
  50. else:
  51. raise NotImplementedError
  52. if normalize is not None:
  53. transform.append(sT.Normalize( mean=normalize['mean'], std=normalize['std'] ))
  54. return sT.Compose(transform)
  55. def _get_attr_graph(self, n):
  56. transform = self._get_transform(n.metadata)
  57. data = torch.stack( [ transform( d ) for d in self.data ], dim=0 )
  58. return depara.get_attribution_graph(
  59. n.model,
  60. attribution_type=InputXGradient,
  61. with_noise=False,
  62. probe_data=data,
  63. device=self.device
  64. )
  65. def __call__(self, n1, n2):
  66. attrgraph_1 = self._cache.get(n1, None)
  67. attrgraph_2 = self._cache.get(n2, None)
  68. if attrgraph_1 is None:
  69. self._cache[n1] = attrgraph_1 = self._get_attr_graph(n1).cpu()
  70. if attrgraph_2 is None:
  71. self._cache[n2] = attrgraph_2 = self._get_attr_graph(n2).cpu()
  72. result = depara.graph_similarity(attrgraph_1, attrgraph_2)
  73. self._cache[n1] = self._cache[n1]
  74. self._cache[n2] = self._cache[n2]
  75. class AttrMapMetric(DeparaMetric):
  76. def _get_attr_map(self, n):
  77. transform = self._get_transform(n.metadata)
  78. data = torch.stack( [ transform( d ).to(self.device) for d in self.data ], dim=0 )
  79. return depara.attribution_map(
  80. n.model.to(self.device),
  81. attribution_type=InputXGradient,
  82. with_noise=False,
  83. probe_data=data,
  84. )
  85. def __call__(self, n1, n2):
  86. attrgraph_1 = self._cache.get(n1, None)
  87. attrgraph_2 = self._cache.get(n2, None)
  88. if attrgraph_1 is None:
  89. self._cache[n1] = attrgraph_1 = self._get_attr_map(n1).cpu()
  90. if attrgraph_2 is None:
  91. self._cache[n2] = attrgraph_2 = self._get_attr_map(n2).cpu()
  92. result = depara.attr_map_distance(attrgraph_1, attrgraph_2)
  93. self._cache[n1] = self._cache[n1]
  94. self._cache[n2] = self._cache[n2]
  95. return result

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

Contributors (1)