Skip to content

Commit 01e6a38

Browse files
committed
auto cast by dtype when conveting to tensor.
1 parent 1142f38 commit 01e6a38

11 files changed

Lines changed: 49 additions & 52 deletions

File tree

‎src/TensorFlowNET.Core/APIs/tf.image.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public Tensor extract_glimpse(Tensor input, Tensor size, Tensor offsets, bool ce
210210
=> image_ops_impl.non_max_suppression_padded(boxes, scores, max_output_size, iou_threshold, score_threshold, pad_to_max_output_size,
211211
name, sorted_input, canonicalized_coordinates, tile_size);
212212

213-
public Tensor resize(Tensor image, TensorShape size, string method = ResizeMethod.BILINEAR)
213+
public Tensor resize(Tensor image, Shape size, string method = ResizeMethod.BILINEAR)
214214
=> image_ops_impl.resize_images_v2(image, size, method: method);
215215

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

‎src/TensorFlowNET.Core/Binding.Util.cs‎

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -521,24 +521,16 @@ public static Shape GetShape(this object data)
521521
}
522522
}
523523

524-
public static unsafe byte[] ToByteArray(Array array)
524+
public static TF_DataType GetDataType(this object data)
525525
{
526-
/*var size = array.GetShape().size;
527-
byte[]? bytes = null;
528-
switch (array)
526+
var type = data.GetType();
527+
switch (data)
529528
{
530-
case float[] arr:
531-
var len = new byte[size * sizeof(float)];
532-
fixed (void* addr = &arr[0])
533-
System.Buffer.MemoryCopy(addr, dst, bytesize, bytesize);
534-
tensor_proto.TensorContent = Google.Protobuf.ByteString.CopyFrom(array.ToArray());
535-
break;
529+
case Shape shape:
530+
return TF_DataType.TF_INT64;
536531
default:
537-
throw new NotImplementedException("");
532+
return type.as_tf_dtype();
538533
}
539-
540-
return bytes;*/
541-
throw new NotImplementedException("");
542534
}
543535
}
544536
}

‎src/TensorFlowNET.Core/Numpy/NDArray.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public NDIterator<T> AsIterator<T>(bool autoreset = false) where T : unmanaged
4141
public NDArray reshape(Shape newshape) => new NDArray(_tensor, newshape);
4242
public NDArray astype(Type type) => throw new NotImplementedException("");
4343
public NDArray astype(TF_DataType type) => throw new NotImplementedException("");
44-
public bool array_equal(NDArray rhs) => throw new NotImplementedException("");
4544
public NDArray ravel() => throw new NotImplementedException("");
4645
public void shuffle(NDArray nd) => throw new NotImplementedException("");
4746
public Array ToMuliDimArray<T>() => throw new NotImplementedException("");

