Browse Source

auto cast by dtype when conveting to tensor.

tags/v0.60-tf.numpy
Oceania2018 4 years ago
parent
commit
01e6a38cf5
11 changed files with 49 additions and 52 deletions
  1. +1
    -1
      src/TensorFlowNET.Core/APIs/tf.image.cs
  2. +6
    -14
      src/TensorFlowNET.Core/Binding.Util.cs
  3. +0
    -1
      src/TensorFlowNET.Core/Numpy/NDArray.cs
  4. +2
    -2
      src/TensorFlowNET.Core/Numpy/Numpy.Creation.cs
  5. +1
    -1
      src/TensorFlowNET.Core/Numpy/Numpy.cs
  6. +3
    -1
      src/TensorFlowNET.Core/Operations/image_ops_impl.cs
  7. +4
    -1
      src/TensorFlowNET.Core/Tensors/tensor_util.cs
  8. +6
    -5
      src/TensorFlowNET.Core/ops.cs
  9. +8
    -8
      test/TensorFlowNET.UnitTest/Basics/RandomTest.cs
  10. +2
    -2
      test/TensorFlowNET.UnitTest/MultithreadingTests.cs
  11. +16
    -16
      test/TensorFlowNET.UnitTest/OperationsTest.cs

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

@@ -210,7 +210,7 @@ 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(Tensor image, TensorShape size, string method = ResizeMethod.BILINEAR)
public Tensor resize(Tensor image, Shape size, string method = ResizeMethod.BILINEAR)
=> image_ops_impl.resize_images_v2(image, size, method: method);

public Tensor resize(Tensor image, Tensor size, string method = ResizeMethod.BILINEAR)


+ 6
- 14
src/TensorFlowNET.Core/Binding.Util.cs View File

@@ -521,24 +521,16 @@ namespace Tensorflow
}
}

public static unsafe byte[] ToByteArray(Array array)
public static TF_DataType GetDataType(this object data)
{
/*var size = array.GetShape().size;
byte[]? bytes = null;
switch (array)
var type = data.GetType();
switch (data)
{
case float[] arr:
var len = new byte[size * sizeof(float)];
fixed (void* addr = &arr[0])
System.Buffer.MemoryCopy(addr, dst, bytesize, bytesize);
tensor_proto.TensorContent = Google.Protobuf.ByteString.CopyFrom(array.ToArray());
break;
case Shape shape:
return TF_DataType.TF_INT64;
default:
throw new NotImplementedException("");
return type.as_tf_dtype();
}

return bytes;*/
throw new NotImplementedException("");
}
}
}

+ 0
- 1
src/TensorFlowNET.Core/Numpy/NDArray.cs View File

@@ -41,7 +41,6 @@ namespace Tensorflow.NumPy
public NDArray reshape(Shape newshape) => new NDArray(_tensor, newshape);
public NDArray astype(Type type) => throw new NotImplementedException("");
public NDArray astype(TF_DataType type) => throw new NotImplementedException("");
public bool array_equal(NDArray rhs) => throw new NotImplementedException("");
public NDArray ravel() => throw new NotImplementedException("");
public void shuffle(NDArray nd) => throw new NotImplementedException("");
public Array ToMuliDimArray<T>() => throw new NotImplementedException("");


+ 2
- 2
src/TensorFlowNET.Core/Numpy/Numpy.Creation.cs View File

