Skip to content

feat: Add UpSampling1D layer and test. #1117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class UpSampling2DArgs : AutoSerializeLayerArgs
[JsonProperty("size")]
public Shape Size { get; set; }
[JsonProperty("data_format")]
public string DataFormat { get; set; }
public string DataFormat { get; set; } = "channels_last";
/// <summary>
/// 'nearest', 'bilinear'
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace Tensorflow.Keras.ArgsDefinition
{
public class UpSampling1DArgs : AutoSerializeLayerArgs
{
[JsonProperty("size")]
public int Size { get; set; }
}
}
4 changes: 4 additions & 0 deletions src/TensorFlowNET.Core/Keras/Layers/ILayersApi.Reshaping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ public partial interface ILayersApi
public ILayer Reshape(Shape target_shape);
public ILayer Reshape(object[] target_shape);

public ILayer UpSampling1D(
int size
);

public ILayer UpSampling2D(Shape size = null,
string data_format = null,
string interpolation = "nearest");
Expand Down
26 changes: 26 additions & 0 deletions src/TensorFlowNET.Keras/BackendImpl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -956,6 +956,32 @@ Tensors _step(Tensors tensors)

}

/// <summary>
/// Repeats the elements of a tensor along an axis, like `np.repeat`.
/// </summary>
/// <param name="x"></param>
/// <param name="rep"></param>
/// <param name="axis"></param>
/// <returns></returns>
public Tensor repeat_elements(Tensor x, int rep, int axis)
{
var x_shape = x.shape.as_int_list();
if (x_shape[axis] != -1)
{
var splits = tf.split(x, x_shape[axis], axis:axis);
var x_rep = splits.SelectMany(s => Enumerable.Repeat(s, rep)).ToArray();
return concatenate(x_rep, axis);
}
//var auxiliary_axis = axis + 1;
//x_shape = x.shape;
//var x_rep = tf.expand_dims(x, auxiliary_axis);
//var reps = np.ones(x_shape.Length + 1);
//reps[auxiliary_axis] = rep;
//x_rep = tf.tile(x_rep, reps);

throw new NotImplementedException();

}
public Tensor reverse(Tensor input, int axis)
{
return reverse(input, new int[] { axis });
Expand Down
61 changes: 37 additions & 24 deletions src/TensorFlowNET.Keras/Layers/LayersApi.Reshaping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,48 @@

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 ILayer ZeroPadding2D ( NDArray padding )

/// <summary>
/// Upsampling layer for 1D inputs. Repeats each temporal step `size` times along the time axis.
/// </summary>
/// <param name="size"></param>
/// <returns></returns>
public ILayer UpSampling1D(int size)
=> new UpSampling1D(new UpSampling1DArgs
{
Size = size
});

/// <summary>
/// Zero-padding layer for 2D input (e.g. picture).
/// </summary>
/// <param name="padding"></param>
/// <returns></returns>
public ILayer 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 ILayer UpSampling2D ( Shape size = null,
string data_format = null,
string interpolation = "nearest" )
=> new UpSampling2D(new UpSampling2DArgs {
Size = size ?? (2, 2)
});
/// <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 ILayer UpSampling2D(Shape size, string data_format, string interpolation)
=> new UpSampling2D(new UpSampling2DArgs
{
Size = size,
DataFormat = data_format,
Interpolation = interpolation
});

/// <summary>
/// Permutes the dimensions of the input according to a given pattern.
/// </summary>
public ILayer Permute ( int[] dims )
/// <summary>
/// Permutes the dimensions of the input according to a given pattern.
/// </summary>
public ILayer Permute ( int[] dims )
=> new Permute(new PermuteArgs {
dims = dims
});
Expand Down
32 changes: 32 additions & 0 deletions src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling1D.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Text;
using Tensorflow.Common.Types;
using Tensorflow.Keras.ArgsDefinition;
using Tensorflow.Keras.Engine;


namespace Tensorflow.Keras.Layers
{
/// <summary>
/// Upsampling layer for 1D inputs.
/// </summary>
public class UpSampling1D : Layer
{
UpSampling1DArgs args;
int size;

public UpSampling1D(UpSampling1DArgs args) : base(args)
{
this.args = args;
size = args.Size;
inputSpec = new InputSpec(ndim: 3);
}

protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null)
{
var output = keras.backend.repeat_elements(inputs, size, axis: 1);
return output;
}
}
}
3 changes: 3 additions & 0 deletions src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

namespace Tensorflow.Keras.Layers
{
/// <summary>
/// Upsampling layer for 2D inputs.
/// </summary>
public class UpSampling2D : Layer
{
UpSampling2DArgs args;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Tensorflow.NumPy;
using static Tensorflow.Binding;
using static Tensorflow.KerasApi;
Expand All @@ -18,6 +19,15 @@ public void ZeroPadding2D()
Assert.AreEqual((1, 2, 3, 2), y.shape);
}

[TestMethod]
public void UpSampling1D()
{
Shape input_shape = (2, 2, 3);
var x = np.arange(input_shape.size).reshape(input_shape);
var y = tf.keras.layers.UpSampling1D(size: 2).Apply(x);
Assert.AreEqual((2, 4, 3), y.shape);
}

[TestMethod]
public void UpSampling2D()
{
Expand Down