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.

hint.py 3.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 .kd import KDDistiller
  15. from kamal.core.tasks.loss import KDLoss
  16. import torch.nn as nn
  17. import torch._ops
  18. import time
  19. class HintDistiller(KDDistiller):
  20. def __init__(self, logger=None, tb_writer=None ):
  21. super(HintDistiller, self).__init__( logger, tb_writer )
  22. def setup(self,
  23. student, teacher, regressor, dataloader, optimizer,
  24. hint_layer=2, T=1.0, alpha=1.0, beta=1.0, gamma=1.0,
  25. stu_hooks=[], tea_hooks=[], out_flags=[], device=None):
  26. super( HintDistiller, self ).setup(
  27. student, teacher, dataloader, optimizer, T=T, alpha=alpha, beta=beta, gamma=gamma, device=device )
  28. self.regressor = regressor
  29. self._hint_layer = hint_layer
  30. self._beta = beta
  31. self.stu_hooks = stu_hooks
  32. self.tea_hooks = tea_hooks
  33. self.out_flags = out_flags
  34. self.regressor.to(device)
  35. def additional_kd_loss(self, engine, batch):
  36. feat_s = [f.feat_out if flag else f.feat_in for (f, flag) in zip(self.stu_hooks, self.out_flags)]
  37. feat_t = [f.feat_out.detach() if flag else f.feat_in for (f, flag) in zip(self.tea_hooks, self.out_flags)]
  38. f_s = self.regressor(feat_s[self._hint_layer])
  39. f_t = feat_t[self._hint_layer]
  40. return nn.functional.mse_loss(f_s, f_t)
  41. class Regressor(nn.Module):
  42. """
  43. Convolutional regression for FitNet
  44. @inproceedings{tian2019crd,
  45. title={Contrastive Representation Distillation},
  46. author={Yonglong Tian and Dilip Krishnan and Phillip Isola},
  47. booktitle={International Conference on Learning Representations},
  48. year={2020}
  49. }
  50. """
  51. def __init__(self, s_shape, t_shape, is_relu=True):
  52. super(Regressor, self).__init__()
  53. self.is_relu = is_relu
  54. _, s_C, s_H, s_W = s_shape
  55. _, t_C, t_H, t_W = t_shape
  56. if s_H == 2 * t_H:
  57. self.conv = nn.Conv2d(s_C, t_C, kernel_size=3, stride=2, padding=1)
  58. elif s_H * 2 == t_H:
  59. self.conv = nn.ConvTranspose2d(
  60. s_C, t_C, kernel_size=4, stride=2, padding=1)
  61. elif s_H >= t_H:
  62. self.conv = nn.Conv2d(s_C, t_C, kernel_size=(1+s_H-t_H, 1+s_W-t_W))
  63. else:
  64. raise NotImplemented(
  65. 'student size {}, teacher size {}'.format(s_H, t_H))
  66. self.bn = nn.BatchNorm2d(t_C)
  67. self.relu = nn.ReLU(inplace=True)
  68. def forward(self, x):
  69. x = self.conv(x)
  70. if self.is_relu:
  71. return self.relu(self.bn(x))
  72. else:
  73. return self.bn(x)

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

Contributors (1)