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.

imagenet_preprocessing_utils.py 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. # Copyright 2019 The TensorFlow Authors. All Rights Reserved.
  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. def preprocess_input(x, data_format=None, mode=None):
  16. if mode == 'tf':
  17. x /= 127.5
  18. x -= 1.
  19. return x
  20. elif mode == 'torch':
  21. x /= 255.
  22. mean = [0.485, 0.456, 0.406]
  23. std = [0.229, 0.224, 0.225]
  24. elif mode == 'caffe':
  25. if data_format == 'channels_first':
  26. # 'RGB'->'BGR'
  27. if x.ndim == 3:
  28. x = x[::-1, ...]
  29. else:
  30. x = x[:, ::-1, ...]
  31. else:
  32. # 'RGB'->'BGR'
  33. x = x[..., ::-1]
  34. mean = [103.939, 116.779, 123.68]
  35. std = None
  36. elif mode == 'tfhub':
  37. x /= 255.
  38. return x
  39. else:
  40. return x
  41. # Zero-center by mean pixel
  42. if data_format == 'channels_first':
  43. if x.ndim == 3:
  44. x[0, :, :] -= mean[0]
  45. x[1, :, :] -= mean[1]
  46. x[2, :, :] -= mean[2]
  47. if std is not None:
  48. x[0, :, :] /= std[0]
  49. x[1, :, :] /= std[1]
  50. x[2, :, :] /= std[2]
  51. else:
  52. x[:, 0, :, :] -= mean[0]
  53. x[:, 1, :, :] -= mean[1]
  54. x[:, 2, :, :] -= mean[2]
  55. if std is not None:
  56. x[:, 0, :, :] /= std[0]
  57. x[:, 1, :, :] /= std[1]
  58. x[:, 2, :, :] /= std[2]
  59. else:
  60. x[..., 0] -= mean[0]
  61. x[..., 1] -= mean[1]
  62. x[..., 2] -= mean[2]
  63. if std is not None:
  64. x[..., 0] /= std[0]
  65. x[..., 1] /= std[1]
  66. x[..., 2] /= std[2]
  67. return x

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