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.

Pooling2D.cs 2.6 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 Tensorflow.Keras.ArgsDefinition;
  14. using Tensorflow.Keras.Engine;
  15. using Tensorflow.Keras.Utils;
  16. namespace Tensorflow.Keras.Layers
  17. {
  18. public class Pooling2D : Layer
  19. {
  20. Pooling2DArgs args;
  21. InputSpec input_spec;
  22. public Pooling2D(Pooling2DArgs args)
  23. : base(args)
  24. {
  25. this.args = args;
  26. args.PoolSize = conv_utils.normalize_tuple(args.PoolSize, 2, "pool_size");
  27. args.Strides = conv_utils.normalize_tuple(args.Strides ?? args.PoolSize, 2, "strides");
  28. args.Padding = conv_utils.normalize_padding(args.Padding);
  29. args.DataFormat = conv_utils.normalize_data_format(args.DataFormat);
  30. input_spec = new InputSpec(ndim: 4);
  31. }
  32. protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null)
  33. {
  34. int[] pool_shape;
  35. int[] strides;
  36. if (args.DataFormat == "channels_last")
  37. {
  38. pool_shape = new int[] { 1, (int)args.PoolSize.dims[0], (int)args.PoolSize.dims[1], 1 };
  39. strides = new int[] { 1, (int)args.Strides.dims[0], (int)args.Strides.dims[1], 1 };
  40. }
  41. else
  42. {
  43. pool_shape = new int[] { 1, 1, (int)args.PoolSize.dims[0], (int)args.PoolSize.dims[1] };
  44. strides = new int[] { 1, 1, (int)args.Strides.dims[0], (int)args.Strides.dims[1] };
  45. }
  46. var outputs = args.PoolFunction.Apply(
  47. inputs,
  48. ksize: pool_shape,
  49. strides: strides,
  50. padding: args.Padding.ToUpper(),
  51. data_format: conv_utils.convert_data_format(args.DataFormat, 4));
  52. return outputs;
  53. }
  54. }
  55. }