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.

BackendBase.cs 3.2 kB

4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using static Tensorflow.Binding;
  15. namespace Tensorflow.Keras
  16. {
  17. public abstract class BackendBase
  18. {
  19. TF_DataType _FLOATX = dtypes.float32;
  20. float _EPSILON = 1e-7f;
  21. ImageDataFormat _IMAGE_DATA_FORMAT = ImageDataFormat.channels_last;
  22. public float epsilon() => _EPSILON;
  23. public void set_epsilon(float e) => _EPSILON = e;
  24. public TF_DataType floatx() => _FLOATX;
  25. public void set_floatx(TF_DataType floatx) => _FLOATX = floatx;
  26. //public NDArray cast_to_floatx(NDArray x) => np.array(x, dtype: _FLOATX.as_numpy_datatype());
  27. public ImageDataFormat image_data_format() => _IMAGE_DATA_FORMAT;
  28. public void set_image_data_format(ImageDataFormat data_format) => _IMAGE_DATA_FORMAT = data_format;
  29. public ImageDataFormat normalize_data_format(object value = null)
  30. {
  31. if (value == null)
  32. value = _IMAGE_DATA_FORMAT;
  33. if (isinstance(value, typeof(ImageDataFormat)))
  34. return (ImageDataFormat)value;
  35. else if (isinstance(value, typeof(string)))
  36. {
  37. ImageDataFormat dataFormat;
  38. if (Enum.TryParse((string)value, true, out dataFormat))
  39. {
  40. if (Enum.IsDefined(typeof(ImageDataFormat), dataFormat) | dataFormat.ToString().Contains(","))
  41. return dataFormat;
  42. }
  43. }
  44. throw new Exception("The `data_format` argument must be one of \"channels_first\", \"channels_last\". Received: " + value.ToString());
  45. }
  46. //Legacy Methods
  47. public void set_image_dim_ordering(ImageDimOrder dim_ordering)
  48. {
  49. if (dim_ordering == ImageDimOrder.th)
  50. _IMAGE_DATA_FORMAT = ImageDataFormat.channels_first;
  51. else if (dim_ordering == ImageDimOrder.tf)
  52. _IMAGE_DATA_FORMAT = ImageDataFormat.channels_last;
  53. else
  54. throw new Exception("Unknown dim_ordering:" + dim_ordering);
  55. }
  56. public ImageDimOrder image_dim_ordering()
  57. {
  58. if (_IMAGE_DATA_FORMAT == ImageDataFormat.channels_first)
  59. return ImageDimOrder.th;
  60. else
  61. return ImageDimOrder.tf;
  62. }
  63. }
  64. public enum ImageDimOrder
  65. {
  66. tf,
  67. th
  68. }
  69. }