distillation_models.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. from __future__ import absolute_import
  15. from __future__ import division
  16. from __future__ import print_function
  17. import math
  18. import paddle
  19. import paddle.nn as nn
  20. from .resnet_vd import ResNet50_vd
  21. from .mobilenet_v3 import MobileNetV3_large_x1_0
  22. from .resnext101_wsl import ResNeXt101_32x16d_wsl
  23. __all__ = [
  24. 'ResNet50_vd_distill_MobileNetV3_large_x1_0',
  25. 'ResNeXt101_32x16d_wsl_distill_ResNet50_vd'
  26. ]
  27. class ResNet50_vd_distill_MobileNetV3_large_x1_0(nn.Layer):
  28. def __init__(self, class_dim=1000, freeze_teacher=True, **args):
  29. super(ResNet50_vd_distill_MobileNetV3_large_x1_0, self).__init__()
  30. self.teacher = ResNet50_vd(class_dim=class_dim, **args)
  31. self.student = MobileNetV3_large_x1_0(class_dim=class_dim, **args)
  32. if freeze_teacher:
  33. for param in self.teacher.parameters():
  34. param.trainable = False
  35. def forward(self, x):
  36. teacher_label = self.teacher(x)
  37. student_label = self.student(x)
  38. return teacher_label, student_label
  39. class ResNeXt101_32x16d_wsl_distill_ResNet50_vd(nn.Layer):
  40. def __init__(self, class_dim=1000, freeze_teacher=True, **args):
  41. super(ResNeXt101_32x16d_wsl_distill_ResNet50_vd, self).__init__()
  42. self.teacher = ResNeXt101_32x16d_wsl(class_dim=class_dim, **args)
  43. self.student = ResNet50_vd(class_dim=class_dim, **args)
  44. if freeze_teacher:
  45. for param in self.teacher.parameters():
  46. param.trainable = False
  47. def forward(self, x):
  48. teacher_label = self.teacher(x)
  49. student_label = self.student(x)
  50. return teacher_label, student_label