From 94ad2f5331560bbaac9712b3a41d8e9550b7b89d Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 29 Nov 2020 10:26:06 -0600 Subject: [PATCH] Add layers of ZeroPadding2D and UpSampling2D. --- .../Reshaping/UpSampling2DArgs.cs | 12 ++++++ .../{ => Reshaping}/ZeroPadding2DArgs.cs | 0 .../Layers/LayersApi.Reshaping.cs | 38 +++++++++++++++++++ src/TensorFlowNET.Keras/Layers/LayersApi.cs | 17 ++------- .../Layers/Reshaping/UpSampling2D.cs | 35 +++++++++++++++++ .../Layers/{ => Reshaping}/ZeroPadding2D.cs | 0 .../Keras/Layers.Reshaping.Test.cs | 30 +++++++++++++++ .../Keras/LayersTest.cs | 1 + 8 files changed, 119 insertions(+), 14 deletions(-) create mode 100644 src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/UpSampling2DArgs.cs rename src/TensorFlowNET.Core/Keras/ArgsDefinition/{ => Reshaping}/ZeroPadding2DArgs.cs (100%) create mode 100644 src/TensorFlowNET.Keras/Layers/LayersApi.Reshaping.cs create mode 100644 src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling2D.cs rename src/TensorFlowNET.Keras/Layers/{ => Reshaping}/ZeroPadding2D.cs (100%) create mode 100644 test/TensorFlowNET.UnitTest/Keras/Layers.Reshaping.Test.cs diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/UpSampling2DArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/UpSampling2DArgs.cs new file mode 100644 index 00000000..049010ad --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/UpSampling2DArgs.cs @@ -0,0 +1,12 @@ +namespace Tensorflow.Keras.ArgsDefinition +{ + public class UpSampling2DArgs : LayerArgs + { + public TensorShape Size { get; set; } + public string DataFormat { get; set; } + /// + /// 'nearest', 'bilinear' + /// + public string Interpolation { get; set; } = "nearest"; + } +} diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/ZeroPadding2DArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/ZeroPadding2DArgs.cs similarity index 100% rename from src/TensorFlowNET.Core/Keras/ArgsDefinition/ZeroPadding2DArgs.cs rename to src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/ZeroPadding2DArgs.cs diff --git a/src/TensorFlowNET.Keras/Layers/LayersApi.Reshaping.cs b/src/TensorFlowNET.Keras/Layers/LayersApi.Reshaping.cs new file mode 100644 index 00000000..fa69d854 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/LayersApi.Reshaping.cs @@ -0,0 +1,38 @@ +using NumSharp; +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Keras.ArgsDefinition; + +namespace Tensorflow.Keras.Layers +{ + public partial class LayersApi + { + /// + /// Zero-padding layer for 2D input (e.g. picture). + /// + /// + /// + public ZeroPadding2D ZeroPadding2D(NDArray padding) + => new ZeroPadding2D(new ZeroPadding2DArgs + { + Padding = padding + }); + + /// + /// Upsampling layer for 2D inputs.
+ /// Repeats the rows and columns of the data by size[0] and size[1] respectively. + ///
+ /// + /// + /// + /// + public UpSampling2D UpSampling2D(TensorShape size = null, + string data_format = null, + string interpolation = "nearest") + => new UpSampling2D(new UpSampling2DArgs + { + Size = size ?? (2, 2) + }); + } +} diff --git a/src/TensorFlowNET.Keras/Layers/LayersApi.cs b/src/TensorFlowNET.Keras/Layers/LayersApi.cs index 4b770cda..b638cc70 100644 --- a/src/TensorFlowNET.Keras/Layers/LayersApi.cs +++ b/src/TensorFlowNET.Keras/Layers/LayersApi.cs @@ -6,7 +6,7 @@ using static Tensorflow.KerasApi; namespace Tensorflow.Keras.Layers { - public class LayersApi + public partial class LayersApi { /// /// Functional interface for the batch normalization layer. @@ -372,19 +372,8 @@ namespace Tensorflow.Keras.Layers InputShape = input_shape }); - /// - /// Zero-padding layer for 2D input (e.g. picture). - /// - /// - /// - public ZeroPadding2D ZeroPadding2D(NDArray padding) - => new ZeroPadding2D(new ZeroPadding2DArgs - { - Padding = padding - }); - - public Tensor add(params Tensor[] inputs) - => new Add(new MergeArgs { Inputs = inputs }).Apply(inputs); + public Add Add(params Tensor[] inputs) + => new Add(new MergeArgs { Inputs = inputs }); public GlobalAveragePooling2D GlobalAveragePooling2D() => new GlobalAveragePooling2D(new Pooling2DArgs { }); diff --git a/src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling2D.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling2D.cs new file mode 100644 index 00000000..3fcf6c99 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling2D.cs @@ -0,0 +1,35 @@ +using System; +using System.Collections.Generic; +using System.Text; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Engine; +using Tensorflow.Keras.Utils; +using static Tensorflow.Binding; +using static Tensorflow.KerasApi; + +namespace Tensorflow.Keras.Layers +{ + public class UpSampling2D : Layer + { + UpSampling2DArgs args; + int[] size; + string data_format; + string interpolation => args.Interpolation; + + public UpSampling2D(UpSampling2DArgs args) : base(args) + { + this.args = args; + data_format = conv_utils.normalize_data_format(args.DataFormat); + size = conv_utils.normalize_tuple(args.Size, 2, "size"); + inputSpec = new InputSpec(ndim: 4); + } + + protected override Tensors Call(Tensors inputs, Tensor state = null, bool is_training = false) + { + return keras.backend.resize_images(inputs, + size[0], size[1], + data_format, + interpolation: interpolation); + } + } +} diff --git a/src/TensorFlowNET.Keras/Layers/ZeroPadding2D.cs b/src/TensorFlowNET.Keras/Layers/Reshaping/ZeroPadding2D.cs similarity index 100% rename from src/TensorFlowNET.Keras/Layers/ZeroPadding2D.cs rename to src/TensorFlowNET.Keras/Layers/Reshaping/ZeroPadding2D.cs diff --git a/test/TensorFlowNET.UnitTest/Keras/Layers.Reshaping.Test.cs b/test/TensorFlowNET.UnitTest/Keras/Layers.Reshaping.Test.cs new file mode 100644 index 00000000..fc5bb525 --- /dev/null +++ b/test/TensorFlowNET.UnitTest/Keras/Layers.Reshaping.Test.cs @@ -0,0 +1,30 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using NumSharp; +using Tensorflow; +using static Tensorflow.KerasApi; + +namespace TensorFlowNET.UnitTest.Keras +{ + [TestClass] + public class LayersReshapingTest : EagerModeTestBase + { + [TestMethod] + public void ZeroPadding2D() + { + var input_shape = new[] { 1, 1, 2, 2 }; + var x = np.arange(np.prod(input_shape)).reshape(input_shape); + var zero_padding_2d = keras.layers.ZeroPadding2D(new[,] { { 1, 0 }, { 1, 0 } }); + var y = zero_padding_2d.Apply(x); + Assert.AreEqual((1, 2, 3, 2), y.shape); + } + + [TestMethod] + public void UpSampling2D() + { + var input_shape = new[] { 2, 2, 1, 3 }; + var x = np.arange(np.prod(input_shape)).reshape(input_shape); + var y = keras.layers.UpSampling2D(size: (1, 2)).Apply(x); + Assert.AreEqual((2, 2, 2, 3), y.shape); + } + } +} diff --git a/test/TensorFlowNET.UnitTest/Keras/LayersTest.cs b/test/TensorFlowNET.UnitTest/Keras/LayersTest.cs index 477c2ae9..279a5db5 100644 --- a/test/TensorFlowNET.UnitTest/Keras/LayersTest.cs +++ b/test/TensorFlowNET.UnitTest/Keras/LayersTest.cs @@ -1,5 +1,6 @@ using Microsoft.VisualStudio.TestTools.UnitTesting; using NumSharp; +using Tensorflow; using static Tensorflow.KerasApi; namespace TensorFlowNET.UnitTest.Keras