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.

init_params.py 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. """Parameters utils"""
  16. from mindspore import Tensor
  17. from mindspore.common.initializer import initializer, TruncatedNormal
  18. def init_net_param(network, initialize_mode='TruncatedNormal'):
  19. """Init the parameters in net."""
  20. params = network.trainable_params()
  21. for p in params:
  22. if isinstance(p.data, Tensor) and 'beta' not in p.name and 'gamma' not in p.name and 'bias' not in p.name:
  23. if initialize_mode == 'TruncatedNormal':
  24. p.set_parameter_data(initializer(TruncatedNormal(0.03), p.data.shape, p.data.dtype))
  25. else:
  26. p.set_parameter_data(initialize_mode, p.data.shape, p.data.dtype)
  27. def load_backbone_params(network, param_dict):
  28. """Init the parameters from pre-train model, default is mobilenetv2."""
  29. for _, param in net.parameters_and_names():
  30. param_name = param.name.replace('network.backbone.', '')
  31. name_split = param_name.split('.')
  32. if 'features_1' in param_name:
  33. param_name = param_name.replace('features_1', 'features')
  34. if 'features_2' in param_name:
  35. param_name = '.'.join(['features', str(int(name_split[1]) + 14)] + name_split[2:])
  36. if param_name in param_dict:
  37. param.set_parameter_data(param_dict[param_name].data)