Browse Source

Added unit tests for Conv2D

tags/v0.40-tf2.4-tstring
Niklas Gustafsson 4 years ago
parent
commit
f3da1cd9b7
2 changed files with 94 additions and 2 deletions
  1. +3
    -2
      src/TensorFlowNET.Keras/Layers/LayersApi.cs
  2. +91
    -0
      test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Convolution.Test.cs

+ 3
- 2
src/TensorFlowNET.Keras/Layers/LayersApi.cs View File

@@ -66,7 +66,7 @@ namespace Tensorflow.Keras.Layers
Trainable = trainable,
Name = name
});
#if false
/// <summary>
/// 2D convolution layer (e.g. spatial convolution over images).
/// This layer creates a convolution kernel that is convolved with the layer input to produce a tensor of outputs.
@@ -119,6 +119,7 @@ namespace Tensorflow.Keras.Layers
ActivityRegularizer = activity_regularizer,
Activation = activation ?? keras.activations.Linear
});
#endif

/// <summary>
/// 2D convolution layer (e.g. spatial convolution over images).
@@ -158,7 +159,7 @@ namespace Tensorflow.Keras.Layers
{
Rank = 2,
Filters = filters,
KernelSize = kernel_size,
KernelSize = (kernel_size == null) ? (5,5) : kernel_size,
Strides = strides == null ? (1, 1) : strides,
Padding = padding,
DataFormat = data_format,


+ 91
- 0
test/TensorFlowNET.Keras.UnitTest/Layers/Layers.Convolution.Test.cs View File

@@ -0,0 +1,91 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NumSharp;
using Tensorflow;
using static Tensorflow.KerasApi;

namespace TensorFlowNET.Keras.UnitTest
{
[TestClass]
public class LayersConvolutionTest : EagerModeTestBase
{
[TestMethod]
public void BasicConv2D()
{
var filters = 8;
var conv = keras.layers.Conv2D(filters);

var x = np.arange(256.0f).reshape(1,8,8,4);
var y = conv.Apply(x);

Assert.AreEqual(4, y.shape.ndim);
Assert.AreEqual(x.shape[0], y.shape[0]);
Assert.AreEqual(x.shape[1] - 4, y.shape[1]);
Assert.AreEqual(x.shape[2] - 4, y.shape[2]);
Assert.AreEqual(filters, y.shape[3]);
}

[TestMethod]
public void BasicConv2D_ksize()
{
var filters = 8;
var conv = keras.layers.Conv2D(filters, kernel_size: 3);

var x = np.arange(256.0f).reshape(1, 8, 8, 4);
var y = conv.Apply(x);

Assert.AreEqual(4, y.shape.ndim);
Assert.AreEqual(x.shape[0], y.shape[0]);
Assert.AreEqual(x.shape[1] - 2, y.shape[1]);
Assert.AreEqual(x.shape[2] - 2, y.shape[2]);
Assert.AreEqual(filters, y.shape[3]);
}

[TestMethod]
public void BasicConv2D_ksize_same()
{
var filters = 8;
var conv = keras.layers.Conv2D(filters, kernel_size: 3, padding: "same");

var x = np.arange(256.0f).reshape(1, 8, 8, 4);
var y = conv.Apply(x);

Assert.AreEqual(4, y.shape.ndim);
Assert.AreEqual(x.shape[0], y.shape[0]);
Assert.AreEqual(x.shape[1], y.shape[1]);
Assert.AreEqual(x.shape[2], y.shape[2]);
Assert.AreEqual(filters, y.shape[3]);
}

[TestMethod]
public void BasicConv2D_ksize_strides()
{
var filters = 8;
var conv = keras.layers.Conv2D(filters, kernel_size: 3, strides: 2);

var x = np.arange(256.0f).reshape(1, 8, 8, 4);
var y = conv.Apply(x);

Assert.AreEqual(4, y.shape.ndim);
Assert.AreEqual(x.shape[0], y.shape[0]);
Assert.AreEqual(x.shape[1] - 5, y.shape[1]);
Assert.AreEqual(x.shape[2] - 5, y.shape[2]);
Assert.AreEqual(filters, y.shape[3]);
}

[TestMethod]
public void BasicConv2D_ksize_dilation()
{
var filters = 8;
var conv = keras.layers.Conv2D(filters, kernel_size: 3, dilation_rate: 2);

var x = np.arange(256.0f).reshape(1, 8, 8, 4);
var y = conv.Apply(x);

Assert.AreEqual(4, y.shape.ndim);
Assert.AreEqual(x.shape[0], y.shape[0]);
Assert.AreEqual(x.shape[1] - 4, y.shape[1]);
Assert.AreEqual(x.shape[2] - 4, y.shape[2]);
Assert.AreEqual(filters, y.shape[3]);
}
}
}

Loading…
Cancel
Save