‎src/TensorFlowNET.Core/Numpy/Numpy.Creation.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace Tensorflow.NumPy
1010
public partial class np
1111
{
1212
public static NDArray array(Array data)
13-
=> new NDArray(tf.constant(data));
13+
=> new NDArray(data);
1414

1515
public static NDArray array<T>(params T[] data)
1616
where T : unmanaged
17-
=> new NDArray(tf.constant(data));
17+
=> new NDArray(data);
1818

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

‎src/TensorFlowNET.Core/Numpy/Numpy.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public partial class np
5151
public static double infinity => double.PositiveInfinity;
5252

5353
public static bool array_equal(NDArray a, NDArray b)
54-
=> throw new NotImplementedException("");
54+
=> a.Equals(b);
5555

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

‎src/TensorFlowNET.Core/Operations/image_ops_impl.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2229,7 +2229,9 @@ public static Tensor resize_images_v2<T>(Tensor images, T size, string method =
22292229

22302230
throw new NotImplementedException("resize_images_v2");
22312231
};
2232-
return _resize_images_common(images, resize_fn, ops.convert_to_tensor(size),
2232+
2233+
var size_tensor = ops.convert_to_tensor(size, dtype: tf.int32);
2234+
return _resize_images_common(images, resize_fn, size_tensor,
22332235
preserve_aspect_ratio: preserve_aspect_ratio,
22342236
skip_resize_if_same: false,
22352237
name: name);

‎src/TensorFlowNET.Core/Tensors/tensor_util.cs‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,10 @@ bool hasattr(Graph property, string attr)
181181

182182
if (tensor.GetType() == typeof(EagerTensor))
183183
{
184-
return new TensorShape(tensor.numpy().ToArray<int>());
184+
if(tensor.dtype == TF_DataType.TF_INT64)
185+
return new TensorShape(tensor.ToArray<long>());
186+
else
187+
return new TensorShape(tensor.ToArray<int>());
185188
}
186189

187190
if (tensor.TensorShape.ndim == 0)

‎src/TensorFlowNET.Core/ops.cs‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ limitations under the License.
2020
using System;
2121
using System.Collections.Generic;
2222
using System.Linq;
23-
using System.Runtime.InteropServices;
2423
using System.Threading;
2524
using Tensorflow.Contexts;
2625
using Tensorflow.Eager;
@@ -126,12 +125,9 @@ public static Tensor convert_to_tensor(object value,
126125

127126
if (value is EagerTensor eager_tensor)
128127
{
129-
if (dtype == TF_DataType.DtInvalid)
130-
dtype = eager_tensor.dtype;
131-
132128
if (tf.executing_eagerly())
133129
{
134-
if (dtype != eager_tensor.dtype)
130+
if (dtype != TF_DataType.DtInvalid && dtype != eager_tensor.dtype)
135131
return gen_math_ops.cast(eager_tensor, dtype.as_base_dtype(), name: name);
136132
return eager_tensor;
137133
}
@@ -146,6 +142,7 @@ public static Tensor convert_to_tensor(object value,
146142
else if (value is NDArray nd)
147143
return nd;
148144

145+
// graph mode
149146
Tensor ret = value switch
150147
{
151148
NDArray nd => constant_op.constant(nd, dtype: dtype, name: name),
@@ -165,6 +162,10 @@ public static Tensor convert_to_tensor(object value,
165162
_ => constant_op.constant(value, dtype: dtype, name: name)
166163
};
167164

165+
var original_dtype = value.GetDataType();
166+
if (dtype != TF_DataType.DtInvalid && dtype != original_dtype)
167+
ret = gen_math_ops.cast(ret, dtype.as_base_dtype(), name: name);
168+
168169
return ret;
169170
}
170171

‎test/TensorFlowNET.UnitTest/Basics/RandomTest.cs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public void TFRandomSeedTest()
3030
tf.set_random_seed(1234);
3131
var a2 = tf.random_uniform(1);
3232
var b2 = tf.random_shuffle(tf.constant(initValue));
33-
Assert.IsTrue(a1.numpy().array_equal(a2.numpy()));
34-
Assert.IsTrue(b1.numpy().array_equal(b2.numpy()));
33+
Assert.AreEqual(a1, a2);
34+
Assert.AreEqual(b1, b2);
3535
}
3636

3737
/// <summary>
@@ -53,8 +53,8 @@ public void TFRandomSeedTest2()
5353
tf.set_random_seed(1234);
5454
var a2 = tf.random_uniform(1);
5555
var b2 = tf.random_shuffle(tf.constant(initValue));
56-
Assert.IsTrue(a1.numpy().array_equal(a2.numpy()));
57-
Assert.IsTrue(b1.numpy().array_equal(b2.numpy()));
56+
Assert.AreEqual(a1, a2);
57+
Assert.AreEqual(b1, b2);
5858
}
5959

6060
/// <summary>
@@ -76,8 +76,8 @@ public void TFRandomRaodomSeedTest()
7676
var a2 = tf.random.normal(1);
7777
var b2 = tf.random.truncated_normal(1);
7878

79-
Assert.IsTrue(a1.numpy().array_equal(a2.numpy()));
80-
Assert.IsTrue(b1.numpy().array_equal(b2.numpy()));
79+
Assert.AreEqual(a1, a2);
80+
Assert.AreEqual(b1, b2);
8181
}
8282

8383
/// <summary>
@@ -99,8 +99,8 @@ public void TFRandomRaodomSeedTest2()
9999
var a2 = tf.random.normal(1, seed:1234);
100100
var b2 = tf.random.truncated_normal(1, seed:1234);
101101

102-
Assert.IsTrue(a1.numpy().array_equal(a2.numpy()));
103-
Assert.IsTrue(b1.numpy().array_equal(b2.numpy()));
102+
Assert.AreEqual(a1, a2);
103+
Assert.AreEqual(b1, b2);
104104
}
105105
}
106106
}

‎test/TensorFlowNET.UnitTest/MultithreadingTests.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ void Core(int tid)
206206
[TestMethod]
207207
public void SessionRun_InsideSession()
208208
{
209-
MultiThreadedUnitTestExecuter.Run(8, Core);
209+
MultiThreadedUnitTestExecuter.Run(1, Core);
210210

211211
//the core method
212212
void Core(int tid)
@@ -220,7 +220,7 @@ void Core(int tid)
220220
var math = a1 + a2;
221221

222222
var result = sess.run(math);
223-
result[0].GetAtIndex<float>(0).Should().Be(5);
223+
result.GetAtIndex<float>(0).Should().Be(5);
224224
}
225225
}
226226
}

0 commit comments

Comments
 (0)