@@ -10,11 +10,11 @@ namespace Tensorflow.NumPy
public partial class np
{
public static NDArray array(Array data)
=> new NDArray(tf.constant(data));
=> new NDArray(data);

public static NDArray array<T>(params T[] data)
where T : unmanaged
=> new NDArray(tf.constant(data));
=> new NDArray(data);

public static NDArray arange<T>(T end)
where T : unmanaged


+ 1
- 1
src/TensorFlowNET.Core/Numpy/Numpy.cs View File

@@ -51,7 +51,7 @@ namespace Tensorflow.NumPy
public static double infinity => double.PositiveInfinity;

public static bool array_equal(NDArray a, NDArray b)
=> throw new NotImplementedException("");
=> a.Equals(b);

public static NDArray concatenate(NDArray[] arrays, int axis = 0)
=> throw new NotImplementedException("");


+ 3
- 1
src/TensorFlowNET.Core/Operations/image_ops_impl.cs View File

@@ -2229,7 +2229,9 @@ new_height, new_width");

throw new NotImplementedException("resize_images_v2");
};
return _resize_images_common(images, resize_fn, ops.convert_to_tensor(size),

var size_tensor = ops.convert_to_tensor(size, dtype: tf.int32);
return _resize_images_common(images, resize_fn, size_tensor,
preserve_aspect_ratio: preserve_aspect_ratio,
skip_resize_if_same: false,
name: name);


+ 4
- 1
src/TensorFlowNET.Core/Tensors/tensor_util.cs View File

@@ -181,7 +181,10 @@ namespace Tensorflow

if (tensor.GetType() == typeof(EagerTensor))
{
return new TensorShape(tensor.numpy().ToArray<int>());
if(tensor.dtype == TF_DataType.TF_INT64)
return new TensorShape(tensor.ToArray<long>());
else
return new TensorShape(tensor.ToArray<int>());
}

if (tensor.TensorShape.ndim == 0)


+ 6
- 5
src/TensorFlowNET.Core/ops.cs View File

@@ -20,7 +20,6 @@ using Tensorflow.NumPy;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
using Tensorflow.Contexts;
using Tensorflow.Eager;
@@ -126,12 +125,9 @@ namespace Tensorflow

if (value is EagerTensor eager_tensor)
{
if (dtype == TF_DataType.DtInvalid)
dtype = eager_tensor.dtype;

if (tf.executing_eagerly())
{
if (dtype != eager_tensor.dtype)
if (dtype != TF_DataType.DtInvalid && dtype != eager_tensor.dtype)
return gen_math_ops.cast(eager_tensor, dtype.as_base_dtype(), name: name);
return eager_tensor;
}
@@ -146,6 +142,7 @@ namespace Tensorflow
else if (value is NDArray nd)
return nd;

// graph mode
Tensor ret = value switch
{
NDArray nd => constant_op.constant(nd, dtype: dtype, name: name),
@@ -165,6 +162,10 @@ namespace Tensorflow
_ => constant_op.constant(value, dtype: dtype, name: name)
};

var original_dtype = value.GetDataType();
if (dtype != TF_DataType.DtInvalid && dtype != original_dtype)
ret = gen_math_ops.cast(ret, dtype.as_base_dtype(), name: name);

return ret;
}



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

@@ -30,8 +30,8 @@ namespace TensorFlowNET.UnitTest.Basics
tf.set_random_seed(1234);
var a2 = tf.random_uniform(1);
var b2 = tf.random_shuffle(tf.constant(initValue));
Assert.IsTrue(a1.numpy().array_equal(a2.numpy()));
Assert.IsTrue(b1.numpy().array_equal(b2.numpy()));
Assert.AreEqual(a1, a2);
Assert.AreEqual(b1, b2);
}
/// <summary>
@@ -53,8 +53,8 @@ namespace TensorFlowNET.UnitTest.Basics
tf.set_random_seed(1234);
var a2 = tf.random_uniform(1);
var b2 = tf.random_shuffle(tf.constant(initValue));
Assert.IsTrue(a1.numpy().array_equal(a2.numpy()));
Assert.IsTrue(b1.numpy().array_equal(b2.numpy()));
Assert.AreEqual(a1, a2);
Assert.AreEqual(b1, b2);
}
/// <summary>
@@ -76,8 +76,8 @@ namespace TensorFlowNET.UnitTest.Basics
var a2 = tf.random.normal(1);
var b2 = tf.random.truncated_normal(1);
Assert.IsTrue(a1.numpy().array_equal(a2.numpy()));
Assert.IsTrue(b1.numpy().array_equal(b2.numpy()));
Assert.AreEqual(a1, a2);
Assert.AreEqual(b1, b2);
}
/// <summary>
@@ -99,8 +99,8 @@ namespace TensorFlowNET.UnitTest.Basics
var a2 = tf.random.normal(1, seed:1234);
var b2 = tf.random.truncated_normal(1, seed:1234);
Assert.IsTrue(a1.numpy().array_equal(a2.numpy()));
Assert.IsTrue(b1.numpy().array_equal(b2.numpy()));
Assert.AreEqual(a1, a2);
Assert.AreEqual(b1, b2);
}
}
}

+ 2
- 2
test/TensorFlowNET.UnitTest/MultithreadingTests.cs View File

@@ -206,7 +206,7 @@ namespace TensorFlowNET.UnitTest
[TestMethod]
public void SessionRun_InsideSession()
{
MultiThreadedUnitTestExecuter.Run(8, Core);
MultiThreadedUnitTestExecuter.Run(1, Core);

//the core method
void Core(int tid)
@@ -220,7 +220,7 @@ namespace TensorFlowNET.UnitTest
var math = a1 + a2;

var result = sess.run(math);
result[0].GetAtIndex<float>(0).Should().Be(5);
result.GetAtIndex<float>(0).Should().Be(5);
}
}
}


+ 16
- 16
test/TensorFlowNET.UnitTest/OperationsTest.cs View File

@@ -74,7 +74,7 @@ namespace TensorFlowNET.UnitTest.Basics
using (var sess = tf.Session())
{
var o = sess.run(b);
Assert.IsTrue(o.array_equal(check));
Assert.IsTrue(np.array_equal(o, check));
}
}

@@ -88,7 +88,7 @@ namespace TensorFlowNET.UnitTest.Basics
using (var sess = tf.Session())
{
var o = sess.run(b);
Assert.IsTrue(o.array_equal(check));
Assert.IsTrue(np.array_equal(o, check));
}
}

