Browse Source

fix eager tensor resize. #608

tags/v0.20
Oceania2018 5 years ago
parent
commit
430927524f
4 changed files with 34 additions and 25 deletions
  1. +2
    -2
      src/TensorFlowNET.Core/APIs/tf.image.cs
  2. +12
    -2
      src/TensorFlowNET.Core/Operations/gen_image_ops.cs
  3. +12
    -2
      src/TensorFlowNET.Core/Tensors/constant_op.cs
  4. +8
    -19
      test/TensorFlowNET.UnitTest/Basics/TensorTest.cs

+ 2
- 2
src/TensorFlowNET.Core/APIs/tf.image.cs View File

@@ -203,8 +203,8 @@ namespace Tensorflow
=> image_ops_impl.non_max_suppression_padded(boxes, scores, max_output_size, iou_threshold, score_threshold, pad_to_max_output_size,
name, sorted_input, canonicalized_coordinates, tile_size);

public Tensor resize_bilinear(Tensor images, Tensor size, bool align_corners = false, string name = null)
=> gen_image_ops.resize_bilinear(images, size, align_corners: align_corners, name: name);
public Tensor resize_bilinear(Tensor images, Tensor size, bool align_corners = false, bool half_pixel_centers = false, string name = null)
=> gen_image_ops.resize_bilinear(images, size, align_corners: align_corners, half_pixel_centers: half_pixel_centers, name: name);

public Tensor resize_images(Tensor images, Tensor size, string method = ResizeMethod.BILINEAR,
bool preserve_aspect_ratio = false, string name = null)


+ 12
- 2
src/TensorFlowNET.Core/Operations/gen_image_ops.cs View File

@@ -155,11 +155,21 @@ namespace Tensorflow
}
}

public static Tensor resize_bilinear(Tensor images, Tensor size, bool align_corners = false, string name = null)
public static Tensor resize_bilinear(Tensor images,
Tensor size,
bool align_corners = false,
bool half_pixel_centers = false,
string name = null)
{
if (tf.Context.executing_eagerly())
{
throw new NotImplementedException("resize_bilinear");
var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
"ResizeBilinear", name,
null,
images, size,
"align_corners", align_corners,
"half_pixel_centers", half_pixel_centers);
return results[0];
}
else
{


+ 12
- 2
src/TensorFlowNET.Core/Tensors/constant_op.cs View File

@@ -64,8 +64,8 @@ namespace Tensorflow

var num_t = t.TensorShape.num_elements();
if (num_t == shape.num_elements())
throw new NotImplementedException("");
if(num_t == 1)
return _eager_reshape(t, shape, tf.Context);
if (num_t == 1)
{
if (t.dtype == dtypes.@bool)
throw new NotImplementedException("");
@@ -100,6 +100,16 @@ namespace Tensorflow
return op.outputs[0];
}

private static Tensor _eager_reshape(EagerTensor tensor, int[] shape, Context ctx)
{
var attr_t = tensor.dtype.as_datatype_enum();
var dims_t = convert_to_eager_tensor(shape, ctx, dtypes.int32);
var inputs_flat = new[] { tensor, dims_t };
var attrs = new object[] { "T", attr_t, "Tshape", TF_DataType.TF_INT32 };
var result = tf.Runner.Execute(ctx, "Reshape", 1, inputs_flat, attrs);
return result[0];
}

private static Tensor _eager_fill(int[] dims, EagerTensor value, Context ctx)
{
var attr_t = value.dtype.as_datatype_enum();


+ 8
- 19
test/TensorFlowNET.UnitTest/Basics/TensorTest.cs View File

@@ -280,36 +280,25 @@ namespace TensorFlowNET.UnitTest.NativeAPI
[TestMethod]
public unsafe void tensor_resize()
{
tf.enable_eager_execution();

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))
using (var t = tf.constant(imageArray, tf.float32, (1, 256, 256, 3)))
{
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);
EXPECT_EQ(resized.shape[0], 1);
EXPECT_EQ(resized.shape[1], 100);
EXPECT_EQ(resized.shape[2], 100);
EXPECT_EQ(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);
}
}
tf.compat.v1.disable_eager_execution();
}
}
}

Loading…
Cancel
Save