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.

utils.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # Copyright 2020 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. """MobileNetV2 utils"""
  16. import time
  17. import numpy as np
  18. from mindspore.train.callback import Callback
  19. from mindspore import Tensor
  20. from mindspore import nn
  21. from mindspore.nn.loss.loss import _Loss
  22. from mindspore.ops import operations as P
  23. from mindspore.ops import functional as F
  24. from mindspore.common import dtype as mstype
  25. class Monitor(Callback):
  26. """
  27. Monitor loss and time.
  28. Args:
  29. lr_init (numpy array): train lr
  30. Returns:
  31. None
  32. Examples:
  33. >>> Monitor(100,lr_init=Tensor([0.05]*100).asnumpy())
  34. """
  35. def __init__(self, lr_init=None, step_threshold=10):
  36. super(Monitor, self).__init__()
  37. self.lr_init = lr_init
  38. self.lr_init_len = len(lr_init)
  39. self.step_threshold = step_threshold
  40. self.step_mseconds = 50000
  41. def epoch_begin(self, run_context):
  42. self.losses = []
  43. self.epoch_time = time.time()
  44. def epoch_end(self, run_context):
  45. cb_params = run_context.original_args()
  46. epoch_mseconds = (time.time() - self.epoch_time) * 1000
  47. per_step_mseconds = epoch_mseconds / cb_params.batch_num
  48. print("epoch time: {:5.3f}, per step time: {:5.3f}, avg loss: {:8.6f}".format(epoch_mseconds,
  49. per_step_mseconds,
  50. np.mean(self.losses)))
  51. self.epoch_mseconds = epoch_mseconds
  52. def step_begin(self, run_context):
  53. self.step_time = time.time()
  54. def step_end(self, run_context):
  55. cb_params = run_context.original_args()
  56. step_mseconds = (time.time() - self.step_time) * 1000
  57. self.step_mseconds = min(self.step_mseconds, step_mseconds)
  58. step_loss = cb_params.net_outputs
  59. if isinstance(step_loss, (tuple, list)) and isinstance(step_loss[0], Tensor):
  60. step_loss = step_loss[0]
  61. if isinstance(step_loss, Tensor):
  62. step_loss = np.mean(step_loss.asnumpy())
  63. self.losses.append(step_loss)
  64. cur_step_in_epoch = (cb_params.cur_step_num - 1) % cb_params.batch_num
  65. print("epoch: [{:3d}/{:3d}], step:[{:5d}/{:5d}], loss:[{:8.6f}/{:5.3f}], time:[{:5.3f}], lr:[{:5.5f}]".format(
  66. cb_params.cur_epoch_num, cb_params.epoch_num, cur_step_in_epoch +
  67. 1, cb_params.batch_num, step_loss,
  68. np.mean(self.losses), self.step_mseconds, self.lr_init[cb_params.cur_step_num - 1]))
  69. if cb_params.cur_step_num == self.step_threshold:
  70. run_context.request_stop()
  71. class CrossEntropyWithLabelSmooth(_Loss):
  72. """
  73. CrossEntropyWith LabelSmooth.
  74. Args:
  75. smooth_factor (float): smooth factor, default=0.
  76. num_classes (int): num classes
  77. Returns:
  78. None.
  79. Examples:
  80. >>> CrossEntropyWithLabelSmooth(smooth_factor=0., num_classes=1000)
  81. """
  82. def __init__(self, smooth_factor=0., num_classes=1000):
  83. super(CrossEntropyWithLabelSmooth, self).__init__()
  84. self.onehot = P.OneHot()
  85. self.on_value = Tensor(1.0 - smooth_factor, mstype.float32)
  86. self.off_value = Tensor(1.0 * smooth_factor /
  87. (num_classes - 1), mstype.float32)
  88. self.ce = nn.SoftmaxCrossEntropyWithLogits()
  89. self.mean = P.ReduceMean(False)
  90. self.cast = P.Cast()
  91. def construct(self, logit, label):
  92. one_hot_label = self.onehot(self.cast(label, mstype.int32), F.shape(logit)[1],
  93. self.on_value, self.off_value)
  94. out_loss = self.ce(logit, one_hot_label)
  95. out_loss = self.mean(out_loss, 0)
  96. return out_loss