From 7f507fa81e0dd942cc1458322b428f12b4f6c267 Mon Sep 17 00:00:00 2001 From: vpenades <5433822+vpenades@users.noreply.github.com> Date: Mon, 31 Aug 2020 12:46:42 +0200 Subject: [PATCH] Added use case for creating and resizing a tensor. --- .../Basics/TensorTest.cs | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/test/TensorFlowNET.UnitTest/Basics/TensorTest.cs b/test/TensorFlowNET.UnitTest/Basics/TensorTest.cs index 6d67d927..69e4dc51 100644 --- a/test/TensorFlowNET.UnitTest/Basics/TensorTest.cs +++ b/test/TensorFlowNET.UnitTest/Basics/TensorTest.cs @@ -273,5 +273,43 @@ namespace TensorFlowNET.UnitTest.NativeAPI Assert.IsTrue(Enumerable.SequenceEqual(new int[] { 0, 2 }, masked.ToArray())); } } + + /// + /// Creates a tensor from an image of 256x256x3 and resizes it to 100x100x3 + /// + [TestMethod] + public unsafe void tensor_resize() + { + var imageArray = new float[256 * 256 * 3]; + + using var newSize = tf.convert_to_tensor(new int[] { 100, 100 }); + + using (var t = new Tensor(imageArray, new long[] { 1, 256, 256, 3 }, tf.float32)) + { + Assert.IsFalse(t.IsDisposed); + Assert.AreEqual(256 * 256 * 3 * sizeof(float), (int)t.bytesize); + + using var resized = tf.image.resize_bilinear(t, newSize); + EXPECT_EQ((int)resized.shape[0], 1); + EXPECT_EQ((int)resized.shape[1], 100); + EXPECT_EQ((int)resized.shape[2], 100); + EXPECT_EQ((int)resized.shape[3], 3); + } + + fixed (float* ptr = &imageArray[0]) + { + using (var t = new Tensor((IntPtr)ptr, new long[] { imageArray.Length }, tf.float32, 4 * imageArray.Length)) + { + Assert.IsFalse(t.IsDisposed); + Assert.AreEqual(256 * 256 * 3 * sizeof(float), (int)t.bytesize); + + using var resized = tf.image.resize_bilinear(t, newSize); + EXPECT_EQ((int)resized.shape[0], 1); + EXPECT_EQ((int)resized.shape[1], 100); + EXPECT_EQ((int)resized.shape[2], 100); + EXPECT_EQ((int)resized.shape[3], 3); + } + } + } } } \ No newline at end of file