Skip to content

Commit a04a1b0

Browse files
LittleLittleCloudBigBigMiao
authored andcommitted
update tensorflow.net to 0.20.0 (dotnet#5404)
* upgrade to 3.1 * write inline data using invariantCulture * upodate tensorflow * update Microsoft.ML.Vision * fix test && comment * udpate tensorflow.net to 0.20.1 * update tf major version * downgrade tf runtime to 1.14.1 * Update Dependencies.props * Update Dependencies.props * update tffact to stop running test on linux with glibc < 2.3) * fix TensorFlowTransformInputShapeTest * use tf.v1 api * fix comment: * fix building error * fix test * fix nit * remove linq Co-authored-by: BigBigMiao <[email protected]>
1 parent 980128a commit a04a1b0

File tree

9 files changed

+96
-151
lines changed

9 files changed

+96
-151
lines changed

build/Dependencies.props

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
<SystemDrawingCommonPackageVersion>4.5.0</SystemDrawingCommonPackageVersion>
2323
<SystemIOFileSystemAccessControl>4.5.0</SystemIOFileSystemAccessControl>
2424
<SystemSecurityPrincipalWindows>4.5.0</SystemSecurityPrincipalWindows>
25-
<TensorFlowVersion>1.14.0</TensorFlowVersion>
26-
<TensorFlowMajorVersion>1</TensorFlowMajorVersion>
27-
<TensorflowDotNETVersion>0.11.8.1</TensorflowDotNETVersion>
25+
<TensorFlowVersion>2.3.1</TensorFlowVersion>
26+
<TensorFlowMajorVersion>2</TensorFlowMajorVersion>
27+
<TensorflowDotNETVersion>0.20.1</TensorflowDotNETVersion>
2828
</PropertyGroup>
2929

3030
<!-- Model Builder Dependencies -->

src/Microsoft.ML.TensorFlow/TensorTypeExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using Microsoft.ML.Internal.Utilities;
77
using Tensorflow;
8+
using Utils = Microsoft.ML.Internal.Utilities.Utils;
89

910
namespace Microsoft.ML.TensorFlow
1011
{

src/Microsoft.ML.TensorFlow/TensorflowTransform.cs

Lines changed: 13 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
using Tensorflow;
2121
using static Microsoft.ML.TensorFlow.TensorFlowUtils;
2222
using static Tensorflow.Binding;
23+
using Utils = Microsoft.ML.Internal.Utilities.Utils;
2324

2425
[assembly: LoadableClass(TensorFlowTransformer.Summary, typeof(IDataTransform), typeof(TensorFlowTransformer),
2526
typeof(TensorFlowEstimator.Options), typeof(SignatureDataTransform), TensorFlowTransformer.UserName, TensorFlowTransformer.ShortName)]
@@ -280,6 +281,7 @@ internal TensorFlowTransformer(IHostEnvironment env, Session session, string[] o
280281
_addBatchDimensionInput = addBatchDimensionInput;
281282
Inputs = inputColumnNames;
282283
Outputs = outputColumnNames;
284+
tf.compat.v1.disable_eager_execution();
283285

284286
(TFOutputTypes, OutputTypes, TFOutputOperations) = GetOutputInfo(Host, Session, Outputs);
285287
(TFInputTypes, TFInputShapes, TFInputOperations) = GetInputInfo(Host, Session, Inputs, batchSize);
@@ -344,15 +346,15 @@ internal static TensorShape GetTensorShape(TF_Output output, Graph graph, Status
344346
new ObjectDisposedException(nameof(graph));
345347

346348
var cstatus = status == null ? new Status() : status;
347-
var n = c_api.TF_GraphGetTensorNumDims(graph, output, cstatus);
349+
var n = c_api.TF_GraphGetTensorNumDims(graph, output, cstatus.Handle);
348350

349351
cstatus.Check();
350352

351353
if (n == -1)
352354
return new TensorShape(new int[0]);
353355

354356
var dims = new long[n];
355-
c_api.TF_GraphGetTensorShape(graph, output, dims, dims.Length, cstatus);
357+
c_api.TF_GraphGetTensorShape(graph, output, dims, dims.Length, cstatus.Handle);
356358
cstatus.Check();
357359
return new TensorShape(dims.Select(x => (int)x).ToArray());
358360
}
@@ -426,12 +428,14 @@ private protected override void SaveModel(ModelSaveContext ctx)
426428
ctx.Writer.WriteBoolByte(_addBatchDimensionInput);
427429
if (isFrozen)
428430
{
429-
Status status = new Status();
430-
var buffer = Session.graph.ToGraphDef(status);
431-
ctx.SaveBinaryStream("TFModel", w =>
431+
using (var status = new Status())
432+
using (var buffer = Session.graph.ToGraphDef(status))
432433
{
433-
w.WriteByteArray(buffer.MemoryBlock.ToArray());
434-
});
434+
ctx.SaveBinaryStream("TFModel", w =>
435+
{
436+
w.WriteByteArray(buffer.DangerousMemoryBlock.ToArray());
437+
});
438+
}
435439
}
436440

437441
Host.AssertNonEmpty(Inputs);
@@ -801,48 +805,10 @@ public Tensor GetTensor()
801805
// This is done to reduce memory allocation every time tensor is created.
802806
_denseData = new T[_vBuffer.Length];
803807
_vBuffer.CopyTo(_denseData);
804-
var tensor = CastDataAndReturnAsTensor(_denseData);
808+
var tensor = TensorFlowUtils.CastDataAndReturnAsTensor(_denseData, _tfShape);
805809
return tensor;
806810
}
807811

