| @@ -0,0 +1,12 @@ | |||||
| namespace Tensorflow.Keras.ArgsDefinition | |||||
| { | |||||
| public class UpSampling2DArgs : LayerArgs | |||||
| { | |||||
| public TensorShape Size { get; set; } | |||||
| public string DataFormat { get; set; } | |||||
| /// <summary> | |||||
| /// 'nearest', 'bilinear' | |||||
| /// </summary> | |||||
| public string Interpolation { get; set; } = "nearest"; | |||||
| } | |||||
| } | |||||
| @@ -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 | |||||
| { | |||||
| /// <summary> | |||||
| /// Zero-padding layer for 2D input (e.g. picture). | |||||
| /// </summary> | |||||
| /// <param name="padding"></param> | |||||
| /// <returns></returns> | |||||
| public ZeroPadding2D ZeroPadding2D(NDArray padding) | |||||
| => new ZeroPadding2D(new ZeroPadding2DArgs | |||||
| { | |||||
| Padding = padding | |||||
| }); | |||||
| /// <summary> | |||||
| /// Upsampling layer for 2D inputs.<br/> | |||||
| /// Repeats the rows and columns of the data by size[0] and size[1] respectively. | |||||
| /// </summary> | |||||
| /// <param name="size"></param> | |||||
| /// <param name="data_format"></param> | |||||
| /// <param name="interpolation"></param> | |||||
| /// <returns></returns> | |||||
| public UpSampling2D UpSampling2D(TensorShape size = null, | |||||
| string data_format = null, | |||||
| string interpolation = "nearest") | |||||
| => new UpSampling2D(new UpSampling2DArgs | |||||
| { | |||||
| Size = size ?? (2, 2) | |||||
| }); | |||||
| } | |||||
| } | |||||
| @@ -6,7 +6,7 @@ using static Tensorflow.KerasApi; | |||||
| namespace Tensorflow.Keras.Layers | namespace Tensorflow.Keras.Layers | ||||
| { | { | ||||
| public class LayersApi | |||||
| public partial class LayersApi | |||||
| { | { | ||||
| /// <summary> | /// <summary> | ||||
| /// Functional interface for the batch normalization layer. | /// Functional interface for the batch normalization layer. | ||||
| @@ -372,19 +372,8 @@ namespace Tensorflow.Keras.Layers | |||||
| InputShape = input_shape | InputShape = input_shape | ||||
| }); | }); | ||||
| /// <summary> | |||||
| /// Zero-padding layer for 2D input (e.g. picture). | |||||
| /// </summary> | |||||
| /// <param name="padding"></param> | |||||
| /// <returns></returns> | |||||
| 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() | public GlobalAveragePooling2D GlobalAveragePooling2D() | ||||
| => new GlobalAveragePooling2D(new Pooling2DArgs { }); | => new GlobalAveragePooling2D(new Pooling2DArgs { }); | ||||
| @@ -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); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -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); | |||||
| } | |||||
| } | |||||
| } | |||||
| @@ -1,5 +1,6 @@ | |||||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | using Microsoft.VisualStudio.TestTools.UnitTesting; | ||||
| using NumSharp; | using NumSharp; | ||||
| using Tensorflow; | |||||
| using static Tensorflow.KerasApi; | using static Tensorflow.KerasApi; | ||||
| namespace TensorFlowNET.UnitTest.Keras | namespace TensorFlowNET.UnitTest.Keras | ||||