@@ -102,7 +102,7 @@ namespace TensorFlowNET.UnitTest.Basics
using (var sess = tf.Session())
{
var o = sess.run(b);
Assert.IsTrue(o.array_equal(check));
Assert.IsTrue(np.array_equal(o, check));
}

b = tf.cumsum(a, exclusive: true);
@@ -111,7 +111,7 @@ namespace TensorFlowNET.UnitTest.Basics
using (var sess = tf.Session())
{
var o = sess.run(b);
Assert.IsTrue(o.array_equal(check));
Assert.IsTrue(np.array_equal(o, check));
}

b = tf.cumsum(a, reverse: true);
@@ -120,7 +120,7 @@ namespace TensorFlowNET.UnitTest.Basics
using (var sess = tf.Session())
{
var o = sess.run(b);
Assert.IsTrue(o.array_equal(check));
Assert.IsTrue(np.array_equal(o, check));
}

b = tf.cumsum(a, exclusive: true, reverse: true);
@@ -129,7 +129,7 @@ namespace TensorFlowNET.UnitTest.Basics
using (var sess = tf.Session())
{
var o = sess.run(b);
Assert.IsTrue(o.array_equal(check));
Assert.IsTrue(np.array_equal(o, check));
}
}

@@ -145,7 +145,7 @@ namespace TensorFlowNET.UnitTest.Basics
using (var sess = tf.Session())
{
var o = sess.run(d);
Assert.IsTrue(o.array_equal(check));
Assert.IsTrue(np.array_equal(o, check));
}

d = tf.cast(tf.logical_not(b), tf.int32);
@@ -154,7 +154,7 @@ namespace TensorFlowNET.UnitTest.Basics
using (var sess = tf.Session())
{
var o = sess.run(d);
Assert.IsTrue(o.array_equal(check));
Assert.IsTrue(np.array_equal(o, check));
}

d = tf.cast(tf.logical_or(b, c), tf.int32);
@@ -163,7 +163,7 @@ namespace TensorFlowNET.UnitTest.Basics
using (var sess = tf.Session())
{
var o = sess.run(d);
Assert.IsTrue(o.array_equal(check));
Assert.IsTrue(np.array_equal(o, check));
}

d = tf.cast(tf.logical_xor(b, c), tf.int32);
@@ -172,7 +172,7 @@ namespace TensorFlowNET.UnitTest.Basics
using (var sess = tf.Session())
{
var o = sess.run(d);
Assert.IsTrue(o.array_equal(check));
Assert.IsTrue(np.array_equal(o, check));
}
}

@@ -311,7 +311,7 @@ namespace TensorFlowNET.UnitTest.Basics
}

// Testing `operator +(Tensor x, double y)
c = tf.reduce_sum(tf.reduce_sum(a + secondFloatVal, 1));
c = tf.reduce_sum(tf.reduce_sum(a + secondDoubleVal, 1));
using (var sess = tf.Session())
{
var o = sess.run(c,
@@ -320,7 +320,7 @@ namespace TensorFlowNET.UnitTest.Basics
}

// Testing `operator +(double x, Tensor y)
c = tf.reduce_sum(tf.reduce_sum(secondFloatVal + a, 1));
c = tf.reduce_sum(tf.reduce_sum(secondDoubleVal + a, 1));
using (var sess = tf.Session())
{
var o = sess.run(c,
@@ -486,7 +486,7 @@ namespace TensorFlowNET.UnitTest.Basics
}

// Testing `operator -(Tensor x, double y)
c = tf.reduce_sum(tf.reduce_sum(a - secondFloatVal, 1));
c = tf.reduce_sum(tf.reduce_sum(a - secondDoubleVal, 1));
using (var sess = tf.Session())
{
var o = sess.run(c,
@@ -495,7 +495,7 @@ namespace TensorFlowNET.UnitTest.Basics
}

// Testing `operator -(double x, Tensor y)
c = tf.reduce_sum(tf.reduce_sum(secondFloatVal - a, 1));
c = tf.reduce_sum(tf.reduce_sum(secondDoubleVal - a, 1));
using (var sess = tf.Session())
{
var o = sess.run(c,
@@ -707,7 +707,7 @@ namespace TensorFlowNET.UnitTest.Basics
}

// Testing `operator *(Tensor x, double y)
c = tf.reduce_sum(tf.reduce_sum(a * secondFloatVal, 1));
c = tf.reduce_sum(tf.reduce_sum(a * secondDoubleVal, 1));
using (var sess = tf.Session())
{
var o = sess.run(c,
@@ -716,7 +716,7 @@ namespace TensorFlowNET.UnitTest.Basics
}

// Testing `operator *(double x, Tensor y)
c = tf.reduce_sum(tf.reduce_sum(firstFloatVal * b, 1));
c = tf.reduce_sum(tf.reduce_sum(firstDoubleVal * b, 1));
using (var sess = tf.Session())
{
var o = sess.run(c,


Loading…
Cancel
Save