Browse Source

Fix MaxPooling1D #969

tags/v0.100.4-load-saved-model
Haiping Chen 2 years ago
parent
commit
f48ba40263
6 changed files with 28 additions and 21 deletions
  1. +3
    -3
      src/TensorFlowNET.Core/Operations/NnOps/MaxPoolFunction.cs
  2. +3
    -3
      src/TensorFlowNET.Core/Tensorflow.Binding.csproj
  3. +14
    -8
      src/TensorFlowNET.Keras/Layers/Pooling/Pooling1D.cs
  4. +1
    -1
      src/TensorFlowNET.Keras/Layers/Pooling/Pooling2D.cs
  5. +4
    -4
      src/TensorFlowNET.Keras/Tensorflow.Keras.csproj
  6. +3
    -2
      test/TensorFlowNET.Keras.UnitTest/Layers/PoolingTest.cs

+ 3
- 3
src/TensorFlowNET.Core/Operations/NnOps/MaxPoolFunction.cs View File

@@ -14,6 +14,7 @@
limitations under the License. limitations under the License.
******************************************************************************/ ******************************************************************************/


using System.Linq;
using static Tensorflow.Binding; using static Tensorflow.Binding;


namespace Tensorflow.Operations namespace Tensorflow.Operations
@@ -24,7 +25,7 @@ namespace Tensorflow.Operations
public class MaxPoolFunction : IPoolFunction public class MaxPoolFunction : IPoolFunction
{ {
public Tensor Apply(Tensor value, public Tensor Apply(Tensor value,
int[] ksize,
int[] pool_size,
int[] strides, int[] strides,
string padding, string padding,
string data_format = "NHWC", string data_format = "NHWC",
@@ -33,10 +34,9 @@ namespace Tensorflow.Operations
return tf_with(ops.name_scope(name, "MaxPool", value), scope => return tf_with(ops.name_scope(name, "MaxPool", value), scope =>
{ {
name = scope; name = scope;
value = ops.convert_to_tensor(value, name: "input");
return gen_nn_ops.max_pool( return gen_nn_ops.max_pool(
value, value,
ksize: ksize,
ksize: pool_size,
strides: strides, strides: strides,
padding: padding, padding: padding,
data_format: data_format, data_format: data_format,


+ 3
- 3
src/TensorFlowNET.Core/Tensorflow.Binding.csproj View File

@@ -5,7 +5,7 @@
<AssemblyName>Tensorflow.Binding</AssemblyName> <AssemblyName>Tensorflow.Binding</AssemblyName>
<RootNamespace>Tensorflow</RootNamespace> <RootNamespace>Tensorflow</RootNamespace>
<TargetTensorFlow>2.10.0</TargetTensorFlow> <TargetTensorFlow>2.10.0</TargetTensorFlow>
<Version>0.100.1</Version>
<Version>0.100.2</Version>
<LangVersion>10.0</LangVersion> <LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Authors>Haiping Chen, Meinrad Recheis, Eli Belash</Authors> <Authors>Haiping Chen, Meinrad Recheis, Eli Belash</Authors>
@@ -20,7 +20,7 @@
<Description>Google's TensorFlow full binding in .NET Standard. <Description>Google's TensorFlow full binding in .NET Standard.
Building, training and infering deep learning models. Building, training and infering deep learning models.
https://tensorflownet.readthedocs.io</Description> https://tensorflownet.readthedocs.io</Description>
<AssemblyVersion>0.100.1.0</AssemblyVersion>
<AssemblyVersion>0.100.2.0</AssemblyVersion>
<PackageReleaseNotes> <PackageReleaseNotes>
tf.net 0.100.x and above are based on tensorflow native 2.10.0 tf.net 0.100.x and above are based on tensorflow native 2.10.0


@@ -38,7 +38,7 @@ https://tensorflownet.readthedocs.io</Description>
tf.net 0.7x.x aligns with TensorFlow v2.7.x native library. tf.net 0.7x.x aligns with TensorFlow v2.7.x native library.
tf.net 0.10x.x aligns with TensorFlow v2.10.x native library. tf.net 0.10x.x aligns with TensorFlow v2.10.x native library.
</PackageReleaseNotes> </PackageReleaseNotes>
<FileVersion>0.100.1.0</FileVersion>
<FileVersion>0.100.2.0</FileVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<SignAssembly>true</SignAssembly> <SignAssembly>true</SignAssembly>


+ 14
- 8
src/TensorFlowNET.Keras/Layers/Pooling/Pooling1D.cs View File

@@ -14,9 +14,11 @@
limitations under the License. limitations under the License.
******************************************************************************/ ******************************************************************************/


using System.Linq;
using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.ArgsDefinition;
using Tensorflow.Keras.Engine; using Tensorflow.Keras.Engine;
using Tensorflow.Keras.Utils; using Tensorflow.Keras.Utils;
using static Tensorflow.Binding;


namespace Tensorflow.Keras.Layers namespace Tensorflow.Keras.Layers
{ {
@@ -36,17 +38,21 @@ namespace Tensorflow.Keras.Layers


protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null) protected override Tensors Call(Tensors inputs, Tensor state = null, bool? training = null)
{ {
int[] pool_shape;
int[] strides;
int pad_axis = args.DataFormat == "channels_first" ? 2 : 3;
inputs = tf.expand_dims(inputs, pad_axis);
int[] pool_shape = new int[] { args.PoolSize, 1 };
int[] strides = new int[] { args.Strides, 1 };
var ndim = inputs[0].ndim;

if (args.DataFormat == "channels_last") if (args.DataFormat == "channels_last")
{ {
pool_shape = new int[] { 1, args.PoolSize, 1 };
strides = new int[] { 1, args.Strides, 1 };
pool_shape = new int[] { 1 }.Concat(pool_shape).Concat(new int[] { 1 }).ToArray();
strides = new int[] { 1 }.Concat(strides).Concat(new int[] { 1 }).ToArray();
} }
else else
{ {
pool_shape = new int[] { 1, 1, args.PoolSize };
strides = new int[] { 1, 1, args.Strides };
pool_shape = new int[] { 1, 1 }.Concat(pool_shape).ToArray();
strides = new int[] { 1, 1 }.Concat(strides).ToArray();
} }


var outputs = args.PoolFunction.Apply( var outputs = args.PoolFunction.Apply(
@@ -54,9 +60,9 @@ namespace Tensorflow.Keras.Layers
ksize: pool_shape, ksize: pool_shape,
strides: strides, strides: strides,
padding: args.Padding.ToUpper(), padding: args.Padding.ToUpper(),
data_format: conv_utils.convert_data_format(args.DataFormat, 3));
data_format: conv_utils.convert_data_format(args.DataFormat, ndim));


return outputs;
return tf.squeeze(outputs, pad_axis);
} }
} }
} }

+ 1
- 1
src/TensorFlowNET.Keras/Layers/Pooling/Pooling2D.cs View File

@@ -42,7 +42,7 @@ namespace Tensorflow.Keras.Layers
int[] strides; int[] strides;
if (args.DataFormat == "channels_last") if (args.DataFormat == "channels_last")
{ {
pool_shape = new int[] { 1, (int)args.PoolSize.dims[0], (int)args.PoolSize.dims[1], 1 };
pool_shape = new int[] { 1, (int)args.PoolSize.dims[0], (int)args.PoolSize.dims[1], 1 };
strides = new int[] { 1, (int)args.Strides.dims[0], (int)args.Strides.dims[1], 1 }; strides = new int[] { 1, (int)args.Strides.dims[0], (int)args.Strides.dims[1], 1 };
} }
else else


+ 4
- 4
src/TensorFlowNET.Keras/Tensorflow.Keras.csproj View File

@@ -7,7 +7,7 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace>Tensorflow.Keras</RootNamespace> <RootNamespace>Tensorflow.Keras</RootNamespace>
<Platforms>AnyCPU;x64</Platforms> <Platforms>AnyCPU;x64</Platforms>
<Version>0.10.1</Version>
<Version>0.10.2</Version>
<Authors>Haiping Chen</Authors> <Authors>Haiping Chen</Authors>
<Product>Keras for .NET</Product> <Product>Keras for .NET</Product>
<Copyright>Apache 2.0, Haiping Chen 2021</Copyright> <Copyright>Apache 2.0, Haiping Chen 2021</Copyright>
@@ -37,8 +37,8 @@ Keras is an API designed for human beings, not machines. Keras follows best prac
<RepositoryType>Git</RepositoryType> <RepositoryType>Git</RepositoryType>
<SignAssembly>true</SignAssembly> <SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Open.snk</AssemblyOriginatorKeyFile> <AssemblyOriginatorKeyFile>Open.snk</AssemblyOriginatorKeyFile>
<AssemblyVersion>0.10.1.0</AssemblyVersion>
<FileVersion>0.10.1.0</FileVersion>
<AssemblyVersion>0.10.2.0</AssemblyVersion>
<FileVersion>0.10.2.0</FileVersion>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<Configurations>Debug;Release;GPU</Configurations> <Configurations>Debug;Release;GPU</Configurations>
</PropertyGroup> </PropertyGroup>
@@ -70,7 +70,7 @@ Keras is an API designed for human beings, not machines. Keras follows best prac
</PropertyGroup> </PropertyGroup>


<ItemGroup> <ItemGroup>
<PackageReference Include="HDF5-CSharp" Version="1.16.2" />
<PackageReference Include="HDF5-CSharp" Version="1.16.3" />
<PackageReference Include="MethodBoundaryAspect.Fody" Version="2.0.148" /> <PackageReference Include="MethodBoundaryAspect.Fody" Version="2.0.148" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="SharpZipLib" Version="1.4.1" /> <PackageReference Include="SharpZipLib" Version="1.4.1" />


+ 3
- 2
test/TensorFlowNET.Keras.UnitTest/Layers/PoolingTest.cs View File

@@ -4,6 +4,7 @@ using System.Linq;
using Tensorflow; using Tensorflow;
using static Tensorflow.Binding; using static Tensorflow.Binding;
using static Tensorflow.KerasApi; using static Tensorflow.KerasApi;
using Microsoft.VisualBasic;


namespace TensorFlowNET.Keras.UnitTest namespace TensorFlowNET.Keras.UnitTest
{ {
@@ -226,7 +227,7 @@ namespace TensorFlowNET.Keras.UnitTest
Assert.AreEqual(expected, y[0].numpy()); Assert.AreEqual(expected, y[0].numpy());
} }


[TestMethod, Ignore("There's an error generated from TF complaining about the shape of the pool. Needs further investigation.")]
[TestMethod]
public void Max1DPoolingChannelsLast() public void Max1DPoolingChannelsLast()
{ {
var x = input_array_1D; var x = input_array_1D;
@@ -239,7 +240,7 @@ namespace TensorFlowNET.Keras.UnitTest


var expected = np.array(new float[,,] var expected = np.array(new float[,,]
{ {
{{2.0f, 2.0f, 3.0f, 3.0f, 3.0f},
{{1.0f, 2.0f, 3.0f, 3.0f, 3.0f},
{ 1.0f, 2.0f, 3.0f, 3.0f, 3.0f}}, { 1.0f, 2.0f, 3.0f, 3.0f, 3.0f}},


{{4.0f, 5.0f, 6.0f, 3.0f, 3.0f}, {{4.0f, 5.0f, 6.0f, 3.0f, 3.0f},


Loading…
Cancel
Save