diff --git a/src/TensorFlowNET.Core/APIs/tf.random.cs b/src/TensorFlowNET.Core/APIs/tf.random.cs index c3d05c13..877c52ac 100644 --- a/src/TensorFlowNET.Core/APIs/tf.random.cs +++ b/src/TensorFlowNET.Core/APIs/tf.random.cs @@ -62,6 +62,19 @@ namespace Tensorflow int? seed = null, string name = null, TF_DataType output_dtype = TF_DataType.DtInvalid) => random_ops.multinomial(logits, num_samples, seed: seed, name: name, output_dtype: output_dtype); + + public Tensor uniform(TensorShape shape, + float minval = 0, + float maxval = 1, + TF_DataType dtype = TF_DataType.TF_FLOAT, + int? seed = null, + string name = null) + { + if (dtype.is_integer()) + return random_ops.random_uniform_int(shape, (int)minval, (int)maxval, dtype, seed, name); + else + return random_ops.random_uniform(shape, minval, maxval, dtype, seed, name); + } } public Tensor random_uniform(TensorShape shape, @@ -70,16 +83,7 @@ namespace Tensorflow TF_DataType dtype = TF_DataType.TF_FLOAT, int? seed = null, string name = null) - { - if (dtype.is_integer()) - { - return random_ops.random_uniform_int(shape, (int)minval, (int)maxval, dtype, seed, name); - } - else - { - return random_ops.random_uniform(shape, minval, maxval, dtype, seed, name); - } - } + => random.uniform(shape, minval: minval, maxval: maxval, dtype: dtype, seed: seed, name: name); public Tensor truncated_normal(TensorShape shape, float mean = 0.0f, diff --git a/src/TensorFlowNET.Core/Keras/ArgsDefinition/Preprocessing/ResizingArgs.cs b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Preprocessing/ResizingArgs.cs new file mode 100644 index 00000000..bb6d9277 --- /dev/null +++ b/src/TensorFlowNET.Core/Keras/ArgsDefinition/Preprocessing/ResizingArgs.cs @@ -0,0 +1,9 @@ +namespace Tensorflow.Keras.ArgsDefinition +{ + public class ResizingArgs : LayerArgs + { + public int Height { get; set; } + public int Width { get; set; } + public string Interpolation { get; set; } = "bilinear"; + } +} diff --git a/src/TensorFlowNET.Keras/Layers/Preprocessing/Resizing.cs b/src/TensorFlowNET.Keras/Layers/Preprocessing/Resizing.cs new file mode 100644 index 00000000..eeb813d7 --- /dev/null +++ b/src/TensorFlowNET.Keras/Layers/Preprocessing/Resizing.cs @@ -0,0 +1,30 @@ +using System; +using System.Text; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Engine; + +namespace Tensorflow.Keras.Layers +{ + /// + /// Resize the batched image input to target height and width. + /// The input should be a 4-D tensor in the format of NHWC. + /// + public class Resizing : Layer + { + ResizingArgs args; + public Resizing(ResizingArgs args) : base(args) + { + this.args = args; + } + + protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) + { + return image_ops_impl.resize_images_v2(inputs, new[] { args.Height, args.Width }, method: args.Interpolation); + } + + public override TensorShape ComputeOutputShape(TensorShape input_shape) + { + return new TensorShape(input_shape.dims[0], args.Height, args.Width, input_shape.dims[3]); + } + } +} diff --git a/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.Resizing.cs b/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.Resizing.cs new file mode 100644 index 00000000..5e93f583 --- /dev/null +++ b/src/TensorFlowNET.Keras/Preprocessings/Preprocessing.Resizing.cs @@ -0,0 +1,26 @@ +using System; +using System.IO; +using Tensorflow.Keras.ArgsDefinition; +using Tensorflow.Keras.Layers; +using static Tensorflow.KerasApi; + +namespace Tensorflow.Keras +{ + public partial class Preprocessing + { + /// + /// Image resizing layer + /// + /// + /// + /// + /// + public Resizing Resizing(int height, int width, string interpolation = "bilinear") + => new Resizing(new ResizingArgs + { + Height = height, + Width = width, + Interpolation = interpolation + }); + } +} diff --git a/test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs b/test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs index 4c9ccb0e..1f4814b9 100644 --- a/test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs +++ b/test/TensorFlowNET.Keras.UnitTest/Layers/LayersTest.cs @@ -131,5 +131,13 @@ namespace TensorFlowNET.Keras.UnitTest Assert.AreEqual((32, 4), output.shape); } + [TestMethod] + public void Resizing() + { + var inputs = tf.random.uniform((10, 32, 32, 3)); + var layer = keras.layers.preprocessing.Resizing(16, 16); + var output = layer.Apply(inputs); + Assert.AreEqual((10, 16, 16, 3), output.shape); + } } }