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.

py_transforms.py 1.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  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. # ==============================================================================
  15. """
  16. This module py_transforms is implemented basing on python. It provides common
  17. operations including OneHotOp.
  18. """
  19. from .validators import check_one_hot_op
  20. from .vision import py_transforms_util as util
  21. class OneHotOp:
  22. """
  23. Apply one hot encoding transformation to the input label, make label be more smoothing and continuous.
  24. Args:
  25. num_classes (int): Num class of object in dataset, type is int and value over 0.
  26. smoothing_rate (float): The adjustable Hyper parameter decides the label smoothing level , 0.0 means not do it.
  27. """
  28. @check_one_hot_op
  29. def __init__(self, num_classes, smoothing_rate=0.0):
  30. self.num_classes = num_classes
  31. self.smoothing_rate = smoothing_rate
  32. def __call__(self, label):
  33. """
  34. Call method.
  35. Args:
  36. label (numpy.ndarray): label to be applied label smoothing.
  37. Returns:
  38. label (numpy.ndarray), label after being Smoothed.
  39. """
  40. return util.one_hot_encoding(label, self.num_classes, self.smoothing_rate)