808-
private Tensor CastDataAndReturnAsTensor(T[] data)
809-
{
810-
if (typeof(T) == typeof(sbyte))
811-
return new Tensor((sbyte[])(object)data, _dims, TF_DataType.TF_INT8);
812-
else if (typeof(T) == typeof(long))
813-
return new Tensor((long[])(object)data, _dims, TF_DataType.TF_INT64);
814-
else if (typeof(T) == typeof(Int32))
815-
return new Tensor((Int32[])(object)data, _dims, TF_DataType.TF_INT32);
816-
else if (typeof(T) == typeof(Int16))
817-
return new Tensor((Int16[])(object)data, _dims, TF_DataType.TF_INT16);
818-
else if (typeof(T) == typeof(byte))
819-
return new Tensor((byte[])(object)data, _dims, TF_DataType.TF_UINT8);
820-
else if (typeof(T) == typeof(ulong))
821-
return new Tensor((ulong[])(object)data, _dims, TF_DataType.TF_UINT64);
822-
else if (typeof(T) == typeof(UInt32))
823-
return new Tensor((UInt32[])(object)data, _dims, TF_DataType.TF_UINT32);
824-
else if (typeof(T) == typeof(UInt16))
825-
return new Tensor((UInt16[])(object)data, _dims, TF_DataType.TF_UINT16);
826-
else if (typeof(T) == typeof(bool))
827-
return new Tensor((bool[])(object)data, _dims, TF_DataType.TF_BOOL);
828-
else if (typeof(T) == typeof(float))
829-
return new Tensor((float[])(object)data, _dims, TF_DataType.TF_FLOAT);
830-
else if (typeof(T) == typeof(double))
831-
return new Tensor((double[])(object)data, _dims, TF_DataType.TF_DOUBLE);
832-
else if (typeof(T) == typeof(ReadOnlyMemory<char>))
833-
{
834-
byte[][] bytes = new byte[_vBuffer.Length][];
835-
for (int i = 0; i < bytes.Length; i++)
836-
{
837-
bytes[i] = Encoding.UTF8.GetBytes(((ReadOnlyMemory<char>)(object)data[i]).ToArray());
838-
}
839-
840-
return new Tensor(bytes, _tfShape.dims.Select(x => (long)x).ToArray());
841-
}
842-
843-
return new Tensor(new NDArray(data, _tfShape));
844-
}
845-
846812
public void BufferTrainingData()
847813
{
848814
_srcgetter(ref _vBuffer);
@@ -853,7 +819,7 @@ public void BufferTrainingData()
853819
public Tensor GetBufferedBatchTensor()
854820
{
855821
_position = 0;
856-
var tensor = CastDataAndReturnAsTensor(_bufferedData);
822+
var tensor = TensorFlowUtils.CastDataAndReturnAsTensor(_denseData, _tfShape);
857823

858824
_bufferedData = new T[_bufferedDataSize];
859825
return tensor;

src/Microsoft.ML.TensorFlow/TensorflowUtils.cs

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
using Microsoft.ML.Runtime;
1313
using Microsoft.ML.TensorFlow;
1414
using Microsoft.ML.Transforms;
15+
using NumSharp;
1516
using Tensorflow;
1617
using static Tensorflow.Binding;
18+
using Utils = Microsoft.ML.Internal.Utilities.Utils;
1719

1820
namespace Microsoft.ML.TensorFlow
1921
{
@@ -410,6 +412,46 @@ internal static bool IsTypeSupported(TF_DataType tfoutput)
410412
}
411413
}
412414

415+
internal static Tensor CastDataAndReturnAsTensor<T>(T[] data, TensorShape tfShape)
416+
{
417+
var dims = tfShape.dims.Select(x => (long)x).ToArray();
418+
419+
if (typeof(T) == typeof(sbyte))
420+
return new Tensor((sbyte[])(object)data, dims, TF_DataType.TF_INT8);
421+
else if (typeof(T) == typeof(long))
422+
return new Tensor((long[])(object)data, dims, TF_DataType.TF_INT64);
423+
else if (typeof(T) == typeof(Int32))
424+
return new Tensor((Int32[])(object)data, dims, TF_DataType.TF_INT32);
425+
else if (typeof(T) == typeof(Int16))
426+
return new Tensor((Int16[])(object)data, dims, TF_DataType.TF_INT16);
427+
else if (typeof(T) == typeof(byte))
428+
return new Tensor((byte[])(object)data, dims, TF_DataType.TF_UINT8);
429+
else if (typeof(T) == typeof(ulong))
430+
return new Tensor((ulong[])(object)data, dims, TF_DataType.TF_UINT64);
431+
else if (typeof(T) == typeof(UInt32))
432+
return new Tensor((UInt32[])(object)data, dims, TF_DataType.TF_UINT32);
433+
else if (typeof(T) == typeof(UInt16))
434+
return new Tensor((UInt16[])(object)data, dims, TF_DataType.TF_UINT16);
435+
else if (typeof(T) == typeof(bool))
436+
return new Tensor((bool[])(object)data, dims, TF_DataType.TF_BOOL);
437+
else if (typeof(T) == typeof(float))
438+
return new Tensor((float[])(object)data, dims, TF_DataType.TF_FLOAT);
439+
else if (typeof(T) == typeof(double))
440+
return new Tensor((double[])(object)data, dims, TF_DataType.TF_DOUBLE);
441+
else if (typeof(T) == typeof(ReadOnlyMemory<char>))
442+
{
443+
string[] strings = new string[data.Length];
444+
for (int i = 0; i < strings.Length; i++)
445+
{
446+
strings[i] = data[i].ToString();
447+
}
448+
449+
return new Tensor(strings);
450+
}
451+
452+
return new Tensor(new NDArray(data, tfShape));
453+
}
454+
413455
/// <summary>
414456
/// Use the runner class to easily configure inputs, outputs and targets to be passed to the session runner.
415457
/// </summary>
@@ -491,7 +533,7 @@ public Tensor[] Run()
491533
{
492534
c_api.TF_SessionRun(_session, null, _inputs, _inputValues,
493535
_inputs.Length, _outputs, _outputValues, _outputValues.Length, _operations,
494-
_operations.Length, IntPtr.Zero, _status);
536+
_operations.Length, IntPtr.Zero, _status.Handle);
495537
}
496538
catch (Exception ex)
497539
{

src/Microsoft.ML.Vision/DnnRetrainTransform.cs

Lines changed: 6 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
using Tensorflow;
2020
using static Microsoft.ML.TensorFlow.TensorFlowUtils;
2121
using static Tensorflow.Binding;
22+
using Utils = Microsoft.ML.Internal.Utilities.Utils;
2223

2324
[assembly: LoadableClass(DnnRetrainTransformer.Summary, typeof(IDataTransform), typeof(DnnRetrainTransformer),
2425
typeof(DnnRetrainEstimator.Options), typeof(SignatureDataTransform), DnnRetrainTransformer.UserName, DnnRetrainTransformer.ShortName)]
@@ -607,15 +608,15 @@ internal static TensorShape GetTensorShape(TF_Output output, Graph graph, Status
607608
new ObjectDisposedException(nameof(graph));
608609

609610
var cstatus = status == null ? new Status() : status;
610-
var n = c_api.TF_GraphGetTensorNumDims(graph, output, cstatus);
611+
var n = c_api.TF_GraphGetTensorNumDims(graph, output, cstatus.Handle);
611612

612613
cstatus.Check();
613614

614615
if (n == -1)
615616
return new TensorShape(new int[0]);
616617

617618
var dims = new long[n];
618-
c_api.TF_GraphGetTensorShape(graph, output, dims, dims.Length, cstatus);
619+
c_api.TF_GraphGetTensorShape(graph, output, dims, dims.Length, cstatus.Handle);
619620
cstatus.Check();
620621
return new TensorShape(dims.Select(x => (int)x).ToArray());
621622
}
@@ -1040,49 +1041,11 @@ public Tensor GetBufferedBatchTensor()
10401041
}
10411042
else
10421043
{
1043-
var tensor = CastDataAndReturnAsTensor(_bufferedData);
1044+
var tensor = TensorFlowUtils.CastDataAndReturnAsTensor(_bufferedData, _tfShape);
10441045
_position = 0;
10451046
return tensor;
10461047
}
10471048
}
1048-
1049-
private Tensor CastDataAndReturnAsTensor(T[] data)
1050-
{
1051-
if (typeof(T) == typeof(sbyte))
1052-
return new Tensor((sbyte[])(object)data, _dims, TF_DataType.TF_INT8);
1053-
else if (typeof(T) == typeof(long))
1054-
return new Tensor((long[])(object)data, _dims, TF_DataType.TF_INT64);
1055-
else if (typeof(T) == typeof(Int32))
1056-
return new Tensor((Int32[])(object)data, _dims, TF_DataType.TF_INT32);
1057-
else if (typeof(T) == typeof(Int16))
1058-
return new Tensor((Int16[])(object)data, _dims, TF_DataType.TF_INT16);
1059-
else if (typeof(T) == typeof(byte))
1060-
return new Tensor((byte[])(object)data, _dims, TF_DataType.TF_UINT8);
1061-
else if (typeof(T) == typeof(ulong))
1062-
return new Tensor((ulong[])(object)data, _dims, TF_DataType.TF_UINT64);
1063-
else if (typeof(T) == typeof(UInt32))
1064-
return new Tensor((UInt32[])(object)data, _dims, TF_DataType.TF_UINT32);
1065-
else if (typeof(T) == typeof(UInt16))
1066-
return new Tensor((UInt16[])(object)data, _dims, TF_DataType.TF_UINT16);
1067-
else if (typeof(T) == typeof(bool))
1068-
return new Tensor((bool[])(object)data, _dims, TF_DataType.TF_BOOL);
1069-
else if (typeof(T) == typeof(float))
1070-
return new Tensor((float[])(object)data, _dims, TF_DataType.TF_FLOAT);
1071-
else if (typeof(T) == typeof(float))
1072-
return new Tensor((double[])(object)data, _dims, TF_DataType.TF_DOUBLE);
1073-
else if (typeof(T) == typeof(ReadOnlyMemory<char>))
1074-
{
1075-
byte[][] bytes = new byte[_bufferedData.Length][];
1076-
for (int i = 0; i < bytes.Length; i++)
1077-
{
1078-
bytes[i] = Encoding.UTF8.GetBytes(((ReadOnlyMemory<char>)(object)data[i]).ToArray());
1079-
}
1080-
1081-
return new Tensor(bytes, _tfShape.dims.Select(x => (long)x).ToArray());
1082-
}
1083-
1084-
return new Tensor(new NDArray(data, _tfShape));
1085-
}
10861049
}
10871050

