Skip to content

Commit 0fe91a4

Browse files
authored
Merge pull request #1117 from Wanglongzhi2001/master
feat: Add UpSampling1D layer and test.
2 parents bfa9f77 + eeb20e4 commit 0fe91a4

File tree

8 files changed

+123
-25
lines changed

8 files changed

+123
-25
lines changed

src/TensorFlowNET.Core/Keras/ArgsDefinition/Reshaping/UpSampling2DArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class UpSampling2DArgs : AutoSerializeLayerArgs
77
[JsonProperty("size")]
88
public Shape Size { get; set; }
99
[JsonProperty("data_format")]
10-
public string DataFormat { get; set; }
10+
public string DataFormat { get; set; } = "channels_last";
1111
/// <summary>
1212
/// 'nearest', 'bilinear'
1313
/// </summary>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Tensorflow.Keras.ArgsDefinition
4+
{
5+
public class UpSampling1DArgs : AutoSerializeLayerArgs
6+
{
7+
[JsonProperty("size")]
8+
public int Size { get; set; }
9+
}
10+
}

src/TensorFlowNET.Core/Keras/Layers/ILayersApi.Reshaping.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ public partial interface ILayersApi
99
public ILayer Reshape(Shape target_shape);
1010
public ILayer Reshape(object[] target_shape);
1111

12+
public ILayer UpSampling1D(
13+
int size
14+
);
15+
1216
public ILayer UpSampling2D(Shape size = null,
1317
string data_format = null,
1418
string interpolation = "nearest");

src/TensorFlowNET.Keras/BackendImpl.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,32 @@ Tensors _step(Tensors tensors)
956956

957957
}
958958