10881051
private class TensorValueGetterVec<T> : ITensorValueGetter
@@ -1126,45 +1089,7 @@ public Tensor GetTensor()
11261089
// This is done to reduce memory allocation every time tensor is created.
11271090
_denseData = new T[_vBuffer.Length];
11281091
_vBuffer.CopyTo(_denseData);
1129-
return CastDataAndReturnAsTensor(_denseData);
1130-
}
1131-
1132-
private Tensor CastDataAndReturnAsTensor(T[] data)
1133-
{
1134-
if (typeof(T) == typeof(sbyte))
1135-
return new Tensor((sbyte[])(object)data, _dims, TF_DataType.TF_INT8);
1136-
else if (typeof(T) == typeof(long))
1137-
return new Tensor((long[])(object)data, _dims, TF_DataType.TF_INT64);
1138-
else if (typeof(T) == typeof(Int32))
1139-
return new Tensor((Int32[])(object)data, _dims, TF_DataType.TF_INT32);
1140-
else if (typeof(T) == typeof(Int16))
1141-
return new Tensor((Int16[])(object)data, _dims, TF_DataType.TF_INT16);
1142-
else if (typeof(T) == typeof(byte))
1143-
return new Tensor((byte[])(object)data, _dims, TF_DataType.TF_UINT8);
1144-
else if (typeof(T) == typeof(ulong))
1145-
return new Tensor((ulong[])(object)data, _dims, TF_DataType.TF_UINT64);
1146-
else if (typeof(T) == typeof(UInt32))
1147-
return new Tensor((UInt32[])(object)data, _dims, TF_DataType.TF_UINT32);
1148-
else if (typeof(T) == typeof(UInt16))
1149-
return new Tensor((UInt16[])(object)data, _dims, TF_DataType.TF_UINT16);
1150-
else if (typeof(T) == typeof(bool))
1151-
return new Tensor((bool[])(object)data, _dims, TF_DataType.TF_BOOL);
1152-
else if (typeof(T) == typeof(float))
1153-
return new Tensor((float[])(object)data, _dims, TF_DataType.TF_FLOAT);
1154-
else if (typeof(T) == typeof(double))
1155-
return new Tensor((double[])(object)data, _dims, TF_DataType.TF_DOUBLE);
1156-
else if (typeof(T) == typeof(ReadOnlyMemory<char>))
1157-
{
1158-
byte[][] bytes = new byte[_vBuffer.Length][];
1159-
for (int i = 0; i < bytes.Length; i++)
1160-
{
1161-
bytes[i] = Encoding.UTF8.GetBytes(((ReadOnlyMemory<char>)(object)data[i]).ToArray());
1162-
}
1163-
1164-
return new Tensor(bytes, _tfShape.dims.Select(x => (long)x).ToArray());
1165-
}
1166-
1167-
return new Tensor(new NDArray(data, _tfShape));
1092+
return TensorFlowUtils.CastDataAndReturnAsTensor(_denseData, _tfShape);
11681093
}
11691094

11701095
public void BufferTrainingData()
@@ -1177,7 +1102,7 @@ public void BufferTrainingData()
11771102
public Tensor GetBufferedBatchTensor()
11781103
{
11791104
_position = 0;
1180-
var tensor = CastDataAndReturnAsTensor(_bufferedData);
1105+
var tensor = TensorFlowUtils.CastDataAndReturnAsTensor(_bufferedData, _tfShape);
11811106
_bufferedData = new T[_bufferedDataSize];
11821107
return tensor;
11831108
}

0 commit comments

Comments
 (0)