959+
/// <summary>
960+
/// Repeats the elements of a tensor along an axis, like `np.repeat`.
961+
/// </summary>
962+
/// <param name="x"></param>
963+
/// <param name="rep"></param>
964+
/// <param name="axis"></param>
965+
/// <returns></returns>
966+
public Tensor repeat_elements(Tensor x, int rep, int axis)
967+
{
968+
var x_shape = x.shape.as_int_list();
969+
if (x_shape[axis] != -1)
970+
{
971+
var splits = tf.split(x, x_shape[axis], axis:axis);
972+
var x_rep = splits.SelectMany(s => Enumerable.Repeat(s, rep)).ToArray();
973+
return concatenate(x_rep, axis);
974+
}
975+
//var auxiliary_axis = axis + 1;
976+
//x_shape = x.shape;
977+
//var x_rep = tf.expand_dims(x, auxiliary_axis);
978+
//var reps = np.ones(x_shape.Length + 1);
979+
//reps[auxiliary_axis] = rep;
980+
//x_rep = tf.tile(x_rep, reps);
981+
982+
throw new NotImplementedException();
983+
984+
}
959985
public Tensor reverse(Tensor input, int axis)
960986
{
961987
return reverse(input, new int[] { axis });

src/TensorFlowNET.Keras/Layers/LayersApi.Reshaping.cs

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,48 @@
66

77
namespace Tensorflow.Keras.Layers {
88
public partial class LayersApi {
9-
/// <summary>
10-
/// Zero-padding layer for 2D input (e.g. picture).
11-
/// </summary>
12-
/// <param name="padding"></param>
13-
/// <returns></returns>
14-
public ILayer ZeroPadding2D ( NDArray padding )
9+
10+
/// <summary>
11+
/// Upsampling layer for 1D inputs. Repeats each temporal step `size` times along the time axis.
12+
/// </summary>
13+
/// <param name="size"></param>
14+
/// <returns></returns>
15+
public ILayer UpSampling1D(int size)
16+
=> new UpSampling1D(new UpSampling1DArgs
17+
{
18+
Size = size
19+
});
20+
21+
/// <summary>
22+
/// Zero-padding layer for 2D input (e.g. picture).
23+
/// </summary>
24+
/// <param name="padding"></param>
25+
/// <returns></returns>
26+
public ILayer ZeroPadding2D ( NDArray padding )
1527
=> new ZeroPadding2D(new ZeroPadding2DArgs {
1628
Padding = padding
1729
});
1830

19-
/// <summary>
20-
/// Upsampling layer for 2D inputs.<br/>
21-
/// Repeats the rows and columns of the data by size[0] and size[1] respectively.
22-
/// </summary>
23-
/// <param name="size"></param>
24-
/// <param name="data_format"></param>
25-
/// <param name="interpolation"></param>
26-
/// <returns></returns>
27-
public ILayer UpSampling2D ( Shape size = null,
28-
string data_format = null,
29-
string interpolation = "nearest" )
30-
=> new UpSampling2D(new UpSampling2DArgs {
31-
Size = size ?? (2, 2)
32-
});
31+
/// <summary>
32+
/// Upsampling layer for 2D inputs.<br/>
33+
/// Repeats the rows and columns of the data by size[0] and size[1] respectively.
34+
/// </summary>
35+
/// <param name="size"></param>
36+
/// <param name="data_format"></param>
37+
/// <param name="interpolation"></param>
38+
/// <returns></returns>
39+
public ILayer UpSampling2D(Shape size, string data_format, string interpolation)
40+
=> new UpSampling2D(new UpSampling2DArgs
41+
{
42+
Size = size,
43+
DataFormat = data_format,
44+
Interpolation = interpolation
45+
});
3346

34-
/// <summary>
35-
/// Permutes the dimensions of the input according to a given pattern.
36-
/// </summary>
37-
public ILayer Permute ( int[] dims )
47+
/// <summary>
48+
/// Permutes the dimensions of the input according to a given pattern.
49+
/// </summary>
50+
public ILayer Permute ( int[] dims )
3851
=> new Permute(new PermuteArgs {
3952
dims = dims
4053
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow.Common.Types;
5+
using Tensorflow.Keras.ArgsDefinition;
6+
using Tensorflow.Keras.Engine;
7+
8+
9+
namespace Tensorflow.Keras.Layers
10+
{
11+
/// <summary>
12+
/// Upsampling layer for 1D inputs.
13+
/// </summary>
14+
public class UpSampling1D : Layer
15+
{
16+
UpSampling1DArgs args;
17+
int size;
18+
19+
public UpSampling1D(UpSampling1DArgs args) : base(args)
20+
{
21+
this.args = args;
22+
size = args.Size;
23+
inputSpec = new InputSpec(ndim: 3);
24+
}
25+
26+
protected override Tensors Call(Tensors inputs, Tensors state = null, bool? training = null, IOptionalArgs? optional_args = null)
27+
{
28+
var output = keras.backend.repeat_elements(inputs, size, axis: 1);
29+
return output;
30+
}
31+
}
32+
}

src/TensorFlowNET.Keras/Layers/Reshaping/UpSampling2D.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
namespace Tensorflow.Keras.Layers
1212
{
13+
/// <summary>
14+
/// Upsampling layer for 2D inputs.
15+
/// </summary>
1316
public class UpSampling2D : Layer
1417
{
1518
UpSampling2DArgs args;

test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Reshaping.Test.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using System;
23
using Tensorflow.NumPy;
34
using static Tensorflow.Binding;
45
using static Tensorflow.KerasApi;
@@ -18,6 +19,15 @@ public void ZeroPadding2D()
1819
Assert.AreEqual((1, 2, 3, 2), y.shape);
1920
}
2021

22+
[TestMethod]
23+
public void UpSampling1D()
24+
{
25+
Shape input_shape = (2, 2, 3);
26+
var x = np.arange(input_shape.size).reshape(input_shape);
27+
var y = tf.keras.layers.UpSampling1D(size: 2).Apply(x);
28+
Assert.AreEqual((2, 4, 3), y.shape);
29+
}
30+
2131
[TestMethod]
2232
public void UpSampling2D()
2333
{

0 commit comments

Comments
 (0)